(language-info-custom-alist): New.
[emacs.git] / src / xterm.c
blobcc9942893d96cdd94bcb0adf7841ebe6864c489b
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
23 /* Xt features made by Fred Pierresteguy. */
25 #include <config.h>
27 /* On 4.3 these lose if they come after xterm.h. */
28 /* Putting these at the beginning seems to be standard for other .c files. */
29 #include <signal.h>
31 #include <stdio.h>
33 #ifdef HAVE_X_WINDOWS
35 #include "lisp.h"
36 #include "blockinput.h"
38 /* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40 #include "syssignal.h"
42 /* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44 #include "xterm.h"
45 #include <X11/cursorfont.h>
47 /* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49 #ifndef makedev
50 #include <sys/types.h>
51 #endif /* makedev */
53 #ifdef BSD_SYSTEM
54 #include <sys/ioctl.h>
55 #endif /* ! defined (BSD_SYSTEM) */
57 #include "systty.h"
58 #include "systime.h"
60 #ifndef INCLUDED_FCNTL
61 #include <fcntl.h>
62 #endif
63 #include <ctype.h>
64 #include <errno.h>
65 #include <setjmp.h>
66 #include <sys/stat.h>
67 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68 /* #include <sys/param.h> */
70 #include "charset.h"
71 #include "character.h"
72 #include "coding.h"
73 #include "ccl.h"
74 #include "frame.h"
75 #include "dispextern.h"
76 #include "fontset.h"
77 #include "termhooks.h"
78 #include "termopts.h"
79 #include "termchar.h"
80 #include "gnu.h"
81 #include "disptab.h"
82 #include "buffer.h"
83 #include "window.h"
84 #include "keyboard.h"
85 #include "intervals.h"
86 #include "process.h"
87 #include "atimer.h"
88 #include "keymap.h"
90 #ifdef USE_X_TOOLKIT
91 #include <X11/Shell.h>
92 #endif
94 #ifdef HAVE_SYS_TIME_H
95 #include <sys/time.h>
96 #endif
97 #ifdef HAVE_UNISTD_H
98 #include <unistd.h>
99 #endif
101 #ifdef USE_LUCID
102 extern int xlwmenu_window_p P_ ((Widget w, Window window));
103 extern void xlwmenu_redisplay P_ ((Widget));
104 #endif
106 #ifdef USE_X_TOOLKIT
108 extern void free_frame_menubar P_ ((struct frame *));
109 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
110 int));
112 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
113 #define HACK_EDITRES
114 extern void _XEditResCheckMessages ();
115 #endif /* not NO_EDITRES */
117 /* Include toolkit specific headers for the scroll bar widget. */
119 #ifdef USE_TOOLKIT_SCROLL_BARS
120 #if defined USE_MOTIF
121 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
122 #include <Xm/ScrollBar.h>
123 #else /* !USE_MOTIF i.e. use Xaw */
125 #ifdef HAVE_XAW3D
126 #include <X11/Xaw3d/Simple.h>
127 #include <X11/Xaw3d/Scrollbar.h>
128 #define ARROW_SCROLLBAR
129 #include <X11/Xaw3d/ScrollbarP.h>
130 #else /* !HAVE_XAW3D */
131 #include <X11/Xaw/Simple.h>
132 #include <X11/Xaw/Scrollbar.h>
133 #endif /* !HAVE_XAW3D */
134 #ifndef XtNpickTop
135 #define XtNpickTop "pickTop"
136 #endif /* !XtNpickTop */
137 #endif /* !USE_MOTIF */
138 #endif /* USE_TOOLKIT_SCROLL_BARS */
140 #endif /* USE_X_TOOLKIT */
142 #ifndef USE_X_TOOLKIT
143 #define x_any_window_to_frame x_window_to_frame
144 #define x_top_window_to_frame x_window_to_frame
145 #endif
147 #ifdef USE_X_TOOLKIT
148 #include "widget.h"
149 #ifndef XtNinitialState
150 #define XtNinitialState "initialState"
151 #endif
152 #endif
154 #ifdef HAVE_X11R6
155 /* This isn't prototyped in OSF 5.0 or or XFree 4.1. */
156 extern char * XSetIMValues P_ ((XIM, ...));
157 #endif
159 #define abs(x) ((x) < 0 ? -(x) : (x))
161 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
164 /* Fringe bitmaps. */
166 enum fringe_bitmap_type
168 NO_FRINGE_BITMAP,
169 LEFT_TRUNCATION_BITMAP,
170 RIGHT_TRUNCATION_BITMAP,
171 OVERLAY_ARROW_BITMAP,
172 CONTINUED_LINE_BITMAP,
173 CONTINUATION_LINE_BITMAP,
174 ZV_LINE_BITMAP
177 /* Bitmap drawn to indicate lines not displaying text if
178 `indicate-empty-lines' is non-nil. */
180 #define zv_width 8
181 #define zv_height 72
182 #define zv_period 3
183 static unsigned char zv_bits[] = {
184 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
185 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
186 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
187 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
188 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
189 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
190 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
191 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
193 /* An arrow like this: `<-'. */
195 #define left_width 8
196 #define left_height 8
197 static unsigned char left_bits[] = {
198 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
200 /* Right truncation arrow bitmap `->'. */
202 #define right_width 8
203 #define right_height 8
204 static unsigned char right_bits[] = {
205 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
207 /* Marker for continued lines. */
209 #define continued_width 8
210 #define continued_height 8
211 static unsigned char continued_bits[] = {
212 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
214 /* Marker for continuation lines. */
216 #define continuation_width 8
217 #define continuation_height 8
218 static unsigned char continuation_bits[] = {
219 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
221 /* Overlay arrow bitmap. */
223 #if 0
224 /* A bomb. */
225 #define ov_width 8
226 #define ov_height 8
227 static unsigned char ov_bits[] = {
228 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
229 #else
230 /* A triangular arrow. */
231 #define ov_width 8
232 #define ov_height 8
233 static unsigned char ov_bits[] = {
234 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
236 #endif
238 extern Lisp_Object Qhelp_echo;
241 /* Non-nil means Emacs uses toolkit scroll bars. */
243 Lisp_Object Vx_toolkit_scroll_bars;
245 /* If a string, XTread_socket generates an event to display that string.
246 (The display is done in read_char.) */
248 static Lisp_Object help_echo;
249 static Lisp_Object help_echo_window;
250 static Lisp_Object help_echo_object;
251 static int help_echo_pos;
253 /* Temporary variable for XTread_socket. */
255 static Lisp_Object previous_help_echo;
257 /* Non-zero means that a HELP_EVENT has been generated since Emacs
258 start. */
260 static int any_help_event_p;
262 /* Non-zero means autoselect window with the mouse cursor. */
264 int x_autoselect_window_p;
266 /* Non-zero means draw block and hollow cursor as wide as the glyph
267 under it. For example, if a block cursor is over a tab, it will be
268 drawn as wide as that tab on the display. */
270 int x_stretch_cursor_p;
272 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
274 int x_use_underline_position_properties;
276 /* This is a chain of structures for all the X displays currently in
277 use. */
279 struct x_display_info *x_display_list;
281 /* This is a list of cons cells, each of the form (NAME
282 . FONT-LIST-CACHE), one for each element of x_display_list and in
283 the same order. NAME is the name of the frame. FONT-LIST-CACHE
284 records previous values returned by x-list-fonts. */
286 Lisp_Object x_display_name_list;
288 /* Frame being updated by update_frame. This is declared in term.c.
289 This is set by update_begin and looked at by all the XT functions.
290 It is zero while not inside an update. In that case, the XT
291 functions assume that `selected_frame' is the frame to apply to. */
293 extern struct frame *updating_frame;
295 /* This is a frame waiting to be auto-raised, within XTread_socket. */
297 struct frame *pending_autoraise_frame;
299 #ifdef USE_X_TOOLKIT
300 /* The application context for Xt use. */
301 XtAppContext Xt_app_con;
302 static String Xt_default_resources[] = {0};
303 #endif /* USE_X_TOOLKIT */
305 /* Nominal cursor position -- where to draw output.
306 HPOS and VPOS are window relative glyph matrix coordinates.
307 X and Y are window relative pixel coordinates. */
309 struct cursor_pos output_cursor;
311 /* Non-zero means user is interacting with a toolkit scroll bar. */
313 static int toolkit_scroll_bar_interaction;
315 /* Mouse movement.
317 Formerly, we used PointerMotionHintMask (in standard_event_mask)
318 so that we would have to call XQueryPointer after each MotionNotify
319 event to ask for another such event. However, this made mouse tracking
320 slow, and there was a bug that made it eventually stop.
322 Simply asking for MotionNotify all the time seems to work better.
324 In order to avoid asking for motion events and then throwing most
325 of them away or busy-polling the server for mouse positions, we ask
326 the server for pointer motion hints. This means that we get only
327 one event per group of mouse movements. "Groups" are delimited by
328 other kinds of events (focus changes and button clicks, for
329 example), or by XQueryPointer calls; when one of these happens, we
330 get another MotionNotify event the next time the mouse moves. This
331 is at least as efficient as getting motion events when mouse
332 tracking is on, and I suspect only negligibly worse when tracking
333 is off. */
335 /* Where the mouse was last time we reported a mouse event. */
337 FRAME_PTR last_mouse_frame;
338 static XRectangle last_mouse_glyph;
339 static Lisp_Object last_mouse_press_frame;
341 /* The scroll bar in which the last X motion event occurred.
343 If the last X motion event occurred in a scroll bar, we set this so
344 XTmouse_position can know whether to report a scroll bar motion or
345 an ordinary motion.
347 If the last X motion event didn't occur in a scroll bar, we set
348 this to Qnil, to tell XTmouse_position to return an ordinary motion
349 event. */
351 static Lisp_Object last_mouse_scroll_bar;
353 /* This is a hack. We would really prefer that XTmouse_position would
354 return the time associated with the position it returns, but there
355 doesn't seem to be any way to wrest the time-stamp from the server
356 along with the position query. So, we just keep track of the time
357 of the last movement we received, and return that in hopes that
358 it's somewhat accurate. */
360 static Time last_mouse_movement_time;
362 /* Incremented by XTread_socket whenever it really tries to read
363 events. */
365 #ifdef __STDC__
366 static int volatile input_signal_count;
367 #else
368 static int input_signal_count;
369 #endif
371 /* Used locally within XTread_socket. */
373 static int x_noop_count;
375 /* Initial values of argv and argc. */
377 extern char **initial_argv;
378 extern int initial_argc;
380 extern Lisp_Object Vcommand_line_args, Vsystem_name;
382 /* Tells if a window manager is present or not. */
384 extern Lisp_Object Vx_no_window_manager;
386 extern Lisp_Object Qface, Qmouse_face;
388 extern int errno;
390 /* A mask of extra modifier bits to put into every keyboard char. */
392 extern int extra_keyboard_modifiers;
394 /* The keysyms to use for the various modifiers. */
396 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
397 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
399 static Lisp_Object Qvendor_specific_keysyms;
401 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
402 extern Lisp_Object x_icon_type P_ ((struct frame *));
405 /* Enumeration for overriding/changing the face to use for drawing
406 glyphs in x_draw_glyphs. */
408 enum draw_glyphs_face
410 DRAW_NORMAL_TEXT,
411 DRAW_INVERSE_VIDEO,
412 DRAW_CURSOR,
413 DRAW_MOUSE_FACE,
414 DRAW_IMAGE_RAISED,
415 DRAW_IMAGE_SUNKEN
418 static int cursor_in_mouse_face_p P_ ((struct window *));
419 static int clear_mouse_face P_ ((struct x_display_info *));
420 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
421 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
422 static const XColor *x_color_cells P_ ((Display *, int *));
423 static void x_update_window_end P_ ((struct window *, int, int));
424 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
425 void x_delete_display P_ ((struct x_display_info *));
426 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
427 unsigned));
428 static int fast_find_position P_ ((struct window *, int, int *, int *,
429 int *, int *, Lisp_Object));
430 static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
431 int *, int *, int *, int *, int));
432 static void set_output_cursor P_ ((struct cursor_pos *));
433 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
434 int *, int *, int *, int));
435 static void note_mode_line_highlight P_ ((struct window *, int, int));
436 static void note_mouse_highlight P_ ((struct frame *, int, int));
437 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
438 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
439 static void show_mouse_face P_ ((struct x_display_info *,
440 enum draw_glyphs_face));
441 static int x_io_error_quitter P_ ((Display *));
442 int x_catch_errors P_ ((Display *));
443 void x_uncatch_errors P_ ((Display *, int));
444 void x_lower_frame P_ ((struct frame *));
445 void x_scroll_bar_clear P_ ((struct frame *));
446 int x_had_errors_p P_ ((Display *));
447 void x_wm_set_size_hint P_ ((struct frame *, long, int));
448 void x_raise_frame P_ ((struct frame *));
449 void x_set_window_size P_ ((struct frame *, int, int, int));
450 void x_wm_set_window_state P_ ((struct frame *, int));
451 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
452 void x_initialize P_ ((void));
453 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
454 static int x_compute_min_glyph_bounds P_ ((struct frame *));
455 static void x_draw_phys_cursor_glyph P_ ((struct window *,
456 struct glyph_row *,
457 enum draw_glyphs_face));
458 static void x_update_end P_ ((struct frame *));
459 static void XTframe_up_to_date P_ ((struct frame *));
460 static void XTset_terminal_modes P_ ((void));
461 static void XTreset_terminal_modes P_ ((void));
462 static void XTcursor_to P_ ((int, int, int, int));
463 static void x_write_glyphs P_ ((struct glyph *, int));
464 static void x_clear_end_of_line P_ ((int));
465 static void x_clear_frame P_ ((void));
466 static void x_clear_cursor P_ ((struct window *));
467 static void frame_highlight P_ ((struct frame *));
468 static void frame_unhighlight P_ ((struct frame *));
469 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
470 static void XTframe_rehighlight P_ ((struct frame *));
471 static void x_frame_rehighlight P_ ((struct x_display_info *));
472 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
473 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
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;
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 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1146 static void x_encode_char P_ ((int, XChar2b *, struct font_info *,
1147 struct charset *));
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, charset)
1232 int c;
1233 XChar2b *char2b;
1234 struct font_info *font_info;
1235 struct charset *charset;
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_ID (charset);
1250 ccl->reg[1] = char2b->byte2;
1252 else
1254 ccl->reg[0] = CHARSET_ID (charset);
1255 ccl->reg[1] = char2b->byte1;
1256 ccl->reg[2] = char2b->byte2;
1259 ccl_driver (ccl, NULL, NULL, 0, 0);
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_type)
1270 /* Fixed encoding scheme. See fontset.h for the meaning of the
1271 encoding numbers. */
1272 unsigned char enc = font_info->encoding_type;
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. Value is a pointer to a
1287 realized face that is ready for display. */
1289 static INLINE struct face *
1290 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1291 struct frame *f;
1292 int c, face_id;
1293 XChar2b *char2b;
1294 int multibyte_p;
1296 struct face *face = FACE_FROM_ID (f, face_id);
1298 if (!multibyte_p)
1300 /* Unibyte case. We don't have to encode, but we have to make
1301 sure to use a face suitable for unibyte. */
1302 char2b->byte1 = 0;
1303 char2b->byte2 = c;
1304 face_id = FACE_FOR_CHAR (f, face, c);
1305 face = FACE_FROM_ID (f, face_id);
1307 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1309 /* Case of ASCII in a face known to fit ASCII. */
1310 char2b->byte1 = 0;
1311 char2b->byte2 = c;
1313 else if (face->font != NULL)
1315 struct font_info *font_info
1316 = FONT_INFO_FROM_ID (f, face->font_info_id);
1317 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
1318 unsigned code = ENCODE_CHAR (charset, c);
1320 if (CHARSET_DIMENSION (charset) == 1)
1321 char2b->byte1 = 0, char2b->byte2 = code;
1322 else
1323 char2b->byte1 = code >> 8, char2b->byte2 = code & 0xFF;
1325 /* Maybe encode the character in *CHAR2B. */
1326 x_encode_char (c, char2b, font_info, charset);
1329 /* Make sure X resources of the face are allocated. */
1330 xassert (face != NULL);
1331 PREPARE_FACE_FOR_DISPLAY (f, face);
1333 return face;
1337 /* Get face and two-byte form of character glyph GLYPH on frame F.
1338 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1339 a pointer to a realized face that is ready for display. */
1341 static INLINE struct face *
1342 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1343 struct frame *f;
1344 struct glyph *glyph;
1345 XChar2b *char2b;
1346 int *two_byte_p;
1348 struct face *face;
1350 xassert (glyph->type == CHAR_GLYPH);
1351 face = FACE_FROM_ID (f, glyph->face_id);
1353 if (two_byte_p)
1354 *two_byte_p = 0;
1356 if (!glyph->multibyte_p)
1358 /* Unibyte case. We don't have to encode, but we have to make
1359 sure to use a face suitable for unibyte. */
1360 char2b->byte1 = 0;
1361 char2b->byte2 = glyph->u.ch;
1363 else if (glyph->u.ch < 128
1364 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1366 /* Case of ASCII in a face known to fit ASCII. */
1367 char2b->byte1 = 0;
1368 char2b->byte2 = glyph->u.ch;
1370 else
1372 struct font_info *font_info
1373 = FONT_INFO_FROM_ID (f, face->font_info_id);
1374 if (font_info)
1376 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
1377 unsigned code = ENCODE_CHAR (charset, glyph->u.ch);
1379 if (CHARSET_DIMENSION (charset) == 1)
1380 char2b->byte1 = 0, char2b->byte2 = code;
1381 else
1382 char2b->byte1 = code >> 8, char2b->byte2 = code & 0xFF;
1384 /* Maybe encode the character in *CHAR2B. */
1385 if (CHARSET_ID (charset) != charset_ascii)
1387 x_encode_char (glyph->u.ch, char2b, font_info, charset);
1388 if (two_byte_p)
1389 *two_byte_p
1390 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1395 /* Make sure X resources of the face are allocated. */
1396 xassert (face != NULL);
1397 PREPARE_FACE_FOR_DISPLAY (f, face);
1398 return face;
1402 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1403 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1405 static INLINE void
1406 x_append_glyph (it)
1407 struct it *it;
1409 struct glyph *glyph;
1410 enum glyph_row_area area = it->area;
1412 xassert (it->glyph_row);
1413 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1415 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1416 if (glyph < it->glyph_row->glyphs[area + 1])
1418 glyph->charpos = CHARPOS (it->position);
1419 glyph->object = it->object;
1420 glyph->pixel_width = it->pixel_width;
1421 glyph->voffset = it->voffset;
1422 glyph->type = CHAR_GLYPH;
1423 glyph->multibyte_p = it->multibyte_p;
1424 glyph->left_box_line_p = it->start_of_box_run_p;
1425 glyph->right_box_line_p = it->end_of_box_run_p;
1426 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1427 || it->phys_descent > it->descent);
1428 glyph->padding_p = 0;
1429 glyph->glyph_not_available_p = it->glyph_not_available_p;
1430 glyph->face_id = it->face_id;
1431 glyph->u.ch = it->char_to_display;
1432 ++it->glyph_row->used[area];
1436 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1437 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1439 static INLINE void
1440 x_append_composite_glyph (it)
1441 struct it *it;
1443 struct glyph *glyph;
1444 enum glyph_row_area area = it->area;
1446 xassert (it->glyph_row);
1448 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1449 if (glyph < it->glyph_row->glyphs[area + 1])
1451 glyph->charpos = CHARPOS (it->position);
1452 glyph->object = it->object;
1453 glyph->pixel_width = it->pixel_width;
1454 glyph->voffset = it->voffset;
1455 glyph->type = COMPOSITE_GLYPH;
1456 glyph->multibyte_p = it->multibyte_p;
1457 glyph->left_box_line_p = it->start_of_box_run_p;
1458 glyph->right_box_line_p = it->end_of_box_run_p;
1459 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1460 || it->phys_descent > it->descent);
1461 glyph->padding_p = 0;
1462 glyph->glyph_not_available_p = 0;
1463 glyph->face_id = it->face_id;
1464 glyph->u.cmp_id = it->cmp_id;
1465 ++it->glyph_row->used[area];
1470 /* Change IT->ascent and IT->height according to the setting of
1471 IT->voffset. */
1473 static INLINE void
1474 take_vertical_position_into_account (it)
1475 struct it *it;
1477 if (it->voffset)
1479 if (it->voffset < 0)
1480 /* Increase the ascent so that we can display the text higher
1481 in the line. */
1482 it->ascent += abs (it->voffset);
1483 else
1484 /* Increase the descent so that we can display the text lower
1485 in the line. */
1486 it->descent += it->voffset;
1491 /* Produce glyphs/get display metrics for the image IT is loaded with.
1492 See the description of struct display_iterator in dispextern.h for
1493 an overview of struct display_iterator. */
1495 static void
1496 x_produce_image_glyph (it)
1497 struct it *it;
1499 struct image *img;
1500 struct face *face;
1502 xassert (it->what == IT_IMAGE);
1504 face = FACE_FROM_ID (it->f, it->face_id);
1505 img = IMAGE_FROM_ID (it->f, it->image_id);
1506 xassert (img);
1508 /* Make sure X resources of the face and image are loaded. */
1509 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1510 prepare_image_for_display (it->f, img);
1512 it->ascent = it->phys_ascent = image_ascent (img, face);
1513 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1514 it->pixel_width = img->width + 2 * img->hmargin;
1516 it->nglyphs = 1;
1518 if (face->box != FACE_NO_BOX)
1520 if (face->box_line_width > 0)
1522 it->ascent += face->box_line_width;
1523 it->descent += face->box_line_width;
1526 if (it->start_of_box_run_p)
1527 it->pixel_width += abs (face->box_line_width);
1528 if (it->end_of_box_run_p)
1529 it->pixel_width += abs (face->box_line_width);
1532 take_vertical_position_into_account (it);
1534 if (it->glyph_row)
1536 struct glyph *glyph;
1537 enum glyph_row_area area = it->area;
1539 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1540 if (glyph < it->glyph_row->glyphs[area + 1])
1542 glyph->charpos = CHARPOS (it->position);
1543 glyph->object = it->object;
1544 glyph->pixel_width = it->pixel_width;
1545 glyph->voffset = it->voffset;
1546 glyph->type = IMAGE_GLYPH;
1547 glyph->multibyte_p = it->multibyte_p;
1548 glyph->left_box_line_p = it->start_of_box_run_p;
1549 glyph->right_box_line_p = it->end_of_box_run_p;
1550 glyph->overlaps_vertically_p = 0;
1551 glyph->padding_p = 0;
1552 glyph->glyph_not_available_p = 0;
1553 glyph->face_id = it->face_id;
1554 glyph->u.img_id = img->id;
1555 ++it->glyph_row->used[area];
1561 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1562 of the glyph, WIDTH and HEIGHT are the width and height of the
1563 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1564 ascent of the glyph (0 <= ASCENT <= 1). */
1566 static void
1567 x_append_stretch_glyph (it, object, width, height, ascent)
1568 struct it *it;
1569 Lisp_Object object;
1570 int width, height;
1571 double ascent;
1573 struct glyph *glyph;
1574 enum glyph_row_area area = it->area;
1576 xassert (ascent >= 0 && ascent <= 1);
1578 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1579 if (glyph < it->glyph_row->glyphs[area + 1])
1581 glyph->charpos = CHARPOS (it->position);
1582 glyph->object = object;
1583 glyph->pixel_width = width;
1584 glyph->voffset = it->voffset;
1585 glyph->type = STRETCH_GLYPH;
1586 glyph->multibyte_p = it->multibyte_p;
1587 glyph->left_box_line_p = it->start_of_box_run_p;
1588 glyph->right_box_line_p = it->end_of_box_run_p;
1589 glyph->overlaps_vertically_p = 0;
1590 glyph->padding_p = 0;
1591 glyph->glyph_not_available_p = 0;
1592 glyph->face_id = it->face_id;
1593 glyph->u.stretch.ascent = height * ascent;
1594 glyph->u.stretch.height = height;
1595 ++it->glyph_row->used[area];
1600 /* Produce a stretch glyph for iterator IT. IT->object is the value
1601 of the glyph property displayed. The value must be a list
1602 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1603 being recognized:
1605 1. `:width WIDTH' specifies that the space should be WIDTH *
1606 canonical char width wide. WIDTH may be an integer or floating
1607 point number.
1609 2. `:relative-width FACTOR' specifies that the width of the stretch
1610 should be computed from the width of the first character having the
1611 `glyph' property, and should be FACTOR times that width.
1613 3. `:align-to HPOS' specifies that the space should be wide enough
1614 to reach HPOS, a value in canonical character units.
1616 Exactly one of the above pairs must be present.
1618 4. `:height HEIGHT' specifies that the height of the stretch produced
1619 should be HEIGHT, measured in canonical character units.
1621 5. `:relative-height FACTOR' specifies that the height of the
1622 stretch should be FACTOR times the height of the characters having
1623 the glyph property.
1625 Either none or exactly one of 4 or 5 must be present.
1627 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1628 of the stretch should be used for the ascent of the stretch.
1629 ASCENT must be in the range 0 <= ASCENT <= 100. */
1631 #define NUMVAL(X) \
1632 ((INTEGERP (X) || FLOATP (X)) \
1633 ? XFLOATINT (X) \
1634 : - 1)
1637 static void
1638 x_produce_stretch_glyph (it)
1639 struct it *it;
1641 /* (space :width WIDTH :height HEIGHT. */
1642 #if GLYPH_DEBUG
1643 extern Lisp_Object Qspace;
1644 #endif
1645 extern Lisp_Object QCwidth, QCheight, QCascent;
1646 extern Lisp_Object QCrelative_width, QCrelative_height;
1647 extern Lisp_Object QCalign_to;
1648 Lisp_Object prop, plist;
1649 double width = 0, height = 0, ascent = 0;
1650 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1651 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1653 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1655 /* List should start with `space'. */
1656 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1657 plist = XCDR (it->object);
1659 /* Compute the width of the stretch. */
1660 if (prop = Fplist_get (plist, QCwidth),
1661 NUMVAL (prop) > 0)
1662 /* Absolute width `:width WIDTH' specified and valid. */
1663 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1664 else if (prop = Fplist_get (plist, QCrelative_width),
1665 NUMVAL (prop) > 0)
1667 /* Relative width `:relative-width FACTOR' specified and valid.
1668 Compute the width of the characters having the `glyph'
1669 property. */
1670 struct it it2;
1671 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1673 it2 = *it;
1674 if (it->multibyte_p)
1676 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1677 - IT_BYTEPOS (*it));
1678 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1680 else
1681 it2.c = *p, it2.len = 1;
1683 it2.glyph_row = NULL;
1684 it2.what = IT_CHARACTER;
1685 x_produce_glyphs (&it2);
1686 width = NUMVAL (prop) * it2.pixel_width;
1688 else if (prop = Fplist_get (plist, QCalign_to),
1689 NUMVAL (prop) > 0)
1690 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1691 else
1692 /* Nothing specified -> width defaults to canonical char width. */
1693 width = CANON_X_UNIT (it->f);
1695 /* Compute height. */
1696 if (prop = Fplist_get (plist, QCheight),
1697 NUMVAL (prop) > 0)
1698 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1699 else if (prop = Fplist_get (plist, QCrelative_height),
1700 NUMVAL (prop) > 0)
1701 height = FONT_HEIGHT (font) * NUMVAL (prop);
1702 else
1703 height = FONT_HEIGHT (font);
1705 /* Compute percentage of height used for ascent. If
1706 `:ascent ASCENT' is present and valid, use that. Otherwise,
1707 derive the ascent from the font in use. */
1708 if (prop = Fplist_get (plist, QCascent),
1709 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1710 ascent = NUMVAL (prop) / 100.0;
1711 else
1712 ascent = (double) font->ascent / FONT_HEIGHT (font);
1714 if (width <= 0)
1715 width = 1;
1716 if (height <= 0)
1717 height = 1;
1719 if (it->glyph_row)
1721 Lisp_Object object = it->stack[it->sp - 1].string;
1722 if (!STRINGP (object))
1723 object = it->w->buffer;
1724 x_append_stretch_glyph (it, object, width, height, ascent);
1727 it->pixel_width = width;
1728 it->ascent = it->phys_ascent = height * ascent;
1729 it->descent = it->phys_descent = height - it->ascent;
1730 it->nglyphs = 1;
1732 if (face->box != FACE_NO_BOX)
1734 if (face->box_line_width > 0)
1736 it->ascent += face->box_line_width;
1737 it->descent += face->box_line_width;
1740 if (it->start_of_box_run_p)
1741 it->pixel_width += abs (face->box_line_width);
1742 if (it->end_of_box_run_p)
1743 it->pixel_width += abs (face->box_line_width);
1746 take_vertical_position_into_account (it);
1749 /* Return proper value to be used as baseline offset of font that has
1750 ASCENT and DESCENT to draw characters by the font at the vertical
1751 center of the line of frame F.
1753 Here, out task is to find the value of BOFF in the following figure;
1755 -------------------------+-----------+-
1756 -+-+---------+-+ | |
1757 | | | | | |
1758 | | | | F_ASCENT F_HEIGHT
1759 | | | ASCENT | |
1760 HEIGHT | | | | |
1761 | | |-|-+------+-----------|------- baseline
1762 | | | | BOFF | |
1763 | |---------|-+-+ | |
1764 | | | DESCENT | |
1765 -+-+---------+-+ F_DESCENT |
1766 -------------------------+-----------+-
1768 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1769 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1770 DESCENT = FONT->descent
1771 HEIGHT = FONT_HEIGHT (FONT)
1772 F_DESCENT = (F->output_data.x->font->descent
1773 - F->output_data.x->baseline_offset)
1774 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1777 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1778 ((FONT)->descent \
1779 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1780 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1781 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1783 /* Produce glyphs/get display metrics for the display element IT is
1784 loaded with. See the description of struct display_iterator in
1785 dispextern.h for an overview of struct display_iterator. */
1787 static void
1788 x_produce_glyphs (it)
1789 struct it *it;
1791 it->glyph_not_available_p = 0;
1793 if (it->what == IT_CHARACTER)
1795 XChar2b char2b;
1796 XFontStruct *font;
1797 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1798 XCharStruct *pcm;
1799 int font_not_found_p;
1800 struct font_info *font_info;
1801 int boff; /* baseline offset */
1802 /* We may change it->multibyte_p upon unibyte<->multibyte
1803 conversion. So, save the current value now and restore it
1804 later.
1806 Note: It seems that we don't have to record multibyte_p in
1807 struct glyph because the character code itself tells if or
1808 not the character is multibyte. Thus, in the future, we must
1809 consider eliminating the field `multibyte_p' in the struct
1810 glyph. */
1811 int saved_multibyte_p = it->multibyte_p;
1813 /* Maybe translate single-byte characters to multibyte, or the
1814 other way. */
1815 it->char_to_display = it->c;
1816 if (! ASCII_CHAR_P (it->c)
1817 && ! it->multibyte_p)
1819 if (SINGLE_BYTE_CHAR_P (it->c)
1820 && unibyte_display_via_language_environment)
1821 it->char_to_display = unibyte_char_to_multibyte (it->c);
1822 if (! SINGLE_BYTE_CHAR_P (it->c))
1824 it->multibyte_p = 1;
1825 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1826 face = FACE_FROM_ID (it->f, it->face_id);
1830 /* Get font to use. Encode IT->char_to_display. */
1831 x_get_char_face_and_encoding (it->f, it->char_to_display,
1832 it->face_id, &char2b,
1833 it->multibyte_p);
1834 font = face->font;
1836 /* When no suitable font found, use the default font. */
1837 font_not_found_p = font == NULL;
1838 if (font_not_found_p)
1840 font = FRAME_FONT (it->f);
1841 boff = it->f->output_data.x->baseline_offset;
1842 font_info = NULL;
1844 else
1846 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1847 boff = font_info->baseline_offset;
1848 if (font_info->vertical_centering)
1849 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1852 if (it->char_to_display >= ' '
1853 && (!it->multibyte_p || it->char_to_display < 128))
1855 /* Either unibyte or ASCII. */
1856 int stretched_p;
1858 it->nglyphs = 1;
1860 pcm = x_per_char_metric (font, &char2b);
1861 it->ascent = font->ascent + boff;
1862 it->descent = font->descent - boff;
1864 if (pcm)
1866 it->phys_ascent = pcm->ascent + boff;
1867 it->phys_descent = pcm->descent - boff;
1868 it->pixel_width = pcm->width;
1870 else
1872 it->glyph_not_available_p = 1;
1873 it->phys_ascent = font->ascent + boff;
1874 it->phys_descent = font->descent - boff;
1875 it->pixel_width = FONT_WIDTH (font);
1878 /* If this is a space inside a region of text with
1879 `space-width' property, change its width. */
1880 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1881 if (stretched_p)
1882 it->pixel_width *= XFLOATINT (it->space_width);
1884 /* If face has a box, add the box thickness to the character
1885 height. If character has a box line to the left and/or
1886 right, add the box line width to the character's width. */
1887 if (face->box != FACE_NO_BOX)
1889 int thick = face->box_line_width;
1891 if (thick > 0)
1893 it->ascent += thick;
1894 it->descent += thick;
1896 else
1897 thick = -thick;
1899 if (it->start_of_box_run_p)
1900 it->pixel_width += thick;
1901 if (it->end_of_box_run_p)
1902 it->pixel_width += thick;
1905 /* If face has an overline, add the height of the overline
1906 (1 pixel) and a 1 pixel margin to the character height. */
1907 if (face->overline_p)
1908 it->ascent += 2;
1910 take_vertical_position_into_account (it);
1912 /* If we have to actually produce glyphs, do it. */
1913 if (it->glyph_row)
1915 if (stretched_p)
1917 /* Translate a space with a `space-width' property
1918 into a stretch glyph. */
1919 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1920 x_append_stretch_glyph (it, it->object, it->pixel_width,
1921 it->ascent + it->descent, ascent);
1923 else
1924 x_append_glyph (it);
1926 /* If characters with lbearing or rbearing are displayed
1927 in this line, record that fact in a flag of the
1928 glyph row. This is used to optimize X output code. */
1929 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1930 it->glyph_row->contains_overlapping_glyphs_p = 1;
1933 else if (it->char_to_display == '\n')
1935 /* A newline has no width but we need the height of the line. */
1936 it->pixel_width = 0;
1937 it->nglyphs = 0;
1938 it->ascent = it->phys_ascent = font->ascent + boff;
1939 it->descent = it->phys_descent = font->descent - boff;
1941 if (face->box != FACE_NO_BOX
1942 && face->box_line_width > 0)
1944 it->ascent += face->box_line_width;
1945 it->descent += face->box_line_width;
1948 else if (it->char_to_display == '\t')
1950 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1951 int x = it->current_x + it->continuation_lines_width;
1952 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1954 /* If the distance from the current position to the next tab
1955 stop is less than a canonical character width, use the
1956 tab stop after that. */
1957 if (next_tab_x - x < CANON_X_UNIT (it->f))
1958 next_tab_x += tab_width;
1960 it->pixel_width = next_tab_x - x;
1961 it->nglyphs = 1;
1962 it->ascent = it->phys_ascent = font->ascent + boff;
1963 it->descent = it->phys_descent = font->descent - boff;
1965 if (it->glyph_row)
1967 double ascent = (double) it->ascent / (it->ascent + it->descent);
1968 x_append_stretch_glyph (it, it->object, it->pixel_width,
1969 it->ascent + it->descent, ascent);
1972 else
1974 /* A multi-byte character. Assume that the display width of the
1975 character is the width of the character multiplied by the
1976 width of the font. */
1978 /* If we found a font, this font should give us the right
1979 metrics. If we didn't find a font, use the frame's
1980 default font and calculate the width of the character by
1981 multiplying the width of font by the width of the
1982 character. */
1983 pcm = x_per_char_metric (font, &char2b);
1984 if (font_not_found_p || !pcm)
1986 it->glyph_not_available_p = 1;
1987 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1988 * CHAR_WIDTH (it->char_to_display));
1989 it->phys_ascent = font->ascent + boff;
1990 it->phys_descent = font->descent - boff;
1992 else
1994 it->pixel_width = pcm->width;
1995 it->phys_ascent = pcm->ascent + boff;
1996 it->phys_descent = pcm->descent - boff;
1997 if (it->glyph_row
1998 && (pcm->lbearing < 0
1999 || pcm->rbearing > pcm->width))
2000 it->glyph_row->contains_overlapping_glyphs_p = 1;
2002 it->nglyphs = 1;
2003 it->ascent = font->ascent + boff;
2004 it->descent = font->descent - boff;
2005 if (face->box != FACE_NO_BOX)
2007 int thick = face->box_line_width;
2009 if (thick > 0)
2011 it->ascent += thick;
2012 it->descent += thick;
2014 else
2015 thick = - thick;
2017 if (it->start_of_box_run_p)
2018 it->pixel_width += thick;
2019 if (it->end_of_box_run_p)
2020 it->pixel_width += thick;
2023 /* If face has an overline, add the height of the overline
2024 (1 pixel) and a 1 pixel margin to the character height. */
2025 if (face->overline_p)
2026 it->ascent += 2;
2028 take_vertical_position_into_account (it);
2030 if (it->glyph_row)
2031 x_append_glyph (it);
2033 it->multibyte_p = saved_multibyte_p;
2035 else if (it->what == IT_COMPOSITION)
2037 /* Note: A composition is represented as one glyph in the
2038 glyph matrix. There are no padding glyphs. */
2039 XChar2b char2b;
2040 XFontStruct *font;
2041 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2042 XCharStruct *pcm;
2043 int font_not_found_p;
2044 struct font_info *font_info;
2045 int boff; /* baseline offset */
2046 struct composition *cmp = composition_table[it->cmp_id];
2048 /* Maybe translate single-byte characters to multibyte. */
2049 it->char_to_display = it->c;
2050 if (unibyte_display_via_language_environment
2051 && it->c >= 0200)
2053 it->char_to_display = unibyte_char_to_multibyte (it->c);
2056 /* Get face and font to use. Encode IT->char_to_display. */
2057 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2058 face = FACE_FROM_ID (it->f, it->face_id);
2059 x_get_char_face_and_encoding (it->f, it->char_to_display,
2060 it->face_id, &char2b, it->multibyte_p);
2061 font = face->font;
2063 /* When no suitable font found, use the default font. */
2064 font_not_found_p = font == NULL;
2065 if (font_not_found_p)
2067 font = FRAME_FONT (it->f);
2068 boff = it->f->output_data.x->baseline_offset;
2069 font_info = NULL;
2071 else
2073 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2074 boff = font_info->baseline_offset;
2075 if (font_info->vertical_centering)
2076 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2079 /* There are no padding glyphs, so there is only one glyph to
2080 produce for the composition. Important is that pixel_width,
2081 ascent and descent are the values of what is drawn by
2082 draw_glyphs (i.e. the values of the overall glyphs composed). */
2083 it->nglyphs = 1;
2085 /* If we have not yet calculated pixel size data of glyphs of
2086 the composition for the current face font, calculate them
2087 now. Theoretically, we have to check all fonts for the
2088 glyphs, but that requires much time and memory space. So,
2089 here we check only the font of the first glyph. This leads
2090 to incorrect display very rarely, and C-l (recenter) can
2091 correct the display anyway. */
2092 if (cmp->font != (void *) font)
2094 /* Ascent and descent of the font of the first character of
2095 this composition (adjusted by baseline offset). Ascent
2096 and descent of overall glyphs should not be less than
2097 them respectively. */
2098 int font_ascent = font->ascent + boff;
2099 int font_descent = font->descent - boff;
2100 /* Bounding box of the overall glyphs. */
2101 int leftmost, rightmost, lowest, highest;
2102 int i, width, ascent, descent;
2104 cmp->font = (void *) font;
2106 /* Initialize the bounding box. */
2107 if (font_info
2108 && (pcm = x_per_char_metric (font, &char2b)))
2110 width = pcm->width;
2111 ascent = pcm->ascent;
2112 descent = pcm->descent;
2114 else
2116 width = FONT_WIDTH (font);
2117 ascent = font->ascent;
2118 descent = font->descent;
2121 rightmost = width;
2122 lowest = - descent + boff;
2123 highest = ascent + boff;
2124 leftmost = 0;
2126 if (font_info
2127 && font_info->default_ascent
2128 && CHAR_TABLE_P (Vuse_default_ascent)
2129 && !NILP (Faref (Vuse_default_ascent,
2130 make_number (it->char_to_display))))
2131 highest = font_info->default_ascent + boff;
2133 /* Draw the first glyph at the normal position. It may be
2134 shifted to right later if some other glyphs are drawn at
2135 the left. */
2136 cmp->offsets[0] = 0;
2137 cmp->offsets[1] = boff;
2139 /* Set cmp->offsets for the remaining glyphs. */
2140 for (i = 1; i < cmp->glyph_len; i++)
2142 int left, right, btm, top;
2143 int ch = COMPOSITION_GLYPH (cmp, i);
2144 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2146 face = FACE_FROM_ID (it->f, face_id);
2147 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2148 it->multibyte_p);
2149 font = face->font;
2150 if (font == NULL)
2152 font = FRAME_FONT (it->f);
2153 boff = it->f->output_data.x->baseline_offset;
2154 font_info = NULL;
2156 else
2158 font_info
2159 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2160 boff = font_info->baseline_offset;
2161 if (font_info->vertical_centering)
2162 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2165 if (font_info
2166 && (pcm = x_per_char_metric (font, &char2b)))
2168 width = pcm->width;
2169 ascent = pcm->ascent;
2170 descent = pcm->descent;
2172 else
2174 width = FONT_WIDTH (font);
2175 ascent = 1;
2176 descent = 0;
2179 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2181 /* Relative composition with or without
2182 alternate chars. */
2183 left = (leftmost + rightmost - width) / 2;
2184 btm = - descent + boff;
2185 if (font_info && font_info->relative_compose
2186 && (! CHAR_TABLE_P (Vignore_relative_composition)
2187 || NILP (Faref (Vignore_relative_composition,
2188 make_number (ch)))))
2191 if (- descent >= font_info->relative_compose)
2192 /* One extra pixel between two glyphs. */
2193 btm = highest + 1;
2194 else if (ascent <= 0)
2195 /* One extra pixel between two glyphs. */
2196 btm = lowest - 1 - ascent - descent;
2199 else
2201 /* A composition rule is specified by an integer
2202 value that encodes global and new reference
2203 points (GREF and NREF). GREF and NREF are
2204 specified by numbers as below:
2206 0---1---2 -- ascent
2210 9--10--11 -- center
2212 ---3---4---5--- baseline
2214 6---7---8 -- descent
2216 int rule = COMPOSITION_RULE (cmp, i);
2217 int gref, nref, grefx, grefy, nrefx, nrefy;
2219 COMPOSITION_DECODE_RULE (rule, gref, nref);
2220 grefx = gref % 3, nrefx = nref % 3;
2221 grefy = gref / 3, nrefy = nref / 3;
2223 left = (leftmost
2224 + grefx * (rightmost - leftmost) / 2
2225 - nrefx * width / 2);
2226 btm = ((grefy == 0 ? highest
2227 : grefy == 1 ? 0
2228 : grefy == 2 ? lowest
2229 : (highest + lowest) / 2)
2230 - (nrefy == 0 ? ascent + descent
2231 : nrefy == 1 ? descent - boff
2232 : nrefy == 2 ? 0
2233 : (ascent + descent) / 2));
2236 cmp->offsets[i * 2] = left;
2237 cmp->offsets[i * 2 + 1] = btm + descent;
2239 /* Update the bounding box of the overall glyphs. */
2240 right = left + width;
2241 top = btm + descent + ascent;
2242 if (left < leftmost)
2243 leftmost = left;
2244 if (right > rightmost)
2245 rightmost = right;
2246 if (top > highest)
2247 highest = top;
2248 if (btm < lowest)
2249 lowest = btm;
2252 /* If there are glyphs whose x-offsets are negative,
2253 shift all glyphs to the right and make all x-offsets
2254 non-negative. */
2255 if (leftmost < 0)
2257 for (i = 0; i < cmp->glyph_len; i++)
2258 cmp->offsets[i * 2] -= leftmost;
2259 rightmost -= leftmost;
2262 cmp->pixel_width = rightmost;
2263 cmp->ascent = highest;
2264 cmp->descent = - lowest;
2265 if (cmp->ascent < font_ascent)
2266 cmp->ascent = font_ascent;
2267 if (cmp->descent < font_descent)
2268 cmp->descent = font_descent;
2271 it->pixel_width = cmp->pixel_width;
2272 it->ascent = it->phys_ascent = cmp->ascent;
2273 it->descent = it->phys_descent = cmp->descent;
2275 if (face->box != FACE_NO_BOX)
2277 int thick = face->box_line_width;
2279 if (thick > 0)
2281 it->ascent += thick;
2282 it->descent += thick;
2284 else
2285 thick = - thick;
2287 if (it->start_of_box_run_p)
2288 it->pixel_width += thick;
2289 if (it->end_of_box_run_p)
2290 it->pixel_width += thick;
2293 /* If face has an overline, add the height of the overline
2294 (1 pixel) and a 1 pixel margin to the character height. */
2295 if (face->overline_p)
2296 it->ascent += 2;
2298 take_vertical_position_into_account (it);
2300 if (it->glyph_row)
2301 x_append_composite_glyph (it);
2303 else if (it->what == IT_IMAGE)
2304 x_produce_image_glyph (it);
2305 else if (it->what == IT_STRETCH)
2306 x_produce_stretch_glyph (it);
2308 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2309 because this isn't true for images with `:ascent 100'. */
2310 xassert (it->ascent >= 0 && it->descent >= 0);
2311 if (it->area == TEXT_AREA)
2312 it->current_x += it->pixel_width;
2314 it->descent += it->extra_line_spacing;
2316 it->max_ascent = max (it->max_ascent, it->ascent);
2317 it->max_descent = max (it->max_descent, it->descent);
2318 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2319 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2323 /* Estimate the pixel height of the mode or top line on frame F.
2324 FACE_ID specifies what line's height to estimate. */
2327 x_estimate_mode_line_height (f, face_id)
2328 struct frame *f;
2329 enum face_id face_id;
2331 int height = FONT_HEIGHT (FRAME_FONT (f));
2333 /* This function is called so early when Emacs starts that the face
2334 cache and mode line face are not yet initialized. */
2335 if (FRAME_FACE_CACHE (f))
2337 struct face *face = FACE_FROM_ID (f, face_id);
2338 if (face)
2340 if (face->font)
2341 height = FONT_HEIGHT (face->font);
2342 if (face->box_line_width > 0)
2343 height += 2 * face->box_line_width;
2347 return height;
2351 /***********************************************************************
2352 Glyph display
2353 ***********************************************************************/
2355 /* A sequence of glyphs to be drawn in the same face.
2357 This data structure is not really completely X specific, so it
2358 could possibly, at least partially, be useful for other systems. It
2359 is currently not part of the external redisplay interface because
2360 it's not clear what other systems will need. */
2362 struct glyph_string
2364 /* X-origin of the string. */
2365 int x;
2367 /* Y-origin and y-position of the base line of this string. */
2368 int y, ybase;
2370 /* The width of the string, not including a face extension. */
2371 int width;
2373 /* The width of the string, including a face extension. */
2374 int background_width;
2376 /* The height of this string. This is the height of the line this
2377 string is drawn in, and can be different from the height of the
2378 font the string is drawn in. */
2379 int height;
2381 /* Number of pixels this string overwrites in front of its x-origin.
2382 This number is zero if the string has an lbearing >= 0; it is
2383 -lbearing, if the string has an lbearing < 0. */
2384 int left_overhang;
2386 /* Number of pixels this string overwrites past its right-most
2387 nominal x-position, i.e. x + width. Zero if the string's
2388 rbearing is <= its nominal width, rbearing - width otherwise. */
2389 int right_overhang;
2391 /* The frame on which the glyph string is drawn. */
2392 struct frame *f;
2394 /* The window on which the glyph string is drawn. */
2395 struct window *w;
2397 /* X display and window for convenience. */
2398 Display *display;
2399 Window window;
2401 /* The glyph row for which this string was built. It determines the
2402 y-origin and height of the string. */
2403 struct glyph_row *row;
2405 /* The area within row. */
2406 enum glyph_row_area area;
2408 /* Characters to be drawn, and number of characters. */
2409 XChar2b *char2b;
2410 int nchars;
2412 /* A face-override for drawing cursors, mouse face and similar. */
2413 enum draw_glyphs_face hl;
2415 /* Face in which this string is to be drawn. */
2416 struct face *face;
2418 /* Font in which this string is to be drawn. */
2419 XFontStruct *font;
2421 /* Font info for this string. */
2422 struct font_info *font_info;
2424 /* Non-null means this string describes (part of) a composition.
2425 All characters from char2b are drawn composed. */
2426 struct composition *cmp;
2428 /* Index of this glyph string's first character in the glyph
2429 definition of CMP. If this is zero, this glyph string describes
2430 the first character of a composition. */
2431 int gidx;
2433 /* 1 means this glyph strings face has to be drawn to the right end
2434 of the window's drawing area. */
2435 unsigned extends_to_end_of_line_p : 1;
2437 /* 1 means the background of this string has been drawn. */
2438 unsigned background_filled_p : 1;
2440 /* 1 means glyph string must be drawn with 16-bit functions. */
2441 unsigned two_byte_p : 1;
2443 /* 1 means that the original font determined for drawing this glyph
2444 string could not be loaded. The member `font' has been set to
2445 the frame's default font in this case. */
2446 unsigned font_not_found_p : 1;
2448 /* 1 means that the face in which this glyph string is drawn has a
2449 stipple pattern. */
2450 unsigned stippled_p : 1;
2452 /* 1 means only the foreground of this glyph string must be drawn,
2453 and we should use the physical height of the line this glyph
2454 string appears in as clip rect. */
2455 unsigned for_overlaps_p : 1;
2457 /* The GC to use for drawing this glyph string. */
2458 GC gc;
2460 /* A pointer to the first glyph in the string. This glyph
2461 corresponds to char2b[0]. Needed to draw rectangles if
2462 font_not_found_p is 1. */
2463 struct glyph *first_glyph;
2465 /* Image, if any. */
2466 struct image *img;
2468 struct glyph_string *next, *prev;
2472 #if GLYPH_DEBUG
2474 static void
2475 x_dump_glyph_string (s)
2476 struct glyph_string *s;
2478 fprintf (stderr, "glyph string\n");
2479 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2480 s->x, s->y, s->width, s->height);
2481 fprintf (stderr, " ybase = %d\n", s->ybase);
2482 fprintf (stderr, " hl = %d\n", s->hl);
2483 fprintf (stderr, " left overhang = %d, right = %d\n",
2484 s->left_overhang, s->right_overhang);
2485 fprintf (stderr, " nchars = %d\n", s->nchars);
2486 fprintf (stderr, " extends to end of line = %d\n",
2487 s->extends_to_end_of_line_p);
2488 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2489 fprintf (stderr, " bg width = %d\n", s->background_width);
2492 #endif /* GLYPH_DEBUG */
2496 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2497 struct glyph_string **,
2498 struct glyph_string *,
2499 struct glyph_string *));
2500 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2501 struct glyph_string **,
2502 struct glyph_string *,
2503 struct glyph_string *));
2504 static void x_append_glyph_string P_ ((struct glyph_string **,
2505 struct glyph_string **,
2506 struct glyph_string *));
2507 static int x_left_overwritten P_ ((struct glyph_string *));
2508 static int x_left_overwriting P_ ((struct glyph_string *));
2509 static int x_right_overwritten P_ ((struct glyph_string *));
2510 static int x_right_overwriting P_ ((struct glyph_string *));
2511 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2512 int));
2513 static void x_init_glyph_string P_ ((struct glyph_string *,
2514 XChar2b *, struct window *,
2515 struct glyph_row *,
2516 enum glyph_row_area, int,
2517 enum draw_glyphs_face));
2518 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2519 enum glyph_row_area, int, int,
2520 enum draw_glyphs_face, int));
2521 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2522 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2523 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2524 int));
2525 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2526 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2527 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2528 static void x_draw_glyph_string P_ ((struct glyph_string *));
2529 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2530 static void x_set_cursor_gc P_ ((struct glyph_string *));
2531 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2532 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2533 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2534 int *, int *));
2535 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2536 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2537 unsigned long *, double, int));
2538 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2539 double, int, unsigned long));
2540 static void x_setup_relief_colors P_ ((struct glyph_string *));
2541 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2542 static void x_draw_image_relief P_ ((struct glyph_string *));
2543 static void x_draw_image_foreground P_ ((struct glyph_string *));
2544 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2545 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2546 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2547 int, int, int));
2548 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2549 int, int, int, int, XRectangle *));
2550 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2551 int, int, int, XRectangle *));
2552 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2553 enum glyph_row_area));
2554 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2555 struct glyph_row *,
2556 enum glyph_row_area, int, int));
2558 #if GLYPH_DEBUG
2559 static void x_check_font P_ ((struct frame *, XFontStruct *));
2560 #endif
2563 /* Append the list of glyph strings with head H and tail T to the list
2564 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2566 static INLINE void
2567 x_append_glyph_string_lists (head, tail, h, t)
2568 struct glyph_string **head, **tail;
2569 struct glyph_string *h, *t;
2571 if (h)
2573 if (*head)
2574 (*tail)->next = h;
2575 else
2576 *head = h;
2577 h->prev = *tail;
2578 *tail = t;
2583 /* Prepend the list of glyph strings with head H and tail T to the
2584 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2585 result. */
2587 static INLINE void
2588 x_prepend_glyph_string_lists (head, tail, h, t)
2589 struct glyph_string **head, **tail;
2590 struct glyph_string *h, *t;
2592 if (h)
2594 if (*head)
2595 (*head)->prev = t;
2596 else
2597 *tail = t;
2598 t->next = *head;
2599 *head = h;
2604 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2605 Set *HEAD and *TAIL to the resulting list. */
2607 static INLINE void
2608 x_append_glyph_string (head, tail, s)
2609 struct glyph_string **head, **tail;
2610 struct glyph_string *s;
2612 s->next = s->prev = NULL;
2613 x_append_glyph_string_lists (head, tail, s, s);
2617 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2618 face. */
2620 static void
2621 x_set_cursor_gc (s)
2622 struct glyph_string *s;
2624 if (s->font == FRAME_FONT (s->f)
2625 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2626 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2627 && !s->cmp)
2628 s->gc = s->f->output_data.x->cursor_gc;
2629 else
2631 /* Cursor on non-default face: must merge. */
2632 XGCValues xgcv;
2633 unsigned long mask;
2635 xgcv.background = s->f->output_data.x->cursor_pixel;
2636 xgcv.foreground = s->face->background;
2638 /* If the glyph would be invisible, try a different foreground. */
2639 if (xgcv.foreground == xgcv.background)
2640 xgcv.foreground = s->face->foreground;
2641 if (xgcv.foreground == xgcv.background)
2642 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2643 if (xgcv.foreground == xgcv.background)
2644 xgcv.foreground = s->face->foreground;
2646 /* Make sure the cursor is distinct from text in this face. */
2647 if (xgcv.background == s->face->background
2648 && xgcv.foreground == s->face->foreground)
2650 xgcv.background = s->face->foreground;
2651 xgcv.foreground = s->face->background;
2654 IF_DEBUG (x_check_font (s->f, s->font));
2655 xgcv.font = s->font->fid;
2656 xgcv.graphics_exposures = False;
2657 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2659 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2660 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2661 mask, &xgcv);
2662 else
2663 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2664 = XCreateGC (s->display, s->window, mask, &xgcv);
2666 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2671 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2673 static void
2674 x_set_mouse_face_gc (s)
2675 struct glyph_string *s;
2677 int face_id;
2678 struct face *face;
2680 /* What face has to be used last for the mouse face? */
2681 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2682 face = FACE_FROM_ID (s->f, face_id);
2683 if (face == NULL)
2684 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2686 if (s->first_glyph->type == CHAR_GLYPH)
2687 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2688 else
2689 face_id = FACE_FOR_CHAR (s->f, face, 0);
2690 s->face = FACE_FROM_ID (s->f, face_id);
2691 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2693 /* If font in this face is same as S->font, use it. */
2694 if (s->font == s->face->font)
2695 s->gc = s->face->gc;
2696 else
2698 /* Otherwise construct scratch_cursor_gc with values from FACE
2699 but font FONT. */
2700 XGCValues xgcv;
2701 unsigned long mask;
2703 xgcv.background = s->face->background;
2704 xgcv.foreground = s->face->foreground;
2705 IF_DEBUG (x_check_font (s->f, s->font));
2706 xgcv.font = s->font->fid;
2707 xgcv.graphics_exposures = False;
2708 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2710 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2711 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2712 mask, &xgcv);
2713 else
2714 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2715 = XCreateGC (s->display, s->window, mask, &xgcv);
2717 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2720 xassert (s->gc != 0);
2724 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2725 Faces to use in the mode line have already been computed when the
2726 matrix was built, so there isn't much to do, here. */
2728 static INLINE void
2729 x_set_mode_line_face_gc (s)
2730 struct glyph_string *s;
2732 s->gc = s->face->gc;
2736 /* Set S->gc of glyph string S for drawing that glyph string. Set
2737 S->stippled_p to a non-zero value if the face of S has a stipple
2738 pattern. */
2740 static INLINE void
2741 x_set_glyph_string_gc (s)
2742 struct glyph_string *s;
2744 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2746 if (s->hl == DRAW_NORMAL_TEXT)
2748 s->gc = s->face->gc;
2749 s->stippled_p = s->face->stipple != 0;
2751 else if (s->hl == DRAW_INVERSE_VIDEO)
2753 x_set_mode_line_face_gc (s);
2754 s->stippled_p = s->face->stipple != 0;
2756 else if (s->hl == DRAW_CURSOR)
2758 x_set_cursor_gc (s);
2759 s->stippled_p = 0;
2761 else if (s->hl == DRAW_MOUSE_FACE)
2763 x_set_mouse_face_gc (s);
2764 s->stippled_p = s->face->stipple != 0;
2766 else if (s->hl == DRAW_IMAGE_RAISED
2767 || s->hl == DRAW_IMAGE_SUNKEN)
2769 s->gc = s->face->gc;
2770 s->stippled_p = s->face->stipple != 0;
2772 else
2774 s->gc = s->face->gc;
2775 s->stippled_p = s->face->stipple != 0;
2778 /* GC must have been set. */
2779 xassert (s->gc != 0);
2783 /* Return in *R the clipping rectangle for glyph string S. */
2785 static void
2786 x_get_glyph_string_clip_rect (s, r)
2787 struct glyph_string *s;
2788 XRectangle *r;
2790 if (s->row->full_width_p)
2792 /* Draw full-width. X coordinates are relative to S->w->left. */
2793 int canon_x = CANON_X_UNIT (s->f);
2795 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2796 r->width = XFASTINT (s->w->width) * canon_x;
2798 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2800 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2801 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2802 r->x -= width;
2805 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2807 /* Unless displaying a mode or menu bar line, which are always
2808 fully visible, clip to the visible part of the row. */
2809 if (s->w->pseudo_window_p)
2810 r->height = s->row->visible_height;
2811 else
2812 r->height = s->height;
2814 else
2816 /* This is a text line that may be partially visible. */
2817 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2818 r->width = window_box_width (s->w, s->area);
2819 r->height = s->row->visible_height;
2822 /* If S draws overlapping rows, it's sufficient to use the top and
2823 bottom of the window for clipping because this glyph string
2824 intentionally draws over other lines. */
2825 if (s->for_overlaps_p)
2827 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2828 r->height = window_text_bottom_y (s->w) - r->y;
2830 else
2832 /* Don't use S->y for clipping because it doesn't take partially
2833 visible lines into account. For example, it can be negative for
2834 partially visible lines at the top of a window. */
2835 if (!s->row->full_width_p
2836 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2837 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2838 else
2839 r->y = max (0, s->row->y);
2841 /* If drawing a tool-bar window, draw it over the internal border
2842 at the top of the window. */
2843 if (s->w == XWINDOW (s->f->tool_bar_window))
2844 r->y -= s->f->output_data.x->internal_border_width;
2847 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2851 /* Set clipping for output of glyph string S. S may be part of a mode
2852 line or menu if we don't have X toolkit support. */
2854 static INLINE void
2855 x_set_glyph_string_clipping (s)
2856 struct glyph_string *s;
2858 XRectangle r;
2859 x_get_glyph_string_clip_rect (s, &r);
2860 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2864 /* Compute left and right overhang of glyph string S. If S is a glyph
2865 string for a composition, assume overhangs don't exist. */
2867 static INLINE void
2868 x_compute_glyph_string_overhangs (s)
2869 struct glyph_string *s;
2871 if (s->cmp == NULL
2872 && s->first_glyph->type == CHAR_GLYPH)
2874 XCharStruct cs;
2875 int direction, font_ascent, font_descent;
2876 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2877 &font_ascent, &font_descent, &cs);
2878 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2879 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2884 /* Compute overhangs and x-positions for glyph string S and its
2885 predecessors, or successors. X is the starting x-position for S.
2886 BACKWARD_P non-zero means process predecessors. */
2888 static void
2889 x_compute_overhangs_and_x (s, x, backward_p)
2890 struct glyph_string *s;
2891 int x;
2892 int backward_p;
2894 if (backward_p)
2896 while (s)
2898 x_compute_glyph_string_overhangs (s);
2899 x -= s->width;
2900 s->x = x;
2901 s = s->prev;
2904 else
2906 while (s)
2908 x_compute_glyph_string_overhangs (s);
2909 s->x = x;
2910 x += s->width;
2911 s = s->next;
2917 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2918 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2919 assumed to be zero. */
2921 static void
2922 x_get_glyph_overhangs (glyph, f, left, right)
2923 struct glyph *glyph;
2924 struct frame *f;
2925 int *left, *right;
2927 *left = *right = 0;
2929 if (glyph->type == CHAR_GLYPH)
2931 XFontStruct *font;
2932 struct face *face;
2933 struct font_info *font_info;
2934 XChar2b char2b;
2935 XCharStruct *pcm;
2937 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2938 font = face->font;
2939 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2940 if (font
2941 && (pcm = x_per_char_metric (font, &char2b)))
2943 if (pcm->rbearing > pcm->width)
2944 *right = pcm->rbearing - pcm->width;
2945 if (pcm->lbearing < 0)
2946 *left = -pcm->lbearing;
2952 /* Return the index of the first glyph preceding glyph string S that
2953 is overwritten by S because of S's left overhang. Value is -1
2954 if no glyphs are overwritten. */
2956 static int
2957 x_left_overwritten (s)
2958 struct glyph_string *s;
2960 int k;
2962 if (s->left_overhang)
2964 int x = 0, i;
2965 struct glyph *glyphs = s->row->glyphs[s->area];
2966 int first = s->first_glyph - glyphs;
2968 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2969 x -= glyphs[i].pixel_width;
2971 k = i + 1;
2973 else
2974 k = -1;
2976 return k;
2980 /* Return the index of the first glyph preceding glyph string S that
2981 is overwriting S because of its right overhang. Value is -1 if no
2982 glyph in front of S overwrites S. */
2984 static int
2985 x_left_overwriting (s)
2986 struct glyph_string *s;
2988 int i, k, x;
2989 struct glyph *glyphs = s->row->glyphs[s->area];
2990 int first = s->first_glyph - glyphs;
2992 k = -1;
2993 x = 0;
2994 for (i = first - 1; i >= 0; --i)
2996 int left, right;
2997 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2998 if (x + right > 0)
2999 k = i;
3000 x -= glyphs[i].pixel_width;
3003 return k;
3007 /* Return the index of the last glyph following glyph string S that is
3008 not overwritten by S because of S's right overhang. Value is -1 if
3009 no such glyph is found. */
3011 static int
3012 x_right_overwritten (s)
3013 struct glyph_string *s;
3015 int k = -1;
3017 if (s->right_overhang)
3019 int x = 0, i;
3020 struct glyph *glyphs = s->row->glyphs[s->area];
3021 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3022 int end = s->row->used[s->area];
3024 for (i = first; i < end && s->right_overhang > x; ++i)
3025 x += glyphs[i].pixel_width;
3027 k = i;
3030 return k;
3034 /* Return the index of the last glyph following glyph string S that
3035 overwrites S because of its left overhang. Value is negative
3036 if no such glyph is found. */
3038 static int
3039 x_right_overwriting (s)
3040 struct glyph_string *s;
3042 int i, k, x;
3043 int end = s->row->used[s->area];
3044 struct glyph *glyphs = s->row->glyphs[s->area];
3045 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3047 k = -1;
3048 x = 0;
3049 for (i = first; i < end; ++i)
3051 int left, right;
3052 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3053 if (x - left < 0)
3054 k = i;
3055 x += glyphs[i].pixel_width;
3058 return k;
3062 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3064 static INLINE void
3065 x_clear_glyph_string_rect (s, x, y, w, h)
3066 struct glyph_string *s;
3067 int x, y, w, h;
3069 XGCValues xgcv;
3070 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3071 XSetForeground (s->display, s->gc, xgcv.background);
3072 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3073 XSetForeground (s->display, s->gc, xgcv.foreground);
3077 /* Draw the background of glyph_string S. If S->background_filled_p
3078 is non-zero don't draw it. FORCE_P non-zero means draw the
3079 background even if it wouldn't be drawn normally. This is used
3080 when a string preceding S draws into the background of S, or S
3081 contains the first component of a composition. */
3083 static void
3084 x_draw_glyph_string_background (s, force_p)
3085 struct glyph_string *s;
3086 int force_p;
3088 /* Nothing to do if background has already been drawn or if it
3089 shouldn't be drawn in the first place. */
3090 if (!s->background_filled_p)
3092 int box_line_width = max (s->face->box_line_width, 0);
3094 if (s->stippled_p)
3096 /* Fill background with a stipple pattern. */
3097 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3098 XFillRectangle (s->display, s->window, s->gc, s->x,
3099 s->y + box_line_width,
3100 s->background_width,
3101 s->height - 2 * box_line_width);
3102 XSetFillStyle (s->display, s->gc, FillSolid);
3103 s->background_filled_p = 1;
3105 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
3106 || s->font_not_found_p
3107 || s->extends_to_end_of_line_p
3108 || force_p)
3110 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
3111 s->background_width,
3112 s->height - 2 * box_line_width);
3113 s->background_filled_p = 1;
3119 /* Draw the foreground of glyph string S. */
3121 static void
3122 x_draw_glyph_string_foreground (s)
3123 struct glyph_string *s;
3125 int i, x;
3127 /* If first glyph of S has a left box line, start drawing the text
3128 of S to the right of that box line. */
3129 if (s->face->box != FACE_NO_BOX
3130 && s->first_glyph->left_box_line_p)
3131 x = s->x + abs (s->face->box_line_width);
3132 else
3133 x = s->x;
3135 /* Draw characters of S as rectangles if S's font could not be
3136 loaded. */
3137 if (s->font_not_found_p)
3139 for (i = 0; i < s->nchars; ++i)
3141 struct glyph *g = s->first_glyph + i;
3142 XDrawRectangle (s->display, s->window,
3143 s->gc, x, s->y, g->pixel_width - 1,
3144 s->height - 1);
3145 x += g->pixel_width;
3148 else
3150 char *char1b = (char *) s->char2b;
3151 int boff = s->font_info->baseline_offset;
3153 if (s->font_info->vertical_centering)
3154 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3156 /* If we can use 8-bit functions, condense S->char2b. */
3157 if (!s->two_byte_p)
3158 for (i = 0; i < s->nchars; ++i)
3159 char1b[i] = s->char2b[i].byte2;
3161 /* Draw text with XDrawString if background has already been
3162 filled. Otherwise, use XDrawImageString. (Note that
3163 XDrawImageString is usually faster than XDrawString.) Always
3164 use XDrawImageString when drawing the cursor so that there is
3165 no chance that characters under a box cursor are invisible. */
3166 if (s->for_overlaps_p
3167 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3169 /* Draw characters with 16-bit or 8-bit functions. */
3170 if (s->two_byte_p)
3171 XDrawString16 (s->display, s->window, s->gc, x,
3172 s->ybase - boff, s->char2b, s->nchars);
3173 else
3174 XDrawString (s->display, s->window, s->gc, x,
3175 s->ybase - boff, char1b, s->nchars);
3177 else
3179 if (s->two_byte_p)
3180 XDrawImageString16 (s->display, s->window, s->gc, x,
3181 s->ybase - boff, s->char2b, s->nchars);
3182 else
3183 XDrawImageString (s->display, s->window, s->gc, x,
3184 s->ybase - boff, char1b, s->nchars);
3189 /* Draw the foreground of composite glyph string S. */
3191 static void
3192 x_draw_composite_glyph_string_foreground (s)
3193 struct glyph_string *s;
3195 int i, x;
3197 /* If first glyph of S has a left box line, start drawing the text
3198 of S to the right of that box line. */
3199 if (s->face->box != FACE_NO_BOX
3200 && s->first_glyph->left_box_line_p)
3201 x = s->x + abs (s->face->box_line_width);
3202 else
3203 x = s->x;
3205 /* S is a glyph string for a composition. S->gidx is the index of
3206 the first character drawn for glyphs of this composition.
3207 S->gidx == 0 means we are drawing the very first character of
3208 this composition. */
3210 /* Draw a rectangle for the composition if the font for the very
3211 first character of the composition could not be loaded. */
3212 if (s->font_not_found_p)
3214 if (s->gidx == 0)
3215 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3216 s->width - 1, s->height - 1);
3218 else
3220 for (i = 0; i < s->nchars; i++, ++s->gidx)
3221 XDrawString16 (s->display, s->window, s->gc,
3222 x + s->cmp->offsets[s->gidx * 2],
3223 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3224 s->char2b + i, 1);
3229 #ifdef USE_X_TOOLKIT
3231 static struct frame *x_frame_of_widget P_ ((Widget));
3232 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
3233 XrmValue *, XrmValue *, XtPointer *));
3234 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
3235 XrmValue *, Cardinal *));
3238 /* Return the frame on which widget WIDGET is used.. Abort if frame
3239 cannot be determined. */
3241 static struct frame *
3242 x_frame_of_widget (widget)
3243 Widget widget;
3245 struct x_display_info *dpyinfo;
3246 Lisp_Object tail;
3247 struct frame *f;
3249 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3251 /* Find the top-level shell of the widget. Note that this function
3252 can be called when the widget is not yet realized, so XtWindow
3253 (widget) == 0. That's the reason we can't simply use
3254 x_any_window_to_frame. */
3255 while (!XtIsTopLevelShell (widget))
3256 widget = XtParent (widget);
3258 /* Look for a frame with that top-level widget. Allocate the color
3259 on that frame to get the right gamma correction value. */
3260 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3261 if (GC_FRAMEP (XCAR (tail))
3262 && (f = XFRAME (XCAR (tail)),
3263 (f->output_data.nothing != 1
3264 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3265 && f->output_data.x->widget == widget)
3266 return f;
3268 abort ();
3272 /* Allocate the color COLOR->pixel on the screen and display of
3273 widget WIDGET in colormap CMAP. If an exact match cannot be
3274 allocated, try the nearest color available. Value is non-zero
3275 if successful. This is called from lwlib. */
3278 x_alloc_nearest_color_for_widget (widget, cmap, color)
3279 Widget widget;
3280 Colormap cmap;
3281 XColor *color;
3283 struct frame *f = x_frame_of_widget (widget);
3284 return x_alloc_nearest_color (f, cmap, color);
3288 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3289 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3290 If this produces the same color as PIXEL, try a color where all RGB
3291 values have DELTA added. Return the allocated color in *PIXEL.
3292 DISPLAY is the X display, CMAP is the colormap to operate on.
3293 Value is non-zero if successful. */
3296 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3297 Widget widget;
3298 Display *display;
3299 Colormap cmap;
3300 unsigned long *pixel;
3301 double factor;
3302 int delta;
3304 struct frame *f = x_frame_of_widget (widget);
3305 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3309 /* Structure specifying which arguments should be passed by Xt to
3310 cvt_string_to_pixel. We want the widget's screen and colormap. */
3312 static XtConvertArgRec cvt_string_to_pixel_args[] =
3314 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
3315 sizeof (Screen *)},
3316 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
3317 sizeof (Colormap)}
3321 /* The address of this variable is returned by
3322 cvt_string_to_pixel. */
3324 static Pixel cvt_string_to_pixel_value;
3327 /* Convert a color name to a pixel color.
3329 DPY is the display we are working on.
3331 ARGS is an array of *NARGS XrmValue structures holding additional
3332 information about the widget for which the conversion takes place.
3333 The contents of this array are determined by the specification
3334 in cvt_string_to_pixel_args.
3336 FROM is a pointer to an XrmValue which points to the color name to
3337 convert. TO is an XrmValue in which to return the pixel color.
3339 CLOSURE_RET is a pointer to user-data, in which we record if
3340 we allocated the color or not.
3342 Value is True if successful, False otherwise. */
3344 static Boolean
3345 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
3346 Display *dpy;
3347 XrmValue *args;
3348 Cardinal *nargs;
3349 XrmValue *from, *to;
3350 XtPointer *closure_ret;
3352 Screen *screen;
3353 Colormap cmap;
3354 Pixel pixel;
3355 String color_name;
3356 XColor color;
3358 if (*nargs != 2)
3360 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3361 "wrongParameters", "cvt_string_to_pixel",
3362 "XtToolkitError",
3363 "Screen and colormap args required", NULL, NULL);
3364 return False;
3367 screen = *(Screen **) args[0].addr;
3368 cmap = *(Colormap *) args[1].addr;
3369 color_name = (String) from->addr;
3371 if (strcmp (color_name, XtDefaultBackground) == 0)
3373 *closure_ret = (XtPointer) False;
3374 pixel = WhitePixelOfScreen (screen);
3376 else if (strcmp (color_name, XtDefaultForeground) == 0)
3378 *closure_ret = (XtPointer) False;
3379 pixel = BlackPixelOfScreen (screen);
3381 else if (XParseColor (dpy, cmap, color_name, &color)
3382 && x_alloc_nearest_color_1 (dpy, cmap, &color))
3384 pixel = color.pixel;
3385 *closure_ret = (XtPointer) True;
3387 else
3389 String params[1];
3390 Cardinal nparams = 1;
3392 params[0] = color_name;
3393 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3394 "badValue", "cvt_string_to_pixel",
3395 "XtToolkitError", "Invalid color `%s'",
3396 params, &nparams);
3397 return False;
3400 if (to->addr != NULL)
3402 if (to->size < sizeof (Pixel))
3404 to->size = sizeof (Pixel);
3405 return False;
3408 *(Pixel *) to->addr = pixel;
3410 else
3412 cvt_string_to_pixel_value = pixel;
3413 to->addr = (XtPointer) &cvt_string_to_pixel_value;
3416 to->size = sizeof (Pixel);
3417 return True;
3421 /* Free a pixel color which was previously allocated via
3422 cvt_string_to_pixel. This is registered as the destructor
3423 for this type of resource via XtSetTypeConverter.
3425 APP is the application context in which we work.
3427 TO is a pointer to an XrmValue holding the color to free.
3428 CLOSURE is the value we stored in CLOSURE_RET for this color
3429 in cvt_string_to_pixel.
3431 ARGS and NARGS are like for cvt_string_to_pixel. */
3433 static void
3434 cvt_pixel_dtor (app, to, closure, args, nargs)
3435 XtAppContext app;
3436 XrmValuePtr to;
3437 XtPointer closure;
3438 XrmValuePtr args;
3439 Cardinal *nargs;
3441 if (*nargs != 2)
3443 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
3444 "XtToolkitError",
3445 "Screen and colormap arguments required",
3446 NULL, NULL);
3448 else if (closure != NULL)
3450 /* We did allocate the pixel, so free it. */
3451 Screen *screen = *(Screen **) args[0].addr;
3452 Colormap cmap = *(Colormap *) args[1].addr;
3453 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
3454 (Pixel *) to->addr, 1);
3459 #endif /* USE_X_TOOLKIT */
3462 /* Value is an array of XColor structures for the contents of the
3463 color map of display DPY. Set *NCELLS to the size of the array.
3464 Note that this probably shouldn't be called for large color maps,
3465 say a 24-bit TrueColor map. */
3467 static const XColor *
3468 x_color_cells (dpy, ncells)
3469 Display *dpy;
3470 int *ncells;
3472 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3474 if (dpyinfo->color_cells == NULL)
3476 Screen *screen = dpyinfo->screen;
3477 int i;
3479 dpyinfo->ncolor_cells
3480 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
3481 dpyinfo->color_cells
3482 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3483 * sizeof *dpyinfo->color_cells);
3485 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3486 dpyinfo->color_cells[i].pixel = i;
3488 XQueryColors (dpy, dpyinfo->cmap,
3489 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3492 *ncells = dpyinfo->ncolor_cells;
3493 return dpyinfo->color_cells;
3497 /* On frame F, translate pixel colors to RGB values for the NCOLORS
3498 colors in COLORS. Use cached information, if available. */
3500 void
3501 x_query_colors (f, colors, ncolors)
3502 struct frame *f;
3503 XColor *colors;
3504 int ncolors;
3506 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3508 if (dpyinfo->color_cells)
3510 int i;
3511 for (i = 0; i < ncolors; ++i)
3513 unsigned long pixel = colors[i].pixel;
3514 xassert (pixel < dpyinfo->ncolor_cells);
3515 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3516 colors[i] = dpyinfo->color_cells[pixel];
3519 else
3520 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3524 /* On frame F, translate pixel color to RGB values for the color in
3525 COLOR. Use cached information, if available. */
3527 void
3528 x_query_color (f, color)
3529 struct frame *f;
3530 XColor *color;
3532 x_query_colors (f, color, 1);
3536 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
3537 exact match can't be allocated, try the nearest color available.
3538 Value is non-zero if successful. Set *COLOR to the color
3539 allocated. */
3541 static int
3542 x_alloc_nearest_color_1 (dpy, cmap, color)
3543 Display *dpy;
3544 Colormap cmap;
3545 XColor *color;
3547 int rc;
3549 rc = XAllocColor (dpy, cmap, color);
3550 if (rc == 0)
3552 /* If we got to this point, the colormap is full, so we're going
3553 to try to get the next closest color. The algorithm used is
3554 a least-squares matching, which is what X uses for closest
3555 color matching with StaticColor visuals. */
3556 int nearest, i;
3557 unsigned long nearest_delta = ~0;
3558 int ncells;
3559 const XColor *cells = x_color_cells (dpy, &ncells);
3561 for (nearest = i = 0; i < ncells; ++i)
3563 long dred = (color->red >> 8) - (cells[i].red >> 8);
3564 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3565 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3566 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3568 if (delta < nearest_delta)
3570 nearest = i;
3571 nearest_delta = delta;
3575 color->red = cells[nearest].red;
3576 color->green = cells[nearest].green;
3577 color->blue = cells[nearest].blue;
3578 rc = XAllocColor (dpy, cmap, color);
3580 else
3582 /* If allocation succeeded, and the allocated pixel color is not
3583 equal to a cached pixel color recorded earlier, there was a
3584 change in the colormap, so clear the color cache. */
3585 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3586 XColor *cached_color;
3588 if (dpyinfo->color_cells
3589 && (cached_color = &dpyinfo->color_cells[color->pixel],
3590 (cached_color->red != color->red
3591 || cached_color->blue != color->blue
3592 || cached_color->green != color->green)))
3594 xfree (dpyinfo->color_cells);
3595 dpyinfo->color_cells = NULL;
3596 dpyinfo->ncolor_cells = 0;
3600 #ifdef DEBUG_X_COLORS
3601 if (rc)
3602 register_color (color->pixel);
3603 #endif /* DEBUG_X_COLORS */
3605 return rc;
3609 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
3610 exact match can't be allocated, try the nearest color available.
3611 Value is non-zero if successful. Set *COLOR to the color
3612 allocated. */
3615 x_alloc_nearest_color (f, cmap, color)
3616 struct frame *f;
3617 Colormap cmap;
3618 XColor *color;
3620 gamma_correct (f, color);
3621 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
3625 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3626 It's necessary to do this instead of just using PIXEL directly to
3627 get color reference counts right. */
3629 unsigned long
3630 x_copy_color (f, pixel)
3631 struct frame *f;
3632 unsigned long pixel;
3634 XColor color;
3636 color.pixel = pixel;
3637 BLOCK_INPUT;
3638 x_query_color (f, &color);
3639 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3640 UNBLOCK_INPUT;
3641 #ifdef DEBUG_X_COLORS
3642 register_color (pixel);
3643 #endif
3644 return color.pixel;
3648 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3649 It's necessary to do this instead of just using PIXEL directly to
3650 get color reference counts right. */
3652 unsigned long
3653 x_copy_dpy_color (dpy, cmap, pixel)
3654 Display *dpy;
3655 Colormap cmap;
3656 unsigned long pixel;
3658 XColor color;
3660 color.pixel = pixel;
3661 BLOCK_INPUT;
3662 XQueryColor (dpy, cmap, &color);
3663 XAllocColor (dpy, cmap, &color);
3664 UNBLOCK_INPUT;
3665 #ifdef DEBUG_X_COLORS
3666 register_color (pixel);
3667 #endif
3668 return color.pixel;
3672 /* Brightness beyond which a color won't have its highlight brightness
3673 boosted.
3675 Nominally, highlight colors for `3d' faces are calculated by
3676 brightening an object's color by a constant scale factor, but this
3677 doesn't yield good results for dark colors, so for colors who's
3678 brightness is less than this value (on a scale of 0-65535) have an
3679 use an additional additive factor.
3681 The value here is set so that the default menu-bar/mode-line color
3682 (grey75) will not have its highlights changed at all. */
3683 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3686 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3687 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3688 If this produces the same color as PIXEL, try a color where all RGB
3689 values have DELTA added. Return the allocated color in *PIXEL.
3690 DISPLAY is the X display, CMAP is the colormap to operate on.
3691 Value is non-zero if successful. */
3693 static int
3694 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3695 struct frame *f;
3696 Display *display;
3697 Colormap cmap;
3698 unsigned long *pixel;
3699 double factor;
3700 int delta;
3702 XColor color, new;
3703 long bright;
3704 int success_p;
3706 /* Get RGB color values. */
3707 color.pixel = *pixel;
3708 x_query_color (f, &color);
3710 /* Change RGB values by specified FACTOR. Avoid overflow! */
3711 xassert (factor >= 0);
3712 new.red = min (0xffff, factor * color.red);
3713 new.green = min (0xffff, factor * color.green);
3714 new.blue = min (0xffff, factor * color.blue);
3716 /* Calculate brightness of COLOR. */
3717 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
3719 /* We only boost colors that are darker than
3720 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3721 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3722 /* Make an additive adjustment to NEW, because it's dark enough so
3723 that scaling by FACTOR alone isn't enough. */
3725 /* How far below the limit this color is (0 - 1, 1 being darker). */
3726 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3727 /* The additive adjustment. */
3728 int min_delta = delta * dimness * factor / 2;
3730 if (factor < 1)
3732 new.red = max (0, new.red - min_delta);
3733 new.green = max (0, new.green - min_delta);
3734 new.blue = max (0, new.blue - min_delta);
3736 else
3738 new.red = min (0xffff, min_delta + new.red);
3739 new.green = min (0xffff, min_delta + new.green);
3740 new.blue = min (0xffff, min_delta + new.blue);
3744 /* Try to allocate the color. */
3745 success_p = x_alloc_nearest_color (f, cmap, &new);
3746 if (success_p)
3748 if (new.pixel == *pixel)
3750 /* If we end up with the same color as before, try adding
3751 delta to the RGB values. */
3752 x_free_colors (f, &new.pixel, 1);
3754 new.red = min (0xffff, delta + color.red);
3755 new.green = min (0xffff, delta + color.green);
3756 new.blue = min (0xffff, delta + color.blue);
3757 success_p = x_alloc_nearest_color (f, cmap, &new);
3759 else
3760 success_p = 1;
3761 *pixel = new.pixel;
3764 return success_p;
3768 /* Set up the foreground color for drawing relief lines of glyph
3769 string S. RELIEF is a pointer to a struct relief containing the GC
3770 with which lines will be drawn. Use a color that is FACTOR or
3771 DELTA lighter or darker than the relief's background which is found
3772 in S->f->output_data.x->relief_background. If such a color cannot
3773 be allocated, use DEFAULT_PIXEL, instead. */
3775 static void
3776 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3777 struct frame *f;
3778 struct relief *relief;
3779 double factor;
3780 int delta;
3781 unsigned long default_pixel;
3783 XGCValues xgcv;
3784 struct x_output *di = f->output_data.x;
3785 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3786 unsigned long pixel;
3787 unsigned long background = di->relief_background;
3788 Colormap cmap = FRAME_X_COLORMAP (f);
3789 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3790 Display *dpy = FRAME_X_DISPLAY (f);
3792 xgcv.graphics_exposures = False;
3793 xgcv.line_width = 1;
3795 /* Free previously allocated color. The color cell will be reused
3796 when it has been freed as many times as it was allocated, so this
3797 doesn't affect faces using the same colors. */
3798 if (relief->gc
3799 && relief->allocated_p)
3801 x_free_colors (f, &relief->pixel, 1);
3802 relief->allocated_p = 0;
3805 /* Allocate new color. */
3806 xgcv.foreground = default_pixel;
3807 pixel = background;
3808 if (dpyinfo->n_planes != 1
3809 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3811 relief->allocated_p = 1;
3812 xgcv.foreground = relief->pixel = pixel;
3815 if (relief->gc == 0)
3817 xgcv.stipple = dpyinfo->gray;
3818 mask |= GCStipple;
3819 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3821 else
3822 XChangeGC (dpy, relief->gc, mask, &xgcv);
3826 /* Set up colors for the relief lines around glyph string S. */
3828 static void
3829 x_setup_relief_colors (s)
3830 struct glyph_string *s;
3832 struct x_output *di = s->f->output_data.x;
3833 unsigned long color;
3835 if (s->face->use_box_color_for_shadows_p)
3836 color = s->face->box_color;
3837 else if (s->first_glyph->type == IMAGE_GLYPH
3838 && s->img->pixmap
3839 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
3840 color = IMAGE_BACKGROUND (s->img, s->f, 0);
3841 else
3843 XGCValues xgcv;
3845 /* Get the background color of the face. */
3846 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3847 color = xgcv.background;
3850 if (di->white_relief.gc == 0
3851 || color != di->relief_background)
3853 di->relief_background = color;
3854 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3855 WHITE_PIX_DEFAULT (s->f));
3856 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3857 BLACK_PIX_DEFAULT (s->f));
3862 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3863 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3864 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3865 relief. LEFT_P non-zero means draw a relief on the left side of
3866 the rectangle. RIGHT_P non-zero means draw a relief on the right
3867 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3868 when drawing. */
3870 static void
3871 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3872 raised_p, left_p, right_p, clip_rect)
3873 struct frame *f;
3874 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3875 XRectangle *clip_rect;
3877 Display *dpy = FRAME_X_DISPLAY (f);
3878 Window window = FRAME_X_WINDOW (f);
3879 int i;
3880 GC gc;
3882 if (raised_p)
3883 gc = f->output_data.x->white_relief.gc;
3884 else
3885 gc = f->output_data.x->black_relief.gc;
3886 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
3888 /* Top. */
3889 for (i = 0; i < width; ++i)
3890 XDrawLine (dpy, window, gc,
3891 left_x + i * left_p, top_y + i,
3892 right_x + 1 - i * right_p, top_y + i);
3894 /* Left. */
3895 if (left_p)
3896 for (i = 0; i < width; ++i)
3897 XDrawLine (dpy, window, gc,
3898 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
3900 XSetClipMask (dpy, gc, None);
3901 if (raised_p)
3902 gc = f->output_data.x->black_relief.gc;
3903 else
3904 gc = f->output_data.x->white_relief.gc;
3905 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
3907 /* Bottom. */
3908 for (i = 0; i < width; ++i)
3909 XDrawLine (dpy, window, gc,
3910 left_x + i * left_p, bottom_y - i,
3911 right_x + 1 - i * right_p, bottom_y - i);
3913 /* Right. */
3914 if (right_p)
3915 for (i = 0; i < width; ++i)
3916 XDrawLine (dpy, window, gc,
3917 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3919 XSetClipMask (dpy, gc, None);
3923 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3924 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3925 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3926 left side of the rectangle. RIGHT_P non-zero means draw a line
3927 on the right side of the rectangle. CLIP_RECT is the clipping
3928 rectangle to use when drawing. */
3930 static void
3931 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3932 left_p, right_p, clip_rect)
3933 struct glyph_string *s;
3934 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3935 XRectangle *clip_rect;
3937 XGCValues xgcv;
3939 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3940 XSetForeground (s->display, s->gc, s->face->box_color);
3941 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3943 /* Top. */
3944 XFillRectangle (s->display, s->window, s->gc,
3945 left_x, top_y, right_x - left_x + 1, width);
3947 /* Left. */
3948 if (left_p)
3949 XFillRectangle (s->display, s->window, s->gc,
3950 left_x, top_y, width, bottom_y - top_y + 1);
3952 /* Bottom. */
3953 XFillRectangle (s->display, s->window, s->gc,
3954 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3956 /* Right. */
3957 if (right_p)
3958 XFillRectangle (s->display, s->window, s->gc,
3959 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3961 XSetForeground (s->display, s->gc, xgcv.foreground);
3962 XSetClipMask (s->display, s->gc, None);
3966 /* Draw a box around glyph string S. */
3968 static void
3969 x_draw_glyph_string_box (s)
3970 struct glyph_string *s;
3972 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3973 int left_p, right_p;
3974 struct glyph *last_glyph;
3975 XRectangle clip_rect;
3977 last_x = window_box_right (s->w, s->area);
3978 if (s->row->full_width_p
3979 && !s->w->pseudo_window_p)
3981 last_x += FRAME_X_RIGHT_FRINGE_WIDTH (s->f);
3982 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3983 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3986 /* The glyph that may have a right box line. */
3987 last_glyph = (s->cmp || s->img
3988 ? s->first_glyph
3989 : s->first_glyph + s->nchars - 1);
3991 width = abs (s->face->box_line_width);
3992 raised_p = s->face->box == FACE_RAISED_BOX;
3993 left_x = s->x;
3994 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
3995 ? last_x - 1
3996 : min (last_x, s->x + s->background_width) - 1);
3997 top_y = s->y;
3998 bottom_y = top_y + s->height - 1;
4000 left_p = (s->first_glyph->left_box_line_p
4001 || (s->hl == DRAW_MOUSE_FACE
4002 && (s->prev == NULL
4003 || s->prev->hl != s->hl)));
4004 right_p = (last_glyph->right_box_line_p
4005 || (s->hl == DRAW_MOUSE_FACE
4006 && (s->next == NULL
4007 || s->next->hl != s->hl)));
4009 x_get_glyph_string_clip_rect (s, &clip_rect);
4011 if (s->face->box == FACE_SIMPLE_BOX)
4012 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
4013 left_p, right_p, &clip_rect);
4014 else
4016 x_setup_relief_colors (s);
4017 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
4018 width, raised_p, left_p, right_p, &clip_rect);
4023 /* Draw foreground of image glyph string S. */
4025 static void
4026 x_draw_image_foreground (s)
4027 struct glyph_string *s;
4029 int x;
4030 int y = s->ybase - image_ascent (s->img, s->face);
4032 /* If first glyph of S has a left box line, start drawing it to the
4033 right of that line. */
4034 if (s->face->box != FACE_NO_BOX
4035 && s->first_glyph->left_box_line_p)
4036 x = s->x + abs (s->face->box_line_width);
4037 else
4038 x = s->x;
4040 /* If there is a margin around the image, adjust x- and y-position
4041 by that margin. */
4042 x += s->img->hmargin;
4043 y += s->img->vmargin;
4045 if (s->img->pixmap)
4047 if (s->img->mask)
4049 /* We can't set both a clip mask and use XSetClipRectangles
4050 because the latter also sets a clip mask. We also can't
4051 trust on the shape extension to be available
4052 (XShapeCombineRegion). So, compute the rectangle to draw
4053 manually. */
4054 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4055 | GCFunction);
4056 XGCValues xgcv;
4057 XRectangle clip_rect, image_rect, r;
4059 xgcv.clip_mask = s->img->mask;
4060 xgcv.clip_x_origin = x;
4061 xgcv.clip_y_origin = y;
4062 xgcv.function = GXcopy;
4063 XChangeGC (s->display, s->gc, mask, &xgcv);
4065 x_get_glyph_string_clip_rect (s, &clip_rect);
4066 image_rect.x = x;
4067 image_rect.y = y;
4068 image_rect.width = s->img->width;
4069 image_rect.height = s->img->height;
4070 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4071 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4072 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4074 else
4076 XRectangle clip_rect, image_rect, r;
4078 x_get_glyph_string_clip_rect (s, &clip_rect);
4079 image_rect.x = x;
4080 image_rect.y = y;
4081 image_rect.width = s->img->width;
4082 image_rect.height = s->img->height;
4083 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4084 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4085 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4087 /* When the image has a mask, we can expect that at
4088 least part of a mouse highlight or a block cursor will
4089 be visible. If the image doesn't have a mask, make
4090 a block cursor visible by drawing a rectangle around
4091 the image. I believe it's looking better if we do
4092 nothing here for mouse-face. */
4093 if (s->hl == DRAW_CURSOR)
4094 XDrawRectangle (s->display, s->window, s->gc, x, y,
4095 s->img->width - 1, s->img->height - 1);
4098 else
4099 /* Draw a rectangle if image could not be loaded. */
4100 XDrawRectangle (s->display, s->window, s->gc, x, y,
4101 s->img->width - 1, s->img->height - 1);
4105 /* Draw a relief around the image glyph string S. */
4107 static void
4108 x_draw_image_relief (s)
4109 struct glyph_string *s;
4111 int x0, y0, x1, y1, thick, raised_p;
4112 XRectangle r;
4113 int x;
4114 int y = s->ybase - image_ascent (s->img, s->face);
4116 /* If first glyph of S has a left box line, start drawing it to the
4117 right of that line. */
4118 if (s->face->box != FACE_NO_BOX
4119 && s->first_glyph->left_box_line_p)
4120 x = s->x + abs (s->face->box_line_width);
4121 else
4122 x = s->x;
4124 /* If there is a margin around the image, adjust x- and y-position
4125 by that margin. */
4126 x += s->img->hmargin;
4127 y += s->img->vmargin;
4129 if (s->hl == DRAW_IMAGE_SUNKEN
4130 || s->hl == DRAW_IMAGE_RAISED)
4132 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
4133 raised_p = s->hl == DRAW_IMAGE_RAISED;
4135 else
4137 thick = abs (s->img->relief);
4138 raised_p = s->img->relief > 0;
4141 x0 = x - thick;
4142 y0 = y - thick;
4143 x1 = x + s->img->width + thick - 1;
4144 y1 = y + s->img->height + thick - 1;
4146 x_setup_relief_colors (s);
4147 x_get_glyph_string_clip_rect (s, &r);
4148 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
4152 /* Draw the foreground of image glyph string S to PIXMAP. */
4154 static void
4155 x_draw_image_foreground_1 (s, pixmap)
4156 struct glyph_string *s;
4157 Pixmap pixmap;
4159 int x;
4160 int y = s->ybase - s->y - image_ascent (s->img, s->face);
4162 /* If first glyph of S has a left box line, start drawing it to the
4163 right of that line. */
4164 if (s->face->box != FACE_NO_BOX
4165 && s->first_glyph->left_box_line_p)
4166 x = abs (s->face->box_line_width);
4167 else
4168 x = 0;
4170 /* If there is a margin around the image, adjust x- and y-position
4171 by that margin. */
4172 x += s->img->hmargin;
4173 y += s->img->vmargin;
4175 if (s->img->pixmap)
4177 if (s->img->mask)
4179 /* We can't set both a clip mask and use XSetClipRectangles
4180 because the latter also sets a clip mask. We also can't
4181 trust on the shape extension to be available
4182 (XShapeCombineRegion). So, compute the rectangle to draw
4183 manually. */
4184 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4185 | GCFunction);
4186 XGCValues xgcv;
4188 xgcv.clip_mask = s->img->mask;
4189 xgcv.clip_x_origin = x;
4190 xgcv.clip_y_origin = y;
4191 xgcv.function = GXcopy;
4192 XChangeGC (s->display, s->gc, mask, &xgcv);
4194 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4195 0, 0, s->img->width, s->img->height, x, y);
4196 XSetClipMask (s->display, s->gc, None);
4198 else
4200 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4201 0, 0, s->img->width, s->img->height, x, y);
4203 /* When the image has a mask, we can expect that at
4204 least part of a mouse highlight or a block cursor will
4205 be visible. If the image doesn't have a mask, make
4206 a block cursor visible by drawing a rectangle around
4207 the image. I believe it's looking better if we do
4208 nothing here for mouse-face. */
4209 if (s->hl == DRAW_CURSOR)
4210 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4211 s->img->width - 1, s->img->height - 1);
4214 else
4215 /* Draw a rectangle if image could not be loaded. */
4216 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4217 s->img->width - 1, s->img->height - 1);
4221 /* Draw part of the background of glyph string S. X, Y, W, and H
4222 give the rectangle to draw. */
4224 static void
4225 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4226 struct glyph_string *s;
4227 int x, y, w, h;
4229 if (s->stippled_p)
4231 /* Fill background with a stipple pattern. */
4232 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4233 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4234 XSetFillStyle (s->display, s->gc, FillSolid);
4236 else
4237 x_clear_glyph_string_rect (s, x, y, w, h);
4241 /* Draw image glyph string S.
4243 s->y
4244 s->x +-------------------------
4245 | s->face->box
4247 | +-------------------------
4248 | | s->img->margin
4250 | | +-------------------
4251 | | | the image
4255 static void
4256 x_draw_image_glyph_string (s)
4257 struct glyph_string *s;
4259 int x, y;
4260 int box_line_hwidth = abs (s->face->box_line_width);
4261 int box_line_vwidth = max (s->face->box_line_width, 0);
4262 int height;
4263 Pixmap pixmap = None;
4265 height = s->height - 2 * box_line_vwidth;
4267 /* Fill background with face under the image. Do it only if row is
4268 taller than image or if image has a clip mask to reduce
4269 flickering. */
4270 s->stippled_p = s->face->stipple != 0;
4271 if (height > s->img->height
4272 || s->img->hmargin
4273 || s->img->vmargin
4274 || s->img->mask
4275 || s->img->pixmap == 0
4276 || s->width != s->background_width)
4278 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4279 x = s->x + box_line_hwidth;
4280 else
4281 x = s->x;
4283 y = s->y + box_line_vwidth;
4285 if (s->img->mask)
4287 /* Create a pixmap as large as the glyph string. Fill it
4288 with the background color. Copy the image to it, using
4289 its mask. Copy the temporary pixmap to the display. */
4290 Screen *screen = FRAME_X_SCREEN (s->f);
4291 int depth = DefaultDepthOfScreen (screen);
4293 /* Create a pixmap as large as the glyph string. */
4294 pixmap = XCreatePixmap (s->display, s->window,
4295 s->background_width,
4296 s->height, depth);
4298 /* Don't clip in the following because we're working on the
4299 pixmap. */
4300 XSetClipMask (s->display, s->gc, None);
4302 /* Fill the pixmap with the background color/stipple. */
4303 if (s->stippled_p)
4305 /* Fill background with a stipple pattern. */
4306 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4307 XFillRectangle (s->display, pixmap, s->gc,
4308 0, 0, s->background_width, s->height);
4309 XSetFillStyle (s->display, s->gc, FillSolid);
4311 else
4313 XGCValues xgcv;
4314 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4315 &xgcv);
4316 XSetForeground (s->display, s->gc, xgcv.background);
4317 XFillRectangle (s->display, pixmap, s->gc,
4318 0, 0, s->background_width, s->height);
4319 XSetForeground (s->display, s->gc, xgcv.foreground);
4322 else
4323 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4325 s->background_filled_p = 1;
4328 /* Draw the foreground. */
4329 if (pixmap != None)
4331 x_draw_image_foreground_1 (s, pixmap);
4332 x_set_glyph_string_clipping (s);
4333 XCopyArea (s->display, pixmap, s->window, s->gc,
4334 0, 0, s->background_width, s->height, s->x, s->y);
4335 XFreePixmap (s->display, pixmap);
4337 else
4338 x_draw_image_foreground (s);
4340 /* If we must draw a relief around the image, do it. */
4341 if (s->img->relief
4342 || s->hl == DRAW_IMAGE_RAISED
4343 || s->hl == DRAW_IMAGE_SUNKEN)
4344 x_draw_image_relief (s);
4348 /* Draw stretch glyph string S. */
4350 static void
4351 x_draw_stretch_glyph_string (s)
4352 struct glyph_string *s;
4354 xassert (s->first_glyph->type == STRETCH_GLYPH);
4355 s->stippled_p = s->face->stipple != 0;
4357 if (s->hl == DRAW_CURSOR
4358 && !x_stretch_cursor_p)
4360 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4361 as wide as the stretch glyph. */
4362 int width = min (CANON_X_UNIT (s->f), s->background_width);
4364 /* Draw cursor. */
4365 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4367 /* Clear rest using the GC of the original non-cursor face. */
4368 if (width < s->background_width)
4370 int x = s->x + width, y = s->y;
4371 int w = s->background_width - width, h = s->height;
4372 XRectangle r;
4373 GC gc;
4375 if (s->row->mouse_face_p
4376 && cursor_in_mouse_face_p (s->w))
4378 x_set_mouse_face_gc (s);
4379 gc = s->gc;
4381 else
4382 gc = s->face->gc;
4384 x_get_glyph_string_clip_rect (s, &r);
4385 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4387 if (s->face->stipple)
4389 /* Fill background with a stipple pattern. */
4390 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4391 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4392 XSetFillStyle (s->display, gc, FillSolid);
4394 else
4396 XGCValues xgcv;
4397 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4398 XSetForeground (s->display, gc, xgcv.background);
4399 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4400 XSetForeground (s->display, gc, xgcv.foreground);
4404 else if (!s->background_filled_p)
4405 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4406 s->height);
4408 s->background_filled_p = 1;
4412 /* Draw glyph string S. */
4414 static void
4415 x_draw_glyph_string (s)
4416 struct glyph_string *s;
4418 int relief_drawn_p = 0;
4420 /* If S draws into the background of its successor, draw the
4421 background of the successor first so that S can draw into it.
4422 This makes S->next use XDrawString instead of XDrawImageString. */
4423 if (s->next && s->right_overhang && !s->for_overlaps_p)
4425 xassert (s->next->img == NULL);
4426 x_set_glyph_string_gc (s->next);
4427 x_set_glyph_string_clipping (s->next);
4428 x_draw_glyph_string_background (s->next, 1);
4431 /* Set up S->gc, set clipping and draw S. */
4432 x_set_glyph_string_gc (s);
4434 /* Draw relief (if any) in advance for char/composition so that the
4435 glyph string can be drawn over it. */
4436 if (!s->for_overlaps_p
4437 && s->face->box != FACE_NO_BOX
4438 && (s->first_glyph->type == CHAR_GLYPH
4439 || s->first_glyph->type == COMPOSITE_GLYPH))
4442 x_set_glyph_string_clipping (s);
4443 x_draw_glyph_string_background (s, 1);
4444 x_draw_glyph_string_box (s);
4445 x_set_glyph_string_clipping (s);
4446 relief_drawn_p = 1;
4448 else
4449 x_set_glyph_string_clipping (s);
4451 switch (s->first_glyph->type)
4453 case IMAGE_GLYPH:
4454 x_draw_image_glyph_string (s);
4455 break;
4457 case STRETCH_GLYPH:
4458 x_draw_stretch_glyph_string (s);
4459 break;
4461 case CHAR_GLYPH:
4462 if (s->for_overlaps_p)
4463 s->background_filled_p = 1;
4464 else
4465 x_draw_glyph_string_background (s, 0);
4466 x_draw_glyph_string_foreground (s);
4467 break;
4469 case COMPOSITE_GLYPH:
4470 if (s->for_overlaps_p || s->gidx > 0)
4471 s->background_filled_p = 1;
4472 else
4473 x_draw_glyph_string_background (s, 1);
4474 x_draw_composite_glyph_string_foreground (s);
4475 break;
4477 default:
4478 abort ();
4481 if (!s->for_overlaps_p)
4483 /* Draw underline. */
4484 if (s->face->underline_p)
4486 unsigned long tem, h;
4487 int y;
4489 /* Get the underline thickness. Default is 1 pixel. */
4490 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4491 h = 1;
4493 /* Get the underline position. This is the recommended
4494 vertical offset in pixels from the baseline to the top of
4495 the underline. This is a signed value according to the
4496 specs, and its default is
4498 ROUND ((maximum descent) / 2), with
4499 ROUND(x) = floor (x + 0.5) */
4501 if (x_use_underline_position_properties
4502 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4503 y = s->ybase + (long) tem;
4504 else if (s->face->font)
4505 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4506 else
4507 y = s->y + s->height - h;
4509 if (s->face->underline_defaulted_p)
4510 XFillRectangle (s->display, s->window, s->gc,
4511 s->x, y, s->width, h);
4512 else
4514 XGCValues xgcv;
4515 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4516 XSetForeground (s->display, s->gc, s->face->underline_color);
4517 XFillRectangle (s->display, s->window, s->gc,
4518 s->x, y, s->width, h);
4519 XSetForeground (s->display, s->gc, xgcv.foreground);
4523 /* Draw overline. */
4524 if (s->face->overline_p)
4526 unsigned long dy = 0, h = 1;
4528 if (s->face->overline_color_defaulted_p)
4529 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4530 s->width, h);
4531 else
4533 XGCValues xgcv;
4534 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4535 XSetForeground (s->display, s->gc, s->face->overline_color);
4536 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4537 s->width, h);
4538 XSetForeground (s->display, s->gc, xgcv.foreground);
4542 /* Draw strike-through. */
4543 if (s->face->strike_through_p)
4545 unsigned long h = 1;
4546 unsigned long dy = (s->height - h) / 2;
4548 if (s->face->strike_through_color_defaulted_p)
4549 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4550 s->width, h);
4551 else
4553 XGCValues xgcv;
4554 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4555 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4556 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4557 s->width, h);
4558 XSetForeground (s->display, s->gc, xgcv.foreground);
4562 /* Draw relief if not yet drawn. */
4563 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
4564 x_draw_glyph_string_box (s);
4567 /* Reset clipping. */
4568 XSetClipMask (s->display, s->gc, None);
4572 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4573 struct face **, int));
4576 /* Fill glyph string S with composition components specified by S->cmp.
4578 FACES is an array of faces for all components of this composition.
4579 S->gidx is the index of the first component for S.
4580 OVERLAPS_P non-zero means S should draw the foreground only, and
4581 use its physical height for clipping.
4583 Value is the index of a component not in S. */
4585 static int
4586 x_fill_composite_glyph_string (s, faces, overlaps_p)
4587 struct glyph_string *s;
4588 struct face **faces;
4589 int overlaps_p;
4591 int i;
4593 xassert (s);
4595 s->for_overlaps_p = overlaps_p;
4597 s->face = faces[s->gidx];
4598 s->font = s->face->font;
4599 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4601 /* For all glyphs of this composition, starting at the offset
4602 S->gidx, until we reach the end of the definition or encounter a
4603 glyph that requires the different face, add it to S. */
4604 ++s->nchars;
4605 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4606 ++s->nchars;
4608 /* All glyph strings for the same composition has the same width,
4609 i.e. the width set for the first component of the composition. */
4611 s->width = s->first_glyph->pixel_width;
4613 /* If the specified font could not be loaded, use the frame's
4614 default font, but record the fact that we couldn't load it in
4615 the glyph string so that we can draw rectangles for the
4616 characters of the glyph string. */
4617 if (s->font == NULL)
4619 s->font_not_found_p = 1;
4620 s->font = FRAME_FONT (s->f);
4623 /* Adjust base line for subscript/superscript text. */
4624 s->ybase += s->first_glyph->voffset;
4626 xassert (s->face && s->face->gc);
4628 /* This glyph string must always be drawn with 16-bit functions. */
4629 s->two_byte_p = 1;
4631 return s->gidx + s->nchars;
4635 /* Fill glyph string S from a sequence of character glyphs.
4637 FACE_ID is the face id of the string. START is the index of the
4638 first glyph to consider, END is the index of the last + 1.
4639 OVERLAPS_P non-zero means S should draw the foreground only, and
4640 use its physical height for clipping.
4642 Value is the index of the first glyph not in S. */
4644 static int
4645 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4646 struct glyph_string *s;
4647 int face_id;
4648 int start, end, overlaps_p;
4650 struct glyph *glyph, *last;
4651 int voffset;
4652 int glyph_not_available_p;
4654 xassert (s->f == XFRAME (s->w->frame));
4655 xassert (s->nchars == 0);
4656 xassert (start >= 0 && end > start);
4658 s->for_overlaps_p = overlaps_p,
4659 glyph = s->row->glyphs[s->area] + start;
4660 last = s->row->glyphs[s->area] + end;
4661 voffset = glyph->voffset;
4663 glyph_not_available_p = glyph->glyph_not_available_p;
4665 while (glyph < last
4666 && glyph->type == CHAR_GLYPH
4667 && glyph->voffset == voffset
4668 /* Same face id implies same font, nowadays. */
4669 && glyph->face_id == face_id
4670 && glyph->glyph_not_available_p == glyph_not_available_p)
4672 int two_byte_p;
4674 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4675 s->char2b + s->nchars,
4676 &two_byte_p);
4677 s->two_byte_p = two_byte_p;
4678 ++s->nchars;
4679 xassert (s->nchars <= end - start);
4680 s->width += glyph->pixel_width;
4681 ++glyph;
4684 s->font = s->face->font;
4685 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4687 /* If the specified font could not be loaded, use the frame's font,
4688 but record the fact that we couldn't load it in
4689 S->font_not_found_p so that we can draw rectangles for the
4690 characters of the glyph string. */
4691 if (s->font == NULL || glyph_not_available_p)
4693 s->font_not_found_p = 1;
4694 s->font = FRAME_FONT (s->f);
4697 /* Adjust base line for subscript/superscript text. */
4698 s->ybase += voffset;
4700 xassert (s->face && s->face->gc);
4701 return glyph - s->row->glyphs[s->area];
4705 /* Fill glyph string S from image glyph S->first_glyph. */
4707 static void
4708 x_fill_image_glyph_string (s)
4709 struct glyph_string *s;
4711 xassert (s->first_glyph->type == IMAGE_GLYPH);
4712 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4713 xassert (s->img);
4714 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4715 s->font = s->face->font;
4716 s->width = s->first_glyph->pixel_width;
4718 /* Adjust base line for subscript/superscript text. */
4719 s->ybase += s->first_glyph->voffset;
4723 /* Fill glyph string S from a sequence of stretch glyphs.
4725 ROW is the glyph row in which the glyphs are found, AREA is the
4726 area within the row. START is the index of the first glyph to
4727 consider, END is the index of the last + 1.
4729 Value is the index of the first glyph not in S. */
4731 static int
4732 x_fill_stretch_glyph_string (s, row, area, start, end)
4733 struct glyph_string *s;
4734 struct glyph_row *row;
4735 enum glyph_row_area area;
4736 int start, end;
4738 struct glyph *glyph, *last;
4739 int voffset, face_id;
4741 xassert (s->first_glyph->type == STRETCH_GLYPH);
4743 glyph = s->row->glyphs[s->area] + start;
4744 last = s->row->glyphs[s->area] + end;
4745 face_id = glyph->face_id;
4746 s->face = FACE_FROM_ID (s->f, face_id);
4747 s->font = s->face->font;
4748 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4749 s->width = glyph->pixel_width;
4750 voffset = glyph->voffset;
4752 for (++glyph;
4753 (glyph < last
4754 && glyph->type == STRETCH_GLYPH
4755 && glyph->voffset == voffset
4756 && glyph->face_id == face_id);
4757 ++glyph)
4758 s->width += glyph->pixel_width;
4760 /* Adjust base line for subscript/superscript text. */
4761 s->ybase += voffset;
4763 /* The case that face->gc == 0 is handled when drawing the glyph
4764 string by calling PREPARE_FACE_FOR_DISPLAY. */
4765 xassert (s->face);
4766 return glyph - s->row->glyphs[s->area];
4770 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4771 of XChar2b structures for S; it can't be allocated in
4772 x_init_glyph_string because it must be allocated via `alloca'. W
4773 is the window on which S is drawn. ROW and AREA are the glyph row
4774 and area within the row from which S is constructed. START is the
4775 index of the first glyph structure covered by S. HL is a
4776 face-override for drawing S. */
4778 static void
4779 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4780 struct glyph_string *s;
4781 XChar2b *char2b;
4782 struct window *w;
4783 struct glyph_row *row;
4784 enum glyph_row_area area;
4785 int start;
4786 enum draw_glyphs_face hl;
4788 bzero (s, sizeof *s);
4789 s->w = w;
4790 s->f = XFRAME (w->frame);
4791 s->display = FRAME_X_DISPLAY (s->f);
4792 s->window = FRAME_X_WINDOW (s->f);
4793 s->char2b = char2b;
4794 s->hl = hl;
4795 s->row = row;
4796 s->area = area;
4797 s->first_glyph = row->glyphs[area] + start;
4798 s->height = row->height;
4799 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4801 /* Display the internal border below the tool-bar window. */
4802 if (s->w == XWINDOW (s->f->tool_bar_window))
4803 s->y -= s->f->output_data.x->internal_border_width;
4805 s->ybase = s->y + row->ascent;
4809 /* Set background width of glyph string S. START is the index of the
4810 first glyph following S. LAST_X is the right-most x-position + 1
4811 in the drawing area. */
4813 static INLINE void
4814 x_set_glyph_string_background_width (s, start, last_x)
4815 struct glyph_string *s;
4816 int start;
4817 int last_x;
4819 /* If the face of this glyph string has to be drawn to the end of
4820 the drawing area, set S->extends_to_end_of_line_p. */
4821 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4823 if (start == s->row->used[s->area]
4824 && s->area == TEXT_AREA
4825 && ((s->hl == DRAW_NORMAL_TEXT
4826 && (s->row->fill_line_p
4827 || s->face->background != default_face->background
4828 || s->face->stipple != default_face->stipple
4829 || s->row->mouse_face_p))
4830 || s->hl == DRAW_MOUSE_FACE
4831 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
4832 && s->row->fill_line_p)))
4833 s->extends_to_end_of_line_p = 1;
4835 /* If S extends its face to the end of the line, set its
4836 background_width to the distance to the right edge of the drawing
4837 area. */
4838 if (s->extends_to_end_of_line_p)
4839 s->background_width = last_x - s->x + 1;
4840 else
4841 s->background_width = s->width;
4845 /* Add a glyph string for a stretch glyph to the list of strings
4846 between HEAD and TAIL. START is the index of the stretch glyph in
4847 row area AREA of glyph row ROW. END is the index of the last glyph
4848 in that glyph row area. X is the current output position assigned
4849 to the new glyph string constructed. HL overrides that face of the
4850 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4851 is the right-most x-position of the drawing area. */
4853 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4854 and below -- keep them on one line. */
4855 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4856 do \
4858 s = (struct glyph_string *) alloca (sizeof *s); \
4859 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4860 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4861 x_append_glyph_string (&HEAD, &TAIL, s); \
4862 s->x = (X); \
4864 while (0)
4867 /* Add a glyph string for an image glyph to the list of strings
4868 between HEAD and TAIL. START is the index of the image glyph in
4869 row area AREA of glyph row ROW. END is the index of the last glyph
4870 in that glyph row area. X is the current output position assigned
4871 to the new glyph string constructed. HL overrides that face of the
4872 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4873 is the right-most x-position of the drawing area. */
4875 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4876 do \
4878 s = (struct glyph_string *) alloca (sizeof *s); \
4879 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4880 x_fill_image_glyph_string (s); \
4881 x_append_glyph_string (&HEAD, &TAIL, s); \
4882 ++START; \
4883 s->x = (X); \
4885 while (0)
4888 /* Add a glyph string for a sequence of character glyphs to the list
4889 of strings between HEAD and TAIL. START is the index of the first
4890 glyph in row area AREA of glyph row ROW that is part of the new
4891 glyph string. END is the index of the last glyph in that glyph row
4892 area. X is the current output position assigned to the new glyph
4893 string constructed. HL overrides that face of the glyph; e.g. it
4894 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4895 right-most x-position of the drawing area. */
4897 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4898 do \
4900 int c, face_id; \
4901 XChar2b *char2b; \
4903 c = (ROW)->glyphs[AREA][START].u.ch; \
4904 face_id = (ROW)->glyphs[AREA][START].face_id; \
4906 s = (struct glyph_string *) alloca (sizeof *s); \
4907 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4908 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4909 x_append_glyph_string (&HEAD, &TAIL, s); \
4910 s->x = (X); \
4911 START = x_fill_glyph_string (s, face_id, START, END, \
4912 OVERLAPS_P); \
4914 while (0)
4917 /* Add a glyph string for a composite sequence to the list of strings
4918 between HEAD and TAIL. START is the index of the first glyph in
4919 row area AREA of glyph row ROW that is part of the new glyph
4920 string. END is the index of the last glyph in that glyph row area.
4921 X is the current output position assigned to the new glyph string
4922 constructed. HL overrides that face of the glyph; e.g. it is
4923 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4924 x-position of the drawing area. */
4926 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4927 do { \
4928 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4929 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4930 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4931 struct composition *cmp = composition_table[cmp_id]; \
4932 int glyph_len = cmp->glyph_len; \
4933 XChar2b *char2b; \
4934 struct face **faces; \
4935 struct glyph_string *first_s = NULL; \
4936 int n; \
4938 base_face = base_face->ascii_face; \
4939 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4940 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4941 /* At first, fill in `char2b' and `faces'. */ \
4942 for (n = 0; n < glyph_len; n++) \
4944 int c = COMPOSITION_GLYPH (cmp, n); \
4945 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4946 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4947 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4948 this_face_id, char2b + n, 1); \
4951 /* Make glyph_strings for each glyph sequence that is drawable by \
4952 the same face, and append them to HEAD/TAIL. */ \
4953 for (n = 0; n < cmp->glyph_len;) \
4955 s = (struct glyph_string *) alloca (sizeof *s); \
4956 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4957 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4958 s->cmp = cmp; \
4959 s->gidx = n; \
4960 s->x = (X); \
4962 if (n == 0) \
4963 first_s = s; \
4965 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4968 ++START; \
4969 s = first_s; \
4970 } while (0)
4973 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4974 of AREA of glyph row ROW on window W between indices START and END.
4975 HL overrides the face for drawing glyph strings, e.g. it is
4976 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4977 x-positions of the drawing area.
4979 This is an ugly monster macro construct because we must use alloca
4980 to allocate glyph strings (because x_draw_glyphs can be called
4981 asynchronously). */
4983 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4984 do \
4986 HEAD = TAIL = NULL; \
4987 while (START < END) \
4989 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4990 switch (first_glyph->type) \
4992 case CHAR_GLYPH: \
4993 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
4994 TAIL, HL, X, LAST_X, \
4995 OVERLAPS_P); \
4996 break; \
4998 case COMPOSITE_GLYPH: \
4999 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
5000 HEAD, TAIL, HL, X, LAST_X,\
5001 OVERLAPS_P); \
5002 break; \
5004 case STRETCH_GLYPH: \
5005 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
5006 HEAD, TAIL, HL, X, LAST_X); \
5007 break; \
5009 case IMAGE_GLYPH: \
5010 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
5011 TAIL, HL, X, LAST_X); \
5012 break; \
5014 default: \
5015 abort (); \
5018 x_set_glyph_string_background_width (s, START, LAST_X); \
5019 (X) += s->width; \
5022 while (0)
5025 /* Draw glyphs between START and END in AREA of ROW on window W,
5026 starting at x-position X. X is relative to AREA in W. HL is a
5027 face-override with the following meaning:
5029 DRAW_NORMAL_TEXT draw normally
5030 DRAW_CURSOR draw in cursor face
5031 DRAW_MOUSE_FACE draw in mouse face.
5032 DRAW_INVERSE_VIDEO draw in mode line face
5033 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
5034 DRAW_IMAGE_RAISED draw an image with a raised relief around it
5036 If OVERLAPS_P is non-zero, draw only the foreground of characters
5037 and clip to the physical height of ROW.
5039 Value is the x-position reached, relative to AREA of W. */
5041 static int
5042 x_draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
5043 struct window *w;
5044 int x;
5045 struct glyph_row *row;
5046 enum glyph_row_area area;
5047 int start, end;
5048 enum draw_glyphs_face hl;
5049 int overlaps_p;
5051 struct glyph_string *head, *tail;
5052 struct glyph_string *s;
5053 int last_x, area_width;
5054 int x_reached;
5055 int i, j;
5057 /* Let's rather be paranoid than getting a SEGV. */
5058 end = min (end, row->used[area]);
5059 start = max (0, start);
5060 start = min (end, start);
5062 /* Translate X to frame coordinates. Set last_x to the right
5063 end of the drawing area. */
5064 if (row->full_width_p)
5066 /* X is relative to the left edge of W, without scroll bars
5067 or fringes. */
5068 struct frame *f = XFRAME (w->frame);
5069 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
5071 x += window_left_x;
5072 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
5073 last_x = window_left_x + area_width;
5075 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5077 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5078 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
5079 last_x += width;
5080 else
5081 x -= width;
5084 x += FRAME_INTERNAL_BORDER_WIDTH (f);
5085 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
5087 else
5089 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
5090 area_width = window_box_width (w, area);
5091 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
5094 /* Build a doubly-linked list of glyph_string structures between
5095 head and tail from what we have to draw. Note that the macro
5096 BUILD_GLYPH_STRINGS will modify its start parameter. That's
5097 the reason we use a separate variable `i'. */
5098 i = start;
5099 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
5100 overlaps_p);
5101 if (tail)
5102 x_reached = tail->x + tail->background_width;
5103 else
5104 x_reached = x;
5106 /* If there are any glyphs with lbearing < 0 or rbearing > width in
5107 the row, redraw some glyphs in front or following the glyph
5108 strings built above. */
5109 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
5111 int dummy_x = 0;
5112 struct glyph_string *h, *t;
5114 /* Compute overhangs for all glyph strings. */
5115 for (s = head; s; s = s->next)
5116 x_compute_glyph_string_overhangs (s);
5118 /* Prepend glyph strings for glyphs in front of the first glyph
5119 string that are overwritten because of the first glyph
5120 string's left overhang. The background of all strings
5121 prepended must be drawn because the first glyph string
5122 draws over it. */
5123 i = x_left_overwritten (head);
5124 if (i >= 0)
5126 j = i;
5127 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
5128 DRAW_NORMAL_TEXT, dummy_x, last_x,
5129 overlaps_p);
5130 start = i;
5131 x_compute_overhangs_and_x (t, head->x, 1);
5132 x_prepend_glyph_string_lists (&head, &tail, h, t);
5135 /* Prepend glyph strings for glyphs in front of the first glyph
5136 string that overwrite that glyph string because of their
5137 right overhang. For these strings, only the foreground must
5138 be drawn, because it draws over the glyph string at `head'.
5139 The background must not be drawn because this would overwrite
5140 right overhangs of preceding glyphs for which no glyph
5141 strings exist. */
5142 i = x_left_overwriting (head);
5143 if (i >= 0)
5145 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
5146 DRAW_NORMAL_TEXT, dummy_x, last_x,
5147 overlaps_p);
5148 for (s = h; s; s = s->next)
5149 s->background_filled_p = 1;
5150 x_compute_overhangs_and_x (t, head->x, 1);
5151 x_prepend_glyph_string_lists (&head, &tail, h, t);
5154 /* Append glyphs strings for glyphs following the last glyph
5155 string tail that are overwritten by tail. The background of
5156 these strings has to be drawn because tail's foreground draws
5157 over it. */
5158 i = x_right_overwritten (tail);
5159 if (i >= 0)
5161 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5162 DRAW_NORMAL_TEXT, x, last_x,
5163 overlaps_p);
5164 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5165 x_append_glyph_string_lists (&head, &tail, h, t);
5168 /* Append glyph strings for glyphs following the last glyph
5169 string tail that overwrite tail. The foreground of such
5170 glyphs has to be drawn because it writes into the background
5171 of tail. The background must not be drawn because it could
5172 paint over the foreground of following glyphs. */
5173 i = x_right_overwriting (tail);
5174 if (i >= 0)
5176 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5177 DRAW_NORMAL_TEXT, x, last_x,
5178 overlaps_p);
5179 for (s = h; s; s = s->next)
5180 s->background_filled_p = 1;
5181 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5182 x_append_glyph_string_lists (&head, &tail, h, t);
5186 /* Draw all strings. */
5187 for (s = head; s; s = s->next)
5188 x_draw_glyph_string (s);
5190 if (area == TEXT_AREA
5191 && !row->full_width_p
5192 /* When drawing overlapping rows, only the glyph strings'
5193 foreground is drawn, which doesn't erase a cursor
5194 completely. */
5195 && !overlaps_p)
5197 int x0 = head ? head->x : x;
5198 int x1 = tail ? tail->x + tail->background_width : x;
5200 x0 = FRAME_TO_WINDOW_PIXEL_X (w, x0);
5201 x1 = FRAME_TO_WINDOW_PIXEL_X (w, x1);
5203 if (XFASTINT (w->left_margin_width) != 0)
5205 int left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
5206 x0 -= left_area_width;
5207 x1 -= left_area_width;
5210 notice_overwritten_cursor (w, area, x0, x1,
5211 row->y, MATRIX_ROW_BOTTOM_Y (row));
5214 /* Value is the x-position up to which drawn, relative to AREA of W.
5215 This doesn't include parts drawn because of overhangs. */
5216 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5217 if (!row->full_width_p)
5219 if (area > LEFT_MARGIN_AREA && XFASTINT (w->left_margin_width) != 0)
5220 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5221 if (area > TEXT_AREA)
5222 x_reached -= window_box_width (w, TEXT_AREA);
5225 return x_reached;
5229 /* Fix the display of area AREA of overlapping row ROW in window W. */
5231 static void
5232 x_fix_overlapping_area (w, row, area)
5233 struct window *w;
5234 struct glyph_row *row;
5235 enum glyph_row_area area;
5237 int i, x;
5239 BLOCK_INPUT;
5241 if (area == LEFT_MARGIN_AREA)
5242 x = 0;
5243 else if (area == TEXT_AREA)
5244 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5245 else
5246 x = (window_box_width (w, LEFT_MARGIN_AREA)
5247 + window_box_width (w, TEXT_AREA));
5249 for (i = 0; i < row->used[area];)
5251 if (row->glyphs[area][i].overlaps_vertically_p)
5253 int start = i, start_x = x;
5257 x += row->glyphs[area][i].pixel_width;
5258 ++i;
5260 while (i < row->used[area]
5261 && row->glyphs[area][i].overlaps_vertically_p);
5263 x_draw_glyphs (w, start_x, row, area, start, i,
5264 DRAW_NORMAL_TEXT, 1);
5266 else
5268 x += row->glyphs[area][i].pixel_width;
5269 ++i;
5273 UNBLOCK_INPUT;
5277 /* Output LEN glyphs starting at START at the nominal cursor position.
5278 Advance the nominal cursor over the text. The global variable
5279 updated_window contains the window being updated, updated_row is
5280 the glyph row being updated, and updated_area is the area of that
5281 row being updated. */
5283 static void
5284 x_write_glyphs (start, len)
5285 struct glyph *start;
5286 int len;
5288 int x, hpos;
5290 xassert (updated_window && updated_row);
5291 BLOCK_INPUT;
5293 /* Write glyphs. */
5295 hpos = start - updated_row->glyphs[updated_area];
5296 x = x_draw_glyphs (updated_window, output_cursor.x,
5297 updated_row, updated_area,
5298 hpos, hpos + len,
5299 DRAW_NORMAL_TEXT, 0);
5301 UNBLOCK_INPUT;
5303 /* Advance the output cursor. */
5304 output_cursor.hpos += len;
5305 output_cursor.x = x;
5309 /* Insert LEN glyphs from START at the nominal cursor position. */
5311 static void
5312 x_insert_glyphs (start, len)
5313 struct glyph *start;
5314 register int len;
5316 struct frame *f;
5317 struct window *w;
5318 int line_height, shift_by_width, shifted_region_width;
5319 struct glyph_row *row;
5320 struct glyph *glyph;
5321 int frame_x, frame_y, hpos;
5323 xassert (updated_window && updated_row);
5324 BLOCK_INPUT;
5325 w = updated_window;
5326 f = XFRAME (WINDOW_FRAME (w));
5328 /* Get the height of the line we are in. */
5329 row = updated_row;
5330 line_height = row->height;
5332 /* Get the width of the glyphs to insert. */
5333 shift_by_width = 0;
5334 for (glyph = start; glyph < start + len; ++glyph)
5335 shift_by_width += glyph->pixel_width;
5337 /* Get the width of the region to shift right. */
5338 shifted_region_width = (window_box_width (w, updated_area)
5339 - output_cursor.x
5340 - shift_by_width);
5342 /* Shift right. */
5343 frame_x = window_box_left (w, updated_area) + output_cursor.x;
5344 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5345 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5346 f->output_data.x->normal_gc,
5347 frame_x, frame_y,
5348 shifted_region_width, line_height,
5349 frame_x + shift_by_width, frame_y);
5351 /* Write the glyphs. */
5352 hpos = start - row->glyphs[updated_area];
5353 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5354 DRAW_NORMAL_TEXT, 0);
5356 /* Advance the output cursor. */
5357 output_cursor.hpos += len;
5358 output_cursor.x += shift_by_width;
5359 UNBLOCK_INPUT;
5363 /* Delete N glyphs at the nominal cursor position. Not implemented
5364 for X frames. */
5366 static void
5367 x_delete_glyphs (n)
5368 register int n;
5370 abort ();
5374 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5375 If they are <= 0, this is probably an error. */
5377 void
5378 x_clear_area (dpy, window, x, y, width, height, exposures)
5379 Display *dpy;
5380 Window window;
5381 int x, y;
5382 int width, height;
5383 int exposures;
5385 xassert (width > 0 && height > 0);
5386 XClearArea (dpy, window, x, y, width, height, exposures);
5390 /* Erase the current text line from the nominal cursor position
5391 (inclusive) to pixel column TO_X (exclusive). The idea is that
5392 everything from TO_X onward is already erased.
5394 TO_X is a pixel position relative to updated_area of
5395 updated_window. TO_X == -1 means clear to the end of this area. */
5397 static void
5398 x_clear_end_of_line (to_x)
5399 int to_x;
5401 struct frame *f;
5402 struct window *w = updated_window;
5403 int max_x, min_y, max_y;
5404 int from_x, from_y, to_y;
5406 xassert (updated_window && updated_row);
5407 f = XFRAME (w->frame);
5409 if (updated_row->full_width_p)
5411 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5412 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5413 && !w->pseudo_window_p)
5414 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5416 else
5417 max_x = window_box_width (w, updated_area);
5418 max_y = window_text_bottom_y (w);
5420 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5421 of window. For TO_X > 0, truncate to end of drawing area. */
5422 if (to_x == 0)
5423 return;
5424 else if (to_x < 0)
5425 to_x = max_x;
5426 else
5427 to_x = min (to_x, max_x);
5429 to_y = min (max_y, output_cursor.y + updated_row->height);
5431 /* Notice if the cursor will be cleared by this operation. */
5432 if (!updated_row->full_width_p)
5433 notice_overwritten_cursor (w, updated_area,
5434 output_cursor.x, -1,
5435 updated_row->y,
5436 MATRIX_ROW_BOTTOM_Y (updated_row));
5438 from_x = output_cursor.x;
5440 /* Translate to frame coordinates. */
5441 if (updated_row->full_width_p)
5443 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5444 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5446 else
5448 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5449 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5452 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5453 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5454 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5456 /* Prevent inadvertently clearing to end of the X window. */
5457 if (to_x > from_x && to_y > from_y)
5459 BLOCK_INPUT;
5460 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5461 from_x, from_y, to_x - from_x, to_y - from_y,
5462 False);
5463 UNBLOCK_INPUT;
5468 /* Clear entire frame. If updating_frame is non-null, clear that
5469 frame. Otherwise clear the selected frame. */
5471 static void
5472 x_clear_frame ()
5474 struct frame *f;
5476 if (updating_frame)
5477 f = updating_frame;
5478 else
5479 f = SELECTED_FRAME ();
5481 /* Clearing the frame will erase any cursor, so mark them all as no
5482 longer visible. */
5483 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5484 output_cursor.hpos = output_cursor.vpos = 0;
5485 output_cursor.x = -1;
5487 /* We don't set the output cursor here because there will always
5488 follow an explicit cursor_to. */
5489 BLOCK_INPUT;
5490 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5492 /* We have to clear the scroll bars, too. If we have changed
5493 colors or something like that, then they should be notified. */
5494 x_scroll_bar_clear (f);
5496 XFlush (FRAME_X_DISPLAY (f));
5497 UNBLOCK_INPUT;
5502 /* Invert the middle quarter of the frame for .15 sec. */
5504 /* We use the select system call to do the waiting, so we have to make
5505 sure it's available. If it isn't, we just won't do visual bells. */
5507 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5510 /* Subtract the `struct timeval' values X and Y, storing the result in
5511 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5513 static int
5514 timeval_subtract (result, x, y)
5515 struct timeval *result, x, y;
5517 /* Perform the carry for the later subtraction by updating y. This
5518 is safer because on some systems the tv_sec member is unsigned. */
5519 if (x.tv_usec < y.tv_usec)
5521 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5522 y.tv_usec -= 1000000 * nsec;
5523 y.tv_sec += nsec;
5526 if (x.tv_usec - y.tv_usec > 1000000)
5528 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5529 y.tv_usec += 1000000 * nsec;
5530 y.tv_sec -= nsec;
5533 /* Compute the time remaining to wait. tv_usec is certainly
5534 positive. */
5535 result->tv_sec = x.tv_sec - y.tv_sec;
5536 result->tv_usec = x.tv_usec - y.tv_usec;
5538 /* Return indication of whether the result should be considered
5539 negative. */
5540 return x.tv_sec < y.tv_sec;
5543 void
5544 XTflash (f)
5545 struct frame *f;
5547 BLOCK_INPUT;
5550 GC gc;
5552 /* Create a GC that will use the GXxor function to flip foreground
5553 pixels into background pixels. */
5555 XGCValues values;
5557 values.function = GXxor;
5558 values.foreground = (f->output_data.x->foreground_pixel
5559 ^ f->output_data.x->background_pixel);
5561 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5562 GCFunction | GCForeground, &values);
5566 /* Get the height not including a menu bar widget. */
5567 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5568 /* Height of each line to flash. */
5569 int flash_height = FRAME_LINE_HEIGHT (f);
5570 /* These will be the left and right margins of the rectangles. */
5571 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5572 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5574 int width;
5576 /* Don't flash the area between a scroll bar and the frame
5577 edge it is next to. */
5578 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5580 case vertical_scroll_bar_left:
5581 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5582 break;
5584 case vertical_scroll_bar_right:
5585 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5586 break;
5588 default:
5589 break;
5592 width = flash_right - flash_left;
5594 /* If window is tall, flash top and bottom line. */
5595 if (height > 3 * FRAME_LINE_HEIGHT (f))
5597 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5598 flash_left,
5599 (FRAME_INTERNAL_BORDER_WIDTH (f)
5600 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5601 width, flash_height);
5602 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5603 flash_left,
5604 (height - flash_height
5605 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5606 width, flash_height);
5608 else
5609 /* If it is short, flash it all. */
5610 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5611 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5612 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5614 x_flush (f);
5617 struct timeval wakeup;
5619 EMACS_GET_TIME (wakeup);
5621 /* Compute time to wait until, propagating carry from usecs. */
5622 wakeup.tv_usec += 150000;
5623 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5624 wakeup.tv_usec %= 1000000;
5626 /* Keep waiting until past the time wakeup or any input gets
5627 available. */
5628 while (! detect_input_pending ())
5630 struct timeval current;
5631 struct timeval timeout;
5633 EMACS_GET_TIME (current);
5635 /* Break if result would be negative. */
5636 if (timeval_subtract (&current, wakeup, current))
5637 break;
5639 /* How long `select' should wait. */
5640 timeout.tv_sec = 0;
5641 timeout.tv_usec = 10000;
5643 /* Try to wait that long--but we might wake up sooner. */
5644 select (0, NULL, NULL, NULL, &timeout);
5648 /* If window is tall, flash top and bottom line. */
5649 if (height > 3 * FRAME_LINE_HEIGHT (f))
5651 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5652 flash_left,
5653 (FRAME_INTERNAL_BORDER_WIDTH (f)
5654 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5655 width, flash_height);
5656 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5657 flash_left,
5658 (height - flash_height
5659 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5660 width, flash_height);
5662 else
5663 /* If it is short, flash it all. */
5664 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5665 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5666 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5668 XFreeGC (FRAME_X_DISPLAY (f), gc);
5669 x_flush (f);
5673 UNBLOCK_INPUT;
5676 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5679 /* Make audible bell. */
5681 void
5682 XTring_bell ()
5684 struct frame *f = SELECTED_FRAME ();
5686 if (FRAME_X_DISPLAY (f))
5688 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5689 if (visible_bell)
5690 XTflash (f);
5691 else
5692 #endif
5694 BLOCK_INPUT;
5695 XBell (FRAME_X_DISPLAY (f), 0);
5696 XFlush (FRAME_X_DISPLAY (f));
5697 UNBLOCK_INPUT;
5703 /* Specify how many text lines, from the top of the window,
5704 should be affected by insert-lines and delete-lines operations.
5705 This, and those operations, are used only within an update
5706 that is bounded by calls to x_update_begin and x_update_end. */
5708 static void
5709 XTset_terminal_window (n)
5710 register int n;
5712 /* This function intentionally left blank. */
5717 /***********************************************************************
5718 Line Dance
5719 ***********************************************************************/
5721 /* Perform an insert-lines or delete-lines operation, inserting N
5722 lines or deleting -N lines at vertical position VPOS. */
5724 static void
5725 x_ins_del_lines (vpos, n)
5726 int vpos, n;
5728 abort ();
5732 /* Scroll part of the display as described by RUN. */
5734 static void
5735 x_scroll_run (w, run)
5736 struct window *w;
5737 struct run *run;
5739 struct frame *f = XFRAME (w->frame);
5740 int x, y, width, height, from_y, to_y, bottom_y;
5742 /* Get frame-relative bounding box of the text display area of W,
5743 without mode lines. Include in this box the left and right
5744 fringe of W. */
5745 window_box (w, -1, &x, &y, &width, &height);
5746 width += FRAME_X_FRINGE_WIDTH (f);
5747 x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
5749 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5750 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5751 bottom_y = y + height;
5753 if (to_y < from_y)
5755 /* Scrolling up. Make sure we don't copy part of the mode
5756 line at the bottom. */
5757 if (from_y + run->height > bottom_y)
5758 height = bottom_y - from_y;
5759 else
5760 height = run->height;
5762 else
5764 /* Scolling down. Make sure we don't copy over the mode line.
5765 at the bottom. */
5766 if (to_y + run->height > bottom_y)
5767 height = bottom_y - to_y;
5768 else
5769 height = run->height;
5772 BLOCK_INPUT;
5774 /* Cursor off. Will be switched on again in x_update_window_end. */
5775 updated_window = w;
5776 x_clear_cursor (w);
5778 XCopyArea (FRAME_X_DISPLAY (f),
5779 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5780 f->output_data.x->normal_gc,
5781 x, from_y,
5782 width, height,
5783 x, to_y);
5785 UNBLOCK_INPUT;
5790 /***********************************************************************
5791 Exposure Events
5792 ***********************************************************************/
5794 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5795 corner of the exposed rectangle. W and H are width and height of
5796 the exposed area. All are pixel values. W or H zero means redraw
5797 the entire frame. */
5799 static void
5800 expose_frame (f, x, y, w, h)
5801 struct frame *f;
5802 int x, y, w, h;
5804 XRectangle r;
5805 int mouse_face_overwritten_p = 0;
5807 TRACE ((stderr, "expose_frame "));
5809 /* No need to redraw if frame will be redrawn soon. */
5810 if (FRAME_GARBAGED_P (f))
5812 TRACE ((stderr, " garbaged\n"));
5813 return;
5816 /* If basic faces haven't been realized yet, there is no point in
5817 trying to redraw anything. This can happen when we get an expose
5818 event while Emacs is starting, e.g. by moving another window. */
5819 if (FRAME_FACE_CACHE (f) == NULL
5820 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5822 TRACE ((stderr, " no faces\n"));
5823 return;
5826 if (w == 0 || h == 0)
5828 r.x = r.y = 0;
5829 r.width = CANON_X_UNIT (f) * f->width;
5830 r.height = CANON_Y_UNIT (f) * f->height;
5832 else
5834 r.x = x;
5835 r.y = y;
5836 r.width = w;
5837 r.height = h;
5840 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5841 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
5843 if (WINDOWP (f->tool_bar_window))
5844 mouse_face_overwritten_p
5845 |= expose_window (XWINDOW (f->tool_bar_window), &r);
5847 #ifndef USE_X_TOOLKIT
5848 if (WINDOWP (f->menu_bar_window))
5849 mouse_face_overwritten_p
5850 |= expose_window (XWINDOW (f->menu_bar_window), &r);
5851 #endif /* not USE_X_TOOLKIT */
5853 /* Some window managers support a focus-follows-mouse style with
5854 delayed raising of frames. Imagine a partially obscured frame,
5855 and moving the mouse into partially obscured mouse-face on that
5856 frame. The visible part of the mouse-face will be highlighted,
5857 then the WM raises the obscured frame. With at least one WM, KDE
5858 2.1, Emacs is not getting any event for the raising of the frame
5859 (even tried with SubstructureRedirectMask), only Expose events.
5860 These expose events will draw text normally, i.e. not
5861 highlighted. Which means we must redo the highlight here.
5862 Subsume it under ``we love X''. --gerd 2001-08-15 */
5863 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
5865 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5866 if (f == dpyinfo->mouse_face_mouse_frame)
5868 int x = dpyinfo->mouse_face_mouse_x;
5869 int y = dpyinfo->mouse_face_mouse_y;
5870 clear_mouse_face (dpyinfo);
5871 note_mouse_highlight (f, x, y);
5877 /* Redraw (parts) of all windows in the window tree rooted at W that
5878 intersect R. R contains frame pixel coordinates. Value is
5879 non-zero if the exposure overwrites mouse-face. */
5881 static int
5882 expose_window_tree (w, r)
5883 struct window *w;
5884 XRectangle *r;
5886 struct frame *f = XFRAME (w->frame);
5887 int mouse_face_overwritten_p = 0;
5889 while (w && !FRAME_GARBAGED_P (f))
5891 if (!NILP (w->hchild))
5892 mouse_face_overwritten_p
5893 |= expose_window_tree (XWINDOW (w->hchild), r);
5894 else if (!NILP (w->vchild))
5895 mouse_face_overwritten_p
5896 |= expose_window_tree (XWINDOW (w->vchild), r);
5897 else
5898 mouse_face_overwritten_p |= expose_window (w, r);
5900 w = NILP (w->next) ? NULL : XWINDOW (w->next);
5903 return mouse_face_overwritten_p;
5907 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5908 which intersects rectangle R. R is in window-relative coordinates. */
5910 static void
5911 expose_area (w, row, r, area)
5912 struct window *w;
5913 struct glyph_row *row;
5914 XRectangle *r;
5915 enum glyph_row_area area;
5917 struct glyph *first = row->glyphs[area];
5918 struct glyph *end = row->glyphs[area] + row->used[area];
5919 struct glyph *last;
5920 int first_x, start_x, x;
5922 if (area == TEXT_AREA && row->fill_line_p)
5923 /* If row extends face to end of line write the whole line. */
5924 x_draw_glyphs (w, 0, row, area, 0, row->used[area],
5925 DRAW_NORMAL_TEXT, 0);
5926 else
5928 /* Set START_X to the window-relative start position for drawing glyphs of
5929 AREA. The first glyph of the text area can be partially visible.
5930 The first glyphs of other areas cannot. */
5931 if (area == LEFT_MARGIN_AREA)
5932 start_x = 0;
5933 else if (area == TEXT_AREA)
5934 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5935 else
5936 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5937 + window_box_width (w, TEXT_AREA));
5938 x = start_x;
5940 /* Find the first glyph that must be redrawn. */
5941 while (first < end
5942 && x + first->pixel_width < r->x)
5944 x += first->pixel_width;
5945 ++first;
5948 /* Find the last one. */
5949 last = first;
5950 first_x = x;
5951 while (last < end
5952 && x < r->x + r->width)
5954 x += last->pixel_width;
5955 ++last;
5958 /* Repaint. */
5959 if (last > first)
5960 x_draw_glyphs (w, first_x - start_x, row, area,
5961 first - row->glyphs[area],
5962 last - row->glyphs[area],
5963 DRAW_NORMAL_TEXT, 0);
5968 /* Redraw the parts of the glyph row ROW on window W intersecting
5969 rectangle R. R is in window-relative coordinates. Value is
5970 non-zero if mouse-face was overwritten. */
5972 static int
5973 expose_line (w, row, r)
5974 struct window *w;
5975 struct glyph_row *row;
5976 XRectangle *r;
5978 xassert (row->enabled_p);
5980 if (row->mode_line_p || w->pseudo_window_p)
5981 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5982 DRAW_NORMAL_TEXT, 0);
5983 else
5985 if (row->used[LEFT_MARGIN_AREA])
5986 expose_area (w, row, r, LEFT_MARGIN_AREA);
5987 if (row->used[TEXT_AREA])
5988 expose_area (w, row, r, TEXT_AREA);
5989 if (row->used[RIGHT_MARGIN_AREA])
5990 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5991 x_draw_row_fringe_bitmaps (w, row);
5994 return row->mouse_face_p;
5998 /* Return non-zero if W's cursor intersects rectangle R. */
6000 static int
6001 x_phys_cursor_in_rect_p (w, r)
6002 struct window *w;
6003 XRectangle *r;
6005 XRectangle cr, result;
6006 struct glyph *cursor_glyph;
6008 cursor_glyph = get_phys_cursor_glyph (w);
6009 if (cursor_glyph)
6011 cr.x = w->phys_cursor.x;
6012 cr.y = w->phys_cursor.y;
6013 cr.width = cursor_glyph->pixel_width;
6014 cr.height = w->phys_cursor_height;
6015 return x_intersect_rectangles (&cr, r, &result);
6017 else
6018 return 0;
6022 /* Redraw the part of window W intersection rectangle FR. Pixel
6023 coordinates in FR are frame-relative. Call this function with
6024 input blocked. Value is non-zero if the exposure overwrites
6025 mouse-face. */
6027 static int
6028 expose_window (w, fr)
6029 struct window *w;
6030 XRectangle *fr;
6032 struct frame *f = XFRAME (w->frame);
6033 XRectangle wr, r;
6034 int mouse_face_overwritten_p = 0;
6036 /* If window is not yet fully initialized, do nothing. This can
6037 happen when toolkit scroll bars are used and a window is split.
6038 Reconfiguring the scroll bar will generate an expose for a newly
6039 created window. */
6040 if (w->current_matrix == NULL)
6041 return 0;
6043 /* When we're currently updating the window, display and current
6044 matrix usually don't agree. Arrange for a thorough display
6045 later. */
6046 if (w == updated_window)
6048 SET_FRAME_GARBAGED (f);
6049 return 0;
6052 /* Frame-relative pixel rectangle of W. */
6053 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
6054 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
6055 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
6056 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
6058 if (x_intersect_rectangles (fr, &wr, &r))
6060 int yb = window_text_bottom_y (w);
6061 struct glyph_row *row;
6062 int cursor_cleared_p;
6064 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
6065 r.x, r.y, r.width, r.height));
6067 /* Convert to window coordinates. */
6068 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
6069 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
6071 /* Turn off the cursor. */
6072 if (!w->pseudo_window_p
6073 && x_phys_cursor_in_rect_p (w, &r))
6075 x_clear_cursor (w);
6076 cursor_cleared_p = 1;
6078 else
6079 cursor_cleared_p = 0;
6081 /* Find the first row intersecting the rectangle R. */
6082 for (row = w->current_matrix->rows;
6083 row->enabled_p;
6084 ++row)
6086 int y0 = row->y;
6087 int y1 = MATRIX_ROW_BOTTOM_Y (row);
6089 if ((y0 >= r.y && y0 < r.y + r.height)
6090 || (y1 > r.y && y1 < r.y + r.height)
6091 || (r.y >= y0 && r.y < y1)
6092 || (r.y + r.height > y0 && r.y + r.height < y1))
6094 if (expose_line (w, row, &r))
6095 mouse_face_overwritten_p = 1;
6098 if (y1 >= yb)
6099 break;
6102 /* Display the mode line if there is one. */
6103 if (WINDOW_WANTS_MODELINE_P (w)
6104 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
6105 row->enabled_p)
6106 && row->y < r.y + r.height)
6108 if (expose_line (w, row, &r))
6109 mouse_face_overwritten_p = 1;
6112 if (!w->pseudo_window_p)
6114 /* Draw border between windows. */
6115 x_draw_vertical_border (w);
6117 /* Turn the cursor on again. */
6118 if (cursor_cleared_p)
6119 x_update_window_cursor (w, 1);
6123 return mouse_face_overwritten_p;
6127 /* Determine the intersection of two rectangles R1 and R2. Return
6128 the intersection in *RESULT. Value is non-zero if RESULT is not
6129 empty. */
6131 static int
6132 x_intersect_rectangles (r1, r2, result)
6133 XRectangle *r1, *r2, *result;
6135 XRectangle *left, *right;
6136 XRectangle *upper, *lower;
6137 int intersection_p = 0;
6139 /* Rearrange so that R1 is the left-most rectangle. */
6140 if (r1->x < r2->x)
6141 left = r1, right = r2;
6142 else
6143 left = r2, right = r1;
6145 /* X0 of the intersection is right.x0, if this is inside R1,
6146 otherwise there is no intersection. */
6147 if (right->x <= left->x + left->width)
6149 result->x = right->x;
6151 /* The right end of the intersection is the minimum of the
6152 the right ends of left and right. */
6153 result->width = (min (left->x + left->width, right->x + right->width)
6154 - result->x);
6156 /* Same game for Y. */
6157 if (r1->y < r2->y)
6158 upper = r1, lower = r2;
6159 else
6160 upper = r2, lower = r1;
6162 /* The upper end of the intersection is lower.y0, if this is inside
6163 of upper. Otherwise, there is no intersection. */
6164 if (lower->y <= upper->y + upper->height)
6166 result->y = lower->y;
6168 /* The lower end of the intersection is the minimum of the lower
6169 ends of upper and lower. */
6170 result->height = (min (lower->y + lower->height,
6171 upper->y + upper->height)
6172 - result->y);
6173 intersection_p = 1;
6177 return intersection_p;
6184 static void
6185 frame_highlight (f)
6186 struct frame *f;
6188 /* We used to only do this if Vx_no_window_manager was non-nil, but
6189 the ICCCM (section 4.1.6) says that the window's border pixmap
6190 and border pixel are window attributes which are "private to the
6191 client", so we can always change it to whatever we want. */
6192 BLOCK_INPUT;
6193 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6194 f->output_data.x->border_pixel);
6195 UNBLOCK_INPUT;
6196 x_update_cursor (f, 1);
6199 static void
6200 frame_unhighlight (f)
6201 struct frame *f;
6203 /* We used to only do this if Vx_no_window_manager was non-nil, but
6204 the ICCCM (section 4.1.6) says that the window's border pixmap
6205 and border pixel are window attributes which are "private to the
6206 client", so we can always change it to whatever we want. */
6207 BLOCK_INPUT;
6208 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6209 f->output_data.x->border_tile);
6210 UNBLOCK_INPUT;
6211 x_update_cursor (f, 1);
6214 /* The focus has changed. Update the frames as necessary to reflect
6215 the new situation. Note that we can't change the selected frame
6216 here, because the Lisp code we are interrupting might become confused.
6217 Each event gets marked with the frame in which it occurred, so the
6218 Lisp code can tell when the switch took place by examining the events. */
6220 static void
6221 x_new_focus_frame (dpyinfo, frame)
6222 struct x_display_info *dpyinfo;
6223 struct frame *frame;
6225 struct frame *old_focus = dpyinfo->x_focus_frame;
6227 if (frame != dpyinfo->x_focus_frame)
6229 /* Set this before calling other routines, so that they see
6230 the correct value of x_focus_frame. */
6231 dpyinfo->x_focus_frame = frame;
6233 if (old_focus && old_focus->auto_lower)
6234 x_lower_frame (old_focus);
6236 #if 0
6237 selected_frame = frame;
6238 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6239 selected_frame);
6240 Fselect_window (selected_frame->selected_window);
6241 choose_minibuf_frame ();
6242 #endif /* ! 0 */
6244 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6245 pending_autoraise_frame = dpyinfo->x_focus_frame;
6246 else
6247 pending_autoraise_frame = 0;
6250 x_frame_rehighlight (dpyinfo);
6253 /* Handle an event saying the mouse has moved out of an Emacs frame. */
6255 void
6256 x_mouse_leave (dpyinfo)
6257 struct x_display_info *dpyinfo;
6259 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
6262 /* The focus has changed, or we have redirected a frame's focus to
6263 another frame (this happens when a frame uses a surrogate
6264 mini-buffer frame). Shift the highlight as appropriate.
6266 The FRAME argument doesn't necessarily have anything to do with which
6267 frame is being highlighted or un-highlighted; we only use it to find
6268 the appropriate X display info. */
6270 static void
6271 XTframe_rehighlight (frame)
6272 struct frame *frame;
6274 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6277 static void
6278 x_frame_rehighlight (dpyinfo)
6279 struct x_display_info *dpyinfo;
6281 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6283 if (dpyinfo->x_focus_frame)
6285 dpyinfo->x_highlight_frame
6286 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6287 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6288 : dpyinfo->x_focus_frame);
6289 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
6291 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6292 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
6295 else
6296 dpyinfo->x_highlight_frame = 0;
6298 if (dpyinfo->x_highlight_frame != old_highlight)
6300 if (old_highlight)
6301 frame_unhighlight (old_highlight);
6302 if (dpyinfo->x_highlight_frame)
6303 frame_highlight (dpyinfo->x_highlight_frame);
6309 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
6311 /* Initialize mode_switch_bit and modifier_meaning. */
6312 static void
6313 x_find_modifier_meanings (dpyinfo)
6314 struct x_display_info *dpyinfo;
6316 int min_code, max_code;
6317 KeySym *syms;
6318 int syms_per_code;
6319 XModifierKeymap *mods;
6321 dpyinfo->meta_mod_mask = 0;
6322 dpyinfo->shift_lock_mask = 0;
6323 dpyinfo->alt_mod_mask = 0;
6324 dpyinfo->super_mod_mask = 0;
6325 dpyinfo->hyper_mod_mask = 0;
6327 #ifdef HAVE_X11R4
6328 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
6329 #else
6330 min_code = dpyinfo->display->min_keycode;
6331 max_code = dpyinfo->display->max_keycode;
6332 #endif
6334 syms = XGetKeyboardMapping (dpyinfo->display,
6335 min_code, max_code - min_code + 1,
6336 &syms_per_code);
6337 mods = XGetModifierMapping (dpyinfo->display);
6339 /* Scan the modifier table to see which modifier bits the Meta and
6340 Alt keysyms are on. */
6342 int row, col; /* The row and column in the modifier table. */
6344 for (row = 3; row < 8; row++)
6345 for (col = 0; col < mods->max_keypermod; col++)
6347 KeyCode code
6348 = mods->modifiermap[(row * mods->max_keypermod) + col];
6350 /* Zeroes are used for filler. Skip them. */
6351 if (code == 0)
6352 continue;
6354 /* Are any of this keycode's keysyms a meta key? */
6356 int code_col;
6358 for (code_col = 0; code_col < syms_per_code; code_col++)
6360 int sym = syms[((code - min_code) * syms_per_code) + code_col];
6362 switch (sym)
6364 case XK_Meta_L:
6365 case XK_Meta_R:
6366 dpyinfo->meta_mod_mask |= (1 << row);
6367 break;
6369 case XK_Alt_L:
6370 case XK_Alt_R:
6371 dpyinfo->alt_mod_mask |= (1 << row);
6372 break;
6374 case XK_Hyper_L:
6375 case XK_Hyper_R:
6376 dpyinfo->hyper_mod_mask |= (1 << row);
6377 break;
6379 case XK_Super_L:
6380 case XK_Super_R:
6381 dpyinfo->super_mod_mask |= (1 << row);
6382 break;
6384 case XK_Shift_Lock:
6385 /* Ignore this if it's not on the lock modifier. */
6386 if ((1 << row) == LockMask)
6387 dpyinfo->shift_lock_mask = LockMask;
6388 break;
6395 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
6396 if (! dpyinfo->meta_mod_mask)
6398 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6399 dpyinfo->alt_mod_mask = 0;
6402 /* If some keys are both alt and meta,
6403 make them just meta, not alt. */
6404 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6406 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6409 XFree ((char *) syms);
6410 XFreeModifiermap (mods);
6413 /* Convert between the modifier bits X uses and the modifier bits
6414 Emacs uses. */
6416 static unsigned int
6417 x_x_to_emacs_modifiers (dpyinfo, state)
6418 struct x_display_info *dpyinfo;
6419 unsigned int state;
6421 EMACS_UINT mod_meta = meta_modifier;
6422 EMACS_UINT mod_alt = alt_modifier;
6423 EMACS_UINT mod_hyper = hyper_modifier;
6424 EMACS_UINT mod_super = super_modifier;
6425 Lisp_Object tem;
6427 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6428 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6429 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6430 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6431 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6432 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6433 tem = Fget (Vx_super_keysym, Qmodifier_value);
6434 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
6437 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6438 | ((state & ControlMask) ? ctrl_modifier : 0)
6439 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
6440 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
6441 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
6442 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
6445 static unsigned int
6446 x_emacs_to_x_modifiers (dpyinfo, state)
6447 struct x_display_info *dpyinfo;
6448 unsigned int state;
6450 EMACS_UINT mod_meta = meta_modifier;
6451 EMACS_UINT mod_alt = alt_modifier;
6452 EMACS_UINT mod_hyper = hyper_modifier;
6453 EMACS_UINT mod_super = super_modifier;
6455 Lisp_Object tem;
6457 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6458 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6459 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6460 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6461 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6462 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6463 tem = Fget (Vx_super_keysym, Qmodifier_value);
6464 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
6467 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
6468 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
6469 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
6470 | ((state & shift_modifier) ? ShiftMask : 0)
6471 | ((state & ctrl_modifier) ? ControlMask : 0)
6472 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
6475 /* Convert a keysym to its name. */
6477 char *
6478 x_get_keysym_name (keysym)
6479 KeySym keysym;
6481 char *value;
6483 BLOCK_INPUT;
6484 value = XKeysymToString (keysym);
6485 UNBLOCK_INPUT;
6487 return value;
6492 /* Mouse clicks and mouse movement. Rah. */
6494 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6495 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6496 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6497 not force the value into range. */
6499 void
6500 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6501 FRAME_PTR f;
6502 register int pix_x, pix_y;
6503 register int *x, *y;
6504 XRectangle *bounds;
6505 int noclip;
6507 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6508 even for negative values. */
6509 if (pix_x < 0)
6510 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6511 if (pix_y < 0)
6512 pix_y -= (f)->output_data.x->line_height - 1;
6514 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6515 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6517 if (bounds)
6519 bounds->width = FONT_WIDTH (f->output_data.x->font);
6520 bounds->height = f->output_data.x->line_height;
6521 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6522 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6525 if (!noclip)
6527 if (pix_x < 0)
6528 pix_x = 0;
6529 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6530 pix_x = FRAME_WINDOW_WIDTH (f);
6532 if (pix_y < 0)
6533 pix_y = 0;
6534 else if (pix_y > f->height)
6535 pix_y = f->height;
6538 *x = pix_x;
6539 *y = pix_y;
6543 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6544 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6545 can't tell the positions because W's display is not up to date,
6546 return 0. */
6549 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6550 struct window *w;
6551 int hpos, vpos;
6552 int *frame_x, *frame_y;
6554 int success_p;
6556 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6557 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6559 if (display_completed)
6561 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6562 struct glyph *glyph = row->glyphs[TEXT_AREA];
6563 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6565 *frame_y = row->y;
6566 *frame_x = row->x;
6567 while (glyph < end)
6569 *frame_x += glyph->pixel_width;
6570 ++glyph;
6573 success_p = 1;
6575 else
6577 *frame_y = *frame_x = 0;
6578 success_p = 0;
6581 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6582 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6583 return success_p;
6587 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6589 If the event is a button press, then note that we have grabbed
6590 the mouse. */
6592 static Lisp_Object
6593 construct_mouse_click (result, event, f)
6594 struct input_event *result;
6595 XButtonEvent *event;
6596 struct frame *f;
6598 /* Make the event type no_event; we'll change that when we decide
6599 otherwise. */
6600 result->kind = mouse_click;
6601 result->code = event->button - Button1;
6602 result->timestamp = event->time;
6603 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6604 event->state)
6605 | (event->type == ButtonRelease
6606 ? up_modifier
6607 : down_modifier));
6609 XSETINT (result->x, event->x);
6610 XSETINT (result->y, event->y);
6611 XSETFRAME (result->frame_or_window, f);
6612 result->arg = Qnil;
6613 return Qnil;
6617 /* Function to report a mouse movement to the mainstream Emacs code.
6618 The input handler calls this.
6620 We have received a mouse movement event, which is given in *event.
6621 If the mouse is over a different glyph than it was last time, tell
6622 the mainstream emacs code by setting mouse_moved. If not, ask for
6623 another motion event, so we can check again the next time it moves. */
6625 static XMotionEvent last_mouse_motion_event;
6626 static Lisp_Object last_mouse_motion_frame;
6628 static void
6629 note_mouse_movement (frame, event)
6630 FRAME_PTR frame;
6631 XMotionEvent *event;
6633 last_mouse_movement_time = event->time;
6634 last_mouse_motion_event = *event;
6635 XSETFRAME (last_mouse_motion_frame, frame);
6637 if (x_autoselect_window_p)
6639 int area;
6640 Lisp_Object window;
6641 static Lisp_Object last_window;
6643 window = window_from_coordinates (frame, event->x, event->y, &area, 0);
6645 /* Window will be selected only when it is not selected now and
6646 last mouse movement event was not in it. Minubuffer window
6647 will be selected iff it is active. */
6648 if (!EQ (window, last_window)
6649 && !EQ (window, selected_window)
6650 && (!MINI_WINDOW_P (XWINDOW (window))
6651 || (EQ (window, minibuf_window) && minibuf_level > 0)))
6652 Fselect_window (window);
6654 last_window=window;
6657 if (event->window != FRAME_X_WINDOW (frame))
6659 frame->mouse_moved = 1;
6660 last_mouse_scroll_bar = Qnil;
6661 note_mouse_highlight (frame, -1, -1);
6664 /* Has the mouse moved off the glyph it was on at the last sighting? */
6665 else if (event->x < last_mouse_glyph.x
6666 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6667 || event->y < last_mouse_glyph.y
6668 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6670 frame->mouse_moved = 1;
6671 last_mouse_scroll_bar = Qnil;
6672 note_mouse_highlight (frame, event->x, event->y);
6677 /************************************************************************
6678 Mouse Face
6679 ************************************************************************/
6681 /* Find the glyph under window-relative coordinates X/Y in window W.
6682 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6683 strings. Return in *HPOS and *VPOS the row and column number of
6684 the glyph found. Return in *AREA the glyph area containing X.
6685 Value is a pointer to the glyph found or null if X/Y is not on
6686 text, or we can't tell because W's current matrix is not up to
6687 date. */
6689 static struct glyph *
6690 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
6691 struct window *w;
6692 int x, y;
6693 int *hpos, *vpos, *area;
6694 int buffer_only_p;
6696 struct glyph *glyph, *end;
6697 struct glyph_row *row = NULL;
6698 int x0, i, left_area_width;
6700 /* Find row containing Y. Give up if some row is not enabled. */
6701 for (i = 0; i < w->current_matrix->nrows; ++i)
6703 row = MATRIX_ROW (w->current_matrix, i);
6704 if (!row->enabled_p)
6705 return NULL;
6706 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6707 break;
6710 *vpos = i;
6711 *hpos = 0;
6713 /* Give up if Y is not in the window. */
6714 if (i == w->current_matrix->nrows)
6715 return NULL;
6717 /* Get the glyph area containing X. */
6718 if (w->pseudo_window_p)
6720 *area = TEXT_AREA;
6721 x0 = 0;
6723 else
6725 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6726 if (x < left_area_width)
6728 *area = LEFT_MARGIN_AREA;
6729 x0 = 0;
6731 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6733 *area = TEXT_AREA;
6734 x0 = row->x + left_area_width;
6736 else
6738 *area = RIGHT_MARGIN_AREA;
6739 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6743 /* Find glyph containing X. */
6744 glyph = row->glyphs[*area];
6745 end = glyph + row->used[*area];
6746 while (glyph < end)
6748 if (x < x0 + glyph->pixel_width)
6750 if (w->pseudo_window_p)
6751 break;
6752 else if (!buffer_only_p || BUFFERP (glyph->object))
6753 break;
6756 x0 += glyph->pixel_width;
6757 ++glyph;
6760 if (glyph == end)
6761 return NULL;
6763 *hpos = glyph - row->glyphs[*area];
6764 return glyph;
6768 /* Convert frame-relative x/y to coordinates relative to window W.
6769 Takes pseudo-windows into account. */
6771 static void
6772 frame_to_window_pixel_xy (w, x, y)
6773 struct window *w;
6774 int *x, *y;
6776 if (w->pseudo_window_p)
6778 /* A pseudo-window is always full-width, and starts at the
6779 left edge of the frame, plus a frame border. */
6780 struct frame *f = XFRAME (w->frame);
6781 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6782 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6784 else
6786 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6787 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6792 /* Take proper action when mouse has moved to the mode or header line of
6793 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6794 mode line. X is relative to the start of the text display area of
6795 W, so the width of fringes and scroll bars must be subtracted
6796 to get a position relative to the start of the mode line. */
6798 static void
6799 note_mode_line_highlight (w, x, mode_line_p)
6800 struct window *w;
6801 int x, mode_line_p;
6803 struct frame *f = XFRAME (w->frame);
6804 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6805 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6806 struct glyph_row *row;
6808 if (mode_line_p)
6809 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6810 else
6811 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6813 if (row->enabled_p)
6815 struct glyph *glyph, *end;
6816 Lisp_Object help, map;
6817 int x0;
6819 /* Find the glyph under X. */
6820 glyph = row->glyphs[TEXT_AREA];
6821 end = glyph + row->used[TEXT_AREA];
6822 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6823 + FRAME_X_LEFT_FRINGE_WIDTH (f));
6825 while (glyph < end
6826 && x >= x0 + glyph->pixel_width)
6828 x0 += glyph->pixel_width;
6829 ++glyph;
6832 if (glyph < end
6833 && STRINGP (glyph->object)
6834 && XSTRING (glyph->object)->intervals
6835 && glyph->charpos >= 0
6836 && glyph->charpos < XSTRING (glyph->object)->size)
6838 /* If we're on a string with `help-echo' text property,
6839 arrange for the help to be displayed. This is done by
6840 setting the global variable help_echo to the help string. */
6841 help = Fget_text_property (make_number (glyph->charpos),
6842 Qhelp_echo, glyph->object);
6843 if (!NILP (help))
6845 help_echo = help;
6846 XSETWINDOW (help_echo_window, w);
6847 help_echo_object = glyph->object;
6848 help_echo_pos = glyph->charpos;
6851 /* Change the mouse pointer according to what is under X/Y. */
6852 map = Fget_text_property (make_number (glyph->charpos),
6853 Qlocal_map, glyph->object);
6854 if (KEYMAPP (map))
6855 cursor = f->output_data.x->nontext_cursor;
6856 else
6858 map = Fget_text_property (make_number (glyph->charpos),
6859 Qkeymap, glyph->object);
6860 if (KEYMAPP (map))
6861 cursor = f->output_data.x->nontext_cursor;
6866 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6870 /* Take proper action when the mouse has moved to position X, Y on
6871 frame F as regards highlighting characters that have mouse-face
6872 properties. Also de-highlighting chars where the mouse was before.
6873 X and Y can be negative or out of range. */
6875 static void
6876 note_mouse_highlight (f, x, y)
6877 struct frame *f;
6878 int x, y;
6880 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6881 int portion;
6882 Lisp_Object window;
6883 struct window *w;
6884 Cursor cursor = None;
6885 struct buffer *b;
6887 /* When a menu is active, don't highlight because this looks odd. */
6888 #ifdef USE_X_TOOLKIT
6889 if (popup_activated ())
6890 return;
6891 #endif
6893 if (NILP (Vmouse_highlight)
6894 || !f->glyphs_initialized_p)
6895 return;
6897 dpyinfo->mouse_face_mouse_x = x;
6898 dpyinfo->mouse_face_mouse_y = y;
6899 dpyinfo->mouse_face_mouse_frame = f;
6901 if (dpyinfo->mouse_face_defer)
6902 return;
6904 if (gc_in_progress)
6906 dpyinfo->mouse_face_deferred_gc = 1;
6907 return;
6910 /* Which window is that in? */
6911 window = window_from_coordinates (f, x, y, &portion, 1);
6913 /* If we were displaying active text in another window, clear that. */
6914 if (! EQ (window, dpyinfo->mouse_face_window))
6915 clear_mouse_face (dpyinfo);
6917 /* Not on a window -> return. */
6918 if (!WINDOWP (window))
6919 return;
6921 /* Convert to window-relative pixel coordinates. */
6922 w = XWINDOW (window);
6923 frame_to_window_pixel_xy (w, &x, &y);
6925 /* Handle tool-bar window differently since it doesn't display a
6926 buffer. */
6927 if (EQ (window, f->tool_bar_window))
6929 note_tool_bar_highlight (f, x, y);
6930 return;
6933 /* Mouse is on the mode or header line? */
6934 if (portion == 1 || portion == 3)
6936 note_mode_line_highlight (w, x, portion == 1);
6937 return;
6940 if (portion == 2)
6941 cursor = f->output_data.x->horizontal_drag_cursor;
6942 else
6943 cursor = f->output_data.x->text_cursor;
6945 /* Are we in a window whose display is up to date?
6946 And verify the buffer's text has not changed. */
6947 b = XBUFFER (w->buffer);
6948 if (/* Within text portion of the window. */
6949 portion == 0
6950 && EQ (w->window_end_valid, w->buffer)
6951 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
6952 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
6954 int hpos, vpos, pos, i, area;
6955 struct glyph *glyph;
6956 Lisp_Object object;
6957 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
6958 Lisp_Object *overlay_vec = NULL;
6959 int len, noverlays;
6960 struct buffer *obuf;
6961 int obegv, ozv, same_region;
6963 /* Find the glyph under X/Y. */
6964 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
6966 /* Clear mouse face if X/Y not over text. */
6967 if (glyph == NULL
6968 || area != TEXT_AREA
6969 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6971 if (clear_mouse_face (dpyinfo))
6972 cursor = None;
6973 goto set_cursor;
6976 pos = glyph->charpos;
6977 object = glyph->object;
6978 if (!STRINGP (object) && !BUFFERP (object))
6979 goto set_cursor;
6981 /* If we get an out-of-range value, return now; avoid an error. */
6982 if (BUFFERP (object) && pos > BUF_Z (b))
6983 goto set_cursor;
6985 /* Make the window's buffer temporarily current for
6986 overlays_at and compute_char_face. */
6987 obuf = current_buffer;
6988 current_buffer = b;
6989 obegv = BEGV;
6990 ozv = ZV;
6991 BEGV = BEG;
6992 ZV = Z;
6994 /* Is this char mouse-active or does it have help-echo? */
6995 position = make_number (pos);
6997 if (BUFFERP (object))
6999 /* Put all the overlays we want in a vector in overlay_vec.
7000 Store the length in len. If there are more than 10, make
7001 enough space for all, and try again. */
7002 len = 10;
7003 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7004 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
7005 if (noverlays > len)
7007 len = noverlays;
7008 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7009 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
7012 /* Sort overlays into increasing priority order. */
7013 noverlays = sort_overlays (overlay_vec, noverlays, w);
7015 else
7016 noverlays = 0;
7018 same_region = (EQ (window, dpyinfo->mouse_face_window)
7019 && vpos >= dpyinfo->mouse_face_beg_row
7020 && vpos <= dpyinfo->mouse_face_end_row
7021 && (vpos > dpyinfo->mouse_face_beg_row
7022 || hpos >= dpyinfo->mouse_face_beg_col)
7023 && (vpos < dpyinfo->mouse_face_end_row
7024 || hpos < dpyinfo->mouse_face_end_col
7025 || dpyinfo->mouse_face_past_end));
7027 if (same_region)
7028 cursor = None;
7030 /* Check mouse-face highlighting. */
7031 if (! same_region
7032 /* If there exists an overlay with mouse-face overlapping
7033 the one we are currently highlighting, we have to
7034 check if we enter the overlapping overlay, and then
7035 highlight only that. */
7036 || (OVERLAYP (dpyinfo->mouse_face_overlay)
7037 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
7039 /* Find the highest priority overlay that has a mouse-face
7040 property. */
7041 overlay = Qnil;
7042 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
7044 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
7045 if (!NILP (mouse_face))
7046 overlay = overlay_vec[i];
7049 /* If we're actually highlighting the same overlay as
7050 before, there's no need to do that again. */
7051 if (!NILP (overlay)
7052 && EQ (overlay, dpyinfo->mouse_face_overlay))
7053 goto check_help_echo;
7055 dpyinfo->mouse_face_overlay = overlay;
7057 /* Clear the display of the old active region, if any. */
7058 if (clear_mouse_face (dpyinfo))
7059 cursor = None;
7061 /* If no overlay applies, get a text property. */
7062 if (NILP (overlay))
7063 mouse_face = Fget_text_property (position, Qmouse_face, object);
7065 /* Handle the overlay case. */
7066 if (!NILP (overlay))
7068 /* Find the range of text around this char that
7069 should be active. */
7070 Lisp_Object before, after;
7071 int ignore;
7073 before = Foverlay_start (overlay);
7074 after = Foverlay_end (overlay);
7075 /* Record this as the current active region. */
7076 fast_find_position (w, XFASTINT (before),
7077 &dpyinfo->mouse_face_beg_col,
7078 &dpyinfo->mouse_face_beg_row,
7079 &dpyinfo->mouse_face_beg_x,
7080 &dpyinfo->mouse_face_beg_y, Qnil);
7082 dpyinfo->mouse_face_past_end
7083 = !fast_find_position (w, XFASTINT (after),
7084 &dpyinfo->mouse_face_end_col,
7085 &dpyinfo->mouse_face_end_row,
7086 &dpyinfo->mouse_face_end_x,
7087 &dpyinfo->mouse_face_end_y, Qnil);
7088 dpyinfo->mouse_face_window = window;
7089 dpyinfo->mouse_face_face_id
7090 = face_at_buffer_position (w, pos, 0, 0,
7091 &ignore, pos + 1, 1);
7093 /* Display it as active. */
7094 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7095 cursor = None;
7097 /* Handle the text property case. */
7098 else if (!NILP (mouse_face) && BUFFERP (object))
7100 /* Find the range of text around this char that
7101 should be active. */
7102 Lisp_Object before, after, beginning, end;
7103 int ignore;
7105 beginning = Fmarker_position (w->start);
7106 end = make_number (BUF_Z (XBUFFER (object))
7107 - XFASTINT (w->window_end_pos));
7108 before
7109 = Fprevious_single_property_change (make_number (pos + 1),
7110 Qmouse_face,
7111 object, beginning);
7112 after
7113 = Fnext_single_property_change (position, Qmouse_face,
7114 object, end);
7116 /* Record this as the current active region. */
7117 fast_find_position (w, XFASTINT (before),
7118 &dpyinfo->mouse_face_beg_col,
7119 &dpyinfo->mouse_face_beg_row,
7120 &dpyinfo->mouse_face_beg_x,
7121 &dpyinfo->mouse_face_beg_y, Qnil);
7122 dpyinfo->mouse_face_past_end
7123 = !fast_find_position (w, XFASTINT (after),
7124 &dpyinfo->mouse_face_end_col,
7125 &dpyinfo->mouse_face_end_row,
7126 &dpyinfo->mouse_face_end_x,
7127 &dpyinfo->mouse_face_end_y, Qnil);
7128 dpyinfo->mouse_face_window = window;
7130 if (BUFFERP (object))
7131 dpyinfo->mouse_face_face_id
7132 = face_at_buffer_position (w, pos, 0, 0,
7133 &ignore, pos + 1, 1);
7135 /* Display it as active. */
7136 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7137 cursor = None;
7139 else if (!NILP (mouse_face) && STRINGP (object))
7141 Lisp_Object b, e;
7142 int ignore;
7144 b = Fprevious_single_property_change (make_number (pos + 1),
7145 Qmouse_face,
7146 object, Qnil);
7147 e = Fnext_single_property_change (position, Qmouse_face,
7148 object, Qnil);
7149 if (NILP (b))
7150 b = make_number (0);
7151 if (NILP (e))
7152 e = make_number (XSTRING (object)->size - 1);
7153 fast_find_string_pos (w, XINT (b), object,
7154 &dpyinfo->mouse_face_beg_col,
7155 &dpyinfo->mouse_face_beg_row,
7156 &dpyinfo->mouse_face_beg_x,
7157 &dpyinfo->mouse_face_beg_y, 0);
7158 fast_find_string_pos (w, XINT (e), object,
7159 &dpyinfo->mouse_face_end_col,
7160 &dpyinfo->mouse_face_end_row,
7161 &dpyinfo->mouse_face_end_x,
7162 &dpyinfo->mouse_face_end_y, 1);
7163 dpyinfo->mouse_face_past_end = 0;
7164 dpyinfo->mouse_face_window = window;
7165 dpyinfo->mouse_face_face_id
7166 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
7167 glyph->face_id, 1);
7168 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7169 cursor = None;
7171 else if (STRINGP (object) && NILP (mouse_face))
7173 /* A string which doesn't have mouse-face, but
7174 the text ``under'' it might have. */
7175 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
7176 int start = MATRIX_ROW_START_CHARPOS (r);
7178 pos = string_buffer_position (w, object, start);
7179 if (pos > 0)
7180 mouse_face = get_char_property_and_overlay (make_number (pos),
7181 Qmouse_face,
7182 w->buffer,
7183 &overlay);
7184 if (!NILP (mouse_face) && !NILP (overlay))
7186 Lisp_Object before = Foverlay_start (overlay);
7187 Lisp_Object after = Foverlay_end (overlay);
7188 int ignore;
7190 /* Note that we might not be able to find position
7191 BEFORE in the glyph matrix if the overlay is
7192 entirely covered by a `display' property. In
7193 this case, we overshoot. So let's stop in
7194 the glyph matrix before glyphs for OBJECT. */
7195 fast_find_position (w, XFASTINT (before),
7196 &dpyinfo->mouse_face_beg_col,
7197 &dpyinfo->mouse_face_beg_row,
7198 &dpyinfo->mouse_face_beg_x,
7199 &dpyinfo->mouse_face_beg_y,
7200 object);
7202 dpyinfo->mouse_face_past_end
7203 = !fast_find_position (w, XFASTINT (after),
7204 &dpyinfo->mouse_face_end_col,
7205 &dpyinfo->mouse_face_end_row,
7206 &dpyinfo->mouse_face_end_x,
7207 &dpyinfo->mouse_face_end_y,
7208 Qnil);
7209 dpyinfo->mouse_face_window = window;
7210 dpyinfo->mouse_face_face_id
7211 = face_at_buffer_position (w, pos, 0, 0,
7212 &ignore, pos + 1, 1);
7214 /* Display it as active. */
7215 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7216 cursor = None;
7221 check_help_echo:
7223 /* Look for a `help-echo' property. */
7225 Lisp_Object help, overlay;
7227 /* Check overlays first. */
7228 help = overlay = Qnil;
7229 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
7231 overlay = overlay_vec[i];
7232 help = Foverlay_get (overlay, Qhelp_echo);
7235 if (!NILP (help))
7237 help_echo = help;
7238 help_echo_window = window;
7239 help_echo_object = overlay;
7240 help_echo_pos = pos;
7242 else
7244 Lisp_Object object = glyph->object;
7245 int charpos = glyph->charpos;
7247 /* Try text properties. */
7248 if (STRINGP (object)
7249 && charpos >= 0
7250 && charpos < XSTRING (object)->size)
7252 help = Fget_text_property (make_number (charpos),
7253 Qhelp_echo, object);
7254 if (NILP (help))
7256 /* If the string itself doesn't specify a help-echo,
7257 see if the buffer text ``under'' it does. */
7258 struct glyph_row *r
7259 = MATRIX_ROW (w->current_matrix, vpos);
7260 int start = MATRIX_ROW_START_CHARPOS (r);
7261 int pos = string_buffer_position (w, object, start);
7262 if (pos > 0)
7264 help = Fget_char_property (make_number (pos),
7265 Qhelp_echo, w->buffer);
7266 if (!NILP (help))
7268 charpos = pos;
7269 object = w->buffer;
7274 else if (BUFFERP (object)
7275 && charpos >= BEGV
7276 && charpos < ZV)
7277 help = Fget_text_property (make_number (charpos), Qhelp_echo,
7278 object);
7280 if (!NILP (help))
7282 help_echo = help;
7283 help_echo_window = window;
7284 help_echo_object = object;
7285 help_echo_pos = charpos;
7290 BEGV = obegv;
7291 ZV = ozv;
7292 current_buffer = obuf;
7295 set_cursor:
7297 if (cursor != None)
7298 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7301 static void
7302 redo_mouse_highlight ()
7304 if (!NILP (last_mouse_motion_frame)
7305 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
7306 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
7307 last_mouse_motion_event.x,
7308 last_mouse_motion_event.y);
7313 /***********************************************************************
7314 Tool-bars
7315 ***********************************************************************/
7317 static int x_tool_bar_item P_ ((struct frame *, int, int,
7318 struct glyph **, int *, int *, int *));
7320 /* Tool-bar item index of the item on which a mouse button was pressed
7321 or -1. */
7323 static int last_tool_bar_item;
7326 /* Get information about the tool-bar item at position X/Y on frame F.
7327 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7328 the current matrix of the tool-bar window of F, or NULL if not
7329 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
7330 item in F->tool_bar_items. Value is
7332 -1 if X/Y is not on a tool-bar item
7333 0 if X/Y is on the same item that was highlighted before.
7334 1 otherwise. */
7336 static int
7337 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
7338 struct frame *f;
7339 int x, y;
7340 struct glyph **glyph;
7341 int *hpos, *vpos, *prop_idx;
7343 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7344 struct window *w = XWINDOW (f->tool_bar_window);
7345 int area;
7347 /* Find the glyph under X/Y. */
7348 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
7349 if (*glyph == NULL)
7350 return -1;
7352 /* Get the start of this tool-bar item's properties in
7353 f->tool_bar_items. */
7354 if (!tool_bar_item_info (f, *glyph, prop_idx))
7355 return -1;
7357 /* Is mouse on the highlighted item? */
7358 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
7359 && *vpos >= dpyinfo->mouse_face_beg_row
7360 && *vpos <= dpyinfo->mouse_face_end_row
7361 && (*vpos > dpyinfo->mouse_face_beg_row
7362 || *hpos >= dpyinfo->mouse_face_beg_col)
7363 && (*vpos < dpyinfo->mouse_face_end_row
7364 || *hpos < dpyinfo->mouse_face_end_col
7365 || dpyinfo->mouse_face_past_end))
7366 return 0;
7368 return 1;
7372 /* Handle mouse button event on the tool-bar of frame F, at
7373 frame-relative coordinates X/Y. EVENT_TYPE is either ButtonPress
7374 or ButtonRelase. */
7376 static void
7377 x_handle_tool_bar_click (f, button_event)
7378 struct frame *f;
7379 XButtonEvent *button_event;
7381 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7382 struct window *w = XWINDOW (f->tool_bar_window);
7383 int hpos, vpos, prop_idx;
7384 struct glyph *glyph;
7385 Lisp_Object enabled_p;
7386 int x = button_event->x;
7387 int y = button_event->y;
7389 /* If not on the highlighted tool-bar item, return. */
7390 frame_to_window_pixel_xy (w, &x, &y);
7391 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
7392 return;
7394 /* If item is disabled, do nothing. */
7395 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7396 if (NILP (enabled_p))
7397 return;
7399 if (button_event->type == ButtonPress)
7401 /* Show item in pressed state. */
7402 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7403 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
7404 last_tool_bar_item = prop_idx;
7406 else
7408 Lisp_Object key, frame;
7409 struct input_event event;
7411 /* Show item in released state. */
7412 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7413 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7415 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
7417 XSETFRAME (frame, f);
7418 event.kind = TOOL_BAR_EVENT;
7419 event.frame_or_window = frame;
7420 event.arg = frame;
7421 kbd_buffer_store_event (&event);
7423 event.kind = TOOL_BAR_EVENT;
7424 event.frame_or_window = frame;
7425 event.arg = key;
7426 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7427 button_event->state);
7428 kbd_buffer_store_event (&event);
7429 last_tool_bar_item = -1;
7434 /* Possibly highlight a tool-bar item on frame F when mouse moves to
7435 tool-bar window-relative coordinates X/Y. Called from
7436 note_mouse_highlight. */
7438 static void
7439 note_tool_bar_highlight (f, x, y)
7440 struct frame *f;
7441 int x, y;
7443 Lisp_Object window = f->tool_bar_window;
7444 struct window *w = XWINDOW (window);
7445 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7446 int hpos, vpos;
7447 struct glyph *glyph;
7448 struct glyph_row *row;
7449 int i;
7450 Lisp_Object enabled_p;
7451 int prop_idx;
7452 enum draw_glyphs_face draw;
7453 int mouse_down_p, rc;
7455 /* Function note_mouse_highlight is called with negative x(y
7456 values when mouse moves outside of the frame. */
7457 if (x <= 0 || y <= 0)
7459 clear_mouse_face (dpyinfo);
7460 return;
7463 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7464 if (rc < 0)
7466 /* Not on tool-bar item. */
7467 clear_mouse_face (dpyinfo);
7468 return;
7470 else if (rc == 0)
7471 goto set_help_echo;
7473 clear_mouse_face (dpyinfo);
7475 /* Mouse is down, but on different tool-bar item? */
7476 mouse_down_p = (dpyinfo->grabbed
7477 && f == last_mouse_frame
7478 && FRAME_LIVE_P (f));
7479 if (mouse_down_p
7480 && last_tool_bar_item != prop_idx)
7481 return;
7483 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7484 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7486 /* If tool-bar item is not enabled, don't highlight it. */
7487 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7488 if (!NILP (enabled_p))
7490 /* Compute the x-position of the glyph. In front and past the
7491 image is a space. We include this is the highlighted area. */
7492 row = MATRIX_ROW (w->current_matrix, vpos);
7493 for (i = x = 0; i < hpos; ++i)
7494 x += row->glyphs[TEXT_AREA][i].pixel_width;
7496 /* Record this as the current active region. */
7497 dpyinfo->mouse_face_beg_col = hpos;
7498 dpyinfo->mouse_face_beg_row = vpos;
7499 dpyinfo->mouse_face_beg_x = x;
7500 dpyinfo->mouse_face_beg_y = row->y;
7501 dpyinfo->mouse_face_past_end = 0;
7503 dpyinfo->mouse_face_end_col = hpos + 1;
7504 dpyinfo->mouse_face_end_row = vpos;
7505 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7506 dpyinfo->mouse_face_end_y = row->y;
7507 dpyinfo->mouse_face_window = window;
7508 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7510 /* Display it as active. */
7511 show_mouse_face (dpyinfo, draw);
7512 dpyinfo->mouse_face_image_state = draw;
7515 set_help_echo:
7517 /* Set help_echo to a help string to display for this tool-bar item.
7518 XTread_socket does the rest. */
7519 help_echo_object = help_echo_window = Qnil;
7520 help_echo_pos = -1;
7521 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
7522 if (NILP (help_echo))
7523 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
7528 /* Find the glyph matrix position of buffer position CHARPOS in window
7529 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7530 current glyphs must be up to date. If CHARPOS is above window
7531 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
7532 of last line in W. In the row containing CHARPOS, stop before glyphs
7533 having STOP as object. */
7535 #if 0 /* This is a version of fast_find_position that's more correct
7536 in the presence of hscrolling, for example. I didn't install
7537 it right away because the problem fixed is minor, it failed
7538 in 20.x as well, and I think it's too risky to install
7539 so near the release of 21.1. 2001-09-25 gerd. */
7541 static int
7542 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
7543 struct window *w;
7544 int charpos;
7545 int *hpos, *vpos, *x, *y;
7546 Lisp_Object stop;
7548 struct glyph_row *row, *first;
7549 struct glyph *glyph, *end;
7550 int i, past_end = 0;
7552 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7553 row = row_containing_pos (w, charpos, first, NULL, 0);
7554 if (row == NULL)
7556 if (charpos < MATRIX_ROW_START_CHARPOS (first))
7558 *x = *y = *hpos = *vpos = 0;
7559 return 0;
7561 else
7563 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
7564 past_end = 1;
7568 *x = row->x;
7569 *y = row->y;
7570 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7572 glyph = row->glyphs[TEXT_AREA];
7573 end = glyph + row->used[TEXT_AREA];
7575 /* Skip over glyphs not having an object at the start of the row.
7576 These are special glyphs like truncation marks on terminal
7577 frames. */
7578 if (row->displays_text_p)
7579 while (glyph < end
7580 && INTEGERP (glyph->object)
7581 && !EQ (stop, glyph->object)
7582 && glyph->charpos < 0)
7584 *x += glyph->pixel_width;
7585 ++glyph;
7588 while (glyph < end
7589 && !INTEGERP (glyph->object)
7590 && !EQ (stop, glyph->object)
7591 && (!BUFFERP (glyph->object)
7592 || glyph->charpos < charpos))
7594 *x += glyph->pixel_width;
7595 ++glyph;
7598 *hpos = glyph - row->glyphs[TEXT_AREA];
7599 return past_end;
7602 #else /* not 0 */
7604 static int
7605 fast_find_position (w, pos, hpos, vpos, x, y, stop)
7606 struct window *w;
7607 int pos;
7608 int *hpos, *vpos, *x, *y;
7609 Lisp_Object stop;
7611 int i;
7612 int lastcol;
7613 int maybe_next_line_p = 0;
7614 int line_start_position;
7615 int yb = window_text_bottom_y (w);
7616 struct glyph_row *row, *best_row;
7617 int row_vpos, best_row_vpos;
7618 int current_x;
7620 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7621 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7623 while (row->y < yb)
7625 if (row->used[TEXT_AREA])
7626 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7627 else
7628 line_start_position = 0;
7630 if (line_start_position > pos)
7631 break;
7632 /* If the position sought is the end of the buffer,
7633 don't include the blank lines at the bottom of the window. */
7634 else if (line_start_position == pos
7635 && pos == BUF_ZV (XBUFFER (w->buffer)))
7637 maybe_next_line_p = 1;
7638 break;
7640 else if (line_start_position > 0)
7642 best_row = row;
7643 best_row_vpos = row_vpos;
7646 if (row->y + row->height >= yb)
7647 break;
7649 ++row;
7650 ++row_vpos;
7653 /* Find the right column within BEST_ROW. */
7654 lastcol = 0;
7655 current_x = best_row->x;
7656 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
7658 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7659 int charpos = glyph->charpos;
7661 if (BUFFERP (glyph->object))
7663 if (charpos == pos)
7665 *hpos = i;
7666 *vpos = best_row_vpos;
7667 *x = current_x;
7668 *y = best_row->y;
7669 return 1;
7671 else if (charpos > pos)
7672 break;
7674 else if (EQ (glyph->object, stop))
7675 break;
7677 if (charpos > 0)
7678 lastcol = i;
7679 current_x += glyph->pixel_width;
7682 /* If we're looking for the end of the buffer,
7683 and we didn't find it in the line we scanned,
7684 use the start of the following line. */
7685 if (maybe_next_line_p)
7687 ++best_row;
7688 ++best_row_vpos;
7689 lastcol = 0;
7690 current_x = best_row->x;
7693 *vpos = best_row_vpos;
7694 *hpos = lastcol + 1;
7695 *x = current_x;
7696 *y = best_row->y;
7697 return 0;
7700 #endif /* not 0 */
7703 /* Find the position of the glyph for position POS in OBJECT in
7704 window W's current matrix, and return in *X, *Y the pixel
7705 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
7707 RIGHT_P non-zero means return the position of the right edge of the
7708 glyph, RIGHT_P zero means return the left edge position.
7710 If no glyph for POS exists in the matrix, return the position of
7711 the glyph with the next smaller position that is in the matrix, if
7712 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7713 exists in the matrix, return the position of the glyph with the
7714 next larger position in OBJECT.
7716 Value is non-zero if a glyph was found. */
7718 static int
7719 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7720 struct window *w;
7721 int pos;
7722 Lisp_Object object;
7723 int *hpos, *vpos, *x, *y;
7724 int right_p;
7726 int yb = window_text_bottom_y (w);
7727 struct glyph_row *r;
7728 struct glyph *best_glyph = NULL;
7729 struct glyph_row *best_row = NULL;
7730 int best_x = 0;
7732 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7733 r->enabled_p && r->y < yb;
7734 ++r)
7736 struct glyph *g = r->glyphs[TEXT_AREA];
7737 struct glyph *e = g + r->used[TEXT_AREA];
7738 int gx;
7740 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7741 if (EQ (g->object, object))
7743 if (g->charpos == pos)
7745 best_glyph = g;
7746 best_x = gx;
7747 best_row = r;
7748 goto found;
7750 else if (best_glyph == NULL
7751 || ((abs (g->charpos - pos)
7752 < abs (best_glyph->charpos - pos))
7753 && (right_p
7754 ? g->charpos < pos
7755 : g->charpos > pos)))
7757 best_glyph = g;
7758 best_x = gx;
7759 best_row = r;
7764 found:
7766 if (best_glyph)
7768 *x = best_x;
7769 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7771 if (right_p)
7773 *x += best_glyph->pixel_width;
7774 ++*hpos;
7777 *y = best_row->y;
7778 *vpos = best_row - w->current_matrix->rows;
7781 return best_glyph != NULL;
7785 /* Display the active region described by mouse_face_*
7786 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7788 static void
7789 show_mouse_face (dpyinfo, draw)
7790 struct x_display_info *dpyinfo;
7791 enum draw_glyphs_face draw;
7793 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7794 struct frame *f = XFRAME (WINDOW_FRAME (w));
7796 if (/* If window is in the process of being destroyed, don't bother
7797 to do anything. */
7798 w->current_matrix != NULL
7799 /* Don't update mouse highlight if hidden */
7800 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
7801 /* Recognize when we are called to operate on rows that don't exist
7802 anymore. This can happen when a window is split. */
7803 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
7805 int phys_cursor_on_p = w->phys_cursor_on_p;
7806 struct glyph_row *row, *first, *last;
7808 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
7809 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
7811 for (row = first; row <= last && row->enabled_p; ++row)
7813 int start_hpos, end_hpos, start_x;
7815 /* For all but the first row, the highlight starts at column 0. */
7816 if (row == first)
7818 start_hpos = dpyinfo->mouse_face_beg_col;
7819 start_x = dpyinfo->mouse_face_beg_x;
7821 else
7823 start_hpos = 0;
7824 start_x = 0;
7827 if (row == last)
7828 end_hpos = dpyinfo->mouse_face_end_col;
7829 else
7830 end_hpos = row->used[TEXT_AREA];
7832 if (end_hpos > start_hpos)
7834 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7835 start_hpos, end_hpos, draw, 0);
7837 row->mouse_face_p
7838 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
7842 /* When we've written over the cursor, arrange for it to
7843 be displayed again. */
7844 if (phys_cursor_on_p && !w->phys_cursor_on_p)
7845 x_display_cursor (w, 1,
7846 w->phys_cursor.hpos, w->phys_cursor.vpos,
7847 w->phys_cursor.x, w->phys_cursor.y);
7850 /* Change the mouse cursor. */
7851 if (draw == DRAW_NORMAL_TEXT)
7852 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7853 f->output_data.x->text_cursor);
7854 else if (draw == DRAW_MOUSE_FACE)
7855 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7856 f->output_data.x->cross_cursor);
7857 else
7858 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7859 f->output_data.x->nontext_cursor);
7862 /* Clear out the mouse-highlighted active region.
7863 Redraw it un-highlighted first. Value is non-zero if mouse
7864 face was actually drawn unhighlighted. */
7866 static int
7867 clear_mouse_face (dpyinfo)
7868 struct x_display_info *dpyinfo;
7870 int cleared = 0;
7872 if (!NILP (dpyinfo->mouse_face_window))
7874 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7875 cleared = 1;
7878 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7879 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7880 dpyinfo->mouse_face_window = Qnil;
7881 dpyinfo->mouse_face_overlay = Qnil;
7882 return cleared;
7886 /* Clear any mouse-face on window W. This function is part of the
7887 redisplay interface, and is called from try_window_id and similar
7888 functions to ensure the mouse-highlight is off. */
7890 static void
7891 x_clear_mouse_face (w)
7892 struct window *w;
7894 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7895 Lisp_Object window;
7897 BLOCK_INPUT;
7898 XSETWINDOW (window, w);
7899 if (EQ (window, dpyinfo->mouse_face_window))
7900 clear_mouse_face (dpyinfo);
7901 UNBLOCK_INPUT;
7905 /* Just discard the mouse face information for frame F, if any.
7906 This is used when the size of F is changed. */
7908 void
7909 cancel_mouse_face (f)
7910 FRAME_PTR f;
7912 Lisp_Object window;
7913 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7915 window = dpyinfo->mouse_face_window;
7916 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7918 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7919 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7920 dpyinfo->mouse_face_window = Qnil;
7925 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
7928 /* Try to determine frame pixel position and size of the glyph under
7929 frame pixel coordinates X/Y on frame F . Return the position and
7930 size in *RECT. Value is non-zero if we could compute these
7931 values. */
7933 static int
7934 glyph_rect (f, x, y, rect)
7935 struct frame *f;
7936 int x, y;
7937 XRectangle *rect;
7939 Lisp_Object window;
7940 int part, found = 0;
7942 window = window_from_coordinates (f, x, y, &part, 0);
7943 if (!NILP (window))
7945 struct window *w = XWINDOW (window);
7946 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7947 struct glyph_row *end = r + w->current_matrix->nrows - 1;
7949 frame_to_window_pixel_xy (w, &x, &y);
7951 for (; !found && r < end && r->enabled_p; ++r)
7952 if (r->y >= y)
7954 struct glyph *g = r->glyphs[TEXT_AREA];
7955 struct glyph *end = g + r->used[TEXT_AREA];
7956 int gx;
7958 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
7959 if (gx >= x)
7961 rect->width = g->pixel_width;
7962 rect->height = r->height;
7963 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
7964 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
7965 found = 1;
7970 return found;
7974 /* Return the current position of the mouse.
7975 *FP should be a frame which indicates which display to ask about.
7977 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
7978 and *PART to the frame, window, and scroll bar part that the mouse
7979 is over. Set *X and *Y to the portion and whole of the mouse's
7980 position on the scroll bar.
7982 If the mouse movement started elsewhere, set *FP to the frame the
7983 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
7984 the mouse is over.
7986 Set *TIME to the server time-stamp for the time at which the mouse
7987 was at this position.
7989 Don't store anything if we don't have a valid set of values to report.
7991 This clears the mouse_moved flag, so we can wait for the next mouse
7992 movement. */
7994 static void
7995 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7996 FRAME_PTR *fp;
7997 int insist;
7998 Lisp_Object *bar_window;
7999 enum scroll_bar_part *part;
8000 Lisp_Object *x, *y;
8001 unsigned long *time;
8003 FRAME_PTR f1;
8005 BLOCK_INPUT;
8007 if (! NILP (last_mouse_scroll_bar) && insist == 0)
8008 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
8009 else
8011 Window root;
8012 int root_x, root_y;
8014 Window dummy_window;
8015 int dummy;
8017 Lisp_Object frame, tail;
8019 /* Clear the mouse-moved flag for every frame on this display. */
8020 FOR_EACH_FRAME (tail, frame)
8021 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
8022 XFRAME (frame)->mouse_moved = 0;
8024 last_mouse_scroll_bar = Qnil;
8026 /* Figure out which root window we're on. */
8027 XQueryPointer (FRAME_X_DISPLAY (*fp),
8028 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
8030 /* The root window which contains the pointer. */
8031 &root,
8033 /* Trash which we can't trust if the pointer is on
8034 a different screen. */
8035 &dummy_window,
8037 /* The position on that root window. */
8038 &root_x, &root_y,
8040 /* More trash we can't trust. */
8041 &dummy, &dummy,
8043 /* Modifier keys and pointer buttons, about which
8044 we don't care. */
8045 (unsigned int *) &dummy);
8047 /* Now we have a position on the root; find the innermost window
8048 containing the pointer. */
8050 Window win, child;
8051 int win_x, win_y;
8052 int parent_x = 0, parent_y = 0;
8053 int count;
8055 win = root;
8057 /* XTranslateCoordinates can get errors if the window
8058 structure is changing at the same time this function
8059 is running. So at least we must not crash from them. */
8061 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
8063 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
8064 && FRAME_LIVE_P (last_mouse_frame))
8066 /* If mouse was grabbed on a frame, give coords for that frame
8067 even if the mouse is now outside it. */
8068 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
8070 /* From-window, to-window. */
8071 root, FRAME_X_WINDOW (last_mouse_frame),
8073 /* From-position, to-position. */
8074 root_x, root_y, &win_x, &win_y,
8076 /* Child of win. */
8077 &child);
8078 f1 = last_mouse_frame;
8080 else
8082 while (1)
8084 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
8086 /* From-window, to-window. */
8087 root, win,
8089 /* From-position, to-position. */
8090 root_x, root_y, &win_x, &win_y,
8092 /* Child of win. */
8093 &child);
8095 if (child == None || child == win)
8096 break;
8098 win = child;
8099 parent_x = win_x;
8100 parent_y = win_y;
8103 /* Now we know that:
8104 win is the innermost window containing the pointer
8105 (XTC says it has no child containing the pointer),
8106 win_x and win_y are the pointer's position in it
8107 (XTC did this the last time through), and
8108 parent_x and parent_y are the pointer's position in win's parent.
8109 (They are what win_x and win_y were when win was child.
8110 If win is the root window, it has no parent, and
8111 parent_{x,y} are invalid, but that's okay, because we'll
8112 never use them in that case.) */
8114 /* Is win one of our frames? */
8115 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
8117 #ifdef USE_X_TOOLKIT
8118 /* If we end up with the menu bar window, say it's not
8119 on the frame. */
8120 if (f1 != NULL
8121 && f1->output_data.x->menubar_widget
8122 && win == XtWindow (f1->output_data.x->menubar_widget))
8123 f1 = NULL;
8124 #endif /* USE_X_TOOLKIT */
8127 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
8128 f1 = 0;
8130 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
8132 /* If not, is it one of our scroll bars? */
8133 if (! f1)
8135 struct scroll_bar *bar = x_window_to_scroll_bar (win);
8137 if (bar)
8139 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8140 win_x = parent_x;
8141 win_y = parent_y;
8145 if (f1 == 0 && insist > 0)
8146 f1 = SELECTED_FRAME ();
8148 if (f1)
8150 /* Ok, we found a frame. Store all the values.
8151 last_mouse_glyph is a rectangle used to reduce the
8152 generation of mouse events. To not miss any motion
8153 events, we must divide the frame into rectangles of the
8154 size of the smallest character that could be displayed
8155 on it, i.e. into the same rectangles that matrices on
8156 the frame are divided into. */
8158 int width, height, gx, gy;
8159 XRectangle rect;
8161 if (glyph_rect (f1, win_x, win_y, &rect))
8162 last_mouse_glyph = rect;
8163 else
8165 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
8166 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
8167 gx = win_x;
8168 gy = win_y;
8170 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
8171 round down even for negative values. */
8172 if (gx < 0)
8173 gx -= width - 1;
8174 if (gy < 0)
8175 gy -= height - 1;
8176 gx = (gx + width - 1) / width * width;
8177 gy = (gy + height - 1) / height * height;
8179 last_mouse_glyph.width = width;
8180 last_mouse_glyph.height = height;
8181 last_mouse_glyph.x = gx;
8182 last_mouse_glyph.y = gy;
8185 *bar_window = Qnil;
8186 *part = 0;
8187 *fp = f1;
8188 XSETINT (*x, win_x);
8189 XSETINT (*y, win_y);
8190 *time = last_mouse_movement_time;
8195 UNBLOCK_INPUT;
8199 #ifdef USE_X_TOOLKIT
8201 /* Atimer callback function for TIMER. Called every 0.1s to process
8202 Xt timeouts, if needed. We must avoid calling XtAppPending as
8203 much as possible because that function does an implicit XFlush
8204 that slows us down. */
8206 static void
8207 x_process_timeouts (timer)
8208 struct atimer *timer;
8210 if (toolkit_scroll_bar_interaction || popup_activated_flag)
8212 BLOCK_INPUT;
8213 while (XtAppPending (Xt_app_con) & XtIMTimer)
8214 XtAppProcessEvent (Xt_app_con, XtIMTimer);
8215 UNBLOCK_INPUT;
8219 #endif /* USE_X_TOOLKIT */
8222 /* Scroll bar support. */
8224 /* Given an X window ID, find the struct scroll_bar which manages it.
8225 This can be called in GC, so we have to make sure to strip off mark
8226 bits. */
8228 static struct scroll_bar *
8229 x_window_to_scroll_bar (window_id)
8230 Window window_id;
8232 Lisp_Object tail;
8234 for (tail = Vframe_list;
8235 XGCTYPE (tail) == Lisp_Cons;
8236 tail = XCDR (tail))
8238 Lisp_Object frame, bar, condemned;
8240 frame = XCAR (tail);
8241 /* All elements of Vframe_list should be frames. */
8242 if (! GC_FRAMEP (frame))
8243 abort ();
8245 /* Scan this frame's scroll bar list for a scroll bar with the
8246 right window ID. */
8247 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
8248 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
8249 /* This trick allows us to search both the ordinary and
8250 condemned scroll bar lists with one loop. */
8251 ! GC_NILP (bar) || (bar = condemned,
8252 condemned = Qnil,
8253 ! GC_NILP (bar));
8254 bar = XSCROLL_BAR (bar)->next)
8255 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
8256 return XSCROLL_BAR (bar);
8259 return 0;
8263 #if defined USE_LUCID
8265 /* Return the Lucid menu bar WINDOW is part of. Return null
8266 if WINDOW is not part of a menu bar. */
8268 static Widget
8269 x_window_to_menu_bar (window)
8270 Window window;
8272 Lisp_Object tail;
8274 for (tail = Vframe_list;
8275 XGCTYPE (tail) == Lisp_Cons;
8276 tail = XCDR (tail))
8278 Lisp_Object frame = XCAR (tail);
8279 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
8281 if (menu_bar && xlwmenu_window_p (menu_bar, window))
8282 return menu_bar;
8285 return NULL;
8288 #endif /* USE_LUCID */
8291 /************************************************************************
8292 Toolkit scroll bars
8293 ************************************************************************/
8295 #ifdef USE_TOOLKIT_SCROLL_BARS
8297 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
8298 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
8299 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
8300 struct scroll_bar *));
8301 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
8302 int, int, int));
8305 /* Id of action hook installed for scroll bars. */
8307 static XtActionHookId action_hook_id;
8309 /* Lisp window being scrolled. Set when starting to interact with
8310 a toolkit scroll bar, reset to nil when ending the interaction. */
8312 static Lisp_Object window_being_scrolled;
8314 /* Last scroll bar part sent in xm_scroll_callback. */
8316 static int last_scroll_bar_part;
8318 /* Whether this is an Xaw with arrow-scrollbars. This should imply
8319 that movements of 1/20 of the screen size are mapped to up/down. */
8321 static Boolean xaw3d_arrow_scroll;
8323 /* Whether the drag scrolling maintains the mouse at the top of the
8324 thumb. If not, resizing the thumb needs to be done more carefully
8325 to avoid jerkyness. */
8327 static Boolean xaw3d_pick_top;
8330 /* Action hook installed via XtAppAddActionHook when toolkit scroll
8331 bars are used.. The hook is responsible for detecting when
8332 the user ends an interaction with the scroll bar, and generates
8333 a `end-scroll' scroll_bar_click' event if so. */
8335 static void
8336 xt_action_hook (widget, client_data, action_name, event, params,
8337 num_params)
8338 Widget widget;
8339 XtPointer client_data;
8340 String action_name;
8341 XEvent *event;
8342 String *params;
8343 Cardinal *num_params;
8345 int scroll_bar_p;
8346 char *end_action;
8348 #ifdef USE_MOTIF
8349 scroll_bar_p = XmIsScrollBar (widget);
8350 end_action = "Release";
8351 #else /* !USE_MOTIF i.e. use Xaw */
8352 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
8353 end_action = "EndScroll";
8354 #endif /* USE_MOTIF */
8356 if (scroll_bar_p
8357 && strcmp (action_name, end_action) == 0
8358 && WINDOWP (window_being_scrolled))
8360 struct window *w;
8362 x_send_scroll_bar_event (window_being_scrolled,
8363 scroll_bar_end_scroll, 0, 0);
8364 w = XWINDOW (window_being_scrolled);
8365 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
8366 window_being_scrolled = Qnil;
8367 last_scroll_bar_part = -1;
8369 /* Xt timeouts no longer needed. */
8370 toolkit_scroll_bar_interaction = 0;
8374 /* A vector of windows used for communication between
8375 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
8377 static struct window **scroll_bar_windows;
8378 static int scroll_bar_windows_size;
8381 /* Send a client message with message type Xatom_Scrollbar for a
8382 scroll action to the frame of WINDOW. PART is a value identifying
8383 the part of the scroll bar that was clicked on. PORTION is the
8384 amount to scroll of a whole of WHOLE. */
8386 static void
8387 x_send_scroll_bar_event (window, part, portion, whole)
8388 Lisp_Object window;
8389 int part, portion, whole;
8391 XEvent event;
8392 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
8393 struct window *w = XWINDOW (window);
8394 struct frame *f = XFRAME (w->frame);
8395 int i;
8397 BLOCK_INPUT;
8399 /* Construct a ClientMessage event to send to the frame. */
8400 ev->type = ClientMessage;
8401 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
8402 ev->display = FRAME_X_DISPLAY (f);
8403 ev->window = FRAME_X_WINDOW (f);
8404 ev->format = 32;
8406 /* We can only transfer 32 bits in the XClientMessageEvent, which is
8407 not enough to store a pointer or Lisp_Object on a 64 bit system.
8408 So, store the window in scroll_bar_windows and pass the index
8409 into that array in the event. */
8410 for (i = 0; i < scroll_bar_windows_size; ++i)
8411 if (scroll_bar_windows[i] == NULL)
8412 break;
8414 if (i == scroll_bar_windows_size)
8416 int new_size = max (10, 2 * scroll_bar_windows_size);
8417 size_t nbytes = new_size * sizeof *scroll_bar_windows;
8418 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
8420 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
8421 nbytes);
8422 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
8423 scroll_bar_windows_size = new_size;
8426 scroll_bar_windows[i] = w;
8427 ev->data.l[0] = (long) i;
8428 ev->data.l[1] = (long) part;
8429 ev->data.l[2] = (long) 0;
8430 ev->data.l[3] = (long) portion;
8431 ev->data.l[4] = (long) whole;
8433 /* Make Xt timeouts work while the scroll bar is active. */
8434 toolkit_scroll_bar_interaction = 1;
8436 /* Setting the event mask to zero means that the message will
8437 be sent to the client that created the window, and if that
8438 window no longer exists, no event will be sent. */
8439 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
8440 UNBLOCK_INPUT;
8444 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
8445 in *IEVENT. */
8447 static void
8448 x_scroll_bar_to_input_event (event, ievent)
8449 XEvent *event;
8450 struct input_event *ievent;
8452 XClientMessageEvent *ev = (XClientMessageEvent *) event;
8453 Lisp_Object window;
8454 struct frame *f;
8455 struct window *w;
8457 w = scroll_bar_windows[ev->data.l[0]];
8458 scroll_bar_windows[ev->data.l[0]] = NULL;
8460 XSETWINDOW (window, w);
8461 f = XFRAME (w->frame);
8463 ievent->kind = scroll_bar_click;
8464 ievent->frame_or_window = window;
8465 ievent->arg = Qnil;
8466 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
8467 ievent->part = ev->data.l[1];
8468 ievent->code = ev->data.l[2];
8469 ievent->x = make_number ((int) ev->data.l[3]);
8470 ievent->y = make_number ((int) ev->data.l[4]);
8471 ievent->modifiers = 0;
8475 #ifdef USE_MOTIF
8477 /* Minimum and maximum values used for Motif scroll bars. */
8479 #define XM_SB_MIN 1
8480 #define XM_SB_MAX 10000000
8481 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
8484 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
8485 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
8486 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
8488 static void
8489 xm_scroll_callback (widget, client_data, call_data)
8490 Widget widget;
8491 XtPointer client_data, call_data;
8493 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8494 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
8495 int part = -1, whole = 0, portion = 0;
8497 switch (cs->reason)
8499 case XmCR_DECREMENT:
8500 bar->dragging = Qnil;
8501 part = scroll_bar_up_arrow;
8502 break;
8504 case XmCR_INCREMENT:
8505 bar->dragging = Qnil;
8506 part = scroll_bar_down_arrow;
8507 break;
8509 case XmCR_PAGE_DECREMENT:
8510 bar->dragging = Qnil;
8511 part = scroll_bar_above_handle;
8512 break;
8514 case XmCR_PAGE_INCREMENT:
8515 bar->dragging = Qnil;
8516 part = scroll_bar_below_handle;
8517 break;
8519 case XmCR_TO_TOP:
8520 bar->dragging = Qnil;
8521 part = scroll_bar_to_top;
8522 break;
8524 case XmCR_TO_BOTTOM:
8525 bar->dragging = Qnil;
8526 part = scroll_bar_to_bottom;
8527 break;
8529 case XmCR_DRAG:
8531 int slider_size;
8532 int dragging_down_p = (INTEGERP (bar->dragging)
8533 && XINT (bar->dragging) <= cs->value);
8535 /* Get the slider size. */
8536 BLOCK_INPUT;
8537 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
8538 UNBLOCK_INPUT;
8540 /* At the max position of the scroll bar, do a line-wise
8541 movement. Without doing anything, we would be called with
8542 the same cs->value again and again. If we want to make
8543 sure that we can reach the end of the buffer, we have to do
8544 something.
8546 Implementation note: setting bar->dragging always to
8547 cs->value gives a smoother movement at the max position.
8548 Setting it to nil when doing line-wise movement gives
8549 a better slider behavior. */
8551 if (cs->value + slider_size == XM_SB_MAX
8552 || (dragging_down_p
8553 && last_scroll_bar_part == scroll_bar_down_arrow))
8555 part = scroll_bar_down_arrow;
8556 bar->dragging = Qnil;
8558 else
8560 whole = XM_SB_RANGE;
8561 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
8562 part = scroll_bar_handle;
8563 bar->dragging = make_number (cs->value);
8566 break;
8568 case XmCR_VALUE_CHANGED:
8569 break;
8572 if (part >= 0)
8574 window_being_scrolled = bar->window;
8575 last_scroll_bar_part = part;
8576 x_send_scroll_bar_event (bar->window, part, portion, whole);
8581 #else /* !USE_MOTIF, i.e. Xaw. */
8584 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
8585 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8586 scroll bar struct. CALL_DATA is a pointer to a float saying where
8587 the thumb is. */
8589 static void
8590 xaw_jump_callback (widget, client_data, call_data)
8591 Widget widget;
8592 XtPointer client_data, call_data;
8594 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8595 float top = *(float *) call_data;
8596 float shown;
8597 int whole, portion, height;
8598 int part;
8600 /* Get the size of the thumb, a value between 0 and 1. */
8601 BLOCK_INPUT;
8602 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
8603 UNBLOCK_INPUT;
8605 whole = 10000000;
8606 portion = shown < 1 ? top * whole : 0;
8608 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8609 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8610 the bottom, so we force the scrolling whenever we see that we're
8611 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8612 we try to ensure that we always stay two pixels away from the
8613 bottom). */
8614 part = scroll_bar_down_arrow;
8615 else
8616 part = scroll_bar_handle;
8618 window_being_scrolled = bar->window;
8619 bar->dragging = make_number (portion);
8620 last_scroll_bar_part = part;
8621 x_send_scroll_bar_event (bar->window, part, portion, whole);
8625 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
8626 i.e. line or page up or down. WIDGET is the Xaw scroll bar
8627 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8628 the scroll bar. CALL_DATA is an integer specifying the action that
8629 has taken place. It's magnitude is in the range 0..height of the
8630 scroll bar. Negative values mean scroll towards buffer start.
8631 Values < height of scroll bar mean line-wise movement. */
8633 static void
8634 xaw_scroll_callback (widget, client_data, call_data)
8635 Widget widget;
8636 XtPointer client_data, call_data;
8638 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8639 int position = (int) call_data;
8640 Dimension height;
8641 int part;
8643 /* Get the height of the scroll bar. */
8644 BLOCK_INPUT;
8645 XtVaGetValues (widget, XtNheight, &height, NULL);
8646 UNBLOCK_INPUT;
8648 if (abs (position) >= height)
8649 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8651 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8652 it maps line-movement to call_data = max(5, height/20). */
8653 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8654 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
8655 else
8656 part = scroll_bar_move_ratio;
8658 window_being_scrolled = bar->window;
8659 bar->dragging = Qnil;
8660 last_scroll_bar_part = part;
8661 x_send_scroll_bar_event (bar->window, part, position, height);
8665 #endif /* not USE_MOTIF */
8668 /* Create the widget for scroll bar BAR on frame F. Record the widget
8669 and X window of the scroll bar in BAR. */
8671 static void
8672 x_create_toolkit_scroll_bar (f, bar)
8673 struct frame *f;
8674 struct scroll_bar *bar;
8676 Window xwindow;
8677 Widget widget;
8678 Arg av[20];
8679 int ac = 0;
8680 char *scroll_bar_name = "verticalScrollBar";
8681 unsigned long pixel;
8683 BLOCK_INPUT;
8685 #ifdef USE_MOTIF
8686 /* Set resources. Create the widget. */
8687 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8688 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8689 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8690 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8691 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8692 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8693 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8695 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8696 if (pixel != -1)
8698 XtSetArg (av[ac], XmNforeground, pixel);
8699 ++ac;
8702 pixel = f->output_data.x->scroll_bar_background_pixel;
8703 if (pixel != -1)
8705 XtSetArg (av[ac], XmNbackground, pixel);
8706 ++ac;
8709 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8710 scroll_bar_name, av, ac);
8712 /* Add one callback for everything that can happen. */
8713 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8714 (XtPointer) bar);
8715 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8716 (XtPointer) bar);
8717 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8718 (XtPointer) bar);
8719 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8720 (XtPointer) bar);
8721 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8722 (XtPointer) bar);
8723 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8724 (XtPointer) bar);
8725 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8726 (XtPointer) bar);
8728 /* Realize the widget. Only after that is the X window created. */
8729 XtRealizeWidget (widget);
8731 /* Set the cursor to an arrow. I didn't find a resource to do that.
8732 And I'm wondering why it hasn't an arrow cursor by default. */
8733 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8734 f->output_data.x->nontext_cursor);
8736 #else /* !USE_MOTIF i.e. use Xaw */
8738 /* Set resources. Create the widget. The background of the
8739 Xaw3d scroll bar widget is a little bit light for my taste.
8740 We don't alter it here to let users change it according
8741 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8742 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8743 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
8744 /* For smoother scrolling with Xaw3d -sm */
8745 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8747 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8748 if (pixel != -1)
8750 XtSetArg (av[ac], XtNforeground, pixel);
8751 ++ac;
8754 pixel = f->output_data.x->scroll_bar_background_pixel;
8755 if (pixel != -1)
8757 XtSetArg (av[ac], XtNbackground, pixel);
8758 ++ac;
8761 /* Top/bottom shadow colors. */
8763 /* Allocate them, if necessary. */
8764 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
8766 pixel = f->output_data.x->scroll_bar_background_pixel;
8767 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8768 &pixel, 1.2, 0x8000))
8769 pixel = -1;
8770 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
8772 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8774 pixel = f->output_data.x->scroll_bar_background_pixel;
8775 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8776 &pixel, 0.6, 0x4000))
8777 pixel = -1;
8778 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
8781 /* Tell the toolkit about them. */
8782 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
8783 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8784 /* We tried to allocate a color for the top/bottom shadow, and
8785 failed, so tell Xaw3d to use dithering instead. */
8787 XtSetArg (av[ac], XtNbeNiceToColormap, True);
8788 ++ac;
8790 else
8791 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
8792 be more consistent with other emacs 3d colors, and since Xaw3d is
8793 not good at dealing with allocation failure. */
8795 /* This tells Xaw3d to use real colors instead of dithering for
8796 the shadows. */
8797 XtSetArg (av[ac], XtNbeNiceToColormap, False);
8798 ++ac;
8800 /* Specify the colors. */
8801 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
8802 if (pixel != -1)
8804 XtSetArg (av[ac], "topShadowPixel", pixel);
8805 ++ac;
8807 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
8808 if (pixel != -1)
8810 XtSetArg (av[ac], "bottomShadowPixel", pixel);
8811 ++ac;
8815 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8816 f->output_data.x->edit_widget, av, ac);
8819 char *initial = "";
8820 char *val = initial;
8821 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8822 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8823 if (val == initial)
8824 { /* ARROW_SCROLL */
8825 xaw3d_arrow_scroll = True;
8826 /* Isn't that just a personal preference ? -sm */
8827 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8831 /* Define callbacks. */
8832 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8833 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
8834 (XtPointer) bar);
8836 /* Realize the widget. Only after that is the X window created. */
8837 XtRealizeWidget (widget);
8839 #endif /* !USE_MOTIF */
8841 /* Install an action hook that let's us detect when the user
8842 finishes interacting with a scroll bar. */
8843 if (action_hook_id == 0)
8844 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8846 /* Remember X window and widget in the scroll bar vector. */
8847 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8848 xwindow = XtWindow (widget);
8849 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8851 UNBLOCK_INPUT;
8855 /* Set the thumb size and position of scroll bar BAR. We are currently
8856 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8858 static void
8859 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8860 struct scroll_bar *bar;
8861 int portion, position, whole;
8863 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8864 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8865 float top, shown;
8867 if (whole == 0)
8868 top = 0, shown = 1;
8869 else
8871 top = (float) position / whole;
8872 shown = (float) portion / whole;
8875 BLOCK_INPUT;
8877 #ifdef USE_MOTIF
8879 int size, value;
8881 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
8882 is the scroll bar's maximum and MIN is the scroll bar's minimum
8883 value. */
8884 size = shown * XM_SB_RANGE;
8885 size = min (size, XM_SB_RANGE);
8886 size = max (size, 1);
8888 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8889 value = top * XM_SB_RANGE;
8890 value = min (value, XM_SB_MAX - size);
8891 value = max (value, XM_SB_MIN);
8893 if (NILP (bar->dragging))
8894 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8895 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8896 /* This has the negative side effect that the slider value is
8897 not what it would be if we scrolled here using line-wise or
8898 page-wise movement. */
8899 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8900 else
8902 /* If currently dragging, only update the slider size.
8903 This reduces flicker effects. */
8904 int old_value, old_size, increment, page_increment;
8906 XmScrollBarGetValues (widget, &old_value, &old_size,
8907 &increment, &page_increment);
8908 XmScrollBarSetValues (widget, old_value,
8909 min (size, XM_SB_RANGE - old_value),
8910 0, 0, False);
8913 #else /* !USE_MOTIF i.e. use Xaw */
8915 float old_top, old_shown;
8916 Dimension height;
8917 XtVaGetValues (widget,
8918 XtNtopOfThumb, &old_top,
8919 XtNshown, &old_shown,
8920 XtNheight, &height,
8921 NULL);
8923 /* Massage the top+shown values. */
8924 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8925 top = max (0, min (1, top));
8926 else
8927 top = old_top;
8928 /* Keep two pixels available for moving the thumb down. */
8929 shown = max (0, min (1 - top - (2.0 / height), shown));
8931 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8932 check that your system's configuration file contains a define
8933 for `NARROWPROTO'. See s/freebsd.h for an example. */
8934 if (top != old_top || shown != old_shown)
8936 if (NILP (bar->dragging))
8937 XawScrollbarSetThumb (widget, top, shown);
8938 else
8940 #ifdef HAVE_XAW3D
8941 ScrollbarWidget sb = (ScrollbarWidget) widget;
8942 int scroll_mode = 0;
8944 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8945 if (xaw3d_arrow_scroll)
8947 /* Xaw3d stupidly ignores resize requests while dragging
8948 so we have to make it believe it's not in dragging mode. */
8949 scroll_mode = sb->scrollbar.scroll_mode;
8950 if (scroll_mode == 2)
8951 sb->scrollbar.scroll_mode = 0;
8953 #endif
8954 /* Try to make the scrolling a tad smoother. */
8955 if (!xaw3d_pick_top)
8956 shown = min (shown, old_shown);
8958 XawScrollbarSetThumb (widget, top, shown);
8960 #ifdef HAVE_XAW3D
8961 if (xaw3d_arrow_scroll && scroll_mode == 2)
8962 sb->scrollbar.scroll_mode = scroll_mode;
8963 #endif
8967 #endif /* !USE_MOTIF */
8969 UNBLOCK_INPUT;
8972 #endif /* USE_TOOLKIT_SCROLL_BARS */
8976 /************************************************************************
8977 Scroll bars, general
8978 ************************************************************************/
8980 /* Create a scroll bar and return the scroll bar vector for it. W is
8981 the Emacs window on which to create the scroll bar. TOP, LEFT,
8982 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8983 scroll bar. */
8985 static struct scroll_bar *
8986 x_scroll_bar_create (w, top, left, width, height)
8987 struct window *w;
8988 int top, left, width, height;
8990 struct frame *f = XFRAME (w->frame);
8991 struct scroll_bar *bar
8992 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8994 BLOCK_INPUT;
8996 #ifdef USE_TOOLKIT_SCROLL_BARS
8997 x_create_toolkit_scroll_bar (f, bar);
8998 #else /* not USE_TOOLKIT_SCROLL_BARS */
9000 XSetWindowAttributes a;
9001 unsigned long mask;
9002 Window window;
9004 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
9005 if (a.background_pixel == -1)
9006 a.background_pixel = f->output_data.x->background_pixel;
9008 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9009 | ButtonMotionMask | PointerMotionHintMask
9010 | ExposureMask);
9011 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
9013 mask = (CWBackPixel | CWEventMask | CWCursor);
9015 /* Clear the area of W that will serve as a scroll bar. This is
9016 for the case that a window has been split horizontally. In
9017 this case, no clear_frame is generated to reduce flickering. */
9018 if (width > 0 && height > 0)
9019 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9020 left, top, width,
9021 window_box_height (w), False);
9023 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9024 /* Position and size of scroll bar. */
9025 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9026 top,
9027 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9028 height,
9029 /* Border width, depth, class, and visual. */
9031 CopyFromParent,
9032 CopyFromParent,
9033 CopyFromParent,
9034 /* Attributes. */
9035 mask, &a);
9036 SET_SCROLL_BAR_X_WINDOW (bar, window);
9038 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9040 XSETWINDOW (bar->window, w);
9041 XSETINT (bar->top, top);
9042 XSETINT (bar->left, left);
9043 XSETINT (bar->width, width);
9044 XSETINT (bar->height, height);
9045 XSETINT (bar->start, 0);
9046 XSETINT (bar->end, 0);
9047 bar->dragging = Qnil;
9049 /* Add bar to its frame's list of scroll bars. */
9050 bar->next = FRAME_SCROLL_BARS (f);
9051 bar->prev = Qnil;
9052 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9053 if (!NILP (bar->next))
9054 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9056 /* Map the window/widget. */
9057 #ifdef USE_TOOLKIT_SCROLL_BARS
9059 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
9060 XtConfigureWidget (scroll_bar,
9061 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9062 top,
9063 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9064 max (height, 1), 0);
9065 XtMapWidget (scroll_bar);
9067 #else /* not USE_TOOLKIT_SCROLL_BARS */
9068 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9069 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9071 UNBLOCK_INPUT;
9072 return bar;
9076 /* Draw BAR's handle in the proper position.
9078 If the handle is already drawn from START to END, don't bother
9079 redrawing it, unless REBUILD is non-zero; in that case, always
9080 redraw it. (REBUILD is handy for drawing the handle after expose
9081 events.)
9083 Normally, we want to constrain the start and end of the handle to
9084 fit inside its rectangle, but if the user is dragging the scroll
9085 bar handle, we want to let them drag it down all the way, so that
9086 the bar's top is as far down as it goes; otherwise, there's no way
9087 to move to the very end of the buffer. */
9089 #ifndef USE_TOOLKIT_SCROLL_BARS
9091 static void
9092 x_scroll_bar_set_handle (bar, start, end, rebuild)
9093 struct scroll_bar *bar;
9094 int start, end;
9095 int rebuild;
9097 int dragging = ! NILP (bar->dragging);
9098 Window w = SCROLL_BAR_X_WINDOW (bar);
9099 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9100 GC gc = f->output_data.x->normal_gc;
9102 /* If the display is already accurate, do nothing. */
9103 if (! rebuild
9104 && start == XINT (bar->start)
9105 && end == XINT (bar->end))
9106 return;
9108 BLOCK_INPUT;
9111 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
9112 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9113 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9115 /* Make sure the values are reasonable, and try to preserve
9116 the distance between start and end. */
9118 int length = end - start;
9120 if (start < 0)
9121 start = 0;
9122 else if (start > top_range)
9123 start = top_range;
9124 end = start + length;
9126 if (end < start)
9127 end = start;
9128 else if (end > top_range && ! dragging)
9129 end = top_range;
9132 /* Store the adjusted setting in the scroll bar. */
9133 XSETINT (bar->start, start);
9134 XSETINT (bar->end, end);
9136 /* Clip the end position, just for display. */
9137 if (end > top_range)
9138 end = top_range;
9140 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
9141 below top positions, to make sure the handle is always at least
9142 that many pixels tall. */
9143 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
9145 /* Draw the empty space above the handle. Note that we can't clear
9146 zero-height areas; that means "clear to end of window." */
9147 if (0 < start)
9148 x_clear_area (FRAME_X_DISPLAY (f), w,
9149 /* x, y, width, height, and exposures. */
9150 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9151 VERTICAL_SCROLL_BAR_TOP_BORDER,
9152 inside_width, start,
9153 False);
9155 /* Change to proper foreground color if one is specified. */
9156 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9157 XSetForeground (FRAME_X_DISPLAY (f), gc,
9158 f->output_data.x->scroll_bar_foreground_pixel);
9160 /* Draw the handle itself. */
9161 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
9162 /* x, y, width, height */
9163 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9164 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
9165 inside_width, end - start);
9167 /* Restore the foreground color of the GC if we changed it above. */
9168 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9169 XSetForeground (FRAME_X_DISPLAY (f), gc,
9170 f->output_data.x->foreground_pixel);
9172 /* Draw the empty space below the handle. Note that we can't
9173 clear zero-height areas; that means "clear to end of window." */
9174 if (end < inside_height)
9175 x_clear_area (FRAME_X_DISPLAY (f), w,
9176 /* x, y, width, height, and exposures. */
9177 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9178 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
9179 inside_width, inside_height - end,
9180 False);
9184 UNBLOCK_INPUT;
9187 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9189 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
9190 nil. */
9192 static void
9193 x_scroll_bar_remove (bar)
9194 struct scroll_bar *bar;
9196 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9197 BLOCK_INPUT;
9199 #ifdef USE_TOOLKIT_SCROLL_BARS
9200 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
9201 #else
9202 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9203 #endif
9205 /* Disassociate this scroll bar from its window. */
9206 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
9208 UNBLOCK_INPUT;
9212 /* Set the handle of the vertical scroll bar for WINDOW to indicate
9213 that we are displaying PORTION characters out of a total of WHOLE
9214 characters, starting at POSITION. If WINDOW has no scroll bar,
9215 create one. */
9217 static void
9218 XTset_vertical_scroll_bar (w, portion, whole, position)
9219 struct window *w;
9220 int portion, whole, position;
9222 struct frame *f = XFRAME (w->frame);
9223 struct scroll_bar *bar;
9224 int top, height, left, sb_left, width, sb_width;
9225 int window_x, window_y, window_width, window_height;
9227 /* Get window dimensions. */
9228 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
9229 top = window_y;
9230 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9231 height = window_height;
9233 /* Compute the left edge of the scroll bar area. */
9234 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9235 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
9236 else
9237 left = XFASTINT (w->left);
9238 left *= CANON_X_UNIT (f);
9239 left += FRAME_INTERNAL_BORDER_WIDTH (f);
9241 /* Compute the width of the scroll bar which might be less than
9242 the width of the area reserved for the scroll bar. */
9243 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
9244 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
9245 else
9246 sb_width = width;
9248 /* Compute the left edge of the scroll bar. */
9249 #ifdef USE_TOOLKIT_SCROLL_BARS
9250 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9251 sb_left = left + width - sb_width - (width - sb_width) / 2;
9252 else
9253 sb_left = left + (width - sb_width) / 2;
9254 #else
9255 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9256 sb_left = left + width - sb_width;
9257 else
9258 sb_left = left;
9259 #endif
9261 /* Does the scroll bar exist yet? */
9262 if (NILP (w->vertical_scroll_bar))
9264 if (width > 0 && height > 0)
9266 BLOCK_INPUT;
9267 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9268 left, top, width, height, False);
9269 UNBLOCK_INPUT;
9272 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
9274 else
9276 /* It may just need to be moved and resized. */
9277 unsigned int mask = 0;
9279 bar = XSCROLL_BAR (w->vertical_scroll_bar);
9281 BLOCK_INPUT;
9283 if (sb_left != XINT (bar->left))
9284 mask |= CWX;
9285 if (top != XINT (bar->top))
9286 mask |= CWY;
9287 if (sb_width != XINT (bar->width))
9288 mask |= CWWidth;
9289 if (height != XINT (bar->height))
9290 mask |= CWHeight;
9292 #ifdef USE_TOOLKIT_SCROLL_BARS
9294 /* Since toolkit scroll bars are smaller than the space reserved
9295 for them on the frame, we have to clear "under" them. */
9296 if (width > 0 && height > 0)
9297 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9298 left, top, width, height, False);
9300 /* Move/size the scroll bar widget. */
9301 if (mask)
9302 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
9303 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9304 top,
9305 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9306 max (height, 1), 0);
9308 #else /* not USE_TOOLKIT_SCROLL_BARS */
9310 /* Clear areas not covered by the scroll bar because of
9311 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
9312 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
9314 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9315 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9316 height, False);
9317 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9318 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9319 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9320 height, False);
9323 /* Clear areas not covered by the scroll bar because it's not as
9324 wide as the area reserved for it . This makes sure a
9325 previous mode line display is cleared after C-x 2 C-x 1, for
9326 example. */
9328 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9329 int rest = area_width - sb_width;
9330 if (rest > 0 && height > 0)
9332 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
9333 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9334 left + area_width - rest, top,
9335 rest, height, False);
9336 else
9337 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9338 left, top, rest, height, False);
9342 /* Move/size the scroll bar window. */
9343 if (mask)
9345 XWindowChanges wc;
9347 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9348 wc.y = top;
9349 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
9350 wc.height = height;
9351 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
9352 mask, &wc);
9355 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9357 /* Remember new settings. */
9358 XSETINT (bar->left, sb_left);
9359 XSETINT (bar->top, top);
9360 XSETINT (bar->width, sb_width);
9361 XSETINT (bar->height, height);
9363 UNBLOCK_INPUT;
9366 #ifdef USE_TOOLKIT_SCROLL_BARS
9367 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
9368 #else /* not USE_TOOLKIT_SCROLL_BARS */
9369 /* Set the scroll bar's current state, unless we're currently being
9370 dragged. */
9371 if (NILP (bar->dragging))
9373 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
9375 if (whole == 0)
9376 x_scroll_bar_set_handle (bar, 0, top_range, 0);
9377 else
9379 int start = ((double) position * top_range) / whole;
9380 int end = ((double) (position + portion) * top_range) / whole;
9381 x_scroll_bar_set_handle (bar, start, end, 0);
9384 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9386 XSETVECTOR (w->vertical_scroll_bar, bar);
9390 /* The following three hooks are used when we're doing a thorough
9391 redisplay of the frame. We don't explicitly know which scroll bars
9392 are going to be deleted, because keeping track of when windows go
9393 away is a real pain - "Can you say set-window-configuration, boys
9394 and girls?" Instead, we just assert at the beginning of redisplay
9395 that *all* scroll bars are to be removed, and then save a scroll bar
9396 from the fiery pit when we actually redisplay its window. */
9398 /* Arrange for all scroll bars on FRAME to be removed at the next call
9399 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
9400 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
9402 static void
9403 XTcondemn_scroll_bars (frame)
9404 FRAME_PTR frame;
9406 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
9407 while (! NILP (FRAME_SCROLL_BARS (frame)))
9409 Lisp_Object bar;
9410 bar = FRAME_SCROLL_BARS (frame);
9411 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
9412 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
9413 XSCROLL_BAR (bar)->prev = Qnil;
9414 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
9415 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
9416 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
9421 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
9422 Note that WINDOW isn't necessarily condemned at all. */
9424 static void
9425 XTredeem_scroll_bar (window)
9426 struct window *window;
9428 struct scroll_bar *bar;
9429 struct frame *f;
9431 /* We can't redeem this window's scroll bar if it doesn't have one. */
9432 if (NILP (window->vertical_scroll_bar))
9433 abort ();
9435 bar = XSCROLL_BAR (window->vertical_scroll_bar);
9437 /* Unlink it from the condemned list. */
9438 f = XFRAME (WINDOW_FRAME (window));
9439 if (NILP (bar->prev))
9441 /* If the prev pointer is nil, it must be the first in one of
9442 the lists. */
9443 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
9444 /* It's not condemned. Everything's fine. */
9445 return;
9446 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
9447 window->vertical_scroll_bar))
9448 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
9449 else
9450 /* If its prev pointer is nil, it must be at the front of
9451 one or the other! */
9452 abort ();
9454 else
9455 XSCROLL_BAR (bar->prev)->next = bar->next;
9457 if (! NILP (bar->next))
9458 XSCROLL_BAR (bar->next)->prev = bar->prev;
9460 bar->next = FRAME_SCROLL_BARS (f);
9461 bar->prev = Qnil;
9462 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9463 if (! NILP (bar->next))
9464 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9467 /* Remove all scroll bars on FRAME that haven't been saved since the
9468 last call to `*condemn_scroll_bars_hook'. */
9470 static void
9471 XTjudge_scroll_bars (f)
9472 FRAME_PTR f;
9474 Lisp_Object bar, next;
9476 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
9478 /* Clear out the condemned list now so we won't try to process any
9479 more events on the hapless scroll bars. */
9480 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
9482 for (; ! NILP (bar); bar = next)
9484 struct scroll_bar *b = XSCROLL_BAR (bar);
9486 x_scroll_bar_remove (b);
9488 next = b->next;
9489 b->next = b->prev = Qnil;
9492 /* Now there should be no references to the condemned scroll bars,
9493 and they should get garbage-collected. */
9497 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
9498 is a no-op when using toolkit scroll bars.
9500 This may be called from a signal handler, so we have to ignore GC
9501 mark bits. */
9503 static void
9504 x_scroll_bar_expose (bar, event)
9505 struct scroll_bar *bar;
9506 XEvent *event;
9508 #ifndef USE_TOOLKIT_SCROLL_BARS
9510 Window w = SCROLL_BAR_X_WINDOW (bar);
9511 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9512 GC gc = f->output_data.x->normal_gc;
9513 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9515 BLOCK_INPUT;
9517 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
9519 /* Draw a one-pixel border just inside the edges of the scroll bar. */
9520 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
9522 /* x, y, width, height */
9523 0, 0,
9524 XINT (bar->width) - 1 - width_trim - width_trim,
9525 XINT (bar->height) - 1);
9527 UNBLOCK_INPUT;
9529 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9532 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
9533 is set to something other than no_event, it is enqueued.
9535 This may be called from a signal handler, so we have to ignore GC
9536 mark bits. */
9538 #ifndef USE_TOOLKIT_SCROLL_BARS
9540 static void
9541 x_scroll_bar_handle_click (bar, event, emacs_event)
9542 struct scroll_bar *bar;
9543 XEvent *event;
9544 struct input_event *emacs_event;
9546 if (! GC_WINDOWP (bar->window))
9547 abort ();
9549 emacs_event->kind = scroll_bar_click;
9550 emacs_event->code = event->xbutton.button - Button1;
9551 emacs_event->modifiers
9552 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
9553 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
9554 event->xbutton.state)
9555 | (event->type == ButtonRelease
9556 ? up_modifier
9557 : down_modifier));
9558 emacs_event->frame_or_window = bar->window;
9559 emacs_event->arg = Qnil;
9560 emacs_event->timestamp = event->xbutton.time;
9562 #if 0
9563 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9564 int internal_height
9565 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9566 #endif
9567 int top_range
9568 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9569 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
9571 if (y < 0) y = 0;
9572 if (y > top_range) y = top_range;
9574 if (y < XINT (bar->start))
9575 emacs_event->part = scroll_bar_above_handle;
9576 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9577 emacs_event->part = scroll_bar_handle;
9578 else
9579 emacs_event->part = scroll_bar_below_handle;
9581 /* Just because the user has clicked on the handle doesn't mean
9582 they want to drag it. Lisp code needs to be able to decide
9583 whether or not we're dragging. */
9584 #if 0
9585 /* If the user has just clicked on the handle, record where they're
9586 holding it. */
9587 if (event->type == ButtonPress
9588 && emacs_event->part == scroll_bar_handle)
9589 XSETINT (bar->dragging, y - XINT (bar->start));
9590 #endif
9592 /* If the user has released the handle, set it to its final position. */
9593 if (event->type == ButtonRelease
9594 && ! NILP (bar->dragging))
9596 int new_start = y - XINT (bar->dragging);
9597 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9599 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9600 bar->dragging = Qnil;
9603 /* Same deal here as the other #if 0. */
9604 #if 0
9605 /* Clicks on the handle are always reported as occurring at the top of
9606 the handle. */
9607 if (emacs_event->part == scroll_bar_handle)
9608 emacs_event->x = bar->start;
9609 else
9610 XSETINT (emacs_event->x, y);
9611 #else
9612 XSETINT (emacs_event->x, y);
9613 #endif
9615 XSETINT (emacs_event->y, top_range);
9619 /* Handle some mouse motion while someone is dragging the scroll bar.
9621 This may be called from a signal handler, so we have to ignore GC
9622 mark bits. */
9624 static void
9625 x_scroll_bar_note_movement (bar, event)
9626 struct scroll_bar *bar;
9627 XEvent *event;
9629 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9631 last_mouse_movement_time = event->xmotion.time;
9633 f->mouse_moved = 1;
9634 XSETVECTOR (last_mouse_scroll_bar, bar);
9636 /* If we're dragging the bar, display it. */
9637 if (! GC_NILP (bar->dragging))
9639 /* Where should the handle be now? */
9640 int new_start = event->xmotion.y - XINT (bar->dragging);
9642 if (new_start != XINT (bar->start))
9644 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9646 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9651 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9653 /* Return information to the user about the current position of the mouse
9654 on the scroll bar. */
9656 static void
9657 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9658 FRAME_PTR *fp;
9659 Lisp_Object *bar_window;
9660 enum scroll_bar_part *part;
9661 Lisp_Object *x, *y;
9662 unsigned long *time;
9664 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
9665 Window w = SCROLL_BAR_X_WINDOW (bar);
9666 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9667 int win_x, win_y;
9668 Window dummy_window;
9669 int dummy_coord;
9670 unsigned int dummy_mask;
9672 BLOCK_INPUT;
9674 /* Get the mouse's position relative to the scroll bar window, and
9675 report that. */
9676 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
9678 /* Root, child, root x and root y. */
9679 &dummy_window, &dummy_window,
9680 &dummy_coord, &dummy_coord,
9682 /* Position relative to scroll bar. */
9683 &win_x, &win_y,
9685 /* Mouse buttons and modifier keys. */
9686 &dummy_mask))
9688 else
9690 #if 0
9691 int inside_height
9692 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9693 #endif
9694 int top_range
9695 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9697 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9699 if (! NILP (bar->dragging))
9700 win_y -= XINT (bar->dragging);
9702 if (win_y < 0)
9703 win_y = 0;
9704 if (win_y > top_range)
9705 win_y = top_range;
9707 *fp = f;
9708 *bar_window = bar->window;
9710 if (! NILP (bar->dragging))
9711 *part = scroll_bar_handle;
9712 else if (win_y < XINT (bar->start))
9713 *part = scroll_bar_above_handle;
9714 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9715 *part = scroll_bar_handle;
9716 else
9717 *part = scroll_bar_below_handle;
9719 XSETINT (*x, win_y);
9720 XSETINT (*y, top_range);
9722 f->mouse_moved = 0;
9723 last_mouse_scroll_bar = Qnil;
9726 *time = last_mouse_movement_time;
9728 UNBLOCK_INPUT;
9732 /* The screen has been cleared so we may have changed foreground or
9733 background colors, and the scroll bars may need to be redrawn.
9734 Clear out the scroll bars, and ask for expose events, so we can
9735 redraw them. */
9737 void
9738 x_scroll_bar_clear (f)
9739 FRAME_PTR f;
9741 #ifndef USE_TOOLKIT_SCROLL_BARS
9742 Lisp_Object bar;
9744 /* We can have scroll bars even if this is 0,
9745 if we just turned off scroll bar mode.
9746 But in that case we should not clear them. */
9747 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9748 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9749 bar = XSCROLL_BAR (bar)->next)
9750 XClearArea (FRAME_X_DISPLAY (f),
9751 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9752 0, 0, 0, 0, True);
9753 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9756 /* This processes Expose events from the menu-bar specific X event
9757 loop in xmenu.c. This allows to redisplay the frame if necessary
9758 when handling menu-bar or pop-up items. */
9761 process_expose_from_menu (event)
9762 XEvent event;
9764 FRAME_PTR f;
9765 struct x_display_info *dpyinfo;
9766 int frame_exposed_p = 0;
9768 BLOCK_INPUT;
9770 dpyinfo = x_display_info_for_display (event.xexpose.display);
9771 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9772 if (f)
9774 if (f->async_visible == 0)
9776 f->async_visible = 1;
9777 f->async_iconified = 0;
9778 f->output_data.x->has_been_visible = 1;
9779 SET_FRAME_GARBAGED (f);
9781 else
9783 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9784 event.xexpose.x, event.xexpose.y,
9785 event.xexpose.width, event.xexpose.height);
9786 frame_exposed_p = 1;
9789 else
9791 struct scroll_bar *bar
9792 = x_window_to_scroll_bar (event.xexpose.window);
9794 if (bar)
9795 x_scroll_bar_expose (bar, &event);
9798 UNBLOCK_INPUT;
9799 return frame_exposed_p;
9802 /* Define a queue to save up SelectionRequest events for later handling. */
9804 struct selection_event_queue
9806 XEvent event;
9807 struct selection_event_queue *next;
9810 static struct selection_event_queue *queue;
9812 /* Nonzero means queue up certain events--don't process them yet. */
9814 static int x_queue_selection_requests;
9816 /* Queue up an X event *EVENT, to be processed later. */
9818 static void
9819 x_queue_event (f, event)
9820 FRAME_PTR f;
9821 XEvent *event;
9823 struct selection_event_queue *queue_tmp
9824 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
9826 if (queue_tmp != NULL)
9828 queue_tmp->event = *event;
9829 queue_tmp->next = queue;
9830 queue = queue_tmp;
9834 /* Take all the queued events and put them back
9835 so that they get processed afresh. */
9837 static void
9838 x_unqueue_events (display)
9839 Display *display;
9841 while (queue != NULL)
9843 struct selection_event_queue *queue_tmp = queue;
9844 XPutBackEvent (display, &queue_tmp->event);
9845 queue = queue_tmp->next;
9846 xfree ((char *)queue_tmp);
9850 /* Start queuing SelectionRequest events. */
9852 void
9853 x_start_queuing_selection_requests (display)
9854 Display *display;
9856 x_queue_selection_requests++;
9859 /* Stop queuing SelectionRequest events. */
9861 void
9862 x_stop_queuing_selection_requests (display)
9863 Display *display;
9865 x_queue_selection_requests--;
9866 x_unqueue_events (display);
9869 /* The main X event-reading loop - XTread_socket. */
9871 #if 0
9872 /* Time stamp of enter window event. This is only used by XTread_socket,
9873 but we have to put it out here, since static variables within functions
9874 sometimes don't work. */
9876 static Time enter_timestamp;
9877 #endif
9879 /* This holds the state XLookupString needs to implement dead keys
9880 and other tricks known as "compose processing". _X Window System_
9881 says that a portable program can't use this, but Stephen Gildea assures
9882 me that letting the compiler initialize it to zeros will work okay.
9884 This must be defined outside of XTread_socket, for the same reasons
9885 given for enter_timestamp, above. */
9887 static XComposeStatus compose_status;
9889 /* Record the last 100 characters stored
9890 to help debug the loss-of-chars-during-GC problem. */
9892 static int temp_index;
9893 static short temp_buffer[100];
9895 /* Set this to nonzero to fake an "X I/O error"
9896 on a particular display. */
9898 struct x_display_info *XTread_socket_fake_io_error;
9900 /* When we find no input here, we occasionally do a no-op command
9901 to verify that the X server is still running and we can still talk with it.
9902 We try all the open displays, one by one.
9903 This variable is used for cycling thru the displays. */
9905 static struct x_display_info *next_noop_dpyinfo;
9907 #define SET_SAVED_MENU_EVENT(size) \
9908 do \
9910 if (f->output_data.x->saved_menu_event == 0) \
9911 f->output_data.x->saved_menu_event \
9912 = (XEvent *) xmalloc (sizeof (XEvent)); \
9913 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9914 if (numchars >= 1) \
9916 bufp->kind = menu_bar_activate_event; \
9917 XSETFRAME (bufp->frame_or_window, f); \
9918 bufp->arg = Qnil; \
9919 bufp++; \
9920 count++; \
9921 numchars--; \
9924 while (0)
9926 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9927 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9929 /* Read events coming from the X server.
9930 This routine is called by the SIGIO handler.
9931 We return as soon as there are no more events to be read.
9933 Events representing keys are stored in buffer BUFP,
9934 which can hold up to NUMCHARS characters.
9935 We return the number of characters stored into the buffer,
9936 thus pretending to be `read'.
9938 EXPECTED is nonzero if the caller knows input is available. */
9941 XTread_socket (sd, bufp, numchars, expected)
9942 register int sd;
9943 /* register */ struct input_event *bufp;
9944 /* register */ int numchars;
9945 int expected;
9947 int count = 0;
9948 int nbytes = 0;
9949 XEvent event;
9950 struct frame *f;
9951 int event_found = 0;
9952 struct x_display_info *dpyinfo;
9953 struct coding_system coding;
9955 if (interrupt_input_blocked)
9957 interrupt_input_pending = 1;
9958 return -1;
9961 interrupt_input_pending = 0;
9962 BLOCK_INPUT;
9964 /* So people can tell when we have read the available input. */
9965 input_signal_count++;
9967 if (numchars <= 0)
9968 abort (); /* Don't think this happens. */
9970 ++handling_signal;
9972 /* The input should be decoded if it is from XIM. Currently the
9973 locale of XIM is the same as that of the system. So, we can use
9974 Vlocale_coding_system which is initialized properly at Emacs
9975 startup time. */
9976 setup_coding_system (Vlocale_coding_system, &coding);
9977 coding.src_multibyte = 0;
9978 coding.dst_multibyte = 1;
9979 /* The input is converted to events, thus we can't handle
9980 composition. Anyway, there's no XIM that gives us composition
9981 information. */
9982 coding.common_flags &= ~CODING_ANNOTATION_MASK;
9984 /* Find the display we are supposed to read input for.
9985 It's the one communicating on descriptor SD. */
9986 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9988 #if 0 /* This ought to be unnecessary; let's verify it. */
9989 #ifdef FIOSNBIO
9990 /* If available, Xlib uses FIOSNBIO to make the socket
9991 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
9992 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
9993 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
9994 fcntl (dpyinfo->connection, F_SETFL, 0);
9995 #endif /* ! defined (FIOSNBIO) */
9996 #endif
9998 #if 0 /* This code can't be made to work, with multiple displays,
9999 and appears not to be used on any system any more.
10000 Also keyboard.c doesn't turn O_NDELAY on and off
10001 for X connections. */
10002 #ifndef SIGIO
10003 #ifndef HAVE_SELECT
10004 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
10006 extern int read_alarm_should_throw;
10007 read_alarm_should_throw = 1;
10008 XPeekEvent (dpyinfo->display, &event);
10009 read_alarm_should_throw = 0;
10011 #endif /* HAVE_SELECT */
10012 #endif /* SIGIO */
10013 #endif
10015 /* For debugging, this gives a way to fake an I/O error. */
10016 if (dpyinfo == XTread_socket_fake_io_error)
10018 XTread_socket_fake_io_error = 0;
10019 x_io_error_quitter (dpyinfo->display);
10022 while (XPending (dpyinfo->display))
10024 XNextEvent (dpyinfo->display, &event);
10026 #ifdef HAVE_X_I18N
10028 /* Filter events for the current X input method.
10029 XFilterEvent returns non-zero if the input method has
10030 consumed the event. We pass the frame's X window to
10031 XFilterEvent because that's the one for which the IC
10032 was created. */
10033 struct frame *f1 = x_any_window_to_frame (dpyinfo,
10034 event.xclient.window);
10035 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
10036 break;
10038 #endif
10039 event_found = 1;
10041 switch (event.type)
10043 case ClientMessage:
10045 if (event.xclient.message_type
10046 == dpyinfo->Xatom_wm_protocols
10047 && event.xclient.format == 32)
10049 if (event.xclient.data.l[0]
10050 == dpyinfo->Xatom_wm_take_focus)
10052 /* Use x_any_window_to_frame because this
10053 could be the shell widget window
10054 if the frame has no title bar. */
10055 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
10056 #ifdef HAVE_X_I18N
10057 /* Not quite sure this is needed -pd */
10058 if (f && FRAME_XIC (f))
10059 XSetICFocus (FRAME_XIC (f));
10060 #endif
10061 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
10062 instructs the WM to set the input focus automatically for
10063 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
10064 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
10065 it has set the focus. So, XSetInputFocus below is not
10066 needed.
10068 The call to XSetInputFocus below has also caused trouble. In
10069 cases where the XSetInputFocus done by the WM and the one
10070 below are temporally close (on a fast machine), the call
10071 below can generate additional FocusIn events which confuse
10072 Emacs. */
10074 /* Since we set WM_TAKE_FOCUS, we must call
10075 XSetInputFocus explicitly. But not if f is null,
10076 since that might be an event for a deleted frame. */
10077 if (f)
10079 Display *d = event.xclient.display;
10080 /* Catch and ignore errors, in case window has been
10081 iconified by a window manager such as GWM. */
10082 int count = x_catch_errors (d);
10083 XSetInputFocus (d, event.xclient.window,
10084 /* The ICCCM says this is
10085 the only valid choice. */
10086 RevertToParent,
10087 event.xclient.data.l[1]);
10088 /* This is needed to detect the error
10089 if there is an error. */
10090 XSync (d, False);
10091 x_uncatch_errors (d, count);
10093 /* Not certain about handling scroll bars here */
10094 #endif /* 0 */
10096 else if (event.xclient.data.l[0]
10097 == dpyinfo->Xatom_wm_save_yourself)
10099 /* Save state modify the WM_COMMAND property to
10100 something which can reinstate us. This notifies
10101 the session manager, who's looking for such a
10102 PropertyNotify. Can restart processing when
10103 a keyboard or mouse event arrives. */
10104 if (numchars > 0)
10106 f = x_top_window_to_frame (dpyinfo,
10107 event.xclient.window);
10109 /* This is just so we only give real data once
10110 for a single Emacs process. */
10111 if (f == SELECTED_FRAME ())
10112 XSetCommand (FRAME_X_DISPLAY (f),
10113 event.xclient.window,
10114 initial_argv, initial_argc);
10115 else if (f)
10116 XSetCommand (FRAME_X_DISPLAY (f),
10117 event.xclient.window,
10118 0, 0);
10121 else if (event.xclient.data.l[0]
10122 == dpyinfo->Xatom_wm_delete_window)
10124 struct frame *f
10125 = x_any_window_to_frame (dpyinfo,
10126 event.xclient.window);
10128 if (f)
10130 if (numchars == 0)
10131 abort ();
10133 bufp->kind = delete_window_event;
10134 XSETFRAME (bufp->frame_or_window, f);
10135 bufp->arg = Qnil;
10136 bufp++;
10138 count += 1;
10139 numchars -= 1;
10143 else if (event.xclient.message_type
10144 == dpyinfo->Xatom_wm_configure_denied)
10147 else if (event.xclient.message_type
10148 == dpyinfo->Xatom_wm_window_moved)
10150 int new_x, new_y;
10151 struct frame *f
10152 = x_window_to_frame (dpyinfo, event.xclient.window);
10154 new_x = event.xclient.data.s[0];
10155 new_y = event.xclient.data.s[1];
10157 if (f)
10159 f->output_data.x->left_pos = new_x;
10160 f->output_data.x->top_pos = new_y;
10163 #ifdef HACK_EDITRES
10164 else if (event.xclient.message_type
10165 == dpyinfo->Xatom_editres)
10167 struct frame *f
10168 = x_any_window_to_frame (dpyinfo, event.xclient.window);
10169 _XEditResCheckMessages (f->output_data.x->widget, NULL,
10170 &event, NULL);
10172 #endif /* HACK_EDITRES */
10173 else if ((event.xclient.message_type
10174 == dpyinfo->Xatom_DONE)
10175 || (event.xclient.message_type
10176 == dpyinfo->Xatom_PAGE))
10178 /* Ghostview job completed. Kill it. We could
10179 reply with "Next" if we received "Page", but we
10180 currently never do because we are interested in
10181 images, only, which should have 1 page. */
10182 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
10183 struct frame *f
10184 = x_window_to_frame (dpyinfo, event.xclient.window);
10185 x_kill_gs_process (pixmap, f);
10186 expose_frame (f, 0, 0, 0, 0);
10188 #ifdef USE_TOOLKIT_SCROLL_BARS
10189 /* Scroll bar callbacks send a ClientMessage from which
10190 we construct an input_event. */
10191 else if (event.xclient.message_type
10192 == dpyinfo->Xatom_Scrollbar)
10194 x_scroll_bar_to_input_event (&event, bufp);
10195 ++bufp, ++count, --numchars;
10196 goto out;
10198 #endif /* USE_TOOLKIT_SCROLL_BARS */
10199 else
10200 goto OTHER;
10202 break;
10204 case SelectionNotify:
10205 #ifdef USE_X_TOOLKIT
10206 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
10207 goto OTHER;
10208 #endif /* not USE_X_TOOLKIT */
10209 x_handle_selection_notify (&event.xselection);
10210 break;
10212 case SelectionClear: /* Someone has grabbed ownership. */
10213 #ifdef USE_X_TOOLKIT
10214 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
10215 goto OTHER;
10216 #endif /* USE_X_TOOLKIT */
10218 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
10220 if (numchars == 0)
10221 abort ();
10223 bufp->kind = selection_clear_event;
10224 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10225 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10226 SELECTION_EVENT_TIME (bufp) = eventp->time;
10227 bufp->frame_or_window = Qnil;
10228 bufp->arg = Qnil;
10229 bufp++;
10231 count += 1;
10232 numchars -= 1;
10234 break;
10236 case SelectionRequest: /* Someone wants our selection. */
10237 #ifdef USE_X_TOOLKIT
10238 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
10239 goto OTHER;
10240 #endif /* USE_X_TOOLKIT */
10241 if (x_queue_selection_requests)
10242 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
10243 &event);
10244 else
10246 XSelectionRequestEvent *eventp
10247 = (XSelectionRequestEvent *) &event;
10249 if (numchars == 0)
10250 abort ();
10252 bufp->kind = selection_request_event;
10253 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10254 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
10255 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10256 SELECTION_EVENT_TARGET (bufp) = eventp->target;
10257 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
10258 SELECTION_EVENT_TIME (bufp) = eventp->time;
10259 bufp->frame_or_window = Qnil;
10260 bufp->arg = Qnil;
10261 bufp++;
10263 count += 1;
10264 numchars -= 1;
10266 break;
10268 case PropertyNotify:
10269 #if 0 /* This is plain wrong. In the case that we are waiting for a
10270 PropertyNotify used as an ACK in incremental selection
10271 transfer, the property will be on the receiver's window. */
10272 #if defined USE_X_TOOLKIT
10273 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
10274 goto OTHER;
10275 #endif
10276 #endif
10277 x_handle_property_notify (&event.xproperty);
10278 goto OTHER;
10280 case ReparentNotify:
10281 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
10282 if (f)
10284 int x, y;
10285 f->output_data.x->parent_desc = event.xreparent.parent;
10286 x_real_positions (f, &x, &y);
10287 f->output_data.x->left_pos = x;
10288 f->output_data.x->top_pos = y;
10290 break;
10292 case Expose:
10293 f = x_window_to_frame (dpyinfo, event.xexpose.window);
10294 if (f)
10296 x_check_fullscreen (f);
10298 if (f->async_visible == 0)
10300 f->async_visible = 1;
10301 f->async_iconified = 0;
10302 f->output_data.x->has_been_visible = 1;
10303 SET_FRAME_GARBAGED (f);
10305 else
10306 expose_frame (x_window_to_frame (dpyinfo,
10307 event.xexpose.window),
10308 event.xexpose.x, event.xexpose.y,
10309 event.xexpose.width, event.xexpose.height);
10311 else
10313 #ifndef USE_TOOLKIT_SCROLL_BARS
10314 struct scroll_bar *bar;
10315 #endif
10316 #if defined USE_LUCID
10317 /* Submenus of the Lucid menu bar aren't widgets
10318 themselves, so there's no way to dispatch events
10319 to them. Recognize this case separately. */
10321 Widget widget
10322 = x_window_to_menu_bar (event.xexpose.window);
10323 if (widget)
10324 xlwmenu_redisplay (widget);
10326 #endif /* USE_LUCID */
10328 #ifdef USE_TOOLKIT_SCROLL_BARS
10329 /* Dispatch event to the widget. */
10330 goto OTHER;
10331 #else /* not USE_TOOLKIT_SCROLL_BARS */
10332 bar = x_window_to_scroll_bar (event.xexpose.window);
10334 if (bar)
10335 x_scroll_bar_expose (bar, &event);
10336 #ifdef USE_X_TOOLKIT
10337 else
10338 goto OTHER;
10339 #endif /* USE_X_TOOLKIT */
10340 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10342 break;
10344 case GraphicsExpose: /* This occurs when an XCopyArea's
10345 source area was obscured or not
10346 available. */
10347 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
10348 if (f)
10350 expose_frame (f,
10351 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
10352 event.xgraphicsexpose.width,
10353 event.xgraphicsexpose.height);
10355 #ifdef USE_X_TOOLKIT
10356 else
10357 goto OTHER;
10358 #endif /* USE_X_TOOLKIT */
10359 break;
10361 case NoExpose: /* This occurs when an XCopyArea's
10362 source area was completely
10363 available. */
10364 break;
10366 case UnmapNotify:
10367 /* Redo the mouse-highlight after the tooltip has gone. */
10368 if (event.xmap.window == tip_window)
10370 tip_window = 0;
10371 redo_mouse_highlight ();
10374 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
10375 if (f) /* F may no longer exist if
10376 the frame was deleted. */
10378 /* While a frame is unmapped, display generation is
10379 disabled; you don't want to spend time updating a
10380 display that won't ever be seen. */
10381 f->async_visible = 0;
10382 /* We can't distinguish, from the event, whether the window
10383 has become iconified or invisible. So assume, if it
10384 was previously visible, than now it is iconified.
10385 But x_make_frame_invisible clears both
10386 the visible flag and the iconified flag;
10387 and that way, we know the window is not iconified now. */
10388 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
10390 f->async_iconified = 1;
10392 bufp->kind = iconify_event;
10393 XSETFRAME (bufp->frame_or_window, f);
10394 bufp->arg = Qnil;
10395 bufp++;
10396 count++;
10397 numchars--;
10400 goto OTHER;
10402 case MapNotify:
10403 if (event.xmap.window == tip_window)
10404 /* The tooltip has been drawn already. Avoid
10405 the SET_FRAME_GARBAGED below. */
10406 goto OTHER;
10408 /* We use x_top_window_to_frame because map events can
10409 come for sub-windows and they don't mean that the
10410 frame is visible. */
10411 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
10412 if (f)
10414 f->async_visible = 1;
10415 f->async_iconified = 0;
10416 f->output_data.x->has_been_visible = 1;
10418 /* wait_reading_process_input will notice this and update
10419 the frame's display structures. */
10420 SET_FRAME_GARBAGED (f);
10422 if (f->iconified)
10424 bufp->kind = deiconify_event;
10425 XSETFRAME (bufp->frame_or_window, f);
10426 bufp->arg = Qnil;
10427 bufp++;
10428 count++;
10429 numchars--;
10431 else if (! NILP (Vframe_list)
10432 && ! NILP (XCDR (Vframe_list)))
10433 /* Force a redisplay sooner or later
10434 to update the frame titles
10435 in case this is the second frame. */
10436 record_asynch_buffer_change ();
10438 goto OTHER;
10440 case KeyPress:
10441 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
10443 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
10445 dpyinfo->mouse_face_hidden = 1;
10446 clear_mouse_face (dpyinfo);
10449 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
10450 if (f == 0)
10452 /* Scroll bars consume key events, but we want
10453 the keys to go to the scroll bar's frame. */
10454 Widget widget = XtWindowToWidget (dpyinfo->display,
10455 event.xkey.window);
10456 if (widget && XmIsScrollBar (widget))
10458 widget = XtParent (widget);
10459 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
10462 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
10464 if (f != 0)
10466 KeySym keysym, orig_keysym;
10467 /* al%imercury@uunet.uu.net says that making this 81
10468 instead of 80 fixed a bug whereby meta chars made
10469 his Emacs hang.
10471 It seems that some version of XmbLookupString has
10472 a bug of not returning XBufferOverflow in
10473 status_return even if the input is too long to
10474 fit in 81 bytes. So, we must prepare sufficient
10475 bytes for copy_buffer. 513 bytes (256 chars for
10476 two-byte character set) seems to be a fairly good
10477 approximation. -- 2000.8.10 handa@etl.go.jp */
10478 unsigned char copy_buffer[513];
10479 unsigned char *copy_bufptr = copy_buffer;
10480 int copy_bufsiz = sizeof (copy_buffer);
10481 int modifiers;
10483 event.xkey.state
10484 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
10485 extra_keyboard_modifiers);
10486 modifiers = event.xkey.state;
10488 /* This will have to go some day... */
10490 /* make_lispy_event turns chars into control chars.
10491 Don't do it here because XLookupString is too eager. */
10492 event.xkey.state &= ~ControlMask;
10493 event.xkey.state &= ~(dpyinfo->meta_mod_mask
10494 | dpyinfo->super_mod_mask
10495 | dpyinfo->hyper_mod_mask
10496 | dpyinfo->alt_mod_mask);
10498 /* In case Meta is ComposeCharacter,
10499 clear its status. According to Markus Ehrnsperger
10500 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
10501 this enables ComposeCharacter to work whether or
10502 not it is combined with Meta. */
10503 if (modifiers & dpyinfo->meta_mod_mask)
10504 bzero (&compose_status, sizeof (compose_status));
10506 #ifdef HAVE_X_I18N
10507 if (FRAME_XIC (f))
10509 Status status_return;
10511 nbytes = XmbLookupString (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 = XmbLookupString (FRAME_XIC (f),
10520 &event.xkey, copy_bufptr,
10521 copy_bufsiz, &keysym,
10522 &status_return);
10525 if (status_return == XLookupNone)
10526 break;
10527 else if (status_return == XLookupChars)
10529 keysym = NoSymbol;
10530 modifiers = 0;
10532 else if (status_return != XLookupKeySym
10533 && status_return != XLookupBoth)
10534 abort ();
10536 else
10537 nbytes = XLookupString (&event.xkey, copy_bufptr,
10538 copy_bufsiz, &keysym,
10539 &compose_status);
10540 #else
10541 nbytes = XLookupString (&event.xkey, copy_bufptr,
10542 copy_bufsiz, &keysym,
10543 &compose_status);
10544 #endif
10546 orig_keysym = keysym;
10548 if (numchars > 1)
10550 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
10551 || keysym == XK_Delete
10552 #ifdef XK_ISO_Left_Tab
10553 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
10554 #endif
10555 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
10556 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
10557 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
10558 #ifdef HPUX
10559 /* This recognizes the "extended function keys".
10560 It seems there's no cleaner way.
10561 Test IsModifierKey to avoid handling mode_switch
10562 incorrectly. */
10563 || ((unsigned) (keysym) >= XK_Select
10564 && (unsigned)(keysym) < XK_KP_Space)
10565 #endif
10566 #ifdef XK_dead_circumflex
10567 || orig_keysym == XK_dead_circumflex
10568 #endif
10569 #ifdef XK_dead_grave
10570 || orig_keysym == XK_dead_grave
10571 #endif
10572 #ifdef XK_dead_tilde
10573 || orig_keysym == XK_dead_tilde
10574 #endif
10575 #ifdef XK_dead_diaeresis
10576 || orig_keysym == XK_dead_diaeresis
10577 #endif
10578 #ifdef XK_dead_macron
10579 || orig_keysym == XK_dead_macron
10580 #endif
10581 #ifdef XK_dead_degree
10582 || orig_keysym == XK_dead_degree
10583 #endif
10584 #ifdef XK_dead_acute
10585 || orig_keysym == XK_dead_acute
10586 #endif
10587 #ifdef XK_dead_cedilla
10588 || orig_keysym == XK_dead_cedilla
10589 #endif
10590 #ifdef XK_dead_breve
10591 || orig_keysym == XK_dead_breve
10592 #endif
10593 #ifdef XK_dead_ogonek
10594 || orig_keysym == XK_dead_ogonek
10595 #endif
10596 #ifdef XK_dead_caron
10597 || orig_keysym == XK_dead_caron
10598 #endif
10599 #ifdef XK_dead_doubleacute
10600 || orig_keysym == XK_dead_doubleacute
10601 #endif
10602 #ifdef XK_dead_abovedot
10603 || orig_keysym == XK_dead_abovedot
10604 #endif
10605 #ifdef XK_dead_abovering
10606 || orig_keysym == XK_dead_abovering
10607 #endif
10608 #ifdef XK_dead_iota
10609 || orig_keysym == XK_dead_iota
10610 #endif
10611 #ifdef XK_dead_belowdot
10612 || orig_keysym == XK_dead_belowdot
10613 #endif
10614 #ifdef XK_dead_voiced_sound
10615 || orig_keysym == XK_dead_voiced_sound
10616 #endif
10617 #ifdef XK_dead_semivoiced_sound
10618 || orig_keysym == XK_dead_semivoiced_sound
10619 #endif
10620 #ifdef XK_dead_hook
10621 || orig_keysym == XK_dead_hook
10622 #endif
10623 #ifdef XK_dead_horn
10624 || orig_keysym == XK_dead_horn
10625 #endif
10626 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10627 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10628 /* Any "vendor-specific" key is ok. */
10629 || (orig_keysym & (1 << 28))
10630 || (keysym != NoSymbol && nbytes == 0))
10631 && ! (IsModifierKey (orig_keysym)
10632 #ifndef HAVE_X11R5
10633 #ifdef XK_Mode_switch
10634 || ((unsigned)(orig_keysym) == XK_Mode_switch)
10635 #endif
10636 #ifdef XK_Num_Lock
10637 || ((unsigned)(orig_keysym) == XK_Num_Lock)
10638 #endif
10639 #endif /* not HAVE_X11R5 */
10642 if (temp_index == sizeof temp_buffer / sizeof (short))
10643 temp_index = 0;
10644 temp_buffer[temp_index++] = keysym;
10645 bufp->kind = non_ascii_keystroke;
10646 bufp->code = keysym;
10647 XSETFRAME (bufp->frame_or_window, f);
10648 bufp->arg = Qnil;
10649 bufp->modifiers
10650 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10651 modifiers);
10652 bufp->timestamp = event.xkey.time;
10653 bufp++;
10654 count++;
10655 numchars--;
10657 else if (numchars > nbytes)
10659 register int i;
10660 register int c;
10661 int nchars, len;
10663 for (i = 0; i < nbytes; i++)
10665 if (temp_index == (sizeof temp_buffer
10666 / sizeof (short)))
10667 temp_index = 0;
10668 temp_buffer[temp_index++] = copy_bufptr[i];
10671 if (/* If the event is not from XIM, */
10672 event.xkey.keycode != 0
10673 /* or the current locale doesn't request
10674 decoding of the input data, ... */
10675 || ! CODING_REQUIRE_DECODING (&coding))
10677 /* ... we can use the input data as is. */
10678 nchars = nbytes;
10680 else
10682 /* We have to decode the input data. */
10683 coding.destination
10684 = (unsigned char *) malloc (nbytes);
10685 if (! coding.destination)
10686 break;
10687 coding.dst_bytes = nbytes;
10688 coding.mode |= CODING_MODE_LAST_BLOCK;
10689 decode_coding_c_string (&coding, copy_bufptr,
10690 nbytes, Qnil);
10691 nbytes = coding.produced;
10692 nchars = coding.produced_char;
10693 if (copy_bufsiz < nbytes)
10695 copy_bufsiz = nbytes;
10696 copy_bufptr = (char *) alloca (nbytes);
10698 bcopy (coding.destination, copy_bufptr, nbytes);
10699 free (coding.destination);
10702 /* Convert the input data to a sequence of
10703 character events. */
10704 for (i = 0; i < nbytes; i += len)
10706 if (nchars == nbytes)
10707 c = copy_bufptr[i], len = 1;
10708 else
10709 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10710 nbytes - i, len);
10712 bufp->kind = (ASCII_CHAR_P (c)
10713 ? ascii_keystroke
10714 : multibyte_char_keystroke);
10715 bufp->code = c;
10716 XSETFRAME (bufp->frame_or_window, f);
10717 bufp->arg = Qnil;
10718 bufp->modifiers
10719 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10720 modifiers);
10721 bufp->timestamp = event.xkey.time;
10722 bufp++;
10725 count += nchars;
10726 numchars -= nchars;
10728 if (keysym == NoSymbol)
10729 break;
10731 else
10732 abort ();
10734 else
10735 abort ();
10737 #ifdef HAVE_X_I18N
10738 /* Don't dispatch this event since XtDispatchEvent calls
10739 XFilterEvent, and two calls in a row may freeze the
10740 client. */
10741 break;
10742 #else
10743 goto OTHER;
10744 #endif
10746 case KeyRelease:
10747 #ifdef HAVE_X_I18N
10748 /* Don't dispatch this event since XtDispatchEvent calls
10749 XFilterEvent, and two calls in a row may freeze the
10750 client. */
10751 break;
10752 #else
10753 goto OTHER;
10754 #endif
10756 /* Here's a possible interpretation of the whole
10757 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
10758 you get a FocusIn event, you have to get a FocusOut
10759 event before you relinquish the focus. If you
10760 haven't received a FocusIn event, then a mere
10761 LeaveNotify is enough to free you. */
10763 case EnterNotify:
10765 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10767 #if 0
10768 if (event.xcrossing.focus)
10770 /* Avoid nasty pop/raise loops. */
10771 if (f && (!(f->auto_raise)
10772 || !(f->auto_lower)
10773 || (event.xcrossing.time - enter_timestamp) > 500))
10775 x_new_focus_frame (dpyinfo, f);
10776 enter_timestamp = event.xcrossing.time;
10779 else if (f == dpyinfo->x_focus_frame)
10780 x_new_focus_frame (dpyinfo, 0);
10781 #endif
10783 /* EnterNotify counts as mouse movement,
10784 so update things that depend on mouse position. */
10785 if (f && !f->output_data.x->hourglass_p)
10786 note_mouse_movement (f, &event.xmotion);
10787 goto OTHER;
10790 case FocusIn:
10791 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10792 if (event.xfocus.detail != NotifyPointer)
10793 dpyinfo->x_focus_event_frame = f;
10794 if (f)
10796 x_new_focus_frame (dpyinfo, f);
10798 /* Don't stop displaying the initial startup message
10799 for a switch-frame event we don't need. */
10800 if (GC_NILP (Vterminal_frame)
10801 && GC_CONSP (Vframe_list)
10802 && !GC_NILP (XCDR (Vframe_list)))
10804 bufp->kind = FOCUS_IN_EVENT;
10805 XSETFRAME (bufp->frame_or_window, f);
10806 bufp->arg = Qnil;
10807 ++bufp, ++count, --numchars;
10811 #ifdef HAVE_X_I18N
10812 if (f && FRAME_XIC (f))
10813 XSetICFocus (FRAME_XIC (f));
10814 #endif
10816 goto OTHER;
10818 case LeaveNotify:
10819 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
10820 if (f)
10822 if (f == dpyinfo->mouse_face_mouse_frame)
10824 /* If we move outside the frame, then we're
10825 certainly no longer on any text in the frame. */
10826 clear_mouse_face (dpyinfo);
10827 dpyinfo->mouse_face_mouse_frame = 0;
10830 /* Generate a nil HELP_EVENT to cancel a help-echo.
10831 Do it only if there's something to cancel.
10832 Otherwise, the startup message is cleared when
10833 the mouse leaves the frame. */
10834 if (any_help_event_p)
10836 Lisp_Object frame;
10837 int n;
10839 XSETFRAME (frame, f);
10840 help_echo = Qnil;
10841 n = gen_help_event (bufp, numchars,
10842 Qnil, frame, Qnil, Qnil, 0);
10843 bufp += n, count += n, numchars -= n;
10846 #if 0
10847 if (event.xcrossing.focus)
10848 x_mouse_leave (dpyinfo);
10849 else
10851 if (f == dpyinfo->x_focus_event_frame)
10852 dpyinfo->x_focus_event_frame = 0;
10853 if (f == dpyinfo->x_focus_frame)
10854 x_new_focus_frame (dpyinfo, 0);
10856 #endif
10858 goto OTHER;
10860 case FocusOut:
10861 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10862 if (event.xfocus.detail != NotifyPointer
10863 && f == dpyinfo->x_focus_event_frame)
10864 dpyinfo->x_focus_event_frame = 0;
10865 if (f && f == dpyinfo->x_focus_frame)
10866 x_new_focus_frame (dpyinfo, 0);
10868 #ifdef HAVE_X_I18N
10869 if (f && FRAME_XIC (f))
10870 XUnsetICFocus (FRAME_XIC (f));
10871 #endif
10873 goto OTHER;
10875 case MotionNotify:
10877 previous_help_echo = help_echo;
10878 help_echo = help_echo_object = help_echo_window = Qnil;
10879 help_echo_pos = -1;
10881 if (dpyinfo->grabbed && last_mouse_frame
10882 && FRAME_LIVE_P (last_mouse_frame))
10883 f = last_mouse_frame;
10884 else
10885 f = x_window_to_frame (dpyinfo, event.xmotion.window);
10887 if (dpyinfo->mouse_face_hidden)
10889 dpyinfo->mouse_face_hidden = 0;
10890 clear_mouse_face (dpyinfo);
10893 if (f)
10894 note_mouse_movement (f, &event.xmotion);
10895 else
10897 #ifndef USE_TOOLKIT_SCROLL_BARS
10898 struct scroll_bar *bar
10899 = x_window_to_scroll_bar (event.xmotion.window);
10901 if (bar)
10902 x_scroll_bar_note_movement (bar, &event);
10903 #endif /* USE_TOOLKIT_SCROLL_BARS */
10905 /* If we move outside the frame, then we're
10906 certainly no longer on any text in the frame. */
10907 clear_mouse_face (dpyinfo);
10910 /* If the contents of the global variable help_echo
10911 has changed, generate a HELP_EVENT. */
10912 if (!NILP (help_echo)
10913 || !NILP (previous_help_echo))
10915 Lisp_Object frame;
10916 int n;
10918 if (f)
10919 XSETFRAME (frame, f);
10920 else
10921 frame = Qnil;
10923 any_help_event_p = 1;
10924 n = gen_help_event (bufp, numchars, help_echo, frame,
10925 help_echo_window, help_echo_object,
10926 help_echo_pos);
10927 bufp += n, count += n, numchars -= n;
10930 goto OTHER;
10933 case ConfigureNotify:
10934 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10935 if (f)
10937 #ifndef USE_X_TOOLKIT
10938 /* If there is a pending resize for fullscreen, don't
10939 do this one, the right one will come later.
10940 The toolkit version doesn't seem to need this, but we
10941 need to reset it below. */
10942 int dont_resize =
10943 ((f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
10944 && FRAME_NEW_WIDTH (f) != 0);
10945 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10946 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
10947 if (dont_resize)
10948 goto OTHER;
10950 /* In the toolkit version, change_frame_size
10951 is called by the code that handles resizing
10952 of the EmacsFrame widget. */
10954 /* Even if the number of character rows and columns has
10955 not changed, the font size may have changed, so we need
10956 to check the pixel dimensions as well. */
10957 if (columns != f->width
10958 || rows != f->height
10959 || event.xconfigure.width != f->output_data.x->pixel_width
10960 || event.xconfigure.height != f->output_data.x->pixel_height)
10962 change_frame_size (f, rows, columns, 0, 1, 0);
10963 SET_FRAME_GARBAGED (f);
10964 cancel_mouse_face (f);
10966 #endif
10968 f->output_data.x->pixel_width = event.xconfigure.width;
10969 f->output_data.x->pixel_height = event.xconfigure.height;
10971 /* What we have now is the position of Emacs's own window.
10972 Convert that to the position of the window manager window. */
10973 x_real_positions (f, &f->output_data.x->left_pos,
10974 &f->output_data.x->top_pos);
10976 x_check_fullscreen_move(f);
10977 if (f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
10978 f->output_data.x->want_fullscreen &=
10979 ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
10980 #ifdef HAVE_X_I18N
10981 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10982 xic_set_statusarea (f);
10983 #endif
10985 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10987 /* Since the WM decorations come below top_pos now,
10988 we must put them below top_pos in the future. */
10989 f->output_data.x->win_gravity = NorthWestGravity;
10990 x_wm_set_size_hint (f, (long) 0, 0);
10992 #ifdef USE_MOTIF
10993 /* Some window managers pass (0,0) as the location of
10994 the window, and the Motif event handler stores it
10995 in the emacs widget, which messes up Motif menus. */
10996 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10998 event.xconfigure.x = f->output_data.x->widget->core.x;
10999 event.xconfigure.y = f->output_data.x->widget->core.y;
11001 #endif /* USE_MOTIF */
11003 goto OTHER;
11005 case ButtonPress:
11006 case ButtonRelease:
11008 /* If we decide we want to generate an event to be seen
11009 by the rest of Emacs, we put it here. */
11010 struct input_event emacs_event;
11011 int tool_bar_p = 0;
11013 emacs_event.kind = no_event;
11014 bzero (&compose_status, sizeof (compose_status));
11016 if (dpyinfo->grabbed
11017 && last_mouse_frame
11018 && FRAME_LIVE_P (last_mouse_frame))
11019 f = last_mouse_frame;
11020 else
11021 f = x_window_to_frame (dpyinfo, event.xbutton.window);
11023 if (f)
11025 /* Is this in the tool-bar? */
11026 if (WINDOWP (f->tool_bar_window)
11027 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
11029 Lisp_Object window;
11030 int p, x, y;
11032 x = event.xbutton.x;
11033 y = event.xbutton.y;
11035 /* Set x and y. */
11036 window = window_from_coordinates (f, x, y, &p, 1);
11037 if (EQ (window, f->tool_bar_window))
11039 x_handle_tool_bar_click (f, &event.xbutton);
11040 tool_bar_p = 1;
11044 if (!tool_bar_p)
11045 if (!dpyinfo->x_focus_frame
11046 || f == dpyinfo->x_focus_frame)
11047 construct_mouse_click (&emacs_event, &event, f);
11049 else
11051 #ifndef USE_TOOLKIT_SCROLL_BARS
11052 struct scroll_bar *bar
11053 = x_window_to_scroll_bar (event.xbutton.window);
11055 if (bar)
11056 x_scroll_bar_handle_click (bar, &event, &emacs_event);
11057 #endif /* not USE_TOOLKIT_SCROLL_BARS */
11060 if (event.type == ButtonPress)
11062 dpyinfo->grabbed |= (1 << event.xbutton.button);
11063 last_mouse_frame = f;
11064 /* Ignore any mouse motion that happened
11065 before this event; any subsequent mouse-movement
11066 Emacs events should reflect only motion after
11067 the ButtonPress. */
11068 if (f != 0)
11069 f->mouse_moved = 0;
11071 if (!tool_bar_p)
11072 last_tool_bar_item = -1;
11074 else
11076 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
11079 if (numchars >= 1 && emacs_event.kind != no_event)
11081 bcopy (&emacs_event, bufp, sizeof (struct input_event));
11082 bufp++;
11083 count++;
11084 numchars--;
11087 #ifdef USE_X_TOOLKIT
11088 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
11089 /* For a down-event in the menu bar,
11090 don't pass it to Xt right now.
11091 Instead, save it away
11092 and we will pass it to Xt from kbd_buffer_get_event.
11093 That way, we can run some Lisp code first. */
11094 if (f && event.type == ButtonPress
11095 /* Verify the event is really within the menu bar
11096 and not just sent to it due to grabbing. */
11097 && event.xbutton.x >= 0
11098 && event.xbutton.x < f->output_data.x->pixel_width
11099 && event.xbutton.y >= 0
11100 && event.xbutton.y < f->output_data.x->menubar_height
11101 && event.xbutton.same_screen)
11103 SET_SAVED_BUTTON_EVENT;
11104 XSETFRAME (last_mouse_press_frame, f);
11106 else if (event.type == ButtonPress)
11108 last_mouse_press_frame = Qnil;
11109 goto OTHER;
11112 #ifdef USE_MOTIF /* This should do not harm for Lucid,
11113 but I am trying to be cautious. */
11114 else if (event.type == ButtonRelease)
11116 if (!NILP (last_mouse_press_frame))
11118 f = XFRAME (last_mouse_press_frame);
11119 if (f->output_data.x)
11120 SET_SAVED_BUTTON_EVENT;
11122 else
11123 goto OTHER;
11125 #endif /* USE_MOTIF */
11126 else
11127 goto OTHER;
11128 #endif /* USE_X_TOOLKIT */
11130 break;
11132 case CirculateNotify:
11133 goto OTHER;
11135 case CirculateRequest:
11136 goto OTHER;
11138 case VisibilityNotify:
11139 goto OTHER;
11141 case MappingNotify:
11142 /* Someone has changed the keyboard mapping - update the
11143 local cache. */
11144 switch (event.xmapping.request)
11146 case MappingModifier:
11147 x_find_modifier_meanings (dpyinfo);
11148 /* This is meant to fall through. */
11149 case MappingKeyboard:
11150 XRefreshKeyboardMapping (&event.xmapping);
11152 goto OTHER;
11154 default:
11155 OTHER:
11156 #ifdef USE_X_TOOLKIT
11157 BLOCK_INPUT;
11158 XtDispatchEvent (&event);
11159 UNBLOCK_INPUT;
11160 #endif /* USE_X_TOOLKIT */
11161 break;
11166 out:;
11168 /* On some systems, an X bug causes Emacs to get no more events
11169 when the window is destroyed. Detect that. (1994.) */
11170 if (! event_found)
11172 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
11173 One XNOOP in 100 loops will make Emacs terminate.
11174 B. Bretthauer, 1994 */
11175 x_noop_count++;
11176 if (x_noop_count >= 100)
11178 x_noop_count=0;
11180 if (next_noop_dpyinfo == 0)
11181 next_noop_dpyinfo = x_display_list;
11183 XNoOp (next_noop_dpyinfo->display);
11185 /* Each time we get here, cycle through the displays now open. */
11186 next_noop_dpyinfo = next_noop_dpyinfo->next;
11190 /* If the focus was just given to an auto-raising frame,
11191 raise it now. */
11192 /* ??? This ought to be able to handle more than one such frame. */
11193 if (pending_autoraise_frame)
11195 x_raise_frame (pending_autoraise_frame);
11196 pending_autoraise_frame = 0;
11199 UNBLOCK_INPUT;
11200 --handling_signal;
11201 return count;
11207 /***********************************************************************
11208 Text Cursor
11209 ***********************************************************************/
11211 /* Notice when the text cursor of window W has been completely
11212 overwritten by a drawing operation that outputs glyphs in AREA
11213 starting at X0 and ending at X1 in the line starting at Y0 and
11214 ending at Y1. X coordinates are area-relative. X1 < 0 means all
11215 the rest of the line after X0 has been written. Y coordinates
11216 are window-relative. */
11218 static void
11219 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
11220 struct window *w;
11221 enum glyph_row_area area;
11222 int x0, y0, x1, y1;
11224 if (area == TEXT_AREA
11225 && w->phys_cursor_on_p
11226 && y0 <= w->phys_cursor.y
11227 && y1 >= w->phys_cursor.y + w->phys_cursor_height
11228 && x0 <= w->phys_cursor.x
11229 && (x1 < 0 || x1 > w->phys_cursor.x))
11230 w->phys_cursor_on_p = 0;
11234 /* Set clipping for output in glyph row ROW. W is the window in which
11235 we operate. GC is the graphics context to set clipping in.
11236 WHOLE_LINE_P non-zero means include the areas used for truncation
11237 mark display and alike in the clipping rectangle.
11239 ROW may be a text row or, e.g., a mode line. Text rows must be
11240 clipped to the interior of the window dedicated to text display,
11241 mode lines must be clipped to the whole window. */
11243 static void
11244 x_clip_to_row (w, row, gc, whole_line_p)
11245 struct window *w;
11246 struct glyph_row *row;
11247 GC gc;
11248 int whole_line_p;
11250 struct frame *f = XFRAME (WINDOW_FRAME (w));
11251 XRectangle clip_rect;
11252 int window_x, window_y, window_width, window_height;
11254 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
11256 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
11257 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
11258 clip_rect.y = max (clip_rect.y, window_y);
11259 clip_rect.width = window_width;
11260 clip_rect.height = row->visible_height;
11262 /* If clipping to the whole line, including trunc marks, extend
11263 the rectangle to the left and increase its width. */
11264 if (whole_line_p)
11266 clip_rect.x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
11267 clip_rect.width += FRAME_X_FRINGE_WIDTH (f);
11270 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
11274 /* Draw a hollow box cursor on window W in glyph row ROW. */
11276 static void
11277 x_draw_hollow_cursor (w, row)
11278 struct window *w;
11279 struct glyph_row *row;
11281 struct frame *f = XFRAME (WINDOW_FRAME (w));
11282 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11283 Display *dpy = FRAME_X_DISPLAY (f);
11284 int x, y, wd, h;
11285 XGCValues xgcv;
11286 struct glyph *cursor_glyph;
11287 GC gc;
11289 /* Compute frame-relative coordinates from window-relative
11290 coordinates. */
11291 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11292 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
11293 + row->ascent - w->phys_cursor_ascent);
11294 h = row->height - 1;
11296 /* Get the glyph the cursor is on. If we can't tell because
11297 the current matrix is invalid or such, give up. */
11298 cursor_glyph = get_phys_cursor_glyph (w);
11299 if (cursor_glyph == NULL)
11300 return;
11302 /* Compute the width of the rectangle to draw. If on a stretch
11303 glyph, and `x-stretch-block-cursor' is nil, don't draw a
11304 rectangle as wide as the glyph, but use a canonical character
11305 width instead. */
11306 wd = cursor_glyph->pixel_width - 1;
11307 if (cursor_glyph->type == STRETCH_GLYPH
11308 && !x_stretch_cursor_p)
11309 wd = min (CANON_X_UNIT (f), wd);
11311 /* The foreground of cursor_gc is typically the same as the normal
11312 background color, which can cause the cursor box to be invisible. */
11313 xgcv.foreground = f->output_data.x->cursor_pixel;
11314 if (dpyinfo->scratch_cursor_gc)
11315 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
11316 else
11317 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
11318 GCForeground, &xgcv);
11319 gc = dpyinfo->scratch_cursor_gc;
11321 /* Set clipping, draw the rectangle, and reset clipping again. */
11322 x_clip_to_row (w, row, gc, 0);
11323 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
11324 XSetClipMask (dpy, gc, None);
11328 /* Draw a bar cursor on window W in glyph row ROW.
11330 Implementation note: One would like to draw a bar cursor with an
11331 angle equal to the one given by the font property XA_ITALIC_ANGLE.
11332 Unfortunately, I didn't find a font yet that has this property set.
11333 --gerd. */
11335 static void
11336 x_draw_bar_cursor (w, row, width)
11337 struct window *w;
11338 struct glyph_row *row;
11339 int width;
11341 struct frame *f = XFRAME (w->frame);
11342 struct glyph *cursor_glyph;
11344 /* If cursor is out of bounds, don't draw garbage. This can happen
11345 in mini-buffer windows when switching between echo area glyphs
11346 and mini-buffer. */
11347 cursor_glyph = get_phys_cursor_glyph (w);
11348 if (cursor_glyph == NULL)
11349 return;
11351 /* If on an image, draw like a normal cursor. That's usually better
11352 visible than drawing a bar, esp. if the image is large so that
11353 the bar might not be in the window. */
11354 if (cursor_glyph->type == IMAGE_GLYPH)
11356 struct glyph_row *row;
11357 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
11358 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
11360 else
11362 Display *dpy = FRAME_X_DISPLAY (f);
11363 Window window = FRAME_X_WINDOW (f);
11364 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
11365 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
11366 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
11367 XGCValues xgcv;
11369 /* If the glyph's background equals the color we normally draw
11370 the bar cursor in, the bar cursor in its normal color is
11371 invisible. Use the glyph's foreground color instead in this
11372 case, on the assumption that the glyph's colors are chosen so
11373 that the glyph is legible. */
11374 if (face->background == f->output_data.x->cursor_pixel)
11375 xgcv.background = xgcv.foreground = face->foreground;
11376 else
11377 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
11378 xgcv.graphics_exposures = 0;
11380 if (gc)
11381 XChangeGC (dpy, gc, mask, &xgcv);
11382 else
11384 gc = XCreateGC (dpy, window, mask, &xgcv);
11385 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
11388 if (width < 0)
11389 width = f->output_data.x->cursor_width;
11390 width = min (cursor_glyph->pixel_width, width);
11392 x_clip_to_row (w, row, gc, 0);
11393 XFillRectangle (dpy, window, gc,
11394 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11395 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
11396 width, row->height);
11397 XSetClipMask (dpy, gc, None);
11402 /* Clear the cursor of window W to background color, and mark the
11403 cursor as not shown. This is used when the text where the cursor
11404 is is about to be rewritten. */
11406 static void
11407 x_clear_cursor (w)
11408 struct window *w;
11410 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
11411 x_update_window_cursor (w, 0);
11415 /* Draw the cursor glyph of window W in glyph row ROW. See the
11416 comment of x_draw_glyphs for the meaning of HL. */
11418 static void
11419 x_draw_phys_cursor_glyph (w, row, hl)
11420 struct window *w;
11421 struct glyph_row *row;
11422 enum draw_glyphs_face hl;
11424 /* If cursor hpos is out of bounds, don't draw garbage. This can
11425 happen in mini-buffer windows when switching between echo area
11426 glyphs and mini-buffer. */
11427 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
11429 int on_p = w->phys_cursor_on_p;
11431 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
11432 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
11433 hl, 0);
11434 w->phys_cursor_on_p = on_p;
11436 /* When we erase the cursor, and ROW is overlapped by other
11437 rows, make sure that these overlapping parts of other rows
11438 are redrawn. */
11439 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
11441 if (row > w->current_matrix->rows
11442 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
11443 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
11445 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
11446 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
11447 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
11453 /* Erase the image of a cursor of window W from the screen. */
11455 static void
11456 x_erase_phys_cursor (w)
11457 struct window *w;
11459 struct frame *f = XFRAME (w->frame);
11460 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11461 int hpos = w->phys_cursor.hpos;
11462 int vpos = w->phys_cursor.vpos;
11463 int mouse_face_here_p = 0;
11464 struct glyph_matrix *active_glyphs = w->current_matrix;
11465 struct glyph_row *cursor_row;
11466 struct glyph *cursor_glyph;
11467 enum draw_glyphs_face hl;
11469 /* No cursor displayed or row invalidated => nothing to do on the
11470 screen. */
11471 if (w->phys_cursor_type == NO_CURSOR)
11472 goto mark_cursor_off;
11474 /* VPOS >= active_glyphs->nrows means that window has been resized.
11475 Don't bother to erase the cursor. */
11476 if (vpos >= active_glyphs->nrows)
11477 goto mark_cursor_off;
11479 /* If row containing cursor is marked invalid, there is nothing we
11480 can do. */
11481 cursor_row = MATRIX_ROW (active_glyphs, vpos);
11482 if (!cursor_row->enabled_p)
11483 goto mark_cursor_off;
11485 /* If row is completely invisible, don't attempt to delete a cursor which
11486 isn't there. This can happen if cursor is at top of a window, and
11487 we switch to a buffer with a header line in that window. */
11488 if (cursor_row->visible_height <= 0)
11489 goto mark_cursor_off;
11491 /* This can happen when the new row is shorter than the old one.
11492 In this case, either x_draw_glyphs or clear_end_of_line
11493 should have cleared the cursor. Note that we wouldn't be
11494 able to erase the cursor in this case because we don't have a
11495 cursor glyph at hand. */
11496 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
11497 goto mark_cursor_off;
11499 /* If the cursor is in the mouse face area, redisplay that when
11500 we clear the cursor. */
11501 if (! NILP (dpyinfo->mouse_face_window)
11502 && w == XWINDOW (dpyinfo->mouse_face_window)
11503 && (vpos > dpyinfo->mouse_face_beg_row
11504 || (vpos == dpyinfo->mouse_face_beg_row
11505 && hpos >= dpyinfo->mouse_face_beg_col))
11506 && (vpos < dpyinfo->mouse_face_end_row
11507 || (vpos == dpyinfo->mouse_face_end_row
11508 && hpos < dpyinfo->mouse_face_end_col))
11509 /* Don't redraw the cursor's spot in mouse face if it is at the
11510 end of a line (on a newline). The cursor appears there, but
11511 mouse highlighting does not. */
11512 && cursor_row->used[TEXT_AREA] > hpos)
11513 mouse_face_here_p = 1;
11515 /* Maybe clear the display under the cursor. */
11516 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
11518 int x;
11519 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11521 cursor_glyph = get_phys_cursor_glyph (w);
11522 if (cursor_glyph == NULL)
11523 goto mark_cursor_off;
11525 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11527 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11529 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
11530 cursor_row->y)),
11531 cursor_glyph->pixel_width,
11532 cursor_row->visible_height,
11533 False);
11536 /* Erase the cursor by redrawing the character underneath it. */
11537 if (mouse_face_here_p)
11538 hl = DRAW_MOUSE_FACE;
11539 else
11540 hl = DRAW_NORMAL_TEXT;
11541 x_draw_phys_cursor_glyph (w, cursor_row, hl);
11543 mark_cursor_off:
11544 w->phys_cursor_on_p = 0;
11545 w->phys_cursor_type = NO_CURSOR;
11549 /* Non-zero if physical cursor of window W is within mouse face. */
11551 static int
11552 cursor_in_mouse_face_p (w)
11553 struct window *w;
11555 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
11556 int in_mouse_face = 0;
11558 if (WINDOWP (dpyinfo->mouse_face_window)
11559 && XWINDOW (dpyinfo->mouse_face_window) == w)
11561 int hpos = w->phys_cursor.hpos;
11562 int vpos = w->phys_cursor.vpos;
11564 if (vpos >= dpyinfo->mouse_face_beg_row
11565 && vpos <= dpyinfo->mouse_face_end_row
11566 && (vpos > dpyinfo->mouse_face_beg_row
11567 || hpos >= dpyinfo->mouse_face_beg_col)
11568 && (vpos < dpyinfo->mouse_face_end_row
11569 || hpos < dpyinfo->mouse_face_end_col
11570 || dpyinfo->mouse_face_past_end))
11571 in_mouse_face = 1;
11574 return in_mouse_face;
11578 /* Display or clear cursor of window W. If ON is zero, clear the
11579 cursor. If it is non-zero, display the cursor. If ON is nonzero,
11580 where to put the cursor is specified by HPOS, VPOS, X and Y. */
11582 void
11583 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
11584 struct window *w;
11585 int on, hpos, vpos, x, y;
11587 struct frame *f = XFRAME (w->frame);
11588 int new_cursor_type;
11589 int new_cursor_width;
11590 struct glyph_matrix *current_glyphs;
11591 struct glyph_row *glyph_row;
11592 struct glyph *glyph;
11593 int cursor_non_selected;
11595 /* This is pointless on invisible frames, and dangerous on garbaged
11596 windows and frames; in the latter case, the frame or window may
11597 be in the midst of changing its size, and x and y may be off the
11598 window. */
11599 if (! FRAME_VISIBLE_P (f)
11600 || FRAME_GARBAGED_P (f)
11601 || vpos >= w->current_matrix->nrows
11602 || hpos >= w->current_matrix->matrix_w)
11603 return;
11605 /* If cursor is off and we want it off, return quickly. */
11606 if (!on && !w->phys_cursor_on_p)
11607 return;
11609 current_glyphs = w->current_matrix;
11610 glyph_row = MATRIX_ROW (current_glyphs, vpos);
11611 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
11613 /* If cursor row is not enabled, we don't really know where to
11614 display the cursor. */
11615 if (!glyph_row->enabled_p)
11617 w->phys_cursor_on_p = 0;
11618 return;
11621 xassert (interrupt_input_blocked);
11623 /* Set new_cursor_type to the cursor we want to be displayed. In a
11624 mini-buffer window, we want the cursor only to appear if we are
11625 reading input from this window. For the selected window, we want
11626 the cursor type given by the frame parameter. If explicitly
11627 marked off, draw no cursor. In all other cases, we want a hollow
11628 box cursor. */
11629 cursor_non_selected
11630 = !NILP (Fbuffer_local_value (Qcursor_in_non_selected_windows,
11631 w->buffer));
11632 new_cursor_width = -1;
11633 if (cursor_in_echo_area
11634 && FRAME_HAS_MINIBUF_P (f)
11635 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
11637 if (w == XWINDOW (echo_area_window))
11638 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11639 else if (cursor_non_selected)
11640 new_cursor_type = HOLLOW_BOX_CURSOR;
11641 else
11642 new_cursor_type = NO_CURSOR;
11644 else
11646 if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
11647 || w != XWINDOW (f->selected_window))
11649 if ((MINI_WINDOW_P (w) && minibuf_level == 0)
11650 || !cursor_non_selected
11651 || NILP (XBUFFER (w->buffer)->cursor_type))
11652 new_cursor_type = NO_CURSOR;
11653 else
11654 new_cursor_type = HOLLOW_BOX_CURSOR;
11656 else if (w->cursor_off_p)
11657 new_cursor_type = NO_CURSOR;
11658 else
11660 struct buffer *b = XBUFFER (w->buffer);
11662 if (EQ (b->cursor_type, Qt))
11663 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11664 else
11665 new_cursor_type = x_specified_cursor_type (b->cursor_type,
11666 &new_cursor_width);
11670 /* If cursor is currently being shown and we don't want it to be or
11671 it is in the wrong place, or the cursor type is not what we want,
11672 erase it. */
11673 if (w->phys_cursor_on_p
11674 && (!on
11675 || w->phys_cursor.x != x
11676 || w->phys_cursor.y != y
11677 || new_cursor_type != w->phys_cursor_type))
11678 x_erase_phys_cursor (w);
11680 /* If the cursor is now invisible and we want it to be visible,
11681 display it. */
11682 if (on && !w->phys_cursor_on_p)
11684 w->phys_cursor_ascent = glyph_row->ascent;
11685 w->phys_cursor_height = glyph_row->height;
11687 /* Set phys_cursor_.* before x_draw_.* is called because some
11688 of them may need the information. */
11689 w->phys_cursor.x = x;
11690 w->phys_cursor.y = glyph_row->y;
11691 w->phys_cursor.hpos = hpos;
11692 w->phys_cursor.vpos = vpos;
11693 w->phys_cursor_type = new_cursor_type;
11694 w->phys_cursor_on_p = 1;
11696 switch (new_cursor_type)
11698 case HOLLOW_BOX_CURSOR:
11699 x_draw_hollow_cursor (w, glyph_row);
11700 break;
11702 case FILLED_BOX_CURSOR:
11703 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
11704 break;
11706 case BAR_CURSOR:
11707 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
11708 break;
11710 case NO_CURSOR:
11711 break;
11713 default:
11714 abort ();
11717 #ifdef HAVE_X_I18N
11718 if (w == XWINDOW (f->selected_window))
11719 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
11720 xic_set_preeditarea (w, x, y);
11721 #endif
11724 #ifndef XFlush
11725 if (updating_frame != f)
11726 XFlush (FRAME_X_DISPLAY (f));
11727 #endif
11731 /* Display the cursor on window W, or clear it. X and Y are window
11732 relative pixel coordinates. HPOS and VPOS are glyph matrix
11733 positions. If W is not the selected window, display a hollow
11734 cursor. ON non-zero means display the cursor at X, Y which
11735 correspond to HPOS, VPOS, otherwise it is cleared. */
11737 void
11738 x_display_cursor (w, on, hpos, vpos, x, y)
11739 struct window *w;
11740 int on, hpos, vpos, x, y;
11742 BLOCK_INPUT;
11743 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
11744 UNBLOCK_INPUT;
11748 /* Display the cursor on window W, or clear it, according to ON_P.
11749 Don't change the cursor's position. */
11751 void
11752 x_update_cursor (f, on_p)
11753 struct frame *f;
11755 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11759 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
11760 in the window tree rooted at W. */
11762 static void
11763 x_update_cursor_in_window_tree (w, on_p)
11764 struct window *w;
11765 int on_p;
11767 while (w)
11769 if (!NILP (w->hchild))
11770 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11771 else if (!NILP (w->vchild))
11772 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11773 else
11774 x_update_window_cursor (w, on_p);
11776 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11781 /* Switch the display of W's cursor on or off, according to the value
11782 of ON. */
11784 static void
11785 x_update_window_cursor (w, on)
11786 struct window *w;
11787 int on;
11789 /* Don't update cursor in windows whose frame is in the process
11790 of being deleted. */
11791 if (w->current_matrix)
11793 BLOCK_INPUT;
11794 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11795 w->phys_cursor.x, w->phys_cursor.y);
11796 UNBLOCK_INPUT;
11803 /* Icons. */
11805 /* Make the x-window of frame F use the gnu icon bitmap. */
11808 x_bitmap_icon (f, file)
11809 struct frame *f;
11810 Lisp_Object file;
11812 int bitmap_id;
11814 if (FRAME_X_WINDOW (f) == 0)
11815 return 1;
11817 /* Free up our existing icon bitmap if any. */
11818 if (f->output_data.x->icon_bitmap > 0)
11819 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11820 f->output_data.x->icon_bitmap = 0;
11822 if (STRINGP (file))
11823 bitmap_id = x_create_bitmap_from_file (f, file);
11824 else
11826 /* Create the GNU bitmap if necessary. */
11827 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
11828 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11829 = x_create_bitmap_from_data (f, gnu_bits,
11830 gnu_width, gnu_height);
11832 /* The first time we create the GNU bitmap,
11833 this increments the ref-count one extra time.
11834 As a result, the GNU bitmap is never freed.
11835 That way, we don't have to worry about allocating it again. */
11836 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
11838 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
11841 x_wm_set_icon_pixmap (f, bitmap_id);
11842 f->output_data.x->icon_bitmap = bitmap_id;
11844 return 0;
11848 /* Make the x-window of frame F use a rectangle with text.
11849 Use ICON_NAME as the text. */
11852 x_text_icon (f, icon_name)
11853 struct frame *f;
11854 char *icon_name;
11856 if (FRAME_X_WINDOW (f) == 0)
11857 return 1;
11859 #ifdef HAVE_X11R4
11861 XTextProperty text;
11862 text.value = (unsigned char *) icon_name;
11863 text.encoding = XA_STRING;
11864 text.format = 8;
11865 text.nitems = strlen (icon_name);
11866 #ifdef USE_X_TOOLKIT
11867 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11868 &text);
11869 #else /* not USE_X_TOOLKIT */
11870 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11871 #endif /* not USE_X_TOOLKIT */
11873 #else /* not HAVE_X11R4 */
11874 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11875 #endif /* not HAVE_X11R4 */
11877 if (f->output_data.x->icon_bitmap > 0)
11878 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11879 f->output_data.x->icon_bitmap = 0;
11880 x_wm_set_icon_pixmap (f, 0);
11882 return 0;
11885 #define X_ERROR_MESSAGE_SIZE 200
11887 /* If non-nil, this should be a string.
11888 It means catch X errors and store the error message in this string. */
11890 static Lisp_Object x_error_message_string;
11892 /* An X error handler which stores the error message in
11893 x_error_message_string. This is called from x_error_handler if
11894 x_catch_errors is in effect. */
11896 static void
11897 x_error_catcher (display, error)
11898 Display *display;
11899 XErrorEvent *error;
11901 XGetErrorText (display, error->error_code,
11902 XSTRING (x_error_message_string)->data,
11903 X_ERROR_MESSAGE_SIZE);
11906 /* Begin trapping X errors for display DPY. Actually we trap X errors
11907 for all displays, but DPY should be the display you are actually
11908 operating on.
11910 After calling this function, X protocol errors no longer cause
11911 Emacs to exit; instead, they are recorded in the string
11912 stored in x_error_message_string.
11914 Calling x_check_errors signals an Emacs error if an X error has
11915 occurred since the last call to x_catch_errors or x_check_errors.
11917 Calling x_uncatch_errors resumes the normal error handling. */
11919 void x_check_errors ();
11920 static Lisp_Object x_catch_errors_unwind ();
11923 x_catch_errors (dpy)
11924 Display *dpy;
11926 int count = specpdl_ptr - specpdl;
11928 /* Make sure any errors from previous requests have been dealt with. */
11929 XSync (dpy, False);
11931 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11933 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11934 XSTRING (x_error_message_string)->data[0] = 0;
11936 return count;
11939 /* Unbind the binding that we made to check for X errors. */
11941 static Lisp_Object
11942 x_catch_errors_unwind (old_val)
11943 Lisp_Object old_val;
11945 x_error_message_string = old_val;
11946 return Qnil;
11949 /* If any X protocol errors have arrived since the last call to
11950 x_catch_errors or x_check_errors, signal an Emacs error using
11951 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11953 void
11954 x_check_errors (dpy, format)
11955 Display *dpy;
11956 char *format;
11958 /* Make sure to catch any errors incurred so far. */
11959 XSync (dpy, False);
11961 if (XSTRING (x_error_message_string)->data[0])
11962 error (format, XSTRING (x_error_message_string)->data);
11965 /* Nonzero if we had any X protocol errors
11966 since we did x_catch_errors on DPY. */
11969 x_had_errors_p (dpy)
11970 Display *dpy;
11972 /* Make sure to catch any errors incurred so far. */
11973 XSync (dpy, False);
11975 return XSTRING (x_error_message_string)->data[0] != 0;
11978 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11980 void
11981 x_clear_errors (dpy)
11982 Display *dpy;
11984 XSTRING (x_error_message_string)->data[0] = 0;
11987 /* Stop catching X protocol errors and let them make Emacs die.
11988 DPY should be the display that was passed to x_catch_errors.
11989 COUNT should be the value that was returned by
11990 the corresponding call to x_catch_errors. */
11992 void
11993 x_uncatch_errors (dpy, count)
11994 Display *dpy;
11995 int count;
11997 unbind_to (count, Qnil);
12000 #if 0
12001 static unsigned int x_wire_count;
12002 x_trace_wire ()
12004 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
12006 #endif /* ! 0 */
12009 /* Handle SIGPIPE, which can happen when the connection to a server
12010 simply goes away. SIGPIPE is handled by x_connection_signal.
12011 Don't need to do anything, because the write which caused the
12012 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
12013 which will do the appropriate cleanup for us. */
12015 static SIGTYPE
12016 x_connection_signal (signalnum) /* If we don't have an argument, */
12017 int signalnum; /* some compilers complain in signal calls. */
12019 #ifdef USG
12020 /* USG systems forget handlers when they are used;
12021 must reestablish each time */
12022 signal (signalnum, x_connection_signal);
12023 #endif /* USG */
12027 /************************************************************************
12028 Handling X errors
12029 ************************************************************************/
12031 /* Error message passed to x_connection_closed. */
12033 static char *error_msg;
12035 /* Function installed as fatal_error_signal_hook in
12036 x_connection_closed. Print the X error message, and exit normally,
12037 instead of dumping core when XtCloseDisplay fails. */
12039 static void
12040 x_fatal_error_signal ()
12042 fprintf (stderr, "%s\n", error_msg);
12043 exit (70);
12046 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
12047 the text of an error message that lead to the connection loss. */
12049 static SIGTYPE
12050 x_connection_closed (dpy, error_message)
12051 Display *dpy;
12052 char *error_message;
12054 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
12055 Lisp_Object frame, tail;
12056 int count;
12058 error_msg = (char *) alloca (strlen (error_message) + 1);
12059 strcpy (error_msg, error_message);
12060 handling_signal = 0;
12062 /* Prevent being called recursively because of an error condition
12063 below. Otherwise, we might end up with printing ``can't find per
12064 display information'' in the recursive call instead of printing
12065 the original message here. */
12066 count = x_catch_errors (dpy);
12068 /* We have to close the display to inform Xt that it doesn't
12069 exist anymore. If we don't, Xt will continue to wait for
12070 events from the display. As a consequence, a sequence of
12072 M-x make-frame-on-display RET :1 RET
12073 ...kill the new frame, so that we get an IO error...
12074 M-x make-frame-on-display RET :1 RET
12076 will indefinitely wait in Xt for events for display `:1', opened
12077 in the first class to make-frame-on-display.
12079 Closing the display is reported to lead to a bus error on
12080 OpenWindows in certain situations. I suspect that is a bug
12081 in OpenWindows. I don't know how to cicumvent it here. */
12083 #ifdef USE_X_TOOLKIT
12084 /* If DPYINFO is null, this means we didn't open the display
12085 in the first place, so don't try to close it. */
12086 if (dpyinfo)
12088 extern void (*fatal_error_signal_hook) P_ ((void));
12089 fatal_error_signal_hook = x_fatal_error_signal;
12090 XtCloseDisplay (dpy);
12091 fatal_error_signal_hook = NULL;
12093 #endif
12095 /* Indicate that this display is dead. */
12096 if (dpyinfo)
12097 dpyinfo->display = 0;
12099 /* First delete frames whose mini-buffers are on frames
12100 that are on the dead display. */
12101 FOR_EACH_FRAME (tail, frame)
12103 Lisp_Object minibuf_frame;
12104 minibuf_frame
12105 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
12106 if (FRAME_X_P (XFRAME (frame))
12107 && FRAME_X_P (XFRAME (minibuf_frame))
12108 && ! EQ (frame, minibuf_frame)
12109 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
12110 Fdelete_frame (frame, Qt);
12113 /* Now delete all remaining frames on the dead display.
12114 We are now sure none of these is used as the mini-buffer
12115 for another frame that we need to delete. */
12116 FOR_EACH_FRAME (tail, frame)
12117 if (FRAME_X_P (XFRAME (frame))
12118 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
12120 /* Set this to t so that Fdelete_frame won't get confused
12121 trying to find a replacement. */
12122 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
12123 Fdelete_frame (frame, Qt);
12126 if (dpyinfo)
12127 x_delete_display (dpyinfo);
12129 x_uncatch_errors (dpy, count);
12131 if (x_display_list == 0)
12133 fprintf (stderr, "%s\n", error_msg);
12134 shut_down_emacs (0, 0, Qnil);
12135 exit (70);
12138 /* Ordinary stack unwind doesn't deal with these. */
12139 #ifdef SIGIO
12140 sigunblock (sigmask (SIGIO));
12141 #endif
12142 sigunblock (sigmask (SIGALRM));
12143 TOTALLY_UNBLOCK_INPUT;
12145 clear_waiting_for_input ();
12146 error ("%s", error_msg);
12150 /* This is the usual handler for X protocol errors.
12151 It kills all frames on the display that we got the error for.
12152 If that was the only one, it prints an error message and kills Emacs. */
12154 static void
12155 x_error_quitter (display, error)
12156 Display *display;
12157 XErrorEvent *error;
12159 char buf[256], buf1[356];
12161 /* Note that there is no real way portable across R3/R4 to get the
12162 original error handler. */
12164 XGetErrorText (display, error->error_code, buf, sizeof (buf));
12165 sprintf (buf1, "X protocol error: %s on protocol request %d",
12166 buf, error->request_code);
12167 x_connection_closed (display, buf1);
12171 /* This is the first-level handler for X protocol errors.
12172 It calls x_error_quitter or x_error_catcher. */
12174 static int
12175 x_error_handler (display, error)
12176 Display *display;
12177 XErrorEvent *error;
12179 if (! NILP (x_error_message_string))
12180 x_error_catcher (display, error);
12181 else
12182 x_error_quitter (display, error);
12183 return 0;
12186 /* This is the handler for X IO errors, always.
12187 It kills all frames on the display that we lost touch with.
12188 If that was the only one, it prints an error message and kills Emacs. */
12190 static int
12191 x_io_error_quitter (display)
12192 Display *display;
12194 char buf[256];
12196 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
12197 x_connection_closed (display, buf);
12198 return 0;
12201 /* Changing the font of the frame. */
12203 /* Give frame F the font named FONTNAME as its default font, and
12204 return the full name of that font. FONTNAME may be a wildcard
12205 pattern; in that case, we choose some font that fits the pattern.
12206 The return value shows which font we chose. */
12208 Lisp_Object
12209 x_new_font (f, fontname)
12210 struct frame *f;
12211 register char *fontname;
12213 struct font_info *fontp
12214 = FS_LOAD_FONT (f, fontname);
12216 if (!fontp)
12217 return Qnil;
12219 f->output_data.x->font = (XFontStruct *) (fontp->font);
12220 f->output_data.x->baseline_offset = fontp->baseline_offset;
12221 f->output_data.x->fontset = -1;
12223 x_compute_fringe_widths (f, 1);
12225 /* Compute the scroll bar width in character columns. */
12226 if (f->scroll_bar_pixel_width > 0)
12228 int wid = FONT_WIDTH (f->output_data.x->font);
12229 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
12231 else
12233 int wid = FONT_WIDTH (f->output_data.x->font);
12234 f->scroll_bar_cols = (14 + wid - 1) / wid;
12237 /* Now make the frame display the given font. */
12238 if (FRAME_X_WINDOW (f) != 0)
12240 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
12241 f->output_data.x->font->fid);
12242 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
12243 f->output_data.x->font->fid);
12244 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
12245 f->output_data.x->font->fid);
12247 frame_update_line_height (f);
12249 /* Don't change the size of a tip frame; there's no point in
12250 doing it because it's done in Fx_show_tip, and it leads to
12251 problems because the tip frame has no widget. */
12252 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
12253 x_set_window_size (f, 0, f->width, f->height);
12255 else
12256 /* If we are setting a new frame's font for the first time,
12257 there are no faces yet, so this font's height is the line height. */
12258 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
12260 return build_string (fontp->full_name);
12263 /* Give frame F the fontset named FONTSETNAME as its default font, and
12264 return the full name of that fontset. FONTSETNAME may be a wildcard
12265 pattern; in that case, we choose some fontset that fits the pattern.
12266 The return value shows which fontset we chose. */
12268 Lisp_Object
12269 x_new_fontset (f, fontsetname)
12270 struct frame *f;
12271 char *fontsetname;
12273 int fontset = fs_query_fontset (build_string (fontsetname), 0);
12274 Lisp_Object result;
12276 if (fontset >= 0 && f->output_data.x->fontset == fontset)
12277 /* This fontset is already set in frame F. There's nothing more
12278 to do. */
12279 return fontset_name (fontset);
12281 if (fontset >= 0)
12282 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
12283 else
12284 result = x_new_font (f, fontsetname);
12286 if (!STRINGP (result))
12287 /* Can't load ASCII font. */
12288 return Qnil;
12290 if (fontset < 0)
12292 Lisp_Object func;
12294 func = intern ("create-fontset-from-ascii-font");
12295 if (! NILP (Ffboundp (func)))
12296 result = call2 (func, result, result);
12297 else
12298 Fnew_fontset (result,
12299 Fcons (Fcons (Qascii, Fcons (result, Qnil)), Qnil));
12300 fontset = fs_query_fontset (result, 0);
12303 /* Since x_new_font doesn't update any fontset information, do it now. */
12304 f->output_data.x->fontset = fontset;
12306 #ifdef HAVE_X_I18N
12307 if (FRAME_XIC (f)
12308 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
12309 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
12310 #endif
12312 return fontset_name (fontset);
12315 /* Compute actual fringe widths */
12317 void
12318 x_compute_fringe_widths (f, redraw)
12319 struct frame *f;
12320 int redraw;
12322 int o_left = f->output_data.x->left_fringe_width;
12323 int o_right = f->output_data.x->right_fringe_width;
12324 int o_cols = f->output_data.x->fringe_cols;
12326 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
12327 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
12328 int left_fringe_width, right_fringe_width;
12330 if (!NILP (left_fringe))
12331 left_fringe = Fcdr (left_fringe);
12332 if (!NILP (right_fringe))
12333 right_fringe = Fcdr (right_fringe);
12335 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
12336 XINT (left_fringe));
12337 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
12338 XINT (right_fringe));
12340 if (left_fringe_width || right_fringe_width)
12342 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
12343 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
12344 int conf_wid = left_wid + right_wid;
12345 int font_wid = FONT_WIDTH (f->output_data.x->font);
12346 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
12347 int real_wid = cols * font_wid;
12348 if (left_wid && right_wid)
12350 if (left_fringe_width < 0)
12352 /* Left fringe width is fixed, adjust right fringe if necessary */
12353 f->output_data.x->left_fringe_width = left_wid;
12354 f->output_data.x->right_fringe_width = real_wid - left_wid;
12356 else if (right_fringe_width < 0)
12358 /* Right fringe width is fixed, adjust left fringe if necessary */
12359 f->output_data.x->left_fringe_width = real_wid - right_wid;
12360 f->output_data.x->right_fringe_width = right_wid;
12362 else
12364 /* Adjust both fringes with an equal amount.
12365 Note that we are doing integer arithmetic here, so don't
12366 lose a pixel if the total width is an odd number. */
12367 int fill = real_wid - conf_wid;
12368 f->output_data.x->left_fringe_width = left_wid + fill/2;
12369 f->output_data.x->right_fringe_width = right_wid + fill - fill/2;
12372 else if (left_fringe_width)
12374 f->output_data.x->left_fringe_width = real_wid;
12375 f->output_data.x->right_fringe_width = 0;
12377 else
12379 f->output_data.x->left_fringe_width = 0;
12380 f->output_data.x->right_fringe_width = real_wid;
12382 f->output_data.x->fringe_cols = cols;
12383 f->output_data.x->fringes_extra = real_wid;
12385 else
12387 f->output_data.x->left_fringe_width = 0;
12388 f->output_data.x->right_fringe_width = 0;
12389 f->output_data.x->fringe_cols = 0;
12390 f->output_data.x->fringes_extra = 0;
12393 if (redraw && FRAME_VISIBLE_P (f))
12394 if (o_left != f->output_data.x->left_fringe_width ||
12395 o_right != f->output_data.x->right_fringe_width ||
12396 o_cols != f->output_data.x->fringe_cols)
12397 redraw_frame (f);
12400 /***********************************************************************
12401 X Input Methods
12402 ***********************************************************************/
12404 #ifdef HAVE_X_I18N
12406 #ifdef HAVE_X11R6
12408 /* XIM destroy callback function, which is called whenever the
12409 connection to input method XIM dies. CLIENT_DATA contains a
12410 pointer to the x_display_info structure corresponding to XIM. */
12412 static void
12413 xim_destroy_callback (xim, client_data, call_data)
12414 XIM xim;
12415 XPointer client_data;
12416 XPointer call_data;
12418 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
12419 Lisp_Object frame, tail;
12421 BLOCK_INPUT;
12423 /* No need to call XDestroyIC.. */
12424 FOR_EACH_FRAME (tail, frame)
12426 struct frame *f = XFRAME (frame);
12427 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
12429 FRAME_XIC (f) = NULL;
12430 if (FRAME_XIC_FONTSET (f))
12432 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
12433 FRAME_XIC_FONTSET (f) = NULL;
12438 /* No need to call XCloseIM. */
12439 dpyinfo->xim = NULL;
12440 XFree (dpyinfo->xim_styles);
12441 UNBLOCK_INPUT;
12444 #endif /* HAVE_X11R6 */
12446 /* Open the connection to the XIM server on display DPYINFO.
12447 RESOURCE_NAME is the resource name Emacs uses. */
12449 static void
12450 xim_open_dpy (dpyinfo, resource_name)
12451 struct x_display_info *dpyinfo;
12452 char *resource_name;
12454 #ifdef USE_XIM
12455 XIM xim;
12457 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
12458 dpyinfo->xim = xim;
12460 if (xim)
12462 #ifdef HAVE_X11R6
12463 XIMCallback destroy;
12464 #endif
12466 /* Get supported styles and XIM values. */
12467 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
12469 #ifdef HAVE_X11R6
12470 destroy.callback = xim_destroy_callback;
12471 destroy.client_data = (XPointer)dpyinfo;
12472 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
12473 #endif
12476 #else /* not USE_XIM */
12477 dpyinfo->xim = NULL;
12478 #endif /* not USE_XIM */
12482 #ifdef HAVE_X11R6_XIM
12484 struct xim_inst_t
12486 struct x_display_info *dpyinfo;
12487 char *resource_name;
12490 /* XIM instantiate callback function, which is called whenever an XIM
12491 server is available. DISPLAY is teh display of the XIM.
12492 CLIENT_DATA contains a pointer to an xim_inst_t structure created
12493 when the callback was registered. */
12495 static void
12496 xim_instantiate_callback (display, client_data, call_data)
12497 Display *display;
12498 XPointer client_data;
12499 XPointer call_data;
12501 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
12502 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
12504 /* We don't support multiple XIM connections. */
12505 if (dpyinfo->xim)
12506 return;
12508 xim_open_dpy (dpyinfo, xim_inst->resource_name);
12510 /* Create XIC for the existing frames on the same display, as long
12511 as they have no XIC. */
12512 if (dpyinfo->xim && dpyinfo->reference_count > 0)
12514 Lisp_Object tail, frame;
12516 BLOCK_INPUT;
12517 FOR_EACH_FRAME (tail, frame)
12519 struct frame *f = XFRAME (frame);
12521 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
12522 if (FRAME_XIC (f) == NULL)
12524 create_frame_xic (f);
12525 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
12526 xic_set_statusarea (f);
12527 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
12529 struct window *w = XWINDOW (f->selected_window);
12530 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
12535 UNBLOCK_INPUT;
12539 #endif /* HAVE_X11R6_XIM */
12542 /* Open a connection to the XIM server on display DPYINFO.
12543 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
12544 connection only at the first time. On X11R6, open the connection
12545 in the XIM instantiate callback function. */
12547 static void
12548 xim_initialize (dpyinfo, resource_name)
12549 struct x_display_info *dpyinfo;
12550 char *resource_name;
12552 #ifdef USE_XIM
12553 #ifdef HAVE_X11R6_XIM
12554 struct xim_inst_t *xim_inst;
12555 int len;
12557 dpyinfo->xim = NULL;
12558 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
12559 xim_inst->dpyinfo = dpyinfo;
12560 len = strlen (resource_name);
12561 xim_inst->resource_name = (char *) xmalloc (len + 1);
12562 bcopy (resource_name, xim_inst->resource_name, len + 1);
12563 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12564 resource_name, EMACS_CLASS,
12565 xim_instantiate_callback,
12566 /* Fixme: This is XPointer in
12567 XFree86 but (XPointer *) on
12568 Tru64, at least. */
12569 (XPointer) xim_inst);
12570 #else /* not HAVE_X11R6_XIM */
12571 dpyinfo->xim = NULL;
12572 xim_open_dpy (dpyinfo, resource_name);
12573 #endif /* not HAVE_X11R6_XIM */
12575 #else /* not USE_XIM */
12576 dpyinfo->xim = NULL;
12577 #endif /* not USE_XIM */
12581 /* Close the connection to the XIM server on display DPYINFO. */
12583 static void
12584 xim_close_dpy (dpyinfo)
12585 struct x_display_info *dpyinfo;
12587 #ifdef USE_XIM
12588 #ifdef HAVE_X11R6_XIM
12589 if (dpyinfo->display)
12590 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12591 NULL, EMACS_CLASS,
12592 xim_instantiate_callback, NULL);
12593 #endif /* not HAVE_X11R6_XIM */
12594 if (dpyinfo->display)
12595 XCloseIM (dpyinfo->xim);
12596 dpyinfo->xim = NULL;
12597 XFree (dpyinfo->xim_styles);
12598 #endif /* USE_XIM */
12601 #endif /* not HAVE_X11R6_XIM */
12605 /* Calculate the absolute position in frame F
12606 from its current recorded position values and gravity. */
12608 void
12609 x_calc_absolute_position (f)
12610 struct frame *f;
12612 Window child;
12613 int win_x = 0, win_y = 0;
12614 int flags = f->output_data.x->size_hint_flags;
12615 int this_window;
12617 /* We have nothing to do if the current position
12618 is already for the top-left corner. */
12619 if (! ((flags & XNegative) || (flags & YNegative)))
12620 return;
12622 #ifdef USE_X_TOOLKIT
12623 this_window = XtWindow (f->output_data.x->widget);
12624 #else
12625 this_window = FRAME_X_WINDOW (f);
12626 #endif
12628 /* Find the position of the outside upper-left corner of
12629 the inner window, with respect to the outer window.
12630 But do this only if we will need the results. */
12631 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
12633 int count;
12635 BLOCK_INPUT;
12636 count = x_catch_errors (FRAME_X_DISPLAY (f));
12637 while (1)
12639 x_clear_errors (FRAME_X_DISPLAY (f));
12640 XTranslateCoordinates (FRAME_X_DISPLAY (f),
12642 /* From-window, to-window. */
12643 this_window,
12644 f->output_data.x->parent_desc,
12646 /* From-position, to-position. */
12647 0, 0, &win_x, &win_y,
12649 /* Child of win. */
12650 &child);
12651 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12653 Window newroot, newparent = 0xdeadbeef;
12654 Window *newchildren;
12655 unsigned int nchildren;
12657 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
12658 &newparent, &newchildren, &nchildren))
12659 break;
12661 XFree ((char *) newchildren);
12663 f->output_data.x->parent_desc = newparent;
12665 else
12666 break;
12669 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12670 UNBLOCK_INPUT;
12673 /* Treat negative positions as relative to the leftmost bottommost
12674 position that fits on the screen. */
12675 if (flags & XNegative)
12676 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
12677 - 2 * f->output_data.x->border_width - win_x
12678 - PIXEL_WIDTH (f)
12679 + f->output_data.x->left_pos);
12682 int height = PIXEL_HEIGHT (f);
12684 #if defined USE_X_TOOLKIT && defined USE_MOTIF
12685 /* Something is fishy here. When using Motif, starting Emacs with
12686 `-g -0-0', the frame appears too low by a few pixels.
12688 This seems to be so because initially, while Emacs is starting,
12689 the column widget's height and the frame's pixel height are
12690 different. The column widget's height is the right one. In
12691 later invocations, when Emacs is up, the frame's pixel height
12692 is right, though.
12694 It's not obvious where the initial small difference comes from.
12695 2000-12-01, gerd. */
12697 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
12698 #endif
12700 if (flags & YNegative)
12701 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
12702 - 2 * f->output_data.x->border_width
12703 - win_y
12704 - height
12705 + f->output_data.x->top_pos);
12708 /* The left_pos and top_pos
12709 are now relative to the top and left screen edges,
12710 so the flags should correspond. */
12711 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12714 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
12715 to really change the position, and 0 when calling from
12716 x_make_frame_visible (in that case, XOFF and YOFF are the current
12717 position values). It is -1 when calling from x_set_frame_parameters,
12718 which means, do adjust for borders but don't change the gravity. */
12720 void
12721 x_set_offset (f, xoff, yoff, change_gravity)
12722 struct frame *f;
12723 register int xoff, yoff;
12724 int change_gravity;
12726 int modified_top, modified_left;
12728 if (change_gravity > 0)
12730 f->output_data.x->top_pos = yoff;
12731 f->output_data.x->left_pos = xoff;
12732 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12733 if (xoff < 0)
12734 f->output_data.x->size_hint_flags |= XNegative;
12735 if (yoff < 0)
12736 f->output_data.x->size_hint_flags |= YNegative;
12737 f->output_data.x->win_gravity = NorthWestGravity;
12739 x_calc_absolute_position (f);
12741 BLOCK_INPUT;
12742 x_wm_set_size_hint (f, (long) 0, 0);
12744 modified_left = f->output_data.x->left_pos;
12745 modified_top = f->output_data.x->top_pos;
12746 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
12747 this seems to be unnecessary and incorrect. rms, 4/17/97. */
12748 /* It is a mystery why we need to add the border_width here
12749 when the frame is already visible, but experiment says we do. */
12750 if (change_gravity != 0)
12752 modified_left += f->output_data.x->border_width;
12753 modified_top += f->output_data.x->border_width;
12755 #endif
12757 #ifdef USE_X_TOOLKIT
12758 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
12759 modified_left, modified_top);
12760 #else /* not USE_X_TOOLKIT */
12761 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12762 modified_left, modified_top);
12763 #endif /* not USE_X_TOOLKIT */
12764 UNBLOCK_INPUT;
12767 /* Check if we need to resize the frame due to a fullscreen request.
12768 If so needed, resize the frame. */
12769 static void
12770 x_check_fullscreen (f)
12771 struct frame *f;
12773 if (f->output_data.x->want_fullscreen & FULLSCREEN_BOTH)
12775 int width, height, ign;
12777 x_real_positions (f, &f->output_data.x->left_pos,
12778 &f->output_data.x->top_pos);
12780 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
12782 /* We do not need to move the window, it shall be taken care of
12783 when setting WM manager hints.
12784 If the frame is visible already, the position is checked by
12785 x_check_fullscreen_move. */
12786 if (f->width != width || f->height != height)
12788 change_frame_size (f, height, width, 0, 1, 0);
12789 SET_FRAME_GARBAGED (f);
12790 cancel_mouse_face (f);
12792 /* Wait for the change of frame size to occur */
12793 f->output_data.x->want_fullscreen |= FULLSCREEN_WAIT;
12799 /* If frame parameters are set after the frame is mapped, we need to move
12800 the window. This is done in xfns.c.
12801 Some window managers moves the window to the right position, some
12802 moves the outer window manager window to the specified position.
12803 Here we check that we are in the right spot. If not, make a second
12804 move, assuming we are dealing with the second kind of window manager. */
12805 static void
12806 x_check_fullscreen_move (f)
12807 struct frame *f;
12809 if (f->output_data.x->want_fullscreen & FULLSCREEN_MOVE_WAIT)
12811 int expect_top = f->output_data.x->top_pos;
12812 int expect_left = f->output_data.x->left_pos;
12814 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
12815 expect_top = 0;
12816 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
12817 expect_left = 0;
12819 if (expect_top != f->output_data.x->top_pos
12820 || expect_left != f->output_data.x->left_pos)
12821 x_set_offset (f, expect_left, expect_top, 1);
12823 /* Just do this once */
12824 f->output_data.x->want_fullscreen &= ~FULLSCREEN_MOVE_WAIT;
12829 /* Calculate fullscreen size. Return in *TOP_POS and *LEFT_POS the
12830 wanted positions of the WM window (not emacs window).
12831 Return in *WIDTH and *HEIGHT the wanted width and height of Emacs
12832 window (FRAME_X_WINDOW).
12834 void
12835 x_fullscreen_adjust (f, width, height, top_pos, left_pos)
12836 struct frame *f;
12837 int *width;
12838 int *height;
12839 int *top_pos;
12840 int *left_pos;
12842 int newwidth = f->width, newheight = f->height;
12844 *top_pos = f->output_data.x->top_pos;
12845 *left_pos = f->output_data.x->left_pos;
12847 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
12849 int ph;
12851 ph = FRAME_X_DISPLAY_INFO (f)->height;
12852 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
12853 ph = CHAR_TO_PIXEL_HEIGHT (f, newheight)
12854 - f->output_data.x->y_pixels_diff;
12855 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
12856 *top_pos = 0;
12859 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
12861 int pw;
12863 pw = FRAME_X_DISPLAY_INFO (f)->width;
12864 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
12865 pw = CHAR_TO_PIXEL_WIDTH (f, newwidth)
12866 - f->output_data.x->x_pixels_diff;
12867 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
12868 *left_pos = 0;
12871 *width = newwidth;
12872 *height = newheight;
12876 /* Change the size of frame F's X window to COLS/ROWS in the case F
12877 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
12878 top-left-corner window gravity for this size change and subsequent
12879 size changes. Otherwise we leave the window gravity unchanged. */
12881 static void
12882 x_set_window_size_1 (f, change_gravity, cols, rows)
12883 struct frame *f;
12884 int change_gravity;
12885 int cols, rows;
12887 int pixelwidth, pixelheight;
12889 check_frame_size (f, &rows, &cols);
12890 f->output_data.x->vertical_scroll_bar_extra
12891 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
12893 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
12894 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
12895 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
12897 x_compute_fringe_widths (f, 0);
12899 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
12900 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
12902 f->output_data.x->win_gravity = NorthWestGravity;
12903 x_wm_set_size_hint (f, (long) 0, 0);
12905 XSync (FRAME_X_DISPLAY (f), False);
12906 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12907 pixelwidth, pixelheight);
12909 /* Now, strictly speaking, we can't be sure that this is accurate,
12910 but the window manager will get around to dealing with the size
12911 change request eventually, and we'll hear how it went when the
12912 ConfigureNotify event gets here.
12914 We could just not bother storing any of this information here,
12915 and let the ConfigureNotify event set everything up, but that
12916 might be kind of confusing to the Lisp code, since size changes
12917 wouldn't be reported in the frame parameters until some random
12918 point in the future when the ConfigureNotify event arrives.
12920 We pass 1 for DELAY since we can't run Lisp code inside of
12921 a BLOCK_INPUT. */
12922 change_frame_size (f, rows, cols, 0, 1, 0);
12923 PIXEL_WIDTH (f) = pixelwidth;
12924 PIXEL_HEIGHT (f) = pixelheight;
12926 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
12927 receive in the ConfigureNotify event; if we get what we asked
12928 for, then the event won't cause the screen to become garbaged, so
12929 we have to make sure to do it here. */
12930 SET_FRAME_GARBAGED (f);
12932 XFlush (FRAME_X_DISPLAY (f));
12936 /* Call this to change the size of frame F's x-window.
12937 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
12938 for this size change and subsequent size changes.
12939 Otherwise we leave the window gravity unchanged. */
12941 void
12942 x_set_window_size (f, change_gravity, cols, rows)
12943 struct frame *f;
12944 int change_gravity;
12945 int cols, rows;
12947 BLOCK_INPUT;
12949 #ifdef USE_X_TOOLKIT
12951 if (f->output_data.x->widget != NULL)
12953 /* The x and y position of the widget is clobbered by the
12954 call to XtSetValues within EmacsFrameSetCharSize.
12955 This is a real kludge, but I don't understand Xt so I can't
12956 figure out a correct fix. Can anyone else tell me? -- rms. */
12957 int xpos = f->output_data.x->widget->core.x;
12958 int ypos = f->output_data.x->widget->core.y;
12959 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
12960 f->output_data.x->widget->core.x = xpos;
12961 f->output_data.x->widget->core.y = ypos;
12963 else
12964 x_set_window_size_1 (f, change_gravity, cols, rows);
12966 #else /* not USE_X_TOOLKIT */
12968 x_set_window_size_1 (f, change_gravity, cols, rows);
12970 #endif /* not USE_X_TOOLKIT */
12972 /* If cursor was outside the new size, mark it as off. */
12973 mark_window_cursors_off (XWINDOW (f->root_window));
12975 /* Clear out any recollection of where the mouse highlighting was,
12976 since it might be in a place that's outside the new frame size.
12977 Actually checking whether it is outside is a pain in the neck,
12978 so don't try--just let the highlighting be done afresh with new size. */
12979 cancel_mouse_face (f);
12981 UNBLOCK_INPUT;
12984 /* Mouse warping. */
12986 void
12987 x_set_mouse_position (f, x, y)
12988 struct frame *f;
12989 int x, y;
12991 int pix_x, pix_y;
12993 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
12994 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
12996 if (pix_x < 0) pix_x = 0;
12997 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
12999 if (pix_y < 0) pix_y = 0;
13000 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
13002 BLOCK_INPUT;
13004 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13005 0, 0, 0, 0, pix_x, pix_y);
13006 UNBLOCK_INPUT;
13009 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
13011 void
13012 x_set_mouse_pixel_position (f, pix_x, pix_y)
13013 struct frame *f;
13014 int pix_x, pix_y;
13016 BLOCK_INPUT;
13018 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13019 0, 0, 0, 0, pix_x, pix_y);
13020 UNBLOCK_INPUT;
13023 /* focus shifting, raising and lowering. */
13025 void
13026 x_focus_on_frame (f)
13027 struct frame *f;
13029 #if 0 /* This proves to be unpleasant. */
13030 x_raise_frame (f);
13031 #endif
13032 #if 0
13033 /* I don't think that the ICCCM allows programs to do things like this
13034 without the interaction of the window manager. Whatever you end up
13035 doing with this code, do it to x_unfocus_frame too. */
13036 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
13037 RevertToPointerRoot, CurrentTime);
13038 #endif /* ! 0 */
13041 void
13042 x_unfocus_frame (f)
13043 struct frame *f;
13045 #if 0
13046 /* Look at the remarks in x_focus_on_frame. */
13047 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
13048 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
13049 RevertToPointerRoot, CurrentTime);
13050 #endif /* ! 0 */
13053 /* Raise frame F. */
13055 void
13056 x_raise_frame (f)
13057 struct frame *f;
13059 if (f->async_visible)
13061 BLOCK_INPUT;
13062 #ifdef USE_X_TOOLKIT
13063 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
13064 #else /* not USE_X_TOOLKIT */
13065 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13066 #endif /* not USE_X_TOOLKIT */
13067 XFlush (FRAME_X_DISPLAY (f));
13068 UNBLOCK_INPUT;
13072 /* Lower frame F. */
13074 void
13075 x_lower_frame (f)
13076 struct frame *f;
13078 if (f->async_visible)
13080 BLOCK_INPUT;
13081 #ifdef USE_X_TOOLKIT
13082 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
13083 #else /* not USE_X_TOOLKIT */
13084 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13085 #endif /* not USE_X_TOOLKIT */
13086 XFlush (FRAME_X_DISPLAY (f));
13087 UNBLOCK_INPUT;
13091 static void
13092 XTframe_raise_lower (f, raise_flag)
13093 FRAME_PTR f;
13094 int raise_flag;
13096 if (raise_flag)
13097 x_raise_frame (f);
13098 else
13099 x_lower_frame (f);
13102 /* Change of visibility. */
13104 /* This tries to wait until the frame is really visible.
13105 However, if the window manager asks the user where to position
13106 the frame, this will return before the user finishes doing that.
13107 The frame will not actually be visible at that time,
13108 but it will become visible later when the window manager
13109 finishes with it. */
13111 void
13112 x_make_frame_visible (f)
13113 struct frame *f;
13115 Lisp_Object type;
13116 int original_top, original_left;
13117 int retry_count = 2;
13119 retry:
13121 BLOCK_INPUT;
13123 type = x_icon_type (f);
13124 if (!NILP (type))
13125 x_bitmap_icon (f, type);
13127 if (! FRAME_VISIBLE_P (f))
13129 /* We test FRAME_GARBAGED_P here to make sure we don't
13130 call x_set_offset a second time
13131 if we get to x_make_frame_visible a second time
13132 before the window gets really visible. */
13133 if (! FRAME_ICONIFIED_P (f)
13134 && ! f->output_data.x->asked_for_visible)
13135 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13137 f->output_data.x->asked_for_visible = 1;
13139 if (! EQ (Vx_no_window_manager, Qt))
13140 x_wm_set_window_state (f, NormalState);
13141 #ifdef USE_X_TOOLKIT
13142 /* This was XtPopup, but that did nothing for an iconified frame. */
13143 XtMapWidget (f->output_data.x->widget);
13144 #else /* not USE_X_TOOLKIT */
13145 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13146 #endif /* not USE_X_TOOLKIT */
13147 #if 0 /* This seems to bring back scroll bars in the wrong places
13148 if the window configuration has changed. They seem
13149 to come back ok without this. */
13150 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
13151 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13152 #endif
13155 XFlush (FRAME_X_DISPLAY (f));
13157 /* Synchronize to ensure Emacs knows the frame is visible
13158 before we do anything else. We do this loop with input not blocked
13159 so that incoming events are handled. */
13161 Lisp_Object frame;
13162 int count;
13163 /* This must be before UNBLOCK_INPUT
13164 since events that arrive in response to the actions above
13165 will set it when they are handled. */
13166 int previously_visible = f->output_data.x->has_been_visible;
13168 original_left = f->output_data.x->left_pos;
13169 original_top = f->output_data.x->top_pos;
13171 /* This must come after we set COUNT. */
13172 UNBLOCK_INPUT;
13174 /* We unblock here so that arriving X events are processed. */
13176 /* Now move the window back to where it was "supposed to be".
13177 But don't do it if the gravity is negative.
13178 When the gravity is negative, this uses a position
13179 that is 3 pixels too low. Perhaps that's really the border width.
13181 Don't do this if the window has never been visible before,
13182 because the window manager may choose the position
13183 and we don't want to override it. */
13185 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
13186 && f->output_data.x->win_gravity == NorthWestGravity
13187 && previously_visible)
13189 Drawable rootw;
13190 int x, y;
13191 unsigned int width, height, border, depth;
13193 BLOCK_INPUT;
13195 /* On some window managers (such as FVWM) moving an existing
13196 window, even to the same place, causes the window manager
13197 to introduce an offset. This can cause the window to move
13198 to an unexpected location. Check the geometry (a little
13199 slow here) and then verify that the window is in the right
13200 place. If the window is not in the right place, move it
13201 there, and take the potential window manager hit. */
13202 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13203 &rootw, &x, &y, &width, &height, &border, &depth);
13205 if (original_left != x || original_top != y)
13206 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13207 original_left, original_top);
13209 UNBLOCK_INPUT;
13212 XSETFRAME (frame, f);
13214 /* Wait until the frame is visible. Process X events until a
13215 MapNotify event has been seen, or until we think we won't get a
13216 MapNotify at all.. */
13217 for (count = input_signal_count + 10;
13218 input_signal_count < count && !FRAME_VISIBLE_P (f);)
13220 /* Force processing of queued events. */
13221 x_sync (f);
13223 /* Machines that do polling rather than SIGIO have been
13224 observed to go into a busy-wait here. So we'll fake an
13225 alarm signal to let the handler know that there's something
13226 to be read. We used to raise a real alarm, but it seems
13227 that the handler isn't always enabled here. This is
13228 probably a bug. */
13229 if (input_polling_used ())
13231 /* It could be confusing if a real alarm arrives while
13232 processing the fake one. Turn it off and let the
13233 handler reset it. */
13234 extern void poll_for_input_1 P_ ((void));
13235 int old_poll_suppress_count = poll_suppress_count;
13236 poll_suppress_count = 1;
13237 poll_for_input_1 ();
13238 poll_suppress_count = old_poll_suppress_count;
13241 /* See if a MapNotify event has been processed. */
13242 FRAME_SAMPLE_VISIBILITY (f);
13245 /* 2000-09-28: In
13247 (let ((f (selected-frame)))
13248 (iconify-frame f)
13249 (raise-frame f))
13251 the frame is not raised with various window managers on
13252 FreeBSD, Linux and Solaris. It turns out that, for some
13253 unknown reason, the call to XtMapWidget is completely ignored.
13254 Mapping the widget a second time works. */
13256 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
13257 goto retry;
13261 /* Change from mapped state to withdrawn state. */
13263 /* Make the frame visible (mapped and not iconified). */
13265 void
13266 x_make_frame_invisible (f)
13267 struct frame *f;
13269 Window window;
13271 #ifdef USE_X_TOOLKIT
13272 /* Use the frame's outermost window, not the one we normally draw on. */
13273 window = XtWindow (f->output_data.x->widget);
13274 #else /* not USE_X_TOOLKIT */
13275 window = FRAME_X_WINDOW (f);
13276 #endif /* not USE_X_TOOLKIT */
13278 /* Don't keep the highlight on an invisible frame. */
13279 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13280 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
13282 #if 0/* This might add unreliability; I don't trust it -- rms. */
13283 if (! f->async_visible && ! f->async_iconified)
13284 return;
13285 #endif
13287 BLOCK_INPUT;
13289 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
13290 that the current position of the window is user-specified, rather than
13291 program-specified, so that when the window is mapped again, it will be
13292 placed at the same location, without forcing the user to position it
13293 by hand again (they have already done that once for this window.) */
13294 x_wm_set_size_hint (f, (long) 0, 1);
13296 #ifdef HAVE_X11R4
13298 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
13299 DefaultScreen (FRAME_X_DISPLAY (f))))
13301 UNBLOCK_INPUT_RESIGNAL;
13302 error ("Can't notify window manager of window withdrawal");
13304 #else /* ! defined (HAVE_X11R4) */
13306 /* Tell the window manager what we're going to do. */
13307 if (! EQ (Vx_no_window_manager, Qt))
13309 XEvent unmap;
13311 unmap.xunmap.type = UnmapNotify;
13312 unmap.xunmap.window = window;
13313 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
13314 unmap.xunmap.from_configure = False;
13315 if (! XSendEvent (FRAME_X_DISPLAY (f),
13316 DefaultRootWindow (FRAME_X_DISPLAY (f)),
13317 False,
13318 SubstructureRedirectMaskSubstructureNotifyMask,
13319 &unmap))
13321 UNBLOCK_INPUT_RESIGNAL;
13322 error ("Can't notify window manager of withdrawal");
13326 /* Unmap the window ourselves. Cheeky! */
13327 XUnmapWindow (FRAME_X_DISPLAY (f), window);
13328 #endif /* ! defined (HAVE_X11R4) */
13330 /* We can't distinguish this from iconification
13331 just by the event that we get from the server.
13332 So we can't win using the usual strategy of letting
13333 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
13334 and synchronize with the server to make sure we agree. */
13335 f->visible = 0;
13336 FRAME_ICONIFIED_P (f) = 0;
13337 f->async_visible = 0;
13338 f->async_iconified = 0;
13340 x_sync (f);
13342 UNBLOCK_INPUT;
13345 /* Change window state from mapped to iconified. */
13347 void
13348 x_iconify_frame (f)
13349 struct frame *f;
13351 int result;
13352 Lisp_Object type;
13354 /* Don't keep the highlight on an invisible frame. */
13355 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13356 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
13358 if (f->async_iconified)
13359 return;
13361 BLOCK_INPUT;
13363 FRAME_SAMPLE_VISIBILITY (f);
13365 type = x_icon_type (f);
13366 if (!NILP (type))
13367 x_bitmap_icon (f, type);
13369 #ifdef USE_X_TOOLKIT
13371 if (! FRAME_VISIBLE_P (f))
13373 if (! EQ (Vx_no_window_manager, Qt))
13374 x_wm_set_window_state (f, IconicState);
13375 /* This was XtPopup, but that did nothing for an iconified frame. */
13376 XtMapWidget (f->output_data.x->widget);
13377 /* The server won't give us any event to indicate
13378 that an invisible frame was changed to an icon,
13379 so we have to record it here. */
13380 f->iconified = 1;
13381 f->visible = 1;
13382 f->async_iconified = 1;
13383 f->async_visible = 0;
13384 UNBLOCK_INPUT;
13385 return;
13388 result = XIconifyWindow (FRAME_X_DISPLAY (f),
13389 XtWindow (f->output_data.x->widget),
13390 DefaultScreen (FRAME_X_DISPLAY (f)));
13391 UNBLOCK_INPUT;
13393 if (!result)
13394 error ("Can't notify window manager of iconification");
13396 f->async_iconified = 1;
13397 f->async_visible = 0;
13400 BLOCK_INPUT;
13401 XFlush (FRAME_X_DISPLAY (f));
13402 UNBLOCK_INPUT;
13403 #else /* not USE_X_TOOLKIT */
13405 /* Make sure the X server knows where the window should be positioned,
13406 in case the user deiconifies with the window manager. */
13407 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
13408 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13410 /* Since we don't know which revision of X we're running, we'll use both
13411 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
13413 /* X11R4: send a ClientMessage to the window manager using the
13414 WM_CHANGE_STATE type. */
13416 XEvent message;
13418 message.xclient.window = FRAME_X_WINDOW (f);
13419 message.xclient.type = ClientMessage;
13420 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
13421 message.xclient.format = 32;
13422 message.xclient.data.l[0] = IconicState;
13424 if (! XSendEvent (FRAME_X_DISPLAY (f),
13425 DefaultRootWindow (FRAME_X_DISPLAY (f)),
13426 False,
13427 SubstructureRedirectMask | SubstructureNotifyMask,
13428 &message))
13430 UNBLOCK_INPUT_RESIGNAL;
13431 error ("Can't notify window manager of iconification");
13435 /* X11R3: set the initial_state field of the window manager hints to
13436 IconicState. */
13437 x_wm_set_window_state (f, IconicState);
13439 if (!FRAME_VISIBLE_P (f))
13441 /* If the frame was withdrawn, before, we must map it. */
13442 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13445 f->async_iconified = 1;
13446 f->async_visible = 0;
13448 XFlush (FRAME_X_DISPLAY (f));
13449 UNBLOCK_INPUT;
13450 #endif /* not USE_X_TOOLKIT */
13454 /* Free X resources of frame F. */
13456 void
13457 x_free_frame_resources (f)
13458 struct frame *f;
13460 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13461 Lisp_Object bar;
13462 struct scroll_bar *b;
13464 BLOCK_INPUT;
13466 /* If a display connection is dead, don't try sending more
13467 commands to the X server. */
13468 if (dpyinfo->display)
13470 if (f->output_data.x->icon_desc)
13471 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
13473 #ifdef USE_X_TOOLKIT
13474 /* Explicitly destroy the scroll bars of the frame. Without
13475 this, we get "BadDrawable" errors from the toolkit later on,
13476 presumably from expose events generated for the disappearing
13477 toolkit scroll bars. */
13478 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
13480 b = XSCROLL_BAR (bar);
13481 x_scroll_bar_remove (b);
13483 #endif
13485 #ifdef HAVE_X_I18N
13486 if (FRAME_XIC (f))
13487 free_frame_xic (f);
13488 #endif
13490 #ifdef USE_X_TOOLKIT
13491 if (f->output_data.x->widget)
13493 XtDestroyWidget (f->output_data.x->widget);
13494 f->output_data.x->widget = NULL;
13496 /* Tooltips don't have widgets, only a simple X window, even if
13497 we are using a toolkit. */
13498 else if (FRAME_X_WINDOW (f))
13499 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13501 free_frame_menubar (f);
13502 #else /* !USE_X_TOOLKIT */
13503 if (FRAME_X_WINDOW (f))
13504 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13505 #endif /* !USE_X_TOOLKIT */
13507 unload_color (f, f->output_data.x->foreground_pixel);
13508 unload_color (f, f->output_data.x->background_pixel);
13509 unload_color (f, f->output_data.x->cursor_pixel);
13510 unload_color (f, f->output_data.x->cursor_foreground_pixel);
13511 unload_color (f, f->output_data.x->border_pixel);
13512 unload_color (f, f->output_data.x->mouse_pixel);
13514 if (f->output_data.x->scroll_bar_background_pixel != -1)
13515 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
13516 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
13517 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
13518 #ifdef USE_TOOLKIT_SCROLL_BARS
13519 /* Scrollbar shadow colors. */
13520 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
13521 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
13522 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
13523 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
13524 #endif /* USE_TOOLKIT_SCROLL_BARS */
13525 if (f->output_data.x->white_relief.allocated_p)
13526 unload_color (f, f->output_data.x->white_relief.pixel);
13527 if (f->output_data.x->black_relief.allocated_p)
13528 unload_color (f, f->output_data.x->black_relief.pixel);
13530 if (FRAME_FACE_CACHE (f))
13531 free_frame_faces (f);
13533 x_free_gcs (f);
13534 XFlush (FRAME_X_DISPLAY (f));
13537 if (f->output_data.x->saved_menu_event)
13538 xfree (f->output_data.x->saved_menu_event);
13540 xfree (f->output_data.x);
13541 f->output_data.x = NULL;
13543 if (f == dpyinfo->x_focus_frame)
13544 dpyinfo->x_focus_frame = 0;
13545 if (f == dpyinfo->x_focus_event_frame)
13546 dpyinfo->x_focus_event_frame = 0;
13547 if (f == dpyinfo->x_highlight_frame)
13548 dpyinfo->x_highlight_frame = 0;
13550 if (f == dpyinfo->mouse_face_mouse_frame)
13552 dpyinfo->mouse_face_beg_row
13553 = dpyinfo->mouse_face_beg_col = -1;
13554 dpyinfo->mouse_face_end_row
13555 = dpyinfo->mouse_face_end_col = -1;
13556 dpyinfo->mouse_face_window = Qnil;
13557 dpyinfo->mouse_face_deferred_gc = 0;
13558 dpyinfo->mouse_face_mouse_frame = 0;
13561 UNBLOCK_INPUT;
13565 /* Destroy the X window of frame F. */
13567 void
13568 x_destroy_window (f)
13569 struct frame *f;
13571 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13573 /* If a display connection is dead, don't try sending more
13574 commands to the X server. */
13575 if (dpyinfo->display != 0)
13576 x_free_frame_resources (f);
13578 dpyinfo->reference_count--;
13582 /* Setting window manager hints. */
13584 /* Set the normal size hints for the window manager, for frame F.
13585 FLAGS is the flags word to use--or 0 meaning preserve the flags
13586 that the window now has.
13587 If USER_POSITION is nonzero, we set the USPosition
13588 flag (this is useful when FLAGS is 0). */
13590 void
13591 x_wm_set_size_hint (f, flags, user_position)
13592 struct frame *f;
13593 long flags;
13594 int user_position;
13596 XSizeHints size_hints;
13598 #ifdef USE_X_TOOLKIT
13599 Arg al[2];
13600 int ac = 0;
13601 Dimension widget_width, widget_height;
13602 Window window = XtWindow (f->output_data.x->widget);
13603 #else /* not USE_X_TOOLKIT */
13604 Window window = FRAME_X_WINDOW (f);
13605 #endif /* not USE_X_TOOLKIT */
13607 /* Setting PMaxSize caused various problems. */
13608 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
13610 size_hints.x = f->output_data.x->left_pos;
13611 size_hints.y = f->output_data.x->top_pos;
13613 #ifdef USE_X_TOOLKIT
13614 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
13615 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
13616 XtGetValues (f->output_data.x->widget, al, ac);
13617 size_hints.height = widget_height;
13618 size_hints.width = widget_width;
13619 #else /* not USE_X_TOOLKIT */
13620 size_hints.height = PIXEL_HEIGHT (f);
13621 size_hints.width = PIXEL_WIDTH (f);
13622 #endif /* not USE_X_TOOLKIT */
13624 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
13625 size_hints.height_inc = f->output_data.x->line_height;
13626 size_hints.max_width
13627 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
13628 size_hints.max_height
13629 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
13631 /* Calculate the base and minimum sizes.
13633 (When we use the X toolkit, we don't do it here.
13634 Instead we copy the values that the widgets are using, below.) */
13635 #ifndef USE_X_TOOLKIT
13637 int base_width, base_height;
13638 int min_rows = 0, min_cols = 0;
13640 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
13641 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
13643 check_frame_size (f, &min_rows, &min_cols);
13645 /* The window manager uses the base width hints to calculate the
13646 current number of rows and columns in the frame while
13647 resizing; min_width and min_height aren't useful for this
13648 purpose, since they might not give the dimensions for a
13649 zero-row, zero-column frame.
13651 We use the base_width and base_height members if we have
13652 them; otherwise, we set the min_width and min_height members
13653 to the size for a zero x zero frame. */
13655 #ifdef HAVE_X11R4
13656 size_hints.flags |= PBaseSize;
13657 size_hints.base_width = base_width;
13658 size_hints.base_height = base_height;
13659 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
13660 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
13661 #else
13662 size_hints.min_width = base_width;
13663 size_hints.min_height = base_height;
13664 #endif
13667 /* If we don't need the old flags, we don't need the old hint at all. */
13668 if (flags)
13670 size_hints.flags |= flags;
13671 goto no_read;
13673 #endif /* not USE_X_TOOLKIT */
13676 XSizeHints hints; /* Sometimes I hate X Windows... */
13677 long supplied_return;
13678 int value;
13680 #ifdef HAVE_X11R4
13681 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
13682 &supplied_return);
13683 #else
13684 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
13685 #endif
13687 #ifdef USE_X_TOOLKIT
13688 size_hints.base_height = hints.base_height;
13689 size_hints.base_width = hints.base_width;
13690 size_hints.min_height = hints.min_height;
13691 size_hints.min_width = hints.min_width;
13692 #endif
13694 if (flags)
13695 size_hints.flags |= flags;
13696 else
13698 if (value == 0)
13699 hints.flags = 0;
13700 if (hints.flags & PSize)
13701 size_hints.flags |= PSize;
13702 if (hints.flags & PPosition)
13703 size_hints.flags |= PPosition;
13704 if (hints.flags & USPosition)
13705 size_hints.flags |= USPosition;
13706 if (hints.flags & USSize)
13707 size_hints.flags |= USSize;
13711 #ifndef USE_X_TOOLKIT
13712 no_read:
13713 #endif
13715 #ifdef PWinGravity
13716 size_hints.win_gravity = f->output_data.x->win_gravity;
13717 size_hints.flags |= PWinGravity;
13719 if (user_position)
13721 size_hints.flags &= ~ PPosition;
13722 size_hints.flags |= USPosition;
13724 #endif /* PWinGravity */
13726 #ifdef HAVE_X11R4
13727 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13728 #else
13729 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13730 #endif
13733 /* Used for IconicState or NormalState */
13735 void
13736 x_wm_set_window_state (f, state)
13737 struct frame *f;
13738 int state;
13740 #ifdef USE_X_TOOLKIT
13741 Arg al[1];
13743 XtSetArg (al[0], XtNinitialState, state);
13744 XtSetValues (f->output_data.x->widget, al, 1);
13745 #else /* not USE_X_TOOLKIT */
13746 Window window = FRAME_X_WINDOW (f);
13748 f->output_data.x->wm_hints.flags |= StateHint;
13749 f->output_data.x->wm_hints.initial_state = state;
13751 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13752 #endif /* not USE_X_TOOLKIT */
13755 void
13756 x_wm_set_icon_pixmap (f, pixmap_id)
13757 struct frame *f;
13758 int pixmap_id;
13760 Pixmap icon_pixmap;
13762 #ifndef USE_X_TOOLKIT
13763 Window window = FRAME_X_WINDOW (f);
13764 #endif
13766 if (pixmap_id > 0)
13768 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
13769 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
13771 else
13773 /* It seems there is no way to turn off use of an icon pixmap.
13774 The following line does it, only if no icon has yet been created,
13775 for some window managers. But with mwm it crashes.
13776 Some people say it should clear the IconPixmapHint bit in this case,
13777 but that doesn't work, and the X consortium said it isn't the
13778 right thing at all. Since there is no way to win,
13779 best to explicitly give up. */
13780 #if 0
13781 f->output_data.x->wm_hints.icon_pixmap = None;
13782 #else
13783 return;
13784 #endif
13787 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
13790 Arg al[1];
13791 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
13792 XtSetValues (f->output_data.x->widget, al, 1);
13795 #else /* not USE_X_TOOLKIT */
13797 f->output_data.x->wm_hints.flags |= IconPixmapHint;
13798 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13800 #endif /* not USE_X_TOOLKIT */
13803 void
13804 x_wm_set_icon_position (f, icon_x, icon_y)
13805 struct frame *f;
13806 int icon_x, icon_y;
13808 #ifdef USE_X_TOOLKIT
13809 Window window = XtWindow (f->output_data.x->widget);
13810 #else
13811 Window window = FRAME_X_WINDOW (f);
13812 #endif
13814 f->output_data.x->wm_hints.flags |= IconPositionHint;
13815 f->output_data.x->wm_hints.icon_x = icon_x;
13816 f->output_data.x->wm_hints.icon_y = icon_y;
13818 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13822 /***********************************************************************
13823 Fonts
13824 ***********************************************************************/
13826 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
13828 struct font_info *
13829 x_get_font_info (f, font_idx)
13830 FRAME_PTR f;
13831 int font_idx;
13833 return (FRAME_X_FONT_TABLE (f) + font_idx);
13837 /* Return a list of names of available fonts matching PATTERN on frame F.
13839 If SIZE is > 0, it is the size (maximum bounds width) of fonts
13840 to be listed.
13842 SIZE < 0 means include scalable fonts.
13844 Frame F null means we have not yet created any frame on X, and
13845 consult the first display in x_display_list. MAXNAMES sets a limit
13846 on how many fonts to match. */
13848 Lisp_Object
13849 x_list_fonts (f, pattern, size, maxnames)
13850 struct frame *f;
13851 Lisp_Object pattern;
13852 int size;
13853 int maxnames;
13855 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
13856 Lisp_Object tem, second_best;
13857 struct x_display_info *dpyinfo
13858 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
13859 Display *dpy = dpyinfo->display;
13860 int try_XLoadQueryFont = 0;
13861 int count;
13862 int allow_scalable_fonts_p = 0;
13864 if (size < 0)
13866 allow_scalable_fonts_p = 1;
13867 size = 0;
13870 patterns = Fassoc (pattern, Valternate_fontname_alist);
13871 if (NILP (patterns))
13872 patterns = Fcons (pattern, Qnil);
13874 if (maxnames == 1 && !size)
13875 /* We can return any single font matching PATTERN. */
13876 try_XLoadQueryFont = 1;
13878 for (; CONSP (patterns); patterns = XCDR (patterns))
13880 int num_fonts;
13881 char **names = NULL;
13883 pattern = XCAR (patterns);
13884 /* See if we cached the result for this particular query.
13885 The cache is an alist of the form:
13886 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
13887 tem = XCDR (dpyinfo->name_list_element);
13888 key = Fcons (Fcons (pattern, make_number (maxnames)),
13889 allow_scalable_fonts_p ? Qt : Qnil);
13890 list = Fassoc (key, tem);
13891 if (!NILP (list))
13893 list = Fcdr_safe (list);
13894 /* We have a cashed list. Don't have to get the list again. */
13895 goto label_cached;
13898 /* At first, put PATTERN in the cache. */
13900 BLOCK_INPUT;
13901 count = x_catch_errors (dpy);
13903 if (try_XLoadQueryFont)
13905 XFontStruct *font;
13906 unsigned long value;
13908 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
13909 if (x_had_errors_p (dpy))
13911 /* This error is perhaps due to insufficient memory on X
13912 server. Let's just ignore it. */
13913 font = NULL;
13914 x_clear_errors (dpy);
13917 if (font
13918 && XGetFontProperty (font, XA_FONT, &value))
13920 char *name = (char *) XGetAtomName (dpy, (Atom) value);
13921 int len = strlen (name);
13922 char *tmp;
13924 /* If DXPC (a Differential X Protocol Compressor)
13925 Ver.3.7 is running, XGetAtomName will return null
13926 string. We must avoid such a name. */
13927 if (len == 0)
13928 try_XLoadQueryFont = 0;
13929 else
13931 num_fonts = 1;
13932 names = (char **) alloca (sizeof (char *));
13933 /* Some systems only allow alloca assigned to a
13934 simple var. */
13935 tmp = (char *) alloca (len + 1); names[0] = tmp;
13936 bcopy (name, names[0], len + 1);
13937 XFree (name);
13940 else
13941 try_XLoadQueryFont = 0;
13943 if (font)
13944 XFreeFont (dpy, font);
13947 if (!try_XLoadQueryFont)
13949 /* We try at least 10 fonts because XListFonts will return
13950 auto-scaled fonts at the head. */
13951 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
13952 &num_fonts);
13953 if (x_had_errors_p (dpy))
13955 /* This error is perhaps due to insufficient memory on X
13956 server. Let's just ignore it. */
13957 names = NULL;
13958 x_clear_errors (dpy);
13962 x_uncatch_errors (dpy, count);
13963 UNBLOCK_INPUT;
13965 if (names)
13967 int i;
13969 /* Make a list of all the fonts we got back.
13970 Store that in the font cache for the display. */
13971 for (i = 0; i < num_fonts; i++)
13973 int width = 0;
13974 char *p = names[i];
13975 int average_width = -1, dashes = 0;
13977 /* Count the number of dashes in NAMES[I]. If there are
13978 14 dashes, and the field value following 12th dash
13979 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
13980 is usually too ugly to be used for editing. Let's
13981 ignore it. */
13982 while (*p)
13983 if (*p++ == '-')
13985 dashes++;
13986 if (dashes == 7) /* PIXEL_SIZE field */
13987 width = atoi (p);
13988 else if (dashes == 12) /* AVERAGE_WIDTH field */
13989 average_width = atoi (p);
13992 if (allow_scalable_fonts_p
13993 || dashes < 14 || average_width != 0)
13995 tem = build_string (names[i]);
13996 if (NILP (Fassoc (tem, list)))
13998 if (STRINGP (Vx_pixel_size_width_font_regexp)
13999 && ((fast_c_string_match_ignore_case
14000 (Vx_pixel_size_width_font_regexp, names[i]))
14001 >= 0))
14002 /* We can set the value of PIXEL_SIZE to the
14003 width of this font. */
14004 list = Fcons (Fcons (tem, make_number (width)), list);
14005 else
14006 /* For the moment, width is not known. */
14007 list = Fcons (Fcons (tem, Qnil), list);
14012 if (!try_XLoadQueryFont)
14014 BLOCK_INPUT;
14015 XFreeFontNames (names);
14016 UNBLOCK_INPUT;
14020 /* Now store the result in the cache. */
14021 XSETCDR (dpyinfo->name_list_element,
14022 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
14024 label_cached:
14025 if (NILP (list)) continue; /* Try the remaining alternatives. */
14027 newlist = second_best = Qnil;
14028 /* Make a list of the fonts that have the right width. */
14029 for (; CONSP (list); list = XCDR (list))
14031 int found_size;
14033 tem = XCAR (list);
14035 if (!CONSP (tem) || NILP (XCAR (tem)))
14036 continue;
14037 if (!size)
14039 newlist = Fcons (XCAR (tem), newlist);
14040 continue;
14043 if (!INTEGERP (XCDR (tem)))
14045 /* Since we have not yet known the size of this font, we
14046 must try slow function call XLoadQueryFont. */
14047 XFontStruct *thisinfo;
14049 BLOCK_INPUT;
14050 count = x_catch_errors (dpy);
14051 thisinfo = XLoadQueryFont (dpy,
14052 XSTRING (XCAR (tem))->data);
14053 if (x_had_errors_p (dpy))
14055 /* This error is perhaps due to insufficient memory on X
14056 server. Let's just ignore it. */
14057 thisinfo = NULL;
14058 x_clear_errors (dpy);
14060 x_uncatch_errors (dpy, count);
14061 UNBLOCK_INPUT;
14063 if (thisinfo)
14065 XSETCDR (tem,
14066 (thisinfo->min_bounds.width == 0
14067 ? make_number (0)
14068 : make_number (thisinfo->max_bounds.width)));
14069 BLOCK_INPUT;
14070 XFreeFont (dpy, thisinfo);
14071 UNBLOCK_INPUT;
14073 else
14074 /* For unknown reason, the previous call of XListFont had
14075 returned a font which can't be opened. Record the size
14076 as 0 not to try to open it again. */
14077 XSETCDR (tem, make_number (0));
14080 found_size = XINT (XCDR (tem));
14081 if (found_size == size)
14082 newlist = Fcons (XCAR (tem), newlist);
14083 else if (found_size > 0)
14085 if (NILP (second_best))
14086 second_best = tem;
14087 else if (found_size < size)
14089 if (XINT (XCDR (second_best)) > size
14090 || XINT (XCDR (second_best)) < found_size)
14091 second_best = tem;
14093 else
14095 if (XINT (XCDR (second_best)) > size
14096 && XINT (XCDR (second_best)) > found_size)
14097 second_best = tem;
14101 if (!NILP (newlist))
14102 break;
14103 else if (!NILP (second_best))
14105 newlist = Fcons (XCAR (second_best), Qnil);
14106 break;
14110 return newlist;
14114 #if GLYPH_DEBUG
14116 /* Check that FONT is valid on frame F. It is if it can be found in F's
14117 font table. */
14119 static void
14120 x_check_font (f, font)
14121 struct frame *f;
14122 XFontStruct *font;
14124 int i;
14125 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14127 xassert (font != NULL);
14129 for (i = 0; i < dpyinfo->n_fonts; i++)
14130 if (dpyinfo->font_table[i].name
14131 && font == dpyinfo->font_table[i].font)
14132 break;
14134 xassert (i < dpyinfo->n_fonts);
14137 #endif /* GLYPH_DEBUG != 0 */
14139 /* Set *W to the minimum width, *H to the minimum font height of FONT.
14140 Note: There are (broken) X fonts out there with invalid XFontStruct
14141 min_bounds contents. For example, handa@etl.go.jp reports that
14142 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
14143 have font->min_bounds.width == 0. */
14145 static INLINE void
14146 x_font_min_bounds (font, w, h)
14147 XFontStruct *font;
14148 int *w, *h;
14150 *h = FONT_HEIGHT (font);
14151 *w = font->min_bounds.width;
14153 /* Try to handle the case where FONT->min_bounds has invalid
14154 contents. Since the only font known to have invalid min_bounds
14155 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
14156 if (*w <= 0)
14157 *w = font->max_bounds.width;
14161 /* Compute the smallest character width and smallest font height over
14162 all fonts available on frame F. Set the members smallest_char_width
14163 and smallest_font_height in F's x_display_info structure to
14164 the values computed. Value is non-zero if smallest_font_height or
14165 smallest_char_width become smaller than they were before. */
14167 static int
14168 x_compute_min_glyph_bounds (f)
14169 struct frame *f;
14171 int i;
14172 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14173 XFontStruct *font;
14174 int old_width = dpyinfo->smallest_char_width;
14175 int old_height = dpyinfo->smallest_font_height;
14177 dpyinfo->smallest_font_height = 100000;
14178 dpyinfo->smallest_char_width = 100000;
14180 for (i = 0; i < dpyinfo->n_fonts; ++i)
14181 if (dpyinfo->font_table[i].name)
14183 struct font_info *fontp = dpyinfo->font_table + i;
14184 int w, h;
14186 font = (XFontStruct *) fontp->font;
14187 xassert (font != (XFontStruct *) ~0);
14188 x_font_min_bounds (font, &w, &h);
14190 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
14191 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
14194 xassert (dpyinfo->smallest_char_width > 0
14195 && dpyinfo->smallest_font_height > 0);
14197 return (dpyinfo->n_fonts == 1
14198 || dpyinfo->smallest_char_width < old_width
14199 || dpyinfo->smallest_font_height < old_height);
14203 /* Load font named FONTNAME of the size SIZE for frame F, and return a
14204 pointer to the structure font_info while allocating it dynamically.
14205 If SIZE is 0, load any size of font.
14206 If loading is failed, return NULL. */
14208 struct font_info *
14209 x_load_font (f, fontname, size)
14210 struct frame *f;
14211 register char *fontname;
14212 int size;
14214 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14215 Lisp_Object font_names;
14216 int count;
14218 /* Get a list of all the fonts that match this name. Once we
14219 have a list of matching fonts, we compare them against the fonts
14220 we already have by comparing names. */
14221 font_names = x_list_fonts (f, build_string (fontname), size, 1);
14223 if (!NILP (font_names))
14225 Lisp_Object tail;
14226 int i;
14228 for (i = 0; i < dpyinfo->n_fonts; i++)
14229 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
14230 if (dpyinfo->font_table[i].name
14231 && (!strcmp (dpyinfo->font_table[i].name,
14232 XSTRING (XCAR (tail))->data)
14233 || !strcmp (dpyinfo->font_table[i].full_name,
14234 XSTRING (XCAR (tail))->data)))
14235 return (dpyinfo->font_table + i);
14238 /* Load the font and add it to the table. */
14240 char *full_name;
14241 XFontStruct *font;
14242 struct font_info *fontp;
14243 unsigned long value;
14244 int i;
14246 /* If we have found fonts by x_list_font, load one of them. If
14247 not, we still try to load a font by the name given as FONTNAME
14248 because XListFonts (called in x_list_font) of some X server has
14249 a bug of not finding a font even if the font surely exists and
14250 is loadable by XLoadQueryFont. */
14251 if (size > 0 && !NILP (font_names))
14252 fontname = (char *) XSTRING (XCAR (font_names))->data;
14254 BLOCK_INPUT;
14255 count = x_catch_errors (FRAME_X_DISPLAY (f));
14256 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
14257 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
14259 /* This error is perhaps due to insufficient memory on X
14260 server. Let's just ignore it. */
14261 font = NULL;
14262 x_clear_errors (FRAME_X_DISPLAY (f));
14264 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
14265 UNBLOCK_INPUT;
14266 if (!font)
14267 return NULL;
14269 /* Find a free slot in the font table. */
14270 for (i = 0; i < dpyinfo->n_fonts; ++i)
14271 if (dpyinfo->font_table[i].name == NULL)
14272 break;
14274 /* If no free slot found, maybe enlarge the font table. */
14275 if (i == dpyinfo->n_fonts
14276 && dpyinfo->n_fonts == dpyinfo->font_table_size)
14278 int sz;
14279 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
14280 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
14281 dpyinfo->font_table
14282 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
14285 fontp = dpyinfo->font_table + i;
14286 if (i == dpyinfo->n_fonts)
14287 ++dpyinfo->n_fonts;
14289 /* Now fill in the slots of *FONTP. */
14290 BLOCK_INPUT;
14291 fontp->font = font;
14292 fontp->font_idx = i;
14293 fontp->charset = -1; /* fs_load_font sets it. */
14294 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
14295 bcopy (fontname, fontp->name, strlen (fontname) + 1);
14297 /* Try to get the full name of FONT. Put it in FULL_NAME. */
14298 full_name = 0;
14299 if (XGetFontProperty (font, XA_FONT, &value))
14301 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
14302 char *p = name;
14303 int dashes = 0;
14305 /* Count the number of dashes in the "full name".
14306 If it is too few, this isn't really the font's full name,
14307 so don't use it.
14308 In X11R4, the fonts did not come with their canonical names
14309 stored in them. */
14310 while (*p)
14312 if (*p == '-')
14313 dashes++;
14314 p++;
14317 if (dashes >= 13)
14319 full_name = (char *) xmalloc (p - name + 1);
14320 bcopy (name, full_name, p - name + 1);
14323 XFree (name);
14326 if (full_name != 0)
14327 fontp->full_name = full_name;
14328 else
14329 fontp->full_name = fontp->name;
14331 fontp->size = font->max_bounds.width;
14332 fontp->height = FONT_HEIGHT (font);
14334 if (NILP (font_names))
14336 /* We come here because of a bug of XListFonts mentioned at
14337 the head of this block. Let's store this information in
14338 the cache for x_list_fonts. */
14339 Lisp_Object lispy_name = build_string (fontname);
14340 Lisp_Object lispy_full_name = build_string (fontp->full_name);
14341 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
14342 Qnil);
14344 XSETCDR (dpyinfo->name_list_element,
14345 Fcons (Fcons (key,
14346 Fcons (Fcons (lispy_full_name,
14347 make_number (fontp->size)),
14348 Qnil)),
14349 XCDR (dpyinfo->name_list_element)));
14350 if (full_name)
14352 key = Fcons (Fcons (lispy_full_name, make_number (256)),
14353 Qnil);
14354 XSETCDR (dpyinfo->name_list_element,
14355 Fcons (Fcons (key,
14356 Fcons (Fcons (lispy_full_name,
14357 make_number (fontp->size)),
14358 Qnil)),
14359 XCDR (dpyinfo->name_list_element)));
14363 /* The slot `encoding' specifies how to map a character
14364 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
14365 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
14366 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
14367 2:0xA020..0xFF7F). For the moment, we don't know which charset
14368 uses this font. So, we set information in fontp->encoding[1]
14369 which is never used by any charset. If mapping can't be
14370 decided, set FONT_ENCODING_NOT_DECIDED. */
14371 fontp->encoding_type
14372 = (font->max_byte1 == 0
14373 /* 1-byte font */
14374 ? (font->min_char_or_byte2 < 0x80
14375 ? (font->max_char_or_byte2 < 0x80
14376 ? 0 /* 0x20..0x7F */
14377 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
14378 : 1) /* 0xA0..0xFF */
14379 /* 2-byte font */
14380 : (font->min_byte1 < 0x80
14381 ? (font->max_byte1 < 0x80
14382 ? (font->min_char_or_byte2 < 0x80
14383 ? (font->max_char_or_byte2 < 0x80
14384 ? 0 /* 0x2020..0x7F7F */
14385 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
14386 : 3) /* 0x20A0..0x7FFF */
14387 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
14388 : (font->min_char_or_byte2 < 0x80
14389 ? (font->max_char_or_byte2 < 0x80
14390 ? 2 /* 0xA020..0xFF7F */
14391 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
14392 : 1))); /* 0xA0A0..0xFFFF */
14394 fontp->baseline_offset
14395 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
14396 ? (long) value : 0);
14397 fontp->relative_compose
14398 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
14399 ? (long) value : 0);
14400 fontp->default_ascent
14401 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
14402 ? (long) value : 0);
14404 /* Set global flag fonts_changed_p to non-zero if the font loaded
14405 has a character with a smaller width than any other character
14406 before, or if the font loaded has a smaller height than any
14407 other font loaded before. If this happens, it will make a
14408 glyph matrix reallocation necessary. */
14409 fonts_changed_p |= x_compute_min_glyph_bounds (f);
14410 UNBLOCK_INPUT;
14411 return fontp;
14416 /* Return a pointer to struct font_info of a font named FONTNAME for
14417 frame F. If no such font is loaded, return NULL. */
14419 struct font_info *
14420 x_query_font (f, fontname)
14421 struct frame *f;
14422 register char *fontname;
14424 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14425 int i;
14427 for (i = 0; i < dpyinfo->n_fonts; i++)
14428 if (dpyinfo->font_table[i].name
14429 && (!strcmp (dpyinfo->font_table[i].name, fontname)
14430 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
14431 return (dpyinfo->font_table + i);
14432 return NULL;
14436 /* Find a CCL program for a font specified by FONTP, and set the member
14437 `encoder' of the structure. */
14439 void
14440 x_find_ccl_program (fontp)
14441 struct font_info *fontp;
14443 Lisp_Object list, elt;
14445 elt = Qnil;
14446 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
14448 elt = XCAR (list);
14449 if (CONSP (elt)
14450 && STRINGP (XCAR (elt))
14451 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
14452 >= 0)
14453 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
14454 >= 0)))
14455 break;
14458 if (! NILP (list))
14460 struct ccl_program *ccl
14461 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
14463 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
14464 xfree (ccl);
14465 else
14466 fontp->font_encoder = ccl;
14471 /* Return a char-table whose elements are t if the font FONT_INFO
14472 contains a glyph for the corresponding character, and nil if not.
14474 Fixme: For the moment, this function works only for fonts whose
14475 glyph encoding is the same as Unicode (e.g. ISO10646-1 fonts). */
14477 Lisp_Object
14478 x_get_font_repertory (f, font_info)
14479 FRAME_PTR f;
14480 struct font_info *font_info;
14482 XFontStruct *font = (XFontStruct *) font_info->font;
14483 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
14484 Lisp_Object table;
14485 int min_byte1, max_byte1, min_byte2, max_byte2;
14487 table = Fmake_char_table (Qnil, Qnil);
14489 min_byte1 = font->min_byte1;
14490 max_byte1 = font->max_byte1;
14491 min_byte2 = font->min_char_or_byte2;
14492 max_byte2 = font->max_char_or_byte2;
14493 if (min_byte1 == 0 && max_byte1 == 0)
14495 if (! font->per_char || font->all_chars_exist == True)
14496 char_table_set_range (table, min_byte2, max_byte2, Qt);
14497 else
14499 XCharStruct *pcm = font->per_char;
14500 int from = -1;
14501 int i;
14503 for (i = min_byte2; i <= max_byte2; i++, pcm++)
14505 if (pcm->width == 0 && pcm->rbearing == pcm->lbearing)
14507 if (from >= 0)
14509 char_table_set_range (table, from, i - 1, Qt);
14510 from = -1;
14513 else if (from < 0)
14514 from = i;
14516 if (from >= 0)
14517 char_table_set_range (table, from, i - 1, Qt);
14520 else
14522 if (! font->per_char || font->all_chars_exist == True)
14524 int i;
14526 for (i = min_byte1; i <= max_byte1; i++)
14527 char_table_set_range (table,
14528 (i << 8) | min_byte2, (i << 8) | max_byte2,
14529 Qt);
14531 else
14533 XCharStruct *pcm = font->per_char;
14534 int i;
14536 for (i = min_byte1; i <= max_byte1; i++)
14538 int from = -1;
14539 int j;
14541 for (j = min_byte2; j <= max_byte2; j++, pcm++)
14543 if (pcm->width == 0 && pcm->rbearing == pcm->lbearing)
14545 if (from >= 0)
14547 char_table_set_range (table, (i << 8) | from,
14548 (i << 8) | (j - 1), Qt);
14549 from = -1;
14552 else if (from < 0)
14553 from = j;
14555 if (from >= 0)
14556 char_table_set_range (table, (i << 8) | from,
14557 (i << 8) | (j - 1), Qt);
14562 return table;
14567 /***********************************************************************
14568 Initialization
14569 ***********************************************************************/
14571 #ifdef USE_X_TOOLKIT
14572 static XrmOptionDescRec emacs_options[] = {
14573 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
14574 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
14576 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
14577 XrmoptionSepArg, NULL},
14578 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
14580 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14581 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14582 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14583 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14584 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14585 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
14586 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
14588 #endif /* USE_X_TOOLKIT */
14590 static int x_initialized;
14592 #ifdef MULTI_KBOARD
14593 /* Test whether two display-name strings agree up to the dot that separates
14594 the screen number from the server number. */
14595 static int
14596 same_x_server (name1, name2)
14597 char *name1, *name2;
14599 int seen_colon = 0;
14600 unsigned char *system_name = XSTRING (Vsystem_name)->data;
14601 int system_name_length = strlen (system_name);
14602 int length_until_period = 0;
14604 while (system_name[length_until_period] != 0
14605 && system_name[length_until_period] != '.')
14606 length_until_period++;
14608 /* Treat `unix' like an empty host name. */
14609 if (! strncmp (name1, "unix:", 5))
14610 name1 += 4;
14611 if (! strncmp (name2, "unix:", 5))
14612 name2 += 4;
14613 /* Treat this host's name like an empty host name. */
14614 if (! strncmp (name1, system_name, system_name_length)
14615 && name1[system_name_length] == ':')
14616 name1 += system_name_length;
14617 if (! strncmp (name2, system_name, system_name_length)
14618 && name2[system_name_length] == ':')
14619 name2 += system_name_length;
14620 /* Treat this host's domainless name like an empty host name. */
14621 if (! strncmp (name1, system_name, length_until_period)
14622 && name1[length_until_period] == ':')
14623 name1 += length_until_period;
14624 if (! strncmp (name2, system_name, length_until_period)
14625 && name2[length_until_period] == ':')
14626 name2 += length_until_period;
14628 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
14630 if (*name1 == ':')
14631 seen_colon++;
14632 if (seen_colon && *name1 == '.')
14633 return 1;
14635 return (seen_colon
14636 && (*name1 == '.' || *name1 == '\0')
14637 && (*name2 == '.' || *name2 == '\0'));
14639 #endif
14641 struct x_display_info *
14642 x_term_init (display_name, xrm_option, resource_name)
14643 Lisp_Object display_name;
14644 char *xrm_option;
14645 char *resource_name;
14647 int connection;
14648 Display *dpy;
14649 struct x_display_info *dpyinfo;
14650 XrmDatabase xrdb;
14652 BLOCK_INPUT;
14654 if (!x_initialized)
14656 x_initialize ();
14657 x_initialized = 1;
14660 #ifdef USE_X_TOOLKIT
14661 /* weiner@footloose.sps.mot.com reports that this causes
14662 errors with X11R5:
14663 X protocol error: BadAtom (invalid Atom parameter)
14664 on protocol request 18skiloaf.
14665 So let's not use it until R6. */
14666 #ifdef HAVE_X11XTR6
14667 XtSetLanguageProc (NULL, NULL, NULL);
14668 #endif
14671 int argc = 0;
14672 char *argv[3];
14674 argv[0] = "";
14675 argc = 1;
14676 if (xrm_option)
14678 argv[argc++] = "-xrm";
14679 argv[argc++] = xrm_option;
14681 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
14682 resource_name, EMACS_CLASS,
14683 emacs_options, XtNumber (emacs_options),
14684 &argc, argv);
14686 #ifdef HAVE_X11XTR6
14687 /* I think this is to compensate for XtSetLanguageProc. */
14688 fixup_locale ();
14689 #endif
14692 #else /* not USE_X_TOOLKIT */
14693 #ifdef HAVE_X11R5
14694 XSetLocaleModifiers ("");
14695 #endif
14696 dpy = XOpenDisplay (XSTRING (display_name)->data);
14697 #endif /* not USE_X_TOOLKIT */
14699 /* Detect failure. */
14700 if (dpy == 0)
14702 UNBLOCK_INPUT;
14703 return 0;
14706 /* We have definitely succeeded. Record the new connection. */
14708 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
14709 bzero (dpyinfo, sizeof *dpyinfo);
14711 #ifdef MULTI_KBOARD
14713 struct x_display_info *share;
14714 Lisp_Object tail;
14716 for (share = x_display_list, tail = x_display_name_list; share;
14717 share = share->next, tail = XCDR (tail))
14718 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
14719 XSTRING (display_name)->data))
14720 break;
14721 if (share)
14722 dpyinfo->kboard = share->kboard;
14723 else
14725 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
14726 init_kboard (dpyinfo->kboard);
14727 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
14729 char *vendor = ServerVendor (dpy);
14730 UNBLOCK_INPUT;
14731 dpyinfo->kboard->Vsystem_key_alist
14732 = call1 (Qvendor_specific_keysyms,
14733 build_string (vendor ? vendor : ""));
14734 BLOCK_INPUT;
14737 dpyinfo->kboard->next_kboard = all_kboards;
14738 all_kboards = dpyinfo->kboard;
14739 /* Don't let the initial kboard remain current longer than necessary.
14740 That would cause problems if a file loaded on startup tries to
14741 prompt in the mini-buffer. */
14742 if (current_kboard == initial_kboard)
14743 current_kboard = dpyinfo->kboard;
14745 dpyinfo->kboard->reference_count++;
14747 #endif
14749 /* Put this display on the chain. */
14750 dpyinfo->next = x_display_list;
14751 x_display_list = dpyinfo;
14753 /* Put it on x_display_name_list as well, to keep them parallel. */
14754 x_display_name_list = Fcons (Fcons (display_name, Qnil),
14755 x_display_name_list);
14756 dpyinfo->name_list_element = XCAR (x_display_name_list);
14758 dpyinfo->display = dpy;
14760 #if 0
14761 XSetAfterFunction (x_current_display, x_trace_wire);
14762 #endif /* ! 0 */
14764 dpyinfo->x_id_name
14765 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
14766 + STRING_BYTES (XSTRING (Vsystem_name))
14767 + 2);
14768 sprintf (dpyinfo->x_id_name, "%s@%s",
14769 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
14771 /* Figure out which modifier bits mean what. */
14772 x_find_modifier_meanings (dpyinfo);
14774 /* Get the scroll bar cursor. */
14775 dpyinfo->vertical_scroll_bar_cursor
14776 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
14778 xrdb = x_load_resources (dpyinfo->display, xrm_option,
14779 resource_name, EMACS_CLASS);
14780 #ifdef HAVE_XRMSETDATABASE
14781 XrmSetDatabase (dpyinfo->display, xrdb);
14782 #else
14783 dpyinfo->display->db = xrdb;
14784 #endif
14785 /* Put the rdb where we can find it in a way that works on
14786 all versions. */
14787 dpyinfo->xrdb = xrdb;
14789 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
14790 DefaultScreen (dpyinfo->display));
14791 select_visual (dpyinfo);
14792 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
14793 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
14794 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
14795 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
14796 dpyinfo->grabbed = 0;
14797 dpyinfo->reference_count = 0;
14798 dpyinfo->icon_bitmap_id = -1;
14799 dpyinfo->font_table = NULL;
14800 dpyinfo->n_fonts = 0;
14801 dpyinfo->font_table_size = 0;
14802 dpyinfo->bitmaps = 0;
14803 dpyinfo->bitmaps_size = 0;
14804 dpyinfo->bitmaps_last = 0;
14805 dpyinfo->scratch_cursor_gc = 0;
14806 dpyinfo->mouse_face_mouse_frame = 0;
14807 dpyinfo->mouse_face_deferred_gc = 0;
14808 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
14809 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
14810 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
14811 dpyinfo->mouse_face_window = Qnil;
14812 dpyinfo->mouse_face_overlay = Qnil;
14813 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
14814 dpyinfo->mouse_face_defer = 0;
14815 dpyinfo->mouse_face_hidden = 0;
14816 dpyinfo->x_focus_frame = 0;
14817 dpyinfo->x_focus_event_frame = 0;
14818 dpyinfo->x_highlight_frame = 0;
14819 dpyinfo->image_cache = make_image_cache ();
14821 /* See if a private colormap is requested. */
14822 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
14824 if (dpyinfo->visual->class == PseudoColor)
14826 Lisp_Object value;
14827 value = display_x_get_resource (dpyinfo,
14828 build_string ("privateColormap"),
14829 build_string ("PrivateColormap"),
14830 Qnil, Qnil);
14831 if (STRINGP (value)
14832 && (!strcmp (XSTRING (value)->data, "true")
14833 || !strcmp (XSTRING (value)->data, "on")))
14834 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
14837 else
14838 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
14839 dpyinfo->visual, AllocNone);
14842 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
14843 double pixels = DisplayHeight (dpyinfo->display, screen_number);
14844 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
14845 dpyinfo->resy = pixels * 25.4 / mm;
14846 pixels = DisplayWidth (dpyinfo->display, screen_number);
14847 mm = DisplayWidthMM (dpyinfo->display, screen_number);
14848 dpyinfo->resx = pixels * 25.4 / mm;
14851 dpyinfo->Xatom_wm_protocols
14852 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
14853 dpyinfo->Xatom_wm_take_focus
14854 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
14855 dpyinfo->Xatom_wm_save_yourself
14856 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
14857 dpyinfo->Xatom_wm_delete_window
14858 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
14859 dpyinfo->Xatom_wm_change_state
14860 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
14861 dpyinfo->Xatom_wm_configure_denied
14862 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
14863 dpyinfo->Xatom_wm_window_moved
14864 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
14865 dpyinfo->Xatom_editres
14866 = XInternAtom (dpyinfo->display, "Editres", False);
14867 dpyinfo->Xatom_CLIPBOARD
14868 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
14869 dpyinfo->Xatom_TIMESTAMP
14870 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
14871 dpyinfo->Xatom_TEXT
14872 = XInternAtom (dpyinfo->display, "TEXT", False);
14873 dpyinfo->Xatom_COMPOUND_TEXT
14874 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
14875 dpyinfo->Xatom_DELETE
14876 = XInternAtom (dpyinfo->display, "DELETE", False);
14877 dpyinfo->Xatom_MULTIPLE
14878 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
14879 dpyinfo->Xatom_INCR
14880 = XInternAtom (dpyinfo->display, "INCR", False);
14881 dpyinfo->Xatom_EMACS_TMP
14882 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
14883 dpyinfo->Xatom_TARGETS
14884 = XInternAtom (dpyinfo->display, "TARGETS", False);
14885 dpyinfo->Xatom_NULL
14886 = XInternAtom (dpyinfo->display, "NULL", False);
14887 dpyinfo->Xatom_ATOM_PAIR
14888 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
14889 /* For properties of font. */
14890 dpyinfo->Xatom_PIXEL_SIZE
14891 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
14892 dpyinfo->Xatom_MULE_BASELINE_OFFSET
14893 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
14894 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
14895 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
14896 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
14897 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
14899 /* Ghostscript support. */
14900 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
14901 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
14903 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
14904 False);
14906 dpyinfo->cut_buffers_initialized = 0;
14908 connection = ConnectionNumber (dpyinfo->display);
14909 dpyinfo->connection = connection;
14912 char null_bits[1];
14914 null_bits[0] = 0x00;
14916 dpyinfo->null_pixel
14917 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14918 null_bits, 1, 1, (long) 0, (long) 0,
14923 extern int gray_bitmap_width, gray_bitmap_height;
14924 extern char *gray_bitmap_bits;
14925 dpyinfo->gray
14926 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14927 gray_bitmap_bits,
14928 gray_bitmap_width, gray_bitmap_height,
14929 (unsigned long) 1, (unsigned long) 0, 1);
14932 #ifdef HAVE_X_I18N
14933 xim_initialize (dpyinfo, resource_name);
14934 #endif
14936 #ifdef subprocesses
14937 /* This is only needed for distinguishing keyboard and process input. */
14938 if (connection != 0)
14939 add_keyboard_wait_descriptor (connection);
14940 #endif
14942 #ifndef F_SETOWN_BUG
14943 #ifdef F_SETOWN
14944 #ifdef F_SETOWN_SOCK_NEG
14945 /* stdin is a socket here */
14946 fcntl (connection, F_SETOWN, -getpid ());
14947 #else /* ! defined (F_SETOWN_SOCK_NEG) */
14948 fcntl (connection, F_SETOWN, getpid ());
14949 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
14950 #endif /* ! defined (F_SETOWN) */
14951 #endif /* F_SETOWN_BUG */
14953 #ifdef SIGIO
14954 if (interrupt_input)
14955 init_sigio (connection);
14956 #endif /* ! defined (SIGIO) */
14958 #ifdef USE_LUCID
14959 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
14960 /* Make sure that we have a valid font for dialog boxes
14961 so that Xt does not crash. */
14963 Display *dpy = dpyinfo->display;
14964 XrmValue d, fr, to;
14965 Font font;
14966 int count;
14968 d.addr = (XPointer)&dpy;
14969 d.size = sizeof (Display *);
14970 fr.addr = XtDefaultFont;
14971 fr.size = sizeof (XtDefaultFont);
14972 to.size = sizeof (Font *);
14973 to.addr = (XPointer)&font;
14974 count = x_catch_errors (dpy);
14975 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
14976 abort ();
14977 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
14978 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
14979 x_uncatch_errors (dpy, count);
14981 #endif
14982 #endif
14984 /* See if we should run in synchronous mode. This is useful
14985 for debugging X code. */
14987 Lisp_Object value;
14988 value = display_x_get_resource (dpyinfo,
14989 build_string ("synchronous"),
14990 build_string ("Synchronous"),
14991 Qnil, Qnil);
14992 if (STRINGP (value)
14993 && (!strcmp (XSTRING (value)->data, "true")
14994 || !strcmp (XSTRING (value)->data, "on")))
14995 XSynchronize (dpyinfo->display, True);
14998 UNBLOCK_INPUT;
15000 return dpyinfo;
15003 /* Get rid of display DPYINFO, assuming all frames are already gone,
15004 and without sending any more commands to the X server. */
15006 void
15007 x_delete_display (dpyinfo)
15008 struct x_display_info *dpyinfo;
15010 delete_keyboard_wait_descriptor (dpyinfo->connection);
15012 /* Discard this display from x_display_name_list and x_display_list.
15013 We can't use Fdelq because that can quit. */
15014 if (! NILP (x_display_name_list)
15015 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
15016 x_display_name_list = XCDR (x_display_name_list);
15017 else
15019 Lisp_Object tail;
15021 tail = x_display_name_list;
15022 while (CONSP (tail) && CONSP (XCDR (tail)))
15024 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
15026 XSETCDR (tail, XCDR (XCDR (tail)));
15027 break;
15029 tail = XCDR (tail);
15033 if (next_noop_dpyinfo == dpyinfo)
15034 next_noop_dpyinfo = dpyinfo->next;
15036 if (x_display_list == dpyinfo)
15037 x_display_list = dpyinfo->next;
15038 else
15040 struct x_display_info *tail;
15042 for (tail = x_display_list; tail; tail = tail->next)
15043 if (tail->next == dpyinfo)
15044 tail->next = tail->next->next;
15047 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
15048 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
15049 XrmDestroyDatabase (dpyinfo->xrdb);
15050 #endif
15051 #endif
15052 #ifdef MULTI_KBOARD
15053 if (--dpyinfo->kboard->reference_count == 0)
15054 delete_kboard (dpyinfo->kboard);
15055 #endif
15056 #ifdef HAVE_X_I18N
15057 if (dpyinfo->xim)
15058 xim_close_dpy (dpyinfo);
15059 #endif
15061 xfree (dpyinfo->font_table);
15062 xfree (dpyinfo->x_id_name);
15063 xfree (dpyinfo->color_cells);
15064 xfree (dpyinfo);
15068 /* Set up use of X before we make the first connection. */
15070 static struct redisplay_interface x_redisplay_interface =
15072 x_produce_glyphs,
15073 x_write_glyphs,
15074 x_insert_glyphs,
15075 x_clear_end_of_line,
15076 x_scroll_run,
15077 x_after_update_window_line,
15078 x_update_window_begin,
15079 x_update_window_end,
15080 XTcursor_to,
15081 x_flush,
15082 x_clear_mouse_face,
15083 x_get_glyph_overhangs,
15084 x_fix_overlapping_area
15087 void
15088 x_initialize ()
15090 rif = &x_redisplay_interface;
15092 clear_frame_hook = x_clear_frame;
15093 ins_del_lines_hook = x_ins_del_lines;
15094 delete_glyphs_hook = x_delete_glyphs;
15095 ring_bell_hook = XTring_bell;
15096 reset_terminal_modes_hook = XTreset_terminal_modes;
15097 set_terminal_modes_hook = XTset_terminal_modes;
15098 update_begin_hook = x_update_begin;
15099 update_end_hook = x_update_end;
15100 set_terminal_window_hook = XTset_terminal_window;
15101 read_socket_hook = XTread_socket;
15102 frame_up_to_date_hook = XTframe_up_to_date;
15103 mouse_position_hook = XTmouse_position;
15104 frame_rehighlight_hook = XTframe_rehighlight;
15105 frame_raise_lower_hook = XTframe_raise_lower;
15106 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
15107 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
15108 redeem_scroll_bar_hook = XTredeem_scroll_bar;
15109 judge_scroll_bars_hook = XTjudge_scroll_bars;
15110 estimate_mode_line_height_hook = x_estimate_mode_line_height;
15112 scroll_region_ok = 1; /* we'll scroll partial frames */
15113 char_ins_del_ok = 1;
15114 line_ins_del_ok = 1; /* we'll just blt 'em */
15115 fast_clear_end_of_line = 1; /* X does this well */
15116 memory_below_frame = 0; /* we don't remember what scrolls
15117 off the bottom */
15118 baud_rate = 19200;
15120 x_noop_count = 0;
15121 last_tool_bar_item = -1;
15122 any_help_event_p = 0;
15124 /* Try to use interrupt input; if we can't, then start polling. */
15125 Fset_input_mode (Qt, Qnil, Qt, Qnil);
15127 #ifdef USE_X_TOOLKIT
15128 XtToolkitInitialize ();
15130 Xt_app_con = XtCreateApplicationContext ();
15132 /* Register a converter from strings to pixels, which uses
15133 Emacs' color allocation infrastructure. */
15134 XtAppSetTypeConverter (Xt_app_con,
15135 XtRString, XtRPixel, cvt_string_to_pixel,
15136 cvt_string_to_pixel_args,
15137 XtNumber (cvt_string_to_pixel_args),
15138 XtCacheByDisplay, cvt_pixel_dtor);
15140 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
15142 /* Install an asynchronous timer that processes Xt timeout events
15143 every 0.1s. This is necessary because some widget sets use
15144 timeouts internally, for example the LessTif menu bar, or the
15145 Xaw3d scroll bar. When Xt timouts aren't processed, these
15146 widgets don't behave normally. */
15148 EMACS_TIME interval;
15149 EMACS_SET_SECS_USECS (interval, 0, 100000);
15150 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
15152 #endif
15154 #ifdef USE_TOOLKIT_SCROLL_BARS
15155 xaw3d_arrow_scroll = False;
15156 xaw3d_pick_top = True;
15157 #endif
15159 /* Note that there is no real way portable across R3/R4 to get the
15160 original error handler. */
15161 XSetErrorHandler (x_error_handler);
15162 XSetIOErrorHandler (x_io_error_quitter);
15164 /* Disable Window Change signals; they are handled by X events. */
15165 #ifdef SIGWINCH
15166 signal (SIGWINCH, SIG_DFL);
15167 #endif /* ! defined (SIGWINCH) */
15169 signal (SIGPIPE, x_connection_signal);
15173 void
15174 syms_of_xterm ()
15176 staticpro (&x_error_message_string);
15177 x_error_message_string = Qnil;
15179 staticpro (&x_display_name_list);
15180 x_display_name_list = Qnil;
15182 staticpro (&last_mouse_scroll_bar);
15183 last_mouse_scroll_bar = Qnil;
15185 staticpro (&Qvendor_specific_keysyms);
15186 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
15188 staticpro (&last_mouse_press_frame);
15189 last_mouse_press_frame = Qnil;
15191 help_echo = Qnil;
15192 staticpro (&help_echo);
15193 help_echo_object = Qnil;
15194 staticpro (&help_echo_object);
15195 help_echo_window = Qnil;
15196 staticpro (&help_echo_window);
15197 previous_help_echo = Qnil;
15198 staticpro (&previous_help_echo);
15199 help_echo_pos = -1;
15201 DEFVAR_BOOL ("x-autoselect-window", &x_autoselect_window_p,
15202 doc: /* *Non-nil means autoselect window with mouse pointer. */);
15203 x_autoselect_window_p = 0;
15205 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
15206 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
15207 For example, if a block cursor is over a tab, it will be drawn as
15208 wide as that tab on the display. */);
15209 x_stretch_cursor_p = 0;
15211 DEFVAR_BOOL ("x-use-underline-position-properties",
15212 &x_use_underline_position_properties,
15213 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
15214 nil means ignore them. If you encounter fonts with bogus
15215 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
15216 to 4.1, set this to nil. */);
15217 x_use_underline_position_properties = 1;
15219 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
15220 doc: /* What X toolkit scroll bars Emacs uses.
15221 A value of nil means Emacs doesn't use X toolkit scroll bars.
15222 Otherwise, value is a symbol describing the X toolkit. */);
15223 #ifdef USE_TOOLKIT_SCROLL_BARS
15224 #ifdef USE_MOTIF
15225 Vx_toolkit_scroll_bars = intern ("motif");
15226 #elif defined HAVE_XAW3D
15227 Vx_toolkit_scroll_bars = intern ("xaw3d");
15228 #else
15229 Vx_toolkit_scroll_bars = intern ("xaw");
15230 #endif
15231 #else
15232 Vx_toolkit_scroll_bars = Qnil;
15233 #endif
15235 staticpro (&last_mouse_motion_frame);
15236 last_mouse_motion_frame = Qnil;
15238 Qmodifier_value = intern ("modifier-value");
15239 Qalt = intern ("alt");
15240 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
15241 Qhyper = intern ("hyper");
15242 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
15243 Qmeta = intern ("meta");
15244 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
15245 Qsuper = intern ("super");
15246 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
15248 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
15249 doc: /* Which keys Emacs uses for the alt modifier.
15250 This should be one of the symbols `alt', `hyper', `meta', `super'.
15251 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
15252 is nil, which is the same as `alt'. */);
15253 Vx_alt_keysym = Qnil;
15255 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
15256 doc: /* Which keys Emacs uses for the hyper modifier.
15257 This should be one of the symbols `alt', `hyper', `meta', `super'.
15258 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
15259 default is nil, which is the same as `hyper'. */);
15260 Vx_hyper_keysym = Qnil;
15262 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
15263 doc: /* Which keys Emacs uses for the meta modifier.
15264 This should be one of the symbols `alt', `hyper', `meta', `super'.
15265 For example, `meta' means use the Meta_L and Meta_R keysyms. The
15266 default is nil, which is the same as `meta'. */);
15267 Vx_meta_keysym = Qnil;
15269 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
15270 doc: /* Which keys Emacs uses for the super modifier.
15271 This should be one of the symbols `alt', `hyper', `meta', `super'.
15272 For example, `super' means use the Super_L and Super_R keysyms. The
15273 default is nil, which is the same as `super'. */);
15274 Vx_super_keysym = Qnil;
15278 #endif /* HAVE_X_WINDOWS */