(f90-get-present-comment-type): Fix bug introduced in version 1.46.
[emacs.git] / src / xterm.c
blobc87d8981daf06147b8af30d44c61456a9842510d
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
23 /* Xt features made by Fred Pierresteguy. */
25 #include <config.h>
27 /* On 4.3 these lose if they come after xterm.h. */
28 /* Putting these at the beginning seems to be standard for other .c files. */
29 #include <signal.h>
31 #include <stdio.h>
33 #ifdef HAVE_X_WINDOWS
35 #include "lisp.h"
36 #include "blockinput.h"
38 /* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40 #include "syssignal.h"
42 /* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44 #include "xterm.h"
45 #include <X11/cursorfont.h>
47 /* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49 #ifndef makedev
50 #include <sys/types.h>
51 #endif /* makedev */
53 #ifdef BSD_SYSTEM
54 #include <sys/ioctl.h>
55 #endif /* ! defined (BSD_SYSTEM) */
57 #include "systty.h"
58 #include "systime.h"
60 #ifndef INCLUDED_FCNTL
61 #include <fcntl.h>
62 #endif
63 #include <ctype.h>
64 #include <errno.h>
65 #include <setjmp.h>
66 #include <sys/stat.h>
67 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68 /* #include <sys/param.h> */
70 #include "charset.h"
71 #include "coding.h"
72 #include "ccl.h"
73 #include "frame.h"
74 #include "dispextern.h"
75 #include "fontset.h"
76 #include "termhooks.h"
77 #include "termopts.h"
78 #include "termchar.h"
79 #include "gnu.h"
80 #include "disptab.h"
81 #include "buffer.h"
82 #include "window.h"
83 #include "keyboard.h"
84 #include "intervals.h"
85 #include "process.h"
86 #include "atimer.h"
87 #include "keymap.h"
89 #ifdef USE_X_TOOLKIT
90 #include <X11/Shell.h>
91 #endif
93 #ifdef HAVE_SYS_TIME_H
94 #include <sys/time.h>
95 #endif
96 #ifdef HAVE_UNISTD_H
97 #include <unistd.h>
98 #endif
100 #ifdef USE_LUCID
101 extern int xlwmenu_window_p (Widget w, Window window);
102 extern void xlwmenu_redisplay P_ ((Widget));
103 #endif
105 #ifdef USE_X_TOOLKIT
107 extern void free_frame_menubar P_ ((struct frame *));
108 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
109 int));
111 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
112 #define HACK_EDITRES
113 extern void _XEditResCheckMessages ();
114 #endif /* not NO_EDITRES */
116 /* Include toolkit specific headers for the scroll bar widget. */
118 #ifdef USE_TOOLKIT_SCROLL_BARS
119 #if defined USE_MOTIF
120 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
121 #include <Xm/ScrollBar.h>
122 #else /* !USE_MOTIF i.e. use Xaw */
124 #ifdef HAVE_XAW3D
125 #include <X11/Xaw3d/Simple.h>
126 #include <X11/Xaw3d/Scrollbar.h>
127 #define ARROW_SCROLLBAR
128 #include <X11/Xaw3d/ScrollbarP.h>
129 #else /* !HAVE_XAW3D */
130 #include <X11/Xaw/Simple.h>
131 #include <X11/Xaw/Scrollbar.h>
132 #endif /* !HAVE_XAW3D */
133 #ifndef XtNpickTop
134 #define XtNpickTop "pickTop"
135 #endif /* !XtNpickTop */
136 #endif /* !USE_MOTIF */
137 #endif /* USE_TOOLKIT_SCROLL_BARS */
139 #endif /* USE_X_TOOLKIT */
141 #ifndef USE_X_TOOLKIT
142 #define x_any_window_to_frame x_window_to_frame
143 #define x_top_window_to_frame x_window_to_frame
144 #endif
146 #ifdef USE_X_TOOLKIT
147 #include "widget.h"
148 #ifndef XtNinitialState
149 #define XtNinitialState "initialState"
150 #endif
151 #endif
153 #define abs(x) ((x) < 0 ? -(x) : (x))
155 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
158 /* Fringe bitmaps. */
160 enum fringe_bitmap_type
162 NO_FRINGE_BITMAP,
163 LEFT_TRUNCATION_BITMAP,
164 RIGHT_TRUNCATION_BITMAP,
165 OVERLAY_ARROW_BITMAP,
166 CONTINUED_LINE_BITMAP,
167 CONTINUATION_LINE_BITMAP,
168 ZV_LINE_BITMAP
171 /* Bitmap drawn to indicate lines not displaying text if
172 `indicate-empty-lines' is non-nil. */
174 #define zv_width 8
175 #define zv_height 72
176 #define zv_period 3
177 static unsigned char zv_bits[] = {
178 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
179 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
180 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
181 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
182 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
183 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
184 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
185 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
187 /* An arrow like this: `<-'. */
189 #define left_width 8
190 #define left_height 8
191 static unsigned char left_bits[] = {
192 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
194 /* Right truncation arrow bitmap `->'. */
196 #define right_width 8
197 #define right_height 8
198 static unsigned char right_bits[] = {
199 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
201 /* Marker for continued lines. */
203 #define continued_width 8
204 #define continued_height 8
205 static unsigned char continued_bits[] = {
206 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
208 /* Marker for continuation lines. */
210 #define continuation_width 8
211 #define continuation_height 8
212 static unsigned char continuation_bits[] = {
213 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
215 /* Overlay arrow bitmap. */
217 #if 0
218 /* A bomb. */
219 #define ov_width 8
220 #define ov_height 8
221 static unsigned char ov_bits[] = {
222 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
223 #else
224 /* A triangular arrow. */
225 #define ov_width 8
226 #define ov_height 8
227 static unsigned char ov_bits[] = {
228 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
230 #endif
232 extern Lisp_Object Qhelp_echo;
235 /* Non-nil means Emacs uses toolkit scroll bars. */
237 Lisp_Object Vx_toolkit_scroll_bars;
239 /* If a string, XTread_socket generates an event to display that string.
240 (The display is done in read_char.) */
242 static Lisp_Object help_echo;
243 static Lisp_Object help_echo_window;
244 static Lisp_Object help_echo_object;
245 static int help_echo_pos;
247 /* Temporary variable for XTread_socket. */
249 static Lisp_Object previous_help_echo;
251 /* Non-zero means that a HELP_EVENT has been generated since Emacs
252 start. */
254 static int any_help_event_p;
256 /* Non-zero means autoselect window with the mouse cursor. */
258 int mouse_autoselect_window;
260 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
261 static Lisp_Object last_window;
263 /* Non-zero means draw block and hollow cursor as wide as the glyph
264 under it. For example, if a block cursor is over a tab, it will be
265 drawn as wide as that tab on the display. */
267 int x_stretch_cursor_p;
269 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
271 int x_use_underline_position_properties;
273 /* This is a chain of structures for all the X displays currently in
274 use. */
276 struct x_display_info *x_display_list;
278 /* This is a list of cons cells, each of the form (NAME
279 . FONT-LIST-CACHE), one for each element of x_display_list and in
280 the same order. NAME is the name of the frame. FONT-LIST-CACHE
281 records previous values returned by x-list-fonts. */
283 Lisp_Object x_display_name_list;
285 /* Frame being updated by update_frame. This is declared in term.c.
286 This is set by update_begin and looked at by all the XT functions.
287 It is zero while not inside an update. In that case, the XT
288 functions assume that `selected_frame' is the frame to apply to. */
290 extern struct frame *updating_frame;
292 /* This is a frame waiting to be auto-raised, within XTread_socket. */
294 struct frame *pending_autoraise_frame;
296 #ifdef USE_X_TOOLKIT
297 /* The application context for Xt use. */
298 XtAppContext Xt_app_con;
299 static String Xt_default_resources[] = {0};
300 #endif /* USE_X_TOOLKIT */
302 /* Nominal cursor position -- where to draw output.
303 HPOS and VPOS are window relative glyph matrix coordinates.
304 X and Y are window relative pixel coordinates. */
306 struct cursor_pos output_cursor;
308 /* Non-zero means user is interacting with a toolkit scroll bar. */
310 static int toolkit_scroll_bar_interaction;
312 /* Mouse movement.
314 Formerly, we used PointerMotionHintMask (in standard_event_mask)
315 so that we would have to call XQueryPointer after each MotionNotify
316 event to ask for another such event. However, this made mouse tracking
317 slow, and there was a bug that made it eventually stop.
319 Simply asking for MotionNotify all the time seems to work better.
321 In order to avoid asking for motion events and then throwing most
322 of them away or busy-polling the server for mouse positions, we ask
323 the server for pointer motion hints. This means that we get only
324 one event per group of mouse movements. "Groups" are delimited by
325 other kinds of events (focus changes and button clicks, for
326 example), or by XQueryPointer calls; when one of these happens, we
327 get another MotionNotify event the next time the mouse moves. This
328 is at least as efficient as getting motion events when mouse
329 tracking is on, and I suspect only negligibly worse when tracking
330 is off. */
332 /* Where the mouse was last time we reported a mouse event. */
334 FRAME_PTR last_mouse_frame;
335 static XRectangle last_mouse_glyph;
336 static Lisp_Object last_mouse_press_frame;
338 /* The scroll bar in which the last X motion event occurred.
340 If the last X motion event occurred in a scroll bar, we set this so
341 XTmouse_position can know whether to report a scroll bar motion or
342 an ordinary motion.
344 If the last X motion event didn't occur in a scroll bar, we set
345 this to Qnil, to tell XTmouse_position to return an ordinary motion
346 event. */
348 static Lisp_Object last_mouse_scroll_bar;
350 /* This is a hack. We would really prefer that XTmouse_position would
351 return the time associated with the position it returns, but there
352 doesn't seem to be any way to wrest the time-stamp from the server
353 along with the position query. So, we just keep track of the time
354 of the last movement we received, and return that in hopes that
355 it's somewhat accurate. */
357 static Time last_mouse_movement_time;
359 /* Incremented by XTread_socket whenever it really tries to read
360 events. */
362 #ifdef __STDC__
363 static int volatile input_signal_count;
364 #else
365 static int input_signal_count;
366 #endif
368 /* Used locally within XTread_socket. */
370 static int x_noop_count;
372 /* Initial values of argv and argc. */
374 extern char **initial_argv;
375 extern int initial_argc;
377 extern Lisp_Object Vcommand_line_args, Vsystem_name;
379 /* Tells if a window manager is present or not. */
381 extern Lisp_Object Vx_no_window_manager;
383 extern Lisp_Object Qface, Qmouse_face;
385 extern int errno;
387 /* A mask of extra modifier bits to put into every keyboard char. */
389 extern EMACS_INT extra_keyboard_modifiers;
391 /* The keysyms to use for the various modifiers. */
393 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
394 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
396 static Lisp_Object Qvendor_specific_keysyms;
397 static Lisp_Object Qlatin_1, Qutf_8;
399 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
400 extern Lisp_Object x_icon_type P_ ((struct frame *));
403 /* Enumeration for overriding/changing the face to use for drawing
404 glyphs in x_draw_glyphs. */
406 enum draw_glyphs_face
408 DRAW_NORMAL_TEXT,
409 DRAW_INVERSE_VIDEO,
410 DRAW_CURSOR,
411 DRAW_MOUSE_FACE,
412 DRAW_IMAGE_RAISED,
413 DRAW_IMAGE_SUNKEN
416 static int cursor_in_mouse_face_p P_ ((struct window *));
417 static int clear_mouse_face P_ ((struct x_display_info *));
418 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
419 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
420 static const XColor *x_color_cells P_ ((Display *, int *));
421 static void x_update_window_end P_ ((struct window *, int, int));
422 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
423 void x_delete_display P_ ((struct x_display_info *));
424 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
425 unsigned));
426 static int fast_find_position P_ ((struct window *, int, int *, int *,
427 int *, int *, Lisp_Object));
428 static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
429 int *, int *, int *, int *, int));
430 static void set_output_cursor P_ ((struct cursor_pos *));
431 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
432 int *, int *, int *, int));
433 static void note_mode_line_or_margin_highlight P_ ((struct window *, int,
434 int, int));
435 static void note_mouse_highlight P_ ((struct frame *, int, int));
436 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
437 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
438 static void show_mouse_face P_ ((struct x_display_info *,
439 enum draw_glyphs_face));
440 static int x_io_error_quitter P_ ((Display *));
441 int x_catch_errors P_ ((Display *));
442 void x_uncatch_errors P_ ((Display *, int));
443 void x_lower_frame P_ ((struct frame *));
444 void x_scroll_bar_clear P_ ((struct frame *));
445 int x_had_errors_p P_ ((Display *));
446 void x_wm_set_size_hint P_ ((struct frame *, long, int));
447 void x_raise_frame P_ ((struct frame *));
448 void x_set_window_size P_ ((struct frame *, int, int, int));
449 void x_wm_set_window_state P_ ((struct frame *, int));
450 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
451 void x_initialize P_ ((void));
452 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
453 static int x_compute_min_glyph_bounds P_ ((struct frame *));
454 static void x_draw_phys_cursor_glyph P_ ((struct window *,
455 struct glyph_row *,
456 enum draw_glyphs_face));
457 static void x_update_end P_ ((struct frame *));
458 static void XTframe_up_to_date P_ ((struct frame *));
459 static void XTset_terminal_modes P_ ((void));
460 static void XTreset_terminal_modes P_ ((void));
461 static void XTcursor_to P_ ((int, int, int, int));
462 static void x_write_glyphs P_ ((struct glyph *, int));
463 static void x_clear_end_of_line P_ ((int));
464 static void x_clear_frame P_ ((void));
465 static void x_clear_cursor P_ ((struct window *));
466 static void frame_highlight P_ ((struct frame *));
467 static void frame_unhighlight P_ ((struct frame *));
468 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
469 static void XTframe_rehighlight P_ ((struct frame *));
470 static void x_frame_rehighlight P_ ((struct x_display_info *));
471 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
472 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
473 enum text_cursor_kinds));
474 static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
475 XRectangle *));
476 static void expose_frame P_ ((struct frame *, int, int, int, int));
477 static int expose_window_tree P_ ((struct window *, XRectangle *));
478 static int expose_window P_ ((struct window *, XRectangle *));
479 static void expose_area P_ ((struct window *, struct glyph_row *,
480 XRectangle *, enum glyph_row_area));
481 static int expose_line P_ ((struct window *, struct glyph_row *,
482 XRectangle *));
483 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
484 static void x_update_window_cursor P_ ((struct window *, int));
485 static void x_erase_phys_cursor P_ ((struct window *));
486 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
487 static void x_draw_fringe_bitmap P_ ((struct window *, struct glyph_row *,
488 enum fringe_bitmap_type, int left_p));
490 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
491 GC, int));
492 static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
493 static void x_draw_row_fringe_bitmaps P_ ((struct window *, struct glyph_row *));
494 static void notice_overwritten_cursor P_ ((struct window *, enum glyph_row_area,
495 int, int, int, int));
496 static void x_flush P_ ((struct frame *f));
497 static void x_update_begin P_ ((struct frame *));
498 static void x_update_window_begin P_ ((struct window *));
499 static void x_draw_vertical_border P_ ((struct window *));
500 static void x_after_update_window_line P_ ((struct glyph_row *));
501 static INLINE void take_vertical_position_into_account P_ ((struct it *));
502 static void x_produce_stretch_glyph P_ ((struct it *));
503 static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
504 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
505 enum scroll_bar_part *,
506 Lisp_Object *, Lisp_Object *,
507 unsigned long *));
508 static void x_check_fullscreen P_ ((struct frame *));
509 static void x_check_fullscreen_move P_ ((struct frame *));
511 /* Flush display of frame F, or of all frames if F is null. */
513 static void
514 x_flush (f)
515 struct frame *f;
517 BLOCK_INPUT;
518 if (f == NULL)
520 Lisp_Object rest, frame;
521 FOR_EACH_FRAME (rest, frame)
522 x_flush (XFRAME (frame));
524 else if (FRAME_X_P (f))
525 XFlush (FRAME_X_DISPLAY (f));
526 UNBLOCK_INPUT;
530 /* Remove calls to XFlush by defining XFlush to an empty replacement.
531 Calls to XFlush should be unnecessary because the X output buffer
532 is flushed automatically as needed by calls to XPending,
533 XNextEvent, or XWindowEvent according to the XFlush man page.
534 XTread_socket calls XPending. Removing XFlush improves
535 performance. */
537 #define XFlush(DISPLAY) (void) 0
540 /***********************************************************************
541 Debugging
542 ***********************************************************************/
544 #if 0
546 /* This is a function useful for recording debugging information about
547 the sequence of occurrences in this file. */
549 struct record
551 char *locus;
552 int type;
555 struct record event_record[100];
557 int event_record_index;
559 record_event (locus, type)
560 char *locus;
561 int type;
563 if (event_record_index == sizeof (event_record) / sizeof (struct record))
564 event_record_index = 0;
566 event_record[event_record_index].locus = locus;
567 event_record[event_record_index].type = type;
568 event_record_index++;
571 #endif /* 0 */
575 /* Return the struct x_display_info corresponding to DPY. */
577 struct x_display_info *
578 x_display_info_for_display (dpy)
579 Display *dpy;
581 struct x_display_info *dpyinfo;
583 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
584 if (dpyinfo->display == dpy)
585 return dpyinfo;
587 return 0;
592 /***********************************************************************
593 Starting and ending an update
594 ***********************************************************************/
596 /* Start an update of frame F. This function is installed as a hook
597 for update_begin, i.e. it is called when update_begin is called.
598 This function is called prior to calls to x_update_window_begin for
599 each window being updated. Currently, there is nothing to do here
600 because all interesting stuff is done on a window basis. */
602 static void
603 x_update_begin (f)
604 struct frame *f;
606 /* Nothing to do. */
610 /* Start update of window W. Set the global variable updated_window
611 to the window being updated and set output_cursor to the cursor
612 position of W. */
614 static void
615 x_update_window_begin (w)
616 struct window *w;
618 struct frame *f = XFRAME (WINDOW_FRAME (w));
619 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
621 updated_window = w;
622 set_output_cursor (&w->cursor);
624 BLOCK_INPUT;
626 if (f == display_info->mouse_face_mouse_frame)
628 /* Don't do highlighting for mouse motion during the update. */
629 display_info->mouse_face_defer = 1;
631 /* If F needs to be redrawn, simply forget about any prior mouse
632 highlighting. */
633 if (FRAME_GARBAGED_P (f))
634 display_info->mouse_face_window = Qnil;
636 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
637 their mouse_face_p flag set, which means that they are always
638 unequal to rows in a desired matrix which never have that
639 flag set. So, rows containing mouse-face glyphs are never
640 scrolled, and we don't have to switch the mouse highlight off
641 here to prevent it from being scrolled. */
643 /* Can we tell that this update does not affect the window
644 where the mouse highlight is? If so, no need to turn off.
645 Likewise, don't do anything if the frame is garbaged;
646 in that case, the frame's current matrix that we would use
647 is all wrong, and we will redisplay that line anyway. */
648 if (!NILP (display_info->mouse_face_window)
649 && w == XWINDOW (display_info->mouse_face_window))
651 int i;
653 for (i = 0; i < w->desired_matrix->nrows; ++i)
654 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
655 break;
657 if (i < w->desired_matrix->nrows)
658 clear_mouse_face (display_info);
660 #endif /* 0 */
663 UNBLOCK_INPUT;
667 /* Draw a vertical window border to the right of window W if W doesn't
668 have vertical scroll bars. */
670 static void
671 x_draw_vertical_border (w)
672 struct window *w;
674 struct frame *f = XFRAME (WINDOW_FRAME (w));
676 /* Redraw borders between horizontally adjacent windows. Don't
677 do it for frames with vertical scroll bars because either the
678 right scroll bar of a window, or the left scroll bar of its
679 neighbor will suffice as a border. */
680 if (!WINDOW_RIGHTMOST_P (w)
681 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
683 int x0, x1, y0, y1;
685 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
686 x1 += FRAME_X_RIGHT_FRINGE_WIDTH (f);
687 y1 -= 1;
689 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
690 f->output_data.x->normal_gc, x1, y0, x1, y1);
695 /* End update of window W (which is equal to updated_window).
697 Draw vertical borders between horizontally adjacent windows, and
698 display W's cursor if CURSOR_ON_P is non-zero.
700 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
701 glyphs in mouse-face were overwritten. In that case we have to
702 make sure that the mouse-highlight is properly redrawn.
704 W may be a menu bar pseudo-window in case we don't have X toolkit
705 support. Such windows don't have a cursor, so don't display it
706 here. */
708 static void
709 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
710 struct window *w;
711 int cursor_on_p, mouse_face_overwritten_p;
713 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
715 if (!w->pseudo_window_p)
717 BLOCK_INPUT;
719 if (cursor_on_p)
720 x_display_and_set_cursor (w, 1, output_cursor.hpos,
721 output_cursor.vpos,
722 output_cursor.x, output_cursor.y);
724 x_draw_vertical_border (w);
725 UNBLOCK_INPUT;
728 /* If a row with mouse-face was overwritten, arrange for
729 XTframe_up_to_date to redisplay the mouse highlight. */
730 if (mouse_face_overwritten_p)
732 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
733 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
734 dpyinfo->mouse_face_window = Qnil;
737 updated_window = NULL;
741 /* End update of frame F. This function is installed as a hook in
742 update_end. */
744 static void
745 x_update_end (f)
746 struct frame *f;
748 /* Mouse highlight may be displayed again. */
749 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
751 BLOCK_INPUT;
752 XFlush (FRAME_X_DISPLAY (f));
753 UNBLOCK_INPUT;
757 /* This function is called from various places in xdisp.c whenever a
758 complete update has been performed. The global variable
759 updated_window is not available here. */
761 static void
762 XTframe_up_to_date (f)
763 struct frame *f;
765 if (FRAME_X_P (f))
767 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
769 if (dpyinfo->mouse_face_deferred_gc
770 || f == dpyinfo->mouse_face_mouse_frame)
772 BLOCK_INPUT;
773 if (dpyinfo->mouse_face_mouse_frame)
774 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
775 dpyinfo->mouse_face_mouse_x,
776 dpyinfo->mouse_face_mouse_y);
777 dpyinfo->mouse_face_deferred_gc = 0;
778 UNBLOCK_INPUT;
784 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
785 arrow bitmaps, or clear the fringes if no bitmaps are required
786 before DESIRED_ROW is made current. The window being updated is
787 found in updated_window. This function It is called from
788 update_window_line only if it is known that there are differences
789 between bitmaps to be drawn between current row and DESIRED_ROW. */
791 static void
792 x_after_update_window_line (desired_row)
793 struct glyph_row *desired_row;
795 struct window *w = updated_window;
796 struct frame *f;
797 int width, height;
799 xassert (w);
801 if (!desired_row->mode_line_p && !w->pseudo_window_p)
803 BLOCK_INPUT;
804 x_draw_row_fringe_bitmaps (w, desired_row);
805 UNBLOCK_INPUT;
808 /* When a window has disappeared, make sure that no rest of
809 full-width rows stays visible in the internal border. Could
810 check here if updated_window is the leftmost/rightmost window,
811 but I guess it's not worth doing since vertically split windows
812 are almost never used, internal border is rarely set, and the
813 overhead is very small. */
814 if (windows_or_buffers_changed
815 && desired_row->full_width_p
816 && (f = XFRAME (w->frame),
817 width = FRAME_INTERNAL_BORDER_WIDTH (f),
818 width != 0)
819 && (height = desired_row->visible_height,
820 height > 0))
822 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
824 /* Internal border is drawn below the tool bar. */
825 if (WINDOWP (f->tool_bar_window)
826 && w == XWINDOW (f->tool_bar_window))
827 y -= width;
829 BLOCK_INPUT;
830 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
831 0, y, width, height, False);
832 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
833 f->output_data.x->pixel_width - width,
834 y, width, height, False);
835 UNBLOCK_INPUT;
840 /* Draw the bitmap WHICH in one of the left or right fringes of
841 window W. ROW is the glyph row for which to display the bitmap; it
842 determines the vertical position at which the bitmap has to be
843 drawn. */
845 static void
846 x_draw_fringe_bitmap (w, row, which, left_p)
847 struct window *w;
848 struct glyph_row *row;
849 enum fringe_bitmap_type which;
850 int left_p;
852 struct frame *f = XFRAME (WINDOW_FRAME (w));
853 Display *display = FRAME_X_DISPLAY (f);
854 Window window = FRAME_X_WINDOW (f);
855 int x, y, wd, h, dy;
856 int b1, b2;
857 unsigned char *bits = NULL;
858 Pixmap pixmap;
859 GC gc = f->output_data.x->normal_gc;
860 struct face *face;
861 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
863 /* Must clip because of partially visible lines. */
864 x_clip_to_row (w, row, gc, 1);
866 /* Convert row to frame coordinates. */
867 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
869 switch (which)
871 case NO_FRINGE_BITMAP:
872 wd = 0;
873 h = 0;
874 break;
876 case LEFT_TRUNCATION_BITMAP:
877 wd = left_width;
878 h = left_height;
879 bits = left_bits;
880 break;
882 case OVERLAY_ARROW_BITMAP:
883 wd = ov_width;
884 h = ov_height;
885 bits = ov_bits;
886 break;
888 case RIGHT_TRUNCATION_BITMAP:
889 wd = right_width;
890 h = right_height;
891 bits = right_bits;
892 break;
894 case CONTINUED_LINE_BITMAP:
895 wd = continued_width;
896 h = continued_height;
897 bits = continued_bits;
898 break;
900 case CONTINUATION_LINE_BITMAP:
901 wd = continuation_width;
902 h = continuation_height;
903 bits = continuation_bits;
904 break;
906 case ZV_LINE_BITMAP:
907 wd = zv_width;
908 h = zv_height - (y % zv_period);
909 bits = zv_bits + (y % zv_period);
910 break;
912 default:
913 abort ();
916 /* Clip bitmap if too high. */
917 if (h > row->height)
918 h = row->height;
920 /* Set dy to the offset in the row to start drawing the bitmap. */
921 dy = (row->height - h) / 2;
923 face = FACE_FROM_ID (f, FRINGE_FACE_ID);
924 PREPARE_FACE_FOR_DISPLAY (f, face);
926 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
927 the fringe. */
928 b1 = b2 = -1;
929 if (left_p)
931 if (wd > FRAME_X_LEFT_FRINGE_WIDTH (f))
932 wd = FRAME_X_LEFT_FRINGE_WIDTH (f);
933 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
934 - wd
935 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - wd) / 2);
936 if (wd < FRAME_X_LEFT_FRINGE_WIDTH (f) || row->height > h)
938 /* If W has a vertical border to its left, don't draw over it. */
939 int border = ((XFASTINT (w->left) > 0
940 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
941 ? 1 : 0);
942 b1 = (window_box_left (w, -1)
943 - FRAME_X_LEFT_FRINGE_WIDTH (f)
944 + border);
945 b2 = (FRAME_X_LEFT_FRINGE_WIDTH (f) - border);
948 else
950 if (wd > FRAME_X_RIGHT_FRINGE_WIDTH (f))
951 wd = FRAME_X_RIGHT_FRINGE_WIDTH (f);
952 x = (window_box_right (w, -1)
953 + (FRAME_X_RIGHT_FRINGE_WIDTH (f) - wd) / 2);
954 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
955 the fringe. */
956 if (wd < FRAME_X_RIGHT_FRINGE_WIDTH (f) || row->height > h)
958 b1 = window_box_right (w, -1);
959 b2 = FRAME_X_RIGHT_FRINGE_WIDTH (f);
963 if (b1 >= 0)
965 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
967 /* In case the same realized face is used for fringes and
968 for something displayed in the text (e.g. face `region' on
969 mono-displays, the fill style may have been changed to
970 FillSolid in x_draw_glyph_string_background. */
971 if (face->stipple)
972 XSetFillStyle (display, face->gc, FillOpaqueStippled);
973 else
974 XSetForeground (display, face->gc, face->background);
976 XFillRectangle (display, window, face->gc,
978 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
979 row->y)),
981 row->visible_height);
982 if (!face->stipple)
983 XSetForeground (display, face->gc, face->foreground);
986 if (which != NO_FRINGE_BITMAP)
988 /* Draw the bitmap. I believe these small pixmaps can be cached
989 by the server. */
990 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
991 face->foreground,
992 face->background, depth);
993 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
994 XFreePixmap (display, pixmap);
997 XSetClipMask (display, gc, None);
1001 /* Draw fringe bitmaps for glyph row ROW on window W. Call this
1002 function with input blocked. */
1004 static void
1005 x_draw_row_fringe_bitmaps (w, row)
1006 struct window *w;
1007 struct glyph_row *row;
1009 struct frame *f = XFRAME (w->frame);
1010 enum fringe_bitmap_type bitmap;
1012 xassert (interrupt_input_blocked);
1014 /* If row is completely invisible, because of vscrolling, we
1015 don't have to draw anything. */
1016 if (row->visible_height <= 0)
1017 return;
1019 if (FRAME_X_LEFT_FRINGE_WIDTH (f) != 0)
1021 /* Decide which bitmap to draw in the left fringe. */
1022 if (row->overlay_arrow_p)
1023 bitmap = OVERLAY_ARROW_BITMAP;
1024 else if (row->truncated_on_left_p)
1025 bitmap = LEFT_TRUNCATION_BITMAP;
1026 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
1027 bitmap = CONTINUATION_LINE_BITMAP;
1028 else if (row->indicate_empty_line_p)
1029 bitmap = ZV_LINE_BITMAP;
1030 else
1031 bitmap = NO_FRINGE_BITMAP;
1033 x_draw_fringe_bitmap (w, row, bitmap, 1);
1036 if (FRAME_X_RIGHT_FRINGE_WIDTH (f) != 0)
1038 /* Decide which bitmap to draw in the right fringe. */
1039 if (row->truncated_on_right_p)
1040 bitmap = RIGHT_TRUNCATION_BITMAP;
1041 else if (row->continued_p)
1042 bitmap = CONTINUED_LINE_BITMAP;
1043 else if (row->indicate_empty_line_p && FRAME_X_LEFT_FRINGE_WIDTH (f) == 0)
1044 bitmap = ZV_LINE_BITMAP;
1045 else
1046 bitmap = NO_FRINGE_BITMAP;
1048 x_draw_fringe_bitmap (w, row, bitmap, 0);
1054 /* This is called when starting Emacs and when restarting after
1055 suspend. When starting Emacs, no X window is mapped. And nothing
1056 must be done to Emacs's own window if it is suspended (though that
1057 rarely happens). */
1059 static void
1060 XTset_terminal_modes ()
1064 /* This is called when exiting or suspending Emacs. Exiting will make
1065 the X-windows go away, and suspending requires no action. */
1067 static void
1068 XTreset_terminal_modes ()
1074 /***********************************************************************
1075 Output Cursor
1076 ***********************************************************************/
1078 /* Set the global variable output_cursor to CURSOR. All cursor
1079 positions are relative to updated_window. */
1081 static void
1082 set_output_cursor (cursor)
1083 struct cursor_pos *cursor;
1085 output_cursor.hpos = cursor->hpos;
1086 output_cursor.vpos = cursor->vpos;
1087 output_cursor.x = cursor->x;
1088 output_cursor.y = cursor->y;
1092 /* Set a nominal cursor position.
1094 HPOS and VPOS are column/row positions in a window glyph matrix. X
1095 and Y are window text area relative pixel positions.
1097 If this is done during an update, updated_window will contain the
1098 window that is being updated and the position is the future output
1099 cursor position for that window. If updated_window is null, use
1100 selected_window and display the cursor at the given position. */
1102 static void
1103 XTcursor_to (vpos, hpos, y, x)
1104 int vpos, hpos, y, x;
1106 struct window *w;
1108 /* If updated_window is not set, work on selected_window. */
1109 if (updated_window)
1110 w = updated_window;
1111 else
1112 w = XWINDOW (selected_window);
1114 /* Set the output cursor. */
1115 output_cursor.hpos = hpos;
1116 output_cursor.vpos = vpos;
1117 output_cursor.x = x;
1118 output_cursor.y = y;
1120 /* If not called as part of an update, really display the cursor.
1121 This will also set the cursor position of W. */
1122 if (updated_window == NULL)
1124 BLOCK_INPUT;
1125 x_display_cursor (w, 1, hpos, vpos, x, y);
1126 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1127 UNBLOCK_INPUT;
1133 /***********************************************************************
1134 Display Iterator
1135 ***********************************************************************/
1137 /* Function prototypes of this page. */
1139 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1140 struct glyph *,
1141 XChar2b *,
1142 int *));
1143 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1144 int, XChar2b *, int,
1145 int));
1146 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1147 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1148 static void x_append_glyph P_ ((struct it *));
1149 static void x_append_composite_glyph P_ ((struct it *));
1150 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1151 int, int, double));
1152 static void x_produce_glyphs P_ ((struct it *));
1153 static void x_produce_image_glyph P_ ((struct it *it));
1156 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1157 is not contained in the font. */
1159 static INLINE XCharStruct *
1160 x_per_char_metric (font, char2b)
1161 XFontStruct *font;
1162 XChar2b *char2b;
1164 /* The result metric information. */
1165 XCharStruct *pcm = NULL;
1167 xassert (font && char2b);
1169 if (font->per_char != NULL)
1171 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1173 /* min_char_or_byte2 specifies the linear character index
1174 corresponding to the first element of the per_char array,
1175 max_char_or_byte2 is the index of the last character. A
1176 character with non-zero CHAR2B->byte1 is not in the font.
1177 A character with byte2 less than min_char_or_byte2 or
1178 greater max_char_or_byte2 is not in the font. */
1179 if (char2b->byte1 == 0
1180 && char2b->byte2 >= font->min_char_or_byte2
1181 && char2b->byte2 <= font->max_char_or_byte2)
1182 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1184 else
1186 /* If either min_byte1 or max_byte1 are nonzero, both
1187 min_char_or_byte2 and max_char_or_byte2 are less than
1188 256, and the 2-byte character index values corresponding
1189 to the per_char array element N (counting from 0) are:
1191 byte1 = N/D + min_byte1
1192 byte2 = N\D + min_char_or_byte2
1194 where:
1196 D = max_char_or_byte2 - min_char_or_byte2 + 1
1197 / = integer division
1198 \ = integer modulus */
1199 if (char2b->byte1 >= font->min_byte1
1200 && char2b->byte1 <= font->max_byte1
1201 && char2b->byte2 >= font->min_char_or_byte2
1202 && char2b->byte2 <= font->max_char_or_byte2)
1204 pcm = (font->per_char
1205 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1206 * (char2b->byte1 - font->min_byte1))
1207 + (char2b->byte2 - font->min_char_or_byte2));
1211 else
1213 /* If the per_char pointer is null, all glyphs between the first
1214 and last character indexes inclusive have the same
1215 information, as given by both min_bounds and max_bounds. */
1216 if (char2b->byte2 >= font->min_char_or_byte2
1217 && char2b->byte2 <= font->max_char_or_byte2)
1218 pcm = &font->max_bounds;
1221 return ((pcm == NULL
1222 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1223 ? NULL : pcm);
1227 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1228 the two-byte form of C. Encoding is returned in *CHAR2B. */
1230 static INLINE void
1231 x_encode_char (c, char2b, font_info)
1232 int c;
1233 XChar2b *char2b;
1234 struct font_info *font_info;
1236 int charset = CHAR_CHARSET (c);
1237 XFontStruct *font = font_info->font;
1239 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1240 This may be either a program in a special encoder language or a
1241 fixed encoding. */
1242 if (font_info->font_encoder)
1244 /* It's a program. */
1245 struct ccl_program *ccl = font_info->font_encoder;
1247 if (CHARSET_DIMENSION (charset) == 1)
1249 ccl->reg[0] = charset;
1250 ccl->reg[1] = char2b->byte2;
1252 else
1254 ccl->reg[0] = charset;
1255 ccl->reg[1] = char2b->byte1;
1256 ccl->reg[2] = char2b->byte2;
1259 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1261 /* We assume that MSBs are appropriately set/reset by CCL
1262 program. */
1263 if (font->max_byte1 == 0) /* 1-byte font */
1264 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1265 else
1266 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1268 else if (font_info->encoding[charset])
1270 /* Fixed encoding scheme. See fontset.h for the meaning of the
1271 encoding numbers. */
1272 int enc = font_info->encoding[charset];
1274 if ((enc == 1 || enc == 2)
1275 && CHARSET_DIMENSION (charset) == 2)
1276 char2b->byte1 |= 0x80;
1278 if (enc == 1 || enc == 3)
1279 char2b->byte2 |= 0x80;
1284 /* Get face and two-byte form of character C in face FACE_ID on frame
1285 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1286 means we want to display multibyte text. DISPLAY_P non-zero means
1287 make sure that X resources for the face returned are allocated.
1288 Value is a pointer to a realized face that is ready for display if
1289 DISPLAY_P is non-zero. */
1291 static INLINE struct face *
1292 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
1293 struct frame *f;
1294 int c, face_id;
1295 XChar2b *char2b;
1296 int multibyte_p, display_p;
1298 struct face *face = FACE_FROM_ID (f, face_id);
1300 if (!multibyte_p)
1302 /* Unibyte case. We don't have to encode, but we have to make
1303 sure to use a face suitable for unibyte. */
1304 char2b->byte1 = 0;
1305 char2b->byte2 = c;
1306 face_id = FACE_FOR_CHAR (f, face, c);
1307 face = FACE_FROM_ID (f, face_id);
1309 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1311 /* Case of ASCII in a face known to fit ASCII. */
1312 char2b->byte1 = 0;
1313 char2b->byte2 = c;
1315 else
1317 int c1, c2, charset;
1319 /* Split characters into bytes. If c2 is -1 afterwards, C is
1320 really a one-byte character so that byte1 is zero. */
1321 SPLIT_CHAR (c, charset, c1, c2);
1322 if (c2 > 0)
1323 char2b->byte1 = c1, char2b->byte2 = c2;
1324 else
1325 char2b->byte1 = 0, char2b->byte2 = c1;
1327 /* Maybe encode the character in *CHAR2B. */
1328 if (face->font != NULL)
1330 struct font_info *font_info
1331 = FONT_INFO_FROM_ID (f, face->font_info_id);
1332 if (font_info)
1333 x_encode_char (c, char2b, font_info);
1337 /* Make sure X resources of the face are allocated. */
1338 if (display_p)
1340 xassert (face != NULL);
1341 PREPARE_FACE_FOR_DISPLAY (f, face);
1344 return face;
1348 /* Get face and two-byte form of character glyph GLYPH on frame F.
1349 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1350 a pointer to a realized face that is ready for display. */
1352 static INLINE struct face *
1353 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1354 struct frame *f;
1355 struct glyph *glyph;
1356 XChar2b *char2b;
1357 int *two_byte_p;
1359 struct face *face;
1361 xassert (glyph->type == CHAR_GLYPH);
1362 face = FACE_FROM_ID (f, glyph->face_id);
1364 if (two_byte_p)
1365 *two_byte_p = 0;
1367 if (!glyph->multibyte_p)
1369 /* Unibyte case. We don't have to encode, but we have to make
1370 sure to use a face suitable for unibyte. */
1371 char2b->byte1 = 0;
1372 char2b->byte2 = glyph->u.ch;
1374 else if (glyph->u.ch < 128
1375 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1377 /* Case of ASCII in a face known to fit ASCII. */
1378 char2b->byte1 = 0;
1379 char2b->byte2 = glyph->u.ch;
1381 else
1383 int c1, c2, charset;
1385 /* Split characters into bytes. If c2 is -1 afterwards, C is
1386 really a one-byte character so that byte1 is zero. */
1387 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1388 if (c2 > 0)
1389 char2b->byte1 = c1, char2b->byte2 = c2;
1390 else
1391 char2b->byte1 = 0, char2b->byte2 = c1;
1393 /* Maybe encode the character in *CHAR2B. */
1394 if (charset != CHARSET_ASCII)
1396 struct font_info *font_info
1397 = FONT_INFO_FROM_ID (f, face->font_info_id);
1398 if (font_info)
1400 x_encode_char (glyph->u.ch, char2b, font_info);
1401 if (two_byte_p)
1402 *two_byte_p
1403 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1408 /* Make sure X resources of the face are allocated. */
1409 xassert (face != NULL);
1410 PREPARE_FACE_FOR_DISPLAY (f, face);
1411 return face;
1415 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1416 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1418 static INLINE void
1419 x_append_glyph (it)
1420 struct it *it;
1422 struct glyph *glyph;
1423 enum glyph_row_area area = it->area;
1425 xassert (it->glyph_row);
1426 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1428 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1429 if (glyph < it->glyph_row->glyphs[area + 1])
1431 glyph->charpos = CHARPOS (it->position);
1432 glyph->object = it->object;
1433 glyph->pixel_width = it->pixel_width;
1434 glyph->voffset = it->voffset;
1435 glyph->type = CHAR_GLYPH;
1436 glyph->multibyte_p = it->multibyte_p;
1437 glyph->left_box_line_p = it->start_of_box_run_p;
1438 glyph->right_box_line_p = it->end_of_box_run_p;
1439 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1440 || it->phys_descent > it->descent);
1441 glyph->padding_p = 0;
1442 glyph->glyph_not_available_p = it->glyph_not_available_p;
1443 glyph->face_id = it->face_id;
1444 glyph->u.ch = it->char_to_display;
1445 ++it->glyph_row->used[area];
1449 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1450 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1452 static INLINE void
1453 x_append_composite_glyph (it)
1454 struct it *it;
1456 struct glyph *glyph;
1457 enum glyph_row_area area = it->area;
1459 xassert (it->glyph_row);
1461 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1462 if (glyph < it->glyph_row->glyphs[area + 1])
1464 glyph->charpos = CHARPOS (it->position);
1465 glyph->object = it->object;
1466 glyph->pixel_width = it->pixel_width;
1467 glyph->voffset = it->voffset;
1468 glyph->type = COMPOSITE_GLYPH;
1469 glyph->multibyte_p = it->multibyte_p;
1470 glyph->left_box_line_p = it->start_of_box_run_p;
1471 glyph->right_box_line_p = it->end_of_box_run_p;
1472 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1473 || it->phys_descent > it->descent);
1474 glyph->padding_p = 0;
1475 glyph->glyph_not_available_p = 0;
1476 glyph->face_id = it->face_id;
1477 glyph->u.cmp_id = it->cmp_id;
1478 ++it->glyph_row->used[area];
1483 /* Change IT->ascent and IT->height according to the setting of
1484 IT->voffset. */
1486 static INLINE void
1487 take_vertical_position_into_account (it)
1488 struct it *it;
1490 if (it->voffset)
1492 if (it->voffset < 0)
1493 /* Increase the ascent so that we can display the text higher
1494 in the line. */
1495 it->ascent += abs (it->voffset);
1496 else
1497 /* Increase the descent so that we can display the text lower
1498 in the line. */
1499 it->descent += it->voffset;
1504 /* Produce glyphs/get display metrics for the image IT is loaded with.
1505 See the description of struct display_iterator in dispextern.h for
1506 an overview of struct display_iterator. */
1508 static void
1509 x_produce_image_glyph (it)
1510 struct it *it;
1512 struct image *img;
1513 struct face *face;
1515 xassert (it->what == IT_IMAGE);
1517 face = FACE_FROM_ID (it->f, it->face_id);
1518 img = IMAGE_FROM_ID (it->f, it->image_id);
1519 xassert (img);
1521 /* Make sure X resources of the face and image are loaded. */
1522 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1523 prepare_image_for_display (it->f, img);
1525 it->ascent = it->phys_ascent = image_ascent (img, face);
1526 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1527 it->pixel_width = img->width + 2 * img->hmargin;
1529 it->nglyphs = 1;
1531 if (face->box != FACE_NO_BOX)
1533 if (face->box_line_width > 0)
1535 it->ascent += face->box_line_width;
1536 it->descent += face->box_line_width;
1539 if (it->start_of_box_run_p)
1540 it->pixel_width += abs (face->box_line_width);
1541 if (it->end_of_box_run_p)
1542 it->pixel_width += abs (face->box_line_width);
1545 take_vertical_position_into_account (it);
1547 if (it->glyph_row)
1549 struct glyph *glyph;
1550 enum glyph_row_area area = it->area;
1552 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1553 if (glyph < it->glyph_row->glyphs[area + 1])
1555 glyph->charpos = CHARPOS (it->position);
1556 glyph->object = it->object;
1557 glyph->pixel_width = it->pixel_width;
1558 glyph->voffset = it->voffset;
1559 glyph->type = IMAGE_GLYPH;
1560 glyph->multibyte_p = it->multibyte_p;
1561 glyph->left_box_line_p = it->start_of_box_run_p;
1562 glyph->right_box_line_p = it->end_of_box_run_p;
1563 glyph->overlaps_vertically_p = 0;
1564 glyph->padding_p = 0;
1565 glyph->glyph_not_available_p = 0;
1566 glyph->face_id = it->face_id;
1567 glyph->u.img_id = img->id;
1568 ++it->glyph_row->used[area];
1574 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1575 of the glyph, WIDTH and HEIGHT are the width and height of the
1576 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1577 ascent of the glyph (0 <= ASCENT <= 1). */
1579 static void
1580 x_append_stretch_glyph (it, object, width, height, ascent)
1581 struct it *it;
1582 Lisp_Object object;
1583 int width, height;
1584 double ascent;
1586 struct glyph *glyph;
1587 enum glyph_row_area area = it->area;
1589 xassert (ascent >= 0 && ascent <= 1);
1591 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1592 if (glyph < it->glyph_row->glyphs[area + 1])
1594 glyph->charpos = CHARPOS (it->position);
1595 glyph->object = object;
1596 glyph->pixel_width = width;
1597 glyph->voffset = it->voffset;
1598 glyph->type = STRETCH_GLYPH;
1599 glyph->multibyte_p = it->multibyte_p;
1600 glyph->left_box_line_p = it->start_of_box_run_p;
1601 glyph->right_box_line_p = it->end_of_box_run_p;
1602 glyph->overlaps_vertically_p = 0;
1603 glyph->padding_p = 0;
1604 glyph->glyph_not_available_p = 0;
1605 glyph->face_id = it->face_id;
1606 glyph->u.stretch.ascent = height * ascent;
1607 glyph->u.stretch.height = height;
1608 ++it->glyph_row->used[area];
1613 /* Produce a stretch glyph for iterator IT. IT->object is the value
1614 of the glyph property displayed. The value must be a list
1615 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1616 being recognized:
1618 1. `:width WIDTH' specifies that the space should be WIDTH *
1619 canonical char width wide. WIDTH may be an integer or floating
1620 point number.
1622 2. `:relative-width FACTOR' specifies that the width of the stretch
1623 should be computed from the width of the first character having the
1624 `glyph' property, and should be FACTOR times that width.
1626 3. `:align-to HPOS' specifies that the space should be wide enough
1627 to reach HPOS, a value in canonical character units.
1629 Exactly one of the above pairs must be present.
1631 4. `:height HEIGHT' specifies that the height of the stretch produced
1632 should be HEIGHT, measured in canonical character units.
1634 5. `:relative-height FACTOR' specifies that the height of the
1635 stretch should be FACTOR times the height of the characters having
1636 the glyph property.
1638 Either none or exactly one of 4 or 5 must be present.
1640 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1641 of the stretch should be used for the ascent of the stretch.
1642 ASCENT must be in the range 0 <= ASCENT <= 100. */
1644 #define NUMVAL(X) \
1645 ((INTEGERP (X) || FLOATP (X)) \
1646 ? XFLOATINT (X) \
1647 : - 1)
1650 static void
1651 x_produce_stretch_glyph (it)
1652 struct it *it;
1654 /* (space :width WIDTH :height HEIGHT. */
1655 #if GLYPH_DEBUG
1656 extern Lisp_Object Qspace;
1657 #endif
1658 extern Lisp_Object QCwidth, QCheight, QCascent;
1659 extern Lisp_Object QCrelative_width, QCrelative_height;
1660 extern Lisp_Object QCalign_to;
1661 Lisp_Object prop, plist;
1662 double width = 0, height = 0, ascent = 0;
1663 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1664 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1666 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1668 /* List should start with `space'. */
1669 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1670 plist = XCDR (it->object);
1672 /* Compute the width of the stretch. */
1673 if (prop = Fplist_get (plist, QCwidth),
1674 NUMVAL (prop) > 0)
1675 /* Absolute width `:width WIDTH' specified and valid. */
1676 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1677 else if (prop = Fplist_get (plist, QCrelative_width),
1678 NUMVAL (prop) > 0)
1680 /* Relative width `:relative-width FACTOR' specified and valid.
1681 Compute the width of the characters having the `glyph'
1682 property. */
1683 struct it it2;
1684 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1686 it2 = *it;
1687 if (it->multibyte_p)
1689 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1690 - IT_BYTEPOS (*it));
1691 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1693 else
1694 it2.c = *p, it2.len = 1;
1696 it2.glyph_row = NULL;
1697 it2.what = IT_CHARACTER;
1698 x_produce_glyphs (&it2);
1699 width = NUMVAL (prop) * it2.pixel_width;
1701 else if (prop = Fplist_get (plist, QCalign_to),
1702 NUMVAL (prop) > 0)
1703 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1704 else
1705 /* Nothing specified -> width defaults to canonical char width. */
1706 width = CANON_X_UNIT (it->f);
1708 /* Compute height. */
1709 if (prop = Fplist_get (plist, QCheight),
1710 NUMVAL (prop) > 0)
1711 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1712 else if (prop = Fplist_get (plist, QCrelative_height),
1713 NUMVAL (prop) > 0)
1714 height = FONT_HEIGHT (font) * NUMVAL (prop);
1715 else
1716 height = FONT_HEIGHT (font);
1718 /* Compute percentage of height used for ascent. If
1719 `:ascent ASCENT' is present and valid, use that. Otherwise,
1720 derive the ascent from the font in use. */
1721 if (prop = Fplist_get (plist, QCascent),
1722 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1723 ascent = NUMVAL (prop) / 100.0;
1724 else
1725 ascent = (double) font->ascent / FONT_HEIGHT (font);
1727 if (width <= 0)
1728 width = 1;
1729 if (height <= 0)
1730 height = 1;
1732 if (it->glyph_row)
1734 Lisp_Object object = it->stack[it->sp - 1].string;
1735 if (!STRINGP (object))
1736 object = it->w->buffer;
1737 x_append_stretch_glyph (it, object, width, height, ascent);
1740 it->pixel_width = width;
1741 it->ascent = it->phys_ascent = height * ascent;
1742 it->descent = it->phys_descent = height - it->ascent;
1743 it->nglyphs = 1;
1745 if (face->box != FACE_NO_BOX)
1747 if (face->box_line_width > 0)
1749 it->ascent += face->box_line_width;
1750 it->descent += face->box_line_width;
1753 if (it->start_of_box_run_p)
1754 it->pixel_width += abs (face->box_line_width);
1755 if (it->end_of_box_run_p)
1756 it->pixel_width += abs (face->box_line_width);
1759 take_vertical_position_into_account (it);
1762 /* Return proper value to be used as baseline offset of font that has
1763 ASCENT and DESCENT to draw characters by the font at the vertical
1764 center of the line of frame F.
1766 Here, out task is to find the value of BOFF in the following figure;
1768 -------------------------+-----------+-
1769 -+-+---------+-+ | |
1770 | | | | | |
1771 | | | | F_ASCENT F_HEIGHT
1772 | | | ASCENT | |
1773 HEIGHT | | | | |
1774 | | |-|-+------+-----------|------- baseline
1775 | | | | BOFF | |
1776 | |---------|-+-+ | |
1777 | | | DESCENT | |
1778 -+-+---------+-+ F_DESCENT |
1779 -------------------------+-----------+-
1781 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1782 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1783 DESCENT = FONT->descent
1784 HEIGHT = FONT_HEIGHT (FONT)
1785 F_DESCENT = (F->output_data.x->font->descent
1786 - F->output_data.x->baseline_offset)
1787 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1790 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1791 ((FONT)->descent \
1792 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1793 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1794 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1796 /* Produce glyphs/get display metrics for the display element IT is
1797 loaded with. See the description of struct display_iterator in
1798 dispextern.h for an overview of struct display_iterator. */
1800 static void
1801 x_produce_glyphs (it)
1802 struct it *it;
1804 it->glyph_not_available_p = 0;
1806 if (it->what == IT_CHARACTER)
1808 XChar2b char2b;
1809 XFontStruct *font;
1810 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1811 XCharStruct *pcm;
1812 int font_not_found_p;
1813 struct font_info *font_info;
1814 int boff; /* baseline offset */
1815 /* We may change it->multibyte_p upon unibyte<->multibyte
1816 conversion. So, save the current value now and restore it
1817 later.
1819 Note: It seems that we don't have to record multibyte_p in
1820 struct glyph because the character code itself tells if or
1821 not the character is multibyte. Thus, in the future, we must
1822 consider eliminating the field `multibyte_p' in the struct
1823 glyph. */
1824 int saved_multibyte_p = it->multibyte_p;
1826 /* Maybe translate single-byte characters to multibyte, or the
1827 other way. */
1828 it->char_to_display = it->c;
1829 if (!ASCII_BYTE_P (it->c))
1831 if (unibyte_display_via_language_environment
1832 && SINGLE_BYTE_CHAR_P (it->c)
1833 && (it->c >= 0240
1834 || !NILP (Vnonascii_translation_table)))
1836 it->char_to_display = unibyte_char_to_multibyte (it->c);
1837 it->multibyte_p = 1;
1838 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1839 face = FACE_FROM_ID (it->f, it->face_id);
1841 else if (!SINGLE_BYTE_CHAR_P (it->c)
1842 && !it->multibyte_p)
1844 it->multibyte_p = 1;
1845 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1846 face = FACE_FROM_ID (it->f, it->face_id);
1850 /* Get font to use. Encode IT->char_to_display. */
1851 x_get_char_face_and_encoding (it->f, it->char_to_display,
1852 it->face_id, &char2b,
1853 it->multibyte_p, 0);
1854 font = face->font;
1856 /* When no suitable font found, use the default font. */
1857 font_not_found_p = font == NULL;
1858 if (font_not_found_p)
1860 font = FRAME_FONT (it->f);
1861 boff = it->f->output_data.x->baseline_offset;
1862 font_info = NULL;
1864 else
1866 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1867 boff = font_info->baseline_offset;
1868 if (font_info->vertical_centering)
1869 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1872 if (it->char_to_display >= ' '
1873 && (!it->multibyte_p || it->char_to_display < 128))
1875 /* Either unibyte or ASCII. */
1876 int stretched_p;
1878 it->nglyphs = 1;
1880 pcm = x_per_char_metric (font, &char2b);
1881 it->ascent = font->ascent + boff;
1882 it->descent = font->descent - boff;
1884 if (pcm)
1886 it->phys_ascent = pcm->ascent + boff;
1887 it->phys_descent = pcm->descent - boff;
1888 it->pixel_width = pcm->width;
1890 else
1892 it->glyph_not_available_p = 1;
1893 it->phys_ascent = font->ascent + boff;
1894 it->phys_descent = font->descent - boff;
1895 it->pixel_width = FONT_WIDTH (font);
1898 /* If this is a space inside a region of text with
1899 `space-width' property, change its width. */
1900 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1901 if (stretched_p)
1902 it->pixel_width *= XFLOATINT (it->space_width);
1904 /* If face has a box, add the box thickness to the character
1905 height. If character has a box line to the left and/or
1906 right, add the box line width to the character's width. */
1907 if (face->box != FACE_NO_BOX)
1909 int thick = face->box_line_width;
1911 if (thick > 0)
1913 it->ascent += thick;
1914 it->descent += thick;
1916 else
1917 thick = -thick;
1919 if (it->start_of_box_run_p)
1920 it->pixel_width += thick;
1921 if (it->end_of_box_run_p)
1922 it->pixel_width += thick;
1925 /* If face has an overline, add the height of the overline
1926 (1 pixel) and a 1 pixel margin to the character height. */
1927 if (face->overline_p)
1928 it->ascent += 2;
1930 take_vertical_position_into_account (it);
1932 /* If we have to actually produce glyphs, do it. */
1933 if (it->glyph_row)
1935 if (stretched_p)
1937 /* Translate a space with a `space-width' property
1938 into a stretch glyph. */
1939 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1940 x_append_stretch_glyph (it, it->object, it->pixel_width,
1941 it->ascent + it->descent, ascent);
1943 else
1944 x_append_glyph (it);
1946 /* If characters with lbearing or rbearing are displayed
1947 in this line, record that fact in a flag of the
1948 glyph row. This is used to optimize X output code. */
1949 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1950 it->glyph_row->contains_overlapping_glyphs_p = 1;
1953 else if (it->char_to_display == '\n')
1955 /* A newline has no width but we need the height of the line. */
1956 it->pixel_width = 0;
1957 it->nglyphs = 0;
1958 it->ascent = it->phys_ascent = font->ascent + boff;
1959 it->descent = it->phys_descent = font->descent - boff;
1961 if (face->box != FACE_NO_BOX
1962 && face->box_line_width > 0)
1964 it->ascent += face->box_line_width;
1965 it->descent += face->box_line_width;
1968 else if (it->char_to_display == '\t')
1970 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1971 int x = it->current_x + it->continuation_lines_width;
1972 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1974 /* If the distance from the current position to the next tab
1975 stop is less than a canonical character width, use the
1976 tab stop after that. */
1977 if (next_tab_x - x < CANON_X_UNIT (it->f))
1978 next_tab_x += tab_width;
1980 it->pixel_width = next_tab_x - x;
1981 it->nglyphs = 1;
1982 it->ascent = it->phys_ascent = font->ascent + boff;
1983 it->descent = it->phys_descent = font->descent - boff;
1985 if (it->glyph_row)
1987 double ascent = (double) it->ascent / (it->ascent + it->descent);
1988 x_append_stretch_glyph (it, it->object, it->pixel_width,
1989 it->ascent + it->descent, ascent);
1992 else
1994 /* A multi-byte character. Assume that the display width of the
1995 character is the width of the character multiplied by the
1996 width of the font. */
1998 /* If we found a font, this font should give us the right
1999 metrics. If we didn't find a font, use the frame's
2000 default font and calculate the width of the character
2001 from the charset width; this is what old redisplay code
2002 did. */
2003 pcm = x_per_char_metric (font, &char2b);
2004 if (font_not_found_p || !pcm)
2006 int charset = CHAR_CHARSET (it->char_to_display);
2008 it->glyph_not_available_p = 1;
2009 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
2010 * CHARSET_WIDTH (charset));
2011 it->phys_ascent = font->ascent + boff;
2012 it->phys_descent = font->descent - boff;
2014 else
2016 it->pixel_width = pcm->width;
2017 it->phys_ascent = pcm->ascent + boff;
2018 it->phys_descent = pcm->descent - boff;
2019 if (it->glyph_row
2020 && (pcm->lbearing < 0
2021 || pcm->rbearing > pcm->width))
2022 it->glyph_row->contains_overlapping_glyphs_p = 1;
2024 it->nglyphs = 1;
2025 it->ascent = font->ascent + boff;
2026 it->descent = font->descent - boff;
2027 if (face->box != FACE_NO_BOX)
2029 int thick = face->box_line_width;
2031 if (thick > 0)
2033 it->ascent += thick;
2034 it->descent += thick;
2036 else
2037 thick = - thick;
2039 if (it->start_of_box_run_p)
2040 it->pixel_width += thick;
2041 if (it->end_of_box_run_p)
2042 it->pixel_width += thick;
2045 /* If face has an overline, add the height of the overline
2046 (1 pixel) and a 1 pixel margin to the character height. */
2047 if (face->overline_p)
2048 it->ascent += 2;
2050 take_vertical_position_into_account (it);
2052 if (it->glyph_row)
2053 x_append_glyph (it);
2055 it->multibyte_p = saved_multibyte_p;
2057 else if (it->what == IT_COMPOSITION)
2059 /* Note: A composition is represented as one glyph in the
2060 glyph matrix. There are no padding glyphs. */
2061 XChar2b char2b;
2062 XFontStruct *font;
2063 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2064 XCharStruct *pcm;
2065 int font_not_found_p;
2066 struct font_info *font_info;
2067 int boff; /* baseline offset */
2068 struct composition *cmp = composition_table[it->cmp_id];
2070 /* Maybe translate single-byte characters to multibyte. */
2071 it->char_to_display = it->c;
2072 if (unibyte_display_via_language_environment
2073 && SINGLE_BYTE_CHAR_P (it->c)
2074 && (it->c >= 0240
2075 || (it->c >= 0200
2076 && !NILP (Vnonascii_translation_table))))
2078 it->char_to_display = unibyte_char_to_multibyte (it->c);
2081 /* Get face and font to use. Encode IT->char_to_display. */
2082 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2083 face = FACE_FROM_ID (it->f, it->face_id);
2084 x_get_char_face_and_encoding (it->f, it->char_to_display,
2085 it->face_id, &char2b, it->multibyte_p, 0);
2086 font = face->font;
2088 /* When no suitable font found, use the default font. */
2089 font_not_found_p = font == NULL;
2090 if (font_not_found_p)
2092 font = FRAME_FONT (it->f);
2093 boff = it->f->output_data.x->baseline_offset;
2094 font_info = NULL;
2096 else
2098 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2099 boff = font_info->baseline_offset;
2100 if (font_info->vertical_centering)
2101 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2104 /* There are no padding glyphs, so there is only one glyph to
2105 produce for the composition. Important is that pixel_width,
2106 ascent and descent are the values of what is drawn by
2107 draw_glyphs (i.e. the values of the overall glyphs composed). */
2108 it->nglyphs = 1;
2110 /* If we have not yet calculated pixel size data of glyphs of
2111 the composition for the current face font, calculate them
2112 now. Theoretically, we have to check all fonts for the
2113 glyphs, but that requires much time and memory space. So,
2114 here we check only the font of the first glyph. This leads
2115 to incorrect display very rarely, and C-l (recenter) can
2116 correct the display anyway. */
2117 if (cmp->font != (void *) font)
2119 /* Ascent and descent of the font of the first character of
2120 this composition (adjusted by baseline offset). Ascent
2121 and descent of overall glyphs should not be less than
2122 them respectively. */
2123 int font_ascent = font->ascent + boff;
2124 int font_descent = font->descent - boff;
2125 /* Bounding box of the overall glyphs. */
2126 int leftmost, rightmost, lowest, highest;
2127 int i, width, ascent, descent;
2129 cmp->font = (void *) font;
2131 /* Initialize the bounding box. */
2132 if (font_info
2133 && (pcm = x_per_char_metric (font, &char2b)))
2135 width = pcm->width;
2136 ascent = pcm->ascent;
2137 descent = pcm->descent;
2139 else
2141 width = FONT_WIDTH (font);
2142 ascent = font->ascent;
2143 descent = font->descent;
2146 rightmost = width;
2147 lowest = - descent + boff;
2148 highest = ascent + boff;
2149 leftmost = 0;
2151 if (font_info
2152 && font_info->default_ascent
2153 && CHAR_TABLE_P (Vuse_default_ascent)
2154 && !NILP (Faref (Vuse_default_ascent,
2155 make_number (it->char_to_display))))
2156 highest = font_info->default_ascent + boff;
2158 /* Draw the first glyph at the normal position. It may be
2159 shifted to right later if some other glyphs are drawn at
2160 the left. */
2161 cmp->offsets[0] = 0;
2162 cmp->offsets[1] = boff;
2164 /* Set cmp->offsets for the remaining glyphs. */
2165 for (i = 1; i < cmp->glyph_len; i++)
2167 int left, right, btm, top;
2168 int ch = COMPOSITION_GLYPH (cmp, i);
2169 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2171 face = FACE_FROM_ID (it->f, face_id);
2172 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2173 it->multibyte_p, 0);
2174 font = face->font;
2175 if (font == NULL)
2177 font = FRAME_FONT (it->f);
2178 boff = it->f->output_data.x->baseline_offset;
2179 font_info = NULL;
2181 else
2183 font_info
2184 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2185 boff = font_info->baseline_offset;
2186 if (font_info->vertical_centering)
2187 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2190 if (font_info
2191 && (pcm = x_per_char_metric (font, &char2b)))
2193 width = pcm->width;
2194 ascent = pcm->ascent;
2195 descent = pcm->descent;
2197 else
2199 width = FONT_WIDTH (font);
2200 ascent = 1;
2201 descent = 0;
2204 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2206 /* Relative composition with or without
2207 alternate chars. */
2208 left = (leftmost + rightmost - width) / 2;
2209 btm = - descent + boff;
2210 if (font_info && font_info->relative_compose
2211 && (! CHAR_TABLE_P (Vignore_relative_composition)
2212 || NILP (Faref (Vignore_relative_composition,
2213 make_number (ch)))))
2216 if (- descent >= font_info->relative_compose)
2217 /* One extra pixel between two glyphs. */
2218 btm = highest + 1;
2219 else if (ascent <= 0)
2220 /* One extra pixel between two glyphs. */
2221 btm = lowest - 1 - ascent - descent;
2224 else
2226 /* A composition rule is specified by an integer
2227 value that encodes global and new reference
2228 points (GREF and NREF). GREF and NREF are
2229 specified by numbers as below:
2231 0---1---2 -- ascent
2235 9--10--11 -- center
2237 ---3---4---5--- baseline
2239 6---7---8 -- descent
2241 int rule = COMPOSITION_RULE (cmp, i);
2242 int gref, nref, grefx, grefy, nrefx, nrefy;
2244 COMPOSITION_DECODE_RULE (rule, gref, nref);
2245 grefx = gref % 3, nrefx = nref % 3;
2246 grefy = gref / 3, nrefy = nref / 3;
2248 left = (leftmost
2249 + grefx * (rightmost - leftmost) / 2
2250 - nrefx * width / 2);
2251 btm = ((grefy == 0 ? highest
2252 : grefy == 1 ? 0
2253 : grefy == 2 ? lowest
2254 : (highest + lowest) / 2)
2255 - (nrefy == 0 ? ascent + descent
2256 : nrefy == 1 ? descent - boff
2257 : nrefy == 2 ? 0
2258 : (ascent + descent) / 2));
2261 cmp->offsets[i * 2] = left;
2262 cmp->offsets[i * 2 + 1] = btm + descent;
2264 /* Update the bounding box of the overall glyphs. */
2265 right = left + width;
2266 top = btm + descent + ascent;
2267 if (left < leftmost)
2268 leftmost = left;
2269 if (right > rightmost)
2270 rightmost = right;
2271 if (top > highest)
2272 highest = top;
2273 if (btm < lowest)
2274 lowest = btm;
2277 /* If there are glyphs whose x-offsets are negative,
2278 shift all glyphs to the right and make all x-offsets
2279 non-negative. */
2280 if (leftmost < 0)
2282 for (i = 0; i < cmp->glyph_len; i++)
2283 cmp->offsets[i * 2] -= leftmost;
2284 rightmost -= leftmost;
2287 cmp->pixel_width = rightmost;
2288 cmp->ascent = highest;
2289 cmp->descent = - lowest;
2290 if (cmp->ascent < font_ascent)
2291 cmp->ascent = font_ascent;
2292 if (cmp->descent < font_descent)
2293 cmp->descent = font_descent;
2296 it->pixel_width = cmp->pixel_width;
2297 it->ascent = it->phys_ascent = cmp->ascent;
2298 it->descent = it->phys_descent = cmp->descent;
2300 if (face->box != FACE_NO_BOX)
2302 int thick = face->box_line_width;
2304 if (thick > 0)
2306 it->ascent += thick;
2307 it->descent += thick;
2309 else
2310 thick = - thick;
2312 if (it->start_of_box_run_p)
2313 it->pixel_width += thick;
2314 if (it->end_of_box_run_p)
2315 it->pixel_width += thick;
2318 /* If face has an overline, add the height of the overline
2319 (1 pixel) and a 1 pixel margin to the character height. */
2320 if (face->overline_p)
2321 it->ascent += 2;
2323 take_vertical_position_into_account (it);
2325 if (it->glyph_row)
2326 x_append_composite_glyph (it);
2328 else if (it->what == IT_IMAGE)
2329 x_produce_image_glyph (it);
2330 else if (it->what == IT_STRETCH)
2331 x_produce_stretch_glyph (it);
2333 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2334 because this isn't true for images with `:ascent 100'. */
2335 xassert (it->ascent >= 0 && it->descent >= 0);
2336 if (it->area == TEXT_AREA)
2337 it->current_x += it->pixel_width;
2339 it->descent += it->extra_line_spacing;
2341 it->max_ascent = max (it->max_ascent, it->ascent);
2342 it->max_descent = max (it->max_descent, it->descent);
2343 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2344 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2348 /* Estimate the pixel height of the mode or top line on frame F.
2349 FACE_ID specifies what line's height to estimate. */
2352 x_estimate_mode_line_height (f, face_id)
2353 struct frame *f;
2354 enum face_id face_id;
2356 int height = FONT_HEIGHT (FRAME_FONT (f));
2358 /* This function is called so early when Emacs starts that the face
2359 cache and mode line face are not yet initialized. */
2360 if (FRAME_FACE_CACHE (f))
2362 struct face *face = FACE_FROM_ID (f, face_id);
2363 if (face)
2365 if (face->font)
2366 height = FONT_HEIGHT (face->font);
2367 if (face->box_line_width > 0)
2368 height += 2 * face->box_line_width;
2372 return height;
2376 /***********************************************************************
2377 Glyph display
2378 ***********************************************************************/
2380 /* A sequence of glyphs to be drawn in the same face.
2382 This data structure is not really completely X specific, so it
2383 could possibly, at least partially, be useful for other systems. It
2384 is currently not part of the external redisplay interface because
2385 it's not clear what other systems will need. */
2387 struct glyph_string
2389 /* X-origin of the string. */
2390 int x;
2392 /* Y-origin and y-position of the base line of this string. */
2393 int y, ybase;
2395 /* The width of the string, not including a face extension. */
2396 int width;
2398 /* The width of the string, including a face extension. */
2399 int background_width;
2401 /* The height of this string. This is the height of the line this
2402 string is drawn in, and can be different from the height of the
2403 font the string is drawn in. */
2404 int height;
2406 /* Number of pixels this string overwrites in front of its x-origin.
2407 This number is zero if the string has an lbearing >= 0; it is
2408 -lbearing, if the string has an lbearing < 0. */
2409 int left_overhang;
2411 /* Number of pixels this string overwrites past its right-most
2412 nominal x-position, i.e. x + width. Zero if the string's
2413 rbearing is <= its nominal width, rbearing - width otherwise. */
2414 int right_overhang;
2416 /* The frame on which the glyph string is drawn. */
2417 struct frame *f;
2419 /* The window on which the glyph string is drawn. */
2420 struct window *w;
2422 /* X display and window for convenience. */
2423 Display *display;
2424 Window window;
2426 /* The glyph row for which this string was built. It determines the
2427 y-origin and height of the string. */
2428 struct glyph_row *row;
2430 /* The area within row. */
2431 enum glyph_row_area area;
2433 /* Characters to be drawn, and number of characters. */
2434 XChar2b *char2b;
2435 int nchars;
2437 /* A face-override for drawing cursors, mouse face and similar. */
2438 enum draw_glyphs_face hl;
2440 /* Face in which this string is to be drawn. */
2441 struct face *face;
2443 /* Font in which this string is to be drawn. */
2444 XFontStruct *font;
2446 /* Font info for this string. */
2447 struct font_info *font_info;
2449 /* Non-null means this string describes (part of) a composition.
2450 All characters from char2b are drawn composed. */
2451 struct composition *cmp;
2453 /* Index of this glyph string's first character in the glyph
2454 definition of CMP. If this is zero, this glyph string describes
2455 the first character of a composition. */
2456 int gidx;
2458 /* 1 means this glyph strings face has to be drawn to the right end
2459 of the window's drawing area. */
2460 unsigned extends_to_end_of_line_p : 1;
2462 /* 1 means the background of this string has been drawn. */
2463 unsigned background_filled_p : 1;
2465 /* 1 means glyph string must be drawn with 16-bit functions. */
2466 unsigned two_byte_p : 1;
2468 /* 1 means that the original font determined for drawing this glyph
2469 string could not be loaded. The member `font' has been set to
2470 the frame's default font in this case. */
2471 unsigned font_not_found_p : 1;
2473 /* 1 means that the face in which this glyph string is drawn has a
2474 stipple pattern. */
2475 unsigned stippled_p : 1;
2477 /* 1 means only the foreground of this glyph string must be drawn,
2478 and we should use the physical height of the line this glyph
2479 string appears in as clip rect. */
2480 unsigned for_overlaps_p : 1;
2482 /* The GC to use for drawing this glyph string. */
2483 GC gc;
2485 /* A pointer to the first glyph in the string. This glyph
2486 corresponds to char2b[0]. Needed to draw rectangles if
2487 font_not_found_p is 1. */
2488 struct glyph *first_glyph;
2490 /* Image, if any. */
2491 struct image *img;
2493 struct glyph_string *next, *prev;
2497 #if GLYPH_DEBUG
2499 static void
2500 x_dump_glyph_string (s)
2501 struct glyph_string *s;
2503 fprintf (stderr, "glyph string\n");
2504 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2505 s->x, s->y, s->width, s->height);
2506 fprintf (stderr, " ybase = %d\n", s->ybase);
2507 fprintf (stderr, " hl = %d\n", s->hl);
2508 fprintf (stderr, " left overhang = %d, right = %d\n",
2509 s->left_overhang, s->right_overhang);
2510 fprintf (stderr, " nchars = %d\n", s->nchars);
2511 fprintf (stderr, " extends to end of line = %d\n",
2512 s->extends_to_end_of_line_p);
2513 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2514 fprintf (stderr, " bg width = %d\n", s->background_width);
2517 #endif /* GLYPH_DEBUG */
2521 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2522 struct glyph_string **,
2523 struct glyph_string *,
2524 struct glyph_string *));
2525 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2526 struct glyph_string **,
2527 struct glyph_string *,
2528 struct glyph_string *));
2529 static void x_append_glyph_string P_ ((struct glyph_string **,
2530 struct glyph_string **,
2531 struct glyph_string *));
2532 static int x_left_overwritten P_ ((struct glyph_string *));
2533 static int x_left_overwriting P_ ((struct glyph_string *));
2534 static int x_right_overwritten P_ ((struct glyph_string *));
2535 static int x_right_overwriting P_ ((struct glyph_string *));
2536 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2537 int));
2538 static void x_init_glyph_string P_ ((struct glyph_string *,
2539 XChar2b *, struct window *,
2540 struct glyph_row *,
2541 enum glyph_row_area, int,
2542 enum draw_glyphs_face));
2543 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2544 enum glyph_row_area, int, int,
2545 enum draw_glyphs_face, int));
2546 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2547 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2548 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2549 int));
2550 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2551 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2552 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2553 static void x_draw_glyph_string P_ ((struct glyph_string *));
2554 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2555 static void x_set_cursor_gc P_ ((struct glyph_string *));
2556 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2557 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2558 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2559 int *, int *));
2560 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2561 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2562 unsigned long *, double, int));
2563 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2564 double, int, unsigned long));
2565 static void x_setup_relief_colors P_ ((struct glyph_string *));
2566 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2567 static void x_draw_image_relief P_ ((struct glyph_string *));
2568 static void x_draw_image_foreground P_ ((struct glyph_string *));
2569 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2570 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2571 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2572 int, int, int));
2573 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2574 int, int, int, int, XRectangle *));
2575 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2576 int, int, int, XRectangle *));
2577 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2578 enum glyph_row_area));
2579 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2580 struct glyph_row *,
2581 enum glyph_row_area, int, int));
2583 #if GLYPH_DEBUG
2584 static void x_check_font P_ ((struct frame *, XFontStruct *));
2585 #endif
2588 /* Append the list of glyph strings with head H and tail T to the list
2589 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2591 static INLINE void
2592 x_append_glyph_string_lists (head, tail, h, t)
2593 struct glyph_string **head, **tail;
2594 struct glyph_string *h, *t;
2596 if (h)
2598 if (*head)
2599 (*tail)->next = h;
2600 else
2601 *head = h;
2602 h->prev = *tail;
2603 *tail = t;
2608 /* Prepend the list of glyph strings with head H and tail T to the
2609 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2610 result. */
2612 static INLINE void
2613 x_prepend_glyph_string_lists (head, tail, h, t)
2614 struct glyph_string **head, **tail;
2615 struct glyph_string *h, *t;
2617 if (h)
2619 if (*head)
2620 (*head)->prev = t;
2621 else
2622 *tail = t;
2623 t->next = *head;
2624 *head = h;
2629 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2630 Set *HEAD and *TAIL to the resulting list. */
2632 static INLINE void
2633 x_append_glyph_string (head, tail, s)
2634 struct glyph_string **head, **tail;
2635 struct glyph_string *s;
2637 s->next = s->prev = NULL;
2638 x_append_glyph_string_lists (head, tail, s, s);
2642 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2643 face. */
2645 static void
2646 x_set_cursor_gc (s)
2647 struct glyph_string *s;
2649 if (s->font == FRAME_FONT (s->f)
2650 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2651 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2652 && !s->cmp)
2653 s->gc = s->f->output_data.x->cursor_gc;
2654 else
2656 /* Cursor on non-default face: must merge. */
2657 XGCValues xgcv;
2658 unsigned long mask;
2660 xgcv.background = s->f->output_data.x->cursor_pixel;
2661 xgcv.foreground = s->face->background;
2663 /* If the glyph would be invisible, try a different foreground. */
2664 if (xgcv.foreground == xgcv.background)
2665 xgcv.foreground = s->face->foreground;
2666 if (xgcv.foreground == xgcv.background)
2667 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2668 if (xgcv.foreground == xgcv.background)
2669 xgcv.foreground = s->face->foreground;
2671 /* Make sure the cursor is distinct from text in this face. */
2672 if (xgcv.background == s->face->background
2673 && xgcv.foreground == s->face->foreground)
2675 xgcv.background = s->face->foreground;
2676 xgcv.foreground = s->face->background;
2679 IF_DEBUG (x_check_font (s->f, s->font));
2680 xgcv.font = s->font->fid;
2681 xgcv.graphics_exposures = False;
2682 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2684 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2685 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2686 mask, &xgcv);
2687 else
2688 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2689 = XCreateGC (s->display, s->window, mask, &xgcv);
2691 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2696 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2698 static void
2699 x_set_mouse_face_gc (s)
2700 struct glyph_string *s;
2702 int face_id;
2703 struct face *face;
2705 /* What face has to be used last for the mouse face? */
2706 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2707 face = FACE_FROM_ID (s->f, face_id);
2708 if (face == NULL)
2709 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2711 if (s->first_glyph->type == CHAR_GLYPH)
2712 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2713 else
2714 face_id = FACE_FOR_CHAR (s->f, face, 0);
2715 s->face = FACE_FROM_ID (s->f, face_id);
2716 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2718 /* If font in this face is same as S->font, use it. */
2719 if (s->font == s->face->font)
2720 s->gc = s->face->gc;
2721 else
2723 /* Otherwise construct scratch_cursor_gc with values from FACE
2724 but font FONT. */
2725 XGCValues xgcv;
2726 unsigned long mask;
2728 xgcv.background = s->face->background;
2729 xgcv.foreground = s->face->foreground;
2730 IF_DEBUG (x_check_font (s->f, s->font));
2731 xgcv.font = s->font->fid;
2732 xgcv.graphics_exposures = False;
2733 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2735 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2736 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2737 mask, &xgcv);
2738 else
2739 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2740 = XCreateGC (s->display, s->window, mask, &xgcv);
2742 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2745 xassert (s->gc != 0);
2749 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2750 Faces to use in the mode line have already been computed when the
2751 matrix was built, so there isn't much to do, here. */
2753 static INLINE void
2754 x_set_mode_line_face_gc (s)
2755 struct glyph_string *s;
2757 s->gc = s->face->gc;
2761 /* Set S->gc of glyph string S for drawing that glyph string. Set
2762 S->stippled_p to a non-zero value if the face of S has a stipple
2763 pattern. */
2765 static INLINE void
2766 x_set_glyph_string_gc (s)
2767 struct glyph_string *s;
2769 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2771 if (s->hl == DRAW_NORMAL_TEXT)
2773 s->gc = s->face->gc;
2774 s->stippled_p = s->face->stipple != 0;
2776 else if (s->hl == DRAW_INVERSE_VIDEO)
2778 x_set_mode_line_face_gc (s);
2779 s->stippled_p = s->face->stipple != 0;
2781 else if (s->hl == DRAW_CURSOR)
2783 x_set_cursor_gc (s);
2784 s->stippled_p = 0;
2786 else if (s->hl == DRAW_MOUSE_FACE)
2788 x_set_mouse_face_gc (s);
2789 s->stippled_p = s->face->stipple != 0;
2791 else if (s->hl == DRAW_IMAGE_RAISED
2792 || s->hl == DRAW_IMAGE_SUNKEN)
2794 s->gc = s->face->gc;
2795 s->stippled_p = s->face->stipple != 0;
2797 else
2799 s->gc = s->face->gc;
2800 s->stippled_p = s->face->stipple != 0;
2803 /* GC must have been set. */
2804 xassert (s->gc != 0);
2808 /* Return in *R the clipping rectangle for glyph string S. */
2810 static void
2811 x_get_glyph_string_clip_rect (s, r)
2812 struct glyph_string *s;
2813 XRectangle *r;
2815 if (s->row->full_width_p)
2817 /* Draw full-width. X coordinates are relative to S->w->left. */
2818 int canon_x = CANON_X_UNIT (s->f);
2820 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2821 r->width = XFASTINT (s->w->width) * canon_x;
2823 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2825 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2826 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2827 r->x -= width;
2830 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2832 /* Unless displaying a mode or menu bar line, which are always
2833 fully visible, clip to the visible part of the row. */
2834 if (s->w->pseudo_window_p)
2835 r->height = s->row->visible_height;
2836 else
2837 r->height = s->height;
2839 else
2841 /* This is a text line that may be partially visible. */
2842 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2843 r->width = window_box_width (s->w, s->area);
2844 r->height = s->row->visible_height;
2847 /* If S draws overlapping rows, it's sufficient to use the top and
2848 bottom of the window for clipping because this glyph string
2849 intentionally draws over other lines. */
2850 if (s->for_overlaps_p)
2852 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2853 r->height = window_text_bottom_y (s->w) - r->y;
2855 else
2857 /* Don't use S->y for clipping because it doesn't take partially
2858 visible lines into account. For example, it can be negative for
2859 partially visible lines at the top of a window. */
2860 if (!s->row->full_width_p
2861 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2862 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2863 else
2864 r->y = max (0, s->row->y);
2866 /* If drawing a tool-bar window, draw it over the internal border
2867 at the top of the window. */
2868 if (s->w == XWINDOW (s->f->tool_bar_window))
2869 r->y -= s->f->output_data.x->internal_border_width;
2872 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2876 /* Set clipping for output of glyph string S. S may be part of a mode
2877 line or menu if we don't have X toolkit support. */
2879 static INLINE void
2880 x_set_glyph_string_clipping (s)
2881 struct glyph_string *s;
2883 XRectangle r;
2884 x_get_glyph_string_clip_rect (s, &r);
2885 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2889 /* Compute left and right overhang of glyph string S. If S is a glyph
2890 string for a composition, assume overhangs don't exist. */
2892 static INLINE void
2893 x_compute_glyph_string_overhangs (s)
2894 struct glyph_string *s;
2896 if (s->cmp == NULL
2897 && s->first_glyph->type == CHAR_GLYPH)
2899 XCharStruct cs;
2900 int direction, font_ascent, font_descent;
2901 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2902 &font_ascent, &font_descent, &cs);
2903 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2904 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2909 /* Compute overhangs and x-positions for glyph string S and its
2910 predecessors, or successors. X is the starting x-position for S.
2911 BACKWARD_P non-zero means process predecessors. */
2913 static void
2914 x_compute_overhangs_and_x (s, x, backward_p)
2915 struct glyph_string *s;
2916 int x;
2917 int backward_p;
2919 if (backward_p)
2921 while (s)
2923 x_compute_glyph_string_overhangs (s);
2924 x -= s->width;
2925 s->x = x;
2926 s = s->prev;
2929 else
2931 while (s)
2933 x_compute_glyph_string_overhangs (s);
2934 s->x = x;
2935 x += s->width;
2936 s = s->next;
2942 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2943 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2944 assumed to be zero. */
2946 static void
2947 x_get_glyph_overhangs (glyph, f, left, right)
2948 struct glyph *glyph;
2949 struct frame *f;
2950 int *left, *right;
2952 *left = *right = 0;
2954 if (glyph->type == CHAR_GLYPH)
2956 XFontStruct *font;
2957 struct face *face;
2958 struct font_info *font_info;
2959 XChar2b char2b;
2960 XCharStruct *pcm;
2962 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2963 font = face->font;
2964 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2965 if (font
2966 && (pcm = x_per_char_metric (font, &char2b)))
2968 if (pcm->rbearing > pcm->width)
2969 *right = pcm->rbearing - pcm->width;
2970 if (pcm->lbearing < 0)
2971 *left = -pcm->lbearing;
2977 /* Return the index of the first glyph preceding glyph string S that
2978 is overwritten by S because of S's left overhang. Value is -1
2979 if no glyphs are overwritten. */
2981 static int
2982 x_left_overwritten (s)
2983 struct glyph_string *s;
2985 int k;
2987 if (s->left_overhang)
2989 int x = 0, i;
2990 struct glyph *glyphs = s->row->glyphs[s->area];
2991 int first = s->first_glyph - glyphs;
2993 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2994 x -= glyphs[i].pixel_width;
2996 k = i + 1;
2998 else
2999 k = -1;
3001 return k;
3005 /* Return the index of the first glyph preceding glyph string S that
3006 is overwriting S because of its right overhang. Value is -1 if no
3007 glyph in front of S overwrites S. */
3009 static int
3010 x_left_overwriting (s)
3011 struct glyph_string *s;
3013 int i, k, x;
3014 struct glyph *glyphs = s->row->glyphs[s->area];
3015 int first = s->first_glyph - glyphs;
3017 k = -1;
3018 x = 0;
3019 for (i = first - 1; i >= 0; --i)
3021 int left, right;
3022 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3023 if (x + right > 0)
3024 k = i;
3025 x -= glyphs[i].pixel_width;
3028 return k;
3032 /* Return the index of the last glyph following glyph string S that is
3033 not overwritten by S because of S's right overhang. Value is -1 if
3034 no such glyph is found. */
3036 static int
3037 x_right_overwritten (s)
3038 struct glyph_string *s;
3040 int k = -1;
3042 if (s->right_overhang)
3044 int x = 0, i;
3045 struct glyph *glyphs = s->row->glyphs[s->area];
3046 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3047 int end = s->row->used[s->area];
3049 for (i = first; i < end && s->right_overhang > x; ++i)
3050 x += glyphs[i].pixel_width;
3052 k = i;
3055 return k;
3059 /* Return the index of the last glyph following glyph string S that
3060 overwrites S because of its left overhang. Value is negative
3061 if no such glyph is found. */
3063 static int
3064 x_right_overwriting (s)
3065 struct glyph_string *s;
3067 int i, k, x;
3068 int end = s->row->used[s->area];
3069 struct glyph *glyphs = s->row->glyphs[s->area];
3070 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3072 k = -1;
3073 x = 0;
3074 for (i = first; i < end; ++i)
3076 int left, right;
3077 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3078 if (x - left < 0)
3079 k = i;
3080 x += glyphs[i].pixel_width;
3083 return k;
3087 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3089 static INLINE void
3090 x_clear_glyph_string_rect (s, x, y, w, h)
3091 struct glyph_string *s;
3092 int x, y, w, h;
3094 XGCValues xgcv;
3095 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3096 XSetForeground (s->display, s->gc, xgcv.background);
3097 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3098 XSetForeground (s->display, s->gc, xgcv.foreground);
3102 /* Draw the background of glyph_string S. If S->background_filled_p
3103 is non-zero don't draw it. FORCE_P non-zero means draw the
3104 background even if it wouldn't be drawn normally. This is used
3105 when a string preceding S draws into the background of S, or S
3106 contains the first component of a composition. */
3108 static void
3109 x_draw_glyph_string_background (s, force_p)
3110 struct glyph_string *s;
3111 int force_p;
3113 /* Nothing to do if background has already been drawn or if it
3114 shouldn't be drawn in the first place. */
3115 if (!s->background_filled_p)
3117 int box_line_width = max (s->face->box_line_width, 0);
3119 if (s->stippled_p)
3121 /* Fill background with a stipple pattern. */
3122 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3123 XFillRectangle (s->display, s->window, s->gc, s->x,
3124 s->y + box_line_width,
3125 s->background_width,
3126 s->height - 2 * box_line_width);
3127 XSetFillStyle (s->display, s->gc, FillSolid);
3128 s->background_filled_p = 1;
3130 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
3131 || s->font_not_found_p
3132 || s->extends_to_end_of_line_p
3133 || force_p)
3135 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
3136 s->background_width,
3137 s->height - 2 * box_line_width);
3138 s->background_filled_p = 1;
3144 /* Draw the foreground of glyph string S. */
3146 static void
3147 x_draw_glyph_string_foreground (s)
3148 struct glyph_string *s;
3150 int i, x;
3152 /* If first glyph of S has a left box line, start drawing the text
3153 of S to the right of that box line. */
3154 if (s->face->box != FACE_NO_BOX
3155 && s->first_glyph->left_box_line_p)
3156 x = s->x + abs (s->face->box_line_width);
3157 else
3158 x = s->x;
3160 /* Draw characters of S as rectangles if S's font could not be
3161 loaded. */
3162 if (s->font_not_found_p)
3164 for (i = 0; i < s->nchars; ++i)
3166 struct glyph *g = s->first_glyph + i;
3167 XDrawRectangle (s->display, s->window,
3168 s->gc, x, s->y, g->pixel_width - 1,
3169 s->height - 1);
3170 x += g->pixel_width;
3173 else
3175 char *char1b = (char *) s->char2b;
3176 int boff = s->font_info->baseline_offset;
3178 if (s->font_info->vertical_centering)
3179 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3181 /* If we can use 8-bit functions, condense S->char2b. */
3182 if (!s->two_byte_p)
3183 for (i = 0; i < s->nchars; ++i)
3184 char1b[i] = s->char2b[i].byte2;
3186 /* Draw text with XDrawString if background has already been
3187 filled. Otherwise, use XDrawImageString. (Note that
3188 XDrawImageString is usually faster than XDrawString.) Always
3189 use XDrawImageString when drawing the cursor so that there is
3190 no chance that characters under a box cursor are invisible. */
3191 if (s->for_overlaps_p
3192 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3194 /* Draw characters with 16-bit or 8-bit functions. */
3195 if (s->two_byte_p)
3196 XDrawString16 (s->display, s->window, s->gc, x,
3197 s->ybase - boff, s->char2b, s->nchars);
3198 else
3199 XDrawString (s->display, s->window, s->gc, x,
3200 s->ybase - boff, char1b, s->nchars);
3202 else
3204 if (s->two_byte_p)
3205 XDrawImageString16 (s->display, s->window, s->gc, x,
3206 s->ybase - boff, s->char2b, s->nchars);
3207 else
3208 XDrawImageString (s->display, s->window, s->gc, x,
3209 s->ybase - boff, char1b, s->nchars);
3214 /* Draw the foreground of composite glyph string S. */
3216 static void
3217 x_draw_composite_glyph_string_foreground (s)
3218 struct glyph_string *s;
3220 int i, x;
3222 /* If first glyph of S has a left box line, start drawing the text
3223 of S to the right of that box line. */
3224 if (s->face->box != FACE_NO_BOX
3225 && s->first_glyph->left_box_line_p)
3226 x = s->x + abs (s->face->box_line_width);
3227 else
3228 x = s->x;
3230 /* S is a glyph string for a composition. S->gidx is the index of
3231 the first character drawn for glyphs of this composition.
3232 S->gidx == 0 means we are drawing the very first character of
3233 this composition. */
3235 /* Draw a rectangle for the composition if the font for the very
3236 first character of the composition could not be loaded. */
3237 if (s->font_not_found_p)
3239 if (s->gidx == 0)
3240 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3241 s->width - 1, s->height - 1);
3243 else
3245 for (i = 0; i < s->nchars; i++, ++s->gidx)
3246 XDrawString16 (s->display, s->window, s->gc,
3247 x + s->cmp->offsets[s->gidx * 2],
3248 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3249 s->char2b + i, 1);
3254 #ifdef USE_X_TOOLKIT
3256 static struct frame *x_frame_of_widget P_ ((Widget));
3257 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
3258 XrmValue *, XrmValue *, XtPointer *));
3259 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
3260 XrmValue *, Cardinal *));
3263 /* Return the frame on which widget WIDGET is used.. Abort if frame
3264 cannot be determined. */
3266 static struct frame *
3267 x_frame_of_widget (widget)
3268 Widget widget;
3270 struct x_display_info *dpyinfo;
3271 Lisp_Object tail;
3272 struct frame *f;
3274 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3276 /* Find the top-level shell of the widget. Note that this function
3277 can be called when the widget is not yet realized, so XtWindow
3278 (widget) == 0. That's the reason we can't simply use
3279 x_any_window_to_frame. */
3280 while (!XtIsTopLevelShell (widget))
3281 widget = XtParent (widget);
3283 /* Look for a frame with that top-level widget. Allocate the color
3284 on that frame to get the right gamma correction value. */
3285 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3286 if (GC_FRAMEP (XCAR (tail))
3287 && (f = XFRAME (XCAR (tail)),
3288 (f->output_data.nothing != 1
3289 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3290 && f->output_data.x->widget == widget)
3291 return f;
3293 abort ();
3297 /* Allocate the color COLOR->pixel on the screen and display of
3298 widget WIDGET in colormap CMAP. If an exact match cannot be
3299 allocated, try the nearest color available. Value is non-zero
3300 if successful. This is called from lwlib. */
3303 x_alloc_nearest_color_for_widget (widget, cmap, color)
3304 Widget widget;
3305 Colormap cmap;
3306 XColor *color;
3308 struct frame *f = x_frame_of_widget (widget);
3309 return x_alloc_nearest_color (f, cmap, color);
3313 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3314 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3315 If this produces the same color as PIXEL, try a color where all RGB
3316 values have DELTA added. Return the allocated color in *PIXEL.
3317 DISPLAY is the X display, CMAP is the colormap to operate on.
3318 Value is non-zero if successful. */
3321 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3322 Widget widget;
3323 Display *display;
3324 Colormap cmap;
3325 unsigned long *pixel;
3326 double factor;
3327 int delta;
3329 struct frame *f = x_frame_of_widget (widget);
3330 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3334 /* Structure specifying which arguments should be passed by Xt to
3335 cvt_string_to_pixel. We want the widget's screen and colormap. */
3337 static XtConvertArgRec cvt_string_to_pixel_args[] =
3339 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
3340 sizeof (Screen *)},
3341 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
3342 sizeof (Colormap)}
3346 /* The address of this variable is returned by
3347 cvt_string_to_pixel. */
3349 static Pixel cvt_string_to_pixel_value;
3352 /* Convert a color name to a pixel color.
3354 DPY is the display we are working on.
3356 ARGS is an array of *NARGS XrmValue structures holding additional
3357 information about the widget for which the conversion takes place.
3358 The contents of this array are determined by the specification
3359 in cvt_string_to_pixel_args.
3361 FROM is a pointer to an XrmValue which points to the color name to
3362 convert. TO is an XrmValue in which to return the pixel color.
3364 CLOSURE_RET is a pointer to user-data, in which we record if
3365 we allocated the color or not.
3367 Value is True if successful, False otherwise. */
3369 static Boolean
3370 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
3371 Display *dpy;
3372 XrmValue *args;
3373 Cardinal *nargs;
3374 XrmValue *from, *to;
3375 XtPointer *closure_ret;
3377 Screen *screen;
3378 Colormap cmap;
3379 Pixel pixel;
3380 String color_name;
3381 XColor color;
3383 if (*nargs != 2)
3385 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3386 "wrongParameters", "cvt_string_to_pixel",
3387 "XtToolkitError",
3388 "Screen and colormap args required", NULL, NULL);
3389 return False;
3392 screen = *(Screen **) args[0].addr;
3393 cmap = *(Colormap *) args[1].addr;
3394 color_name = (String) from->addr;
3396 if (strcmp (color_name, XtDefaultBackground) == 0)
3398 *closure_ret = (XtPointer) False;
3399 pixel = WhitePixelOfScreen (screen);
3401 else if (strcmp (color_name, XtDefaultForeground) == 0)
3403 *closure_ret = (XtPointer) False;
3404 pixel = BlackPixelOfScreen (screen);
3406 else if (XParseColor (dpy, cmap, color_name, &color)
3407 && x_alloc_nearest_color_1 (dpy, cmap, &color))
3409 pixel = color.pixel;
3410 *closure_ret = (XtPointer) True;
3412 else
3414 String params[1];
3415 Cardinal nparams = 1;
3417 params[0] = color_name;
3418 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3419 "badValue", "cvt_string_to_pixel",
3420 "XtToolkitError", "Invalid color `%s'",
3421 params, &nparams);
3422 return False;
3425 if (to->addr != NULL)
3427 if (to->size < sizeof (Pixel))
3429 to->size = sizeof (Pixel);
3430 return False;
3433 *(Pixel *) to->addr = pixel;
3435 else
3437 cvt_string_to_pixel_value = pixel;
3438 to->addr = (XtPointer) &cvt_string_to_pixel_value;
3441 to->size = sizeof (Pixel);
3442 return True;
3446 /* Free a pixel color which was previously allocated via
3447 cvt_string_to_pixel. This is registered as the destructor
3448 for this type of resource via XtSetTypeConverter.
3450 APP is the application context in which we work.
3452 TO is a pointer to an XrmValue holding the color to free.
3453 CLOSURE is the value we stored in CLOSURE_RET for this color
3454 in cvt_string_to_pixel.
3456 ARGS and NARGS are like for cvt_string_to_pixel. */
3458 static void
3459 cvt_pixel_dtor (app, to, closure, args, nargs)
3460 XtAppContext app;
3461 XrmValuePtr to;
3462 XtPointer closure;
3463 XrmValuePtr args;
3464 Cardinal *nargs;
3466 if (*nargs != 2)
3468 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
3469 "XtToolkitError",
3470 "Screen and colormap arguments required",
3471 NULL, NULL);
3473 else if (closure != NULL)
3475 /* We did allocate the pixel, so free it. */
3476 Screen *screen = *(Screen **) args[0].addr;
3477 Colormap cmap = *(Colormap *) args[1].addr;
3478 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
3479 (Pixel *) to->addr, 1);
3484 #endif /* USE_X_TOOLKIT */
3487 /* Value is an array of XColor structures for the contents of the
3488 color map of display DPY. Set *NCELLS to the size of the array.
3489 Note that this probably shouldn't be called for large color maps,
3490 say a 24-bit TrueColor map. */
3492 static const XColor *
3493 x_color_cells (dpy, ncells)
3494 Display *dpy;
3495 int *ncells;
3497 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3499 if (dpyinfo->color_cells == NULL)
3501 Screen *screen = dpyinfo->screen;
3502 int i;
3504 dpyinfo->ncolor_cells
3505 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
3506 dpyinfo->color_cells
3507 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3508 * sizeof *dpyinfo->color_cells);
3510 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3511 dpyinfo->color_cells[i].pixel = i;
3513 XQueryColors (dpy, dpyinfo->cmap,
3514 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3517 *ncells = dpyinfo->ncolor_cells;
3518 return dpyinfo->color_cells;
3522 /* On frame F, translate pixel colors to RGB values for the NCOLORS
3523 colors in COLORS. Use cached information, if available. */
3525 void
3526 x_query_colors (f, colors, ncolors)
3527 struct frame *f;
3528 XColor *colors;
3529 int ncolors;
3531 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3533 if (dpyinfo->color_cells)
3535 int i;
3536 for (i = 0; i < ncolors; ++i)
3538 unsigned long pixel = colors[i].pixel;
3539 xassert (pixel < dpyinfo->ncolor_cells);
3540 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3541 colors[i] = dpyinfo->color_cells[pixel];
3544 else
3545 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3549 /* On frame F, translate pixel color to RGB values for the color in
3550 COLOR. Use cached information, if available. */
3552 void
3553 x_query_color (f, color)
3554 struct frame *f;
3555 XColor *color;
3557 x_query_colors (f, color, 1);
3561 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
3562 exact match can't be allocated, try the nearest color available.
3563 Value is non-zero if successful. Set *COLOR to the color
3564 allocated. */
3566 static int
3567 x_alloc_nearest_color_1 (dpy, cmap, color)
3568 Display *dpy;
3569 Colormap cmap;
3570 XColor *color;
3572 int rc;
3574 rc = XAllocColor (dpy, cmap, color);
3575 if (rc == 0)
3577 /* If we got to this point, the colormap is full, so we're going
3578 to try to get the next closest color. The algorithm used is
3579 a least-squares matching, which is what X uses for closest
3580 color matching with StaticColor visuals. */
3581 int nearest, i;
3582 unsigned long nearest_delta = ~0;
3583 int ncells;
3584 const XColor *cells = x_color_cells (dpy, &ncells);
3586 for (nearest = i = 0; i < ncells; ++i)
3588 long dred = (color->red >> 8) - (cells[i].red >> 8);
3589 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3590 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3591 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3593 if (delta < nearest_delta)
3595 nearest = i;
3596 nearest_delta = delta;
3600 color->red = cells[nearest].red;
3601 color->green = cells[nearest].green;
3602 color->blue = cells[nearest].blue;
3603 rc = XAllocColor (dpy, cmap, color);
3605 else
3607 /* If allocation succeeded, and the allocated pixel color is not
3608 equal to a cached pixel color recorded earlier, there was a
3609 change in the colormap, so clear the color cache. */
3610 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3611 XColor *cached_color;
3613 if (dpyinfo->color_cells
3614 && (cached_color = &dpyinfo->color_cells[color->pixel],
3615 (cached_color->red != color->red
3616 || cached_color->blue != color->blue
3617 || cached_color->green != color->green)))
3619 xfree (dpyinfo->color_cells);
3620 dpyinfo->color_cells = NULL;
3621 dpyinfo->ncolor_cells = 0;
3625 #ifdef DEBUG_X_COLORS
3626 if (rc)
3627 register_color (color->pixel);
3628 #endif /* DEBUG_X_COLORS */
3630 return rc;
3634 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
3635 exact match can't be allocated, try the nearest color available.
3636 Value is non-zero if successful. Set *COLOR to the color
3637 allocated. */
3640 x_alloc_nearest_color (f, cmap, color)
3641 struct frame *f;
3642 Colormap cmap;
3643 XColor *color;
3645 gamma_correct (f, color);
3646 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
3650 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3651 It's necessary to do this instead of just using PIXEL directly to
3652 get color reference counts right. */
3654 unsigned long
3655 x_copy_color (f, pixel)
3656 struct frame *f;
3657 unsigned long pixel;
3659 XColor color;
3661 color.pixel = pixel;
3662 BLOCK_INPUT;
3663 x_query_color (f, &color);
3664 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3665 UNBLOCK_INPUT;
3666 #ifdef DEBUG_X_COLORS
3667 register_color (pixel);
3668 #endif
3669 return color.pixel;
3673 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3674 It's necessary to do this instead of just using PIXEL directly to
3675 get color reference counts right. */
3677 unsigned long
3678 x_copy_dpy_color (dpy, cmap, pixel)
3679 Display *dpy;
3680 Colormap cmap;
3681 unsigned long pixel;
3683 XColor color;
3685 color.pixel = pixel;
3686 BLOCK_INPUT;
3687 XQueryColor (dpy, cmap, &color);
3688 XAllocColor (dpy, cmap, &color);
3689 UNBLOCK_INPUT;
3690 #ifdef DEBUG_X_COLORS
3691 register_color (pixel);
3692 #endif
3693 return color.pixel;
3697 /* Brightness beyond which a color won't have its highlight brightness
3698 boosted.
3700 Nominally, highlight colors for `3d' faces are calculated by
3701 brightening an object's color by a constant scale factor, but this
3702 doesn't yield good results for dark colors, so for colors who's
3703 brightness is less than this value (on a scale of 0-65535) have an
3704 use an additional additive factor.
3706 The value here is set so that the default menu-bar/mode-line color
3707 (grey75) will not have its highlights changed at all. */
3708 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3711 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3712 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3713 If this produces the same color as PIXEL, try a color where all RGB
3714 values have DELTA added. Return the allocated color in *PIXEL.
3715 DISPLAY is the X display, CMAP is the colormap to operate on.
3716 Value is non-zero if successful. */
3718 static int
3719 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3720 struct frame *f;
3721 Display *display;
3722 Colormap cmap;
3723 unsigned long *pixel;
3724 double factor;
3725 int delta;
3727 XColor color, new;
3728 long bright;
3729 int success_p;
3731 /* Get RGB color values. */
3732 color.pixel = *pixel;
3733 x_query_color (f, &color);
3735 /* Change RGB values by specified FACTOR. Avoid overflow! */
3736 xassert (factor >= 0);
3737 new.red = min (0xffff, factor * color.red);
3738 new.green = min (0xffff, factor * color.green);
3739 new.blue = min (0xffff, factor * color.blue);
3741 /* Calculate brightness of COLOR. */
3742 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
3744 /* We only boost colors that are darker than
3745 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3746 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3747 /* Make an additive adjustment to NEW, because it's dark enough so
3748 that scaling by FACTOR alone isn't enough. */
3750 /* How far below the limit this color is (0 - 1, 1 being darker). */
3751 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3752 /* The additive adjustment. */
3753 int min_delta = delta * dimness * factor / 2;
3755 if (factor < 1)
3757 new.red = max (0, new.red - min_delta);
3758 new.green = max (0, new.green - min_delta);
3759 new.blue = max (0, new.blue - min_delta);
3761 else
3763 new.red = min (0xffff, min_delta + new.red);
3764 new.green = min (0xffff, min_delta + new.green);
3765 new.blue = min (0xffff, min_delta + new.blue);
3769 /* Try to allocate the color. */
3770 success_p = x_alloc_nearest_color (f, cmap, &new);
3771 if (success_p)
3773 if (new.pixel == *pixel)
3775 /* If we end up with the same color as before, try adding
3776 delta to the RGB values. */
3777 x_free_colors (f, &new.pixel, 1);
3779 new.red = min (0xffff, delta + color.red);
3780 new.green = min (0xffff, delta + color.green);
3781 new.blue = min (0xffff, delta + color.blue);
3782 success_p = x_alloc_nearest_color (f, cmap, &new);
3784 else
3785 success_p = 1;
3786 *pixel = new.pixel;
3789 return success_p;
3793 /* Set up the foreground color for drawing relief lines of glyph
3794 string S. RELIEF is a pointer to a struct relief containing the GC
3795 with which lines will be drawn. Use a color that is FACTOR or
3796 DELTA lighter or darker than the relief's background which is found
3797 in S->f->output_data.x->relief_background. If such a color cannot
3798 be allocated, use DEFAULT_PIXEL, instead. */
3800 static void
3801 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3802 struct frame *f;
3803 struct relief *relief;
3804 double factor;
3805 int delta;
3806 unsigned long default_pixel;
3808 XGCValues xgcv;
3809 struct x_output *di = f->output_data.x;
3810 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3811 unsigned long pixel;
3812 unsigned long background = di->relief_background;
3813 Colormap cmap = FRAME_X_COLORMAP (f);
3814 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3815 Display *dpy = FRAME_X_DISPLAY (f);
3817 xgcv.graphics_exposures = False;
3818 xgcv.line_width = 1;
3820 /* Free previously allocated color. The color cell will be reused
3821 when it has been freed as many times as it was allocated, so this
3822 doesn't affect faces using the same colors. */
3823 if (relief->gc
3824 && relief->allocated_p)
3826 x_free_colors (f, &relief->pixel, 1);
3827 relief->allocated_p = 0;
3830 /* Allocate new color. */
3831 xgcv.foreground = default_pixel;
3832 pixel = background;
3833 if (dpyinfo->n_planes != 1
3834 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3836 relief->allocated_p = 1;
3837 xgcv.foreground = relief->pixel = pixel;
3840 if (relief->gc == 0)
3842 xgcv.stipple = dpyinfo->gray;
3843 mask |= GCStipple;
3844 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3846 else
3847 XChangeGC (dpy, relief->gc, mask, &xgcv);
3851 /* Set up colors for the relief lines around glyph string S. */
3853 static void
3854 x_setup_relief_colors (s)
3855 struct glyph_string *s;
3857 struct x_output *di = s->f->output_data.x;
3858 unsigned long color;
3860 if (s->face->use_box_color_for_shadows_p)
3861 color = s->face->box_color;
3862 else if (s->first_glyph->type == IMAGE_GLYPH
3863 && s->img->pixmap
3864 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
3865 color = IMAGE_BACKGROUND (s->img, s->f, 0);
3866 else
3868 XGCValues xgcv;
3870 /* Get the background color of the face. */
3871 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3872 color = xgcv.background;
3875 if (di->white_relief.gc == 0
3876 || color != di->relief_background)
3878 di->relief_background = color;
3879 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3880 WHITE_PIX_DEFAULT (s->f));
3881 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3882 BLACK_PIX_DEFAULT (s->f));
3887 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3888 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3889 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3890 relief. LEFT_P non-zero means draw a relief on the left side of
3891 the rectangle. RIGHT_P non-zero means draw a relief on the right
3892 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3893 when drawing. */
3895 static void
3896 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3897 raised_p, left_p, right_p, clip_rect)
3898 struct frame *f;
3899 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3900 XRectangle *clip_rect;
3902 Display *dpy = FRAME_X_DISPLAY (f);
3903 Window window = FRAME_X_WINDOW (f);
3904 int i;
3905 GC gc;
3907 if (raised_p)
3908 gc = f->output_data.x->white_relief.gc;
3909 else
3910 gc = f->output_data.x->black_relief.gc;
3911 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
3913 /* Top. */
3914 for (i = 0; i < width; ++i)
3915 XDrawLine (dpy, window, gc,
3916 left_x + i * left_p, top_y + i,
3917 right_x + 1 - i * right_p, top_y + i);
3919 /* Left. */
3920 if (left_p)
3921 for (i = 0; i < width; ++i)
3922 XDrawLine (dpy, window, gc,
3923 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
3925 XSetClipMask (dpy, gc, None);
3926 if (raised_p)
3927 gc = f->output_data.x->black_relief.gc;
3928 else
3929 gc = f->output_data.x->white_relief.gc;
3930 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
3932 /* Bottom. */
3933 for (i = 0; i < width; ++i)
3934 XDrawLine (dpy, window, gc,
3935 left_x + i * left_p, bottom_y - i,
3936 right_x + 1 - i * right_p, bottom_y - i);
3938 /* Right. */
3939 if (right_p)
3940 for (i = 0; i < width; ++i)
3941 XDrawLine (dpy, window, gc,
3942 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3944 XSetClipMask (dpy, gc, None);
3948 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3949 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3950 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3951 left side of the rectangle. RIGHT_P non-zero means draw a line
3952 on the right side of the rectangle. CLIP_RECT is the clipping
3953 rectangle to use when drawing. */
3955 static void
3956 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3957 left_p, right_p, clip_rect)
3958 struct glyph_string *s;
3959 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3960 XRectangle *clip_rect;
3962 XGCValues xgcv;
3964 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3965 XSetForeground (s->display, s->gc, s->face->box_color);
3966 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3968 /* Top. */
3969 XFillRectangle (s->display, s->window, s->gc,
3970 left_x, top_y, right_x - left_x + 1, width);
3972 /* Left. */
3973 if (left_p)
3974 XFillRectangle (s->display, s->window, s->gc,
3975 left_x, top_y, width, bottom_y - top_y + 1);
3977 /* Bottom. */
3978 XFillRectangle (s->display, s->window, s->gc,
3979 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3981 /* Right. */
3982 if (right_p)
3983 XFillRectangle (s->display, s->window, s->gc,
3984 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3986 XSetForeground (s->display, s->gc, xgcv.foreground);
3987 XSetClipMask (s->display, s->gc, None);
3991 /* Draw a box around glyph string S. */
3993 static void
3994 x_draw_glyph_string_box (s)
3995 struct glyph_string *s;
3997 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3998 int left_p, right_p;
3999 struct glyph *last_glyph;
4000 XRectangle clip_rect;
4002 last_x = window_box_right (s->w, s->area);
4003 if (s->row->full_width_p
4004 && !s->w->pseudo_window_p)
4006 last_x += FRAME_X_RIGHT_FRINGE_WIDTH (s->f);
4007 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
4008 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
4011 /* The glyph that may have a right box line. */
4012 last_glyph = (s->cmp || s->img
4013 ? s->first_glyph
4014 : s->first_glyph + s->nchars - 1);
4016 width = abs (s->face->box_line_width);
4017 raised_p = s->face->box == FACE_RAISED_BOX;
4018 left_x = s->x;
4019 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
4020 ? last_x - 1
4021 : min (last_x, s->x + s->background_width) - 1);
4022 top_y = s->y;
4023 bottom_y = top_y + s->height - 1;
4025 left_p = (s->first_glyph->left_box_line_p
4026 || (s->hl == DRAW_MOUSE_FACE
4027 && (s->prev == NULL
4028 || s->prev->hl != s->hl)));
4029 right_p = (last_glyph->right_box_line_p
4030 || (s->hl == DRAW_MOUSE_FACE
4031 && (s->next == NULL
4032 || s->next->hl != s->hl)));
4034 x_get_glyph_string_clip_rect (s, &clip_rect);
4036 if (s->face->box == FACE_SIMPLE_BOX)
4037 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
4038 left_p, right_p, &clip_rect);
4039 else
4041 x_setup_relief_colors (s);
4042 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
4043 width, raised_p, left_p, right_p, &clip_rect);
4048 /* Draw foreground of image glyph string S. */
4050 static void
4051 x_draw_image_foreground (s)
4052 struct glyph_string *s;
4054 int x;
4055 int y = s->ybase - image_ascent (s->img, s->face);
4057 /* If first glyph of S has a left box line, start drawing it to the
4058 right of that line. */
4059 if (s->face->box != FACE_NO_BOX
4060 && s->first_glyph->left_box_line_p)
4061 x = s->x + abs (s->face->box_line_width);
4062 else
4063 x = s->x;
4065 /* If there is a margin around the image, adjust x- and y-position
4066 by that margin. */
4067 x += s->img->hmargin;
4068 y += s->img->vmargin;
4070 if (s->img->pixmap)
4072 if (s->img->mask)
4074 /* We can't set both a clip mask and use XSetClipRectangles
4075 because the latter also sets a clip mask. We also can't
4076 trust on the shape extension to be available
4077 (XShapeCombineRegion). So, compute the rectangle to draw
4078 manually. */
4079 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4080 | GCFunction);
4081 XGCValues xgcv;
4082 XRectangle clip_rect, image_rect, r;
4084 xgcv.clip_mask = s->img->mask;
4085 xgcv.clip_x_origin = x;
4086 xgcv.clip_y_origin = y;
4087 xgcv.function = GXcopy;
4088 XChangeGC (s->display, s->gc, mask, &xgcv);
4090 x_get_glyph_string_clip_rect (s, &clip_rect);
4091 image_rect.x = x;
4092 image_rect.y = y;
4093 image_rect.width = s->img->width;
4094 image_rect.height = s->img->height;
4095 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4096 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4097 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4099 else
4101 XRectangle clip_rect, image_rect, r;
4103 x_get_glyph_string_clip_rect (s, &clip_rect);
4104 image_rect.x = x;
4105 image_rect.y = y;
4106 image_rect.width = s->img->width;
4107 image_rect.height = s->img->height;
4108 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4109 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4110 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4112 /* When the image has a mask, we can expect that at
4113 least part of a mouse highlight or a block cursor will
4114 be visible. If the image doesn't have a mask, make
4115 a block cursor visible by drawing a rectangle around
4116 the image. I believe it's looking better if we do
4117 nothing here for mouse-face. */
4118 if (s->hl == DRAW_CURSOR)
4120 int r = s->img->relief;
4121 if (r < 0) r = -r;
4122 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
4123 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
4127 else
4128 /* Draw a rectangle if image could not be loaded. */
4129 XDrawRectangle (s->display, s->window, s->gc, x, y,
4130 s->img->width - 1, s->img->height - 1);
4134 /* Draw a relief around the image glyph string S. */
4136 static void
4137 x_draw_image_relief (s)
4138 struct glyph_string *s;
4140 int x0, y0, x1, y1, thick, raised_p;
4141 XRectangle r;
4142 int x;
4143 int y = s->ybase - image_ascent (s->img, s->face);
4145 /* If first glyph of S has a left box line, start drawing it to the
4146 right of that line. */
4147 if (s->face->box != FACE_NO_BOX
4148 && s->first_glyph->left_box_line_p)
4149 x = s->x + abs (s->face->box_line_width);
4150 else
4151 x = s->x;
4153 /* If there is a margin around the image, adjust x- and y-position
4154 by that margin. */
4155 x += s->img->hmargin;
4156 y += s->img->vmargin;
4158 if (s->hl == DRAW_IMAGE_SUNKEN
4159 || s->hl == DRAW_IMAGE_RAISED)
4161 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
4162 raised_p = s->hl == DRAW_IMAGE_RAISED;
4164 else
4166 thick = abs (s->img->relief);
4167 raised_p = s->img->relief > 0;
4170 x0 = x - thick;
4171 y0 = y - thick;
4172 x1 = x + s->img->width + thick - 1;
4173 y1 = y + s->img->height + thick - 1;
4175 x_setup_relief_colors (s);
4176 x_get_glyph_string_clip_rect (s, &r);
4177 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
4181 /* Draw the foreground of image glyph string S to PIXMAP. */
4183 static void
4184 x_draw_image_foreground_1 (s, pixmap)
4185 struct glyph_string *s;
4186 Pixmap pixmap;
4188 int x;
4189 int y = s->ybase - s->y - image_ascent (s->img, s->face);
4191 /* If first glyph of S has a left box line, start drawing it to the
4192 right of that line. */
4193 if (s->face->box != FACE_NO_BOX
4194 && s->first_glyph->left_box_line_p)
4195 x = abs (s->face->box_line_width);
4196 else
4197 x = 0;
4199 /* If there is a margin around the image, adjust x- and y-position
4200 by that margin. */
4201 x += s->img->hmargin;
4202 y += s->img->vmargin;
4204 if (s->img->pixmap)
4206 if (s->img->mask)
4208 /* We can't set both a clip mask and use XSetClipRectangles
4209 because the latter also sets a clip mask. We also can't
4210 trust on the shape extension to be available
4211 (XShapeCombineRegion). So, compute the rectangle to draw
4212 manually. */
4213 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4214 | GCFunction);
4215 XGCValues xgcv;
4217 xgcv.clip_mask = s->img->mask;
4218 xgcv.clip_x_origin = x;
4219 xgcv.clip_y_origin = y;
4220 xgcv.function = GXcopy;
4221 XChangeGC (s->display, s->gc, mask, &xgcv);
4223 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4224 0, 0, s->img->width, s->img->height, x, y);
4225 XSetClipMask (s->display, s->gc, None);
4227 else
4229 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4230 0, 0, s->img->width, s->img->height, x, y);
4232 /* When the image has a mask, we can expect that at
4233 least part of a mouse highlight or a block cursor will
4234 be visible. If the image doesn't have a mask, make
4235 a block cursor visible by drawing a rectangle around
4236 the image. I believe it's looking better if we do
4237 nothing here for mouse-face. */
4238 if (s->hl == DRAW_CURSOR)
4240 int r = s->img->relief;
4241 if (r < 0) r = -r;
4242 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
4243 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
4247 else
4248 /* Draw a rectangle if image could not be loaded. */
4249 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4250 s->img->width - 1, s->img->height - 1);
4254 /* Draw part of the background of glyph string S. X, Y, W, and H
4255 give the rectangle to draw. */
4257 static void
4258 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4259 struct glyph_string *s;
4260 int x, y, w, h;
4262 if (s->stippled_p)
4264 /* Fill background with a stipple pattern. */
4265 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4266 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4267 XSetFillStyle (s->display, s->gc, FillSolid);
4269 else
4270 x_clear_glyph_string_rect (s, x, y, w, h);
4274 /* Draw image glyph string S.
4276 s->y
4277 s->x +-------------------------
4278 | s->face->box
4280 | +-------------------------
4281 | | s->img->margin
4283 | | +-------------------
4284 | | | the image
4288 static void
4289 x_draw_image_glyph_string (s)
4290 struct glyph_string *s;
4292 int x, y;
4293 int box_line_hwidth = abs (s->face->box_line_width);
4294 int box_line_vwidth = max (s->face->box_line_width, 0);
4295 int height;
4296 Pixmap pixmap = None;
4298 height = s->height - 2 * box_line_vwidth;
4300 /* Fill background with face under the image. Do it only if row is
4301 taller than image or if image has a clip mask to reduce
4302 flickering. */
4303 s->stippled_p = s->face->stipple != 0;
4304 if (height > s->img->height
4305 || s->img->hmargin
4306 || s->img->vmargin
4307 || s->img->mask
4308 || s->img->pixmap == 0
4309 || s->width != s->background_width)
4311 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4312 x = s->x + box_line_hwidth;
4313 else
4314 x = s->x;
4316 y = s->y + box_line_vwidth;
4318 if (s->img->mask)
4320 /* Create a pixmap as large as the glyph string. Fill it
4321 with the background color. Copy the image to it, using
4322 its mask. Copy the temporary pixmap to the display. */
4323 Screen *screen = FRAME_X_SCREEN (s->f);
4324 int depth = DefaultDepthOfScreen (screen);
4326 /* Create a pixmap as large as the glyph string. */
4327 pixmap = XCreatePixmap (s->display, s->window,
4328 s->background_width,
4329 s->height, depth);
4331 /* Don't clip in the following because we're working on the
4332 pixmap. */
4333 XSetClipMask (s->display, s->gc, None);
4335 /* Fill the pixmap with the background color/stipple. */
4336 if (s->stippled_p)
4338 /* Fill background with a stipple pattern. */
4339 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4340 XFillRectangle (s->display, pixmap, s->gc,
4341 0, 0, s->background_width, s->height);
4342 XSetFillStyle (s->display, s->gc, FillSolid);
4344 else
4346 XGCValues xgcv;
4347 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4348 &xgcv);
4349 XSetForeground (s->display, s->gc, xgcv.background);
4350 XFillRectangle (s->display, pixmap, s->gc,
4351 0, 0, s->background_width, s->height);
4352 XSetForeground (s->display, s->gc, xgcv.foreground);
4355 else
4356 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4358 s->background_filled_p = 1;
4361 /* Draw the foreground. */
4362 if (pixmap != None)
4364 x_draw_image_foreground_1 (s, pixmap);
4365 x_set_glyph_string_clipping (s);
4366 XCopyArea (s->display, pixmap, s->window, s->gc,
4367 0, 0, s->background_width, s->height, s->x, s->y);
4368 XFreePixmap (s->display, pixmap);
4370 else
4371 x_draw_image_foreground (s);
4373 /* If we must draw a relief around the image, do it. */
4374 if (s->img->relief
4375 || s->hl == DRAW_IMAGE_RAISED
4376 || s->hl == DRAW_IMAGE_SUNKEN)
4377 x_draw_image_relief (s);
4381 /* Draw stretch glyph string S. */
4383 static void
4384 x_draw_stretch_glyph_string (s)
4385 struct glyph_string *s;
4387 xassert (s->first_glyph->type == STRETCH_GLYPH);
4388 s->stippled_p = s->face->stipple != 0;
4390 if (s->hl == DRAW_CURSOR
4391 && !x_stretch_cursor_p)
4393 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4394 as wide as the stretch glyph. */
4395 int width = min (CANON_X_UNIT (s->f), s->background_width);
4397 /* Draw cursor. */
4398 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4400 /* Clear rest using the GC of the original non-cursor face. */
4401 if (width < s->background_width)
4403 int x = s->x + width, y = s->y;
4404 int w = s->background_width - width, h = s->height;
4405 XRectangle r;
4406 GC gc;
4408 if (s->row->mouse_face_p
4409 && cursor_in_mouse_face_p (s->w))
4411 x_set_mouse_face_gc (s);
4412 gc = s->gc;
4414 else
4415 gc = s->face->gc;
4417 x_get_glyph_string_clip_rect (s, &r);
4418 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4420 if (s->face->stipple)
4422 /* Fill background with a stipple pattern. */
4423 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4424 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4425 XSetFillStyle (s->display, gc, FillSolid);
4427 else
4429 XGCValues xgcv;
4430 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4431 XSetForeground (s->display, gc, xgcv.background);
4432 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4433 XSetForeground (s->display, gc, xgcv.foreground);
4437 else if (!s->background_filled_p)
4438 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4439 s->height);
4441 s->background_filled_p = 1;
4445 /* Draw glyph string S. */
4447 static void
4448 x_draw_glyph_string (s)
4449 struct glyph_string *s;
4451 int relief_drawn_p = 0;
4453 /* If S draws into the background of its successor, draw the
4454 background of the successor first so that S can draw into it.
4455 This makes S->next use XDrawString instead of XDrawImageString. */
4456 if (s->next && s->right_overhang && !s->for_overlaps_p)
4458 xassert (s->next->img == NULL);
4459 x_set_glyph_string_gc (s->next);
4460 x_set_glyph_string_clipping (s->next);
4461 x_draw_glyph_string_background (s->next, 1);
4464 /* Set up S->gc, set clipping and draw S. */
4465 x_set_glyph_string_gc (s);
4467 /* Draw relief (if any) in advance for char/composition so that the
4468 glyph string can be drawn over it. */
4469 if (!s->for_overlaps_p
4470 && s->face->box != FACE_NO_BOX
4471 && (s->first_glyph->type == CHAR_GLYPH
4472 || s->first_glyph->type == COMPOSITE_GLYPH))
4475 x_set_glyph_string_clipping (s);
4476 x_draw_glyph_string_background (s, 1);
4477 x_draw_glyph_string_box (s);
4478 x_set_glyph_string_clipping (s);
4479 relief_drawn_p = 1;
4481 else
4482 x_set_glyph_string_clipping (s);
4484 switch (s->first_glyph->type)
4486 case IMAGE_GLYPH:
4487 x_draw_image_glyph_string (s);
4488 break;
4490 case STRETCH_GLYPH:
4491 x_draw_stretch_glyph_string (s);
4492 break;
4494 case CHAR_GLYPH:
4495 if (s->for_overlaps_p)
4496 s->background_filled_p = 1;
4497 else
4498 x_draw_glyph_string_background (s, 0);
4499 x_draw_glyph_string_foreground (s);
4500 break;
4502 case COMPOSITE_GLYPH:
4503 if (s->for_overlaps_p || s->gidx > 0)
4504 s->background_filled_p = 1;
4505 else
4506 x_draw_glyph_string_background (s, 1);
4507 x_draw_composite_glyph_string_foreground (s);
4508 break;
4510 default:
4511 abort ();
4514 if (!s->for_overlaps_p)
4516 /* Draw underline. */
4517 if (s->face->underline_p)
4519 unsigned long tem, h;
4520 int y;
4522 /* Get the underline thickness. Default is 1 pixel. */
4523 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4524 h = 1;
4526 /* Get the underline position. This is the recommended
4527 vertical offset in pixels from the baseline to the top of
4528 the underline. This is a signed value according to the
4529 specs, and its default is
4531 ROUND ((maximum descent) / 2), with
4532 ROUND(x) = floor (x + 0.5) */
4534 if (x_use_underline_position_properties
4535 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4536 y = s->ybase + (long) tem;
4537 else if (s->face->font)
4538 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4539 else
4540 y = s->y + s->height - h;
4542 if (s->face->underline_defaulted_p)
4543 XFillRectangle (s->display, s->window, s->gc,
4544 s->x, y, s->width, h);
4545 else
4547 XGCValues xgcv;
4548 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4549 XSetForeground (s->display, s->gc, s->face->underline_color);
4550 XFillRectangle (s->display, s->window, s->gc,
4551 s->x, y, s->width, h);
4552 XSetForeground (s->display, s->gc, xgcv.foreground);
4556 /* Draw overline. */
4557 if (s->face->overline_p)
4559 unsigned long dy = 0, h = 1;
4561 if (s->face->overline_color_defaulted_p)
4562 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4563 s->width, h);
4564 else
4566 XGCValues xgcv;
4567 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4568 XSetForeground (s->display, s->gc, s->face->overline_color);
4569 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4570 s->width, h);
4571 XSetForeground (s->display, s->gc, xgcv.foreground);
4575 /* Draw strike-through. */
4576 if (s->face->strike_through_p)
4578 unsigned long h = 1;
4579 unsigned long dy = (s->height - h) / 2;
4581 if (s->face->strike_through_color_defaulted_p)
4582 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4583 s->width, h);
4584 else
4586 XGCValues xgcv;
4587 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4588 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4589 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4590 s->width, h);
4591 XSetForeground (s->display, s->gc, xgcv.foreground);
4595 /* Draw relief if not yet drawn. */
4596 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
4597 x_draw_glyph_string_box (s);
4600 /* Reset clipping. */
4601 XSetClipMask (s->display, s->gc, None);
4605 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4606 struct face **, int));
4609 /* Fill glyph string S with composition components specified by S->cmp.
4611 FACES is an array of faces for all components of this composition.
4612 S->gidx is the index of the first component for S.
4613 OVERLAPS_P non-zero means S should draw the foreground only, and
4614 use its physical height for clipping.
4616 Value is the index of a component not in S. */
4618 static int
4619 x_fill_composite_glyph_string (s, faces, overlaps_p)
4620 struct glyph_string *s;
4621 struct face **faces;
4622 int overlaps_p;
4624 int i;
4626 xassert (s);
4628 s->for_overlaps_p = overlaps_p;
4630 s->face = faces[s->gidx];
4631 s->font = s->face->font;
4632 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4634 /* For all glyphs of this composition, starting at the offset
4635 S->gidx, until we reach the end of the definition or encounter a
4636 glyph that requires the different face, add it to S. */
4637 ++s->nchars;
4638 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4639 ++s->nchars;
4641 /* All glyph strings for the same composition has the same width,
4642 i.e. the width set for the first component of the composition. */
4644 s->width = s->first_glyph->pixel_width;
4646 /* If the specified font could not be loaded, use the frame's
4647 default font, but record the fact that we couldn't load it in
4648 the glyph string so that we can draw rectangles for the
4649 characters of the glyph string. */
4650 if (s->font == NULL)
4652 s->font_not_found_p = 1;
4653 s->font = FRAME_FONT (s->f);
4656 /* Adjust base line for subscript/superscript text. */
4657 s->ybase += s->first_glyph->voffset;
4659 xassert (s->face && s->face->gc);
4661 /* This glyph string must always be drawn with 16-bit functions. */
4662 s->two_byte_p = 1;
4664 return s->gidx + s->nchars;
4668 /* Fill glyph string S from a sequence of character glyphs.
4670 FACE_ID is the face id of the string. START is the index of the
4671 first glyph to consider, END is the index of the last + 1.
4672 OVERLAPS_P non-zero means S should draw the foreground only, and
4673 use its physical height for clipping.
4675 Value is the index of the first glyph not in S. */
4677 static int
4678 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4679 struct glyph_string *s;
4680 int face_id;
4681 int start, end, overlaps_p;
4683 struct glyph *glyph, *last;
4684 int voffset;
4685 int glyph_not_available_p;
4687 xassert (s->f == XFRAME (s->w->frame));
4688 xassert (s->nchars == 0);
4689 xassert (start >= 0 && end > start);
4691 s->for_overlaps_p = overlaps_p,
4692 glyph = s->row->glyphs[s->area] + start;
4693 last = s->row->glyphs[s->area] + end;
4694 voffset = glyph->voffset;
4696 glyph_not_available_p = glyph->glyph_not_available_p;
4698 while (glyph < last
4699 && glyph->type == CHAR_GLYPH
4700 && glyph->voffset == voffset
4701 /* Same face id implies same font, nowadays. */
4702 && glyph->face_id == face_id
4703 && glyph->glyph_not_available_p == glyph_not_available_p)
4705 int two_byte_p;
4707 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4708 s->char2b + s->nchars,
4709 &two_byte_p);
4710 s->two_byte_p = two_byte_p;
4711 ++s->nchars;
4712 xassert (s->nchars <= end - start);
4713 s->width += glyph->pixel_width;
4714 ++glyph;
4717 s->font = s->face->font;
4718 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4720 /* If the specified font could not be loaded, use the frame's font,
4721 but record the fact that we couldn't load it in
4722 S->font_not_found_p so that we can draw rectangles for the
4723 characters of the glyph string. */
4724 if (s->font == NULL || glyph_not_available_p)
4726 s->font_not_found_p = 1;
4727 s->font = FRAME_FONT (s->f);
4730 /* Adjust base line for subscript/superscript text. */
4731 s->ybase += voffset;
4733 xassert (s->face && s->face->gc);
4734 return glyph - s->row->glyphs[s->area];
4738 /* Fill glyph string S from image glyph S->first_glyph. */
4740 static void
4741 x_fill_image_glyph_string (s)
4742 struct glyph_string *s;
4744 xassert (s->first_glyph->type == IMAGE_GLYPH);
4745 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4746 xassert (s->img);
4747 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4748 s->font = s->face->font;
4749 s->width = s->first_glyph->pixel_width;
4751 /* Adjust base line for subscript/superscript text. */
4752 s->ybase += s->first_glyph->voffset;
4756 /* Fill glyph string S from a sequence of stretch glyphs.
4758 ROW is the glyph row in which the glyphs are found, AREA is the
4759 area within the row. START is the index of the first glyph to
4760 consider, END is the index of the last + 1.
4762 Value is the index of the first glyph not in S. */
4764 static int
4765 x_fill_stretch_glyph_string (s, row, area, start, end)
4766 struct glyph_string *s;
4767 struct glyph_row *row;
4768 enum glyph_row_area area;
4769 int start, end;
4771 struct glyph *glyph, *last;
4772 int voffset, face_id;
4774 xassert (s->first_glyph->type == STRETCH_GLYPH);
4776 glyph = s->row->glyphs[s->area] + start;
4777 last = s->row->glyphs[s->area] + end;
4778 face_id = glyph->face_id;
4779 s->face = FACE_FROM_ID (s->f, face_id);
4780 s->font = s->face->font;
4781 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4782 s->width = glyph->pixel_width;
4783 voffset = glyph->voffset;
4785 for (++glyph;
4786 (glyph < last
4787 && glyph->type == STRETCH_GLYPH
4788 && glyph->voffset == voffset
4789 && glyph->face_id == face_id);
4790 ++glyph)
4791 s->width += glyph->pixel_width;
4793 /* Adjust base line for subscript/superscript text. */
4794 s->ybase += voffset;
4796 /* The case that face->gc == 0 is handled when drawing the glyph
4797 string by calling PREPARE_FACE_FOR_DISPLAY. */
4798 xassert (s->face);
4799 return glyph - s->row->glyphs[s->area];
4803 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4804 of XChar2b structures for S; it can't be allocated in
4805 x_init_glyph_string because it must be allocated via `alloca'. W
4806 is the window on which S is drawn. ROW and AREA are the glyph row
4807 and area within the row from which S is constructed. START is the
4808 index of the first glyph structure covered by S. HL is a
4809 face-override for drawing S. */
4811 static void
4812 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4813 struct glyph_string *s;
4814 XChar2b *char2b;
4815 struct window *w;
4816 struct glyph_row *row;
4817 enum glyph_row_area area;
4818 int start;
4819 enum draw_glyphs_face hl;
4821 bzero (s, sizeof *s);
4822 s->w = w;
4823 s->f = XFRAME (w->frame);
4824 s->display = FRAME_X_DISPLAY (s->f);
4825 s->window = FRAME_X_WINDOW (s->f);
4826 s->char2b = char2b;
4827 s->hl = hl;
4828 s->row = row;
4829 s->area = area;
4830 s->first_glyph = row->glyphs[area] + start;
4831 s->height = row->height;
4832 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4834 /* Display the internal border below the tool-bar window. */
4835 if (s->w == XWINDOW (s->f->tool_bar_window))
4836 s->y -= s->f->output_data.x->internal_border_width;
4838 s->ybase = s->y + row->ascent;
4842 /* Set background width of glyph string S. START is the index of the
4843 first glyph following S. LAST_X is the right-most x-position + 1
4844 in the drawing area. */
4846 static INLINE void
4847 x_set_glyph_string_background_width (s, start, last_x)
4848 struct glyph_string *s;
4849 int start;
4850 int last_x;
4852 /* If the face of this glyph string has to be drawn to the end of
4853 the drawing area, set S->extends_to_end_of_line_p. */
4854 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4856 if (start == s->row->used[s->area]
4857 && s->area == TEXT_AREA
4858 && ((s->hl == DRAW_NORMAL_TEXT
4859 && (s->row->fill_line_p
4860 || s->face->background != default_face->background
4861 || s->face->stipple != default_face->stipple
4862 || s->row->mouse_face_p))
4863 || s->hl == DRAW_MOUSE_FACE
4864 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
4865 && s->row->fill_line_p)))
4866 s->extends_to_end_of_line_p = 1;
4868 /* If S extends its face to the end of the line, set its
4869 background_width to the distance to the right edge of the drawing
4870 area. */
4871 if (s->extends_to_end_of_line_p)
4872 s->background_width = last_x - s->x + 1;
4873 else
4874 s->background_width = s->width;
4878 /* Add a glyph string for a stretch glyph to the list of strings
4879 between HEAD and TAIL. START is the index of the stretch glyph in
4880 row area AREA of glyph row ROW. END is the index of the last glyph
4881 in that glyph row area. X is the current output position assigned
4882 to the new glyph string constructed. HL overrides that face of the
4883 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4884 is the right-most x-position of the drawing area. */
4886 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4887 and below -- keep them on one line. */
4888 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4889 do \
4891 s = (struct glyph_string *) alloca (sizeof *s); \
4892 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4893 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4894 x_append_glyph_string (&HEAD, &TAIL, s); \
4895 s->x = (X); \
4897 while (0)
4900 /* Add a glyph string for an image glyph to the list of strings
4901 between HEAD and TAIL. START is the index of the image glyph in
4902 row area AREA of glyph row ROW. END is the index of the last glyph
4903 in that glyph row area. X is the current output position assigned
4904 to the new glyph string constructed. HL overrides that face of the
4905 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4906 is the right-most x-position of the drawing area. */
4908 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4909 do \
4911 s = (struct glyph_string *) alloca (sizeof *s); \
4912 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4913 x_fill_image_glyph_string (s); \
4914 x_append_glyph_string (&HEAD, &TAIL, s); \
4915 ++START; \
4916 s->x = (X); \
4918 while (0)
4921 /* Add a glyph string for a sequence of character glyphs to the list
4922 of strings between HEAD and TAIL. START is the index of the first
4923 glyph in row area AREA of glyph row ROW that is part of the new
4924 glyph string. END is the index of the last glyph in that glyph row
4925 area. X is the current output position assigned to the new glyph
4926 string constructed. HL overrides that face of the glyph; e.g. it
4927 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4928 right-most x-position of the drawing area. */
4930 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4931 do \
4933 int c, face_id; \
4934 XChar2b *char2b; \
4936 c = (ROW)->glyphs[AREA][START].u.ch; \
4937 face_id = (ROW)->glyphs[AREA][START].face_id; \
4939 s = (struct glyph_string *) alloca (sizeof *s); \
4940 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4941 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4942 x_append_glyph_string (&HEAD, &TAIL, s); \
4943 s->x = (X); \
4944 START = x_fill_glyph_string (s, face_id, START, END, \
4945 OVERLAPS_P); \
4947 while (0)
4950 /* Add a glyph string for a composite sequence to the list of strings
4951 between HEAD and TAIL. START is the index of the first glyph in
4952 row area AREA of glyph row ROW that is part of the new glyph
4953 string. END is the index of the last glyph in that glyph row area.
4954 X is the current output position assigned to the new glyph string
4955 constructed. HL overrides that face of the glyph; e.g. it is
4956 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4957 x-position of the drawing area. */
4959 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4960 do { \
4961 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4962 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4963 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4964 struct composition *cmp = composition_table[cmp_id]; \
4965 int glyph_len = cmp->glyph_len; \
4966 XChar2b *char2b; \
4967 struct face **faces; \
4968 struct glyph_string *first_s = NULL; \
4969 int n; \
4971 base_face = base_face->ascii_face; \
4972 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4973 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4974 /* At first, fill in `char2b' and `faces'. */ \
4975 for (n = 0; n < glyph_len; n++) \
4977 int c = COMPOSITION_GLYPH (cmp, n); \
4978 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4979 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4980 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4981 this_face_id, char2b + n, 1, 1); \
4984 /* Make glyph_strings for each glyph sequence that is drawable by \
4985 the same face, and append them to HEAD/TAIL. */ \
4986 for (n = 0; n < cmp->glyph_len;) \
4988 s = (struct glyph_string *) alloca (sizeof *s); \
4989 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4990 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4991 s->cmp = cmp; \
4992 s->gidx = n; \
4993 s->x = (X); \
4995 if (n == 0) \
4996 first_s = s; \
4998 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
5001 ++START; \
5002 s = first_s; \
5003 } while (0)
5006 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
5007 of AREA of glyph row ROW on window W between indices START and END.
5008 HL overrides the face for drawing glyph strings, e.g. it is
5009 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
5010 x-positions of the drawing area.
5012 This is an ugly monster macro construct because we must use alloca
5013 to allocate glyph strings (because x_draw_glyphs can be called
5014 asynchronously). */
5016 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
5017 do \
5019 HEAD = TAIL = NULL; \
5020 while (START < END) \
5022 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
5023 switch (first_glyph->type) \
5025 case CHAR_GLYPH: \
5026 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
5027 TAIL, HL, X, LAST_X, \
5028 OVERLAPS_P); \
5029 break; \
5031 case COMPOSITE_GLYPH: \
5032 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
5033 HEAD, TAIL, HL, X, LAST_X,\
5034 OVERLAPS_P); \
5035 break; \
5037 case STRETCH_GLYPH: \
5038 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
5039 HEAD, TAIL, HL, X, LAST_X); \
5040 break; \
5042 case IMAGE_GLYPH: \
5043 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
5044 TAIL, HL, X, LAST_X); \
5045 break; \
5047 default: \
5048 abort (); \
5051 x_set_glyph_string_background_width (s, START, LAST_X); \
5052 (X) += s->width; \
5055 while (0)
5058 /* Draw glyphs between START and END in AREA of ROW on window W,
5059 starting at x-position X. X is relative to AREA in W. HL is a
5060 face-override with the following meaning:
5062 DRAW_NORMAL_TEXT draw normally
5063 DRAW_CURSOR draw in cursor face
5064 DRAW_MOUSE_FACE draw in mouse face.
5065 DRAW_INVERSE_VIDEO draw in mode line face
5066 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
5067 DRAW_IMAGE_RAISED draw an image with a raised relief around it
5069 If OVERLAPS_P is non-zero, draw only the foreground of characters
5070 and clip to the physical height of ROW.
5072 Value is the x-position reached, relative to AREA of W. */
5074 static int
5075 x_draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
5076 struct window *w;
5077 int x;
5078 struct glyph_row *row;
5079 enum glyph_row_area area;
5080 int start, end;
5081 enum draw_glyphs_face hl;
5082 int overlaps_p;
5084 struct glyph_string *head, *tail;
5085 struct glyph_string *s;
5086 int last_x, area_width;
5087 int x_reached;
5088 int i, j;
5090 /* Let's rather be paranoid than getting a SEGV. */
5091 end = min (end, row->used[area]);
5092 start = max (0, start);
5093 start = min (end, start);
5095 /* Translate X to frame coordinates. Set last_x to the right
5096 end of the drawing area. */
5097 if (row->full_width_p)
5099 /* X is relative to the left edge of W, without scroll bars
5100 or fringes. */
5101 struct frame *f = XFRAME (w->frame);
5102 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
5104 x += window_left_x;
5105 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
5106 last_x = window_left_x + area_width;
5108 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5110 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5111 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
5112 last_x += width;
5113 else
5114 x -= width;
5117 x += FRAME_INTERNAL_BORDER_WIDTH (f);
5118 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
5120 else
5122 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
5123 area_width = window_box_width (w, area);
5124 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
5127 /* Build a doubly-linked list of glyph_string structures between
5128 head and tail from what we have to draw. Note that the macro
5129 BUILD_GLYPH_STRINGS will modify its start parameter. That's
5130 the reason we use a separate variable `i'. */
5131 i = start;
5132 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
5133 overlaps_p);
5134 if (tail)
5135 x_reached = tail->x + tail->background_width;
5136 else
5137 x_reached = x;
5139 /* If there are any glyphs with lbearing < 0 or rbearing > width in
5140 the row, redraw some glyphs in front or following the glyph
5141 strings built above. */
5142 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
5144 int dummy_x = 0;
5145 struct glyph_string *h, *t;
5147 /* Compute overhangs for all glyph strings. */
5148 for (s = head; s; s = s->next)
5149 x_compute_glyph_string_overhangs (s);
5151 /* Prepend glyph strings for glyphs in front of the first glyph
5152 string that are overwritten because of the first glyph
5153 string's left overhang. The background of all strings
5154 prepended must be drawn because the first glyph string
5155 draws over it. */
5156 i = x_left_overwritten (head);
5157 if (i >= 0)
5159 j = i;
5160 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
5161 DRAW_NORMAL_TEXT, dummy_x, last_x,
5162 overlaps_p);
5163 start = i;
5164 x_compute_overhangs_and_x (t, head->x, 1);
5165 x_prepend_glyph_string_lists (&head, &tail, h, t);
5168 /* Prepend glyph strings for glyphs in front of the first glyph
5169 string that overwrite that glyph string because of their
5170 right overhang. For these strings, only the foreground must
5171 be drawn, because it draws over the glyph string at `head'.
5172 The background must not be drawn because this would overwrite
5173 right overhangs of preceding glyphs for which no glyph
5174 strings exist. */
5175 i = x_left_overwriting (head);
5176 if (i >= 0)
5178 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
5179 DRAW_NORMAL_TEXT, dummy_x, last_x,
5180 overlaps_p);
5181 for (s = h; s; s = s->next)
5182 s->background_filled_p = 1;
5183 x_compute_overhangs_and_x (t, head->x, 1);
5184 x_prepend_glyph_string_lists (&head, &tail, h, t);
5187 /* Append glyphs strings for glyphs following the last glyph
5188 string tail that are overwritten by tail. The background of
5189 these strings has to be drawn because tail's foreground draws
5190 over it. */
5191 i = x_right_overwritten (tail);
5192 if (i >= 0)
5194 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5195 DRAW_NORMAL_TEXT, x, last_x,
5196 overlaps_p);
5197 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5198 x_append_glyph_string_lists (&head, &tail, h, t);
5201 /* Append glyph strings for glyphs following the last glyph
5202 string tail that overwrite tail. The foreground of such
5203 glyphs has to be drawn because it writes into the background
5204 of tail. The background must not be drawn because it could
5205 paint over the foreground of following glyphs. */
5206 i = x_right_overwriting (tail);
5207 if (i >= 0)
5209 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5210 DRAW_NORMAL_TEXT, x, last_x,
5211 overlaps_p);
5212 for (s = h; s; s = s->next)
5213 s->background_filled_p = 1;
5214 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5215 x_append_glyph_string_lists (&head, &tail, h, t);
5219 /* Draw all strings. */
5220 for (s = head; s; s = s->next)
5221 x_draw_glyph_string (s);
5223 if (area == TEXT_AREA
5224 && !row->full_width_p
5225 /* When drawing overlapping rows, only the glyph strings'
5226 foreground is drawn, which doesn't erase a cursor
5227 completely. */
5228 && !overlaps_p)
5230 int x0 = head ? head->x : x;
5231 int x1 = tail ? tail->x + tail->background_width : x;
5233 x0 = FRAME_TO_WINDOW_PIXEL_X (w, x0);
5234 x1 = FRAME_TO_WINDOW_PIXEL_X (w, x1);
5236 if (XFASTINT (w->left_margin_width) != 0)
5238 int left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
5239 x0 -= left_area_width;
5240 x1 -= left_area_width;
5243 notice_overwritten_cursor (w, area, x0, x1,
5244 row->y, MATRIX_ROW_BOTTOM_Y (row));
5247 /* Value is the x-position up to which drawn, relative to AREA of W.
5248 This doesn't include parts drawn because of overhangs. */
5249 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5250 if (!row->full_width_p)
5252 if (area > LEFT_MARGIN_AREA && XFASTINT (w->left_margin_width) != 0)
5253 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5254 if (area > TEXT_AREA)
5255 x_reached -= window_box_width (w, TEXT_AREA);
5258 return x_reached;
5262 /* Fix the display of area AREA of overlapping row ROW in window W. */
5264 static void
5265 x_fix_overlapping_area (w, row, area)
5266 struct window *w;
5267 struct glyph_row *row;
5268 enum glyph_row_area area;
5270 int i, x;
5272 BLOCK_INPUT;
5274 if (area == LEFT_MARGIN_AREA)
5275 x = 0;
5276 else if (area == TEXT_AREA)
5277 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5278 else
5279 x = (window_box_width (w, LEFT_MARGIN_AREA)
5280 + window_box_width (w, TEXT_AREA));
5282 for (i = 0; i < row->used[area];)
5284 if (row->glyphs[area][i].overlaps_vertically_p)
5286 int start = i, start_x = x;
5290 x += row->glyphs[area][i].pixel_width;
5291 ++i;
5293 while (i < row->used[area]
5294 && row->glyphs[area][i].overlaps_vertically_p);
5296 x_draw_glyphs (w, start_x, row, area, start, i,
5297 DRAW_NORMAL_TEXT, 1);
5299 else
5301 x += row->glyphs[area][i].pixel_width;
5302 ++i;
5306 UNBLOCK_INPUT;
5310 /* Output LEN glyphs starting at START at the nominal cursor position.
5311 Advance the nominal cursor over the text. The global variable
5312 updated_window contains the window being updated, updated_row is
5313 the glyph row being updated, and updated_area is the area of that
5314 row being updated. */
5316 static void
5317 x_write_glyphs (start, len)
5318 struct glyph *start;
5319 int len;
5321 int x, hpos;
5323 xassert (updated_window && updated_row);
5324 BLOCK_INPUT;
5326 /* Write glyphs. */
5328 hpos = start - updated_row->glyphs[updated_area];
5329 x = x_draw_glyphs (updated_window, output_cursor.x,
5330 updated_row, updated_area,
5331 hpos, hpos + len,
5332 DRAW_NORMAL_TEXT, 0);
5334 UNBLOCK_INPUT;
5336 /* Advance the output cursor. */
5337 output_cursor.hpos += len;
5338 output_cursor.x = x;
5342 /* Insert LEN glyphs from START at the nominal cursor position. */
5344 static void
5345 x_insert_glyphs (start, len)
5346 struct glyph *start;
5347 register int len;
5349 struct frame *f;
5350 struct window *w;
5351 int line_height, shift_by_width, shifted_region_width;
5352 struct glyph_row *row;
5353 struct glyph *glyph;
5354 int frame_x, frame_y, hpos;
5356 xassert (updated_window && updated_row);
5357 BLOCK_INPUT;
5358 w = updated_window;
5359 f = XFRAME (WINDOW_FRAME (w));
5361 /* Get the height of the line we are in. */
5362 row = updated_row;
5363 line_height = row->height;
5365 /* Get the width of the glyphs to insert. */
5366 shift_by_width = 0;
5367 for (glyph = start; glyph < start + len; ++glyph)
5368 shift_by_width += glyph->pixel_width;
5370 /* Get the width of the region to shift right. */
5371 shifted_region_width = (window_box_width (w, updated_area)
5372 - output_cursor.x
5373 - shift_by_width);
5375 /* Shift right. */
5376 frame_x = window_box_left (w, updated_area) + output_cursor.x;
5377 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5378 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5379 f->output_data.x->normal_gc,
5380 frame_x, frame_y,
5381 shifted_region_width, line_height,
5382 frame_x + shift_by_width, frame_y);
5384 /* Write the glyphs. */
5385 hpos = start - row->glyphs[updated_area];
5386 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5387 DRAW_NORMAL_TEXT, 0);
5389 /* Advance the output cursor. */
5390 output_cursor.hpos += len;
5391 output_cursor.x += shift_by_width;
5392 UNBLOCK_INPUT;
5396 /* Delete N glyphs at the nominal cursor position. Not implemented
5397 for X frames. */
5399 static void
5400 x_delete_glyphs (n)
5401 register int n;
5403 abort ();
5407 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5408 If they are <= 0, this is probably an error. */
5410 void
5411 x_clear_area (dpy, window, x, y, width, height, exposures)
5412 Display *dpy;
5413 Window window;
5414 int x, y;
5415 int width, height;
5416 int exposures;
5418 xassert (width > 0 && height > 0);
5419 XClearArea (dpy, window, x, y, width, height, exposures);
5423 /* Erase the current text line from the nominal cursor position
5424 (inclusive) to pixel column TO_X (exclusive). The idea is that
5425 everything from TO_X onward is already erased.
5427 TO_X is a pixel position relative to updated_area of
5428 updated_window. TO_X == -1 means clear to the end of this area. */
5430 static void
5431 x_clear_end_of_line (to_x)
5432 int to_x;
5434 struct frame *f;
5435 struct window *w = updated_window;
5436 int max_x, min_y, max_y;
5437 int from_x, from_y, to_y;
5439 xassert (updated_window && updated_row);
5440 f = XFRAME (w->frame);
5442 if (updated_row->full_width_p)
5444 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5445 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5446 && !w->pseudo_window_p)
5447 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5449 else
5450 max_x = window_box_width (w, updated_area);
5451 max_y = window_text_bottom_y (w);
5453 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5454 of window. For TO_X > 0, truncate to end of drawing area. */
5455 if (to_x == 0)
5456 return;
5457 else if (to_x < 0)
5458 to_x = max_x;
5459 else
5460 to_x = min (to_x, max_x);
5462 to_y = min (max_y, output_cursor.y + updated_row->height);
5464 /* Notice if the cursor will be cleared by this operation. */
5465 if (!updated_row->full_width_p)
5466 notice_overwritten_cursor (w, updated_area,
5467 output_cursor.x, -1,
5468 updated_row->y,
5469 MATRIX_ROW_BOTTOM_Y (updated_row));
5471 from_x = output_cursor.x;
5473 /* Translate to frame coordinates. */
5474 if (updated_row->full_width_p)
5476 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5477 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5479 else
5481 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5482 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5485 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5486 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5487 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5489 /* Prevent inadvertently clearing to end of the X window. */
5490 if (to_x > from_x && to_y > from_y)
5492 BLOCK_INPUT;
5493 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5494 from_x, from_y, to_x - from_x, to_y - from_y,
5495 False);
5496 UNBLOCK_INPUT;
5501 /* Clear entire frame. If updating_frame is non-null, clear that
5502 frame. Otherwise clear the selected frame. */
5504 static void
5505 x_clear_frame ()
5507 struct frame *f;
5509 if (updating_frame)
5510 f = updating_frame;
5511 else
5512 f = SELECTED_FRAME ();
5514 /* Clearing the frame will erase any cursor, so mark them all as no
5515 longer visible. */
5516 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5517 output_cursor.hpos = output_cursor.vpos = 0;
5518 output_cursor.x = -1;
5520 /* We don't set the output cursor here because there will always
5521 follow an explicit cursor_to. */
5522 BLOCK_INPUT;
5523 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5525 /* We have to clear the scroll bars, too. If we have changed
5526 colors or something like that, then they should be notified. */
5527 x_scroll_bar_clear (f);
5529 XFlush (FRAME_X_DISPLAY (f));
5530 UNBLOCK_INPUT;
5535 /* Invert the middle quarter of the frame for .15 sec. */
5537 /* We use the select system call to do the waiting, so we have to make
5538 sure it's available. If it isn't, we just won't do visual bells. */
5540 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5543 /* Subtract the `struct timeval' values X and Y, storing the result in
5544 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5546 static int
5547 timeval_subtract (result, x, y)
5548 struct timeval *result, x, y;
5550 /* Perform the carry for the later subtraction by updating y. This
5551 is safer because on some systems the tv_sec member is unsigned. */
5552 if (x.tv_usec < y.tv_usec)
5554 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5555 y.tv_usec -= 1000000 * nsec;
5556 y.tv_sec += nsec;
5559 if (x.tv_usec - y.tv_usec > 1000000)
5561 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5562 y.tv_usec += 1000000 * nsec;
5563 y.tv_sec -= nsec;
5566 /* Compute the time remaining to wait. tv_usec is certainly
5567 positive. */
5568 result->tv_sec = x.tv_sec - y.tv_sec;
5569 result->tv_usec = x.tv_usec - y.tv_usec;
5571 /* Return indication of whether the result should be considered
5572 negative. */
5573 return x.tv_sec < y.tv_sec;
5576 void
5577 XTflash (f)
5578 struct frame *f;
5580 BLOCK_INPUT;
5583 GC gc;
5585 /* Create a GC that will use the GXxor function to flip foreground
5586 pixels into background pixels. */
5588 XGCValues values;
5590 values.function = GXxor;
5591 values.foreground = (f->output_data.x->foreground_pixel
5592 ^ f->output_data.x->background_pixel);
5594 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5595 GCFunction | GCForeground, &values);
5599 /* Get the height not including a menu bar widget. */
5600 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5601 /* Height of each line to flash. */
5602 int flash_height = FRAME_LINE_HEIGHT (f);
5603 /* These will be the left and right margins of the rectangles. */
5604 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5605 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5607 int width;
5609 /* Don't flash the area between a scroll bar and the frame
5610 edge it is next to. */
5611 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5613 case vertical_scroll_bar_left:
5614 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5615 break;
5617 case vertical_scroll_bar_right:
5618 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5619 break;
5621 default:
5622 break;
5625 width = flash_right - flash_left;
5627 /* If window is tall, flash top and bottom line. */
5628 if (height > 3 * FRAME_LINE_HEIGHT (f))
5630 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5631 flash_left,
5632 (FRAME_INTERNAL_BORDER_WIDTH (f)
5633 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5634 width, flash_height);
5635 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5636 flash_left,
5637 (height - flash_height
5638 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5639 width, flash_height);
5641 else
5642 /* If it is short, flash it all. */
5643 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5644 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5645 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5647 x_flush (f);
5650 struct timeval wakeup;
5652 EMACS_GET_TIME (wakeup);
5654 /* Compute time to wait until, propagating carry from usecs. */
5655 wakeup.tv_usec += 150000;
5656 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5657 wakeup.tv_usec %= 1000000;
5659 /* Keep waiting until past the time wakeup or any input gets
5660 available. */
5661 while (! detect_input_pending ())
5663 struct timeval current;
5664 struct timeval timeout;
5666 EMACS_GET_TIME (current);
5668 /* Break if result would be negative. */
5669 if (timeval_subtract (&current, wakeup, current))
5670 break;
5672 /* How long `select' should wait. */
5673 timeout.tv_sec = 0;
5674 timeout.tv_usec = 10000;
5676 /* Try to wait that long--but we might wake up sooner. */
5677 select (0, NULL, NULL, NULL, &timeout);
5681 /* If window is tall, flash top and bottom line. */
5682 if (height > 3 * FRAME_LINE_HEIGHT (f))
5684 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5685 flash_left,
5686 (FRAME_INTERNAL_BORDER_WIDTH (f)
5687 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5688 width, flash_height);
5689 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5690 flash_left,
5691 (height - flash_height
5692 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5693 width, flash_height);
5695 else
5696 /* If it is short, flash it all. */
5697 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5698 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5699 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5701 XFreeGC (FRAME_X_DISPLAY (f), gc);
5702 x_flush (f);
5706 UNBLOCK_INPUT;
5709 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5712 /* Make audible bell. */
5714 void
5715 XTring_bell ()
5717 struct frame *f = SELECTED_FRAME ();
5719 if (FRAME_X_DISPLAY (f))
5721 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5722 if (visible_bell)
5723 XTflash (f);
5724 else
5725 #endif
5727 BLOCK_INPUT;
5728 XBell (FRAME_X_DISPLAY (f), 0);
5729 XFlush (FRAME_X_DISPLAY (f));
5730 UNBLOCK_INPUT;
5736 /* Specify how many text lines, from the top of the window,
5737 should be affected by insert-lines and delete-lines operations.
5738 This, and those operations, are used only within an update
5739 that is bounded by calls to x_update_begin and x_update_end. */
5741 static void
5742 XTset_terminal_window (n)
5743 register int n;
5745 /* This function intentionally left blank. */
5750 /***********************************************************************
5751 Line Dance
5752 ***********************************************************************/
5754 /* Perform an insert-lines or delete-lines operation, inserting N
5755 lines or deleting -N lines at vertical position VPOS. */
5757 static void
5758 x_ins_del_lines (vpos, n)
5759 int vpos, n;
5761 abort ();
5765 /* Scroll part of the display as described by RUN. */
5767 static void
5768 x_scroll_run (w, run)
5769 struct window *w;
5770 struct run *run;
5772 struct frame *f = XFRAME (w->frame);
5773 int x, y, width, height, from_y, to_y, bottom_y;
5775 /* Get frame-relative bounding box of the text display area of W,
5776 without mode lines. Include in this box the left and right
5777 fringe of W. */
5778 window_box (w, -1, &x, &y, &width, &height);
5779 width += FRAME_X_FRINGE_WIDTH (f);
5780 x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
5782 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5783 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5784 bottom_y = y + height;
5786 if (to_y < from_y)
5788 /* Scrolling up. Make sure we don't copy part of the mode
5789 line at the bottom. */
5790 if (from_y + run->height > bottom_y)
5791 height = bottom_y - from_y;
5792 else
5793 height = run->height;
5795 else
5797 /* Scolling down. Make sure we don't copy over the mode line.
5798 at the bottom. */
5799 if (to_y + run->height > bottom_y)
5800 height = bottom_y - to_y;
5801 else
5802 height = run->height;
5805 BLOCK_INPUT;
5807 /* Cursor off. Will be switched on again in x_update_window_end. */
5808 updated_window = w;
5809 x_clear_cursor (w);
5811 XCopyArea (FRAME_X_DISPLAY (f),
5812 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5813 f->output_data.x->normal_gc,
5814 x, from_y,
5815 width, height,
5816 x, to_y);
5818 UNBLOCK_INPUT;
5823 /***********************************************************************
5824 Exposure Events
5825 ***********************************************************************/
5827 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5828 corner of the exposed rectangle. W and H are width and height of
5829 the exposed area. All are pixel values. W or H zero means redraw
5830 the entire frame. */
5832 static void
5833 expose_frame (f, x, y, w, h)
5834 struct frame *f;
5835 int x, y, w, h;
5837 XRectangle r;
5838 int mouse_face_overwritten_p = 0;
5840 TRACE ((stderr, "expose_frame "));
5842 /* No need to redraw if frame will be redrawn soon. */
5843 if (FRAME_GARBAGED_P (f))
5845 TRACE ((stderr, " garbaged\n"));
5846 return;
5849 /* If basic faces haven't been realized yet, there is no point in
5850 trying to redraw anything. This can happen when we get an expose
5851 event while Emacs is starting, e.g. by moving another window. */
5852 if (FRAME_FACE_CACHE (f) == NULL
5853 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5855 TRACE ((stderr, " no faces\n"));
5856 return;
5859 if (w == 0 || h == 0)
5861 r.x = r.y = 0;
5862 r.width = CANON_X_UNIT (f) * f->width;
5863 r.height = CANON_Y_UNIT (f) * f->height;
5865 else
5867 r.x = x;
5868 r.y = y;
5869 r.width = w;
5870 r.height = h;
5873 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5874 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
5876 if (WINDOWP (f->tool_bar_window))
5877 mouse_face_overwritten_p
5878 |= expose_window (XWINDOW (f->tool_bar_window), &r);
5880 #ifndef USE_X_TOOLKIT
5881 if (WINDOWP (f->menu_bar_window))
5882 mouse_face_overwritten_p
5883 |= expose_window (XWINDOW (f->menu_bar_window), &r);
5884 #endif /* not USE_X_TOOLKIT */
5886 /* Some window managers support a focus-follows-mouse style with
5887 delayed raising of frames. Imagine a partially obscured frame,
5888 and moving the mouse into partially obscured mouse-face on that
5889 frame. The visible part of the mouse-face will be highlighted,
5890 then the WM raises the obscured frame. With at least one WM, KDE
5891 2.1, Emacs is not getting any event for the raising of the frame
5892 (even tried with SubstructureRedirectMask), only Expose events.
5893 These expose events will draw text normally, i.e. not
5894 highlighted. Which means we must redo the highlight here.
5895 Subsume it under ``we love X''. --gerd 2001-08-15 */
5896 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
5898 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5899 if (f == dpyinfo->mouse_face_mouse_frame)
5901 int x = dpyinfo->mouse_face_mouse_x;
5902 int y = dpyinfo->mouse_face_mouse_y;
5903 clear_mouse_face (dpyinfo);
5904 note_mouse_highlight (f, x, y);
5910 /* Redraw (parts) of all windows in the window tree rooted at W that
5911 intersect R. R contains frame pixel coordinates. Value is
5912 non-zero if the exposure overwrites mouse-face. */
5914 static int
5915 expose_window_tree (w, r)
5916 struct window *w;
5917 XRectangle *r;
5919 struct frame *f = XFRAME (w->frame);
5920 int mouse_face_overwritten_p = 0;
5922 while (w && !FRAME_GARBAGED_P (f))
5924 if (!NILP (w->hchild))
5925 mouse_face_overwritten_p
5926 |= expose_window_tree (XWINDOW (w->hchild), r);
5927 else if (!NILP (w->vchild))
5928 mouse_face_overwritten_p
5929 |= expose_window_tree (XWINDOW (w->vchild), r);
5930 else
5931 mouse_face_overwritten_p |= expose_window (w, r);
5933 w = NILP (w->next) ? NULL : XWINDOW (w->next);
5936 return mouse_face_overwritten_p;
5940 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5941 which intersects rectangle R. R is in window-relative coordinates. */
5943 static void
5944 expose_area (w, row, r, area)
5945 struct window *w;
5946 struct glyph_row *row;
5947 XRectangle *r;
5948 enum glyph_row_area area;
5950 struct glyph *first = row->glyphs[area];
5951 struct glyph *end = row->glyphs[area] + row->used[area];
5952 struct glyph *last;
5953 int first_x, start_x, x;
5955 if (area == TEXT_AREA && row->fill_line_p)
5956 /* If row extends face to end of line write the whole line. */
5957 x_draw_glyphs (w, 0, row, area, 0, row->used[area],
5958 DRAW_NORMAL_TEXT, 0);
5959 else
5961 /* Set START_X to the window-relative start position for drawing glyphs of
5962 AREA. The first glyph of the text area can be partially visible.
5963 The first glyphs of other areas cannot. */
5964 if (area == LEFT_MARGIN_AREA)
5965 start_x = 0;
5966 else if (area == TEXT_AREA)
5967 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5968 else
5969 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5970 + window_box_width (w, TEXT_AREA));
5971 x = start_x;
5973 /* Find the first glyph that must be redrawn. */
5974 while (first < end
5975 && x + first->pixel_width < r->x)
5977 x += first->pixel_width;
5978 ++first;
5981 /* Find the last one. */
5982 last = first;
5983 first_x = x;
5984 while (last < end
5985 && x < r->x + r->width)
5987 x += last->pixel_width;
5988 ++last;
5991 /* Repaint. */
5992 if (last > first)
5993 x_draw_glyphs (w, first_x - start_x, row, area,
5994 first - row->glyphs[area],
5995 last - row->glyphs[area],
5996 DRAW_NORMAL_TEXT, 0);
6001 /* Redraw the parts of the glyph row ROW on window W intersecting
6002 rectangle R. R is in window-relative coordinates. Value is
6003 non-zero if mouse-face was overwritten. */
6005 static int
6006 expose_line (w, row, r)
6007 struct window *w;
6008 struct glyph_row *row;
6009 XRectangle *r;
6011 xassert (row->enabled_p);
6013 if (row->mode_line_p || w->pseudo_window_p)
6014 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
6015 DRAW_NORMAL_TEXT, 0);
6016 else
6018 if (row->used[LEFT_MARGIN_AREA])
6019 expose_area (w, row, r, LEFT_MARGIN_AREA);
6020 if (row->used[TEXT_AREA])
6021 expose_area (w, row, r, TEXT_AREA);
6022 if (row->used[RIGHT_MARGIN_AREA])
6023 expose_area (w, row, r, RIGHT_MARGIN_AREA);
6024 x_draw_row_fringe_bitmaps (w, row);
6027 return row->mouse_face_p;
6031 /* Return non-zero if W's cursor intersects rectangle R. */
6033 static int
6034 x_phys_cursor_in_rect_p (w, r)
6035 struct window *w;
6036 XRectangle *r;
6038 XRectangle cr, result;
6039 struct glyph *cursor_glyph;
6041 cursor_glyph = get_phys_cursor_glyph (w);
6042 if (cursor_glyph)
6044 cr.x = w->phys_cursor.x;
6045 cr.y = w->phys_cursor.y;
6046 cr.width = cursor_glyph->pixel_width;
6047 cr.height = w->phys_cursor_height;
6048 return x_intersect_rectangles (&cr, r, &result);
6050 else
6051 return 0;
6055 /* Redraw the part of window W intersection rectangle FR. Pixel
6056 coordinates in FR are frame-relative. Call this function with
6057 input blocked. Value is non-zero if the exposure overwrites
6058 mouse-face. */
6060 static int
6061 expose_window (w, fr)
6062 struct window *w;
6063 XRectangle *fr;
6065 struct frame *f = XFRAME (w->frame);
6066 XRectangle wr, r;
6067 int mouse_face_overwritten_p = 0;
6069 /* If window is not yet fully initialized, do nothing. This can
6070 happen when toolkit scroll bars are used and a window is split.
6071 Reconfiguring the scroll bar will generate an expose for a newly
6072 created window. */
6073 if (w->current_matrix == NULL)
6074 return 0;
6076 /* When we're currently updating the window, display and current
6077 matrix usually don't agree. Arrange for a thorough display
6078 later. */
6079 if (w == updated_window)
6081 SET_FRAME_GARBAGED (f);
6082 return 0;
6085 /* Frame-relative pixel rectangle of W. */
6086 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
6087 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
6088 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
6089 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
6091 if (x_intersect_rectangles (fr, &wr, &r))
6093 int yb = window_text_bottom_y (w);
6094 struct glyph_row *row;
6095 int cursor_cleared_p;
6097 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
6098 r.x, r.y, r.width, r.height));
6100 /* Convert to window coordinates. */
6101 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
6102 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
6104 /* Turn off the cursor. */
6105 if (!w->pseudo_window_p
6106 && x_phys_cursor_in_rect_p (w, &r))
6108 x_clear_cursor (w);
6109 cursor_cleared_p = 1;
6111 else
6112 cursor_cleared_p = 0;
6114 /* Find the first row intersecting the rectangle R. */
6115 for (row = w->current_matrix->rows;
6116 row->enabled_p;
6117 ++row)
6119 int y0 = row->y;
6120 int y1 = MATRIX_ROW_BOTTOM_Y (row);
6122 if ((y0 >= r.y && y0 < r.y + r.height)
6123 || (y1 > r.y && y1 < r.y + r.height)
6124 || (r.y >= y0 && r.y < y1)
6125 || (r.y + r.height > y0 && r.y + r.height < y1))
6127 if (expose_line (w, row, &r))
6128 mouse_face_overwritten_p = 1;
6131 if (y1 >= yb)
6132 break;
6135 /* Display the mode line if there is one. */
6136 if (WINDOW_WANTS_MODELINE_P (w)
6137 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
6138 row->enabled_p)
6139 && row->y < r.y + r.height)
6141 if (expose_line (w, row, &r))
6142 mouse_face_overwritten_p = 1;
6145 if (!w->pseudo_window_p)
6147 /* Draw border between windows. */
6148 x_draw_vertical_border (w);
6150 /* Turn the cursor on again. */
6151 if (cursor_cleared_p)
6152 x_update_window_cursor (w, 1);
6156 return mouse_face_overwritten_p;
6160 /* Determine the intersection of two rectangles R1 and R2. Return
6161 the intersection in *RESULT. Value is non-zero if RESULT is not
6162 empty. */
6164 static int
6165 x_intersect_rectangles (r1, r2, result)
6166 XRectangle *r1, *r2, *result;
6168 XRectangle *left, *right;
6169 XRectangle *upper, *lower;
6170 int intersection_p = 0;
6172 /* Rearrange so that R1 is the left-most rectangle. */
6173 if (r1->x < r2->x)
6174 left = r1, right = r2;
6175 else
6176 left = r2, right = r1;
6178 /* X0 of the intersection is right.x0, if this is inside R1,
6179 otherwise there is no intersection. */
6180 if (right->x <= left->x + left->width)
6182 result->x = right->x;
6184 /* The right end of the intersection is the minimum of the
6185 the right ends of left and right. */
6186 result->width = (min (left->x + left->width, right->x + right->width)
6187 - result->x);
6189 /* Same game for Y. */
6190 if (r1->y < r2->y)
6191 upper = r1, lower = r2;
6192 else
6193 upper = r2, lower = r1;
6195 /* The upper end of the intersection is lower.y0, if this is inside
6196 of upper. Otherwise, there is no intersection. */
6197 if (lower->y <= upper->y + upper->height)
6199 result->y = lower->y;
6201 /* The lower end of the intersection is the minimum of the lower
6202 ends of upper and lower. */
6203 result->height = (min (lower->y + lower->height,
6204 upper->y + upper->height)
6205 - result->y);
6206 intersection_p = 1;
6210 return intersection_p;
6217 static void
6218 frame_highlight (f)
6219 struct frame *f;
6221 /* We used to only do this if Vx_no_window_manager was non-nil, but
6222 the ICCCM (section 4.1.6) says that the window's border pixmap
6223 and border pixel are window attributes which are "private to the
6224 client", so we can always change it to whatever we want. */
6225 BLOCK_INPUT;
6226 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6227 f->output_data.x->border_pixel);
6228 UNBLOCK_INPUT;
6229 x_update_cursor (f, 1);
6232 static void
6233 frame_unhighlight (f)
6234 struct frame *f;
6236 /* We used to only do this if Vx_no_window_manager was non-nil, but
6237 the ICCCM (section 4.1.6) says that the window's border pixmap
6238 and border pixel are window attributes which are "private to the
6239 client", so we can always change it to whatever we want. */
6240 BLOCK_INPUT;
6241 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6242 f->output_data.x->border_tile);
6243 UNBLOCK_INPUT;
6244 x_update_cursor (f, 1);
6247 /* The focus has changed. Update the frames as necessary to reflect
6248 the new situation. Note that we can't change the selected frame
6249 here, because the Lisp code we are interrupting might become confused.
6250 Each event gets marked with the frame in which it occurred, so the
6251 Lisp code can tell when the switch took place by examining the events. */
6253 static void
6254 x_new_focus_frame (dpyinfo, frame)
6255 struct x_display_info *dpyinfo;
6256 struct frame *frame;
6258 struct frame *old_focus = dpyinfo->x_focus_frame;
6260 if (frame != dpyinfo->x_focus_frame)
6262 /* Set this before calling other routines, so that they see
6263 the correct value of x_focus_frame. */
6264 dpyinfo->x_focus_frame = frame;
6266 if (old_focus && old_focus->auto_lower)
6267 x_lower_frame (old_focus);
6269 #if 0
6270 selected_frame = frame;
6271 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6272 selected_frame);
6273 Fselect_window (selected_frame->selected_window);
6274 choose_minibuf_frame ();
6275 #endif /* ! 0 */
6277 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6278 pending_autoraise_frame = dpyinfo->x_focus_frame;
6279 else
6280 pending_autoraise_frame = 0;
6283 x_frame_rehighlight (dpyinfo);
6286 /* Handle an event saying the mouse has moved out of an Emacs frame. */
6288 void
6289 x_mouse_leave (dpyinfo)
6290 struct x_display_info *dpyinfo;
6292 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
6295 /* The focus has changed, or we have redirected a frame's focus to
6296 another frame (this happens when a frame uses a surrogate
6297 mini-buffer frame). Shift the highlight as appropriate.
6299 The FRAME argument doesn't necessarily have anything to do with which
6300 frame is being highlighted or un-highlighted; we only use it to find
6301 the appropriate X display info. */
6303 static void
6304 XTframe_rehighlight (frame)
6305 struct frame *frame;
6307 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6310 static void
6311 x_frame_rehighlight (dpyinfo)
6312 struct x_display_info *dpyinfo;
6314 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6316 if (dpyinfo->x_focus_frame)
6318 dpyinfo->x_highlight_frame
6319 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6320 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6321 : dpyinfo->x_focus_frame);
6322 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
6324 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6325 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
6328 else
6329 dpyinfo->x_highlight_frame = 0;
6331 if (dpyinfo->x_highlight_frame != old_highlight)
6333 if (old_highlight)
6334 frame_unhighlight (old_highlight);
6335 if (dpyinfo->x_highlight_frame)
6336 frame_highlight (dpyinfo->x_highlight_frame);
6342 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
6344 /* Initialize mode_switch_bit and modifier_meaning. */
6345 static void
6346 x_find_modifier_meanings (dpyinfo)
6347 struct x_display_info *dpyinfo;
6349 int min_code, max_code;
6350 KeySym *syms;
6351 int syms_per_code;
6352 XModifierKeymap *mods;
6354 dpyinfo->meta_mod_mask = 0;
6355 dpyinfo->shift_lock_mask = 0;
6356 dpyinfo->alt_mod_mask = 0;
6357 dpyinfo->super_mod_mask = 0;
6358 dpyinfo->hyper_mod_mask = 0;
6360 #ifdef HAVE_X11R4
6361 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
6362 #else
6363 min_code = dpyinfo->display->min_keycode;
6364 max_code = dpyinfo->display->max_keycode;
6365 #endif
6367 syms = XGetKeyboardMapping (dpyinfo->display,
6368 min_code, max_code - min_code + 1,
6369 &syms_per_code);
6370 mods = XGetModifierMapping (dpyinfo->display);
6372 /* Scan the modifier table to see which modifier bits the Meta and
6373 Alt keysyms are on. */
6375 int row, col; /* The row and column in the modifier table. */
6377 for (row = 3; row < 8; row++)
6378 for (col = 0; col < mods->max_keypermod; col++)
6380 KeyCode code
6381 = mods->modifiermap[(row * mods->max_keypermod) + col];
6383 /* Zeroes are used for filler. Skip them. */
6384 if (code == 0)
6385 continue;
6387 /* Are any of this keycode's keysyms a meta key? */
6389 int code_col;
6391 for (code_col = 0; code_col < syms_per_code; code_col++)
6393 int sym = syms[((code - min_code) * syms_per_code) + code_col];
6395 switch (sym)
6397 case XK_Meta_L:
6398 case XK_Meta_R:
6399 dpyinfo->meta_mod_mask |= (1 << row);
6400 break;
6402 case XK_Alt_L:
6403 case XK_Alt_R:
6404 dpyinfo->alt_mod_mask |= (1 << row);
6405 break;
6407 case XK_Hyper_L:
6408 case XK_Hyper_R:
6409 dpyinfo->hyper_mod_mask |= (1 << row);
6410 break;
6412 case XK_Super_L:
6413 case XK_Super_R:
6414 dpyinfo->super_mod_mask |= (1 << row);
6415 break;
6417 case XK_Shift_Lock:
6418 /* Ignore this if it's not on the lock modifier. */
6419 if ((1 << row) == LockMask)
6420 dpyinfo->shift_lock_mask = LockMask;
6421 break;
6428 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
6429 if (! dpyinfo->meta_mod_mask)
6431 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6432 dpyinfo->alt_mod_mask = 0;
6435 /* If some keys are both alt and meta,
6436 make them just meta, not alt. */
6437 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6439 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6442 XFree ((char *) syms);
6443 XFreeModifiermap (mods);
6446 /* Convert between the modifier bits X uses and the modifier bits
6447 Emacs uses. */
6449 static unsigned int
6450 x_x_to_emacs_modifiers (dpyinfo, state)
6451 struct x_display_info *dpyinfo;
6452 unsigned int state;
6454 EMACS_UINT mod_meta = meta_modifier;
6455 EMACS_UINT mod_alt = alt_modifier;
6456 EMACS_UINT mod_hyper = hyper_modifier;
6457 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 & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6471 | ((state & ControlMask) ? ctrl_modifier : 0)
6472 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
6473 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
6474 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
6475 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
6478 static unsigned int
6479 x_emacs_to_x_modifiers (dpyinfo, state)
6480 struct x_display_info *dpyinfo;
6481 unsigned int state;
6483 EMACS_UINT mod_meta = meta_modifier;
6484 EMACS_UINT mod_alt = alt_modifier;
6485 EMACS_UINT mod_hyper = hyper_modifier;
6486 EMACS_UINT mod_super = super_modifier;
6488 Lisp_Object tem;
6490 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6491 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6492 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6493 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6494 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6495 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6496 tem = Fget (Vx_super_keysym, Qmodifier_value);
6497 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
6500 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
6501 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
6502 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
6503 | ((state & shift_modifier) ? ShiftMask : 0)
6504 | ((state & ctrl_modifier) ? ControlMask : 0)
6505 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
6508 /* Convert a keysym to its name. */
6510 char *
6511 x_get_keysym_name (keysym)
6512 KeySym keysym;
6514 char *value;
6516 BLOCK_INPUT;
6517 value = XKeysymToString (keysym);
6518 UNBLOCK_INPUT;
6520 return value;
6525 /* Mouse clicks and mouse movement. Rah. */
6527 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6528 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6529 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6530 not force the value into range. */
6532 void
6533 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6534 FRAME_PTR f;
6535 register int pix_x, pix_y;
6536 register int *x, *y;
6537 XRectangle *bounds;
6538 int noclip;
6540 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6541 even for negative values. */
6542 if (pix_x < 0)
6543 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6544 if (pix_y < 0)
6545 pix_y -= (f)->output_data.x->line_height - 1;
6547 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6548 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6550 if (bounds)
6552 bounds->width = FONT_WIDTH (f->output_data.x->font);
6553 bounds->height = f->output_data.x->line_height;
6554 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6555 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6558 if (!noclip)
6560 if (pix_x < 0)
6561 pix_x = 0;
6562 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6563 pix_x = FRAME_WINDOW_WIDTH (f);
6565 if (pix_y < 0)
6566 pix_y = 0;
6567 else if (pix_y > f->height)
6568 pix_y = f->height;
6571 *x = pix_x;
6572 *y = pix_y;
6576 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6577 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6578 can't tell the positions because W's display is not up to date,
6579 return 0. */
6582 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6583 struct window *w;
6584 int hpos, vpos;
6585 int *frame_x, *frame_y;
6587 int success_p;
6589 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6590 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6592 if (display_completed)
6594 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6595 struct glyph *glyph = row->glyphs[TEXT_AREA];
6596 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6598 *frame_y = row->y;
6599 *frame_x = row->x;
6600 while (glyph < end)
6602 *frame_x += glyph->pixel_width;
6603 ++glyph;
6606 success_p = 1;
6608 else
6610 *frame_y = *frame_x = 0;
6611 success_p = 0;
6614 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6615 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6616 return success_p;
6620 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6622 If the event is a button press, then note that we have grabbed
6623 the mouse. */
6625 static Lisp_Object
6626 construct_mouse_click (result, event, f)
6627 struct input_event *result;
6628 XButtonEvent *event;
6629 struct frame *f;
6631 /* Make the event type no_event; we'll change that when we decide
6632 otherwise. */
6633 result->kind = mouse_click;
6634 result->code = event->button - Button1;
6635 result->timestamp = event->time;
6636 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6637 event->state)
6638 | (event->type == ButtonRelease
6639 ? up_modifier
6640 : down_modifier));
6642 XSETINT (result->x, event->x);
6643 XSETINT (result->y, event->y);
6644 XSETFRAME (result->frame_or_window, f);
6645 result->arg = Qnil;
6646 return Qnil;
6650 /* Function to report a mouse movement to the mainstream Emacs code.
6651 The input handler calls this.
6653 We have received a mouse movement event, which is given in *event.
6654 If the mouse is over a different glyph than it was last time, tell
6655 the mainstream emacs code by setting mouse_moved. If not, ask for
6656 another motion event, so we can check again the next time it moves. */
6658 static XMotionEvent last_mouse_motion_event;
6659 static Lisp_Object last_mouse_motion_frame;
6661 static void
6662 note_mouse_movement (frame, event)
6663 FRAME_PTR frame;
6664 XMotionEvent *event;
6666 last_mouse_movement_time = event->time;
6667 last_mouse_motion_event = *event;
6668 XSETFRAME (last_mouse_motion_frame, frame);
6670 if (event->window != FRAME_X_WINDOW (frame))
6672 frame->mouse_moved = 1;
6673 last_mouse_scroll_bar = Qnil;
6674 note_mouse_highlight (frame, -1, -1);
6677 /* Has the mouse moved off the glyph it was on at the last sighting? */
6678 else if (event->x < last_mouse_glyph.x
6679 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6680 || event->y < last_mouse_glyph.y
6681 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6683 frame->mouse_moved = 1;
6684 last_mouse_scroll_bar = Qnil;
6685 note_mouse_highlight (frame, event->x, event->y);
6690 /************************************************************************
6691 Mouse Face
6692 ************************************************************************/
6694 /* Find the glyph under window-relative coordinates X/Y in window W.
6695 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6696 strings. Return in *HPOS and *VPOS the row and column number of
6697 the glyph found. Return in *AREA the glyph area containing X.
6698 Value is a pointer to the glyph found or null if X/Y is not on
6699 text, or we can't tell because W's current matrix is not up to
6700 date. */
6702 static struct glyph *
6703 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
6704 struct window *w;
6705 int x, y;
6706 int *hpos, *vpos, *area;
6707 int buffer_only_p;
6709 struct glyph *glyph, *end;
6710 struct glyph_row *row = NULL;
6711 int x0, i, left_area_width;
6713 /* Find row containing Y. Give up if some row is not enabled. */
6714 for (i = 0; i < w->current_matrix->nrows; ++i)
6716 row = MATRIX_ROW (w->current_matrix, i);
6717 if (!row->enabled_p)
6718 return NULL;
6719 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6720 break;
6723 *vpos = i;
6724 *hpos = 0;
6726 /* Give up if Y is not in the window. */
6727 if (i == w->current_matrix->nrows)
6728 return NULL;
6730 /* Get the glyph area containing X. */
6731 if (w->pseudo_window_p)
6733 *area = TEXT_AREA;
6734 x0 = 0;
6736 else
6738 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6739 if (x < left_area_width)
6741 *area = LEFT_MARGIN_AREA;
6742 x0 = 0;
6744 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6746 *area = TEXT_AREA;
6747 x0 = row->x + left_area_width;
6749 else
6751 *area = RIGHT_MARGIN_AREA;
6752 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6756 /* Find glyph containing X. */
6757 glyph = row->glyphs[*area];
6758 end = glyph + row->used[*area];
6759 while (glyph < end)
6761 if (x < x0 + glyph->pixel_width)
6763 if (w->pseudo_window_p)
6764 break;
6765 else if (!buffer_only_p || BUFFERP (glyph->object))
6766 break;
6769 x0 += glyph->pixel_width;
6770 ++glyph;
6773 if (glyph == end)
6774 return NULL;
6776 *hpos = glyph - row->glyphs[*area];
6777 return glyph;
6781 /* Convert frame-relative x/y to coordinates relative to window W.
6782 Takes pseudo-windows into account. */
6784 static void
6785 frame_to_window_pixel_xy (w, x, y)
6786 struct window *w;
6787 int *x, *y;
6789 if (w->pseudo_window_p)
6791 /* A pseudo-window is always full-width, and starts at the
6792 left edge of the frame, plus a frame border. */
6793 struct frame *f = XFRAME (w->frame);
6794 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6795 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6797 else
6799 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6800 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6805 /* Take proper action when mouse has moved to the mode or header line
6806 or marginal area of window W, x-position X and y-position Y. Area
6807 is 1, 3, 6 or 7 for the mode line, header line, left and right
6808 marginal area respectively. X is relative to the start of the text
6809 display area of W, so the width of bitmap areas and scroll bars
6810 must be subtracted to get a position relative to the start of the
6811 mode line. */
6813 static void
6814 note_mode_line_or_margin_highlight (w, x, y, portion)
6815 struct window *w;
6816 int x, y, portion;
6818 struct frame *f = XFRAME (w->frame);
6819 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6820 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6821 int charpos;
6822 Lisp_Object string, help, map, pos;
6824 if (portion == 1 || portion == 3)
6825 string = mode_line_string (w, x, y, portion == 1, &charpos);
6826 else
6827 string = marginal_area_string (w, x, y, portion, &charpos);
6829 if (STRINGP (string))
6831 pos = make_number (charpos);
6833 /* If we're on a string with `help-echo' text property, arrange
6834 for the help to be displayed. This is done by setting the
6835 global variable help_echo to the help string. */
6836 help = Fget_text_property (pos, Qhelp_echo, string);
6837 if (!NILP (help))
6839 help_echo = help;
6840 XSETWINDOW (help_echo_window, w);
6841 help_echo_object = string;
6842 help_echo_pos = charpos;
6845 /* Change the mouse pointer according to what is under X/Y. */
6846 map = Fget_text_property (pos, Qlocal_map, string);
6847 if (!KEYMAPP (map))
6848 map = Fget_text_property (pos, Qkeymap, string);
6849 if (KEYMAPP (map))
6850 cursor = f->output_data.x->nontext_cursor;
6853 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6857 /* Take proper action when the mouse has moved to position X, Y on
6858 frame F as regards highlighting characters that have mouse-face
6859 properties. Also de-highlighting chars where the mouse was before.
6860 X and Y can be negative or out of range. */
6862 static void
6863 note_mouse_highlight (f, x, y)
6864 struct frame *f;
6865 int x, y;
6867 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6868 int portion;
6869 Lisp_Object window;
6870 struct window *w;
6871 Cursor cursor = None;
6872 struct buffer *b;
6874 /* When a menu is active, don't highlight because this looks odd. */
6875 #ifdef USE_X_TOOLKIT
6876 if (popup_activated ())
6877 return;
6878 #endif
6880 if (NILP (Vmouse_highlight)
6881 || !f->glyphs_initialized_p)
6882 return;
6884 dpyinfo->mouse_face_mouse_x = x;
6885 dpyinfo->mouse_face_mouse_y = y;
6886 dpyinfo->mouse_face_mouse_frame = f;
6888 if (dpyinfo->mouse_face_defer)
6889 return;
6891 if (gc_in_progress)
6893 dpyinfo->mouse_face_deferred_gc = 1;
6894 return;
6897 /* Which window is that in? */
6898 window = window_from_coordinates (f, x, y, &portion, 1);
6900 /* If we were displaying active text in another window, clear that. */
6901 if (! EQ (window, dpyinfo->mouse_face_window))
6902 clear_mouse_face (dpyinfo);
6904 /* Not on a window -> return. */
6905 if (!WINDOWP (window))
6906 return;
6908 /* Convert to window-relative pixel coordinates. */
6909 w = XWINDOW (window);
6910 frame_to_window_pixel_xy (w, &x, &y);
6912 /* Handle tool-bar window differently since it doesn't display a
6913 buffer. */
6914 if (EQ (window, f->tool_bar_window))
6916 note_tool_bar_highlight (f, x, y);
6917 return;
6920 /* Mouse is on the mode, header line or margin? */
6921 if (portion == 1 || portion == 3 || portion == 6 || portion == 7)
6923 note_mode_line_or_margin_highlight (w, x, y, portion);
6924 return;
6927 if (portion == 2)
6928 cursor = f->output_data.x->horizontal_drag_cursor;
6929 else
6930 cursor = f->output_data.x->text_cursor;
6932 /* Are we in a window whose display is up to date?
6933 And verify the buffer's text has not changed. */
6934 b = XBUFFER (w->buffer);
6935 if (/* Within text portion of the window. */
6936 portion == 0
6937 && EQ (w->window_end_valid, w->buffer)
6938 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
6939 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
6941 int hpos, vpos, pos, i, area;
6942 struct glyph *glyph;
6943 Lisp_Object object;
6944 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
6945 Lisp_Object *overlay_vec = NULL;
6946 int len, noverlays;
6947 struct buffer *obuf;
6948 int obegv, ozv, same_region;
6950 /* Find the glyph under X/Y. */
6951 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
6953 /* Clear mouse face if X/Y not over text. */
6954 if (glyph == NULL
6955 || area != TEXT_AREA
6956 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6958 if (clear_mouse_face (dpyinfo))
6959 cursor = None;
6960 goto set_cursor;
6963 pos = glyph->charpos;
6964 object = glyph->object;
6965 if (!STRINGP (object) && !BUFFERP (object))
6966 goto set_cursor;
6968 /* If we get an out-of-range value, return now; avoid an error. */
6969 if (BUFFERP (object) && pos > BUF_Z (b))
6970 goto set_cursor;
6972 /* Make the window's buffer temporarily current for
6973 overlays_at and compute_char_face. */
6974 obuf = current_buffer;
6975 current_buffer = b;
6976 obegv = BEGV;
6977 ozv = ZV;
6978 BEGV = BEG;
6979 ZV = Z;
6981 /* Is this char mouse-active or does it have help-echo? */
6982 position = make_number (pos);
6984 if (BUFFERP (object))
6986 /* Put all the overlays we want in a vector in overlay_vec.
6987 Store the length in len. If there are more than 10, make
6988 enough space for all, and try again. */
6989 len = 10;
6990 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6991 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6992 if (noverlays > len)
6994 len = noverlays;
6995 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6996 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6999 /* Sort overlays into increasing priority order. */
7000 noverlays = sort_overlays (overlay_vec, noverlays, w);
7002 else
7003 noverlays = 0;
7005 same_region = (EQ (window, dpyinfo->mouse_face_window)
7006 && vpos >= dpyinfo->mouse_face_beg_row
7007 && vpos <= dpyinfo->mouse_face_end_row
7008 && (vpos > dpyinfo->mouse_face_beg_row
7009 || hpos >= dpyinfo->mouse_face_beg_col)
7010 && (vpos < dpyinfo->mouse_face_end_row
7011 || hpos < dpyinfo->mouse_face_end_col
7012 || dpyinfo->mouse_face_past_end));
7014 if (same_region)
7015 cursor = None;
7017 /* Check mouse-face highlighting. */
7018 if (! same_region
7019 /* If there exists an overlay with mouse-face overlapping
7020 the one we are currently highlighting, we have to
7021 check if we enter the overlapping overlay, and then
7022 highlight only that. */
7023 || (OVERLAYP (dpyinfo->mouse_face_overlay)
7024 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
7026 /* Find the highest priority overlay that has a mouse-face
7027 property. */
7028 overlay = Qnil;
7029 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
7031 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
7032 if (!NILP (mouse_face))
7033 overlay = overlay_vec[i];
7036 /* If we're actually highlighting the same overlay as
7037 before, there's no need to do that again. */
7038 if (!NILP (overlay)
7039 && EQ (overlay, dpyinfo->mouse_face_overlay))
7040 goto check_help_echo;
7042 dpyinfo->mouse_face_overlay = overlay;
7044 /* Clear the display of the old active region, if any. */
7045 if (clear_mouse_face (dpyinfo))
7046 cursor = None;
7048 /* If no overlay applies, get a text property. */
7049 if (NILP (overlay))
7050 mouse_face = Fget_text_property (position, Qmouse_face, object);
7052 /* Handle the overlay case. */
7053 if (!NILP (overlay))
7055 /* Find the range of text around this char that
7056 should be active. */
7057 Lisp_Object before, after;
7058 int ignore;
7060 before = Foverlay_start (overlay);
7061 after = Foverlay_end (overlay);
7062 /* Record this as the current active region. */
7063 fast_find_position (w, XFASTINT (before),
7064 &dpyinfo->mouse_face_beg_col,
7065 &dpyinfo->mouse_face_beg_row,
7066 &dpyinfo->mouse_face_beg_x,
7067 &dpyinfo->mouse_face_beg_y, Qnil);
7069 dpyinfo->mouse_face_past_end
7070 = !fast_find_position (w, XFASTINT (after),
7071 &dpyinfo->mouse_face_end_col,
7072 &dpyinfo->mouse_face_end_row,
7073 &dpyinfo->mouse_face_end_x,
7074 &dpyinfo->mouse_face_end_y, Qnil);
7075 dpyinfo->mouse_face_window = window;
7076 dpyinfo->mouse_face_face_id
7077 = face_at_buffer_position (w, pos, 0, 0,
7078 &ignore, pos + 1, 1);
7080 /* Display it as active. */
7081 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7082 cursor = None;
7084 /* Handle the text property case. */
7085 else if (!NILP (mouse_face) && BUFFERP (object))
7087 /* Find the range of text around this char that
7088 should be active. */
7089 Lisp_Object before, after, beginning, end;
7090 int ignore;
7092 beginning = Fmarker_position (w->start);
7093 end = make_number (BUF_Z (XBUFFER (object))
7094 - XFASTINT (w->window_end_pos));
7095 before
7096 = Fprevious_single_property_change (make_number (pos + 1),
7097 Qmouse_face,
7098 object, beginning);
7099 after
7100 = Fnext_single_property_change (position, Qmouse_face,
7101 object, end);
7103 /* Record this as the current active region. */
7104 fast_find_position (w, XFASTINT (before),
7105 &dpyinfo->mouse_face_beg_col,
7106 &dpyinfo->mouse_face_beg_row,
7107 &dpyinfo->mouse_face_beg_x,
7108 &dpyinfo->mouse_face_beg_y, Qnil);
7109 dpyinfo->mouse_face_past_end
7110 = !fast_find_position (w, XFASTINT (after),
7111 &dpyinfo->mouse_face_end_col,
7112 &dpyinfo->mouse_face_end_row,
7113 &dpyinfo->mouse_face_end_x,
7114 &dpyinfo->mouse_face_end_y, Qnil);
7115 dpyinfo->mouse_face_window = window;
7117 if (BUFFERP (object))
7118 dpyinfo->mouse_face_face_id
7119 = face_at_buffer_position (w, pos, 0, 0,
7120 &ignore, pos + 1, 1);
7122 /* Display it as active. */
7123 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7124 cursor = None;
7126 else if (!NILP (mouse_face) && STRINGP (object))
7128 Lisp_Object b, e;
7129 int ignore;
7131 b = Fprevious_single_property_change (make_number (pos + 1),
7132 Qmouse_face,
7133 object, Qnil);
7134 e = Fnext_single_property_change (position, Qmouse_face,
7135 object, Qnil);
7136 if (NILP (b))
7137 b = make_number (0);
7138 if (NILP (e))
7139 e = make_number (XSTRING (object)->size - 1);
7140 fast_find_string_pos (w, XINT (b), object,
7141 &dpyinfo->mouse_face_beg_col,
7142 &dpyinfo->mouse_face_beg_row,
7143 &dpyinfo->mouse_face_beg_x,
7144 &dpyinfo->mouse_face_beg_y, 0);
7145 fast_find_string_pos (w, XINT (e), object,
7146 &dpyinfo->mouse_face_end_col,
7147 &dpyinfo->mouse_face_end_row,
7148 &dpyinfo->mouse_face_end_x,
7149 &dpyinfo->mouse_face_end_y, 1);
7150 dpyinfo->mouse_face_past_end = 0;
7151 dpyinfo->mouse_face_window = window;
7152 dpyinfo->mouse_face_face_id
7153 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
7154 glyph->face_id, 1);
7155 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7156 cursor = None;
7158 else if (STRINGP (object) && NILP (mouse_face))
7160 /* A string which doesn't have mouse-face, but
7161 the text ``under'' it might have. */
7162 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
7163 int start = MATRIX_ROW_START_CHARPOS (r);
7165 pos = string_buffer_position (w, object, start);
7166 if (pos > 0)
7167 mouse_face = get_char_property_and_overlay (make_number (pos),
7168 Qmouse_face,
7169 w->buffer,
7170 &overlay);
7171 if (!NILP (mouse_face) && !NILP (overlay))
7173 Lisp_Object before = Foverlay_start (overlay);
7174 Lisp_Object after = Foverlay_end (overlay);
7175 int ignore;
7177 /* Note that we might not be able to find position
7178 BEFORE in the glyph matrix if the overlay is
7179 entirely covered by a `display' property. In
7180 this case, we overshoot. So let's stop in
7181 the glyph matrix before glyphs for OBJECT. */
7182 fast_find_position (w, XFASTINT (before),
7183 &dpyinfo->mouse_face_beg_col,
7184 &dpyinfo->mouse_face_beg_row,
7185 &dpyinfo->mouse_face_beg_x,
7186 &dpyinfo->mouse_face_beg_y,
7187 object);
7189 dpyinfo->mouse_face_past_end
7190 = !fast_find_position (w, XFASTINT (after),
7191 &dpyinfo->mouse_face_end_col,
7192 &dpyinfo->mouse_face_end_row,
7193 &dpyinfo->mouse_face_end_x,
7194 &dpyinfo->mouse_face_end_y,
7195 Qnil);
7196 dpyinfo->mouse_face_window = window;
7197 dpyinfo->mouse_face_face_id
7198 = face_at_buffer_position (w, pos, 0, 0,
7199 &ignore, pos + 1, 1);
7201 /* Display it as active. */
7202 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7203 cursor = None;
7208 check_help_echo:
7210 /* Look for a `help-echo' property. */
7212 Lisp_Object help, overlay;
7214 /* Check overlays first. */
7215 help = overlay = Qnil;
7216 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
7218 overlay = overlay_vec[i];
7219 help = Foverlay_get (overlay, Qhelp_echo);
7222 if (!NILP (help))
7224 help_echo = help;
7225 help_echo_window = window;
7226 help_echo_object = overlay;
7227 help_echo_pos = pos;
7229 else
7231 Lisp_Object object = glyph->object;
7232 int charpos = glyph->charpos;
7234 /* Try text properties. */
7235 if (STRINGP (object)
7236 && charpos >= 0
7237 && charpos < XSTRING (object)->size)
7239 help = Fget_text_property (make_number (charpos),
7240 Qhelp_echo, object);
7241 if (NILP (help))
7243 /* If the string itself doesn't specify a help-echo,
7244 see if the buffer text ``under'' it does. */
7245 struct glyph_row *r
7246 = MATRIX_ROW (w->current_matrix, vpos);
7247 int start = MATRIX_ROW_START_CHARPOS (r);
7248 int pos = string_buffer_position (w, object, start);
7249 if (pos > 0)
7251 help = Fget_char_property (make_number (pos),
7252 Qhelp_echo, w->buffer);
7253 if (!NILP (help))
7255 charpos = pos;
7256 object = w->buffer;
7261 else if (BUFFERP (object)
7262 && charpos >= BEGV
7263 && charpos < ZV)
7264 help = Fget_text_property (make_number (charpos), Qhelp_echo,
7265 object);
7267 if (!NILP (help))
7269 help_echo = help;
7270 help_echo_window = window;
7271 help_echo_object = object;
7272 help_echo_pos = charpos;
7277 BEGV = obegv;
7278 ZV = ozv;
7279 current_buffer = obuf;
7282 set_cursor:
7284 if (cursor != None)
7285 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7288 static void
7289 redo_mouse_highlight ()
7291 if (!NILP (last_mouse_motion_frame)
7292 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
7293 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
7294 last_mouse_motion_event.x,
7295 last_mouse_motion_event.y);
7300 /***********************************************************************
7301 Tool-bars
7302 ***********************************************************************/
7304 static int x_tool_bar_item P_ ((struct frame *, int, int,
7305 struct glyph **, int *, int *, int *));
7307 /* Tool-bar item index of the item on which a mouse button was pressed
7308 or -1. */
7310 static int last_tool_bar_item;
7313 /* Get information about the tool-bar item at position X/Y on frame F.
7314 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7315 the current matrix of the tool-bar window of F, or NULL if not
7316 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
7317 item in F->tool_bar_items. Value is
7319 -1 if X/Y is not on a tool-bar item
7320 0 if X/Y is on the same item that was highlighted before.
7321 1 otherwise. */
7323 static int
7324 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
7325 struct frame *f;
7326 int x, y;
7327 struct glyph **glyph;
7328 int *hpos, *vpos, *prop_idx;
7330 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7331 struct window *w = XWINDOW (f->tool_bar_window);
7332 int area;
7334 /* Find the glyph under X/Y. */
7335 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
7336 if (*glyph == NULL)
7337 return -1;
7339 /* Get the start of this tool-bar item's properties in
7340 f->tool_bar_items. */
7341 if (!tool_bar_item_info (f, *glyph, prop_idx))
7342 return -1;
7344 /* Is mouse on the highlighted item? */
7345 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
7346 && *vpos >= dpyinfo->mouse_face_beg_row
7347 && *vpos <= dpyinfo->mouse_face_end_row
7348 && (*vpos > dpyinfo->mouse_face_beg_row
7349 || *hpos >= dpyinfo->mouse_face_beg_col)
7350 && (*vpos < dpyinfo->mouse_face_end_row
7351 || *hpos < dpyinfo->mouse_face_end_col
7352 || dpyinfo->mouse_face_past_end))
7353 return 0;
7355 return 1;
7359 /* Handle mouse button event on the tool-bar of frame F, at
7360 frame-relative coordinates X/Y. EVENT_TYPE is either ButtonPress
7361 or ButtonRelase. */
7363 static void
7364 x_handle_tool_bar_click (f, button_event)
7365 struct frame *f;
7366 XButtonEvent *button_event;
7368 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7369 struct window *w = XWINDOW (f->tool_bar_window);
7370 int hpos, vpos, prop_idx;
7371 struct glyph *glyph;
7372 Lisp_Object enabled_p;
7373 int x = button_event->x;
7374 int y = button_event->y;
7376 /* If not on the highlighted tool-bar item, return. */
7377 frame_to_window_pixel_xy (w, &x, &y);
7378 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
7379 return;
7381 /* If item is disabled, do nothing. */
7382 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7383 if (NILP (enabled_p))
7384 return;
7386 if (button_event->type == ButtonPress)
7388 /* Show item in pressed state. */
7389 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7390 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
7391 last_tool_bar_item = prop_idx;
7393 else
7395 Lisp_Object key, frame;
7396 struct input_event event;
7398 /* Show item in released state. */
7399 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7400 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7402 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
7404 XSETFRAME (frame, f);
7405 event.kind = TOOL_BAR_EVENT;
7406 event.frame_or_window = frame;
7407 event.arg = frame;
7408 kbd_buffer_store_event (&event);
7410 event.kind = TOOL_BAR_EVENT;
7411 event.frame_or_window = frame;
7412 event.arg = key;
7413 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7414 button_event->state);
7415 kbd_buffer_store_event (&event);
7416 last_tool_bar_item = -1;
7421 /* Possibly highlight a tool-bar item on frame F when mouse moves to
7422 tool-bar window-relative coordinates X/Y. Called from
7423 note_mouse_highlight. */
7425 static void
7426 note_tool_bar_highlight (f, x, y)
7427 struct frame *f;
7428 int x, y;
7430 Lisp_Object window = f->tool_bar_window;
7431 struct window *w = XWINDOW (window);
7432 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7433 int hpos, vpos;
7434 struct glyph *glyph;
7435 struct glyph_row *row;
7436 int i;
7437 Lisp_Object enabled_p;
7438 int prop_idx;
7439 enum draw_glyphs_face draw;
7440 int mouse_down_p, rc;
7442 /* Function note_mouse_highlight is called with negative x(y
7443 values when mouse moves outside of the frame. */
7444 if (x <= 0 || y <= 0)
7446 clear_mouse_face (dpyinfo);
7447 return;
7450 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7451 if (rc < 0)
7453 /* Not on tool-bar item. */
7454 clear_mouse_face (dpyinfo);
7455 return;
7457 else if (rc == 0)
7458 goto set_help_echo;
7460 clear_mouse_face (dpyinfo);
7462 /* Mouse is down, but on different tool-bar item? */
7463 mouse_down_p = (dpyinfo->grabbed
7464 && f == last_mouse_frame
7465 && FRAME_LIVE_P (f));
7466 if (mouse_down_p
7467 && last_tool_bar_item != prop_idx)
7468 return;
7470 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7471 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7473 /* If tool-bar item is not enabled, don't highlight it. */
7474 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7475 if (!NILP (enabled_p))
7477 /* Compute the x-position of the glyph. In front and past the
7478 image is a space. We include this is the highlighted area. */
7479 row = MATRIX_ROW (w->current_matrix, vpos);
7480 for (i = x = 0; i < hpos; ++i)
7481 x += row->glyphs[TEXT_AREA][i].pixel_width;
7483 /* Record this as the current active region. */
7484 dpyinfo->mouse_face_beg_col = hpos;
7485 dpyinfo->mouse_face_beg_row = vpos;
7486 dpyinfo->mouse_face_beg_x = x;
7487 dpyinfo->mouse_face_beg_y = row->y;
7488 dpyinfo->mouse_face_past_end = 0;
7490 dpyinfo->mouse_face_end_col = hpos + 1;
7491 dpyinfo->mouse_face_end_row = vpos;
7492 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7493 dpyinfo->mouse_face_end_y = row->y;
7494 dpyinfo->mouse_face_window = window;
7495 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7497 /* Display it as active. */
7498 show_mouse_face (dpyinfo, draw);
7499 dpyinfo->mouse_face_image_state = draw;
7502 set_help_echo:
7504 /* Set help_echo to a help string to display for this tool-bar item.
7505 XTread_socket does the rest. */
7506 help_echo_object = help_echo_window = Qnil;
7507 help_echo_pos = -1;
7508 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
7509 if (NILP (help_echo))
7510 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
7515 /* Find the glyph matrix position of buffer position CHARPOS in window
7516 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7517 current glyphs must be up to date. If CHARPOS is above window
7518 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
7519 of last line in W. In the row containing CHARPOS, stop before glyphs
7520 having STOP as object. */
7522 #if 0 /* This is a version of fast_find_position that's more correct
7523 in the presence of hscrolling, for example. I didn't install
7524 it right away because the problem fixed is minor, it failed
7525 in 20.x as well, and I think it's too risky to install
7526 so near the release of 21.1. 2001-09-25 gerd. */
7528 static int
7529 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
7530 struct window *w;
7531 int charpos;
7532 int *hpos, *vpos, *x, *y;
7533 Lisp_Object stop;
7535 struct glyph_row *row, *first;
7536 struct glyph *glyph, *end;
7537 int i, past_end = 0;
7539 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7540 row = row_containing_pos (w, charpos, first, NULL, 0);
7541 if (row == NULL)
7543 if (charpos < MATRIX_ROW_START_CHARPOS (first))
7545 *x = *y = *hpos = *vpos = 0;
7546 return 0;
7548 else
7550 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
7551 past_end = 1;
7555 *x = row->x;
7556 *y = row->y;
7557 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7559 glyph = row->glyphs[TEXT_AREA];
7560 end = glyph + row->used[TEXT_AREA];
7562 /* Skip over glyphs not having an object at the start of the row.
7563 These are special glyphs like truncation marks on terminal
7564 frames. */
7565 if (row->displays_text_p)
7566 while (glyph < end
7567 && INTEGERP (glyph->object)
7568 && !EQ (stop, glyph->object)
7569 && glyph->charpos < 0)
7571 *x += glyph->pixel_width;
7572 ++glyph;
7575 while (glyph < end
7576 && !INTEGERP (glyph->object)
7577 && !EQ (stop, glyph->object)
7578 && (!BUFFERP (glyph->object)
7579 || glyph->charpos < charpos))
7581 *x += glyph->pixel_width;
7582 ++glyph;
7585 *hpos = glyph - row->glyphs[TEXT_AREA];
7586 return past_end;
7589 #else /* not 0 */
7591 static int
7592 fast_find_position (w, pos, hpos, vpos, x, y, stop)
7593 struct window *w;
7594 int pos;
7595 int *hpos, *vpos, *x, *y;
7596 Lisp_Object stop;
7598 int i;
7599 int lastcol;
7600 int maybe_next_line_p = 0;
7601 int line_start_position;
7602 int yb = window_text_bottom_y (w);
7603 struct glyph_row *row, *best_row;
7604 int row_vpos, best_row_vpos;
7605 int current_x;
7607 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7608 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7610 while (row->y < yb)
7612 if (row->used[TEXT_AREA])
7613 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7614 else
7615 line_start_position = 0;
7617 if (line_start_position > pos)
7618 break;
7619 /* If the position sought is the end of the buffer,
7620 don't include the blank lines at the bottom of the window. */
7621 else if (line_start_position == pos
7622 && pos == BUF_ZV (XBUFFER (w->buffer)))
7624 maybe_next_line_p = 1;
7625 break;
7627 else if (line_start_position > 0)
7629 best_row = row;
7630 best_row_vpos = row_vpos;
7633 if (row->y + row->height >= yb)
7634 break;
7636 ++row;
7637 ++row_vpos;
7640 /* Find the right column within BEST_ROW. */
7641 lastcol = 0;
7642 current_x = best_row->x;
7643 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
7645 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7646 int charpos = glyph->charpos;
7648 if (BUFFERP (glyph->object))
7650 if (charpos == pos)
7652 *hpos = i;
7653 *vpos = best_row_vpos;
7654 *x = current_x;
7655 *y = best_row->y;
7656 return 1;
7658 else if (charpos > pos)
7659 break;
7661 else if (EQ (glyph->object, stop))
7662 break;
7664 if (charpos > 0)
7665 lastcol = i;
7666 current_x += glyph->pixel_width;
7669 /* If we're looking for the end of the buffer,
7670 and we didn't find it in the line we scanned,
7671 use the start of the following line. */
7672 if (maybe_next_line_p)
7674 ++best_row;
7675 ++best_row_vpos;
7676 lastcol = 0;
7677 current_x = best_row->x;
7680 *vpos = best_row_vpos;
7681 *hpos = lastcol + 1;
7682 *x = current_x;
7683 *y = best_row->y;
7684 return 0;
7687 #endif /* not 0 */
7690 /* Find the position of the glyph for position POS in OBJECT in
7691 window W's current matrix, and return in *X, *Y the pixel
7692 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
7694 RIGHT_P non-zero means return the position of the right edge of the
7695 glyph, RIGHT_P zero means return the left edge position.
7697 If no glyph for POS exists in the matrix, return the position of
7698 the glyph with the next smaller position that is in the matrix, if
7699 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7700 exists in the matrix, return the position of the glyph with the
7701 next larger position in OBJECT.
7703 Value is non-zero if a glyph was found. */
7705 static int
7706 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7707 struct window *w;
7708 int pos;
7709 Lisp_Object object;
7710 int *hpos, *vpos, *x, *y;
7711 int right_p;
7713 int yb = window_text_bottom_y (w);
7714 struct glyph_row *r;
7715 struct glyph *best_glyph = NULL;
7716 struct glyph_row *best_row = NULL;
7717 int best_x = 0;
7719 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7720 r->enabled_p && r->y < yb;
7721 ++r)
7723 struct glyph *g = r->glyphs[TEXT_AREA];
7724 struct glyph *e = g + r->used[TEXT_AREA];
7725 int gx;
7727 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7728 if (EQ (g->object, object))
7730 if (g->charpos == pos)
7732 best_glyph = g;
7733 best_x = gx;
7734 best_row = r;
7735 goto found;
7737 else if (best_glyph == NULL
7738 || ((abs (g->charpos - pos)
7739 < abs (best_glyph->charpos - pos))
7740 && (right_p
7741 ? g->charpos < pos
7742 : g->charpos > pos)))
7744 best_glyph = g;
7745 best_x = gx;
7746 best_row = r;
7751 found:
7753 if (best_glyph)
7755 *x = best_x;
7756 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7758 if (right_p)
7760 *x += best_glyph->pixel_width;
7761 ++*hpos;
7764 *y = best_row->y;
7765 *vpos = best_row - w->current_matrix->rows;
7768 return best_glyph != NULL;
7772 /* Display the active region described by mouse_face_*
7773 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7775 static void
7776 show_mouse_face (dpyinfo, draw)
7777 struct x_display_info *dpyinfo;
7778 enum draw_glyphs_face draw;
7780 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7781 struct frame *f = XFRAME (WINDOW_FRAME (w));
7783 if (/* If window is in the process of being destroyed, don't bother
7784 to do anything. */
7785 w->current_matrix != NULL
7786 /* Don't update mouse highlight if hidden */
7787 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
7788 /* Recognize when we are called to operate on rows that don't exist
7789 anymore. This can happen when a window is split. */
7790 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
7792 int phys_cursor_on_p = w->phys_cursor_on_p;
7793 struct glyph_row *row, *first, *last;
7795 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
7796 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
7798 for (row = first; row <= last && row->enabled_p; ++row)
7800 int start_hpos, end_hpos, start_x;
7802 /* For all but the first row, the highlight starts at column 0. */
7803 if (row == first)
7805 start_hpos = dpyinfo->mouse_face_beg_col;
7806 start_x = dpyinfo->mouse_face_beg_x;
7808 else
7810 start_hpos = 0;
7811 start_x = 0;
7814 if (row == last)
7815 end_hpos = dpyinfo->mouse_face_end_col;
7816 else
7817 end_hpos = row->used[TEXT_AREA];
7819 if (end_hpos > start_hpos)
7821 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7822 start_hpos, end_hpos, draw, 0);
7824 row->mouse_face_p
7825 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
7829 /* When we've written over the cursor, arrange for it to
7830 be displayed again. */
7831 if (phys_cursor_on_p && !w->phys_cursor_on_p)
7832 x_display_cursor (w, 1,
7833 w->phys_cursor.hpos, w->phys_cursor.vpos,
7834 w->phys_cursor.x, w->phys_cursor.y);
7837 /* Change the mouse cursor. */
7838 if (draw == DRAW_NORMAL_TEXT)
7839 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7840 f->output_data.x->text_cursor);
7841 else if (draw == DRAW_MOUSE_FACE)
7842 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7843 f->output_data.x->cross_cursor);
7844 else
7845 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7846 f->output_data.x->nontext_cursor);
7849 /* Clear out the mouse-highlighted active region.
7850 Redraw it un-highlighted first. Value is non-zero if mouse
7851 face was actually drawn unhighlighted. */
7853 static int
7854 clear_mouse_face (dpyinfo)
7855 struct x_display_info *dpyinfo;
7857 int cleared = 0;
7859 if (!NILP (dpyinfo->mouse_face_window))
7861 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7862 cleared = 1;
7865 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7866 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7867 dpyinfo->mouse_face_window = Qnil;
7868 dpyinfo->mouse_face_overlay = Qnil;
7869 return cleared;
7873 /* Clear any mouse-face on window W. This function is part of the
7874 redisplay interface, and is called from try_window_id and similar
7875 functions to ensure the mouse-highlight is off. */
7877 static void
7878 x_clear_mouse_face (w)
7879 struct window *w;
7881 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7882 Lisp_Object window;
7884 BLOCK_INPUT;
7885 XSETWINDOW (window, w);
7886 if (EQ (window, dpyinfo->mouse_face_window))
7887 clear_mouse_face (dpyinfo);
7888 UNBLOCK_INPUT;
7892 /* Just discard the mouse face information for frame F, if any.
7893 This is used when the size of F is changed. */
7895 void
7896 cancel_mouse_face (f)
7897 FRAME_PTR f;
7899 Lisp_Object window;
7900 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7902 window = dpyinfo->mouse_face_window;
7903 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7905 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7906 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7907 dpyinfo->mouse_face_window = Qnil;
7912 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
7915 /* Try to determine frame pixel position and size of the glyph under
7916 frame pixel coordinates X/Y on frame F . Return the position and
7917 size in *RECT. Value is non-zero if we could compute these
7918 values. */
7920 static int
7921 glyph_rect (f, x, y, rect)
7922 struct frame *f;
7923 int x, y;
7924 XRectangle *rect;
7926 Lisp_Object window;
7927 int part, found = 0;
7929 window = window_from_coordinates (f, x, y, &part, 0);
7930 if (!NILP (window))
7932 struct window *w = XWINDOW (window);
7933 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7934 struct glyph_row *end = r + w->current_matrix->nrows - 1;
7936 frame_to_window_pixel_xy (w, &x, &y);
7938 for (; !found && r < end && r->enabled_p; ++r)
7939 if (r->y >= y)
7941 struct glyph *g = r->glyphs[TEXT_AREA];
7942 struct glyph *end = g + r->used[TEXT_AREA];
7943 int gx;
7945 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
7946 if (gx >= x)
7948 rect->width = g->pixel_width;
7949 rect->height = r->height;
7950 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
7951 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
7952 found = 1;
7957 return found;
7961 /* Return the current position of the mouse.
7962 *FP should be a frame which indicates which display to ask about.
7964 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
7965 and *PART to the frame, window, and scroll bar part that the mouse
7966 is over. Set *X and *Y to the portion and whole of the mouse's
7967 position on the scroll bar.
7969 If the mouse movement started elsewhere, set *FP to the frame the
7970 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
7971 the mouse is over.
7973 Set *TIME to the server time-stamp for the time at which the mouse
7974 was at this position.
7976 Don't store anything if we don't have a valid set of values to report.
7978 This clears the mouse_moved flag, so we can wait for the next mouse
7979 movement. */
7981 static void
7982 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7983 FRAME_PTR *fp;
7984 int insist;
7985 Lisp_Object *bar_window;
7986 enum scroll_bar_part *part;
7987 Lisp_Object *x, *y;
7988 unsigned long *time;
7990 FRAME_PTR f1;
7992 BLOCK_INPUT;
7994 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7995 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7996 else
7998 Window root;
7999 int root_x, root_y;
8001 Window dummy_window;
8002 int dummy;
8004 Lisp_Object frame, tail;
8006 /* Clear the mouse-moved flag for every frame on this display. */
8007 FOR_EACH_FRAME (tail, frame)
8008 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
8009 XFRAME (frame)->mouse_moved = 0;
8011 last_mouse_scroll_bar = Qnil;
8013 /* Figure out which root window we're on. */
8014 XQueryPointer (FRAME_X_DISPLAY (*fp),
8015 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
8017 /* The root window which contains the pointer. */
8018 &root,
8020 /* Trash which we can't trust if the pointer is on
8021 a different screen. */
8022 &dummy_window,
8024 /* The position on that root window. */
8025 &root_x, &root_y,
8027 /* More trash we can't trust. */
8028 &dummy, &dummy,
8030 /* Modifier keys and pointer buttons, about which
8031 we don't care. */
8032 (unsigned int *) &dummy);
8034 /* Now we have a position on the root; find the innermost window
8035 containing the pointer. */
8037 Window win, child;
8038 int win_x, win_y;
8039 int parent_x = 0, parent_y = 0;
8040 int count;
8042 win = root;
8044 /* XTranslateCoordinates can get errors if the window
8045 structure is changing at the same time this function
8046 is running. So at least we must not crash from them. */
8048 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
8050 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
8051 && FRAME_LIVE_P (last_mouse_frame))
8053 /* If mouse was grabbed on a frame, give coords for that frame
8054 even if the mouse is now outside it. */
8055 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
8057 /* From-window, to-window. */
8058 root, FRAME_X_WINDOW (last_mouse_frame),
8060 /* From-position, to-position. */
8061 root_x, root_y, &win_x, &win_y,
8063 /* Child of win. */
8064 &child);
8065 f1 = last_mouse_frame;
8067 else
8069 while (1)
8071 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
8073 /* From-window, to-window. */
8074 root, win,
8076 /* From-position, to-position. */
8077 root_x, root_y, &win_x, &win_y,
8079 /* Child of win. */
8080 &child);
8082 if (child == None || child == win)
8083 break;
8085 win = child;
8086 parent_x = win_x;
8087 parent_y = win_y;
8090 /* Now we know that:
8091 win is the innermost window containing the pointer
8092 (XTC says it has no child containing the pointer),
8093 win_x and win_y are the pointer's position in it
8094 (XTC did this the last time through), and
8095 parent_x and parent_y are the pointer's position in win's parent.
8096 (They are what win_x and win_y were when win was child.
8097 If win is the root window, it has no parent, and
8098 parent_{x,y} are invalid, but that's okay, because we'll
8099 never use them in that case.) */
8101 /* Is win one of our frames? */
8102 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
8104 #ifdef USE_X_TOOLKIT
8105 /* If we end up with the menu bar window, say it's not
8106 on the frame. */
8107 if (f1 != NULL
8108 && f1->output_data.x->menubar_widget
8109 && win == XtWindow (f1->output_data.x->menubar_widget))
8110 f1 = NULL;
8111 #endif /* USE_X_TOOLKIT */
8114 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
8115 f1 = 0;
8117 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
8119 /* If not, is it one of our scroll bars? */
8120 if (! f1)
8122 struct scroll_bar *bar = x_window_to_scroll_bar (win);
8124 if (bar)
8126 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8127 win_x = parent_x;
8128 win_y = parent_y;
8132 if (f1 == 0 && insist > 0)
8133 f1 = SELECTED_FRAME ();
8135 if (f1)
8137 /* Ok, we found a frame. Store all the values.
8138 last_mouse_glyph is a rectangle used to reduce the
8139 generation of mouse events. To not miss any motion
8140 events, we must divide the frame into rectangles of the
8141 size of the smallest character that could be displayed
8142 on it, i.e. into the same rectangles that matrices on
8143 the frame are divided into. */
8145 int width, height, gx, gy;
8146 XRectangle rect;
8148 if (glyph_rect (f1, win_x, win_y, &rect))
8149 last_mouse_glyph = rect;
8150 else
8152 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
8153 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
8154 gx = win_x;
8155 gy = win_y;
8157 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
8158 round down even for negative values. */
8159 if (gx < 0)
8160 gx -= width - 1;
8161 if (gy < 0)
8162 gy -= height - 1;
8163 gx = (gx + width - 1) / width * width;
8164 gy = (gy + height - 1) / height * height;
8166 last_mouse_glyph.width = width;
8167 last_mouse_glyph.height = height;
8168 last_mouse_glyph.x = gx;
8169 last_mouse_glyph.y = gy;
8172 *bar_window = Qnil;
8173 *part = 0;
8174 *fp = f1;
8175 XSETINT (*x, win_x);
8176 XSETINT (*y, win_y);
8177 *time = last_mouse_movement_time;
8182 UNBLOCK_INPUT;
8186 #ifdef USE_X_TOOLKIT
8188 /* Atimer callback function for TIMER. Called every 0.1s to process
8189 Xt timeouts, if needed. We must avoid calling XtAppPending as
8190 much as possible because that function does an implicit XFlush
8191 that slows us down. */
8193 static void
8194 x_process_timeouts (timer)
8195 struct atimer *timer;
8197 if (toolkit_scroll_bar_interaction || popup_activated_flag)
8199 BLOCK_INPUT;
8200 while (XtAppPending (Xt_app_con) & XtIMTimer)
8201 XtAppProcessEvent (Xt_app_con, XtIMTimer);
8202 UNBLOCK_INPUT;
8206 #endif /* USE_X_TOOLKIT */
8209 /* Scroll bar support. */
8211 /* Given an X window ID, find the struct scroll_bar which manages it.
8212 This can be called in GC, so we have to make sure to strip off mark
8213 bits. */
8215 static struct scroll_bar *
8216 x_window_to_scroll_bar (window_id)
8217 Window window_id;
8219 Lisp_Object tail;
8221 for (tail = Vframe_list;
8222 XGCTYPE (tail) == Lisp_Cons;
8223 tail = XCDR (tail))
8225 Lisp_Object frame, bar, condemned;
8227 frame = XCAR (tail);
8228 /* All elements of Vframe_list should be frames. */
8229 if (! GC_FRAMEP (frame))
8230 abort ();
8232 /* Scan this frame's scroll bar list for a scroll bar with the
8233 right window ID. */
8234 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
8235 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
8236 /* This trick allows us to search both the ordinary and
8237 condemned scroll bar lists with one loop. */
8238 ! GC_NILP (bar) || (bar = condemned,
8239 condemned = Qnil,
8240 ! GC_NILP (bar));
8241 bar = XSCROLL_BAR (bar)->next)
8242 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
8243 return XSCROLL_BAR (bar);
8246 return 0;
8250 #if defined USE_LUCID
8252 /* Return the Lucid menu bar WINDOW is part of. Return null
8253 if WINDOW is not part of a menu bar. */
8255 static Widget
8256 x_window_to_menu_bar (window)
8257 Window window;
8259 Lisp_Object tail;
8261 for (tail = Vframe_list;
8262 XGCTYPE (tail) == Lisp_Cons;
8263 tail = XCDR (tail))
8265 Lisp_Object frame = XCAR (tail);
8266 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
8268 if (menu_bar && xlwmenu_window_p (menu_bar, window))
8269 return menu_bar;
8272 return NULL;
8275 #endif /* USE_LUCID */
8278 /************************************************************************
8279 Toolkit scroll bars
8280 ************************************************************************/
8282 #ifdef USE_TOOLKIT_SCROLL_BARS
8284 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
8285 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
8286 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
8287 struct scroll_bar *));
8288 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
8289 int, int, int));
8292 /* Id of action hook installed for scroll bars. */
8294 static XtActionHookId action_hook_id;
8296 /* Lisp window being scrolled. Set when starting to interact with
8297 a toolkit scroll bar, reset to nil when ending the interaction. */
8299 static Lisp_Object window_being_scrolled;
8301 /* Last scroll bar part sent in xm_scroll_callback. */
8303 static int last_scroll_bar_part;
8305 /* Whether this is an Xaw with arrow-scrollbars. This should imply
8306 that movements of 1/20 of the screen size are mapped to up/down. */
8308 static Boolean xaw3d_arrow_scroll;
8310 /* Whether the drag scrolling maintains the mouse at the top of the
8311 thumb. If not, resizing the thumb needs to be done more carefully
8312 to avoid jerkyness. */
8314 static Boolean xaw3d_pick_top;
8317 /* Action hook installed via XtAppAddActionHook when toolkit scroll
8318 bars are used.. The hook is responsible for detecting when
8319 the user ends an interaction with the scroll bar, and generates
8320 a `end-scroll' scroll_bar_click' event if so. */
8322 static void
8323 xt_action_hook (widget, client_data, action_name, event, params,
8324 num_params)
8325 Widget widget;
8326 XtPointer client_data;
8327 String action_name;
8328 XEvent *event;
8329 String *params;
8330 Cardinal *num_params;
8332 int scroll_bar_p;
8333 char *end_action;
8335 #ifdef USE_MOTIF
8336 scroll_bar_p = XmIsScrollBar (widget);
8337 end_action = "Release";
8338 #else /* !USE_MOTIF i.e. use Xaw */
8339 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
8340 end_action = "EndScroll";
8341 #endif /* USE_MOTIF */
8343 if (scroll_bar_p
8344 && strcmp (action_name, end_action) == 0
8345 && WINDOWP (window_being_scrolled))
8347 struct window *w;
8349 x_send_scroll_bar_event (window_being_scrolled,
8350 scroll_bar_end_scroll, 0, 0);
8351 w = XWINDOW (window_being_scrolled);
8352 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
8353 window_being_scrolled = Qnil;
8354 last_scroll_bar_part = -1;
8356 /* Xt timeouts no longer needed. */
8357 toolkit_scroll_bar_interaction = 0;
8361 /* A vector of windows used for communication between
8362 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
8364 static struct window **scroll_bar_windows;
8365 static int scroll_bar_windows_size;
8368 /* Send a client message with message type Xatom_Scrollbar for a
8369 scroll action to the frame of WINDOW. PART is a value identifying
8370 the part of the scroll bar that was clicked on. PORTION is the
8371 amount to scroll of a whole of WHOLE. */
8373 static void
8374 x_send_scroll_bar_event (window, part, portion, whole)
8375 Lisp_Object window;
8376 int part, portion, whole;
8378 XEvent event;
8379 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
8380 struct window *w = XWINDOW (window);
8381 struct frame *f = XFRAME (w->frame);
8382 int i;
8384 BLOCK_INPUT;
8386 /* Construct a ClientMessage event to send to the frame. */
8387 ev->type = ClientMessage;
8388 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
8389 ev->display = FRAME_X_DISPLAY (f);
8390 ev->window = FRAME_X_WINDOW (f);
8391 ev->format = 32;
8393 /* We can only transfer 32 bits in the XClientMessageEvent, which is
8394 not enough to store a pointer or Lisp_Object on a 64 bit system.
8395 So, store the window in scroll_bar_windows and pass the index
8396 into that array in the event. */
8397 for (i = 0; i < scroll_bar_windows_size; ++i)
8398 if (scroll_bar_windows[i] == NULL)
8399 break;
8401 if (i == scroll_bar_windows_size)
8403 int new_size = max (10, 2 * scroll_bar_windows_size);
8404 size_t nbytes = new_size * sizeof *scroll_bar_windows;
8405 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
8407 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
8408 nbytes);
8409 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
8410 scroll_bar_windows_size = new_size;
8413 scroll_bar_windows[i] = w;
8414 ev->data.l[0] = (long) i;
8415 ev->data.l[1] = (long) part;
8416 ev->data.l[2] = (long) 0;
8417 ev->data.l[3] = (long) portion;
8418 ev->data.l[4] = (long) whole;
8420 /* Make Xt timeouts work while the scroll bar is active. */
8421 toolkit_scroll_bar_interaction = 1;
8423 /* Setting the event mask to zero means that the message will
8424 be sent to the client that created the window, and if that
8425 window no longer exists, no event will be sent. */
8426 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
8427 UNBLOCK_INPUT;
8431 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
8432 in *IEVENT. */
8434 static void
8435 x_scroll_bar_to_input_event (event, ievent)
8436 XEvent *event;
8437 struct input_event *ievent;
8439 XClientMessageEvent *ev = (XClientMessageEvent *) event;
8440 Lisp_Object window;
8441 struct frame *f;
8442 struct window *w;
8444 w = scroll_bar_windows[ev->data.l[0]];
8445 scroll_bar_windows[ev->data.l[0]] = NULL;
8447 XSETWINDOW (window, w);
8448 f = XFRAME (w->frame);
8450 ievent->kind = scroll_bar_click;
8451 ievent->frame_or_window = window;
8452 ievent->arg = Qnil;
8453 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
8454 ievent->part = ev->data.l[1];
8455 ievent->code = ev->data.l[2];
8456 ievent->x = make_number ((int) ev->data.l[3]);
8457 ievent->y = make_number ((int) ev->data.l[4]);
8458 ievent->modifiers = 0;
8462 #ifdef USE_MOTIF
8464 /* Minimum and maximum values used for Motif scroll bars. */
8466 #define XM_SB_MIN 1
8467 #define XM_SB_MAX 10000000
8468 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
8471 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
8472 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
8473 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
8475 static void
8476 xm_scroll_callback (widget, client_data, call_data)
8477 Widget widget;
8478 XtPointer client_data, call_data;
8480 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8481 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
8482 int part = -1, whole = 0, portion = 0;
8484 switch (cs->reason)
8486 case XmCR_DECREMENT:
8487 bar->dragging = Qnil;
8488 part = scroll_bar_up_arrow;
8489 break;
8491 case XmCR_INCREMENT:
8492 bar->dragging = Qnil;
8493 part = scroll_bar_down_arrow;
8494 break;
8496 case XmCR_PAGE_DECREMENT:
8497 bar->dragging = Qnil;
8498 part = scroll_bar_above_handle;
8499 break;
8501 case XmCR_PAGE_INCREMENT:
8502 bar->dragging = Qnil;
8503 part = scroll_bar_below_handle;
8504 break;
8506 case XmCR_TO_TOP:
8507 bar->dragging = Qnil;
8508 part = scroll_bar_to_top;
8509 break;
8511 case XmCR_TO_BOTTOM:
8512 bar->dragging = Qnil;
8513 part = scroll_bar_to_bottom;
8514 break;
8516 case XmCR_DRAG:
8518 int slider_size;
8519 int dragging_down_p = (INTEGERP (bar->dragging)
8520 && XINT (bar->dragging) <= cs->value);
8522 /* Get the slider size. */
8523 BLOCK_INPUT;
8524 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
8525 UNBLOCK_INPUT;
8527 whole = XM_SB_RANGE - slider_size;
8528 portion = min (cs->value - XM_SB_MIN, whole);
8529 part = scroll_bar_handle;
8530 bar->dragging = make_number (cs->value);
8532 break;
8534 case XmCR_VALUE_CHANGED:
8535 break;
8538 if (part >= 0)
8540 window_being_scrolled = bar->window;
8541 last_scroll_bar_part = part;
8542 x_send_scroll_bar_event (bar->window, part, portion, whole);
8547 #else /* !USE_MOTIF, i.e. Xaw. */
8550 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
8551 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8552 scroll bar struct. CALL_DATA is a pointer to a float saying where
8553 the thumb is. */
8555 static void
8556 xaw_jump_callback (widget, client_data, call_data)
8557 Widget widget;
8558 XtPointer client_data, call_data;
8560 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8561 float top = *(float *) call_data;
8562 float shown;
8563 int whole, portion, height;
8564 int part;
8566 /* Get the size of the thumb, a value between 0 and 1. */
8567 BLOCK_INPUT;
8568 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
8569 UNBLOCK_INPUT;
8571 whole = 10000000;
8572 portion = shown < 1 ? top * whole : 0;
8574 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8575 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8576 the bottom, so we force the scrolling whenever we see that we're
8577 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8578 we try to ensure that we always stay two pixels away from the
8579 bottom). */
8580 part = scroll_bar_down_arrow;
8581 else
8582 part = scroll_bar_handle;
8584 window_being_scrolled = bar->window;
8585 bar->dragging = make_number (portion);
8586 last_scroll_bar_part = part;
8587 x_send_scroll_bar_event (bar->window, part, portion, whole);
8591 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
8592 i.e. line or page up or down. WIDGET is the Xaw scroll bar
8593 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8594 the scroll bar. CALL_DATA is an integer specifying the action that
8595 has taken place. It's magnitude is in the range 0..height of the
8596 scroll bar. Negative values mean scroll towards buffer start.
8597 Values < height of scroll bar mean line-wise movement. */
8599 static void
8600 xaw_scroll_callback (widget, client_data, call_data)
8601 Widget widget;
8602 XtPointer client_data, call_data;
8604 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8605 int position = (int) call_data;
8606 Dimension height;
8607 int part;
8609 /* Get the height of the scroll bar. */
8610 BLOCK_INPUT;
8611 XtVaGetValues (widget, XtNheight, &height, NULL);
8612 UNBLOCK_INPUT;
8614 if (abs (position) >= height)
8615 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8617 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8618 it maps line-movement to call_data = max(5, height/20). */
8619 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8620 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
8621 else
8622 part = scroll_bar_move_ratio;
8624 window_being_scrolled = bar->window;
8625 bar->dragging = Qnil;
8626 last_scroll_bar_part = part;
8627 x_send_scroll_bar_event (bar->window, part, position, height);
8631 #endif /* not USE_MOTIF */
8634 /* Create the widget for scroll bar BAR on frame F. Record the widget
8635 and X window of the scroll bar in BAR. */
8637 static void
8638 x_create_toolkit_scroll_bar (f, bar)
8639 struct frame *f;
8640 struct scroll_bar *bar;
8642 Window xwindow;
8643 Widget widget;
8644 Arg av[20];
8645 int ac = 0;
8646 char *scroll_bar_name = "verticalScrollBar";
8647 unsigned long pixel;
8649 BLOCK_INPUT;
8651 #ifdef USE_MOTIF
8652 /* Set resources. Create the widget. */
8653 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8654 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8655 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8656 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8657 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8658 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8659 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8661 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8662 if (pixel != -1)
8664 XtSetArg (av[ac], XmNforeground, pixel);
8665 ++ac;
8668 pixel = f->output_data.x->scroll_bar_background_pixel;
8669 if (pixel != -1)
8671 XtSetArg (av[ac], XmNbackground, pixel);
8672 ++ac;
8675 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8676 scroll_bar_name, av, ac);
8678 /* Add one callback for everything that can happen. */
8679 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8680 (XtPointer) bar);
8681 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8682 (XtPointer) bar);
8683 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8684 (XtPointer) bar);
8685 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8686 (XtPointer) bar);
8687 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8688 (XtPointer) bar);
8689 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8690 (XtPointer) bar);
8691 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8692 (XtPointer) bar);
8694 /* Realize the widget. Only after that is the X window created. */
8695 XtRealizeWidget (widget);
8697 /* Set the cursor to an arrow. I didn't find a resource to do that.
8698 And I'm wondering why it hasn't an arrow cursor by default. */
8699 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8700 f->output_data.x->nontext_cursor);
8702 #else /* !USE_MOTIF i.e. use Xaw */
8704 /* Set resources. Create the widget. The background of the
8705 Xaw3d scroll bar widget is a little bit light for my taste.
8706 We don't alter it here to let users change it according
8707 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8708 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8709 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
8710 /* For smoother scrolling with Xaw3d -sm */
8711 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8713 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8714 if (pixel != -1)
8716 XtSetArg (av[ac], XtNforeground, pixel);
8717 ++ac;
8720 pixel = f->output_data.x->scroll_bar_background_pixel;
8721 if (pixel != -1)
8723 XtSetArg (av[ac], XtNbackground, pixel);
8724 ++ac;
8727 /* Top/bottom shadow colors. */
8729 /* Allocate them, if necessary. */
8730 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
8732 pixel = f->output_data.x->scroll_bar_background_pixel;
8733 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8734 &pixel, 1.2, 0x8000))
8735 pixel = -1;
8736 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
8738 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8740 pixel = f->output_data.x->scroll_bar_background_pixel;
8741 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8742 &pixel, 0.6, 0x4000))
8743 pixel = -1;
8744 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
8747 /* Tell the toolkit about them. */
8748 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
8749 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8750 /* We tried to allocate a color for the top/bottom shadow, and
8751 failed, so tell Xaw3d to use dithering instead. */
8753 XtSetArg (av[ac], XtNbeNiceToColormap, True);
8754 ++ac;
8756 else
8757 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
8758 be more consistent with other emacs 3d colors, and since Xaw3d is
8759 not good at dealing with allocation failure. */
8761 /* This tells Xaw3d to use real colors instead of dithering for
8762 the shadows. */
8763 XtSetArg (av[ac], XtNbeNiceToColormap, False);
8764 ++ac;
8766 /* Specify the colors. */
8767 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
8768 if (pixel != -1)
8770 XtSetArg (av[ac], "topShadowPixel", pixel);
8771 ++ac;
8773 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
8774 if (pixel != -1)
8776 XtSetArg (av[ac], "bottomShadowPixel", pixel);
8777 ++ac;
8781 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8782 f->output_data.x->edit_widget, av, ac);
8785 char *initial = "";
8786 char *val = initial;
8787 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8788 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8789 if (val == initial)
8790 { /* ARROW_SCROLL */
8791 xaw3d_arrow_scroll = True;
8792 /* Isn't that just a personal preference ? -sm */
8793 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8797 /* Define callbacks. */
8798 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8799 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
8800 (XtPointer) bar);
8802 /* Realize the widget. Only after that is the X window created. */
8803 XtRealizeWidget (widget);
8805 #endif /* !USE_MOTIF */
8807 /* Install an action hook that let's us detect when the user
8808 finishes interacting with a scroll bar. */
8809 if (action_hook_id == 0)
8810 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8812 /* Remember X window and widget in the scroll bar vector. */
8813 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8814 xwindow = XtWindow (widget);
8815 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8817 UNBLOCK_INPUT;
8821 /* Set the thumb size and position of scroll bar BAR. We are currently
8822 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8824 static void
8825 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8826 struct scroll_bar *bar;
8827 int portion, position, whole;
8829 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8830 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8831 float top, shown;
8833 BLOCK_INPUT;
8835 #ifdef USE_MOTIF
8837 /* We use an estimate of 30 chars per line rather than the real
8838 `portion' value. This has the disadvantage that the thumb size
8839 is not very representative, but it makes our life a lot easier.
8840 Otherwise, we have to constantly adjust the thumb size, which
8841 we can't always do quickly enough: while dragging, the size of
8842 the thumb might prevent the user from dragging the thumb all the
8843 way to the end. but Motif and some versions of Xaw3d don't allow
8844 updating the thumb size while dragging. Also, even if we can update
8845 its size, the update will often happen too late.
8846 If you don't believe it, check out revision 1.650 of xterm.c to see
8847 what hoops we were going through and the still poor behavior we got. */
8848 portion = XFASTINT (XWINDOW (bar->window)->height) * 30;
8849 /* When the thumb is at the bottom, position == whole.
8850 So we need to increase `whole' to make space for the thumb. */
8851 whole += portion;
8853 if (whole <= 0)
8854 top = 0, shown = 1;
8855 else
8857 top = (float) position / whole;
8858 shown = (float) portion / whole;
8861 if (NILP (bar->dragging))
8863 int size, value;
8865 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
8866 is the scroll bar's maximum and MIN is the scroll bar's minimum
8867 value. */
8868 size = shown * XM_SB_RANGE;
8869 size = min (size, XM_SB_RANGE);
8870 size = max (size, 1);
8872 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8873 value = top * XM_SB_RANGE;
8874 value = min (value, XM_SB_MAX - size);
8875 value = max (value, XM_SB_MIN);
8877 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8879 #else /* !USE_MOTIF i.e. use Xaw */
8881 if (whole == 0)
8882 top = 0, shown = 1;
8883 else
8885 top = (float) position / whole;
8886 shown = (float) portion / whole;
8890 float old_top, old_shown;
8891 Dimension height;
8892 XtVaGetValues (widget,
8893 XtNtopOfThumb, &old_top,
8894 XtNshown, &old_shown,
8895 XtNheight, &height,
8896 NULL);
8898 /* Massage the top+shown values. */
8899 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8900 top = max (0, min (1, top));
8901 else
8902 top = old_top;
8903 /* Keep two pixels available for moving the thumb down. */
8904 shown = max (0, min (1 - top - (2.0 / height), shown));
8906 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8907 check that your system's configuration file contains a define
8908 for `NARROWPROTO'. See s/freebsd.h for an example. */
8909 if (top != old_top || shown != old_shown)
8911 if (NILP (bar->dragging))
8912 XawScrollbarSetThumb (widget, top, shown);
8913 else
8915 #ifdef HAVE_XAW3D
8916 ScrollbarWidget sb = (ScrollbarWidget) widget;
8917 int scroll_mode = 0;
8919 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8920 if (xaw3d_arrow_scroll)
8922 /* Xaw3d stupidly ignores resize requests while dragging
8923 so we have to make it believe it's not in dragging mode. */
8924 scroll_mode = sb->scrollbar.scroll_mode;
8925 if (scroll_mode == 2)
8926 sb->scrollbar.scroll_mode = 0;
8928 #endif
8929 /* Try to make the scrolling a tad smoother. */
8930 if (!xaw3d_pick_top)
8931 shown = min (shown, old_shown);
8933 XawScrollbarSetThumb (widget, top, shown);
8935 #ifdef HAVE_XAW3D
8936 if (xaw3d_arrow_scroll && scroll_mode == 2)
8937 sb->scrollbar.scroll_mode = scroll_mode;
8938 #endif
8942 #endif /* !USE_MOTIF */
8944 UNBLOCK_INPUT;
8947 #endif /* USE_TOOLKIT_SCROLL_BARS */
8951 /************************************************************************
8952 Scroll bars, general
8953 ************************************************************************/
8955 /* Create a scroll bar and return the scroll bar vector for it. W is
8956 the Emacs window on which to create the scroll bar. TOP, LEFT,
8957 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
8958 scroll bar. */
8960 static struct scroll_bar *
8961 x_scroll_bar_create (w, top, left, width, height)
8962 struct window *w;
8963 int top, left, width, height;
8965 struct frame *f = XFRAME (w->frame);
8966 struct scroll_bar *bar
8967 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8969 BLOCK_INPUT;
8971 #ifdef USE_TOOLKIT_SCROLL_BARS
8972 x_create_toolkit_scroll_bar (f, bar);
8973 #else /* not USE_TOOLKIT_SCROLL_BARS */
8975 XSetWindowAttributes a;
8976 unsigned long mask;
8977 Window window;
8979 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8980 if (a.background_pixel == -1)
8981 a.background_pixel = f->output_data.x->background_pixel;
8983 a.event_mask = (ButtonPressMask | ButtonReleaseMask
8984 | ButtonMotionMask | PointerMotionHintMask
8985 | ExposureMask);
8986 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
8988 mask = (CWBackPixel | CWEventMask | CWCursor);
8990 /* Clear the area of W that will serve as a scroll bar. This is
8991 for the case that a window has been split horizontally. In
8992 this case, no clear_frame is generated to reduce flickering. */
8993 if (width > 0 && height > 0)
8994 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8995 left, top, width,
8996 window_box_height (w), False);
8998 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8999 /* Position and size of scroll bar. */
9000 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9001 top,
9002 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9003 height,
9004 /* Border width, depth, class, and visual. */
9006 CopyFromParent,
9007 CopyFromParent,
9008 CopyFromParent,
9009 /* Attributes. */
9010 mask, &a);
9011 SET_SCROLL_BAR_X_WINDOW (bar, window);
9013 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9015 XSETWINDOW (bar->window, w);
9016 XSETINT (bar->top, top);
9017 XSETINT (bar->left, left);
9018 XSETINT (bar->width, width);
9019 XSETINT (bar->height, height);
9020 XSETINT (bar->start, 0);
9021 XSETINT (bar->end, 0);
9022 bar->dragging = Qnil;
9024 /* Add bar to its frame's list of scroll bars. */
9025 bar->next = FRAME_SCROLL_BARS (f);
9026 bar->prev = Qnil;
9027 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9028 if (!NILP (bar->next))
9029 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9031 /* Map the window/widget. */
9032 #ifdef USE_TOOLKIT_SCROLL_BARS
9034 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
9035 XtConfigureWidget (scroll_bar,
9036 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9037 top,
9038 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9039 max (height, 1), 0);
9040 XtMapWidget (scroll_bar);
9042 #else /* not USE_TOOLKIT_SCROLL_BARS */
9043 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9044 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9046 UNBLOCK_INPUT;
9047 return bar;
9051 /* Draw BAR's handle in the proper position.
9053 If the handle is already drawn from START to END, don't bother
9054 redrawing it, unless REBUILD is non-zero; in that case, always
9055 redraw it. (REBUILD is handy for drawing the handle after expose
9056 events.)
9058 Normally, we want to constrain the start and end of the handle to
9059 fit inside its rectangle, but if the user is dragging the scroll
9060 bar handle, we want to let them drag it down all the way, so that
9061 the bar's top is as far down as it goes; otherwise, there's no way
9062 to move to the very end of the buffer. */
9064 #ifndef USE_TOOLKIT_SCROLL_BARS
9066 static void
9067 x_scroll_bar_set_handle (bar, start, end, rebuild)
9068 struct scroll_bar *bar;
9069 int start, end;
9070 int rebuild;
9072 int dragging = ! NILP (bar->dragging);
9073 Window w = SCROLL_BAR_X_WINDOW (bar);
9074 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9075 GC gc = f->output_data.x->normal_gc;
9077 /* If the display is already accurate, do nothing. */
9078 if (! rebuild
9079 && start == XINT (bar->start)
9080 && end == XINT (bar->end))
9081 return;
9083 BLOCK_INPUT;
9086 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
9087 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9088 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9090 /* Make sure the values are reasonable, and try to preserve
9091 the distance between start and end. */
9093 int length = end - start;
9095 if (start < 0)
9096 start = 0;
9097 else if (start > top_range)
9098 start = top_range;
9099 end = start + length;
9101 if (end < start)
9102 end = start;
9103 else if (end > top_range && ! dragging)
9104 end = top_range;
9107 /* Store the adjusted setting in the scroll bar. */
9108 XSETINT (bar->start, start);
9109 XSETINT (bar->end, end);
9111 /* Clip the end position, just for display. */
9112 if (end > top_range)
9113 end = top_range;
9115 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
9116 below top positions, to make sure the handle is always at least
9117 that many pixels tall. */
9118 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
9120 /* Draw the empty space above the handle. Note that we can't clear
9121 zero-height areas; that means "clear to end of window." */
9122 if (0 < start)
9123 x_clear_area (FRAME_X_DISPLAY (f), w,
9124 /* x, y, width, height, and exposures. */
9125 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9126 VERTICAL_SCROLL_BAR_TOP_BORDER,
9127 inside_width, start,
9128 False);
9130 /* Change to proper foreground color if one is specified. */
9131 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9132 XSetForeground (FRAME_X_DISPLAY (f), gc,
9133 f->output_data.x->scroll_bar_foreground_pixel);
9135 /* Draw the handle itself. */
9136 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
9137 /* x, y, width, height */
9138 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9139 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
9140 inside_width, end - start);
9142 /* Restore the foreground color of the GC if we changed it above. */
9143 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9144 XSetForeground (FRAME_X_DISPLAY (f), gc,
9145 f->output_data.x->foreground_pixel);
9147 /* Draw the empty space below the handle. Note that we can't
9148 clear zero-height areas; that means "clear to end of window." */
9149 if (end < inside_height)
9150 x_clear_area (FRAME_X_DISPLAY (f), w,
9151 /* x, y, width, height, and exposures. */
9152 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9153 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
9154 inside_width, inside_height - end,
9155 False);
9159 UNBLOCK_INPUT;
9162 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9164 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
9165 nil. */
9167 static void
9168 x_scroll_bar_remove (bar)
9169 struct scroll_bar *bar;
9171 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9172 BLOCK_INPUT;
9174 #ifdef USE_TOOLKIT_SCROLL_BARS
9175 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
9176 #else
9177 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9178 #endif
9180 /* Disassociate this scroll bar from its window. */
9181 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
9183 UNBLOCK_INPUT;
9187 /* Set the handle of the vertical scroll bar for WINDOW to indicate
9188 that we are displaying PORTION characters out of a total of WHOLE
9189 characters, starting at POSITION. If WINDOW has no scroll bar,
9190 create one. */
9192 static void
9193 XTset_vertical_scroll_bar (w, portion, whole, position)
9194 struct window *w;
9195 int portion, whole, position;
9197 struct frame *f = XFRAME (w->frame);
9198 struct scroll_bar *bar;
9199 int top, height, left, sb_left, width, sb_width;
9200 int window_x, window_y, window_width, window_height;
9202 /* Get window dimensions. */
9203 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
9204 top = window_y;
9205 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9206 height = window_height;
9208 /* Compute the left edge of the scroll bar area. */
9209 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9210 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
9211 else
9212 left = XFASTINT (w->left);
9213 left *= CANON_X_UNIT (f);
9214 left += FRAME_INTERNAL_BORDER_WIDTH (f);
9216 /* Compute the width of the scroll bar which might be less than
9217 the width of the area reserved for the scroll bar. */
9218 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
9219 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
9220 else
9221 sb_width = width;
9223 /* Compute the left edge of the scroll bar. */
9224 #ifdef USE_TOOLKIT_SCROLL_BARS
9225 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9226 sb_left = left + width - sb_width - (width - sb_width) / 2;
9227 else
9228 sb_left = left + (width - sb_width) / 2;
9229 #else
9230 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9231 sb_left = left + width - sb_width;
9232 else
9233 sb_left = left;
9234 #endif
9236 /* Does the scroll bar exist yet? */
9237 if (NILP (w->vertical_scroll_bar))
9239 if (width > 0 && height > 0)
9241 BLOCK_INPUT;
9242 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9243 left, top, width, height, False);
9244 UNBLOCK_INPUT;
9247 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
9249 else
9251 /* It may just need to be moved and resized. */
9252 unsigned int mask = 0;
9254 bar = XSCROLL_BAR (w->vertical_scroll_bar);
9256 BLOCK_INPUT;
9258 if (sb_left != XINT (bar->left))
9259 mask |= CWX;
9260 if (top != XINT (bar->top))
9261 mask |= CWY;
9262 if (sb_width != XINT (bar->width))
9263 mask |= CWWidth;
9264 if (height != XINT (bar->height))
9265 mask |= CWHeight;
9267 #ifdef USE_TOOLKIT_SCROLL_BARS
9269 /* Since toolkit scroll bars are smaller than the space reserved
9270 for them on the frame, we have to clear "under" them. */
9271 if (width > 0 && height > 0)
9272 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9273 left, top, width, height, False);
9275 /* Move/size the scroll bar widget. */
9276 if (mask)
9277 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
9278 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9279 top,
9280 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9281 max (height, 1), 0);
9283 #else /* not USE_TOOLKIT_SCROLL_BARS */
9285 /* Clear areas not covered by the scroll bar because of
9286 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
9287 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
9289 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9290 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9291 height, False);
9292 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9293 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9294 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9295 height, False);
9298 /* Clear areas not covered by the scroll bar because it's not as
9299 wide as the area reserved for it. This makes sure a
9300 previous mode line display is cleared after C-x 2 C-x 1, for
9301 example. */
9303 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9304 int rest = area_width - sb_width;
9305 if (rest > 0 && height > 0)
9307 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
9308 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9309 left + area_width - rest, top,
9310 rest, height, False);
9311 else
9312 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9313 left, top, rest, height, False);
9317 /* Move/size the scroll bar window. */
9318 if (mask)
9320 XWindowChanges wc;
9322 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9323 wc.y = top;
9324 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
9325 wc.height = height;
9326 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
9327 mask, &wc);
9330 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9332 /* Remember new settings. */
9333 XSETINT (bar->left, sb_left);
9334 XSETINT (bar->top, top);
9335 XSETINT (bar->width, sb_width);
9336 XSETINT (bar->height, height);
9338 UNBLOCK_INPUT;
9341 #ifdef USE_TOOLKIT_SCROLL_BARS
9342 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
9343 #else /* not USE_TOOLKIT_SCROLL_BARS */
9344 /* Set the scroll bar's current state, unless we're currently being
9345 dragged. */
9346 if (NILP (bar->dragging))
9348 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
9350 if (whole == 0)
9351 x_scroll_bar_set_handle (bar, 0, top_range, 0);
9352 else
9354 int start = ((double) position * top_range) / whole;
9355 int end = ((double) (position + portion) * top_range) / whole;
9356 x_scroll_bar_set_handle (bar, start, end, 0);
9359 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9361 XSETVECTOR (w->vertical_scroll_bar, bar);
9365 /* The following three hooks are used when we're doing a thorough
9366 redisplay of the frame. We don't explicitly know which scroll bars
9367 are going to be deleted, because keeping track of when windows go
9368 away is a real pain - "Can you say set-window-configuration, boys
9369 and girls?" Instead, we just assert at the beginning of redisplay
9370 that *all* scroll bars are to be removed, and then save a scroll bar
9371 from the fiery pit when we actually redisplay its window. */
9373 /* Arrange for all scroll bars on FRAME to be removed at the next call
9374 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
9375 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
9377 static void
9378 XTcondemn_scroll_bars (frame)
9379 FRAME_PTR frame;
9381 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
9382 while (! NILP (FRAME_SCROLL_BARS (frame)))
9384 Lisp_Object bar;
9385 bar = FRAME_SCROLL_BARS (frame);
9386 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
9387 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
9388 XSCROLL_BAR (bar)->prev = Qnil;
9389 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
9390 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
9391 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
9396 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
9397 Note that WINDOW isn't necessarily condemned at all. */
9399 static void
9400 XTredeem_scroll_bar (window)
9401 struct window *window;
9403 struct scroll_bar *bar;
9404 struct frame *f;
9406 /* We can't redeem this window's scroll bar if it doesn't have one. */
9407 if (NILP (window->vertical_scroll_bar))
9408 abort ();
9410 bar = XSCROLL_BAR (window->vertical_scroll_bar);
9412 /* Unlink it from the condemned list. */
9413 f = XFRAME (WINDOW_FRAME (window));
9414 if (NILP (bar->prev))
9416 /* If the prev pointer is nil, it must be the first in one of
9417 the lists. */
9418 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
9419 /* It's not condemned. Everything's fine. */
9420 return;
9421 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
9422 window->vertical_scroll_bar))
9423 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
9424 else
9425 /* If its prev pointer is nil, it must be at the front of
9426 one or the other! */
9427 abort ();
9429 else
9430 XSCROLL_BAR (bar->prev)->next = bar->next;
9432 if (! NILP (bar->next))
9433 XSCROLL_BAR (bar->next)->prev = bar->prev;
9435 bar->next = FRAME_SCROLL_BARS (f);
9436 bar->prev = Qnil;
9437 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9438 if (! NILP (bar->next))
9439 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9442 /* Remove all scroll bars on FRAME that haven't been saved since the
9443 last call to `*condemn_scroll_bars_hook'. */
9445 static void
9446 XTjudge_scroll_bars (f)
9447 FRAME_PTR f;
9449 Lisp_Object bar, next;
9451 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
9453 /* Clear out the condemned list now so we won't try to process any
9454 more events on the hapless scroll bars. */
9455 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
9457 for (; ! NILP (bar); bar = next)
9459 struct scroll_bar *b = XSCROLL_BAR (bar);
9461 x_scroll_bar_remove (b);
9463 next = b->next;
9464 b->next = b->prev = Qnil;
9467 /* Now there should be no references to the condemned scroll bars,
9468 and they should get garbage-collected. */
9472 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
9473 is a no-op when using toolkit scroll bars.
9475 This may be called from a signal handler, so we have to ignore GC
9476 mark bits. */
9478 static void
9479 x_scroll_bar_expose (bar, event)
9480 struct scroll_bar *bar;
9481 XEvent *event;
9483 #ifndef USE_TOOLKIT_SCROLL_BARS
9485 Window w = SCROLL_BAR_X_WINDOW (bar);
9486 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9487 GC gc = f->output_data.x->normal_gc;
9488 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9490 BLOCK_INPUT;
9492 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
9494 /* Draw a one-pixel border just inside the edges of the scroll bar. */
9495 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
9497 /* x, y, width, height */
9498 0, 0,
9499 XINT (bar->width) - 1 - width_trim - width_trim,
9500 XINT (bar->height) - 1);
9502 UNBLOCK_INPUT;
9504 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9507 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
9508 is set to something other than no_event, it is enqueued.
9510 This may be called from a signal handler, so we have to ignore GC
9511 mark bits. */
9513 #ifndef USE_TOOLKIT_SCROLL_BARS
9515 static void
9516 x_scroll_bar_handle_click (bar, event, emacs_event)
9517 struct scroll_bar *bar;
9518 XEvent *event;
9519 struct input_event *emacs_event;
9521 if (! GC_WINDOWP (bar->window))
9522 abort ();
9524 emacs_event->kind = scroll_bar_click;
9525 emacs_event->code = event->xbutton.button - Button1;
9526 emacs_event->modifiers
9527 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
9528 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
9529 event->xbutton.state)
9530 | (event->type == ButtonRelease
9531 ? up_modifier
9532 : down_modifier));
9533 emacs_event->frame_or_window = bar->window;
9534 emacs_event->arg = Qnil;
9535 emacs_event->timestamp = event->xbutton.time;
9537 #if 0
9538 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9539 int internal_height
9540 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9541 #endif
9542 int top_range
9543 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9544 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
9546 if (y < 0) y = 0;
9547 if (y > top_range) y = top_range;
9549 if (y < XINT (bar->start))
9550 emacs_event->part = scroll_bar_above_handle;
9551 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9552 emacs_event->part = scroll_bar_handle;
9553 else
9554 emacs_event->part = scroll_bar_below_handle;
9556 /* Just because the user has clicked on the handle doesn't mean
9557 they want to drag it. Lisp code needs to be able to decide
9558 whether or not we're dragging. */
9559 #if 0
9560 /* If the user has just clicked on the handle, record where they're
9561 holding it. */
9562 if (event->type == ButtonPress
9563 && emacs_event->part == scroll_bar_handle)
9564 XSETINT (bar->dragging, y - XINT (bar->start));
9565 #endif
9567 /* If the user has released the handle, set it to its final position. */
9568 if (event->type == ButtonRelease
9569 && ! NILP (bar->dragging))
9571 int new_start = y - XINT (bar->dragging);
9572 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9574 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9575 bar->dragging = Qnil;
9578 /* Same deal here as the other #if 0. */
9579 #if 0
9580 /* Clicks on the handle are always reported as occurring at the top of
9581 the handle. */
9582 if (emacs_event->part == scroll_bar_handle)
9583 emacs_event->x = bar->start;
9584 else
9585 XSETINT (emacs_event->x, y);
9586 #else
9587 XSETINT (emacs_event->x, y);
9588 #endif
9590 XSETINT (emacs_event->y, top_range);
9594 /* Handle some mouse motion while someone is dragging the scroll bar.
9596 This may be called from a signal handler, so we have to ignore GC
9597 mark bits. */
9599 static void
9600 x_scroll_bar_note_movement (bar, event)
9601 struct scroll_bar *bar;
9602 XEvent *event;
9604 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9606 last_mouse_movement_time = event->xmotion.time;
9608 f->mouse_moved = 1;
9609 XSETVECTOR (last_mouse_scroll_bar, bar);
9611 /* If we're dragging the bar, display it. */
9612 if (! GC_NILP (bar->dragging))
9614 /* Where should the handle be now? */
9615 int new_start = event->xmotion.y - XINT (bar->dragging);
9617 if (new_start != XINT (bar->start))
9619 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9621 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9626 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9628 /* Return information to the user about the current position of the mouse
9629 on the scroll bar. */
9631 static void
9632 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9633 FRAME_PTR *fp;
9634 Lisp_Object *bar_window;
9635 enum scroll_bar_part *part;
9636 Lisp_Object *x, *y;
9637 unsigned long *time;
9639 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
9640 Window w = SCROLL_BAR_X_WINDOW (bar);
9641 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9642 int win_x, win_y;
9643 Window dummy_window;
9644 int dummy_coord;
9645 unsigned int dummy_mask;
9647 BLOCK_INPUT;
9649 /* Get the mouse's position relative to the scroll bar window, and
9650 report that. */
9651 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
9653 /* Root, child, root x and root y. */
9654 &dummy_window, &dummy_window,
9655 &dummy_coord, &dummy_coord,
9657 /* Position relative to scroll bar. */
9658 &win_x, &win_y,
9660 /* Mouse buttons and modifier keys. */
9661 &dummy_mask))
9663 else
9665 #if 0
9666 int inside_height
9667 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9668 #endif
9669 int top_range
9670 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9672 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9674 if (! NILP (bar->dragging))
9675 win_y -= XINT (bar->dragging);
9677 if (win_y < 0)
9678 win_y = 0;
9679 if (win_y > top_range)
9680 win_y = top_range;
9682 *fp = f;
9683 *bar_window = bar->window;
9685 if (! NILP (bar->dragging))
9686 *part = scroll_bar_handle;
9687 else if (win_y < XINT (bar->start))
9688 *part = scroll_bar_above_handle;
9689 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9690 *part = scroll_bar_handle;
9691 else
9692 *part = scroll_bar_below_handle;
9694 XSETINT (*x, win_y);
9695 XSETINT (*y, top_range);
9697 f->mouse_moved = 0;
9698 last_mouse_scroll_bar = Qnil;
9701 *time = last_mouse_movement_time;
9703 UNBLOCK_INPUT;
9707 /* The screen has been cleared so we may have changed foreground or
9708 background colors, and the scroll bars may need to be redrawn.
9709 Clear out the scroll bars, and ask for expose events, so we can
9710 redraw them. */
9712 void
9713 x_scroll_bar_clear (f)
9714 FRAME_PTR f;
9716 #ifndef USE_TOOLKIT_SCROLL_BARS
9717 Lisp_Object bar;
9719 /* We can have scroll bars even if this is 0,
9720 if we just turned off scroll bar mode.
9721 But in that case we should not clear them. */
9722 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9723 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9724 bar = XSCROLL_BAR (bar)->next)
9725 XClearArea (FRAME_X_DISPLAY (f),
9726 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9727 0, 0, 0, 0, True);
9728 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9731 /* This processes Expose events from the menu-bar specific X event
9732 loop in xmenu.c. This allows to redisplay the frame if necessary
9733 when handling menu-bar or pop-up items. */
9736 process_expose_from_menu (event)
9737 XEvent event;
9739 FRAME_PTR f;
9740 struct x_display_info *dpyinfo;
9741 int frame_exposed_p = 0;
9743 BLOCK_INPUT;
9745 dpyinfo = x_display_info_for_display (event.xexpose.display);
9746 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9747 if (f)
9749 if (f->async_visible == 0)
9751 f->async_visible = 1;
9752 f->async_iconified = 0;
9753 f->output_data.x->has_been_visible = 1;
9754 SET_FRAME_GARBAGED (f);
9756 else
9758 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9759 event.xexpose.x, event.xexpose.y,
9760 event.xexpose.width, event.xexpose.height);
9761 frame_exposed_p = 1;
9764 else
9766 struct scroll_bar *bar
9767 = x_window_to_scroll_bar (event.xexpose.window);
9769 if (bar)
9770 x_scroll_bar_expose (bar, &event);
9773 UNBLOCK_INPUT;
9774 return frame_exposed_p;
9777 /* Define a queue to save up SelectionRequest events for later handling. */
9779 struct selection_event_queue
9781 XEvent event;
9782 struct selection_event_queue *next;
9785 static struct selection_event_queue *queue;
9787 /* Nonzero means queue up certain events--don't process them yet. */
9789 static int x_queue_selection_requests;
9791 /* Queue up an X event *EVENT, to be processed later. */
9793 static void
9794 x_queue_event (f, event)
9795 FRAME_PTR f;
9796 XEvent *event;
9798 struct selection_event_queue *queue_tmp
9799 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
9801 if (queue_tmp != NULL)
9803 queue_tmp->event = *event;
9804 queue_tmp->next = queue;
9805 queue = queue_tmp;
9809 /* Take all the queued events and put them back
9810 so that they get processed afresh. */
9812 static void
9813 x_unqueue_events (display)
9814 Display *display;
9816 while (queue != NULL)
9818 struct selection_event_queue *queue_tmp = queue;
9819 XPutBackEvent (display, &queue_tmp->event);
9820 queue = queue_tmp->next;
9821 xfree ((char *)queue_tmp);
9825 /* Start queuing SelectionRequest events. */
9827 void
9828 x_start_queuing_selection_requests (display)
9829 Display *display;
9831 x_queue_selection_requests++;
9834 /* Stop queuing SelectionRequest events. */
9836 void
9837 x_stop_queuing_selection_requests (display)
9838 Display *display;
9840 x_queue_selection_requests--;
9841 x_unqueue_events (display);
9844 /* The main X event-reading loop - XTread_socket. */
9846 #if 0
9847 /* Time stamp of enter window event. This is only used by XTread_socket,
9848 but we have to put it out here, since static variables within functions
9849 sometimes don't work. */
9851 static Time enter_timestamp;
9852 #endif
9854 /* This holds the state XLookupString needs to implement dead keys
9855 and other tricks known as "compose processing". _X Window System_
9856 says that a portable program can't use this, but Stephen Gildea assures
9857 me that letting the compiler initialize it to zeros will work okay.
9859 This must be defined outside of XTread_socket, for the same reasons
9860 given for enter_timestamp, above. */
9862 static XComposeStatus compose_status;
9864 /* Record the last 100 characters stored
9865 to help debug the loss-of-chars-during-GC problem. */
9867 static int temp_index;
9868 static short temp_buffer[100];
9870 /* Set this to nonzero to fake an "X I/O error"
9871 on a particular display. */
9873 struct x_display_info *XTread_socket_fake_io_error;
9875 /* When we find no input here, we occasionally do a no-op command
9876 to verify that the X server is still running and we can still talk with it.
9877 We try all the open displays, one by one.
9878 This variable is used for cycling thru the displays. */
9880 static struct x_display_info *next_noop_dpyinfo;
9882 #define SET_SAVED_MENU_EVENT(size) \
9883 do \
9885 if (f->output_data.x->saved_menu_event == 0) \
9886 f->output_data.x->saved_menu_event \
9887 = (XEvent *) xmalloc (sizeof (XEvent)); \
9888 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9889 if (numchars >= 1) \
9891 bufp->kind = menu_bar_activate_event; \
9892 XSETFRAME (bufp->frame_or_window, f); \
9893 bufp->arg = Qnil; \
9894 bufp++; \
9895 count++; \
9896 numchars--; \
9899 while (0)
9901 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9902 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9904 /* Read events coming from the X server.
9905 This routine is called by the SIGIO handler.
9906 We return as soon as there are no more events to be read.
9908 Events representing keys are stored in buffer BUFP,
9909 which can hold up to NUMCHARS characters.
9910 We return the number of characters stored into the buffer,
9911 thus pretending to be `read'.
9913 EXPECTED is nonzero if the caller knows input is available. */
9915 static int
9916 XTread_socket (sd, bufp, numchars, expected)
9917 register int sd;
9918 /* register */ struct input_event *bufp;
9919 /* register */ int numchars;
9920 int expected;
9922 int count = 0;
9923 int nbytes = 0;
9924 XEvent event;
9925 struct frame *f;
9926 int event_found = 0;
9927 struct x_display_info *dpyinfo;
9928 struct coding_system coding;
9930 if (interrupt_input_blocked)
9932 interrupt_input_pending = 1;
9933 return -1;
9936 interrupt_input_pending = 0;
9937 BLOCK_INPUT;
9939 /* So people can tell when we have read the available input. */
9940 input_signal_count++;
9942 if (numchars <= 0)
9943 abort (); /* Don't think this happens. */
9945 ++handling_signal;
9947 /* Find the display we are supposed to read input for.
9948 It's the one communicating on descriptor SD. */
9949 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9951 #if 0 /* This ought to be unnecessary; let's verify it. */
9952 #ifdef FIOSNBIO
9953 /* If available, Xlib uses FIOSNBIO to make the socket
9954 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
9955 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
9956 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
9957 fcntl (dpyinfo->connection, F_SETFL, 0);
9958 #endif /* ! defined (FIOSNBIO) */
9959 #endif
9961 #if 0 /* This code can't be made to work, with multiple displays,
9962 and appears not to be used on any system any more.
9963 Also keyboard.c doesn't turn O_NDELAY on and off
9964 for X connections. */
9965 #ifndef SIGIO
9966 #ifndef HAVE_SELECT
9967 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9969 extern int read_alarm_should_throw;
9970 read_alarm_should_throw = 1;
9971 XPeekEvent (dpyinfo->display, &event);
9972 read_alarm_should_throw = 0;
9974 #endif /* HAVE_SELECT */
9975 #endif /* SIGIO */
9976 #endif
9978 /* For debugging, this gives a way to fake an I/O error. */
9979 if (dpyinfo == XTread_socket_fake_io_error)
9981 XTread_socket_fake_io_error = 0;
9982 x_io_error_quitter (dpyinfo->display);
9985 #ifdef HAVE_X_SM
9986 BLOCK_INPUT;
9987 count += x_session_check_input (bufp, &numchars);
9988 UNBLOCK_INPUT;
9989 #endif
9991 while (XPending (dpyinfo->display))
9993 XNextEvent (dpyinfo->display, &event);
9995 #ifdef HAVE_X_I18N
9997 /* Filter events for the current X input method.
9998 XFilterEvent returns non-zero if the input method has
9999 consumed the event. We pass the frame's X window to
10000 XFilterEvent because that's the one for which the IC
10001 was created. */
10002 struct frame *f1 = x_any_window_to_frame (dpyinfo,
10003 event.xclient.window);
10004 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
10005 break;
10007 #endif
10008 event_found = 1;
10010 switch (event.type)
10012 case ClientMessage:
10014 if (event.xclient.message_type
10015 == dpyinfo->Xatom_wm_protocols
10016 && event.xclient.format == 32)
10018 if (event.xclient.data.l[0]
10019 == dpyinfo->Xatom_wm_take_focus)
10021 /* Use x_any_window_to_frame because this
10022 could be the shell widget window
10023 if the frame has no title bar. */
10024 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
10025 #ifdef HAVE_X_I18N
10026 /* Not quite sure this is needed -pd */
10027 if (f && FRAME_XIC (f))
10028 XSetICFocus (FRAME_XIC (f));
10029 #endif
10030 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
10031 instructs the WM to set the input focus automatically for
10032 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
10033 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
10034 it has set the focus. So, XSetInputFocus below is not
10035 needed.
10037 The call to XSetInputFocus below has also caused trouble. In
10038 cases where the XSetInputFocus done by the WM and the one
10039 below are temporally close (on a fast machine), the call
10040 below can generate additional FocusIn events which confuse
10041 Emacs. */
10043 /* Since we set WM_TAKE_FOCUS, we must call
10044 XSetInputFocus explicitly. But not if f is null,
10045 since that might be an event for a deleted frame. */
10046 if (f)
10048 Display *d = event.xclient.display;
10049 /* Catch and ignore errors, in case window has been
10050 iconified by a window manager such as GWM. */
10051 int count = x_catch_errors (d);
10052 XSetInputFocus (d, event.xclient.window,
10053 /* The ICCCM says this is
10054 the only valid choice. */
10055 RevertToParent,
10056 event.xclient.data.l[1]);
10057 /* This is needed to detect the error
10058 if there is an error. */
10059 XSync (d, False);
10060 x_uncatch_errors (d, count);
10062 /* Not certain about handling scroll bars here */
10063 #endif /* 0 */
10065 else if (event.xclient.data.l[0]
10066 == dpyinfo->Xatom_wm_save_yourself)
10068 /* Save state modify the WM_COMMAND property to
10069 something which can reinstate us. This notifies
10070 the session manager, who's looking for such a
10071 PropertyNotify. Can restart processing when
10072 a keyboard or mouse event arrives. */
10073 /* If we have a session manager, don't set this.
10074 KDE will then start two Emacsen, one for the
10075 session manager and one for this. */
10076 if (numchars > 0
10077 #ifdef HAVE_X_SM
10078 && ! x_session_have_connection ()
10079 #endif
10082 f = x_top_window_to_frame (dpyinfo,
10083 event.xclient.window);
10084 /* This is just so we only give real data once
10085 for a single Emacs process. */
10086 if (f == SELECTED_FRAME ())
10087 XSetCommand (FRAME_X_DISPLAY (f),
10088 event.xclient.window,
10089 initial_argv, initial_argc);
10090 else if (f)
10091 XSetCommand (FRAME_X_DISPLAY (f),
10092 event.xclient.window,
10093 0, 0);
10096 else if (event.xclient.data.l[0]
10097 == dpyinfo->Xatom_wm_delete_window)
10099 struct frame *f
10100 = x_any_window_to_frame (dpyinfo,
10101 event.xclient.window);
10103 if (f)
10105 if (numchars == 0)
10106 abort ();
10108 bufp->kind = delete_window_event;
10109 XSETFRAME (bufp->frame_or_window, f);
10110 bufp->arg = Qnil;
10111 bufp++;
10113 count += 1;
10114 numchars -= 1;
10118 else if (event.xclient.message_type
10119 == dpyinfo->Xatom_wm_configure_denied)
10122 else if (event.xclient.message_type
10123 == dpyinfo->Xatom_wm_window_moved)
10125 int new_x, new_y;
10126 struct frame *f
10127 = x_window_to_frame (dpyinfo, event.xclient.window);
10129 new_x = event.xclient.data.s[0];
10130 new_y = event.xclient.data.s[1];
10132 if (f)
10134 f->output_data.x->left_pos = new_x;
10135 f->output_data.x->top_pos = new_y;
10138 #ifdef HACK_EDITRES
10139 else if (event.xclient.message_type
10140 == dpyinfo->Xatom_editres)
10142 struct frame *f
10143 = x_any_window_to_frame (dpyinfo, event.xclient.window);
10144 _XEditResCheckMessages (f->output_data.x->widget, NULL,
10145 &event, NULL);
10147 #endif /* HACK_EDITRES */
10148 else if ((event.xclient.message_type
10149 == dpyinfo->Xatom_DONE)
10150 || (event.xclient.message_type
10151 == dpyinfo->Xatom_PAGE))
10153 /* Ghostview job completed. Kill it. We could
10154 reply with "Next" if we received "Page", but we
10155 currently never do because we are interested in
10156 images, only, which should have 1 page. */
10157 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
10158 struct frame *f
10159 = x_window_to_frame (dpyinfo, event.xclient.window);
10160 x_kill_gs_process (pixmap, f);
10161 expose_frame (f, 0, 0, 0, 0);
10163 #ifdef USE_TOOLKIT_SCROLL_BARS
10164 /* Scroll bar callbacks send a ClientMessage from which
10165 we construct an input_event. */
10166 else if (event.xclient.message_type
10167 == dpyinfo->Xatom_Scrollbar)
10169 x_scroll_bar_to_input_event (&event, bufp);
10170 ++bufp, ++count, --numchars;
10171 goto out;
10173 #endif /* USE_TOOLKIT_SCROLL_BARS */
10174 else
10175 goto OTHER;
10177 break;
10179 case SelectionNotify:
10180 #ifdef USE_X_TOOLKIT
10181 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
10182 goto OTHER;
10183 #endif /* not USE_X_TOOLKIT */
10184 x_handle_selection_notify (&event.xselection);
10185 break;
10187 case SelectionClear: /* Someone has grabbed ownership. */
10188 #ifdef USE_X_TOOLKIT
10189 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
10190 goto OTHER;
10191 #endif /* USE_X_TOOLKIT */
10193 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
10195 if (numchars == 0)
10196 abort ();
10198 bufp->kind = selection_clear_event;
10199 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10200 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10201 SELECTION_EVENT_TIME (bufp) = eventp->time;
10202 bufp->frame_or_window = Qnil;
10203 bufp->arg = Qnil;
10204 bufp++;
10206 count += 1;
10207 numchars -= 1;
10209 break;
10211 case SelectionRequest: /* Someone wants our selection. */
10212 #ifdef USE_X_TOOLKIT
10213 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
10214 goto OTHER;
10215 #endif /* USE_X_TOOLKIT */
10216 if (x_queue_selection_requests)
10217 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
10218 &event);
10219 else
10221 XSelectionRequestEvent *eventp
10222 = (XSelectionRequestEvent *) &event;
10224 if (numchars == 0)
10225 abort ();
10227 bufp->kind = selection_request_event;
10228 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10229 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
10230 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10231 SELECTION_EVENT_TARGET (bufp) = eventp->target;
10232 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
10233 SELECTION_EVENT_TIME (bufp) = eventp->time;
10234 bufp->frame_or_window = Qnil;
10235 bufp->arg = Qnil;
10236 bufp++;
10238 count += 1;
10239 numchars -= 1;
10241 break;
10243 case PropertyNotify:
10244 #if 0 /* This is plain wrong. In the case that we are waiting for a
10245 PropertyNotify used as an ACK in incremental selection
10246 transfer, the property will be on the receiver's window. */
10247 #if defined USE_X_TOOLKIT
10248 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
10249 goto OTHER;
10250 #endif
10251 #endif
10252 x_handle_property_notify (&event.xproperty);
10253 goto OTHER;
10255 case ReparentNotify:
10256 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
10257 if (f)
10259 int x, y;
10260 f->output_data.x->parent_desc = event.xreparent.parent;
10261 x_real_positions (f, &x, &y);
10262 f->output_data.x->left_pos = x;
10263 f->output_data.x->top_pos = y;
10265 break;
10267 case Expose:
10268 f = x_window_to_frame (dpyinfo, event.xexpose.window);
10269 if (f)
10271 x_check_fullscreen (f);
10273 if (f->async_visible == 0)
10275 f->async_visible = 1;
10276 f->async_iconified = 0;
10277 f->output_data.x->has_been_visible = 1;
10278 SET_FRAME_GARBAGED (f);
10280 else
10281 expose_frame (x_window_to_frame (dpyinfo,
10282 event.xexpose.window),
10283 event.xexpose.x, event.xexpose.y,
10284 event.xexpose.width, event.xexpose.height);
10286 else
10288 #ifndef USE_TOOLKIT_SCROLL_BARS
10289 struct scroll_bar *bar;
10290 #endif
10291 #if defined USE_LUCID
10292 /* Submenus of the Lucid menu bar aren't widgets
10293 themselves, so there's no way to dispatch events
10294 to them. Recognize this case separately. */
10296 Widget widget
10297 = x_window_to_menu_bar (event.xexpose.window);
10298 if (widget)
10299 xlwmenu_redisplay (widget);
10301 #endif /* USE_LUCID */
10303 #ifdef USE_TOOLKIT_SCROLL_BARS
10304 /* Dispatch event to the widget. */
10305 goto OTHER;
10306 #else /* not USE_TOOLKIT_SCROLL_BARS */
10307 bar = x_window_to_scroll_bar (event.xexpose.window);
10309 if (bar)
10310 x_scroll_bar_expose (bar, &event);
10311 #ifdef USE_X_TOOLKIT
10312 else
10313 goto OTHER;
10314 #endif /* USE_X_TOOLKIT */
10315 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10317 break;
10319 case GraphicsExpose: /* This occurs when an XCopyArea's
10320 source area was obscured or not
10321 available. */
10322 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
10323 if (f)
10325 expose_frame (f,
10326 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
10327 event.xgraphicsexpose.width,
10328 event.xgraphicsexpose.height);
10330 #ifdef USE_X_TOOLKIT
10331 else
10332 goto OTHER;
10333 #endif /* USE_X_TOOLKIT */
10334 break;
10336 case NoExpose: /* This occurs when an XCopyArea's
10337 source area was completely
10338 available. */
10339 break;
10341 case UnmapNotify:
10342 /* Redo the mouse-highlight after the tooltip has gone. */
10343 if (event.xmap.window == tip_window)
10345 tip_window = 0;
10346 redo_mouse_highlight ();
10349 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
10350 if (f) /* F may no longer exist if
10351 the frame was deleted. */
10353 /* While a frame is unmapped, display generation is
10354 disabled; you don't want to spend time updating a
10355 display that won't ever be seen. */
10356 f->async_visible = 0;
10357 /* We can't distinguish, from the event, whether the window
10358 has become iconified or invisible. So assume, if it
10359 was previously visible, than now it is iconified.
10360 But x_make_frame_invisible clears both
10361 the visible flag and the iconified flag;
10362 and that way, we know the window is not iconified now. */
10363 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
10365 f->async_iconified = 1;
10367 bufp->kind = iconify_event;
10368 XSETFRAME (bufp->frame_or_window, f);
10369 bufp->arg = Qnil;
10370 bufp++;
10371 count++;
10372 numchars--;
10375 goto OTHER;
10377 case MapNotify:
10378 if (event.xmap.window == tip_window)
10379 /* The tooltip has been drawn already. Avoid
10380 the SET_FRAME_GARBAGED below. */
10381 goto OTHER;
10383 /* We use x_top_window_to_frame because map events can
10384 come for sub-windows and they don't mean that the
10385 frame is visible. */
10386 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
10387 if (f)
10389 f->async_visible = 1;
10390 f->async_iconified = 0;
10391 f->output_data.x->has_been_visible = 1;
10393 /* wait_reading_process_input will notice this and update
10394 the frame's display structures. */
10395 SET_FRAME_GARBAGED (f);
10397 if (f->iconified)
10399 bufp->kind = deiconify_event;
10400 XSETFRAME (bufp->frame_or_window, f);
10401 bufp->arg = Qnil;
10402 bufp++;
10403 count++;
10404 numchars--;
10406 else if (! NILP (Vframe_list)
10407 && ! NILP (XCDR (Vframe_list)))
10408 /* Force a redisplay sooner or later
10409 to update the frame titles
10410 in case this is the second frame. */
10411 record_asynch_buffer_change ();
10413 goto OTHER;
10415 case KeyPress:
10417 /* Dispatch KeyPress events when in menu. */
10418 if (popup_activated_flag)
10419 goto OTHER;
10421 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
10423 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
10425 dpyinfo->mouse_face_hidden = 1;
10426 clear_mouse_face (dpyinfo);
10429 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
10430 if (f == 0)
10432 /* Scroll bars consume key events, but we want
10433 the keys to go to the scroll bar's frame. */
10434 Widget widget = XtWindowToWidget (dpyinfo->display,
10435 event.xkey.window);
10436 if (widget && XmIsScrollBar (widget))
10438 widget = XtParent (widget);
10439 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
10442 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
10444 if (f != 0)
10446 KeySym keysym, orig_keysym;
10447 /* al%imercury@uunet.uu.net says that making this 81
10448 instead of 80 fixed a bug whereby meta chars made
10449 his Emacs hang.
10451 It seems that some version of XmbLookupString has
10452 a bug of not returning XBufferOverflow in
10453 status_return even if the input is too long to
10454 fit in 81 bytes. So, we must prepare sufficient
10455 bytes for copy_buffer. 513 bytes (256 chars for
10456 two-byte character set) seems to be a fairly good
10457 approximation. -- 2000.8.10 handa@etl.go.jp */
10458 unsigned char copy_buffer[513];
10459 unsigned char *copy_bufptr = copy_buffer;
10460 int copy_bufsiz = sizeof (copy_buffer);
10461 int modifiers;
10462 Lisp_Object coding_system = Qlatin_1;
10464 event.xkey.state
10465 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
10466 extra_keyboard_modifiers);
10467 modifiers = event.xkey.state;
10469 /* This will have to go some day... */
10471 /* make_lispy_event turns chars into control chars.
10472 Don't do it here because XLookupString is too eager. */
10473 event.xkey.state &= ~ControlMask;
10474 event.xkey.state &= ~(dpyinfo->meta_mod_mask
10475 | dpyinfo->super_mod_mask
10476 | dpyinfo->hyper_mod_mask
10477 | dpyinfo->alt_mod_mask);
10479 /* In case Meta is ComposeCharacter,
10480 clear its status. According to Markus Ehrnsperger
10481 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
10482 this enables ComposeCharacter to work whether or
10483 not it is combined with Meta. */
10484 if (modifiers & dpyinfo->meta_mod_mask)
10485 bzero (&compose_status, sizeof (compose_status));
10487 #ifdef HAVE_X_I18N
10488 if (FRAME_XIC (f))
10490 Status status_return;
10492 coding_system = Vlocale_coding_system;
10493 nbytes = XmbLookupString (FRAME_XIC (f),
10494 &event.xkey, copy_bufptr,
10495 copy_bufsiz, &keysym,
10496 &status_return);
10497 if (status_return == XBufferOverflow)
10499 copy_bufsiz = nbytes + 1;
10500 copy_bufptr = (char *) alloca (copy_bufsiz);
10501 nbytes = XmbLookupString (FRAME_XIC (f),
10502 &event.xkey, copy_bufptr,
10503 copy_bufsiz, &keysym,
10504 &status_return);
10506 /* Xutf8LookupString is a new but already deprecated interface. -stef */
10507 #if 0 && defined X_HAVE_UTF8_STRING
10508 else if (status_return == XLookupKeySym)
10509 { /* Try again but with utf-8. */
10510 coding_system = Qutf_8;
10511 nbytes = Xutf8LookupString (FRAME_XIC (f),
10512 &event.xkey, copy_bufptr,
10513 copy_bufsiz, &keysym,
10514 &status_return);
10515 if (status_return == XBufferOverflow)
10517 copy_bufsiz = nbytes + 1;
10518 copy_bufptr = (char *) alloca (copy_bufsiz);
10519 nbytes = Xutf8LookupString (FRAME_XIC (f),
10520 &event.xkey,
10521 copy_bufptr,
10522 copy_bufsiz, &keysym,
10523 &status_return);
10526 #endif
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 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10609 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10610 /* Any "vendor-specific" key is ok. */
10611 || (orig_keysym & (1 << 28))
10612 || (keysym != NoSymbol && nbytes == 0))
10613 && ! (IsModifierKey (orig_keysym)
10614 #ifndef HAVE_X11R5
10615 #ifdef XK_Mode_switch
10616 || ((unsigned)(orig_keysym) == XK_Mode_switch)
10617 #endif
10618 #ifdef XK_Num_Lock
10619 || ((unsigned)(orig_keysym) == XK_Num_Lock)
10620 #endif
10621 #endif /* not HAVE_X11R5 */
10622 /* The symbols from XK_ISO_Lock to
10623 XK_ISO_Last_Group_Lock doesn't have real
10624 modifiers but should be treated similarly
10625 to Mode_switch by Emacs. */
10626 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
10627 || ((unsigned)(orig_keysym) >= XK_ISO_Lock
10628 && (unsigned)(orig_keysym) <= XK_ISO_Last_Group_Lock)
10629 #endif
10632 if (temp_index == sizeof temp_buffer / sizeof (short))
10633 temp_index = 0;
10634 temp_buffer[temp_index++] = keysym;
10635 bufp->kind = non_ascii_keystroke;
10636 bufp->code = keysym;
10637 XSETFRAME (bufp->frame_or_window, f);
10638 bufp->arg = Qnil;
10639 bufp->modifiers
10640 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10641 modifiers);
10642 bufp->timestamp = event.xkey.time;
10643 bufp++;
10644 count++;
10645 numchars--;
10647 else if (numchars > nbytes)
10649 register int i;
10650 register int c;
10651 int nchars, len;
10653 /* The input should be decoded with `coding_system'
10654 which depends on which X*LookupString function
10655 we used just above and the locale. */
10656 setup_coding_system (coding_system, &coding);
10657 coding.src_multibyte = 0;
10658 coding.dst_multibyte = 1;
10659 /* The input is converted to events, thus we can't
10660 handle composition. Anyway, there's no XIM that
10661 gives us composition information. */
10662 coding.composing = COMPOSITION_DISABLED;
10664 for (i = 0; i < nbytes; i++)
10666 if (temp_index == (sizeof temp_buffer
10667 / sizeof (short)))
10668 temp_index = 0;
10669 temp_buffer[temp_index++] = copy_bufptr[i];
10673 /* Decode the input data. */
10674 int require;
10675 unsigned char *p;
10677 require = decoding_buffer_size (&coding, nbytes);
10678 p = (unsigned char *) alloca (require);
10679 coding.mode |= CODING_MODE_LAST_BLOCK;
10680 decode_coding (&coding, copy_bufptr, p,
10681 nbytes, require);
10682 nbytes = coding.produced;
10683 nchars = coding.produced_char;
10684 copy_bufptr = p;
10687 /* Convert the input data to a sequence of
10688 character events. */
10689 for (i = 0; i < nbytes; i += len)
10691 if (nchars == nbytes)
10692 c = copy_bufptr[i], len = 1;
10693 else
10694 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10695 nbytes - i, len);
10697 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
10698 ? ascii_keystroke
10699 : multibyte_char_keystroke);
10700 bufp->code = c;
10701 XSETFRAME (bufp->frame_or_window, f);
10702 bufp->arg = Qnil;
10703 bufp->modifiers
10704 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10705 modifiers);
10706 bufp->timestamp = event.xkey.time;
10707 bufp++;
10710 count += nchars;
10711 numchars -= nchars;
10713 if (keysym == NoSymbol)
10714 break;
10716 else
10717 abort ();
10719 else
10720 abort ();
10722 #ifdef HAVE_X_I18N
10723 /* Don't dispatch this event since XtDispatchEvent calls
10724 XFilterEvent, and two calls in a row may freeze the
10725 client. */
10726 break;
10727 #else
10728 goto OTHER;
10729 #endif
10731 case KeyRelease:
10732 #ifdef HAVE_X_I18N
10733 /* Don't dispatch this event since XtDispatchEvent calls
10734 XFilterEvent, and two calls in a row may freeze the
10735 client. */
10736 break;
10737 #else
10738 goto OTHER;
10739 #endif
10741 /* Here's a possible interpretation of the whole
10742 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
10743 you get a FocusIn event, you have to get a FocusOut
10744 event before you relinquish the focus. If you
10745 haven't received a FocusIn event, then a mere
10746 LeaveNotify is enough to free you. */
10748 case EnterNotify:
10750 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10752 #if 0
10753 if (event.xcrossing.focus)
10755 /* Avoid nasty pop/raise loops. */
10756 if (f && (!(f->auto_raise)
10757 || !(f->auto_lower)
10758 || (event.xcrossing.time - enter_timestamp) > 500))
10760 x_new_focus_frame (dpyinfo, f);
10761 enter_timestamp = event.xcrossing.time;
10764 else if (f == dpyinfo->x_focus_frame)
10765 x_new_focus_frame (dpyinfo, 0);
10766 #endif
10768 /* EnterNotify counts as mouse movement,
10769 so update things that depend on mouse position. */
10770 if (f && !f->output_data.x->hourglass_p)
10771 note_mouse_movement (f, &event.xmotion);
10772 goto OTHER;
10775 case FocusIn:
10776 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10777 if (event.xfocus.detail != NotifyPointer)
10778 dpyinfo->x_focus_event_frame = f;
10779 if (f)
10781 x_new_focus_frame (dpyinfo, f);
10783 /* Don't stop displaying the initial startup message
10784 for a switch-frame event we don't need. */
10785 if (GC_NILP (Vterminal_frame)
10786 && GC_CONSP (Vframe_list)
10787 && !GC_NILP (XCDR (Vframe_list)))
10789 bufp->kind = FOCUS_IN_EVENT;
10790 XSETFRAME (bufp->frame_or_window, f);
10791 bufp->arg = Qnil;
10792 ++bufp, ++count, --numchars;
10796 #ifdef HAVE_X_I18N
10797 if (f && FRAME_XIC (f))
10798 XSetICFocus (FRAME_XIC (f));
10799 #endif
10801 goto OTHER;
10803 case LeaveNotify:
10804 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
10805 if (f)
10807 if (f == dpyinfo->mouse_face_mouse_frame)
10809 /* If we move outside the frame, then we're
10810 certainly no longer on any text in the frame. */
10811 clear_mouse_face (dpyinfo);
10812 dpyinfo->mouse_face_mouse_frame = 0;
10815 /* Generate a nil HELP_EVENT to cancel a help-echo.
10816 Do it only if there's something to cancel.
10817 Otherwise, the startup message is cleared when
10818 the mouse leaves the frame. */
10819 if (any_help_event_p)
10821 Lisp_Object frame;
10822 int n;
10824 XSETFRAME (frame, f);
10825 help_echo = Qnil;
10826 n = gen_help_event (bufp, numchars,
10827 Qnil, frame, Qnil, Qnil, 0);
10828 bufp += n, count += n, numchars -= n;
10831 #if 0
10832 if (event.xcrossing.focus)
10833 x_mouse_leave (dpyinfo);
10834 else
10836 if (f == dpyinfo->x_focus_event_frame)
10837 dpyinfo->x_focus_event_frame = 0;
10838 if (f == dpyinfo->x_focus_frame)
10839 x_new_focus_frame (dpyinfo, 0);
10841 #endif
10843 goto OTHER;
10845 case FocusOut:
10846 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10847 if (event.xfocus.detail != NotifyPointer
10848 && f == dpyinfo->x_focus_event_frame)
10849 dpyinfo->x_focus_event_frame = 0;
10850 if (f && f == dpyinfo->x_focus_frame)
10851 x_new_focus_frame (dpyinfo, 0);
10853 #ifdef HAVE_X_I18N
10854 if (f && FRAME_XIC (f))
10855 XUnsetICFocus (FRAME_XIC (f));
10856 #endif
10858 goto OTHER;
10860 case MotionNotify:
10862 previous_help_echo = help_echo;
10863 help_echo = help_echo_object = help_echo_window = Qnil;
10864 help_echo_pos = -1;
10866 if (dpyinfo->grabbed && last_mouse_frame
10867 && FRAME_LIVE_P (last_mouse_frame))
10868 f = last_mouse_frame;
10869 else
10870 f = x_window_to_frame (dpyinfo, event.xmotion.window);
10872 if (dpyinfo->mouse_face_hidden)
10874 dpyinfo->mouse_face_hidden = 0;
10875 clear_mouse_face (dpyinfo);
10878 if (f)
10881 /* Generate SELECT_WINDOW_EVENTs when needed. */
10882 if (mouse_autoselect_window)
10884 Lisp_Object window;
10885 int area;
10887 window = window_from_coordinates (f,
10888 event.xmotion.x, event.xmotion.y,
10889 &area, 0);
10891 /* Window will be selected only when it is not selected now and
10892 last mouse movement event was not in it. Minibuffer window
10893 will be selected iff it is active. */
10894 if (WINDOWP(window)
10895 && !EQ (window, last_window)
10896 && !EQ (window, selected_window)
10897 && numchars > 0)
10899 bufp->kind = SELECT_WINDOW_EVENT;
10900 bufp->frame_or_window = window;
10901 bufp->arg = Qnil;
10902 ++bufp, ++count, --numchars;
10905 last_window=window;
10907 note_mouse_movement (f, &event.xmotion);
10909 else
10911 #ifndef USE_TOOLKIT_SCROLL_BARS
10912 struct scroll_bar *bar
10913 = x_window_to_scroll_bar (event.xmotion.window);
10915 if (bar)
10916 x_scroll_bar_note_movement (bar, &event);
10917 #endif /* USE_TOOLKIT_SCROLL_BARS */
10919 /* If we move outside the frame, then we're
10920 certainly no longer on any text in the frame. */
10921 clear_mouse_face (dpyinfo);
10924 /* If the contents of the global variable help_echo
10925 has changed, generate a HELP_EVENT. */
10926 if (!NILP (help_echo)
10927 || !NILP (previous_help_echo))
10929 Lisp_Object frame;
10930 int n;
10932 if (f)
10933 XSETFRAME (frame, f);
10934 else
10935 frame = Qnil;
10937 any_help_event_p = 1;
10938 n = gen_help_event (bufp, numchars, help_echo, frame,
10939 help_echo_window, help_echo_object,
10940 help_echo_pos);
10941 bufp += n, count += n, numchars -= n;
10944 goto OTHER;
10947 case ConfigureNotify:
10948 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10949 if (f)
10951 #ifndef USE_X_TOOLKIT
10952 /* If there is a pending resize for fullscreen, don't
10953 do this one, the right one will come later.
10954 The toolkit version doesn't seem to need this, but we
10955 need to reset it below. */
10956 int dont_resize =
10957 ((f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
10958 && FRAME_NEW_WIDTH (f) != 0);
10959 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10960 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
10961 if (dont_resize)
10962 goto OTHER;
10964 /* In the toolkit version, change_frame_size
10965 is called by the code that handles resizing
10966 of the EmacsFrame widget. */
10968 /* Even if the number of character rows and columns has
10969 not changed, the font size may have changed, so we need
10970 to check the pixel dimensions as well. */
10971 if (columns != f->width
10972 || rows != f->height
10973 || event.xconfigure.width != f->output_data.x->pixel_width
10974 || event.xconfigure.height != f->output_data.x->pixel_height)
10976 change_frame_size (f, rows, columns, 0, 1, 0);
10977 SET_FRAME_GARBAGED (f);
10978 cancel_mouse_face (f);
10980 #endif
10982 f->output_data.x->pixel_width = event.xconfigure.width;
10983 f->output_data.x->pixel_height = event.xconfigure.height;
10985 /* What we have now is the position of Emacs's own window.
10986 Convert that to the position of the window manager window. */
10987 x_real_positions (f, &f->output_data.x->left_pos,
10988 &f->output_data.x->top_pos);
10990 x_check_fullscreen_move(f);
10991 if (f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
10992 f->output_data.x->want_fullscreen &=
10993 ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
10994 #ifdef HAVE_X_I18N
10995 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10996 xic_set_statusarea (f);
10997 #endif
10999 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
11001 /* Since the WM decorations come below top_pos now,
11002 we must put them below top_pos in the future. */
11003 f->output_data.x->win_gravity = NorthWestGravity;
11004 x_wm_set_size_hint (f, (long) 0, 0);
11006 #ifdef USE_MOTIF
11007 /* Some window managers pass (0,0) as the location of
11008 the window, and the Motif event handler stores it
11009 in the emacs widget, which messes up Motif menus. */
11010 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
11012 event.xconfigure.x = f->output_data.x->widget->core.x;
11013 event.xconfigure.y = f->output_data.x->widget->core.y;
11015 #endif /* USE_MOTIF */
11017 goto OTHER;
11019 case ButtonPress:
11020 case ButtonRelease:
11022 /* If we decide we want to generate an event to be seen
11023 by the rest of Emacs, we put it here. */
11024 struct input_event emacs_event;
11025 int tool_bar_p = 0;
11027 emacs_event.kind = no_event;
11028 bzero (&compose_status, sizeof (compose_status));
11030 if (dpyinfo->grabbed
11031 && last_mouse_frame
11032 && FRAME_LIVE_P (last_mouse_frame))
11033 f = last_mouse_frame;
11034 else
11035 f = x_window_to_frame (dpyinfo, event.xbutton.window);
11037 if (f)
11039 /* Is this in the tool-bar? */
11040 if (WINDOWP (f->tool_bar_window)
11041 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
11043 Lisp_Object window;
11044 int p, x, y;
11046 x = event.xbutton.x;
11047 y = event.xbutton.y;
11049 /* Set x and y. */
11050 window = window_from_coordinates (f, x, y, &p, 1);
11051 if (EQ (window, f->tool_bar_window))
11053 x_handle_tool_bar_click (f, &event.xbutton);
11054 tool_bar_p = 1;
11058 if (!tool_bar_p)
11059 if (!dpyinfo->x_focus_frame
11060 || f == dpyinfo->x_focus_frame)
11061 construct_mouse_click (&emacs_event, &event, f);
11063 else
11065 #ifndef USE_TOOLKIT_SCROLL_BARS
11066 struct scroll_bar *bar
11067 = x_window_to_scroll_bar (event.xbutton.window);
11069 if (bar)
11070 x_scroll_bar_handle_click (bar, &event, &emacs_event);
11071 #endif /* not USE_TOOLKIT_SCROLL_BARS */
11074 if (event.type == ButtonPress)
11076 dpyinfo->grabbed |= (1 << event.xbutton.button);
11077 last_mouse_frame = f;
11078 /* Ignore any mouse motion that happened
11079 before this event; any subsequent mouse-movement
11080 Emacs events should reflect only motion after
11081 the ButtonPress. */
11082 if (f != 0)
11083 f->mouse_moved = 0;
11085 if (!tool_bar_p)
11086 last_tool_bar_item = -1;
11088 else
11090 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
11093 if (numchars >= 1 && emacs_event.kind != no_event)
11095 bcopy (&emacs_event, bufp, sizeof (struct input_event));
11096 bufp++;
11097 count++;
11098 numchars--;
11101 #ifdef USE_X_TOOLKIT
11102 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
11103 /* For a down-event in the menu bar,
11104 don't pass it to Xt right now.
11105 Instead, save it away
11106 and we will pass it to Xt from kbd_buffer_get_event.
11107 That way, we can run some Lisp code first. */
11108 if (f && event.type == ButtonPress
11109 /* Verify the event is really within the menu bar
11110 and not just sent to it due to grabbing. */
11111 && event.xbutton.x >= 0
11112 && event.xbutton.x < f->output_data.x->pixel_width
11113 && event.xbutton.y >= 0
11114 && event.xbutton.y < f->output_data.x->menubar_height
11115 && event.xbutton.same_screen)
11117 SET_SAVED_BUTTON_EVENT;
11118 XSETFRAME (last_mouse_press_frame, f);
11120 else if (event.type == ButtonPress)
11122 last_mouse_press_frame = Qnil;
11123 goto OTHER;
11126 #ifdef USE_MOTIF /* This should do not harm for Lucid,
11127 but I am trying to be cautious. */
11128 else if (event.type == ButtonRelease)
11130 if (!NILP (last_mouse_press_frame))
11132 f = XFRAME (last_mouse_press_frame);
11133 if (f->output_data.x)
11134 SET_SAVED_BUTTON_EVENT;
11136 else
11137 goto OTHER;
11139 #endif /* USE_MOTIF */
11140 else
11141 goto OTHER;
11142 #endif /* USE_X_TOOLKIT */
11144 break;
11146 case CirculateNotify:
11147 goto OTHER;
11149 case CirculateRequest:
11150 goto OTHER;
11152 case VisibilityNotify:
11153 goto OTHER;
11155 case MappingNotify:
11156 /* Someone has changed the keyboard mapping - update the
11157 local cache. */
11158 switch (event.xmapping.request)
11160 case MappingModifier:
11161 x_find_modifier_meanings (dpyinfo);
11162 /* This is meant to fall through. */
11163 case MappingKeyboard:
11164 XRefreshKeyboardMapping (&event.xmapping);
11166 goto OTHER;
11168 default:
11169 OTHER:
11170 #ifdef USE_X_TOOLKIT
11171 BLOCK_INPUT;
11172 XtDispatchEvent (&event);
11173 UNBLOCK_INPUT;
11174 #endif /* USE_X_TOOLKIT */
11175 break;
11180 out:;
11182 /* On some systems, an X bug causes Emacs to get no more events
11183 when the window is destroyed. Detect that. (1994.) */
11184 if (! event_found)
11186 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
11187 One XNOOP in 100 loops will make Emacs terminate.
11188 B. Bretthauer, 1994 */
11189 x_noop_count++;
11190 if (x_noop_count >= 100)
11192 x_noop_count=0;
11194 if (next_noop_dpyinfo == 0)
11195 next_noop_dpyinfo = x_display_list;
11197 XNoOp (next_noop_dpyinfo->display);
11199 /* Each time we get here, cycle through the displays now open. */
11200 next_noop_dpyinfo = next_noop_dpyinfo->next;
11204 /* If the focus was just given to an auto-raising frame,
11205 raise it now. */
11206 /* ??? This ought to be able to handle more than one such frame. */
11207 if (pending_autoraise_frame)
11209 x_raise_frame (pending_autoraise_frame);
11210 pending_autoraise_frame = 0;
11213 UNBLOCK_INPUT;
11214 --handling_signal;
11215 return count;
11221 /***********************************************************************
11222 Text Cursor
11223 ***********************************************************************/
11225 /* Notice when the text cursor of window W has been completely
11226 overwritten by a drawing operation that outputs glyphs in AREA
11227 starting at X0 and ending at X1 in the line starting at Y0 and
11228 ending at Y1. X coordinates are area-relative. X1 < 0 means all
11229 the rest of the line after X0 has been written. Y coordinates
11230 are window-relative. */
11232 static void
11233 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
11234 struct window *w;
11235 enum glyph_row_area area;
11236 int x0, y0, x1, y1;
11238 if (area == TEXT_AREA && w->phys_cursor_on_p)
11240 int cx0 = w->phys_cursor.x;
11241 int cx1 = cx0 + w->phys_cursor_width;
11242 int cy0 = w->phys_cursor.y;
11243 int cy1 = cy0 + w->phys_cursor_height;
11245 if (x0 <= cx0 && (x1 < 0 || x1 >= cx1))
11247 /* The cursor image will be completely removed from the
11248 screen if the output area intersects the cursor area in
11249 y-direction. When we draw in [y0 y1[, and some part of
11250 the cursor is at y < y0, that part must have been drawn
11251 before. When scrolling, the cursor is erased before
11252 actually scrolling, so we don't come here. When not
11253 scrolling, the rows above the old cursor row must have
11254 changed, and in this case these rows must have written
11255 over the cursor image.
11257 Likewise if part of the cursor is below y1, with the
11258 exception of the cursor being in the first blank row at
11259 the buffer and window end because update_text_area
11260 doesn't draw that row. (Except when it does, but
11261 that's handled in update_text_area.) */
11263 if (((y0 >= cy0 && y0 < cy1) || (y1 > cy0 && y1 < cy1))
11264 && w->current_matrix->rows[w->phys_cursor.vpos].displays_text_p)
11265 w->phys_cursor_on_p = 0;
11271 /* Set clipping for output in glyph row ROW. W is the window in which
11272 we operate. GC is the graphics context to set clipping in.
11273 WHOLE_LINE_P non-zero means include the areas used for truncation
11274 mark display and alike in the clipping rectangle.
11276 ROW may be a text row or, e.g., a mode line. Text rows must be
11277 clipped to the interior of the window dedicated to text display,
11278 mode lines must be clipped to the whole window. */
11280 static void
11281 x_clip_to_row (w, row, gc, whole_line_p)
11282 struct window *w;
11283 struct glyph_row *row;
11284 GC gc;
11285 int whole_line_p;
11287 struct frame *f = XFRAME (WINDOW_FRAME (w));
11288 XRectangle clip_rect;
11289 int window_x, window_y, window_width, window_height;
11291 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
11293 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
11294 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
11295 clip_rect.y = max (clip_rect.y, window_y);
11296 clip_rect.width = window_width;
11297 clip_rect.height = row->visible_height;
11299 /* If clipping to the whole line, including trunc marks, extend
11300 the rectangle to the left and increase its width. */
11301 if (whole_line_p)
11303 clip_rect.x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
11304 clip_rect.width += FRAME_X_FRINGE_WIDTH (f);
11307 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
11311 /* Draw a hollow box cursor on window W in glyph row ROW. */
11313 static void
11314 x_draw_hollow_cursor (w, row)
11315 struct window *w;
11316 struct glyph_row *row;
11318 struct frame *f = XFRAME (WINDOW_FRAME (w));
11319 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11320 Display *dpy = FRAME_X_DISPLAY (f);
11321 int x, y, wd, h;
11322 XGCValues xgcv;
11323 struct glyph *cursor_glyph;
11324 GC gc;
11326 /* Compute frame-relative coordinates from window-relative
11327 coordinates. */
11328 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11329 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
11330 + row->ascent - w->phys_cursor_ascent);
11331 h = row->height - 1;
11333 /* Get the glyph the cursor is on. If we can't tell because
11334 the current matrix is invalid or such, give up. */
11335 cursor_glyph = get_phys_cursor_glyph (w);
11336 if (cursor_glyph == NULL)
11337 return;
11339 /* Compute the width of the rectangle to draw. If on a stretch
11340 glyph, and `x-stretch-block-cursor' is nil, don't draw a
11341 rectangle as wide as the glyph, but use a canonical character
11342 width instead. */
11343 wd = cursor_glyph->pixel_width - 1;
11344 if (cursor_glyph->type == STRETCH_GLYPH
11345 && !x_stretch_cursor_p)
11346 wd = min (CANON_X_UNIT (f), wd);
11347 w->phys_cursor_width = wd;
11349 /* The foreground of cursor_gc is typically the same as the normal
11350 background color, which can cause the cursor box to be invisible. */
11351 xgcv.foreground = f->output_data.x->cursor_pixel;
11352 if (dpyinfo->scratch_cursor_gc)
11353 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
11354 else
11355 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
11356 GCForeground, &xgcv);
11357 gc = dpyinfo->scratch_cursor_gc;
11359 /* Set clipping, draw the rectangle, and reset clipping again. */
11360 x_clip_to_row (w, row, gc, 0);
11361 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
11362 XSetClipMask (dpy, gc, None);
11366 /* Draw a bar cursor on window W in glyph row ROW.
11368 Implementation note: One would like to draw a bar cursor with an
11369 angle equal to the one given by the font property XA_ITALIC_ANGLE.
11370 Unfortunately, I didn't find a font yet that has this property set.
11371 --gerd. */
11373 static void
11374 x_draw_bar_cursor (w, row, width, kind)
11375 struct window *w;
11376 struct glyph_row *row;
11377 int width;
11378 enum text_cursor_kinds kind;
11380 struct frame *f = XFRAME (w->frame);
11381 struct glyph *cursor_glyph;
11383 /* If cursor is out of bounds, don't draw garbage. This can happen
11384 in mini-buffer windows when switching between echo area glyphs
11385 and mini-buffer. */
11386 cursor_glyph = get_phys_cursor_glyph (w);
11387 if (cursor_glyph == NULL)
11388 return;
11390 /* If on an image, draw like a normal cursor. That's usually better
11391 visible than drawing a bar, esp. if the image is large so that
11392 the bar might not be in the window. */
11393 if (cursor_glyph->type == IMAGE_GLYPH)
11395 struct glyph_row *row;
11396 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
11397 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
11399 else
11401 Display *dpy = FRAME_X_DISPLAY (f);
11402 Window window = FRAME_X_WINDOW (f);
11403 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
11404 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
11405 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
11406 XGCValues xgcv;
11408 /* If the glyph's background equals the color we normally draw
11409 the bar cursor in, the bar cursor in its normal color is
11410 invisible. Use the glyph's foreground color instead in this
11411 case, on the assumption that the glyph's colors are chosen so
11412 that the glyph is legible. */
11413 if (face->background == f->output_data.x->cursor_pixel)
11414 xgcv.background = xgcv.foreground = face->foreground;
11415 else
11416 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
11417 xgcv.graphics_exposures = 0;
11419 if (gc)
11420 XChangeGC (dpy, gc, mask, &xgcv);
11421 else
11423 gc = XCreateGC (dpy, window, mask, &xgcv);
11424 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
11427 if (width < 0)
11428 width = f->output_data.x->cursor_width;
11429 width = min (cursor_glyph->pixel_width, width);
11431 w->phys_cursor_width = width;
11432 x_clip_to_row (w, row, gc, 0);
11434 if (kind == BAR_CURSOR)
11435 XFillRectangle (dpy, window, gc,
11436 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11437 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
11438 width, row->height);
11439 else
11440 XFillRectangle (dpy, window, gc,
11441 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11442 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
11443 row->height - width),
11444 cursor_glyph->pixel_width,
11445 width);
11447 XSetClipMask (dpy, gc, None);
11452 /* Clear the cursor of window W to background color, and mark the
11453 cursor as not shown. This is used when the text where the cursor
11454 is is about to be rewritten. */
11456 static void
11457 x_clear_cursor (w)
11458 struct window *w;
11460 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
11461 x_update_window_cursor (w, 0);
11465 /* Draw the cursor glyph of window W in glyph row ROW. See the
11466 comment of x_draw_glyphs for the meaning of HL. */
11468 static void
11469 x_draw_phys_cursor_glyph (w, row, hl)
11470 struct window *w;
11471 struct glyph_row *row;
11472 enum draw_glyphs_face hl;
11474 /* If cursor hpos is out of bounds, don't draw garbage. This can
11475 happen in mini-buffer windows when switching between echo area
11476 glyphs and mini-buffer. */
11477 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
11479 int on_p = w->phys_cursor_on_p;
11480 int x1;
11482 x1 = x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
11483 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
11484 hl, 0);
11485 w->phys_cursor_on_p = on_p;
11487 if (hl == DRAW_CURSOR)
11488 w->phys_cursor_width = x1 - w->phys_cursor.x;
11490 /* When we erase the cursor, and ROW is overlapped by other
11491 rows, make sure that these overlapping parts of other rows
11492 are redrawn. */
11493 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
11495 if (row > w->current_matrix->rows
11496 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
11497 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
11499 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
11500 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
11501 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
11507 /* Erase the image of a cursor of window W from the screen. */
11509 static void
11510 x_erase_phys_cursor (w)
11511 struct window *w;
11513 struct frame *f = XFRAME (w->frame);
11514 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11515 int hpos = w->phys_cursor.hpos;
11516 int vpos = w->phys_cursor.vpos;
11517 int mouse_face_here_p = 0;
11518 struct glyph_matrix *active_glyphs = w->current_matrix;
11519 struct glyph_row *cursor_row;
11520 struct glyph *cursor_glyph;
11521 enum draw_glyphs_face hl;
11523 /* No cursor displayed or row invalidated => nothing to do on the
11524 screen. */
11525 if (w->phys_cursor_type == NO_CURSOR)
11526 goto mark_cursor_off;
11528 /* VPOS >= active_glyphs->nrows means that window has been resized.
11529 Don't bother to erase the cursor. */
11530 if (vpos >= active_glyphs->nrows)
11531 goto mark_cursor_off;
11533 /* If row containing cursor is marked invalid, there is nothing we
11534 can do. */
11535 cursor_row = MATRIX_ROW (active_glyphs, vpos);
11536 if (!cursor_row->enabled_p)
11537 goto mark_cursor_off;
11539 /* If row is completely invisible, don't attempt to delete a cursor which
11540 isn't there. This can happen if cursor is at top of a window, and
11541 we switch to a buffer with a header line in that window. */
11542 if (cursor_row->visible_height <= 0)
11543 goto mark_cursor_off;
11545 /* This can happen when the new row is shorter than the old one.
11546 In this case, either x_draw_glyphs or clear_end_of_line
11547 should have cleared the cursor. Note that we wouldn't be
11548 able to erase the cursor in this case because we don't have a
11549 cursor glyph at hand. */
11550 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
11551 goto mark_cursor_off;
11553 /* If the cursor is in the mouse face area, redisplay that when
11554 we clear the cursor. */
11555 if (! NILP (dpyinfo->mouse_face_window)
11556 && w == XWINDOW (dpyinfo->mouse_face_window)
11557 && (vpos > dpyinfo->mouse_face_beg_row
11558 || (vpos == dpyinfo->mouse_face_beg_row
11559 && hpos >= dpyinfo->mouse_face_beg_col))
11560 && (vpos < dpyinfo->mouse_face_end_row
11561 || (vpos == dpyinfo->mouse_face_end_row
11562 && hpos < dpyinfo->mouse_face_end_col))
11563 /* Don't redraw the cursor's spot in mouse face if it is at the
11564 end of a line (on a newline). The cursor appears there, but
11565 mouse highlighting does not. */
11566 && cursor_row->used[TEXT_AREA] > hpos)
11567 mouse_face_here_p = 1;
11569 /* Maybe clear the display under the cursor. */
11570 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
11572 int x;
11573 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11575 cursor_glyph = get_phys_cursor_glyph (w);
11576 if (cursor_glyph == NULL)
11577 goto mark_cursor_off;
11579 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11581 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11583 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
11584 cursor_row->y)),
11585 cursor_glyph->pixel_width,
11586 cursor_row->visible_height,
11587 False);
11590 /* Erase the cursor by redrawing the character underneath it. */
11591 if (mouse_face_here_p)
11592 hl = DRAW_MOUSE_FACE;
11593 else
11594 hl = DRAW_NORMAL_TEXT;
11595 x_draw_phys_cursor_glyph (w, cursor_row, hl);
11597 mark_cursor_off:
11598 w->phys_cursor_on_p = 0;
11599 w->phys_cursor_type = NO_CURSOR;
11603 /* Non-zero if physical cursor of window W is within mouse face. */
11605 static int
11606 cursor_in_mouse_face_p (w)
11607 struct window *w;
11609 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
11610 int in_mouse_face = 0;
11612 if (WINDOWP (dpyinfo->mouse_face_window)
11613 && XWINDOW (dpyinfo->mouse_face_window) == w)
11615 int hpos = w->phys_cursor.hpos;
11616 int vpos = w->phys_cursor.vpos;
11618 if (vpos >= dpyinfo->mouse_face_beg_row
11619 && vpos <= dpyinfo->mouse_face_end_row
11620 && (vpos > dpyinfo->mouse_face_beg_row
11621 || hpos >= dpyinfo->mouse_face_beg_col)
11622 && (vpos < dpyinfo->mouse_face_end_row
11623 || hpos < dpyinfo->mouse_face_end_col
11624 || dpyinfo->mouse_face_past_end))
11625 in_mouse_face = 1;
11628 return in_mouse_face;
11632 /* Display or clear cursor of window W. If ON is zero, clear the
11633 cursor. If it is non-zero, display the cursor. If ON is nonzero,
11634 where to put the cursor is specified by HPOS, VPOS, X and Y. */
11636 void
11637 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
11638 struct window *w;
11639 int on, hpos, vpos, x, y;
11641 struct frame *f = XFRAME (w->frame);
11642 int new_cursor_type;
11643 int new_cursor_width;
11644 struct glyph_matrix *current_glyphs;
11645 struct glyph_row *glyph_row;
11646 struct glyph *glyph;
11648 /* This is pointless on invisible frames, and dangerous on garbaged
11649 windows and frames; in the latter case, the frame or window may
11650 be in the midst of changing its size, and x and y may be off the
11651 window. */
11652 if (! FRAME_VISIBLE_P (f)
11653 || FRAME_GARBAGED_P (f)
11654 || vpos >= w->current_matrix->nrows
11655 || hpos >= w->current_matrix->matrix_w)
11656 return;
11658 /* If cursor is off and we want it off, return quickly. */
11659 if (!on && !w->phys_cursor_on_p)
11660 return;
11662 current_glyphs = w->current_matrix;
11663 glyph_row = MATRIX_ROW (current_glyphs, vpos);
11664 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
11666 /* If cursor row is not enabled, we don't really know where to
11667 display the cursor. */
11668 if (!glyph_row->enabled_p)
11670 w->phys_cursor_on_p = 0;
11671 return;
11674 xassert (interrupt_input_blocked);
11676 /* Set new_cursor_type to the cursor we want to be displayed. In a
11677 mini-buffer window, we want the cursor only to appear if we are
11678 reading input from this window. For the selected window, we want
11679 the cursor type given by the frame parameter. If explicitly
11680 marked off, draw no cursor. In all other cases, we want a hollow
11681 box cursor. */
11682 new_cursor_width = -1;
11683 if (cursor_in_echo_area
11684 && FRAME_HAS_MINIBUF_P (f)
11685 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
11687 if (w == XWINDOW (echo_area_window))
11688 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11689 else if (!NILP (Fbuffer_local_value (Qcursor_in_non_selected_windows,
11690 w->buffer)))
11691 new_cursor_type = HOLLOW_BOX_CURSOR;
11692 else
11693 new_cursor_type = NO_CURSOR;
11695 else
11697 if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
11698 || w != XWINDOW (f->selected_window))
11700 if ((MINI_WINDOW_P (w) && minibuf_level == 0)
11701 || NILP (Fbuffer_local_value (Qcursor_in_non_selected_windows,
11702 w->buffer))
11703 || NILP (XBUFFER (w->buffer)->cursor_type))
11704 new_cursor_type = NO_CURSOR;
11705 else
11706 new_cursor_type = HOLLOW_BOX_CURSOR;
11708 else
11710 struct buffer *b = XBUFFER (w->buffer);
11712 if (EQ (b->cursor_type, Qt))
11713 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11714 else
11715 new_cursor_type = x_specified_cursor_type (b->cursor_type,
11716 &new_cursor_width);
11717 if (w->cursor_off_p)
11719 if (new_cursor_type == FILLED_BOX_CURSOR)
11720 new_cursor_type = HOLLOW_BOX_CURSOR;
11721 else if (new_cursor_type == BAR_CURSOR && new_cursor_width > 1)
11722 new_cursor_width = 1;
11723 else
11724 new_cursor_type = NO_CURSOR;
11729 /* If cursor is currently being shown and we don't want it to be or
11730 it is in the wrong place, or the cursor type is not what we want,
11731 erase it. */
11732 if (w->phys_cursor_on_p
11733 && (!on
11734 || w->phys_cursor.x != x
11735 || w->phys_cursor.y != y
11736 || new_cursor_type != w->phys_cursor_type
11737 || (new_cursor_type == BAR_CURSOR
11738 && new_cursor_width != w->phys_cursor_width)))
11739 x_erase_phys_cursor (w);
11741 /* Don't check phys_cursor_on_p here because that flag is only set
11742 to zero in some cases where we know that the cursor has been
11743 completely erased, to avoid the extra work of erasing the cursor
11744 twice. In other words, phys_cursor_on_p can be 1 and the cursor
11745 still not be visible, or it has only been partly erased. */
11746 if (on)
11748 w->phys_cursor_ascent = glyph_row->ascent;
11749 w->phys_cursor_height = glyph_row->height;
11751 /* Set phys_cursor_.* before x_draw_.* is called because some
11752 of them may need the information. */
11753 w->phys_cursor.x = x;
11754 w->phys_cursor.y = glyph_row->y;
11755 w->phys_cursor.hpos = hpos;
11756 w->phys_cursor.vpos = vpos;
11757 w->phys_cursor_type = new_cursor_type;
11758 w->phys_cursor_on_p = 1;
11760 switch (new_cursor_type)
11762 case HOLLOW_BOX_CURSOR:
11763 x_draw_hollow_cursor (w, glyph_row);
11764 break;
11766 case FILLED_BOX_CURSOR:
11767 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
11768 break;
11770 case BAR_CURSOR:
11771 x_draw_bar_cursor (w, glyph_row, new_cursor_width, BAR_CURSOR);
11772 break;
11774 case HBAR_CURSOR:
11775 x_draw_bar_cursor (w, glyph_row, new_cursor_width, HBAR_CURSOR);
11776 break;
11778 case NO_CURSOR:
11779 w->phys_cursor_width = 0;
11780 break;
11782 default:
11783 abort ();
11786 #ifdef HAVE_X_I18N
11787 if (w == XWINDOW (f->selected_window))
11788 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
11789 xic_set_preeditarea (w, x, y);
11790 #endif
11793 #ifndef XFlush
11794 if (updating_frame != f)
11795 XFlush (FRAME_X_DISPLAY (f));
11796 #endif
11800 /* Display the cursor on window W, or clear it. X and Y are window
11801 relative pixel coordinates. HPOS and VPOS are glyph matrix
11802 positions. If W is not the selected window, display a hollow
11803 cursor. ON non-zero means display the cursor at X, Y which
11804 correspond to HPOS, VPOS, otherwise it is cleared. */
11806 void
11807 x_display_cursor (w, on, hpos, vpos, x, y)
11808 struct window *w;
11809 int on, hpos, vpos, x, y;
11811 BLOCK_INPUT;
11812 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
11813 UNBLOCK_INPUT;
11817 /* Display the cursor on window W, or clear it, according to ON_P.
11818 Don't change the cursor's position. */
11820 void
11821 x_update_cursor (f, on_p)
11822 struct frame *f;
11824 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11828 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
11829 in the window tree rooted at W. */
11831 static void
11832 x_update_cursor_in_window_tree (w, on_p)
11833 struct window *w;
11834 int on_p;
11836 while (w)
11838 if (!NILP (w->hchild))
11839 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11840 else if (!NILP (w->vchild))
11841 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11842 else
11843 x_update_window_cursor (w, on_p);
11845 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11850 /* Switch the display of W's cursor on or off, according to the value
11851 of ON. */
11853 static void
11854 x_update_window_cursor (w, on)
11855 struct window *w;
11856 int on;
11858 /* Don't update cursor in windows whose frame is in the process
11859 of being deleted. */
11860 if (w->current_matrix)
11862 BLOCK_INPUT;
11863 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11864 w->phys_cursor.x, w->phys_cursor.y);
11865 UNBLOCK_INPUT;
11872 /* Icons. */
11874 /* Make the x-window of frame F use the gnu icon bitmap. */
11877 x_bitmap_icon (f, file)
11878 struct frame *f;
11879 Lisp_Object file;
11881 int bitmap_id;
11883 if (FRAME_X_WINDOW (f) == 0)
11884 return 1;
11886 /* Free up our existing icon bitmap if any. */
11887 if (f->output_data.x->icon_bitmap > 0)
11888 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11889 f->output_data.x->icon_bitmap = 0;
11891 if (STRINGP (file))
11892 bitmap_id = x_create_bitmap_from_file (f, file);
11893 else
11895 /* Create the GNU bitmap if necessary. */
11896 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
11897 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11898 = x_create_bitmap_from_data (f, gnu_bits,
11899 gnu_width, gnu_height);
11901 /* The first time we create the GNU bitmap,
11902 this increments the ref-count one extra time.
11903 As a result, the GNU bitmap is never freed.
11904 That way, we don't have to worry about allocating it again. */
11905 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
11907 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
11910 x_wm_set_icon_pixmap (f, bitmap_id);
11911 f->output_data.x->icon_bitmap = bitmap_id;
11913 return 0;
11917 /* Make the x-window of frame F use a rectangle with text.
11918 Use ICON_NAME as the text. */
11921 x_text_icon (f, icon_name)
11922 struct frame *f;
11923 char *icon_name;
11925 if (FRAME_X_WINDOW (f) == 0)
11926 return 1;
11928 #ifdef HAVE_X11R4
11930 XTextProperty text;
11931 text.value = (unsigned char *) icon_name;
11932 text.encoding = XA_STRING;
11933 text.format = 8;
11934 text.nitems = strlen (icon_name);
11935 #ifdef USE_X_TOOLKIT
11936 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11937 &text);
11938 #else /* not USE_X_TOOLKIT */
11939 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11940 #endif /* not USE_X_TOOLKIT */
11942 #else /* not HAVE_X11R4 */
11943 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11944 #endif /* not HAVE_X11R4 */
11946 if (f->output_data.x->icon_bitmap > 0)
11947 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11948 f->output_data.x->icon_bitmap = 0;
11949 x_wm_set_icon_pixmap (f, 0);
11951 return 0;
11954 #define X_ERROR_MESSAGE_SIZE 200
11956 /* If non-nil, this should be a string.
11957 It means catch X errors and store the error message in this string. */
11959 static Lisp_Object x_error_message_string;
11961 /* An X error handler which stores the error message in
11962 x_error_message_string. This is called from x_error_handler if
11963 x_catch_errors is in effect. */
11965 static void
11966 x_error_catcher (display, error)
11967 Display *display;
11968 XErrorEvent *error;
11970 XGetErrorText (display, error->error_code,
11971 XSTRING (x_error_message_string)->data,
11972 X_ERROR_MESSAGE_SIZE);
11975 /* Begin trapping X errors for display DPY. Actually we trap X errors
11976 for all displays, but DPY should be the display you are actually
11977 operating on.
11979 After calling this function, X protocol errors no longer cause
11980 Emacs to exit; instead, they are recorded in the string
11981 stored in x_error_message_string.
11983 Calling x_check_errors signals an Emacs error if an X error has
11984 occurred since the last call to x_catch_errors or x_check_errors.
11986 Calling x_uncatch_errors resumes the normal error handling. */
11988 void x_check_errors ();
11989 static Lisp_Object x_catch_errors_unwind ();
11992 x_catch_errors (dpy)
11993 Display *dpy;
11995 int count = specpdl_ptr - specpdl;
11997 /* Make sure any errors from previous requests have been dealt with. */
11998 XSync (dpy, False);
12000 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
12002 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
12003 XSTRING (x_error_message_string)->data[0] = 0;
12005 return count;
12008 /* Unbind the binding that we made to check for X errors. */
12010 static Lisp_Object
12011 x_catch_errors_unwind (old_val)
12012 Lisp_Object old_val;
12014 x_error_message_string = old_val;
12015 return Qnil;
12018 /* If any X protocol errors have arrived since the last call to
12019 x_catch_errors or x_check_errors, signal an Emacs error using
12020 sprintf (a buffer, FORMAT, the x error message text) as the text. */
12022 void
12023 x_check_errors (dpy, format)
12024 Display *dpy;
12025 char *format;
12027 /* Make sure to catch any errors incurred so far. */
12028 XSync (dpy, False);
12030 if (XSTRING (x_error_message_string)->data[0])
12031 error (format, XSTRING (x_error_message_string)->data);
12034 /* Nonzero if we had any X protocol errors
12035 since we did x_catch_errors on DPY. */
12038 x_had_errors_p (dpy)
12039 Display *dpy;
12041 /* Make sure to catch any errors incurred so far. */
12042 XSync (dpy, False);
12044 return XSTRING (x_error_message_string)->data[0] != 0;
12047 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
12049 void
12050 x_clear_errors (dpy)
12051 Display *dpy;
12053 XSTRING (x_error_message_string)->data[0] = 0;
12056 /* Stop catching X protocol errors and let them make Emacs die.
12057 DPY should be the display that was passed to x_catch_errors.
12058 COUNT should be the value that was returned by
12059 the corresponding call to x_catch_errors. */
12061 void
12062 x_uncatch_errors (dpy, count)
12063 Display *dpy;
12064 int count;
12066 unbind_to (count, Qnil);
12069 #if 0
12070 static unsigned int x_wire_count;
12071 x_trace_wire ()
12073 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
12075 #endif /* ! 0 */
12078 /* Handle SIGPIPE, which can happen when the connection to a server
12079 simply goes away. SIGPIPE is handled by x_connection_signal.
12080 Don't need to do anything, because the write which caused the
12081 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
12082 which will do the appropriate cleanup for us. */
12084 static SIGTYPE
12085 x_connection_signal (signalnum) /* If we don't have an argument, */
12086 int signalnum; /* some compilers complain in signal calls. */
12088 #ifdef USG
12089 /* USG systems forget handlers when they are used;
12090 must reestablish each time */
12091 signal (signalnum, x_connection_signal);
12092 #endif /* USG */
12096 /************************************************************************
12097 Handling X errors
12098 ************************************************************************/
12100 /* Error message passed to x_connection_closed. */
12102 static char *error_msg;
12104 /* Function installed as fatal_error_signal_hook in
12105 x_connection_closed. Print the X error message, and exit normally,
12106 instead of dumping core when XtCloseDisplay fails. */
12108 static void
12109 x_fatal_error_signal ()
12111 fprintf (stderr, "%s\n", error_msg);
12112 exit (70);
12115 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
12116 the text of an error message that lead to the connection loss. */
12118 static SIGTYPE
12119 x_connection_closed (dpy, error_message)
12120 Display *dpy;
12121 char *error_message;
12123 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
12124 Lisp_Object frame, tail;
12125 int count;
12127 error_msg = (char *) alloca (strlen (error_message) + 1);
12128 strcpy (error_msg, error_message);
12129 handling_signal = 0;
12131 /* Prevent being called recursively because of an error condition
12132 below. Otherwise, we might end up with printing ``can't find per
12133 display information'' in the recursive call instead of printing
12134 the original message here. */
12135 count = x_catch_errors (dpy);
12137 /* We have to close the display to inform Xt that it doesn't
12138 exist anymore. If we don't, Xt will continue to wait for
12139 events from the display. As a consequence, a sequence of
12141 M-x make-frame-on-display RET :1 RET
12142 ...kill the new frame, so that we get an IO error...
12143 M-x make-frame-on-display RET :1 RET
12145 will indefinitely wait in Xt for events for display `:1', opened
12146 in the first class to make-frame-on-display.
12148 Closing the display is reported to lead to a bus error on
12149 OpenWindows in certain situations. I suspect that is a bug
12150 in OpenWindows. I don't know how to cicumvent it here. */
12152 #ifdef USE_X_TOOLKIT
12153 /* If DPYINFO is null, this means we didn't open the display
12154 in the first place, so don't try to close it. */
12155 if (dpyinfo)
12157 extern void (*fatal_error_signal_hook) P_ ((void));
12158 fatal_error_signal_hook = x_fatal_error_signal;
12159 XtCloseDisplay (dpy);
12160 fatal_error_signal_hook = NULL;
12162 #endif
12164 /* Indicate that this display is dead. */
12165 if (dpyinfo)
12166 dpyinfo->display = 0;
12168 /* First delete frames whose mini-buffers are on frames
12169 that are on the dead display. */
12170 FOR_EACH_FRAME (tail, frame)
12172 Lisp_Object minibuf_frame;
12173 minibuf_frame
12174 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
12175 if (FRAME_X_P (XFRAME (frame))
12176 && FRAME_X_P (XFRAME (minibuf_frame))
12177 && ! EQ (frame, minibuf_frame)
12178 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
12179 Fdelete_frame (frame, Qt);
12182 /* Now delete all remaining frames on the dead display.
12183 We are now sure none of these is used as the mini-buffer
12184 for another frame that we need to delete. */
12185 FOR_EACH_FRAME (tail, frame)
12186 if (FRAME_X_P (XFRAME (frame))
12187 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
12189 /* Set this to t so that Fdelete_frame won't get confused
12190 trying to find a replacement. */
12191 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
12192 Fdelete_frame (frame, Qt);
12195 if (dpyinfo)
12196 x_delete_display (dpyinfo);
12198 x_uncatch_errors (dpy, count);
12200 if (x_display_list == 0)
12202 fprintf (stderr, "%s\n", error_msg);
12203 shut_down_emacs (0, 0, Qnil);
12204 exit (70);
12207 /* Ordinary stack unwind doesn't deal with these. */
12208 #ifdef SIGIO
12209 sigunblock (sigmask (SIGIO));
12210 #endif
12211 sigunblock (sigmask (SIGALRM));
12212 TOTALLY_UNBLOCK_INPUT;
12214 clear_waiting_for_input ();
12215 error ("%s", error_msg);
12219 /* This is the usual handler for X protocol errors.
12220 It kills all frames on the display that we got the error for.
12221 If that was the only one, it prints an error message and kills Emacs. */
12223 static void
12224 x_error_quitter (display, error)
12225 Display *display;
12226 XErrorEvent *error;
12228 char buf[256], buf1[356];
12230 /* Note that there is no real way portable across R3/R4 to get the
12231 original error handler. */
12233 XGetErrorText (display, error->error_code, buf, sizeof (buf));
12234 sprintf (buf1, "X protocol error: %s on protocol request %d",
12235 buf, error->request_code);
12236 x_connection_closed (display, buf1);
12240 /* This is the first-level handler for X protocol errors.
12241 It calls x_error_quitter or x_error_catcher. */
12243 static int
12244 x_error_handler (display, error)
12245 Display *display;
12246 XErrorEvent *error;
12248 if (! NILP (x_error_message_string))
12249 x_error_catcher (display, error);
12250 else
12251 x_error_quitter (display, error);
12252 return 0;
12255 /* This is the handler for X IO errors, always.
12256 It kills all frames on the display that we lost touch with.
12257 If that was the only one, it prints an error message and kills Emacs. */
12259 static int
12260 x_io_error_quitter (display)
12261 Display *display;
12263 char buf[256];
12265 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
12266 x_connection_closed (display, buf);
12267 return 0;
12270 /* Changing the font of the frame. */
12272 /* Give frame F the font named FONTNAME as its default font, and
12273 return the full name of that font. FONTNAME may be a wildcard
12274 pattern; in that case, we choose some font that fits the pattern.
12275 The return value shows which font we chose. */
12277 Lisp_Object
12278 x_new_font (f, fontname)
12279 struct frame *f;
12280 register char *fontname;
12282 struct font_info *fontp
12283 = FS_LOAD_FONT (f, 0, fontname, -1);
12285 if (!fontp)
12286 return Qnil;
12288 f->output_data.x->font = (XFontStruct *) (fontp->font);
12289 f->output_data.x->baseline_offset = fontp->baseline_offset;
12290 f->output_data.x->fontset = -1;
12292 x_compute_fringe_widths (f, 1);
12294 /* Compute the scroll bar width in character columns. */
12295 if (f->scroll_bar_pixel_width > 0)
12297 int wid = FONT_WIDTH (f->output_data.x->font);
12298 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
12300 else
12302 int wid = FONT_WIDTH (f->output_data.x->font);
12303 f->scroll_bar_cols = (14 + wid - 1) / wid;
12306 /* Now make the frame display the given font. */
12307 if (FRAME_X_WINDOW (f) != 0)
12309 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
12310 f->output_data.x->font->fid);
12311 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
12312 f->output_data.x->font->fid);
12313 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
12314 f->output_data.x->font->fid);
12316 frame_update_line_height (f);
12318 /* Don't change the size of a tip frame; there's no point in
12319 doing it because it's done in Fx_show_tip, and it leads to
12320 problems because the tip frame has no widget. */
12321 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
12322 x_set_window_size (f, 0, f->width, f->height);
12324 else
12325 /* If we are setting a new frame's font for the first time,
12326 there are no faces yet, so this font's height is the line height. */
12327 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
12329 return build_string (fontp->full_name);
12332 /* Give frame F the fontset named FONTSETNAME as its default font, and
12333 return the full name of that fontset. FONTSETNAME may be a wildcard
12334 pattern; in that case, we choose some fontset that fits the pattern.
12335 The return value shows which fontset we chose. */
12337 Lisp_Object
12338 x_new_fontset (f, fontsetname)
12339 struct frame *f;
12340 char *fontsetname;
12342 int fontset = fs_query_fontset (build_string (fontsetname), 0);
12343 Lisp_Object result;
12345 if (fontset < 0)
12346 return Qnil;
12348 if (f->output_data.x->fontset == fontset)
12349 /* This fontset is already set in frame F. There's nothing more
12350 to do. */
12351 return fontset_name (fontset);
12353 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
12355 if (!STRINGP (result))
12356 /* Can't load ASCII font. */
12357 return Qnil;
12359 /* Since x_new_font doesn't update any fontset information, do it now. */
12360 f->output_data.x->fontset = fontset;
12362 #ifdef HAVE_X_I18N
12363 if (FRAME_XIC (f)
12364 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
12365 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
12366 #endif
12368 return build_string (fontsetname);
12371 /* Compute actual fringe widths */
12373 void
12374 x_compute_fringe_widths (f, redraw)
12375 struct frame *f;
12376 int redraw;
12378 int o_left = f->output_data.x->left_fringe_width;
12379 int o_right = f->output_data.x->right_fringe_width;
12380 int o_cols = f->output_data.x->fringe_cols;
12382 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
12383 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
12384 int left_fringe_width, right_fringe_width;
12386 if (!NILP (left_fringe))
12387 left_fringe = Fcdr (left_fringe);
12388 if (!NILP (right_fringe))
12389 right_fringe = Fcdr (right_fringe);
12391 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
12392 XINT (left_fringe));
12393 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
12394 XINT (right_fringe));
12396 if (left_fringe_width || right_fringe_width)
12398 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
12399 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
12400 int conf_wid = left_wid + right_wid;
12401 int font_wid = FONT_WIDTH (f->output_data.x->font);
12402 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
12403 int real_wid = cols * font_wid;
12404 if (left_wid && right_wid)
12406 if (left_fringe_width < 0)
12408 /* Left fringe width is fixed, adjust right fringe if necessary */
12409 f->output_data.x->left_fringe_width = left_wid;
12410 f->output_data.x->right_fringe_width = real_wid - left_wid;
12412 else if (right_fringe_width < 0)
12414 /* Right fringe width is fixed, adjust left fringe if necessary */
12415 f->output_data.x->left_fringe_width = real_wid - right_wid;
12416 f->output_data.x->right_fringe_width = right_wid;
12418 else
12420 /* Adjust both fringes with an equal amount.
12421 Note that we are doing integer arithmetic here, so don't
12422 lose a pixel if the total width is an odd number. */
12423 int fill = real_wid - conf_wid;
12424 f->output_data.x->left_fringe_width = left_wid + fill/2;
12425 f->output_data.x->right_fringe_width = right_wid + fill - fill/2;
12428 else if (left_fringe_width)
12430 f->output_data.x->left_fringe_width = real_wid;
12431 f->output_data.x->right_fringe_width = 0;
12433 else
12435 f->output_data.x->left_fringe_width = 0;
12436 f->output_data.x->right_fringe_width = real_wid;
12438 f->output_data.x->fringe_cols = cols;
12439 f->output_data.x->fringes_extra = real_wid;
12441 else
12443 f->output_data.x->left_fringe_width = 0;
12444 f->output_data.x->right_fringe_width = 0;
12445 f->output_data.x->fringe_cols = 0;
12446 f->output_data.x->fringes_extra = 0;
12449 if (redraw && FRAME_VISIBLE_P (f))
12450 if (o_left != f->output_data.x->left_fringe_width ||
12451 o_right != f->output_data.x->right_fringe_width ||
12452 o_cols != f->output_data.x->fringe_cols)
12453 redraw_frame (f);
12456 /***********************************************************************
12457 X Input Methods
12458 ***********************************************************************/
12460 #ifdef HAVE_X_I18N
12462 #ifdef HAVE_X11R6
12464 /* XIM destroy callback function, which is called whenever the
12465 connection to input method XIM dies. CLIENT_DATA contains a
12466 pointer to the x_display_info structure corresponding to XIM. */
12468 static void
12469 xim_destroy_callback (xim, client_data, call_data)
12470 XIM xim;
12471 XPointer client_data;
12472 XPointer call_data;
12474 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
12475 Lisp_Object frame, tail;
12477 BLOCK_INPUT;
12479 /* No need to call XDestroyIC.. */
12480 FOR_EACH_FRAME (tail, frame)
12482 struct frame *f = XFRAME (frame);
12483 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
12485 FRAME_XIC (f) = NULL;
12486 if (FRAME_XIC_FONTSET (f))
12488 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
12489 FRAME_XIC_FONTSET (f) = NULL;
12494 /* No need to call XCloseIM. */
12495 dpyinfo->xim = NULL;
12496 XFree (dpyinfo->xim_styles);
12497 UNBLOCK_INPUT;
12500 #endif /* HAVE_X11R6 */
12502 /* Open the connection to the XIM server on display DPYINFO.
12503 RESOURCE_NAME is the resource name Emacs uses. */
12505 static void
12506 xim_open_dpy (dpyinfo, resource_name)
12507 struct x_display_info *dpyinfo;
12508 char *resource_name;
12510 #ifdef USE_XIM
12511 XIM xim;
12513 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
12514 dpyinfo->xim = xim;
12516 if (xim)
12518 #ifdef HAVE_X11R6
12519 XIMCallback destroy;
12520 #endif
12522 /* Get supported styles and XIM values. */
12523 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
12525 #ifdef HAVE_X11R6
12526 destroy.callback = xim_destroy_callback;
12527 destroy.client_data = (XPointer)dpyinfo;
12528 /* This isn't prototyped in OSF 5.0. */
12529 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
12530 #endif
12533 #else /* not USE_XIM */
12534 dpyinfo->xim = NULL;
12535 #endif /* not USE_XIM */
12539 #ifdef HAVE_X11R6_XIM
12541 struct xim_inst_t
12543 struct x_display_info *dpyinfo;
12544 char *resource_name;
12547 /* XIM instantiate callback function, which is called whenever an XIM
12548 server is available. DISPLAY is teh display of the XIM.
12549 CLIENT_DATA contains a pointer to an xim_inst_t structure created
12550 when the callback was registered. */
12552 static void
12553 xim_instantiate_callback (display, client_data, call_data)
12554 Display *display;
12555 XPointer client_data;
12556 XPointer call_data;
12558 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
12559 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
12561 /* We don't support multiple XIM connections. */
12562 if (dpyinfo->xim)
12563 return;
12565 xim_open_dpy (dpyinfo, xim_inst->resource_name);
12567 /* Create XIC for the existing frames on the same display, as long
12568 as they have no XIC. */
12569 if (dpyinfo->xim && dpyinfo->reference_count > 0)
12571 Lisp_Object tail, frame;
12573 BLOCK_INPUT;
12574 FOR_EACH_FRAME (tail, frame)
12576 struct frame *f = XFRAME (frame);
12578 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
12579 if (FRAME_XIC (f) == NULL)
12581 create_frame_xic (f);
12582 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
12583 xic_set_statusarea (f);
12584 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
12586 struct window *w = XWINDOW (f->selected_window);
12587 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
12592 UNBLOCK_INPUT;
12596 #endif /* HAVE_X11R6_XIM */
12599 /* Open a connection to the XIM server on display DPYINFO.
12600 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
12601 connection only at the first time. On X11R6, open the connection
12602 in the XIM instantiate callback function. */
12604 static void
12605 xim_initialize (dpyinfo, resource_name)
12606 struct x_display_info *dpyinfo;
12607 char *resource_name;
12609 #ifdef USE_XIM
12610 #ifdef HAVE_X11R6_XIM
12611 struct xim_inst_t *xim_inst;
12612 int len;
12614 dpyinfo->xim = NULL;
12615 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
12616 xim_inst->dpyinfo = dpyinfo;
12617 len = strlen (resource_name);
12618 xim_inst->resource_name = (char *) xmalloc (len + 1);
12619 bcopy (resource_name, xim_inst->resource_name, len + 1);
12620 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12621 resource_name, EMACS_CLASS,
12622 xim_instantiate_callback,
12623 /* Fixme: This is XPointer in
12624 XFree86 but (XPointer *) on
12625 Tru64, at least. */
12626 (XPointer) xim_inst);
12627 #else /* not HAVE_X11R6_XIM */
12628 dpyinfo->xim = NULL;
12629 xim_open_dpy (dpyinfo, resource_name);
12630 #endif /* not HAVE_X11R6_XIM */
12632 #else /* not USE_XIM */
12633 dpyinfo->xim = NULL;
12634 #endif /* not USE_XIM */
12638 /* Close the connection to the XIM server on display DPYINFO. */
12640 static void
12641 xim_close_dpy (dpyinfo)
12642 struct x_display_info *dpyinfo;
12644 #ifdef USE_XIM
12645 #ifdef HAVE_X11R6_XIM
12646 if (dpyinfo->display)
12647 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12648 NULL, EMACS_CLASS,
12649 xim_instantiate_callback, NULL);
12650 #endif /* not HAVE_X11R6_XIM */
12651 if (dpyinfo->display)
12652 XCloseIM (dpyinfo->xim);
12653 dpyinfo->xim = NULL;
12654 XFree (dpyinfo->xim_styles);
12655 #endif /* USE_XIM */
12658 #endif /* not HAVE_X11R6_XIM */
12662 /* Calculate the absolute position in frame F
12663 from its current recorded position values and gravity. */
12665 void
12666 x_calc_absolute_position (f)
12667 struct frame *f;
12669 Window child;
12670 int win_x = 0, win_y = 0;
12671 int flags = f->output_data.x->size_hint_flags;
12672 int this_window;
12674 /* We have nothing to do if the current position
12675 is already for the top-left corner. */
12676 if (! ((flags & XNegative) || (flags & YNegative)))
12677 return;
12679 #ifdef USE_X_TOOLKIT
12680 this_window = XtWindow (f->output_data.x->widget);
12681 #else
12682 this_window = FRAME_X_WINDOW (f);
12683 #endif
12685 /* Find the position of the outside upper-left corner of
12686 the inner window, with respect to the outer window.
12687 But do this only if we will need the results. */
12688 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
12690 int count;
12692 BLOCK_INPUT;
12693 count = x_catch_errors (FRAME_X_DISPLAY (f));
12694 while (1)
12696 x_clear_errors (FRAME_X_DISPLAY (f));
12697 XTranslateCoordinates (FRAME_X_DISPLAY (f),
12699 /* From-window, to-window. */
12700 this_window,
12701 f->output_data.x->parent_desc,
12703 /* From-position, to-position. */
12704 0, 0, &win_x, &win_y,
12706 /* Child of win. */
12707 &child);
12708 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12710 Window newroot, newparent = 0xdeadbeef;
12711 Window *newchildren;
12712 unsigned int nchildren;
12714 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
12715 &newparent, &newchildren, &nchildren))
12716 break;
12718 XFree ((char *) newchildren);
12720 f->output_data.x->parent_desc = newparent;
12722 else
12723 break;
12726 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12727 UNBLOCK_INPUT;
12730 /* Treat negative positions as relative to the leftmost bottommost
12731 position that fits on the screen. */
12732 if (flags & XNegative)
12733 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
12734 - 2 * f->output_data.x->border_width - win_x
12735 - PIXEL_WIDTH (f)
12736 + f->output_data.x->left_pos);
12739 int height = PIXEL_HEIGHT (f);
12741 #if defined USE_X_TOOLKIT && defined USE_MOTIF
12742 /* Something is fishy here. When using Motif, starting Emacs with
12743 `-g -0-0', the frame appears too low by a few pixels.
12745 This seems to be so because initially, while Emacs is starting,
12746 the column widget's height and the frame's pixel height are
12747 different. The column widget's height is the right one. In
12748 later invocations, when Emacs is up, the frame's pixel height
12749 is right, though.
12751 It's not obvious where the initial small difference comes from.
12752 2000-12-01, gerd. */
12754 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
12755 #endif
12757 if (flags & YNegative)
12758 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
12759 - 2 * f->output_data.x->border_width
12760 - win_y
12761 - height
12762 + f->output_data.x->top_pos);
12765 /* The left_pos and top_pos
12766 are now relative to the top and left screen edges,
12767 so the flags should correspond. */
12768 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12771 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
12772 to really change the position, and 0 when calling from
12773 x_make_frame_visible (in that case, XOFF and YOFF are the current
12774 position values). It is -1 when calling from x_set_frame_parameters,
12775 which means, do adjust for borders but don't change the gravity. */
12777 void
12778 x_set_offset (f, xoff, yoff, change_gravity)
12779 struct frame *f;
12780 register int xoff, yoff;
12781 int change_gravity;
12783 int modified_top, modified_left;
12785 if (change_gravity > 0)
12787 f->output_data.x->top_pos = yoff;
12788 f->output_data.x->left_pos = xoff;
12789 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12790 if (xoff < 0)
12791 f->output_data.x->size_hint_flags |= XNegative;
12792 if (yoff < 0)
12793 f->output_data.x->size_hint_flags |= YNegative;
12794 f->output_data.x->win_gravity = NorthWestGravity;
12796 x_calc_absolute_position (f);
12798 BLOCK_INPUT;
12799 x_wm_set_size_hint (f, (long) 0, 0);
12801 modified_left = f->output_data.x->left_pos;
12802 modified_top = f->output_data.x->top_pos;
12803 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
12804 this seems to be unnecessary and incorrect. rms, 4/17/97. */
12805 /* It is a mystery why we need to add the border_width here
12806 when the frame is already visible, but experiment says we do. */
12807 if (change_gravity != 0)
12809 modified_left += f->output_data.x->border_width;
12810 modified_top += f->output_data.x->border_width;
12812 #endif
12814 #ifdef USE_X_TOOLKIT
12815 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
12816 modified_left, modified_top);
12817 #else /* not USE_X_TOOLKIT */
12818 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12819 modified_left, modified_top);
12820 #endif /* not USE_X_TOOLKIT */
12821 UNBLOCK_INPUT;
12824 /* Check if we need to resize the frame due to a fullscreen request.
12825 If so needed, resize the frame. */
12826 static void
12827 x_check_fullscreen (f)
12828 struct frame *f;
12830 if (f->output_data.x->want_fullscreen & FULLSCREEN_BOTH)
12832 int width, height, ign;
12834 x_real_positions (f, &f->output_data.x->left_pos,
12835 &f->output_data.x->top_pos);
12837 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
12839 /* We do not need to move the window, it shall be taken care of
12840 when setting WM manager hints.
12841 If the frame is visible already, the position is checked by
12842 x_check_fullscreen_move. */
12843 if (f->width != width || f->height != height)
12845 change_frame_size (f, height, width, 0, 1, 0);
12846 SET_FRAME_GARBAGED (f);
12847 cancel_mouse_face (f);
12849 /* Wait for the change of frame size to occur */
12850 f->output_data.x->want_fullscreen |= FULLSCREEN_WAIT;
12856 /* If frame parameters are set after the frame is mapped, we need to move
12857 the window. This is done in xfns.c.
12858 Some window managers moves the window to the right position, some
12859 moves the outer window manager window to the specified position.
12860 Here we check that we are in the right spot. If not, make a second
12861 move, assuming we are dealing with the second kind of window manager. */
12862 static void
12863 x_check_fullscreen_move (f)
12864 struct frame *f;
12866 if (f->output_data.x->want_fullscreen & FULLSCREEN_MOVE_WAIT)
12868 int expect_top = f->output_data.x->top_pos;
12869 int expect_left = f->output_data.x->left_pos;
12871 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
12872 expect_top = 0;
12873 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
12874 expect_left = 0;
12876 if (expect_top != f->output_data.x->top_pos
12877 || expect_left != f->output_data.x->left_pos)
12878 x_set_offset (f, expect_left, expect_top, 1);
12880 /* Just do this once */
12881 f->output_data.x->want_fullscreen &= ~FULLSCREEN_MOVE_WAIT;
12886 /* Calculate fullscreen size. Return in *TOP_POS and *LEFT_POS the
12887 wanted positions of the WM window (not emacs window).
12888 Return in *WIDTH and *HEIGHT the wanted width and height of Emacs
12889 window (FRAME_X_WINDOW).
12891 void
12892 x_fullscreen_adjust (f, width, height, top_pos, left_pos)
12893 struct frame *f;
12894 int *width;
12895 int *height;
12896 int *top_pos;
12897 int *left_pos;
12899 int newwidth = f->width, newheight = f->height;
12901 *top_pos = f->output_data.x->top_pos;
12902 *left_pos = f->output_data.x->left_pos;
12904 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
12906 int ph;
12908 ph = FRAME_X_DISPLAY_INFO (f)->height;
12909 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
12910 ph = CHAR_TO_PIXEL_HEIGHT (f, newheight)
12911 - f->output_data.x->y_pixels_diff;
12912 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
12913 *top_pos = 0;
12916 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
12918 int pw;
12920 pw = FRAME_X_DISPLAY_INFO (f)->width;
12921 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
12922 pw = CHAR_TO_PIXEL_WIDTH (f, newwidth)
12923 - f->output_data.x->x_pixels_diff;
12924 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
12925 *left_pos = 0;
12928 *width = newwidth;
12929 *height = newheight;
12933 /* Change the size of frame F's X window to COLS/ROWS in the case F
12934 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
12935 top-left-corner window gravity for this size change and subsequent
12936 size changes. Otherwise we leave the window gravity unchanged. */
12938 static void
12939 x_set_window_size_1 (f, change_gravity, cols, rows)
12940 struct frame *f;
12941 int change_gravity;
12942 int cols, rows;
12944 int pixelwidth, pixelheight;
12946 check_frame_size (f, &rows, &cols);
12947 f->output_data.x->vertical_scroll_bar_extra
12948 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
12950 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
12951 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
12952 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
12954 x_compute_fringe_widths (f, 0);
12956 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
12957 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
12959 f->output_data.x->win_gravity = NorthWestGravity;
12960 x_wm_set_size_hint (f, (long) 0, 0);
12962 XSync (FRAME_X_DISPLAY (f), False);
12963 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12964 pixelwidth, pixelheight);
12966 /* Now, strictly speaking, we can't be sure that this is accurate,
12967 but the window manager will get around to dealing with the size
12968 change request eventually, and we'll hear how it went when the
12969 ConfigureNotify event gets here.
12971 We could just not bother storing any of this information here,
12972 and let the ConfigureNotify event set everything up, but that
12973 might be kind of confusing to the Lisp code, since size changes
12974 wouldn't be reported in the frame parameters until some random
12975 point in the future when the ConfigureNotify event arrives.
12977 We pass 1 for DELAY since we can't run Lisp code inside of
12978 a BLOCK_INPUT. */
12979 change_frame_size (f, rows, cols, 0, 1, 0);
12980 PIXEL_WIDTH (f) = pixelwidth;
12981 PIXEL_HEIGHT (f) = pixelheight;
12983 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
12984 receive in the ConfigureNotify event; if we get what we asked
12985 for, then the event won't cause the screen to become garbaged, so
12986 we have to make sure to do it here. */
12987 SET_FRAME_GARBAGED (f);
12989 XFlush (FRAME_X_DISPLAY (f));
12993 /* Call this to change the size of frame F's x-window.
12994 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
12995 for this size change and subsequent size changes.
12996 Otherwise we leave the window gravity unchanged. */
12998 void
12999 x_set_window_size (f, change_gravity, cols, rows)
13000 struct frame *f;
13001 int change_gravity;
13002 int cols, rows;
13004 BLOCK_INPUT;
13006 #ifdef USE_X_TOOLKIT
13008 if (f->output_data.x->widget != NULL)
13010 /* The x and y position of the widget is clobbered by the
13011 call to XtSetValues within EmacsFrameSetCharSize.
13012 This is a real kludge, but I don't understand Xt so I can't
13013 figure out a correct fix. Can anyone else tell me? -- rms. */
13014 int xpos = f->output_data.x->widget->core.x;
13015 int ypos = f->output_data.x->widget->core.y;
13016 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
13017 f->output_data.x->widget->core.x = xpos;
13018 f->output_data.x->widget->core.y = ypos;
13020 else
13021 x_set_window_size_1 (f, change_gravity, cols, rows);
13023 #else /* not USE_X_TOOLKIT */
13025 x_set_window_size_1 (f, change_gravity, cols, rows);
13027 #endif /* not USE_X_TOOLKIT */
13029 /* If cursor was outside the new size, mark it as off. */
13030 mark_window_cursors_off (XWINDOW (f->root_window));
13032 /* Clear out any recollection of where the mouse highlighting was,
13033 since it might be in a place that's outside the new frame size.
13034 Actually checking whether it is outside is a pain in the neck,
13035 so don't try--just let the highlighting be done afresh with new size. */
13036 cancel_mouse_face (f);
13038 UNBLOCK_INPUT;
13041 /* Mouse warping. */
13043 void
13044 x_set_mouse_position (f, x, y)
13045 struct frame *f;
13046 int x, y;
13048 int pix_x, pix_y;
13050 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
13051 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
13053 if (pix_x < 0) pix_x = 0;
13054 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
13056 if (pix_y < 0) pix_y = 0;
13057 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
13059 BLOCK_INPUT;
13061 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13062 0, 0, 0, 0, pix_x, pix_y);
13063 UNBLOCK_INPUT;
13066 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
13068 void
13069 x_set_mouse_pixel_position (f, pix_x, pix_y)
13070 struct frame *f;
13071 int pix_x, pix_y;
13073 BLOCK_INPUT;
13075 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13076 0, 0, 0, 0, pix_x, pix_y);
13077 UNBLOCK_INPUT;
13080 /* focus shifting, raising and lowering. */
13082 void
13083 x_focus_on_frame (f)
13084 struct frame *f;
13086 #if 0 /* This proves to be unpleasant. */
13087 x_raise_frame (f);
13088 #endif
13089 #if 0
13090 /* I don't think that the ICCCM allows programs to do things like this
13091 without the interaction of the window manager. Whatever you end up
13092 doing with this code, do it to x_unfocus_frame too. */
13093 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
13094 RevertToPointerRoot, CurrentTime);
13095 #endif /* ! 0 */
13098 void
13099 x_unfocus_frame (f)
13100 struct frame *f;
13102 #if 0
13103 /* Look at the remarks in x_focus_on_frame. */
13104 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
13105 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
13106 RevertToPointerRoot, CurrentTime);
13107 #endif /* ! 0 */
13110 /* Raise frame F. */
13112 void
13113 x_raise_frame (f)
13114 struct frame *f;
13116 if (f->async_visible)
13118 BLOCK_INPUT;
13119 #ifdef USE_X_TOOLKIT
13120 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
13121 #else /* not USE_X_TOOLKIT */
13122 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13123 #endif /* not USE_X_TOOLKIT */
13124 XFlush (FRAME_X_DISPLAY (f));
13125 UNBLOCK_INPUT;
13129 /* Lower frame F. */
13131 void
13132 x_lower_frame (f)
13133 struct frame *f;
13135 if (f->async_visible)
13137 BLOCK_INPUT;
13138 #ifdef USE_X_TOOLKIT
13139 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
13140 #else /* not USE_X_TOOLKIT */
13141 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13142 #endif /* not USE_X_TOOLKIT */
13143 XFlush (FRAME_X_DISPLAY (f));
13144 UNBLOCK_INPUT;
13148 static void
13149 XTframe_raise_lower (f, raise_flag)
13150 FRAME_PTR f;
13151 int raise_flag;
13153 if (raise_flag)
13154 x_raise_frame (f);
13155 else
13156 x_lower_frame (f);
13159 /* Change of visibility. */
13161 /* This tries to wait until the frame is really visible.
13162 However, if the window manager asks the user where to position
13163 the frame, this will return before the user finishes doing that.
13164 The frame will not actually be visible at that time,
13165 but it will become visible later when the window manager
13166 finishes with it. */
13168 void
13169 x_make_frame_visible (f)
13170 struct frame *f;
13172 Lisp_Object type;
13173 int original_top, original_left;
13174 int retry_count = 2;
13176 retry:
13178 BLOCK_INPUT;
13180 type = x_icon_type (f);
13181 if (!NILP (type))
13182 x_bitmap_icon (f, type);
13184 if (! FRAME_VISIBLE_P (f))
13186 /* We test FRAME_GARBAGED_P here to make sure we don't
13187 call x_set_offset a second time
13188 if we get to x_make_frame_visible a second time
13189 before the window gets really visible. */
13190 if (! FRAME_ICONIFIED_P (f)
13191 && ! f->output_data.x->asked_for_visible)
13192 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13194 f->output_data.x->asked_for_visible = 1;
13196 if (! EQ (Vx_no_window_manager, Qt))
13197 x_wm_set_window_state (f, NormalState);
13198 #ifdef USE_X_TOOLKIT
13199 /* This was XtPopup, but that did nothing for an iconified frame. */
13200 XtMapWidget (f->output_data.x->widget);
13201 #else /* not USE_X_TOOLKIT */
13202 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13203 #endif /* not USE_X_TOOLKIT */
13204 #if 0 /* This seems to bring back scroll bars in the wrong places
13205 if the window configuration has changed. They seem
13206 to come back ok without this. */
13207 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
13208 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13209 #endif
13212 XFlush (FRAME_X_DISPLAY (f));
13214 /* Synchronize to ensure Emacs knows the frame is visible
13215 before we do anything else. We do this loop with input not blocked
13216 so that incoming events are handled. */
13218 Lisp_Object frame;
13219 int count;
13220 /* This must be before UNBLOCK_INPUT
13221 since events that arrive in response to the actions above
13222 will set it when they are handled. */
13223 int previously_visible = f->output_data.x->has_been_visible;
13225 original_left = f->output_data.x->left_pos;
13226 original_top = f->output_data.x->top_pos;
13228 /* This must come after we set COUNT. */
13229 UNBLOCK_INPUT;
13231 /* We unblock here so that arriving X events are processed. */
13233 /* Now move the window back to where it was "supposed to be".
13234 But don't do it if the gravity is negative.
13235 When the gravity is negative, this uses a position
13236 that is 3 pixels too low. Perhaps that's really the border width.
13238 Don't do this if the window has never been visible before,
13239 because the window manager may choose the position
13240 and we don't want to override it. */
13242 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
13243 && f->output_data.x->win_gravity == NorthWestGravity
13244 && previously_visible)
13246 Drawable rootw;
13247 int x, y;
13248 unsigned int width, height, border, depth;
13250 BLOCK_INPUT;
13252 /* On some window managers (such as FVWM) moving an existing
13253 window, even to the same place, causes the window manager
13254 to introduce an offset. This can cause the window to move
13255 to an unexpected location. Check the geometry (a little
13256 slow here) and then verify that the window is in the right
13257 place. If the window is not in the right place, move it
13258 there, and take the potential window manager hit. */
13259 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13260 &rootw, &x, &y, &width, &height, &border, &depth);
13262 if (original_left != x || original_top != y)
13263 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13264 original_left, original_top);
13266 UNBLOCK_INPUT;
13269 XSETFRAME (frame, f);
13271 /* Wait until the frame is visible. Process X events until a
13272 MapNotify event has been seen, or until we think we won't get a
13273 MapNotify at all.. */
13274 for (count = input_signal_count + 10;
13275 input_signal_count < count && !FRAME_VISIBLE_P (f);)
13277 /* Force processing of queued events. */
13278 x_sync (f);
13280 /* Machines that do polling rather than SIGIO have been
13281 observed to go into a busy-wait here. So we'll fake an
13282 alarm signal to let the handler know that there's something
13283 to be read. We used to raise a real alarm, but it seems
13284 that the handler isn't always enabled here. This is
13285 probably a bug. */
13286 if (input_polling_used ())
13288 /* It could be confusing if a real alarm arrives while
13289 processing the fake one. Turn it off and let the
13290 handler reset it. */
13291 extern void poll_for_input_1 P_ ((void));
13292 int old_poll_suppress_count = poll_suppress_count;
13293 poll_suppress_count = 1;
13294 poll_for_input_1 ();
13295 poll_suppress_count = old_poll_suppress_count;
13298 /* See if a MapNotify event has been processed. */
13299 FRAME_SAMPLE_VISIBILITY (f);
13302 /* 2000-09-28: In
13304 (let ((f (selected-frame)))
13305 (iconify-frame f)
13306 (raise-frame f))
13308 the frame is not raised with various window managers on
13309 FreeBSD, Linux and Solaris. It turns out that, for some
13310 unknown reason, the call to XtMapWidget is completely ignored.
13311 Mapping the widget a second time works. */
13313 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
13314 goto retry;
13318 /* Change from mapped state to withdrawn state. */
13320 /* Make the frame visible (mapped and not iconified). */
13322 void
13323 x_make_frame_invisible (f)
13324 struct frame *f;
13326 Window window;
13328 #ifdef USE_X_TOOLKIT
13329 /* Use the frame's outermost window, not the one we normally draw on. */
13330 window = XtWindow (f->output_data.x->widget);
13331 #else /* not USE_X_TOOLKIT */
13332 window = FRAME_X_WINDOW (f);
13333 #endif /* not USE_X_TOOLKIT */
13335 /* Don't keep the highlight on an invisible frame. */
13336 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13337 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
13339 #if 0/* This might add unreliability; I don't trust it -- rms. */
13340 if (! f->async_visible && ! f->async_iconified)
13341 return;
13342 #endif
13344 BLOCK_INPUT;
13346 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
13347 that the current position of the window is user-specified, rather than
13348 program-specified, so that when the window is mapped again, it will be
13349 placed at the same location, without forcing the user to position it
13350 by hand again (they have already done that once for this window.) */
13351 x_wm_set_size_hint (f, (long) 0, 1);
13353 #ifdef HAVE_X11R4
13355 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
13356 DefaultScreen (FRAME_X_DISPLAY (f))))
13358 UNBLOCK_INPUT_RESIGNAL;
13359 error ("Can't notify window manager of window withdrawal");
13361 #else /* ! defined (HAVE_X11R4) */
13363 /* Tell the window manager what we're going to do. */
13364 if (! EQ (Vx_no_window_manager, Qt))
13366 XEvent unmap;
13368 unmap.xunmap.type = UnmapNotify;
13369 unmap.xunmap.window = window;
13370 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
13371 unmap.xunmap.from_configure = False;
13372 if (! XSendEvent (FRAME_X_DISPLAY (f),
13373 DefaultRootWindow (FRAME_X_DISPLAY (f)),
13374 False,
13375 SubstructureRedirectMaskSubstructureNotifyMask,
13376 &unmap))
13378 UNBLOCK_INPUT_RESIGNAL;
13379 error ("Can't notify window manager of withdrawal");
13383 /* Unmap the window ourselves. Cheeky! */
13384 XUnmapWindow (FRAME_X_DISPLAY (f), window);
13385 #endif /* ! defined (HAVE_X11R4) */
13387 /* We can't distinguish this from iconification
13388 just by the event that we get from the server.
13389 So we can't win using the usual strategy of letting
13390 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
13391 and synchronize with the server to make sure we agree. */
13392 f->visible = 0;
13393 FRAME_ICONIFIED_P (f) = 0;
13394 f->async_visible = 0;
13395 f->async_iconified = 0;
13397 x_sync (f);
13399 UNBLOCK_INPUT;
13402 /* Change window state from mapped to iconified. */
13404 void
13405 x_iconify_frame (f)
13406 struct frame *f;
13408 int result;
13409 Lisp_Object type;
13411 /* Don't keep the highlight on an invisible frame. */
13412 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13413 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
13415 if (f->async_iconified)
13416 return;
13418 BLOCK_INPUT;
13420 FRAME_SAMPLE_VISIBILITY (f);
13422 type = x_icon_type (f);
13423 if (!NILP (type))
13424 x_bitmap_icon (f, type);
13426 #ifdef USE_X_TOOLKIT
13428 if (! FRAME_VISIBLE_P (f))
13430 if (! EQ (Vx_no_window_manager, Qt))
13431 x_wm_set_window_state (f, IconicState);
13432 /* This was XtPopup, but that did nothing for an iconified frame. */
13433 XtMapWidget (f->output_data.x->widget);
13434 /* The server won't give us any event to indicate
13435 that an invisible frame was changed to an icon,
13436 so we have to record it here. */
13437 f->iconified = 1;
13438 f->visible = 1;
13439 f->async_iconified = 1;
13440 f->async_visible = 0;
13441 UNBLOCK_INPUT;
13442 return;
13445 result = XIconifyWindow (FRAME_X_DISPLAY (f),
13446 XtWindow (f->output_data.x->widget),
13447 DefaultScreen (FRAME_X_DISPLAY (f)));
13448 UNBLOCK_INPUT;
13450 if (!result)
13451 error ("Can't notify window manager of iconification");
13453 f->async_iconified = 1;
13454 f->async_visible = 0;
13457 BLOCK_INPUT;
13458 XFlush (FRAME_X_DISPLAY (f));
13459 UNBLOCK_INPUT;
13460 #else /* not USE_X_TOOLKIT */
13462 /* Make sure the X server knows where the window should be positioned,
13463 in case the user deiconifies with the window manager. */
13464 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
13465 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13467 /* Since we don't know which revision of X we're running, we'll use both
13468 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
13470 /* X11R4: send a ClientMessage to the window manager using the
13471 WM_CHANGE_STATE type. */
13473 XEvent message;
13475 message.xclient.window = FRAME_X_WINDOW (f);
13476 message.xclient.type = ClientMessage;
13477 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
13478 message.xclient.format = 32;
13479 message.xclient.data.l[0] = IconicState;
13481 if (! XSendEvent (FRAME_X_DISPLAY (f),
13482 DefaultRootWindow (FRAME_X_DISPLAY (f)),
13483 False,
13484 SubstructureRedirectMask | SubstructureNotifyMask,
13485 &message))
13487 UNBLOCK_INPUT_RESIGNAL;
13488 error ("Can't notify window manager of iconification");
13492 /* X11R3: set the initial_state field of the window manager hints to
13493 IconicState. */
13494 x_wm_set_window_state (f, IconicState);
13496 if (!FRAME_VISIBLE_P (f))
13498 /* If the frame was withdrawn, before, we must map it. */
13499 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13502 f->async_iconified = 1;
13503 f->async_visible = 0;
13505 XFlush (FRAME_X_DISPLAY (f));
13506 UNBLOCK_INPUT;
13507 #endif /* not USE_X_TOOLKIT */
13511 /* Free X resources of frame F. */
13513 void
13514 x_free_frame_resources (f)
13515 struct frame *f;
13517 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13518 Lisp_Object bar;
13519 struct scroll_bar *b;
13521 BLOCK_INPUT;
13523 /* If a display connection is dead, don't try sending more
13524 commands to the X server. */
13525 if (dpyinfo->display)
13527 if (f->output_data.x->icon_desc)
13528 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
13530 #ifdef USE_X_TOOLKIT
13531 /* Explicitly destroy the scroll bars of the frame. Without
13532 this, we get "BadDrawable" errors from the toolkit later on,
13533 presumably from expose events generated for the disappearing
13534 toolkit scroll bars. */
13535 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
13537 b = XSCROLL_BAR (bar);
13538 x_scroll_bar_remove (b);
13540 #endif
13542 #ifdef HAVE_X_I18N
13543 if (FRAME_XIC (f))
13544 free_frame_xic (f);
13545 #endif
13547 #ifdef USE_X_TOOLKIT
13548 if (f->output_data.x->widget)
13550 XtDestroyWidget (f->output_data.x->widget);
13551 f->output_data.x->widget = NULL;
13553 /* Tooltips don't have widgets, only a simple X window, even if
13554 we are using a toolkit. */
13555 else if (FRAME_X_WINDOW (f))
13556 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13558 free_frame_menubar (f);
13559 #else /* !USE_X_TOOLKIT */
13560 if (FRAME_X_WINDOW (f))
13561 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13562 #endif /* !USE_X_TOOLKIT */
13564 unload_color (f, f->output_data.x->foreground_pixel);
13565 unload_color (f, f->output_data.x->background_pixel);
13566 unload_color (f, f->output_data.x->cursor_pixel);
13567 unload_color (f, f->output_data.x->cursor_foreground_pixel);
13568 unload_color (f, f->output_data.x->border_pixel);
13569 unload_color (f, f->output_data.x->mouse_pixel);
13571 if (f->output_data.x->scroll_bar_background_pixel != -1)
13572 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
13573 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
13574 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
13575 #ifdef USE_TOOLKIT_SCROLL_BARS
13576 /* Scrollbar shadow colors. */
13577 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
13578 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
13579 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
13580 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
13581 #endif /* USE_TOOLKIT_SCROLL_BARS */
13582 if (f->output_data.x->white_relief.allocated_p)
13583 unload_color (f, f->output_data.x->white_relief.pixel);
13584 if (f->output_data.x->black_relief.allocated_p)
13585 unload_color (f, f->output_data.x->black_relief.pixel);
13587 if (FRAME_FACE_CACHE (f))
13588 free_frame_faces (f);
13590 x_free_gcs (f);
13591 XFlush (FRAME_X_DISPLAY (f));
13594 if (f->output_data.x->saved_menu_event)
13595 xfree (f->output_data.x->saved_menu_event);
13597 xfree (f->output_data.x);
13598 f->output_data.x = NULL;
13600 if (f == dpyinfo->x_focus_frame)
13601 dpyinfo->x_focus_frame = 0;
13602 if (f == dpyinfo->x_focus_event_frame)
13603 dpyinfo->x_focus_event_frame = 0;
13604 if (f == dpyinfo->x_highlight_frame)
13605 dpyinfo->x_highlight_frame = 0;
13607 if (f == dpyinfo->mouse_face_mouse_frame)
13609 dpyinfo->mouse_face_beg_row
13610 = dpyinfo->mouse_face_beg_col = -1;
13611 dpyinfo->mouse_face_end_row
13612 = dpyinfo->mouse_face_end_col = -1;
13613 dpyinfo->mouse_face_window = Qnil;
13614 dpyinfo->mouse_face_deferred_gc = 0;
13615 dpyinfo->mouse_face_mouse_frame = 0;
13618 UNBLOCK_INPUT;
13622 /* Destroy the X window of frame F. */
13624 void
13625 x_destroy_window (f)
13626 struct frame *f;
13628 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13630 /* If a display connection is dead, don't try sending more
13631 commands to the X server. */
13632 if (dpyinfo->display != 0)
13633 x_free_frame_resources (f);
13635 dpyinfo->reference_count--;
13639 /* Setting window manager hints. */
13641 /* Set the normal size hints for the window manager, for frame F.
13642 FLAGS is the flags word to use--or 0 meaning preserve the flags
13643 that the window now has.
13644 If USER_POSITION is nonzero, we set the USPosition
13645 flag (this is useful when FLAGS is 0). */
13647 void
13648 x_wm_set_size_hint (f, flags, user_position)
13649 struct frame *f;
13650 long flags;
13651 int user_position;
13653 XSizeHints size_hints;
13655 #ifdef USE_X_TOOLKIT
13656 Arg al[2];
13657 int ac = 0;
13658 Dimension widget_width, widget_height;
13659 Window window = XtWindow (f->output_data.x->widget);
13660 #else /* not USE_X_TOOLKIT */
13661 Window window = FRAME_X_WINDOW (f);
13662 #endif /* not USE_X_TOOLKIT */
13664 /* Setting PMaxSize caused various problems. */
13665 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
13667 size_hints.x = f->output_data.x->left_pos;
13668 size_hints.y = f->output_data.x->top_pos;
13670 #ifdef USE_X_TOOLKIT
13671 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
13672 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
13673 XtGetValues (f->output_data.x->widget, al, ac);
13674 size_hints.height = widget_height;
13675 size_hints.width = widget_width;
13676 #else /* not USE_X_TOOLKIT */
13677 size_hints.height = PIXEL_HEIGHT (f);
13678 size_hints.width = PIXEL_WIDTH (f);
13679 #endif /* not USE_X_TOOLKIT */
13681 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
13682 size_hints.height_inc = f->output_data.x->line_height;
13683 size_hints.max_width
13684 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
13685 size_hints.max_height
13686 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
13688 /* Calculate the base and minimum sizes.
13690 (When we use the X toolkit, we don't do it here.
13691 Instead we copy the values that the widgets are using, below.) */
13692 #ifndef USE_X_TOOLKIT
13694 int base_width, base_height;
13695 int min_rows = 0, min_cols = 0;
13697 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
13698 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
13700 check_frame_size (f, &min_rows, &min_cols);
13702 /* The window manager uses the base width hints to calculate the
13703 current number of rows and columns in the frame while
13704 resizing; min_width and min_height aren't useful for this
13705 purpose, since they might not give the dimensions for a
13706 zero-row, zero-column frame.
13708 We use the base_width and base_height members if we have
13709 them; otherwise, we set the min_width and min_height members
13710 to the size for a zero x zero frame. */
13712 #ifdef HAVE_X11R4
13713 size_hints.flags |= PBaseSize;
13714 size_hints.base_width = base_width;
13715 size_hints.base_height = base_height;
13716 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
13717 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
13718 #else
13719 size_hints.min_width = base_width;
13720 size_hints.min_height = base_height;
13721 #endif
13724 /* If we don't need the old flags, we don't need the old hint at all. */
13725 if (flags)
13727 size_hints.flags |= flags;
13728 goto no_read;
13730 #endif /* not USE_X_TOOLKIT */
13733 XSizeHints hints; /* Sometimes I hate X Windows... */
13734 long supplied_return;
13735 int value;
13737 #ifdef HAVE_X11R4
13738 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
13739 &supplied_return);
13740 #else
13741 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
13742 #endif
13744 #ifdef USE_X_TOOLKIT
13745 size_hints.base_height = hints.base_height;
13746 size_hints.base_width = hints.base_width;
13747 size_hints.min_height = hints.min_height;
13748 size_hints.min_width = hints.min_width;
13749 #endif
13751 if (flags)
13752 size_hints.flags |= flags;
13753 else
13755 if (value == 0)
13756 hints.flags = 0;
13757 if (hints.flags & PSize)
13758 size_hints.flags |= PSize;
13759 if (hints.flags & PPosition)
13760 size_hints.flags |= PPosition;
13761 if (hints.flags & USPosition)
13762 size_hints.flags |= USPosition;
13763 if (hints.flags & USSize)
13764 size_hints.flags |= USSize;
13768 #ifndef USE_X_TOOLKIT
13769 no_read:
13770 #endif
13772 #ifdef PWinGravity
13773 size_hints.win_gravity = f->output_data.x->win_gravity;
13774 size_hints.flags |= PWinGravity;
13776 if (user_position)
13778 size_hints.flags &= ~ PPosition;
13779 size_hints.flags |= USPosition;
13781 #endif /* PWinGravity */
13783 #ifdef HAVE_X11R4
13784 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13785 #else
13786 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13787 #endif
13790 /* Used for IconicState or NormalState */
13792 void
13793 x_wm_set_window_state (f, state)
13794 struct frame *f;
13795 int state;
13797 #ifdef USE_X_TOOLKIT
13798 Arg al[1];
13800 XtSetArg (al[0], XtNinitialState, state);
13801 XtSetValues (f->output_data.x->widget, al, 1);
13802 #else /* not USE_X_TOOLKIT */
13803 Window window = FRAME_X_WINDOW (f);
13805 f->output_data.x->wm_hints.flags |= StateHint;
13806 f->output_data.x->wm_hints.initial_state = state;
13808 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13809 #endif /* not USE_X_TOOLKIT */
13812 void
13813 x_wm_set_icon_pixmap (f, pixmap_id)
13814 struct frame *f;
13815 int pixmap_id;
13817 Pixmap icon_pixmap;
13819 #ifndef USE_X_TOOLKIT
13820 Window window = FRAME_X_WINDOW (f);
13821 #endif
13823 if (pixmap_id > 0)
13825 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
13826 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
13828 else
13830 /* It seems there is no way to turn off use of an icon pixmap.
13831 The following line does it, only if no icon has yet been created,
13832 for some window managers. But with mwm it crashes.
13833 Some people say it should clear the IconPixmapHint bit in this case,
13834 but that doesn't work, and the X consortium said it isn't the
13835 right thing at all. Since there is no way to win,
13836 best to explicitly give up. */
13837 #if 0
13838 f->output_data.x->wm_hints.icon_pixmap = None;
13839 #else
13840 return;
13841 #endif
13844 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
13847 Arg al[1];
13848 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
13849 XtSetValues (f->output_data.x->widget, al, 1);
13852 #else /* not USE_X_TOOLKIT */
13854 f->output_data.x->wm_hints.flags |= IconPixmapHint;
13855 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13857 #endif /* not USE_X_TOOLKIT */
13860 void
13861 x_wm_set_icon_position (f, icon_x, icon_y)
13862 struct frame *f;
13863 int icon_x, icon_y;
13865 #ifdef USE_X_TOOLKIT
13866 Window window = XtWindow (f->output_data.x->widget);
13867 #else
13868 Window window = FRAME_X_WINDOW (f);
13869 #endif
13871 f->output_data.x->wm_hints.flags |= IconPositionHint;
13872 f->output_data.x->wm_hints.icon_x = icon_x;
13873 f->output_data.x->wm_hints.icon_y = icon_y;
13875 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13879 /***********************************************************************
13880 Fonts
13881 ***********************************************************************/
13883 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
13885 struct font_info *
13886 x_get_font_info (f, font_idx)
13887 FRAME_PTR f;
13888 int font_idx;
13890 return (FRAME_X_FONT_TABLE (f) + font_idx);
13894 /* Return a list of names of available fonts matching PATTERN on frame F.
13896 If SIZE is > 0, it is the size (maximum bounds width) of fonts
13897 to be listed.
13899 SIZE < 0 means include scalable fonts.
13901 Frame F null means we have not yet created any frame on X, and
13902 consult the first display in x_display_list. MAXNAMES sets a limit
13903 on how many fonts to match. */
13905 Lisp_Object
13906 x_list_fonts (f, pattern, size, maxnames)
13907 struct frame *f;
13908 Lisp_Object pattern;
13909 int size;
13910 int maxnames;
13912 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
13913 Lisp_Object tem, second_best;
13914 struct x_display_info *dpyinfo
13915 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
13916 Display *dpy = dpyinfo->display;
13917 int try_XLoadQueryFont = 0;
13918 int count;
13919 int allow_scalable_fonts_p = 0;
13921 if (size < 0)
13923 allow_scalable_fonts_p = 1;
13924 size = 0;
13927 patterns = Fassoc (pattern, Valternate_fontname_alist);
13928 if (NILP (patterns))
13929 patterns = Fcons (pattern, Qnil);
13931 if (maxnames == 1 && !size)
13932 /* We can return any single font matching PATTERN. */
13933 try_XLoadQueryFont = 1;
13935 for (; CONSP (patterns); patterns = XCDR (patterns))
13937 int num_fonts;
13938 char **names = NULL;
13940 pattern = XCAR (patterns);
13941 /* See if we cached the result for this particular query.
13942 The cache is an alist of the form:
13943 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
13944 tem = XCDR (dpyinfo->name_list_element);
13945 key = Fcons (Fcons (pattern, make_number (maxnames)),
13946 allow_scalable_fonts_p ? Qt : Qnil);
13947 list = Fassoc (key, tem);
13948 if (!NILP (list))
13950 list = Fcdr_safe (list);
13951 /* We have a cashed list. Don't have to get the list again. */
13952 goto label_cached;
13955 /* At first, put PATTERN in the cache. */
13957 BLOCK_INPUT;
13958 count = x_catch_errors (dpy);
13960 if (try_XLoadQueryFont)
13962 XFontStruct *font;
13963 unsigned long value;
13965 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
13966 if (x_had_errors_p (dpy))
13968 /* This error is perhaps due to insufficient memory on X
13969 server. Let's just ignore it. */
13970 font = NULL;
13971 x_clear_errors (dpy);
13974 if (font
13975 && XGetFontProperty (font, XA_FONT, &value))
13977 char *name = (char *) XGetAtomName (dpy, (Atom) value);
13978 int len = strlen (name);
13979 char *tmp;
13981 /* If DXPC (a Differential X Protocol Compressor)
13982 Ver.3.7 is running, XGetAtomName will return null
13983 string. We must avoid such a name. */
13984 if (len == 0)
13985 try_XLoadQueryFont = 0;
13986 else
13988 num_fonts = 1;
13989 names = (char **) alloca (sizeof (char *));
13990 /* Some systems only allow alloca assigned to a
13991 simple var. */
13992 tmp = (char *) alloca (len + 1); names[0] = tmp;
13993 bcopy (name, names[0], len + 1);
13994 XFree (name);
13997 else
13998 try_XLoadQueryFont = 0;
14000 if (font)
14001 XFreeFont (dpy, font);
14004 if (!try_XLoadQueryFont)
14006 /* We try at least 10 fonts because XListFonts will return
14007 auto-scaled fonts at the head. */
14008 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
14009 &num_fonts);
14010 if (x_had_errors_p (dpy))
14012 /* This error is perhaps due to insufficient memory on X
14013 server. Let's just ignore it. */
14014 names = NULL;
14015 x_clear_errors (dpy);
14019 x_uncatch_errors (dpy, count);
14020 UNBLOCK_INPUT;
14022 if (names)
14024 int i;
14026 /* Make a list of all the fonts we got back.
14027 Store that in the font cache for the display. */
14028 for (i = 0; i < num_fonts; i++)
14030 int width = 0;
14031 char *p = names[i];
14032 int average_width = -1, dashes = 0;
14034 /* Count the number of dashes in NAMES[I]. If there are
14035 14 dashes, and the field value following 12th dash
14036 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
14037 is usually too ugly to be used for editing. Let's
14038 ignore it. */
14039 while (*p)
14040 if (*p++ == '-')
14042 dashes++;
14043 if (dashes == 7) /* PIXEL_SIZE field */
14044 width = atoi (p);
14045 else if (dashes == 12) /* AVERAGE_WIDTH field */
14046 average_width = atoi (p);
14049 if (allow_scalable_fonts_p
14050 || dashes < 14 || average_width != 0)
14052 tem = build_string (names[i]);
14053 if (NILP (Fassoc (tem, list)))
14055 if (STRINGP (Vx_pixel_size_width_font_regexp)
14056 && ((fast_c_string_match_ignore_case
14057 (Vx_pixel_size_width_font_regexp, names[i]))
14058 >= 0))
14059 /* We can set the value of PIXEL_SIZE to the
14060 width of this font. */
14061 list = Fcons (Fcons (tem, make_number (width)), list);
14062 else
14063 /* For the moment, width is not known. */
14064 list = Fcons (Fcons (tem, Qnil), list);
14069 if (!try_XLoadQueryFont)
14071 BLOCK_INPUT;
14072 XFreeFontNames (names);
14073 UNBLOCK_INPUT;
14077 /* Now store the result in the cache. */
14078 XSETCDR (dpyinfo->name_list_element,
14079 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
14081 label_cached:
14082 if (NILP (list)) continue; /* Try the remaining alternatives. */
14084 newlist = second_best = Qnil;
14085 /* Make a list of the fonts that have the right width. */
14086 for (; CONSP (list); list = XCDR (list))
14088 int found_size;
14090 tem = XCAR (list);
14092 if (!CONSP (tem) || NILP (XCAR (tem)))
14093 continue;
14094 if (!size)
14096 newlist = Fcons (XCAR (tem), newlist);
14097 continue;
14100 if (!INTEGERP (XCDR (tem)))
14102 /* Since we have not yet known the size of this font, we
14103 must try slow function call XLoadQueryFont. */
14104 XFontStruct *thisinfo;
14106 BLOCK_INPUT;
14107 count = x_catch_errors (dpy);
14108 thisinfo = XLoadQueryFont (dpy,
14109 XSTRING (XCAR (tem))->data);
14110 if (x_had_errors_p (dpy))
14112 /* This error is perhaps due to insufficient memory on X
14113 server. Let's just ignore it. */
14114 thisinfo = NULL;
14115 x_clear_errors (dpy);
14117 x_uncatch_errors (dpy, count);
14118 UNBLOCK_INPUT;
14120 if (thisinfo)
14122 XSETCDR (tem,
14123 (thisinfo->min_bounds.width == 0
14124 ? make_number (0)
14125 : make_number (thisinfo->max_bounds.width)));
14126 BLOCK_INPUT;
14127 XFreeFont (dpy, thisinfo);
14128 UNBLOCK_INPUT;
14130 else
14131 /* For unknown reason, the previous call of XListFont had
14132 returned a font which can't be opened. Record the size
14133 as 0 not to try to open it again. */
14134 XSETCDR (tem, make_number (0));
14137 found_size = XINT (XCDR (tem));
14138 if (found_size == size)
14139 newlist = Fcons (XCAR (tem), newlist);
14140 else if (found_size > 0)
14142 if (NILP (second_best))
14143 second_best = tem;
14144 else if (found_size < size)
14146 if (XINT (XCDR (second_best)) > size
14147 || XINT (XCDR (second_best)) < found_size)
14148 second_best = tem;
14150 else
14152 if (XINT (XCDR (second_best)) > size
14153 && XINT (XCDR (second_best)) > found_size)
14154 second_best = tem;
14158 if (!NILP (newlist))
14159 break;
14160 else if (!NILP (second_best))
14162 newlist = Fcons (XCAR (second_best), Qnil);
14163 break;
14167 return newlist;
14171 #if GLYPH_DEBUG
14173 /* Check that FONT is valid on frame F. It is if it can be found in F's
14174 font table. */
14176 static void
14177 x_check_font (f, font)
14178 struct frame *f;
14179 XFontStruct *font;
14181 int i;
14182 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14184 xassert (font != NULL);
14186 for (i = 0; i < dpyinfo->n_fonts; i++)
14187 if (dpyinfo->font_table[i].name
14188 && font == dpyinfo->font_table[i].font)
14189 break;
14191 xassert (i < dpyinfo->n_fonts);
14194 #endif /* GLYPH_DEBUG != 0 */
14196 /* Set *W to the minimum width, *H to the minimum font height of FONT.
14197 Note: There are (broken) X fonts out there with invalid XFontStruct
14198 min_bounds contents. For example, handa@etl.go.jp reports that
14199 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
14200 have font->min_bounds.width == 0. */
14202 static INLINE void
14203 x_font_min_bounds (font, w, h)
14204 XFontStruct *font;
14205 int *w, *h;
14207 *h = FONT_HEIGHT (font);
14208 *w = font->min_bounds.width;
14210 /* Try to handle the case where FONT->min_bounds has invalid
14211 contents. Since the only font known to have invalid min_bounds
14212 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
14213 if (*w <= 0)
14214 *w = font->max_bounds.width;
14218 /* Compute the smallest character width and smallest font height over
14219 all fonts available on frame F. Set the members smallest_char_width
14220 and smallest_font_height in F's x_display_info structure to
14221 the values computed. Value is non-zero if smallest_font_height or
14222 smallest_char_width become smaller than they were before. */
14224 static int
14225 x_compute_min_glyph_bounds (f)
14226 struct frame *f;
14228 int i;
14229 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14230 XFontStruct *font;
14231 int old_width = dpyinfo->smallest_char_width;
14232 int old_height = dpyinfo->smallest_font_height;
14234 dpyinfo->smallest_font_height = 100000;
14235 dpyinfo->smallest_char_width = 100000;
14237 for (i = 0; i < dpyinfo->n_fonts; ++i)
14238 if (dpyinfo->font_table[i].name)
14240 struct font_info *fontp = dpyinfo->font_table + i;
14241 int w, h;
14243 font = (XFontStruct *) fontp->font;
14244 xassert (font != (XFontStruct *) ~0);
14245 x_font_min_bounds (font, &w, &h);
14247 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
14248 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
14251 xassert (dpyinfo->smallest_char_width > 0
14252 && dpyinfo->smallest_font_height > 0);
14254 return (dpyinfo->n_fonts == 1
14255 || dpyinfo->smallest_char_width < old_width
14256 || dpyinfo->smallest_font_height < old_height);
14260 /* Load font named FONTNAME of the size SIZE for frame F, and return a
14261 pointer to the structure font_info while allocating it dynamically.
14262 If SIZE is 0, load any size of font.
14263 If loading is failed, return NULL. */
14265 struct font_info *
14266 x_load_font (f, fontname, size)
14267 struct frame *f;
14268 register char *fontname;
14269 int size;
14271 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14272 Lisp_Object font_names;
14273 int count;
14275 /* Get a list of all the fonts that match this name. Once we
14276 have a list of matching fonts, we compare them against the fonts
14277 we already have by comparing names. */
14278 font_names = x_list_fonts (f, build_string (fontname), size, 1);
14280 if (!NILP (font_names))
14282 Lisp_Object tail;
14283 int i;
14285 for (i = 0; i < dpyinfo->n_fonts; i++)
14286 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
14287 if (dpyinfo->font_table[i].name
14288 && (!strcmp (dpyinfo->font_table[i].name,
14289 XSTRING (XCAR (tail))->data)
14290 || !strcmp (dpyinfo->font_table[i].full_name,
14291 XSTRING (XCAR (tail))->data)))
14292 return (dpyinfo->font_table + i);
14295 /* Load the font and add it to the table. */
14297 char *full_name;
14298 XFontStruct *font;
14299 struct font_info *fontp;
14300 unsigned long value;
14301 int i;
14303 /* If we have found fonts by x_list_font, load one of them. If
14304 not, we still try to load a font by the name given as FONTNAME
14305 because XListFonts (called in x_list_font) of some X server has
14306 a bug of not finding a font even if the font surely exists and
14307 is loadable by XLoadQueryFont. */
14308 if (size > 0 && !NILP (font_names))
14309 fontname = (char *) XSTRING (XCAR (font_names))->data;
14311 BLOCK_INPUT;
14312 count = x_catch_errors (FRAME_X_DISPLAY (f));
14313 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
14314 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
14316 /* This error is perhaps due to insufficient memory on X
14317 server. Let's just ignore it. */
14318 font = NULL;
14319 x_clear_errors (FRAME_X_DISPLAY (f));
14321 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
14322 UNBLOCK_INPUT;
14323 if (!font)
14324 return NULL;
14326 /* Find a free slot in the font table. */
14327 for (i = 0; i < dpyinfo->n_fonts; ++i)
14328 if (dpyinfo->font_table[i].name == NULL)
14329 break;
14331 /* If no free slot found, maybe enlarge the font table. */
14332 if (i == dpyinfo->n_fonts
14333 && dpyinfo->n_fonts == dpyinfo->font_table_size)
14335 int sz;
14336 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
14337 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
14338 dpyinfo->font_table
14339 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
14342 fontp = dpyinfo->font_table + i;
14343 if (i == dpyinfo->n_fonts)
14344 ++dpyinfo->n_fonts;
14346 /* Now fill in the slots of *FONTP. */
14347 BLOCK_INPUT;
14348 fontp->font = font;
14349 fontp->font_idx = i;
14350 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
14351 bcopy (fontname, fontp->name, strlen (fontname) + 1);
14353 /* Try to get the full name of FONT. Put it in FULL_NAME. */
14354 full_name = 0;
14355 if (XGetFontProperty (font, XA_FONT, &value))
14357 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
14358 char *p = name;
14359 int dashes = 0;
14361 /* Count the number of dashes in the "full name".
14362 If it is too few, this isn't really the font's full name,
14363 so don't use it.
14364 In X11R4, the fonts did not come with their canonical names
14365 stored in them. */
14366 while (*p)
14368 if (*p == '-')
14369 dashes++;
14370 p++;
14373 if (dashes >= 13)
14375 full_name = (char *) xmalloc (p - name + 1);
14376 bcopy (name, full_name, p - name + 1);
14379 XFree (name);
14382 if (full_name != 0)
14383 fontp->full_name = full_name;
14384 else
14385 fontp->full_name = fontp->name;
14387 fontp->size = font->max_bounds.width;
14388 fontp->height = FONT_HEIGHT (font);
14390 if (NILP (font_names))
14392 /* We come here because of a bug of XListFonts mentioned at
14393 the head of this block. Let's store this information in
14394 the cache for x_list_fonts. */
14395 Lisp_Object lispy_name = build_string (fontname);
14396 Lisp_Object lispy_full_name = build_string (fontp->full_name);
14397 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
14398 Qnil);
14400 XSETCDR (dpyinfo->name_list_element,
14401 Fcons (Fcons (key,
14402 Fcons (Fcons (lispy_full_name,
14403 make_number (fontp->size)),
14404 Qnil)),
14405 XCDR (dpyinfo->name_list_element)));
14406 if (full_name)
14408 key = Fcons (Fcons (lispy_full_name, make_number (256)),
14409 Qnil);
14410 XSETCDR (dpyinfo->name_list_element,
14411 Fcons (Fcons (key,
14412 Fcons (Fcons (lispy_full_name,
14413 make_number (fontp->size)),
14414 Qnil)),
14415 XCDR (dpyinfo->name_list_element)));
14419 /* The slot `encoding' specifies how to map a character
14420 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
14421 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
14422 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
14423 2:0xA020..0xFF7F). For the moment, we don't know which charset
14424 uses this font. So, we set information in fontp->encoding[1]
14425 which is never used by any charset. If mapping can't be
14426 decided, set FONT_ENCODING_NOT_DECIDED. */
14427 fontp->encoding[1]
14428 = (font->max_byte1 == 0
14429 /* 1-byte font */
14430 ? (font->min_char_or_byte2 < 0x80
14431 ? (font->max_char_or_byte2 < 0x80
14432 ? 0 /* 0x20..0x7F */
14433 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
14434 : 1) /* 0xA0..0xFF */
14435 /* 2-byte font */
14436 : (font->min_byte1 < 0x80
14437 ? (font->max_byte1 < 0x80
14438 ? (font->min_char_or_byte2 < 0x80
14439 ? (font->max_char_or_byte2 < 0x80
14440 ? 0 /* 0x2020..0x7F7F */
14441 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
14442 : 3) /* 0x20A0..0x7FFF */
14443 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
14444 : (font->min_char_or_byte2 < 0x80
14445 ? (font->max_char_or_byte2 < 0x80
14446 ? 2 /* 0xA020..0xFF7F */
14447 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
14448 : 1))); /* 0xA0A0..0xFFFF */
14450 fontp->baseline_offset
14451 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
14452 ? (long) value : 0);
14453 fontp->relative_compose
14454 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
14455 ? (long) value : 0);
14456 fontp->default_ascent
14457 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
14458 ? (long) value : 0);
14460 /* Set global flag fonts_changed_p to non-zero if the font loaded
14461 has a character with a smaller width than any other character
14462 before, or if the font loaded has a smaller height than any
14463 other font loaded before. If this happens, it will make a
14464 glyph matrix reallocation necessary. */
14465 fonts_changed_p |= x_compute_min_glyph_bounds (f);
14466 UNBLOCK_INPUT;
14467 return fontp;
14472 /* Return a pointer to struct font_info of a font named FONTNAME for
14473 frame F. If no such font is loaded, return NULL. */
14475 struct font_info *
14476 x_query_font (f, fontname)
14477 struct frame *f;
14478 register char *fontname;
14480 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14481 int i;
14483 for (i = 0; i < dpyinfo->n_fonts; i++)
14484 if (dpyinfo->font_table[i].name
14485 && (!strcmp (dpyinfo->font_table[i].name, fontname)
14486 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
14487 return (dpyinfo->font_table + i);
14488 return NULL;
14492 /* Find a CCL program for a font specified by FONTP, and set the member
14493 `encoder' of the structure. */
14495 void
14496 x_find_ccl_program (fontp)
14497 struct font_info *fontp;
14499 Lisp_Object list, elt;
14501 elt = Qnil;
14502 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
14504 elt = XCAR (list);
14505 if (CONSP (elt)
14506 && STRINGP (XCAR (elt))
14507 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
14508 >= 0)
14509 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
14510 >= 0)))
14511 break;
14514 if (! NILP (list))
14516 struct ccl_program *ccl
14517 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
14519 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
14520 xfree (ccl);
14521 else
14522 fontp->font_encoder = ccl;
14528 /***********************************************************************
14529 Initialization
14530 ***********************************************************************/
14532 #ifdef USE_X_TOOLKIT
14533 static XrmOptionDescRec emacs_options[] = {
14534 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
14535 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
14537 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
14538 XrmoptionSepArg, NULL},
14539 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
14541 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14542 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14543 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14544 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14545 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14546 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
14547 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
14549 #endif /* USE_X_TOOLKIT */
14551 static int x_initialized;
14553 #ifdef MULTI_KBOARD
14554 /* Test whether two display-name strings agree up to the dot that separates
14555 the screen number from the server number. */
14556 static int
14557 same_x_server (name1, name2)
14558 char *name1, *name2;
14560 int seen_colon = 0;
14561 unsigned char *system_name = XSTRING (Vsystem_name)->data;
14562 int system_name_length = strlen (system_name);
14563 int length_until_period = 0;
14565 while (system_name[length_until_period] != 0
14566 && system_name[length_until_period] != '.')
14567 length_until_period++;
14569 /* Treat `unix' like an empty host name. */
14570 if (! strncmp (name1, "unix:", 5))
14571 name1 += 4;
14572 if (! strncmp (name2, "unix:", 5))
14573 name2 += 4;
14574 /* Treat this host's name like an empty host name. */
14575 if (! strncmp (name1, system_name, system_name_length)
14576 && name1[system_name_length] == ':')
14577 name1 += system_name_length;
14578 if (! strncmp (name2, system_name, system_name_length)
14579 && name2[system_name_length] == ':')
14580 name2 += system_name_length;
14581 /* Treat this host's domainless name like an empty host name. */
14582 if (! strncmp (name1, system_name, length_until_period)
14583 && name1[length_until_period] == ':')
14584 name1 += length_until_period;
14585 if (! strncmp (name2, system_name, length_until_period)
14586 && name2[length_until_period] == ':')
14587 name2 += length_until_period;
14589 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
14591 if (*name1 == ':')
14592 seen_colon++;
14593 if (seen_colon && *name1 == '.')
14594 return 1;
14596 return (seen_colon
14597 && (*name1 == '.' || *name1 == '\0')
14598 && (*name2 == '.' || *name2 == '\0'));
14600 #endif
14602 struct x_display_info *
14603 x_term_init (display_name, xrm_option, resource_name)
14604 Lisp_Object display_name;
14605 char *xrm_option;
14606 char *resource_name;
14608 int connection;
14609 Display *dpy;
14610 struct x_display_info *dpyinfo;
14611 XrmDatabase xrdb;
14613 BLOCK_INPUT;
14615 if (!x_initialized)
14617 x_initialize ();
14618 x_initialized = 1;
14621 #ifdef USE_X_TOOLKIT
14622 /* weiner@footloose.sps.mot.com reports that this causes
14623 errors with X11R5:
14624 X protocol error: BadAtom (invalid Atom parameter)
14625 on protocol request 18skiloaf.
14626 So let's not use it until R6. */
14627 #ifdef HAVE_X11XTR6
14628 XtSetLanguageProc (NULL, NULL, NULL);
14629 #endif
14632 int argc = 0;
14633 char *argv[3];
14635 argv[0] = "";
14636 argc = 1;
14637 if (xrm_option)
14639 argv[argc++] = "-xrm";
14640 argv[argc++] = xrm_option;
14642 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
14643 resource_name, EMACS_CLASS,
14644 emacs_options, XtNumber (emacs_options),
14645 &argc, argv);
14647 #ifdef HAVE_X11XTR6
14648 /* I think this is to compensate for XtSetLanguageProc. */
14649 fixup_locale ();
14650 #endif
14653 #else /* not USE_X_TOOLKIT */
14654 #ifdef HAVE_X11R5
14655 XSetLocaleModifiers ("");
14656 #endif
14657 dpy = XOpenDisplay (XSTRING (display_name)->data);
14658 #endif /* not USE_X_TOOLKIT */
14660 /* Detect failure. */
14661 if (dpy == 0)
14663 UNBLOCK_INPUT;
14664 return 0;
14667 /* We have definitely succeeded. Record the new connection. */
14669 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
14670 bzero (dpyinfo, sizeof *dpyinfo);
14672 #ifdef MULTI_KBOARD
14674 struct x_display_info *share;
14675 Lisp_Object tail;
14677 for (share = x_display_list, tail = x_display_name_list; share;
14678 share = share->next, tail = XCDR (tail))
14679 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
14680 XSTRING (display_name)->data))
14681 break;
14682 if (share)
14683 dpyinfo->kboard = share->kboard;
14684 else
14686 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
14687 init_kboard (dpyinfo->kboard);
14688 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
14690 char *vendor = ServerVendor (dpy);
14691 UNBLOCK_INPUT;
14692 dpyinfo->kboard->Vsystem_key_alist
14693 = call1 (Qvendor_specific_keysyms,
14694 build_string (vendor ? vendor : ""));
14695 BLOCK_INPUT;
14698 dpyinfo->kboard->next_kboard = all_kboards;
14699 all_kboards = dpyinfo->kboard;
14700 /* Don't let the initial kboard remain current longer than necessary.
14701 That would cause problems if a file loaded on startup tries to
14702 prompt in the mini-buffer. */
14703 if (current_kboard == initial_kboard)
14704 current_kboard = dpyinfo->kboard;
14706 dpyinfo->kboard->reference_count++;
14708 #endif
14710 /* Put this display on the chain. */
14711 dpyinfo->next = x_display_list;
14712 x_display_list = dpyinfo;
14714 /* Put it on x_display_name_list as well, to keep them parallel. */
14715 x_display_name_list = Fcons (Fcons (display_name, Qnil),
14716 x_display_name_list);
14717 dpyinfo->name_list_element = XCAR (x_display_name_list);
14719 dpyinfo->display = dpy;
14721 #if 0
14722 XSetAfterFunction (x_current_display, x_trace_wire);
14723 #endif /* ! 0 */
14725 dpyinfo->x_id_name
14726 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
14727 + STRING_BYTES (XSTRING (Vsystem_name))
14728 + 2);
14729 sprintf (dpyinfo->x_id_name, "%s@%s",
14730 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
14732 /* Figure out which modifier bits mean what. */
14733 x_find_modifier_meanings (dpyinfo);
14735 /* Get the scroll bar cursor. */
14736 dpyinfo->vertical_scroll_bar_cursor
14737 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
14739 xrdb = x_load_resources (dpyinfo->display, xrm_option,
14740 resource_name, EMACS_CLASS);
14741 #ifdef HAVE_XRMSETDATABASE
14742 XrmSetDatabase (dpyinfo->display, xrdb);
14743 #else
14744 dpyinfo->display->db = xrdb;
14745 #endif
14746 /* Put the rdb where we can find it in a way that works on
14747 all versions. */
14748 dpyinfo->xrdb = xrdb;
14750 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
14751 DefaultScreen (dpyinfo->display));
14752 select_visual (dpyinfo);
14753 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
14754 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
14755 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
14756 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
14757 dpyinfo->grabbed = 0;
14758 dpyinfo->reference_count = 0;
14759 dpyinfo->icon_bitmap_id = -1;
14760 dpyinfo->font_table = NULL;
14761 dpyinfo->n_fonts = 0;
14762 dpyinfo->font_table_size = 0;
14763 dpyinfo->bitmaps = 0;
14764 dpyinfo->bitmaps_size = 0;
14765 dpyinfo->bitmaps_last = 0;
14766 dpyinfo->scratch_cursor_gc = 0;
14767 dpyinfo->mouse_face_mouse_frame = 0;
14768 dpyinfo->mouse_face_deferred_gc = 0;
14769 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
14770 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
14771 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
14772 dpyinfo->mouse_face_window = Qnil;
14773 dpyinfo->mouse_face_overlay = Qnil;
14774 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
14775 dpyinfo->mouse_face_defer = 0;
14776 dpyinfo->mouse_face_hidden = 0;
14777 dpyinfo->x_focus_frame = 0;
14778 dpyinfo->x_focus_event_frame = 0;
14779 dpyinfo->x_highlight_frame = 0;
14780 dpyinfo->image_cache = make_image_cache ();
14782 /* See if a private colormap is requested. */
14783 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
14785 if (dpyinfo->visual->class == PseudoColor)
14787 Lisp_Object value;
14788 value = display_x_get_resource (dpyinfo,
14789 build_string ("privateColormap"),
14790 build_string ("PrivateColormap"),
14791 Qnil, Qnil);
14792 if (STRINGP (value)
14793 && (!strcmp (XSTRING (value)->data, "true")
14794 || !strcmp (XSTRING (value)->data, "on")))
14795 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
14798 else
14799 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
14800 dpyinfo->visual, AllocNone);
14803 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
14804 double pixels = DisplayHeight (dpyinfo->display, screen_number);
14805 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
14806 dpyinfo->resy = pixels * 25.4 / mm;
14807 pixels = DisplayWidth (dpyinfo->display, screen_number);
14808 mm = DisplayWidthMM (dpyinfo->display, screen_number);
14809 dpyinfo->resx = pixels * 25.4 / mm;
14812 dpyinfo->Xatom_wm_protocols
14813 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
14814 dpyinfo->Xatom_wm_take_focus
14815 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
14816 dpyinfo->Xatom_wm_save_yourself
14817 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
14818 dpyinfo->Xatom_wm_delete_window
14819 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
14820 dpyinfo->Xatom_wm_change_state
14821 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
14822 dpyinfo->Xatom_wm_configure_denied
14823 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
14824 dpyinfo->Xatom_wm_window_moved
14825 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
14826 dpyinfo->Xatom_editres
14827 = XInternAtom (dpyinfo->display, "Editres", False);
14828 dpyinfo->Xatom_CLIPBOARD
14829 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
14830 dpyinfo->Xatom_TIMESTAMP
14831 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
14832 dpyinfo->Xatom_TEXT
14833 = XInternAtom (dpyinfo->display, "TEXT", False);
14834 dpyinfo->Xatom_COMPOUND_TEXT
14835 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
14836 dpyinfo->Xatom_DELETE
14837 = XInternAtom (dpyinfo->display, "DELETE", False);
14838 dpyinfo->Xatom_MULTIPLE
14839 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
14840 dpyinfo->Xatom_INCR
14841 = XInternAtom (dpyinfo->display, "INCR", False);
14842 dpyinfo->Xatom_EMACS_TMP
14843 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
14844 dpyinfo->Xatom_TARGETS
14845 = XInternAtom (dpyinfo->display, "TARGETS", False);
14846 dpyinfo->Xatom_NULL
14847 = XInternAtom (dpyinfo->display, "NULL", False);
14848 dpyinfo->Xatom_ATOM_PAIR
14849 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
14850 /* For properties of font. */
14851 dpyinfo->Xatom_PIXEL_SIZE
14852 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
14853 dpyinfo->Xatom_MULE_BASELINE_OFFSET
14854 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
14855 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
14856 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
14857 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
14858 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
14860 /* Ghostscript support. */
14861 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
14862 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
14864 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
14865 False);
14867 dpyinfo->cut_buffers_initialized = 0;
14869 connection = ConnectionNumber (dpyinfo->display);
14870 dpyinfo->connection = connection;
14873 char null_bits[1];
14875 null_bits[0] = 0x00;
14877 dpyinfo->null_pixel
14878 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14879 null_bits, 1, 1, (long) 0, (long) 0,
14884 extern int gray_bitmap_width, gray_bitmap_height;
14885 extern char *gray_bitmap_bits;
14886 dpyinfo->gray
14887 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14888 gray_bitmap_bits,
14889 gray_bitmap_width, gray_bitmap_height,
14890 (unsigned long) 1, (unsigned long) 0, 1);
14893 #ifdef HAVE_X_I18N
14894 xim_initialize (dpyinfo, resource_name);
14895 #endif
14897 #ifdef subprocesses
14898 /* This is only needed for distinguishing keyboard and process input. */
14899 if (connection != 0)
14900 add_keyboard_wait_descriptor (connection);
14901 #endif
14903 #ifndef F_SETOWN_BUG
14904 #ifdef F_SETOWN
14905 #ifdef F_SETOWN_SOCK_NEG
14906 /* stdin is a socket here */
14907 fcntl (connection, F_SETOWN, -getpid ());
14908 #else /* ! defined (F_SETOWN_SOCK_NEG) */
14909 fcntl (connection, F_SETOWN, getpid ());
14910 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
14911 #endif /* ! defined (F_SETOWN) */
14912 #endif /* F_SETOWN_BUG */
14914 #ifdef SIGIO
14915 if (interrupt_input)
14916 init_sigio (connection);
14917 #endif /* ! defined (SIGIO) */
14919 #ifdef USE_LUCID
14920 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
14921 /* Make sure that we have a valid font for dialog boxes
14922 so that Xt does not crash. */
14924 Display *dpy = dpyinfo->display;
14925 XrmValue d, fr, to;
14926 Font font;
14927 int count;
14929 d.addr = (XPointer)&dpy;
14930 d.size = sizeof (Display *);
14931 fr.addr = XtDefaultFont;
14932 fr.size = sizeof (XtDefaultFont);
14933 to.size = sizeof (Font *);
14934 to.addr = (XPointer)&font;
14935 count = x_catch_errors (dpy);
14936 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
14937 abort ();
14938 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
14939 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
14940 x_uncatch_errors (dpy, count);
14942 #endif
14943 #endif
14945 /* See if we should run in synchronous mode. This is useful
14946 for debugging X code. */
14948 Lisp_Object value;
14949 value = display_x_get_resource (dpyinfo,
14950 build_string ("synchronous"),
14951 build_string ("Synchronous"),
14952 Qnil, Qnil);
14953 if (STRINGP (value)
14954 && (!strcmp (XSTRING (value)->data, "true")
14955 || !strcmp (XSTRING (value)->data, "on")))
14956 XSynchronize (dpyinfo->display, True);
14959 UNBLOCK_INPUT;
14961 return dpyinfo;
14964 /* Get rid of display DPYINFO, assuming all frames are already gone,
14965 and without sending any more commands to the X server. */
14967 void
14968 x_delete_display (dpyinfo)
14969 struct x_display_info *dpyinfo;
14971 delete_keyboard_wait_descriptor (dpyinfo->connection);
14973 /* Discard this display from x_display_name_list and x_display_list.
14974 We can't use Fdelq because that can quit. */
14975 if (! NILP (x_display_name_list)
14976 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
14977 x_display_name_list = XCDR (x_display_name_list);
14978 else
14980 Lisp_Object tail;
14982 tail = x_display_name_list;
14983 while (CONSP (tail) && CONSP (XCDR (tail)))
14985 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
14987 XSETCDR (tail, XCDR (XCDR (tail)));
14988 break;
14990 tail = XCDR (tail);
14994 if (next_noop_dpyinfo == dpyinfo)
14995 next_noop_dpyinfo = dpyinfo->next;
14997 if (x_display_list == dpyinfo)
14998 x_display_list = dpyinfo->next;
14999 else
15001 struct x_display_info *tail;
15003 for (tail = x_display_list; tail; tail = tail->next)
15004 if (tail->next == dpyinfo)
15005 tail->next = tail->next->next;
15008 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
15009 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
15010 XrmDestroyDatabase (dpyinfo->xrdb);
15011 #endif
15012 #endif
15013 #ifdef MULTI_KBOARD
15014 if (--dpyinfo->kboard->reference_count == 0)
15015 delete_kboard (dpyinfo->kboard);
15016 #endif
15017 #ifdef HAVE_X_I18N
15018 if (dpyinfo->xim)
15019 xim_close_dpy (dpyinfo);
15020 #endif
15022 xfree (dpyinfo->font_table);
15023 xfree (dpyinfo->x_id_name);
15024 xfree (dpyinfo->color_cells);
15025 xfree (dpyinfo);
15029 /* Set up use of X before we make the first connection. */
15031 static struct redisplay_interface x_redisplay_interface =
15033 x_produce_glyphs,
15034 x_write_glyphs,
15035 x_insert_glyphs,
15036 x_clear_end_of_line,
15037 x_scroll_run,
15038 x_after_update_window_line,
15039 x_update_window_begin,
15040 x_update_window_end,
15041 XTcursor_to,
15042 x_flush,
15043 x_clear_mouse_face,
15044 x_get_glyph_overhangs,
15045 x_fix_overlapping_area
15048 void
15049 x_initialize ()
15051 rif = &x_redisplay_interface;
15053 clear_frame_hook = x_clear_frame;
15054 ins_del_lines_hook = x_ins_del_lines;
15055 delete_glyphs_hook = x_delete_glyphs;
15056 ring_bell_hook = XTring_bell;
15057 reset_terminal_modes_hook = XTreset_terminal_modes;
15058 set_terminal_modes_hook = XTset_terminal_modes;
15059 update_begin_hook = x_update_begin;
15060 update_end_hook = x_update_end;
15061 set_terminal_window_hook = XTset_terminal_window;
15062 read_socket_hook = XTread_socket;
15063 frame_up_to_date_hook = XTframe_up_to_date;
15064 mouse_position_hook = XTmouse_position;
15065 frame_rehighlight_hook = XTframe_rehighlight;
15066 frame_raise_lower_hook = XTframe_raise_lower;
15067 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
15068 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
15069 redeem_scroll_bar_hook = XTredeem_scroll_bar;
15070 judge_scroll_bars_hook = XTjudge_scroll_bars;
15071 estimate_mode_line_height_hook = x_estimate_mode_line_height;
15073 scroll_region_ok = 1; /* we'll scroll partial frames */
15074 char_ins_del_ok = 1;
15075 line_ins_del_ok = 1; /* we'll just blt 'em */
15076 fast_clear_end_of_line = 1; /* X does this well */
15077 memory_below_frame = 0; /* we don't remember what scrolls
15078 off the bottom */
15079 baud_rate = 19200;
15081 x_noop_count = 0;
15082 last_tool_bar_item = -1;
15083 any_help_event_p = 0;
15085 /* Try to use interrupt input; if we can't, then start polling. */
15086 Fset_input_mode (Qt, Qnil, Qt, Qnil);
15088 #ifdef USE_X_TOOLKIT
15089 XtToolkitInitialize ();
15091 Xt_app_con = XtCreateApplicationContext ();
15093 /* Register a converter from strings to pixels, which uses
15094 Emacs' color allocation infrastructure. */
15095 XtAppSetTypeConverter (Xt_app_con,
15096 XtRString, XtRPixel, cvt_string_to_pixel,
15097 cvt_string_to_pixel_args,
15098 XtNumber (cvt_string_to_pixel_args),
15099 XtCacheByDisplay, cvt_pixel_dtor);
15101 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
15103 /* Install an asynchronous timer that processes Xt timeout events
15104 every 0.1s. This is necessary because some widget sets use
15105 timeouts internally, for example the LessTif menu bar, or the
15106 Xaw3d scroll bar. When Xt timouts aren't processed, these
15107 widgets don't behave normally. */
15109 EMACS_TIME interval;
15110 EMACS_SET_SECS_USECS (interval, 0, 100000);
15111 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
15113 #endif
15115 #ifdef USE_TOOLKIT_SCROLL_BARS
15116 xaw3d_arrow_scroll = False;
15117 xaw3d_pick_top = True;
15118 #endif
15120 /* Note that there is no real way portable across R3/R4 to get the
15121 original error handler. */
15122 XSetErrorHandler (x_error_handler);
15123 XSetIOErrorHandler (x_io_error_quitter);
15125 /* Disable Window Change signals; they are handled by X events. */
15126 #ifdef SIGWINCH
15127 signal (SIGWINCH, SIG_DFL);
15128 #endif /* SIGWINCH */
15130 signal (SIGPIPE, x_connection_signal);
15132 #ifdef HAVE_X_SM
15133 x_session_initialize ();
15134 #endif
15138 void
15139 syms_of_xterm ()
15141 staticpro (&x_error_message_string);
15142 x_error_message_string = Qnil;
15144 staticpro (&x_display_name_list);
15145 x_display_name_list = Qnil;
15147 staticpro (&last_mouse_scroll_bar);
15148 last_mouse_scroll_bar = Qnil;
15150 staticpro (&Qvendor_specific_keysyms);
15151 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
15153 staticpro (&Qutf_8);
15154 Qutf_8 = intern ("utf-8");
15155 staticpro (&Qlatin_1);
15156 Qlatin_1 = intern ("latin-1");
15158 staticpro (&last_mouse_press_frame);
15159 last_mouse_press_frame = Qnil;
15161 help_echo = Qnil;
15162 staticpro (&help_echo);
15163 help_echo_object = Qnil;
15164 staticpro (&help_echo_object);
15165 help_echo_window = Qnil;
15166 staticpro (&help_echo_window);
15167 previous_help_echo = Qnil;
15168 staticpro (&previous_help_echo);
15169 help_echo_pos = -1;
15171 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
15172 doc: /* *Non-nil means autoselect window with mouse pointer. */);
15173 mouse_autoselect_window = 0;
15175 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
15176 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
15177 For example, if a block cursor is over a tab, it will be drawn as
15178 wide as that tab on the display. */);
15179 x_stretch_cursor_p = 0;
15181 DEFVAR_BOOL ("x-use-underline-position-properties",
15182 &x_use_underline_position_properties,
15183 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
15184 nil means ignore them. If you encounter fonts with bogus
15185 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
15186 to 4.1, set this to nil. */);
15187 x_use_underline_position_properties = 1;
15189 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
15190 doc: /* What X toolkit scroll bars Emacs uses.
15191 A value of nil means Emacs doesn't use X toolkit scroll bars.
15192 Otherwise, value is a symbol describing the X toolkit. */);
15193 #ifdef USE_TOOLKIT_SCROLL_BARS
15194 #ifdef USE_MOTIF
15195 Vx_toolkit_scroll_bars = intern ("motif");
15196 #elif defined HAVE_XAW3D
15197 Vx_toolkit_scroll_bars = intern ("xaw3d");
15198 #else
15199 Vx_toolkit_scroll_bars = intern ("xaw");
15200 #endif
15201 #else
15202 Vx_toolkit_scroll_bars = Qnil;
15203 #endif
15205 staticpro (&last_mouse_motion_frame);
15206 last_mouse_motion_frame = Qnil;
15208 Qmodifier_value = intern ("modifier-value");
15209 Qalt = intern ("alt");
15210 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
15211 Qhyper = intern ("hyper");
15212 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
15213 Qmeta = intern ("meta");
15214 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
15215 Qsuper = intern ("super");
15216 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
15218 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
15219 doc: /* Which keys Emacs uses for the alt modifier.
15220 This should be one of the symbols `alt', `hyper', `meta', `super'.
15221 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
15222 is nil, which is the same as `alt'. */);
15223 Vx_alt_keysym = Qnil;
15225 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
15226 doc: /* Which keys Emacs uses for the hyper modifier.
15227 This should be one of the symbols `alt', `hyper', `meta', `super'.
15228 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
15229 default is nil, which is the same as `hyper'. */);
15230 Vx_hyper_keysym = Qnil;
15232 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
15233 doc: /* Which keys Emacs uses for the meta modifier.
15234 This should be one of the symbols `alt', `hyper', `meta', `super'.
15235 For example, `meta' means use the Meta_L and Meta_R keysyms. The
15236 default is nil, which is the same as `meta'. */);
15237 Vx_meta_keysym = Qnil;
15239 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
15240 doc: /* Which keys Emacs uses for the super modifier.
15241 This should be one of the symbols `alt', `hyper', `meta', `super'.
15242 For example, `super' means use the Super_L and Super_R keysyms. The
15243 default is nil, which is the same as `super'. */);
15244 Vx_super_keysym = Qnil;
15248 #endif /* HAVE_X_WINDOWS */