*** empty log message ***
[emacs.git] / src / xterm.c
blobb472bde7656c9f901be4e6d8acfe2c888093a273
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 "coding.h"
72 #include "ccl.h"
73 #include "frame.h"
74 #include "dispextern.h"
75 #include "fontset.h"
76 #include "termhooks.h"
77 #include "termopts.h"
78 #include "termchar.h"
79 #if 0
80 #include "sink.h"
81 #include "sinkmask.h"
82 #endif /* ! 0 */
83 #include "gnu.h"
84 #include "disptab.h"
85 #include "buffer.h"
86 #include "window.h"
87 #include "keyboard.h"
88 #include "intervals.h"
89 #include "process.h"
90 #include "atimer.h"
92 #ifdef USE_X_TOOLKIT
93 #include <X11/Shell.h>
94 #endif
96 #ifdef HAVE_SYS_TIME_H
97 #include <sys/time.h>
98 #endif
99 #ifdef HAVE_UNISTD_H
100 #include <unistd.h>
101 #endif
103 #ifdef USE_X_TOOLKIT
105 extern void free_frame_menubar P_ ((struct frame *));
106 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
107 int));
109 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
110 #define HACK_EDITRES
111 extern void _XEditResCheckMessages ();
112 #endif /* not NO_EDITRES */
114 /* Include toolkit specific headers for the scroll bar widget. */
116 #ifdef USE_TOOLKIT_SCROLL_BARS
117 #if defined USE_MOTIF
118 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
119 #include <Xm/ScrollBar.h>
120 #else /* !USE_MOTIF i.e. use Xaw */
122 #ifdef HAVE_XAW3D
123 #include <X11/Xaw3d/Simple.h>
124 #include <X11/Xaw3d/Scrollbar.h>
125 #define ARROW_SCROLLBAR
126 #include <X11/Xaw3d/ScrollbarP.h>
127 #else /* !HAVE_XAW3D */
128 #include <X11/Xaw/Simple.h>
129 #include <X11/Xaw/Scrollbar.h>
130 #endif /* !HAVE_XAW3D */
131 #ifndef XtNpickTop
132 #define XtNpickTop "pickTop"
133 #endif /* !XtNpickTop */
134 #endif /* !USE_MOTIF */
135 #endif /* USE_TOOLKIT_SCROLL_BARS */
137 #endif /* USE_X_TOOLKIT */
139 #ifndef USE_X_TOOLKIT
140 #define x_any_window_to_frame x_window_to_frame
141 #define x_top_window_to_frame x_window_to_frame
142 #endif
144 #ifdef USE_X_TOOLKIT
145 #include "widget.h"
146 #ifndef XtNinitialState
147 #define XtNinitialState "initialState"
148 #endif
149 #endif
151 #ifndef min
152 #define min(a,b) ((a) < (b) ? (a) : (b))
153 #endif
154 #ifndef max
155 #define max(a,b) ((a) > (b) ? (a) : (b))
156 #endif
158 #define abs(x) ((x) < 0 ? -(x) : (x))
160 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
163 /* Bitmaps for truncated lines. */
165 enum bitmap_type
167 NO_BITMAP,
168 LEFT_TRUNCATION_BITMAP,
169 RIGHT_TRUNCATION_BITMAP,
170 OVERLAY_ARROW_BITMAP,
171 CONTINUED_LINE_BITMAP,
172 CONTINUATION_LINE_BITMAP,
173 ZV_LINE_BITMAP
176 /* Bitmap drawn to indicate lines not displaying text if
177 `indicate-empty-lines' is non-nil. */
179 #define zv_width 8
180 #define zv_height 8
181 static unsigned char zv_bits[] = {
182 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
184 /* An arrow like this: `<-'. */
186 #define left_width 8
187 #define left_height 8
188 static unsigned char left_bits[] = {
189 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
191 /* Right truncation arrow bitmap `->'. */
193 #define right_width 8
194 #define right_height 8
195 static unsigned char right_bits[] = {
196 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
198 /* Marker for continued lines. */
200 #define continued_width 8
201 #define continued_height 8
202 static unsigned char continued_bits[] = {
203 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
205 /* Marker for continuation lines. */
207 #define continuation_width 8
208 #define continuation_height 8
209 static unsigned char continuation_bits[] = {
210 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
212 /* Overlay arrow bitmap. */
214 #if 0
215 /* A bomb. */
216 #define ov_width 8
217 #define ov_height 8
218 static unsigned char ov_bits[] = {
219 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
220 #else
221 /* A triangular arrow. */
222 #define ov_width 8
223 #define ov_height 8
224 static unsigned char ov_bits[] = {
225 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
227 #endif
229 extern Lisp_Object Qhelp_echo;
232 /* Non-nil means Emacs uses toolkit scroll bars. */
234 Lisp_Object Vx_toolkit_scroll_bars;
236 /* If a string, XTread_socket generates an event to display that string.
237 (The display is done in read_char.) */
239 static Lisp_Object help_echo;
240 static Lisp_Object help_echo_window;
241 static Lisp_Object help_echo_object;
242 static int help_echo_pos;
244 /* Temporary variable for XTread_socket. */
246 static Lisp_Object previous_help_echo;
248 /* Non-zero means that a HELP_EVENT has been generated since Emacs
249 start. */
251 static int any_help_event_p;
253 /* Non-zero means draw block and hollow cursor as wide as the glyph
254 under it. For example, if a block cursor is over a tab, it will be
255 drawn as wide as that tab on the display. */
257 int x_stretch_cursor_p;
259 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
261 int x_use_underline_position_properties;
263 /* This is a chain of structures for all the X displays currently in
264 use. */
266 struct x_display_info *x_display_list;
268 /* This is a list of cons cells, each of the form (NAME
269 . FONT-LIST-CACHE), one for each element of x_display_list and in
270 the same order. NAME is the name of the frame. FONT-LIST-CACHE
271 records previous values returned by x-list-fonts. */
273 Lisp_Object x_display_name_list;
275 /* Frame being updated by update_frame. This is declared in term.c.
276 This is set by update_begin and looked at by all the XT functions.
277 It is zero while not inside an update. In that case, the XT
278 functions assume that `selected_frame' is the frame to apply to. */
280 extern struct frame *updating_frame;
282 extern int waiting_for_input;
284 /* This is a frame waiting to be auto-raised, within XTread_socket. */
286 struct frame *pending_autoraise_frame;
288 #ifdef USE_X_TOOLKIT
289 /* The application context for Xt use. */
290 XtAppContext Xt_app_con;
291 static String Xt_default_resources[] = {0};
292 #endif /* USE_X_TOOLKIT */
294 /* Nominal cursor position -- where to draw output.
295 HPOS and VPOS are window relative glyph matrix coordinates.
296 X and Y are window relative pixel coordinates. */
298 struct cursor_pos output_cursor;
300 /* Non-zero means user is interacting with a toolkit scroll bar. */
302 static int toolkit_scroll_bar_interaction;
304 /* Mouse movement.
306 Formerly, we used PointerMotionHintMask (in standard_event_mask)
307 so that we would have to call XQueryPointer after each MotionNotify
308 event to ask for another such event. However, this made mouse tracking
309 slow, and there was a bug that made it eventually stop.
311 Simply asking for MotionNotify all the time seems to work better.
313 In order to avoid asking for motion events and then throwing most
314 of them away or busy-polling the server for mouse positions, we ask
315 the server for pointer motion hints. This means that we get only
316 one event per group of mouse movements. "Groups" are delimited by
317 other kinds of events (focus changes and button clicks, for
318 example), or by XQueryPointer calls; when one of these happens, we
319 get another MotionNotify event the next time the mouse moves. This
320 is at least as efficient as getting motion events when mouse
321 tracking is on, and I suspect only negligibly worse when tracking
322 is off. */
324 /* Where the mouse was last time we reported a mouse event. */
326 FRAME_PTR last_mouse_frame;
327 static XRectangle last_mouse_glyph;
328 static Lisp_Object last_mouse_press_frame;
330 /* The scroll bar in which the last X motion event occurred.
332 If the last X motion event occurred in a scroll bar, we set this so
333 XTmouse_position can know whether to report a scroll bar motion or
334 an ordinary motion.
336 If the last X motion event didn't occur in a scroll bar, we set
337 this to Qnil, to tell XTmouse_position to return an ordinary motion
338 event. */
340 static Lisp_Object last_mouse_scroll_bar;
342 /* This is a hack. We would really prefer that XTmouse_position would
343 return the time associated with the position it returns, but there
344 doesn't seem to be any way to wrest the time-stamp from the server
345 along with the position query. So, we just keep track of the time
346 of the last movement we received, and return that in hopes that
347 it's somewhat accurate. */
349 static Time last_mouse_movement_time;
351 /* Incremented by XTread_socket whenever it really tries to read
352 events. */
354 #ifdef __STDC__
355 static int volatile input_signal_count;
356 #else
357 static int input_signal_count;
358 #endif
360 /* Used locally within XTread_socket. */
362 static int x_noop_count;
364 /* Initial values of argv and argc. */
366 extern char **initial_argv;
367 extern int initial_argc;
369 extern Lisp_Object Vcommand_line_args, Vsystem_name;
371 /* Tells if a window manager is present or not. */
373 extern Lisp_Object Vx_no_window_manager;
375 extern Lisp_Object Qface, Qmouse_face;
377 extern int errno;
379 /* A mask of extra modifier bits to put into every keyboard char. */
381 extern int extra_keyboard_modifiers;
383 static Lisp_Object Qvendor_specific_keysyms;
385 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
386 extern Lisp_Object x_icon_type P_ ((struct frame *));
389 /* Enumeration for overriding/changing the face to use for drawing
390 glyphs in x_draw_glyphs. */
392 enum draw_glyphs_face
394 DRAW_NORMAL_TEXT,
395 DRAW_INVERSE_VIDEO,
396 DRAW_CURSOR,
397 DRAW_MOUSE_FACE,
398 DRAW_IMAGE_RAISED,
399 DRAW_IMAGE_SUNKEN
402 static int cursor_in_mouse_face_p P_ ((struct window *));
403 static int clear_mouse_face P_ ((struct x_display_info *));
404 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
405 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
406 static const XColor *x_color_cells P_ ((Display *, int *));
407 static void x_update_window_end P_ ((struct window *, int, int));
408 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
409 void x_delete_display P_ ((struct x_display_info *));
410 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
411 unsigned));
412 static int fast_find_position P_ ((struct window *, int, int *, int *,
413 int *, int *));
414 static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
415 int *, int *, int *, int *, int));
416 static void set_output_cursor P_ ((struct cursor_pos *));
417 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
418 int *, int *, int *, int));
419 static void note_mode_line_highlight P_ ((struct window *, int, int));
420 static void note_mouse_highlight P_ ((struct frame *, int, int));
421 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
422 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
423 static void show_mouse_face P_ ((struct x_display_info *,
424 enum draw_glyphs_face));
425 static int x_io_error_quitter P_ ((Display *));
426 int x_catch_errors P_ ((Display *));
427 void x_uncatch_errors P_ ((Display *, int));
428 void x_lower_frame P_ ((struct frame *));
429 void x_scroll_bar_clear P_ ((struct frame *));
430 int x_had_errors_p P_ ((Display *));
431 void x_wm_set_size_hint P_ ((struct frame *, long, int));
432 void x_raise_frame P_ ((struct frame *));
433 void x_set_window_size P_ ((struct frame *, int, int, int));
434 void x_wm_set_window_state P_ ((struct frame *, int));
435 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
436 void x_initialize P_ ((void));
437 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
438 static int x_compute_min_glyph_bounds P_ ((struct frame *));
439 static void x_draw_phys_cursor_glyph P_ ((struct window *,
440 struct glyph_row *,
441 enum draw_glyphs_face));
442 static void x_update_end P_ ((struct frame *));
443 static void XTframe_up_to_date P_ ((struct frame *));
444 static void XTreassert_line_highlight P_ ((int, int));
445 static void x_change_line_highlight P_ ((int, int, int, int));
446 static void XTset_terminal_modes P_ ((void));
447 static void XTreset_terminal_modes P_ ((void));
448 static void XTcursor_to P_ ((int, int, int, int));
449 static void x_write_glyphs P_ ((struct glyph *, int));
450 static void x_clear_end_of_line P_ ((int));
451 static void x_clear_frame P_ ((void));
452 static void x_clear_cursor P_ ((struct window *));
453 static void frame_highlight P_ ((struct frame *));
454 static void frame_unhighlight P_ ((struct frame *));
455 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
456 static void XTframe_rehighlight P_ ((struct frame *));
457 static void x_frame_rehighlight P_ ((struct x_display_info *));
458 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
459 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
460 static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
461 XRectangle *));
462 static void expose_frame P_ ((struct frame *, int, int, int, int));
463 static int expose_window_tree P_ ((struct window *, XRectangle *));
464 static int expose_window P_ ((struct window *, XRectangle *));
465 static void expose_area P_ ((struct window *, struct glyph_row *,
466 XRectangle *, enum glyph_row_area));
467 static int expose_line P_ ((struct window *, struct glyph_row *,
468 XRectangle *));
469 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
470 static void x_update_window_cursor P_ ((struct window *, int));
471 static void x_erase_phys_cursor P_ ((struct window *));
472 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
473 static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
474 enum bitmap_type));
476 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
477 GC, int));
478 static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
479 static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
480 static void note_overwritten_text_cursor P_ ((struct window *, int, int));
481 static void x_flush P_ ((struct frame *f));
482 static void x_update_begin P_ ((struct frame *));
483 static void x_update_window_begin P_ ((struct window *));
484 static void x_draw_vertical_border P_ ((struct window *));
485 static void x_after_update_window_line P_ ((struct glyph_row *));
486 static INLINE void take_vertical_position_into_account P_ ((struct it *));
487 static void x_produce_stretch_glyph P_ ((struct it *));
488 static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
489 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
490 enum scroll_bar_part *,
491 Lisp_Object *, Lisp_Object *,
492 unsigned long *));
494 /* Flush display of frame F, or of all frames if F is null. */
496 static void
497 x_flush (f)
498 struct frame *f;
500 BLOCK_INPUT;
501 if (f == NULL)
503 Lisp_Object rest, frame;
504 FOR_EACH_FRAME (rest, frame)
505 x_flush (XFRAME (frame));
507 else if (FRAME_X_P (f))
508 XFlush (FRAME_X_DISPLAY (f));
509 UNBLOCK_INPUT;
513 /* Remove calls to XFlush by defining XFlush to an empty replacement.
514 Calls to XFlush should be unnecessary because the X output buffer
515 is flushed automatically as needed by calls to XPending,
516 XNextEvent, or XWindowEvent according to the XFlush man page.
517 XTread_socket calls XPending. Removing XFlush improves
518 performance. */
520 #define XFlush(DISPLAY) (void) 0
523 /***********************************************************************
524 Debugging
525 ***********************************************************************/
527 #if 0
529 /* This is a function useful for recording debugging information about
530 the sequence of occurrences in this file. */
532 struct record
534 char *locus;
535 int type;
538 struct record event_record[100];
540 int event_record_index;
542 record_event (locus, type)
543 char *locus;
544 int type;
546 if (event_record_index == sizeof (event_record) / sizeof (struct record))
547 event_record_index = 0;
549 event_record[event_record_index].locus = locus;
550 event_record[event_record_index].type = type;
551 event_record_index++;
554 #endif /* 0 */
558 /* Return the struct x_display_info corresponding to DPY. */
560 struct x_display_info *
561 x_display_info_for_display (dpy)
562 Display *dpy;
564 struct x_display_info *dpyinfo;
566 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
567 if (dpyinfo->display == dpy)
568 return dpyinfo;
570 return 0;
575 /***********************************************************************
576 Starting and ending an update
577 ***********************************************************************/
579 /* Start an update of frame F. This function is installed as a hook
580 for update_begin, i.e. it is called when update_begin is called.
581 This function is called prior to calls to x_update_window_begin for
582 each window being updated. Currently, there is nothing to do here
583 because all interesting stuff is done on a window basis. */
585 static void
586 x_update_begin (f)
587 struct frame *f;
589 /* Nothing to do. */
593 /* Start update of window W. Set the global variable updated_window
594 to the window being updated and set output_cursor to the cursor
595 position of W. */
597 static void
598 x_update_window_begin (w)
599 struct window *w;
601 struct frame *f = XFRAME (WINDOW_FRAME (w));
602 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
604 updated_window = w;
605 set_output_cursor (&w->cursor);
607 BLOCK_INPUT;
609 if (f == display_info->mouse_face_mouse_frame)
611 /* Don't do highlighting for mouse motion during the update. */
612 display_info->mouse_face_defer = 1;
614 /* If F needs to be redrawn, simply forget about any prior mouse
615 highlighting. */
616 if (FRAME_GARBAGED_P (f))
617 display_info->mouse_face_window = Qnil;
619 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
620 their mouse_face_p flag set, which means that they are always
621 unequal to rows in a desired matrix which never have that
622 flag set. So, rows containing mouse-face glyphs are never
623 scrolled, and we don't have to switch the mouse highlight off
624 here to prevent it from being scrolled. */
626 /* Can we tell that this update does not affect the window
627 where the mouse highlight is? If so, no need to turn off.
628 Likewise, don't do anything if the frame is garbaged;
629 in that case, the frame's current matrix that we would use
630 is all wrong, and we will redisplay that line anyway. */
631 if (!NILP (display_info->mouse_face_window)
632 && w == XWINDOW (display_info->mouse_face_window))
634 int i;
636 for (i = 0; i < w->desired_matrix->nrows; ++i)
637 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
638 break;
640 if (i < w->desired_matrix->nrows)
641 clear_mouse_face (display_info);
643 #endif /* 0 */
646 UNBLOCK_INPUT;
650 /* Draw a vertical window border to the right of window W if W doesn't
651 have vertical scroll bars. */
653 static void
654 x_draw_vertical_border (w)
655 struct window *w;
657 struct frame *f = XFRAME (WINDOW_FRAME (w));
659 /* Redraw borders between horizontally adjacent windows. Don't
660 do it for frames with vertical scroll bars because either the
661 right scroll bar of a window, or the left scroll bar of its
662 neighbor will suffice as a border. */
663 if (!WINDOW_RIGHTMOST_P (w)
664 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
666 int x0, x1, y0, y1;
668 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
669 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
670 y1 -= 1;
672 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
673 f->output_data.x->normal_gc, x1, y0, x1, y1);
678 /* End update of window W (which is equal to updated_window).
680 Draw vertical borders between horizontally adjacent windows, and
681 display W's cursor if CURSOR_ON_P is non-zero.
683 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
684 glyphs in mouse-face were overwritten. In that case we have to
685 make sure that the mouse-highlight is properly redrawn.
687 W may be a menu bar pseudo-window in case we don't have X toolkit
688 support. Such windows don't have a cursor, so don't display it
689 here. */
691 static void
692 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
693 struct window *w;
694 int cursor_on_p, mouse_face_overwritten_p;
696 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
698 if (!w->pseudo_window_p)
700 BLOCK_INPUT;
702 if (cursor_on_p)
703 x_display_and_set_cursor (w, 1, output_cursor.hpos,
704 output_cursor.vpos,
705 output_cursor.x, output_cursor.y);
707 x_draw_vertical_border (w);
708 UNBLOCK_INPUT;
711 /* If a row with mouse-face was overwritten, arrange for
712 XTframe_up_to_date to redisplay the mouse highlight. */
713 if (mouse_face_overwritten_p)
715 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
716 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
717 dpyinfo->mouse_face_window = Qnil;
720 updated_window = NULL;
724 /* End update of frame F. This function is installed as a hook in
725 update_end. */
727 static void
728 x_update_end (f)
729 struct frame *f;
731 /* Mouse highlight may be displayed again. */
732 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
734 BLOCK_INPUT;
735 XFlush (FRAME_X_DISPLAY (f));
736 UNBLOCK_INPUT;
740 /* This function is called from various places in xdisp.c whenever a
741 complete update has been performed. The global variable
742 updated_window is not available here. */
744 static void
745 XTframe_up_to_date (f)
746 struct frame *f;
748 if (FRAME_X_P (f))
750 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
752 if (dpyinfo->mouse_face_deferred_gc
753 || f == dpyinfo->mouse_face_mouse_frame)
755 BLOCK_INPUT;
756 if (dpyinfo->mouse_face_mouse_frame)
757 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
758 dpyinfo->mouse_face_mouse_x,
759 dpyinfo->mouse_face_mouse_y);
760 dpyinfo->mouse_face_deferred_gc = 0;
761 UNBLOCK_INPUT;
767 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
768 arrow bitmaps, or clear the areas where they would be displayed
769 before DESIRED_ROW is made current. The window being updated is
770 found in updated_window. This function It is called from
771 update_window_line only if it is known that there are differences
772 between bitmaps to be drawn between current row and DESIRED_ROW. */
774 static void
775 x_after_update_window_line (desired_row)
776 struct glyph_row *desired_row;
778 struct window *w = updated_window;
780 xassert (w);
782 if (!desired_row->mode_line_p && !w->pseudo_window_p)
784 struct frame *f;
785 int width;
787 BLOCK_INPUT;
788 x_draw_row_bitmaps (w, desired_row);
790 /* When a window has disappeared, make sure that no rest of
791 full-width rows stays visible in the internal border. */
792 if (windows_or_buffers_changed
793 && (f = XFRAME (w->frame),
794 width = FRAME_INTERNAL_BORDER_WIDTH (f),
795 width != 0))
797 int height = desired_row->visible_height;
798 int x = (window_box_right (w, -1)
799 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
800 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
802 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
803 x, y, width, height, False);
806 UNBLOCK_INPUT;
811 /* Draw the bitmap WHICH in one of the areas to the left or right of
812 window W. ROW is the glyph row for which to display the bitmap; it
813 determines the vertical position at which the bitmap has to be
814 drawn. */
816 static void
817 x_draw_bitmap (w, row, which)
818 struct window *w;
819 struct glyph_row *row;
820 enum bitmap_type which;
822 struct frame *f = XFRAME (WINDOW_FRAME (w));
823 Display *display = FRAME_X_DISPLAY (f);
824 Window window = FRAME_X_WINDOW (f);
825 int x, y, wd, h, dy;
826 unsigned char *bits;
827 Pixmap pixmap;
828 GC gc = f->output_data.x->normal_gc;
829 struct face *face;
830 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
832 /* Must clip because of partially visible lines. */
833 x_clip_to_row (w, row, gc, 1);
835 switch (which)
837 case LEFT_TRUNCATION_BITMAP:
838 wd = left_width;
839 h = left_height;
840 bits = left_bits;
841 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
842 - wd
843 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
844 break;
846 case OVERLAY_ARROW_BITMAP:
847 wd = left_width;
848 h = left_height;
849 bits = ov_bits;
850 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
851 - wd
852 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
853 break;
855 case RIGHT_TRUNCATION_BITMAP:
856 wd = right_width;
857 h = right_height;
858 bits = right_bits;
859 x = window_box_right (w, -1);
860 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
861 break;
863 case CONTINUED_LINE_BITMAP:
864 wd = right_width;
865 h = right_height;
866 bits = continued_bits;
867 x = window_box_right (w, -1);
868 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
869 break;
871 case CONTINUATION_LINE_BITMAP:
872 wd = continuation_width;
873 h = continuation_height;
874 bits = continuation_bits;
875 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
876 - wd
877 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
878 break;
880 case ZV_LINE_BITMAP:
881 wd = zv_width;
882 h = zv_height;
883 bits = zv_bits;
884 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
885 - wd
886 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
887 break;
889 default:
890 abort ();
893 /* Convert to frame coordinates. Set dy to the offset in the row to
894 start drawing the bitmap. */
895 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
896 dy = (row->height - h) / 2;
898 /* Draw the bitmap. I believe these small pixmaps can be cached
899 by the server. */
900 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
901 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
902 face->foreground,
903 face->background, depth);
904 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
905 XFreePixmap (display, pixmap);
906 XSetClipMask (display, gc, None);
910 /* Draw flags bitmaps for glyph row ROW on window W. Call this
911 function with input blocked. */
913 static void
914 x_draw_row_bitmaps (w, row)
915 struct window *w;
916 struct glyph_row *row;
918 struct frame *f = XFRAME (w->frame);
919 enum bitmap_type bitmap;
920 struct face *face;
921 int header_line_height = -1;
923 xassert (interrupt_input_blocked);
925 /* If row is completely invisible, because of vscrolling, we
926 don't have to draw anything. */
927 if (row->visible_height <= 0)
928 return;
930 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
931 PREPARE_FACE_FOR_DISPLAY (f, face);
933 /* Decide which bitmap to draw at the left side. */
934 if (row->overlay_arrow_p)
935 bitmap = OVERLAY_ARROW_BITMAP;
936 else if (row->truncated_on_left_p)
937 bitmap = LEFT_TRUNCATION_BITMAP;
938 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
939 bitmap = CONTINUATION_LINE_BITMAP;
940 else if (row->indicate_empty_line_p)
941 bitmap = ZV_LINE_BITMAP;
942 else
943 bitmap = NO_BITMAP;
945 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
946 the flags area. */
947 if (bitmap == NO_BITMAP
948 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
949 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
951 /* If W has a vertical border to its left, don't draw over it. */
952 int border = ((XFASTINT (w->left) > 0
953 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
954 ? 1 : 0);
955 int left = window_box_left (w, -1);
957 if (header_line_height < 0)
958 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
960 /* In case the same realized face is used for bitmap areas and
961 for something displayed in the text (e.g. face `region' on
962 mono-displays, the fill style may have been changed to
963 FillSolid in x_draw_glyph_string_background. */
964 if (face->stipple)
965 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
966 else
967 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
969 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
970 face->gc,
971 (left
972 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
973 + border),
974 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
975 row->y)),
976 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
977 row->visible_height);
978 if (!face->stipple)
979 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
982 /* Draw the left bitmap. */
983 if (bitmap != NO_BITMAP)
984 x_draw_bitmap (w, row, bitmap);
986 /* Decide which bitmap to draw at the right side. */
987 if (row->truncated_on_right_p)
988 bitmap = RIGHT_TRUNCATION_BITMAP;
989 else if (row->continued_p)
990 bitmap = CONTINUED_LINE_BITMAP;
991 else
992 bitmap = NO_BITMAP;
994 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
995 the flags area. */
996 if (bitmap == NO_BITMAP
997 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
998 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
1000 int right = window_box_right (w, -1);
1002 if (header_line_height < 0)
1003 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1005 /* In case the same realized face is used for bitmap areas and
1006 for something displayed in the text (e.g. face `region' on
1007 mono-displays, the fill style may have been changed to
1008 FillSolid in x_draw_glyph_string_background. */
1009 if (face->stipple)
1010 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
1011 else
1012 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
1013 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1014 face->gc,
1015 right,
1016 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
1017 row->y)),
1018 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
1019 row->visible_height);
1020 if (!face->stipple)
1021 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
1024 /* Draw the right bitmap. */
1025 if (bitmap != NO_BITMAP)
1026 x_draw_bitmap (w, row, bitmap);
1030 /***********************************************************************
1031 Line Highlighting
1032 ***********************************************************************/
1034 /* External interface to control of standout mode. Not used for X
1035 frames. Aborts when called. */
1037 static void
1038 XTreassert_line_highlight (new, vpos)
1039 int new, vpos;
1041 abort ();
1045 /* Call this when about to modify line at position VPOS and change
1046 whether it is highlighted. Not used for X frames. Aborts when
1047 called. */
1049 static void
1050 x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1051 int new_highlight, vpos, y, first_unused_hpos;
1053 abort ();
1057 /* This is called when starting Emacs and when restarting after
1058 suspend. When starting Emacs, no X window is mapped. And nothing
1059 must be done to Emacs's own window if it is suspended (though that
1060 rarely happens). */
1062 static void
1063 XTset_terminal_modes ()
1067 /* This is called when exiting or suspending Emacs. Exiting will make
1068 the X-windows go away, and suspending requires no action. */
1070 static void
1071 XTreset_terminal_modes ()
1077 /***********************************************************************
1078 Output Cursor
1079 ***********************************************************************/
1081 /* Set the global variable output_cursor to CURSOR. All cursor
1082 positions are relative to updated_window. */
1084 static void
1085 set_output_cursor (cursor)
1086 struct cursor_pos *cursor;
1088 output_cursor.hpos = cursor->hpos;
1089 output_cursor.vpos = cursor->vpos;
1090 output_cursor.x = cursor->x;
1091 output_cursor.y = cursor->y;
1095 /* Set a nominal cursor position.
1097 HPOS and VPOS are column/row positions in a window glyph matrix. X
1098 and Y are window text area relative pixel positions.
1100 If this is done during an update, updated_window will contain the
1101 window that is being updated and the position is the future output
1102 cursor position for that window. If updated_window is null, use
1103 selected_window and display the cursor at the given position. */
1105 static void
1106 XTcursor_to (vpos, hpos, y, x)
1107 int vpos, hpos, y, x;
1109 struct window *w;
1111 /* If updated_window is not set, work on selected_window. */
1112 if (updated_window)
1113 w = updated_window;
1114 else
1115 w = XWINDOW (selected_window);
1117 /* Set the output cursor. */
1118 output_cursor.hpos = hpos;
1119 output_cursor.vpos = vpos;
1120 output_cursor.x = x;
1121 output_cursor.y = y;
1123 /* If not called as part of an update, really display the cursor.
1124 This will also set the cursor position of W. */
1125 if (updated_window == NULL)
1127 BLOCK_INPUT;
1128 x_display_cursor (w, 1, hpos, vpos, x, y);
1129 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1130 UNBLOCK_INPUT;
1136 /***********************************************************************
1137 Display Iterator
1138 ***********************************************************************/
1140 /* Function prototypes of this page. */
1142 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1143 struct glyph *,
1144 XChar2b *,
1145 int *));
1146 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1147 int, XChar2b *, int));
1148 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1149 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1150 static void x_append_glyph P_ ((struct it *));
1151 static void x_append_composite_glyph P_ ((struct it *));
1152 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1153 int, int, double));
1154 static void x_produce_glyphs P_ ((struct it *));
1155 static void x_produce_image_glyph P_ ((struct it *it));
1158 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1159 is not contained in the font. */
1161 static INLINE XCharStruct *
1162 x_per_char_metric (font, char2b)
1163 XFontStruct *font;
1164 XChar2b *char2b;
1166 /* The result metric information. */
1167 XCharStruct *pcm = NULL;
1169 xassert (font && char2b);
1171 if (font->per_char != NULL)
1173 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1175 /* min_char_or_byte2 specifies the linear character index
1176 corresponding to the first element of the per_char array,
1177 max_char_or_byte2 is the index of the last character. A
1178 character with non-zero CHAR2B->byte1 is not in the font.
1179 A character with byte2 less than min_char_or_byte2 or
1180 greater max_char_or_byte2 is not in the font. */
1181 if (char2b->byte1 == 0
1182 && char2b->byte2 >= font->min_char_or_byte2
1183 && char2b->byte2 <= font->max_char_or_byte2)
1184 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1186 else
1188 /* If either min_byte1 or max_byte1 are nonzero, both
1189 min_char_or_byte2 and max_char_or_byte2 are less than
1190 256, and the 2-byte character index values corresponding
1191 to the per_char array element N (counting from 0) are:
1193 byte1 = N/D + min_byte1
1194 byte2 = N\D + min_char_or_byte2
1196 where:
1198 D = max_char_or_byte2 - min_char_or_byte2 + 1
1199 / = integer division
1200 \ = integer modulus */
1201 if (char2b->byte1 >= font->min_byte1
1202 && char2b->byte1 <= font->max_byte1
1203 && char2b->byte2 >= font->min_char_or_byte2
1204 && char2b->byte2 <= font->max_char_or_byte2)
1206 pcm = (font->per_char
1207 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1208 * (char2b->byte1 - font->min_byte1))
1209 + (char2b->byte2 - font->min_char_or_byte2));
1213 else
1215 /* If the per_char pointer is null, all glyphs between the first
1216 and last character indexes inclusive have the same
1217 information, as given by both min_bounds and max_bounds. */
1218 if (char2b->byte2 >= font->min_char_or_byte2
1219 && char2b->byte2 <= font->max_char_or_byte2)
1220 pcm = &font->max_bounds;
1223 return ((pcm == NULL
1224 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1225 ? NULL : pcm);
1229 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1230 the two-byte form of C. Encoding is returned in *CHAR2B. */
1232 static INLINE void
1233 x_encode_char (c, char2b, font_info)
1234 int c;
1235 XChar2b *char2b;
1236 struct font_info *font_info;
1238 int charset = CHAR_CHARSET (c);
1239 XFontStruct *font = font_info->font;
1241 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1242 This may be either a program in a special encoder language or a
1243 fixed encoding. */
1244 if (font_info->font_encoder)
1246 /* It's a program. */
1247 struct ccl_program *ccl = font_info->font_encoder;
1249 if (CHARSET_DIMENSION (charset) == 1)
1251 ccl->reg[0] = charset;
1252 ccl->reg[1] = char2b->byte2;
1254 else
1256 ccl->reg[0] = charset;
1257 ccl->reg[1] = char2b->byte1;
1258 ccl->reg[2] = char2b->byte2;
1261 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1263 /* We assume that MSBs are appropriately set/reset by CCL
1264 program. */
1265 if (font->max_byte1 == 0) /* 1-byte font */
1266 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1267 else
1268 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1270 else if (font_info->encoding[charset])
1272 /* Fixed encoding scheme. See fontset.h for the meaning of the
1273 encoding numbers. */
1274 int enc = font_info->encoding[charset];
1276 if ((enc == 1 || enc == 2)
1277 && CHARSET_DIMENSION (charset) == 2)
1278 char2b->byte1 |= 0x80;
1280 if (enc == 1 || enc == 3)
1281 char2b->byte2 |= 0x80;
1286 /* Get face and two-byte form of character C in face FACE_ID on frame
1287 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1288 means we want to display multibyte text. Value is a pointer to a
1289 realized face that is ready for display. */
1291 static INLINE struct face *
1292 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1293 struct frame *f;
1294 int c, face_id;
1295 XChar2b *char2b;
1296 int multibyte_p;
1298 struct face *face = FACE_FROM_ID (f, face_id);
1300 if (!multibyte_p)
1302 /* Unibyte case. We don't have to encode, but we have to make
1303 sure to use a face suitable for unibyte. */
1304 char2b->byte1 = 0;
1305 char2b->byte2 = c;
1306 face_id = FACE_FOR_CHAR (f, face, c);
1307 face = FACE_FROM_ID (f, face_id);
1309 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1311 /* Case of ASCII in a face known to fit ASCII. */
1312 char2b->byte1 = 0;
1313 char2b->byte2 = c;
1315 else
1317 int c1, c2, charset;
1319 /* Split characters into bytes. If c2 is -1 afterwards, C is
1320 really a one-byte character so that byte1 is zero. */
1321 SPLIT_CHAR (c, charset, c1, c2);
1322 if (c2 > 0)
1323 char2b->byte1 = c1, char2b->byte2 = c2;
1324 else
1325 char2b->byte1 = 0, char2b->byte2 = c1;
1327 /* Maybe encode the character in *CHAR2B. */
1328 if (face->font != NULL)
1330 struct font_info *font_info
1331 = FONT_INFO_FROM_ID (f, face->font_info_id);
1332 if (font_info)
1333 x_encode_char (c, char2b, font_info);
1337 /* Make sure X resources of the face are allocated. */
1338 xassert (face != NULL);
1339 PREPARE_FACE_FOR_DISPLAY (f, face);
1341 return face;
1345 /* Get face and two-byte form of character glyph GLYPH on frame F.
1346 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1347 a pointer to a realized face that is ready for display. */
1349 static INLINE struct face *
1350 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1351 struct frame *f;
1352 struct glyph *glyph;
1353 XChar2b *char2b;
1354 int *two_byte_p;
1356 struct face *face;
1358 xassert (glyph->type == CHAR_GLYPH);
1359 face = FACE_FROM_ID (f, glyph->face_id);
1361 if (two_byte_p)
1362 *two_byte_p = 0;
1364 if (!glyph->multibyte_p)
1366 /* Unibyte case. We don't have to encode, but we have to make
1367 sure to use a face suitable for unibyte. */
1368 char2b->byte1 = 0;
1369 char2b->byte2 = glyph->u.ch;
1371 else if (glyph->u.ch < 128
1372 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1374 /* Case of ASCII in a face known to fit ASCII. */
1375 char2b->byte1 = 0;
1376 char2b->byte2 = glyph->u.ch;
1378 else
1380 int c1, c2, charset;
1382 /* Split characters into bytes. If c2 is -1 afterwards, C is
1383 really a one-byte character so that byte1 is zero. */
1384 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1385 if (c2 > 0)
1386 char2b->byte1 = c1, char2b->byte2 = c2;
1387 else
1388 char2b->byte1 = 0, char2b->byte2 = c1;
1390 /* Maybe encode the character in *CHAR2B. */
1391 if (charset != CHARSET_ASCII)
1393 struct font_info *font_info
1394 = FONT_INFO_FROM_ID (f, face->font_info_id);
1395 if (font_info)
1397 x_encode_char (glyph->u.ch, char2b, font_info);
1398 if (two_byte_p)
1399 *two_byte_p
1400 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1405 /* Make sure X resources of the face are allocated. */
1406 xassert (face != NULL);
1407 PREPARE_FACE_FOR_DISPLAY (f, face);
1408 return face;
1412 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1413 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1415 static INLINE void
1416 x_append_glyph (it)
1417 struct it *it;
1419 struct glyph *glyph;
1420 enum glyph_row_area area = it->area;
1422 xassert (it->glyph_row);
1423 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1425 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1426 if (glyph < it->glyph_row->glyphs[area + 1])
1428 glyph->charpos = CHARPOS (it->position);
1429 glyph->object = it->object;
1430 glyph->pixel_width = it->pixel_width;
1431 glyph->voffset = it->voffset;
1432 glyph->type = CHAR_GLYPH;
1433 glyph->multibyte_p = it->multibyte_p;
1434 glyph->left_box_line_p = it->start_of_box_run_p;
1435 glyph->right_box_line_p = it->end_of_box_run_p;
1436 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1437 || it->phys_descent > it->descent);
1438 glyph->padding_p = 0;
1439 glyph->glyph_not_available_p = it->glyph_not_available_p;
1440 glyph->face_id = it->face_id;
1441 glyph->u.ch = it->char_to_display;
1442 ++it->glyph_row->used[area];
1446 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1447 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1449 static INLINE void
1450 x_append_composite_glyph (it)
1451 struct it *it;
1453 struct glyph *glyph;
1454 enum glyph_row_area area = it->area;
1456 xassert (it->glyph_row);
1458 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1459 if (glyph < it->glyph_row->glyphs[area + 1])
1461 glyph->charpos = CHARPOS (it->position);
1462 glyph->object = it->object;
1463 glyph->pixel_width = it->pixel_width;
1464 glyph->voffset = it->voffset;
1465 glyph->type = COMPOSITE_GLYPH;
1466 glyph->multibyte_p = it->multibyte_p;
1467 glyph->left_box_line_p = it->start_of_box_run_p;
1468 glyph->right_box_line_p = it->end_of_box_run_p;
1469 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1470 || it->phys_descent > it->descent);
1471 glyph->padding_p = 0;
1472 glyph->glyph_not_available_p = 0;
1473 glyph->face_id = it->face_id;
1474 glyph->u.cmp_id = it->cmp_id;
1475 ++it->glyph_row->used[area];
1480 /* Change IT->ascent and IT->height according to the setting of
1481 IT->voffset. */
1483 static INLINE void
1484 take_vertical_position_into_account (it)
1485 struct it *it;
1487 if (it->voffset)
1489 if (it->voffset < 0)
1490 /* Increase the ascent so that we can display the text higher
1491 in the line. */
1492 it->ascent += abs (it->voffset);
1493 else
1494 /* Increase the descent so that we can display the text lower
1495 in the line. */
1496 it->descent += it->voffset;
1501 /* Produce glyphs/get display metrics for the image IT is loaded with.
1502 See the description of struct display_iterator in dispextern.h for
1503 an overview of struct display_iterator. */
1505 static void
1506 x_produce_image_glyph (it)
1507 struct it *it;
1509 struct image *img;
1510 struct face *face;
1512 xassert (it->what == IT_IMAGE);
1514 face = FACE_FROM_ID (it->f, it->face_id);
1515 img = IMAGE_FROM_ID (it->f, it->image_id);
1516 xassert (img);
1518 /* Make sure X resources of the face and image are loaded. */
1519 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1520 prepare_image_for_display (it->f, img);
1522 it->ascent = it->phys_ascent = image_ascent (img, face);
1523 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1524 it->pixel_width = img->width + 2 * img->hmargin;
1526 it->nglyphs = 1;
1528 if (face->box != FACE_NO_BOX)
1530 if (face->box_line_width > 0)
1532 it->ascent += face->box_line_width;
1533 it->descent += face->box_line_width;
1536 if (it->start_of_box_run_p)
1537 it->pixel_width += abs (face->box_line_width);
1538 if (it->end_of_box_run_p)
1539 it->pixel_width += abs (face->box_line_width);
1542 take_vertical_position_into_account (it);
1544 if (it->glyph_row)
1546 struct glyph *glyph;
1547 enum glyph_row_area area = it->area;
1549 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1550 if (glyph < it->glyph_row->glyphs[area + 1])
1552 glyph->charpos = CHARPOS (it->position);
1553 glyph->object = it->object;
1554 glyph->pixel_width = it->pixel_width;
1555 glyph->voffset = it->voffset;
1556 glyph->type = IMAGE_GLYPH;
1557 glyph->multibyte_p = it->multibyte_p;
1558 glyph->left_box_line_p = it->start_of_box_run_p;
1559 glyph->right_box_line_p = it->end_of_box_run_p;
1560 glyph->overlaps_vertically_p = 0;
1561 glyph->padding_p = 0;
1562 glyph->glyph_not_available_p = 0;
1563 glyph->face_id = it->face_id;
1564 glyph->u.img_id = img->id;
1565 ++it->glyph_row->used[area];
1571 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1572 of the glyph, WIDTH and HEIGHT are the width and height of the
1573 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1574 ascent of the glyph (0 <= ASCENT <= 1). */
1576 static void
1577 x_append_stretch_glyph (it, object, width, height, ascent)
1578 struct it *it;
1579 Lisp_Object object;
1580 int width, height;
1581 double ascent;
1583 struct glyph *glyph;
1584 enum glyph_row_area area = it->area;
1586 xassert (ascent >= 0 && ascent <= 1);
1588 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1589 if (glyph < it->glyph_row->glyphs[area + 1])
1591 glyph->charpos = CHARPOS (it->position);
1592 glyph->object = object;
1593 glyph->pixel_width = width;
1594 glyph->voffset = it->voffset;
1595 glyph->type = STRETCH_GLYPH;
1596 glyph->multibyte_p = it->multibyte_p;
1597 glyph->left_box_line_p = it->start_of_box_run_p;
1598 glyph->right_box_line_p = it->end_of_box_run_p;
1599 glyph->overlaps_vertically_p = 0;
1600 glyph->padding_p = 0;
1601 glyph->glyph_not_available_p = 0;
1602 glyph->face_id = it->face_id;
1603 glyph->u.stretch.ascent = height * ascent;
1604 glyph->u.stretch.height = height;
1605 ++it->glyph_row->used[area];
1610 /* Produce a stretch glyph for iterator IT. IT->object is the value
1611 of the glyph property displayed. The value must be a list
1612 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1613 being recognized:
1615 1. `:width WIDTH' specifies that the space should be WIDTH *
1616 canonical char width wide. WIDTH may be an integer or floating
1617 point number.
1619 2. `:relative-width FACTOR' specifies that the width of the stretch
1620 should be computed from the width of the first character having the
1621 `glyph' property, and should be FACTOR times that width.
1623 3. `:align-to HPOS' specifies that the space should be wide enough
1624 to reach HPOS, a value in canonical character units.
1626 Exactly one of the above pairs must be present.
1628 4. `:height HEIGHT' specifies that the height of the stretch produced
1629 should be HEIGHT, measured in canonical character units.
1631 5. `:relative-height FACTOR' specifies that the height of the the
1632 stretch should be FACTOR times the height of the characters having
1633 the glyph property.
1635 Either none or exactly one of 4 or 5 must be present.
1637 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1638 of the stretch should be used for the ascent of the stretch.
1639 ASCENT must be in the range 0 <= ASCENT <= 100. */
1641 #define NUMVAL(X) \
1642 ((INTEGERP (X) || FLOATP (X)) \
1643 ? XFLOATINT (X) \
1644 : - 1)
1647 static void
1648 x_produce_stretch_glyph (it)
1649 struct it *it;
1651 /* (space :width WIDTH :height HEIGHT. */
1652 #if GLYPH_DEBUG
1653 extern Lisp_Object Qspace;
1654 #endif
1655 extern Lisp_Object QCwidth, QCheight, QCascent;
1656 extern Lisp_Object QCrelative_width, QCrelative_height;
1657 extern Lisp_Object QCalign_to;
1658 Lisp_Object prop, plist;
1659 double width = 0, height = 0, ascent = 0;
1660 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1661 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1663 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1665 /* List should start with `space'. */
1666 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1667 plist = XCDR (it->object);
1669 /* Compute the width of the stretch. */
1670 if (prop = Fplist_get (plist, QCwidth),
1671 NUMVAL (prop) > 0)
1672 /* Absolute width `:width WIDTH' specified and valid. */
1673 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1674 else if (prop = Fplist_get (plist, QCrelative_width),
1675 NUMVAL (prop) > 0)
1677 /* Relative width `:relative-width FACTOR' specified and valid.
1678 Compute the width of the characters having the `glyph'
1679 property. */
1680 struct it it2;
1681 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1683 it2 = *it;
1684 if (it->multibyte_p)
1686 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1687 - IT_BYTEPOS (*it));
1688 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1690 else
1691 it2.c = *p, it2.len = 1;
1693 it2.glyph_row = NULL;
1694 it2.what = IT_CHARACTER;
1695 x_produce_glyphs (&it2);
1696 width = NUMVAL (prop) * it2.pixel_width;
1698 else if (prop = Fplist_get (plist, QCalign_to),
1699 NUMVAL (prop) > 0)
1700 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1701 else
1702 /* Nothing specified -> width defaults to canonical char width. */
1703 width = CANON_X_UNIT (it->f);
1705 /* Compute height. */
1706 if (prop = Fplist_get (plist, QCheight),
1707 NUMVAL (prop) > 0)
1708 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1709 else if (prop = Fplist_get (plist, QCrelative_height),
1710 NUMVAL (prop) > 0)
1711 height = FONT_HEIGHT (font) * NUMVAL (prop);
1712 else
1713 height = FONT_HEIGHT (font);
1715 /* Compute percentage of height used for ascent. If
1716 `:ascent ASCENT' is present and valid, use that. Otherwise,
1717 derive the ascent from the font in use. */
1718 if (prop = Fplist_get (plist, QCascent),
1719 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1720 ascent = NUMVAL (prop) / 100.0;
1721 else
1722 ascent = (double) font->ascent / FONT_HEIGHT (font);
1724 if (width <= 0)
1725 width = 1;
1726 if (height <= 0)
1727 height = 1;
1729 if (it->glyph_row)
1731 Lisp_Object object = it->stack[it->sp - 1].string;
1732 if (!STRINGP (object))
1733 object = it->w->buffer;
1734 x_append_stretch_glyph (it, object, width, height, ascent);
1737 it->pixel_width = width;
1738 it->ascent = it->phys_ascent = height * ascent;
1739 it->descent = it->phys_descent = height - it->ascent;
1740 it->nglyphs = 1;
1742 if (face->box != FACE_NO_BOX)
1744 if (face->box_line_width > 0)
1746 it->ascent += face->box_line_width;
1747 it->descent += face->box_line_width;
1750 if (it->start_of_box_run_p)
1751 it->pixel_width += abs (face->box_line_width);
1752 if (it->end_of_box_run_p)
1753 it->pixel_width += abs (face->box_line_width);
1756 take_vertical_position_into_account (it);
1759 /* Return proper value to be used as baseline offset of font that has
1760 ASCENT and DESCENT to draw characters by the font at the vertical
1761 center of the line of frame F.
1763 Here, out task is to find the value of BOFF in the following figure;
1765 -------------------------+-----------+-
1766 -+-+---------+-+ | |
1767 | | | | | |
1768 | | | | F_ASCENT F_HEIGHT
1769 | | | ASCENT | |
1770 HEIGHT | | | | |
1771 | | |-|-+------+-----------|------- baseline
1772 | | | | BOFF | |
1773 | |---------|-+-+ | |
1774 | | | DESCENT | |
1775 -+-+---------+-+ F_DESCENT |
1776 -------------------------+-----------+-
1778 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1779 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1780 DESCENT = FONT->descent
1781 HEIGHT = FONT_HEIGHT (FONT)
1782 F_DESCENT = (F->output_data.x->font->descent
1783 - F->output_data.x->baseline_offset)
1784 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1787 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1788 ((FONT)->descent \
1789 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1790 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1791 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1793 /* Produce glyphs/get display metrics for the display element IT is
1794 loaded with. See the description of struct display_iterator in
1795 dispextern.h for an overview of struct display_iterator. */
1797 static void
1798 x_produce_glyphs (it)
1799 struct it *it;
1801 it->glyph_not_available_p = 0;
1803 if (it->what == IT_CHARACTER)
1805 XChar2b char2b;
1806 XFontStruct *font;
1807 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1808 XCharStruct *pcm;
1809 int font_not_found_p;
1810 struct font_info *font_info;
1811 int boff; /* baseline offset */
1812 /* We may change it->multibyte_p upon unibyte<->multibyte
1813 conversion. So, save the current value now and restore it
1814 later.
1816 Note: It seems that we don't have to record multibyte_p in
1817 struct glyph because the character code itself tells if or
1818 not the character is multibyte. Thus, in the future, we must
1819 consider eliminating the field `multibyte_p' in the struct
1820 glyph. */
1821 int saved_multibyte_p = it->multibyte_p;
1823 /* Maybe translate single-byte characters to multibyte, or the
1824 other way. */
1825 it->char_to_display = it->c;
1826 if (!ASCII_BYTE_P (it->c))
1828 if (unibyte_display_via_language_environment
1829 && SINGLE_BYTE_CHAR_P (it->c)
1830 && (it->c >= 0240
1831 || !NILP (Vnonascii_translation_table)))
1833 it->char_to_display = unibyte_char_to_multibyte (it->c);
1834 it->multibyte_p = 1;
1835 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1836 face = FACE_FROM_ID (it->f, it->face_id);
1838 else if (!SINGLE_BYTE_CHAR_P (it->c)
1839 && !it->multibyte_p)
1841 it->multibyte_p = 1;
1842 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1843 face = FACE_FROM_ID (it->f, it->face_id);
1847 /* Get font to use. Encode IT->char_to_display. */
1848 x_get_char_face_and_encoding (it->f, it->char_to_display,
1849 it->face_id, &char2b,
1850 it->multibyte_p);
1851 font = face->font;
1853 /* When no suitable font found, use the default font. */
1854 font_not_found_p = font == NULL;
1855 if (font_not_found_p)
1857 font = FRAME_FONT (it->f);
1858 boff = it->f->output_data.x->baseline_offset;
1859 font_info = NULL;
1861 else
1863 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1864 boff = font_info->baseline_offset;
1865 if (font_info->vertical_centering)
1866 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1869 if (it->char_to_display >= ' '
1870 && (!it->multibyte_p || it->char_to_display < 128))
1872 /* Either unibyte or ASCII. */
1873 int stretched_p;
1875 it->nglyphs = 1;
1877 pcm = x_per_char_metric (font, &char2b);
1878 it->ascent = font->ascent + boff;
1879 it->descent = font->descent - boff;
1881 if (pcm)
1883 it->phys_ascent = pcm->ascent + boff;
1884 it->phys_descent = pcm->descent - boff;
1885 it->pixel_width = pcm->width;
1887 else
1889 it->glyph_not_available_p = 1;
1890 it->phys_ascent = font->ascent + boff;
1891 it->phys_descent = font->descent - boff;
1892 it->pixel_width = FONT_WIDTH (font);
1895 /* If this is a space inside a region of text with
1896 `space-width' property, change its width. */
1897 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1898 if (stretched_p)
1899 it->pixel_width *= XFLOATINT (it->space_width);
1901 /* If face has a box, add the box thickness to the character
1902 height. If character has a box line to the left and/or
1903 right, add the box line width to the character's width. */
1904 if (face->box != FACE_NO_BOX)
1906 int thick = face->box_line_width;
1908 if (thick > 0)
1910 it->ascent += thick;
1911 it->descent += thick;
1913 else
1914 thick = -thick;
1916 if (it->start_of_box_run_p)
1917 it->pixel_width += thick;
1918 if (it->end_of_box_run_p)
1919 it->pixel_width += thick;
1922 /* If face has an overline, add the height of the overline
1923 (1 pixel) and a 1 pixel margin to the character height. */
1924 if (face->overline_p)
1925 it->ascent += 2;
1927 take_vertical_position_into_account (it);
1929 /* If we have to actually produce glyphs, do it. */
1930 if (it->glyph_row)
1932 if (stretched_p)
1934 /* Translate a space with a `space-width' property
1935 into a stretch glyph. */
1936 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1937 x_append_stretch_glyph (it, it->object, it->pixel_width,
1938 it->ascent + it->descent, ascent);
1940 else
1941 x_append_glyph (it);
1943 /* If characters with lbearing or rbearing are displayed
1944 in this line, record that fact in a flag of the
1945 glyph row. This is used to optimize X output code. */
1946 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1947 it->glyph_row->contains_overlapping_glyphs_p = 1;
1950 else if (it->char_to_display == '\n')
1952 /* A newline has no width but we need the height of the line. */
1953 it->pixel_width = 0;
1954 it->nglyphs = 0;
1955 it->ascent = it->phys_ascent = font->ascent + boff;
1956 it->descent = it->phys_descent = font->descent - boff;
1958 if (face->box != FACE_NO_BOX
1959 && face->box_line_width > 0)
1961 it->ascent += face->box_line_width;
1962 it->descent += face->box_line_width;
1965 else if (it->char_to_display == '\t')
1967 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1968 int x = it->current_x + it->continuation_lines_width;
1969 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1971 /* If the distance from the current position to the next tab
1972 stop is less than a canonical character width, use the
1973 tab stop after that. */
1974 if (next_tab_x - x < CANON_X_UNIT (it->f))
1975 next_tab_x += tab_width;
1977 it->pixel_width = next_tab_x - x;
1978 it->nglyphs = 1;
1979 it->ascent = it->phys_ascent = font->ascent + boff;
1980 it->descent = it->phys_descent = font->descent - boff;
1982 if (it->glyph_row)
1984 double ascent = (double) it->ascent / (it->ascent + it->descent);
1985 x_append_stretch_glyph (it, it->object, it->pixel_width,
1986 it->ascent + it->descent, ascent);
1989 else
1991 /* A multi-byte character. Assume that the display width of the
1992 character is the width of the character multiplied by the
1993 width of the font. */
1995 /* If we found a font, this font should give us the right
1996 metrics. If we didn't find a font, use the frame's
1997 default font and calculate the width of the character
1998 from the charset width; this is what old redisplay code
1999 did. */
2000 pcm = x_per_char_metric (font, &char2b);
2001 if (font_not_found_p || !pcm)
2003 int charset = CHAR_CHARSET (it->char_to_display);
2005 it->glyph_not_available_p = 1;
2006 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
2007 * CHARSET_WIDTH (charset));
2008 it->phys_ascent = font->ascent + boff;
2009 it->phys_descent = font->descent - boff;
2011 else
2013 it->pixel_width = pcm->width;
2014 it->phys_ascent = pcm->ascent + boff;
2015 it->phys_descent = pcm->descent - boff;
2016 if (it->glyph_row
2017 && (pcm->lbearing < 0
2018 || pcm->rbearing > pcm->width))
2019 it->glyph_row->contains_overlapping_glyphs_p = 1;
2021 it->nglyphs = 1;
2022 it->ascent = font->ascent + boff;
2023 it->descent = font->descent - boff;
2024 if (face->box != FACE_NO_BOX)
2026 int thick = face->box_line_width;
2028 if (thick > 0)
2030 it->ascent += thick;
2031 it->descent += thick;
2033 else
2034 thick = - thick;
2036 if (it->start_of_box_run_p)
2037 it->pixel_width += thick;
2038 if (it->end_of_box_run_p)
2039 it->pixel_width += thick;
2042 /* If face has an overline, add the height of the overline
2043 (1 pixel) and a 1 pixel margin to the character height. */
2044 if (face->overline_p)
2045 it->ascent += 2;
2047 take_vertical_position_into_account (it);
2049 if (it->glyph_row)
2050 x_append_glyph (it);
2052 it->multibyte_p = saved_multibyte_p;
2054 else if (it->what == IT_COMPOSITION)
2056 /* Note: A composition is represented as one glyph in the
2057 glyph matrix. There are no padding glyphs. */
2058 XChar2b char2b;
2059 XFontStruct *font;
2060 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2061 XCharStruct *pcm;
2062 int font_not_found_p;
2063 struct font_info *font_info;
2064 int boff; /* baseline offset */
2065 struct composition *cmp = composition_table[it->cmp_id];
2067 /* Maybe translate single-byte characters to multibyte. */
2068 it->char_to_display = it->c;
2069 if (unibyte_display_via_language_environment
2070 && SINGLE_BYTE_CHAR_P (it->c)
2071 && (it->c >= 0240
2072 || (it->c >= 0200
2073 && !NILP (Vnonascii_translation_table))))
2075 it->char_to_display = unibyte_char_to_multibyte (it->c);
2078 /* Get face and font to use. Encode IT->char_to_display. */
2079 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2080 face = FACE_FROM_ID (it->f, it->face_id);
2081 x_get_char_face_and_encoding (it->f, it->char_to_display,
2082 it->face_id, &char2b, it->multibyte_p);
2083 font = face->font;
2085 /* When no suitable font found, use the default font. */
2086 font_not_found_p = font == NULL;
2087 if (font_not_found_p)
2089 font = FRAME_FONT (it->f);
2090 boff = it->f->output_data.x->baseline_offset;
2091 font_info = NULL;
2093 else
2095 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2096 boff = font_info->baseline_offset;
2097 if (font_info->vertical_centering)
2098 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2101 /* There are no padding glyphs, so there is only one glyph to
2102 produce for the composition. Important is that pixel_width,
2103 ascent and descent are the values of what is drawn by
2104 draw_glyphs (i.e. the values of the overall glyphs composed). */
2105 it->nglyphs = 1;
2107 /* If we have not yet calculated pixel size data of glyphs of
2108 the composition for the current face font, calculate them
2109 now. Theoretically, we have to check all fonts for the
2110 glyphs, but that requires much time and memory space. So,
2111 here we check only the font of the first glyph. This leads
2112 to incorrect display very rarely, and C-l (recenter) can
2113 correct the display anyway. */
2114 if (cmp->font != (void *) font)
2116 /* Ascent and descent of the font of the first character of
2117 this composition (adjusted by baseline offset). Ascent
2118 and descent of overall glyphs should not be less than
2119 them respectively. */
2120 int font_ascent = font->ascent + boff;
2121 int font_descent = font->descent - boff;
2122 /* Bounding box of the overall glyphs. */
2123 int leftmost, rightmost, lowest, highest;
2124 int i, width, ascent, descent;
2126 cmp->font = (void *) font;
2128 /* Initialize the bounding box. */
2129 if (font_info
2130 && (pcm = x_per_char_metric (font, &char2b)))
2132 width = pcm->width;
2133 ascent = pcm->ascent;
2134 descent = pcm->descent;
2136 else
2138 width = FONT_WIDTH (font);
2139 ascent = font->ascent;
2140 descent = font->descent;
2143 rightmost = width;
2144 lowest = - descent + boff;
2145 highest = ascent + boff;
2146 leftmost = 0;
2148 if (font_info
2149 && font_info->default_ascent
2150 && CHAR_TABLE_P (Vuse_default_ascent)
2151 && !NILP (Faref (Vuse_default_ascent,
2152 make_number (it->char_to_display))))
2153 highest = font_info->default_ascent + boff;
2155 /* Draw the first glyph at the normal position. It may be
2156 shifted to right later if some other glyphs are drawn at
2157 the left. */
2158 cmp->offsets[0] = 0;
2159 cmp->offsets[1] = boff;
2161 /* Set cmp->offsets for the remaining glyphs. */
2162 for (i = 1; i < cmp->glyph_len; i++)
2164 int left, right, btm, top;
2165 int ch = COMPOSITION_GLYPH (cmp, i);
2166 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2168 face = FACE_FROM_ID (it->f, face_id);
2169 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2170 it->multibyte_p);
2171 font = face->font;
2172 if (font == NULL)
2174 font = FRAME_FONT (it->f);
2175 boff = it->f->output_data.x->baseline_offset;
2176 font_info = NULL;
2178 else
2180 font_info
2181 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2182 boff = font_info->baseline_offset;
2183 if (font_info->vertical_centering)
2184 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2187 if (font_info
2188 && (pcm = x_per_char_metric (font, &char2b)))
2190 width = pcm->width;
2191 ascent = pcm->ascent;
2192 descent = pcm->descent;
2194 else
2196 width = FONT_WIDTH (font);
2197 ascent = 1;
2198 descent = 0;
2201 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2203 /* Relative composition with or without
2204 alternate chars. */
2205 left = (leftmost + rightmost - width) / 2;
2206 btm = - descent + boff;
2207 if (font_info && font_info->relative_compose
2208 && (! CHAR_TABLE_P (Vignore_relative_composition)
2209 || NILP (Faref (Vignore_relative_composition,
2210 make_number (ch)))))
2213 if (- descent >= font_info->relative_compose)
2214 /* One extra pixel between two glyphs. */
2215 btm = highest + 1;
2216 else if (ascent <= 0)
2217 /* One extra pixel between two glyphs. */
2218 btm = lowest - 1 - ascent - descent;
2221 else
2223 /* A composition rule is specified by an integer
2224 value that encodes global and new reference
2225 points (GREF and NREF). GREF and NREF are
2226 specified by numbers as below:
2228 0---1---2 -- ascent
2232 9--10--11 -- center
2234 ---3---4---5--- baseline
2236 6---7---8 -- descent
2238 int rule = COMPOSITION_RULE (cmp, i);
2239 int gref, nref, grefx, grefy, nrefx, nrefy;
2241 COMPOSITION_DECODE_RULE (rule, gref, nref);
2242 grefx = gref % 3, nrefx = nref % 3;
2243 grefy = gref / 3, nrefy = nref / 3;
2245 left = (leftmost
2246 + grefx * (rightmost - leftmost) / 2
2247 - nrefx * width / 2);
2248 btm = ((grefy == 0 ? highest
2249 : grefy == 1 ? 0
2250 : grefy == 2 ? lowest
2251 : (highest + lowest) / 2)
2252 - (nrefy == 0 ? ascent + descent
2253 : nrefy == 1 ? descent - boff
2254 : nrefy == 2 ? 0
2255 : (ascent + descent) / 2));
2258 cmp->offsets[i * 2] = left;
2259 cmp->offsets[i * 2 + 1] = btm + descent;
2261 /* Update the bounding box of the overall glyphs. */
2262 right = left + width;
2263 top = btm + descent + ascent;
2264 if (left < leftmost)
2265 leftmost = left;
2266 if (right > rightmost)
2267 rightmost = right;
2268 if (top > highest)
2269 highest = top;
2270 if (btm < lowest)
2271 lowest = btm;
2274 /* If there are glyphs whose x-offsets are negative,
2275 shift all glyphs to the right and make all x-offsets
2276 non-negative. */
2277 if (leftmost < 0)
2279 for (i = 0; i < cmp->glyph_len; i++)
2280 cmp->offsets[i * 2] -= leftmost;
2281 rightmost -= leftmost;
2284 cmp->pixel_width = rightmost;
2285 cmp->ascent = highest;
2286 cmp->descent = - lowest;
2287 if (cmp->ascent < font_ascent)
2288 cmp->ascent = font_ascent;
2289 if (cmp->descent < font_descent)
2290 cmp->descent = font_descent;
2293 it->pixel_width = cmp->pixel_width;
2294 it->ascent = it->phys_ascent = cmp->ascent;
2295 it->descent = it->phys_descent = cmp->descent;
2297 if (face->box != FACE_NO_BOX)
2299 int thick = face->box_line_width;
2301 if (thick > 0)
2303 it->ascent += thick;
2304 it->descent += thick;
2306 else
2307 thick = - thick;
2309 if (it->start_of_box_run_p)
2310 it->pixel_width += thick;
2311 if (it->end_of_box_run_p)
2312 it->pixel_width += thick;
2315 /* If face has an overline, add the height of the overline
2316 (1 pixel) and a 1 pixel margin to the character height. */
2317 if (face->overline_p)
2318 it->ascent += 2;
2320 take_vertical_position_into_account (it);
2322 if (it->glyph_row)
2323 x_append_composite_glyph (it);
2325 else if (it->what == IT_IMAGE)
2326 x_produce_image_glyph (it);
2327 else if (it->what == IT_STRETCH)
2328 x_produce_stretch_glyph (it);
2330 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2331 because this isn't true for images with `:ascent 100'. */
2332 xassert (it->ascent >= 0 && it->descent >= 0);
2333 if (it->area == TEXT_AREA)
2334 it->current_x += it->pixel_width;
2336 it->descent += it->extra_line_spacing;
2338 it->max_ascent = max (it->max_ascent, it->ascent);
2339 it->max_descent = max (it->max_descent, it->descent);
2340 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2341 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2345 /* Estimate the pixel height of the mode or top line on frame F.
2346 FACE_ID specifies what line's height to estimate. */
2349 x_estimate_mode_line_height (f, face_id)
2350 struct frame *f;
2351 enum face_id face_id;
2353 int height = FONT_HEIGHT (FRAME_FONT (f));
2355 /* This function is called so early when Emacs starts that the face
2356 cache and mode line face are not yet initialized. */
2357 if (FRAME_FACE_CACHE (f))
2359 struct face *face = FACE_FROM_ID (f, face_id);
2360 if (face)
2362 if (face->font)
2363 height = FONT_HEIGHT (face->font);
2364 if (face->box_line_width > 0)
2365 height += 2 * face->box_line_width;
2369 return height;
2373 /***********************************************************************
2374 Glyph display
2375 ***********************************************************************/
2377 /* A sequence of glyphs to be drawn in the same face.
2379 This data structure is not really completely X specific, so it
2380 could possibly, at least partially, be useful for other systems. It
2381 is currently not part of the external redisplay interface because
2382 it's not clear what other systems will need. */
2384 struct glyph_string
2386 /* X-origin of the string. */
2387 int x;
2389 /* Y-origin and y-position of the base line of this string. */
2390 int y, ybase;
2392 /* The width of the string, not including a face extension. */
2393 int width;
2395 /* The width of the string, including a face extension. */
2396 int background_width;
2398 /* The height of this string. This is the height of the line this
2399 string is drawn in, and can be different from the height of the
2400 font the string is drawn in. */
2401 int height;
2403 /* Number of pixels this string overwrites in front of its x-origin.
2404 This number is zero if the string has an lbearing >= 0; it is
2405 -lbearing, if the string has an lbearing < 0. */
2406 int left_overhang;
2408 /* Number of pixels this string overwrites past its right-most
2409 nominal x-position, i.e. x + width. Zero if the string's
2410 rbearing is <= its nominal width, rbearing - width otherwise. */
2411 int right_overhang;
2413 /* The frame on which the glyph string is drawn. */
2414 struct frame *f;
2416 /* The window on which the glyph string is drawn. */
2417 struct window *w;
2419 /* X display and window for convenience. */
2420 Display *display;
2421 Window window;
2423 /* The glyph row for which this string was built. It determines the
2424 y-origin and height of the string. */
2425 struct glyph_row *row;
2427 /* The area within row. */
2428 enum glyph_row_area area;
2430 /* Characters to be drawn, and number of characters. */
2431 XChar2b *char2b;
2432 int nchars;
2434 /* A face-override for drawing cursors, mouse face and similar. */
2435 enum draw_glyphs_face hl;
2437 /* Face in which this string is to be drawn. */
2438 struct face *face;
2440 /* Font in which this string is to be drawn. */
2441 XFontStruct *font;
2443 /* Font info for this string. */
2444 struct font_info *font_info;
2446 /* Non-null means this string describes (part of) a composition.
2447 All characters from char2b are drawn composed. */
2448 struct composition *cmp;
2450 /* Index of this glyph string's first character in the glyph
2451 definition of CMP. If this is zero, this glyph string describes
2452 the first character of a composition. */
2453 int gidx;
2455 /* 1 means this glyph strings face has to be drawn to the right end
2456 of the window's drawing area. */
2457 unsigned extends_to_end_of_line_p : 1;
2459 /* 1 means the background of this string has been drawn. */
2460 unsigned background_filled_p : 1;
2462 /* 1 means glyph string must be drawn with 16-bit functions. */
2463 unsigned two_byte_p : 1;
2465 /* 1 means that the original font determined for drawing this glyph
2466 string could not be loaded. The member `font' has been set to
2467 the frame's default font in this case. */
2468 unsigned font_not_found_p : 1;
2470 /* 1 means that the face in which this glyph string is drawn has a
2471 stipple pattern. */
2472 unsigned stippled_p : 1;
2474 /* 1 means only the foreground of this glyph string must be drawn,
2475 and we should use the physical height of the line this glyph
2476 string appears in as clip rect. */
2477 unsigned for_overlaps_p : 1;
2479 /* The GC to use for drawing this glyph string. */
2480 GC gc;
2482 /* A pointer to the first glyph in the string. This glyph
2483 corresponds to char2b[0]. Needed to draw rectangles if
2484 font_not_found_p is 1. */
2485 struct glyph *first_glyph;
2487 /* Image, if any. */
2488 struct image *img;
2490 struct glyph_string *next, *prev;
2494 #if GLYPH_DEBUG
2496 static void
2497 x_dump_glyph_string (s)
2498 struct glyph_string *s;
2500 fprintf (stderr, "glyph string\n");
2501 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2502 s->x, s->y, s->width, s->height);
2503 fprintf (stderr, " ybase = %d\n", s->ybase);
2504 fprintf (stderr, " hl = %d\n", s->hl);
2505 fprintf (stderr, " left overhang = %d, right = %d\n",
2506 s->left_overhang, s->right_overhang);
2507 fprintf (stderr, " nchars = %d\n", s->nchars);
2508 fprintf (stderr, " extends to end of line = %d\n",
2509 s->extends_to_end_of_line_p);
2510 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2511 fprintf (stderr, " bg width = %d\n", s->background_width);
2514 #endif /* GLYPH_DEBUG */
2518 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2519 struct glyph_string **,
2520 struct glyph_string *,
2521 struct glyph_string *));
2522 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2523 struct glyph_string **,
2524 struct glyph_string *,
2525 struct glyph_string *));
2526 static void x_append_glyph_string P_ ((struct glyph_string **,
2527 struct glyph_string **,
2528 struct glyph_string *));
2529 static int x_left_overwritten P_ ((struct glyph_string *));
2530 static int x_left_overwriting P_ ((struct glyph_string *));
2531 static int x_right_overwritten P_ ((struct glyph_string *));
2532 static int x_right_overwriting P_ ((struct glyph_string *));
2533 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2534 int));
2535 static void x_init_glyph_string P_ ((struct glyph_string *,
2536 XChar2b *, struct window *,
2537 struct glyph_row *,
2538 enum glyph_row_area, int,
2539 enum draw_glyphs_face));
2540 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2541 enum glyph_row_area, int, int,
2542 enum draw_glyphs_face, int *, int *, int));
2543 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2544 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2545 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2546 int));
2547 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2548 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2549 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2550 static void x_draw_glyph_string P_ ((struct glyph_string *));
2551 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2552 static void x_set_cursor_gc P_ ((struct glyph_string *));
2553 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2554 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2555 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2556 int *, int *));
2557 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2558 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2559 unsigned long *, double, int));
2560 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2561 double, int, unsigned long));
2562 static void x_setup_relief_colors P_ ((struct glyph_string *));
2563 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2564 static void x_draw_image_relief P_ ((struct glyph_string *));
2565 static void x_draw_image_foreground P_ ((struct glyph_string *));
2566 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2567 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2568 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2569 int, int, int));
2570 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2571 int, int, int, int, XRectangle *));
2572 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2573 int, int, int, XRectangle *));
2574 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2575 enum glyph_row_area));
2576 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2577 struct glyph_row *,
2578 enum glyph_row_area, int, int));
2580 #if GLYPH_DEBUG
2581 static void x_check_font P_ ((struct frame *, XFontStruct *));
2582 #endif
2585 /* Append the list of glyph strings with head H and tail T to the list
2586 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2588 static INLINE void
2589 x_append_glyph_string_lists (head, tail, h, t)
2590 struct glyph_string **head, **tail;
2591 struct glyph_string *h, *t;
2593 if (h)
2595 if (*head)
2596 (*tail)->next = h;
2597 else
2598 *head = h;
2599 h->prev = *tail;
2600 *tail = t;
2605 /* Prepend the list of glyph strings with head H and tail T to the
2606 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2607 result. */
2609 static INLINE void
2610 x_prepend_glyph_string_lists (head, tail, h, t)
2611 struct glyph_string **head, **tail;
2612 struct glyph_string *h, *t;
2614 if (h)
2616 if (*head)
2617 (*head)->prev = t;
2618 else
2619 *tail = t;
2620 t->next = *head;
2621 *head = h;
2626 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2627 Set *HEAD and *TAIL to the resulting list. */
2629 static INLINE void
2630 x_append_glyph_string (head, tail, s)
2631 struct glyph_string **head, **tail;
2632 struct glyph_string *s;
2634 s->next = s->prev = NULL;
2635 x_append_glyph_string_lists (head, tail, s, s);
2639 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2640 face. */
2642 static void
2643 x_set_cursor_gc (s)
2644 struct glyph_string *s;
2646 if (s->font == FRAME_FONT (s->f)
2647 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2648 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2649 && !s->cmp)
2650 s->gc = s->f->output_data.x->cursor_gc;
2651 else
2653 /* Cursor on non-default face: must merge. */
2654 XGCValues xgcv;
2655 unsigned long mask;
2657 xgcv.background = s->f->output_data.x->cursor_pixel;
2658 xgcv.foreground = s->face->background;
2660 /* If the glyph would be invisible, try a different foreground. */
2661 if (xgcv.foreground == xgcv.background)
2662 xgcv.foreground = s->face->foreground;
2663 if (xgcv.foreground == xgcv.background)
2664 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2665 if (xgcv.foreground == xgcv.background)
2666 xgcv.foreground = s->face->foreground;
2668 /* Make sure the cursor is distinct from text in this face. */
2669 if (xgcv.background == s->face->background
2670 && xgcv.foreground == s->face->foreground)
2672 xgcv.background = s->face->foreground;
2673 xgcv.foreground = s->face->background;
2676 IF_DEBUG (x_check_font (s->f, s->font));
2677 xgcv.font = s->font->fid;
2678 xgcv.graphics_exposures = False;
2679 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2681 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2682 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2683 mask, &xgcv);
2684 else
2685 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2686 = XCreateGC (s->display, s->window, mask, &xgcv);
2688 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2693 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2695 static void
2696 x_set_mouse_face_gc (s)
2697 struct glyph_string *s;
2699 int face_id;
2700 struct face *face;
2702 /* What face has to be used last for the mouse face? */
2703 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2704 face = FACE_FROM_ID (s->f, face_id);
2705 if (face == NULL)
2706 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2708 if (s->first_glyph->type == CHAR_GLYPH)
2709 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2710 else
2711 face_id = FACE_FOR_CHAR (s->f, face, 0);
2712 s->face = FACE_FROM_ID (s->f, face_id);
2713 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2715 /* If font in this face is same as S->font, use it. */
2716 if (s->font == s->face->font)
2717 s->gc = s->face->gc;
2718 else
2720 /* Otherwise construct scratch_cursor_gc with values from FACE
2721 but font FONT. */
2722 XGCValues xgcv;
2723 unsigned long mask;
2725 xgcv.background = s->face->background;
2726 xgcv.foreground = s->face->foreground;
2727 IF_DEBUG (x_check_font (s->f, s->font));
2728 xgcv.font = s->font->fid;
2729 xgcv.graphics_exposures = False;
2730 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2732 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2733 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2734 mask, &xgcv);
2735 else
2736 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2737 = XCreateGC (s->display, s->window, mask, &xgcv);
2739 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2742 xassert (s->gc != 0);
2746 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2747 Faces to use in the mode line have already been computed when the
2748 matrix was built, so there isn't much to do, here. */
2750 static INLINE void
2751 x_set_mode_line_face_gc (s)
2752 struct glyph_string *s;
2754 s->gc = s->face->gc;
2758 /* Set S->gc of glyph string S for drawing that glyph string. Set
2759 S->stippled_p to a non-zero value if the face of S has a stipple
2760 pattern. */
2762 static INLINE void
2763 x_set_glyph_string_gc (s)
2764 struct glyph_string *s;
2766 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2768 if (s->hl == DRAW_NORMAL_TEXT)
2770 s->gc = s->face->gc;
2771 s->stippled_p = s->face->stipple != 0;
2773 else if (s->hl == DRAW_INVERSE_VIDEO)
2775 x_set_mode_line_face_gc (s);
2776 s->stippled_p = s->face->stipple != 0;
2778 else if (s->hl == DRAW_CURSOR)
2780 x_set_cursor_gc (s);
2781 s->stippled_p = 0;
2783 else if (s->hl == DRAW_MOUSE_FACE)
2785 x_set_mouse_face_gc (s);
2786 s->stippled_p = s->face->stipple != 0;
2788 else if (s->hl == DRAW_IMAGE_RAISED
2789 || s->hl == DRAW_IMAGE_SUNKEN)
2791 s->gc = s->face->gc;
2792 s->stippled_p = s->face->stipple != 0;
2794 else
2796 s->gc = s->face->gc;
2797 s->stippled_p = s->face->stipple != 0;
2800 /* GC must have been set. */
2801 xassert (s->gc != 0);
2805 /* Return in *R the clipping rectangle for glyph string S. */
2807 static void
2808 x_get_glyph_string_clip_rect (s, r)
2809 struct glyph_string *s;
2810 XRectangle *r;
2812 if (s->row->full_width_p)
2814 /* Draw full-width. X coordinates are relative to S->w->left. */
2815 int canon_x = CANON_X_UNIT (s->f);
2817 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2818 r->width = XFASTINT (s->w->width) * canon_x;
2820 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2822 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2823 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2824 r->x -= width;
2827 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2829 /* Unless displaying a mode or menu bar line, which are always
2830 fully visible, clip to the visible part of the row. */
2831 if (s->w->pseudo_window_p)
2832 r->height = s->row->visible_height;
2833 else
2834 r->height = s->height;
2836 else
2838 /* This is a text line that may be partially visible. */
2839 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2840 r->width = window_box_width (s->w, s->area);
2841 r->height = s->row->visible_height;
2844 /* If S draws overlapping rows, it's sufficient to use the top and
2845 bottom of the window for clipping because this glyph string
2846 intentionally draws over other lines. */
2847 if (s->for_overlaps_p)
2849 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2850 r->height = window_text_bottom_y (s->w) - r->y;
2852 else
2854 /* Don't use S->y for clipping because it doesn't take partially
2855 visible lines into account. For example, it can be negative for
2856 partially visible lines at the top of a window. */
2857 if (!s->row->full_width_p
2858 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2859 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2860 else
2861 r->y = max (0, s->row->y);
2863 /* If drawing a tool-bar window, draw it over the internal border
2864 at the top of the window. */
2865 if (s->w == XWINDOW (s->f->tool_bar_window))
2866 r->y -= s->f->output_data.x->internal_border_width;
2869 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2873 /* Set clipping for output of glyph string S. S may be part of a mode
2874 line or menu if we don't have X toolkit support. */
2876 static INLINE void
2877 x_set_glyph_string_clipping (s)
2878 struct glyph_string *s;
2880 XRectangle r;
2881 x_get_glyph_string_clip_rect (s, &r);
2882 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2886 /* Compute left and right overhang of glyph string S. If S is a glyph
2887 string for a composition, assume overhangs don't exist. */
2889 static INLINE void
2890 x_compute_glyph_string_overhangs (s)
2891 struct glyph_string *s;
2893 if (s->cmp == NULL
2894 && s->first_glyph->type == CHAR_GLYPH)
2896 XCharStruct cs;
2897 int direction, font_ascent, font_descent;
2898 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2899 &font_ascent, &font_descent, &cs);
2900 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2901 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2906 /* Compute overhangs and x-positions for glyph string S and its
2907 predecessors, or successors. X is the starting x-position for S.
2908 BACKWARD_P non-zero means process predecessors. */
2910 static void
2911 x_compute_overhangs_and_x (s, x, backward_p)
2912 struct glyph_string *s;
2913 int x;
2914 int backward_p;
2916 if (backward_p)
2918 while (s)
2920 x_compute_glyph_string_overhangs (s);
2921 x -= s->width;
2922 s->x = x;
2923 s = s->prev;
2926 else
2928 while (s)
2930 x_compute_glyph_string_overhangs (s);
2931 s->x = x;
2932 x += s->width;
2933 s = s->next;
2939 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2940 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2941 assumed to be zero. */
2943 static void
2944 x_get_glyph_overhangs (glyph, f, left, right)
2945 struct glyph *glyph;
2946 struct frame *f;
2947 int *left, *right;
2949 *left = *right = 0;
2951 if (glyph->type == CHAR_GLYPH)
2953 XFontStruct *font;
2954 struct face *face;
2955 struct font_info *font_info;
2956 XChar2b char2b;
2957 XCharStruct *pcm;
2959 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2960 font = face->font;
2961 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2962 if (font
2963 && (pcm = x_per_char_metric (font, &char2b)))
2965 if (pcm->rbearing > pcm->width)
2966 *right = pcm->rbearing - pcm->width;
2967 if (pcm->lbearing < 0)
2968 *left = -pcm->lbearing;
2974 /* Return the index of the first glyph preceding glyph string S that
2975 is overwritten by S because of S's left overhang. Value is -1
2976 if no glyphs are overwritten. */
2978 static int
2979 x_left_overwritten (s)
2980 struct glyph_string *s;
2982 int k;
2984 if (s->left_overhang)
2986 int x = 0, i;
2987 struct glyph *glyphs = s->row->glyphs[s->area];
2988 int first = s->first_glyph - glyphs;
2990 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2991 x -= glyphs[i].pixel_width;
2993 k = i + 1;
2995 else
2996 k = -1;
2998 return k;
3002 /* Return the index of the first glyph preceding glyph string S that
3003 is overwriting S because of its right overhang. Value is -1 if no
3004 glyph in front of S overwrites S. */
3006 static int
3007 x_left_overwriting (s)
3008 struct glyph_string *s;
3010 int i, k, x;
3011 struct glyph *glyphs = s->row->glyphs[s->area];
3012 int first = s->first_glyph - glyphs;
3014 k = -1;
3015 x = 0;
3016 for (i = first - 1; i >= 0; --i)
3018 int left, right;
3019 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3020 if (x + right > 0)
3021 k = i;
3022 x -= glyphs[i].pixel_width;
3025 return k;
3029 /* Return the index of the last glyph following glyph string S that is
3030 not overwritten by S because of S's right overhang. Value is -1 if
3031 no such glyph is found. */
3033 static int
3034 x_right_overwritten (s)
3035 struct glyph_string *s;
3037 int k = -1;
3039 if (s->right_overhang)
3041 int x = 0, i;
3042 struct glyph *glyphs = s->row->glyphs[s->area];
3043 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3044 int end = s->row->used[s->area];
3046 for (i = first; i < end && s->right_overhang > x; ++i)
3047 x += glyphs[i].pixel_width;
3049 k = i;
3052 return k;
3056 /* Return the index of the last glyph following glyph string S that
3057 overwrites S because of its left overhang. Value is negative
3058 if no such glyph is found. */
3060 static int
3061 x_right_overwriting (s)
3062 struct glyph_string *s;
3064 int i, k, x;
3065 int end = s->row->used[s->area];
3066 struct glyph *glyphs = s->row->glyphs[s->area];
3067 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3069 k = -1;
3070 x = 0;
3071 for (i = first; i < end; ++i)
3073 int left, right;
3074 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3075 if (x - left < 0)
3076 k = i;
3077 x += glyphs[i].pixel_width;
3080 return k;
3084 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3086 static INLINE void
3087 x_clear_glyph_string_rect (s, x, y, w, h)
3088 struct glyph_string *s;
3089 int x, y, w, h;
3091 XGCValues xgcv;
3092 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3093 XSetForeground (s->display, s->gc, xgcv.background);
3094 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3095 XSetForeground (s->display, s->gc, xgcv.foreground);
3099 /* Draw the background of glyph_string S. If S->background_filled_p
3100 is non-zero don't draw it. FORCE_P non-zero means draw the
3101 background even if it wouldn't be drawn normally. This is used
3102 when a string preceding S draws into the background of S, or S
3103 contains the first component of a composition. */
3105 static void
3106 x_draw_glyph_string_background (s, force_p)
3107 struct glyph_string *s;
3108 int force_p;
3110 /* Nothing to do if background has already been drawn or if it
3111 shouldn't be drawn in the first place. */
3112 if (!s->background_filled_p)
3114 int box_line_width = max (s->face->box_line_width, 0);
3116 if (s->stippled_p)
3118 /* Fill background with a stipple pattern. */
3119 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3120 XFillRectangle (s->display, s->window, s->gc, s->x,
3121 s->y + box_line_width,
3122 s->background_width,
3123 s->height - 2 * box_line_width);
3124 XSetFillStyle (s->display, s->gc, FillSolid);
3125 s->background_filled_p = 1;
3127 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
3128 || s->font_not_found_p
3129 || s->extends_to_end_of_line_p
3130 || force_p)
3132 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
3133 s->background_width,
3134 s->height - 2 * box_line_width);
3135 s->background_filled_p = 1;
3141 /* Draw the foreground of glyph string S. */
3143 static void
3144 x_draw_glyph_string_foreground (s)
3145 struct glyph_string *s;
3147 int i, x;
3149 /* If first glyph of S has a left box line, start drawing the text
3150 of S to the right of that box line. */
3151 if (s->face->box != FACE_NO_BOX
3152 && s->first_glyph->left_box_line_p)
3153 x = s->x + abs (s->face->box_line_width);
3154 else
3155 x = s->x;
3157 /* Draw characters of S as rectangles if S's font could not be
3158 loaded. */
3159 if (s->font_not_found_p)
3161 for (i = 0; i < s->nchars; ++i)
3163 struct glyph *g = s->first_glyph + i;
3164 XDrawRectangle (s->display, s->window,
3165 s->gc, x, s->y, g->pixel_width - 1,
3166 s->height - 1);
3167 x += g->pixel_width;
3170 else
3172 char *char1b = (char *) s->char2b;
3173 int boff = s->font_info->baseline_offset;
3175 if (s->font_info->vertical_centering)
3176 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3178 /* If we can use 8-bit functions, condense S->char2b. */
3179 if (!s->two_byte_p)
3180 for (i = 0; i < s->nchars; ++i)
3181 char1b[i] = s->char2b[i].byte2;
3183 /* Draw text with XDrawString if background has already been
3184 filled. Otherwise, use XDrawImageString. (Note that
3185 XDrawImageString is usually faster than XDrawString.) Always
3186 use XDrawImageString when drawing the cursor so that there is
3187 no chance that characters under a box cursor are invisible. */
3188 if (s->for_overlaps_p
3189 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3191 /* Draw characters with 16-bit or 8-bit functions. */
3192 if (s->two_byte_p)
3193 XDrawString16 (s->display, s->window, s->gc, x,
3194 s->ybase - boff, s->char2b, s->nchars);
3195 else
3196 XDrawString (s->display, s->window, s->gc, x,
3197 s->ybase - boff, char1b, s->nchars);
3199 else
3201 if (s->two_byte_p)
3202 XDrawImageString16 (s->display, s->window, s->gc, x,
3203 s->ybase - boff, s->char2b, s->nchars);
3204 else
3205 XDrawImageString (s->display, s->window, s->gc, x,
3206 s->ybase - boff, char1b, s->nchars);
3211 /* Draw the foreground of composite glyph string S. */
3213 static void
3214 x_draw_composite_glyph_string_foreground (s)
3215 struct glyph_string *s;
3217 int i, x;
3219 /* If first glyph of S has a left box line, start drawing the text
3220 of S to the right of that box line. */
3221 if (s->face->box != FACE_NO_BOX
3222 && s->first_glyph->left_box_line_p)
3223 x = s->x + abs (s->face->box_line_width);
3224 else
3225 x = s->x;
3227 /* S is a glyph string for a composition. S->gidx is the index of
3228 the first character drawn for glyphs of this composition.
3229 S->gidx == 0 means we are drawing the very first character of
3230 this composition. */
3232 /* Draw a rectangle for the composition if the font for the very
3233 first character of the composition could not be loaded. */
3234 if (s->font_not_found_p)
3236 if (s->gidx == 0)
3237 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3238 s->width - 1, s->height - 1);
3240 else
3242 for (i = 0; i < s->nchars; i++, ++s->gidx)
3243 XDrawString16 (s->display, s->window, s->gc,
3244 x + s->cmp->offsets[s->gidx * 2],
3245 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3246 s->char2b + i, 1);
3251 #ifdef USE_X_TOOLKIT
3253 static struct frame *x_frame_of_widget P_ ((Widget));
3254 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
3255 XrmValue *, XrmValue *, XtPointer *));
3256 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
3257 XrmValue *, Cardinal *));
3260 /* Return the frame on which widget WIDGET is used.. Abort if frame
3261 cannot be determined. */
3263 static struct frame *
3264 x_frame_of_widget (widget)
3265 Widget widget;
3267 struct x_display_info *dpyinfo;
3268 Lisp_Object tail;
3269 struct frame *f;
3271 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3273 /* Find the top-level shell of the widget. Note that this function
3274 can be called when the widget is not yet realized, so XtWindow
3275 (widget) == 0. That's the reason we can't simply use
3276 x_any_window_to_frame. */
3277 while (!XtIsTopLevelShell (widget))
3278 widget = XtParent (widget);
3280 /* Look for a frame with that top-level widget. Allocate the color
3281 on that frame to get the right gamma correction value. */
3282 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3283 if (GC_FRAMEP (XCAR (tail))
3284 && (f = XFRAME (XCAR (tail)),
3285 (f->output_data.nothing != 1
3286 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3287 && f->output_data.x->widget == widget)
3288 return f;
3290 abort ();
3294 /* Allocate the color COLOR->pixel on the screen and display of
3295 widget WIDGET in colormap CMAP. If an exact match cannot be
3296 allocated, try the nearest color available. Value is non-zero
3297 if successful. This is called from lwlib. */
3300 x_alloc_nearest_color_for_widget (widget, cmap, color)
3301 Widget widget;
3302 Colormap cmap;
3303 XColor *color;
3305 struct frame *f = x_frame_of_widget (widget);
3306 return x_alloc_nearest_color (f, cmap, color);
3310 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3311 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3312 If this produces the same color as PIXEL, try a color where all RGB
3313 values have DELTA added. Return the allocated color in *PIXEL.
3314 DISPLAY is the X display, CMAP is the colormap to operate on.
3315 Value is non-zero if successful. */
3318 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3319 Widget widget;
3320 Display *display;
3321 Colormap cmap;
3322 unsigned long *pixel;
3323 double factor;
3324 int delta;
3326 struct frame *f = x_frame_of_widget (widget);
3327 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3331 /* Structure specifying which arguments should be passed by Xt to
3332 cvt_string_to_pixel. We want the widget's screen and colormap. */
3334 static XtConvertArgRec cvt_string_to_pixel_args[] =
3336 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
3337 sizeof (Screen *)},
3338 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
3339 sizeof (Colormap)}
3343 /* The address of this variable is returned by
3344 cvt_string_to_pixel. */
3346 static Pixel cvt_string_to_pixel_value;
3349 /* Convert a color name to a pixel color.
3351 DPY is the display we are working on.
3353 ARGS is an array of *NARGS XrmValue structures holding additional
3354 information about the widget for which the conversion takes place.
3355 The contents of this array are determined by the specification
3356 in cvt_string_to_pixel_args.
3358 FROM is a pointer to an XrmValue which points to the color name to
3359 convert. TO is an XrmValue in which to return the pixel color.
3361 CLOSURE_RET is a pointer to user-data, in which we record if
3362 we allocated the color or not.
3364 Value is True if successful, False otherwise. */
3366 static Boolean
3367 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
3368 Display *dpy;
3369 XrmValue *args;
3370 Cardinal *nargs;
3371 XrmValue *from, *to;
3372 XtPointer *closure_ret;
3374 Screen *screen;
3375 Colormap cmap;
3376 Pixel pixel;
3377 String color_name;
3378 XColor color;
3380 if (*nargs != 2)
3382 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3383 "wrongParameters", "cvt_string_to_pixel",
3384 "XtToolkitError",
3385 "Screen and colormap args required", NULL, NULL);
3386 return False;
3389 screen = *(Screen **) args[0].addr;
3390 cmap = *(Colormap *) args[1].addr;
3391 color_name = (String) from->addr;
3393 if (strcmp (color_name, XtDefaultBackground) == 0)
3395 *closure_ret = (XtPointer) False;
3396 pixel = WhitePixelOfScreen (screen);
3398 else if (strcmp (color_name, XtDefaultForeground) == 0)
3400 *closure_ret = (XtPointer) False;
3401 pixel = BlackPixelOfScreen (screen);
3403 else if (XParseColor (dpy, cmap, color_name, &color)
3404 && x_alloc_nearest_color_1 (dpy, cmap, &color))
3406 pixel = color.pixel;
3407 *closure_ret = (XtPointer) True;
3409 else
3411 String params[1];
3412 Cardinal nparams = 1;
3414 params[0] = color_name;
3415 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3416 "badValue", "cvt_string_to_pixel",
3417 "XtToolkitError", "Invalid color `%s'",
3418 params, &nparams);
3419 return False;
3422 if (to->addr != NULL)
3424 if (to->size < sizeof (Pixel))
3426 to->size = sizeof (Pixel);
3427 return False;
3430 *(Pixel *) to->addr = pixel;
3432 else
3434 cvt_string_to_pixel_value = pixel;
3435 to->addr = (XtPointer) &cvt_string_to_pixel_value;
3438 to->size = sizeof (Pixel);
3439 return True;
3443 /* Free a pixel color which was previously allocated via
3444 cvt_string_to_pixel. This is registered as the destructor
3445 for this type of resource via XtSetTypeConverter.
3447 APP is the application context in which we work.
3449 TO is a pointer to an XrmValue holding the color to free.
3450 CLOSURE is the value we stored in CLOSURE_RET for this color
3451 in cvt_string_to_pixel.
3453 ARGS and NARGS are like for cvt_string_to_pixel. */
3455 static void
3456 cvt_pixel_dtor (app, to, closure, args, nargs)
3457 XtAppContext app;
3458 XrmValuePtr to;
3459 XtPointer closure;
3460 XrmValuePtr args;
3461 Cardinal *nargs;
3463 if (*nargs != 2)
3465 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
3466 "XtToolkitError",
3467 "Screen and colormap arguments required",
3468 NULL, NULL);
3470 else if (closure != NULL)
3472 /* We did allocate the pixel, so free it. */
3473 Screen *screen = *(Screen **) args[0].addr;
3474 Colormap cmap = *(Colormap *) args[1].addr;
3475 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
3476 (Pixel *) to->addr, 1);
3481 #endif /* USE_X_TOOLKIT */
3484 /* Value is an array of XColor structures for the contents of the
3485 color map of display DPY. Set *NCELLS to the size of the array.
3486 Note that this probably shouldn't be called for large color maps,
3487 say a 24-bit TrueColor map. */
3489 static const XColor *
3490 x_color_cells (dpy, ncells)
3491 Display *dpy;
3492 int *ncells;
3494 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3496 if (dpyinfo->color_cells == NULL)
3498 Screen *screen = dpyinfo->screen;
3499 int i;
3501 dpyinfo->ncolor_cells
3502 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
3503 dpyinfo->color_cells
3504 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3505 * sizeof *dpyinfo->color_cells);
3507 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3508 dpyinfo->color_cells[i].pixel = i;
3510 XQueryColors (dpy, dpyinfo->cmap,
3511 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3514 *ncells = dpyinfo->ncolor_cells;
3515 return dpyinfo->color_cells;
3519 /* On frame F, translate pixel colors to RGB values for the NCOLORS
3520 colors in COLORS. Use cached information, if available. */
3522 void
3523 x_query_colors (f, colors, ncolors)
3524 struct frame *f;
3525 XColor *colors;
3526 int ncolors;
3528 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3530 if (dpyinfo->color_cells)
3532 int i;
3533 for (i = 0; i < ncolors; ++i)
3535 unsigned long pixel = colors[i].pixel;
3536 xassert (pixel < dpyinfo->ncolor_cells);
3537 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3538 colors[i] = dpyinfo->color_cells[pixel];
3541 else
3542 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3546 /* On frame F, translate pixel color to RGB values for the color in
3547 COLOR. Use cached information, if available. */
3549 void
3550 x_query_color (f, color)
3551 struct frame *f;
3552 XColor *color;
3554 x_query_colors (f, color, 1);
3558 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
3559 exact match can't be allocated, try the nearest color available.
3560 Value is non-zero if successful. Set *COLOR to the color
3561 allocated. */
3563 static int
3564 x_alloc_nearest_color_1 (dpy, cmap, color)
3565 Display *dpy;
3566 Colormap cmap;
3567 XColor *color;
3569 int rc;
3571 rc = XAllocColor (dpy, cmap, color);
3572 if (rc == 0)
3574 /* If we got to this point, the colormap is full, so we're going
3575 to try to get the next closest color. The algorithm used is
3576 a least-squares matching, which is what X uses for closest
3577 color matching with StaticColor visuals. */
3578 int nearest, i;
3579 unsigned long nearest_delta = ~0;
3580 int ncells;
3581 const XColor *cells = x_color_cells (dpy, &ncells);
3583 for (nearest = i = 0; i < ncells; ++i)
3585 long dred = (color->red >> 8) - (cells[i].red >> 8);
3586 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3587 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3588 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3590 if (delta < nearest_delta)
3592 nearest = i;
3593 nearest_delta = delta;
3597 color->red = cells[nearest].red;
3598 color->green = cells[nearest].green;
3599 color->blue = cells[nearest].blue;
3600 rc = XAllocColor (dpy, cmap, color);
3602 else
3604 /* If allocation succeeded, and the allocated pixel color is not
3605 equal to a cached pixel color recorded earlier, there was a
3606 change in the colormap, so clear the color cache. */
3607 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3608 XColor *cached_color;
3610 if (dpyinfo->color_cells
3611 && (cached_color = &dpyinfo->color_cells[color->pixel],
3612 (cached_color->red != color->red
3613 || cached_color->blue != color->blue
3614 || cached_color->green != color->green)))
3616 xfree (dpyinfo->color_cells);
3617 dpyinfo->color_cells = NULL;
3618 dpyinfo->ncolor_cells = 0;
3622 #ifdef DEBUG_X_COLORS
3623 if (rc)
3624 register_color (color->pixel);
3625 #endif /* DEBUG_X_COLORS */
3627 return rc;
3631 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
3632 exact match can't be allocated, try the nearest color available.
3633 Value is non-zero if successful. Set *COLOR to the color
3634 allocated. */
3637 x_alloc_nearest_color (f, cmap, color)
3638 struct frame *f;
3639 Colormap cmap;
3640 XColor *color;
3642 gamma_correct (f, color);
3643 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
3647 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3648 It's necessary to do this instead of just using PIXEL directly to
3649 get color reference counts right. */
3651 unsigned long
3652 x_copy_color (f, pixel)
3653 struct frame *f;
3654 unsigned long pixel;
3656 XColor color;
3658 color.pixel = pixel;
3659 BLOCK_INPUT;
3660 x_query_color (f, &color);
3661 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3662 UNBLOCK_INPUT;
3663 #ifdef DEBUG_X_COLORS
3664 register_color (pixel);
3665 #endif
3666 return color.pixel;
3670 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3671 It's necessary to do this instead of just using PIXEL directly to
3672 get color reference counts right. */
3674 unsigned long
3675 x_copy_dpy_color (dpy, cmap, pixel)
3676 Display *dpy;
3677 Colormap cmap;
3678 unsigned long pixel;
3680 XColor color;
3682 color.pixel = pixel;
3683 BLOCK_INPUT;
3684 XQueryColor (dpy, cmap, &color);
3685 XAllocColor (dpy, cmap, &color);
3686 UNBLOCK_INPUT;
3687 #ifdef DEBUG_X_COLORS
3688 register_color (pixel);
3689 #endif
3690 return color.pixel;
3694 /* Brightness beyond which a color won't have its highlight brightness
3695 boosted.
3697 Nominally, highlight colors for `3d' faces are calculated by
3698 brightening an object's color by a constant scale factor, but this
3699 doesn't yield good results for dark colors, so for colors who's
3700 brightness is less than this value (on a scale of 0-65535) have an
3701 use an additional additive factor.
3703 The value here is set so that the default menu-bar/mode-line color
3704 (grey75) will not have its highlights changed at all. */
3705 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3708 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3709 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3710 If this produces the same color as PIXEL, try a color where all RGB
3711 values have DELTA added. Return the allocated color in *PIXEL.
3712 DISPLAY is the X display, CMAP is the colormap to operate on.
3713 Value is non-zero if successful. */
3715 static int
3716 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3717 struct frame *f;
3718 Display *display;
3719 Colormap cmap;
3720 unsigned long *pixel;
3721 double factor;
3722 int delta;
3724 XColor color, new;
3725 long bright;
3726 int success_p;
3728 /* Get RGB color values. */
3729 color.pixel = *pixel;
3730 x_query_color (f, &color);
3732 /* Change RGB values by specified FACTOR. Avoid overflow! */
3733 xassert (factor >= 0);
3734 new.red = min (0xffff, factor * color.red);
3735 new.green = min (0xffff, factor * color.green);
3736 new.blue = min (0xffff, factor * color.blue);
3738 /* Calculate brightness of COLOR. */
3739 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
3741 /* We only boost colors that are darker than
3742 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3743 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3744 /* Make an additive adjustment to NEW, because it's dark enough so
3745 that scaling by FACTOR alone isn't enough. */
3747 /* How far below the limit this color is (0 - 1, 1 being darker). */
3748 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3749 /* The additive adjustment. */
3750 int min_delta = delta * dimness * factor / 2;
3752 if (factor < 1)
3754 new.red = max (0, new.red - min_delta);
3755 new.green = max (0, new.green - min_delta);
3756 new.blue = max (0, new.blue - min_delta);
3758 else
3760 new.red = min (0xffff, min_delta + new.red);
3761 new.green = min (0xffff, min_delta + new.green);
3762 new.blue = min (0xffff, min_delta + new.blue);
3766 /* Try to allocate the color. */
3767 success_p = x_alloc_nearest_color (f, cmap, &new);
3768 if (success_p)
3770 if (new.pixel == *pixel)
3772 /* If we end up with the same color as before, try adding
3773 delta to the RGB values. */
3774 x_free_colors (f, &new.pixel, 1);
3776 new.red = min (0xffff, delta + color.red);
3777 new.green = min (0xffff, delta + color.green);
3778 new.blue = min (0xffff, delta + color.blue);
3779 success_p = x_alloc_nearest_color (f, cmap, &new);
3781 else
3782 success_p = 1;
3783 *pixel = new.pixel;
3786 return success_p;
3790 /* Set up the foreground color for drawing relief lines of glyph
3791 string S. RELIEF is a pointer to a struct relief containing the GC
3792 with which lines will be drawn. Use a color that is FACTOR or
3793 DELTA lighter or darker than the relief's background which is found
3794 in S->f->output_data.x->relief_background. If such a color cannot
3795 be allocated, use DEFAULT_PIXEL, instead. */
3797 static void
3798 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3799 struct frame *f;
3800 struct relief *relief;
3801 double factor;
3802 int delta;
3803 unsigned long default_pixel;
3805 XGCValues xgcv;
3806 struct x_output *di = f->output_data.x;
3807 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3808 unsigned long pixel;
3809 unsigned long background = di->relief_background;
3810 Colormap cmap = FRAME_X_COLORMAP (f);
3811 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3812 Display *dpy = FRAME_X_DISPLAY (f);
3814 xgcv.graphics_exposures = False;
3815 xgcv.line_width = 1;
3817 /* Free previously allocated color. The color cell will be reused
3818 when it has been freed as many times as it was allocated, so this
3819 doesn't affect faces using the same colors. */
3820 if (relief->gc
3821 && relief->allocated_p)
3823 x_free_colors (f, &relief->pixel, 1);
3824 relief->allocated_p = 0;
3827 /* Allocate new color. */
3828 xgcv.foreground = default_pixel;
3829 pixel = background;
3830 if (dpyinfo->n_planes != 1
3831 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3833 relief->allocated_p = 1;
3834 xgcv.foreground = relief->pixel = pixel;
3837 if (relief->gc == 0)
3839 xgcv.stipple = dpyinfo->gray;
3840 mask |= GCStipple;
3841 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3843 else
3844 XChangeGC (dpy, relief->gc, mask, &xgcv);
3848 /* Set up colors for the relief lines around glyph string S. */
3850 static void
3851 x_setup_relief_colors (s)
3852 struct glyph_string *s;
3854 struct x_output *di = s->f->output_data.x;
3855 unsigned long color;
3857 if (s->face->use_box_color_for_shadows_p)
3858 color = s->face->box_color;
3859 else
3861 XGCValues xgcv;
3863 /* Get the background color of the face. */
3864 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3865 color = xgcv.background;
3868 if (di->white_relief.gc == 0
3869 || color != di->relief_background)
3871 di->relief_background = color;
3872 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3873 WHITE_PIX_DEFAULT (s->f));
3874 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3875 BLACK_PIX_DEFAULT (s->f));
3880 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3881 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3882 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3883 relief. LEFT_P non-zero means draw a relief on the left side of
3884 the rectangle. RIGHT_P non-zero means draw a relief on the right
3885 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3886 when drawing. */
3888 static void
3889 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3890 raised_p, left_p, right_p, clip_rect)
3891 struct frame *f;
3892 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3893 XRectangle *clip_rect;
3895 int i;
3896 GC gc;
3898 if (raised_p)
3899 gc = f->output_data.x->white_relief.gc;
3900 else
3901 gc = f->output_data.x->black_relief.gc;
3902 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3904 /* Top. */
3905 for (i = 0; i < width; ++i)
3906 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3907 left_x + i * left_p, top_y + i,
3908 right_x + 1 - i * right_p, top_y + i);
3910 /* Left. */
3911 if (left_p)
3912 for (i = 0; i < width; ++i)
3913 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3914 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
3916 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3917 if (raised_p)
3918 gc = f->output_data.x->black_relief.gc;
3919 else
3920 gc = f->output_data.x->white_relief.gc;
3921 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3923 /* Bottom. */
3924 for (i = 0; i < width; ++i)
3925 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3926 left_x + i * left_p + 1, bottom_y - i,
3927 right_x + 1 - i * right_p, bottom_y - i);
3929 /* Right. */
3930 if (right_p)
3931 for (i = 0; i < width; ++i)
3932 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3933 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3935 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3939 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3940 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3941 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3942 left side of the rectangle. RIGHT_P non-zero means draw a line
3943 on the right side of the rectangle. CLIP_RECT is the clipping
3944 rectangle to use when drawing. */
3946 static void
3947 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3948 left_p, right_p, clip_rect)
3949 struct glyph_string *s;
3950 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3951 XRectangle *clip_rect;
3953 XGCValues xgcv;
3955 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3956 XSetForeground (s->display, s->gc, s->face->box_color);
3957 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3959 /* Top. */
3960 XFillRectangle (s->display, s->window, s->gc,
3961 left_x, top_y, right_x - left_x + 1, width);
3963 /* Left. */
3964 if (left_p)
3965 XFillRectangle (s->display, s->window, s->gc,
3966 left_x, top_y, width, bottom_y - top_y + 1);
3968 /* Bottom. */
3969 XFillRectangle (s->display, s->window, s->gc,
3970 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3972 /* Right. */
3973 if (right_p)
3974 XFillRectangle (s->display, s->window, s->gc,
3975 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3977 XSetForeground (s->display, s->gc, xgcv.foreground);
3978 XSetClipMask (s->display, s->gc, None);
3982 /* Draw a box around glyph string S. */
3984 static void
3985 x_draw_glyph_string_box (s)
3986 struct glyph_string *s;
3988 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3989 int left_p, right_p;
3990 struct glyph *last_glyph;
3991 XRectangle clip_rect;
3993 last_x = window_box_right (s->w, s->area);
3994 if (s->row->full_width_p
3995 && !s->w->pseudo_window_p)
3997 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3998 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3999 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
4002 /* The glyph that may have a right box line. */
4003 last_glyph = (s->cmp || s->img
4004 ? s->first_glyph
4005 : s->first_glyph + s->nchars - 1);
4007 width = abs (s->face->box_line_width);
4008 raised_p = s->face->box == FACE_RAISED_BOX;
4009 left_x = s->x;
4010 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
4011 ? last_x - 1
4012 : min (last_x, s->x + s->background_width) - 1);
4013 top_y = s->y;
4014 bottom_y = top_y + s->height - 1;
4016 left_p = (s->first_glyph->left_box_line_p
4017 || (s->hl == DRAW_MOUSE_FACE
4018 && (s->prev == NULL
4019 || s->prev->hl != s->hl)));
4020 right_p = (last_glyph->right_box_line_p
4021 || (s->hl == DRAW_MOUSE_FACE
4022 && (s->next == NULL
4023 || s->next->hl != s->hl)));
4025 x_get_glyph_string_clip_rect (s, &clip_rect);
4027 if (s->face->box == FACE_SIMPLE_BOX)
4028 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
4029 left_p, right_p, &clip_rect);
4030 else
4032 x_setup_relief_colors (s);
4033 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
4034 width, raised_p, left_p, right_p, &clip_rect);
4039 /* Draw foreground of image glyph string S. */
4041 static void
4042 x_draw_image_foreground (s)
4043 struct glyph_string *s;
4045 int x;
4046 int y = s->ybase - image_ascent (s->img, s->face);
4048 /* If first glyph of S has a left box line, start drawing it to the
4049 right of that line. */
4050 if (s->face->box != FACE_NO_BOX
4051 && s->first_glyph->left_box_line_p)
4052 x = s->x + abs (s->face->box_line_width);
4053 else
4054 x = s->x;
4056 /* If there is a margin around the image, adjust x- and y-position
4057 by that margin. */
4058 x += s->img->hmargin;
4059 y += s->img->vmargin;
4061 if (s->img->pixmap)
4063 if (s->img->mask)
4065 /* We can't set both a clip mask and use XSetClipRectangles
4066 because the latter also sets a clip mask. We also can't
4067 trust on the shape extension to be available
4068 (XShapeCombineRegion). So, compute the rectangle to draw
4069 manually. */
4070 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4071 | GCFunction);
4072 XGCValues xgcv;
4073 XRectangle clip_rect, image_rect, r;
4075 xgcv.clip_mask = s->img->mask;
4076 xgcv.clip_x_origin = x;
4077 xgcv.clip_y_origin = y;
4078 xgcv.function = GXcopy;
4079 XChangeGC (s->display, s->gc, mask, &xgcv);
4081 x_get_glyph_string_clip_rect (s, &clip_rect);
4082 image_rect.x = x;
4083 image_rect.y = y;
4084 image_rect.width = s->img->width;
4085 image_rect.height = s->img->height;
4086 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4087 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4088 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4090 else
4092 unsigned long mask = GCClipXOrigin | GCClipYOrigin | GCFunction;
4093 XGCValues xgcv;
4094 XRectangle clip_rect, image_rect, r;
4096 x_get_glyph_string_clip_rect (s, &clip_rect);
4097 image_rect.x = x;
4098 image_rect.y = y;
4099 image_rect.width = s->img->width;
4100 image_rect.height = s->img->height;
4101 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4102 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4103 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4105 /* When the image has a mask, we can expect that at
4106 least part of a mouse highlight or a block cursor will
4107 be visible. If the image doesn't have a mask, make
4108 a block cursor visible by drawing a rectangle around
4109 the image. I believe it's looking better if we do
4110 nothing here for mouse-face. */
4111 if (s->hl == DRAW_CURSOR)
4112 XDrawRectangle (s->display, s->window, s->gc, x, y,
4113 s->img->width - 1, s->img->height - 1);
4116 else
4117 /* Draw a rectangle if image could not be loaded. */
4118 XDrawRectangle (s->display, s->window, s->gc, x, y,
4119 s->img->width - 1, s->img->height - 1);
4123 /* Draw a relief around the image glyph string S. */
4125 static void
4126 x_draw_image_relief (s)
4127 struct glyph_string *s;
4129 int x0, y0, x1, y1, thick, raised_p;
4130 XRectangle r;
4131 int x;
4132 int y = s->ybase - image_ascent (s->img, s->face);
4134 /* If first glyph of S has a left box line, start drawing it to the
4135 right of that line. */
4136 if (s->face->box != FACE_NO_BOX
4137 && s->first_glyph->left_box_line_p)
4138 x = s->x + abs (s->face->box_line_width);
4139 else
4140 x = s->x;
4142 /* If there is a margin around the image, adjust x- and y-position
4143 by that margin. */
4144 x += s->img->hmargin;
4145 y += s->img->vmargin;
4147 if (s->hl == DRAW_IMAGE_SUNKEN
4148 || s->hl == DRAW_IMAGE_RAISED)
4150 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
4151 raised_p = s->hl == DRAW_IMAGE_RAISED;
4153 else
4155 thick = abs (s->img->relief);
4156 raised_p = s->img->relief > 0;
4159 x0 = x - thick;
4160 y0 = y - thick;
4161 x1 = x + s->img->width + thick - 1;
4162 y1 = y + s->img->height + thick - 1;
4164 x_setup_relief_colors (s);
4165 x_get_glyph_string_clip_rect (s, &r);
4166 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
4170 /* Draw the foreground of image glyph string S to PIXMAP. */
4172 static void
4173 x_draw_image_foreground_1 (s, pixmap)
4174 struct glyph_string *s;
4175 Pixmap pixmap;
4177 int x;
4178 int y = s->ybase - s->y - image_ascent (s->img, s->face);
4180 /* If first glyph of S has a left box line, start drawing it to the
4181 right of that line. */
4182 if (s->face->box != FACE_NO_BOX
4183 && s->first_glyph->left_box_line_p)
4184 x = abs (s->face->box_line_width);
4185 else
4186 x = 0;
4188 /* If there is a margin around the image, adjust x- and y-position
4189 by that margin. */
4190 x += s->img->hmargin;
4191 y += s->img->vmargin;
4193 if (s->img->pixmap)
4195 if (s->img->mask)
4197 /* We can't set both a clip mask and use XSetClipRectangles
4198 because the latter also sets a clip mask. We also can't
4199 trust on the shape extension to be available
4200 (XShapeCombineRegion). So, compute the rectangle to draw
4201 manually. */
4202 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4203 | GCFunction);
4204 XGCValues xgcv;
4206 xgcv.clip_mask = s->img->mask;
4207 xgcv.clip_x_origin = x;
4208 xgcv.clip_y_origin = y;
4209 xgcv.function = GXcopy;
4210 XChangeGC (s->display, s->gc, mask, &xgcv);
4212 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4213 0, 0, s->img->width, s->img->height, x, y);
4214 XSetClipMask (s->display, s->gc, None);
4216 else
4218 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4219 0, 0, s->img->width, s->img->height, x, y);
4221 /* When the image has a mask, we can expect that at
4222 least part of a mouse highlight or a block cursor will
4223 be visible. If the image doesn't have a mask, make
4224 a block cursor visible by drawing a rectangle around
4225 the image. I believe it's looking better if we do
4226 nothing here for mouse-face. */
4227 if (s->hl == DRAW_CURSOR)
4228 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4229 s->img->width - 1, s->img->height - 1);
4232 else
4233 /* Draw a rectangle if image could not be loaded. */
4234 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4235 s->img->width - 1, s->img->height - 1);
4239 /* Draw part of the background of glyph string S. X, Y, W, and H
4240 give the rectangle to draw. */
4242 static void
4243 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4244 struct glyph_string *s;
4245 int x, y, w, h;
4247 if (s->stippled_p)
4249 /* Fill background with a stipple pattern. */
4250 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4251 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4252 XSetFillStyle (s->display, s->gc, FillSolid);
4254 else
4255 x_clear_glyph_string_rect (s, x, y, w, h);
4259 /* Draw image glyph string S.
4261 s->y
4262 s->x +-------------------------
4263 | s->face->box
4265 | +-------------------------
4266 | | s->img->margin
4268 | | +-------------------
4269 | | | the image
4273 static void
4274 x_draw_image_glyph_string (s)
4275 struct glyph_string *s;
4277 int x, y;
4278 int box_line_hwidth = abs (s->face->box_line_width);
4279 int box_line_vwidth = max (s->face->box_line_width, 0);
4280 int height;
4281 Pixmap pixmap = None;
4283 height = s->height - 2 * box_line_vwidth;
4285 /* Fill background with face under the image. Do it only if row is
4286 taller than image or if image has a clip mask to reduce
4287 flickering. */
4288 s->stippled_p = s->face->stipple != 0;
4289 if (height > s->img->height
4290 || s->img->hmargin
4291 || s->img->vmargin
4292 || s->img->mask
4293 || s->img->pixmap == 0
4294 || s->width != s->background_width)
4296 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4297 x = s->x + box_line_hwidth;
4298 else
4299 x = s->x;
4301 y = s->y + box_line_vwidth;
4303 if (s->img->mask)
4305 /* Create a pixmap as large as the glyph string. Fill it
4306 with the background color. Copy the image to it, using
4307 its mask. Copy the temporary pixmap to the display. */
4308 Screen *screen = FRAME_X_SCREEN (s->f);
4309 int depth = DefaultDepthOfScreen (screen);
4311 /* Create a pixmap as large as the glyph string. */
4312 pixmap = XCreatePixmap (s->display, s->window,
4313 s->background_width,
4314 s->height, depth);
4316 /* Don't clip in the following because we're working on the
4317 pixmap. */
4318 XSetClipMask (s->display, s->gc, None);
4320 /* Fill the pixmap with the background color/stipple. */
4321 if (s->stippled_p)
4323 /* Fill background with a stipple pattern. */
4324 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4325 XFillRectangle (s->display, pixmap, s->gc,
4326 0, 0, s->background_width, s->height);
4327 XSetFillStyle (s->display, s->gc, FillSolid);
4329 else
4331 XGCValues xgcv;
4332 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4333 &xgcv);
4334 XSetForeground (s->display, s->gc, xgcv.background);
4335 XFillRectangle (s->display, pixmap, s->gc,
4336 0, 0, s->background_width, s->height);
4337 XSetForeground (s->display, s->gc, xgcv.foreground);
4340 else
4341 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4343 s->background_filled_p = 1;
4346 /* Draw the foreground. */
4347 if (pixmap != None)
4349 x_draw_image_foreground_1 (s, pixmap);
4350 x_set_glyph_string_clipping (s);
4351 XCopyArea (s->display, pixmap, s->window, s->gc,
4352 0, 0, s->background_width, s->height, s->x, s->y);
4353 XFreePixmap (s->display, pixmap);
4355 else
4356 x_draw_image_foreground (s);
4358 /* If we must draw a relief around the image, do it. */
4359 if (s->img->relief
4360 || s->hl == DRAW_IMAGE_RAISED
4361 || s->hl == DRAW_IMAGE_SUNKEN)
4362 x_draw_image_relief (s);
4366 /* Draw stretch glyph string S. */
4368 static void
4369 x_draw_stretch_glyph_string (s)
4370 struct glyph_string *s;
4372 xassert (s->first_glyph->type == STRETCH_GLYPH);
4373 s->stippled_p = s->face->stipple != 0;
4375 if (s->hl == DRAW_CURSOR
4376 && !x_stretch_cursor_p)
4378 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4379 as wide as the stretch glyph. */
4380 int width = min (CANON_X_UNIT (s->f), s->background_width);
4382 /* Draw cursor. */
4383 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4385 /* Clear rest using the GC of the original non-cursor face. */
4386 if (width < s->background_width)
4388 int x = s->x + width, y = s->y;
4389 int w = s->background_width - width, h = s->height;
4390 XRectangle r;
4391 GC gc;
4393 if (s->row->mouse_face_p
4394 && cursor_in_mouse_face_p (s->w))
4396 x_set_mouse_face_gc (s);
4397 gc = s->gc;
4399 else
4400 gc = s->face->gc;
4402 x_get_glyph_string_clip_rect (s, &r);
4403 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4405 if (s->face->stipple)
4407 /* Fill background with a stipple pattern. */
4408 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4409 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4410 XSetFillStyle (s->display, gc, FillSolid);
4412 else
4414 XGCValues xgcv;
4415 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4416 XSetForeground (s->display, gc, xgcv.background);
4417 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4418 XSetForeground (s->display, gc, xgcv.foreground);
4422 else if (!s->background_filled_p)
4423 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4424 s->height);
4426 s->background_filled_p = 1;
4430 /* Draw glyph string S. */
4432 static void
4433 x_draw_glyph_string (s)
4434 struct glyph_string *s;
4436 int relief_drawn_p = 0;
4438 /* If S draws into the background of its successor, draw the
4439 background of the successor first so that S can draw into it.
4440 This makes S->next use XDrawString instead of XDrawImageString. */
4441 if (s->next && s->right_overhang && !s->for_overlaps_p)
4443 xassert (s->next->img == NULL);
4444 x_set_glyph_string_gc (s->next);
4445 x_set_glyph_string_clipping (s->next);
4446 x_draw_glyph_string_background (s->next, 1);
4449 /* Set up S->gc, set clipping and draw S. */
4450 x_set_glyph_string_gc (s);
4451 x_set_glyph_string_clipping (s);
4453 /* Draw relief (if any) in advance for char/composition so that the
4454 glyph string can be drawn over it. */
4455 if (!s->for_overlaps_p
4456 && s->face->box != FACE_NO_BOX
4457 && (s->first_glyph->type == CHAR_GLYPH
4458 || s->first_glyph->type == COMPOSITE_GLYPH))
4461 x_draw_glyph_string_background (s, 1);
4462 x_draw_glyph_string_box (s);
4463 relief_drawn_p = 1;
4466 switch (s->first_glyph->type)
4468 case IMAGE_GLYPH:
4469 x_draw_image_glyph_string (s);
4470 break;
4472 case STRETCH_GLYPH:
4473 x_draw_stretch_glyph_string (s);
4474 break;
4476 case CHAR_GLYPH:
4477 if (s->for_overlaps_p)
4478 s->background_filled_p = 1;
4479 else
4480 x_draw_glyph_string_background (s, 0);
4481 x_draw_glyph_string_foreground (s);
4482 break;
4484 case COMPOSITE_GLYPH:
4485 if (s->for_overlaps_p || s->gidx > 0)
4486 s->background_filled_p = 1;
4487 else
4488 x_draw_glyph_string_background (s, 1);
4489 x_draw_composite_glyph_string_foreground (s);
4490 break;
4492 default:
4493 abort ();
4496 if (!s->for_overlaps_p)
4498 /* Draw underline. */
4499 if (s->face->underline_p)
4501 unsigned long tem, h;
4502 int y;
4504 /* Get the underline thickness. Default is 1 pixel. */
4505 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4506 h = 1;
4508 /* Get the underline position. This is the recommended
4509 vertical offset in pixels from the baseline to the top of
4510 the underline. This is a signed value according to the
4511 specs, and its default is
4513 ROUND ((maximum descent) / 2), with
4514 ROUND(x) = floor (x + 0.5) */
4516 if (x_use_underline_position_properties
4517 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4518 y = s->ybase + (long) tem;
4519 else if (s->face->font)
4520 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4521 else
4522 y = s->y + s->height - h;
4524 if (s->face->underline_defaulted_p)
4525 XFillRectangle (s->display, s->window, s->gc,
4526 s->x, y, s->width, h);
4527 else
4529 XGCValues xgcv;
4530 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4531 XSetForeground (s->display, s->gc, s->face->underline_color);
4532 XFillRectangle (s->display, s->window, s->gc,
4533 s->x, y, s->width, h);
4534 XSetForeground (s->display, s->gc, xgcv.foreground);
4538 /* Draw overline. */
4539 if (s->face->overline_p)
4541 unsigned long dy = 0, h = 1;
4543 if (s->face->overline_color_defaulted_p)
4544 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4545 s->width, h);
4546 else
4548 XGCValues xgcv;
4549 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4550 XSetForeground (s->display, s->gc, s->face->overline_color);
4551 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4552 s->width, h);
4553 XSetForeground (s->display, s->gc, xgcv.foreground);
4557 /* Draw strike-through. */
4558 if (s->face->strike_through_p)
4560 unsigned long h = 1;
4561 unsigned long dy = (s->height - h) / 2;
4563 if (s->face->strike_through_color_defaulted_p)
4564 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4565 s->width, h);
4566 else
4568 XGCValues xgcv;
4569 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4570 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4571 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4572 s->width, h);
4573 XSetForeground (s->display, s->gc, xgcv.foreground);
4577 /* Draw relief if not yet drawn. */
4578 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
4579 x_draw_glyph_string_box (s);
4582 /* Reset clipping. */
4583 XSetClipMask (s->display, s->gc, None);
4587 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4588 struct face **, int));
4591 /* Fill glyph string S with composition components specified by S->cmp.
4593 FACES is an array of faces for all components of this composition.
4594 S->gidx is the index of the first component for S.
4595 OVERLAPS_P non-zero means S should draw the foreground only, and
4596 use its physical height for clipping.
4598 Value is the index of a component not in S. */
4600 static int
4601 x_fill_composite_glyph_string (s, faces, overlaps_p)
4602 struct glyph_string *s;
4603 struct face **faces;
4604 int overlaps_p;
4606 int i;
4608 xassert (s);
4610 s->for_overlaps_p = overlaps_p;
4612 s->face = faces[s->gidx];
4613 s->font = s->face->font;
4614 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4616 /* For all glyphs of this composition, starting at the offset
4617 S->gidx, until we reach the end of the definition or encounter a
4618 glyph that requires the different face, add it to S. */
4619 ++s->nchars;
4620 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4621 ++s->nchars;
4623 /* All glyph strings for the same composition has the same width,
4624 i.e. the width set for the first component of the composition. */
4626 s->width = s->first_glyph->pixel_width;
4628 /* If the specified font could not be loaded, use the frame's
4629 default font, but record the fact that we couldn't load it in
4630 the glyph string so that we can draw rectangles for the
4631 characters of the glyph string. */
4632 if (s->font == NULL)
4634 s->font_not_found_p = 1;
4635 s->font = FRAME_FONT (s->f);
4638 /* Adjust base line for subscript/superscript text. */
4639 s->ybase += s->first_glyph->voffset;
4641 xassert (s->face && s->face->gc);
4643 /* This glyph string must always be drawn with 16-bit functions. */
4644 s->two_byte_p = 1;
4646 return s->gidx + s->nchars;
4650 /* Fill glyph string S from a sequence of character glyphs.
4652 FACE_ID is the face id of the string. START is the index of the
4653 first glyph to consider, END is the index of the last + 1.
4654 OVERLAPS_P non-zero means S should draw the foreground only, and
4655 use its physical height for clipping.
4657 Value is the index of the first glyph not in S. */
4659 static int
4660 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4661 struct glyph_string *s;
4662 int face_id;
4663 int start, end, overlaps_p;
4665 struct glyph *glyph, *last;
4666 int voffset;
4667 int glyph_not_available_p;
4669 xassert (s->f == XFRAME (s->w->frame));
4670 xassert (s->nchars == 0);
4671 xassert (start >= 0 && end > start);
4673 s->for_overlaps_p = overlaps_p,
4674 glyph = s->row->glyphs[s->area] + start;
4675 last = s->row->glyphs[s->area] + end;
4676 voffset = glyph->voffset;
4678 glyph_not_available_p = glyph->glyph_not_available_p;
4680 while (glyph < last
4681 && glyph->type == CHAR_GLYPH
4682 && glyph->voffset == voffset
4683 /* Same face id implies same font, nowadays. */
4684 && glyph->face_id == face_id
4685 && glyph->glyph_not_available_p == glyph_not_available_p)
4687 int two_byte_p;
4689 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4690 s->char2b + s->nchars,
4691 &two_byte_p);
4692 s->two_byte_p = two_byte_p;
4693 ++s->nchars;
4694 xassert (s->nchars <= end - start);
4695 s->width += glyph->pixel_width;
4696 ++glyph;
4699 s->font = s->face->font;
4700 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4702 /* If the specified font could not be loaded, use the frame's font,
4703 but record the fact that we couldn't load it in
4704 S->font_not_found_p so that we can draw rectangles for the
4705 characters of the glyph string. */
4706 if (s->font == NULL || glyph_not_available_p)
4708 s->font_not_found_p = 1;
4709 s->font = FRAME_FONT (s->f);
4712 /* Adjust base line for subscript/superscript text. */
4713 s->ybase += voffset;
4715 xassert (s->face && s->face->gc);
4716 return glyph - s->row->glyphs[s->area];
4720 /* Fill glyph string S from image glyph S->first_glyph. */
4722 static void
4723 x_fill_image_glyph_string (s)
4724 struct glyph_string *s;
4726 xassert (s->first_glyph->type == IMAGE_GLYPH);
4727 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4728 xassert (s->img);
4729 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4730 s->font = s->face->font;
4731 s->width = s->first_glyph->pixel_width;
4733 /* Adjust base line for subscript/superscript text. */
4734 s->ybase += s->first_glyph->voffset;
4738 /* Fill glyph string S from a sequence of stretch glyphs.
4740 ROW is the glyph row in which the glyphs are found, AREA is the
4741 area within the row. START is the index of the first glyph to
4742 consider, END is the index of the last + 1.
4744 Value is the index of the first glyph not in S. */
4746 static int
4747 x_fill_stretch_glyph_string (s, row, area, start, end)
4748 struct glyph_string *s;
4749 struct glyph_row *row;
4750 enum glyph_row_area area;
4751 int start, end;
4753 struct glyph *glyph, *last;
4754 int voffset, face_id;
4756 xassert (s->first_glyph->type == STRETCH_GLYPH);
4758 glyph = s->row->glyphs[s->area] + start;
4759 last = s->row->glyphs[s->area] + end;
4760 face_id = glyph->face_id;
4761 s->face = FACE_FROM_ID (s->f, face_id);
4762 s->font = s->face->font;
4763 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4764 s->width = glyph->pixel_width;
4765 voffset = glyph->voffset;
4767 for (++glyph;
4768 (glyph < last
4769 && glyph->type == STRETCH_GLYPH
4770 && glyph->voffset == voffset
4771 && glyph->face_id == face_id);
4772 ++glyph)
4773 s->width += glyph->pixel_width;
4775 /* Adjust base line for subscript/superscript text. */
4776 s->ybase += voffset;
4778 /* The case that face->gc == 0 is handled when drawing the glyph
4779 string by calling PREPARE_FACE_FOR_DISPLAY. */
4780 xassert (s->face);
4781 return glyph - s->row->glyphs[s->area];
4785 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4786 of XChar2b structures for S; it can't be allocated in
4787 x_init_glyph_string because it must be allocated via `alloca'. W
4788 is the window on which S is drawn. ROW and AREA are the glyph row
4789 and area within the row from which S is constructed. START is the
4790 index of the first glyph structure covered by S. HL is a
4791 face-override for drawing S. */
4793 static void
4794 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4795 struct glyph_string *s;
4796 XChar2b *char2b;
4797 struct window *w;
4798 struct glyph_row *row;
4799 enum glyph_row_area area;
4800 int start;
4801 enum draw_glyphs_face hl;
4803 bzero (s, sizeof *s);
4804 s->w = w;
4805 s->f = XFRAME (w->frame);
4806 s->display = FRAME_X_DISPLAY (s->f);
4807 s->window = FRAME_X_WINDOW (s->f);
4808 s->char2b = char2b;
4809 s->hl = hl;
4810 s->row = row;
4811 s->area = area;
4812 s->first_glyph = row->glyphs[area] + start;
4813 s->height = row->height;
4814 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4816 /* Display the internal border below the tool-bar window. */
4817 if (s->w == XWINDOW (s->f->tool_bar_window))
4818 s->y -= s->f->output_data.x->internal_border_width;
4820 s->ybase = s->y + row->ascent;
4824 /* Set background width of glyph string S. START is the index of the
4825 first glyph following S. LAST_X is the right-most x-position + 1
4826 in the drawing area. */
4828 static INLINE void
4829 x_set_glyph_string_background_width (s, start, last_x)
4830 struct glyph_string *s;
4831 int start;
4832 int last_x;
4834 /* If the face of this glyph string has to be drawn to the end of
4835 the drawing area, set S->extends_to_end_of_line_p. */
4836 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4838 if (start == s->row->used[s->area]
4839 && s->hl == DRAW_NORMAL_TEXT
4840 && s->area == TEXT_AREA
4841 && (s->row->fill_line_p
4842 || s->face->background != default_face->background
4843 || s->face->stipple != default_face->stipple))
4844 s->extends_to_end_of_line_p = 1;
4846 /* If S extends its face to the end of the line, set its
4847 background_width to the distance to the right edge of the drawing
4848 area. */
4849 if (s->extends_to_end_of_line_p)
4850 s->background_width = last_x - s->x + 1;
4851 else
4852 s->background_width = s->width;
4856 /* Add a glyph string for a stretch glyph to the list of strings
4857 between HEAD and TAIL. START is the index of the stretch glyph in
4858 row area AREA of glyph row ROW. END is the index of the last glyph
4859 in that glyph row area. X is the current output position assigned
4860 to the new glyph string constructed. HL overrides that face of the
4861 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4862 is the right-most x-position of the drawing area. */
4864 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4865 and below -- keep them on one line. */
4866 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4867 do \
4869 s = (struct glyph_string *) alloca (sizeof *s); \
4870 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4871 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4872 x_append_glyph_string (&HEAD, &TAIL, s); \
4873 s->x = (X); \
4875 while (0)
4878 /* Add a glyph string for an image glyph to the list of strings
4879 between HEAD and TAIL. START is the index of the image glyph in
4880 row area AREA of glyph row ROW. END is the index of the last glyph
4881 in that glyph row area. X is the current output position assigned
4882 to the new glyph string constructed. HL overrides that face of the
4883 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4884 is the right-most x-position of the drawing area. */
4886 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4887 do \
4889 s = (struct glyph_string *) alloca (sizeof *s); \
4890 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4891 x_fill_image_glyph_string (s); \
4892 x_append_glyph_string (&HEAD, &TAIL, s); \
4893 ++START; \
4894 s->x = (X); \
4896 while (0)
4899 /* Add a glyph string for a sequence of character glyphs to the list
4900 of strings between HEAD and TAIL. START is the index of the first
4901 glyph in row area AREA of glyph row ROW that is part of the new
4902 glyph string. END is the index of the last glyph in that glyph row
4903 area. X is the current output position assigned to the new glyph
4904 string constructed. HL overrides that face of the glyph; e.g. it
4905 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4906 right-most x-position of the drawing area. */
4908 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4909 do \
4911 int c, face_id; \
4912 XChar2b *char2b; \
4914 c = (ROW)->glyphs[AREA][START].u.ch; \
4915 face_id = (ROW)->glyphs[AREA][START].face_id; \
4917 s = (struct glyph_string *) alloca (sizeof *s); \
4918 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4919 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4920 x_append_glyph_string (&HEAD, &TAIL, s); \
4921 s->x = (X); \
4922 START = x_fill_glyph_string (s, face_id, START, END, \
4923 OVERLAPS_P); \
4925 while (0)
4928 /* Add a glyph string for a composite sequence to the list of strings
4929 between HEAD and TAIL. START is the index of the first glyph in
4930 row area AREA of glyph row ROW that is part of the new glyph
4931 string. END is the index of the last glyph in that glyph row area.
4932 X is the current output position assigned to the new glyph string
4933 constructed. HL overrides that face of the glyph; e.g. it is
4934 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4935 x-position of the drawing area. */
4937 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4938 do { \
4939 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4940 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4941 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4942 struct composition *cmp = composition_table[cmp_id]; \
4943 int glyph_len = cmp->glyph_len; \
4944 XChar2b *char2b; \
4945 struct face **faces; \
4946 struct glyph_string *first_s = NULL; \
4947 int n; \
4949 base_face = base_face->ascii_face; \
4950 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4951 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4952 /* At first, fill in `char2b' and `faces'. */ \
4953 for (n = 0; n < glyph_len; n++) \
4955 int c = COMPOSITION_GLYPH (cmp, n); \
4956 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4957 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4958 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4959 this_face_id, char2b + n, 1); \
4962 /* Make glyph_strings for each glyph sequence that is drawable by \
4963 the same face, and append them to HEAD/TAIL. */ \
4964 for (n = 0; n < cmp->glyph_len;) \
4966 s = (struct glyph_string *) alloca (sizeof *s); \
4967 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4968 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4969 s->cmp = cmp; \
4970 s->gidx = n; \
4971 s->x = (X); \
4973 if (n == 0) \
4974 first_s = s; \
4976 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4979 ++START; \
4980 s = first_s; \
4981 } while (0)
4984 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4985 of AREA of glyph row ROW on window W between indices START and END.
4986 HL overrides the face for drawing glyph strings, e.g. it is
4987 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4988 x-positions of the drawing area.
4990 This is an ugly monster macro construct because we must use alloca
4991 to allocate glyph strings (because x_draw_glyphs can be called
4992 asynchronously). */
4994 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4995 do \
4997 HEAD = TAIL = NULL; \
4998 while (START < END) \
5000 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
5001 switch (first_glyph->type) \
5003 case CHAR_GLYPH: \
5004 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
5005 TAIL, HL, X, LAST_X, \
5006 OVERLAPS_P); \
5007 break; \
5009 case COMPOSITE_GLYPH: \
5010 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
5011 HEAD, TAIL, HL, X, LAST_X,\
5012 OVERLAPS_P); \
5013 break; \
5015 case STRETCH_GLYPH: \
5016 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
5017 HEAD, TAIL, HL, X, LAST_X); \
5018 break; \
5020 case IMAGE_GLYPH: \
5021 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
5022 TAIL, HL, X, LAST_X); \
5023 break; \
5025 default: \
5026 abort (); \
5029 x_set_glyph_string_background_width (s, START, LAST_X); \
5030 (X) += s->width; \
5033 while (0)
5036 /* Draw glyphs between START and END in AREA of ROW on window W,
5037 starting at x-position X. X is relative to AREA in W. HL is a
5038 face-override with the following meaning:
5040 DRAW_NORMAL_TEXT draw normally
5041 DRAW_CURSOR draw in cursor face
5042 DRAW_MOUSE_FACE draw in mouse face.
5043 DRAW_INVERSE_VIDEO draw in mode line face
5044 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
5045 DRAW_IMAGE_RAISED draw an image with a raised relief around it
5047 If REAL_START is non-null, return in *REAL_START the real starting
5048 position for display. This can be different from START in case
5049 overlapping glyphs must be displayed. If REAL_END is non-null,
5050 return in *REAL_END the real end position for display. This can be
5051 different from END in case overlapping glyphs must be displayed.
5053 If OVERLAPS_P is non-zero, draw only the foreground of characters
5054 and clip to the physical height of ROW.
5056 Value is the x-position reached, relative to AREA of W. */
5058 static int
5059 x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
5060 overlaps_p)
5061 struct window *w;
5062 int x;
5063 struct glyph_row *row;
5064 enum glyph_row_area area;
5065 int start, end;
5066 enum draw_glyphs_face hl;
5067 int *real_start, *real_end;
5068 int overlaps_p;
5070 struct glyph_string *head, *tail;
5071 struct glyph_string *s;
5072 int last_x, area_width;
5073 int x_reached;
5074 int i, j;
5076 /* Let's rather be paranoid than getting a SEGV. */
5077 end = min (end, row->used[area]);
5078 start = max (0, start);
5079 start = min (end, start);
5080 if (real_start)
5081 *real_start = start;
5082 if (real_end)
5083 *real_end = end;
5085 /* Translate X to frame coordinates. Set last_x to the right
5086 end of the drawing area. */
5087 if (row->full_width_p)
5089 /* X is relative to the left edge of W, without scroll bars
5090 or flag areas. */
5091 struct frame *f = XFRAME (w->frame);
5092 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
5093 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
5095 x += window_left_x;
5096 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
5097 last_x = window_left_x + area_width;
5099 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5101 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5102 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
5103 last_x += width;
5104 else
5105 x -= width;
5108 x += FRAME_INTERNAL_BORDER_WIDTH (f);
5109 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
5111 else
5113 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
5114 area_width = window_box_width (w, area);
5115 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
5118 /* Build a doubly-linked list of glyph_string structures between
5119 head and tail from what we have to draw. Note that the macro
5120 BUILD_GLYPH_STRINGS will modify its start parameter. That's
5121 the reason we use a separate variable `i'. */
5122 i = start;
5123 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
5124 overlaps_p);
5125 if (tail)
5126 x_reached = tail->x + tail->background_width;
5127 else
5128 x_reached = x;
5130 /* If there are any glyphs with lbearing < 0 or rbearing > width in
5131 the row, redraw some glyphs in front or following the glyph
5132 strings built above. */
5133 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
5135 int dummy_x = 0;
5136 struct glyph_string *h, *t;
5138 /* Compute overhangs for all glyph strings. */
5139 for (s = head; s; s = s->next)
5140 x_compute_glyph_string_overhangs (s);
5142 /* Prepend glyph strings for glyphs in front of the first glyph
5143 string that are overwritten because of the first glyph
5144 string's left overhang. The background of all strings
5145 prepended must be drawn because the first glyph string
5146 draws over it. */
5147 i = x_left_overwritten (head);
5148 if (i >= 0)
5150 j = i;
5151 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
5152 DRAW_NORMAL_TEXT, dummy_x, last_x,
5153 overlaps_p);
5154 start = i;
5155 if (real_start)
5156 *real_start = start;
5157 x_compute_overhangs_and_x (t, head->x, 1);
5158 x_prepend_glyph_string_lists (&head, &tail, h, t);
5161 /* Prepend glyph strings for glyphs in front of the first glyph
5162 string that overwrite that glyph string because of their
5163 right overhang. For these strings, only the foreground must
5164 be drawn, because it draws over the glyph string at `head'.
5165 The background must not be drawn because this would overwrite
5166 right overhangs of preceding glyphs for which no glyph
5167 strings exist. */
5168 i = x_left_overwriting (head);
5169 if (i >= 0)
5171 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
5172 DRAW_NORMAL_TEXT, dummy_x, last_x,
5173 overlaps_p);
5174 for (s = h; s; s = s->next)
5175 s->background_filled_p = 1;
5176 if (real_start)
5177 *real_start = i;
5178 x_compute_overhangs_and_x (t, head->x, 1);
5179 x_prepend_glyph_string_lists (&head, &tail, h, t);
5182 /* Append glyphs strings for glyphs following the last glyph
5183 string tail that are overwritten by tail. The background of
5184 these strings has to be drawn because tail's foreground draws
5185 over it. */
5186 i = x_right_overwritten (tail);
5187 if (i >= 0)
5189 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5190 DRAW_NORMAL_TEXT, x, last_x,
5191 overlaps_p);
5192 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5193 x_append_glyph_string_lists (&head, &tail, h, t);
5194 if (real_end)
5195 *real_end = i;
5198 /* Append glyph strings for glyphs following the last glyph
5199 string tail that overwrite tail. The foreground of such
5200 glyphs has to be drawn because it writes into the background
5201 of tail. The background must not be drawn because it could
5202 paint over the foreground of following glyphs. */
5203 i = x_right_overwriting (tail);
5204 if (i >= 0)
5206 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5207 DRAW_NORMAL_TEXT, x, last_x,
5208 overlaps_p);
5209 for (s = h; s; s = s->next)
5210 s->background_filled_p = 1;
5211 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5212 x_append_glyph_string_lists (&head, &tail, h, t);
5213 if (real_end)
5214 *real_end = i;
5218 /* Draw all strings. */
5219 for (s = head; s; s = s->next)
5220 x_draw_glyph_string (s);
5222 /* Value is the x-position up to which drawn, relative to AREA of W.
5223 This doesn't include parts drawn because of overhangs. */
5224 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5225 if (!row->full_width_p)
5227 if (area > LEFT_MARGIN_AREA)
5228 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5229 if (area > TEXT_AREA)
5230 x_reached -= window_box_width (w, TEXT_AREA);
5233 return x_reached;
5237 /* Fix the display of area AREA of overlapping row ROW in window W. */
5239 static void
5240 x_fix_overlapping_area (w, row, area)
5241 struct window *w;
5242 struct glyph_row *row;
5243 enum glyph_row_area area;
5245 int i, x;
5247 BLOCK_INPUT;
5249 if (area == LEFT_MARGIN_AREA)
5250 x = 0;
5251 else if (area == TEXT_AREA)
5252 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5253 else
5254 x = (window_box_width (w, LEFT_MARGIN_AREA)
5255 + window_box_width (w, TEXT_AREA));
5257 for (i = 0; i < row->used[area];)
5259 if (row->glyphs[area][i].overlaps_vertically_p)
5261 int start = i, start_x = x;
5265 x += row->glyphs[area][i].pixel_width;
5266 ++i;
5268 while (i < row->used[area]
5269 && row->glyphs[area][i].overlaps_vertically_p);
5271 x_draw_glyphs (w, start_x, row, area, start, i,
5272 (row->inverse_p
5273 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5274 NULL, NULL, 1);
5276 else
5278 x += row->glyphs[area][i].pixel_width;
5279 ++i;
5283 UNBLOCK_INPUT;
5287 /* Output LEN glyphs starting at START at the nominal cursor position.
5288 Advance the nominal cursor over the text. The global variable
5289 updated_window contains the window being updated, updated_row is
5290 the glyph row being updated, and updated_area is the area of that
5291 row being updated. */
5293 static void
5294 x_write_glyphs (start, len)
5295 struct glyph *start;
5296 int len;
5298 int x, hpos, real_start, real_end;
5300 xassert (updated_window && updated_row);
5301 BLOCK_INPUT;
5303 /* Write glyphs. */
5305 hpos = start - updated_row->glyphs[updated_area];
5306 x = x_draw_glyphs (updated_window, output_cursor.x,
5307 updated_row, updated_area,
5308 hpos, hpos + len,
5309 (updated_row->inverse_p
5310 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5311 &real_start, &real_end, 0);
5313 /* If we drew over the cursor, note that it is not visible any more. */
5314 note_overwritten_text_cursor (updated_window, real_start,
5315 real_end - real_start);
5317 UNBLOCK_INPUT;
5319 /* Advance the output cursor. */
5320 output_cursor.hpos += len;
5321 output_cursor.x = x;
5325 /* Insert LEN glyphs from START at the nominal cursor position. */
5327 static void
5328 x_insert_glyphs (start, len)
5329 struct glyph *start;
5330 register int len;
5332 struct frame *f;
5333 struct window *w;
5334 int line_height, shift_by_width, shifted_region_width;
5335 struct glyph_row *row;
5336 struct glyph *glyph;
5337 int frame_x, frame_y, hpos, real_start, real_end;
5339 xassert (updated_window && updated_row);
5340 BLOCK_INPUT;
5341 w = updated_window;
5342 f = XFRAME (WINDOW_FRAME (w));
5344 /* Get the height of the line we are in. */
5345 row = updated_row;
5346 line_height = row->height;
5348 /* Get the width of the glyphs to insert. */
5349 shift_by_width = 0;
5350 for (glyph = start; glyph < start + len; ++glyph)
5351 shift_by_width += glyph->pixel_width;
5353 /* Get the width of the region to shift right. */
5354 shifted_region_width = (window_box_width (w, updated_area)
5355 - output_cursor.x
5356 - shift_by_width);
5358 /* Shift right. */
5359 frame_x = window_box_left (w, updated_area) + output_cursor.x;
5360 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5361 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5362 f->output_data.x->normal_gc,
5363 frame_x, frame_y,
5364 shifted_region_width, line_height,
5365 frame_x + shift_by_width, frame_y);
5367 /* Write the glyphs. */
5368 hpos = start - row->glyphs[updated_area];
5369 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5370 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
5371 note_overwritten_text_cursor (w, real_start, real_end - real_start);
5373 /* Advance the output cursor. */
5374 output_cursor.hpos += len;
5375 output_cursor.x += shift_by_width;
5376 UNBLOCK_INPUT;
5380 /* Delete N glyphs at the nominal cursor position. Not implemented
5381 for X frames. */
5383 static void
5384 x_delete_glyphs (n)
5385 register int n;
5387 abort ();
5391 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5392 If they are <= 0, this is probably an error. */
5394 void
5395 x_clear_area (dpy, window, x, y, width, height, exposures)
5396 Display *dpy;
5397 Window window;
5398 int x, y;
5399 int width, height;
5400 int exposures;
5402 xassert (width > 0 && height > 0);
5403 XClearArea (dpy, window, x, y, width, height, exposures);
5407 /* Erase the current text line from the nominal cursor position
5408 (inclusive) to pixel column TO_X (exclusive). The idea is that
5409 everything from TO_X onward is already erased.
5411 TO_X is a pixel position relative to updated_area of
5412 updated_window. TO_X == -1 means clear to the end of this area. */
5414 static void
5415 x_clear_end_of_line (to_x)
5416 int to_x;
5418 struct frame *f;
5419 struct window *w = updated_window;
5420 int max_x, min_y, max_y;
5421 int from_x, from_y, to_y;
5423 xassert (updated_window && updated_row);
5424 f = XFRAME (w->frame);
5426 if (updated_row->full_width_p)
5428 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5429 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5430 && !w->pseudo_window_p)
5431 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5433 else
5434 max_x = window_box_width (w, updated_area);
5435 max_y = window_text_bottom_y (w);
5437 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5438 of window. For TO_X > 0, truncate to end of drawing area. */
5439 if (to_x == 0)
5440 return;
5441 else if (to_x < 0)
5442 to_x = max_x;
5443 else
5444 to_x = min (to_x, max_x);
5446 to_y = min (max_y, output_cursor.y + updated_row->height);
5448 /* Notice if the cursor will be cleared by this operation. */
5449 if (!updated_row->full_width_p)
5450 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
5452 from_x = output_cursor.x;
5454 /* Translate to frame coordinates. */
5455 if (updated_row->full_width_p)
5457 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5458 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5460 else
5462 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5463 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5466 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5467 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5468 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5470 /* Prevent inadvertently clearing to end of the X window. */
5471 if (to_x > from_x && to_y > from_y)
5473 BLOCK_INPUT;
5474 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5475 from_x, from_y, to_x - from_x, to_y - from_y,
5476 False);
5477 UNBLOCK_INPUT;
5482 /* Clear entire frame. If updating_frame is non-null, clear that
5483 frame. Otherwise clear the selected frame. */
5485 static void
5486 x_clear_frame ()
5488 struct frame *f;
5490 if (updating_frame)
5491 f = updating_frame;
5492 else
5493 f = SELECTED_FRAME ();
5495 /* Clearing the frame will erase any cursor, so mark them all as no
5496 longer visible. */
5497 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5498 output_cursor.hpos = output_cursor.vpos = 0;
5499 output_cursor.x = -1;
5501 /* We don't set the output cursor here because there will always
5502 follow an explicit cursor_to. */
5503 BLOCK_INPUT;
5504 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5506 /* We have to clear the scroll bars, too. If we have changed
5507 colors or something like that, then they should be notified. */
5508 x_scroll_bar_clear (f);
5510 XFlush (FRAME_X_DISPLAY (f));
5511 UNBLOCK_INPUT;
5516 /* Invert the middle quarter of the frame for .15 sec. */
5518 /* We use the select system call to do the waiting, so we have to make
5519 sure it's available. If it isn't, we just won't do visual bells. */
5521 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5524 /* Subtract the `struct timeval' values X and Y, storing the result in
5525 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5527 static int
5528 timeval_subtract (result, x, y)
5529 struct timeval *result, x, y;
5531 /* Perform the carry for the later subtraction by updating y. This
5532 is safer because on some systems the tv_sec member is unsigned. */
5533 if (x.tv_usec < y.tv_usec)
5535 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5536 y.tv_usec -= 1000000 * nsec;
5537 y.tv_sec += nsec;
5540 if (x.tv_usec - y.tv_usec > 1000000)
5542 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5543 y.tv_usec += 1000000 * nsec;
5544 y.tv_sec -= nsec;
5547 /* Compute the time remaining to wait. tv_usec is certainly
5548 positive. */
5549 result->tv_sec = x.tv_sec - y.tv_sec;
5550 result->tv_usec = x.tv_usec - y.tv_usec;
5552 /* Return indication of whether the result should be considered
5553 negative. */
5554 return x.tv_sec < y.tv_sec;
5557 void
5558 XTflash (f)
5559 struct frame *f;
5561 BLOCK_INPUT;
5564 GC gc;
5566 /* Create a GC that will use the GXxor function to flip foreground
5567 pixels into background pixels. */
5569 XGCValues values;
5571 values.function = GXxor;
5572 values.foreground = (f->output_data.x->foreground_pixel
5573 ^ f->output_data.x->background_pixel);
5575 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5576 GCFunction | GCForeground, &values);
5580 /* Get the height not including a menu bar widget. */
5581 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5582 /* Height of each line to flash. */
5583 int flash_height = FRAME_LINE_HEIGHT (f);
5584 /* These will be the left and right margins of the rectangles. */
5585 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5586 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5588 int width;
5590 /* Don't flash the area between a scroll bar and the frame
5591 edge it is next to. */
5592 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5594 case vertical_scroll_bar_left:
5595 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5596 break;
5598 case vertical_scroll_bar_right:
5599 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5600 break;
5602 default:
5603 break;
5606 width = flash_right - flash_left;
5608 /* If window is tall, flash top and bottom line. */
5609 if (height > 3 * FRAME_LINE_HEIGHT (f))
5611 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5612 flash_left,
5613 (FRAME_INTERNAL_BORDER_WIDTH (f)
5614 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5615 width, flash_height);
5616 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5617 flash_left,
5618 (height - flash_height
5619 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5620 width, flash_height);
5622 else
5623 /* If it is short, flash it all. */
5624 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5625 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5626 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5628 x_flush (f);
5631 struct timeval wakeup;
5633 EMACS_GET_TIME (wakeup);
5635 /* Compute time to wait until, propagating carry from usecs. */
5636 wakeup.tv_usec += 150000;
5637 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5638 wakeup.tv_usec %= 1000000;
5640 /* Keep waiting until past the time wakeup or any input gets
5641 available. */
5642 while (! detect_input_pending ())
5644 struct timeval current;
5645 struct timeval timeout;
5647 EMACS_GET_TIME (current);
5649 /* Break if result would be negative. */
5650 if (timeval_subtract (&current, wakeup, current))
5651 break;
5653 /* How long `select' should wait. */
5654 timeout.tv_sec = 0;
5655 timeout.tv_usec = 10000;
5657 /* Try to wait that long--but we might wake up sooner. */
5658 select (0, NULL, NULL, NULL, &timeout);
5662 /* If window is tall, flash top and bottom line. */
5663 if (height > 3 * FRAME_LINE_HEIGHT (f))
5665 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5666 flash_left,
5667 (FRAME_INTERNAL_BORDER_WIDTH (f)
5668 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5669 width, flash_height);
5670 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5671 flash_left,
5672 (height - flash_height
5673 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5674 width, flash_height);
5676 else
5677 /* If it is short, flash it all. */
5678 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5679 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5680 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5682 XFreeGC (FRAME_X_DISPLAY (f), gc);
5683 x_flush (f);
5687 UNBLOCK_INPUT;
5690 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5693 /* Make audible bell. */
5695 void
5696 XTring_bell ()
5698 struct frame *f = SELECTED_FRAME ();
5700 if (FRAME_X_DISPLAY (f))
5702 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5703 if (visible_bell)
5704 XTflash (f);
5705 else
5706 #endif
5708 BLOCK_INPUT;
5709 XBell (FRAME_X_DISPLAY (f), 0);
5710 XFlush (FRAME_X_DISPLAY (f));
5711 UNBLOCK_INPUT;
5717 /* Specify how many text lines, from the top of the window,
5718 should be affected by insert-lines and delete-lines operations.
5719 This, and those operations, are used only within an update
5720 that is bounded by calls to x_update_begin and x_update_end. */
5722 static void
5723 XTset_terminal_window (n)
5724 register int n;
5726 /* This function intentionally left blank. */
5731 /***********************************************************************
5732 Line Dance
5733 ***********************************************************************/
5735 /* Perform an insert-lines or delete-lines operation, inserting N
5736 lines or deleting -N lines at vertical position VPOS. */
5738 static void
5739 x_ins_del_lines (vpos, n)
5740 int vpos, n;
5742 abort ();
5746 /* Scroll part of the display as described by RUN. */
5748 static void
5749 x_scroll_run (w, run)
5750 struct window *w;
5751 struct run *run;
5753 struct frame *f = XFRAME (w->frame);
5754 int x, y, width, height, from_y, to_y, bottom_y;
5756 /* Get frame-relative bounding box of the text display area of W,
5757 without mode lines. Include in this box the flags areas to the
5758 left and right of W. */
5759 window_box (w, -1, &x, &y, &width, &height);
5760 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5761 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
5763 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5764 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5765 bottom_y = y + height;
5767 if (to_y < from_y)
5769 /* Scrolling up. Make sure we don't copy part of the mode
5770 line at the bottom. */
5771 if (from_y + run->height > bottom_y)
5772 height = bottom_y - from_y;
5773 else
5774 height = run->height;
5776 else
5778 /* Scolling down. Make sure we don't copy over the mode line.
5779 at the bottom. */
5780 if (to_y + run->height > bottom_y)
5781 height = bottom_y - to_y;
5782 else
5783 height = run->height;
5786 BLOCK_INPUT;
5788 /* Cursor off. Will be switched on again in x_update_window_end. */
5789 updated_window = w;
5790 x_clear_cursor (w);
5792 XCopyArea (FRAME_X_DISPLAY (f),
5793 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5794 f->output_data.x->normal_gc,
5795 x, from_y,
5796 width, height,
5797 x, to_y);
5799 UNBLOCK_INPUT;
5804 /***********************************************************************
5805 Exposure Events
5806 ***********************************************************************/
5808 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5809 corner of the exposed rectangle. W and H are width and height of
5810 the exposed area. All are pixel values. W or H zero means redraw
5811 the entire frame. */
5813 static void
5814 expose_frame (f, x, y, w, h)
5815 struct frame *f;
5816 int x, y, w, h;
5818 XRectangle r;
5819 int mouse_face_overwritten_p = 0;
5821 TRACE ((stderr, "expose_frame "));
5823 /* No need to redraw if frame will be redrawn soon. */
5824 if (FRAME_GARBAGED_P (f))
5826 TRACE ((stderr, " garbaged\n"));
5827 return;
5830 /* If basic faces haven't been realized yet, there is no point in
5831 trying to redraw anything. This can happen when we get an expose
5832 event while Emacs is starting, e.g. by moving another window. */
5833 if (FRAME_FACE_CACHE (f) == NULL
5834 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5836 TRACE ((stderr, " no faces\n"));
5837 return;
5840 if (w == 0 || h == 0)
5842 r.x = r.y = 0;
5843 r.width = CANON_X_UNIT (f) * f->width;
5844 r.height = CANON_Y_UNIT (f) * f->height;
5846 else
5848 r.x = x;
5849 r.y = y;
5850 r.width = w;
5851 r.height = h;
5854 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5855 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
5857 if (WINDOWP (f->tool_bar_window))
5858 mouse_face_overwritten_p
5859 |= expose_window (XWINDOW (f->tool_bar_window), &r);
5861 #ifndef USE_X_TOOLKIT
5862 if (WINDOWP (f->menu_bar_window))
5863 mouse_face_overwritten_p
5864 |= expose_window (XWINDOW (f->menu_bar_window), &r);
5865 #endif /* not USE_X_TOOLKIT */
5867 /* Some window managers support a focus-follows-mouse style with
5868 delayed raising of frames. Imagine a partially obscured frame,
5869 and moving the mouse into partially obscured mouse-face on that
5870 frame. The visible part of the mouse-face will be highlighted,
5871 then the WM raises the obscured frame. With at least one WM, KDE
5872 2.1, Emacs is not getting any event for the raising of the frame
5873 (even tried with SubstructureRedirectMask), only Expose events.
5874 These expose events will draw text normally, i.e. not
5875 highlighted. Which means we must redo the highlight here.
5876 Subsume it under ``we love X''. --gerd 2001-08-15 */
5877 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
5879 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5880 if (f == dpyinfo->mouse_face_mouse_frame)
5882 int x = dpyinfo->mouse_face_mouse_x;
5883 int y = dpyinfo->mouse_face_mouse_y;
5884 clear_mouse_face (dpyinfo);
5885 note_mouse_highlight (f, x, y);
5891 /* Redraw (parts) of all windows in the window tree rooted at W that
5892 intersect R. R contains frame pixel coordinates. Value is
5893 non-zero if the exposure overwrites mouse-face. */
5895 static int
5896 expose_window_tree (w, r)
5897 struct window *w;
5898 XRectangle *r;
5900 struct frame *f = XFRAME (w->frame);
5901 int mouse_face_overwritten_p = 0;
5903 while (w && !FRAME_GARBAGED_P (f))
5905 if (!NILP (w->hchild))
5906 mouse_face_overwritten_p
5907 |= expose_window_tree (XWINDOW (w->hchild), r);
5908 else if (!NILP (w->vchild))
5909 mouse_face_overwritten_p
5910 |= expose_window_tree (XWINDOW (w->vchild), r);
5911 else
5912 mouse_face_overwritten_p |= expose_window (w, r);
5914 w = NILP (w->next) ? NULL : XWINDOW (w->next);
5917 return mouse_face_overwritten_p;
5921 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5922 which intersects rectangle R. R is in window-relative coordinates. */
5924 static void
5925 expose_area (w, row, r, area)
5926 struct window *w;
5927 struct glyph_row *row;
5928 XRectangle *r;
5929 enum glyph_row_area area;
5931 struct glyph *first = row->glyphs[area];
5932 struct glyph *end = row->glyphs[area] + row->used[area];
5933 struct glyph *last;
5934 int first_x, start_x, x;
5936 if (area == TEXT_AREA && row->fill_line_p)
5937 /* If row extends face to end of line write the whole line. */
5938 x_draw_glyphs (w, 0, row, area,
5939 0, row->used[area],
5940 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5941 NULL, NULL, 0);
5942 else
5944 /* Set START_X to the window-relative start position for drawing glyphs of
5945 AREA. The first glyph of the text area can be partially visible.
5946 The first glyphs of other areas cannot. */
5947 if (area == LEFT_MARGIN_AREA)
5948 start_x = 0;
5949 else if (area == TEXT_AREA)
5950 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5951 else
5952 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5953 + window_box_width (w, TEXT_AREA));
5954 x = start_x;
5956 /* Find the first glyph that must be redrawn. */
5957 while (first < end
5958 && x + first->pixel_width < r->x)
5960 x += first->pixel_width;
5961 ++first;
5964 /* Find the last one. */
5965 last = first;
5966 first_x = x;
5967 while (last < end
5968 && x < r->x + r->width)
5970 x += last->pixel_width;
5971 ++last;
5974 /* Repaint. */
5975 if (last > first)
5976 x_draw_glyphs (w, first_x - start_x, row, area,
5977 first - row->glyphs[area],
5978 last - row->glyphs[area],
5979 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5980 NULL, NULL, 0);
5985 /* Redraw the parts of the glyph row ROW on window W intersecting
5986 rectangle R. R is in window-relative coordinates. Value is
5987 non-zero if mouse-face was overwritten. */
5989 static int
5990 expose_line (w, row, r)
5991 struct window *w;
5992 struct glyph_row *row;
5993 XRectangle *r;
5995 xassert (row->enabled_p);
5997 if (row->mode_line_p || w->pseudo_window_p)
5998 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5999 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
6000 NULL, NULL, 0);
6001 else
6003 if (row->used[LEFT_MARGIN_AREA])
6004 expose_area (w, row, r, LEFT_MARGIN_AREA);
6005 if (row->used[TEXT_AREA])
6006 expose_area (w, row, r, TEXT_AREA);
6007 if (row->used[RIGHT_MARGIN_AREA])
6008 expose_area (w, row, r, RIGHT_MARGIN_AREA);
6009 x_draw_row_bitmaps (w, row);
6012 return row->mouse_face_p;
6016 /* Return non-zero if W's cursor intersects rectangle R. */
6018 static int
6019 x_phys_cursor_in_rect_p (w, r)
6020 struct window *w;
6021 XRectangle *r;
6023 XRectangle cr, result;
6024 struct glyph *cursor_glyph;
6026 cursor_glyph = get_phys_cursor_glyph (w);
6027 if (cursor_glyph)
6029 cr.x = w->phys_cursor.x;
6030 cr.y = w->phys_cursor.y;
6031 cr.width = cursor_glyph->pixel_width;
6032 cr.height = w->phys_cursor_height;
6033 return x_intersect_rectangles (&cr, r, &result);
6035 else
6036 return 0;
6040 /* Redraw the part of window W intersection rectangle FR. Pixel
6041 coordinates in FR are frame-relative. Call this function with
6042 input blocked. Value is non-zero if the exposure overwrites
6043 mouse-face. */
6045 static int
6046 expose_window (w, fr)
6047 struct window *w;
6048 XRectangle *fr;
6050 struct frame *f = XFRAME (w->frame);
6051 XRectangle wr, r;
6052 int mouse_face_overwritten_p = 0;
6054 /* If window is not yet fully initialized, do nothing. This can
6055 happen when toolkit scroll bars are used and a window is split.
6056 Reconfiguring the scroll bar will generate an expose for a newly
6057 created window. */
6058 if (w->current_matrix == NULL)
6059 return 0;
6061 /* When we're currently updating the window, display and current
6062 matrix usually don't agree. Arrange for a thorough display
6063 later. */
6064 if (w == updated_window)
6066 SET_FRAME_GARBAGED (f);
6067 return 0;
6070 /* Frame-relative pixel rectangle of W. */
6071 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
6072 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
6073 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
6074 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
6076 if (x_intersect_rectangles (fr, &wr, &r))
6078 int yb = window_text_bottom_y (w);
6079 struct glyph_row *row;
6080 int cursor_cleared_p;
6082 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
6083 r.x, r.y, r.width, r.height));
6085 /* Convert to window coordinates. */
6086 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
6087 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
6089 /* Turn off the cursor. */
6090 if (!w->pseudo_window_p
6091 && x_phys_cursor_in_rect_p (w, &r))
6093 x_clear_cursor (w);
6094 cursor_cleared_p = 1;
6096 else
6097 cursor_cleared_p = 0;
6099 /* Find the first row intersecting the rectangle R. */
6100 for (row = w->current_matrix->rows;
6101 row->enabled_p;
6102 ++row)
6104 int y0 = row->y;
6105 int y1 = MATRIX_ROW_BOTTOM_Y (row);
6107 if ((y0 >= r.y && y0 < r.y + r.height)
6108 || (y1 > r.y && y1 < r.y + r.height)
6109 || (r.y >= y0 && r.y < y1)
6110 || (r.y + r.height > y0 && r.y + r.height < y1))
6112 if (expose_line (w, row, &r))
6113 mouse_face_overwritten_p = 1;
6116 if (y1 >= yb)
6117 break;
6120 /* Display the mode line if there is one. */
6121 if (WINDOW_WANTS_MODELINE_P (w)
6122 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
6123 row->enabled_p)
6124 && row->y < r.y + r.height)
6126 if (expose_line (w, row, &r))
6127 mouse_face_overwritten_p = 1;
6130 if (!w->pseudo_window_p)
6132 /* Draw border between windows. */
6133 x_draw_vertical_border (w);
6135 /* Turn the cursor on again. */
6136 if (cursor_cleared_p)
6137 x_update_window_cursor (w, 1);
6141 return mouse_face_overwritten_p;
6145 /* Determine the intersection of two rectangles R1 and R2. Return
6146 the intersection in *RESULT. Value is non-zero if RESULT is not
6147 empty. */
6149 static int
6150 x_intersect_rectangles (r1, r2, result)
6151 XRectangle *r1, *r2, *result;
6153 XRectangle *left, *right;
6154 XRectangle *upper, *lower;
6155 int intersection_p = 0;
6157 /* Rearrange so that R1 is the left-most rectangle. */
6158 if (r1->x < r2->x)
6159 left = r1, right = r2;
6160 else
6161 left = r2, right = r1;
6163 /* X0 of the intersection is right.x0, if this is inside R1,
6164 otherwise there is no intersection. */
6165 if (right->x <= left->x + left->width)
6167 result->x = right->x;
6169 /* The right end of the intersection is the minimum of the
6170 the right ends of left and right. */
6171 result->width = (min (left->x + left->width, right->x + right->width)
6172 - result->x);
6174 /* Same game for Y. */
6175 if (r1->y < r2->y)
6176 upper = r1, lower = r2;
6177 else
6178 upper = r2, lower = r1;
6180 /* The upper end of the intersection is lower.y0, if this is inside
6181 of upper. Otherwise, there is no intersection. */
6182 if (lower->y <= upper->y + upper->height)
6184 result->y = lower->y;
6186 /* The lower end of the intersection is the minimum of the lower
6187 ends of upper and lower. */
6188 result->height = (min (lower->y + lower->height,
6189 upper->y + upper->height)
6190 - result->y);
6191 intersection_p = 1;
6195 return intersection_p;
6202 static void
6203 frame_highlight (f)
6204 struct frame *f;
6206 /* We used to only do this if Vx_no_window_manager was non-nil, but
6207 the ICCCM (section 4.1.6) says that the window's border pixmap
6208 and border pixel are window attributes which are "private to the
6209 client", so we can always change it to whatever we want. */
6210 BLOCK_INPUT;
6211 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6212 f->output_data.x->border_pixel);
6213 UNBLOCK_INPUT;
6214 x_update_cursor (f, 1);
6217 static void
6218 frame_unhighlight (f)
6219 struct frame *f;
6221 /* We used to only do this if Vx_no_window_manager was non-nil, but
6222 the ICCCM (section 4.1.6) says that the window's border pixmap
6223 and border pixel are window attributes which are "private to the
6224 client", so we can always change it to whatever we want. */
6225 BLOCK_INPUT;
6226 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6227 f->output_data.x->border_tile);
6228 UNBLOCK_INPUT;
6229 x_update_cursor (f, 1);
6232 /* The focus has changed. Update the frames as necessary to reflect
6233 the new situation. Note that we can't change the selected frame
6234 here, because the Lisp code we are interrupting might become confused.
6235 Each event gets marked with the frame in which it occurred, so the
6236 Lisp code can tell when the switch took place by examining the events. */
6238 static void
6239 x_new_focus_frame (dpyinfo, frame)
6240 struct x_display_info *dpyinfo;
6241 struct frame *frame;
6243 struct frame *old_focus = dpyinfo->x_focus_frame;
6245 if (frame != dpyinfo->x_focus_frame)
6247 /* Set this before calling other routines, so that they see
6248 the correct value of x_focus_frame. */
6249 dpyinfo->x_focus_frame = frame;
6251 if (old_focus && old_focus->auto_lower)
6252 x_lower_frame (old_focus);
6254 #if 0
6255 selected_frame = frame;
6256 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6257 selected_frame);
6258 Fselect_window (selected_frame->selected_window);
6259 choose_minibuf_frame ();
6260 #endif /* ! 0 */
6262 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6263 pending_autoraise_frame = dpyinfo->x_focus_frame;
6264 else
6265 pending_autoraise_frame = 0;
6268 x_frame_rehighlight (dpyinfo);
6271 /* Handle an event saying the mouse has moved out of an Emacs frame. */
6273 void
6274 x_mouse_leave (dpyinfo)
6275 struct x_display_info *dpyinfo;
6277 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
6280 /* The focus has changed, or we have redirected a frame's focus to
6281 another frame (this happens when a frame uses a surrogate
6282 mini-buffer frame). Shift the highlight as appropriate.
6284 The FRAME argument doesn't necessarily have anything to do with which
6285 frame is being highlighted or un-highlighted; we only use it to find
6286 the appropriate X display info. */
6288 static void
6289 XTframe_rehighlight (frame)
6290 struct frame *frame;
6292 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6295 static void
6296 x_frame_rehighlight (dpyinfo)
6297 struct x_display_info *dpyinfo;
6299 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6301 if (dpyinfo->x_focus_frame)
6303 dpyinfo->x_highlight_frame
6304 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6305 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6306 : dpyinfo->x_focus_frame);
6307 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
6309 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6310 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
6313 else
6314 dpyinfo->x_highlight_frame = 0;
6316 if (dpyinfo->x_highlight_frame != old_highlight)
6318 if (old_highlight)
6319 frame_unhighlight (old_highlight);
6320 if (dpyinfo->x_highlight_frame)
6321 frame_highlight (dpyinfo->x_highlight_frame);
6327 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
6329 /* Initialize mode_switch_bit and modifier_meaning. */
6330 static void
6331 x_find_modifier_meanings (dpyinfo)
6332 struct x_display_info *dpyinfo;
6334 int min_code, max_code;
6335 KeySym *syms;
6336 int syms_per_code;
6337 XModifierKeymap *mods;
6339 dpyinfo->meta_mod_mask = 0;
6340 dpyinfo->shift_lock_mask = 0;
6341 dpyinfo->alt_mod_mask = 0;
6342 dpyinfo->super_mod_mask = 0;
6343 dpyinfo->hyper_mod_mask = 0;
6345 #ifdef HAVE_X11R4
6346 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
6347 #else
6348 min_code = dpyinfo->display->min_keycode;
6349 max_code = dpyinfo->display->max_keycode;
6350 #endif
6352 syms = XGetKeyboardMapping (dpyinfo->display,
6353 min_code, max_code - min_code + 1,
6354 &syms_per_code);
6355 mods = XGetModifierMapping (dpyinfo->display);
6357 /* Scan the modifier table to see which modifier bits the Meta and
6358 Alt keysyms are on. */
6360 int row, col; /* The row and column in the modifier table. */
6362 for (row = 3; row < 8; row++)
6363 for (col = 0; col < mods->max_keypermod; col++)
6365 KeyCode code
6366 = mods->modifiermap[(row * mods->max_keypermod) + col];
6368 /* Zeroes are used for filler. Skip them. */
6369 if (code == 0)
6370 continue;
6372 /* Are any of this keycode's keysyms a meta key? */
6374 int code_col;
6376 for (code_col = 0; code_col < syms_per_code; code_col++)
6378 int sym = syms[((code - min_code) * syms_per_code) + code_col];
6380 switch (sym)
6382 case XK_Meta_L:
6383 case XK_Meta_R:
6384 dpyinfo->meta_mod_mask |= (1 << row);
6385 break;
6387 case XK_Alt_L:
6388 case XK_Alt_R:
6389 dpyinfo->alt_mod_mask |= (1 << row);
6390 break;
6392 case XK_Hyper_L:
6393 case XK_Hyper_R:
6394 dpyinfo->hyper_mod_mask |= (1 << row);
6395 break;
6397 case XK_Super_L:
6398 case XK_Super_R:
6399 dpyinfo->super_mod_mask |= (1 << row);
6400 break;
6402 case XK_Shift_Lock:
6403 /* Ignore this if it's not on the lock modifier. */
6404 if ((1 << row) == LockMask)
6405 dpyinfo->shift_lock_mask = LockMask;
6406 break;
6413 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
6414 if (! dpyinfo->meta_mod_mask)
6416 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6417 dpyinfo->alt_mod_mask = 0;
6420 /* If some keys are both alt and meta,
6421 make them just meta, not alt. */
6422 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6424 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6427 XFree ((char *) syms);
6428 XFreeModifiermap (mods);
6431 /* Convert between the modifier bits X uses and the modifier bits
6432 Emacs uses. */
6434 static unsigned int
6435 x_x_to_emacs_modifiers (dpyinfo, state)
6436 struct x_display_info *dpyinfo;
6437 unsigned int state;
6439 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6440 | ((state & ControlMask) ? ctrl_modifier : 0)
6441 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6442 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6443 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6444 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
6447 static unsigned int
6448 x_emacs_to_x_modifiers (dpyinfo, state)
6449 struct x_display_info *dpyinfo;
6450 unsigned int state;
6452 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6453 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6454 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6455 | ((state & shift_modifier) ? ShiftMask : 0)
6456 | ((state & ctrl_modifier) ? ControlMask : 0)
6457 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
6460 /* Convert a keysym to its name. */
6462 char *
6463 x_get_keysym_name (keysym)
6464 KeySym keysym;
6466 char *value;
6468 BLOCK_INPUT;
6469 value = XKeysymToString (keysym);
6470 UNBLOCK_INPUT;
6472 return value;
6477 /* Mouse clicks and mouse movement. Rah. */
6479 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6480 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6481 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6482 not force the value into range. */
6484 void
6485 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6486 FRAME_PTR f;
6487 register int pix_x, pix_y;
6488 register int *x, *y;
6489 XRectangle *bounds;
6490 int noclip;
6492 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6493 even for negative values. */
6494 if (pix_x < 0)
6495 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6496 if (pix_y < 0)
6497 pix_y -= (f)->output_data.x->line_height - 1;
6499 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6500 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6502 if (bounds)
6504 bounds->width = FONT_WIDTH (f->output_data.x->font);
6505 bounds->height = f->output_data.x->line_height;
6506 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6507 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6510 if (!noclip)
6512 if (pix_x < 0)
6513 pix_x = 0;
6514 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6515 pix_x = FRAME_WINDOW_WIDTH (f);
6517 if (pix_y < 0)
6518 pix_y = 0;
6519 else if (pix_y > f->height)
6520 pix_y = f->height;
6523 *x = pix_x;
6524 *y = pix_y;
6528 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6529 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6530 can't tell the positions because W's display is not up to date,
6531 return 0. */
6534 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6535 struct window *w;
6536 int hpos, vpos;
6537 int *frame_x, *frame_y;
6539 int success_p;
6541 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6542 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6544 if (display_completed)
6546 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6547 struct glyph *glyph = row->glyphs[TEXT_AREA];
6548 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6550 *frame_y = row->y;
6551 *frame_x = row->x;
6552 while (glyph < end)
6554 *frame_x += glyph->pixel_width;
6555 ++glyph;
6558 success_p = 1;
6560 else
6562 *frame_y = *frame_x = 0;
6563 success_p = 0;
6566 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6567 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6568 return success_p;
6572 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6574 If the event is a button press, then note that we have grabbed
6575 the mouse. */
6577 static Lisp_Object
6578 construct_mouse_click (result, event, f)
6579 struct input_event *result;
6580 XButtonEvent *event;
6581 struct frame *f;
6583 /* Make the event type no_event; we'll change that when we decide
6584 otherwise. */
6585 result->kind = mouse_click;
6586 result->code = event->button - Button1;
6587 result->timestamp = event->time;
6588 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6589 event->state)
6590 | (event->type == ButtonRelease
6591 ? up_modifier
6592 : down_modifier));
6594 XSETINT (result->x, event->x);
6595 XSETINT (result->y, event->y);
6596 XSETFRAME (result->frame_or_window, f);
6597 result->arg = Qnil;
6598 return Qnil;
6602 /* Function to report a mouse movement to the mainstream Emacs code.
6603 The input handler calls this.
6605 We have received a mouse movement event, which is given in *event.
6606 If the mouse is over a different glyph than it was last time, tell
6607 the mainstream emacs code by setting mouse_moved. If not, ask for
6608 another motion event, so we can check again the next time it moves. */
6610 static XMotionEvent last_mouse_motion_event;
6611 static Lisp_Object last_mouse_motion_frame;
6613 static void
6614 note_mouse_movement (frame, event)
6615 FRAME_PTR frame;
6616 XMotionEvent *event;
6618 last_mouse_movement_time = event->time;
6619 last_mouse_motion_event = *event;
6620 XSETFRAME (last_mouse_motion_frame, frame);
6622 if (event->window != FRAME_X_WINDOW (frame))
6624 frame->mouse_moved = 1;
6625 last_mouse_scroll_bar = Qnil;
6626 note_mouse_highlight (frame, -1, -1);
6629 /* Has the mouse moved off the glyph it was on at the last sighting? */
6630 else if (event->x < last_mouse_glyph.x
6631 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6632 || event->y < last_mouse_glyph.y
6633 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6635 frame->mouse_moved = 1;
6636 last_mouse_scroll_bar = Qnil;
6637 note_mouse_highlight (frame, event->x, event->y);
6641 /* This is used for debugging, to turn off note_mouse_highlight. */
6643 int disable_mouse_highlight;
6647 /************************************************************************
6648 Mouse Face
6649 ************************************************************************/
6651 /* Find the glyph under window-relative coordinates X/Y in window W.
6652 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6653 strings. Return in *HPOS and *VPOS the row and column number of
6654 the glyph found. Return in *AREA the glyph area containing X.
6655 Value is a pointer to the glyph found or null if X/Y is not on
6656 text, or we can't tell because W's current matrix is not up to
6657 date. */
6659 static struct glyph *
6660 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
6661 struct window *w;
6662 int x, y;
6663 int *hpos, *vpos, *area;
6664 int buffer_only_p;
6666 struct glyph *glyph, *end;
6667 struct glyph_row *row = NULL;
6668 int x0, i, left_area_width;
6670 /* Find row containing Y. Give up if some row is not enabled. */
6671 for (i = 0; i < w->current_matrix->nrows; ++i)
6673 row = MATRIX_ROW (w->current_matrix, i);
6674 if (!row->enabled_p)
6675 return NULL;
6676 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6677 break;
6680 *vpos = i;
6681 *hpos = 0;
6683 /* Give up if Y is not in the window. */
6684 if (i == w->current_matrix->nrows)
6685 return NULL;
6687 /* Get the glyph area containing X. */
6688 if (w->pseudo_window_p)
6690 *area = TEXT_AREA;
6691 x0 = 0;
6693 else
6695 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6696 if (x < left_area_width)
6698 *area = LEFT_MARGIN_AREA;
6699 x0 = 0;
6701 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6703 *area = TEXT_AREA;
6704 x0 = row->x + left_area_width;
6706 else
6708 *area = RIGHT_MARGIN_AREA;
6709 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6713 /* Find glyph containing X. */
6714 glyph = row->glyphs[*area];
6715 end = glyph + row->used[*area];
6716 while (glyph < end)
6718 if (x < x0 + glyph->pixel_width)
6720 if (w->pseudo_window_p)
6721 break;
6722 else if (!buffer_only_p || BUFFERP (glyph->object))
6723 break;
6726 x0 += glyph->pixel_width;
6727 ++glyph;
6730 if (glyph == end)
6731 return NULL;
6733 *hpos = glyph - row->glyphs[*area];
6734 return glyph;
6738 /* Convert frame-relative x/y to coordinates relative to window W.
6739 Takes pseudo-windows into account. */
6741 static void
6742 frame_to_window_pixel_xy (w, x, y)
6743 struct window *w;
6744 int *x, *y;
6746 if (w->pseudo_window_p)
6748 /* A pseudo-window is always full-width, and starts at the
6749 left edge of the frame, plus a frame border. */
6750 struct frame *f = XFRAME (w->frame);
6751 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6752 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6754 else
6756 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6757 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6762 /* Take proper action when mouse has moved to the mode or header line of
6763 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6764 mode line. X is relative to the start of the text display area of
6765 W, so the width of bitmap areas and scroll bars must be subtracted
6766 to get a position relative to the start of the mode line. */
6768 static void
6769 note_mode_line_highlight (w, x, mode_line_p)
6770 struct window *w;
6771 int x, mode_line_p;
6773 struct frame *f = XFRAME (w->frame);
6774 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6775 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6776 struct glyph_row *row;
6778 if (mode_line_p)
6779 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6780 else
6781 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6783 if (row->enabled_p)
6785 struct glyph *glyph, *end;
6786 Lisp_Object help, map;
6787 int x0;
6789 /* Find the glyph under X. */
6790 glyph = row->glyphs[TEXT_AREA];
6791 end = glyph + row->used[TEXT_AREA];
6792 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6793 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
6795 while (glyph < end
6796 && x >= x0 + glyph->pixel_width)
6798 x0 += glyph->pixel_width;
6799 ++glyph;
6802 if (glyph < end
6803 && STRINGP (glyph->object)
6804 && XSTRING (glyph->object)->intervals
6805 && glyph->charpos >= 0
6806 && glyph->charpos < XSTRING (glyph->object)->size)
6808 /* If we're on a string with `help-echo' text property,
6809 arrange for the help to be displayed. This is done by
6810 setting the global variable help_echo to the help string. */
6811 help = Fget_text_property (make_number (glyph->charpos),
6812 Qhelp_echo, glyph->object);
6813 if (!NILP (help))
6815 help_echo = help;
6816 XSETWINDOW (help_echo_window, w);
6817 help_echo_object = glyph->object;
6818 help_echo_pos = glyph->charpos;
6821 /* Change the mouse pointer according to what is under X/Y. */
6822 map = Fget_text_property (make_number (glyph->charpos),
6823 Qlocal_map, glyph->object);
6824 if (KEYMAPP (map))
6825 cursor = f->output_data.x->nontext_cursor;
6826 else
6828 map = Fget_text_property (make_number (glyph->charpos),
6829 Qkeymap, glyph->object);
6830 if (KEYMAPP (map))
6831 cursor = f->output_data.x->nontext_cursor;
6836 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6840 /* Take proper action when the mouse has moved to position X, Y on
6841 frame F as regards highlighting characters that have mouse-face
6842 properties. Also de-highlighting chars where the mouse was before.
6843 X and Y can be negative or out of range. */
6845 static void
6846 note_mouse_highlight (f, x, y)
6847 struct frame *f;
6848 int x, y;
6850 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6851 int portion;
6852 Lisp_Object window;
6853 struct window *w;
6854 Cursor cursor = None;
6855 struct buffer *b;
6857 /* When a menu is active, don't highlight because this looks odd. */
6858 #ifdef USE_X_TOOLKIT
6859 if (popup_activated ())
6860 return;
6861 #endif
6863 if (disable_mouse_highlight
6864 || !f->glyphs_initialized_p)
6865 return;
6867 dpyinfo->mouse_face_mouse_x = x;
6868 dpyinfo->mouse_face_mouse_y = y;
6869 dpyinfo->mouse_face_mouse_frame = f;
6871 if (dpyinfo->mouse_face_defer)
6872 return;
6874 if (gc_in_progress)
6876 dpyinfo->mouse_face_deferred_gc = 1;
6877 return;
6880 /* Which window is that in? */
6881 window = window_from_coordinates (f, x, y, &portion, 1);
6883 /* If we were displaying active text in another window, clear that. */
6884 if (! EQ (window, dpyinfo->mouse_face_window))
6885 clear_mouse_face (dpyinfo);
6887 /* Not on a window -> return. */
6888 if (!WINDOWP (window))
6889 return;
6891 /* Convert to window-relative pixel coordinates. */
6892 w = XWINDOW (window);
6893 frame_to_window_pixel_xy (w, &x, &y);
6895 /* Handle tool-bar window differently since it doesn't display a
6896 buffer. */
6897 if (EQ (window, f->tool_bar_window))
6899 note_tool_bar_highlight (f, x, y);
6900 return;
6903 /* Mouse is on the mode or header line? */
6904 if (portion == 1 || portion == 3)
6906 note_mode_line_highlight (w, x, portion == 1);
6907 return;
6910 if (portion == 2)
6911 cursor = f->output_data.x->horizontal_drag_cursor;
6912 else
6913 cursor = f->output_data.x->text_cursor;
6915 /* Are we in a window whose display is up to date?
6916 And verify the buffer's text has not changed. */
6917 b = XBUFFER (w->buffer);
6918 if (/* Within text portion of the window. */
6919 portion == 0
6920 && EQ (w->window_end_valid, w->buffer)
6921 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
6922 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
6924 int hpos, vpos, pos, i, area;
6925 struct glyph *glyph;
6926 Lisp_Object object;
6927 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
6928 Lisp_Object *overlay_vec = NULL;
6929 int len, noverlays;
6930 struct buffer *obuf;
6931 int obegv, ozv, same_region;
6933 /* Find the glyph under X/Y. */
6934 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
6936 /* Clear mouse face if X/Y not over text. */
6937 if (glyph == NULL
6938 || area != TEXT_AREA
6939 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6941 if (clear_mouse_face (dpyinfo))
6942 cursor = None;
6943 goto set_cursor;
6946 pos = glyph->charpos;
6947 object = glyph->object;
6948 if (!STRINGP (object) && !BUFFERP (object))
6949 goto set_cursor;
6951 /* If we get an out-of-range value, return now; avoid an error. */
6952 if (BUFFERP (object) && pos > BUF_Z (b))
6953 goto set_cursor;
6955 /* Make the window's buffer temporarily current for
6956 overlays_at and compute_char_face. */
6957 obuf = current_buffer;
6958 current_buffer = b;
6959 obegv = BEGV;
6960 ozv = ZV;
6961 BEGV = BEG;
6962 ZV = Z;
6964 /* Is this char mouse-active or does it have help-echo? */
6965 position = make_number (pos);
6967 if (BUFFERP (object))
6969 /* Put all the overlays we want in a vector in overlay_vec.
6970 Store the length in len. If there are more than 10, make
6971 enough space for all, and try again. */
6972 len = 10;
6973 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6974 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6975 if (noverlays > len)
6977 len = noverlays;
6978 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6979 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6982 /* Sort overlays into increasing priority order. */
6983 noverlays = sort_overlays (overlay_vec, noverlays, w);
6985 else
6986 noverlays = 0;
6988 same_region = (EQ (window, dpyinfo->mouse_face_window)
6989 && vpos >= dpyinfo->mouse_face_beg_row
6990 && vpos <= dpyinfo->mouse_face_end_row
6991 && (vpos > dpyinfo->mouse_face_beg_row
6992 || hpos >= dpyinfo->mouse_face_beg_col)
6993 && (vpos < dpyinfo->mouse_face_end_row
6994 || hpos < dpyinfo->mouse_face_end_col
6995 || dpyinfo->mouse_face_past_end));
6997 if (same_region)
6998 cursor = None;
7000 /* Check mouse-face highlighting. */
7001 if (! same_region
7002 /* If there exists an overlay with mouse-face overlapping
7003 the one we are currently highlighting, we have to
7004 check if we enter the overlapping overlay, and then
7005 highlight only that. */
7006 || (OVERLAYP (dpyinfo->mouse_face_overlay)
7007 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
7009 /* Find the highest priority overlay that has a mouse-face
7010 property. */
7011 overlay = Qnil;
7012 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
7014 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
7015 if (!NILP (mouse_face))
7016 overlay = overlay_vec[i];
7019 /* If we're actually highlighting the same overlay as
7020 before, there's no need to do that again. */
7021 if (!NILP (overlay)
7022 && EQ (overlay, dpyinfo->mouse_face_overlay))
7023 goto check_help_echo;
7025 dpyinfo->mouse_face_overlay = overlay;
7027 /* Clear the display of the old active region, if any. */
7028 if (clear_mouse_face (dpyinfo))
7029 cursor = None;
7031 /* If no overlay applies, get a text property. */
7032 if (NILP (overlay))
7033 mouse_face = Fget_text_property (position, Qmouse_face, object);
7035 /* Handle the overlay case. */
7036 if (!NILP (overlay))
7038 /* Find the range of text around this char that
7039 should be active. */
7040 Lisp_Object before, after;
7041 int ignore;
7043 before = Foverlay_start (overlay);
7044 after = Foverlay_end (overlay);
7045 /* Record this as the current active region. */
7046 fast_find_position (w, XFASTINT (before),
7047 &dpyinfo->mouse_face_beg_col,
7048 &dpyinfo->mouse_face_beg_row,
7049 &dpyinfo->mouse_face_beg_x,
7050 &dpyinfo->mouse_face_beg_y);
7051 dpyinfo->mouse_face_past_end
7052 = !fast_find_position (w, XFASTINT (after),
7053 &dpyinfo->mouse_face_end_col,
7054 &dpyinfo->mouse_face_end_row,
7055 &dpyinfo->mouse_face_end_x,
7056 &dpyinfo->mouse_face_end_y);
7057 dpyinfo->mouse_face_window = window;
7058 dpyinfo->mouse_face_face_id
7059 = face_at_buffer_position (w, pos, 0, 0,
7060 &ignore, pos + 1, 1);
7062 /* Display it as active. */
7063 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7064 cursor = None;
7066 /* Handle the text property case. */
7067 else if (!NILP (mouse_face) && BUFFERP (object))
7069 /* Find the range of text around this char that
7070 should be active. */
7071 Lisp_Object before, after, beginning, end;
7072 int ignore;
7074 beginning = Fmarker_position (w->start);
7075 end = make_number (BUF_Z (XBUFFER (object))
7076 - XFASTINT (w->window_end_pos));
7077 before
7078 = Fprevious_single_property_change (make_number (pos + 1),
7079 Qmouse_face,
7080 object, beginning);
7081 after
7082 = Fnext_single_property_change (position, Qmouse_face,
7083 object, end);
7085 /* Record this as the current active region. */
7086 fast_find_position (w, XFASTINT (before),
7087 &dpyinfo->mouse_face_beg_col,
7088 &dpyinfo->mouse_face_beg_row,
7089 &dpyinfo->mouse_face_beg_x,
7090 &dpyinfo->mouse_face_beg_y);
7091 dpyinfo->mouse_face_past_end
7092 = !fast_find_position (w, XFASTINT (after),
7093 &dpyinfo->mouse_face_end_col,
7094 &dpyinfo->mouse_face_end_row,
7095 &dpyinfo->mouse_face_end_x,
7096 &dpyinfo->mouse_face_end_y);
7097 dpyinfo->mouse_face_window = window;
7099 if (BUFFERP (object))
7100 dpyinfo->mouse_face_face_id
7101 = face_at_buffer_position (w, pos, 0, 0,
7102 &ignore, pos + 1, 1);
7104 /* Display it as active. */
7105 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7106 cursor = None;
7108 else if (!NILP (mouse_face) && STRINGP (object))
7110 Lisp_Object b, e;
7111 int ignore;
7113 b = Fprevious_single_property_change (make_number (pos + 1),
7114 Qmouse_face,
7115 object, Qnil);
7116 e = Fnext_single_property_change (position, Qmouse_face,
7117 object, Qnil);
7118 if (NILP (b))
7119 b = make_number (0);
7120 if (NILP (e))
7121 e = make_number (XSTRING (object)->size - 1);
7122 fast_find_string_pos (w, XINT (b), object,
7123 &dpyinfo->mouse_face_beg_col,
7124 &dpyinfo->mouse_face_beg_row,
7125 &dpyinfo->mouse_face_beg_x,
7126 &dpyinfo->mouse_face_beg_y, 0);
7127 fast_find_string_pos (w, XINT (e), object,
7128 &dpyinfo->mouse_face_end_col,
7129 &dpyinfo->mouse_face_end_row,
7130 &dpyinfo->mouse_face_end_x,
7131 &dpyinfo->mouse_face_end_y, 1);
7132 dpyinfo->mouse_face_past_end = 0;
7133 dpyinfo->mouse_face_window = window;
7134 dpyinfo->mouse_face_face_id
7135 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
7136 glyph->face_id, 1);
7137 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7138 cursor = None;
7142 check_help_echo:
7144 /* Look for a `help-echo' property. */
7146 Lisp_Object help, overlay;
7148 /* Check overlays first. */
7149 help = overlay = Qnil;
7150 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
7152 overlay = overlay_vec[i];
7153 help = Foverlay_get (overlay, Qhelp_echo);
7156 if (!NILP (help))
7158 help_echo = help;
7159 help_echo_window = window;
7160 help_echo_object = overlay;
7161 help_echo_pos = pos;
7163 else
7165 Lisp_Object object = glyph->object;
7166 int charpos = glyph->charpos;
7168 /* Try text properties. */
7169 if (STRINGP (object)
7170 && charpos >= 0
7171 && charpos < XSTRING (object)->size)
7173 help = Fget_text_property (make_number (charpos),
7174 Qhelp_echo, object);
7175 if (NILP (help))
7177 /* If the string itself doesn't specify a help-echo,
7178 see if the buffer text ``under'' it does. */
7179 struct glyph_row *r
7180 = MATRIX_ROW (w->current_matrix, vpos);
7181 int start = MATRIX_ROW_START_CHARPOS (r);
7182 int pos = string_buffer_position (w, object, start);
7183 if (pos > 0)
7185 help = Fget_text_property (make_number (pos),
7186 Qhelp_echo, w->buffer);
7187 if (!NILP (help))
7189 charpos = pos;
7190 object = w->buffer;
7195 else if (BUFFERP (object)
7196 && charpos >= BEGV
7197 && charpos < ZV)
7198 help = Fget_text_property (make_number (charpos), Qhelp_echo,
7199 object);
7201 if (!NILP (help))
7203 help_echo = help;
7204 help_echo_window = window;
7205 help_echo_object = object;
7206 help_echo_pos = charpos;
7211 BEGV = obegv;
7212 ZV = ozv;
7213 current_buffer = obuf;
7216 set_cursor:
7218 if (cursor != None)
7219 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7222 static void
7223 redo_mouse_highlight ()
7225 if (!NILP (last_mouse_motion_frame)
7226 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
7227 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
7228 last_mouse_motion_event.x,
7229 last_mouse_motion_event.y);
7234 /***********************************************************************
7235 Tool-bars
7236 ***********************************************************************/
7238 static int x_tool_bar_item P_ ((struct frame *, int, int,
7239 struct glyph **, int *, int *, int *));
7241 /* Tool-bar item index of the item on which a mouse button was pressed
7242 or -1. */
7244 static int last_tool_bar_item;
7247 /* Get information about the tool-bar item at position X/Y on frame F.
7248 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7249 the current matrix of the tool-bar window of F, or NULL if not
7250 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
7251 item in F->tool_bar_items. Value is
7253 -1 if X/Y is not on a tool-bar item
7254 0 if X/Y is on the same item that was highlighted before.
7255 1 otherwise. */
7257 static int
7258 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
7259 struct frame *f;
7260 int x, y;
7261 struct glyph **glyph;
7262 int *hpos, *vpos, *prop_idx;
7264 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7265 struct window *w = XWINDOW (f->tool_bar_window);
7266 int area;
7268 /* Find the glyph under X/Y. */
7269 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
7270 if (*glyph == NULL)
7271 return -1;
7273 /* Get the start of this tool-bar item's properties in
7274 f->tool_bar_items. */
7275 if (!tool_bar_item_info (f, *glyph, prop_idx))
7276 return -1;
7278 /* Is mouse on the highlighted item? */
7279 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
7280 && *vpos >= dpyinfo->mouse_face_beg_row
7281 && *vpos <= dpyinfo->mouse_face_end_row
7282 && (*vpos > dpyinfo->mouse_face_beg_row
7283 || *hpos >= dpyinfo->mouse_face_beg_col)
7284 && (*vpos < dpyinfo->mouse_face_end_row
7285 || *hpos < dpyinfo->mouse_face_end_col
7286 || dpyinfo->mouse_face_past_end))
7287 return 0;
7289 return 1;
7293 /* Handle mouse button event on the tool-bar of frame F, at
7294 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
7295 or ButtonRelase. */
7297 static void
7298 x_handle_tool_bar_click (f, button_event)
7299 struct frame *f;
7300 XButtonEvent *button_event;
7302 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7303 struct window *w = XWINDOW (f->tool_bar_window);
7304 int hpos, vpos, prop_idx;
7305 struct glyph *glyph;
7306 Lisp_Object enabled_p;
7307 int x = button_event->x;
7308 int y = button_event->y;
7310 /* If not on the highlighted tool-bar item, return. */
7311 frame_to_window_pixel_xy (w, &x, &y);
7312 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
7313 return;
7315 /* If item is disabled, do nothing. */
7316 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7317 if (NILP (enabled_p))
7318 return;
7320 if (button_event->type == ButtonPress)
7322 /* Show item in pressed state. */
7323 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7324 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
7325 last_tool_bar_item = prop_idx;
7327 else
7329 Lisp_Object key, frame;
7330 struct input_event event;
7332 /* Show item in released state. */
7333 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7334 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7336 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
7338 XSETFRAME (frame, f);
7339 event.kind = TOOL_BAR_EVENT;
7340 event.frame_or_window = frame;
7341 event.arg = frame;
7342 kbd_buffer_store_event (&event);
7344 event.kind = TOOL_BAR_EVENT;
7345 event.frame_or_window = frame;
7346 event.arg = key;
7347 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7348 button_event->state);
7349 kbd_buffer_store_event (&event);
7350 last_tool_bar_item = -1;
7355 /* Possibly highlight a tool-bar item on frame F when mouse moves to
7356 tool-bar window-relative coordinates X/Y. Called from
7357 note_mouse_highlight. */
7359 static void
7360 note_tool_bar_highlight (f, x, y)
7361 struct frame *f;
7362 int x, y;
7364 Lisp_Object window = f->tool_bar_window;
7365 struct window *w = XWINDOW (window);
7366 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7367 int hpos, vpos;
7368 struct glyph *glyph;
7369 struct glyph_row *row;
7370 int i;
7371 Lisp_Object enabled_p;
7372 int prop_idx;
7373 enum draw_glyphs_face draw;
7374 int mouse_down_p, rc;
7376 /* Function note_mouse_highlight is called with negative x(y
7377 values when mouse moves outside of the frame. */
7378 if (x <= 0 || y <= 0)
7380 clear_mouse_face (dpyinfo);
7381 return;
7384 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7385 if (rc < 0)
7387 /* Not on tool-bar item. */
7388 clear_mouse_face (dpyinfo);
7389 return;
7391 else if (rc == 0)
7392 goto set_help_echo;
7394 clear_mouse_face (dpyinfo);
7396 /* Mouse is down, but on different tool-bar item? */
7397 mouse_down_p = (dpyinfo->grabbed
7398 && f == last_mouse_frame
7399 && FRAME_LIVE_P (f));
7400 if (mouse_down_p
7401 && last_tool_bar_item != prop_idx)
7402 return;
7404 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7405 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7407 /* If tool-bar item is not enabled, don't highlight it. */
7408 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7409 if (!NILP (enabled_p))
7411 /* Compute the x-position of the glyph. In front and past the
7412 image is a space. We include this is the highlighted area. */
7413 row = MATRIX_ROW (w->current_matrix, vpos);
7414 for (i = x = 0; i < hpos; ++i)
7415 x += row->glyphs[TEXT_AREA][i].pixel_width;
7417 /* Record this as the current active region. */
7418 dpyinfo->mouse_face_beg_col = hpos;
7419 dpyinfo->mouse_face_beg_row = vpos;
7420 dpyinfo->mouse_face_beg_x = x;
7421 dpyinfo->mouse_face_beg_y = row->y;
7422 dpyinfo->mouse_face_past_end = 0;
7424 dpyinfo->mouse_face_end_col = hpos + 1;
7425 dpyinfo->mouse_face_end_row = vpos;
7426 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7427 dpyinfo->mouse_face_end_y = row->y;
7428 dpyinfo->mouse_face_window = window;
7429 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7431 /* Display it as active. */
7432 show_mouse_face (dpyinfo, draw);
7433 dpyinfo->mouse_face_image_state = draw;
7436 set_help_echo:
7438 /* Set help_echo to a help string.to display for this tool-bar item.
7439 XTread_socket does the rest. */
7440 help_echo_object = help_echo_window = Qnil;
7441 help_echo_pos = -1;
7442 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
7443 if (NILP (help_echo))
7444 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
7449 /* Find the glyph matrix position of buffer position POS in window W.
7450 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7451 current glyphs must be up to date. If POS is above window start
7452 return (0, 0, 0, 0). If POS is after end of W, return end of
7453 last line in W. */
7455 static int
7456 fast_find_position (w, pos, hpos, vpos, x, y)
7457 struct window *w;
7458 int pos;
7459 int *hpos, *vpos, *x, *y;
7461 int i;
7462 int lastcol;
7463 int maybe_next_line_p = 0;
7464 int line_start_position;
7465 int yb = window_text_bottom_y (w);
7466 struct glyph_row *row, *best_row;
7467 int row_vpos, best_row_vpos;
7468 int current_x;
7470 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7471 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7473 while (row->y < yb)
7475 if (row->used[TEXT_AREA])
7476 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7477 else
7478 line_start_position = 0;
7480 if (line_start_position > pos)
7481 break;
7482 /* If the position sought is the end of the buffer,
7483 don't include the blank lines at the bottom of the window. */
7484 else if (line_start_position == pos
7485 && pos == BUF_ZV (XBUFFER (w->buffer)))
7487 maybe_next_line_p = 1;
7488 break;
7490 else if (line_start_position > 0)
7492 best_row = row;
7493 best_row_vpos = row_vpos;
7496 if (row->y + row->height >= yb)
7497 break;
7499 ++row;
7500 ++row_vpos;
7503 /* Find the right column within BEST_ROW. */
7504 lastcol = 0;
7505 current_x = best_row->x;
7506 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
7508 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7509 int charpos;
7511 charpos = glyph->charpos;
7512 if (charpos == pos)
7514 *hpos = i;
7515 *vpos = best_row_vpos;
7516 *x = current_x;
7517 *y = best_row->y;
7518 return 1;
7520 else if (charpos > pos)
7521 break;
7522 else if (charpos > 0)
7523 lastcol = i;
7525 current_x += glyph->pixel_width;
7528 /* If we're looking for the end of the buffer,
7529 and we didn't find it in the line we scanned,
7530 use the start of the following line. */
7531 if (maybe_next_line_p)
7533 ++best_row;
7534 ++best_row_vpos;
7535 lastcol = 0;
7536 current_x = best_row->x;
7539 *vpos = best_row_vpos;
7540 *hpos = lastcol + 1;
7541 *x = current_x;
7542 *y = best_row->y;
7543 return 0;
7547 /* Find the position of the the glyph for position POS in OBJECT in
7548 window W's current matrix, and return in *X/*Y the pixel
7549 coordinates, and return in *HPOS/*VPOS the column/row of the glyph.
7551 RIGHT_P non-zero means return the position of the right edge of the
7552 glyph, RIGHT_P zero means return the left edge position.
7554 If no glyph for POS exists in the matrix, return the position of
7555 the glyph with the next smaller position that is in the matrix, if
7556 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7557 exists in the matrix, return the position of the glyph with the
7558 next larger position in OBJECT.
7560 Value is non-zero if a glyph was found. */
7562 static int
7563 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7564 struct window *w;
7565 int pos;
7566 Lisp_Object object;
7567 int *hpos, *vpos, *x, *y;
7568 int right_p;
7570 int yb = window_text_bottom_y (w);
7571 struct glyph_row *r;
7572 struct glyph *best_glyph = NULL;
7573 struct glyph_row *best_row = NULL;
7574 int best_x = 0;
7576 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7577 r->enabled_p && r->y < yb;
7578 ++r)
7580 struct glyph *g = r->glyphs[TEXT_AREA];
7581 struct glyph *e = g + r->used[TEXT_AREA];
7582 int gx;
7584 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7585 if (EQ (g->object, object))
7587 if (g->charpos == pos)
7589 best_glyph = g;
7590 best_x = gx;
7591 best_row = r;
7592 goto found;
7594 else if (best_glyph == NULL
7595 || ((abs (g->charpos - pos)
7596 < abs (best_glyph->charpos - pos))
7597 && (right_p
7598 ? g->charpos < pos
7599 : g->charpos > pos)))
7601 best_glyph = g;
7602 best_x = gx;
7603 best_row = r;
7608 found:
7610 if (best_glyph)
7612 *x = best_x;
7613 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7615 if (right_p)
7617 *x += best_glyph->pixel_width;
7618 ++*hpos;
7621 *y = best_row->y;
7622 *vpos = best_row - w->current_matrix->rows;
7625 return best_glyph != NULL;
7629 /* Display the active region described by mouse_face_*
7630 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7632 static void
7633 show_mouse_face (dpyinfo, draw)
7634 struct x_display_info *dpyinfo;
7635 enum draw_glyphs_face draw;
7637 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7638 struct frame *f = XFRAME (WINDOW_FRAME (w));
7639 int i;
7640 int cursor_off_p = 0;
7641 struct cursor_pos saved_cursor;
7643 saved_cursor = output_cursor;
7645 /* If window is in the process of being destroyed, don't bother
7646 to do anything. */
7647 if (w->current_matrix == NULL)
7648 goto set_x_cursor;
7650 /* Recognize when we are called to operate on rows that don't exist
7651 anymore. This can happen when a window is split. */
7652 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7653 goto set_x_cursor;
7655 set_output_cursor (&w->phys_cursor);
7657 /* Note that mouse_face_beg_row etc. are window relative. */
7658 for (i = dpyinfo->mouse_face_beg_row;
7659 i <= dpyinfo->mouse_face_end_row;
7660 i++)
7662 int start_hpos, end_hpos, start_x;
7663 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7665 /* Don't do anything if row doesn't have valid contents. */
7666 if (!row->enabled_p)
7667 continue;
7669 /* For all but the first row, the highlight starts at column 0. */
7670 if (i == dpyinfo->mouse_face_beg_row)
7672 start_hpos = dpyinfo->mouse_face_beg_col;
7673 start_x = dpyinfo->mouse_face_beg_x;
7675 else
7677 start_hpos = 0;
7678 start_x = 0;
7681 if (i == dpyinfo->mouse_face_end_row)
7682 end_hpos = dpyinfo->mouse_face_end_col;
7683 else
7684 end_hpos = row->used[TEXT_AREA];
7686 /* If the cursor's in the text we are about to rewrite, turn the
7687 cursor off. */
7688 if (!w->pseudo_window_p
7689 && i == output_cursor.vpos
7690 && output_cursor.hpos >= start_hpos - 1
7691 && output_cursor.hpos <= end_hpos)
7693 x_update_window_cursor (w, 0);
7694 cursor_off_p = 1;
7697 if (end_hpos > start_hpos)
7699 row->mouse_face_p = draw == DRAW_MOUSE_FACE || DRAW_IMAGE_RAISED;
7700 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7701 start_hpos, end_hpos, draw, NULL, NULL, 0);
7705 /* If we turned the cursor off, turn it back on. */
7706 if (cursor_off_p)
7707 x_display_cursor (w, 1,
7708 output_cursor.hpos, output_cursor.vpos,
7709 output_cursor.x, output_cursor.y);
7711 output_cursor = saved_cursor;
7713 set_x_cursor:
7715 /* Change the mouse cursor. */
7716 if (draw == DRAW_NORMAL_TEXT)
7717 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7718 f->output_data.x->text_cursor);
7719 else if (draw == DRAW_MOUSE_FACE)
7720 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7721 f->output_data.x->cross_cursor);
7722 else
7723 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7724 f->output_data.x->nontext_cursor);
7727 /* Clear out the mouse-highlighted active region.
7728 Redraw it un-highlighted first. Value is non-zero if mouse
7729 face was actually drawn unhighlighted. */
7731 static int
7732 clear_mouse_face (dpyinfo)
7733 struct x_display_info *dpyinfo;
7735 int cleared = 0;
7737 if (!NILP (dpyinfo->mouse_face_window))
7739 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7740 cleared = 1;
7743 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7744 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7745 dpyinfo->mouse_face_window = Qnil;
7746 dpyinfo->mouse_face_overlay = Qnil;
7747 return cleared;
7751 /* Clear any mouse-face on window W. This function is part of the
7752 redisplay interface, and is called from try_window_id and similar
7753 functions to ensure the mouse-highlight is off. */
7755 static void
7756 x_clear_mouse_face (w)
7757 struct window *w;
7759 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7760 Lisp_Object window;
7762 BLOCK_INPUT;
7763 XSETWINDOW (window, w);
7764 if (EQ (window, dpyinfo->mouse_face_window))
7765 clear_mouse_face (dpyinfo);
7766 UNBLOCK_INPUT;
7770 /* Just discard the mouse face information for frame F, if any.
7771 This is used when the size of F is changed. */
7773 void
7774 cancel_mouse_face (f)
7775 FRAME_PTR f;
7777 Lisp_Object window;
7778 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7780 window = dpyinfo->mouse_face_window;
7781 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7783 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7784 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7785 dpyinfo->mouse_face_window = Qnil;
7790 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
7793 /* Try to determine frame pixel position and size of the glyph under
7794 frame pixel coordinates X/Y on frame F . Return the position and
7795 size in *RECT. Value is non-zero if we could compute these
7796 values. */
7798 static int
7799 glyph_rect (f, x, y, rect)
7800 struct frame *f;
7801 int x, y;
7802 XRectangle *rect;
7804 Lisp_Object window;
7805 int part, found = 0;
7807 window = window_from_coordinates (f, x, y, &part, 0);
7808 if (!NILP (window))
7810 struct window *w = XWINDOW (window);
7811 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7812 struct glyph_row *end = r + w->current_matrix->nrows - 1;
7813 int area;
7815 frame_to_window_pixel_xy (w, &x, &y);
7817 for (; !found && r < end && r->enabled_p; ++r)
7818 if (r->y >= y)
7820 struct glyph *g = r->glyphs[TEXT_AREA];
7821 struct glyph *end = g + r->used[TEXT_AREA];
7822 int gx;
7824 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
7825 if (gx >= x)
7827 rect->width = g->pixel_width;
7828 rect->height = r->height;
7829 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
7830 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
7831 found = 1;
7836 return found;
7840 /* Return the current position of the mouse.
7841 *FP should be a frame which indicates which display to ask about.
7843 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
7844 and *PART to the frame, window, and scroll bar part that the mouse
7845 is over. Set *X and *Y to the portion and whole of the mouse's
7846 position on the scroll bar.
7848 If the mouse movement started elsewhere, set *FP to the frame the
7849 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
7850 the mouse is over.
7852 Set *TIME to the server time-stamp for the time at which the mouse
7853 was at this position.
7855 Don't store anything if we don't have a valid set of values to report.
7857 This clears the mouse_moved flag, so we can wait for the next mouse
7858 movement. */
7860 static void
7861 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7862 FRAME_PTR *fp;
7863 int insist;
7864 Lisp_Object *bar_window;
7865 enum scroll_bar_part *part;
7866 Lisp_Object *x, *y;
7867 unsigned long *time;
7869 FRAME_PTR f1;
7871 BLOCK_INPUT;
7873 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7874 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7875 else
7877 Window root;
7878 int root_x, root_y;
7880 Window dummy_window;
7881 int dummy;
7883 Lisp_Object frame, tail;
7885 /* Clear the mouse-moved flag for every frame on this display. */
7886 FOR_EACH_FRAME (tail, frame)
7887 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7888 XFRAME (frame)->mouse_moved = 0;
7890 last_mouse_scroll_bar = Qnil;
7892 /* Figure out which root window we're on. */
7893 XQueryPointer (FRAME_X_DISPLAY (*fp),
7894 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
7896 /* The root window which contains the pointer. */
7897 &root,
7899 /* Trash which we can't trust if the pointer is on
7900 a different screen. */
7901 &dummy_window,
7903 /* The position on that root window. */
7904 &root_x, &root_y,
7906 /* More trash we can't trust. */
7907 &dummy, &dummy,
7909 /* Modifier keys and pointer buttons, about which
7910 we don't care. */
7911 (unsigned int *) &dummy);
7913 /* Now we have a position on the root; find the innermost window
7914 containing the pointer. */
7916 Window win, child;
7917 int win_x, win_y;
7918 int parent_x = 0, parent_y = 0;
7919 int count;
7921 win = root;
7923 /* XTranslateCoordinates can get errors if the window
7924 structure is changing at the same time this function
7925 is running. So at least we must not crash from them. */
7927 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
7929 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
7930 && FRAME_LIVE_P (last_mouse_frame))
7932 /* If mouse was grabbed on a frame, give coords for that frame
7933 even if the mouse is now outside it. */
7934 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7936 /* From-window, to-window. */
7937 root, FRAME_X_WINDOW (last_mouse_frame),
7939 /* From-position, to-position. */
7940 root_x, root_y, &win_x, &win_y,
7942 /* Child of win. */
7943 &child);
7944 f1 = last_mouse_frame;
7946 else
7948 while (1)
7950 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7952 /* From-window, to-window. */
7953 root, win,
7955 /* From-position, to-position. */
7956 root_x, root_y, &win_x, &win_y,
7958 /* Child of win. */
7959 &child);
7961 if (child == None || child == win)
7962 break;
7964 win = child;
7965 parent_x = win_x;
7966 parent_y = win_y;
7969 /* Now we know that:
7970 win is the innermost window containing the pointer
7971 (XTC says it has no child containing the pointer),
7972 win_x and win_y are the pointer's position in it
7973 (XTC did this the last time through), and
7974 parent_x and parent_y are the pointer's position in win's parent.
7975 (They are what win_x and win_y were when win was child.
7976 If win is the root window, it has no parent, and
7977 parent_{x,y} are invalid, but that's okay, because we'll
7978 never use them in that case.) */
7980 /* Is win one of our frames? */
7981 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
7983 #ifdef USE_X_TOOLKIT
7984 /* If we end up with the menu bar window, say it's not
7985 on the frame. */
7986 if (f1 != NULL
7987 && f1->output_data.x->menubar_widget
7988 && win == XtWindow (f1->output_data.x->menubar_widget))
7989 f1 = NULL;
7990 #endif /* USE_X_TOOLKIT */
7993 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7994 f1 = 0;
7996 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
7998 /* If not, is it one of our scroll bars? */
7999 if (! f1)
8001 struct scroll_bar *bar = x_window_to_scroll_bar (win);
8003 if (bar)
8005 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8006 win_x = parent_x;
8007 win_y = parent_y;
8011 if (f1 == 0 && insist > 0)
8012 f1 = SELECTED_FRAME ();
8014 if (f1)
8016 /* Ok, we found a frame. Store all the values.
8017 last_mouse_glyph is a rectangle used to reduce the
8018 generation of mouse events. To not miss any motion
8019 events, we must divide the frame into rectangles of the
8020 size of the smallest character that could be displayed
8021 on it, i.e. into the same rectangles that matrices on
8022 the frame are divided into. */
8024 int width, height, gx, gy;
8025 XRectangle rect;
8027 if (glyph_rect (f1, win_x, win_y, &rect))
8028 last_mouse_glyph = rect;
8029 else
8031 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
8032 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
8033 gx = win_x;
8034 gy = win_y;
8036 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
8037 round down even for negative values. */
8038 if (gx < 0)
8039 gx -= width - 1;
8040 if (gy < 0)
8041 gy -= height - 1;
8042 gx = (gx + width - 1) / width * width;
8043 gy = (gy + height - 1) / height * height;
8045 last_mouse_glyph.width = width;
8046 last_mouse_glyph.height = height;
8047 last_mouse_glyph.x = gx;
8048 last_mouse_glyph.y = gy;
8051 *bar_window = Qnil;
8052 *part = 0;
8053 *fp = f1;
8054 XSETINT (*x, win_x);
8055 XSETINT (*y, win_y);
8056 *time = last_mouse_movement_time;
8061 UNBLOCK_INPUT;
8065 #ifdef USE_X_TOOLKIT
8067 /* Atimer callback function for TIMER. Called every 0.1s to process
8068 Xt timeouts, if needed. We must avoid calling XtAppPending as
8069 much as possible because that function does an implicit XFlush
8070 that slows us down. */
8072 static void
8073 x_process_timeouts (timer)
8074 struct atimer *timer;
8076 if (toolkit_scroll_bar_interaction || popup_activated_flag)
8078 BLOCK_INPUT;
8079 while (XtAppPending (Xt_app_con) & XtIMTimer)
8080 XtAppProcessEvent (Xt_app_con, XtIMTimer);
8081 UNBLOCK_INPUT;
8085 #endif /* USE_X_TOOLKIT */
8088 /* Scroll bar support. */
8090 /* Given an X window ID, find the struct scroll_bar which manages it.
8091 This can be called in GC, so we have to make sure to strip off mark
8092 bits. */
8094 static struct scroll_bar *
8095 x_window_to_scroll_bar (window_id)
8096 Window window_id;
8098 Lisp_Object tail;
8100 for (tail = Vframe_list;
8101 XGCTYPE (tail) == Lisp_Cons;
8102 tail = XCDR (tail))
8104 Lisp_Object frame, bar, condemned;
8106 frame = XCAR (tail);
8107 /* All elements of Vframe_list should be frames. */
8108 if (! GC_FRAMEP (frame))
8109 abort ();
8111 /* Scan this frame's scroll bar list for a scroll bar with the
8112 right window ID. */
8113 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
8114 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
8115 /* This trick allows us to search both the ordinary and
8116 condemned scroll bar lists with one loop. */
8117 ! GC_NILP (bar) || (bar = condemned,
8118 condemned = Qnil,
8119 ! GC_NILP (bar));
8120 bar = XSCROLL_BAR (bar)->next)
8121 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
8122 return XSCROLL_BAR (bar);
8125 return 0;
8129 #if defined USE_X_TOOLKIT && defined USE_LUCID
8131 /* Return the Lucid menu bar WINDOW is part of. Return null
8132 if WINDOW is not part of a menu bar. */
8134 static Widget
8135 x_window_to_menu_bar (window)
8136 Window window;
8138 Lisp_Object tail;
8140 for (tail = Vframe_list;
8141 XGCTYPE (tail) == Lisp_Cons;
8142 tail = XCDR (tail))
8144 Lisp_Object frame = XCAR (tail);
8145 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
8147 if (menu_bar && xlwmenu_window_p (menu_bar, window))
8148 return menu_bar;
8151 return NULL;
8154 #endif /* USE_X_TOOLKIT && USE_LUCID */
8157 /************************************************************************
8158 Toolkit scroll bars
8159 ************************************************************************/
8161 #ifdef USE_TOOLKIT_SCROLL_BARS
8163 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
8164 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
8165 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
8166 struct scroll_bar *));
8167 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
8168 int, int, int));
8171 /* Id of action hook installed for scroll bars. */
8173 static XtActionHookId action_hook_id;
8175 /* Lisp window being scrolled. Set when starting to interact with
8176 a toolkit scroll bar, reset to nil when ending the interaction. */
8178 static Lisp_Object window_being_scrolled;
8180 /* Last scroll bar part sent in xm_scroll_callback. */
8182 static int last_scroll_bar_part;
8184 /* Whether this is an Xaw with arrow-scrollbars. This should imply
8185 that movements of 1/20 of the screen size are mapped to up/down. */
8187 static Boolean xaw3d_arrow_scroll;
8189 /* Whether the drag scrolling maintains the mouse at the top of the
8190 thumb. If not, resizing the thumb needs to be done more carefully
8191 to avoid jerkyness. */
8193 static Boolean xaw3d_pick_top;
8196 /* Action hook installed via XtAppAddActionHook when toolkit scroll
8197 bars are used.. The hook is responsible for detecting when
8198 the user ends an interaction with the scroll bar, and generates
8199 a `end-scroll' scroll_bar_click' event if so. */
8201 static void
8202 xt_action_hook (widget, client_data, action_name, event, params,
8203 num_params)
8204 Widget widget;
8205 XtPointer client_data;
8206 String action_name;
8207 XEvent *event;
8208 String *params;
8209 Cardinal *num_params;
8211 int scroll_bar_p;
8212 char *end_action;
8214 #ifdef USE_MOTIF
8215 scroll_bar_p = XmIsScrollBar (widget);
8216 end_action = "Release";
8217 #else /* !USE_MOTIF i.e. use Xaw */
8218 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
8219 end_action = "EndScroll";
8220 #endif /* USE_MOTIF */
8222 if (scroll_bar_p
8223 && strcmp (action_name, end_action) == 0
8224 && WINDOWP (window_being_scrolled))
8226 struct window *w;
8228 x_send_scroll_bar_event (window_being_scrolled,
8229 scroll_bar_end_scroll, 0, 0);
8230 w = XWINDOW (window_being_scrolled);
8231 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
8232 window_being_scrolled = Qnil;
8233 last_scroll_bar_part = -1;
8235 /* Xt timeouts no longer needed. */
8236 toolkit_scroll_bar_interaction = 0;
8240 /* A vector of windows used for communication between
8241 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
8243 static struct window **scroll_bar_windows;
8244 static int scroll_bar_windows_size;
8247 /* Send a client message with message type Xatom_Scrollbar for a
8248 scroll action to the frame of WINDOW. PART is a value identifying
8249 the part of the scroll bar that was clicked on. PORTION is the
8250 amount to scroll of a whole of WHOLE. */
8252 static void
8253 x_send_scroll_bar_event (window, part, portion, whole)
8254 Lisp_Object window;
8255 int part, portion, whole;
8257 XEvent event;
8258 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
8259 struct window *w = XWINDOW (window);
8260 struct frame *f = XFRAME (w->frame);
8261 int i;
8263 BLOCK_INPUT;
8265 /* Construct a ClientMessage event to send to the frame. */
8266 ev->type = ClientMessage;
8267 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
8268 ev->display = FRAME_X_DISPLAY (f);
8269 ev->window = FRAME_X_WINDOW (f);
8270 ev->format = 32;
8272 /* We can only transfer 32 bits in the XClientMessageEvent, which is
8273 not enough to store a pointer or Lisp_Object on a 64 bit system.
8274 So, store the window in scroll_bar_windows and pass the index
8275 into that array in the event. */
8276 for (i = 0; i < scroll_bar_windows_size; ++i)
8277 if (scroll_bar_windows[i] == NULL)
8278 break;
8280 if (i == scroll_bar_windows_size)
8282 int new_size = max (10, 2 * scroll_bar_windows_size);
8283 size_t nbytes = new_size * sizeof *scroll_bar_windows;
8284 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
8286 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
8287 nbytes);
8288 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
8289 scroll_bar_windows_size = new_size;
8292 scroll_bar_windows[i] = w;
8293 ev->data.l[0] = (long) i;
8294 ev->data.l[1] = (long) part;
8295 ev->data.l[2] = (long) 0;
8296 ev->data.l[3] = (long) portion;
8297 ev->data.l[4] = (long) whole;
8299 /* Make Xt timeouts work while the scroll bar is active. */
8300 toolkit_scroll_bar_interaction = 1;
8302 /* Setting the event mask to zero means that the message will
8303 be sent to the client that created the window, and if that
8304 window no longer exists, no event will be sent. */
8305 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
8306 UNBLOCK_INPUT;
8310 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
8311 in *IEVENT. */
8313 static void
8314 x_scroll_bar_to_input_event (event, ievent)
8315 XEvent *event;
8316 struct input_event *ievent;
8318 XClientMessageEvent *ev = (XClientMessageEvent *) event;
8319 Lisp_Object window;
8320 struct frame *f;
8321 struct window *w;
8323 w = scroll_bar_windows[ev->data.l[0]];
8324 scroll_bar_windows[ev->data.l[0]] = NULL;
8326 XSETWINDOW (window, w);
8327 f = XFRAME (w->frame);
8329 ievent->kind = scroll_bar_click;
8330 ievent->frame_or_window = window;
8331 ievent->arg = Qnil;
8332 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
8333 ievent->part = ev->data.l[1];
8334 ievent->code = ev->data.l[2];
8335 ievent->x = make_number ((int) ev->data.l[3]);
8336 ievent->y = make_number ((int) ev->data.l[4]);
8337 ievent->modifiers = 0;
8341 #ifdef USE_MOTIF
8343 /* Minimum and maximum values used for Motif scroll bars. */
8345 #define XM_SB_MIN 1
8346 #define XM_SB_MAX 10000000
8347 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
8350 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
8351 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
8352 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
8354 static void
8355 xm_scroll_callback (widget, client_data, call_data)
8356 Widget widget;
8357 XtPointer client_data, call_data;
8359 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8360 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
8361 double percent;
8362 int part = -1, whole = 0, portion = 0;
8364 switch (cs->reason)
8366 case XmCR_DECREMENT:
8367 bar->dragging = Qnil;
8368 part = scroll_bar_up_arrow;
8369 break;
8371 case XmCR_INCREMENT:
8372 bar->dragging = Qnil;
8373 part = scroll_bar_down_arrow;
8374 break;
8376 case XmCR_PAGE_DECREMENT:
8377 bar->dragging = Qnil;
8378 part = scroll_bar_above_handle;
8379 break;
8381 case XmCR_PAGE_INCREMENT:
8382 bar->dragging = Qnil;
8383 part = scroll_bar_below_handle;
8384 break;
8386 case XmCR_TO_TOP:
8387 bar->dragging = Qnil;
8388 part = scroll_bar_to_top;
8389 break;
8391 case XmCR_TO_BOTTOM:
8392 bar->dragging = Qnil;
8393 part = scroll_bar_to_bottom;
8394 break;
8396 case XmCR_DRAG:
8398 int slider_size;
8399 int dragging_down_p = (INTEGERP (bar->dragging)
8400 && XINT (bar->dragging) <= cs->value);
8402 /* Get the slider size. */
8403 BLOCK_INPUT;
8404 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
8405 UNBLOCK_INPUT;
8407 /* At the max position of the scroll bar, do a line-wise
8408 movement. Without doing anything, we would be called with
8409 the same cs->value again and again. If we want to make
8410 sure that we can reach the end of the buffer, we have to do
8411 something.
8413 Implementation note: setting bar->dragging always to
8414 cs->value gives a smoother movement at the max position.
8415 Setting it to nil when doing line-wise movement gives
8416 a better slider behavior. */
8418 if (cs->value + slider_size == XM_SB_MAX
8419 || (dragging_down_p
8420 && last_scroll_bar_part == scroll_bar_down_arrow))
8422 part = scroll_bar_down_arrow;
8423 bar->dragging = Qnil;
8425 else
8427 whole = XM_SB_RANGE;
8428 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
8429 part = scroll_bar_handle;
8430 bar->dragging = make_number (cs->value);
8433 break;
8435 case XmCR_VALUE_CHANGED:
8436 break;
8439 if (part >= 0)
8441 window_being_scrolled = bar->window;
8442 last_scroll_bar_part = part;
8443 x_send_scroll_bar_event (bar->window, part, portion, whole);
8448 #else /* !USE_MOTIF, i.e. Xaw. */
8451 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
8452 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8453 scroll bar struct. CALL_DATA is a pointer to a float saying where
8454 the thumb is. */
8456 static void
8457 xaw_jump_callback (widget, client_data, call_data)
8458 Widget widget;
8459 XtPointer client_data, call_data;
8461 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8462 float top = *(float *) call_data;
8463 float shown;
8464 int whole, portion, height;
8465 int part;
8467 /* Get the size of the thumb, a value between 0 and 1. */
8468 BLOCK_INPUT;
8469 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
8470 UNBLOCK_INPUT;
8472 whole = 10000000;
8473 portion = shown < 1 ? top * whole : 0;
8475 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8476 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8477 the bottom, so we force the scrolling whenever we see that we're
8478 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8479 we try to ensure that we always stay two pixels away from the
8480 bottom). */
8481 part = scroll_bar_down_arrow;
8482 else
8483 part = scroll_bar_handle;
8485 window_being_scrolled = bar->window;
8486 bar->dragging = make_number (portion);
8487 last_scroll_bar_part = part;
8488 x_send_scroll_bar_event (bar->window, part, portion, whole);
8492 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
8493 i.e. line or page up or down. WIDGET is the Xaw scroll bar
8494 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8495 the scroll bar. CALL_DATA is an integer specifying the action that
8496 has taken place. It's magnitude is in the range 0..height of the
8497 scroll bar. Negative values mean scroll towards buffer start.
8498 Values < height of scroll bar mean line-wise movement. */
8500 static void
8501 xaw_scroll_callback (widget, client_data, call_data)
8502 Widget widget;
8503 XtPointer client_data, call_data;
8505 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8506 int position = (int) call_data;
8507 Dimension height;
8508 int part;
8510 /* Get the height of the scroll bar. */
8511 BLOCK_INPUT;
8512 XtVaGetValues (widget, XtNheight, &height, NULL);
8513 UNBLOCK_INPUT;
8515 if (abs (position) >= height)
8516 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8518 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8519 it maps line-movement to call_data = max(5, height/20). */
8520 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8521 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
8522 else
8523 part = scroll_bar_move_ratio;
8525 window_being_scrolled = bar->window;
8526 bar->dragging = Qnil;
8527 last_scroll_bar_part = part;
8528 x_send_scroll_bar_event (bar->window, part, position, height);
8532 #endif /* not USE_MOTIF */
8535 /* Create the widget for scroll bar BAR on frame F. Record the widget
8536 and X window of the scroll bar in BAR. */
8538 static void
8539 x_create_toolkit_scroll_bar (f, bar)
8540 struct frame *f;
8541 struct scroll_bar *bar;
8543 Window xwindow;
8544 Widget widget;
8545 Arg av[20];
8546 int ac = 0;
8547 char *scroll_bar_name = "verticalScrollBar";
8548 unsigned long pixel;
8550 BLOCK_INPUT;
8552 #ifdef USE_MOTIF
8553 /* Set resources. Create the widget. */
8554 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8555 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8556 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8557 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8558 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8559 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8560 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8562 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8563 if (pixel != -1)
8565 XtSetArg (av[ac], XmNforeground, pixel);
8566 ++ac;
8569 pixel = f->output_data.x->scroll_bar_background_pixel;
8570 if (pixel != -1)
8572 XtSetArg (av[ac], XmNbackground, pixel);
8573 ++ac;
8576 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8577 scroll_bar_name, av, ac);
8579 /* Add one callback for everything that can happen. */
8580 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8581 (XtPointer) bar);
8582 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8583 (XtPointer) bar);
8584 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8585 (XtPointer) bar);
8586 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8587 (XtPointer) bar);
8588 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8589 (XtPointer) bar);
8590 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8591 (XtPointer) bar);
8592 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8593 (XtPointer) bar);
8595 /* Realize the widget. Only after that is the X window created. */
8596 XtRealizeWidget (widget);
8598 /* Set the cursor to an arrow. I didn't find a resource to do that.
8599 And I'm wondering why it hasn't an arrow cursor by default. */
8600 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8601 f->output_data.x->nontext_cursor);
8603 #else /* !USE_MOTIF i.e. use Xaw */
8605 /* Set resources. Create the widget. The background of the
8606 Xaw3d scroll bar widget is a little bit light for my taste.
8607 We don't alter it here to let users change it according
8608 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8609 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8610 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
8611 /* For smoother scrolling with Xaw3d -sm */
8612 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8613 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
8615 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8616 if (pixel != -1)
8618 XtSetArg (av[ac], XtNforeground, pixel);
8619 ++ac;
8622 pixel = f->output_data.x->scroll_bar_background_pixel;
8623 if (pixel != -1)
8625 XtSetArg (av[ac], XtNbackground, pixel);
8626 ++ac;
8629 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8630 f->output_data.x->edit_widget, av, ac);
8633 char *initial = "";
8634 char *val = initial;
8635 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8636 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8637 if (val == initial)
8638 { /* ARROW_SCROLL */
8639 xaw3d_arrow_scroll = True;
8640 /* Isn't that just a personal preference ? -sm */
8641 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8645 /* Define callbacks. */
8646 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8647 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
8648 (XtPointer) bar);
8650 /* Realize the widget. Only after that is the X window created. */
8651 XtRealizeWidget (widget);
8653 #endif /* !USE_MOTIF */
8655 /* Install an action hook that let's us detect when the user
8656 finishes interacting with a scroll bar. */
8657 if (action_hook_id == 0)
8658 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8660 /* Remember X window and widget in the scroll bar vector. */
8661 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8662 xwindow = XtWindow (widget);
8663 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8665 UNBLOCK_INPUT;
8669 /* Set the thumb size and position of scroll bar BAR. We are currently
8670 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8672 static void
8673 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8674 struct scroll_bar *bar;
8675 int portion, position, whole;
8677 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8678 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8679 float top, shown;
8681 if (whole == 0)
8682 top = 0, shown = 1;
8683 else
8685 top = (float) position / whole;
8686 shown = (float) portion / whole;
8689 BLOCK_INPUT;
8691 #ifdef USE_MOTIF
8693 int size, value;
8694 XmScrollBarWidget sb;
8696 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
8697 is the scroll bar's maximum and MIN is the scroll bar's minimum
8698 value. */
8699 size = shown * XM_SB_RANGE;
8700 size = min (size, XM_SB_RANGE);
8701 size = max (size, 1);
8703 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8704 value = top * XM_SB_RANGE;
8705 value = min (value, XM_SB_MAX - size);
8706 value = max (value, XM_SB_MIN);
8708 if (NILP (bar->dragging))
8709 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8710 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8711 /* This has the negative side effect that the slider value is
8712 not what it would be if we scrolled here using line-wise or
8713 page-wise movement. */
8714 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8715 else
8717 /* If currently dragging, only update the slider size.
8718 This reduces flicker effects. */
8719 int old_value, old_size, increment, page_increment;
8721 XmScrollBarGetValues (widget, &old_value, &old_size,
8722 &increment, &page_increment);
8723 XmScrollBarSetValues (widget, old_value,
8724 min (size, XM_SB_RANGE - old_value),
8725 0, 0, False);
8728 #else /* !USE_MOTIF i.e. use Xaw */
8730 float old_top, old_shown;
8731 Dimension height;
8732 XtVaGetValues (widget,
8733 XtNtopOfThumb, &old_top,
8734 XtNshown, &old_shown,
8735 XtNheight, &height,
8736 NULL);
8738 /* Massage the top+shown values. */
8739 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8740 top = max (0, min (1, top));
8741 else
8742 top = old_top;
8743 /* Keep two pixels available for moving the thumb down. */
8744 shown = max (0, min (1 - top - (2.0 / height), shown));
8746 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8747 check that your system's configuration file contains a define
8748 for `NARROWPROTO'. See s/freebsd.h for an example. */
8749 if (top != old_top || shown != old_shown)
8751 if (NILP (bar->dragging))
8752 XawScrollbarSetThumb (widget, top, shown);
8753 else
8755 #ifdef HAVE_XAW3D
8756 ScrollbarWidget sb = (ScrollbarWidget) widget;
8757 int scroll_mode = 0;
8759 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8760 if (xaw3d_arrow_scroll)
8762 /* Xaw3d stupidly ignores resize requests while dragging
8763 so we have to make it believe it's not in dragging mode. */
8764 scroll_mode = sb->scrollbar.scroll_mode;
8765 if (scroll_mode == 2)
8766 sb->scrollbar.scroll_mode = 0;
8768 #endif
8769 /* Try to make the scrolling a tad smoother. */
8770 if (!xaw3d_pick_top)
8771 shown = min (shown, old_shown);
8773 XawScrollbarSetThumb (widget, top, shown);
8775 #ifdef HAVE_XAW3D
8776 if (xaw3d_arrow_scroll && scroll_mode == 2)
8777 sb->scrollbar.scroll_mode = scroll_mode;
8778 #endif
8782 #endif /* !USE_MOTIF */
8784 UNBLOCK_INPUT;
8787 #endif /* USE_TOOLKIT_SCROLL_BARS */
8791 /************************************************************************
8792 Scroll bars, general
8793 ************************************************************************/
8795 /* Create a scroll bar and return the scroll bar vector for it. W is
8796 the Emacs window on which to create the scroll bar. TOP, LEFT,
8797 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8798 scroll bar. */
8800 static struct scroll_bar *
8801 x_scroll_bar_create (w, top, left, width, height)
8802 struct window *w;
8803 int top, left, width, height;
8805 struct frame *f = XFRAME (w->frame);
8806 struct scroll_bar *bar
8807 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8809 BLOCK_INPUT;
8811 #ifdef USE_TOOLKIT_SCROLL_BARS
8812 x_create_toolkit_scroll_bar (f, bar);
8813 #else /* not USE_TOOLKIT_SCROLL_BARS */
8815 XSetWindowAttributes a;
8816 unsigned long mask;
8817 Window window;
8819 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8820 if (a.background_pixel == -1)
8821 a.background_pixel = f->output_data.x->background_pixel;
8823 a.event_mask = (ButtonPressMask | ButtonReleaseMask
8824 | ButtonMotionMask | PointerMotionHintMask
8825 | ExposureMask);
8826 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
8828 mask = (CWBackPixel | CWEventMask | CWCursor);
8830 /* Clear the area of W that will serve as a scroll bar. This is
8831 for the case that a window has been split horizontally. In
8832 this case, no clear_frame is generated to reduce flickering. */
8833 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8834 left, top, width,
8835 window_box_height (w), False);
8837 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8838 /* Position and size of scroll bar. */
8839 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8840 top,
8841 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8842 height,
8843 /* Border width, depth, class, and visual. */
8845 CopyFromParent,
8846 CopyFromParent,
8847 CopyFromParent,
8848 /* Attributes. */
8849 mask, &a);
8850 SET_SCROLL_BAR_X_WINDOW (bar, window);
8852 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8854 XSETWINDOW (bar->window, w);
8855 XSETINT (bar->top, top);
8856 XSETINT (bar->left, left);
8857 XSETINT (bar->width, width);
8858 XSETINT (bar->height, height);
8859 XSETINT (bar->start, 0);
8860 XSETINT (bar->end, 0);
8861 bar->dragging = Qnil;
8863 /* Add bar to its frame's list of scroll bars. */
8864 bar->next = FRAME_SCROLL_BARS (f);
8865 bar->prev = Qnil;
8866 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8867 if (!NILP (bar->next))
8868 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8870 /* Map the window/widget. */
8871 #ifdef USE_TOOLKIT_SCROLL_BARS
8873 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8874 XtConfigureWidget (scroll_bar,
8875 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8876 top,
8877 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8878 max (height, 1), 0);
8879 XtMapWidget (scroll_bar);
8881 #else /* not USE_TOOLKIT_SCROLL_BARS */
8882 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8883 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8885 UNBLOCK_INPUT;
8886 return bar;
8890 /* Draw BAR's handle in the proper position.
8892 If the handle is already drawn from START to END, don't bother
8893 redrawing it, unless REBUILD is non-zero; in that case, always
8894 redraw it. (REBUILD is handy for drawing the handle after expose
8895 events.)
8897 Normally, we want to constrain the start and end of the handle to
8898 fit inside its rectangle, but if the user is dragging the scroll
8899 bar handle, we want to let them drag it down all the way, so that
8900 the bar's top is as far down as it goes; otherwise, there's no way
8901 to move to the very end of the buffer. */
8903 #ifndef USE_TOOLKIT_SCROLL_BARS
8905 static void
8906 x_scroll_bar_set_handle (bar, start, end, rebuild)
8907 struct scroll_bar *bar;
8908 int start, end;
8909 int rebuild;
8911 int dragging = ! NILP (bar->dragging);
8912 Window w = SCROLL_BAR_X_WINDOW (bar);
8913 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8914 GC gc = f->output_data.x->normal_gc;
8916 /* If the display is already accurate, do nothing. */
8917 if (! rebuild
8918 && start == XINT (bar->start)
8919 && end == XINT (bar->end))
8920 return;
8922 BLOCK_INPUT;
8925 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8926 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8927 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8929 /* Make sure the values are reasonable, and try to preserve
8930 the distance between start and end. */
8932 int length = end - start;
8934 if (start < 0)
8935 start = 0;
8936 else if (start > top_range)
8937 start = top_range;
8938 end = start + length;
8940 if (end < start)
8941 end = start;
8942 else if (end > top_range && ! dragging)
8943 end = top_range;
8946 /* Store the adjusted setting in the scroll bar. */
8947 XSETINT (bar->start, start);
8948 XSETINT (bar->end, end);
8950 /* Clip the end position, just for display. */
8951 if (end > top_range)
8952 end = top_range;
8954 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
8955 below top positions, to make sure the handle is always at least
8956 that many pixels tall. */
8957 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
8959 /* Draw the empty space above the handle. Note that we can't clear
8960 zero-height areas; that means "clear to end of window." */
8961 if (0 < start)
8962 x_clear_area (FRAME_X_DISPLAY (f), w,
8963 /* x, y, width, height, and exposures. */
8964 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8965 VERTICAL_SCROLL_BAR_TOP_BORDER,
8966 inside_width, start,
8967 False);
8969 /* Change to proper foreground color if one is specified. */
8970 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8971 XSetForeground (FRAME_X_DISPLAY (f), gc,
8972 f->output_data.x->scroll_bar_foreground_pixel);
8974 /* Draw the handle itself. */
8975 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
8976 /* x, y, width, height */
8977 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8978 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
8979 inside_width, end - start);
8981 /* Restore the foreground color of the GC if we changed it above. */
8982 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8983 XSetForeground (FRAME_X_DISPLAY (f), gc,
8984 f->output_data.x->foreground_pixel);
8986 /* Draw the empty space below the handle. Note that we can't
8987 clear zero-height areas; that means "clear to end of window." */
8988 if (end < inside_height)
8989 x_clear_area (FRAME_X_DISPLAY (f), w,
8990 /* x, y, width, height, and exposures. */
8991 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8992 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
8993 inside_width, inside_height - end,
8994 False);
8998 UNBLOCK_INPUT;
9001 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9003 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
9004 nil. */
9006 static void
9007 x_scroll_bar_remove (bar)
9008 struct scroll_bar *bar;
9010 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9011 BLOCK_INPUT;
9013 #ifdef USE_TOOLKIT_SCROLL_BARS
9014 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
9015 #else
9016 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9017 #endif
9019 /* Disassociate this scroll bar from its window. */
9020 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
9022 UNBLOCK_INPUT;
9026 /* Set the handle of the vertical scroll bar for WINDOW to indicate
9027 that we are displaying PORTION characters out of a total of WHOLE
9028 characters, starting at POSITION. If WINDOW has no scroll bar,
9029 create one. */
9031 static void
9032 XTset_vertical_scroll_bar (w, portion, whole, position)
9033 struct window *w;
9034 int portion, whole, position;
9036 struct frame *f = XFRAME (w->frame);
9037 struct scroll_bar *bar;
9038 int top, height, left, sb_left, width, sb_width;
9039 int window_x, window_y, window_width, window_height;
9041 /* Get window dimensions. */
9042 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
9043 top = window_y;
9044 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9045 height = window_height;
9047 /* Compute the left edge of the scroll bar area. */
9048 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9049 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
9050 else
9051 left = XFASTINT (w->left);
9052 left *= CANON_X_UNIT (f);
9053 left += FRAME_INTERNAL_BORDER_WIDTH (f);
9055 /* Compute the width of the scroll bar which might be less than
9056 the width of the area reserved for the scroll bar. */
9057 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
9058 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
9059 else
9060 sb_width = width;
9062 /* Compute the left edge of the scroll bar. */
9063 #ifdef USE_TOOLKIT_SCROLL_BARS
9064 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9065 sb_left = left + width - sb_width - (width - sb_width) / 2;
9066 else
9067 sb_left = left + (width - sb_width) / 2;
9068 #else
9069 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9070 sb_left = left + width - sb_width;
9071 else
9072 sb_left = left;
9073 #endif
9075 /* Does the scroll bar exist yet? */
9076 if (NILP (w->vertical_scroll_bar))
9078 BLOCK_INPUT;
9079 if (width && height)
9080 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9081 left, top, width, height, False);
9082 UNBLOCK_INPUT;
9083 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
9085 else
9087 /* It may just need to be moved and resized. */
9088 unsigned int mask = 0;
9090 bar = XSCROLL_BAR (w->vertical_scroll_bar);
9092 BLOCK_INPUT;
9094 if (sb_left != XINT (bar->left))
9095 mask |= CWX;
9096 if (top != XINT (bar->top))
9097 mask |= CWY;
9098 if (sb_width != XINT (bar->width))
9099 mask |= CWWidth;
9100 if (height != XINT (bar->height))
9101 mask |= CWHeight;
9103 #ifdef USE_TOOLKIT_SCROLL_BARS
9105 /* Since toolkit scroll bars are smaller than the space reserved
9106 for them on the frame, we have to clear "under" them. */
9107 if (width && height)
9108 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9109 left, top, width, height, False);
9111 /* Move/size the scroll bar widget. */
9112 if (mask)
9113 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
9114 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9115 top,
9116 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9117 max (height, 1), 0);
9119 #else /* not USE_TOOLKIT_SCROLL_BARS */
9121 /* Clear areas not covered by the scroll bar because of
9122 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
9123 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
9125 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9126 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9127 height, False);
9128 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9129 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9130 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9131 height, False);
9134 /* Clear areas not covered by the scroll bar because it's not as
9135 wide as the area reserved for it . This makes sure a
9136 previous mode line display is cleared after C-x 2 C-x 1, for
9137 example. */
9139 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9140 int rest = area_width - sb_width;
9141 if (rest > 0)
9142 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9143 left + area_width - rest, 0,
9144 rest, max (height, 1), False);
9147 /* Move/size the scroll bar window. */
9148 if (mask)
9150 XWindowChanges wc;
9152 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9153 wc.y = top;
9154 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
9155 wc.height = height;
9156 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
9157 mask, &wc);
9160 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9162 /* Remember new settings. */
9163 XSETINT (bar->left, sb_left);
9164 XSETINT (bar->top, top);
9165 XSETINT (bar->width, sb_width);
9166 XSETINT (bar->height, height);
9168 UNBLOCK_INPUT;
9171 #ifdef USE_TOOLKIT_SCROLL_BARS
9172 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
9173 #else /* not USE_TOOLKIT_SCROLL_BARS */
9174 /* Set the scroll bar's current state, unless we're currently being
9175 dragged. */
9176 if (NILP (bar->dragging))
9178 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
9180 if (whole == 0)
9181 x_scroll_bar_set_handle (bar, 0, top_range, 0);
9182 else
9184 int start = ((double) position * top_range) / whole;
9185 int end = ((double) (position + portion) * top_range) / whole;
9186 x_scroll_bar_set_handle (bar, start, end, 0);
9189 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9191 XSETVECTOR (w->vertical_scroll_bar, bar);
9195 /* The following three hooks are used when we're doing a thorough
9196 redisplay of the frame. We don't explicitly know which scroll bars
9197 are going to be deleted, because keeping track of when windows go
9198 away is a real pain - "Can you say set-window-configuration, boys
9199 and girls?" Instead, we just assert at the beginning of redisplay
9200 that *all* scroll bars are to be removed, and then save a scroll bar
9201 from the fiery pit when we actually redisplay its window. */
9203 /* Arrange for all scroll bars on FRAME to be removed at the next call
9204 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
9205 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
9207 static void
9208 XTcondemn_scroll_bars (frame)
9209 FRAME_PTR frame;
9211 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
9212 while (! NILP (FRAME_SCROLL_BARS (frame)))
9214 Lisp_Object bar;
9215 bar = FRAME_SCROLL_BARS (frame);
9216 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
9217 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
9218 XSCROLL_BAR (bar)->prev = Qnil;
9219 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
9220 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
9221 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
9226 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
9227 Note that WINDOW isn't necessarily condemned at all. */
9229 static void
9230 XTredeem_scroll_bar (window)
9231 struct window *window;
9233 struct scroll_bar *bar;
9234 struct frame *f;
9236 /* We can't redeem this window's scroll bar if it doesn't have one. */
9237 if (NILP (window->vertical_scroll_bar))
9238 abort ();
9240 bar = XSCROLL_BAR (window->vertical_scroll_bar);
9242 /* Unlink it from the condemned list. */
9243 f = XFRAME (WINDOW_FRAME (window));
9244 if (NILP (bar->prev))
9246 /* If the prev pointer is nil, it must be the first in one of
9247 the lists. */
9248 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
9249 /* It's not condemned. Everything's fine. */
9250 return;
9251 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
9252 window->vertical_scroll_bar))
9253 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
9254 else
9255 /* If its prev pointer is nil, it must be at the front of
9256 one or the other! */
9257 abort ();
9259 else
9260 XSCROLL_BAR (bar->prev)->next = bar->next;
9262 if (! NILP (bar->next))
9263 XSCROLL_BAR (bar->next)->prev = bar->prev;
9265 bar->next = FRAME_SCROLL_BARS (f);
9266 bar->prev = Qnil;
9267 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9268 if (! NILP (bar->next))
9269 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9272 /* Remove all scroll bars on FRAME that haven't been saved since the
9273 last call to `*condemn_scroll_bars_hook'. */
9275 static void
9276 XTjudge_scroll_bars (f)
9277 FRAME_PTR f;
9279 Lisp_Object bar, next;
9281 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
9283 /* Clear out the condemned list now so we won't try to process any
9284 more events on the hapless scroll bars. */
9285 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
9287 for (; ! NILP (bar); bar = next)
9289 struct scroll_bar *b = XSCROLL_BAR (bar);
9291 x_scroll_bar_remove (b);
9293 next = b->next;
9294 b->next = b->prev = Qnil;
9297 /* Now there should be no references to the condemned scroll bars,
9298 and they should get garbage-collected. */
9302 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
9303 is a no-op when using toolkit scroll bars.
9305 This may be called from a signal handler, so we have to ignore GC
9306 mark bits. */
9308 static void
9309 x_scroll_bar_expose (bar, event)
9310 struct scroll_bar *bar;
9311 XEvent *event;
9313 #ifndef USE_TOOLKIT_SCROLL_BARS
9315 Window w = SCROLL_BAR_X_WINDOW (bar);
9316 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9317 GC gc = f->output_data.x->normal_gc;
9318 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9320 BLOCK_INPUT;
9322 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
9324 /* Draw a one-pixel border just inside the edges of the scroll bar. */
9325 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
9327 /* x, y, width, height */
9328 0, 0,
9329 XINT (bar->width) - 1 - width_trim - width_trim,
9330 XINT (bar->height) - 1);
9332 UNBLOCK_INPUT;
9334 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9337 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
9338 is set to something other than no_event, it is enqueued.
9340 This may be called from a signal handler, so we have to ignore GC
9341 mark bits. */
9343 #ifndef USE_TOOLKIT_SCROLL_BARS
9345 static void
9346 x_scroll_bar_handle_click (bar, event, emacs_event)
9347 struct scroll_bar *bar;
9348 XEvent *event;
9349 struct input_event *emacs_event;
9351 if (! GC_WINDOWP (bar->window))
9352 abort ();
9354 emacs_event->kind = scroll_bar_click;
9355 emacs_event->code = event->xbutton.button - Button1;
9356 emacs_event->modifiers
9357 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
9358 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
9359 event->xbutton.state)
9360 | (event->type == ButtonRelease
9361 ? up_modifier
9362 : down_modifier));
9363 emacs_event->frame_or_window = bar->window;
9364 emacs_event->arg = Qnil;
9365 emacs_event->timestamp = event->xbutton.time;
9367 #if 0
9368 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9369 int internal_height
9370 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9371 #endif
9372 int top_range
9373 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9374 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
9376 if (y < 0) y = 0;
9377 if (y > top_range) y = top_range;
9379 if (y < XINT (bar->start))
9380 emacs_event->part = scroll_bar_above_handle;
9381 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9382 emacs_event->part = scroll_bar_handle;
9383 else
9384 emacs_event->part = scroll_bar_below_handle;
9386 /* Just because the user has clicked on the handle doesn't mean
9387 they want to drag it. Lisp code needs to be able to decide
9388 whether or not we're dragging. */
9389 #if 0
9390 /* If the user has just clicked on the handle, record where they're
9391 holding it. */
9392 if (event->type == ButtonPress
9393 && emacs_event->part == scroll_bar_handle)
9394 XSETINT (bar->dragging, y - XINT (bar->start));
9395 #endif
9397 /* If the user has released the handle, set it to its final position. */
9398 if (event->type == ButtonRelease
9399 && ! NILP (bar->dragging))
9401 int new_start = y - XINT (bar->dragging);
9402 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9404 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9405 bar->dragging = Qnil;
9408 /* Same deal here as the other #if 0. */
9409 #if 0
9410 /* Clicks on the handle are always reported as occurring at the top of
9411 the handle. */
9412 if (emacs_event->part == scroll_bar_handle)
9413 emacs_event->x = bar->start;
9414 else
9415 XSETINT (emacs_event->x, y);
9416 #else
9417 XSETINT (emacs_event->x, y);
9418 #endif
9420 XSETINT (emacs_event->y, top_range);
9424 /* Handle some mouse motion while someone is dragging the scroll bar.
9426 This may be called from a signal handler, so we have to ignore GC
9427 mark bits. */
9429 static void
9430 x_scroll_bar_note_movement (bar, event)
9431 struct scroll_bar *bar;
9432 XEvent *event;
9434 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9436 last_mouse_movement_time = event->xmotion.time;
9438 f->mouse_moved = 1;
9439 XSETVECTOR (last_mouse_scroll_bar, bar);
9441 /* If we're dragging the bar, display it. */
9442 if (! GC_NILP (bar->dragging))
9444 /* Where should the handle be now? */
9445 int new_start = event->xmotion.y - XINT (bar->dragging);
9447 if (new_start != XINT (bar->start))
9449 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9451 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9456 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9458 /* Return information to the user about the current position of the mouse
9459 on the scroll bar. */
9461 static void
9462 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9463 FRAME_PTR *fp;
9464 Lisp_Object *bar_window;
9465 enum scroll_bar_part *part;
9466 Lisp_Object *x, *y;
9467 unsigned long *time;
9469 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
9470 Window w = SCROLL_BAR_X_WINDOW (bar);
9471 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9472 int win_x, win_y;
9473 Window dummy_window;
9474 int dummy_coord;
9475 unsigned int dummy_mask;
9477 BLOCK_INPUT;
9479 /* Get the mouse's position relative to the scroll bar window, and
9480 report that. */
9481 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
9483 /* Root, child, root x and root y. */
9484 &dummy_window, &dummy_window,
9485 &dummy_coord, &dummy_coord,
9487 /* Position relative to scroll bar. */
9488 &win_x, &win_y,
9490 /* Mouse buttons and modifier keys. */
9491 &dummy_mask))
9493 else
9495 #if 0
9496 int inside_height
9497 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9498 #endif
9499 int top_range
9500 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9502 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9504 if (! NILP (bar->dragging))
9505 win_y -= XINT (bar->dragging);
9507 if (win_y < 0)
9508 win_y = 0;
9509 if (win_y > top_range)
9510 win_y = top_range;
9512 *fp = f;
9513 *bar_window = bar->window;
9515 if (! NILP (bar->dragging))
9516 *part = scroll_bar_handle;
9517 else if (win_y < XINT (bar->start))
9518 *part = scroll_bar_above_handle;
9519 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9520 *part = scroll_bar_handle;
9521 else
9522 *part = scroll_bar_below_handle;
9524 XSETINT (*x, win_y);
9525 XSETINT (*y, top_range);
9527 f->mouse_moved = 0;
9528 last_mouse_scroll_bar = Qnil;
9531 *time = last_mouse_movement_time;
9533 UNBLOCK_INPUT;
9537 /* The screen has been cleared so we may have changed foreground or
9538 background colors, and the scroll bars may need to be redrawn.
9539 Clear out the scroll bars, and ask for expose events, so we can
9540 redraw them. */
9542 void
9543 x_scroll_bar_clear (f)
9544 FRAME_PTR f;
9546 #ifndef USE_TOOLKIT_SCROLL_BARS
9547 Lisp_Object bar;
9549 /* We can have scroll bars even if this is 0,
9550 if we just turned off scroll bar mode.
9551 But in that case we should not clear them. */
9552 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9553 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9554 bar = XSCROLL_BAR (bar)->next)
9555 XClearArea (FRAME_X_DISPLAY (f),
9556 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9557 0, 0, 0, 0, True);
9558 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9561 /* This processes Expose events from the menu-bar specific X event
9562 loop in xmenu.c. This allows to redisplay the frame if necessary
9563 when handling menu-bar or pop-up items. */
9566 process_expose_from_menu (event)
9567 XEvent event;
9569 FRAME_PTR f;
9570 struct x_display_info *dpyinfo;
9571 int frame_exposed_p = 0;
9573 BLOCK_INPUT;
9575 dpyinfo = x_display_info_for_display (event.xexpose.display);
9576 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9577 if (f)
9579 if (f->async_visible == 0)
9581 f->async_visible = 1;
9582 f->async_iconified = 0;
9583 f->output_data.x->has_been_visible = 1;
9584 SET_FRAME_GARBAGED (f);
9586 else
9588 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9589 event.xexpose.x, event.xexpose.y,
9590 event.xexpose.width, event.xexpose.height);
9591 frame_exposed_p = 1;
9594 else
9596 struct scroll_bar *bar
9597 = x_window_to_scroll_bar (event.xexpose.window);
9599 if (bar)
9600 x_scroll_bar_expose (bar, &event);
9603 UNBLOCK_INPUT;
9604 return frame_exposed_p;
9607 /* Define a queue to save up SelectionRequest events for later handling. */
9609 struct selection_event_queue
9611 XEvent event;
9612 struct selection_event_queue *next;
9615 static struct selection_event_queue *queue;
9617 /* Nonzero means queue up certain events--don't process them yet. */
9619 static int x_queue_selection_requests;
9621 /* Queue up an X event *EVENT, to be processed later. */
9623 static void
9624 x_queue_event (f, event)
9625 FRAME_PTR f;
9626 XEvent *event;
9628 struct selection_event_queue *queue_tmp
9629 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
9631 if (queue_tmp != NULL)
9633 queue_tmp->event = *event;
9634 queue_tmp->next = queue;
9635 queue = queue_tmp;
9639 /* Take all the queued events and put them back
9640 so that they get processed afresh. */
9642 static void
9643 x_unqueue_events (display)
9644 Display *display;
9646 while (queue != NULL)
9648 struct selection_event_queue *queue_tmp = queue;
9649 XPutBackEvent (display, &queue_tmp->event);
9650 queue = queue_tmp->next;
9651 xfree ((char *)queue_tmp);
9655 /* Start queuing SelectionRequest events. */
9657 void
9658 x_start_queuing_selection_requests (display)
9659 Display *display;
9661 x_queue_selection_requests++;
9664 /* Stop queuing SelectionRequest events. */
9666 void
9667 x_stop_queuing_selection_requests (display)
9668 Display *display;
9670 x_queue_selection_requests--;
9671 x_unqueue_events (display);
9674 /* The main X event-reading loop - XTread_socket. */
9676 /* Time stamp of enter window event. This is only used by XTread_socket,
9677 but we have to put it out here, since static variables within functions
9678 sometimes don't work. */
9680 static Time enter_timestamp;
9682 /* This holds the state XLookupString needs to implement dead keys
9683 and other tricks known as "compose processing". _X Window System_
9684 says that a portable program can't use this, but Stephen Gildea assures
9685 me that letting the compiler initialize it to zeros will work okay.
9687 This must be defined outside of XTread_socket, for the same reasons
9688 given for enter_time stamp, above. */
9690 static XComposeStatus compose_status;
9692 /* Record the last 100 characters stored
9693 to help debug the loss-of-chars-during-GC problem. */
9695 static int temp_index;
9696 static short temp_buffer[100];
9698 /* Set this to nonzero to fake an "X I/O error"
9699 on a particular display. */
9701 struct x_display_info *XTread_socket_fake_io_error;
9703 /* When we find no input here, we occasionally do a no-op command
9704 to verify that the X server is still running and we can still talk with it.
9705 We try all the open displays, one by one.
9706 This variable is used for cycling thru the displays. */
9708 static struct x_display_info *next_noop_dpyinfo;
9710 #define SET_SAVED_MENU_EVENT(size) \
9711 do \
9713 if (f->output_data.x->saved_menu_event == 0) \
9714 f->output_data.x->saved_menu_event \
9715 = (XEvent *) xmalloc (sizeof (XEvent)); \
9716 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9717 if (numchars >= 1) \
9719 bufp->kind = menu_bar_activate_event; \
9720 XSETFRAME (bufp->frame_or_window, f); \
9721 bufp->arg = Qnil; \
9722 bufp++; \
9723 count++; \
9724 numchars--; \
9727 while (0)
9729 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9730 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9732 /* Read events coming from the X server.
9733 This routine is called by the SIGIO handler.
9734 We return as soon as there are no more events to be read.
9736 Events representing keys are stored in buffer BUFP,
9737 which can hold up to NUMCHARS characters.
9738 We return the number of characters stored into the buffer,
9739 thus pretending to be `read'.
9741 EXPECTED is nonzero if the caller knows input is available. */
9744 XTread_socket (sd, bufp, numchars, expected)
9745 register int sd;
9746 /* register */ struct input_event *bufp;
9747 /* register */ int numchars;
9748 int expected;
9750 int count = 0;
9751 int nbytes = 0;
9752 XEvent event;
9753 struct frame *f;
9754 int event_found = 0;
9755 struct x_display_info *dpyinfo;
9756 struct coding_system coding;
9758 if (interrupt_input_blocked)
9760 interrupt_input_pending = 1;
9761 return -1;
9764 interrupt_input_pending = 0;
9765 BLOCK_INPUT;
9767 /* So people can tell when we have read the available input. */
9768 input_signal_count++;
9770 if (numchars <= 0)
9771 abort (); /* Don't think this happens. */
9773 ++handling_signal;
9775 /* The input should be decoded if it is from XIM. Currently the
9776 locale of XIM is the same as that of the system. So, we can use
9777 Vlocale_coding_system which is initialized properly at Emacs
9778 startup time. */
9779 setup_coding_system (Vlocale_coding_system, &coding);
9780 coding.src_multibyte = 0;
9781 coding.dst_multibyte = 1;
9782 /* The input is converted to events, thus we can't handle
9783 composition. Anyway, there's no XIM that gives us composition
9784 information. */
9785 coding.composing = COMPOSITION_DISABLED;
9787 /* Find the display we are supposed to read input for.
9788 It's the one communicating on descriptor SD. */
9789 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9791 #if 0 /* This ought to be unnecessary; let's verify it. */
9792 #ifdef FIOSNBIO
9793 /* If available, Xlib uses FIOSNBIO to make the socket
9794 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
9795 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
9796 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
9797 fcntl (dpyinfo->connection, F_SETFL, 0);
9798 #endif /* ! defined (FIOSNBIO) */
9799 #endif
9801 #if 0 /* This code can't be made to work, with multiple displays,
9802 and appears not to be used on any system any more.
9803 Also keyboard.c doesn't turn O_NDELAY on and off
9804 for X connections. */
9805 #ifndef SIGIO
9806 #ifndef HAVE_SELECT
9807 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9809 extern int read_alarm_should_throw;
9810 read_alarm_should_throw = 1;
9811 XPeekEvent (dpyinfo->display, &event);
9812 read_alarm_should_throw = 0;
9814 #endif /* HAVE_SELECT */
9815 #endif /* SIGIO */
9816 #endif
9818 /* For debugging, this gives a way to fake an I/O error. */
9819 if (dpyinfo == XTread_socket_fake_io_error)
9821 XTread_socket_fake_io_error = 0;
9822 x_io_error_quitter (dpyinfo->display);
9825 while (XPending (dpyinfo->display))
9827 XNextEvent (dpyinfo->display, &event);
9829 #ifdef HAVE_X_I18N
9831 /* Filter events for the current X input method.
9832 XFilterEvent returns non-zero if the input method has
9833 consumed the event. We pass the frame's X window to
9834 XFilterEvent because that's the one for which the IC
9835 was created. */
9836 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9837 event.xclient.window);
9838 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
9839 break;
9841 #endif
9842 event_found = 1;
9844 switch (event.type)
9846 case ClientMessage:
9848 if (event.xclient.message_type
9849 == dpyinfo->Xatom_wm_protocols
9850 && event.xclient.format == 32)
9852 if (event.xclient.data.l[0]
9853 == dpyinfo->Xatom_wm_take_focus)
9855 /* Use x_any_window_to_frame because this
9856 could be the shell widget window
9857 if the frame has no title bar. */
9858 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
9859 #ifdef HAVE_X_I18N
9860 /* Not quite sure this is needed -pd */
9861 if (f && FRAME_XIC (f))
9862 XSetICFocus (FRAME_XIC (f));
9863 #endif
9864 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9865 instructs the WM to set the input focus automatically for
9866 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9867 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9868 it has set the focus. So, XSetInputFocus below is not
9869 needed.
9871 The call to XSetInputFocus below has also caused trouble. In
9872 cases where the XSetInputFocus done by the WM and the one
9873 below are temporally close (on a fast machine), the call
9874 below can generate additional FocusIn events which confuse
9875 Emacs. */
9877 /* Since we set WM_TAKE_FOCUS, we must call
9878 XSetInputFocus explicitly. But not if f is null,
9879 since that might be an event for a deleted frame. */
9880 if (f)
9882 Display *d = event.xclient.display;
9883 /* Catch and ignore errors, in case window has been
9884 iconified by a window manager such as GWM. */
9885 int count = x_catch_errors (d);
9886 XSetInputFocus (d, event.xclient.window,
9887 /* The ICCCM says this is
9888 the only valid choice. */
9889 RevertToParent,
9890 event.xclient.data.l[1]);
9891 /* This is needed to detect the error
9892 if there is an error. */
9893 XSync (d, False);
9894 x_uncatch_errors (d, count);
9896 /* Not certain about handling scroll bars here */
9897 #endif /* 0 */
9899 else if (event.xclient.data.l[0]
9900 == dpyinfo->Xatom_wm_save_yourself)
9902 /* Save state modify the WM_COMMAND property to
9903 something which can reinstate us. This notifies
9904 the session manager, who's looking for such a
9905 PropertyNotify. Can restart processing when
9906 a keyboard or mouse event arrives. */
9907 if (numchars > 0)
9909 f = x_top_window_to_frame (dpyinfo,
9910 event.xclient.window);
9912 /* This is just so we only give real data once
9913 for a single Emacs process. */
9914 if (f == SELECTED_FRAME ())
9915 XSetCommand (FRAME_X_DISPLAY (f),
9916 event.xclient.window,
9917 initial_argv, initial_argc);
9918 else if (f)
9919 XSetCommand (FRAME_X_DISPLAY (f),
9920 event.xclient.window,
9921 0, 0);
9924 else if (event.xclient.data.l[0]
9925 == dpyinfo->Xatom_wm_delete_window)
9927 struct frame *f
9928 = x_any_window_to_frame (dpyinfo,
9929 event.xclient.window);
9931 if (f)
9933 if (numchars == 0)
9934 abort ();
9936 bufp->kind = delete_window_event;
9937 XSETFRAME (bufp->frame_or_window, f);
9938 bufp->arg = Qnil;
9939 bufp++;
9941 count += 1;
9942 numchars -= 1;
9946 else if (event.xclient.message_type
9947 == dpyinfo->Xatom_wm_configure_denied)
9950 else if (event.xclient.message_type
9951 == dpyinfo->Xatom_wm_window_moved)
9953 int new_x, new_y;
9954 struct frame *f
9955 = x_window_to_frame (dpyinfo, event.xclient.window);
9957 new_x = event.xclient.data.s[0];
9958 new_y = event.xclient.data.s[1];
9960 if (f)
9962 f->output_data.x->left_pos = new_x;
9963 f->output_data.x->top_pos = new_y;
9966 #ifdef HACK_EDITRES
9967 else if (event.xclient.message_type
9968 == dpyinfo->Xatom_editres)
9970 struct frame *f
9971 = x_any_window_to_frame (dpyinfo, event.xclient.window);
9972 _XEditResCheckMessages (f->output_data.x->widget, NULL,
9973 &event, NULL);
9975 #endif /* HACK_EDITRES */
9976 else if ((event.xclient.message_type
9977 == dpyinfo->Xatom_DONE)
9978 || (event.xclient.message_type
9979 == dpyinfo->Xatom_PAGE))
9981 /* Ghostview job completed. Kill it. We could
9982 reply with "Next" if we received "Page", but we
9983 currently never do because we are interested in
9984 images, only, which should have 1 page. */
9985 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9986 struct frame *f
9987 = x_window_to_frame (dpyinfo, event.xclient.window);
9988 x_kill_gs_process (pixmap, f);
9989 expose_frame (f, 0, 0, 0, 0);
9991 #ifdef USE_TOOLKIT_SCROLL_BARS
9992 /* Scroll bar callbacks send a ClientMessage from which
9993 we construct an input_event. */
9994 else if (event.xclient.message_type
9995 == dpyinfo->Xatom_Scrollbar)
9997 x_scroll_bar_to_input_event (&event, bufp);
9998 ++bufp, ++count, --numchars;
9999 goto out;
10001 #endif /* USE_TOOLKIT_SCROLL_BARS */
10002 else
10003 goto OTHER;
10005 break;
10007 case SelectionNotify:
10008 #ifdef USE_X_TOOLKIT
10009 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
10010 goto OTHER;
10011 #endif /* not USE_X_TOOLKIT */
10012 x_handle_selection_notify (&event.xselection);
10013 break;
10015 case SelectionClear: /* Someone has grabbed ownership. */
10016 #ifdef USE_X_TOOLKIT
10017 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
10018 goto OTHER;
10019 #endif /* USE_X_TOOLKIT */
10021 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
10023 if (numchars == 0)
10024 abort ();
10026 bufp->kind = selection_clear_event;
10027 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10028 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10029 SELECTION_EVENT_TIME (bufp) = eventp->time;
10030 bufp->frame_or_window = Qnil;
10031 bufp->arg = Qnil;
10032 bufp++;
10034 count += 1;
10035 numchars -= 1;
10037 break;
10039 case SelectionRequest: /* Someone wants our selection. */
10040 #ifdef USE_X_TOOLKIT
10041 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
10042 goto OTHER;
10043 #endif /* USE_X_TOOLKIT */
10044 if (x_queue_selection_requests)
10045 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
10046 &event);
10047 else
10049 XSelectionRequestEvent *eventp
10050 = (XSelectionRequestEvent *) &event;
10052 if (numchars == 0)
10053 abort ();
10055 bufp->kind = selection_request_event;
10056 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10057 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
10058 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10059 SELECTION_EVENT_TARGET (bufp) = eventp->target;
10060 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
10061 SELECTION_EVENT_TIME (bufp) = eventp->time;
10062 bufp->frame_or_window = Qnil;
10063 bufp->arg = Qnil;
10064 bufp++;
10066 count += 1;
10067 numchars -= 1;
10069 break;
10071 case PropertyNotify:
10072 #if 0 /* This is plain wrong. In the case that we are waiting for a
10073 PropertyNotify used as an ACK in incremental selection
10074 transfer, the property will be on the receiver's window. */
10075 #if defined USE_X_TOOLKIT
10076 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
10077 goto OTHER;
10078 #endif
10079 #endif
10080 x_handle_property_notify (&event.xproperty);
10081 goto OTHER;
10083 case ReparentNotify:
10084 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
10085 if (f)
10087 int x, y;
10088 f->output_data.x->parent_desc = event.xreparent.parent;
10089 x_real_positions (f, &x, &y);
10090 f->output_data.x->left_pos = x;
10091 f->output_data.x->top_pos = y;
10093 break;
10095 case Expose:
10096 f = x_window_to_frame (dpyinfo, event.xexpose.window);
10097 if (f)
10099 if (f->async_visible == 0)
10101 f->async_visible = 1;
10102 f->async_iconified = 0;
10103 f->output_data.x->has_been_visible = 1;
10104 SET_FRAME_GARBAGED (f);
10106 else
10107 expose_frame (x_window_to_frame (dpyinfo,
10108 event.xexpose.window),
10109 event.xexpose.x, event.xexpose.y,
10110 event.xexpose.width, event.xexpose.height);
10112 else
10114 #ifndef USE_TOOLKIT_SCROLL_BARS
10115 struct scroll_bar *bar;
10116 #endif
10117 #if defined USE_X_TOOLKIT && defined USE_LUCID
10118 /* Submenus of the Lucid menu bar aren't widgets
10119 themselves, so there's no way to dispatch events
10120 to them. Recognize this case separately. */
10122 Widget widget
10123 = x_window_to_menu_bar (event.xexpose.window);
10124 if (widget)
10125 xlwmenu_redisplay (widget);
10127 #endif /* USE_X_TOOLKIT && USE_LUCID */
10129 #ifdef USE_TOOLKIT_SCROLL_BARS
10130 /* Dispatch event to the widget. */
10131 goto OTHER;
10132 #else /* not USE_TOOLKIT_SCROLL_BARS */
10133 bar = x_window_to_scroll_bar (event.xexpose.window);
10135 if (bar)
10136 x_scroll_bar_expose (bar, &event);
10137 #ifdef USE_X_TOOLKIT
10138 else
10139 goto OTHER;
10140 #endif /* USE_X_TOOLKIT */
10141 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10143 break;
10145 case GraphicsExpose: /* This occurs when an XCopyArea's
10146 source area was obscured or not
10147 available.*/
10148 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
10149 if (f)
10151 expose_frame (f,
10152 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
10153 event.xgraphicsexpose.width,
10154 event.xgraphicsexpose.height);
10156 #ifdef USE_X_TOOLKIT
10157 else
10158 goto OTHER;
10159 #endif /* USE_X_TOOLKIT */
10160 break;
10162 case NoExpose: /* This occurs when an XCopyArea's
10163 source area was completely
10164 available */
10165 break;
10167 case UnmapNotify:
10168 /* Redo the mouse-highlight after the tooltip has gone. */
10169 if (event.xmap.window == tip_window)
10171 tip_window = 0;
10172 redo_mouse_highlight ();
10175 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
10176 if (f) /* F may no longer exist if
10177 the frame was deleted. */
10179 /* While a frame is unmapped, display generation is
10180 disabled; you don't want to spend time updating a
10181 display that won't ever be seen. */
10182 f->async_visible = 0;
10183 /* We can't distinguish, from the event, whether the window
10184 has become iconified or invisible. So assume, if it
10185 was previously visible, than now it is iconified.
10186 But x_make_frame_invisible clears both
10187 the visible flag and the iconified flag;
10188 and that way, we know the window is not iconified now. */
10189 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
10191 f->async_iconified = 1;
10193 bufp->kind = iconify_event;
10194 XSETFRAME (bufp->frame_or_window, f);
10195 bufp->arg = Qnil;
10196 bufp++;
10197 count++;
10198 numchars--;
10201 goto OTHER;
10203 case MapNotify:
10204 if (event.xmap.window == tip_window)
10205 /* The tooltip has been drawn already. Avoid
10206 the SET_FRAME_GARBAGED below. */
10207 goto OTHER;
10209 /* We use x_top_window_to_frame because map events can
10210 come for sub-windows and they don't mean that the
10211 frame is visible. */
10212 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
10213 if (f)
10215 f->async_visible = 1;
10216 f->async_iconified = 0;
10217 f->output_data.x->has_been_visible = 1;
10219 /* wait_reading_process_input will notice this and update
10220 the frame's display structures. */
10221 SET_FRAME_GARBAGED (f);
10223 if (f->iconified)
10225 bufp->kind = deiconify_event;
10226 XSETFRAME (bufp->frame_or_window, f);
10227 bufp->arg = Qnil;
10228 bufp++;
10229 count++;
10230 numchars--;
10232 else if (! NILP (Vframe_list)
10233 && ! NILP (XCDR (Vframe_list)))
10234 /* Force a redisplay sooner or later
10235 to update the frame titles
10236 in case this is the second frame. */
10237 record_asynch_buffer_change ();
10239 goto OTHER;
10241 case KeyPress:
10242 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
10244 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
10245 if (f == 0)
10247 /* Scroll bars consume key events, but we want
10248 the keys to go to the scroll bar's frame. */
10249 Widget widget = XtWindowToWidget (dpyinfo->display,
10250 event.xkey.window);
10251 if (widget && XmIsScrollBar (widget))
10253 widget = XtParent (widget);
10254 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
10257 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
10259 if (f != 0)
10261 KeySym keysym, orig_keysym;
10262 /* al%imercury@uunet.uu.net says that making this 81
10263 instead of 80 fixed a bug whereby meta chars made
10264 his Emacs hang.
10266 It seems that some version of XmbLookupString has
10267 a bug of not returning XBufferOverflow in
10268 status_return even if the input is too long to
10269 fit in 81 bytes. So, we must prepare sufficient
10270 bytes for copy_buffer. 513 bytes (256 chars for
10271 two-byte character set) seems to be a faily good
10272 approximation. -- 2000.8.10 handa@etl.go.jp */
10273 unsigned char copy_buffer[513];
10274 unsigned char *copy_bufptr = copy_buffer;
10275 int copy_bufsiz = sizeof (copy_buffer);
10276 int modifiers;
10278 event.xkey.state
10279 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
10280 extra_keyboard_modifiers);
10281 modifiers = event.xkey.state;
10283 /* This will have to go some day... */
10285 /* make_lispy_event turns chars into control chars.
10286 Don't do it here because XLookupString is too eager. */
10287 event.xkey.state &= ~ControlMask;
10288 event.xkey.state &= ~(dpyinfo->meta_mod_mask
10289 | dpyinfo->super_mod_mask
10290 | dpyinfo->hyper_mod_mask
10291 | dpyinfo->alt_mod_mask);
10293 /* In case Meta is ComposeCharacter,
10294 clear its status. According to Markus Ehrnsperger
10295 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
10296 this enables ComposeCharacter to work whether or
10297 not it is combined with Meta. */
10298 if (modifiers & dpyinfo->meta_mod_mask)
10299 bzero (&compose_status, sizeof (compose_status));
10301 #ifdef HAVE_X_I18N
10302 if (FRAME_XIC (f))
10304 Status status_return;
10306 nbytes = XmbLookupString (FRAME_XIC (f),
10307 &event.xkey, copy_bufptr,
10308 copy_bufsiz, &keysym,
10309 &status_return);
10310 if (status_return == XBufferOverflow)
10312 copy_bufsiz = nbytes + 1;
10313 copy_bufptr = (char *) alloca (copy_bufsiz);
10314 nbytes = XmbLookupString (FRAME_XIC (f),
10315 &event.xkey, copy_bufptr,
10316 copy_bufsiz, &keysym,
10317 &status_return);
10320 if (status_return == XLookupNone)
10321 break;
10322 else if (status_return == XLookupChars)
10324 keysym = NoSymbol;
10325 modifiers = 0;
10327 else if (status_return != XLookupKeySym
10328 && status_return != XLookupBoth)
10329 abort ();
10331 else
10332 nbytes = XLookupString (&event.xkey, copy_bufptr,
10333 copy_bufsiz, &keysym,
10334 &compose_status);
10335 #else
10336 nbytes = XLookupString (&event.xkey, copy_bufptr,
10337 copy_bufsiz, &keysym,
10338 &compose_status);
10339 #endif
10341 orig_keysym = keysym;
10343 if (numchars > 1)
10345 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
10346 || keysym == XK_Delete
10347 #ifdef XK_ISO_Left_Tab
10348 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
10349 #endif
10350 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
10351 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
10352 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
10353 #ifdef HPUX
10354 /* This recognizes the "extended function keys".
10355 It seems there's no cleaner way.
10356 Test IsModifierKey to avoid handling mode_switch
10357 incorrectly. */
10358 || ((unsigned) (keysym) >= XK_Select
10359 && (unsigned)(keysym) < XK_KP_Space)
10360 #endif
10361 #ifdef XK_dead_circumflex
10362 || orig_keysym == XK_dead_circumflex
10363 #endif
10364 #ifdef XK_dead_grave
10365 || orig_keysym == XK_dead_grave
10366 #endif
10367 #ifdef XK_dead_tilde
10368 || orig_keysym == XK_dead_tilde
10369 #endif
10370 #ifdef XK_dead_diaeresis
10371 || orig_keysym == XK_dead_diaeresis
10372 #endif
10373 #ifdef XK_dead_macron
10374 || orig_keysym == XK_dead_macron
10375 #endif
10376 #ifdef XK_dead_degree
10377 || orig_keysym == XK_dead_degree
10378 #endif
10379 #ifdef XK_dead_acute
10380 || orig_keysym == XK_dead_acute
10381 #endif
10382 #ifdef XK_dead_cedilla
10383 || orig_keysym == XK_dead_cedilla
10384 #endif
10385 #ifdef XK_dead_breve
10386 || orig_keysym == XK_dead_breve
10387 #endif
10388 #ifdef XK_dead_ogonek
10389 || orig_keysym == XK_dead_ogonek
10390 #endif
10391 #ifdef XK_dead_caron
10392 || orig_keysym == XK_dead_caron
10393 #endif
10394 #ifdef XK_dead_doubleacute
10395 || orig_keysym == XK_dead_doubleacute
10396 #endif
10397 #ifdef XK_dead_abovedot
10398 || orig_keysym == XK_dead_abovedot
10399 #endif
10400 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10401 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10402 /* Any "vendor-specific" key is ok. */
10403 || (orig_keysym & (1 << 28)))
10404 && ! (IsModifierKey (orig_keysym)
10405 #ifndef HAVE_X11R5
10406 #ifdef XK_Mode_switch
10407 || ((unsigned)(orig_keysym) == XK_Mode_switch)
10408 #endif
10409 #ifdef XK_Num_Lock
10410 || ((unsigned)(orig_keysym) == XK_Num_Lock)
10411 #endif
10412 #endif /* not HAVE_X11R5 */
10415 if (temp_index == sizeof temp_buffer / sizeof (short))
10416 temp_index = 0;
10417 temp_buffer[temp_index++] = keysym;
10418 bufp->kind = non_ascii_keystroke;
10419 bufp->code = keysym;
10420 XSETFRAME (bufp->frame_or_window, f);
10421 bufp->arg = Qnil;
10422 bufp->modifiers
10423 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10424 modifiers);
10425 bufp->timestamp = event.xkey.time;
10426 bufp++;
10427 count++;
10428 numchars--;
10430 else if (numchars > nbytes)
10432 register int i;
10433 register int c;
10434 int nchars, len;
10436 for (i = 0; i < nbytes; i++)
10438 if (temp_index == (sizeof temp_buffer
10439 / sizeof (short)))
10440 temp_index = 0;
10441 temp_buffer[temp_index++] = copy_bufptr[i];
10444 if (/* If the event is not from XIM, */
10445 event.xkey.keycode != 0
10446 /* or the current locale doesn't request
10447 decoding of the intup data, ... */
10448 || coding.type == coding_type_raw_text
10449 || coding.type == coding_type_no_conversion)
10451 /* ... we can use the input data as is. */
10452 nchars = nbytes;
10454 else
10456 /* We have to decode the input data. */
10457 int require;
10458 unsigned char *p;
10460 require = decoding_buffer_size (&coding, nbytes);
10461 p = (unsigned char *) alloca (require);
10462 coding.mode |= CODING_MODE_LAST_BLOCK;
10463 decode_coding (&coding, copy_bufptr, p,
10464 nbytes, require);
10465 nbytes = coding.produced;
10466 nchars = coding.produced_char;
10467 copy_bufptr = p;
10470 /* Convert the input data to a sequence of
10471 character events. */
10472 for (i = 0; i < nbytes; i += len)
10474 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10475 nbytes - i, len);
10476 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
10477 ? ascii_keystroke
10478 : multibyte_char_keystroke);
10479 bufp->code = c;
10480 XSETFRAME (bufp->frame_or_window, f);
10481 bufp->arg = Qnil;
10482 bufp->modifiers
10483 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10484 modifiers);
10485 bufp->timestamp = event.xkey.time;
10486 bufp++;
10489 count += nchars;
10490 numchars -= nchars;
10492 if (keysym == NoSymbol)
10493 break;
10495 else
10496 abort ();
10498 else
10499 abort ();
10501 #ifdef HAVE_X_I18N
10502 /* Don't dispatch this event since XtDispatchEvent calls
10503 XFilterEvent, and two calls in a row may freeze the
10504 client. */
10505 break;
10506 #else
10507 goto OTHER;
10508 #endif
10510 case KeyRelease:
10511 #ifdef HAVE_X_I18N
10512 /* Don't dispatch this event since XtDispatchEvent calls
10513 XFilterEvent, and two calls in a row may freeze the
10514 client. */
10515 break;
10516 #else
10517 goto OTHER;
10518 #endif
10520 /* Here's a possible interpretation of the whole
10521 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
10522 you get a FocusIn event, you have to get a FocusOut
10523 event before you relinquish the focus. If you
10524 haven't received a FocusIn event, then a mere
10525 LeaveNotify is enough to free you. */
10527 case EnterNotify:
10529 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10531 if (event.xcrossing.focus)
10533 /* Avoid nasty pop/raise loops. */
10534 if (f && (!(f->auto_raise)
10535 || !(f->auto_lower)
10536 || (event.xcrossing.time - enter_timestamp) > 500))
10538 x_new_focus_frame (dpyinfo, f);
10539 enter_timestamp = event.xcrossing.time;
10542 else if (f == dpyinfo->x_focus_frame)
10543 x_new_focus_frame (dpyinfo, 0);
10545 /* EnterNotify counts as mouse movement,
10546 so update things that depend on mouse position. */
10547 if (f && !f->output_data.x->hourglass_p)
10548 note_mouse_movement (f, &event.xmotion);
10549 goto OTHER;
10552 case FocusIn:
10553 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10554 if (event.xfocus.detail != NotifyPointer)
10555 dpyinfo->x_focus_event_frame = f;
10556 if (f)
10558 x_new_focus_frame (dpyinfo, f);
10560 /* Don't stop displaying the initial startup message
10561 for a switch-frame event we don't need. */
10562 if (GC_NILP (Vterminal_frame)
10563 && GC_CONSP (Vframe_list)
10564 && !GC_NILP (XCDR (Vframe_list)))
10566 bufp->kind = FOCUS_IN_EVENT;
10567 XSETFRAME (bufp->frame_or_window, f);
10568 bufp->arg = Qnil;
10569 ++bufp, ++count, --numchars;
10573 #ifdef HAVE_X_I18N
10574 if (f && FRAME_XIC (f))
10575 XSetICFocus (FRAME_XIC (f));
10576 #endif
10578 goto OTHER;
10580 case LeaveNotify:
10581 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
10582 if (f)
10584 if (f == dpyinfo->mouse_face_mouse_frame)
10586 /* If we move outside the frame, then we're
10587 certainly no longer on any text in the frame. */
10588 clear_mouse_face (dpyinfo);
10589 dpyinfo->mouse_face_mouse_frame = 0;
10592 /* Generate a nil HELP_EVENT to cancel a help-echo.
10593 Do it only if there's something to cancel.
10594 Otherwise, the startup message is cleared when
10595 the mouse leaves the frame. */
10596 if (any_help_event_p)
10598 Lisp_Object frame;
10599 int n;
10601 XSETFRAME (frame, f);
10602 help_echo = Qnil;
10603 n = gen_help_event (bufp, numchars,
10604 Qnil, frame, Qnil, Qnil, 0);
10605 bufp += n, count += n, numchars -= n;
10608 if (event.xcrossing.focus)
10609 x_mouse_leave (dpyinfo);
10610 else
10612 if (f == dpyinfo->x_focus_event_frame)
10613 dpyinfo->x_focus_event_frame = 0;
10614 if (f == dpyinfo->x_focus_frame)
10615 x_new_focus_frame (dpyinfo, 0);
10618 goto OTHER;
10620 case FocusOut:
10621 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10622 if (event.xfocus.detail != NotifyPointer
10623 && f == dpyinfo->x_focus_event_frame)
10624 dpyinfo->x_focus_event_frame = 0;
10625 if (f && f == dpyinfo->x_focus_frame)
10626 x_new_focus_frame (dpyinfo, 0);
10628 #ifdef HAVE_X_I18N
10629 if (f && FRAME_XIC (f))
10630 XUnsetICFocus (FRAME_XIC (f));
10631 #endif
10633 goto OTHER;
10635 case MotionNotify:
10637 previous_help_echo = help_echo;
10638 help_echo = help_echo_object = help_echo_window = Qnil;
10639 help_echo_pos = -1;
10641 if (dpyinfo->grabbed && last_mouse_frame
10642 && FRAME_LIVE_P (last_mouse_frame))
10643 f = last_mouse_frame;
10644 else
10645 f = x_window_to_frame (dpyinfo, event.xmotion.window);
10647 if (f)
10648 note_mouse_movement (f, &event.xmotion);
10649 else
10651 #ifndef USE_TOOLKIT_SCROLL_BARS
10652 struct scroll_bar *bar
10653 = x_window_to_scroll_bar (event.xmotion.window);
10655 if (bar)
10656 x_scroll_bar_note_movement (bar, &event);
10657 #endif /* USE_TOOLKIT_SCROLL_BARS */
10659 /* If we move outside the frame, then we're
10660 certainly no longer on any text in the frame. */
10661 clear_mouse_face (dpyinfo);
10664 /* If the contents of the global variable help_echo
10665 has changed, generate a HELP_EVENT. */
10666 if (!NILP (help_echo)
10667 || !NILP (previous_help_echo))
10669 Lisp_Object frame;
10670 int n;
10672 if (f)
10673 XSETFRAME (frame, f);
10674 else
10675 frame = Qnil;
10677 any_help_event_p = 1;
10678 n = gen_help_event (bufp, numchars, help_echo, frame,
10679 help_echo_window, help_echo_object,
10680 help_echo_pos);
10681 bufp += n, count += n, numchars -= n;
10684 goto OTHER;
10687 case ConfigureNotify:
10688 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10689 if (f)
10691 #ifndef USE_X_TOOLKIT
10692 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10693 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
10695 /* In the toolkit version, change_frame_size
10696 is called by the code that handles resizing
10697 of the EmacsFrame widget. */
10699 /* Even if the number of character rows and columns has
10700 not changed, the font size may have changed, so we need
10701 to check the pixel dimensions as well. */
10702 if (columns != f->width
10703 || rows != f->height
10704 || event.xconfigure.width != f->output_data.x->pixel_width
10705 || event.xconfigure.height != f->output_data.x->pixel_height)
10707 change_frame_size (f, rows, columns, 0, 1, 0);
10708 SET_FRAME_GARBAGED (f);
10709 cancel_mouse_face (f);
10711 #endif
10713 f->output_data.x->pixel_width = event.xconfigure.width;
10714 f->output_data.x->pixel_height = event.xconfigure.height;
10716 /* What we have now is the position of Emacs's own window.
10717 Convert that to the position of the window manager window. */
10718 x_real_positions (f, &f->output_data.x->left_pos,
10719 &f->output_data.x->top_pos);
10721 #ifdef HAVE_X_I18N
10722 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10723 xic_set_statusarea (f);
10724 #endif
10726 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10728 /* Since the WM decorations come below top_pos now,
10729 we must put them below top_pos in the future. */
10730 f->output_data.x->win_gravity = NorthWestGravity;
10731 x_wm_set_size_hint (f, (long) 0, 0);
10733 #ifdef USE_MOTIF
10734 /* Some window managers pass (0,0) as the location of
10735 the window, and the Motif event handler stores it
10736 in the emacs widget, which messes up Motif menus. */
10737 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10739 event.xconfigure.x = f->output_data.x->widget->core.x;
10740 event.xconfigure.y = f->output_data.x->widget->core.y;
10742 #endif /* USE_MOTIF */
10744 goto OTHER;
10746 case ButtonPress:
10747 case ButtonRelease:
10749 /* If we decide we want to generate an event to be seen
10750 by the rest of Emacs, we put it here. */
10751 struct input_event emacs_event;
10752 int tool_bar_p = 0;
10754 emacs_event.kind = no_event;
10755 bzero (&compose_status, sizeof (compose_status));
10757 if (dpyinfo->grabbed
10758 && last_mouse_frame
10759 && FRAME_LIVE_P (last_mouse_frame))
10760 f = last_mouse_frame;
10761 else
10762 f = x_window_to_frame (dpyinfo, event.xbutton.window);
10764 if (f)
10766 /* Is this in the tool-bar? */
10767 if (WINDOWP (f->tool_bar_window)
10768 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
10770 Lisp_Object window;
10771 int p, x, y;
10773 x = event.xbutton.x;
10774 y = event.xbutton.y;
10776 /* Set x and y. */
10777 window = window_from_coordinates (f, x, y, &p, 1);
10778 if (EQ (window, f->tool_bar_window))
10780 x_handle_tool_bar_click (f, &event.xbutton);
10781 tool_bar_p = 1;
10785 if (!tool_bar_p)
10786 if (!dpyinfo->x_focus_frame
10787 || f == dpyinfo->x_focus_frame)
10788 construct_mouse_click (&emacs_event, &event, f);
10790 else
10792 #ifndef USE_TOOLKIT_SCROLL_BARS
10793 struct scroll_bar *bar
10794 = x_window_to_scroll_bar (event.xbutton.window);
10796 if (bar)
10797 x_scroll_bar_handle_click (bar, &event, &emacs_event);
10798 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10801 if (event.type == ButtonPress)
10803 dpyinfo->grabbed |= (1 << event.xbutton.button);
10804 last_mouse_frame = f;
10805 /* Ignore any mouse motion that happened
10806 before this event; any subsequent mouse-movement
10807 Emacs events should reflect only motion after
10808 the ButtonPress. */
10809 if (f != 0)
10810 f->mouse_moved = 0;
10812 if (!tool_bar_p)
10813 last_tool_bar_item = -1;
10815 else
10817 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
10820 if (numchars >= 1 && emacs_event.kind != no_event)
10822 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10823 bufp++;
10824 count++;
10825 numchars--;
10828 #ifdef USE_X_TOOLKIT
10829 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10830 /* For a down-event in the menu bar,
10831 don't pass it to Xt right now.
10832 Instead, save it away
10833 and we will pass it to Xt from kbd_buffer_get_event.
10834 That way, we can run some Lisp code first. */
10835 if (f && event.type == ButtonPress
10836 /* Verify the event is really within the menu bar
10837 and not just sent to it due to grabbing. */
10838 && event.xbutton.x >= 0
10839 && event.xbutton.x < f->output_data.x->pixel_width
10840 && event.xbutton.y >= 0
10841 && event.xbutton.y < f->output_data.x->menubar_height
10842 && event.xbutton.same_screen)
10844 SET_SAVED_BUTTON_EVENT;
10845 XSETFRAME (last_mouse_press_frame, f);
10847 else if (event.type == ButtonPress)
10849 last_mouse_press_frame = Qnil;
10850 goto OTHER;
10853 #ifdef USE_MOTIF /* This should do not harm for Lucid,
10854 but I am trying to be cautious. */
10855 else if (event.type == ButtonRelease)
10857 if (!NILP (last_mouse_press_frame))
10859 f = XFRAME (last_mouse_press_frame);
10860 if (f->output_data.x)
10861 SET_SAVED_BUTTON_EVENT;
10863 else
10864 goto OTHER;
10866 #endif /* USE_MOTIF */
10867 else
10868 goto OTHER;
10869 #endif /* USE_X_TOOLKIT */
10871 break;
10873 case CirculateNotify:
10874 goto OTHER;
10876 case CirculateRequest:
10877 goto OTHER;
10879 case VisibilityNotify:
10880 goto OTHER;
10882 case MappingNotify:
10883 /* Someone has changed the keyboard mapping - update the
10884 local cache. */
10885 switch (event.xmapping.request)
10887 case MappingModifier:
10888 x_find_modifier_meanings (dpyinfo);
10889 /* This is meant to fall through. */
10890 case MappingKeyboard:
10891 XRefreshKeyboardMapping (&event.xmapping);
10893 goto OTHER;
10895 default:
10896 OTHER:
10897 #ifdef USE_X_TOOLKIT
10898 BLOCK_INPUT;
10899 XtDispatchEvent (&event);
10900 UNBLOCK_INPUT;
10901 #endif /* USE_X_TOOLKIT */
10902 break;
10907 out:;
10909 /* On some systems, an X bug causes Emacs to get no more events
10910 when the window is destroyed. Detect that. (1994.) */
10911 if (! event_found)
10913 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10914 One XNOOP in 100 loops will make Emacs terminate.
10915 B. Bretthauer, 1994 */
10916 x_noop_count++;
10917 if (x_noop_count >= 100)
10919 x_noop_count=0;
10921 if (next_noop_dpyinfo == 0)
10922 next_noop_dpyinfo = x_display_list;
10924 XNoOp (next_noop_dpyinfo->display);
10926 /* Each time we get here, cycle through the displays now open. */
10927 next_noop_dpyinfo = next_noop_dpyinfo->next;
10931 /* If the focus was just given to an auto-raising frame,
10932 raise it now. */
10933 /* ??? This ought to be able to handle more than one such frame. */
10934 if (pending_autoraise_frame)
10936 x_raise_frame (pending_autoraise_frame);
10937 pending_autoraise_frame = 0;
10940 UNBLOCK_INPUT;
10941 --handling_signal;
10942 return count;
10948 /***********************************************************************
10949 Text Cursor
10950 ***********************************************************************/
10952 /* Note if the text cursor of window W has been overwritten by a
10953 drawing operation that outputs N glyphs starting at HPOS in the
10954 line given by output_cursor.vpos. N < 0 means all the rest of the
10955 line after HPOS has been written. */
10957 static void
10958 note_overwritten_text_cursor (w, hpos, n)
10959 struct window *w;
10960 int hpos, n;
10962 if (updated_area == TEXT_AREA
10963 && output_cursor.vpos == w->phys_cursor.vpos
10964 && hpos <= w->phys_cursor.hpos
10965 && (n < 0
10966 || hpos + n > w->phys_cursor.hpos))
10967 w->phys_cursor_on_p = 0;
10971 /* Set clipping for output in glyph row ROW. W is the window in which
10972 we operate. GC is the graphics context to set clipping in.
10973 WHOLE_LINE_P non-zero means include the areas used for truncation
10974 mark display and alike in the clipping rectangle.
10976 ROW may be a text row or, e.g., a mode line. Text rows must be
10977 clipped to the interior of the window dedicated to text display,
10978 mode lines must be clipped to the whole window. */
10980 static void
10981 x_clip_to_row (w, row, gc, whole_line_p)
10982 struct window *w;
10983 struct glyph_row *row;
10984 GC gc;
10985 int whole_line_p;
10987 struct frame *f = XFRAME (WINDOW_FRAME (w));
10988 XRectangle clip_rect;
10989 int window_x, window_y, window_width, window_height;
10991 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
10993 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10994 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10995 clip_rect.y = max (clip_rect.y, window_y);
10996 clip_rect.width = window_width;
10997 clip_rect.height = row->visible_height;
10999 /* If clipping to the whole line, including trunc marks, extend
11000 the rectangle to the left and increase its width. */
11001 if (whole_line_p)
11003 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
11004 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
11007 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
11011 /* Draw a hollow box cursor on window W in glyph row ROW. */
11013 static void
11014 x_draw_hollow_cursor (w, row)
11015 struct window *w;
11016 struct glyph_row *row;
11018 struct frame *f = XFRAME (WINDOW_FRAME (w));
11019 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11020 Display *dpy = FRAME_X_DISPLAY (f);
11021 int x, y, wd, h;
11022 XGCValues xgcv;
11023 struct glyph *cursor_glyph;
11024 GC gc;
11026 /* Compute frame-relative coordinates from window-relative
11027 coordinates. */
11028 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11029 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
11030 + row->ascent - w->phys_cursor_ascent);
11031 h = row->height - 1;
11033 /* Get the glyph the cursor is on. If we can't tell because
11034 the current matrix is invalid or such, give up. */
11035 cursor_glyph = get_phys_cursor_glyph (w);
11036 if (cursor_glyph == NULL)
11037 return;
11039 /* Compute the width of the rectangle to draw. If on a stretch
11040 glyph, and `x-stretch-block-cursor' is nil, don't draw a
11041 rectangle as wide as the glyph, but use a canonical character
11042 width instead. */
11043 wd = cursor_glyph->pixel_width - 1;
11044 if (cursor_glyph->type == STRETCH_GLYPH
11045 && !x_stretch_cursor_p)
11046 wd = min (CANON_X_UNIT (f), wd);
11048 /* The foreground of cursor_gc is typically the same as the normal
11049 background color, which can cause the cursor box to be invisible. */
11050 xgcv.foreground = f->output_data.x->cursor_pixel;
11051 if (dpyinfo->scratch_cursor_gc)
11052 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
11053 else
11054 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
11055 GCForeground, &xgcv);
11056 gc = dpyinfo->scratch_cursor_gc;
11058 /* Set clipping, draw the rectangle, and reset clipping again. */
11059 x_clip_to_row (w, row, gc, 0);
11060 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
11061 XSetClipMask (dpy, gc, None);
11065 /* Draw a bar cursor on window W in glyph row ROW.
11067 Implementation note: One would like to draw a bar cursor with an
11068 angle equal to the one given by the font property XA_ITALIC_ANGLE.
11069 Unfortunately, I didn't find a font yet that has this property set.
11070 --gerd. */
11072 static void
11073 x_draw_bar_cursor (w, row, width)
11074 struct window *w;
11075 struct glyph_row *row;
11076 int width;
11078 struct frame *f = XFRAME (w->frame);
11079 struct glyph *cursor_glyph;
11080 GC gc;
11081 int x;
11082 unsigned long mask;
11083 XGCValues xgcv;
11084 Display *dpy;
11085 Window window;
11087 /* If cursor is out of bounds, don't draw garbage. This can happen
11088 in mini-buffer windows when switching between echo area glyphs
11089 and mini-buffer. */
11090 cursor_glyph = get_phys_cursor_glyph (w);
11091 if (cursor_glyph == NULL)
11092 return;
11094 /* If on an image, draw like a normal cursor. That's usually better
11095 visible than drawing a bar, esp. if the image is large so that
11096 the bar might not be in the window. */
11097 if (cursor_glyph->type == IMAGE_GLYPH)
11099 struct glyph_row *row;
11100 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
11101 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
11103 else
11105 xgcv.background = f->output_data.x->cursor_pixel;
11106 xgcv.foreground = f->output_data.x->cursor_pixel;
11107 xgcv.graphics_exposures = 0;
11108 mask = GCForeground | GCBackground | GCGraphicsExposures;
11109 dpy = FRAME_X_DISPLAY (f);
11110 window = FRAME_X_WINDOW (f);
11111 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
11113 if (gc)
11114 XChangeGC (dpy, gc, mask, &xgcv);
11115 else
11117 gc = XCreateGC (dpy, window, mask, &xgcv);
11118 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
11121 if (width < 0)
11122 width = f->output_data.x->cursor_width;
11124 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11125 x_clip_to_row (w, row, gc, 0);
11126 XFillRectangle (dpy, window, gc,
11128 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
11129 min (cursor_glyph->pixel_width, width),
11130 row->height);
11131 XSetClipMask (dpy, gc, None);
11136 /* Clear the cursor of window W to background color, and mark the
11137 cursor as not shown. This is used when the text where the cursor
11138 is is about to be rewritten. */
11140 static void
11141 x_clear_cursor (w)
11142 struct window *w;
11144 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
11145 x_update_window_cursor (w, 0);
11149 /* Draw the cursor glyph of window W in glyph row ROW. See the
11150 comment of x_draw_glyphs for the meaning of HL. */
11152 static void
11153 x_draw_phys_cursor_glyph (w, row, hl)
11154 struct window *w;
11155 struct glyph_row *row;
11156 enum draw_glyphs_face hl;
11158 /* If cursor hpos is out of bounds, don't draw garbage. This can
11159 happen in mini-buffer windows when switching between echo area
11160 glyphs and mini-buffer. */
11161 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
11163 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
11164 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
11165 hl, 0, 0, 0);
11167 /* When we erase the cursor, and ROW is overlapped by other
11168 rows, make sure that these overlapping parts of other rows
11169 are redrawn. */
11170 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
11172 if (row > w->current_matrix->rows
11173 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
11174 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
11176 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
11177 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
11178 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
11184 /* Erase the image of a cursor of window W from the screen. */
11186 static void
11187 x_erase_phys_cursor (w)
11188 struct window *w;
11190 struct frame *f = XFRAME (w->frame);
11191 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11192 int hpos = w->phys_cursor.hpos;
11193 int vpos = w->phys_cursor.vpos;
11194 int mouse_face_here_p = 0;
11195 struct glyph_matrix *active_glyphs = w->current_matrix;
11196 struct glyph_row *cursor_row;
11197 struct glyph *cursor_glyph;
11198 enum draw_glyphs_face hl;
11200 /* No cursor displayed or row invalidated => nothing to do on the
11201 screen. */
11202 if (w->phys_cursor_type == NO_CURSOR)
11203 goto mark_cursor_off;
11205 /* VPOS >= active_glyphs->nrows means that window has been resized.
11206 Don't bother to erase the cursor. */
11207 if (vpos >= active_glyphs->nrows)
11208 goto mark_cursor_off;
11210 /* If row containing cursor is marked invalid, there is nothing we
11211 can do. */
11212 cursor_row = MATRIX_ROW (active_glyphs, vpos);
11213 if (!cursor_row->enabled_p)
11214 goto mark_cursor_off;
11216 /* This can happen when the new row is shorter than the old one.
11217 In this case, either x_draw_glyphs or clear_end_of_line
11218 should have cleared the cursor. Note that we wouldn't be
11219 able to erase the cursor in this case because we don't have a
11220 cursor glyph at hand. */
11221 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
11222 goto mark_cursor_off;
11224 /* If the cursor is in the mouse face area, redisplay that when
11225 we clear the cursor. */
11226 if (! NILP (dpyinfo->mouse_face_window)
11227 && w == XWINDOW (dpyinfo->mouse_face_window)
11228 && (vpos > dpyinfo->mouse_face_beg_row
11229 || (vpos == dpyinfo->mouse_face_beg_row
11230 && hpos >= dpyinfo->mouse_face_beg_col))
11231 && (vpos < dpyinfo->mouse_face_end_row
11232 || (vpos == dpyinfo->mouse_face_end_row
11233 && hpos < dpyinfo->mouse_face_end_col))
11234 /* Don't redraw the cursor's spot in mouse face if it is at the
11235 end of a line (on a newline). The cursor appears there, but
11236 mouse highlighting does not. */
11237 && cursor_row->used[TEXT_AREA] > hpos)
11238 mouse_face_here_p = 1;
11240 /* Maybe clear the display under the cursor. */
11241 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
11243 int x;
11244 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11246 cursor_glyph = get_phys_cursor_glyph (w);
11247 if (cursor_glyph == NULL)
11248 goto mark_cursor_off;
11250 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11252 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11254 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
11255 cursor_row->y)),
11256 cursor_glyph->pixel_width,
11257 cursor_row->visible_height,
11258 False);
11261 /* Erase the cursor by redrawing the character underneath it. */
11262 if (mouse_face_here_p)
11263 hl = DRAW_MOUSE_FACE;
11264 else if (cursor_row->inverse_p)
11265 hl = DRAW_INVERSE_VIDEO;
11266 else
11267 hl = DRAW_NORMAL_TEXT;
11268 x_draw_phys_cursor_glyph (w, cursor_row, hl);
11270 mark_cursor_off:
11271 w->phys_cursor_on_p = 0;
11272 w->phys_cursor_type = NO_CURSOR;
11276 /* Non-zero if physical cursor of window W is within mouse face. */
11278 static int
11279 cursor_in_mouse_face_p (w)
11280 struct window *w;
11282 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
11283 int in_mouse_face = 0;
11285 if (WINDOWP (dpyinfo->mouse_face_window)
11286 && XWINDOW (dpyinfo->mouse_face_window) == w)
11288 int hpos = w->phys_cursor.hpos;
11289 int vpos = w->phys_cursor.vpos;
11291 if (vpos >= dpyinfo->mouse_face_beg_row
11292 && vpos <= dpyinfo->mouse_face_end_row
11293 && (vpos > dpyinfo->mouse_face_beg_row
11294 || hpos >= dpyinfo->mouse_face_beg_col)
11295 && (vpos < dpyinfo->mouse_face_end_row
11296 || hpos < dpyinfo->mouse_face_end_col
11297 || dpyinfo->mouse_face_past_end))
11298 in_mouse_face = 1;
11301 return in_mouse_face;
11305 /* Display or clear cursor of window W. If ON is zero, clear the
11306 cursor. If it is non-zero, display the cursor. If ON is nonzero,
11307 where to put the cursor is specified by HPOS, VPOS, X and Y. */
11309 void
11310 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
11311 struct window *w;
11312 int on, hpos, vpos, x, y;
11314 struct frame *f = XFRAME (w->frame);
11315 int new_cursor_type;
11316 int new_cursor_width;
11317 struct glyph_matrix *current_glyphs;
11318 struct glyph_row *glyph_row;
11319 struct glyph *glyph;
11321 /* This is pointless on invisible frames, and dangerous on garbaged
11322 windows and frames; in the latter case, the frame or window may
11323 be in the midst of changing its size, and x and y may be off the
11324 window. */
11325 if (! FRAME_VISIBLE_P (f)
11326 || FRAME_GARBAGED_P (f)
11327 || vpos >= w->current_matrix->nrows
11328 || hpos >= w->current_matrix->matrix_w)
11329 return;
11331 /* If cursor is off and we want it off, return quickly. */
11332 if (!on && !w->phys_cursor_on_p)
11333 return;
11335 current_glyphs = w->current_matrix;
11336 glyph_row = MATRIX_ROW (current_glyphs, vpos);
11337 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
11339 /* If cursor row is not enabled, we don't really know where to
11340 display the cursor. */
11341 if (!glyph_row->enabled_p)
11343 w->phys_cursor_on_p = 0;
11344 return;
11347 xassert (interrupt_input_blocked);
11349 /* Set new_cursor_type to the cursor we want to be displayed. In a
11350 mini-buffer window, we want the cursor only to appear if we are
11351 reading input from this window. For the selected window, we want
11352 the cursor type given by the frame parameter. If explicitly
11353 marked off, draw no cursor. In all other cases, we want a hollow
11354 box cursor. */
11355 new_cursor_width = -1;
11356 if (cursor_in_echo_area
11357 && FRAME_HAS_MINIBUF_P (f)
11358 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
11360 if (w == XWINDOW (echo_area_window))
11361 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11362 else
11363 new_cursor_type = HOLLOW_BOX_CURSOR;
11365 else
11367 if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
11368 || w != XWINDOW (f->selected_window))
11370 extern int cursor_in_non_selected_windows;
11372 if (MINI_WINDOW_P (w)
11373 || !cursor_in_non_selected_windows
11374 || NILP (XBUFFER (w->buffer)->cursor_type))
11375 new_cursor_type = NO_CURSOR;
11376 else
11377 new_cursor_type = HOLLOW_BOX_CURSOR;
11379 else if (w->cursor_off_p)
11380 new_cursor_type = NO_CURSOR;
11381 else
11383 struct buffer *b = XBUFFER (w->buffer);
11385 if (EQ (b->cursor_type, Qt))
11386 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11387 else
11388 new_cursor_type = x_specified_cursor_type (b->cursor_type,
11389 &new_cursor_width);
11393 /* If cursor is currently being shown and we don't want it to be or
11394 it is in the wrong place, or the cursor type is not what we want,
11395 erase it. */
11396 if (w->phys_cursor_on_p
11397 && (!on
11398 || w->phys_cursor.x != x
11399 || w->phys_cursor.y != y
11400 || new_cursor_type != w->phys_cursor_type))
11401 x_erase_phys_cursor (w);
11403 /* If the cursor is now invisible and we want it to be visible,
11404 display it. */
11405 if (on && !w->phys_cursor_on_p)
11407 w->phys_cursor_ascent = glyph_row->ascent;
11408 w->phys_cursor_height = glyph_row->height;
11410 /* Set phys_cursor_.* before x_draw_.* is called because some
11411 of them may need the information. */
11412 w->phys_cursor.x = x;
11413 w->phys_cursor.y = glyph_row->y;
11414 w->phys_cursor.hpos = hpos;
11415 w->phys_cursor.vpos = vpos;
11416 w->phys_cursor_type = new_cursor_type;
11417 w->phys_cursor_on_p = 1;
11419 switch (new_cursor_type)
11421 case HOLLOW_BOX_CURSOR:
11422 x_draw_hollow_cursor (w, glyph_row);
11423 break;
11425 case FILLED_BOX_CURSOR:
11426 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
11427 break;
11429 case BAR_CURSOR:
11430 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
11431 break;
11433 case NO_CURSOR:
11434 break;
11436 default:
11437 abort ();
11440 #ifdef HAVE_X_I18N
11441 if (w == XWINDOW (f->selected_window))
11442 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
11443 xic_set_preeditarea (w, x, y);
11444 #endif
11447 #ifndef XFlush
11448 if (updating_frame != f)
11449 XFlush (FRAME_X_DISPLAY (f));
11450 #endif
11454 /* Display the cursor on window W, or clear it. X and Y are window
11455 relative pixel coordinates. HPOS and VPOS are glyph matrix
11456 positions. If W is not the selected window, display a hollow
11457 cursor. ON non-zero means display the cursor at X, Y which
11458 correspond to HPOS, VPOS, otherwise it is cleared. */
11460 void
11461 x_display_cursor (w, on, hpos, vpos, x, y)
11462 struct window *w;
11463 int on, hpos, vpos, x, y;
11465 BLOCK_INPUT;
11466 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
11467 UNBLOCK_INPUT;
11471 /* Display the cursor on window W, or clear it, according to ON_P.
11472 Don't change the cursor's position. */
11474 void
11475 x_update_cursor (f, on_p)
11476 struct frame *f;
11478 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11482 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
11483 in the window tree rooted at W. */
11485 static void
11486 x_update_cursor_in_window_tree (w, on_p)
11487 struct window *w;
11488 int on_p;
11490 while (w)
11492 if (!NILP (w->hchild))
11493 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11494 else if (!NILP (w->vchild))
11495 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11496 else
11497 x_update_window_cursor (w, on_p);
11499 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11504 /* Switch the display of W's cursor on or off, according to the value
11505 of ON. */
11507 static void
11508 x_update_window_cursor (w, on)
11509 struct window *w;
11510 int on;
11512 /* Don't update cursor in windows whose frame is in the process
11513 of being deleted. */
11514 if (w->current_matrix)
11516 BLOCK_INPUT;
11517 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11518 w->phys_cursor.x, w->phys_cursor.y);
11519 UNBLOCK_INPUT;
11526 /* Icons. */
11528 /* Refresh bitmap kitchen sink icon for frame F
11529 when we get an expose event for it. */
11531 void
11532 refreshicon (f)
11533 struct frame *f;
11535 /* Normally, the window manager handles this function. */
11538 /* Make the x-window of frame F use the gnu icon bitmap. */
11541 x_bitmap_icon (f, file)
11542 struct frame *f;
11543 Lisp_Object file;
11545 int bitmap_id;
11547 if (FRAME_X_WINDOW (f) == 0)
11548 return 1;
11550 /* Free up our existing icon bitmap if any. */
11551 if (f->output_data.x->icon_bitmap > 0)
11552 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11553 f->output_data.x->icon_bitmap = 0;
11555 if (STRINGP (file))
11556 bitmap_id = x_create_bitmap_from_file (f, file);
11557 else
11559 /* Create the GNU bitmap if necessary. */
11560 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
11561 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11562 = x_create_bitmap_from_data (f, gnu_bits,
11563 gnu_width, gnu_height);
11565 /* The first time we create the GNU bitmap,
11566 this increments the ref-count one extra time.
11567 As a result, the GNU bitmap is never freed.
11568 That way, we don't have to worry about allocating it again. */
11569 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
11571 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
11574 x_wm_set_icon_pixmap (f, bitmap_id);
11575 f->output_data.x->icon_bitmap = bitmap_id;
11577 return 0;
11581 /* Make the x-window of frame F use a rectangle with text.
11582 Use ICON_NAME as the text. */
11585 x_text_icon (f, icon_name)
11586 struct frame *f;
11587 char *icon_name;
11589 if (FRAME_X_WINDOW (f) == 0)
11590 return 1;
11592 #ifdef HAVE_X11R4
11594 XTextProperty text;
11595 text.value = (unsigned char *) icon_name;
11596 text.encoding = XA_STRING;
11597 text.format = 8;
11598 text.nitems = strlen (icon_name);
11599 #ifdef USE_X_TOOLKIT
11600 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11601 &text);
11602 #else /* not USE_X_TOOLKIT */
11603 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11604 #endif /* not USE_X_TOOLKIT */
11606 #else /* not HAVE_X11R4 */
11607 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11608 #endif /* not HAVE_X11R4 */
11610 if (f->output_data.x->icon_bitmap > 0)
11611 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11612 f->output_data.x->icon_bitmap = 0;
11613 x_wm_set_icon_pixmap (f, 0);
11615 return 0;
11618 #define X_ERROR_MESSAGE_SIZE 200
11620 /* If non-nil, this should be a string.
11621 It means catch X errors and store the error message in this string. */
11623 static Lisp_Object x_error_message_string;
11625 /* An X error handler which stores the error message in
11626 x_error_message_string. This is called from x_error_handler if
11627 x_catch_errors is in effect. */
11629 static void
11630 x_error_catcher (display, error)
11631 Display *display;
11632 XErrorEvent *error;
11634 XGetErrorText (display, error->error_code,
11635 XSTRING (x_error_message_string)->data,
11636 X_ERROR_MESSAGE_SIZE);
11639 /* Begin trapping X errors for display DPY. Actually we trap X errors
11640 for all displays, but DPY should be the display you are actually
11641 operating on.
11643 After calling this function, X protocol errors no longer cause
11644 Emacs to exit; instead, they are recorded in the string
11645 stored in x_error_message_string.
11647 Calling x_check_errors signals an Emacs error if an X error has
11648 occurred since the last call to x_catch_errors or x_check_errors.
11650 Calling x_uncatch_errors resumes the normal error handling. */
11652 void x_check_errors ();
11653 static Lisp_Object x_catch_errors_unwind ();
11656 x_catch_errors (dpy)
11657 Display *dpy;
11659 int count = specpdl_ptr - specpdl;
11661 /* Make sure any errors from previous requests have been dealt with. */
11662 XSync (dpy, False);
11664 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11666 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11667 XSTRING (x_error_message_string)->data[0] = 0;
11669 return count;
11672 /* Unbind the binding that we made to check for X errors. */
11674 static Lisp_Object
11675 x_catch_errors_unwind (old_val)
11676 Lisp_Object old_val;
11678 x_error_message_string = old_val;
11679 return Qnil;
11682 /* If any X protocol errors have arrived since the last call to
11683 x_catch_errors or x_check_errors, signal an Emacs error using
11684 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11686 void
11687 x_check_errors (dpy, format)
11688 Display *dpy;
11689 char *format;
11691 /* Make sure to catch any errors incurred so far. */
11692 XSync (dpy, False);
11694 if (XSTRING (x_error_message_string)->data[0])
11695 error (format, XSTRING (x_error_message_string)->data);
11698 /* Nonzero if we had any X protocol errors
11699 since we did x_catch_errors on DPY. */
11702 x_had_errors_p (dpy)
11703 Display *dpy;
11705 /* Make sure to catch any errors incurred so far. */
11706 XSync (dpy, False);
11708 return XSTRING (x_error_message_string)->data[0] != 0;
11711 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11713 void
11714 x_clear_errors (dpy)
11715 Display *dpy;
11717 XSTRING (x_error_message_string)->data[0] = 0;
11720 /* Stop catching X protocol errors and let them make Emacs die.
11721 DPY should be the display that was passed to x_catch_errors.
11722 COUNT should be the value that was returned by
11723 the corresponding call to x_catch_errors. */
11725 void
11726 x_uncatch_errors (dpy, count)
11727 Display *dpy;
11728 int count;
11730 unbind_to (count, Qnil);
11733 #if 0
11734 static unsigned int x_wire_count;
11735 x_trace_wire ()
11737 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11739 #endif /* ! 0 */
11742 /* Handle SIGPIPE, which can happen when the connection to a server
11743 simply goes away. SIGPIPE is handled by x_connection_signal.
11744 Don't need to do anything, because the write which caused the
11745 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
11746 which will do the appropriate cleanup for us. */
11748 static SIGTYPE
11749 x_connection_signal (signalnum) /* If we don't have an argument, */
11750 int signalnum; /* some compilers complain in signal calls. */
11752 #ifdef USG
11753 /* USG systems forget handlers when they are used;
11754 must reestablish each time */
11755 signal (signalnum, x_connection_signal);
11756 #endif /* USG */
11760 /************************************************************************
11761 Handling X errors
11762 ************************************************************************/
11764 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
11765 the text of an error message that lead to the connection loss. */
11767 static SIGTYPE
11768 x_connection_closed (dpy, error_message)
11769 Display *dpy;
11770 char *error_message;
11772 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
11773 Lisp_Object frame, tail;
11774 int count;
11775 char *msg;
11777 msg = (char *) alloca (strlen (error_message) + 1);
11778 strcpy (msg, error_message);
11779 handling_signal = 0;
11781 /* Prevent being called recursively because of an error condition
11782 below. Otherwise, we might end up with printing ``can't find per
11783 display information'' in the recursive call instead of printing
11784 the original message here. */
11785 count = x_catch_errors (dpy);
11787 /* We have to close the display to inform Xt that it doesn't
11788 exist anymore. If we don't, Xt will continue to wait for
11789 events from the display. As a consequence, a sequence of
11791 M-x make-frame-on-display RET :1 RET
11792 ...kill the new frame, so that we get an IO error...
11793 M-x make-frame-on-display RET :1 RET
11795 will indefinitely wait in Xt for events for display `:1', opened
11796 in the first class to make-frame-on-display.
11798 Closing the display is reported to lead to a bus error on
11799 OpenWindows in certain situations. I suspect that is a bug
11800 in OpenWindows. I don't know how to cicumvent it here. */
11802 #ifdef USE_X_TOOLKIT
11803 /* If DPYINFO is null, this means we didn't open the display
11804 in the first place, so don't try to close it. */
11805 if (dpyinfo)
11806 XtCloseDisplay (dpy);
11807 #endif
11809 /* Indicate that this display is dead. */
11810 if (dpyinfo)
11811 dpyinfo->display = 0;
11813 /* First delete frames whose mini-buffers are on frames
11814 that are on the dead display. */
11815 FOR_EACH_FRAME (tail, frame)
11817 Lisp_Object minibuf_frame;
11818 minibuf_frame
11819 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
11820 if (FRAME_X_P (XFRAME (frame))
11821 && FRAME_X_P (XFRAME (minibuf_frame))
11822 && ! EQ (frame, minibuf_frame)
11823 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
11824 Fdelete_frame (frame, Qt);
11827 /* Now delete all remaining frames on the dead display.
11828 We are now sure none of these is used as the mini-buffer
11829 for another frame that we need to delete. */
11830 FOR_EACH_FRAME (tail, frame)
11831 if (FRAME_X_P (XFRAME (frame))
11832 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
11834 /* Set this to t so that Fdelete_frame won't get confused
11835 trying to find a replacement. */
11836 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11837 Fdelete_frame (frame, Qt);
11840 if (dpyinfo)
11841 x_delete_display (dpyinfo);
11843 x_uncatch_errors (dpy, count);
11845 if (x_display_list == 0)
11847 fprintf (stderr, "%s\n", msg);
11848 shut_down_emacs (0, 0, Qnil);
11849 exit (70);
11852 /* Ordinary stack unwind doesn't deal with these. */
11853 #ifdef SIGIO
11854 sigunblock (sigmask (SIGIO));
11855 #endif
11856 sigunblock (sigmask (SIGALRM));
11857 TOTALLY_UNBLOCK_INPUT;
11859 clear_waiting_for_input ();
11860 error ("%s", msg);
11864 /* This is the usual handler for X protocol errors.
11865 It kills all frames on the display that we got the error for.
11866 If that was the only one, it prints an error message and kills Emacs. */
11868 static void
11869 x_error_quitter (display, error)
11870 Display *display;
11871 XErrorEvent *error;
11873 char buf[256], buf1[356];
11875 /* Note that there is no real way portable across R3/R4 to get the
11876 original error handler. */
11878 XGetErrorText (display, error->error_code, buf, sizeof (buf));
11879 sprintf (buf1, "X protocol error: %s on protocol request %d",
11880 buf, error->request_code);
11881 x_connection_closed (display, buf1);
11885 /* This is the first-level handler for X protocol errors.
11886 It calls x_error_quitter or x_error_catcher. */
11888 static int
11889 x_error_handler (display, error)
11890 Display *display;
11891 XErrorEvent *error;
11893 if (! NILP (x_error_message_string))
11894 x_error_catcher (display, error);
11895 else
11896 x_error_quitter (display, error);
11897 return 0;
11900 /* This is the handler for X IO errors, always.
11901 It kills all frames on the display that we lost touch with.
11902 If that was the only one, it prints an error message and kills Emacs. */
11904 static int
11905 x_io_error_quitter (display)
11906 Display *display;
11908 char buf[256];
11910 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11911 x_connection_closed (display, buf);
11912 return 0;
11915 /* Changing the font of the frame. */
11917 /* Give frame F the font named FONTNAME as its default font, and
11918 return the full name of that font. FONTNAME may be a wildcard
11919 pattern; in that case, we choose some font that fits the pattern.
11920 The return value shows which font we chose. */
11922 Lisp_Object
11923 x_new_font (f, fontname)
11924 struct frame *f;
11925 register char *fontname;
11927 struct font_info *fontp
11928 = FS_LOAD_FONT (f, 0, fontname, -1);
11930 if (!fontp)
11931 return Qnil;
11933 f->output_data.x->font = (XFontStruct *) (fontp->font);
11934 f->output_data.x->baseline_offset = fontp->baseline_offset;
11935 f->output_data.x->fontset = -1;
11937 /* Compute the scroll bar width in character columns. */
11938 if (f->scroll_bar_pixel_width > 0)
11940 int wid = FONT_WIDTH (f->output_data.x->font);
11941 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11943 else
11945 int wid = FONT_WIDTH (f->output_data.x->font);
11946 f->scroll_bar_cols = (14 + wid - 1) / wid;
11949 /* Now make the frame display the given font. */
11950 if (FRAME_X_WINDOW (f) != 0)
11952 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11953 f->output_data.x->font->fid);
11954 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11955 f->output_data.x->font->fid);
11956 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11957 f->output_data.x->font->fid);
11959 frame_update_line_height (f);
11961 /* Don't change the size of a tip frame; there's no point in
11962 doing it because it's done in Fx_show_tip, and it leads to
11963 problems because the tip frame has no widget. */
11964 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
11965 x_set_window_size (f, 0, f->width, f->height);
11967 else
11968 /* If we are setting a new frame's font for the first time,
11969 there are no faces yet, so this font's height is the line height. */
11970 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
11972 return build_string (fontp->full_name);
11975 /* Give frame F the fontset named FONTSETNAME as its default font, and
11976 return the full name of that fontset. FONTSETNAME may be a wildcard
11977 pattern; in that case, we choose some fontset that fits the pattern.
11978 The return value shows which fontset we chose. */
11980 Lisp_Object
11981 x_new_fontset (f, fontsetname)
11982 struct frame *f;
11983 char *fontsetname;
11985 int fontset = fs_query_fontset (build_string (fontsetname), 0);
11986 Lisp_Object result;
11988 if (fontset < 0)
11989 return Qnil;
11991 if (f->output_data.x->fontset == fontset)
11992 /* This fontset is already set in frame F. There's nothing more
11993 to do. */
11994 return fontset_name (fontset);
11996 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
11998 if (!STRINGP (result))
11999 /* Can't load ASCII font. */
12000 return Qnil;
12002 /* Since x_new_font doesn't update any fontset information, do it now. */
12003 f->output_data.x->fontset = fontset;
12005 #ifdef HAVE_X_I18N
12006 if (FRAME_XIC (f)
12007 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
12008 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
12009 #endif
12011 return build_string (fontsetname);
12015 /***********************************************************************
12016 X Input Methods
12017 ***********************************************************************/
12019 #ifdef HAVE_X_I18N
12021 #ifdef HAVE_X11R6
12023 /* XIM destroy callback function, which is called whenever the
12024 connection to input method XIM dies. CLIENT_DATA contains a
12025 pointer to the x_display_info structure corresponding to XIM. */
12027 static void
12028 xim_destroy_callback (xim, client_data, call_data)
12029 XIM xim;
12030 XPointer client_data;
12031 XPointer call_data;
12033 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
12034 Lisp_Object frame, tail;
12036 BLOCK_INPUT;
12038 /* No need to call XDestroyIC.. */
12039 FOR_EACH_FRAME (tail, frame)
12041 struct frame *f = XFRAME (frame);
12042 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
12044 FRAME_XIC (f) = NULL;
12045 if (FRAME_XIC_FONTSET (f))
12047 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
12048 FRAME_XIC_FONTSET (f) = NULL;
12053 /* No need to call XCloseIM. */
12054 dpyinfo->xim = NULL;
12055 XFree (dpyinfo->xim_styles);
12056 UNBLOCK_INPUT;
12059 #endif /* HAVE_X11R6 */
12061 /* Open the connection to the XIM server on display DPYINFO.
12062 RESOURCE_NAME is the resource name Emacs uses. */
12064 static void
12065 xim_open_dpy (dpyinfo, resource_name)
12066 struct x_display_info *dpyinfo;
12067 char *resource_name;
12069 #ifdef USE_XIM
12070 XIM xim;
12072 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
12073 dpyinfo->xim = xim;
12075 if (xim)
12077 #ifdef HAVE_X11R6
12078 XIMCallback destroy;
12079 #endif
12081 /* Get supported styles and XIM values. */
12082 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
12084 #ifdef HAVE_X11R6
12085 destroy.callback = xim_destroy_callback;
12086 destroy.client_data = (XPointer)dpyinfo;
12087 /* This isn't prototyped in OSF 5.0. */
12088 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
12089 #endif
12092 #else /* not USE_XIM */
12093 dpyinfo->xim = NULL;
12094 #endif /* not USE_XIM */
12098 #ifdef HAVE_X11R6_XIM
12100 struct xim_inst_t
12102 struct x_display_info *dpyinfo;
12103 char *resource_name;
12106 /* XIM instantiate callback function, which is called whenever an XIM
12107 server is available. DISPLAY is teh display of the XIM.
12108 CLIENT_DATA contains a pointer to an xim_inst_t structure created
12109 when the callback was registered. */
12111 static void
12112 xim_instantiate_callback (display, client_data, call_data)
12113 Display *display;
12114 XPointer client_data;
12115 XPointer call_data;
12117 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
12118 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
12120 /* We don't support multiple XIM connections. */
12121 if (dpyinfo->xim)
12122 return;
12124 xim_open_dpy (dpyinfo, xim_inst->resource_name);
12126 /* Create XIC for the existing frames on the same display, as long
12127 as they have no XIC. */
12128 if (dpyinfo->xim && dpyinfo->reference_count > 0)
12130 Lisp_Object tail, frame;
12132 BLOCK_INPUT;
12133 FOR_EACH_FRAME (tail, frame)
12135 struct frame *f = XFRAME (frame);
12137 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
12138 if (FRAME_XIC (f) == NULL)
12140 create_frame_xic (f);
12141 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
12142 xic_set_statusarea (f);
12143 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
12145 struct window *w = XWINDOW (f->selected_window);
12146 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
12151 UNBLOCK_INPUT;
12155 #endif /* HAVE_X11R6_XIM */
12158 /* Open a connection to the XIM server on display DPYINFO.
12159 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
12160 connection only at the first time. On X11R6, open the connection
12161 in the XIM instantiate callback function. */
12163 static void
12164 xim_initialize (dpyinfo, resource_name)
12165 struct x_display_info *dpyinfo;
12166 char *resource_name;
12168 #ifdef USE_XIM
12169 #ifdef HAVE_X11R6_XIM
12170 struct xim_inst_t *xim_inst;
12171 int len;
12173 dpyinfo->xim = NULL;
12174 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
12175 xim_inst->dpyinfo = dpyinfo;
12176 len = strlen (resource_name);
12177 xim_inst->resource_name = (char *) xmalloc (len + 1);
12178 bcopy (resource_name, xim_inst->resource_name, len + 1);
12179 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12180 resource_name, EMACS_CLASS,
12181 xim_instantiate_callback,
12182 /* Fixme: This is XPointer in
12183 XFree86 but (XPointer *) on
12184 Tru64, at least. */
12185 (XPointer) xim_inst);
12186 #else /* not HAVE_X11R6_XIM */
12187 dpyinfo->xim = NULL;
12188 xim_open_dpy (dpyinfo, resource_name);
12189 #endif /* not HAVE_X11R6_XIM */
12191 #else /* not USE_XIM */
12192 dpyinfo->xim = NULL;
12193 #endif /* not USE_XIM */
12197 /* Close the connection to the XIM server on display DPYINFO. */
12199 static void
12200 xim_close_dpy (dpyinfo)
12201 struct x_display_info *dpyinfo;
12203 #ifdef USE_XIM
12204 #ifdef HAVE_X11R6_XIM
12205 if (dpyinfo->display)
12206 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12207 NULL, EMACS_CLASS,
12208 xim_instantiate_callback, NULL);
12209 #endif /* not HAVE_X11R6_XIM */
12210 if (dpyinfo->display)
12211 XCloseIM (dpyinfo->xim);
12212 dpyinfo->xim = NULL;
12213 XFree (dpyinfo->xim_styles);
12214 #endif /* USE_XIM */
12217 #endif /* not HAVE_X11R6_XIM */
12221 /* Calculate the absolute position in frame F
12222 from its current recorded position values and gravity. */
12224 void
12225 x_calc_absolute_position (f)
12226 struct frame *f;
12228 Window child;
12229 int win_x = 0, win_y = 0;
12230 int flags = f->output_data.x->size_hint_flags;
12231 int this_window;
12233 /* We have nothing to do if the current position
12234 is already for the top-left corner. */
12235 if (! ((flags & XNegative) || (flags & YNegative)))
12236 return;
12238 #ifdef USE_X_TOOLKIT
12239 this_window = XtWindow (f->output_data.x->widget);
12240 #else
12241 this_window = FRAME_X_WINDOW (f);
12242 #endif
12244 /* Find the position of the outside upper-left corner of
12245 the inner window, with respect to the outer window.
12246 But do this only if we will need the results. */
12247 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
12249 int count;
12251 BLOCK_INPUT;
12252 count = x_catch_errors (FRAME_X_DISPLAY (f));
12253 while (1)
12255 x_clear_errors (FRAME_X_DISPLAY (f));
12256 XTranslateCoordinates (FRAME_X_DISPLAY (f),
12258 /* From-window, to-window. */
12259 this_window,
12260 f->output_data.x->parent_desc,
12262 /* From-position, to-position. */
12263 0, 0, &win_x, &win_y,
12265 /* Child of win. */
12266 &child);
12267 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12269 Window newroot, newparent = 0xdeadbeef;
12270 Window *newchildren;
12271 unsigned int nchildren;
12273 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
12274 &newparent, &newchildren, &nchildren))
12275 break;
12277 XFree ((char *) newchildren);
12279 f->output_data.x->parent_desc = newparent;
12281 else
12282 break;
12285 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12286 UNBLOCK_INPUT;
12289 /* Treat negative positions as relative to the leftmost bottommost
12290 position that fits on the screen. */
12291 if (flags & XNegative)
12292 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
12293 - 2 * f->output_data.x->border_width - win_x
12294 - PIXEL_WIDTH (f)
12295 + f->output_data.x->left_pos);
12298 int height = PIXEL_HEIGHT (f);
12300 #if defined USE_X_TOOLKIT && defined USE_MOTIF
12301 /* Something is fishy here. When using Motif, starting Emacs with
12302 `-g -0-0', the frame appears too low by a few pixels.
12304 This seems to be so because initially, while Emacs is starting,
12305 the column widget's height and the frame's pixel height are
12306 different. The column widget's height is the right one. In
12307 later invocations, when Emacs is up, the frame's pixel height
12308 is right, though.
12310 It's not obvious where the initial small difference comes from.
12311 2000-12-01, gerd. */
12313 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
12314 #endif
12316 if (flags & YNegative)
12317 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
12318 - 2 * f->output_data.x->border_width
12319 - win_y
12320 - height
12321 + f->output_data.x->top_pos);
12324 /* The left_pos and top_pos
12325 are now relative to the top and left screen edges,
12326 so the flags should correspond. */
12327 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12330 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
12331 to really change the position, and 0 when calling from
12332 x_make_frame_visible (in that case, XOFF and YOFF are the current
12333 position values). It is -1 when calling from x_set_frame_parameters,
12334 which means, do adjust for borders but don't change the gravity. */
12336 void
12337 x_set_offset (f, xoff, yoff, change_gravity)
12338 struct frame *f;
12339 register int xoff, yoff;
12340 int change_gravity;
12342 int modified_top, modified_left;
12344 if (change_gravity > 0)
12346 f->output_data.x->top_pos = yoff;
12347 f->output_data.x->left_pos = xoff;
12348 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12349 if (xoff < 0)
12350 f->output_data.x->size_hint_flags |= XNegative;
12351 if (yoff < 0)
12352 f->output_data.x->size_hint_flags |= YNegative;
12353 f->output_data.x->win_gravity = NorthWestGravity;
12355 x_calc_absolute_position (f);
12357 BLOCK_INPUT;
12358 x_wm_set_size_hint (f, (long) 0, 0);
12360 modified_left = f->output_data.x->left_pos;
12361 modified_top = f->output_data.x->top_pos;
12362 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
12363 this seems to be unnecessary and incorrect. rms, 4/17/97. */
12364 /* It is a mystery why we need to add the border_width here
12365 when the frame is already visible, but experiment says we do. */
12366 if (change_gravity != 0)
12368 modified_left += f->output_data.x->border_width;
12369 modified_top += f->output_data.x->border_width;
12371 #endif
12373 #ifdef USE_X_TOOLKIT
12374 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
12375 modified_left, modified_top);
12376 #else /* not USE_X_TOOLKIT */
12377 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12378 modified_left, modified_top);
12379 #endif /* not USE_X_TOOLKIT */
12380 UNBLOCK_INPUT;
12384 /* Change the size of frame F's X window to COLS/ROWS in the case F
12385 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
12386 top-left-corner window gravity for this size change and subsequent
12387 size changes. Otherwise we leave the window gravity unchanged. */
12389 static void
12390 x_set_window_size_1 (f, change_gravity, cols, rows)
12391 struct frame *f;
12392 int change_gravity;
12393 int cols, rows;
12395 int pixelwidth, pixelheight;
12397 check_frame_size (f, &rows, &cols);
12398 f->output_data.x->vertical_scroll_bar_extra
12399 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
12401 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
12402 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
12403 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
12404 f->output_data.x->flags_areas_extra
12405 = FRAME_FLAGS_AREA_WIDTH (f);
12406 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
12407 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
12409 f->output_data.x->win_gravity = NorthWestGravity;
12410 x_wm_set_size_hint (f, (long) 0, 0);
12412 XSync (FRAME_X_DISPLAY (f), False);
12413 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12414 pixelwidth, pixelheight);
12416 /* Now, strictly speaking, we can't be sure that this is accurate,
12417 but the window manager will get around to dealing with the size
12418 change request eventually, and we'll hear how it went when the
12419 ConfigureNotify event gets here.
12421 We could just not bother storing any of this information here,
12422 and let the ConfigureNotify event set everything up, but that
12423 might be kind of confusing to the Lisp code, since size changes
12424 wouldn't be reported in the frame parameters until some random
12425 point in the future when the ConfigureNotify event arrives.
12427 We pass 1 for DELAY since we can't run Lisp code inside of
12428 a BLOCK_INPUT. */
12429 change_frame_size (f, rows, cols, 0, 1, 0);
12430 PIXEL_WIDTH (f) = pixelwidth;
12431 PIXEL_HEIGHT (f) = pixelheight;
12433 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
12434 receive in the ConfigureNotify event; if we get what we asked
12435 for, then the event won't cause the screen to become garbaged, so
12436 we have to make sure to do it here. */
12437 SET_FRAME_GARBAGED (f);
12439 XFlush (FRAME_X_DISPLAY (f));
12443 /* Call this to change the size of frame F's x-window.
12444 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
12445 for this size change and subsequent size changes.
12446 Otherwise we leave the window gravity unchanged. */
12448 void
12449 x_set_window_size (f, change_gravity, cols, rows)
12450 struct frame *f;
12451 int change_gravity;
12452 int cols, rows;
12454 BLOCK_INPUT;
12456 #ifdef USE_X_TOOLKIT
12458 if (f->output_data.x->widget != NULL)
12460 /* The x and y position of the widget is clobbered by the
12461 call to XtSetValues within EmacsFrameSetCharSize.
12462 This is a real kludge, but I don't understand Xt so I can't
12463 figure out a correct fix. Can anyone else tell me? -- rms. */
12464 int xpos = f->output_data.x->widget->core.x;
12465 int ypos = f->output_data.x->widget->core.y;
12466 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
12467 f->output_data.x->widget->core.x = xpos;
12468 f->output_data.x->widget->core.y = ypos;
12470 else
12471 x_set_window_size_1 (f, change_gravity, cols, rows);
12473 #else /* not USE_X_TOOLKIT */
12475 x_set_window_size_1 (f, change_gravity, cols, rows);
12477 #endif /* not USE_X_TOOLKIT */
12479 /* If cursor was outside the new size, mark it as off. */
12480 mark_window_cursors_off (XWINDOW (f->root_window));
12482 /* Clear out any recollection of where the mouse highlighting was,
12483 since it might be in a place that's outside the new frame size.
12484 Actually checking whether it is outside is a pain in the neck,
12485 so don't try--just let the highlighting be done afresh with new size. */
12486 cancel_mouse_face (f);
12488 UNBLOCK_INPUT;
12491 /* Mouse warping. */
12493 void
12494 x_set_mouse_position (f, x, y)
12495 struct frame *f;
12496 int x, y;
12498 int pix_x, pix_y;
12500 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
12501 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
12503 if (pix_x < 0) pix_x = 0;
12504 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
12506 if (pix_y < 0) pix_y = 0;
12507 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
12509 BLOCK_INPUT;
12511 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12512 0, 0, 0, 0, pix_x, pix_y);
12513 UNBLOCK_INPUT;
12516 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
12518 void
12519 x_set_mouse_pixel_position (f, pix_x, pix_y)
12520 struct frame *f;
12521 int pix_x, pix_y;
12523 BLOCK_INPUT;
12525 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12526 0, 0, 0, 0, pix_x, pix_y);
12527 UNBLOCK_INPUT;
12530 /* focus shifting, raising and lowering. */
12532 void
12533 x_focus_on_frame (f)
12534 struct frame *f;
12536 #if 0 /* This proves to be unpleasant. */
12537 x_raise_frame (f);
12538 #endif
12539 #if 0
12540 /* I don't think that the ICCCM allows programs to do things like this
12541 without the interaction of the window manager. Whatever you end up
12542 doing with this code, do it to x_unfocus_frame too. */
12543 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12544 RevertToPointerRoot, CurrentTime);
12545 #endif /* ! 0 */
12548 void
12549 x_unfocus_frame (f)
12550 struct frame *f;
12552 #if 0
12553 /* Look at the remarks in x_focus_on_frame. */
12554 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
12555 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
12556 RevertToPointerRoot, CurrentTime);
12557 #endif /* ! 0 */
12560 /* Raise frame F. */
12562 void
12563 x_raise_frame (f)
12564 struct frame *f;
12566 if (f->async_visible)
12568 BLOCK_INPUT;
12569 #ifdef USE_X_TOOLKIT
12570 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12571 #else /* not USE_X_TOOLKIT */
12572 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12573 #endif /* not USE_X_TOOLKIT */
12574 XFlush (FRAME_X_DISPLAY (f));
12575 UNBLOCK_INPUT;
12579 /* Lower frame F. */
12581 void
12582 x_lower_frame (f)
12583 struct frame *f;
12585 if (f->async_visible)
12587 BLOCK_INPUT;
12588 #ifdef USE_X_TOOLKIT
12589 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12590 #else /* not USE_X_TOOLKIT */
12591 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12592 #endif /* not USE_X_TOOLKIT */
12593 XFlush (FRAME_X_DISPLAY (f));
12594 UNBLOCK_INPUT;
12598 static void
12599 XTframe_raise_lower (f, raise_flag)
12600 FRAME_PTR f;
12601 int raise_flag;
12603 if (raise_flag)
12604 x_raise_frame (f);
12605 else
12606 x_lower_frame (f);
12609 /* Change of visibility. */
12611 /* This tries to wait until the frame is really visible.
12612 However, if the window manager asks the user where to position
12613 the frame, this will return before the user finishes doing that.
12614 The frame will not actually be visible at that time,
12615 but it will become visible later when the window manager
12616 finishes with it. */
12618 void
12619 x_make_frame_visible (f)
12620 struct frame *f;
12622 Lisp_Object type;
12623 int original_top, original_left;
12624 int retry_count = 2;
12626 retry:
12628 BLOCK_INPUT;
12630 type = x_icon_type (f);
12631 if (!NILP (type))
12632 x_bitmap_icon (f, type);
12634 if (! FRAME_VISIBLE_P (f))
12636 /* We test FRAME_GARBAGED_P here to make sure we don't
12637 call x_set_offset a second time
12638 if we get to x_make_frame_visible a second time
12639 before the window gets really visible. */
12640 if (! FRAME_ICONIFIED_P (f)
12641 && ! f->output_data.x->asked_for_visible)
12642 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12644 f->output_data.x->asked_for_visible = 1;
12646 if (! EQ (Vx_no_window_manager, Qt))
12647 x_wm_set_window_state (f, NormalState);
12648 #ifdef USE_X_TOOLKIT
12649 /* This was XtPopup, but that did nothing for an iconified frame. */
12650 XtMapWidget (f->output_data.x->widget);
12651 #else /* not USE_X_TOOLKIT */
12652 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12653 #endif /* not USE_X_TOOLKIT */
12654 #if 0 /* This seems to bring back scroll bars in the wrong places
12655 if the window configuration has changed. They seem
12656 to come back ok without this. */
12657 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
12658 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12659 #endif
12662 XFlush (FRAME_X_DISPLAY (f));
12664 /* Synchronize to ensure Emacs knows the frame is visible
12665 before we do anything else. We do this loop with input not blocked
12666 so that incoming events are handled. */
12668 Lisp_Object frame;
12669 int count;
12670 /* This must be before UNBLOCK_INPUT
12671 since events that arrive in response to the actions above
12672 will set it when they are handled. */
12673 int previously_visible = f->output_data.x->has_been_visible;
12675 original_left = f->output_data.x->left_pos;
12676 original_top = f->output_data.x->top_pos;
12678 /* This must come after we set COUNT. */
12679 UNBLOCK_INPUT;
12681 /* We unblock here so that arriving X events are processed. */
12683 /* Now move the window back to where it was "supposed to be".
12684 But don't do it if the gravity is negative.
12685 When the gravity is negative, this uses a position
12686 that is 3 pixels too low. Perhaps that's really the border width.
12688 Don't do this if the window has never been visible before,
12689 because the window manager may choose the position
12690 and we don't want to override it. */
12692 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
12693 && f->output_data.x->win_gravity == NorthWestGravity
12694 && previously_visible)
12696 Drawable rootw;
12697 int x, y;
12698 unsigned int width, height, border, depth;
12700 BLOCK_INPUT;
12702 /* On some window managers (such as FVWM) moving an existing
12703 window, even to the same place, causes the window manager
12704 to introduce an offset. This can cause the window to move
12705 to an unexpected location. Check the geometry (a little
12706 slow here) and then verify that the window is in the right
12707 place. If the window is not in the right place, move it
12708 there, and take the potential window manager hit. */
12709 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12710 &rootw, &x, &y, &width, &height, &border, &depth);
12712 if (original_left != x || original_top != y)
12713 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12714 original_left, original_top);
12716 UNBLOCK_INPUT;
12719 XSETFRAME (frame, f);
12721 /* Wait until the frame is visible. Process X events until a
12722 MapNotify event has been seen, or until we think we won't get a
12723 MapNotify at all.. */
12724 for (count = input_signal_count + 10;
12725 input_signal_count < count && !FRAME_VISIBLE_P (f);)
12727 /* Force processing of queued events. */
12728 x_sync (f);
12730 /* Machines that do polling rather than SIGIO have been
12731 observed to go into a busy-wait here. So we'll fake an
12732 alarm signal to let the handler know that there's something
12733 to be read. We used to raise a real alarm, but it seems
12734 that the handler isn't always enabled here. This is
12735 probably a bug. */
12736 if (input_polling_used ())
12738 /* It could be confusing if a real alarm arrives while
12739 processing the fake one. Turn it off and let the
12740 handler reset it. */
12741 extern void poll_for_input_1 P_ ((void));
12742 int old_poll_suppress_count = poll_suppress_count;
12743 poll_suppress_count = 1;
12744 poll_for_input_1 ();
12745 poll_suppress_count = old_poll_suppress_count;
12748 /* See if a MapNotify event has been processed. */
12749 FRAME_SAMPLE_VISIBILITY (f);
12752 /* 2000-09-28: In
12754 (let ((f (selected-frame)))
12755 (iconify-frame f)
12756 (raise-frame f))
12758 the frame is not raised with various window managers on
12759 FreeBSD, Linux and Solaris. It turns out that, for some
12760 unknown reason, the call to XtMapWidget is completely ignored.
12761 Mapping the widget a second time works. */
12763 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
12764 goto retry;
12768 /* Change from mapped state to withdrawn state. */
12770 /* Make the frame visible (mapped and not iconified). */
12772 void
12773 x_make_frame_invisible (f)
12774 struct frame *f;
12776 Window window;
12778 #ifdef USE_X_TOOLKIT
12779 /* Use the frame's outermost window, not the one we normally draw on. */
12780 window = XtWindow (f->output_data.x->widget);
12781 #else /* not USE_X_TOOLKIT */
12782 window = FRAME_X_WINDOW (f);
12783 #endif /* not USE_X_TOOLKIT */
12785 /* Don't keep the highlight on an invisible frame. */
12786 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12787 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12789 #if 0/* This might add unreliability; I don't trust it -- rms. */
12790 if (! f->async_visible && ! f->async_iconified)
12791 return;
12792 #endif
12794 BLOCK_INPUT;
12796 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
12797 that the current position of the window is user-specified, rather than
12798 program-specified, so that when the window is mapped again, it will be
12799 placed at the same location, without forcing the user to position it
12800 by hand again (they have already done that once for this window.) */
12801 x_wm_set_size_hint (f, (long) 0, 1);
12803 #ifdef HAVE_X11R4
12805 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
12806 DefaultScreen (FRAME_X_DISPLAY (f))))
12808 UNBLOCK_INPUT_RESIGNAL;
12809 error ("Can't notify window manager of window withdrawal");
12811 #else /* ! defined (HAVE_X11R4) */
12813 /* Tell the window manager what we're going to do. */
12814 if (! EQ (Vx_no_window_manager, Qt))
12816 XEvent unmap;
12818 unmap.xunmap.type = UnmapNotify;
12819 unmap.xunmap.window = window;
12820 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
12821 unmap.xunmap.from_configure = False;
12822 if (! XSendEvent (FRAME_X_DISPLAY (f),
12823 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12824 False,
12825 SubstructureRedirectMaskSubstructureNotifyMask,
12826 &unmap))
12828 UNBLOCK_INPUT_RESIGNAL;
12829 error ("Can't notify window manager of withdrawal");
12833 /* Unmap the window ourselves. Cheeky! */
12834 XUnmapWindow (FRAME_X_DISPLAY (f), window);
12835 #endif /* ! defined (HAVE_X11R4) */
12837 /* We can't distinguish this from iconification
12838 just by the event that we get from the server.
12839 So we can't win using the usual strategy of letting
12840 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
12841 and synchronize with the server to make sure we agree. */
12842 f->visible = 0;
12843 FRAME_ICONIFIED_P (f) = 0;
12844 f->async_visible = 0;
12845 f->async_iconified = 0;
12847 x_sync (f);
12849 UNBLOCK_INPUT;
12852 /* Change window state from mapped to iconified. */
12854 void
12855 x_iconify_frame (f)
12856 struct frame *f;
12858 int result;
12859 Lisp_Object type;
12861 /* Don't keep the highlight on an invisible frame. */
12862 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12863 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12865 if (f->async_iconified)
12866 return;
12868 BLOCK_INPUT;
12870 FRAME_SAMPLE_VISIBILITY (f);
12872 type = x_icon_type (f);
12873 if (!NILP (type))
12874 x_bitmap_icon (f, type);
12876 #ifdef USE_X_TOOLKIT
12878 if (! FRAME_VISIBLE_P (f))
12880 if (! EQ (Vx_no_window_manager, Qt))
12881 x_wm_set_window_state (f, IconicState);
12882 /* This was XtPopup, but that did nothing for an iconified frame. */
12883 XtMapWidget (f->output_data.x->widget);
12884 /* The server won't give us any event to indicate
12885 that an invisible frame was changed to an icon,
12886 so we have to record it here. */
12887 f->iconified = 1;
12888 f->visible = 1;
12889 f->async_iconified = 1;
12890 f->async_visible = 0;
12891 UNBLOCK_INPUT;
12892 return;
12895 result = XIconifyWindow (FRAME_X_DISPLAY (f),
12896 XtWindow (f->output_data.x->widget),
12897 DefaultScreen (FRAME_X_DISPLAY (f)));
12898 UNBLOCK_INPUT;
12900 if (!result)
12901 error ("Can't notify window manager of iconification");
12903 f->async_iconified = 1;
12904 f->async_visible = 0;
12907 BLOCK_INPUT;
12908 XFlush (FRAME_X_DISPLAY (f));
12909 UNBLOCK_INPUT;
12910 #else /* not USE_X_TOOLKIT */
12912 /* Make sure the X server knows where the window should be positioned,
12913 in case the user deiconifies with the window manager. */
12914 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
12915 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12917 /* Since we don't know which revision of X we're running, we'll use both
12918 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
12920 /* X11R4: send a ClientMessage to the window manager using the
12921 WM_CHANGE_STATE type. */
12923 XEvent message;
12925 message.xclient.window = FRAME_X_WINDOW (f);
12926 message.xclient.type = ClientMessage;
12927 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
12928 message.xclient.format = 32;
12929 message.xclient.data.l[0] = IconicState;
12931 if (! XSendEvent (FRAME_X_DISPLAY (f),
12932 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12933 False,
12934 SubstructureRedirectMask | SubstructureNotifyMask,
12935 &message))
12937 UNBLOCK_INPUT_RESIGNAL;
12938 error ("Can't notify window manager of iconification");
12942 /* X11R3: set the initial_state field of the window manager hints to
12943 IconicState. */
12944 x_wm_set_window_state (f, IconicState);
12946 if (!FRAME_VISIBLE_P (f))
12948 /* If the frame was withdrawn, before, we must map it. */
12949 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12952 f->async_iconified = 1;
12953 f->async_visible = 0;
12955 XFlush (FRAME_X_DISPLAY (f));
12956 UNBLOCK_INPUT;
12957 #endif /* not USE_X_TOOLKIT */
12961 /* Free X resources of frame F. */
12963 void
12964 x_free_frame_resources (f)
12965 struct frame *f;
12967 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12969 BLOCK_INPUT;
12971 /* If a display connection is dead, don't try sending more
12972 commands to the X server. */
12973 if (dpyinfo->display)
12975 if (f->output_data.x->icon_desc)
12976 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
12978 #ifdef HAVE_X_I18N
12979 if (FRAME_XIC (f))
12980 free_frame_xic (f);
12981 #endif
12983 if (FRAME_X_WINDOW (f))
12984 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12986 #ifdef USE_X_TOOLKIT
12987 if (f->output_data.x->widget)
12989 XtDestroyWidget (f->output_data.x->widget);
12990 f->output_data.x->widget = NULL;
12992 free_frame_menubar (f);
12993 #endif /* USE_X_TOOLKIT */
12995 unload_color (f, f->output_data.x->foreground_pixel);
12996 unload_color (f, f->output_data.x->background_pixel);
12997 unload_color (f, f->output_data.x->cursor_pixel);
12998 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12999 unload_color (f, f->output_data.x->border_pixel);
13000 unload_color (f, f->output_data.x->mouse_pixel);
13002 if (f->output_data.x->scroll_bar_background_pixel != -1)
13003 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
13004 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
13005 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
13006 if (f->output_data.x->white_relief.allocated_p)
13007 unload_color (f, f->output_data.x->white_relief.pixel);
13008 if (f->output_data.x->black_relief.allocated_p)
13009 unload_color (f, f->output_data.x->black_relief.pixel);
13011 if (FRAME_FACE_CACHE (f))
13012 free_frame_faces (f);
13014 x_free_gcs (f);
13015 XFlush (FRAME_X_DISPLAY (f));
13018 if (f->output_data.x->saved_menu_event)
13019 xfree (f->output_data.x->saved_menu_event);
13021 xfree (f->output_data.x);
13022 f->output_data.x = NULL;
13024 if (f == dpyinfo->x_focus_frame)
13025 dpyinfo->x_focus_frame = 0;
13026 if (f == dpyinfo->x_focus_event_frame)
13027 dpyinfo->x_focus_event_frame = 0;
13028 if (f == dpyinfo->x_highlight_frame)
13029 dpyinfo->x_highlight_frame = 0;
13031 if (f == dpyinfo->mouse_face_mouse_frame)
13033 dpyinfo->mouse_face_beg_row
13034 = dpyinfo->mouse_face_beg_col = -1;
13035 dpyinfo->mouse_face_end_row
13036 = dpyinfo->mouse_face_end_col = -1;
13037 dpyinfo->mouse_face_window = Qnil;
13038 dpyinfo->mouse_face_deferred_gc = 0;
13039 dpyinfo->mouse_face_mouse_frame = 0;
13042 UNBLOCK_INPUT;
13046 /* Destroy the X window of frame F. */
13048 void
13049 x_destroy_window (f)
13050 struct frame *f;
13052 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13054 /* If a display connection is dead, don't try sending more
13055 commands to the X server. */
13056 if (dpyinfo->display != 0)
13057 x_free_frame_resources (f);
13059 dpyinfo->reference_count--;
13063 /* Setting window manager hints. */
13065 /* Set the normal size hints for the window manager, for frame F.
13066 FLAGS is the flags word to use--or 0 meaning preserve the flags
13067 that the window now has.
13068 If USER_POSITION is nonzero, we set the USPosition
13069 flag (this is useful when FLAGS is 0). */
13071 void
13072 x_wm_set_size_hint (f, flags, user_position)
13073 struct frame *f;
13074 long flags;
13075 int user_position;
13077 XSizeHints size_hints;
13079 #ifdef USE_X_TOOLKIT
13080 Arg al[2];
13081 int ac = 0;
13082 Dimension widget_width, widget_height;
13083 Window window = XtWindow (f->output_data.x->widget);
13084 #else /* not USE_X_TOOLKIT */
13085 Window window = FRAME_X_WINDOW (f);
13086 #endif /* not USE_X_TOOLKIT */
13088 /* Setting PMaxSize caused various problems. */
13089 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
13091 size_hints.x = f->output_data.x->left_pos;
13092 size_hints.y = f->output_data.x->top_pos;
13094 #ifdef USE_X_TOOLKIT
13095 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
13096 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
13097 XtGetValues (f->output_data.x->widget, al, ac);
13098 size_hints.height = widget_height;
13099 size_hints.width = widget_width;
13100 #else /* not USE_X_TOOLKIT */
13101 size_hints.height = PIXEL_HEIGHT (f);
13102 size_hints.width = PIXEL_WIDTH (f);
13103 #endif /* not USE_X_TOOLKIT */
13105 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
13106 size_hints.height_inc = f->output_data.x->line_height;
13107 size_hints.max_width
13108 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
13109 size_hints.max_height
13110 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
13112 /* Calculate the base and minimum sizes.
13114 (When we use the X toolkit, we don't do it here.
13115 Instead we copy the values that the widgets are using, below.) */
13116 #ifndef USE_X_TOOLKIT
13118 int base_width, base_height;
13119 int min_rows = 0, min_cols = 0;
13121 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
13122 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
13124 check_frame_size (f, &min_rows, &min_cols);
13126 /* The window manager uses the base width hints to calculate the
13127 current number of rows and columns in the frame while
13128 resizing; min_width and min_height aren't useful for this
13129 purpose, since they might not give the dimensions for a
13130 zero-row, zero-column frame.
13132 We use the base_width and base_height members if we have
13133 them; otherwise, we set the min_width and min_height members
13134 to the size for a zero x zero frame. */
13136 #ifdef HAVE_X11R4
13137 size_hints.flags |= PBaseSize;
13138 size_hints.base_width = base_width;
13139 size_hints.base_height = base_height;
13140 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
13141 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
13142 #else
13143 size_hints.min_width = base_width;
13144 size_hints.min_height = base_height;
13145 #endif
13148 /* If we don't need the old flags, we don't need the old hint at all. */
13149 if (flags)
13151 size_hints.flags |= flags;
13152 goto no_read;
13154 #endif /* not USE_X_TOOLKIT */
13157 XSizeHints hints; /* Sometimes I hate X Windows... */
13158 long supplied_return;
13159 int value;
13161 #ifdef HAVE_X11R4
13162 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
13163 &supplied_return);
13164 #else
13165 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
13166 #endif
13168 #ifdef USE_X_TOOLKIT
13169 size_hints.base_height = hints.base_height;
13170 size_hints.base_width = hints.base_width;
13171 size_hints.min_height = hints.min_height;
13172 size_hints.min_width = hints.min_width;
13173 #endif
13175 if (flags)
13176 size_hints.flags |= flags;
13177 else
13179 if (value == 0)
13180 hints.flags = 0;
13181 if (hints.flags & PSize)
13182 size_hints.flags |= PSize;
13183 if (hints.flags & PPosition)
13184 size_hints.flags |= PPosition;
13185 if (hints.flags & USPosition)
13186 size_hints.flags |= USPosition;
13187 if (hints.flags & USSize)
13188 size_hints.flags |= USSize;
13192 #ifndef USE_X_TOOLKIT
13193 no_read:
13194 #endif
13196 #ifdef PWinGravity
13197 size_hints.win_gravity = f->output_data.x->win_gravity;
13198 size_hints.flags |= PWinGravity;
13200 if (user_position)
13202 size_hints.flags &= ~ PPosition;
13203 size_hints.flags |= USPosition;
13205 #endif /* PWinGravity */
13207 #ifdef HAVE_X11R4
13208 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13209 #else
13210 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13211 #endif
13214 /* Used for IconicState or NormalState */
13216 void
13217 x_wm_set_window_state (f, state)
13218 struct frame *f;
13219 int state;
13221 #ifdef USE_X_TOOLKIT
13222 Arg al[1];
13224 XtSetArg (al[0], XtNinitialState, state);
13225 XtSetValues (f->output_data.x->widget, al, 1);
13226 #else /* not USE_X_TOOLKIT */
13227 Window window = FRAME_X_WINDOW (f);
13229 f->output_data.x->wm_hints.flags |= StateHint;
13230 f->output_data.x->wm_hints.initial_state = state;
13232 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13233 #endif /* not USE_X_TOOLKIT */
13236 void
13237 x_wm_set_icon_pixmap (f, pixmap_id)
13238 struct frame *f;
13239 int pixmap_id;
13241 Pixmap icon_pixmap;
13243 #ifndef USE_X_TOOLKIT
13244 Window window = FRAME_X_WINDOW (f);
13245 #endif
13247 if (pixmap_id > 0)
13249 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
13250 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
13252 else
13254 /* It seems there is no way to turn off use of an icon pixmap.
13255 The following line does it, only if no icon has yet been created,
13256 for some window managers. But with mwm it crashes.
13257 Some people say it should clear the IconPixmapHint bit in this case,
13258 but that doesn't work, and the X consortium said it isn't the
13259 right thing at all. Since there is no way to win,
13260 best to explicitly give up. */
13261 #if 0
13262 f->output_data.x->wm_hints.icon_pixmap = None;
13263 #else
13264 return;
13265 #endif
13268 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
13271 Arg al[1];
13272 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
13273 XtSetValues (f->output_data.x->widget, al, 1);
13276 #else /* not USE_X_TOOLKIT */
13278 f->output_data.x->wm_hints.flags |= IconPixmapHint;
13279 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13281 #endif /* not USE_X_TOOLKIT */
13284 void
13285 x_wm_set_icon_position (f, icon_x, icon_y)
13286 struct frame *f;
13287 int icon_x, icon_y;
13289 #ifdef USE_X_TOOLKIT
13290 Window window = XtWindow (f->output_data.x->widget);
13291 #else
13292 Window window = FRAME_X_WINDOW (f);
13293 #endif
13295 f->output_data.x->wm_hints.flags |= IconPositionHint;
13296 f->output_data.x->wm_hints.icon_x = icon_x;
13297 f->output_data.x->wm_hints.icon_y = icon_y;
13299 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13303 /***********************************************************************
13304 Fonts
13305 ***********************************************************************/
13307 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
13309 struct font_info *
13310 x_get_font_info (f, font_idx)
13311 FRAME_PTR f;
13312 int font_idx;
13314 return (FRAME_X_FONT_TABLE (f) + font_idx);
13318 /* Return a list of names of available fonts matching PATTERN on frame F.
13320 If SIZE is > 0, it is the size (maximum bounds width) of fonts
13321 to be listed.
13323 SIZE < 0 means include scalable fonts.
13325 Frame F null means we have not yet created any frame on X, and
13326 consult the first display in x_display_list. MAXNAMES sets a limit
13327 on how many fonts to match. */
13329 Lisp_Object
13330 x_list_fonts (f, pattern, size, maxnames)
13331 struct frame *f;
13332 Lisp_Object pattern;
13333 int size;
13334 int maxnames;
13336 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
13337 Lisp_Object tem, second_best;
13338 struct x_display_info *dpyinfo
13339 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
13340 Display *dpy = dpyinfo->display;
13341 int try_XLoadQueryFont = 0;
13342 int count;
13343 int allow_scalable_fonts_p = 0;
13345 if (size < 0)
13347 allow_scalable_fonts_p = 1;
13348 size = 0;
13351 patterns = Fassoc (pattern, Valternate_fontname_alist);
13352 if (NILP (patterns))
13353 patterns = Fcons (pattern, Qnil);
13355 if (maxnames == 1 && !size)
13356 /* We can return any single font matching PATTERN. */
13357 try_XLoadQueryFont = 1;
13359 for (; CONSP (patterns); patterns = XCDR (patterns))
13361 int num_fonts;
13362 char **names = NULL;
13364 pattern = XCAR (patterns);
13365 /* See if we cached the result for this particular query.
13366 The cache is an alist of the form:
13367 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
13368 tem = XCDR (dpyinfo->name_list_element);
13369 key = Fcons (Fcons (pattern, make_number (maxnames)),
13370 allow_scalable_fonts_p ? Qt : Qnil);
13371 list = Fassoc (key, tem);
13372 if (!NILP (list))
13374 list = Fcdr_safe (list);
13375 /* We have a cashed list. Don't have to get the list again. */
13376 goto label_cached;
13379 /* At first, put PATTERN in the cache. */
13381 BLOCK_INPUT;
13382 count = x_catch_errors (dpy);
13384 if (try_XLoadQueryFont)
13386 XFontStruct *font;
13387 unsigned long value;
13389 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
13390 if (x_had_errors_p (dpy))
13392 /* This error is perhaps due to insufficient memory on X
13393 server. Let's just ignore it. */
13394 font = NULL;
13395 x_clear_errors (dpy);
13398 if (font
13399 && XGetFontProperty (font, XA_FONT, &value))
13401 char *name = (char *) XGetAtomName (dpy, (Atom) value);
13402 int len = strlen (name);
13403 char *tmp;
13405 /* If DXPC (a Differential X Protocol Compressor)
13406 Ver.3.7 is running, XGetAtomName will return null
13407 string. We must avoid such a name. */
13408 if (len == 0)
13409 try_XLoadQueryFont = 0;
13410 else
13412 num_fonts = 1;
13413 names = (char **) alloca (sizeof (char *));
13414 /* Some systems only allow alloca assigned to a
13415 simple var. */
13416 tmp = (char *) alloca (len + 1); names[0] = tmp;
13417 bcopy (name, names[0], len + 1);
13418 XFree (name);
13421 else
13422 try_XLoadQueryFont = 0;
13424 if (font)
13425 XFreeFont (dpy, font);
13428 if (!try_XLoadQueryFont)
13430 /* We try at least 10 fonts because XListFonts will return
13431 auto-scaled fonts at the head. */
13432 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
13433 &num_fonts);
13434 if (x_had_errors_p (dpy))
13436 /* This error is perhaps due to insufficient memory on X
13437 server. Let's just ignore it. */
13438 names = NULL;
13439 x_clear_errors (dpy);
13443 x_uncatch_errors (dpy, count);
13444 UNBLOCK_INPUT;
13446 if (names)
13448 int i;
13450 /* Make a list of all the fonts we got back.
13451 Store that in the font cache for the display. */
13452 for (i = 0; i < num_fonts; i++)
13454 int width = 0;
13455 char *p = names[i];
13456 int average_width = -1, dashes = 0;
13458 /* Count the number of dashes in NAMES[I]. If there are
13459 14 dashes, and the field value following 12th dash
13460 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
13461 is usually too ugly to be used for editing. Let's
13462 ignore it. */
13463 while (*p)
13464 if (*p++ == '-')
13466 dashes++;
13467 if (dashes == 7) /* PIXEL_SIZE field */
13468 width = atoi (p);
13469 else if (dashes == 12) /* AVERAGE_WIDTH field */
13470 average_width = atoi (p);
13473 if (allow_scalable_fonts_p
13474 || dashes < 14 || average_width != 0)
13476 tem = build_string (names[i]);
13477 if (NILP (Fassoc (tem, list)))
13479 if (STRINGP (Vx_pixel_size_width_font_regexp)
13480 && ((fast_c_string_match_ignore_case
13481 (Vx_pixel_size_width_font_regexp, names[i]))
13482 >= 0))
13483 /* We can set the value of PIXEL_SIZE to the
13484 width of this font. */
13485 list = Fcons (Fcons (tem, make_number (width)), list);
13486 else
13487 /* For the moment, width is not known. */
13488 list = Fcons (Fcons (tem, Qnil), list);
13493 if (!try_XLoadQueryFont)
13495 BLOCK_INPUT;
13496 XFreeFontNames (names);
13497 UNBLOCK_INPUT;
13501 /* Now store the result in the cache. */
13502 XCDR (dpyinfo->name_list_element)
13503 = Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element));
13505 label_cached:
13506 if (NILP (list)) continue; /* Try the remaining alternatives. */
13508 newlist = second_best = Qnil;
13509 /* Make a list of the fonts that have the right width. */
13510 for (; CONSP (list); list = XCDR (list))
13512 int found_size;
13514 tem = XCAR (list);
13516 if (!CONSP (tem) || NILP (XCAR (tem)))
13517 continue;
13518 if (!size)
13520 newlist = Fcons (XCAR (tem), newlist);
13521 continue;
13524 if (!INTEGERP (XCDR (tem)))
13526 /* Since we have not yet known the size of this font, we
13527 must try slow function call XLoadQueryFont. */
13528 XFontStruct *thisinfo;
13530 BLOCK_INPUT;
13531 count = x_catch_errors (dpy);
13532 thisinfo = XLoadQueryFont (dpy,
13533 XSTRING (XCAR (tem))->data);
13534 if (x_had_errors_p (dpy))
13536 /* This error is perhaps due to insufficient memory on X
13537 server. Let's just ignore it. */
13538 thisinfo = NULL;
13539 x_clear_errors (dpy);
13541 x_uncatch_errors (dpy, count);
13542 UNBLOCK_INPUT;
13544 if (thisinfo)
13546 XCDR (tem)
13547 = (thisinfo->min_bounds.width == 0
13548 ? make_number (0)
13549 : make_number (thisinfo->max_bounds.width));
13550 BLOCK_INPUT;
13551 XFreeFont (dpy, thisinfo);
13552 UNBLOCK_INPUT;
13554 else
13555 /* For unknown reason, the previous call of XListFont had
13556 returned a font which can't be opened. Record the size
13557 as 0 not to try to open it again. */
13558 XCDR (tem) = make_number (0);
13561 found_size = XINT (XCDR (tem));
13562 if (found_size == size)
13563 newlist = Fcons (XCAR (tem), newlist);
13564 else if (found_size > 0)
13566 if (NILP (second_best))
13567 second_best = tem;
13568 else if (found_size < size)
13570 if (XINT (XCDR (second_best)) > size
13571 || XINT (XCDR (second_best)) < found_size)
13572 second_best = tem;
13574 else
13576 if (XINT (XCDR (second_best)) > size
13577 && XINT (XCDR (second_best)) > found_size)
13578 second_best = tem;
13582 if (!NILP (newlist))
13583 break;
13584 else if (!NILP (second_best))
13586 newlist = Fcons (XCAR (second_best), Qnil);
13587 break;
13591 return newlist;
13595 #if GLYPH_DEBUG
13597 /* Check that FONT is valid on frame F. It is if it can be found in F's
13598 font table. */
13600 static void
13601 x_check_font (f, font)
13602 struct frame *f;
13603 XFontStruct *font;
13605 int i;
13606 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13608 xassert (font != NULL);
13610 for (i = 0; i < dpyinfo->n_fonts; i++)
13611 if (dpyinfo->font_table[i].name
13612 && font == dpyinfo->font_table[i].font)
13613 break;
13615 xassert (i < dpyinfo->n_fonts);
13618 #endif /* GLYPH_DEBUG != 0 */
13620 /* Set *W to the minimum width, *H to the minimum font height of FONT.
13621 Note: There are (broken) X fonts out there with invalid XFontStruct
13622 min_bounds contents. For example, handa@etl.go.jp reports that
13623 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
13624 have font->min_bounds.width == 0. */
13626 static INLINE void
13627 x_font_min_bounds (font, w, h)
13628 XFontStruct *font;
13629 int *w, *h;
13631 *h = FONT_HEIGHT (font);
13632 *w = font->min_bounds.width;
13634 /* Try to handle the case where FONT->min_bounds has invalid
13635 contents. Since the only font known to have invalid min_bounds
13636 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
13637 if (*w <= 0)
13638 *w = font->max_bounds.width;
13642 /* Compute the smallest character width and smallest font height over
13643 all fonts available on frame F. Set the members smallest_char_width
13644 and smallest_font_height in F's x_display_info structure to
13645 the values computed. Value is non-zero if smallest_font_height or
13646 smallest_char_width become smaller than they were before. */
13648 static int
13649 x_compute_min_glyph_bounds (f)
13650 struct frame *f;
13652 int i;
13653 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13654 XFontStruct *font;
13655 int old_width = dpyinfo->smallest_char_width;
13656 int old_height = dpyinfo->smallest_font_height;
13658 dpyinfo->smallest_font_height = 100000;
13659 dpyinfo->smallest_char_width = 100000;
13661 for (i = 0; i < dpyinfo->n_fonts; ++i)
13662 if (dpyinfo->font_table[i].name)
13664 struct font_info *fontp = dpyinfo->font_table + i;
13665 int w, h;
13667 font = (XFontStruct *) fontp->font;
13668 xassert (font != (XFontStruct *) ~0);
13669 x_font_min_bounds (font, &w, &h);
13671 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
13672 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
13675 xassert (dpyinfo->smallest_char_width > 0
13676 && dpyinfo->smallest_font_height > 0);
13678 return (dpyinfo->n_fonts == 1
13679 || dpyinfo->smallest_char_width < old_width
13680 || dpyinfo->smallest_font_height < old_height);
13684 /* Load font named FONTNAME of the size SIZE for frame F, and return a
13685 pointer to the structure font_info while allocating it dynamically.
13686 If SIZE is 0, load any size of font.
13687 If loading is failed, return NULL. */
13689 struct font_info *
13690 x_load_font (f, fontname, size)
13691 struct frame *f;
13692 register char *fontname;
13693 int size;
13695 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13696 Lisp_Object font_names;
13697 int count;
13699 /* Get a list of all the fonts that match this name. Once we
13700 have a list of matching fonts, we compare them against the fonts
13701 we already have by comparing names. */
13702 font_names = x_list_fonts (f, build_string (fontname), size, 1);
13704 if (!NILP (font_names))
13706 Lisp_Object tail;
13707 int i;
13709 for (i = 0; i < dpyinfo->n_fonts; i++)
13710 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
13711 if (dpyinfo->font_table[i].name
13712 && (!strcmp (dpyinfo->font_table[i].name,
13713 XSTRING (XCAR (tail))->data)
13714 || !strcmp (dpyinfo->font_table[i].full_name,
13715 XSTRING (XCAR (tail))->data)))
13716 return (dpyinfo->font_table + i);
13719 /* Load the font and add it to the table. */
13721 char *full_name;
13722 XFontStruct *font;
13723 struct font_info *fontp;
13724 unsigned long value;
13725 int i;
13727 /* If we have found fonts by x_list_font, load one of them. If
13728 not, we still try to load a font by the name given as FONTNAME
13729 because XListFonts (called in x_list_font) of some X server has
13730 a bug of not finding a font even if the font surely exists and
13731 is loadable by XLoadQueryFont. */
13732 if (size > 0 && !NILP (font_names))
13733 fontname = (char *) XSTRING (XCAR (font_names))->data;
13735 BLOCK_INPUT;
13736 count = x_catch_errors (FRAME_X_DISPLAY (f));
13737 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
13738 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
13740 /* This error is perhaps due to insufficient memory on X
13741 server. Let's just ignore it. */
13742 font = NULL;
13743 x_clear_errors (FRAME_X_DISPLAY (f));
13745 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
13746 UNBLOCK_INPUT;
13747 if (!font)
13748 return NULL;
13750 /* Find a free slot in the font table. */
13751 for (i = 0; i < dpyinfo->n_fonts; ++i)
13752 if (dpyinfo->font_table[i].name == NULL)
13753 break;
13755 /* If no free slot found, maybe enlarge the font table. */
13756 if (i == dpyinfo->n_fonts
13757 && dpyinfo->n_fonts == dpyinfo->font_table_size)
13759 int sz;
13760 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
13761 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
13762 dpyinfo->font_table
13763 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
13766 fontp = dpyinfo->font_table + i;
13767 if (i == dpyinfo->n_fonts)
13768 ++dpyinfo->n_fonts;
13770 /* Now fill in the slots of *FONTP. */
13771 BLOCK_INPUT;
13772 fontp->font = font;
13773 fontp->font_idx = i;
13774 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
13775 bcopy (fontname, fontp->name, strlen (fontname) + 1);
13777 /* Try to get the full name of FONT. Put it in FULL_NAME. */
13778 full_name = 0;
13779 if (XGetFontProperty (font, XA_FONT, &value))
13781 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
13782 char *p = name;
13783 int dashes = 0;
13785 /* Count the number of dashes in the "full name".
13786 If it is too few, this isn't really the font's full name,
13787 so don't use it.
13788 In X11R4, the fonts did not come with their canonical names
13789 stored in them. */
13790 while (*p)
13792 if (*p == '-')
13793 dashes++;
13794 p++;
13797 if (dashes >= 13)
13799 full_name = (char *) xmalloc (p - name + 1);
13800 bcopy (name, full_name, p - name + 1);
13803 XFree (name);
13806 if (full_name != 0)
13807 fontp->full_name = full_name;
13808 else
13809 fontp->full_name = fontp->name;
13811 fontp->size = font->max_bounds.width;
13812 fontp->height = FONT_HEIGHT (font);
13814 if (NILP (font_names))
13816 /* We come here because of a bug of XListFonts mentioned at
13817 the head of this block. Let's store this information in
13818 the cache for x_list_fonts. */
13819 Lisp_Object lispy_name = build_string (fontname);
13820 Lisp_Object lispy_full_name = build_string (fontp->full_name);
13821 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
13822 Qnil);
13824 XCDR (dpyinfo->name_list_element)
13825 = Fcons (Fcons (key,
13826 Fcons (Fcons (lispy_full_name,
13827 make_number (fontp->size)),
13828 Qnil)),
13829 XCDR (dpyinfo->name_list_element));
13830 if (full_name)
13832 key = Fcons (Fcons (lispy_full_name, make_number (256)),
13833 Qnil);
13834 XCDR (dpyinfo->name_list_element)
13835 = Fcons (Fcons (key,
13836 Fcons (Fcons (lispy_full_name,
13837 make_number (fontp->size)),
13838 Qnil)),
13839 XCDR (dpyinfo->name_list_element));
13843 /* The slot `encoding' specifies how to map a character
13844 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
13845 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
13846 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
13847 2:0xA020..0xFF7F). For the moment, we don't know which charset
13848 uses this font. So, we set information in fontp->encoding[1]
13849 which is never used by any charset. If mapping can't be
13850 decided, set FONT_ENCODING_NOT_DECIDED. */
13851 fontp->encoding[1]
13852 = (font->max_byte1 == 0
13853 /* 1-byte font */
13854 ? (font->min_char_or_byte2 < 0x80
13855 ? (font->max_char_or_byte2 < 0x80
13856 ? 0 /* 0x20..0x7F */
13857 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
13858 : 1) /* 0xA0..0xFF */
13859 /* 2-byte font */
13860 : (font->min_byte1 < 0x80
13861 ? (font->max_byte1 < 0x80
13862 ? (font->min_char_or_byte2 < 0x80
13863 ? (font->max_char_or_byte2 < 0x80
13864 ? 0 /* 0x2020..0x7F7F */
13865 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
13866 : 3) /* 0x20A0..0x7FFF */
13867 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
13868 : (font->min_char_or_byte2 < 0x80
13869 ? (font->max_char_or_byte2 < 0x80
13870 ? 2 /* 0xA020..0xFF7F */
13871 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
13872 : 1))); /* 0xA0A0..0xFFFF */
13874 fontp->baseline_offset
13875 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
13876 ? (long) value : 0);
13877 fontp->relative_compose
13878 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
13879 ? (long) value : 0);
13880 fontp->default_ascent
13881 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
13882 ? (long) value : 0);
13884 /* Set global flag fonts_changed_p to non-zero if the font loaded
13885 has a character with a smaller width than any other character
13886 before, or if the font loaded has a smalle>r height than any
13887 other font loaded before. If this happens, it will make a
13888 glyph matrix reallocation necessary. */
13889 fonts_changed_p = x_compute_min_glyph_bounds (f);
13890 UNBLOCK_INPUT;
13891 return fontp;
13896 /* Return a pointer to struct font_info of a font named FONTNAME for
13897 frame F. If no such font is loaded, return NULL. */
13899 struct font_info *
13900 x_query_font (f, fontname)
13901 struct frame *f;
13902 register char *fontname;
13904 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13905 int i;
13907 for (i = 0; i < dpyinfo->n_fonts; i++)
13908 if (dpyinfo->font_table[i].name
13909 && (!strcmp (dpyinfo->font_table[i].name, fontname)
13910 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
13911 return (dpyinfo->font_table + i);
13912 return NULL;
13916 /* Find a CCL program for a font specified by FONTP, and set the member
13917 `encoder' of the structure. */
13919 void
13920 x_find_ccl_program (fontp)
13921 struct font_info *fontp;
13923 Lisp_Object list, elt;
13925 elt = Qnil;
13926 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
13928 elt = XCAR (list);
13929 if (CONSP (elt)
13930 && STRINGP (XCAR (elt))
13931 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
13932 >= 0)
13933 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
13934 >= 0)))
13935 break;
13938 if (! NILP (list))
13940 struct ccl_program *ccl
13941 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
13943 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
13944 xfree (ccl);
13945 else
13946 fontp->font_encoder = ccl;
13952 /***********************************************************************
13953 Initialization
13954 ***********************************************************************/
13956 #ifdef USE_X_TOOLKIT
13957 static XrmOptionDescRec emacs_options[] = {
13958 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13959 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13961 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13962 XrmoptionSepArg, NULL},
13963 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13965 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13966 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13967 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13968 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13969 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13970 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13971 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13973 #endif /* USE_X_TOOLKIT */
13975 static int x_initialized;
13977 #ifdef MULTI_KBOARD
13978 /* Test whether two display-name strings agree up to the dot that separates
13979 the screen number from the server number. */
13980 static int
13981 same_x_server (name1, name2)
13982 char *name1, *name2;
13984 int seen_colon = 0;
13985 unsigned char *system_name = XSTRING (Vsystem_name)->data;
13986 int system_name_length = strlen (system_name);
13987 int length_until_period = 0;
13989 while (system_name[length_until_period] != 0
13990 && system_name[length_until_period] != '.')
13991 length_until_period++;
13993 /* Treat `unix' like an empty host name. */
13994 if (! strncmp (name1, "unix:", 5))
13995 name1 += 4;
13996 if (! strncmp (name2, "unix:", 5))
13997 name2 += 4;
13998 /* Treat this host's name like an empty host name. */
13999 if (! strncmp (name1, system_name, system_name_length)
14000 && name1[system_name_length] == ':')
14001 name1 += system_name_length;
14002 if (! strncmp (name2, system_name, system_name_length)
14003 && name2[system_name_length] == ':')
14004 name2 += system_name_length;
14005 /* Treat this host's domainless name like an empty host name. */
14006 if (! strncmp (name1, system_name, length_until_period)
14007 && name1[length_until_period] == ':')
14008 name1 += length_until_period;
14009 if (! strncmp (name2, system_name, length_until_period)
14010 && name2[length_until_period] == ':')
14011 name2 += length_until_period;
14013 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
14015 if (*name1 == ':')
14016 seen_colon++;
14017 if (seen_colon && *name1 == '.')
14018 return 1;
14020 return (seen_colon
14021 && (*name1 == '.' || *name1 == '\0')
14022 && (*name2 == '.' || *name2 == '\0'));
14024 #endif
14026 struct x_display_info *
14027 x_term_init (display_name, xrm_option, resource_name)
14028 Lisp_Object display_name;
14029 char *xrm_option;
14030 char *resource_name;
14032 int connection;
14033 Display *dpy;
14034 struct x_display_info *dpyinfo;
14035 XrmDatabase xrdb;
14037 BLOCK_INPUT;
14039 if (!x_initialized)
14041 x_initialize ();
14042 x_initialized = 1;
14045 #ifdef USE_X_TOOLKIT
14046 /* weiner@footloose.sps.mot.com reports that this causes
14047 errors with X11R5:
14048 X protocol error: BadAtom (invalid Atom parameter)
14049 on protocol request 18skiloaf.
14050 So let's not use it until R6. */
14051 #ifdef HAVE_X11XTR6
14052 XtSetLanguageProc (NULL, NULL, NULL);
14053 #endif
14056 int argc = 0;
14057 char *argv[3];
14059 argv[0] = "";
14060 argc = 1;
14061 if (xrm_option)
14063 argv[argc++] = "-xrm";
14064 argv[argc++] = xrm_option;
14066 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
14067 resource_name, EMACS_CLASS,
14068 emacs_options, XtNumber (emacs_options),
14069 &argc, argv);
14071 #ifdef HAVE_X11XTR6
14072 /* I think this is to compensate for XtSetLanguageProc. */
14073 fixup_locale ();
14074 #endif
14077 #else /* not USE_X_TOOLKIT */
14078 #ifdef HAVE_X11R5
14079 XSetLocaleModifiers ("");
14080 #endif
14081 dpy = XOpenDisplay (XSTRING (display_name)->data);
14082 #endif /* not USE_X_TOOLKIT */
14084 /* Detect failure. */
14085 if (dpy == 0)
14087 UNBLOCK_INPUT;
14088 return 0;
14091 /* We have definitely succeeded. Record the new connection. */
14093 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
14094 bzero (dpyinfo, sizeof *dpyinfo);
14096 #ifdef MULTI_KBOARD
14098 struct x_display_info *share;
14099 Lisp_Object tail;
14101 for (share = x_display_list, tail = x_display_name_list; share;
14102 share = share->next, tail = XCDR (tail))
14103 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
14104 XSTRING (display_name)->data))
14105 break;
14106 if (share)
14107 dpyinfo->kboard = share->kboard;
14108 else
14110 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
14111 init_kboard (dpyinfo->kboard);
14112 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
14114 char *vendor = ServerVendor (dpy);
14115 UNBLOCK_INPUT;
14116 dpyinfo->kboard->Vsystem_key_alist
14117 = call1 (Qvendor_specific_keysyms,
14118 build_string (vendor ? vendor : ""));
14119 BLOCK_INPUT;
14122 dpyinfo->kboard->next_kboard = all_kboards;
14123 all_kboards = dpyinfo->kboard;
14124 /* Don't let the initial kboard remain current longer than necessary.
14125 That would cause problems if a file loaded on startup tries to
14126 prompt in the mini-buffer. */
14127 if (current_kboard == initial_kboard)
14128 current_kboard = dpyinfo->kboard;
14130 dpyinfo->kboard->reference_count++;
14132 #endif
14134 /* Put this display on the chain. */
14135 dpyinfo->next = x_display_list;
14136 x_display_list = dpyinfo;
14138 /* Put it on x_display_name_list as well, to keep them parallel. */
14139 x_display_name_list = Fcons (Fcons (display_name, Qnil),
14140 x_display_name_list);
14141 dpyinfo->name_list_element = XCAR (x_display_name_list);
14143 dpyinfo->display = dpy;
14145 #if 0
14146 XSetAfterFunction (x_current_display, x_trace_wire);
14147 #endif /* ! 0 */
14149 dpyinfo->x_id_name
14150 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
14151 + STRING_BYTES (XSTRING (Vsystem_name))
14152 + 2);
14153 sprintf (dpyinfo->x_id_name, "%s@%s",
14154 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
14156 /* Figure out which modifier bits mean what. */
14157 x_find_modifier_meanings (dpyinfo);
14159 /* Get the scroll bar cursor. */
14160 dpyinfo->vertical_scroll_bar_cursor
14161 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
14163 xrdb = x_load_resources (dpyinfo->display, xrm_option,
14164 resource_name, EMACS_CLASS);
14165 #ifdef HAVE_XRMSETDATABASE
14166 XrmSetDatabase (dpyinfo->display, xrdb);
14167 #else
14168 dpyinfo->display->db = xrdb;
14169 #endif
14170 /* Put the rdb where we can find it in a way that works on
14171 all versions. */
14172 dpyinfo->xrdb = xrdb;
14174 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
14175 DefaultScreen (dpyinfo->display));
14176 select_visual (dpyinfo);
14177 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
14178 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
14179 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
14180 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
14181 dpyinfo->grabbed = 0;
14182 dpyinfo->reference_count = 0;
14183 dpyinfo->icon_bitmap_id = -1;
14184 dpyinfo->font_table = NULL;
14185 dpyinfo->n_fonts = 0;
14186 dpyinfo->font_table_size = 0;
14187 dpyinfo->bitmaps = 0;
14188 dpyinfo->bitmaps_size = 0;
14189 dpyinfo->bitmaps_last = 0;
14190 dpyinfo->scratch_cursor_gc = 0;
14191 dpyinfo->mouse_face_mouse_frame = 0;
14192 dpyinfo->mouse_face_deferred_gc = 0;
14193 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
14194 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
14195 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
14196 dpyinfo->mouse_face_window = Qnil;
14197 dpyinfo->mouse_face_overlay = Qnil;
14198 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
14199 dpyinfo->mouse_face_defer = 0;
14200 dpyinfo->x_focus_frame = 0;
14201 dpyinfo->x_focus_event_frame = 0;
14202 dpyinfo->x_highlight_frame = 0;
14203 dpyinfo->image_cache = make_image_cache ();
14205 /* See if a private colormap is requested. */
14206 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
14208 if (dpyinfo->visual->class == PseudoColor)
14210 Lisp_Object value;
14211 value = display_x_get_resource (dpyinfo,
14212 build_string ("privateColormap"),
14213 build_string ("PrivateColormap"),
14214 Qnil, Qnil);
14215 if (STRINGP (value)
14216 && (!strcmp (XSTRING (value)->data, "true")
14217 || !strcmp (XSTRING (value)->data, "on")))
14218 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
14221 else
14222 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
14223 dpyinfo->visual, AllocNone);
14226 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
14227 double pixels = DisplayHeight (dpyinfo->display, screen_number);
14228 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
14229 dpyinfo->resy = pixels * 25.4 / mm;
14230 pixels = DisplayWidth (dpyinfo->display, screen_number);
14231 mm = DisplayWidthMM (dpyinfo->display, screen_number);
14232 dpyinfo->resx = pixels * 25.4 / mm;
14235 dpyinfo->Xatom_wm_protocols
14236 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
14237 dpyinfo->Xatom_wm_take_focus
14238 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
14239 dpyinfo->Xatom_wm_save_yourself
14240 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
14241 dpyinfo->Xatom_wm_delete_window
14242 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
14243 dpyinfo->Xatom_wm_change_state
14244 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
14245 dpyinfo->Xatom_wm_configure_denied
14246 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
14247 dpyinfo->Xatom_wm_window_moved
14248 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
14249 dpyinfo->Xatom_editres
14250 = XInternAtom (dpyinfo->display, "Editres", False);
14251 dpyinfo->Xatom_CLIPBOARD
14252 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
14253 dpyinfo->Xatom_TIMESTAMP
14254 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
14255 dpyinfo->Xatom_TEXT
14256 = XInternAtom (dpyinfo->display, "TEXT", False);
14257 dpyinfo->Xatom_COMPOUND_TEXT
14258 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
14259 dpyinfo->Xatom_DELETE
14260 = XInternAtom (dpyinfo->display, "DELETE", False);
14261 dpyinfo->Xatom_MULTIPLE
14262 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
14263 dpyinfo->Xatom_INCR
14264 = XInternAtom (dpyinfo->display, "INCR", False);
14265 dpyinfo->Xatom_EMACS_TMP
14266 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
14267 dpyinfo->Xatom_TARGETS
14268 = XInternAtom (dpyinfo->display, "TARGETS", False);
14269 dpyinfo->Xatom_NULL
14270 = XInternAtom (dpyinfo->display, "NULL", False);
14271 dpyinfo->Xatom_ATOM_PAIR
14272 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
14273 /* For properties of font. */
14274 dpyinfo->Xatom_PIXEL_SIZE
14275 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
14276 dpyinfo->Xatom_MULE_BASELINE_OFFSET
14277 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
14278 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
14279 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
14280 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
14281 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
14283 /* Ghostscript support. */
14284 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
14285 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
14287 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
14288 False);
14290 dpyinfo->cut_buffers_initialized = 0;
14292 connection = ConnectionNumber (dpyinfo->display);
14293 dpyinfo->connection = connection;
14296 char null_bits[1];
14298 null_bits[0] = 0x00;
14300 dpyinfo->null_pixel
14301 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14302 null_bits, 1, 1, (long) 0, (long) 0,
14307 extern int gray_bitmap_width, gray_bitmap_height;
14308 extern char *gray_bitmap_bits;
14309 dpyinfo->gray
14310 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14311 gray_bitmap_bits,
14312 gray_bitmap_width, gray_bitmap_height,
14313 (unsigned long) 1, (unsigned long) 0, 1);
14316 #ifdef HAVE_X_I18N
14317 xim_initialize (dpyinfo, resource_name);
14318 #endif
14320 #ifdef subprocesses
14321 /* This is only needed for distinguishing keyboard and process input. */
14322 if (connection != 0)
14323 add_keyboard_wait_descriptor (connection);
14324 #endif
14326 #ifndef F_SETOWN_BUG
14327 #ifdef F_SETOWN
14328 #ifdef F_SETOWN_SOCK_NEG
14329 /* stdin is a socket here */
14330 fcntl (connection, F_SETOWN, -getpid ());
14331 #else /* ! defined (F_SETOWN_SOCK_NEG) */
14332 fcntl (connection, F_SETOWN, getpid ());
14333 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
14334 #endif /* ! defined (F_SETOWN) */
14335 #endif /* F_SETOWN_BUG */
14337 #ifdef SIGIO
14338 if (interrupt_input)
14339 init_sigio (connection);
14340 #endif /* ! defined (SIGIO) */
14342 #ifdef USE_LUCID
14343 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
14344 /* Make sure that we have a valid font for dialog boxes
14345 so that Xt does not crash. */
14347 Display *dpy = dpyinfo->display;
14348 XrmValue d, fr, to;
14349 Font font;
14350 int count;
14352 d.addr = (XPointer)&dpy;
14353 d.size = sizeof (Display *);
14354 fr.addr = XtDefaultFont;
14355 fr.size = sizeof (XtDefaultFont);
14356 to.size = sizeof (Font *);
14357 to.addr = (XPointer)&font;
14358 count = x_catch_errors (dpy);
14359 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
14360 abort ();
14361 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
14362 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
14363 x_uncatch_errors (dpy, count);
14365 #endif
14366 #endif
14368 /* See if we should run in synchronous mode. This is useful
14369 for debugging X code. */
14371 Lisp_Object value;
14372 value = display_x_get_resource (dpyinfo,
14373 build_string ("synchronous"),
14374 build_string ("Synchronous"),
14375 Qnil, Qnil);
14376 if (STRINGP (value)
14377 && (!strcmp (XSTRING (value)->data, "true")
14378 || !strcmp (XSTRING (value)->data, "on")))
14379 XSynchronize (dpyinfo->display, True);
14382 UNBLOCK_INPUT;
14384 return dpyinfo;
14387 /* Get rid of display DPYINFO, assuming all frames are already gone,
14388 and without sending any more commands to the X server. */
14390 void
14391 x_delete_display (dpyinfo)
14392 struct x_display_info *dpyinfo;
14394 delete_keyboard_wait_descriptor (dpyinfo->connection);
14396 /* Discard this display from x_display_name_list and x_display_list.
14397 We can't use Fdelq because that can quit. */
14398 if (! NILP (x_display_name_list)
14399 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
14400 x_display_name_list = XCDR (x_display_name_list);
14401 else
14403 Lisp_Object tail;
14405 tail = x_display_name_list;
14406 while (CONSP (tail) && CONSP (XCDR (tail)))
14408 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
14410 XCDR (tail) = XCDR (XCDR (tail));
14411 break;
14413 tail = XCDR (tail);
14417 if (next_noop_dpyinfo == dpyinfo)
14418 next_noop_dpyinfo = dpyinfo->next;
14420 if (x_display_list == dpyinfo)
14421 x_display_list = dpyinfo->next;
14422 else
14424 struct x_display_info *tail;
14426 for (tail = x_display_list; tail; tail = tail->next)
14427 if (tail->next == dpyinfo)
14428 tail->next = tail->next->next;
14431 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
14432 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
14433 XrmDestroyDatabase (dpyinfo->xrdb);
14434 #endif
14435 #endif
14436 #ifdef MULTI_KBOARD
14437 if (--dpyinfo->kboard->reference_count == 0)
14438 delete_kboard (dpyinfo->kboard);
14439 #endif
14440 #ifdef HAVE_X_I18N
14441 if (dpyinfo->xim)
14442 xim_close_dpy (dpyinfo);
14443 #endif
14445 xfree (dpyinfo->font_table);
14446 xfree (dpyinfo->x_id_name);
14447 xfree (dpyinfo->color_cells);
14448 xfree (dpyinfo);
14452 /* Set up use of X before we make the first connection. */
14454 static struct redisplay_interface x_redisplay_interface =
14456 x_produce_glyphs,
14457 x_write_glyphs,
14458 x_insert_glyphs,
14459 x_clear_end_of_line,
14460 x_scroll_run,
14461 x_after_update_window_line,
14462 x_update_window_begin,
14463 x_update_window_end,
14464 XTcursor_to,
14465 x_flush,
14466 x_clear_mouse_face,
14467 x_get_glyph_overhangs,
14468 x_fix_overlapping_area
14471 void
14472 x_initialize ()
14474 rif = &x_redisplay_interface;
14476 clear_frame_hook = x_clear_frame;
14477 ins_del_lines_hook = x_ins_del_lines;
14478 change_line_highlight_hook = x_change_line_highlight;
14479 delete_glyphs_hook = x_delete_glyphs;
14480 ring_bell_hook = XTring_bell;
14481 reset_terminal_modes_hook = XTreset_terminal_modes;
14482 set_terminal_modes_hook = XTset_terminal_modes;
14483 update_begin_hook = x_update_begin;
14484 update_end_hook = x_update_end;
14485 set_terminal_window_hook = XTset_terminal_window;
14486 read_socket_hook = XTread_socket;
14487 frame_up_to_date_hook = XTframe_up_to_date;
14488 reassert_line_highlight_hook = XTreassert_line_highlight;
14489 mouse_position_hook = XTmouse_position;
14490 frame_rehighlight_hook = XTframe_rehighlight;
14491 frame_raise_lower_hook = XTframe_raise_lower;
14492 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
14493 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
14494 redeem_scroll_bar_hook = XTredeem_scroll_bar;
14495 judge_scroll_bars_hook = XTjudge_scroll_bars;
14496 estimate_mode_line_height_hook = x_estimate_mode_line_height;
14498 scroll_region_ok = 1; /* we'll scroll partial frames */
14499 char_ins_del_ok = 1;
14500 line_ins_del_ok = 1; /* we'll just blt 'em */
14501 fast_clear_end_of_line = 1; /* X does this well */
14502 memory_below_frame = 0; /* we don't remember what scrolls
14503 off the bottom */
14504 baud_rate = 19200;
14506 x_noop_count = 0;
14507 last_tool_bar_item = -1;
14508 any_help_event_p = 0;
14510 /* Try to use interrupt input; if we can't, then start polling. */
14511 Fset_input_mode (Qt, Qnil, Qt, Qnil);
14513 #ifdef USE_X_TOOLKIT
14514 XtToolkitInitialize ();
14516 Xt_app_con = XtCreateApplicationContext ();
14518 /* Register a converter from strings to pixels, which uses
14519 Emacs' color allocation infrastructure. */
14520 XtAppSetTypeConverter (Xt_app_con,
14521 XtRString, XtRPixel, cvt_string_to_pixel,
14522 cvt_string_to_pixel_args,
14523 XtNumber (cvt_string_to_pixel_args),
14524 XtCacheByDisplay, cvt_pixel_dtor);
14526 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
14528 /* Install an asynchronous timer that processes Xt timeout events
14529 every 0.1s. This is necessary because some widget sets use
14530 timeouts internally, for example the LessTif menu bar, or the
14531 Xaw3d scroll bar. When Xt timouts aren't processed, these
14532 widgets don't behave normally. */
14534 EMACS_TIME interval;
14535 EMACS_SET_SECS_USECS (interval, 0, 100000);
14536 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
14538 #endif
14540 #ifdef USE_TOOLKIT_SCROLL_BARS
14541 xaw3d_arrow_scroll = False;
14542 xaw3d_pick_top = True;
14543 #endif
14545 /* Note that there is no real way portable across R3/R4 to get the
14546 original error handler. */
14547 XSetErrorHandler (x_error_handler);
14548 XSetIOErrorHandler (x_io_error_quitter);
14550 /* Disable Window Change signals; they are handled by X events. */
14551 #ifdef SIGWINCH
14552 signal (SIGWINCH, SIG_DFL);
14553 #endif /* ! defined (SIGWINCH) */
14555 signal (SIGPIPE, x_connection_signal);
14559 void
14560 syms_of_xterm ()
14562 staticpro (&x_error_message_string);
14563 x_error_message_string = Qnil;
14565 staticpro (&x_display_name_list);
14566 x_display_name_list = Qnil;
14568 staticpro (&last_mouse_scroll_bar);
14569 last_mouse_scroll_bar = Qnil;
14571 staticpro (&Qvendor_specific_keysyms);
14572 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
14574 staticpro (&last_mouse_press_frame);
14575 last_mouse_press_frame = Qnil;
14577 help_echo = Qnil;
14578 staticpro (&help_echo);
14579 help_echo_object = Qnil;
14580 staticpro (&help_echo_object);
14581 help_echo_window = Qnil;
14582 staticpro (&help_echo_window);
14583 previous_help_echo = Qnil;
14584 staticpro (&previous_help_echo);
14585 help_echo_pos = -1;
14587 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
14588 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
14589 For example, if a block cursor is over a tab, it will be drawn as\n\
14590 wide as that tab on the display.");
14591 x_stretch_cursor_p = 0;
14593 DEFVAR_BOOL ("x-use-underline-position-properties",
14594 &x_use_underline_position_properties,
14595 "*Non-nil means make use of UNDERLINE_POSITION font properties.\n\
14596 Nil means ignore them. If you encounter fonts with bogus\n\
14597 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior\n\
14598 to 4.1, set this to nil.");
14599 x_use_underline_position_properties = 1;
14601 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
14602 "What X toolkit scroll bars Emacs uses.\n\
14603 A value of nil means Emacs doesn't use X toolkit scroll bars.\n\
14604 Otherwise, value is a symbol describing the X toolkit.");
14605 #ifdef USE_TOOLKIT_SCROLL_BARS
14606 #ifdef USE_MOTIF
14607 Vx_toolkit_scroll_bars = intern ("motif");
14608 #elif defined HAVE_XAW3D
14609 Vx_toolkit_scroll_bars = intern ("xaw3d");
14610 #else
14611 Vx_toolkit_scroll_bars = intern ("xaw");
14612 #endif
14613 #else
14614 Vx_toolkit_scroll_bars = Qnil;
14615 #endif
14617 staticpro (&last_mouse_motion_frame);
14618 last_mouse_motion_frame = Qnil;
14621 #endif /* not HAVE_X_WINDOWS */