Doc and coding conventions fixes.
[emacs/old-mirror.git] / src / xterm.c
blob7061d549607bab6c68ee8940a0c5e7f7cbc9e81a
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 void 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 void 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 1
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 /* Don't use S->y for clipping because it doesn't take partially
2845 visible lines into account. For example, it can be negative for
2846 partially visible lines at the top of a window. */
2847 if (!s->row->full_width_p
2848 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2849 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2850 else
2851 r->y = max (0, s->row->y);
2853 /* If drawing a tool-bar window, draw it over the internal border
2854 at the top of the window. */
2855 if (s->w == XWINDOW (s->f->tool_bar_window))
2856 r->y -= s->f->output_data.x->internal_border_width;
2858 /* If S draws overlapping rows, it's sufficient to use the top and
2859 bottom of the window for clipping because this glyph string
2860 intentionally draws over other lines. */
2861 if (s->for_overlaps_p)
2863 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2864 r->height = window_text_bottom_y (s->w) - r->y;
2867 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2871 /* Set clipping for output of glyph string S. S may be part of a mode
2872 line or menu if we don't have X toolkit support. */
2874 static INLINE void
2875 x_set_glyph_string_clipping (s)
2876 struct glyph_string *s;
2878 XRectangle r;
2879 x_get_glyph_string_clip_rect (s, &r);
2880 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2884 /* Compute left and right overhang of glyph string S. If S is a glyph
2885 string for a composition, assume overhangs don't exist. */
2887 static INLINE void
2888 x_compute_glyph_string_overhangs (s)
2889 struct glyph_string *s;
2891 if (s->cmp == NULL
2892 && s->first_glyph->type == CHAR_GLYPH)
2894 XCharStruct cs;
2895 int direction, font_ascent, font_descent;
2896 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2897 &font_ascent, &font_descent, &cs);
2898 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2899 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2904 /* Compute overhangs and x-positions for glyph string S and its
2905 predecessors, or successors. X is the starting x-position for S.
2906 BACKWARD_P non-zero means process predecessors. */
2908 static void
2909 x_compute_overhangs_and_x (s, x, backward_p)
2910 struct glyph_string *s;
2911 int x;
2912 int backward_p;
2914 if (backward_p)
2916 while (s)
2918 x_compute_glyph_string_overhangs (s);
2919 x -= s->width;
2920 s->x = x;
2921 s = s->prev;
2924 else
2926 while (s)
2928 x_compute_glyph_string_overhangs (s);
2929 s->x = x;
2930 x += s->width;
2931 s = s->next;
2937 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2938 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2939 assumed to be zero. */
2941 static void
2942 x_get_glyph_overhangs (glyph, f, left, right)
2943 struct glyph *glyph;
2944 struct frame *f;
2945 int *left, *right;
2947 *left = *right = 0;
2949 if (glyph->type == CHAR_GLYPH)
2951 XFontStruct *font;
2952 struct face *face;
2953 struct font_info *font_info;
2954 XChar2b char2b;
2955 XCharStruct *pcm;
2957 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2958 font = face->font;
2959 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2960 if (font
2961 && (pcm = x_per_char_metric (font, &char2b)))
2963 if (pcm->rbearing > pcm->width)
2964 *right = pcm->rbearing - pcm->width;
2965 if (pcm->lbearing < 0)
2966 *left = -pcm->lbearing;
2972 /* Return the index of the first glyph preceding glyph string S that
2973 is overwritten by S because of S's left overhang. Value is -1
2974 if no glyphs are overwritten. */
2976 static int
2977 x_left_overwritten (s)
2978 struct glyph_string *s;
2980 int k;
2982 if (s->left_overhang)
2984 int x = 0, i;
2985 struct glyph *glyphs = s->row->glyphs[s->area];
2986 int first = s->first_glyph - glyphs;
2988 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2989 x -= glyphs[i].pixel_width;
2991 k = i + 1;
2993 else
2994 k = -1;
2996 return k;
3000 /* Return the index of the first glyph preceding glyph string S that
3001 is overwriting S because of its right overhang. Value is -1 if no
3002 glyph in front of S overwrites S. */
3004 static int
3005 x_left_overwriting (s)
3006 struct glyph_string *s;
3008 int i, k, x;
3009 struct glyph *glyphs = s->row->glyphs[s->area];
3010 int first = s->first_glyph - glyphs;
3012 k = -1;
3013 x = 0;
3014 for (i = first - 1; i >= 0; --i)
3016 int left, right;
3017 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3018 if (x + right > 0)
3019 k = i;
3020 x -= glyphs[i].pixel_width;
3023 return k;
3027 /* Return the index of the last glyph following glyph string S that is
3028 not overwritten by S because of S's right overhang. Value is -1 if
3029 no such glyph is found. */
3031 static int
3032 x_right_overwritten (s)
3033 struct glyph_string *s;
3035 int k = -1;
3037 if (s->right_overhang)
3039 int x = 0, i;
3040 struct glyph *glyphs = s->row->glyphs[s->area];
3041 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3042 int end = s->row->used[s->area];
3044 for (i = first; i < end && s->right_overhang > x; ++i)
3045 x += glyphs[i].pixel_width;
3047 k = i;
3050 return k;
3054 /* Return the index of the last glyph following glyph string S that
3055 overwrites S because of its left overhang. Value is negative
3056 if no such glyph is found. */
3058 static int
3059 x_right_overwriting (s)
3060 struct glyph_string *s;
3062 int i, k, x;
3063 int end = s->row->used[s->area];
3064 struct glyph *glyphs = s->row->glyphs[s->area];
3065 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3067 k = -1;
3068 x = 0;
3069 for (i = first; i < end; ++i)
3071 int left, right;
3072 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3073 if (x - left < 0)
3074 k = i;
3075 x += glyphs[i].pixel_width;
3078 return k;
3082 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3084 static INLINE void
3085 x_clear_glyph_string_rect (s, x, y, w, h)
3086 struct glyph_string *s;
3087 int x, y, w, h;
3089 XGCValues xgcv;
3090 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3091 XSetForeground (s->display, s->gc, xgcv.background);
3092 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3093 XSetForeground (s->display, s->gc, xgcv.foreground);
3097 /* Draw the background of glyph_string S. If S->background_filled_p
3098 is non-zero don't draw it. FORCE_P non-zero means draw the
3099 background even if it wouldn't be drawn normally. This is used
3100 when a string preceding S draws into the background of S, or S
3101 contains the first component of a composition. */
3103 static void
3104 x_draw_glyph_string_background (s, force_p)
3105 struct glyph_string *s;
3106 int force_p;
3108 /* Nothing to do if background has already been drawn or if it
3109 shouldn't be drawn in the first place. */
3110 if (!s->background_filled_p)
3112 int box_line_width = max (s->face->box_line_width, 0);
3114 if (s->stippled_p)
3116 /* Fill background with a stipple pattern. */
3117 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3118 XFillRectangle (s->display, s->window, s->gc, s->x,
3119 s->y + box_line_width,
3120 s->background_width,
3121 s->height - 2 * box_line_width);
3122 XSetFillStyle (s->display, s->gc, FillSolid);
3123 s->background_filled_p = 1;
3125 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
3126 || s->font_not_found_p
3127 || s->extends_to_end_of_line_p
3128 || force_p)
3130 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
3131 s->background_width,
3132 s->height - 2 * box_line_width);
3133 s->background_filled_p = 1;
3139 /* Draw the foreground of glyph string S. */
3141 static void
3142 x_draw_glyph_string_foreground (s)
3143 struct glyph_string *s;
3145 int i, x;
3147 /* If first glyph of S has a left box line, start drawing the text
3148 of S to the right of that box line. */
3149 if (s->face->box != FACE_NO_BOX
3150 && s->first_glyph->left_box_line_p)
3151 x = s->x + abs (s->face->box_line_width);
3152 else
3153 x = s->x;
3155 /* Draw characters of S as rectangles if S's font could not be
3156 loaded. */
3157 if (s->font_not_found_p)
3159 for (i = 0; i < s->nchars; ++i)
3161 struct glyph *g = s->first_glyph + i;
3162 XDrawRectangle (s->display, s->window,
3163 s->gc, x, s->y, g->pixel_width - 1,
3164 s->height - 1);
3165 x += g->pixel_width;
3168 else
3170 char *char1b = (char *) s->char2b;
3171 int boff = s->font_info->baseline_offset;
3173 if (s->font_info->vertical_centering)
3174 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3176 /* If we can use 8-bit functions, condense S->char2b. */
3177 if (!s->two_byte_p)
3178 for (i = 0; i < s->nchars; ++i)
3179 char1b[i] = s->char2b[i].byte2;
3181 /* Draw text with XDrawString if background has already been
3182 filled. Otherwise, use XDrawImageString. (Note that
3183 XDrawImageString is usually faster than XDrawString.) Always
3184 use XDrawImageString when drawing the cursor so that there is
3185 no chance that characters under a box cursor are invisible. */
3186 if (s->for_overlaps_p
3187 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3189 /* Draw characters with 16-bit or 8-bit functions. */
3190 if (s->two_byte_p)
3191 XDrawString16 (s->display, s->window, s->gc, x,
3192 s->ybase - boff, s->char2b, s->nchars);
3193 else
3194 XDrawString (s->display, s->window, s->gc, x,
3195 s->ybase - boff, char1b, s->nchars);
3197 else
3199 if (s->two_byte_p)
3200 XDrawImageString16 (s->display, s->window, s->gc, x,
3201 s->ybase - boff, s->char2b, s->nchars);
3202 else
3203 XDrawImageString (s->display, s->window, s->gc, x,
3204 s->ybase - boff, char1b, s->nchars);
3209 /* Draw the foreground of composite glyph string S. */
3211 static void
3212 x_draw_composite_glyph_string_foreground (s)
3213 struct glyph_string *s;
3215 int i, x;
3217 /* If first glyph of S has a left box line, start drawing the text
3218 of S to the right of that box line. */
3219 if (s->face->box != FACE_NO_BOX
3220 && s->first_glyph->left_box_line_p)
3221 x = s->x + abs (s->face->box_line_width);
3222 else
3223 x = s->x;
3225 /* S is a glyph string for a composition. S->gidx is the index of
3226 the first character drawn for glyphs of this composition.
3227 S->gidx == 0 means we are drawing the very first character of
3228 this composition. */
3230 /* Draw a rectangle for the composition if the font for the very
3231 first character of the composition could not be loaded. */
3232 if (s->font_not_found_p)
3234 if (s->gidx == 0)
3235 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3236 s->width - 1, s->height - 1);
3238 else
3240 for (i = 0; i < s->nchars; i++, ++s->gidx)
3241 XDrawString16 (s->display, s->window, s->gc,
3242 x + s->cmp->offsets[s->gidx * 2],
3243 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3244 s->char2b + i, 1);
3249 #ifdef USE_X_TOOLKIT
3251 static struct frame *x_frame_of_widget P_ ((Widget));
3252 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
3253 XrmValue *, XrmValue *, XtPointer *));
3254 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
3255 XrmValue *, Cardinal *));
3258 /* Return the frame on which widget WIDGET is used.. Abort if frame
3259 cannot be determined. */
3261 static struct frame *
3262 x_frame_of_widget (widget)
3263 Widget widget;
3265 struct x_display_info *dpyinfo;
3266 Lisp_Object tail;
3267 struct frame *f;
3269 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3271 /* Find the top-level shell of the widget. Note that this function
3272 can be called when the widget is not yet realized, so XtWindow
3273 (widget) == 0. That's the reason we can't simply use
3274 x_any_window_to_frame. */
3275 while (!XtIsTopLevelShell (widget))
3276 widget = XtParent (widget);
3278 /* Look for a frame with that top-level widget. Allocate the color
3279 on that frame to get the right gamma correction value. */
3280 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3281 if (GC_FRAMEP (XCAR (tail))
3282 && (f = XFRAME (XCAR (tail)),
3283 (f->output_data.nothing != 1
3284 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3285 && f->output_data.x->widget == widget)
3286 return f;
3288 abort ();
3292 /* Allocate the color COLOR->pixel on the screen and display of
3293 widget WIDGET in colormap CMAP. If an exact match cannot be
3294 allocated, try the nearest color available. Value is non-zero
3295 if successful. This is called from lwlib. */
3298 x_alloc_nearest_color_for_widget (widget, cmap, color)
3299 Widget widget;
3300 Colormap cmap;
3301 XColor *color;
3303 struct frame *f = x_frame_of_widget (widget);
3304 return x_alloc_nearest_color (f, cmap, color);
3308 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3309 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3310 If this produces the same color as PIXEL, try a color where all RGB
3311 values have DELTA added. Return the allocated color in *PIXEL.
3312 DISPLAY is the X display, CMAP is the colormap to operate on.
3313 Value is non-zero if successful. */
3316 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3317 Widget widget;
3318 Display *display;
3319 Colormap cmap;
3320 unsigned long *pixel;
3321 double factor;
3322 int delta;
3324 struct frame *f = x_frame_of_widget (widget);
3325 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3329 /* Structure specifying which arguments should be passed by Xt to
3330 cvt_string_to_pixel. We want the widget's screen and colormap. */
3332 static XtConvertArgRec cvt_string_to_pixel_args[] =
3334 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
3335 sizeof (Screen *)},
3336 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
3337 sizeof (Colormap)}
3341 /* The address of this variable is returned by
3342 cvt_string_to_pixel. */
3344 static Pixel cvt_string_to_pixel_value;
3347 /* Convert a color name to a pixel color.
3349 DPY is the display we are working on.
3351 ARGS is an array of *NARGS XrmValue structures holding additional
3352 information about the widget for which the conversion takes place.
3353 The contents of this array are determined by the specification
3354 in cvt_string_to_pixel_args.
3356 FROM is a pointer to an XrmValue which points to the color name to
3357 convert. TO is an XrmValue in which to return the pixel color.
3359 CLOSURE_RET is a pointer to user-data, in which we record if
3360 we allocated the color or not.
3362 Value is True if successful, False otherwise. */
3364 static Boolean
3365 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
3366 Display *dpy;
3367 XrmValue *args;
3368 Cardinal *nargs;
3369 XrmValue *from, *to;
3370 XtPointer *closure_ret;
3372 Screen *screen;
3373 Colormap cmap;
3374 Pixel pixel;
3375 String color_name;
3376 XColor color;
3378 if (*nargs != 2)
3380 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3381 "wrongParameters", "cvt_string_to_pixel",
3382 "XtToolkitError",
3383 "Screen and colormap args required", NULL, NULL);
3384 return False;
3387 screen = *(Screen **) args[0].addr;
3388 cmap = *(Colormap *) args[1].addr;
3389 color_name = (String) from->addr;
3391 if (strcmp (color_name, XtDefaultBackground) == 0)
3393 *closure_ret = (XtPointer) False;
3394 pixel = WhitePixelOfScreen (screen);
3396 else if (strcmp (color_name, XtDefaultForeground) == 0)
3398 *closure_ret = (XtPointer) False;
3399 pixel = BlackPixelOfScreen (screen);
3401 else if (XParseColor (dpy, cmap, color_name, &color)
3402 && x_alloc_nearest_color_1 (dpy, cmap, &color))
3404 pixel = color.pixel;
3405 *closure_ret = (XtPointer) True;
3407 else
3409 String params[1];
3410 Cardinal nparams = 1;
3412 params[0] = color_name;
3413 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3414 "badValue", "cvt_string_to_pixel",
3415 "XtToolkitError", "Invalid color `%s'",
3416 params, &nparams);
3417 return False;
3420 if (to->addr != NULL)
3422 if (to->size < sizeof (Pixel))
3424 to->size = sizeof (Pixel);
3425 return False;
3428 *(Pixel *) to->addr = pixel;
3430 else
3432 cvt_string_to_pixel_value = pixel;
3433 to->addr = (XtPointer) &cvt_string_to_pixel_value;
3436 to->size = sizeof (Pixel);
3437 return True;
3441 /* Free a pixel color which was previously allocated via
3442 cvt_string_to_pixel. This is registered as the destructor
3443 for this type of resource via XtSetTypeConverter.
3445 APP is the application context in which we work.
3447 TO is a pointer to an XrmValue holding the color to free.
3448 CLOSURE is the value we stored in CLOSURE_RET for this color
3449 in cvt_string_to_pixel.
3451 ARGS and NARGS are like for cvt_string_to_pixel. */
3453 static void
3454 cvt_pixel_dtor (app, to, closure, args, nargs)
3455 XtAppContext app;
3456 XrmValuePtr to;
3457 XtPointer closure;
3458 XrmValuePtr args;
3459 Cardinal *nargs;
3461 if (*nargs != 2)
3463 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
3464 "XtToolkitError",
3465 "Screen and colormap arguments required",
3466 NULL, NULL);
3468 else if (closure != NULL)
3470 /* We did allocate the pixel, so free it. */
3471 Screen *screen = *(Screen **) args[0].addr;
3472 Colormap cmap = *(Colormap *) args[1].addr;
3473 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
3474 (Pixel *) to->addr, 1);
3479 #endif /* USE_X_TOOLKIT */
3482 /* Value is an array of XColor structures for the contents of the
3483 color map of display DPY. Set *NCELLS to the size of the array.
3484 Note that this probably shouldn't be called for large color maps,
3485 say a 24-bit TrueColor map. */
3487 static const XColor *
3488 x_color_cells (dpy, ncells)
3489 Display *dpy;
3490 int *ncells;
3492 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3494 if (dpyinfo->color_cells == NULL)
3496 Screen *screen = dpyinfo->screen;
3497 int i;
3499 dpyinfo->ncolor_cells
3500 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
3501 dpyinfo->color_cells
3502 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3503 * sizeof *dpyinfo->color_cells);
3505 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3506 dpyinfo->color_cells[i].pixel = i;
3508 XQueryColors (dpy, dpyinfo->cmap,
3509 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3512 *ncells = dpyinfo->ncolor_cells;
3513 return dpyinfo->color_cells;
3517 /* On frame F, translate pixel colors to RGB values for the NCOLORS
3518 colors in COLORS. Use cached information, if available. */
3520 void
3521 x_query_colors (f, colors, ncolors)
3522 struct frame *f;
3523 XColor *colors;
3524 int ncolors;
3526 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3528 if (dpyinfo->color_cells)
3530 int i;
3531 for (i = 0; i < ncolors; ++i)
3533 unsigned long pixel = colors[i].pixel;
3534 xassert (pixel < dpyinfo->ncolor_cells);
3535 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3536 colors[i] = dpyinfo->color_cells[pixel];
3539 else
3540 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3544 /* On frame F, translate pixel color to RGB values for the color in
3545 COLOR. Use cached information, if available. */
3547 void
3548 x_query_color (f, color)
3549 struct frame *f;
3550 XColor *color;
3552 x_query_colors (f, color, 1);
3556 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
3557 exact match can't be allocated, try the nearest color available.
3558 Value is non-zero if successful. Set *COLOR to the color
3559 allocated. */
3561 static int
3562 x_alloc_nearest_color_1 (dpy, cmap, color)
3563 Display *dpy;
3564 Colormap cmap;
3565 XColor *color;
3567 int rc;
3569 rc = XAllocColor (dpy, cmap, color);
3570 if (rc == 0)
3572 /* If we got to this point, the colormap is full, so we're going
3573 to try to get the next closest color. The algorithm used is
3574 a least-squares matching, which is what X uses for closest
3575 color matching with StaticColor visuals. */
3576 int nearest, i;
3577 unsigned long nearest_delta = ~0;
3578 int ncells;
3579 const XColor *cells = x_color_cells (dpy, &ncells);
3581 for (nearest = i = 0; i < ncells; ++i)
3583 long dred = (color->red >> 8) - (cells[i].red >> 8);
3584 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3585 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3586 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3588 if (delta < nearest_delta)
3590 nearest = i;
3591 nearest_delta = delta;
3595 color->red = cells[nearest].red;
3596 color->green = cells[nearest].green;
3597 color->blue = cells[nearest].blue;
3598 rc = XAllocColor (dpy, cmap, color);
3600 else
3602 /* If allocation succeeded, and the allocated pixel color is not
3603 equal to a cached pixel color recorded earlier, there was a
3604 change in the colormap, so clear the color cache. */
3605 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3606 XColor *cached_color;
3608 if (dpyinfo->color_cells
3609 && (cached_color = &dpyinfo->color_cells[color->pixel],
3610 (cached_color->red != color->red
3611 || cached_color->blue != color->blue
3612 || cached_color->green != color->green)))
3614 xfree (dpyinfo->color_cells);
3615 dpyinfo->color_cells = NULL;
3616 dpyinfo->ncolor_cells = 0;
3620 #ifdef DEBUG_X_COLORS
3621 if (rc)
3622 register_color (color->pixel);
3623 #endif /* DEBUG_X_COLORS */
3625 return rc;
3629 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
3630 exact match can't be allocated, try the nearest color available.
3631 Value is non-zero if successful. Set *COLOR to the color
3632 allocated. */
3635 x_alloc_nearest_color (f, cmap, color)
3636 struct frame *f;
3637 Colormap cmap;
3638 XColor *color;
3640 gamma_correct (f, color);
3641 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
3645 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3646 It's necessary to do this instead of just using PIXEL directly to
3647 get color reference counts right. */
3649 unsigned long
3650 x_copy_color (f, pixel)
3651 struct frame *f;
3652 unsigned long pixel;
3654 XColor color;
3656 color.pixel = pixel;
3657 BLOCK_INPUT;
3658 x_query_color (f, &color);
3659 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3660 UNBLOCK_INPUT;
3661 #ifdef DEBUG_X_COLORS
3662 register_color (pixel);
3663 #endif
3664 return color.pixel;
3668 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3669 It's necessary to do this instead of just using PIXEL directly to
3670 get color reference counts right. */
3672 unsigned long
3673 x_copy_dpy_color (dpy, cmap, pixel)
3674 Display *dpy;
3675 Colormap cmap;
3676 unsigned long pixel;
3678 XColor color;
3680 color.pixel = pixel;
3681 BLOCK_INPUT;
3682 XQueryColor (dpy, cmap, &color);
3683 XAllocColor (dpy, cmap, &color);
3684 UNBLOCK_INPUT;
3685 #ifdef DEBUG_X_COLORS
3686 register_color (pixel);
3687 #endif
3688 return color.pixel;
3692 /* Brightness beyond which a color won't have its highlight brightness
3693 boosted.
3695 Nominally, highlight colors for `3d' faces are calculated by
3696 brightening an object's color by a constant scale factor, but this
3697 doesn't yield good results for dark colors, so for colors who's
3698 brightness is less than this value (on a scale of 0-65535) have an
3699 use an additional additive factor.
3701 The value here is set so that the default menu-bar/mode-line color
3702 (grey75) will not have its highlights changed at all. */
3703 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3706 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3707 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3708 If this produces the same color as PIXEL, try a color where all RGB
3709 values have DELTA added. Return the allocated color in *PIXEL.
3710 DISPLAY is the X display, CMAP is the colormap to operate on.
3711 Value is non-zero if successful. */
3713 static int
3714 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3715 struct frame *f;
3716 Display *display;
3717 Colormap cmap;
3718 unsigned long *pixel;
3719 double factor;
3720 int delta;
3722 XColor color, new;
3723 long bright;
3724 int success_p;
3726 /* Get RGB color values. */
3727 color.pixel = *pixel;
3728 x_query_color (f, &color);
3730 /* Change RGB values by specified FACTOR. Avoid overflow! */
3731 xassert (factor >= 0);
3732 new.red = min (0xffff, factor * color.red);
3733 new.green = min (0xffff, factor * color.green);
3734 new.blue = min (0xffff, factor * color.blue);
3736 /* Calculate brightness of COLOR. */
3737 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
3739 /* We only boost colors that are darker than
3740 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3741 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3742 /* Make an additive adjustment to NEW, because it's dark enough so
3743 that scaling by FACTOR alone isn't enough. */
3745 /* How far below the limit this color is (0 - 1, 1 being darker). */
3746 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3747 /* The additive adjustment. */
3748 int min_delta = delta * dimness * factor / 2;
3750 if (factor < 1)
3752 new.red = max (0, new.red - min_delta);
3753 new.green = max (0, new.green - min_delta);
3754 new.blue = max (0, new.blue - min_delta);
3756 else
3758 new.red = min (0xffff, min_delta + new.red);
3759 new.green = min (0xffff, min_delta + new.green);
3760 new.blue = min (0xffff, min_delta + new.blue);
3764 /* Try to allocate the color. */
3765 success_p = x_alloc_nearest_color (f, cmap, &new);
3766 if (success_p)
3768 if (new.pixel == *pixel)
3770 /* If we end up with the same color as before, try adding
3771 delta to the RGB values. */
3772 x_free_colors (f, &new.pixel, 1);
3774 new.red = min (0xffff, delta + color.red);
3775 new.green = min (0xffff, delta + color.green);
3776 new.blue = min (0xffff, delta + color.blue);
3777 success_p = x_alloc_nearest_color (f, cmap, &new);
3779 else
3780 success_p = 1;
3781 *pixel = new.pixel;
3784 return success_p;
3788 /* Set up the foreground color for drawing relief lines of glyph
3789 string S. RELIEF is a pointer to a struct relief containing the GC
3790 with which lines will be drawn. Use a color that is FACTOR or
3791 DELTA lighter or darker than the relief's background which is found
3792 in S->f->output_data.x->relief_background. If such a color cannot
3793 be allocated, use DEFAULT_PIXEL, instead. */
3795 static void
3796 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3797 struct frame *f;
3798 struct relief *relief;
3799 double factor;
3800 int delta;
3801 unsigned long default_pixel;
3803 XGCValues xgcv;
3804 struct x_output *di = f->output_data.x;
3805 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3806 unsigned long pixel;
3807 unsigned long background = di->relief_background;
3808 Colormap cmap = FRAME_X_COLORMAP (f);
3809 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3810 Display *dpy = FRAME_X_DISPLAY (f);
3812 xgcv.graphics_exposures = False;
3813 xgcv.line_width = 1;
3815 /* Free previously allocated color. The color cell will be reused
3816 when it has been freed as many times as it was allocated, so this
3817 doesn't affect faces using the same colors. */
3818 if (relief->gc
3819 && relief->allocated_p)
3821 x_free_colors (f, &relief->pixel, 1);
3822 relief->allocated_p = 0;
3825 /* Allocate new color. */
3826 xgcv.foreground = default_pixel;
3827 pixel = background;
3828 if (dpyinfo->n_planes != 1
3829 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3831 relief->allocated_p = 1;
3832 xgcv.foreground = relief->pixel = pixel;
3835 if (relief->gc == 0)
3837 xgcv.stipple = dpyinfo->gray;
3838 mask |= GCStipple;
3839 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3841 else
3842 XChangeGC (dpy, relief->gc, mask, &xgcv);
3846 /* Set up colors for the relief lines around glyph string S. */
3848 static void
3849 x_setup_relief_colors (s)
3850 struct glyph_string *s;
3852 struct x_output *di = s->f->output_data.x;
3853 unsigned long color;
3855 if (s->face->use_box_color_for_shadows_p)
3856 color = s->face->box_color;
3857 else
3859 XGCValues xgcv;
3861 /* Get the background color of the face. */
3862 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3863 color = xgcv.background;
3866 if (di->white_relief.gc == 0
3867 || color != di->relief_background)
3869 di->relief_background = color;
3870 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3871 WHITE_PIX_DEFAULT (s->f));
3872 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3873 BLACK_PIX_DEFAULT (s->f));
3878 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3879 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3880 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3881 relief. LEFT_P non-zero means draw a relief on the left side of
3882 the rectangle. RIGHT_P non-zero means draw a relief on the right
3883 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3884 when drawing. */
3886 static void
3887 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3888 raised_p, left_p, right_p, clip_rect)
3889 struct frame *f;
3890 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3891 XRectangle *clip_rect;
3893 int i;
3894 GC gc;
3896 if (raised_p)
3897 gc = f->output_data.x->white_relief.gc;
3898 else
3899 gc = f->output_data.x->black_relief.gc;
3900 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3902 /* Top. */
3903 for (i = 0; i < width; ++i)
3904 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3905 left_x + i * left_p, top_y + i,
3906 right_x + 1 - i * right_p, top_y + i);
3908 /* Left. */
3909 if (left_p)
3910 for (i = 0; i < width; ++i)
3911 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3912 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
3914 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3915 if (raised_p)
3916 gc = f->output_data.x->black_relief.gc;
3917 else
3918 gc = f->output_data.x->white_relief.gc;
3919 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3921 /* Bottom. */
3922 for (i = 0; i < width; ++i)
3923 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3924 left_x + i * left_p + 1, bottom_y - i,
3925 right_x + 1 - i * right_p, bottom_y - i);
3927 /* Right. */
3928 if (right_p)
3929 for (i = 0; i < width; ++i)
3930 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3931 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3933 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3937 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3938 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3939 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3940 left side of the rectangle. RIGHT_P non-zero means draw a line
3941 on the right side of the rectangle. CLIP_RECT is the clipping
3942 rectangle to use when drawing. */
3944 static void
3945 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3946 left_p, right_p, clip_rect)
3947 struct glyph_string *s;
3948 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3949 XRectangle *clip_rect;
3951 XGCValues xgcv;
3953 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3954 XSetForeground (s->display, s->gc, s->face->box_color);
3955 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3957 /* Top. */
3958 XFillRectangle (s->display, s->window, s->gc,
3959 left_x, top_y, right_x - left_x + 1, width);
3961 /* Left. */
3962 if (left_p)
3963 XFillRectangle (s->display, s->window, s->gc,
3964 left_x, top_y, width, bottom_y - top_y + 1);
3966 /* Bottom. */
3967 XFillRectangle (s->display, s->window, s->gc,
3968 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3970 /* Right. */
3971 if (right_p)
3972 XFillRectangle (s->display, s->window, s->gc,
3973 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3975 XSetForeground (s->display, s->gc, xgcv.foreground);
3976 XSetClipMask (s->display, s->gc, None);
3980 /* Draw a box around glyph string S. */
3982 static void
3983 x_draw_glyph_string_box (s)
3984 struct glyph_string *s;
3986 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3987 int left_p, right_p;
3988 struct glyph *last_glyph;
3989 XRectangle clip_rect;
3991 last_x = window_box_right (s->w, s->area);
3992 if (s->row->full_width_p
3993 && !s->w->pseudo_window_p)
3995 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3996 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3997 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
4000 /* The glyph that may have a right box line. */
4001 last_glyph = (s->cmp || s->img
4002 ? s->first_glyph
4003 : s->first_glyph + s->nchars - 1);
4005 width = abs (s->face->box_line_width);
4006 raised_p = s->face->box == FACE_RAISED_BOX;
4007 left_x = s->x;
4008 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
4009 ? last_x - 1
4010 : min (last_x, s->x + s->background_width) - 1);
4011 top_y = s->y;
4012 bottom_y = top_y + s->height - 1;
4014 left_p = (s->first_glyph->left_box_line_p
4015 || (s->hl == DRAW_MOUSE_FACE
4016 && (s->prev == NULL
4017 || s->prev->hl != s->hl)));
4018 right_p = (last_glyph->right_box_line_p
4019 || (s->hl == DRAW_MOUSE_FACE
4020 && (s->next == NULL
4021 || s->next->hl != s->hl)));
4023 x_get_glyph_string_clip_rect (s, &clip_rect);
4025 if (s->face->box == FACE_SIMPLE_BOX)
4026 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
4027 left_p, right_p, &clip_rect);
4028 else
4030 x_setup_relief_colors (s);
4031 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
4032 width, raised_p, left_p, right_p, &clip_rect);
4037 /* Draw foreground of image glyph string S. */
4039 static void
4040 x_draw_image_foreground (s)
4041 struct glyph_string *s;
4043 int x;
4044 int y = s->ybase - image_ascent (s->img, s->face);
4046 /* If first glyph of S has a left box line, start drawing it to the
4047 right of that line. */
4048 if (s->face->box != FACE_NO_BOX
4049 && s->first_glyph->left_box_line_p)
4050 x = s->x + abs (s->face->box_line_width);
4051 else
4052 x = s->x;
4054 /* If there is a margin around the image, adjust x- and y-position
4055 by that margin. */
4056 x += s->img->hmargin;
4057 y += s->img->vmargin;
4059 if (s->img->pixmap)
4061 if (s->img->mask)
4063 /* We can't set both a clip mask and use XSetClipRectangles
4064 because the latter also sets a clip mask. We also can't
4065 trust on the shape extension to be available
4066 (XShapeCombineRegion). So, compute the rectangle to draw
4067 manually. */
4068 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4069 | GCFunction);
4070 XGCValues xgcv;
4071 XRectangle clip_rect, image_rect, r;
4073 xgcv.clip_mask = s->img->mask;
4074 xgcv.clip_x_origin = x;
4075 xgcv.clip_y_origin = y;
4076 xgcv.function = GXcopy;
4077 XChangeGC (s->display, s->gc, mask, &xgcv);
4079 x_get_glyph_string_clip_rect (s, &clip_rect);
4080 image_rect.x = x;
4081 image_rect.y = y;
4082 image_rect.width = s->img->width;
4083 image_rect.height = s->img->height;
4084 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4085 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4086 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4088 else
4090 unsigned long mask = GCClipXOrigin | GCClipYOrigin | GCFunction;
4091 XGCValues xgcv;
4092 XRectangle clip_rect, image_rect, r;
4094 x_get_glyph_string_clip_rect (s, &clip_rect);
4095 image_rect.x = x;
4096 image_rect.y = y;
4097 image_rect.width = s->img->width;
4098 image_rect.height = s->img->height;
4099 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4100 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4101 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4103 /* When the image has a mask, we can expect that at
4104 least part of a mouse highlight or a block cursor will
4105 be visible. If the image doesn't have a mask, make
4106 a block cursor visible by drawing a rectangle around
4107 the image. I believe it's looking better if we do
4108 nothing here for mouse-face. */
4109 if (s->hl == DRAW_CURSOR)
4110 XDrawRectangle (s->display, s->window, s->gc, x, y,
4111 s->img->width - 1, s->img->height - 1);
4114 else
4115 /* Draw a rectangle if image could not be loaded. */
4116 XDrawRectangle (s->display, s->window, s->gc, x, y,
4117 s->img->width - 1, s->img->height - 1);
4121 /* Draw a relief around the image glyph string S. */
4123 static void
4124 x_draw_image_relief (s)
4125 struct glyph_string *s;
4127 int x0, y0, x1, y1, thick, raised_p;
4128 XRectangle r;
4129 int x;
4130 int y = s->ybase - image_ascent (s->img, s->face);
4132 /* If first glyph of S has a left box line, start drawing it to the
4133 right of that line. */
4134 if (s->face->box != FACE_NO_BOX
4135 && s->first_glyph->left_box_line_p)
4136 x = s->x + abs (s->face->box_line_width);
4137 else
4138 x = s->x;
4140 /* If there is a margin around the image, adjust x- and y-position
4141 by that margin. */
4142 x += s->img->hmargin;
4143 y += s->img->vmargin;
4145 if (s->hl == DRAW_IMAGE_SUNKEN
4146 || s->hl == DRAW_IMAGE_RAISED)
4148 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
4149 raised_p = s->hl == DRAW_IMAGE_RAISED;
4151 else
4153 thick = abs (s->img->relief);
4154 raised_p = s->img->relief > 0;
4157 x0 = x - thick;
4158 y0 = y - thick;
4159 x1 = x + s->img->width + thick - 1;
4160 y1 = y + s->img->height + thick - 1;
4162 x_setup_relief_colors (s);
4163 x_get_glyph_string_clip_rect (s, &r);
4164 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
4168 /* Draw the foreground of image glyph string S to PIXMAP. */
4170 static void
4171 x_draw_image_foreground_1 (s, pixmap)
4172 struct glyph_string *s;
4173 Pixmap pixmap;
4175 int x;
4176 int y = s->ybase - s->y - image_ascent (s->img, s->face);
4178 /* If first glyph of S has a left box line, start drawing it to the
4179 right of that line. */
4180 if (s->face->box != FACE_NO_BOX
4181 && s->first_glyph->left_box_line_p)
4182 x = abs (s->face->box_line_width);
4183 else
4184 x = 0;
4186 /* If there is a margin around the image, adjust x- and y-position
4187 by that margin. */
4188 x += s->img->hmargin;
4189 y += s->img->vmargin;
4191 if (s->img->pixmap)
4193 if (s->img->mask)
4195 /* We can't set both a clip mask and use XSetClipRectangles
4196 because the latter also sets a clip mask. We also can't
4197 trust on the shape extension to be available
4198 (XShapeCombineRegion). So, compute the rectangle to draw
4199 manually. */
4200 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4201 | GCFunction);
4202 XGCValues xgcv;
4204 xgcv.clip_mask = s->img->mask;
4205 xgcv.clip_x_origin = x;
4206 xgcv.clip_y_origin = y;
4207 xgcv.function = GXcopy;
4208 XChangeGC (s->display, s->gc, mask, &xgcv);
4210 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4211 0, 0, s->img->width, s->img->height, x, y);
4212 XSetClipMask (s->display, s->gc, None);
4214 else
4216 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4217 0, 0, s->img->width, s->img->height, x, y);
4219 /* When the image has a mask, we can expect that at
4220 least part of a mouse highlight or a block cursor will
4221 be visible. If the image doesn't have a mask, make
4222 a block cursor visible by drawing a rectangle around
4223 the image. I believe it's looking better if we do
4224 nothing here for mouse-face. */
4225 if (s->hl == DRAW_CURSOR)
4226 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4227 s->img->width - 1, s->img->height - 1);
4230 else
4231 /* Draw a rectangle if image could not be loaded. */
4232 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4233 s->img->width - 1, s->img->height - 1);
4237 /* Draw part of the background of glyph string S. X, Y, W, and H
4238 give the rectangle to draw. */
4240 static void
4241 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4242 struct glyph_string *s;
4243 int x, y, w, h;
4245 if (s->stippled_p)
4247 /* Fill background with a stipple pattern. */
4248 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4249 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4250 XSetFillStyle (s->display, s->gc, FillSolid);
4252 else
4253 x_clear_glyph_string_rect (s, x, y, w, h);
4257 /* Draw image glyph string S.
4259 s->y
4260 s->x +-------------------------
4261 | s->face->box
4263 | +-------------------------
4264 | | s->img->margin
4266 | | +-------------------
4267 | | | the image
4271 static void
4272 x_draw_image_glyph_string (s)
4273 struct glyph_string *s;
4275 int x, y;
4276 int box_line_hwidth = abs (s->face->box_line_width);
4277 int box_line_vwidth = max (s->face->box_line_width, 0);
4278 int height;
4279 Pixmap pixmap = None;
4281 height = s->height - 2 * box_line_vwidth;
4283 /* Fill background with face under the image. Do it only if row is
4284 taller than image or if image has a clip mask to reduce
4285 flickering. */
4286 s->stippled_p = s->face->stipple != 0;
4287 if (height > s->img->height
4288 || s->img->hmargin
4289 || s->img->vmargin
4290 || s->img->mask
4291 || s->img->pixmap == 0
4292 || s->width != s->background_width)
4294 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4295 x = s->x + box_line_hwidth;
4296 else
4297 x = s->x;
4299 y = s->y + box_line_vwidth;
4301 if (s->img->mask)
4303 /* Create a pixmap as large as the glyph string. Fill it
4304 with the background color. Copy the image to it, using
4305 its mask. Copy the temporary pixmap to the display. */
4306 Screen *screen = FRAME_X_SCREEN (s->f);
4307 int depth = DefaultDepthOfScreen (screen);
4309 /* Create a pixmap as large as the glyph string. */
4310 pixmap = XCreatePixmap (s->display, s->window,
4311 s->background_width,
4312 s->height, depth);
4314 /* Don't clip in the following because we're working on the
4315 pixmap. */
4316 XSetClipMask (s->display, s->gc, None);
4318 /* Fill the pixmap with the background color/stipple. */
4319 if (s->stippled_p)
4321 /* Fill background with a stipple pattern. */
4322 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4323 XFillRectangle (s->display, pixmap, s->gc,
4324 0, 0, s->background_width, s->height);
4325 XSetFillStyle (s->display, s->gc, FillSolid);
4327 else
4329 XGCValues xgcv;
4330 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4331 &xgcv);
4332 XSetForeground (s->display, s->gc, xgcv.background);
4333 XFillRectangle (s->display, pixmap, s->gc,
4334 0, 0, s->background_width, s->height);
4335 XSetForeground (s->display, s->gc, xgcv.foreground);
4338 else
4339 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4341 s->background_filled_p = 1;
4344 /* Draw the foreground. */
4345 if (pixmap != None)
4347 x_draw_image_foreground_1 (s, pixmap);
4348 x_set_glyph_string_clipping (s);
4349 XCopyArea (s->display, pixmap, s->window, s->gc,
4350 0, 0, s->background_width, s->height, s->x, s->y);
4351 XFreePixmap (s->display, pixmap);
4353 else
4354 x_draw_image_foreground (s);
4356 /* If we must draw a relief around the image, do it. */
4357 if (s->img->relief
4358 || s->hl == DRAW_IMAGE_RAISED
4359 || s->hl == DRAW_IMAGE_SUNKEN)
4360 x_draw_image_relief (s);
4364 /* Draw stretch glyph string S. */
4366 static void
4367 x_draw_stretch_glyph_string (s)
4368 struct glyph_string *s;
4370 xassert (s->first_glyph->type == STRETCH_GLYPH);
4371 s->stippled_p = s->face->stipple != 0;
4373 if (s->hl == DRAW_CURSOR
4374 && !x_stretch_cursor_p)
4376 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4377 as wide as the stretch glyph. */
4378 int width = min (CANON_X_UNIT (s->f), s->background_width);
4380 /* Draw cursor. */
4381 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4383 /* Clear rest using the GC of the original non-cursor face. */
4384 if (width < s->background_width)
4386 int x = s->x + width, y = s->y;
4387 int w = s->background_width - width, h = s->height;
4388 XRectangle r;
4389 GC gc;
4391 if (s->row->mouse_face_p
4392 && cursor_in_mouse_face_p (s->w))
4394 x_set_mouse_face_gc (s);
4395 gc = s->gc;
4397 else
4398 gc = s->face->gc;
4400 x_get_glyph_string_clip_rect (s, &r);
4401 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4403 if (s->face->stipple)
4405 /* Fill background with a stipple pattern. */
4406 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4407 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4408 XSetFillStyle (s->display, gc, FillSolid);
4410 else
4412 XGCValues xgcv;
4413 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4414 XSetForeground (s->display, gc, xgcv.background);
4415 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4416 XSetForeground (s->display, gc, xgcv.foreground);
4420 else if (!s->background_filled_p)
4421 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4422 s->height);
4424 s->background_filled_p = 1;
4428 /* Draw glyph string S. */
4430 static void
4431 x_draw_glyph_string (s)
4432 struct glyph_string *s;
4434 int relief_drawn_p = 0;
4436 /* If S draws into the background of its successor, draw the
4437 background of the successor first so that S can draw into it.
4438 This makes S->next use XDrawString instead of XDrawImageString. */
4439 if (s->next && s->right_overhang && !s->for_overlaps_p)
4441 xassert (s->next->img == NULL);
4442 x_set_glyph_string_gc (s->next);
4443 x_set_glyph_string_clipping (s->next);
4444 x_draw_glyph_string_background (s->next, 1);
4447 /* Set up S->gc, set clipping and draw S. */
4448 x_set_glyph_string_gc (s);
4449 x_set_glyph_string_clipping (s);
4451 /* Draw relief (if any) in advance for char/composition so that the
4452 glyph string can be drawn over it. */
4453 if (!s->for_overlaps_p
4454 && s->face->box != FACE_NO_BOX
4455 && (s->first_glyph->type == CHAR_GLYPH
4456 || s->first_glyph->type == COMPOSITE_GLYPH))
4459 x_draw_glyph_string_background (s, 1);
4460 x_draw_glyph_string_box (s);
4461 relief_drawn_p = 1;
4464 switch (s->first_glyph->type)
4466 case IMAGE_GLYPH:
4467 x_draw_image_glyph_string (s);
4468 break;
4470 case STRETCH_GLYPH:
4471 x_draw_stretch_glyph_string (s);
4472 break;
4474 case CHAR_GLYPH:
4475 if (s->for_overlaps_p)
4476 s->background_filled_p = 1;
4477 else
4478 x_draw_glyph_string_background (s, 0);
4479 x_draw_glyph_string_foreground (s);
4480 break;
4482 case COMPOSITE_GLYPH:
4483 if (s->for_overlaps_p || s->gidx > 0)
4484 s->background_filled_p = 1;
4485 else
4486 x_draw_glyph_string_background (s, 1);
4487 x_draw_composite_glyph_string_foreground (s);
4488 break;
4490 default:
4491 abort ();
4494 if (!s->for_overlaps_p)
4496 /* Draw underline. */
4497 if (s->face->underline_p)
4499 unsigned long tem, h;
4500 int y;
4502 /* Get the underline thickness. Default is 1 pixel. */
4503 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4504 h = 1;
4506 /* Get the underline position. This is the recommended
4507 vertical offset in pixels from the baseline to the top of
4508 the underline. This is a signed value according to the
4509 specs, and its default is
4511 ROUND ((maximum descent) / 2), with
4512 ROUND(x) = floor (x + 0.5) */
4514 if (x_use_underline_position_properties
4515 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4516 y = s->ybase + (long) tem;
4517 else if (s->face->font)
4518 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4519 else
4520 y = s->y + s->height - h;
4522 if (s->face->underline_defaulted_p)
4523 XFillRectangle (s->display, s->window, s->gc,
4524 s->x, y, s->width, h);
4525 else
4527 XGCValues xgcv;
4528 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4529 XSetForeground (s->display, s->gc, s->face->underline_color);
4530 XFillRectangle (s->display, s->window, s->gc,
4531 s->x, y, s->width, h);
4532 XSetForeground (s->display, s->gc, xgcv.foreground);
4536 /* Draw overline. */
4537 if (s->face->overline_p)
4539 unsigned long dy = 0, h = 1;
4541 if (s->face->overline_color_defaulted_p)
4542 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4543 s->width, h);
4544 else
4546 XGCValues xgcv;
4547 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4548 XSetForeground (s->display, s->gc, s->face->overline_color);
4549 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4550 s->width, h);
4551 XSetForeground (s->display, s->gc, xgcv.foreground);
4555 /* Draw strike-through. */
4556 if (s->face->strike_through_p)
4558 unsigned long h = 1;
4559 unsigned long dy = (s->height - h) / 2;
4561 if (s->face->strike_through_color_defaulted_p)
4562 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4563 s->width, h);
4564 else
4566 XGCValues xgcv;
4567 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4568 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4569 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4570 s->width, h);
4571 XSetForeground (s->display, s->gc, xgcv.foreground);
4575 /* Draw relief if not yet drawn. */
4576 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
4577 x_draw_glyph_string_box (s);
4580 /* Reset clipping. */
4581 XSetClipMask (s->display, s->gc, None);
4585 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4586 struct face **, int));
4589 /* Fill glyph string S with composition components specified by S->cmp.
4591 FACES is an array of faces for all components of this composition.
4592 S->gidx is the index of the first component for S.
4593 OVERLAPS_P non-zero means S should draw the foreground only, and
4594 use its physical height for clipping.
4596 Value is the index of a component not in S. */
4598 static int
4599 x_fill_composite_glyph_string (s, faces, overlaps_p)
4600 struct glyph_string *s;
4601 struct face **faces;
4602 int overlaps_p;
4604 int i;
4606 xassert (s);
4608 s->for_overlaps_p = overlaps_p;
4610 s->face = faces[s->gidx];
4611 s->font = s->face->font;
4612 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4614 /* For all glyphs of this composition, starting at the offset
4615 S->gidx, until we reach the end of the definition or encounter a
4616 glyph that requires the different face, add it to S. */
4617 ++s->nchars;
4618 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4619 ++s->nchars;
4621 /* All glyph strings for the same composition has the same width,
4622 i.e. the width set for the first component of the composition. */
4624 s->width = s->first_glyph->pixel_width;
4626 /* If the specified font could not be loaded, use the frame's
4627 default font, but record the fact that we couldn't load it in
4628 the glyph string so that we can draw rectangles for the
4629 characters of the glyph string. */
4630 if (s->font == NULL)
4632 s->font_not_found_p = 1;
4633 s->font = FRAME_FONT (s->f);
4636 /* Adjust base line for subscript/superscript text. */
4637 s->ybase += s->first_glyph->voffset;
4639 xassert (s->face && s->face->gc);
4641 /* This glyph string must always be drawn with 16-bit functions. */
4642 s->two_byte_p = 1;
4644 return s->gidx + s->nchars;
4648 /* Fill glyph string S from a sequence of character glyphs.
4650 FACE_ID is the face id of the string. START is the index of the
4651 first glyph to consider, END is the index of the last + 1.
4652 OVERLAPS_P non-zero means S should draw the foreground only, and
4653 use its physical height for clipping.
4655 Value is the index of the first glyph not in S. */
4657 static int
4658 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4659 struct glyph_string *s;
4660 int face_id;
4661 int start, end, overlaps_p;
4663 struct glyph *glyph, *last;
4664 int voffset;
4665 int glyph_not_available_p;
4667 xassert (s->f == XFRAME (s->w->frame));
4668 xassert (s->nchars == 0);
4669 xassert (start >= 0 && end > start);
4671 s->for_overlaps_p = overlaps_p,
4672 glyph = s->row->glyphs[s->area] + start;
4673 last = s->row->glyphs[s->area] + end;
4674 voffset = glyph->voffset;
4676 glyph_not_available_p = glyph->glyph_not_available_p;
4678 while (glyph < last
4679 && glyph->type == CHAR_GLYPH
4680 && glyph->voffset == voffset
4681 /* Same face id implies same font, nowadays. */
4682 && glyph->face_id == face_id
4683 && glyph->glyph_not_available_p == glyph_not_available_p)
4685 int two_byte_p;
4687 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4688 s->char2b + s->nchars,
4689 &two_byte_p);
4690 s->two_byte_p = two_byte_p;
4691 ++s->nchars;
4692 xassert (s->nchars <= end - start);
4693 s->width += glyph->pixel_width;
4694 ++glyph;
4697 s->font = s->face->font;
4698 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4700 /* If the specified font could not be loaded, use the frame's font,
4701 but record the fact that we couldn't load it in
4702 S->font_not_found_p so that we can draw rectangles for the
4703 characters of the glyph string. */
4704 if (s->font == NULL || glyph_not_available_p)
4706 s->font_not_found_p = 1;
4707 s->font = FRAME_FONT (s->f);
4710 /* Adjust base line for subscript/superscript text. */
4711 s->ybase += voffset;
4713 xassert (s->face && s->face->gc);
4714 return glyph - s->row->glyphs[s->area];
4718 /* Fill glyph string S from image glyph S->first_glyph. */
4720 static void
4721 x_fill_image_glyph_string (s)
4722 struct glyph_string *s;
4724 xassert (s->first_glyph->type == IMAGE_GLYPH);
4725 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4726 xassert (s->img);
4727 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4728 s->font = s->face->font;
4729 s->width = s->first_glyph->pixel_width;
4731 /* Adjust base line for subscript/superscript text. */
4732 s->ybase += s->first_glyph->voffset;
4736 /* Fill glyph string S from a sequence of stretch glyphs.
4738 ROW is the glyph row in which the glyphs are found, AREA is the
4739 area within the row. START is the index of the first glyph to
4740 consider, END is the index of the last + 1.
4742 Value is the index of the first glyph not in S. */
4744 static int
4745 x_fill_stretch_glyph_string (s, row, area, start, end)
4746 struct glyph_string *s;
4747 struct glyph_row *row;
4748 enum glyph_row_area area;
4749 int start, end;
4751 struct glyph *glyph, *last;
4752 int voffset, face_id;
4754 xassert (s->first_glyph->type == STRETCH_GLYPH);
4756 glyph = s->row->glyphs[s->area] + start;
4757 last = s->row->glyphs[s->area] + end;
4758 face_id = glyph->face_id;
4759 s->face = FACE_FROM_ID (s->f, face_id);
4760 s->font = s->face->font;
4761 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4762 s->width = glyph->pixel_width;
4763 voffset = glyph->voffset;
4765 for (++glyph;
4766 (glyph < last
4767 && glyph->type == STRETCH_GLYPH
4768 && glyph->voffset == voffset
4769 && glyph->face_id == face_id);
4770 ++glyph)
4771 s->width += glyph->pixel_width;
4773 /* Adjust base line for subscript/superscript text. */
4774 s->ybase += voffset;
4776 xassert (s->face && s->face->gc);
4777 return glyph - s->row->glyphs[s->area];
4781 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4782 of XChar2b structures for S; it can't be allocated in
4783 x_init_glyph_string because it must be allocated via `alloca'. W
4784 is the window on which S is drawn. ROW and AREA are the glyph row
4785 and area within the row from which S is constructed. START is the
4786 index of the first glyph structure covered by S. HL is a
4787 face-override for drawing S. */
4789 static void
4790 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4791 struct glyph_string *s;
4792 XChar2b *char2b;
4793 struct window *w;
4794 struct glyph_row *row;
4795 enum glyph_row_area area;
4796 int start;
4797 enum draw_glyphs_face hl;
4799 bzero (s, sizeof *s);
4800 s->w = w;
4801 s->f = XFRAME (w->frame);
4802 s->display = FRAME_X_DISPLAY (s->f);
4803 s->window = FRAME_X_WINDOW (s->f);
4804 s->char2b = char2b;
4805 s->hl = hl;
4806 s->row = row;
4807 s->area = area;
4808 s->first_glyph = row->glyphs[area] + start;
4809 s->height = row->height;
4810 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4812 /* Display the internal border below the tool-bar window. */
4813 if (s->w == XWINDOW (s->f->tool_bar_window))
4814 s->y -= s->f->output_data.x->internal_border_width;
4816 s->ybase = s->y + row->ascent;
4820 /* Set background width of glyph string S. START is the index of the
4821 first glyph following S. LAST_X is the right-most x-position + 1
4822 in the drawing area. */
4824 static INLINE void
4825 x_set_glyph_string_background_width (s, start, last_x)
4826 struct glyph_string *s;
4827 int start;
4828 int last_x;
4830 /* If the face of this glyph string has to be drawn to the end of
4831 the drawing area, set S->extends_to_end_of_line_p. */
4832 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4834 if (start == s->row->used[s->area]
4835 && s->hl == DRAW_NORMAL_TEXT
4836 && s->area == TEXT_AREA
4837 && (s->row->fill_line_p
4838 || s->face->background != default_face->background
4839 || s->face->stipple != default_face->stipple))
4840 s->extends_to_end_of_line_p = 1;
4842 /* If S extends its face to the end of the line, set its
4843 background_width to the distance to the right edge of the drawing
4844 area. */
4845 if (s->extends_to_end_of_line_p)
4846 s->background_width = last_x - s->x + 1;
4847 else
4848 s->background_width = s->width;
4852 /* Add a glyph string for a stretch glyph to the list of strings
4853 between HEAD and TAIL. START is the index of the stretch glyph in
4854 row area AREA of glyph row ROW. END is the index of the last glyph
4855 in that glyph row area. X is the current output position assigned
4856 to the new glyph string constructed. HL overrides that face of the
4857 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4858 is the right-most x-position of the drawing area. */
4860 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4861 and below -- keep them on one line. */
4862 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4863 do \
4865 s = (struct glyph_string *) alloca (sizeof *s); \
4866 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4867 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4868 x_append_glyph_string (&HEAD, &TAIL, s); \
4869 s->x = (X); \
4871 while (0)
4874 /* Add a glyph string for an image glyph to the list of strings
4875 between HEAD and TAIL. START is the index of the image glyph in
4876 row area AREA of glyph row ROW. END is the index of the last glyph
4877 in that glyph row area. X is the current output position assigned
4878 to the new glyph string constructed. HL overrides that face of the
4879 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4880 is the right-most x-position of the drawing area. */
4882 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4883 do \
4885 s = (struct glyph_string *) alloca (sizeof *s); \
4886 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4887 x_fill_image_glyph_string (s); \
4888 x_append_glyph_string (&HEAD, &TAIL, s); \
4889 ++START; \
4890 s->x = (X); \
4892 while (0)
4895 /* Add a glyph string for a sequence of character glyphs to the list
4896 of strings between HEAD and TAIL. START is the index of the first
4897 glyph in row area AREA of glyph row ROW that is part of the new
4898 glyph string. END is the index of the last glyph in that glyph row
4899 area. X is the current output position assigned to the new glyph
4900 string constructed. HL overrides that face of the glyph; e.g. it
4901 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4902 right-most x-position of the drawing area. */
4904 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4905 do \
4907 int c, face_id; \
4908 XChar2b *char2b; \
4910 c = (ROW)->glyphs[AREA][START].u.ch; \
4911 face_id = (ROW)->glyphs[AREA][START].face_id; \
4913 s = (struct glyph_string *) alloca (sizeof *s); \
4914 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4915 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4916 x_append_glyph_string (&HEAD, &TAIL, s); \
4917 s->x = (X); \
4918 START = x_fill_glyph_string (s, face_id, START, END, \
4919 OVERLAPS_P); \
4921 while (0)
4924 /* Add a glyph string for a composite sequence to the list of strings
4925 between HEAD and TAIL. START is the index of the first glyph in
4926 row area AREA of glyph row ROW that is part of the new glyph
4927 string. END is the index of the last glyph in that glyph row area.
4928 X is the current output position assigned to the new glyph string
4929 constructed. HL overrides that face of the glyph; e.g. it is
4930 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4931 x-position of the drawing area. */
4933 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4934 do { \
4935 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4936 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4937 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4938 struct composition *cmp = composition_table[cmp_id]; \
4939 int glyph_len = cmp->glyph_len; \
4940 XChar2b *char2b; \
4941 struct face **faces; \
4942 struct glyph_string *first_s = NULL; \
4943 int n; \
4945 base_face = base_face->ascii_face; \
4946 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4947 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4948 /* At first, fill in `char2b' and `faces'. */ \
4949 for (n = 0; n < glyph_len; n++) \
4951 int c = COMPOSITION_GLYPH (cmp, n); \
4952 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4953 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4954 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4955 this_face_id, char2b + n, 1); \
4958 /* Make glyph_strings for each glyph sequence that is drawable by \
4959 the same face, and append them to HEAD/TAIL. */ \
4960 for (n = 0; n < cmp->glyph_len;) \
4962 s = (struct glyph_string *) alloca (sizeof *s); \
4963 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4964 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4965 s->cmp = cmp; \
4966 s->gidx = n; \
4967 s->x = (X); \
4969 if (n == 0) \
4970 first_s = s; \
4972 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4975 ++START; \
4976 s = first_s; \
4977 } while (0)
4980 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4981 of AREA of glyph row ROW on window W between indices START and END.
4982 HL overrides the face for drawing glyph strings, e.g. it is
4983 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4984 x-positions of the drawing area.
4986 This is an ugly monster macro construct because we must use alloca
4987 to allocate glyph strings (because x_draw_glyphs can be called
4988 asynchronously). */
4990 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4991 do \
4993 HEAD = TAIL = NULL; \
4994 while (START < END) \
4996 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4997 switch (first_glyph->type) \
4999 case CHAR_GLYPH: \
5000 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
5001 TAIL, HL, X, LAST_X, \
5002 OVERLAPS_P); \
5003 break; \
5005 case COMPOSITE_GLYPH: \
5006 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
5007 HEAD, TAIL, HL, X, LAST_X,\
5008 OVERLAPS_P); \
5009 break; \
5011 case STRETCH_GLYPH: \
5012 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
5013 HEAD, TAIL, HL, X, LAST_X); \
5014 break; \
5016 case IMAGE_GLYPH: \
5017 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
5018 TAIL, HL, X, LAST_X); \
5019 break; \
5021 default: \
5022 abort (); \
5025 x_set_glyph_string_background_width (s, START, LAST_X); \
5026 (X) += s->width; \
5029 while (0)
5032 /* Draw glyphs between START and END in AREA of ROW on window W,
5033 starting at x-position X. X is relative to AREA in W. HL is a
5034 face-override with the following meaning:
5036 DRAW_NORMAL_TEXT draw normally
5037 DRAW_CURSOR draw in cursor face
5038 DRAW_MOUSE_FACE draw in mouse face.
5039 DRAW_INVERSE_VIDEO draw in mode line face
5040 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
5041 DRAW_IMAGE_RAISED draw an image with a raised relief around it
5043 If REAL_START is non-null, return in *REAL_START the real starting
5044 position for display. This can be different from START in case
5045 overlapping glyphs must be displayed. If REAL_END is non-null,
5046 return in *REAL_END the real end position for display. This can be
5047 different from END in case overlapping glyphs must be displayed.
5049 If OVERLAPS_P is non-zero, draw only the foreground of characters
5050 and clip to the physical height of ROW.
5052 Value is the x-position reached, relative to AREA of W. */
5054 static int
5055 x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
5056 overlaps_p)
5057 struct window *w;
5058 int x;
5059 struct glyph_row *row;
5060 enum glyph_row_area area;
5061 int start, end;
5062 enum draw_glyphs_face hl;
5063 int *real_start, *real_end;
5064 int overlaps_p;
5066 struct glyph_string *head, *tail;
5067 struct glyph_string *s;
5068 int last_x, area_width;
5069 int x_reached;
5070 int i, j;
5072 /* Let's rather be paranoid than getting a SEGV. */
5073 end = min (end, row->used[area]);
5074 start = max (0, start);
5075 start = min (end, start);
5076 if (real_start)
5077 *real_start = start;
5078 if (real_end)
5079 *real_end = end;
5081 /* Translate X to frame coordinates. Set last_x to the right
5082 end of the drawing area. */
5083 if (row->full_width_p)
5085 /* X is relative to the left edge of W, without scroll bars
5086 or flag areas. */
5087 struct frame *f = XFRAME (w->frame);
5088 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
5089 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
5091 x += window_left_x;
5092 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
5093 last_x = window_left_x + area_width;
5095 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5097 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5098 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
5099 last_x += width;
5100 else
5101 x -= width;
5104 x += FRAME_INTERNAL_BORDER_WIDTH (f);
5105 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
5107 else
5109 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
5110 area_width = window_box_width (w, area);
5111 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
5114 /* Build a doubly-linked list of glyph_string structures between
5115 head and tail from what we have to draw. Note that the macro
5116 BUILD_GLYPH_STRINGS will modify its start parameter. That's
5117 the reason we use a separate variable `i'. */
5118 i = start;
5119 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
5120 overlaps_p);
5121 if (tail)
5122 x_reached = tail->x + tail->background_width;
5123 else
5124 x_reached = x;
5126 /* If there are any glyphs with lbearing < 0 or rbearing > width in
5127 the row, redraw some glyphs in front or following the glyph
5128 strings built above. */
5129 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
5131 int dummy_x = 0;
5132 struct glyph_string *h, *t;
5134 /* Compute overhangs for all glyph strings. */
5135 for (s = head; s; s = s->next)
5136 x_compute_glyph_string_overhangs (s);
5138 /* Prepend glyph strings for glyphs in front of the first glyph
5139 string that are overwritten because of the first glyph
5140 string's left overhang. The background of all strings
5141 prepended must be drawn because the first glyph string
5142 draws over it. */
5143 i = x_left_overwritten (head);
5144 if (i >= 0)
5146 j = i;
5147 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
5148 DRAW_NORMAL_TEXT, dummy_x, last_x,
5149 overlaps_p);
5150 start = i;
5151 if (real_start)
5152 *real_start = start;
5153 x_compute_overhangs_and_x (t, head->x, 1);
5154 x_prepend_glyph_string_lists (&head, &tail, h, t);
5157 /* Prepend glyph strings for glyphs in front of the first glyph
5158 string that overwrite that glyph string because of their
5159 right overhang. For these strings, only the foreground must
5160 be drawn, because it draws over the glyph string at `head'.
5161 The background must not be drawn because this would overwrite
5162 right overhangs of preceding glyphs for which no glyph
5163 strings exist. */
5164 i = x_left_overwriting (head);
5165 if (i >= 0)
5167 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
5168 DRAW_NORMAL_TEXT, dummy_x, last_x,
5169 overlaps_p);
5170 for (s = h; s; s = s->next)
5171 s->background_filled_p = 1;
5172 if (real_start)
5173 *real_start = i;
5174 x_compute_overhangs_and_x (t, head->x, 1);
5175 x_prepend_glyph_string_lists (&head, &tail, h, t);
5178 /* Append glyphs strings for glyphs following the last glyph
5179 string tail that are overwritten by tail. The background of
5180 these strings has to be drawn because tail's foreground draws
5181 over it. */
5182 i = x_right_overwritten (tail);
5183 if (i >= 0)
5185 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5186 DRAW_NORMAL_TEXT, x, last_x,
5187 overlaps_p);
5188 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5189 x_append_glyph_string_lists (&head, &tail, h, t);
5190 if (real_end)
5191 *real_end = i;
5194 /* Append glyph strings for glyphs following the last glyph
5195 string tail that overwrite tail. The foreground of such
5196 glyphs has to be drawn because it writes into the background
5197 of tail. The background must not be drawn because it could
5198 paint over the foreground of following glyphs. */
5199 i = x_right_overwriting (tail);
5200 if (i >= 0)
5202 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5203 DRAW_NORMAL_TEXT, x, last_x,
5204 overlaps_p);
5205 for (s = h; s; s = s->next)
5206 s->background_filled_p = 1;
5207 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5208 x_append_glyph_string_lists (&head, &tail, h, t);
5209 if (real_end)
5210 *real_end = i;
5214 /* Draw all strings. */
5215 for (s = head; s; s = s->next)
5216 x_draw_glyph_string (s);
5218 /* Value is the x-position up to which drawn, relative to AREA of W.
5219 This doesn't include parts drawn because of overhangs. */
5220 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5221 if (!row->full_width_p)
5223 if (area > LEFT_MARGIN_AREA)
5224 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5225 if (area > TEXT_AREA)
5226 x_reached -= window_box_width (w, TEXT_AREA);
5229 return x_reached;
5233 /* Fix the display of area AREA of overlapping row ROW in window W. */
5235 static void
5236 x_fix_overlapping_area (w, row, area)
5237 struct window *w;
5238 struct glyph_row *row;
5239 enum glyph_row_area area;
5241 int i, x;
5243 BLOCK_INPUT;
5245 if (area == LEFT_MARGIN_AREA)
5246 x = 0;
5247 else if (area == TEXT_AREA)
5248 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5249 else
5250 x = (window_box_width (w, LEFT_MARGIN_AREA)
5251 + window_box_width (w, TEXT_AREA));
5253 for (i = 0; i < row->used[area];)
5255 if (row->glyphs[area][i].overlaps_vertically_p)
5257 int start = i, start_x = x;
5261 x += row->glyphs[area][i].pixel_width;
5262 ++i;
5264 while (i < row->used[area]
5265 && row->glyphs[area][i].overlaps_vertically_p);
5267 x_draw_glyphs (w, start_x, row, area, start, i,
5268 (row->inverse_p
5269 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5270 NULL, NULL, 1);
5272 else
5274 x += row->glyphs[area][i].pixel_width;
5275 ++i;
5279 UNBLOCK_INPUT;
5283 /* Output LEN glyphs starting at START at the nominal cursor position.
5284 Advance the nominal cursor over the text. The global variable
5285 updated_window contains the window being updated, updated_row is
5286 the glyph row being updated, and updated_area is the area of that
5287 row being updated. */
5289 static void
5290 x_write_glyphs (start, len)
5291 struct glyph *start;
5292 int len;
5294 int x, hpos, real_start, real_end;
5296 xassert (updated_window && updated_row);
5297 BLOCK_INPUT;
5299 /* Write glyphs. */
5301 hpos = start - updated_row->glyphs[updated_area];
5302 x = x_draw_glyphs (updated_window, output_cursor.x,
5303 updated_row, updated_area,
5304 hpos, hpos + len,
5305 (updated_row->inverse_p
5306 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5307 &real_start, &real_end, 0);
5309 /* If we drew over the cursor, note that it is not visible any more. */
5310 note_overwritten_text_cursor (updated_window, real_start,
5311 real_end - real_start);
5313 UNBLOCK_INPUT;
5315 /* Advance the output cursor. */
5316 output_cursor.hpos += len;
5317 output_cursor.x = x;
5321 /* Insert LEN glyphs from START at the nominal cursor position. */
5323 static void
5324 x_insert_glyphs (start, len)
5325 struct glyph *start;
5326 register int len;
5328 struct frame *f;
5329 struct window *w;
5330 int line_height, shift_by_width, shifted_region_width;
5331 struct glyph_row *row;
5332 struct glyph *glyph;
5333 int frame_x, frame_y, hpos, real_start, real_end;
5335 xassert (updated_window && updated_row);
5336 BLOCK_INPUT;
5337 w = updated_window;
5338 f = XFRAME (WINDOW_FRAME (w));
5340 /* Get the height of the line we are in. */
5341 row = updated_row;
5342 line_height = row->height;
5344 /* Get the width of the glyphs to insert. */
5345 shift_by_width = 0;
5346 for (glyph = start; glyph < start + len; ++glyph)
5347 shift_by_width += glyph->pixel_width;
5349 /* Get the width of the region to shift right. */
5350 shifted_region_width = (window_box_width (w, updated_area)
5351 - output_cursor.x
5352 - shift_by_width);
5354 /* Shift right. */
5355 frame_x = window_box_left (w, updated_area) + output_cursor.x;
5356 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5357 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5358 f->output_data.x->normal_gc,
5359 frame_x, frame_y,
5360 shifted_region_width, line_height,
5361 frame_x + shift_by_width, frame_y);
5363 /* Write the glyphs. */
5364 hpos = start - row->glyphs[updated_area];
5365 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5366 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
5367 note_overwritten_text_cursor (w, real_start, real_end - real_start);
5369 /* Advance the output cursor. */
5370 output_cursor.hpos += len;
5371 output_cursor.x += shift_by_width;
5372 UNBLOCK_INPUT;
5376 /* Delete N glyphs at the nominal cursor position. Not implemented
5377 for X frames. */
5379 static void
5380 x_delete_glyphs (n)
5381 register int n;
5383 abort ();
5387 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5388 If they are <= 0, this is probably an error. */
5390 void
5391 x_clear_area (dpy, window, x, y, width, height, exposures)
5392 Display *dpy;
5393 Window window;
5394 int x, y;
5395 int width, height;
5396 int exposures;
5398 xassert (width > 0 && height > 0);
5399 XClearArea (dpy, window, x, y, width, height, exposures);
5403 /* Erase the current text line from the nominal cursor position
5404 (inclusive) to pixel column TO_X (exclusive). The idea is that
5405 everything from TO_X onward is already erased.
5407 TO_X is a pixel position relative to updated_area of
5408 updated_window. TO_X == -1 means clear to the end of this area. */
5410 static void
5411 x_clear_end_of_line (to_x)
5412 int to_x;
5414 struct frame *f;
5415 struct window *w = updated_window;
5416 int max_x, min_y, max_y;
5417 int from_x, from_y, to_y;
5419 xassert (updated_window && updated_row);
5420 f = XFRAME (w->frame);
5422 if (updated_row->full_width_p)
5424 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5425 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5426 && !w->pseudo_window_p)
5427 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5429 else
5430 max_x = window_box_width (w, updated_area);
5431 max_y = window_text_bottom_y (w);
5433 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5434 of window. For TO_X > 0, truncate to end of drawing area. */
5435 if (to_x == 0)
5436 return;
5437 else if (to_x < 0)
5438 to_x = max_x;
5439 else
5440 to_x = min (to_x, max_x);
5442 to_y = min (max_y, output_cursor.y + updated_row->height);
5444 /* Notice if the cursor will be cleared by this operation. */
5445 if (!updated_row->full_width_p)
5446 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
5448 from_x = output_cursor.x;
5450 /* Translate to frame coordinates. */
5451 if (updated_row->full_width_p)
5453 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5454 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5456 else
5458 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5459 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5462 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5463 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5464 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5466 /* Prevent inadvertently clearing to end of the X window. */
5467 if (to_x > from_x && to_y > from_y)
5469 BLOCK_INPUT;
5470 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5471 from_x, from_y, to_x - from_x, to_y - from_y,
5472 False);
5473 UNBLOCK_INPUT;
5478 /* Clear entire frame. If updating_frame is non-null, clear that
5479 frame. Otherwise clear the selected frame. */
5481 static void
5482 x_clear_frame ()
5484 struct frame *f;
5486 if (updating_frame)
5487 f = updating_frame;
5488 else
5489 f = SELECTED_FRAME ();
5491 /* Clearing the frame will erase any cursor, so mark them all as no
5492 longer visible. */
5493 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5494 output_cursor.hpos = output_cursor.vpos = 0;
5495 output_cursor.x = -1;
5497 /* We don't set the output cursor here because there will always
5498 follow an explicit cursor_to. */
5499 BLOCK_INPUT;
5500 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5502 /* We have to clear the scroll bars, too. If we have changed
5503 colors or something like that, then they should be notified. */
5504 x_scroll_bar_clear (f);
5506 XFlush (FRAME_X_DISPLAY (f));
5507 UNBLOCK_INPUT;
5512 /* Invert the middle quarter of the frame for .15 sec. */
5514 /* We use the select system call to do the waiting, so we have to make
5515 sure it's available. If it isn't, we just won't do visual bells. */
5517 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5520 /* Subtract the `struct timeval' values X and Y, storing the result in
5521 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5523 static int
5524 timeval_subtract (result, x, y)
5525 struct timeval *result, x, y;
5527 /* Perform the carry for the later subtraction by updating y. This
5528 is safer because on some systems the tv_sec member is unsigned. */
5529 if (x.tv_usec < y.tv_usec)
5531 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5532 y.tv_usec -= 1000000 * nsec;
5533 y.tv_sec += nsec;
5536 if (x.tv_usec - y.tv_usec > 1000000)
5538 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5539 y.tv_usec += 1000000 * nsec;
5540 y.tv_sec -= nsec;
5543 /* Compute the time remaining to wait. tv_usec is certainly
5544 positive. */
5545 result->tv_sec = x.tv_sec - y.tv_sec;
5546 result->tv_usec = x.tv_usec - y.tv_usec;
5548 /* Return indication of whether the result should be considered
5549 negative. */
5550 return x.tv_sec < y.tv_sec;
5553 void
5554 XTflash (f)
5555 struct frame *f;
5557 BLOCK_INPUT;
5560 GC gc;
5562 /* Create a GC that will use the GXxor function to flip foreground
5563 pixels into background pixels. */
5565 XGCValues values;
5567 values.function = GXxor;
5568 values.foreground = (f->output_data.x->foreground_pixel
5569 ^ f->output_data.x->background_pixel);
5571 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5572 GCFunction | GCForeground, &values);
5576 /* Get the height not including a menu bar widget. */
5577 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5578 /* Height of each line to flash. */
5579 int flash_height = FRAME_LINE_HEIGHT (f);
5580 /* These will be the left and right margins of the rectangles. */
5581 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5582 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5584 int width;
5586 /* Don't flash the area between a scroll bar and the frame
5587 edge it is next to. */
5588 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5590 case vertical_scroll_bar_left:
5591 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5592 break;
5594 case vertical_scroll_bar_right:
5595 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5596 break;
5598 default:
5599 break;
5602 width = flash_right - flash_left;
5604 /* If window is tall, flash top and bottom line. */
5605 if (height > 3 * FRAME_LINE_HEIGHT (f))
5607 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5608 flash_left,
5609 (FRAME_INTERNAL_BORDER_WIDTH (f)
5610 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5611 width, flash_height);
5612 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5613 flash_left,
5614 (height - flash_height
5615 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5616 width, flash_height);
5618 else
5619 /* If it is short, flash it all. */
5620 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5621 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5622 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5624 x_flush (f);
5627 struct timeval wakeup;
5629 EMACS_GET_TIME (wakeup);
5631 /* Compute time to wait until, propagating carry from usecs. */
5632 wakeup.tv_usec += 150000;
5633 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5634 wakeup.tv_usec %= 1000000;
5636 /* Keep waiting until past the time wakeup or any input gets
5637 available. */
5638 while (! detect_input_pending ())
5640 struct timeval current;
5641 struct timeval timeout;
5643 EMACS_GET_TIME (current);
5645 /* Break if result would be negative. */
5646 if (timeval_subtract (&current, wakeup, current))
5647 break;
5649 /* How long `select' should wait. */
5650 timeout.tv_sec = 0;
5651 timeout.tv_usec = 10000;
5653 /* Try to wait that long--but we might wake up sooner. */
5654 select (0, NULL, NULL, NULL, &timeout);
5658 /* If window is tall, flash top and bottom line. */
5659 if (height > 3 * FRAME_LINE_HEIGHT (f))
5661 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5662 flash_left,
5663 (FRAME_INTERNAL_BORDER_WIDTH (f)
5664 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5665 width, flash_height);
5666 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5667 flash_left,
5668 (height - flash_height
5669 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5670 width, flash_height);
5672 else
5673 /* If it is short, flash it all. */
5674 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5675 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5676 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5678 XFreeGC (FRAME_X_DISPLAY (f), gc);
5679 x_flush (f);
5683 UNBLOCK_INPUT;
5686 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5689 /* Make audible bell. */
5691 void
5692 XTring_bell ()
5694 struct frame *f = SELECTED_FRAME ();
5696 if (FRAME_X_DISPLAY (f))
5698 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5699 if (visible_bell)
5700 XTflash (f);
5701 else
5702 #endif
5704 BLOCK_INPUT;
5705 XBell (FRAME_X_DISPLAY (f), 0);
5706 XFlush (FRAME_X_DISPLAY (f));
5707 UNBLOCK_INPUT;
5713 /* Specify how many text lines, from the top of the window,
5714 should be affected by insert-lines and delete-lines operations.
5715 This, and those operations, are used only within an update
5716 that is bounded by calls to x_update_begin and x_update_end. */
5718 static void
5719 XTset_terminal_window (n)
5720 register int n;
5722 /* This function intentionally left blank. */
5727 /***********************************************************************
5728 Line Dance
5729 ***********************************************************************/
5731 /* Perform an insert-lines or delete-lines operation, inserting N
5732 lines or deleting -N lines at vertical position VPOS. */
5734 static void
5735 x_ins_del_lines (vpos, n)
5736 int vpos, n;
5738 abort ();
5742 /* Scroll part of the display as described by RUN. */
5744 static void
5745 x_scroll_run (w, run)
5746 struct window *w;
5747 struct run *run;
5749 struct frame *f = XFRAME (w->frame);
5750 int x, y, width, height, from_y, to_y, bottom_y;
5752 /* Get frame-relative bounding box of the text display area of W,
5753 without mode lines. Include in this box the flags areas to the
5754 left and right of W. */
5755 window_box (w, -1, &x, &y, &width, &height);
5756 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5757 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
5759 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5760 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5761 bottom_y = y + height;
5763 if (to_y < from_y)
5765 /* Scrolling up. Make sure we don't copy part of the mode
5766 line at the bottom. */
5767 if (from_y + run->height > bottom_y)
5768 height = bottom_y - from_y;
5769 else
5770 height = run->height;
5772 else
5774 /* Scolling down. Make sure we don't copy over the mode line.
5775 at the bottom. */
5776 if (to_y + run->height > bottom_y)
5777 height = bottom_y - to_y;
5778 else
5779 height = run->height;
5782 BLOCK_INPUT;
5784 /* Cursor off. Will be switched on again in x_update_window_end. */
5785 updated_window = w;
5786 x_clear_cursor (w);
5788 XCopyArea (FRAME_X_DISPLAY (f),
5789 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5790 f->output_data.x->normal_gc,
5791 x, from_y,
5792 width, height,
5793 x, to_y);
5795 UNBLOCK_INPUT;
5800 /***********************************************************************
5801 Exposure Events
5802 ***********************************************************************/
5804 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5805 corner of the exposed rectangle. W and H are width and height of
5806 the exposed area. All are pixel values. W or H zero means redraw
5807 the entire frame. */
5809 static void
5810 expose_frame (f, x, y, w, h)
5811 struct frame *f;
5812 int x, y, w, h;
5814 XRectangle r;
5816 TRACE ((stderr, "expose_frame "));
5818 /* No need to redraw if frame will be redrawn soon. */
5819 if (FRAME_GARBAGED_P (f))
5821 TRACE ((stderr, " garbaged\n"));
5822 return;
5825 /* If basic faces haven't been realized yet, there is no point in
5826 trying to redraw anything. This can happen when we get an expose
5827 event while Emacs is starting, e.g. by moving another window. */
5828 if (FRAME_FACE_CACHE (f) == NULL
5829 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5831 TRACE ((stderr, " no faces\n"));
5832 return;
5835 if (w == 0 || h == 0)
5837 r.x = r.y = 0;
5838 r.width = CANON_X_UNIT (f) * f->width;
5839 r.height = CANON_Y_UNIT (f) * f->height;
5841 else
5843 r.x = x;
5844 r.y = y;
5845 r.width = w;
5846 r.height = h;
5849 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5850 expose_window_tree (XWINDOW (f->root_window), &r);
5852 if (WINDOWP (f->tool_bar_window))
5853 expose_window (XWINDOW (f->tool_bar_window), &r);
5855 #ifndef USE_X_TOOLKIT
5856 if (WINDOWP (f->menu_bar_window))
5857 expose_window (XWINDOW (f->menu_bar_window), &r);
5858 #endif /* not USE_X_TOOLKIT */
5862 /* Redraw (parts) of all windows in the window tree rooted at W that
5863 intersect R. R contains frame pixel coordinates. */
5865 static void
5866 expose_window_tree (w, r)
5867 struct window *w;
5868 XRectangle *r;
5870 while (w)
5872 if (!NILP (w->hchild))
5873 expose_window_tree (XWINDOW (w->hchild), r);
5874 else if (!NILP (w->vchild))
5875 expose_window_tree (XWINDOW (w->vchild), r);
5876 else if (expose_window (w, r) == 0)
5877 break;
5878 w = NILP (w->next) ? NULL : XWINDOW (w->next);
5883 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5884 which intersects rectangle R. R is in window-relative coordinates. */
5886 static void
5887 expose_area (w, row, r, area)
5888 struct window *w;
5889 struct glyph_row *row;
5890 XRectangle *r;
5891 enum glyph_row_area area;
5893 struct glyph *first = row->glyphs[area];
5894 struct glyph *end = row->glyphs[area] + row->used[area];
5895 struct glyph *last;
5896 int first_x, start_x, x;
5898 if (area == TEXT_AREA && row->fill_line_p)
5899 /* If row extends face to end of line write the whole line. */
5900 x_draw_glyphs (w, 0, row, area,
5901 0, row->used[area],
5902 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5903 NULL, NULL, 0);
5904 else
5906 /* Set START_X to the window-relative start position for drawing glyphs of
5907 AREA. The first glyph of the text area can be partially visible.
5908 The first glyphs of other areas cannot. */
5909 if (area == LEFT_MARGIN_AREA)
5910 start_x = 0;
5911 else if (area == TEXT_AREA)
5912 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5913 else
5914 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5915 + window_box_width (w, TEXT_AREA));
5916 x = start_x;
5918 /* Find the first glyph that must be redrawn. */
5919 while (first < end
5920 && x + first->pixel_width < r->x)
5922 x += first->pixel_width;
5923 ++first;
5926 /* Find the last one. */
5927 last = first;
5928 first_x = x;
5929 while (last < end
5930 && x < r->x + r->width)
5932 x += last->pixel_width;
5933 ++last;
5936 /* Repaint. */
5937 if (last > first)
5938 x_draw_glyphs (w, first_x - start_x, row, area,
5939 first - row->glyphs[area],
5940 last - row->glyphs[area],
5941 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5942 NULL, NULL, 0);
5947 /* Redraw the parts of the glyph row ROW on window W intersecting
5948 rectangle R. R is in window-relative coordinates. */
5950 static void
5951 expose_line (w, row, r)
5952 struct window *w;
5953 struct glyph_row *row;
5954 XRectangle *r;
5956 xassert (row->enabled_p);
5958 if (row->mode_line_p || w->pseudo_window_p)
5959 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5960 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5961 NULL, NULL, 0);
5962 else
5964 if (row->used[LEFT_MARGIN_AREA])
5965 expose_area (w, row, r, LEFT_MARGIN_AREA);
5966 if (row->used[TEXT_AREA])
5967 expose_area (w, row, r, TEXT_AREA);
5968 if (row->used[RIGHT_MARGIN_AREA])
5969 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5970 x_draw_row_bitmaps (w, row);
5975 /* Return non-zero if W's cursor intersects rectangle R. */
5977 static int
5978 x_phys_cursor_in_rect_p (w, r)
5979 struct window *w;
5980 XRectangle *r;
5982 XRectangle cr, result;
5983 struct glyph *cursor_glyph;
5985 cursor_glyph = get_phys_cursor_glyph (w);
5986 if (cursor_glyph)
5988 cr.x = w->phys_cursor.x;
5989 cr.y = w->phys_cursor.y;
5990 cr.width = cursor_glyph->pixel_width;
5991 cr.height = w->phys_cursor_height;
5992 return x_intersect_rectangles (&cr, r, &result);
5994 else
5995 return 0;
5999 /* Redraw the part of window W intersection rectangle FR. Pixel
6000 coordinates in FR are frame-relative. Call this function with
6001 input blocked. */
6003 static int
6004 expose_window (w, fr)
6005 struct window *w;
6006 XRectangle *fr;
6008 struct frame *f = XFRAME (w->frame);
6009 XRectangle wr, r;
6011 /* If window is not yet fully initialized, do nothing. This can
6012 happen when toolkit scroll bars are used and a window is split.
6013 Reconfiguring the scroll bar will generate an expose for a newly
6014 created window. */
6015 if (w->current_matrix == NULL)
6016 return 1;
6018 /* When we're currently updating the window, display and current
6019 matrix usually don't agree. Arrange for a thorough display
6020 later. */
6021 if (w == updated_window)
6023 SET_FRAME_GARBAGED (f);
6024 return 0;
6027 /* Frame-relative pixel rectangle of W. */
6028 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
6029 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
6030 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
6031 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
6033 if (x_intersect_rectangles (fr, &wr, &r))
6035 int yb = window_text_bottom_y (w);
6036 struct glyph_row *row;
6037 int cursor_cleared_p;
6039 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
6040 r.x, r.y, r.width, r.height));
6042 /* Convert to window coordinates. */
6043 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
6044 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
6046 /* Turn off the cursor. */
6047 if (!w->pseudo_window_p
6048 && x_phys_cursor_in_rect_p (w, &r))
6050 x_clear_cursor (w);
6051 cursor_cleared_p = 1;
6053 else
6054 cursor_cleared_p = 0;
6056 /* Find the first row intersecting the rectangle R. */
6057 for (row = w->current_matrix->rows;
6058 row->enabled_p;
6059 ++row)
6061 int y0 = row->y;
6062 int y1 = MATRIX_ROW_BOTTOM_Y (row);
6064 if ((y0 >= r.y && y0 < r.y + r.height)
6065 || (y1 > r.y && y1 < r.y + r.height)
6066 || (r.y >= y0 && r.y < y1)
6067 || (r.y + r.height > y0 && r.y + r.height < y1))
6068 expose_line (w, row, &r);
6070 if (y1 >= yb)
6071 break;
6074 /* Display the mode line if there is one. */
6075 if (WINDOW_WANTS_MODELINE_P (w)
6076 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
6077 row->enabled_p)
6078 && row->y < r.y + r.height)
6079 expose_line (w, row, &r);
6081 if (!w->pseudo_window_p)
6083 /* Draw border between windows. */
6084 x_draw_vertical_border (w);
6086 /* Turn the cursor on again. */
6087 if (cursor_cleared_p)
6088 x_update_window_cursor (w, 1);
6092 return 1;
6096 /* Determine the intersection of two rectangles R1 and R2. Return
6097 the intersection in *RESULT. Value is non-zero if RESULT is not
6098 empty. */
6100 static int
6101 x_intersect_rectangles (r1, r2, result)
6102 XRectangle *r1, *r2, *result;
6104 XRectangle *left, *right;
6105 XRectangle *upper, *lower;
6106 int intersection_p = 0;
6108 /* Rearrange so that R1 is the left-most rectangle. */
6109 if (r1->x < r2->x)
6110 left = r1, right = r2;
6111 else
6112 left = r2, right = r1;
6114 /* X0 of the intersection is right.x0, if this is inside R1,
6115 otherwise there is no intersection. */
6116 if (right->x <= left->x + left->width)
6118 result->x = right->x;
6120 /* The right end of the intersection is the minimum of the
6121 the right ends of left and right. */
6122 result->width = (min (left->x + left->width, right->x + right->width)
6123 - result->x);
6125 /* Same game for Y. */
6126 if (r1->y < r2->y)
6127 upper = r1, lower = r2;
6128 else
6129 upper = r2, lower = r1;
6131 /* The upper end of the intersection is lower.y0, if this is inside
6132 of upper. Otherwise, there is no intersection. */
6133 if (lower->y <= upper->y + upper->height)
6135 result->y = lower->y;
6137 /* The lower end of the intersection is the minimum of the lower
6138 ends of upper and lower. */
6139 result->height = (min (lower->y + lower->height,
6140 upper->y + upper->height)
6141 - result->y);
6142 intersection_p = 1;
6146 return intersection_p;
6153 static void
6154 frame_highlight (f)
6155 struct frame *f;
6157 /* We used to only do this if Vx_no_window_manager was non-nil, but
6158 the ICCCM (section 4.1.6) says that the window's border pixmap
6159 and border pixel are window attributes which are "private to the
6160 client", so we can always change it to whatever we want. */
6161 BLOCK_INPUT;
6162 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6163 f->output_data.x->border_pixel);
6164 UNBLOCK_INPUT;
6165 x_update_cursor (f, 1);
6168 static void
6169 frame_unhighlight (f)
6170 struct frame *f;
6172 /* We used to only do this if Vx_no_window_manager was non-nil, but
6173 the ICCCM (section 4.1.6) says that the window's border pixmap
6174 and border pixel are window attributes which are "private to the
6175 client", so we can always change it to whatever we want. */
6176 BLOCK_INPUT;
6177 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6178 f->output_data.x->border_tile);
6179 UNBLOCK_INPUT;
6180 x_update_cursor (f, 1);
6183 /* The focus has changed. Update the frames as necessary to reflect
6184 the new situation. Note that we can't change the selected frame
6185 here, because the Lisp code we are interrupting might become confused.
6186 Each event gets marked with the frame in which it occurred, so the
6187 Lisp code can tell when the switch took place by examining the events. */
6189 static void
6190 x_new_focus_frame (dpyinfo, frame)
6191 struct x_display_info *dpyinfo;
6192 struct frame *frame;
6194 struct frame *old_focus = dpyinfo->x_focus_frame;
6196 if (frame != dpyinfo->x_focus_frame)
6198 /* Set this before calling other routines, so that they see
6199 the correct value of x_focus_frame. */
6200 dpyinfo->x_focus_frame = frame;
6202 if (old_focus && old_focus->auto_lower)
6203 x_lower_frame (old_focus);
6205 #if 0
6206 selected_frame = frame;
6207 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6208 selected_frame);
6209 Fselect_window (selected_frame->selected_window);
6210 choose_minibuf_frame ();
6211 #endif /* ! 0 */
6213 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6214 pending_autoraise_frame = dpyinfo->x_focus_frame;
6215 else
6216 pending_autoraise_frame = 0;
6219 x_frame_rehighlight (dpyinfo);
6222 /* Handle an event saying the mouse has moved out of an Emacs frame. */
6224 void
6225 x_mouse_leave (dpyinfo)
6226 struct x_display_info *dpyinfo;
6228 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
6231 /* The focus has changed, or we have redirected a frame's focus to
6232 another frame (this happens when a frame uses a surrogate
6233 mini-buffer frame). Shift the highlight as appropriate.
6235 The FRAME argument doesn't necessarily have anything to do with which
6236 frame is being highlighted or un-highlighted; we only use it to find
6237 the appropriate X display info. */
6239 static void
6240 XTframe_rehighlight (frame)
6241 struct frame *frame;
6243 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6246 static void
6247 x_frame_rehighlight (dpyinfo)
6248 struct x_display_info *dpyinfo;
6250 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6252 if (dpyinfo->x_focus_frame)
6254 dpyinfo->x_highlight_frame
6255 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6256 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6257 : dpyinfo->x_focus_frame);
6258 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
6260 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6261 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
6264 else
6265 dpyinfo->x_highlight_frame = 0;
6267 if (dpyinfo->x_highlight_frame != old_highlight)
6269 if (old_highlight)
6270 frame_unhighlight (old_highlight);
6271 if (dpyinfo->x_highlight_frame)
6272 frame_highlight (dpyinfo->x_highlight_frame);
6278 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
6280 /* Initialize mode_switch_bit and modifier_meaning. */
6281 static void
6282 x_find_modifier_meanings (dpyinfo)
6283 struct x_display_info *dpyinfo;
6285 int min_code, max_code;
6286 KeySym *syms;
6287 int syms_per_code;
6288 XModifierKeymap *mods;
6290 dpyinfo->meta_mod_mask = 0;
6291 dpyinfo->shift_lock_mask = 0;
6292 dpyinfo->alt_mod_mask = 0;
6293 dpyinfo->super_mod_mask = 0;
6294 dpyinfo->hyper_mod_mask = 0;
6296 #ifdef HAVE_X11R4
6297 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
6298 #else
6299 min_code = dpyinfo->display->min_keycode;
6300 max_code = dpyinfo->display->max_keycode;
6301 #endif
6303 syms = XGetKeyboardMapping (dpyinfo->display,
6304 min_code, max_code - min_code + 1,
6305 &syms_per_code);
6306 mods = XGetModifierMapping (dpyinfo->display);
6308 /* Scan the modifier table to see which modifier bits the Meta and
6309 Alt keysyms are on. */
6311 int row, col; /* The row and column in the modifier table. */
6313 for (row = 3; row < 8; row++)
6314 for (col = 0; col < mods->max_keypermod; col++)
6316 KeyCode code
6317 = mods->modifiermap[(row * mods->max_keypermod) + col];
6319 /* Zeroes are used for filler. Skip them. */
6320 if (code == 0)
6321 continue;
6323 /* Are any of this keycode's keysyms a meta key? */
6325 int code_col;
6327 for (code_col = 0; code_col < syms_per_code; code_col++)
6329 int sym = syms[((code - min_code) * syms_per_code) + code_col];
6331 switch (sym)
6333 case XK_Meta_L:
6334 case XK_Meta_R:
6335 dpyinfo->meta_mod_mask |= (1 << row);
6336 break;
6338 case XK_Alt_L:
6339 case XK_Alt_R:
6340 dpyinfo->alt_mod_mask |= (1 << row);
6341 break;
6343 case XK_Hyper_L:
6344 case XK_Hyper_R:
6345 dpyinfo->hyper_mod_mask |= (1 << row);
6346 break;
6348 case XK_Super_L:
6349 case XK_Super_R:
6350 dpyinfo->super_mod_mask |= (1 << row);
6351 break;
6353 case XK_Shift_Lock:
6354 /* Ignore this if it's not on the lock modifier. */
6355 if ((1 << row) == LockMask)
6356 dpyinfo->shift_lock_mask = LockMask;
6357 break;
6364 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
6365 if (! dpyinfo->meta_mod_mask)
6367 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6368 dpyinfo->alt_mod_mask = 0;
6371 /* If some keys are both alt and meta,
6372 make them just meta, not alt. */
6373 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6375 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6378 XFree ((char *) syms);
6379 XFreeModifiermap (mods);
6382 /* Convert between the modifier bits X uses and the modifier bits
6383 Emacs uses. */
6385 static unsigned int
6386 x_x_to_emacs_modifiers (dpyinfo, state)
6387 struct x_display_info *dpyinfo;
6388 unsigned int state;
6390 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6391 | ((state & ControlMask) ? ctrl_modifier : 0)
6392 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6393 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6394 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6395 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
6398 static unsigned int
6399 x_emacs_to_x_modifiers (dpyinfo, state)
6400 struct x_display_info *dpyinfo;
6401 unsigned int state;
6403 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6404 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6405 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6406 | ((state & shift_modifier) ? ShiftMask : 0)
6407 | ((state & ctrl_modifier) ? ControlMask : 0)
6408 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
6411 /* Convert a keysym to its name. */
6413 char *
6414 x_get_keysym_name (keysym)
6415 KeySym keysym;
6417 char *value;
6419 BLOCK_INPUT;
6420 value = XKeysymToString (keysym);
6421 UNBLOCK_INPUT;
6423 return value;
6428 /* Mouse clicks and mouse movement. Rah. */
6430 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6431 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6432 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6433 not force the value into range. */
6435 void
6436 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6437 FRAME_PTR f;
6438 register int pix_x, pix_y;
6439 register int *x, *y;
6440 XRectangle *bounds;
6441 int noclip;
6443 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6444 even for negative values. */
6445 if (pix_x < 0)
6446 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6447 if (pix_y < 0)
6448 pix_y -= (f)->output_data.x->line_height - 1;
6450 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6451 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6453 if (bounds)
6455 bounds->width = FONT_WIDTH (f->output_data.x->font);
6456 bounds->height = f->output_data.x->line_height;
6457 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6458 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6461 if (!noclip)
6463 if (pix_x < 0)
6464 pix_x = 0;
6465 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6466 pix_x = FRAME_WINDOW_WIDTH (f);
6468 if (pix_y < 0)
6469 pix_y = 0;
6470 else if (pix_y > f->height)
6471 pix_y = f->height;
6474 *x = pix_x;
6475 *y = pix_y;
6479 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6480 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6481 can't tell the positions because W's display is not up to date,
6482 return 0. */
6485 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6486 struct window *w;
6487 int hpos, vpos;
6488 int *frame_x, *frame_y;
6490 int success_p;
6492 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6493 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6495 if (display_completed)
6497 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6498 struct glyph *glyph = row->glyphs[TEXT_AREA];
6499 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6501 *frame_y = row->y;
6502 *frame_x = row->x;
6503 while (glyph < end)
6505 *frame_x += glyph->pixel_width;
6506 ++glyph;
6509 success_p = 1;
6511 else
6513 *frame_y = *frame_x = 0;
6514 success_p = 0;
6517 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6518 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6519 return success_p;
6523 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6525 If the event is a button press, then note that we have grabbed
6526 the mouse. */
6528 static Lisp_Object
6529 construct_mouse_click (result, event, f)
6530 struct input_event *result;
6531 XButtonEvent *event;
6532 struct frame *f;
6534 /* Make the event type no_event; we'll change that when we decide
6535 otherwise. */
6536 result->kind = mouse_click;
6537 result->code = event->button - Button1;
6538 result->timestamp = event->time;
6539 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6540 event->state)
6541 | (event->type == ButtonRelease
6542 ? up_modifier
6543 : down_modifier));
6545 XSETINT (result->x, event->x);
6546 XSETINT (result->y, event->y);
6547 XSETFRAME (result->frame_or_window, f);
6548 result->arg = Qnil;
6549 return Qnil;
6553 /* Function to report a mouse movement to the mainstream Emacs code.
6554 The input handler calls this.
6556 We have received a mouse movement event, which is given in *event.
6557 If the mouse is over a different glyph than it was last time, tell
6558 the mainstream emacs code by setting mouse_moved. If not, ask for
6559 another motion event, so we can check again the next time it moves. */
6561 static XMotionEvent last_mouse_motion_event;
6562 static Lisp_Object last_mouse_motion_frame;
6564 static void
6565 note_mouse_movement (frame, event)
6566 FRAME_PTR frame;
6567 XMotionEvent *event;
6569 last_mouse_movement_time = event->time;
6570 last_mouse_motion_event = *event;
6571 XSETFRAME (last_mouse_motion_frame, frame);
6573 if (event->window != FRAME_X_WINDOW (frame))
6575 frame->mouse_moved = 1;
6576 last_mouse_scroll_bar = Qnil;
6577 note_mouse_highlight (frame, -1, -1);
6580 /* Has the mouse moved off the glyph it was on at the last sighting? */
6581 else if (event->x < last_mouse_glyph.x
6582 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6583 || event->y < last_mouse_glyph.y
6584 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6586 frame->mouse_moved = 1;
6587 last_mouse_scroll_bar = Qnil;
6588 note_mouse_highlight (frame, event->x, event->y);
6592 /* This is used for debugging, to turn off note_mouse_highlight. */
6594 int disable_mouse_highlight;
6598 /************************************************************************
6599 Mouse Face
6600 ************************************************************************/
6602 /* Find the glyph under window-relative coordinates X/Y in window W.
6603 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6604 strings. Return in *HPOS and *VPOS the row and column number of
6605 the glyph found. Return in *AREA the glyph area containing X.
6606 Value is a pointer to the glyph found or null if X/Y is not on
6607 text, or we can't tell because W's current matrix is not up to
6608 date. */
6610 static struct glyph *
6611 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
6612 struct window *w;
6613 int x, y;
6614 int *hpos, *vpos, *area;
6615 int buffer_only_p;
6617 struct glyph *glyph, *end;
6618 struct glyph_row *row = NULL;
6619 int x0, i, left_area_width;
6621 /* Find row containing Y. Give up if some row is not enabled. */
6622 for (i = 0; i < w->current_matrix->nrows; ++i)
6624 row = MATRIX_ROW (w->current_matrix, i);
6625 if (!row->enabled_p)
6626 return NULL;
6627 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6628 break;
6631 *vpos = i;
6632 *hpos = 0;
6634 /* Give up if Y is not in the window. */
6635 if (i == w->current_matrix->nrows)
6636 return NULL;
6638 /* Get the glyph area containing X. */
6639 if (w->pseudo_window_p)
6641 *area = TEXT_AREA;
6642 x0 = 0;
6644 else
6646 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6647 if (x < left_area_width)
6649 *area = LEFT_MARGIN_AREA;
6650 x0 = 0;
6652 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6654 *area = TEXT_AREA;
6655 x0 = row->x + left_area_width;
6657 else
6659 *area = RIGHT_MARGIN_AREA;
6660 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6664 /* Find glyph containing X. */
6665 glyph = row->glyphs[*area];
6666 end = glyph + row->used[*area];
6667 while (glyph < end)
6669 if (x < x0 + glyph->pixel_width)
6671 if (w->pseudo_window_p)
6672 break;
6673 else if (!buffer_only_p || BUFFERP (glyph->object))
6674 break;
6677 x0 += glyph->pixel_width;
6678 ++glyph;
6681 if (glyph == end)
6682 return NULL;
6684 *hpos = glyph - row->glyphs[*area];
6685 return glyph;
6689 /* Convert frame-relative x/y to coordinates relative to window W.
6690 Takes pseudo-windows into account. */
6692 static void
6693 frame_to_window_pixel_xy (w, x, y)
6694 struct window *w;
6695 int *x, *y;
6697 if (w->pseudo_window_p)
6699 /* A pseudo-window is always full-width, and starts at the
6700 left edge of the frame, plus a frame border. */
6701 struct frame *f = XFRAME (w->frame);
6702 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6703 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6705 else
6707 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6708 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6713 /* Take proper action when mouse has moved to the mode or header line of
6714 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6715 mode line. X is relative to the start of the text display area of
6716 W, so the width of bitmap areas and scroll bars must be subtracted
6717 to get a position relative to the start of the mode line. */
6719 static void
6720 note_mode_line_highlight (w, x, mode_line_p)
6721 struct window *w;
6722 int x, mode_line_p;
6724 struct frame *f = XFRAME (w->frame);
6725 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6726 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6727 struct glyph_row *row;
6729 if (mode_line_p)
6730 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6731 else
6732 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6734 if (row->enabled_p)
6736 struct glyph *glyph, *end;
6737 Lisp_Object help, map;
6738 int x0;
6740 /* Find the glyph under X. */
6741 glyph = row->glyphs[TEXT_AREA];
6742 end = glyph + row->used[TEXT_AREA];
6743 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6744 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
6746 while (glyph < end
6747 && x >= x0 + glyph->pixel_width)
6749 x0 += glyph->pixel_width;
6750 ++glyph;
6753 if (glyph < end
6754 && STRINGP (glyph->object)
6755 && XSTRING (glyph->object)->intervals
6756 && glyph->charpos >= 0
6757 && glyph->charpos < XSTRING (glyph->object)->size)
6759 /* If we're on a string with `help-echo' text property,
6760 arrange for the help to be displayed. This is done by
6761 setting the global variable help_echo to the help string. */
6762 help = Fget_text_property (make_number (glyph->charpos),
6763 Qhelp_echo, glyph->object);
6764 if (!NILP (help))
6766 help_echo = help;
6767 XSETWINDOW (help_echo_window, w);
6768 help_echo_object = glyph->object;
6769 help_echo_pos = glyph->charpos;
6772 /* Change the mouse pointer according to what is under X/Y. */
6773 map = Fget_text_property (make_number (glyph->charpos),
6774 Qlocal_map, glyph->object);
6775 if (KEYMAPP (map))
6776 cursor = f->output_data.x->nontext_cursor;
6777 else
6779 map = Fget_text_property (make_number (glyph->charpos),
6780 Qkeymap, glyph->object);
6781 if (KEYMAPP (map))
6782 cursor = f->output_data.x->nontext_cursor;
6787 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6791 /* Take proper action when the mouse has moved to position X, Y on
6792 frame F as regards highlighting characters that have mouse-face
6793 properties. Also de-highlighting chars where the mouse was before.
6794 X and Y can be negative or out of range. */
6796 static void
6797 note_mouse_highlight (f, x, y)
6798 struct frame *f;
6799 int x, y;
6801 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6802 int portion;
6803 Lisp_Object window;
6804 struct window *w;
6805 Cursor cursor = None;
6806 struct buffer *b;
6808 /* When a menu is active, don't highlight because this looks odd. */
6809 #ifdef USE_X_TOOLKIT
6810 if (popup_activated ())
6811 return;
6812 #endif
6814 if (disable_mouse_highlight
6815 || !f->glyphs_initialized_p)
6816 return;
6818 dpyinfo->mouse_face_mouse_x = x;
6819 dpyinfo->mouse_face_mouse_y = y;
6820 dpyinfo->mouse_face_mouse_frame = f;
6822 if (dpyinfo->mouse_face_defer)
6823 return;
6825 if (gc_in_progress)
6827 dpyinfo->mouse_face_deferred_gc = 1;
6828 return;
6831 /* Which window is that in? */
6832 window = window_from_coordinates (f, x, y, &portion, 1);
6834 /* If we were displaying active text in another window, clear that. */
6835 if (! EQ (window, dpyinfo->mouse_face_window))
6836 clear_mouse_face (dpyinfo);
6838 /* Not on a window -> return. */
6839 if (!WINDOWP (window))
6840 return;
6842 /* Convert to window-relative pixel coordinates. */
6843 w = XWINDOW (window);
6844 frame_to_window_pixel_xy (w, &x, &y);
6846 /* Handle tool-bar window differently since it doesn't display a
6847 buffer. */
6848 if (EQ (window, f->tool_bar_window))
6850 note_tool_bar_highlight (f, x, y);
6851 return;
6854 /* Mouse is on the mode or header line? */
6855 if (portion == 1 || portion == 3)
6857 note_mode_line_highlight (w, x, portion == 1);
6858 return;
6861 if (portion == 2)
6862 cursor = f->output_data.x->horizontal_drag_cursor;
6863 else
6864 cursor = f->output_data.x->text_cursor;
6866 /* Are we in a window whose display is up to date?
6867 And verify the buffer's text has not changed. */
6868 b = XBUFFER (w->buffer);
6869 if (/* Within text portion of the window. */
6870 portion == 0
6871 && EQ (w->window_end_valid, w->buffer)
6872 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
6873 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
6875 int hpos, vpos, pos, i, area;
6876 struct glyph *glyph;
6877 Lisp_Object object;
6878 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
6879 Lisp_Object *overlay_vec = NULL;
6880 int len, noverlays;
6881 struct buffer *obuf;
6882 int obegv, ozv, same_region;
6884 /* Find the glyph under X/Y. */
6885 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
6887 /* Clear mouse face if X/Y not over text. */
6888 if (glyph == NULL
6889 || area != TEXT_AREA
6890 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6892 if (clear_mouse_face (dpyinfo))
6893 cursor = None;
6894 goto set_cursor;
6897 pos = glyph->charpos;
6898 object = glyph->object;
6899 if (!STRINGP (object) && !BUFFERP (object))
6900 goto set_cursor;
6902 /* If we get an out-of-range value, return now; avoid an error. */
6903 if (BUFFERP (object) && pos > BUF_Z (b))
6904 goto set_cursor;
6906 /* Make the window's buffer temporarily current for
6907 overlays_at and compute_char_face. */
6908 obuf = current_buffer;
6909 current_buffer = b;
6910 obegv = BEGV;
6911 ozv = ZV;
6912 BEGV = BEG;
6913 ZV = Z;
6915 /* Is this char mouse-active or does it have help-echo? */
6916 position = make_number (pos);
6918 if (BUFFERP (object))
6920 /* Put all the overlays we want in a vector in overlay_vec.
6921 Store the length in len. If there are more than 10, make
6922 enough space for all, and try again. */
6923 len = 10;
6924 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6925 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6926 if (noverlays > len)
6928 len = noverlays;
6929 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6930 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6933 /* Sort overlays into increasing priority order. */
6934 noverlays = sort_overlays (overlay_vec, noverlays, w);
6936 else
6937 noverlays = 0;
6939 same_region = (EQ (window, dpyinfo->mouse_face_window)
6940 && vpos >= dpyinfo->mouse_face_beg_row
6941 && vpos <= dpyinfo->mouse_face_end_row
6942 && (vpos > dpyinfo->mouse_face_beg_row
6943 || hpos >= dpyinfo->mouse_face_beg_col)
6944 && (vpos < dpyinfo->mouse_face_end_row
6945 || hpos < dpyinfo->mouse_face_end_col
6946 || dpyinfo->mouse_face_past_end));
6948 if (same_region)
6949 cursor = None;
6951 /* Check mouse-face highlighting. */
6952 if (! same_region
6953 /* If there exists an overlay with mouse-face overlapping
6954 the one we are currently highlighting, we have to
6955 check if we enter the overlapping overlay, and then
6956 highlight only that. */
6957 || (OVERLAYP (dpyinfo->mouse_face_overlay)
6958 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
6960 /* Find the highest priority overlay that has a mouse-face
6961 property. */
6962 overlay = Qnil;
6963 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
6965 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6966 if (!NILP (mouse_face))
6967 overlay = overlay_vec[i];
6970 /* If we're actually highlighting the same overlay as
6971 before, there's no need to do that again. */
6972 if (!NILP (overlay)
6973 && EQ (overlay, dpyinfo->mouse_face_overlay))
6974 goto check_help_echo;
6976 dpyinfo->mouse_face_overlay = overlay;
6978 /* Clear the display of the old active region, if any. */
6979 if (clear_mouse_face (dpyinfo))
6980 cursor = None;
6982 /* If no overlay applies, get a text property. */
6983 if (NILP (overlay))
6984 mouse_face = Fget_text_property (position, Qmouse_face, object);
6986 /* Handle the overlay case. */
6987 if (!NILP (overlay))
6989 /* Find the range of text around this char that
6990 should be active. */
6991 Lisp_Object before, after;
6992 int ignore;
6994 before = Foverlay_start (overlay);
6995 after = Foverlay_end (overlay);
6996 /* Record this as the current active region. */
6997 fast_find_position (w, XFASTINT (before),
6998 &dpyinfo->mouse_face_beg_col,
6999 &dpyinfo->mouse_face_beg_row,
7000 &dpyinfo->mouse_face_beg_x,
7001 &dpyinfo->mouse_face_beg_y);
7002 dpyinfo->mouse_face_past_end
7003 = !fast_find_position (w, XFASTINT (after),
7004 &dpyinfo->mouse_face_end_col,
7005 &dpyinfo->mouse_face_end_row,
7006 &dpyinfo->mouse_face_end_x,
7007 &dpyinfo->mouse_face_end_y);
7008 dpyinfo->mouse_face_window = window;
7009 dpyinfo->mouse_face_face_id
7010 = face_at_buffer_position (w, pos, 0, 0,
7011 &ignore, pos + 1, 1);
7013 /* Display it as active. */
7014 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7015 cursor = None;
7017 /* Handle the text property case. */
7018 else if (!NILP (mouse_face) && BUFFERP (object))
7020 /* Find the range of text around this char that
7021 should be active. */
7022 Lisp_Object before, after, beginning, end;
7023 int ignore;
7025 beginning = Fmarker_position (w->start);
7026 end = make_number (BUF_Z (XBUFFER (object))
7027 - XFASTINT (w->window_end_pos));
7028 before
7029 = Fprevious_single_property_change (make_number (pos + 1),
7030 Qmouse_face,
7031 object, beginning);
7032 after
7033 = Fnext_single_property_change (position, Qmouse_face,
7034 object, end);
7036 /* Record this as the current active region. */
7037 fast_find_position (w, XFASTINT (before),
7038 &dpyinfo->mouse_face_beg_col,
7039 &dpyinfo->mouse_face_beg_row,
7040 &dpyinfo->mouse_face_beg_x,
7041 &dpyinfo->mouse_face_beg_y);
7042 dpyinfo->mouse_face_past_end
7043 = !fast_find_position (w, XFASTINT (after),
7044 &dpyinfo->mouse_face_end_col,
7045 &dpyinfo->mouse_face_end_row,
7046 &dpyinfo->mouse_face_end_x,
7047 &dpyinfo->mouse_face_end_y);
7048 dpyinfo->mouse_face_window = window;
7050 if (BUFFERP (object))
7051 dpyinfo->mouse_face_face_id
7052 = face_at_buffer_position (w, pos, 0, 0,
7053 &ignore, pos + 1, 1);
7055 /* Display it as active. */
7056 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7057 cursor = None;
7059 else if (!NILP (mouse_face) && STRINGP (object))
7061 Lisp_Object b, e;
7062 int ignore;
7064 b = Fprevious_single_property_change (make_number (pos + 1),
7065 Qmouse_face,
7066 object, Qnil);
7067 e = Fnext_single_property_change (position, Qmouse_face,
7068 object, Qnil);
7069 if (NILP (b))
7070 b = make_number (0);
7071 if (NILP (e))
7072 e = make_number (XSTRING (object)->size - 1);
7073 fast_find_string_pos (w, XINT (b), object,
7074 &dpyinfo->mouse_face_beg_col,
7075 &dpyinfo->mouse_face_beg_row,
7076 &dpyinfo->mouse_face_beg_x,
7077 &dpyinfo->mouse_face_beg_y, 0);
7078 fast_find_string_pos (w, XINT (e), object,
7079 &dpyinfo->mouse_face_end_col,
7080 &dpyinfo->mouse_face_end_row,
7081 &dpyinfo->mouse_face_end_x,
7082 &dpyinfo->mouse_face_end_y, 1);
7083 dpyinfo->mouse_face_past_end = 0;
7084 dpyinfo->mouse_face_window = window;
7085 dpyinfo->mouse_face_face_id
7086 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
7087 glyph->face_id, 1);
7088 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7089 cursor = None;
7093 check_help_echo:
7095 /* Look for a `help-echo' property. */
7097 Lisp_Object help, overlay;
7099 /* Check overlays first. */
7100 help = overlay = Qnil;
7101 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
7103 overlay = overlay_vec[i];
7104 help = Foverlay_get (overlay, Qhelp_echo);
7107 if (!NILP (help))
7109 help_echo = help;
7110 help_echo_window = window;
7111 help_echo_object = overlay;
7112 help_echo_pos = pos;
7114 else
7116 Lisp_Object object = glyph->object;
7117 int charpos = glyph->charpos;
7119 /* Try text properties. */
7120 if (STRINGP (object)
7121 && charpos >= 0
7122 && charpos < XSTRING (object)->size)
7124 help = Fget_text_property (make_number (charpos),
7125 Qhelp_echo, object);
7126 if (NILP (help))
7128 /* If the string itself doesn't specify a help-echo,
7129 see if the buffer text ``under'' it does. */
7130 struct glyph_row *r
7131 = MATRIX_ROW (w->current_matrix, vpos);
7132 int start = MATRIX_ROW_START_CHARPOS (r);
7133 int pos = string_buffer_position (w, object, start);
7134 if (pos > 0)
7136 help = Fget_text_property (make_number (pos),
7137 Qhelp_echo, w->buffer);
7138 if (!NILP (help))
7140 charpos = pos;
7141 object = w->buffer;
7146 else if (BUFFERP (object)
7147 && charpos >= BEGV
7148 && charpos < ZV)
7149 help = Fget_text_property (make_number (charpos), Qhelp_echo,
7150 object);
7152 if (!NILP (help))
7154 help_echo = help;
7155 help_echo_window = window;
7156 help_echo_object = object;
7157 help_echo_pos = charpos;
7162 BEGV = obegv;
7163 ZV = ozv;
7164 current_buffer = obuf;
7167 set_cursor:
7169 if (cursor != None)
7170 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7173 static void
7174 redo_mouse_highlight ()
7176 if (!NILP (last_mouse_motion_frame)
7177 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
7178 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
7179 last_mouse_motion_event.x,
7180 last_mouse_motion_event.y);
7185 /***********************************************************************
7186 Tool-bars
7187 ***********************************************************************/
7189 static int x_tool_bar_item P_ ((struct frame *, int, int,
7190 struct glyph **, int *, int *, int *));
7192 /* Tool-bar item index of the item on which a mouse button was pressed
7193 or -1. */
7195 static int last_tool_bar_item;
7198 /* Get information about the tool-bar item at position X/Y on frame F.
7199 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7200 the current matrix of the tool-bar window of F, or NULL if not
7201 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
7202 item in F->tool_bar_items. Value is
7204 -1 if X/Y is not on a tool-bar item
7205 0 if X/Y is on the same item that was highlighted before.
7206 1 otherwise. */
7208 static int
7209 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
7210 struct frame *f;
7211 int x, y;
7212 struct glyph **glyph;
7213 int *hpos, *vpos, *prop_idx;
7215 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7216 struct window *w = XWINDOW (f->tool_bar_window);
7217 int area;
7219 /* Find the glyph under X/Y. */
7220 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
7221 if (*glyph == NULL)
7222 return -1;
7224 /* Get the start of this tool-bar item's properties in
7225 f->tool_bar_items. */
7226 if (!tool_bar_item_info (f, *glyph, prop_idx))
7227 return -1;
7229 /* Is mouse on the highlighted item? */
7230 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
7231 && *vpos >= dpyinfo->mouse_face_beg_row
7232 && *vpos <= dpyinfo->mouse_face_end_row
7233 && (*vpos > dpyinfo->mouse_face_beg_row
7234 || *hpos >= dpyinfo->mouse_face_beg_col)
7235 && (*vpos < dpyinfo->mouse_face_end_row
7236 || *hpos < dpyinfo->mouse_face_end_col
7237 || dpyinfo->mouse_face_past_end))
7238 return 0;
7240 return 1;
7244 /* Handle mouse button event on the tool-bar of frame F, at
7245 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
7246 or ButtonRelase. */
7248 static void
7249 x_handle_tool_bar_click (f, button_event)
7250 struct frame *f;
7251 XButtonEvent *button_event;
7253 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7254 struct window *w = XWINDOW (f->tool_bar_window);
7255 int hpos, vpos, prop_idx;
7256 struct glyph *glyph;
7257 Lisp_Object enabled_p;
7258 int x = button_event->x;
7259 int y = button_event->y;
7261 /* If not on the highlighted tool-bar item, return. */
7262 frame_to_window_pixel_xy (w, &x, &y);
7263 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
7264 return;
7266 /* If item is disabled, do nothing. */
7267 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7268 if (NILP (enabled_p))
7269 return;
7271 if (button_event->type == ButtonPress)
7273 /* Show item in pressed state. */
7274 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7275 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
7276 last_tool_bar_item = prop_idx;
7278 else
7280 Lisp_Object key, frame;
7281 struct input_event event;
7283 /* Show item in released state. */
7284 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7285 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7287 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
7289 XSETFRAME (frame, f);
7290 event.kind = TOOL_BAR_EVENT;
7291 event.frame_or_window = frame;
7292 event.arg = frame;
7293 kbd_buffer_store_event (&event);
7295 event.kind = TOOL_BAR_EVENT;
7296 event.frame_or_window = frame;
7297 event.arg = key;
7298 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7299 button_event->state);
7300 kbd_buffer_store_event (&event);
7301 last_tool_bar_item = -1;
7306 /* Possibly highlight a tool-bar item on frame F when mouse moves to
7307 tool-bar window-relative coordinates X/Y. Called from
7308 note_mouse_highlight. */
7310 static void
7311 note_tool_bar_highlight (f, x, y)
7312 struct frame *f;
7313 int x, y;
7315 Lisp_Object window = f->tool_bar_window;
7316 struct window *w = XWINDOW (window);
7317 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7318 int hpos, vpos;
7319 struct glyph *glyph;
7320 struct glyph_row *row;
7321 int i;
7322 Lisp_Object enabled_p;
7323 int prop_idx;
7324 enum draw_glyphs_face draw;
7325 int mouse_down_p, rc;
7327 /* Function note_mouse_highlight is called with negative x(y
7328 values when mouse moves outside of the frame. */
7329 if (x <= 0 || y <= 0)
7331 clear_mouse_face (dpyinfo);
7332 return;
7335 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7336 if (rc < 0)
7338 /* Not on tool-bar item. */
7339 clear_mouse_face (dpyinfo);
7340 return;
7342 else if (rc == 0)
7343 goto set_help_echo;
7345 clear_mouse_face (dpyinfo);
7347 /* Mouse is down, but on different tool-bar item? */
7348 mouse_down_p = (dpyinfo->grabbed
7349 && f == last_mouse_frame
7350 && FRAME_LIVE_P (f));
7351 if (mouse_down_p
7352 && last_tool_bar_item != prop_idx)
7353 return;
7355 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7356 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7358 /* If tool-bar item is not enabled, don't highlight it. */
7359 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7360 if (!NILP (enabled_p))
7362 /* Compute the x-position of the glyph. In front and past the
7363 image is a space. We include this is the highlighted area. */
7364 row = MATRIX_ROW (w->current_matrix, vpos);
7365 for (i = x = 0; i < hpos; ++i)
7366 x += row->glyphs[TEXT_AREA][i].pixel_width;
7368 /* Record this as the current active region. */
7369 dpyinfo->mouse_face_beg_col = hpos;
7370 dpyinfo->mouse_face_beg_row = vpos;
7371 dpyinfo->mouse_face_beg_x = x;
7372 dpyinfo->mouse_face_beg_y = row->y;
7373 dpyinfo->mouse_face_past_end = 0;
7375 dpyinfo->mouse_face_end_col = hpos + 1;
7376 dpyinfo->mouse_face_end_row = vpos;
7377 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7378 dpyinfo->mouse_face_end_y = row->y;
7379 dpyinfo->mouse_face_window = window;
7380 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7382 /* Display it as active. */
7383 show_mouse_face (dpyinfo, draw);
7384 dpyinfo->mouse_face_image_state = draw;
7387 set_help_echo:
7389 /* Set help_echo to a help string.to display for this tool-bar item.
7390 XTread_socket does the rest. */
7391 help_echo_object = help_echo_window = Qnil;
7392 help_echo_pos = -1;
7393 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
7394 if (NILP (help_echo))
7395 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
7400 /* Find the glyph matrix position of buffer position POS in window W.
7401 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7402 current glyphs must be up to date. If POS is above window start
7403 return (0, 0, 0, 0). If POS is after end of W, return end of
7404 last line in W. */
7406 static int
7407 fast_find_position (w, pos, hpos, vpos, x, y)
7408 struct window *w;
7409 int pos;
7410 int *hpos, *vpos, *x, *y;
7412 int i;
7413 int lastcol;
7414 int maybe_next_line_p = 0;
7415 int line_start_position;
7416 int yb = window_text_bottom_y (w);
7417 struct glyph_row *row, *best_row;
7418 int row_vpos, best_row_vpos;
7419 int current_x;
7421 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7422 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7424 while (row->y < yb)
7426 if (row->used[TEXT_AREA])
7427 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7428 else
7429 line_start_position = 0;
7431 if (line_start_position > pos)
7432 break;
7433 /* If the position sought is the end of the buffer,
7434 don't include the blank lines at the bottom of the window. */
7435 else if (line_start_position == pos
7436 && pos == BUF_ZV (XBUFFER (w->buffer)))
7438 maybe_next_line_p = 1;
7439 break;
7441 else if (line_start_position > 0)
7443 best_row = row;
7444 best_row_vpos = row_vpos;
7447 if (row->y + row->height >= yb)
7448 break;
7450 ++row;
7451 ++row_vpos;
7454 /* Find the right column within BEST_ROW. */
7455 lastcol = 0;
7456 current_x = best_row->x;
7457 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
7459 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7460 int charpos;
7462 charpos = glyph->charpos;
7463 if (charpos == pos)
7465 *hpos = i;
7466 *vpos = best_row_vpos;
7467 *x = current_x;
7468 *y = best_row->y;
7469 return 1;
7471 else if (charpos > pos)
7472 break;
7473 else if (charpos > 0)
7474 lastcol = i;
7476 current_x += glyph->pixel_width;
7479 /* If we're looking for the end of the buffer,
7480 and we didn't find it in the line we scanned,
7481 use the start of the following line. */
7482 if (maybe_next_line_p)
7484 ++best_row;
7485 ++best_row_vpos;
7486 lastcol = 0;
7487 current_x = best_row->x;
7490 *vpos = best_row_vpos;
7491 *hpos = lastcol + 1;
7492 *x = current_x;
7493 *y = best_row->y;
7494 return 0;
7498 /* Find the position of the the glyph for position POS in OBJECT in
7499 window W's current matrix, and return in *X/*Y the pixel
7500 coordinates, and return in *HPOS/*VPOS the column/row of the glyph.
7502 RIGHT_P non-zero means return the position of the right edge of the
7503 glyph, RIGHT_P zero means return the left edge position.
7505 If no glyph for POS exists in the matrix, return the position of
7506 the glyph with the next smaller position that is in the matrix, if
7507 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7508 exists in the matrix, return the position of the glyph with the
7509 next larger position in OBJECT.
7511 Value is non-zero if a glyph was found. */
7513 static int
7514 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7515 struct window *w;
7516 int pos;
7517 Lisp_Object object;
7518 int *hpos, *vpos, *x, *y;
7519 int right_p;
7521 int yb = window_text_bottom_y (w);
7522 struct glyph_row *r;
7523 struct glyph *best_glyph = NULL;
7524 struct glyph_row *best_row = NULL;
7525 int best_x = 0;
7527 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7528 r->enabled_p && r->y < yb;
7529 ++r)
7531 struct glyph *g = r->glyphs[TEXT_AREA];
7532 struct glyph *e = g + r->used[TEXT_AREA];
7533 int gx;
7535 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7536 if (EQ (g->object, object))
7538 if (g->charpos == pos)
7540 best_glyph = g;
7541 best_x = gx;
7542 best_row = r;
7543 goto found;
7545 else if (best_glyph == NULL
7546 || ((abs (g->charpos - pos)
7547 < abs (best_glyph->charpos - pos))
7548 && (right_p
7549 ? g->charpos < pos
7550 : g->charpos > pos)))
7552 best_glyph = g;
7553 best_x = gx;
7554 best_row = r;
7559 found:
7561 if (best_glyph)
7563 *x = best_x;
7564 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7566 if (right_p)
7568 *x += best_glyph->pixel_width;
7569 ++*hpos;
7572 *y = best_row->y;
7573 *vpos = best_row - w->current_matrix->rows;
7576 return best_glyph != NULL;
7580 /* Display the active region described by mouse_face_*
7581 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7583 static void
7584 show_mouse_face (dpyinfo, draw)
7585 struct x_display_info *dpyinfo;
7586 enum draw_glyphs_face draw;
7588 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7589 struct frame *f = XFRAME (WINDOW_FRAME (w));
7590 int i;
7591 int cursor_off_p = 0;
7592 struct cursor_pos saved_cursor;
7594 saved_cursor = output_cursor;
7596 /* If window is in the process of being destroyed, don't bother
7597 to do anything. */
7598 if (w->current_matrix == NULL)
7599 goto set_x_cursor;
7601 /* Recognize when we are called to operate on rows that don't exist
7602 anymore. This can happen when a window is split. */
7603 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7604 goto set_x_cursor;
7606 set_output_cursor (&w->phys_cursor);
7608 /* Note that mouse_face_beg_row etc. are window relative. */
7609 for (i = dpyinfo->mouse_face_beg_row;
7610 i <= dpyinfo->mouse_face_end_row;
7611 i++)
7613 int start_hpos, end_hpos, start_x;
7614 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7616 /* Don't do anything if row doesn't have valid contents. */
7617 if (!row->enabled_p)
7618 continue;
7620 /* For all but the first row, the highlight starts at column 0. */
7621 if (i == dpyinfo->mouse_face_beg_row)
7623 start_hpos = dpyinfo->mouse_face_beg_col;
7624 start_x = dpyinfo->mouse_face_beg_x;
7626 else
7628 start_hpos = 0;
7629 start_x = 0;
7632 if (i == dpyinfo->mouse_face_end_row)
7633 end_hpos = dpyinfo->mouse_face_end_col;
7634 else
7635 end_hpos = row->used[TEXT_AREA];
7637 /* If the cursor's in the text we are about to rewrite, turn the
7638 cursor off. */
7639 if (!w->pseudo_window_p
7640 && i == output_cursor.vpos
7641 && output_cursor.hpos >= start_hpos - 1
7642 && output_cursor.hpos <= end_hpos)
7644 x_update_window_cursor (w, 0);
7645 cursor_off_p = 1;
7648 if (end_hpos > start_hpos)
7650 row->mouse_face_p = draw == DRAW_MOUSE_FACE || DRAW_IMAGE_RAISED;
7651 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7652 start_hpos, end_hpos, draw, NULL, NULL, 0);
7656 /* If we turned the cursor off, turn it back on. */
7657 if (cursor_off_p)
7658 x_display_cursor (w, 1,
7659 output_cursor.hpos, output_cursor.vpos,
7660 output_cursor.x, output_cursor.y);
7662 output_cursor = saved_cursor;
7664 set_x_cursor:
7666 /* Change the mouse cursor. */
7667 if (draw == DRAW_NORMAL_TEXT)
7668 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7669 f->output_data.x->text_cursor);
7670 else if (draw == DRAW_MOUSE_FACE)
7671 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7672 f->output_data.x->cross_cursor);
7673 else
7674 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7675 f->output_data.x->nontext_cursor);
7678 /* Clear out the mouse-highlighted active region.
7679 Redraw it un-highlighted first. Value is non-zero if mouse
7680 face was actually drawn unhighlighted. */
7682 static int
7683 clear_mouse_face (dpyinfo)
7684 struct x_display_info *dpyinfo;
7686 int cleared = 0;
7688 if (!NILP (dpyinfo->mouse_face_window))
7690 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7691 cleared = 1;
7694 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7695 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7696 dpyinfo->mouse_face_window = Qnil;
7697 dpyinfo->mouse_face_overlay = Qnil;
7698 return cleared;
7702 /* Clear any mouse-face on window W. This function is part of the
7703 redisplay interface, and is called from try_window_id and similar
7704 functions to ensure the mouse-highlight is off. */
7706 static void
7707 x_clear_mouse_face (w)
7708 struct window *w;
7710 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7711 Lisp_Object window;
7713 BLOCK_INPUT;
7714 XSETWINDOW (window, w);
7715 if (EQ (window, dpyinfo->mouse_face_window))
7716 clear_mouse_face (dpyinfo);
7717 UNBLOCK_INPUT;
7721 /* Just discard the mouse face information for frame F, if any.
7722 This is used when the size of F is changed. */
7724 void
7725 cancel_mouse_face (f)
7726 FRAME_PTR f;
7728 Lisp_Object window;
7729 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7731 window = dpyinfo->mouse_face_window;
7732 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7734 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7735 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7736 dpyinfo->mouse_face_window = Qnil;
7741 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
7744 /* Try to determine frame pixel position and size of the glyph under
7745 frame pixel coordinates X/Y on frame F . Return the position and
7746 size in *RECT. Value is non-zero if we could compute these
7747 values. */
7749 static int
7750 glyph_rect (f, x, y, rect)
7751 struct frame *f;
7752 int x, y;
7753 XRectangle *rect;
7755 Lisp_Object window;
7756 int part, found = 0;
7758 window = window_from_coordinates (f, x, y, &part, 0);
7759 if (!NILP (window))
7761 struct window *w = XWINDOW (window);
7762 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7763 struct glyph_row *end = r + w->current_matrix->nrows - 1;
7764 int area;
7766 frame_to_window_pixel_xy (w, &x, &y);
7768 for (; !found && r < end && r->enabled_p; ++r)
7769 if (r->y >= y)
7771 struct glyph *g = r->glyphs[TEXT_AREA];
7772 struct glyph *end = g + r->used[TEXT_AREA];
7773 int gx;
7775 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
7776 if (gx >= x)
7778 rect->width = g->pixel_width;
7779 rect->height = r->height;
7780 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
7781 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
7782 found = 1;
7787 return found;
7791 /* Return the current position of the mouse.
7792 *FP should be a frame which indicates which display to ask about.
7794 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
7795 and *PART to the frame, window, and scroll bar part that the mouse
7796 is over. Set *X and *Y to the portion and whole of the mouse's
7797 position on the scroll bar.
7799 If the mouse movement started elsewhere, set *FP to the frame the
7800 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
7801 the mouse is over.
7803 Set *TIME to the server time-stamp for the time at which the mouse
7804 was at this position.
7806 Don't store anything if we don't have a valid set of values to report.
7808 This clears the mouse_moved flag, so we can wait for the next mouse
7809 movement. */
7811 static void
7812 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7813 FRAME_PTR *fp;
7814 int insist;
7815 Lisp_Object *bar_window;
7816 enum scroll_bar_part *part;
7817 Lisp_Object *x, *y;
7818 unsigned long *time;
7820 FRAME_PTR f1;
7822 BLOCK_INPUT;
7824 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7825 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7826 else
7828 Window root;
7829 int root_x, root_y;
7831 Window dummy_window;
7832 int dummy;
7834 Lisp_Object frame, tail;
7836 /* Clear the mouse-moved flag for every frame on this display. */
7837 FOR_EACH_FRAME (tail, frame)
7838 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7839 XFRAME (frame)->mouse_moved = 0;
7841 last_mouse_scroll_bar = Qnil;
7843 /* Figure out which root window we're on. */
7844 XQueryPointer (FRAME_X_DISPLAY (*fp),
7845 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
7847 /* The root window which contains the pointer. */
7848 &root,
7850 /* Trash which we can't trust if the pointer is on
7851 a different screen. */
7852 &dummy_window,
7854 /* The position on that root window. */
7855 &root_x, &root_y,
7857 /* More trash we can't trust. */
7858 &dummy, &dummy,
7860 /* Modifier keys and pointer buttons, about which
7861 we don't care. */
7862 (unsigned int *) &dummy);
7864 /* Now we have a position on the root; find the innermost window
7865 containing the pointer. */
7867 Window win, child;
7868 int win_x, win_y;
7869 int parent_x = 0, parent_y = 0;
7870 int count;
7872 win = root;
7874 /* XTranslateCoordinates can get errors if the window
7875 structure is changing at the same time this function
7876 is running. So at least we must not crash from them. */
7878 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
7880 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
7881 && FRAME_LIVE_P (last_mouse_frame))
7883 /* If mouse was grabbed on a frame, give coords for that frame
7884 even if the mouse is now outside it. */
7885 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7887 /* From-window, to-window. */
7888 root, FRAME_X_WINDOW (last_mouse_frame),
7890 /* From-position, to-position. */
7891 root_x, root_y, &win_x, &win_y,
7893 /* Child of win. */
7894 &child);
7895 f1 = last_mouse_frame;
7897 else
7899 while (1)
7901 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7903 /* From-window, to-window. */
7904 root, win,
7906 /* From-position, to-position. */
7907 root_x, root_y, &win_x, &win_y,
7909 /* Child of win. */
7910 &child);
7912 if (child == None || child == win)
7913 break;
7915 win = child;
7916 parent_x = win_x;
7917 parent_y = win_y;
7920 /* Now we know that:
7921 win is the innermost window containing the pointer
7922 (XTC says it has no child containing the pointer),
7923 win_x and win_y are the pointer's position in it
7924 (XTC did this the last time through), and
7925 parent_x and parent_y are the pointer's position in win's parent.
7926 (They are what win_x and win_y were when win was child.
7927 If win is the root window, it has no parent, and
7928 parent_{x,y} are invalid, but that's okay, because we'll
7929 never use them in that case.) */
7931 /* Is win one of our frames? */
7932 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
7934 #ifdef USE_X_TOOLKIT
7935 /* If we end up with the menu bar window, say it's not
7936 on the frame. */
7937 if (f1 != NULL
7938 && f1->output_data.x->menubar_widget
7939 && win == XtWindow (f1->output_data.x->menubar_widget))
7940 f1 = NULL;
7941 #endif /* USE_X_TOOLKIT */
7944 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7945 f1 = 0;
7947 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
7949 /* If not, is it one of our scroll bars? */
7950 if (! f1)
7952 struct scroll_bar *bar = x_window_to_scroll_bar (win);
7954 if (bar)
7956 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7957 win_x = parent_x;
7958 win_y = parent_y;
7962 if (f1 == 0 && insist > 0)
7963 f1 = SELECTED_FRAME ();
7965 if (f1)
7967 /* Ok, we found a frame. Store all the values.
7968 last_mouse_glyph is a rectangle used to reduce the
7969 generation of mouse events. To not miss any motion
7970 events, we must divide the frame into rectangles of the
7971 size of the smallest character that could be displayed
7972 on it, i.e. into the same rectangles that matrices on
7973 the frame are divided into. */
7975 int width, height, gx, gy;
7976 XRectangle rect;
7978 if (glyph_rect (f1, win_x, win_y, &rect))
7979 last_mouse_glyph = rect;
7980 else
7982 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7983 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7984 gx = win_x;
7985 gy = win_y;
7987 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7988 round down even for negative values. */
7989 if (gx < 0)
7990 gx -= width - 1;
7991 if (gy < 0)
7992 gy -= height - 1;
7993 gx = (gx + width - 1) / width * width;
7994 gy = (gy + height - 1) / height * height;
7996 last_mouse_glyph.width = width;
7997 last_mouse_glyph.height = height;
7998 last_mouse_glyph.x = gx;
7999 last_mouse_glyph.y = gy;
8002 *bar_window = Qnil;
8003 *part = 0;
8004 *fp = f1;
8005 XSETINT (*x, win_x);
8006 XSETINT (*y, win_y);
8007 *time = last_mouse_movement_time;
8012 UNBLOCK_INPUT;
8016 #ifdef USE_X_TOOLKIT
8018 /* Atimer callback function for TIMER. Called every 0.1s to process
8019 Xt timeouts, if needed. We must avoid calling XtAppPending as
8020 much as possible because that function does an implicit XFlush
8021 that slows us down. */
8023 static void
8024 x_process_timeouts (timer)
8025 struct atimer *timer;
8027 if (toolkit_scroll_bar_interaction || popup_activated_flag)
8029 BLOCK_INPUT;
8030 while (XtAppPending (Xt_app_con) & XtIMTimer)
8031 XtAppProcessEvent (Xt_app_con, XtIMTimer);
8032 UNBLOCK_INPUT;
8036 #endif /* USE_X_TOOLKIT */
8039 /* Scroll bar support. */
8041 /* Given an X window ID, find the struct scroll_bar which manages it.
8042 This can be called in GC, so we have to make sure to strip off mark
8043 bits. */
8045 static struct scroll_bar *
8046 x_window_to_scroll_bar (window_id)
8047 Window window_id;
8049 Lisp_Object tail;
8051 for (tail = Vframe_list;
8052 XGCTYPE (tail) == Lisp_Cons;
8053 tail = XCDR (tail))
8055 Lisp_Object frame, bar, condemned;
8057 frame = XCAR (tail);
8058 /* All elements of Vframe_list should be frames. */
8059 if (! GC_FRAMEP (frame))
8060 abort ();
8062 /* Scan this frame's scroll bar list for a scroll bar with the
8063 right window ID. */
8064 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
8065 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
8066 /* This trick allows us to search both the ordinary and
8067 condemned scroll bar lists with one loop. */
8068 ! GC_NILP (bar) || (bar = condemned,
8069 condemned = Qnil,
8070 ! GC_NILP (bar));
8071 bar = XSCROLL_BAR (bar)->next)
8072 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
8073 return XSCROLL_BAR (bar);
8076 return 0;
8080 #if defined USE_X_TOOLKIT && defined USE_LUCID
8082 /* Return the Lucid menu bar WINDOW is part of. Return null
8083 if WINDOW is not part of a menu bar. */
8085 static Widget
8086 x_window_to_menu_bar (window)
8087 Window window;
8089 Lisp_Object tail;
8091 for (tail = Vframe_list;
8092 XGCTYPE (tail) == Lisp_Cons;
8093 tail = XCDR (tail))
8095 Lisp_Object frame = XCAR (tail);
8096 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
8098 if (menu_bar && xlwmenu_window_p (menu_bar, window))
8099 return menu_bar;
8102 return NULL;
8105 #endif /* USE_X_TOOLKIT && USE_LUCID */
8108 /************************************************************************
8109 Toolkit scroll bars
8110 ************************************************************************/
8112 #ifdef USE_TOOLKIT_SCROLL_BARS
8114 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
8115 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
8116 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
8117 struct scroll_bar *));
8118 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
8119 int, int, int));
8122 /* Id of action hook installed for scroll bars. */
8124 static XtActionHookId action_hook_id;
8126 /* Lisp window being scrolled. Set when starting to interact with
8127 a toolkit scroll bar, reset to nil when ending the interaction. */
8129 static Lisp_Object window_being_scrolled;
8131 /* Last scroll bar part sent in xm_scroll_callback. */
8133 static int last_scroll_bar_part;
8135 /* Whether this is an Xaw with arrow-scrollbars. This should imply
8136 that movements of 1/20 of the screen size are mapped to up/down. */
8138 static Boolean xaw3d_arrow_scroll;
8140 /* Whether the drag scrolling maintains the mouse at the top of the
8141 thumb. If not, resizing the thumb needs to be done more carefully
8142 to avoid jerkyness. */
8144 static Boolean xaw3d_pick_top;
8147 /* Action hook installed via XtAppAddActionHook when toolkit scroll
8148 bars are used.. The hook is responsible for detecting when
8149 the user ends an interaction with the scroll bar, and generates
8150 a `end-scroll' scroll_bar_click' event if so. */
8152 static void
8153 xt_action_hook (widget, client_data, action_name, event, params,
8154 num_params)
8155 Widget widget;
8156 XtPointer client_data;
8157 String action_name;
8158 XEvent *event;
8159 String *params;
8160 Cardinal *num_params;
8162 int scroll_bar_p;
8163 char *end_action;
8165 #ifdef USE_MOTIF
8166 scroll_bar_p = XmIsScrollBar (widget);
8167 end_action = "Release";
8168 #else /* !USE_MOTIF i.e. use Xaw */
8169 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
8170 end_action = "EndScroll";
8171 #endif /* USE_MOTIF */
8173 if (scroll_bar_p
8174 && strcmp (action_name, end_action) == 0
8175 && WINDOWP (window_being_scrolled))
8177 struct window *w;
8179 x_send_scroll_bar_event (window_being_scrolled,
8180 scroll_bar_end_scroll, 0, 0);
8181 w = XWINDOW (window_being_scrolled);
8182 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
8183 window_being_scrolled = Qnil;
8184 last_scroll_bar_part = -1;
8186 /* Xt timeouts no longer needed. */
8187 toolkit_scroll_bar_interaction = 0;
8191 /* A vector of windows used for communication between
8192 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
8194 static struct window **scroll_bar_windows;
8195 static int scroll_bar_windows_size;
8198 /* Send a client message with message type Xatom_Scrollbar for a
8199 scroll action to the frame of WINDOW. PART is a value identifying
8200 the part of the scroll bar that was clicked on. PORTION is the
8201 amount to scroll of a whole of WHOLE. */
8203 static void
8204 x_send_scroll_bar_event (window, part, portion, whole)
8205 Lisp_Object window;
8206 int part, portion, whole;
8208 XEvent event;
8209 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
8210 struct window *w = XWINDOW (window);
8211 struct frame *f = XFRAME (w->frame);
8212 int i;
8214 BLOCK_INPUT;
8216 /* Construct a ClientMessage event to send to the frame. */
8217 ev->type = ClientMessage;
8218 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
8219 ev->display = FRAME_X_DISPLAY (f);
8220 ev->window = FRAME_X_WINDOW (f);
8221 ev->format = 32;
8223 /* We can only transfer 32 bits in the XClientMessageEvent, which is
8224 not enough to store a pointer or Lisp_Object on a 64 bit system.
8225 So, store the window in scroll_bar_windows and pass the index
8226 into that array in the event. */
8227 for (i = 0; i < scroll_bar_windows_size; ++i)
8228 if (scroll_bar_windows[i] == NULL)
8229 break;
8231 if (i == scroll_bar_windows_size)
8233 int new_size = max (10, 2 * scroll_bar_windows_size);
8234 size_t nbytes = new_size * sizeof *scroll_bar_windows;
8235 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
8237 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
8238 nbytes);
8239 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
8240 scroll_bar_windows_size = new_size;
8243 scroll_bar_windows[i] = w;
8244 ev->data.l[0] = (long) i;
8245 ev->data.l[1] = (long) part;
8246 ev->data.l[2] = (long) 0;
8247 ev->data.l[3] = (long) portion;
8248 ev->data.l[4] = (long) whole;
8250 /* Make Xt timeouts work while the scroll bar is active. */
8251 toolkit_scroll_bar_interaction = 1;
8253 /* Setting the event mask to zero means that the message will
8254 be sent to the client that created the window, and if that
8255 window no longer exists, no event will be sent. */
8256 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
8257 UNBLOCK_INPUT;
8261 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
8262 in *IEVENT. */
8264 static void
8265 x_scroll_bar_to_input_event (event, ievent)
8266 XEvent *event;
8267 struct input_event *ievent;
8269 XClientMessageEvent *ev = (XClientMessageEvent *) event;
8270 Lisp_Object window;
8271 struct frame *f;
8272 struct window *w;
8274 w = scroll_bar_windows[ev->data.l[0]];
8275 scroll_bar_windows[ev->data.l[0]] = NULL;
8277 XSETWINDOW (window, w);
8278 f = XFRAME (w->frame);
8280 ievent->kind = scroll_bar_click;
8281 ievent->frame_or_window = window;
8282 ievent->arg = Qnil;
8283 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
8284 ievent->part = ev->data.l[1];
8285 ievent->code = ev->data.l[2];
8286 ievent->x = make_number ((int) ev->data.l[3]);
8287 ievent->y = make_number ((int) ev->data.l[4]);
8288 ievent->modifiers = 0;
8292 #ifdef USE_MOTIF
8294 /* Minimum and maximum values used for Motif scroll bars. */
8296 #define XM_SB_MIN 1
8297 #define XM_SB_MAX 10000000
8298 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
8301 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
8302 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
8303 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
8305 static void
8306 xm_scroll_callback (widget, client_data, call_data)
8307 Widget widget;
8308 XtPointer client_data, call_data;
8310 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8311 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
8312 double percent;
8313 int part = -1, whole = 0, portion = 0;
8315 switch (cs->reason)
8317 case XmCR_DECREMENT:
8318 bar->dragging = Qnil;
8319 part = scroll_bar_up_arrow;
8320 break;
8322 case XmCR_INCREMENT:
8323 bar->dragging = Qnil;
8324 part = scroll_bar_down_arrow;
8325 break;
8327 case XmCR_PAGE_DECREMENT:
8328 bar->dragging = Qnil;
8329 part = scroll_bar_above_handle;
8330 break;
8332 case XmCR_PAGE_INCREMENT:
8333 bar->dragging = Qnil;
8334 part = scroll_bar_below_handle;
8335 break;
8337 case XmCR_TO_TOP:
8338 bar->dragging = Qnil;
8339 part = scroll_bar_to_top;
8340 break;
8342 case XmCR_TO_BOTTOM:
8343 bar->dragging = Qnil;
8344 part = scroll_bar_to_bottom;
8345 break;
8347 case XmCR_DRAG:
8349 int slider_size;
8350 int dragging_down_p = (INTEGERP (bar->dragging)
8351 && XINT (bar->dragging) <= cs->value);
8353 /* Get the slider size. */
8354 BLOCK_INPUT;
8355 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
8356 UNBLOCK_INPUT;
8358 /* At the max position of the scroll bar, do a line-wise
8359 movement. Without doing anything, we would be called with
8360 the same cs->value again and again. If we want to make
8361 sure that we can reach the end of the buffer, we have to do
8362 something.
8364 Implementation note: setting bar->dragging always to
8365 cs->value gives a smoother movement at the max position.
8366 Setting it to nil when doing line-wise movement gives
8367 a better slider behavior. */
8369 if (cs->value + slider_size == XM_SB_MAX
8370 || (dragging_down_p
8371 && last_scroll_bar_part == scroll_bar_down_arrow))
8373 part = scroll_bar_down_arrow;
8374 bar->dragging = Qnil;
8376 else
8378 whole = XM_SB_RANGE;
8379 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
8380 part = scroll_bar_handle;
8381 bar->dragging = make_number (cs->value);
8384 break;
8386 case XmCR_VALUE_CHANGED:
8387 break;
8390 if (part >= 0)
8392 window_being_scrolled = bar->window;
8393 last_scroll_bar_part = part;
8394 x_send_scroll_bar_event (bar->window, part, portion, whole);
8399 #else /* !USE_MOTIF, i.e. Xaw. */
8402 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
8403 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8404 scroll bar struct. CALL_DATA is a pointer to a float saying where
8405 the thumb is. */
8407 static void
8408 xaw_jump_callback (widget, client_data, call_data)
8409 Widget widget;
8410 XtPointer client_data, call_data;
8412 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8413 float top = *(float *) call_data;
8414 float shown;
8415 int whole, portion, height;
8416 int part;
8418 /* Get the size of the thumb, a value between 0 and 1. */
8419 BLOCK_INPUT;
8420 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
8421 UNBLOCK_INPUT;
8423 whole = 10000000;
8424 portion = shown < 1 ? top * whole : 0;
8426 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8427 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8428 the bottom, so we force the scrolling whenever we see that we're
8429 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8430 we try to ensure that we always stay two pixels away from the
8431 bottom). */
8432 part = scroll_bar_down_arrow;
8433 else
8434 part = scroll_bar_handle;
8436 window_being_scrolled = bar->window;
8437 bar->dragging = make_number (portion);
8438 last_scroll_bar_part = part;
8439 x_send_scroll_bar_event (bar->window, part, portion, whole);
8443 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
8444 i.e. line or page up or down. WIDGET is the Xaw scroll bar
8445 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8446 the scroll bar. CALL_DATA is an integer specifying the action that
8447 has taken place. It's magnitude is in the range 0..height of the
8448 scroll bar. Negative values mean scroll towards buffer start.
8449 Values < height of scroll bar mean line-wise movement. */
8451 static void
8452 xaw_scroll_callback (widget, client_data, call_data)
8453 Widget widget;
8454 XtPointer client_data, call_data;
8456 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8457 int position = (int) call_data;
8458 Dimension height;
8459 int part;
8461 /* Get the height of the scroll bar. */
8462 BLOCK_INPUT;
8463 XtVaGetValues (widget, XtNheight, &height, NULL);
8464 UNBLOCK_INPUT;
8466 if (abs (position) >= height)
8467 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8469 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8470 it maps line-movement to call_data = max(5, height/20). */
8471 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8472 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
8473 else
8474 part = scroll_bar_move_ratio;
8476 window_being_scrolled = bar->window;
8477 bar->dragging = Qnil;
8478 last_scroll_bar_part = part;
8479 x_send_scroll_bar_event (bar->window, part, position, height);
8483 #endif /* not USE_MOTIF */
8486 /* Create the widget for scroll bar BAR on frame F. Record the widget
8487 and X window of the scroll bar in BAR. */
8489 static void
8490 x_create_toolkit_scroll_bar (f, bar)
8491 struct frame *f;
8492 struct scroll_bar *bar;
8494 Window xwindow;
8495 Widget widget;
8496 Arg av[20];
8497 int ac = 0;
8498 char *scroll_bar_name = "verticalScrollBar";
8499 unsigned long pixel;
8501 BLOCK_INPUT;
8503 #ifdef USE_MOTIF
8504 /* Set resources. Create the widget. */
8505 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8506 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8507 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8508 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8509 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8510 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8511 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8513 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8514 if (pixel != -1)
8516 XtSetArg (av[ac], XmNforeground, pixel);
8517 ++ac;
8520 pixel = f->output_data.x->scroll_bar_background_pixel;
8521 if (pixel != -1)
8523 XtSetArg (av[ac], XmNbackground, pixel);
8524 ++ac;
8527 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8528 scroll_bar_name, av, ac);
8530 /* Add one callback for everything that can happen. */
8531 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8532 (XtPointer) bar);
8533 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8534 (XtPointer) bar);
8535 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8536 (XtPointer) bar);
8537 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8538 (XtPointer) bar);
8539 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8540 (XtPointer) bar);
8541 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8542 (XtPointer) bar);
8543 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8544 (XtPointer) bar);
8546 /* Realize the widget. Only after that is the X window created. */
8547 XtRealizeWidget (widget);
8549 /* Set the cursor to an arrow. I didn't find a resource to do that.
8550 And I'm wondering why it hasn't an arrow cursor by default. */
8551 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8552 f->output_data.x->nontext_cursor);
8554 #else /* !USE_MOTIF i.e. use Xaw */
8556 /* Set resources. Create the widget. The background of the
8557 Xaw3d scroll bar widget is a little bit light for my taste.
8558 We don't alter it here to let users change it according
8559 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8560 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8561 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
8562 /* For smoother scrolling with Xaw3d -sm */
8563 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8564 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
8566 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8567 if (pixel != -1)
8569 XtSetArg (av[ac], XtNforeground, pixel);
8570 ++ac;
8573 pixel = f->output_data.x->scroll_bar_background_pixel;
8574 if (pixel != -1)
8576 XtSetArg (av[ac], XtNbackground, pixel);
8577 ++ac;
8580 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8581 f->output_data.x->edit_widget, av, ac);
8584 char *initial = "";
8585 char *val = initial;
8586 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8587 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8588 if (val == initial)
8589 { /* ARROW_SCROLL */
8590 xaw3d_arrow_scroll = True;
8591 /* Isn't that just a personal preference ? -sm */
8592 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8596 /* Define callbacks. */
8597 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8598 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
8599 (XtPointer) bar);
8601 /* Realize the widget. Only after that is the X window created. */
8602 XtRealizeWidget (widget);
8604 #endif /* !USE_MOTIF */
8606 /* Install an action hook that let's us detect when the user
8607 finishes interacting with a scroll bar. */
8608 if (action_hook_id == 0)
8609 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8611 /* Remember X window and widget in the scroll bar vector. */
8612 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8613 xwindow = XtWindow (widget);
8614 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8616 UNBLOCK_INPUT;
8620 /* Set the thumb size and position of scroll bar BAR. We are currently
8621 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8623 static void
8624 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8625 struct scroll_bar *bar;
8626 int portion, position, whole;
8628 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8629 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8630 float top, shown;
8632 if (whole == 0)
8633 top = 0, shown = 1;
8634 else
8636 top = (float) position / whole;
8637 shown = (float) portion / whole;
8640 BLOCK_INPUT;
8642 #ifdef USE_MOTIF
8644 int size, value;
8645 XmScrollBarWidget sb;
8647 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
8648 is the scroll bar's maximum and MIN is the scroll bar's minimum
8649 value. */
8650 size = shown * XM_SB_RANGE;
8651 size = min (size, XM_SB_RANGE);
8652 size = max (size, 1);
8654 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8655 value = top * XM_SB_RANGE;
8656 value = min (value, XM_SB_MAX - size);
8657 value = max (value, XM_SB_MIN);
8659 if (NILP (bar->dragging))
8660 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8661 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8662 /* This has the negative side effect that the slider value is
8663 not what it would be if we scrolled here using line-wise or
8664 page-wise movement. */
8665 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8666 else
8668 /* If currently dragging, only update the slider size.
8669 This reduces flicker effects. */
8670 int old_value, old_size, increment, page_increment;
8672 XmScrollBarGetValues (widget, &old_value, &old_size,
8673 &increment, &page_increment);
8674 XmScrollBarSetValues (widget, old_value,
8675 min (size, XM_SB_RANGE - old_value),
8676 0, 0, False);
8679 #else /* !USE_MOTIF i.e. use Xaw */
8681 float old_top, old_shown;
8682 Dimension height;
8683 XtVaGetValues (widget,
8684 XtNtopOfThumb, &old_top,
8685 XtNshown, &old_shown,
8686 XtNheight, &height,
8687 NULL);
8689 /* Massage the top+shown values. */
8690 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8691 top = max (0, min (1, top));
8692 else
8693 top = old_top;
8694 /* Keep two pixels available for moving the thumb down. */
8695 shown = max (0, min (1 - top - (2.0 / height), shown));
8697 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8698 check that your system's configuration file contains a define
8699 for `NARROWPROTO'. See s/freebsd.h for an example. */
8700 if (top != old_top || shown != old_shown)
8702 if (NILP (bar->dragging))
8703 XawScrollbarSetThumb (widget, top, shown);
8704 else
8706 #ifdef HAVE_XAW3D
8707 ScrollbarWidget sb = (ScrollbarWidget) widget;
8708 int scroll_mode = 0;
8710 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8711 if (xaw3d_arrow_scroll)
8713 /* Xaw3d stupidly ignores resize requests while dragging
8714 so we have to make it believe it's not in dragging mode. */
8715 scroll_mode = sb->scrollbar.scroll_mode;
8716 if (scroll_mode == 2)
8717 sb->scrollbar.scroll_mode = 0;
8719 #endif
8720 /* Try to make the scrolling a tad smoother. */
8721 if (!xaw3d_pick_top)
8722 shown = min (shown, old_shown);
8724 XawScrollbarSetThumb (widget, top, shown);
8726 #ifdef HAVE_XAW3D
8727 if (xaw3d_arrow_scroll && scroll_mode == 2)
8728 sb->scrollbar.scroll_mode = scroll_mode;
8729 #endif
8733 #endif /* !USE_MOTIF */
8735 UNBLOCK_INPUT;
8738 #endif /* USE_TOOLKIT_SCROLL_BARS */
8742 /************************************************************************
8743 Scroll bars, general
8744 ************************************************************************/
8746 /* Create a scroll bar and return the scroll bar vector for it. W is
8747 the Emacs window on which to create the scroll bar. TOP, LEFT,
8748 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8749 scroll bar. */
8751 static struct scroll_bar *
8752 x_scroll_bar_create (w, top, left, width, height)
8753 struct window *w;
8754 int top, left, width, height;
8756 struct frame *f = XFRAME (w->frame);
8757 struct scroll_bar *bar
8758 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8760 BLOCK_INPUT;
8762 #ifdef USE_TOOLKIT_SCROLL_BARS
8763 x_create_toolkit_scroll_bar (f, bar);
8764 #else /* not USE_TOOLKIT_SCROLL_BARS */
8766 XSetWindowAttributes a;
8767 unsigned long mask;
8768 Window window;
8770 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8771 if (a.background_pixel == -1)
8772 a.background_pixel = f->output_data.x->background_pixel;
8774 a.event_mask = (ButtonPressMask | ButtonReleaseMask
8775 | ButtonMotionMask | PointerMotionHintMask
8776 | ExposureMask);
8777 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
8779 mask = (CWBackPixel | CWEventMask | CWCursor);
8781 /* Clear the area of W that will serve as a scroll bar. This is
8782 for the case that a window has been split horizontally. In
8783 this case, no clear_frame is generated to reduce flickering. */
8784 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8785 left, top, width,
8786 window_box_height (w), False);
8788 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8789 /* Position and size of scroll bar. */
8790 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8791 top,
8792 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8793 height,
8794 /* Border width, depth, class, and visual. */
8796 CopyFromParent,
8797 CopyFromParent,
8798 CopyFromParent,
8799 /* Attributes. */
8800 mask, &a);
8801 SET_SCROLL_BAR_X_WINDOW (bar, window);
8803 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8805 XSETWINDOW (bar->window, w);
8806 XSETINT (bar->top, top);
8807 XSETINT (bar->left, left);
8808 XSETINT (bar->width, width);
8809 XSETINT (bar->height, height);
8810 XSETINT (bar->start, 0);
8811 XSETINT (bar->end, 0);
8812 bar->dragging = Qnil;
8814 /* Add bar to its frame's list of scroll bars. */
8815 bar->next = FRAME_SCROLL_BARS (f);
8816 bar->prev = Qnil;
8817 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8818 if (!NILP (bar->next))
8819 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8821 /* Map the window/widget. */
8822 #ifdef USE_TOOLKIT_SCROLL_BARS
8824 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8825 XtConfigureWidget (scroll_bar,
8826 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8827 top,
8828 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8829 max (height, 1), 0);
8830 XtMapWidget (scroll_bar);
8832 #else /* not USE_TOOLKIT_SCROLL_BARS */
8833 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8834 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8836 UNBLOCK_INPUT;
8837 return bar;
8841 /* Draw BAR's handle in the proper position.
8843 If the handle is already drawn from START to END, don't bother
8844 redrawing it, unless REBUILD is non-zero; in that case, always
8845 redraw it. (REBUILD is handy for drawing the handle after expose
8846 events.)
8848 Normally, we want to constrain the start and end of the handle to
8849 fit inside its rectangle, but if the user is dragging the scroll
8850 bar handle, we want to let them drag it down all the way, so that
8851 the bar's top is as far down as it goes; otherwise, there's no way
8852 to move to the very end of the buffer. */
8854 #ifndef USE_TOOLKIT_SCROLL_BARS
8856 static void
8857 x_scroll_bar_set_handle (bar, start, end, rebuild)
8858 struct scroll_bar *bar;
8859 int start, end;
8860 int rebuild;
8862 int dragging = ! NILP (bar->dragging);
8863 Window w = SCROLL_BAR_X_WINDOW (bar);
8864 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8865 GC gc = f->output_data.x->normal_gc;
8867 /* If the display is already accurate, do nothing. */
8868 if (! rebuild
8869 && start == XINT (bar->start)
8870 && end == XINT (bar->end))
8871 return;
8873 BLOCK_INPUT;
8876 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8877 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8878 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8880 /* Make sure the values are reasonable, and try to preserve
8881 the distance between start and end. */
8883 int length = end - start;
8885 if (start < 0)
8886 start = 0;
8887 else if (start > top_range)
8888 start = top_range;
8889 end = start + length;
8891 if (end < start)
8892 end = start;
8893 else if (end > top_range && ! dragging)
8894 end = top_range;
8897 /* Store the adjusted setting in the scroll bar. */
8898 XSETINT (bar->start, start);
8899 XSETINT (bar->end, end);
8901 /* Clip the end position, just for display. */
8902 if (end > top_range)
8903 end = top_range;
8905 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
8906 below top positions, to make sure the handle is always at least
8907 that many pixels tall. */
8908 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
8910 /* Draw the empty space above the handle. Note that we can't clear
8911 zero-height areas; that means "clear to end of window." */
8912 if (0 < start)
8913 x_clear_area (FRAME_X_DISPLAY (f), w,
8914 /* x, y, width, height, and exposures. */
8915 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8916 VERTICAL_SCROLL_BAR_TOP_BORDER,
8917 inside_width, start,
8918 False);
8920 /* Change to proper foreground color if one is specified. */
8921 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8922 XSetForeground (FRAME_X_DISPLAY (f), gc,
8923 f->output_data.x->scroll_bar_foreground_pixel);
8925 /* Draw the handle itself. */
8926 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
8927 /* x, y, width, height */
8928 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8929 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
8930 inside_width, end - start);
8932 /* Restore the foreground color of the GC if we changed it above. */
8933 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8934 XSetForeground (FRAME_X_DISPLAY (f), gc,
8935 f->output_data.x->foreground_pixel);
8937 /* Draw the empty space below the handle. Note that we can't
8938 clear zero-height areas; that means "clear to end of window." */
8939 if (end < inside_height)
8940 x_clear_area (FRAME_X_DISPLAY (f), w,
8941 /* x, y, width, height, and exposures. */
8942 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8943 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
8944 inside_width, inside_height - end,
8945 False);
8949 UNBLOCK_INPUT;
8952 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8954 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8955 nil. */
8957 static void
8958 x_scroll_bar_remove (bar)
8959 struct scroll_bar *bar;
8961 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8962 BLOCK_INPUT;
8964 #ifdef USE_TOOLKIT_SCROLL_BARS
8965 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
8966 #else
8967 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8968 #endif
8970 /* Disassociate this scroll bar from its window. */
8971 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
8973 UNBLOCK_INPUT;
8977 /* Set the handle of the vertical scroll bar for WINDOW to indicate
8978 that we are displaying PORTION characters out of a total of WHOLE
8979 characters, starting at POSITION. If WINDOW has no scroll bar,
8980 create one. */
8982 static void
8983 XTset_vertical_scroll_bar (w, portion, whole, position)
8984 struct window *w;
8985 int portion, whole, position;
8987 struct frame *f = XFRAME (w->frame);
8988 struct scroll_bar *bar;
8989 int top, height, left, sb_left, width, sb_width;
8990 int window_x, window_y, window_width, window_height;
8992 /* Get window dimensions. */
8993 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8994 top = window_y;
8995 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8996 height = window_height;
8998 /* Compute the left edge of the scroll bar area. */
8999 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9000 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
9001 else
9002 left = XFASTINT (w->left);
9003 left *= CANON_X_UNIT (f);
9004 left += FRAME_INTERNAL_BORDER_WIDTH (f);
9006 /* Compute the width of the scroll bar which might be less than
9007 the width of the area reserved for the scroll bar. */
9008 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
9009 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
9010 else
9011 sb_width = width;
9013 /* Compute the left edge of the scroll bar. */
9014 #ifdef USE_TOOLKIT_SCROLL_BARS
9015 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9016 sb_left = left + width - sb_width - (width - sb_width) / 2;
9017 else
9018 sb_left = left + (width - sb_width) / 2;
9019 #else
9020 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9021 sb_left = left + width - sb_width;
9022 else
9023 sb_left = left;
9024 #endif
9026 /* Does the scroll bar exist yet? */
9027 if (NILP (w->vertical_scroll_bar))
9029 BLOCK_INPUT;
9030 if (width && height)
9031 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9032 left, top, width, height, False);
9033 UNBLOCK_INPUT;
9034 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
9036 else
9038 /* It may just need to be moved and resized. */
9039 unsigned int mask = 0;
9041 bar = XSCROLL_BAR (w->vertical_scroll_bar);
9043 BLOCK_INPUT;
9045 if (sb_left != XINT (bar->left))
9046 mask |= CWX;
9047 if (top != XINT (bar->top))
9048 mask |= CWY;
9049 if (sb_width != XINT (bar->width))
9050 mask |= CWWidth;
9051 if (height != XINT (bar->height))
9052 mask |= CWHeight;
9054 #ifdef USE_TOOLKIT_SCROLL_BARS
9056 /* Since toolkit scroll bars are smaller than the space reserved
9057 for them on the frame, we have to clear "under" them. */
9058 if (width && height)
9059 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9060 left, top, width, height, False);
9062 /* Move/size the scroll bar widget. */
9063 if (mask)
9064 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
9065 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9066 top,
9067 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9068 max (height, 1), 0);
9070 #else /* not USE_TOOLKIT_SCROLL_BARS */
9072 /* Clear areas not covered by the scroll bar because of
9073 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
9074 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
9076 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9077 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9078 height, False);
9079 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9080 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9081 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9082 height, False);
9085 /* Clear areas not covered by the scroll bar because it's not as
9086 wide as the area reserved for it . This makes sure a
9087 previous mode line display is cleared after C-x 2 C-x 1, for
9088 example. */
9090 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9091 int rest = area_width - sb_width;
9092 if (rest > 0)
9093 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9094 left + area_width - rest, 0,
9095 rest, max (height, 1), False);
9098 /* Move/size the scroll bar window. */
9099 if (mask)
9101 XWindowChanges wc;
9103 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9104 wc.y = top;
9105 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
9106 wc.height = height;
9107 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
9108 mask, &wc);
9111 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9113 /* Remember new settings. */
9114 XSETINT (bar->left, sb_left);
9115 XSETINT (bar->top, top);
9116 XSETINT (bar->width, sb_width);
9117 XSETINT (bar->height, height);
9119 UNBLOCK_INPUT;
9122 #ifdef USE_TOOLKIT_SCROLL_BARS
9123 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
9124 #else /* not USE_TOOLKIT_SCROLL_BARS */
9125 /* Set the scroll bar's current state, unless we're currently being
9126 dragged. */
9127 if (NILP (bar->dragging))
9129 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
9131 if (whole == 0)
9132 x_scroll_bar_set_handle (bar, 0, top_range, 0);
9133 else
9135 int start = ((double) position * top_range) / whole;
9136 int end = ((double) (position + portion) * top_range) / whole;
9137 x_scroll_bar_set_handle (bar, start, end, 0);
9140 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9142 XSETVECTOR (w->vertical_scroll_bar, bar);
9146 /* The following three hooks are used when we're doing a thorough
9147 redisplay of the frame. We don't explicitly know which scroll bars
9148 are going to be deleted, because keeping track of when windows go
9149 away is a real pain - "Can you say set-window-configuration, boys
9150 and girls?" Instead, we just assert at the beginning of redisplay
9151 that *all* scroll bars are to be removed, and then save a scroll bar
9152 from the fiery pit when we actually redisplay its window. */
9154 /* Arrange for all scroll bars on FRAME to be removed at the next call
9155 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
9156 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
9158 static void
9159 XTcondemn_scroll_bars (frame)
9160 FRAME_PTR frame;
9162 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
9163 while (! NILP (FRAME_SCROLL_BARS (frame)))
9165 Lisp_Object bar;
9166 bar = FRAME_SCROLL_BARS (frame);
9167 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
9168 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
9169 XSCROLL_BAR (bar)->prev = Qnil;
9170 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
9171 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
9172 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
9177 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
9178 Note that WINDOW isn't necessarily condemned at all. */
9180 static void
9181 XTredeem_scroll_bar (window)
9182 struct window *window;
9184 struct scroll_bar *bar;
9185 struct frame *f;
9187 /* We can't redeem this window's scroll bar if it doesn't have one. */
9188 if (NILP (window->vertical_scroll_bar))
9189 abort ();
9191 bar = XSCROLL_BAR (window->vertical_scroll_bar);
9193 /* Unlink it from the condemned list. */
9194 f = XFRAME (WINDOW_FRAME (window));
9195 if (NILP (bar->prev))
9197 /* If the prev pointer is nil, it must be the first in one of
9198 the lists. */
9199 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
9200 /* It's not condemned. Everything's fine. */
9201 return;
9202 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
9203 window->vertical_scroll_bar))
9204 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
9205 else
9206 /* If its prev pointer is nil, it must be at the front of
9207 one or the other! */
9208 abort ();
9210 else
9211 XSCROLL_BAR (bar->prev)->next = bar->next;
9213 if (! NILP (bar->next))
9214 XSCROLL_BAR (bar->next)->prev = bar->prev;
9216 bar->next = FRAME_SCROLL_BARS (f);
9217 bar->prev = Qnil;
9218 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9219 if (! NILP (bar->next))
9220 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9223 /* Remove all scroll bars on FRAME that haven't been saved since the
9224 last call to `*condemn_scroll_bars_hook'. */
9226 static void
9227 XTjudge_scroll_bars (f)
9228 FRAME_PTR f;
9230 Lisp_Object bar, next;
9232 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
9234 /* Clear out the condemned list now so we won't try to process any
9235 more events on the hapless scroll bars. */
9236 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
9238 for (; ! NILP (bar); bar = next)
9240 struct scroll_bar *b = XSCROLL_BAR (bar);
9242 x_scroll_bar_remove (b);
9244 next = b->next;
9245 b->next = b->prev = Qnil;
9248 /* Now there should be no references to the condemned scroll bars,
9249 and they should get garbage-collected. */
9253 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
9254 is a no-op when using toolkit scroll bars.
9256 This may be called from a signal handler, so we have to ignore GC
9257 mark bits. */
9259 static void
9260 x_scroll_bar_expose (bar, event)
9261 struct scroll_bar *bar;
9262 XEvent *event;
9264 #ifndef USE_TOOLKIT_SCROLL_BARS
9266 Window w = SCROLL_BAR_X_WINDOW (bar);
9267 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9268 GC gc = f->output_data.x->normal_gc;
9269 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9271 BLOCK_INPUT;
9273 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
9275 /* Draw a one-pixel border just inside the edges of the scroll bar. */
9276 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
9278 /* x, y, width, height */
9279 0, 0,
9280 XINT (bar->width) - 1 - width_trim - width_trim,
9281 XINT (bar->height) - 1);
9283 UNBLOCK_INPUT;
9285 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9288 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
9289 is set to something other than no_event, it is enqueued.
9291 This may be called from a signal handler, so we have to ignore GC
9292 mark bits. */
9294 #ifndef USE_TOOLKIT_SCROLL_BARS
9296 static void
9297 x_scroll_bar_handle_click (bar, event, emacs_event)
9298 struct scroll_bar *bar;
9299 XEvent *event;
9300 struct input_event *emacs_event;
9302 if (! GC_WINDOWP (bar->window))
9303 abort ();
9305 emacs_event->kind = scroll_bar_click;
9306 emacs_event->code = event->xbutton.button - Button1;
9307 emacs_event->modifiers
9308 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
9309 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
9310 event->xbutton.state)
9311 | (event->type == ButtonRelease
9312 ? up_modifier
9313 : down_modifier));
9314 emacs_event->frame_or_window = bar->window;
9315 emacs_event->arg = Qnil;
9316 emacs_event->timestamp = event->xbutton.time;
9318 #if 0
9319 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9320 int internal_height
9321 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9322 #endif
9323 int top_range
9324 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9325 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
9327 if (y < 0) y = 0;
9328 if (y > top_range) y = top_range;
9330 if (y < XINT (bar->start))
9331 emacs_event->part = scroll_bar_above_handle;
9332 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9333 emacs_event->part = scroll_bar_handle;
9334 else
9335 emacs_event->part = scroll_bar_below_handle;
9337 /* Just because the user has clicked on the handle doesn't mean
9338 they want to drag it. Lisp code needs to be able to decide
9339 whether or not we're dragging. */
9340 #if 0
9341 /* If the user has just clicked on the handle, record where they're
9342 holding it. */
9343 if (event->type == ButtonPress
9344 && emacs_event->part == scroll_bar_handle)
9345 XSETINT (bar->dragging, y - XINT (bar->start));
9346 #endif
9348 /* If the user has released the handle, set it to its final position. */
9349 if (event->type == ButtonRelease
9350 && ! NILP (bar->dragging))
9352 int new_start = y - XINT (bar->dragging);
9353 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9355 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9356 bar->dragging = Qnil;
9359 /* Same deal here as the other #if 0. */
9360 #if 0
9361 /* Clicks on the handle are always reported as occurring at the top of
9362 the handle. */
9363 if (emacs_event->part == scroll_bar_handle)
9364 emacs_event->x = bar->start;
9365 else
9366 XSETINT (emacs_event->x, y);
9367 #else
9368 XSETINT (emacs_event->x, y);
9369 #endif
9371 XSETINT (emacs_event->y, top_range);
9375 /* Handle some mouse motion while someone is dragging the scroll bar.
9377 This may be called from a signal handler, so we have to ignore GC
9378 mark bits. */
9380 static void
9381 x_scroll_bar_note_movement (bar, event)
9382 struct scroll_bar *bar;
9383 XEvent *event;
9385 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9387 last_mouse_movement_time = event->xmotion.time;
9389 f->mouse_moved = 1;
9390 XSETVECTOR (last_mouse_scroll_bar, bar);
9392 /* If we're dragging the bar, display it. */
9393 if (! GC_NILP (bar->dragging))
9395 /* Where should the handle be now? */
9396 int new_start = event->xmotion.y - XINT (bar->dragging);
9398 if (new_start != XINT (bar->start))
9400 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9402 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9407 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9409 /* Return information to the user about the current position of the mouse
9410 on the scroll bar. */
9412 static void
9413 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9414 FRAME_PTR *fp;
9415 Lisp_Object *bar_window;
9416 enum scroll_bar_part *part;
9417 Lisp_Object *x, *y;
9418 unsigned long *time;
9420 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
9421 Window w = SCROLL_BAR_X_WINDOW (bar);
9422 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9423 int win_x, win_y;
9424 Window dummy_window;
9425 int dummy_coord;
9426 unsigned int dummy_mask;
9428 BLOCK_INPUT;
9430 /* Get the mouse's position relative to the scroll bar window, and
9431 report that. */
9432 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
9434 /* Root, child, root x and root y. */
9435 &dummy_window, &dummy_window,
9436 &dummy_coord, &dummy_coord,
9438 /* Position relative to scroll bar. */
9439 &win_x, &win_y,
9441 /* Mouse buttons and modifier keys. */
9442 &dummy_mask))
9444 else
9446 #if 0
9447 int inside_height
9448 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9449 #endif
9450 int top_range
9451 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9453 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9455 if (! NILP (bar->dragging))
9456 win_y -= XINT (bar->dragging);
9458 if (win_y < 0)
9459 win_y = 0;
9460 if (win_y > top_range)
9461 win_y = top_range;
9463 *fp = f;
9464 *bar_window = bar->window;
9466 if (! NILP (bar->dragging))
9467 *part = scroll_bar_handle;
9468 else if (win_y < XINT (bar->start))
9469 *part = scroll_bar_above_handle;
9470 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9471 *part = scroll_bar_handle;
9472 else
9473 *part = scroll_bar_below_handle;
9475 XSETINT (*x, win_y);
9476 XSETINT (*y, top_range);
9478 f->mouse_moved = 0;
9479 last_mouse_scroll_bar = Qnil;
9482 *time = last_mouse_movement_time;
9484 UNBLOCK_INPUT;
9488 /* The screen has been cleared so we may have changed foreground or
9489 background colors, and the scroll bars may need to be redrawn.
9490 Clear out the scroll bars, and ask for expose events, so we can
9491 redraw them. */
9493 void
9494 x_scroll_bar_clear (f)
9495 FRAME_PTR f;
9497 #ifndef USE_TOOLKIT_SCROLL_BARS
9498 Lisp_Object bar;
9500 /* We can have scroll bars even if this is 0,
9501 if we just turned off scroll bar mode.
9502 But in that case we should not clear them. */
9503 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9504 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9505 bar = XSCROLL_BAR (bar)->next)
9506 XClearArea (FRAME_X_DISPLAY (f),
9507 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9508 0, 0, 0, 0, True);
9509 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9512 /* This processes Expose events from the menu-bar specific X event
9513 loop in xmenu.c. This allows to redisplay the frame if necessary
9514 when handling menu-bar or pop-up items. */
9517 process_expose_from_menu (event)
9518 XEvent event;
9520 FRAME_PTR f;
9521 struct x_display_info *dpyinfo;
9522 int frame_exposed_p = 0;
9524 BLOCK_INPUT;
9526 dpyinfo = x_display_info_for_display (event.xexpose.display);
9527 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9528 if (f)
9530 if (f->async_visible == 0)
9532 f->async_visible = 1;
9533 f->async_iconified = 0;
9534 f->output_data.x->has_been_visible = 1;
9535 SET_FRAME_GARBAGED (f);
9537 else
9539 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9540 event.xexpose.x, event.xexpose.y,
9541 event.xexpose.width, event.xexpose.height);
9542 frame_exposed_p = 1;
9545 else
9547 struct scroll_bar *bar
9548 = x_window_to_scroll_bar (event.xexpose.window);
9550 if (bar)
9551 x_scroll_bar_expose (bar, &event);
9554 UNBLOCK_INPUT;
9555 return frame_exposed_p;
9558 /* Define a queue to save up SelectionRequest events for later handling. */
9560 struct selection_event_queue
9562 XEvent event;
9563 struct selection_event_queue *next;
9566 static struct selection_event_queue *queue;
9568 /* Nonzero means queue up certain events--don't process them yet. */
9570 static int x_queue_selection_requests;
9572 /* Queue up an X event *EVENT, to be processed later. */
9574 static void
9575 x_queue_event (f, event)
9576 FRAME_PTR f;
9577 XEvent *event;
9579 struct selection_event_queue *queue_tmp
9580 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
9582 if (queue_tmp != NULL)
9584 queue_tmp->event = *event;
9585 queue_tmp->next = queue;
9586 queue = queue_tmp;
9590 /* Take all the queued events and put them back
9591 so that they get processed afresh. */
9593 static void
9594 x_unqueue_events (display)
9595 Display *display;
9597 while (queue != NULL)
9599 struct selection_event_queue *queue_tmp = queue;
9600 XPutBackEvent (display, &queue_tmp->event);
9601 queue = queue_tmp->next;
9602 xfree ((char *)queue_tmp);
9606 /* Start queuing SelectionRequest events. */
9608 void
9609 x_start_queuing_selection_requests (display)
9610 Display *display;
9612 x_queue_selection_requests++;
9615 /* Stop queuing SelectionRequest events. */
9617 void
9618 x_stop_queuing_selection_requests (display)
9619 Display *display;
9621 x_queue_selection_requests--;
9622 x_unqueue_events (display);
9625 /* The main X event-reading loop - XTread_socket. */
9627 /* Time stamp of enter window event. This is only used by XTread_socket,
9628 but we have to put it out here, since static variables within functions
9629 sometimes don't work. */
9631 static Time enter_timestamp;
9633 /* This holds the state XLookupString needs to implement dead keys
9634 and other tricks known as "compose processing". _X Window System_
9635 says that a portable program can't use this, but Stephen Gildea assures
9636 me that letting the compiler initialize it to zeros will work okay.
9638 This must be defined outside of XTread_socket, for the same reasons
9639 given for enter_time stamp, above. */
9641 static XComposeStatus compose_status;
9643 /* Record the last 100 characters stored
9644 to help debug the loss-of-chars-during-GC problem. */
9646 static int temp_index;
9647 static short temp_buffer[100];
9649 /* Set this to nonzero to fake an "X I/O error"
9650 on a particular display. */
9652 struct x_display_info *XTread_socket_fake_io_error;
9654 /* When we find no input here, we occasionally do a no-op command
9655 to verify that the X server is still running and we can still talk with it.
9656 We try all the open displays, one by one.
9657 This variable is used for cycling thru the displays. */
9659 static struct x_display_info *next_noop_dpyinfo;
9661 #define SET_SAVED_MENU_EVENT(size) \
9662 do \
9664 if (f->output_data.x->saved_menu_event == 0) \
9665 f->output_data.x->saved_menu_event \
9666 = (XEvent *) xmalloc (sizeof (XEvent)); \
9667 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9668 if (numchars >= 1) \
9670 bufp->kind = menu_bar_activate_event; \
9671 XSETFRAME (bufp->frame_or_window, f); \
9672 bufp->arg = Qnil; \
9673 bufp++; \
9674 count++; \
9675 numchars--; \
9678 while (0)
9680 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9681 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9683 /* Read events coming from the X server.
9684 This routine is called by the SIGIO handler.
9685 We return as soon as there are no more events to be read.
9687 Events representing keys are stored in buffer BUFP,
9688 which can hold up to NUMCHARS characters.
9689 We return the number of characters stored into the buffer,
9690 thus pretending to be `read'.
9692 EXPECTED is nonzero if the caller knows input is available. */
9695 XTread_socket (sd, bufp, numchars, expected)
9696 register int sd;
9697 /* register */ struct input_event *bufp;
9698 /* register */ int numchars;
9699 int expected;
9701 int count = 0;
9702 int nbytes = 0;
9703 XEvent event;
9704 struct frame *f;
9705 int event_found = 0;
9706 struct x_display_info *dpyinfo;
9707 struct coding_system coding;
9709 if (interrupt_input_blocked)
9711 interrupt_input_pending = 1;
9712 return -1;
9715 interrupt_input_pending = 0;
9716 BLOCK_INPUT;
9718 /* So people can tell when we have read the available input. */
9719 input_signal_count++;
9721 if (numchars <= 0)
9722 abort (); /* Don't think this happens. */
9724 ++handling_signal;
9726 /* The input should be decoded if it is from XIM. Currently the
9727 locale of XIM is the same as that of the system. So, we can use
9728 Vlocale_coding_system which is initialized properly at Emacs
9729 startup time. */
9730 setup_coding_system (Vlocale_coding_system, &coding);
9731 coding.src_multibyte = 0;
9732 coding.dst_multibyte = 1;
9733 /* The input is converted to events, thus we can't handle
9734 composition. Anyway, there's no XIM that gives us composition
9735 information. */
9736 coding.composing = COMPOSITION_DISABLED;
9738 /* Find the display we are supposed to read input for.
9739 It's the one communicating on descriptor SD. */
9740 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9742 #if 0 /* This ought to be unnecessary; let's verify it. */
9743 #ifdef FIOSNBIO
9744 /* If available, Xlib uses FIOSNBIO to make the socket
9745 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
9746 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
9747 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
9748 fcntl (dpyinfo->connection, F_SETFL, 0);
9749 #endif /* ! defined (FIOSNBIO) */
9750 #endif
9752 #if 0 /* This code can't be made to work, with multiple displays,
9753 and appears not to be used on any system any more.
9754 Also keyboard.c doesn't turn O_NDELAY on and off
9755 for X connections. */
9756 #ifndef SIGIO
9757 #ifndef HAVE_SELECT
9758 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9760 extern int read_alarm_should_throw;
9761 read_alarm_should_throw = 1;
9762 XPeekEvent (dpyinfo->display, &event);
9763 read_alarm_should_throw = 0;
9765 #endif /* HAVE_SELECT */
9766 #endif /* SIGIO */
9767 #endif
9769 /* For debugging, this gives a way to fake an I/O error. */
9770 if (dpyinfo == XTread_socket_fake_io_error)
9772 XTread_socket_fake_io_error = 0;
9773 x_io_error_quitter (dpyinfo->display);
9776 while (XPending (dpyinfo->display))
9778 XNextEvent (dpyinfo->display, &event);
9780 #ifdef HAVE_X_I18N
9782 /* Filter events for the current X input method.
9783 XFilterEvent returns non-zero if the input method has
9784 consumed the event. We pass the frame's X window to
9785 XFilterEvent because that's the one for which the IC
9786 was created. */
9787 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9788 event.xclient.window);
9789 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
9790 break;
9792 #endif
9793 event_found = 1;
9795 switch (event.type)
9797 case ClientMessage:
9799 if (event.xclient.message_type
9800 == dpyinfo->Xatom_wm_protocols
9801 && event.xclient.format == 32)
9803 if (event.xclient.data.l[0]
9804 == dpyinfo->Xatom_wm_take_focus)
9806 /* Use x_any_window_to_frame because this
9807 could be the shell widget window
9808 if the frame has no title bar. */
9809 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
9810 #ifdef HAVE_X_I18N
9811 /* Not quite sure this is needed -pd */
9812 if (f && FRAME_XIC (f))
9813 XSetICFocus (FRAME_XIC (f));
9814 #endif
9815 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9816 instructs the WM to set the input focus automatically for
9817 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9818 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9819 it has set the focus. So, XSetInputFocus below is not
9820 needed.
9822 The call to XSetInputFocus below has also caused trouble. In
9823 cases where the XSetInputFocus done by the WM and the one
9824 below are temporally close (on a fast machine), the call
9825 below can generate additional FocusIn events which confuse
9826 Emacs. */
9828 /* Since we set WM_TAKE_FOCUS, we must call
9829 XSetInputFocus explicitly. But not if f is null,
9830 since that might be an event for a deleted frame. */
9831 if (f)
9833 Display *d = event.xclient.display;
9834 /* Catch and ignore errors, in case window has been
9835 iconified by a window manager such as GWM. */
9836 int count = x_catch_errors (d);
9837 XSetInputFocus (d, event.xclient.window,
9838 /* The ICCCM says this is
9839 the only valid choice. */
9840 RevertToParent,
9841 event.xclient.data.l[1]);
9842 /* This is needed to detect the error
9843 if there is an error. */
9844 XSync (d, False);
9845 x_uncatch_errors (d, count);
9847 /* Not certain about handling scroll bars here */
9848 #endif /* 0 */
9850 else if (event.xclient.data.l[0]
9851 == dpyinfo->Xatom_wm_save_yourself)
9853 /* Save state modify the WM_COMMAND property to
9854 something which can reinstate us. This notifies
9855 the session manager, who's looking for such a
9856 PropertyNotify. Can restart processing when
9857 a keyboard or mouse event arrives. */
9858 if (numchars > 0)
9860 f = x_top_window_to_frame (dpyinfo,
9861 event.xclient.window);
9863 /* This is just so we only give real data once
9864 for a single Emacs process. */
9865 if (f == SELECTED_FRAME ())
9866 XSetCommand (FRAME_X_DISPLAY (f),
9867 event.xclient.window,
9868 initial_argv, initial_argc);
9869 else if (f)
9870 XSetCommand (FRAME_X_DISPLAY (f),
9871 event.xclient.window,
9872 0, 0);
9875 else if (event.xclient.data.l[0]
9876 == dpyinfo->Xatom_wm_delete_window)
9878 struct frame *f
9879 = x_any_window_to_frame (dpyinfo,
9880 event.xclient.window);
9882 if (f)
9884 if (numchars == 0)
9885 abort ();
9887 bufp->kind = delete_window_event;
9888 XSETFRAME (bufp->frame_or_window, f);
9889 bufp->arg = Qnil;
9890 bufp++;
9892 count += 1;
9893 numchars -= 1;
9897 else if (event.xclient.message_type
9898 == dpyinfo->Xatom_wm_configure_denied)
9901 else if (event.xclient.message_type
9902 == dpyinfo->Xatom_wm_window_moved)
9904 int new_x, new_y;
9905 struct frame *f
9906 = x_window_to_frame (dpyinfo, event.xclient.window);
9908 new_x = event.xclient.data.s[0];
9909 new_y = event.xclient.data.s[1];
9911 if (f)
9913 f->output_data.x->left_pos = new_x;
9914 f->output_data.x->top_pos = new_y;
9917 #ifdef HACK_EDITRES
9918 else if (event.xclient.message_type
9919 == dpyinfo->Xatom_editres)
9921 struct frame *f
9922 = x_any_window_to_frame (dpyinfo, event.xclient.window);
9923 _XEditResCheckMessages (f->output_data.x->widget, NULL,
9924 &event, NULL);
9926 #endif /* HACK_EDITRES */
9927 else if ((event.xclient.message_type
9928 == dpyinfo->Xatom_DONE)
9929 || (event.xclient.message_type
9930 == dpyinfo->Xatom_PAGE))
9932 /* Ghostview job completed. Kill it. We could
9933 reply with "Next" if we received "Page", but we
9934 currently never do because we are interested in
9935 images, only, which should have 1 page. */
9936 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9937 struct frame *f
9938 = x_window_to_frame (dpyinfo, event.xclient.window);
9939 x_kill_gs_process (pixmap, f);
9940 expose_frame (f, 0, 0, 0, 0);
9942 #ifdef USE_TOOLKIT_SCROLL_BARS
9943 /* Scroll bar callbacks send a ClientMessage from which
9944 we construct an input_event. */
9945 else if (event.xclient.message_type
9946 == dpyinfo->Xatom_Scrollbar)
9948 x_scroll_bar_to_input_event (&event, bufp);
9949 ++bufp, ++count, --numchars;
9950 goto out;
9952 #endif /* USE_TOOLKIT_SCROLL_BARS */
9953 else
9954 goto OTHER;
9956 break;
9958 case SelectionNotify:
9959 #ifdef USE_X_TOOLKIT
9960 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
9961 goto OTHER;
9962 #endif /* not USE_X_TOOLKIT */
9963 x_handle_selection_notify (&event.xselection);
9964 break;
9966 case SelectionClear: /* Someone has grabbed ownership. */
9967 #ifdef USE_X_TOOLKIT
9968 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
9969 goto OTHER;
9970 #endif /* USE_X_TOOLKIT */
9972 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
9974 if (numchars == 0)
9975 abort ();
9977 bufp->kind = selection_clear_event;
9978 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9979 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9980 SELECTION_EVENT_TIME (bufp) = eventp->time;
9981 bufp->frame_or_window = Qnil;
9982 bufp->arg = Qnil;
9983 bufp++;
9985 count += 1;
9986 numchars -= 1;
9988 break;
9990 case SelectionRequest: /* Someone wants our selection. */
9991 #ifdef USE_X_TOOLKIT
9992 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
9993 goto OTHER;
9994 #endif /* USE_X_TOOLKIT */
9995 if (x_queue_selection_requests)
9996 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
9997 &event);
9998 else
10000 XSelectionRequestEvent *eventp
10001 = (XSelectionRequestEvent *) &event;
10003 if (numchars == 0)
10004 abort ();
10006 bufp->kind = selection_request_event;
10007 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10008 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
10009 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10010 SELECTION_EVENT_TARGET (bufp) = eventp->target;
10011 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
10012 SELECTION_EVENT_TIME (bufp) = eventp->time;
10013 bufp->frame_or_window = Qnil;
10014 bufp->arg = Qnil;
10015 bufp++;
10017 count += 1;
10018 numchars -= 1;
10020 break;
10022 case PropertyNotify:
10023 #if 0 /* This is plain wrong. In the case that we are waiting for a
10024 PropertyNotify used as an ACK in incremental selection
10025 transfer, the property will be on the receiver's window. */
10026 #if defined USE_X_TOOLKIT
10027 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
10028 goto OTHER;
10029 #endif
10030 #endif
10031 x_handle_property_notify (&event.xproperty);
10032 goto OTHER;
10034 case ReparentNotify:
10035 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
10036 if (f)
10038 int x, y;
10039 f->output_data.x->parent_desc = event.xreparent.parent;
10040 x_real_positions (f, &x, &y);
10041 f->output_data.x->left_pos = x;
10042 f->output_data.x->top_pos = y;
10044 break;
10046 case Expose:
10047 f = x_window_to_frame (dpyinfo, event.xexpose.window);
10048 if (f)
10050 if (f->async_visible == 0)
10052 f->async_visible = 1;
10053 f->async_iconified = 0;
10054 f->output_data.x->has_been_visible = 1;
10055 SET_FRAME_GARBAGED (f);
10057 else
10058 expose_frame (x_window_to_frame (dpyinfo,
10059 event.xexpose.window),
10060 event.xexpose.x, event.xexpose.y,
10061 event.xexpose.width, event.xexpose.height);
10063 else
10065 #ifndef USE_TOOLKIT_SCROLL_BARS
10066 struct scroll_bar *bar;
10067 #endif
10068 #if defined USE_X_TOOLKIT && defined USE_LUCID
10069 /* Submenus of the Lucid menu bar aren't widgets
10070 themselves, so there's no way to dispatch events
10071 to them. Recognize this case separately. */
10073 Widget widget
10074 = x_window_to_menu_bar (event.xexpose.window);
10075 if (widget)
10076 xlwmenu_redisplay (widget);
10078 #endif /* USE_X_TOOLKIT && USE_LUCID */
10080 #ifdef USE_TOOLKIT_SCROLL_BARS
10081 /* Dispatch event to the widget. */
10082 goto OTHER;
10083 #else /* not USE_TOOLKIT_SCROLL_BARS */
10084 bar = x_window_to_scroll_bar (event.xexpose.window);
10086 if (bar)
10087 x_scroll_bar_expose (bar, &event);
10088 #ifdef USE_X_TOOLKIT
10089 else
10090 goto OTHER;
10091 #endif /* USE_X_TOOLKIT */
10092 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10094 break;
10096 case GraphicsExpose: /* This occurs when an XCopyArea's
10097 source area was obscured or not
10098 available.*/
10099 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
10100 if (f)
10102 expose_frame (f,
10103 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
10104 event.xgraphicsexpose.width,
10105 event.xgraphicsexpose.height);
10107 #ifdef USE_X_TOOLKIT
10108 else
10109 goto OTHER;
10110 #endif /* USE_X_TOOLKIT */
10111 break;
10113 case NoExpose: /* This occurs when an XCopyArea's
10114 source area was completely
10115 available */
10116 break;
10118 case UnmapNotify:
10119 /* Redo the mouse-highlight after the tooltip has gone. */
10120 if (event.xmap.window == tip_window)
10122 tip_window = 0;
10123 redo_mouse_highlight ();
10126 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
10127 if (f) /* F may no longer exist if
10128 the frame was deleted. */
10130 /* While a frame is unmapped, display generation is
10131 disabled; you don't want to spend time updating a
10132 display that won't ever be seen. */
10133 f->async_visible = 0;
10134 /* We can't distinguish, from the event, whether the window
10135 has become iconified or invisible. So assume, if it
10136 was previously visible, than now it is iconified.
10137 But x_make_frame_invisible clears both
10138 the visible flag and the iconified flag;
10139 and that way, we know the window is not iconified now. */
10140 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
10142 f->async_iconified = 1;
10144 bufp->kind = iconify_event;
10145 XSETFRAME (bufp->frame_or_window, f);
10146 bufp->arg = Qnil;
10147 bufp++;
10148 count++;
10149 numchars--;
10152 goto OTHER;
10154 case MapNotify:
10155 if (event.xmap.window == tip_window)
10156 /* The tooltip has been drawn already. Avoid
10157 the SET_FRAME_GARBAGED below. */
10158 goto OTHER;
10160 /* We use x_top_window_to_frame because map events can
10161 come for sub-windows and they don't mean that the
10162 frame is visible. */
10163 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
10164 if (f)
10166 f->async_visible = 1;
10167 f->async_iconified = 0;
10168 f->output_data.x->has_been_visible = 1;
10170 /* wait_reading_process_input will notice this and update
10171 the frame's display structures. */
10172 SET_FRAME_GARBAGED (f);
10174 if (f->iconified)
10176 bufp->kind = deiconify_event;
10177 XSETFRAME (bufp->frame_or_window, f);
10178 bufp->arg = Qnil;
10179 bufp++;
10180 count++;
10181 numchars--;
10183 else if (! NILP (Vframe_list)
10184 && ! NILP (XCDR (Vframe_list)))
10185 /* Force a redisplay sooner or later
10186 to update the frame titles
10187 in case this is the second frame. */
10188 record_asynch_buffer_change ();
10190 goto OTHER;
10192 case KeyPress:
10193 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
10195 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
10196 if (f == 0)
10198 /* Scroll bars consume key events, but we want
10199 the keys to go to the scroll bar's frame. */
10200 Widget widget = XtWindowToWidget (dpyinfo->display,
10201 event.xkey.window);
10202 if (widget && XmIsScrollBar (widget))
10204 widget = XtParent (widget);
10205 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
10208 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
10210 if (f != 0)
10212 KeySym keysym, orig_keysym;
10213 /* al%imercury@uunet.uu.net says that making this 81
10214 instead of 80 fixed a bug whereby meta chars made
10215 his Emacs hang.
10217 It seems that some version of XmbLookupString has
10218 a bug of not returning XBufferOverflow in
10219 status_return even if the input is too long to
10220 fit in 81 bytes. So, we must prepare sufficient
10221 bytes for copy_buffer. 513 bytes (256 chars for
10222 two-byte character set) seems to be a faily good
10223 approximation. -- 2000.8.10 handa@etl.go.jp */
10224 unsigned char copy_buffer[513];
10225 unsigned char *copy_bufptr = copy_buffer;
10226 int copy_bufsiz = sizeof (copy_buffer);
10227 int modifiers;
10229 event.xkey.state
10230 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
10231 extra_keyboard_modifiers);
10232 modifiers = event.xkey.state;
10234 /* This will have to go some day... */
10236 /* make_lispy_event turns chars into control chars.
10237 Don't do it here because XLookupString is too eager. */
10238 event.xkey.state &= ~ControlMask;
10239 event.xkey.state &= ~(dpyinfo->meta_mod_mask
10240 | dpyinfo->super_mod_mask
10241 | dpyinfo->hyper_mod_mask
10242 | dpyinfo->alt_mod_mask);
10244 /* In case Meta is ComposeCharacter,
10245 clear its status. According to Markus Ehrnsperger
10246 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
10247 this enables ComposeCharacter to work whether or
10248 not it is combined with Meta. */
10249 if (modifiers & dpyinfo->meta_mod_mask)
10250 bzero (&compose_status, sizeof (compose_status));
10252 #ifdef HAVE_X_I18N
10253 if (FRAME_XIC (f))
10255 Status status_return;
10257 nbytes = XmbLookupString (FRAME_XIC (f),
10258 &event.xkey, copy_bufptr,
10259 copy_bufsiz, &keysym,
10260 &status_return);
10261 if (status_return == XBufferOverflow)
10263 copy_bufsiz = nbytes + 1;
10264 copy_bufptr = (char *) alloca (copy_bufsiz);
10265 nbytes = XmbLookupString (FRAME_XIC (f),
10266 &event.xkey, copy_bufptr,
10267 copy_bufsiz, &keysym,
10268 &status_return);
10271 if (status_return == XLookupNone)
10272 break;
10273 else if (status_return == XLookupChars)
10275 keysym = NoSymbol;
10276 modifiers = 0;
10278 else if (status_return != XLookupKeySym
10279 && status_return != XLookupBoth)
10280 abort ();
10282 else
10283 nbytes = XLookupString (&event.xkey, copy_bufptr,
10284 copy_bufsiz, &keysym,
10285 &compose_status);
10286 #else
10287 nbytes = XLookupString (&event.xkey, copy_bufptr,
10288 copy_bufsiz, &keysym,
10289 &compose_status);
10290 #endif
10292 orig_keysym = keysym;
10294 if (numchars > 1)
10296 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
10297 || keysym == XK_Delete
10298 #ifdef XK_ISO_Left_Tab
10299 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
10300 #endif
10301 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
10302 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
10303 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
10304 #ifdef HPUX
10305 /* This recognizes the "extended function keys".
10306 It seems there's no cleaner way.
10307 Test IsModifierKey to avoid handling mode_switch
10308 incorrectly. */
10309 || ((unsigned) (keysym) >= XK_Select
10310 && (unsigned)(keysym) < XK_KP_Space)
10311 #endif
10312 #ifdef XK_dead_circumflex
10313 || orig_keysym == XK_dead_circumflex
10314 #endif
10315 #ifdef XK_dead_grave
10316 || orig_keysym == XK_dead_grave
10317 #endif
10318 #ifdef XK_dead_tilde
10319 || orig_keysym == XK_dead_tilde
10320 #endif
10321 #ifdef XK_dead_diaeresis
10322 || orig_keysym == XK_dead_diaeresis
10323 #endif
10324 #ifdef XK_dead_macron
10325 || orig_keysym == XK_dead_macron
10326 #endif
10327 #ifdef XK_dead_degree
10328 || orig_keysym == XK_dead_degree
10329 #endif
10330 #ifdef XK_dead_acute
10331 || orig_keysym == XK_dead_acute
10332 #endif
10333 #ifdef XK_dead_cedilla
10334 || orig_keysym == XK_dead_cedilla
10335 #endif
10336 #ifdef XK_dead_breve
10337 || orig_keysym == XK_dead_breve
10338 #endif
10339 #ifdef XK_dead_ogonek
10340 || orig_keysym == XK_dead_ogonek
10341 #endif
10342 #ifdef XK_dead_caron
10343 || orig_keysym == XK_dead_caron
10344 #endif
10345 #ifdef XK_dead_doubleacute
10346 || orig_keysym == XK_dead_doubleacute
10347 #endif
10348 #ifdef XK_dead_abovedot
10349 || orig_keysym == XK_dead_abovedot
10350 #endif
10351 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10352 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10353 /* Any "vendor-specific" key is ok. */
10354 || (orig_keysym & (1 << 28)))
10355 && ! (IsModifierKey (orig_keysym)
10356 #ifndef HAVE_X11R5
10357 #ifdef XK_Mode_switch
10358 || ((unsigned)(orig_keysym) == XK_Mode_switch)
10359 #endif
10360 #ifdef XK_Num_Lock
10361 || ((unsigned)(orig_keysym) == XK_Num_Lock)
10362 #endif
10363 #endif /* not HAVE_X11R5 */
10366 if (temp_index == sizeof temp_buffer / sizeof (short))
10367 temp_index = 0;
10368 temp_buffer[temp_index++] = keysym;
10369 bufp->kind = non_ascii_keystroke;
10370 bufp->code = keysym;
10371 XSETFRAME (bufp->frame_or_window, f);
10372 bufp->arg = Qnil;
10373 bufp->modifiers
10374 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10375 modifiers);
10376 bufp->timestamp = event.xkey.time;
10377 bufp++;
10378 count++;
10379 numchars--;
10381 else if (numchars > nbytes)
10383 register int i;
10384 register int c;
10385 int nchars, len;
10387 for (i = 0; i < nbytes; i++)
10389 if (temp_index == (sizeof temp_buffer
10390 / sizeof (short)))
10391 temp_index = 0;
10392 temp_buffer[temp_index++] = copy_bufptr[i];
10395 if (/* If the event is not from XIM, */
10396 event.xkey.keycode != 0
10397 /* or the current locale doesn't request
10398 decoding of the intup data, ... */
10399 || coding.type == coding_type_raw_text
10400 || coding.type == coding_type_no_conversion)
10402 /* ... we can use the input data as is. */
10403 nchars = nbytes;
10405 else
10407 /* We have to decode the input data. */
10408 int require;
10409 unsigned char *p;
10411 require = decoding_buffer_size (&coding, nbytes);
10412 p = (unsigned char *) alloca (require);
10413 coding.mode |= CODING_MODE_LAST_BLOCK;
10414 decode_coding (&coding, copy_bufptr, p,
10415 nbytes, require);
10416 nbytes = coding.produced;
10417 nchars = coding.produced_char;
10418 copy_bufptr = p;
10421 /* Convert the input data to a sequence of
10422 character events. */
10423 for (i = 0; i < nbytes; i += len)
10425 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10426 nbytes - i, len);
10427 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
10428 ? ascii_keystroke
10429 : multibyte_char_keystroke);
10430 bufp->code = c;
10431 XSETFRAME (bufp->frame_or_window, f);
10432 bufp->arg = Qnil;
10433 bufp->modifiers
10434 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10435 modifiers);
10436 bufp->timestamp = event.xkey.time;
10437 bufp++;
10440 count += nchars;
10441 numchars -= nchars;
10443 if (keysym == NoSymbol)
10444 break;
10446 else
10447 abort ();
10449 else
10450 abort ();
10452 #ifdef HAVE_X_I18N
10453 /* Don't dispatch this event since XtDispatchEvent calls
10454 XFilterEvent, and two calls in a row may freeze the
10455 client. */
10456 break;
10457 #else
10458 goto OTHER;
10459 #endif
10461 case KeyRelease:
10462 #ifdef HAVE_X_I18N
10463 /* Don't dispatch this event since XtDispatchEvent calls
10464 XFilterEvent, and two calls in a row may freeze the
10465 client. */
10466 break;
10467 #else
10468 goto OTHER;
10469 #endif
10471 /* Here's a possible interpretation of the whole
10472 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
10473 you get a FocusIn event, you have to get a FocusOut
10474 event before you relinquish the focus. If you
10475 haven't received a FocusIn event, then a mere
10476 LeaveNotify is enough to free you. */
10478 case EnterNotify:
10480 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10482 if (event.xcrossing.focus)
10484 /* Avoid nasty pop/raise loops. */
10485 if (f && (!(f->auto_raise)
10486 || !(f->auto_lower)
10487 || (event.xcrossing.time - enter_timestamp) > 500))
10489 x_new_focus_frame (dpyinfo, f);
10490 enter_timestamp = event.xcrossing.time;
10493 else if (f == dpyinfo->x_focus_frame)
10494 x_new_focus_frame (dpyinfo, 0);
10496 /* EnterNotify counts as mouse movement,
10497 so update things that depend on mouse position. */
10498 if (f && !f->output_data.x->hourglass_p)
10499 note_mouse_movement (f, &event.xmotion);
10500 goto OTHER;
10503 case FocusIn:
10504 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10505 if (event.xfocus.detail != NotifyPointer)
10506 dpyinfo->x_focus_event_frame = f;
10507 if (f)
10509 x_new_focus_frame (dpyinfo, f);
10511 /* Don't stop displaying the initial startup message
10512 for a switch-frame event we don't need. */
10513 if (GC_NILP (Vterminal_frame)
10514 && GC_CONSP (Vframe_list)
10515 && !GC_NILP (XCDR (Vframe_list)))
10517 bufp->kind = FOCUS_IN_EVENT;
10518 XSETFRAME (bufp->frame_or_window, f);
10519 bufp->arg = Qnil;
10520 ++bufp, ++count, --numchars;
10524 #ifdef HAVE_X_I18N
10525 if (f && FRAME_XIC (f))
10526 XSetICFocus (FRAME_XIC (f));
10527 #endif
10529 goto OTHER;
10531 case LeaveNotify:
10532 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
10533 if (f)
10535 if (f == dpyinfo->mouse_face_mouse_frame)
10537 /* If we move outside the frame, then we're
10538 certainly no longer on any text in the frame. */
10539 clear_mouse_face (dpyinfo);
10540 dpyinfo->mouse_face_mouse_frame = 0;
10543 /* Generate a nil HELP_EVENT to cancel a help-echo.
10544 Do it only if there's something to cancel.
10545 Otherwise, the startup message is cleared when
10546 the mouse leaves the frame. */
10547 if (any_help_event_p)
10549 Lisp_Object frame;
10550 int n;
10552 XSETFRAME (frame, f);
10553 help_echo = Qnil;
10554 n = gen_help_event (bufp, numchars,
10555 Qnil, frame, Qnil, Qnil, 0);
10556 bufp += n, count += n, numchars -= n;
10559 if (event.xcrossing.focus)
10560 x_mouse_leave (dpyinfo);
10561 else
10563 if (f == dpyinfo->x_focus_event_frame)
10564 dpyinfo->x_focus_event_frame = 0;
10565 if (f == dpyinfo->x_focus_frame)
10566 x_new_focus_frame (dpyinfo, 0);
10569 goto OTHER;
10571 case FocusOut:
10572 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10573 if (event.xfocus.detail != NotifyPointer
10574 && f == dpyinfo->x_focus_event_frame)
10575 dpyinfo->x_focus_event_frame = 0;
10576 if (f && f == dpyinfo->x_focus_frame)
10577 x_new_focus_frame (dpyinfo, 0);
10579 #ifdef HAVE_X_I18N
10580 if (f && FRAME_XIC (f))
10581 XUnsetICFocus (FRAME_XIC (f));
10582 #endif
10584 goto OTHER;
10586 case MotionNotify:
10588 previous_help_echo = help_echo;
10589 help_echo = help_echo_object = help_echo_window = Qnil;
10590 help_echo_pos = -1;
10592 if (dpyinfo->grabbed && last_mouse_frame
10593 && FRAME_LIVE_P (last_mouse_frame))
10594 f = last_mouse_frame;
10595 else
10596 f = x_window_to_frame (dpyinfo, event.xmotion.window);
10598 if (f)
10599 note_mouse_movement (f, &event.xmotion);
10600 else
10602 #ifndef USE_TOOLKIT_SCROLL_BARS
10603 struct scroll_bar *bar
10604 = x_window_to_scroll_bar (event.xmotion.window);
10606 if (bar)
10607 x_scroll_bar_note_movement (bar, &event);
10608 #endif /* USE_TOOLKIT_SCROLL_BARS */
10610 /* If we move outside the frame, then we're
10611 certainly no longer on any text in the frame. */
10612 clear_mouse_face (dpyinfo);
10615 /* If the contents of the global variable help_echo
10616 has changed, generate a HELP_EVENT. */
10617 if (!NILP (help_echo)
10618 || !NILP (previous_help_echo))
10620 Lisp_Object frame;
10621 int n;
10623 if (f)
10624 XSETFRAME (frame, f);
10625 else
10626 frame = Qnil;
10628 any_help_event_p = 1;
10629 n = gen_help_event (bufp, numchars, help_echo, frame,
10630 help_echo_window, help_echo_object,
10631 help_echo_pos);
10632 bufp += n, count += n, numchars -= n;
10635 goto OTHER;
10638 case ConfigureNotify:
10639 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10640 if (f)
10642 #ifndef USE_X_TOOLKIT
10643 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10644 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
10646 /* In the toolkit version, change_frame_size
10647 is called by the code that handles resizing
10648 of the EmacsFrame widget. */
10650 /* Even if the number of character rows and columns has
10651 not changed, the font size may have changed, so we need
10652 to check the pixel dimensions as well. */
10653 if (columns != f->width
10654 || rows != f->height
10655 || event.xconfigure.width != f->output_data.x->pixel_width
10656 || event.xconfigure.height != f->output_data.x->pixel_height)
10658 change_frame_size (f, rows, columns, 0, 1, 0);
10659 SET_FRAME_GARBAGED (f);
10660 cancel_mouse_face (f);
10662 #endif
10664 f->output_data.x->pixel_width = event.xconfigure.width;
10665 f->output_data.x->pixel_height = event.xconfigure.height;
10667 /* What we have now is the position of Emacs's own window.
10668 Convert that to the position of the window manager window. */
10669 x_real_positions (f, &f->output_data.x->left_pos,
10670 &f->output_data.x->top_pos);
10672 #ifdef HAVE_X_I18N
10673 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10674 xic_set_statusarea (f);
10675 #endif
10677 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10679 /* Since the WM decorations come below top_pos now,
10680 we must put them below top_pos in the future. */
10681 f->output_data.x->win_gravity = NorthWestGravity;
10682 x_wm_set_size_hint (f, (long) 0, 0);
10684 #ifdef USE_MOTIF
10685 /* Some window managers pass (0,0) as the location of
10686 the window, and the Motif event handler stores it
10687 in the emacs widget, which messes up Motif menus. */
10688 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10690 event.xconfigure.x = f->output_data.x->widget->core.x;
10691 event.xconfigure.y = f->output_data.x->widget->core.y;
10693 #endif /* USE_MOTIF */
10695 goto OTHER;
10697 case ButtonPress:
10698 case ButtonRelease:
10700 /* If we decide we want to generate an event to be seen
10701 by the rest of Emacs, we put it here. */
10702 struct input_event emacs_event;
10703 int tool_bar_p = 0;
10705 emacs_event.kind = no_event;
10706 bzero (&compose_status, sizeof (compose_status));
10708 if (dpyinfo->grabbed
10709 && last_mouse_frame
10710 && FRAME_LIVE_P (last_mouse_frame))
10711 f = last_mouse_frame;
10712 else
10713 f = x_window_to_frame (dpyinfo, event.xbutton.window);
10715 if (f)
10717 /* Is this in the tool-bar? */
10718 if (WINDOWP (f->tool_bar_window)
10719 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
10721 Lisp_Object window;
10722 int p, x, y;
10724 x = event.xbutton.x;
10725 y = event.xbutton.y;
10727 /* Set x and y. */
10728 window = window_from_coordinates (f, x, y, &p, 1);
10729 if (EQ (window, f->tool_bar_window))
10731 x_handle_tool_bar_click (f, &event.xbutton);
10732 tool_bar_p = 1;
10736 if (!tool_bar_p)
10737 if (!dpyinfo->x_focus_frame
10738 || f == dpyinfo->x_focus_frame)
10739 construct_mouse_click (&emacs_event, &event, f);
10741 else
10743 #ifndef USE_TOOLKIT_SCROLL_BARS
10744 struct scroll_bar *bar
10745 = x_window_to_scroll_bar (event.xbutton.window);
10747 if (bar)
10748 x_scroll_bar_handle_click (bar, &event, &emacs_event);
10749 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10752 if (event.type == ButtonPress)
10754 dpyinfo->grabbed |= (1 << event.xbutton.button);
10755 last_mouse_frame = f;
10756 /* Ignore any mouse motion that happened
10757 before this event; any subsequent mouse-movement
10758 Emacs events should reflect only motion after
10759 the ButtonPress. */
10760 if (f != 0)
10761 f->mouse_moved = 0;
10763 if (!tool_bar_p)
10764 last_tool_bar_item = -1;
10766 else
10768 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
10771 if (numchars >= 1 && emacs_event.kind != no_event)
10773 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10774 bufp++;
10775 count++;
10776 numchars--;
10779 #ifdef USE_X_TOOLKIT
10780 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10781 /* For a down-event in the menu bar,
10782 don't pass it to Xt right now.
10783 Instead, save it away
10784 and we will pass it to Xt from kbd_buffer_get_event.
10785 That way, we can run some Lisp code first. */
10786 if (f && event.type == ButtonPress
10787 /* Verify the event is really within the menu bar
10788 and not just sent to it due to grabbing. */
10789 && event.xbutton.x >= 0
10790 && event.xbutton.x < f->output_data.x->pixel_width
10791 && event.xbutton.y >= 0
10792 && event.xbutton.y < f->output_data.x->menubar_height
10793 && event.xbutton.same_screen)
10795 SET_SAVED_BUTTON_EVENT;
10796 XSETFRAME (last_mouse_press_frame, f);
10798 else if (event.type == ButtonPress)
10800 last_mouse_press_frame = Qnil;
10801 goto OTHER;
10804 #ifdef USE_MOTIF /* This should do not harm for Lucid,
10805 but I am trying to be cautious. */
10806 else if (event.type == ButtonRelease)
10808 if (!NILP (last_mouse_press_frame))
10810 f = XFRAME (last_mouse_press_frame);
10811 if (f->output_data.x)
10812 SET_SAVED_BUTTON_EVENT;
10814 else
10815 goto OTHER;
10817 #endif /* USE_MOTIF */
10818 else
10819 goto OTHER;
10820 #endif /* USE_X_TOOLKIT */
10822 break;
10824 case CirculateNotify:
10825 goto OTHER;
10827 case CirculateRequest:
10828 goto OTHER;
10830 case VisibilityNotify:
10831 goto OTHER;
10833 case MappingNotify:
10834 /* Someone has changed the keyboard mapping - update the
10835 local cache. */
10836 switch (event.xmapping.request)
10838 case MappingModifier:
10839 x_find_modifier_meanings (dpyinfo);
10840 /* This is meant to fall through. */
10841 case MappingKeyboard:
10842 XRefreshKeyboardMapping (&event.xmapping);
10844 goto OTHER;
10846 default:
10847 OTHER:
10848 #ifdef USE_X_TOOLKIT
10849 BLOCK_INPUT;
10850 XtDispatchEvent (&event);
10851 UNBLOCK_INPUT;
10852 #endif /* USE_X_TOOLKIT */
10853 break;
10858 out:;
10860 /* On some systems, an X bug causes Emacs to get no more events
10861 when the window is destroyed. Detect that. (1994.) */
10862 if (! event_found)
10864 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10865 One XNOOP in 100 loops will make Emacs terminate.
10866 B. Bretthauer, 1994 */
10867 x_noop_count++;
10868 if (x_noop_count >= 100)
10870 x_noop_count=0;
10872 if (next_noop_dpyinfo == 0)
10873 next_noop_dpyinfo = x_display_list;
10875 XNoOp (next_noop_dpyinfo->display);
10877 /* Each time we get here, cycle through the displays now open. */
10878 next_noop_dpyinfo = next_noop_dpyinfo->next;
10882 /* If the focus was just given to an auto-raising frame,
10883 raise it now. */
10884 /* ??? This ought to be able to handle more than one such frame. */
10885 if (pending_autoraise_frame)
10887 x_raise_frame (pending_autoraise_frame);
10888 pending_autoraise_frame = 0;
10891 UNBLOCK_INPUT;
10892 --handling_signal;
10893 return count;
10899 /***********************************************************************
10900 Text Cursor
10901 ***********************************************************************/
10903 /* Note if the text cursor of window W has been overwritten by a
10904 drawing operation that outputs N glyphs starting at HPOS in the
10905 line given by output_cursor.vpos. N < 0 means all the rest of the
10906 line after HPOS has been written. */
10908 static void
10909 note_overwritten_text_cursor (w, hpos, n)
10910 struct window *w;
10911 int hpos, n;
10913 if (updated_area == TEXT_AREA
10914 && output_cursor.vpos == w->phys_cursor.vpos
10915 && hpos <= w->phys_cursor.hpos
10916 && (n < 0
10917 || hpos + n > w->phys_cursor.hpos))
10918 w->phys_cursor_on_p = 0;
10922 /* Set clipping for output in glyph row ROW. W is the window in which
10923 we operate. GC is the graphics context to set clipping in.
10924 WHOLE_LINE_P non-zero means include the areas used for truncation
10925 mark display and alike in the clipping rectangle.
10927 ROW may be a text row or, e.g., a mode line. Text rows must be
10928 clipped to the interior of the window dedicated to text display,
10929 mode lines must be clipped to the whole window. */
10931 static void
10932 x_clip_to_row (w, row, gc, whole_line_p)
10933 struct window *w;
10934 struct glyph_row *row;
10935 GC gc;
10936 int whole_line_p;
10938 struct frame *f = XFRAME (WINDOW_FRAME (w));
10939 XRectangle clip_rect;
10940 int window_x, window_y, window_width, window_height;
10942 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
10944 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10945 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10946 clip_rect.y = max (clip_rect.y, window_y);
10947 clip_rect.width = window_width;
10948 clip_rect.height = row->visible_height;
10950 /* If clipping to the whole line, including trunc marks, extend
10951 the rectangle to the left and increase its width. */
10952 if (whole_line_p)
10954 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10955 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
10958 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
10962 /* Draw a hollow box cursor on window W in glyph row ROW. */
10964 static void
10965 x_draw_hollow_cursor (w, row)
10966 struct window *w;
10967 struct glyph_row *row;
10969 struct frame *f = XFRAME (WINDOW_FRAME (w));
10970 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10971 Display *dpy = FRAME_X_DISPLAY (f);
10972 int x, y, wd, h;
10973 XGCValues xgcv;
10974 struct glyph *cursor_glyph;
10975 GC gc;
10977 /* Compute frame-relative coordinates from window-relative
10978 coordinates. */
10979 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10980 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10981 + row->ascent - w->phys_cursor_ascent);
10982 h = row->height - 1;
10984 /* Get the glyph the cursor is on. If we can't tell because
10985 the current matrix is invalid or such, give up. */
10986 cursor_glyph = get_phys_cursor_glyph (w);
10987 if (cursor_glyph == NULL)
10988 return;
10990 /* Compute the width of the rectangle to draw. If on a stretch
10991 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10992 rectangle as wide as the glyph, but use a canonical character
10993 width instead. */
10994 wd = cursor_glyph->pixel_width - 1;
10995 if (cursor_glyph->type == STRETCH_GLYPH
10996 && !x_stretch_cursor_p)
10997 wd = min (CANON_X_UNIT (f), wd);
10999 /* The foreground of cursor_gc is typically the same as the normal
11000 background color, which can cause the cursor box to be invisible. */
11001 xgcv.foreground = f->output_data.x->cursor_pixel;
11002 if (dpyinfo->scratch_cursor_gc)
11003 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
11004 else
11005 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
11006 GCForeground, &xgcv);
11007 gc = dpyinfo->scratch_cursor_gc;
11009 /* Set clipping, draw the rectangle, and reset clipping again. */
11010 x_clip_to_row (w, row, gc, 0);
11011 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
11012 XSetClipMask (dpy, gc, None);
11016 /* Draw a bar cursor on window W in glyph row ROW.
11018 Implementation note: One would like to draw a bar cursor with an
11019 angle equal to the one given by the font property XA_ITALIC_ANGLE.
11020 Unfortunately, I didn't find a font yet that has this property set.
11021 --gerd. */
11023 static void
11024 x_draw_bar_cursor (w, row, width)
11025 struct window *w;
11026 struct glyph_row *row;
11027 int width;
11029 struct frame *f = XFRAME (w->frame);
11030 struct glyph *cursor_glyph;
11031 GC gc;
11032 int x;
11033 unsigned long mask;
11034 XGCValues xgcv;
11035 Display *dpy;
11036 Window window;
11038 /* If cursor is out of bounds, don't draw garbage. This can happen
11039 in mini-buffer windows when switching between echo area glyphs
11040 and mini-buffer. */
11041 cursor_glyph = get_phys_cursor_glyph (w);
11042 if (cursor_glyph == NULL)
11043 return;
11045 /* If on an image, draw like a normal cursor. That's usually better
11046 visible than drawing a bar, esp. if the image is large so that
11047 the bar might not be in the window. */
11048 if (cursor_glyph->type == IMAGE_GLYPH)
11050 struct glyph_row *row;
11051 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
11052 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
11054 else
11056 xgcv.background = f->output_data.x->cursor_pixel;
11057 xgcv.foreground = f->output_data.x->cursor_pixel;
11058 xgcv.graphics_exposures = 0;
11059 mask = GCForeground | GCBackground | GCGraphicsExposures;
11060 dpy = FRAME_X_DISPLAY (f);
11061 window = FRAME_X_WINDOW (f);
11062 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
11064 if (gc)
11065 XChangeGC (dpy, gc, mask, &xgcv);
11066 else
11068 gc = XCreateGC (dpy, window, mask, &xgcv);
11069 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
11072 if (width < 0)
11073 width = f->output_data.x->cursor_width;
11075 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11076 x_clip_to_row (w, row, gc, 0);
11077 XFillRectangle (dpy, window, gc,
11079 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
11080 min (cursor_glyph->pixel_width, width),
11081 row->height);
11082 XSetClipMask (dpy, gc, None);
11087 /* Clear the cursor of window W to background color, and mark the
11088 cursor as not shown. This is used when the text where the cursor
11089 is is about to be rewritten. */
11091 static void
11092 x_clear_cursor (w)
11093 struct window *w;
11095 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
11096 x_update_window_cursor (w, 0);
11100 /* Draw the cursor glyph of window W in glyph row ROW. See the
11101 comment of x_draw_glyphs for the meaning of HL. */
11103 static void
11104 x_draw_phys_cursor_glyph (w, row, hl)
11105 struct window *w;
11106 struct glyph_row *row;
11107 enum draw_glyphs_face hl;
11109 /* If cursor hpos is out of bounds, don't draw garbage. This can
11110 happen in mini-buffer windows when switching between echo area
11111 glyphs and mini-buffer. */
11112 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
11114 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
11115 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
11116 hl, 0, 0, 0);
11118 /* When we erase the cursor, and ROW is overlapped by other
11119 rows, make sure that these overlapping parts of other rows
11120 are redrawn. */
11121 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
11123 if (row > w->current_matrix->rows
11124 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
11125 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
11127 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
11128 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
11129 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
11135 /* Erase the image of a cursor of window W from the screen. */
11137 static void
11138 x_erase_phys_cursor (w)
11139 struct window *w;
11141 struct frame *f = XFRAME (w->frame);
11142 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11143 int hpos = w->phys_cursor.hpos;
11144 int vpos = w->phys_cursor.vpos;
11145 int mouse_face_here_p = 0;
11146 struct glyph_matrix *active_glyphs = w->current_matrix;
11147 struct glyph_row *cursor_row;
11148 struct glyph *cursor_glyph;
11149 enum draw_glyphs_face hl;
11151 /* No cursor displayed or row invalidated => nothing to do on the
11152 screen. */
11153 if (w->phys_cursor_type == NO_CURSOR)
11154 goto mark_cursor_off;
11156 /* VPOS >= active_glyphs->nrows means that window has been resized.
11157 Don't bother to erase the cursor. */
11158 if (vpos >= active_glyphs->nrows)
11159 goto mark_cursor_off;
11161 /* If row containing cursor is marked invalid, there is nothing we
11162 can do. */
11163 cursor_row = MATRIX_ROW (active_glyphs, vpos);
11164 if (!cursor_row->enabled_p)
11165 goto mark_cursor_off;
11167 /* This can happen when the new row is shorter than the old one.
11168 In this case, either x_draw_glyphs or clear_end_of_line
11169 should have cleared the cursor. Note that we wouldn't be
11170 able to erase the cursor in this case because we don't have a
11171 cursor glyph at hand. */
11172 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
11173 goto mark_cursor_off;
11175 /* If the cursor is in the mouse face area, redisplay that when
11176 we clear the cursor. */
11177 if (! NILP (dpyinfo->mouse_face_window)
11178 && w == XWINDOW (dpyinfo->mouse_face_window)
11179 && (vpos > dpyinfo->mouse_face_beg_row
11180 || (vpos == dpyinfo->mouse_face_beg_row
11181 && hpos >= dpyinfo->mouse_face_beg_col))
11182 && (vpos < dpyinfo->mouse_face_end_row
11183 || (vpos == dpyinfo->mouse_face_end_row
11184 && hpos < dpyinfo->mouse_face_end_col))
11185 /* Don't redraw the cursor's spot in mouse face if it is at the
11186 end of a line (on a newline). The cursor appears there, but
11187 mouse highlighting does not. */
11188 && cursor_row->used[TEXT_AREA] > hpos)
11189 mouse_face_here_p = 1;
11191 /* Maybe clear the display under the cursor. */
11192 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
11194 int x;
11195 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11197 cursor_glyph = get_phys_cursor_glyph (w);
11198 if (cursor_glyph == NULL)
11199 goto mark_cursor_off;
11201 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11203 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11205 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
11206 cursor_row->y)),
11207 cursor_glyph->pixel_width,
11208 cursor_row->visible_height,
11209 False);
11212 /* Erase the cursor by redrawing the character underneath it. */
11213 if (mouse_face_here_p)
11214 hl = DRAW_MOUSE_FACE;
11215 else if (cursor_row->inverse_p)
11216 hl = DRAW_INVERSE_VIDEO;
11217 else
11218 hl = DRAW_NORMAL_TEXT;
11219 x_draw_phys_cursor_glyph (w, cursor_row, hl);
11221 mark_cursor_off:
11222 w->phys_cursor_on_p = 0;
11223 w->phys_cursor_type = NO_CURSOR;
11227 /* Non-zero if physical cursor of window W is within mouse face. */
11229 static int
11230 cursor_in_mouse_face_p (w)
11231 struct window *w;
11233 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
11234 int in_mouse_face = 0;
11236 if (WINDOWP (dpyinfo->mouse_face_window)
11237 && XWINDOW (dpyinfo->mouse_face_window) == w)
11239 int hpos = w->phys_cursor.hpos;
11240 int vpos = w->phys_cursor.vpos;
11242 if (vpos >= dpyinfo->mouse_face_beg_row
11243 && vpos <= dpyinfo->mouse_face_end_row
11244 && (vpos > dpyinfo->mouse_face_beg_row
11245 || hpos >= dpyinfo->mouse_face_beg_col)
11246 && (vpos < dpyinfo->mouse_face_end_row
11247 || hpos < dpyinfo->mouse_face_end_col
11248 || dpyinfo->mouse_face_past_end))
11249 in_mouse_face = 1;
11252 return in_mouse_face;
11256 /* Display or clear cursor of window W. If ON is zero, clear the
11257 cursor. If it is non-zero, display the cursor. If ON is nonzero,
11258 where to put the cursor is specified by HPOS, VPOS, X and Y. */
11260 void
11261 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
11262 struct window *w;
11263 int on, hpos, vpos, x, y;
11265 struct frame *f = XFRAME (w->frame);
11266 int new_cursor_type;
11267 int new_cursor_width;
11268 struct glyph_matrix *current_glyphs;
11269 struct glyph_row *glyph_row;
11270 struct glyph *glyph;
11272 /* This is pointless on invisible frames, and dangerous on garbaged
11273 windows and frames; in the latter case, the frame or window may
11274 be in the midst of changing its size, and x and y may be off the
11275 window. */
11276 if (! FRAME_VISIBLE_P (f)
11277 || FRAME_GARBAGED_P (f)
11278 || vpos >= w->current_matrix->nrows
11279 || hpos >= w->current_matrix->matrix_w)
11280 return;
11282 /* If cursor is off and we want it off, return quickly. */
11283 if (!on && !w->phys_cursor_on_p)
11284 return;
11286 current_glyphs = w->current_matrix;
11287 glyph_row = MATRIX_ROW (current_glyphs, vpos);
11288 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
11290 /* If cursor row is not enabled, we don't really know where to
11291 display the cursor. */
11292 if (!glyph_row->enabled_p)
11294 w->phys_cursor_on_p = 0;
11295 return;
11298 xassert (interrupt_input_blocked);
11300 /* Set new_cursor_type to the cursor we want to be displayed. In a
11301 mini-buffer window, we want the cursor only to appear if we are
11302 reading input from this window. For the selected window, we want
11303 the cursor type given by the frame parameter. If explicitly
11304 marked off, draw no cursor. In all other cases, we want a hollow
11305 box cursor. */
11306 new_cursor_width = -1;
11307 if (cursor_in_echo_area
11308 && FRAME_HAS_MINIBUF_P (f)
11309 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
11311 if (w == XWINDOW (echo_area_window))
11312 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11313 else
11314 new_cursor_type = HOLLOW_BOX_CURSOR;
11316 else
11318 if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
11319 || w != XWINDOW (f->selected_window))
11321 extern int cursor_in_non_selected_windows;
11323 if (MINI_WINDOW_P (w)
11324 || !cursor_in_non_selected_windows
11325 || NILP (XBUFFER (w->buffer)->cursor_type))
11326 new_cursor_type = NO_CURSOR;
11327 else
11328 new_cursor_type = HOLLOW_BOX_CURSOR;
11330 else if (w->cursor_off_p)
11331 new_cursor_type = NO_CURSOR;
11332 else
11334 struct buffer *b = XBUFFER (w->buffer);
11336 if (EQ (b->cursor_type, Qt))
11337 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11338 else
11339 new_cursor_type = x_specified_cursor_type (b->cursor_type,
11340 &new_cursor_width);
11344 /* If cursor is currently being shown and we don't want it to be or
11345 it is in the wrong place, or the cursor type is not what we want,
11346 erase it. */
11347 if (w->phys_cursor_on_p
11348 && (!on
11349 || w->phys_cursor.x != x
11350 || w->phys_cursor.y != y
11351 || new_cursor_type != w->phys_cursor_type))
11352 x_erase_phys_cursor (w);
11354 /* If the cursor is now invisible and we want it to be visible,
11355 display it. */
11356 if (on && !w->phys_cursor_on_p)
11358 w->phys_cursor_ascent = glyph_row->ascent;
11359 w->phys_cursor_height = glyph_row->height;
11361 /* Set phys_cursor_.* before x_draw_.* is called because some
11362 of them may need the information. */
11363 w->phys_cursor.x = x;
11364 w->phys_cursor.y = glyph_row->y;
11365 w->phys_cursor.hpos = hpos;
11366 w->phys_cursor.vpos = vpos;
11367 w->phys_cursor_type = new_cursor_type;
11368 w->phys_cursor_on_p = 1;
11370 switch (new_cursor_type)
11372 case HOLLOW_BOX_CURSOR:
11373 x_draw_hollow_cursor (w, glyph_row);
11374 break;
11376 case FILLED_BOX_CURSOR:
11377 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
11378 break;
11380 case BAR_CURSOR:
11381 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
11382 break;
11384 case NO_CURSOR:
11385 break;
11387 default:
11388 abort ();
11391 #ifdef HAVE_X_I18N
11392 if (w == XWINDOW (f->selected_window))
11393 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
11394 xic_set_preeditarea (w, x, y);
11395 #endif
11398 #ifndef XFlush
11399 if (updating_frame != f)
11400 XFlush (FRAME_X_DISPLAY (f));
11401 #endif
11405 /* Display the cursor on window W, or clear it. X and Y are window
11406 relative pixel coordinates. HPOS and VPOS are glyph matrix
11407 positions. If W is not the selected window, display a hollow
11408 cursor. ON non-zero means display the cursor at X, Y which
11409 correspond to HPOS, VPOS, otherwise it is cleared. */
11411 void
11412 x_display_cursor (w, on, hpos, vpos, x, y)
11413 struct window *w;
11414 int on, hpos, vpos, x, y;
11416 BLOCK_INPUT;
11417 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
11418 UNBLOCK_INPUT;
11422 /* Display the cursor on window W, or clear it, according to ON_P.
11423 Don't change the cursor's position. */
11425 void
11426 x_update_cursor (f, on_p)
11427 struct frame *f;
11429 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11433 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
11434 in the window tree rooted at W. */
11436 static void
11437 x_update_cursor_in_window_tree (w, on_p)
11438 struct window *w;
11439 int on_p;
11441 while (w)
11443 if (!NILP (w->hchild))
11444 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11445 else if (!NILP (w->vchild))
11446 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11447 else
11448 x_update_window_cursor (w, on_p);
11450 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11455 /* Switch the display of W's cursor on or off, according to the value
11456 of ON. */
11458 static void
11459 x_update_window_cursor (w, on)
11460 struct window *w;
11461 int on;
11463 /* Don't update cursor in windows whose frame is in the process
11464 of being deleted. */
11465 if (w->current_matrix)
11467 BLOCK_INPUT;
11468 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11469 w->phys_cursor.x, w->phys_cursor.y);
11470 UNBLOCK_INPUT;
11477 /* Icons. */
11479 /* Refresh bitmap kitchen sink icon for frame F
11480 when we get an expose event for it. */
11482 void
11483 refreshicon (f)
11484 struct frame *f;
11486 /* Normally, the window manager handles this function. */
11489 /* Make the x-window of frame F use the gnu icon bitmap. */
11492 x_bitmap_icon (f, file)
11493 struct frame *f;
11494 Lisp_Object file;
11496 int bitmap_id;
11498 if (FRAME_X_WINDOW (f) == 0)
11499 return 1;
11501 /* Free up our existing icon bitmap if any. */
11502 if (f->output_data.x->icon_bitmap > 0)
11503 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11504 f->output_data.x->icon_bitmap = 0;
11506 if (STRINGP (file))
11507 bitmap_id = x_create_bitmap_from_file (f, file);
11508 else
11510 /* Create the GNU bitmap if necessary. */
11511 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
11512 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11513 = x_create_bitmap_from_data (f, gnu_bits,
11514 gnu_width, gnu_height);
11516 /* The first time we create the GNU bitmap,
11517 this increments the ref-count one extra time.
11518 As a result, the GNU bitmap is never freed.
11519 That way, we don't have to worry about allocating it again. */
11520 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
11522 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
11525 x_wm_set_icon_pixmap (f, bitmap_id);
11526 f->output_data.x->icon_bitmap = bitmap_id;
11528 return 0;
11532 /* Make the x-window of frame F use a rectangle with text.
11533 Use ICON_NAME as the text. */
11536 x_text_icon (f, icon_name)
11537 struct frame *f;
11538 char *icon_name;
11540 if (FRAME_X_WINDOW (f) == 0)
11541 return 1;
11543 #ifdef HAVE_X11R4
11545 XTextProperty text;
11546 text.value = (unsigned char *) icon_name;
11547 text.encoding = XA_STRING;
11548 text.format = 8;
11549 text.nitems = strlen (icon_name);
11550 #ifdef USE_X_TOOLKIT
11551 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11552 &text);
11553 #else /* not USE_X_TOOLKIT */
11554 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11555 #endif /* not USE_X_TOOLKIT */
11557 #else /* not HAVE_X11R4 */
11558 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11559 #endif /* not HAVE_X11R4 */
11561 if (f->output_data.x->icon_bitmap > 0)
11562 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11563 f->output_data.x->icon_bitmap = 0;
11564 x_wm_set_icon_pixmap (f, 0);
11566 return 0;
11569 #define X_ERROR_MESSAGE_SIZE 200
11571 /* If non-nil, this should be a string.
11572 It means catch X errors and store the error message in this string. */
11574 static Lisp_Object x_error_message_string;
11576 /* An X error handler which stores the error message in
11577 x_error_message_string. This is called from x_error_handler if
11578 x_catch_errors is in effect. */
11580 static void
11581 x_error_catcher (display, error)
11582 Display *display;
11583 XErrorEvent *error;
11585 XGetErrorText (display, error->error_code,
11586 XSTRING (x_error_message_string)->data,
11587 X_ERROR_MESSAGE_SIZE);
11590 /* Begin trapping X errors for display DPY. Actually we trap X errors
11591 for all displays, but DPY should be the display you are actually
11592 operating on.
11594 After calling this function, X protocol errors no longer cause
11595 Emacs to exit; instead, they are recorded in the string
11596 stored in x_error_message_string.
11598 Calling x_check_errors signals an Emacs error if an X error has
11599 occurred since the last call to x_catch_errors or x_check_errors.
11601 Calling x_uncatch_errors resumes the normal error handling. */
11603 void x_check_errors ();
11604 static Lisp_Object x_catch_errors_unwind ();
11607 x_catch_errors (dpy)
11608 Display *dpy;
11610 int count = specpdl_ptr - specpdl;
11612 /* Make sure any errors from previous requests have been dealt with. */
11613 XSync (dpy, False);
11615 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11617 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11618 XSTRING (x_error_message_string)->data[0] = 0;
11620 return count;
11623 /* Unbind the binding that we made to check for X errors. */
11625 static Lisp_Object
11626 x_catch_errors_unwind (old_val)
11627 Lisp_Object old_val;
11629 x_error_message_string = old_val;
11630 return Qnil;
11633 /* If any X protocol errors have arrived since the last call to
11634 x_catch_errors or x_check_errors, signal an Emacs error using
11635 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11637 void
11638 x_check_errors (dpy, format)
11639 Display *dpy;
11640 char *format;
11642 /* Make sure to catch any errors incurred so far. */
11643 XSync (dpy, False);
11645 if (XSTRING (x_error_message_string)->data[0])
11646 error (format, XSTRING (x_error_message_string)->data);
11649 /* Nonzero if we had any X protocol errors
11650 since we did x_catch_errors on DPY. */
11653 x_had_errors_p (dpy)
11654 Display *dpy;
11656 /* Make sure to catch any errors incurred so far. */
11657 XSync (dpy, False);
11659 return XSTRING (x_error_message_string)->data[0] != 0;
11662 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11664 void
11665 x_clear_errors (dpy)
11666 Display *dpy;
11668 XSTRING (x_error_message_string)->data[0] = 0;
11671 /* Stop catching X protocol errors and let them make Emacs die.
11672 DPY should be the display that was passed to x_catch_errors.
11673 COUNT should be the value that was returned by
11674 the corresponding call to x_catch_errors. */
11676 void
11677 x_uncatch_errors (dpy, count)
11678 Display *dpy;
11679 int count;
11681 unbind_to (count, Qnil);
11684 #if 0
11685 static unsigned int x_wire_count;
11686 x_trace_wire ()
11688 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11690 #endif /* ! 0 */
11693 /* Handle SIGPIPE, which can happen when the connection to a server
11694 simply goes away. SIGPIPE is handled by x_connection_signal.
11695 Don't need to do anything, because the write which caused the
11696 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
11697 which will do the appropriate cleanup for us. */
11699 static SIGTYPE
11700 x_connection_signal (signalnum) /* If we don't have an argument, */
11701 int signalnum; /* some compilers complain in signal calls. */
11703 #ifdef USG
11704 /* USG systems forget handlers when they are used;
11705 must reestablish each time */
11706 signal (signalnum, x_connection_signal);
11707 #endif /* USG */
11711 /************************************************************************
11712 Handling X errors
11713 ************************************************************************/
11715 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
11716 the text of an error message that lead to the connection loss. */
11718 static SIGTYPE
11719 x_connection_closed (dpy, error_message)
11720 Display *dpy;
11721 char *error_message;
11723 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
11724 Lisp_Object frame, tail;
11725 int count;
11726 char *msg;
11728 msg = (char *) alloca (strlen (error_message) + 1);
11729 strcpy (msg, error_message);
11730 handling_signal = 0;
11732 /* Prevent being called recursively because of an error condition
11733 below. Otherwise, we might end up with printing ``can't find per
11734 display information'' in the recursive call instead of printing
11735 the original message here. */
11736 count = x_catch_errors (dpy);
11738 /* We have to close the display to inform Xt that it doesn't
11739 exist anymore. If we don't, Xt will continue to wait for
11740 events from the display. As a consequence, a sequence of
11742 M-x make-frame-on-display RET :1 RET
11743 ...kill the new frame, so that we get an IO error...
11744 M-x make-frame-on-display RET :1 RET
11746 will indefinitely wait in Xt for events for display `:1', opened
11747 in the first class to make-frame-on-display.
11749 Closing the display is reported to lead to a bus error on
11750 OpenWindows in certain situations. I suspect that is a bug
11751 in OpenWindows. I don't know how to cicumvent it here. */
11753 #ifdef USE_X_TOOLKIT
11754 /* If DPYINFO is null, this means we didn't open the display
11755 in the first place, so don't try to close it. */
11756 if (dpyinfo)
11757 XtCloseDisplay (dpy);
11758 #endif
11760 /* Indicate that this display is dead. */
11761 if (dpyinfo)
11762 dpyinfo->display = 0;
11764 /* First delete frames whose mini-buffers are on frames
11765 that are on the dead display. */
11766 FOR_EACH_FRAME (tail, frame)
11768 Lisp_Object minibuf_frame;
11769 minibuf_frame
11770 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
11771 if (FRAME_X_P (XFRAME (frame))
11772 && FRAME_X_P (XFRAME (minibuf_frame))
11773 && ! EQ (frame, minibuf_frame)
11774 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
11775 Fdelete_frame (frame, Qt);
11778 /* Now delete all remaining frames on the dead display.
11779 We are now sure none of these is used as the mini-buffer
11780 for another frame that we need to delete. */
11781 FOR_EACH_FRAME (tail, frame)
11782 if (FRAME_X_P (XFRAME (frame))
11783 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
11785 /* Set this to t so that Fdelete_frame won't get confused
11786 trying to find a replacement. */
11787 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11788 Fdelete_frame (frame, Qt);
11791 if (dpyinfo)
11792 x_delete_display (dpyinfo);
11794 x_uncatch_errors (dpy, count);
11796 if (x_display_list == 0)
11798 fprintf (stderr, "%s\n", msg);
11799 shut_down_emacs (0, 0, Qnil);
11800 exit (70);
11803 /* Ordinary stack unwind doesn't deal with these. */
11804 #ifdef SIGIO
11805 sigunblock (sigmask (SIGIO));
11806 #endif
11807 sigunblock (sigmask (SIGALRM));
11808 TOTALLY_UNBLOCK_INPUT;
11810 clear_waiting_for_input ();
11811 error ("%s", msg);
11815 /* This is the usual handler for X protocol errors.
11816 It kills all frames on the display that we got the error for.
11817 If that was the only one, it prints an error message and kills Emacs. */
11819 static void
11820 x_error_quitter (display, error)
11821 Display *display;
11822 XErrorEvent *error;
11824 char buf[256], buf1[356];
11826 /* Note that there is no real way portable across R3/R4 to get the
11827 original error handler. */
11829 XGetErrorText (display, error->error_code, buf, sizeof (buf));
11830 sprintf (buf1, "X protocol error: %s on protocol request %d",
11831 buf, error->request_code);
11832 x_connection_closed (display, buf1);
11836 /* This is the first-level handler for X protocol errors.
11837 It calls x_error_quitter or x_error_catcher. */
11839 static int
11840 x_error_handler (display, error)
11841 Display *display;
11842 XErrorEvent *error;
11844 if (! NILP (x_error_message_string))
11845 x_error_catcher (display, error);
11846 else
11847 x_error_quitter (display, error);
11848 return 0;
11851 /* This is the handler for X IO errors, always.
11852 It kills all frames on the display that we lost touch with.
11853 If that was the only one, it prints an error message and kills Emacs. */
11855 static int
11856 x_io_error_quitter (display)
11857 Display *display;
11859 char buf[256];
11861 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11862 x_connection_closed (display, buf);
11863 return 0;
11866 /* Changing the font of the frame. */
11868 /* Give frame F the font named FONTNAME as its default font, and
11869 return the full name of that font. FONTNAME may be a wildcard
11870 pattern; in that case, we choose some font that fits the pattern.
11871 The return value shows which font we chose. */
11873 Lisp_Object
11874 x_new_font (f, fontname)
11875 struct frame *f;
11876 register char *fontname;
11878 struct font_info *fontp
11879 = FS_LOAD_FONT (f, 0, fontname, -1);
11881 if (!fontp)
11882 return Qnil;
11884 f->output_data.x->font = (XFontStruct *) (fontp->font);
11885 f->output_data.x->baseline_offset = fontp->baseline_offset;
11886 f->output_data.x->fontset = -1;
11888 /* Compute the scroll bar width in character columns. */
11889 if (f->scroll_bar_pixel_width > 0)
11891 int wid = FONT_WIDTH (f->output_data.x->font);
11892 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11894 else
11896 int wid = FONT_WIDTH (f->output_data.x->font);
11897 f->scroll_bar_cols = (14 + wid - 1) / wid;
11900 /* Now make the frame display the given font. */
11901 if (FRAME_X_WINDOW (f) != 0)
11903 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11904 f->output_data.x->font->fid);
11905 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11906 f->output_data.x->font->fid);
11907 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11908 f->output_data.x->font->fid);
11910 frame_update_line_height (f);
11912 /* Don't change the size of a tip frame; there's no point in
11913 doing it because it's done in Fx_show_tip, and it leads to
11914 problems because the tip frame has no widget. */
11915 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
11916 x_set_window_size (f, 0, f->width, f->height);
11918 else
11919 /* If we are setting a new frame's font for the first time,
11920 there are no faces yet, so this font's height is the line height. */
11921 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
11923 return build_string (fontp->full_name);
11926 /* Give frame F the fontset named FONTSETNAME as its default font, and
11927 return the full name of that fontset. FONTSETNAME may be a wildcard
11928 pattern; in that case, we choose some fontset that fits the pattern.
11929 The return value shows which fontset we chose. */
11931 Lisp_Object
11932 x_new_fontset (f, fontsetname)
11933 struct frame *f;
11934 char *fontsetname;
11936 int fontset = fs_query_fontset (build_string (fontsetname), 0);
11937 Lisp_Object result;
11939 if (fontset < 0)
11940 return Qnil;
11942 if (f->output_data.x->fontset == fontset)
11943 /* This fontset is already set in frame F. There's nothing more
11944 to do. */
11945 return fontset_name (fontset);
11947 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
11949 if (!STRINGP (result))
11950 /* Can't load ASCII font. */
11951 return Qnil;
11953 /* Since x_new_font doesn't update any fontset information, do it now. */
11954 f->output_data.x->fontset = fontset;
11956 #ifdef HAVE_X_I18N
11957 if (FRAME_XIC (f)
11958 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
11959 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
11960 #endif
11962 return build_string (fontsetname);
11966 /***********************************************************************
11967 X Input Methods
11968 ***********************************************************************/
11970 #ifdef HAVE_X_I18N
11972 #ifdef HAVE_X11R6
11974 /* XIM destroy callback function, which is called whenever the
11975 connection to input method XIM dies. CLIENT_DATA contains a
11976 pointer to the x_display_info structure corresponding to XIM. */
11978 static void
11979 xim_destroy_callback (xim, client_data, call_data)
11980 XIM xim;
11981 XPointer client_data;
11982 XPointer call_data;
11984 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11985 Lisp_Object frame, tail;
11987 BLOCK_INPUT;
11989 /* No need to call XDestroyIC.. */
11990 FOR_EACH_FRAME (tail, frame)
11992 struct frame *f = XFRAME (frame);
11993 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11995 FRAME_XIC (f) = NULL;
11996 if (FRAME_XIC_FONTSET (f))
11998 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11999 FRAME_XIC_FONTSET (f) = NULL;
12004 /* No need to call XCloseIM. */
12005 dpyinfo->xim = NULL;
12006 XFree (dpyinfo->xim_styles);
12007 UNBLOCK_INPUT;
12010 #endif /* HAVE_X11R6 */
12012 /* Open the connection to the XIM server on display DPYINFO.
12013 RESOURCE_NAME is the resource name Emacs uses. */
12015 static void
12016 xim_open_dpy (dpyinfo, resource_name)
12017 struct x_display_info *dpyinfo;
12018 char *resource_name;
12020 #ifdef USE_XIM
12021 XIM xim;
12023 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
12024 dpyinfo->xim = xim;
12026 if (xim)
12028 #ifdef HAVE_X11R6
12029 XIMCallback destroy;
12030 #endif
12032 /* Get supported styles and XIM values. */
12033 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
12035 #ifdef HAVE_X11R6
12036 destroy.callback = xim_destroy_callback;
12037 destroy.client_data = (XPointer)dpyinfo;
12038 /* This isn't prototyped in OSF 5.0. */
12039 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
12040 #endif
12043 #else /* not USE_XIM */
12044 dpyinfo->xim = NULL;
12045 #endif /* not USE_XIM */
12049 #ifdef HAVE_X11R6_XIM
12051 struct xim_inst_t
12053 struct x_display_info *dpyinfo;
12054 char *resource_name;
12057 /* XIM instantiate callback function, which is called whenever an XIM
12058 server is available. DISPLAY is teh display of the XIM.
12059 CLIENT_DATA contains a pointer to an xim_inst_t structure created
12060 when the callback was registered. */
12062 static void
12063 xim_instantiate_callback (display, client_data, call_data)
12064 Display *display;
12065 XPointer client_data;
12066 XPointer call_data;
12068 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
12069 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
12071 /* We don't support multiple XIM connections. */
12072 if (dpyinfo->xim)
12073 return;
12075 xim_open_dpy (dpyinfo, xim_inst->resource_name);
12077 /* Create XIC for the existing frames on the same display, as long
12078 as they have no XIC. */
12079 if (dpyinfo->xim && dpyinfo->reference_count > 0)
12081 Lisp_Object tail, frame;
12083 BLOCK_INPUT;
12084 FOR_EACH_FRAME (tail, frame)
12086 struct frame *f = XFRAME (frame);
12088 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
12089 if (FRAME_XIC (f) == NULL)
12091 create_frame_xic (f);
12092 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
12093 xic_set_statusarea (f);
12094 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
12096 struct window *w = XWINDOW (f->selected_window);
12097 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
12102 UNBLOCK_INPUT;
12106 #endif /* HAVE_X11R6_XIM */
12109 /* Open a connection to the XIM server on display DPYINFO.
12110 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
12111 connection only at the first time. On X11R6, open the connection
12112 in the XIM instantiate callback function. */
12114 static void
12115 xim_initialize (dpyinfo, resource_name)
12116 struct x_display_info *dpyinfo;
12117 char *resource_name;
12119 #ifdef USE_XIM
12120 #ifdef HAVE_X11R6_XIM
12121 struct xim_inst_t *xim_inst;
12122 int len;
12124 dpyinfo->xim = NULL;
12125 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
12126 xim_inst->dpyinfo = dpyinfo;
12127 len = strlen (resource_name);
12128 xim_inst->resource_name = (char *) xmalloc (len + 1);
12129 bcopy (resource_name, xim_inst->resource_name, len + 1);
12130 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12131 resource_name, EMACS_CLASS,
12132 xim_instantiate_callback,
12133 /* Fixme: This is XPointer in
12134 XFree86 but (XPointer *) on
12135 Tru64, at least. */
12136 (XPointer) xim_inst);
12137 #else /* not HAVE_X11R6_XIM */
12138 dpyinfo->xim = NULL;
12139 xim_open_dpy (dpyinfo, resource_name);
12140 #endif /* not HAVE_X11R6_XIM */
12142 #else /* not USE_XIM */
12143 dpyinfo->xim = NULL;
12144 #endif /* not USE_XIM */
12148 /* Close the connection to the XIM server on display DPYINFO. */
12150 static void
12151 xim_close_dpy (dpyinfo)
12152 struct x_display_info *dpyinfo;
12154 #ifdef USE_XIM
12155 #ifdef HAVE_X11R6_XIM
12156 if (dpyinfo->display)
12157 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12158 NULL, EMACS_CLASS,
12159 xim_instantiate_callback, NULL);
12160 #endif /* not HAVE_X11R6_XIM */
12161 if (dpyinfo->display)
12162 XCloseIM (dpyinfo->xim);
12163 dpyinfo->xim = NULL;
12164 XFree (dpyinfo->xim_styles);
12165 #endif /* USE_XIM */
12168 #endif /* not HAVE_X11R6_XIM */
12172 /* Calculate the absolute position in frame F
12173 from its current recorded position values and gravity. */
12175 void
12176 x_calc_absolute_position (f)
12177 struct frame *f;
12179 Window child;
12180 int win_x = 0, win_y = 0;
12181 int flags = f->output_data.x->size_hint_flags;
12182 int this_window;
12184 /* We have nothing to do if the current position
12185 is already for the top-left corner. */
12186 if (! ((flags & XNegative) || (flags & YNegative)))
12187 return;
12189 #ifdef USE_X_TOOLKIT
12190 this_window = XtWindow (f->output_data.x->widget);
12191 #else
12192 this_window = FRAME_X_WINDOW (f);
12193 #endif
12195 /* Find the position of the outside upper-left corner of
12196 the inner window, with respect to the outer window.
12197 But do this only if we will need the results. */
12198 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
12200 int count;
12202 BLOCK_INPUT;
12203 count = x_catch_errors (FRAME_X_DISPLAY (f));
12204 while (1)
12206 x_clear_errors (FRAME_X_DISPLAY (f));
12207 XTranslateCoordinates (FRAME_X_DISPLAY (f),
12209 /* From-window, to-window. */
12210 this_window,
12211 f->output_data.x->parent_desc,
12213 /* From-position, to-position. */
12214 0, 0, &win_x, &win_y,
12216 /* Child of win. */
12217 &child);
12218 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12220 Window newroot, newparent = 0xdeadbeef;
12221 Window *newchildren;
12222 unsigned int nchildren;
12224 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
12225 &newparent, &newchildren, &nchildren))
12226 break;
12228 XFree ((char *) newchildren);
12230 f->output_data.x->parent_desc = newparent;
12232 else
12233 break;
12236 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12237 UNBLOCK_INPUT;
12240 /* Treat negative positions as relative to the leftmost bottommost
12241 position that fits on the screen. */
12242 if (flags & XNegative)
12243 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
12244 - 2 * f->output_data.x->border_width - win_x
12245 - PIXEL_WIDTH (f)
12246 + f->output_data.x->left_pos);
12249 int height = PIXEL_HEIGHT (f);
12251 #if defined USE_X_TOOLKIT && defined USE_MOTIF
12252 /* Something is fishy here. When using Motif, starting Emacs with
12253 `-g -0-0', the frame appears too low by a few pixels.
12255 This seems to be so because initially, while Emacs is starting,
12256 the column widget's height and the frame's pixel height are
12257 different. The column widget's height is the right one. In
12258 later invocations, when Emacs is up, the frame's pixel height
12259 is right, though.
12261 It's not obvious where the initial small difference comes from.
12262 2000-12-01, gerd. */
12264 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
12265 #endif
12267 if (flags & YNegative)
12268 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
12269 - 2 * f->output_data.x->border_width
12270 - win_y
12271 - height
12272 + f->output_data.x->top_pos);
12275 /* The left_pos and top_pos
12276 are now relative to the top and left screen edges,
12277 so the flags should correspond. */
12278 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12281 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
12282 to really change the position, and 0 when calling from
12283 x_make_frame_visible (in that case, XOFF and YOFF are the current
12284 position values). It is -1 when calling from x_set_frame_parameters,
12285 which means, do adjust for borders but don't change the gravity. */
12287 void
12288 x_set_offset (f, xoff, yoff, change_gravity)
12289 struct frame *f;
12290 register int xoff, yoff;
12291 int change_gravity;
12293 int modified_top, modified_left;
12295 if (change_gravity > 0)
12297 f->output_data.x->top_pos = yoff;
12298 f->output_data.x->left_pos = xoff;
12299 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12300 if (xoff < 0)
12301 f->output_data.x->size_hint_flags |= XNegative;
12302 if (yoff < 0)
12303 f->output_data.x->size_hint_flags |= YNegative;
12304 f->output_data.x->win_gravity = NorthWestGravity;
12306 x_calc_absolute_position (f);
12308 BLOCK_INPUT;
12309 x_wm_set_size_hint (f, (long) 0, 0);
12311 modified_left = f->output_data.x->left_pos;
12312 modified_top = f->output_data.x->top_pos;
12313 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
12314 this seems to be unnecessary and incorrect. rms, 4/17/97. */
12315 /* It is a mystery why we need to add the border_width here
12316 when the frame is already visible, but experiment says we do. */
12317 if (change_gravity != 0)
12319 modified_left += f->output_data.x->border_width;
12320 modified_top += f->output_data.x->border_width;
12322 #endif
12324 #ifdef USE_X_TOOLKIT
12325 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
12326 modified_left, modified_top);
12327 #else /* not USE_X_TOOLKIT */
12328 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12329 modified_left, modified_top);
12330 #endif /* not USE_X_TOOLKIT */
12331 UNBLOCK_INPUT;
12335 /* Change the size of frame F's X window to COLS/ROWS in the case F
12336 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
12337 top-left-corner window gravity for this size change and subsequent
12338 size changes. Otherwise we leave the window gravity unchanged. */
12340 static void
12341 x_set_window_size_1 (f, change_gravity, cols, rows)
12342 struct frame *f;
12343 int change_gravity;
12344 int cols, rows;
12346 int pixelwidth, pixelheight;
12348 check_frame_size (f, &rows, &cols);
12349 f->output_data.x->vertical_scroll_bar_extra
12350 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
12352 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
12353 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
12354 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
12355 f->output_data.x->flags_areas_extra
12356 = FRAME_FLAGS_AREA_WIDTH (f);
12357 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
12358 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
12360 f->output_data.x->win_gravity = NorthWestGravity;
12361 x_wm_set_size_hint (f, (long) 0, 0);
12363 XSync (FRAME_X_DISPLAY (f), False);
12364 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12365 pixelwidth, pixelheight);
12367 /* Now, strictly speaking, we can't be sure that this is accurate,
12368 but the window manager will get around to dealing with the size
12369 change request eventually, and we'll hear how it went when the
12370 ConfigureNotify event gets here.
12372 We could just not bother storing any of this information here,
12373 and let the ConfigureNotify event set everything up, but that
12374 might be kind of confusing to the Lisp code, since size changes
12375 wouldn't be reported in the frame parameters until some random
12376 point in the future when the ConfigureNotify event arrives.
12378 We pass 1 for DELAY since we can't run Lisp code inside of
12379 a BLOCK_INPUT. */
12380 change_frame_size (f, rows, cols, 0, 1, 0);
12381 PIXEL_WIDTH (f) = pixelwidth;
12382 PIXEL_HEIGHT (f) = pixelheight;
12384 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
12385 receive in the ConfigureNotify event; if we get what we asked
12386 for, then the event won't cause the screen to become garbaged, so
12387 we have to make sure to do it here. */
12388 SET_FRAME_GARBAGED (f);
12390 XFlush (FRAME_X_DISPLAY (f));
12394 /* Call this to change the size of frame F's x-window.
12395 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
12396 for this size change and subsequent size changes.
12397 Otherwise we leave the window gravity unchanged. */
12399 void
12400 x_set_window_size (f, change_gravity, cols, rows)
12401 struct frame *f;
12402 int change_gravity;
12403 int cols, rows;
12405 BLOCK_INPUT;
12407 #ifdef USE_X_TOOLKIT
12409 if (f->output_data.x->widget != NULL)
12411 /* The x and y position of the widget is clobbered by the
12412 call to XtSetValues within EmacsFrameSetCharSize.
12413 This is a real kludge, but I don't understand Xt so I can't
12414 figure out a correct fix. Can anyone else tell me? -- rms. */
12415 int xpos = f->output_data.x->widget->core.x;
12416 int ypos = f->output_data.x->widget->core.y;
12417 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
12418 f->output_data.x->widget->core.x = xpos;
12419 f->output_data.x->widget->core.y = ypos;
12421 else
12422 x_set_window_size_1 (f, change_gravity, cols, rows);
12424 #else /* not USE_X_TOOLKIT */
12426 x_set_window_size_1 (f, change_gravity, cols, rows);
12428 #endif /* not USE_X_TOOLKIT */
12430 /* If cursor was outside the new size, mark it as off. */
12431 mark_window_cursors_off (XWINDOW (f->root_window));
12433 /* Clear out any recollection of where the mouse highlighting was,
12434 since it might be in a place that's outside the new frame size.
12435 Actually checking whether it is outside is a pain in the neck,
12436 so don't try--just let the highlighting be done afresh with new size. */
12437 cancel_mouse_face (f);
12439 UNBLOCK_INPUT;
12442 /* Mouse warping. */
12444 void
12445 x_set_mouse_position (f, x, y)
12446 struct frame *f;
12447 int x, y;
12449 int pix_x, pix_y;
12451 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
12452 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
12454 if (pix_x < 0) pix_x = 0;
12455 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
12457 if (pix_y < 0) pix_y = 0;
12458 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
12460 BLOCK_INPUT;
12462 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12463 0, 0, 0, 0, pix_x, pix_y);
12464 UNBLOCK_INPUT;
12467 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
12469 void
12470 x_set_mouse_pixel_position (f, pix_x, pix_y)
12471 struct frame *f;
12472 int pix_x, pix_y;
12474 BLOCK_INPUT;
12476 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12477 0, 0, 0, 0, pix_x, pix_y);
12478 UNBLOCK_INPUT;
12481 /* focus shifting, raising and lowering. */
12483 void
12484 x_focus_on_frame (f)
12485 struct frame *f;
12487 #if 0 /* This proves to be unpleasant. */
12488 x_raise_frame (f);
12489 #endif
12490 #if 0
12491 /* I don't think that the ICCCM allows programs to do things like this
12492 without the interaction of the window manager. Whatever you end up
12493 doing with this code, do it to x_unfocus_frame too. */
12494 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12495 RevertToPointerRoot, CurrentTime);
12496 #endif /* ! 0 */
12499 void
12500 x_unfocus_frame (f)
12501 struct frame *f;
12503 #if 0
12504 /* Look at the remarks in x_focus_on_frame. */
12505 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
12506 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
12507 RevertToPointerRoot, CurrentTime);
12508 #endif /* ! 0 */
12511 /* Raise frame F. */
12513 void
12514 x_raise_frame (f)
12515 struct frame *f;
12517 if (f->async_visible)
12519 BLOCK_INPUT;
12520 #ifdef USE_X_TOOLKIT
12521 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12522 #else /* not USE_X_TOOLKIT */
12523 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12524 #endif /* not USE_X_TOOLKIT */
12525 XFlush (FRAME_X_DISPLAY (f));
12526 UNBLOCK_INPUT;
12530 /* Lower frame F. */
12532 void
12533 x_lower_frame (f)
12534 struct frame *f;
12536 if (f->async_visible)
12538 BLOCK_INPUT;
12539 #ifdef USE_X_TOOLKIT
12540 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12541 #else /* not USE_X_TOOLKIT */
12542 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12543 #endif /* not USE_X_TOOLKIT */
12544 XFlush (FRAME_X_DISPLAY (f));
12545 UNBLOCK_INPUT;
12549 static void
12550 XTframe_raise_lower (f, raise_flag)
12551 FRAME_PTR f;
12552 int raise_flag;
12554 if (raise_flag)
12555 x_raise_frame (f);
12556 else
12557 x_lower_frame (f);
12560 /* Change of visibility. */
12562 /* This tries to wait until the frame is really visible.
12563 However, if the window manager asks the user where to position
12564 the frame, this will return before the user finishes doing that.
12565 The frame will not actually be visible at that time,
12566 but it will become visible later when the window manager
12567 finishes with it. */
12569 void
12570 x_make_frame_visible (f)
12571 struct frame *f;
12573 Lisp_Object type;
12574 int original_top, original_left;
12575 int retry_count = 2;
12577 retry:
12579 BLOCK_INPUT;
12581 type = x_icon_type (f);
12582 if (!NILP (type))
12583 x_bitmap_icon (f, type);
12585 if (! FRAME_VISIBLE_P (f))
12587 /* We test FRAME_GARBAGED_P here to make sure we don't
12588 call x_set_offset a second time
12589 if we get to x_make_frame_visible a second time
12590 before the window gets really visible. */
12591 if (! FRAME_ICONIFIED_P (f)
12592 && ! f->output_data.x->asked_for_visible)
12593 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12595 f->output_data.x->asked_for_visible = 1;
12597 if (! EQ (Vx_no_window_manager, Qt))
12598 x_wm_set_window_state (f, NormalState);
12599 #ifdef USE_X_TOOLKIT
12600 /* This was XtPopup, but that did nothing for an iconified frame. */
12601 XtMapWidget (f->output_data.x->widget);
12602 #else /* not USE_X_TOOLKIT */
12603 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12604 #endif /* not USE_X_TOOLKIT */
12605 #if 0 /* This seems to bring back scroll bars in the wrong places
12606 if the window configuration has changed. They seem
12607 to come back ok without this. */
12608 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
12609 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12610 #endif
12613 XFlush (FRAME_X_DISPLAY (f));
12615 /* Synchronize to ensure Emacs knows the frame is visible
12616 before we do anything else. We do this loop with input not blocked
12617 so that incoming events are handled. */
12619 Lisp_Object frame;
12620 int count;
12621 /* This must be before UNBLOCK_INPUT
12622 since events that arrive in response to the actions above
12623 will set it when they are handled. */
12624 int previously_visible = f->output_data.x->has_been_visible;
12626 original_left = f->output_data.x->left_pos;
12627 original_top = f->output_data.x->top_pos;
12629 /* This must come after we set COUNT. */
12630 UNBLOCK_INPUT;
12632 /* We unblock here so that arriving X events are processed. */
12634 /* Now move the window back to where it was "supposed to be".
12635 But don't do it if the gravity is negative.
12636 When the gravity is negative, this uses a position
12637 that is 3 pixels too low. Perhaps that's really the border width.
12639 Don't do this if the window has never been visible before,
12640 because the window manager may choose the position
12641 and we don't want to override it. */
12643 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
12644 && f->output_data.x->win_gravity == NorthWestGravity
12645 && previously_visible)
12647 Drawable rootw;
12648 int x, y;
12649 unsigned int width, height, border, depth;
12651 BLOCK_INPUT;
12653 /* On some window managers (such as FVWM) moving an existing
12654 window, even to the same place, causes the window manager
12655 to introduce an offset. This can cause the window to move
12656 to an unexpected location. Check the geometry (a little
12657 slow here) and then verify that the window is in the right
12658 place. If the window is not in the right place, move it
12659 there, and take the potential window manager hit. */
12660 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12661 &rootw, &x, &y, &width, &height, &border, &depth);
12663 if (original_left != x || original_top != y)
12664 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12665 original_left, original_top);
12667 UNBLOCK_INPUT;
12670 XSETFRAME (frame, f);
12672 /* Wait until the frame is visible. Process X events until a
12673 MapNotify event has been seen, or until we think we won't get a
12674 MapNotify at all.. */
12675 for (count = input_signal_count + 10;
12676 input_signal_count < count && !FRAME_VISIBLE_P (f);)
12678 /* Force processing of queued events. */
12679 x_sync (f);
12681 /* Machines that do polling rather than SIGIO have been
12682 observed to go into a busy-wait here. So we'll fake an
12683 alarm signal to let the handler know that there's something
12684 to be read. We used to raise a real alarm, but it seems
12685 that the handler isn't always enabled here. This is
12686 probably a bug. */
12687 if (input_polling_used ())
12689 /* It could be confusing if a real alarm arrives while
12690 processing the fake one. Turn it off and let the
12691 handler reset it. */
12692 extern void poll_for_input_1 P_ ((void));
12693 int old_poll_suppress_count = poll_suppress_count;
12694 poll_suppress_count = 1;
12695 poll_for_input_1 ();
12696 poll_suppress_count = old_poll_suppress_count;
12699 /* See if a MapNotify event has been processed. */
12700 FRAME_SAMPLE_VISIBILITY (f);
12703 /* 2000-09-28: In
12705 (let ((f (selected-frame)))
12706 (iconify-frame f)
12707 (raise-frame f))
12709 the frame is not raised with various window managers on
12710 FreeBSD, Linux and Solaris. It turns out that, for some
12711 unknown reason, the call to XtMapWidget is completely ignored.
12712 Mapping the widget a second time works. */
12714 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
12715 goto retry;
12719 /* Change from mapped state to withdrawn state. */
12721 /* Make the frame visible (mapped and not iconified). */
12723 void
12724 x_make_frame_invisible (f)
12725 struct frame *f;
12727 Window window;
12729 #ifdef USE_X_TOOLKIT
12730 /* Use the frame's outermost window, not the one we normally draw on. */
12731 window = XtWindow (f->output_data.x->widget);
12732 #else /* not USE_X_TOOLKIT */
12733 window = FRAME_X_WINDOW (f);
12734 #endif /* not USE_X_TOOLKIT */
12736 /* Don't keep the highlight on an invisible frame. */
12737 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12738 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12740 #if 0/* This might add unreliability; I don't trust it -- rms. */
12741 if (! f->async_visible && ! f->async_iconified)
12742 return;
12743 #endif
12745 BLOCK_INPUT;
12747 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
12748 that the current position of the window is user-specified, rather than
12749 program-specified, so that when the window is mapped again, it will be
12750 placed at the same location, without forcing the user to position it
12751 by hand again (they have already done that once for this window.) */
12752 x_wm_set_size_hint (f, (long) 0, 1);
12754 #ifdef HAVE_X11R4
12756 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
12757 DefaultScreen (FRAME_X_DISPLAY (f))))
12759 UNBLOCK_INPUT_RESIGNAL;
12760 error ("Can't notify window manager of window withdrawal");
12762 #else /* ! defined (HAVE_X11R4) */
12764 /* Tell the window manager what we're going to do. */
12765 if (! EQ (Vx_no_window_manager, Qt))
12767 XEvent unmap;
12769 unmap.xunmap.type = UnmapNotify;
12770 unmap.xunmap.window = window;
12771 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
12772 unmap.xunmap.from_configure = False;
12773 if (! XSendEvent (FRAME_X_DISPLAY (f),
12774 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12775 False,
12776 SubstructureRedirectMaskSubstructureNotifyMask,
12777 &unmap))
12779 UNBLOCK_INPUT_RESIGNAL;
12780 error ("Can't notify window manager of withdrawal");
12784 /* Unmap the window ourselves. Cheeky! */
12785 XUnmapWindow (FRAME_X_DISPLAY (f), window);
12786 #endif /* ! defined (HAVE_X11R4) */
12788 /* We can't distinguish this from iconification
12789 just by the event that we get from the server.
12790 So we can't win using the usual strategy of letting
12791 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
12792 and synchronize with the server to make sure we agree. */
12793 f->visible = 0;
12794 FRAME_ICONIFIED_P (f) = 0;
12795 f->async_visible = 0;
12796 f->async_iconified = 0;
12798 x_sync (f);
12800 UNBLOCK_INPUT;
12803 /* Change window state from mapped to iconified. */
12805 void
12806 x_iconify_frame (f)
12807 struct frame *f;
12809 int result;
12810 Lisp_Object type;
12812 /* Don't keep the highlight on an invisible frame. */
12813 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12814 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12816 if (f->async_iconified)
12817 return;
12819 BLOCK_INPUT;
12821 FRAME_SAMPLE_VISIBILITY (f);
12823 type = x_icon_type (f);
12824 if (!NILP (type))
12825 x_bitmap_icon (f, type);
12827 #ifdef USE_X_TOOLKIT
12829 if (! FRAME_VISIBLE_P (f))
12831 if (! EQ (Vx_no_window_manager, Qt))
12832 x_wm_set_window_state (f, IconicState);
12833 /* This was XtPopup, but that did nothing for an iconified frame. */
12834 XtMapWidget (f->output_data.x->widget);
12835 /* The server won't give us any event to indicate
12836 that an invisible frame was changed to an icon,
12837 so we have to record it here. */
12838 f->iconified = 1;
12839 f->visible = 1;
12840 f->async_iconified = 1;
12841 f->async_visible = 0;
12842 UNBLOCK_INPUT;
12843 return;
12846 result = XIconifyWindow (FRAME_X_DISPLAY (f),
12847 XtWindow (f->output_data.x->widget),
12848 DefaultScreen (FRAME_X_DISPLAY (f)));
12849 UNBLOCK_INPUT;
12851 if (!result)
12852 error ("Can't notify window manager of iconification");
12854 f->async_iconified = 1;
12855 f->async_visible = 0;
12858 BLOCK_INPUT;
12859 XFlush (FRAME_X_DISPLAY (f));
12860 UNBLOCK_INPUT;
12861 #else /* not USE_X_TOOLKIT */
12863 /* Make sure the X server knows where the window should be positioned,
12864 in case the user deiconifies with the window manager. */
12865 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
12866 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12868 /* Since we don't know which revision of X we're running, we'll use both
12869 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
12871 /* X11R4: send a ClientMessage to the window manager using the
12872 WM_CHANGE_STATE type. */
12874 XEvent message;
12876 message.xclient.window = FRAME_X_WINDOW (f);
12877 message.xclient.type = ClientMessage;
12878 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
12879 message.xclient.format = 32;
12880 message.xclient.data.l[0] = IconicState;
12882 if (! XSendEvent (FRAME_X_DISPLAY (f),
12883 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12884 False,
12885 SubstructureRedirectMask | SubstructureNotifyMask,
12886 &message))
12888 UNBLOCK_INPUT_RESIGNAL;
12889 error ("Can't notify window manager of iconification");
12893 /* X11R3: set the initial_state field of the window manager hints to
12894 IconicState. */
12895 x_wm_set_window_state (f, IconicState);
12897 if (!FRAME_VISIBLE_P (f))
12899 /* If the frame was withdrawn, before, we must map it. */
12900 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12903 f->async_iconified = 1;
12904 f->async_visible = 0;
12906 XFlush (FRAME_X_DISPLAY (f));
12907 UNBLOCK_INPUT;
12908 #endif /* not USE_X_TOOLKIT */
12912 /* Free X resources of frame F. */
12914 void
12915 x_free_frame_resources (f)
12916 struct frame *f;
12918 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12920 BLOCK_INPUT;
12922 /* If a display connection is dead, don't try sending more
12923 commands to the X server. */
12924 if (dpyinfo->display)
12926 if (f->output_data.x->icon_desc)
12927 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
12929 #ifdef HAVE_X_I18N
12930 if (FRAME_XIC (f))
12931 free_frame_xic (f);
12932 #endif
12934 if (FRAME_X_WINDOW (f))
12935 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12937 #ifdef USE_X_TOOLKIT
12938 if (f->output_data.x->widget)
12940 XtDestroyWidget (f->output_data.x->widget);
12941 f->output_data.x->widget = NULL;
12943 free_frame_menubar (f);
12944 #endif /* USE_X_TOOLKIT */
12946 unload_color (f, f->output_data.x->foreground_pixel);
12947 unload_color (f, f->output_data.x->background_pixel);
12948 unload_color (f, f->output_data.x->cursor_pixel);
12949 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12950 unload_color (f, f->output_data.x->border_pixel);
12951 unload_color (f, f->output_data.x->mouse_pixel);
12953 if (f->output_data.x->scroll_bar_background_pixel != -1)
12954 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
12955 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
12956 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
12957 if (f->output_data.x->white_relief.allocated_p)
12958 unload_color (f, f->output_data.x->white_relief.pixel);
12959 if (f->output_data.x->black_relief.allocated_p)
12960 unload_color (f, f->output_data.x->black_relief.pixel);
12962 if (FRAME_FACE_CACHE (f))
12963 free_frame_faces (f);
12965 x_free_gcs (f);
12966 XFlush (FRAME_X_DISPLAY (f));
12969 if (f->output_data.x->saved_menu_event)
12970 xfree (f->output_data.x->saved_menu_event);
12972 xfree (f->output_data.x);
12973 f->output_data.x = NULL;
12975 if (f == dpyinfo->x_focus_frame)
12976 dpyinfo->x_focus_frame = 0;
12977 if (f == dpyinfo->x_focus_event_frame)
12978 dpyinfo->x_focus_event_frame = 0;
12979 if (f == dpyinfo->x_highlight_frame)
12980 dpyinfo->x_highlight_frame = 0;
12982 if (f == dpyinfo->mouse_face_mouse_frame)
12984 dpyinfo->mouse_face_beg_row
12985 = dpyinfo->mouse_face_beg_col = -1;
12986 dpyinfo->mouse_face_end_row
12987 = dpyinfo->mouse_face_end_col = -1;
12988 dpyinfo->mouse_face_window = Qnil;
12989 dpyinfo->mouse_face_deferred_gc = 0;
12990 dpyinfo->mouse_face_mouse_frame = 0;
12993 UNBLOCK_INPUT;
12997 /* Destroy the X window of frame F. */
12999 void
13000 x_destroy_window (f)
13001 struct frame *f;
13003 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13005 /* If a display connection is dead, don't try sending more
13006 commands to the X server. */
13007 if (dpyinfo->display != 0)
13008 x_free_frame_resources (f);
13010 dpyinfo->reference_count--;
13014 /* Setting window manager hints. */
13016 /* Set the normal size hints for the window manager, for frame F.
13017 FLAGS is the flags word to use--or 0 meaning preserve the flags
13018 that the window now has.
13019 If USER_POSITION is nonzero, we set the USPosition
13020 flag (this is useful when FLAGS is 0). */
13022 void
13023 x_wm_set_size_hint (f, flags, user_position)
13024 struct frame *f;
13025 long flags;
13026 int user_position;
13028 XSizeHints size_hints;
13030 #ifdef USE_X_TOOLKIT
13031 Arg al[2];
13032 int ac = 0;
13033 Dimension widget_width, widget_height;
13034 Window window = XtWindow (f->output_data.x->widget);
13035 #else /* not USE_X_TOOLKIT */
13036 Window window = FRAME_X_WINDOW (f);
13037 #endif /* not USE_X_TOOLKIT */
13039 /* Setting PMaxSize caused various problems. */
13040 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
13042 size_hints.x = f->output_data.x->left_pos;
13043 size_hints.y = f->output_data.x->top_pos;
13045 #ifdef USE_X_TOOLKIT
13046 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
13047 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
13048 XtGetValues (f->output_data.x->widget, al, ac);
13049 size_hints.height = widget_height;
13050 size_hints.width = widget_width;
13051 #else /* not USE_X_TOOLKIT */
13052 size_hints.height = PIXEL_HEIGHT (f);
13053 size_hints.width = PIXEL_WIDTH (f);
13054 #endif /* not USE_X_TOOLKIT */
13056 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
13057 size_hints.height_inc = f->output_data.x->line_height;
13058 size_hints.max_width
13059 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
13060 size_hints.max_height
13061 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
13063 /* Calculate the base and minimum sizes.
13065 (When we use the X toolkit, we don't do it here.
13066 Instead we copy the values that the widgets are using, below.) */
13067 #ifndef USE_X_TOOLKIT
13069 int base_width, base_height;
13070 int min_rows = 0, min_cols = 0;
13072 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
13073 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
13075 check_frame_size (f, &min_rows, &min_cols);
13077 /* The window manager uses the base width hints to calculate the
13078 current number of rows and columns in the frame while
13079 resizing; min_width and min_height aren't useful for this
13080 purpose, since they might not give the dimensions for a
13081 zero-row, zero-column frame.
13083 We use the base_width and base_height members if we have
13084 them; otherwise, we set the min_width and min_height members
13085 to the size for a zero x zero frame. */
13087 #ifdef HAVE_X11R4
13088 size_hints.flags |= PBaseSize;
13089 size_hints.base_width = base_width;
13090 size_hints.base_height = base_height;
13091 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
13092 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
13093 #else
13094 size_hints.min_width = base_width;
13095 size_hints.min_height = base_height;
13096 #endif
13099 /* If we don't need the old flags, we don't need the old hint at all. */
13100 if (flags)
13102 size_hints.flags |= flags;
13103 goto no_read;
13105 #endif /* not USE_X_TOOLKIT */
13108 XSizeHints hints; /* Sometimes I hate X Windows... */
13109 long supplied_return;
13110 int value;
13112 #ifdef HAVE_X11R4
13113 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
13114 &supplied_return);
13115 #else
13116 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
13117 #endif
13119 #ifdef USE_X_TOOLKIT
13120 size_hints.base_height = hints.base_height;
13121 size_hints.base_width = hints.base_width;
13122 size_hints.min_height = hints.min_height;
13123 size_hints.min_width = hints.min_width;
13124 #endif
13126 if (flags)
13127 size_hints.flags |= flags;
13128 else
13130 if (value == 0)
13131 hints.flags = 0;
13132 if (hints.flags & PSize)
13133 size_hints.flags |= PSize;
13134 if (hints.flags & PPosition)
13135 size_hints.flags |= PPosition;
13136 if (hints.flags & USPosition)
13137 size_hints.flags |= USPosition;
13138 if (hints.flags & USSize)
13139 size_hints.flags |= USSize;
13143 #ifndef USE_X_TOOLKIT
13144 no_read:
13145 #endif
13147 #ifdef PWinGravity
13148 size_hints.win_gravity = f->output_data.x->win_gravity;
13149 size_hints.flags |= PWinGravity;
13151 if (user_position)
13153 size_hints.flags &= ~ PPosition;
13154 size_hints.flags |= USPosition;
13156 #endif /* PWinGravity */
13158 #ifdef HAVE_X11R4
13159 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13160 #else
13161 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13162 #endif
13165 /* Used for IconicState or NormalState */
13167 void
13168 x_wm_set_window_state (f, state)
13169 struct frame *f;
13170 int state;
13172 #ifdef USE_X_TOOLKIT
13173 Arg al[1];
13175 XtSetArg (al[0], XtNinitialState, state);
13176 XtSetValues (f->output_data.x->widget, al, 1);
13177 #else /* not USE_X_TOOLKIT */
13178 Window window = FRAME_X_WINDOW (f);
13180 f->output_data.x->wm_hints.flags |= StateHint;
13181 f->output_data.x->wm_hints.initial_state = state;
13183 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13184 #endif /* not USE_X_TOOLKIT */
13187 void
13188 x_wm_set_icon_pixmap (f, pixmap_id)
13189 struct frame *f;
13190 int pixmap_id;
13192 Pixmap icon_pixmap;
13194 #ifndef USE_X_TOOLKIT
13195 Window window = FRAME_X_WINDOW (f);
13196 #endif
13198 if (pixmap_id > 0)
13200 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
13201 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
13203 else
13205 /* It seems there is no way to turn off use of an icon pixmap.
13206 The following line does it, only if no icon has yet been created,
13207 for some window managers. But with mwm it crashes.
13208 Some people say it should clear the IconPixmapHint bit in this case,
13209 but that doesn't work, and the X consortium said it isn't the
13210 right thing at all. Since there is no way to win,
13211 best to explicitly give up. */
13212 #if 0
13213 f->output_data.x->wm_hints.icon_pixmap = None;
13214 #else
13215 return;
13216 #endif
13219 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
13222 Arg al[1];
13223 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
13224 XtSetValues (f->output_data.x->widget, al, 1);
13227 #else /* not USE_X_TOOLKIT */
13229 f->output_data.x->wm_hints.flags |= IconPixmapHint;
13230 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13232 #endif /* not USE_X_TOOLKIT */
13235 void
13236 x_wm_set_icon_position (f, icon_x, icon_y)
13237 struct frame *f;
13238 int icon_x, icon_y;
13240 #ifdef USE_X_TOOLKIT
13241 Window window = XtWindow (f->output_data.x->widget);
13242 #else
13243 Window window = FRAME_X_WINDOW (f);
13244 #endif
13246 f->output_data.x->wm_hints.flags |= IconPositionHint;
13247 f->output_data.x->wm_hints.icon_x = icon_x;
13248 f->output_data.x->wm_hints.icon_y = icon_y;
13250 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13254 /***********************************************************************
13255 Fonts
13256 ***********************************************************************/
13258 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
13260 struct font_info *
13261 x_get_font_info (f, font_idx)
13262 FRAME_PTR f;
13263 int font_idx;
13265 return (FRAME_X_FONT_TABLE (f) + font_idx);
13269 /* Return a list of names of available fonts matching PATTERN on frame F.
13271 If SIZE is > 0, it is the size (maximum bounds width) of fonts
13272 to be listed.
13274 SIZE < 0 means include scalable fonts.
13276 Frame F null means we have not yet created any frame on X, and
13277 consult the first display in x_display_list. MAXNAMES sets a limit
13278 on how many fonts to match. */
13280 Lisp_Object
13281 x_list_fonts (f, pattern, size, maxnames)
13282 struct frame *f;
13283 Lisp_Object pattern;
13284 int size;
13285 int maxnames;
13287 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
13288 Lisp_Object tem, second_best;
13289 struct x_display_info *dpyinfo
13290 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
13291 Display *dpy = dpyinfo->display;
13292 int try_XLoadQueryFont = 0;
13293 int count;
13294 int allow_scalable_fonts_p = 0;
13296 if (size < 0)
13298 allow_scalable_fonts_p = 1;
13299 size = 0;
13302 patterns = Fassoc (pattern, Valternate_fontname_alist);
13303 if (NILP (patterns))
13304 patterns = Fcons (pattern, Qnil);
13306 if (maxnames == 1 && !size)
13307 /* We can return any single font matching PATTERN. */
13308 try_XLoadQueryFont = 1;
13310 for (; CONSP (patterns); patterns = XCDR (patterns))
13312 int num_fonts;
13313 char **names = NULL;
13315 pattern = XCAR (patterns);
13316 /* See if we cached the result for this particular query.
13317 The cache is an alist of the form:
13318 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
13319 tem = XCDR (dpyinfo->name_list_element);
13320 key = Fcons (Fcons (pattern, make_number (maxnames)),
13321 allow_scalable_fonts_p ? Qt : Qnil);
13322 list = Fassoc (key, tem);
13323 if (!NILP (list))
13325 list = Fcdr_safe (list);
13326 /* We have a cashed list. Don't have to get the list again. */
13327 goto label_cached;
13330 /* At first, put PATTERN in the cache. */
13332 BLOCK_INPUT;
13333 count = x_catch_errors (dpy);
13335 if (try_XLoadQueryFont)
13337 XFontStruct *font;
13338 unsigned long value;
13340 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
13341 if (x_had_errors_p (dpy))
13343 /* This error is perhaps due to insufficient memory on X
13344 server. Let's just ignore it. */
13345 font = NULL;
13346 x_clear_errors (dpy);
13349 if (font
13350 && XGetFontProperty (font, XA_FONT, &value))
13352 char *name = (char *) XGetAtomName (dpy, (Atom) value);
13353 int len = strlen (name);
13354 char *tmp;
13356 /* If DXPC (a Differential X Protocol Compressor)
13357 Ver.3.7 is running, XGetAtomName will return null
13358 string. We must avoid such a name. */
13359 if (len == 0)
13360 try_XLoadQueryFont = 0;
13361 else
13363 num_fonts = 1;
13364 names = (char **) alloca (sizeof (char *));
13365 /* Some systems only allow alloca assigned to a
13366 simple var. */
13367 tmp = (char *) alloca (len + 1); names[0] = tmp;
13368 bcopy (name, names[0], len + 1);
13369 XFree (name);
13372 else
13373 try_XLoadQueryFont = 0;
13375 if (font)
13376 XFreeFont (dpy, font);
13379 if (!try_XLoadQueryFont)
13381 /* We try at least 10 fonts because XListFonts will return
13382 auto-scaled fonts at the head. */
13383 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
13384 &num_fonts);
13385 if (x_had_errors_p (dpy))
13387 /* This error is perhaps due to insufficient memory on X
13388 server. Let's just ignore it. */
13389 names = NULL;
13390 x_clear_errors (dpy);
13394 x_uncatch_errors (dpy, count);
13395 UNBLOCK_INPUT;
13397 if (names)
13399 int i;
13401 /* Make a list of all the fonts we got back.
13402 Store that in the font cache for the display. */
13403 for (i = 0; i < num_fonts; i++)
13405 int width = 0;
13406 char *p = names[i];
13407 int average_width = -1, dashes = 0;
13409 /* Count the number of dashes in NAMES[I]. If there are
13410 14 dashes, and the field value following 12th dash
13411 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
13412 is usually too ugly to be used for editing. Let's
13413 ignore it. */
13414 while (*p)
13415 if (*p++ == '-')
13417 dashes++;
13418 if (dashes == 7) /* PIXEL_SIZE field */
13419 width = atoi (p);
13420 else if (dashes == 12) /* AVERAGE_WIDTH field */
13421 average_width = atoi (p);
13424 if (allow_scalable_fonts_p
13425 || dashes < 14 || average_width != 0)
13427 tem = build_string (names[i]);
13428 if (NILP (Fassoc (tem, list)))
13430 if (STRINGP (Vx_pixel_size_width_font_regexp)
13431 && ((fast_c_string_match_ignore_case
13432 (Vx_pixel_size_width_font_regexp, names[i]))
13433 >= 0))
13434 /* We can set the value of PIXEL_SIZE to the
13435 width of this font. */
13436 list = Fcons (Fcons (tem, make_number (width)), list);
13437 else
13438 /* For the moment, width is not known. */
13439 list = Fcons (Fcons (tem, Qnil), list);
13444 if (!try_XLoadQueryFont)
13446 BLOCK_INPUT;
13447 XFreeFontNames (names);
13448 UNBLOCK_INPUT;
13452 /* Now store the result in the cache. */
13453 XCDR (dpyinfo->name_list_element)
13454 = Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element));
13456 label_cached:
13457 if (NILP (list)) continue; /* Try the remaining alternatives. */
13459 newlist = second_best = Qnil;
13460 /* Make a list of the fonts that have the right width. */
13461 for (; CONSP (list); list = XCDR (list))
13463 int found_size;
13465 tem = XCAR (list);
13467 if (!CONSP (tem) || NILP (XCAR (tem)))
13468 continue;
13469 if (!size)
13471 newlist = Fcons (XCAR (tem), newlist);
13472 continue;
13475 if (!INTEGERP (XCDR (tem)))
13477 /* Since we have not yet known the size of this font, we
13478 must try slow function call XLoadQueryFont. */
13479 XFontStruct *thisinfo;
13481 BLOCK_INPUT;
13482 count = x_catch_errors (dpy);
13483 thisinfo = XLoadQueryFont (dpy,
13484 XSTRING (XCAR (tem))->data);
13485 if (x_had_errors_p (dpy))
13487 /* This error is perhaps due to insufficient memory on X
13488 server. Let's just ignore it. */
13489 thisinfo = NULL;
13490 x_clear_errors (dpy);
13492 x_uncatch_errors (dpy, count);
13493 UNBLOCK_INPUT;
13495 if (thisinfo)
13497 XCDR (tem)
13498 = (thisinfo->min_bounds.width == 0
13499 ? make_number (0)
13500 : make_number (thisinfo->max_bounds.width));
13501 BLOCK_INPUT;
13502 XFreeFont (dpy, thisinfo);
13503 UNBLOCK_INPUT;
13505 else
13506 /* For unknown reason, the previous call of XListFont had
13507 returned a font which can't be opened. Record the size
13508 as 0 not to try to open it again. */
13509 XCDR (tem) = make_number (0);
13512 found_size = XINT (XCDR (tem));
13513 if (found_size == size)
13514 newlist = Fcons (XCAR (tem), newlist);
13515 else if (found_size > 0)
13517 if (NILP (second_best))
13518 second_best = tem;
13519 else if (found_size < size)
13521 if (XINT (XCDR (second_best)) > size
13522 || XINT (XCDR (second_best)) < found_size)
13523 second_best = tem;
13525 else
13527 if (XINT (XCDR (second_best)) > size
13528 && XINT (XCDR (second_best)) > found_size)
13529 second_best = tem;
13533 if (!NILP (newlist))
13534 break;
13535 else if (!NILP (second_best))
13537 newlist = Fcons (XCAR (second_best), Qnil);
13538 break;
13542 return newlist;
13546 #if GLYPH_DEBUG
13548 /* Check that FONT is valid on frame F. It is if it can be found in F's
13549 font table. */
13551 static void
13552 x_check_font (f, font)
13553 struct frame *f;
13554 XFontStruct *font;
13556 int i;
13557 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13559 xassert (font != NULL);
13561 for (i = 0; i < dpyinfo->n_fonts; i++)
13562 if (dpyinfo->font_table[i].name
13563 && font == dpyinfo->font_table[i].font)
13564 break;
13566 xassert (i < dpyinfo->n_fonts);
13569 #endif /* GLYPH_DEBUG != 0 */
13571 /* Set *W to the minimum width, *H to the minimum font height of FONT.
13572 Note: There are (broken) X fonts out there with invalid XFontStruct
13573 min_bounds contents. For example, handa@etl.go.jp reports that
13574 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
13575 have font->min_bounds.width == 0. */
13577 static INLINE void
13578 x_font_min_bounds (font, w, h)
13579 XFontStruct *font;
13580 int *w, *h;
13582 *h = FONT_HEIGHT (font);
13583 *w = font->min_bounds.width;
13585 /* Try to handle the case where FONT->min_bounds has invalid
13586 contents. Since the only font known to have invalid min_bounds
13587 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
13588 if (*w <= 0)
13589 *w = font->max_bounds.width;
13593 /* Compute the smallest character width and smallest font height over
13594 all fonts available on frame F. Set the members smallest_char_width
13595 and smallest_font_height in F's x_display_info structure to
13596 the values computed. Value is non-zero if smallest_font_height or
13597 smallest_char_width become smaller than they were before. */
13599 static int
13600 x_compute_min_glyph_bounds (f)
13601 struct frame *f;
13603 int i;
13604 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13605 XFontStruct *font;
13606 int old_width = dpyinfo->smallest_char_width;
13607 int old_height = dpyinfo->smallest_font_height;
13609 dpyinfo->smallest_font_height = 100000;
13610 dpyinfo->smallest_char_width = 100000;
13612 for (i = 0; i < dpyinfo->n_fonts; ++i)
13613 if (dpyinfo->font_table[i].name)
13615 struct font_info *fontp = dpyinfo->font_table + i;
13616 int w, h;
13618 font = (XFontStruct *) fontp->font;
13619 xassert (font != (XFontStruct *) ~0);
13620 x_font_min_bounds (font, &w, &h);
13622 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
13623 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
13626 xassert (dpyinfo->smallest_char_width > 0
13627 && dpyinfo->smallest_font_height > 0);
13629 return (dpyinfo->n_fonts == 1
13630 || dpyinfo->smallest_char_width < old_width
13631 || dpyinfo->smallest_font_height < old_height);
13635 /* Load font named FONTNAME of the size SIZE for frame F, and return a
13636 pointer to the structure font_info while allocating it dynamically.
13637 If SIZE is 0, load any size of font.
13638 If loading is failed, return NULL. */
13640 struct font_info *
13641 x_load_font (f, fontname, size)
13642 struct frame *f;
13643 register char *fontname;
13644 int size;
13646 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13647 Lisp_Object font_names;
13648 int count;
13650 /* Get a list of all the fonts that match this name. Once we
13651 have a list of matching fonts, we compare them against the fonts
13652 we already have by comparing names. */
13653 font_names = x_list_fonts (f, build_string (fontname), size, 1);
13655 if (!NILP (font_names))
13657 Lisp_Object tail;
13658 int i;
13660 for (i = 0; i < dpyinfo->n_fonts; i++)
13661 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
13662 if (dpyinfo->font_table[i].name
13663 && (!strcmp (dpyinfo->font_table[i].name,
13664 XSTRING (XCAR (tail))->data)
13665 || !strcmp (dpyinfo->font_table[i].full_name,
13666 XSTRING (XCAR (tail))->data)))
13667 return (dpyinfo->font_table + i);
13670 /* Load the font and add it to the table. */
13672 char *full_name;
13673 XFontStruct *font;
13674 struct font_info *fontp;
13675 unsigned long value;
13676 int i;
13678 /* If we have found fonts by x_list_font, load one of them. If
13679 not, we still try to load a font by the name given as FONTNAME
13680 because XListFonts (called in x_list_font) of some X server has
13681 a bug of not finding a font even if the font surely exists and
13682 is loadable by XLoadQueryFont. */
13683 if (size > 0 && !NILP (font_names))
13684 fontname = (char *) XSTRING (XCAR (font_names))->data;
13686 BLOCK_INPUT;
13687 count = x_catch_errors (FRAME_X_DISPLAY (f));
13688 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
13689 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
13691 /* This error is perhaps due to insufficient memory on X
13692 server. Let's just ignore it. */
13693 font = NULL;
13694 x_clear_errors (FRAME_X_DISPLAY (f));
13696 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
13697 UNBLOCK_INPUT;
13698 if (!font)
13699 return NULL;
13701 /* Find a free slot in the font table. */
13702 for (i = 0; i < dpyinfo->n_fonts; ++i)
13703 if (dpyinfo->font_table[i].name == NULL)
13704 break;
13706 /* If no free slot found, maybe enlarge the font table. */
13707 if (i == dpyinfo->n_fonts
13708 && dpyinfo->n_fonts == dpyinfo->font_table_size)
13710 int sz;
13711 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
13712 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
13713 dpyinfo->font_table
13714 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
13717 fontp = dpyinfo->font_table + i;
13718 if (i == dpyinfo->n_fonts)
13719 ++dpyinfo->n_fonts;
13721 /* Now fill in the slots of *FONTP. */
13722 BLOCK_INPUT;
13723 fontp->font = font;
13724 fontp->font_idx = i;
13725 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
13726 bcopy (fontname, fontp->name, strlen (fontname) + 1);
13728 /* Try to get the full name of FONT. Put it in FULL_NAME. */
13729 full_name = 0;
13730 if (XGetFontProperty (font, XA_FONT, &value))
13732 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
13733 char *p = name;
13734 int dashes = 0;
13736 /* Count the number of dashes in the "full name".
13737 If it is too few, this isn't really the font's full name,
13738 so don't use it.
13739 In X11R4, the fonts did not come with their canonical names
13740 stored in them. */
13741 while (*p)
13743 if (*p == '-')
13744 dashes++;
13745 p++;
13748 if (dashes >= 13)
13750 full_name = (char *) xmalloc (p - name + 1);
13751 bcopy (name, full_name, p - name + 1);
13754 XFree (name);
13757 if (full_name != 0)
13758 fontp->full_name = full_name;
13759 else
13760 fontp->full_name = fontp->name;
13762 fontp->size = font->max_bounds.width;
13763 fontp->height = FONT_HEIGHT (font);
13765 if (NILP (font_names))
13767 /* We come here because of a bug of XListFonts mentioned at
13768 the head of this block. Let's store this information in
13769 the cache for x_list_fonts. */
13770 Lisp_Object lispy_name = build_string (fontname);
13771 Lisp_Object lispy_full_name = build_string (fontp->full_name);
13772 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
13773 Qnil);
13775 XCDR (dpyinfo->name_list_element)
13776 = Fcons (Fcons (key,
13777 Fcons (Fcons (lispy_full_name,
13778 make_number (fontp->size)),
13779 Qnil)),
13780 XCDR (dpyinfo->name_list_element));
13781 if (full_name)
13783 key = Fcons (Fcons (lispy_full_name, make_number (256)),
13784 Qnil);
13785 XCDR (dpyinfo->name_list_element)
13786 = Fcons (Fcons (key,
13787 Fcons (Fcons (lispy_full_name,
13788 make_number (fontp->size)),
13789 Qnil)),
13790 XCDR (dpyinfo->name_list_element));
13794 /* The slot `encoding' specifies how to map a character
13795 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
13796 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
13797 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
13798 2:0xA020..0xFF7F). For the moment, we don't know which charset
13799 uses this font. So, we set information in fontp->encoding[1]
13800 which is never used by any charset. If mapping can't be
13801 decided, set FONT_ENCODING_NOT_DECIDED. */
13802 fontp->encoding[1]
13803 = (font->max_byte1 == 0
13804 /* 1-byte font */
13805 ? (font->min_char_or_byte2 < 0x80
13806 ? (font->max_char_or_byte2 < 0x80
13807 ? 0 /* 0x20..0x7F */
13808 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
13809 : 1) /* 0xA0..0xFF */
13810 /* 2-byte font */
13811 : (font->min_byte1 < 0x80
13812 ? (font->max_byte1 < 0x80
13813 ? (font->min_char_or_byte2 < 0x80
13814 ? (font->max_char_or_byte2 < 0x80
13815 ? 0 /* 0x2020..0x7F7F */
13816 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
13817 : 3) /* 0x20A0..0x7FFF */
13818 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
13819 : (font->min_char_or_byte2 < 0x80
13820 ? (font->max_char_or_byte2 < 0x80
13821 ? 2 /* 0xA020..0xFF7F */
13822 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
13823 : 1))); /* 0xA0A0..0xFFFF */
13825 fontp->baseline_offset
13826 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
13827 ? (long) value : 0);
13828 fontp->relative_compose
13829 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
13830 ? (long) value : 0);
13831 fontp->default_ascent
13832 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
13833 ? (long) value : 0);
13835 /* Set global flag fonts_changed_p to non-zero if the font loaded
13836 has a character with a smaller width than any other character
13837 before, or if the font loaded has a smalle>r height than any
13838 other font loaded before. If this happens, it will make a
13839 glyph matrix reallocation necessary. */
13840 fonts_changed_p = x_compute_min_glyph_bounds (f);
13841 UNBLOCK_INPUT;
13842 return fontp;
13847 /* Return a pointer to struct font_info of a font named FONTNAME for
13848 frame F. If no such font is loaded, return NULL. */
13850 struct font_info *
13851 x_query_font (f, fontname)
13852 struct frame *f;
13853 register char *fontname;
13855 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13856 int i;
13858 for (i = 0; i < dpyinfo->n_fonts; i++)
13859 if (dpyinfo->font_table[i].name
13860 && (!strcmp (dpyinfo->font_table[i].name, fontname)
13861 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
13862 return (dpyinfo->font_table + i);
13863 return NULL;
13867 /* Find a CCL program for a font specified by FONTP, and set the member
13868 `encoder' of the structure. */
13870 void
13871 x_find_ccl_program (fontp)
13872 struct font_info *fontp;
13874 Lisp_Object list, elt;
13876 elt = Qnil;
13877 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
13879 elt = XCAR (list);
13880 if (CONSP (elt)
13881 && STRINGP (XCAR (elt))
13882 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
13883 >= 0)
13884 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
13885 >= 0)))
13886 break;
13889 if (! NILP (list))
13891 struct ccl_program *ccl
13892 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
13894 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
13895 xfree (ccl);
13896 else
13897 fontp->font_encoder = ccl;
13903 /***********************************************************************
13904 Initialization
13905 ***********************************************************************/
13907 #ifdef USE_X_TOOLKIT
13908 static XrmOptionDescRec emacs_options[] = {
13909 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13910 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13912 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13913 XrmoptionSepArg, NULL},
13914 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13916 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13917 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13918 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13919 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13920 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13921 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13922 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13924 #endif /* USE_X_TOOLKIT */
13926 static int x_initialized;
13928 #ifdef MULTI_KBOARD
13929 /* Test whether two display-name strings agree up to the dot that separates
13930 the screen number from the server number. */
13931 static int
13932 same_x_server (name1, name2)
13933 char *name1, *name2;
13935 int seen_colon = 0;
13936 unsigned char *system_name = XSTRING (Vsystem_name)->data;
13937 int system_name_length = strlen (system_name);
13938 int length_until_period = 0;
13940 while (system_name[length_until_period] != 0
13941 && system_name[length_until_period] != '.')
13942 length_until_period++;
13944 /* Treat `unix' like an empty host name. */
13945 if (! strncmp (name1, "unix:", 5))
13946 name1 += 4;
13947 if (! strncmp (name2, "unix:", 5))
13948 name2 += 4;
13949 /* Treat this host's name like an empty host name. */
13950 if (! strncmp (name1, system_name, system_name_length)
13951 && name1[system_name_length] == ':')
13952 name1 += system_name_length;
13953 if (! strncmp (name2, system_name, system_name_length)
13954 && name2[system_name_length] == ':')
13955 name2 += system_name_length;
13956 /* Treat this host's domainless name like an empty host name. */
13957 if (! strncmp (name1, system_name, length_until_period)
13958 && name1[length_until_period] == ':')
13959 name1 += length_until_period;
13960 if (! strncmp (name2, system_name, length_until_period)
13961 && name2[length_until_period] == ':')
13962 name2 += length_until_period;
13964 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13966 if (*name1 == ':')
13967 seen_colon++;
13968 if (seen_colon && *name1 == '.')
13969 return 1;
13971 return (seen_colon
13972 && (*name1 == '.' || *name1 == '\0')
13973 && (*name2 == '.' || *name2 == '\0'));
13975 #endif
13977 struct x_display_info *
13978 x_term_init (display_name, xrm_option, resource_name)
13979 Lisp_Object display_name;
13980 char *xrm_option;
13981 char *resource_name;
13983 int connection;
13984 Display *dpy;
13985 struct x_display_info *dpyinfo;
13986 XrmDatabase xrdb;
13988 BLOCK_INPUT;
13990 if (!x_initialized)
13992 x_initialize ();
13993 x_initialized = 1;
13996 #ifdef USE_X_TOOLKIT
13997 /* weiner@footloose.sps.mot.com reports that this causes
13998 errors with X11R5:
13999 X protocol error: BadAtom (invalid Atom parameter)
14000 on protocol request 18skiloaf.
14001 So let's not use it until R6. */
14002 #ifdef HAVE_X11XTR6
14003 XtSetLanguageProc (NULL, NULL, NULL);
14004 #endif
14007 int argc = 0;
14008 char *argv[3];
14010 argv[0] = "";
14011 argc = 1;
14012 if (xrm_option)
14014 argv[argc++] = "-xrm";
14015 argv[argc++] = xrm_option;
14017 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
14018 resource_name, EMACS_CLASS,
14019 emacs_options, XtNumber (emacs_options),
14020 &argc, argv);
14022 #ifdef HAVE_X11XTR6
14023 /* I think this is to compensate for XtSetLanguageProc. */
14024 fixup_locale ();
14025 #endif
14028 #else /* not USE_X_TOOLKIT */
14029 #ifdef HAVE_X11R5
14030 XSetLocaleModifiers ("");
14031 #endif
14032 dpy = XOpenDisplay (XSTRING (display_name)->data);
14033 #endif /* not USE_X_TOOLKIT */
14035 /* Detect failure. */
14036 if (dpy == 0)
14038 UNBLOCK_INPUT;
14039 return 0;
14042 /* We have definitely succeeded. Record the new connection. */
14044 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
14045 bzero (dpyinfo, sizeof *dpyinfo);
14047 #ifdef MULTI_KBOARD
14049 struct x_display_info *share;
14050 Lisp_Object tail;
14052 for (share = x_display_list, tail = x_display_name_list; share;
14053 share = share->next, tail = XCDR (tail))
14054 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
14055 XSTRING (display_name)->data))
14056 break;
14057 if (share)
14058 dpyinfo->kboard = share->kboard;
14059 else
14061 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
14062 init_kboard (dpyinfo->kboard);
14063 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
14065 char *vendor = ServerVendor (dpy);
14066 UNBLOCK_INPUT;
14067 dpyinfo->kboard->Vsystem_key_alist
14068 = call1 (Qvendor_specific_keysyms,
14069 build_string (vendor ? vendor : ""));
14070 BLOCK_INPUT;
14073 dpyinfo->kboard->next_kboard = all_kboards;
14074 all_kboards = dpyinfo->kboard;
14075 /* Don't let the initial kboard remain current longer than necessary.
14076 That would cause problems if a file loaded on startup tries to
14077 prompt in the mini-buffer. */
14078 if (current_kboard == initial_kboard)
14079 current_kboard = dpyinfo->kboard;
14081 dpyinfo->kboard->reference_count++;
14083 #endif
14085 /* Put this display on the chain. */
14086 dpyinfo->next = x_display_list;
14087 x_display_list = dpyinfo;
14089 /* Put it on x_display_name_list as well, to keep them parallel. */
14090 x_display_name_list = Fcons (Fcons (display_name, Qnil),
14091 x_display_name_list);
14092 dpyinfo->name_list_element = XCAR (x_display_name_list);
14094 dpyinfo->display = dpy;
14096 #if 0
14097 XSetAfterFunction (x_current_display, x_trace_wire);
14098 #endif /* ! 0 */
14100 dpyinfo->x_id_name
14101 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
14102 + STRING_BYTES (XSTRING (Vsystem_name))
14103 + 2);
14104 sprintf (dpyinfo->x_id_name, "%s@%s",
14105 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
14107 /* Figure out which modifier bits mean what. */
14108 x_find_modifier_meanings (dpyinfo);
14110 /* Get the scroll bar cursor. */
14111 dpyinfo->vertical_scroll_bar_cursor
14112 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
14114 xrdb = x_load_resources (dpyinfo->display, xrm_option,
14115 resource_name, EMACS_CLASS);
14116 #ifdef HAVE_XRMSETDATABASE
14117 XrmSetDatabase (dpyinfo->display, xrdb);
14118 #else
14119 dpyinfo->display->db = xrdb;
14120 #endif
14121 /* Put the rdb where we can find it in a way that works on
14122 all versions. */
14123 dpyinfo->xrdb = xrdb;
14125 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
14126 DefaultScreen (dpyinfo->display));
14127 select_visual (dpyinfo);
14128 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
14129 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
14130 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
14131 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
14132 dpyinfo->grabbed = 0;
14133 dpyinfo->reference_count = 0;
14134 dpyinfo->icon_bitmap_id = -1;
14135 dpyinfo->font_table = NULL;
14136 dpyinfo->n_fonts = 0;
14137 dpyinfo->font_table_size = 0;
14138 dpyinfo->bitmaps = 0;
14139 dpyinfo->bitmaps_size = 0;
14140 dpyinfo->bitmaps_last = 0;
14141 dpyinfo->scratch_cursor_gc = 0;
14142 dpyinfo->mouse_face_mouse_frame = 0;
14143 dpyinfo->mouse_face_deferred_gc = 0;
14144 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
14145 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
14146 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
14147 dpyinfo->mouse_face_window = Qnil;
14148 dpyinfo->mouse_face_overlay = Qnil;
14149 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
14150 dpyinfo->mouse_face_defer = 0;
14151 dpyinfo->x_focus_frame = 0;
14152 dpyinfo->x_focus_event_frame = 0;
14153 dpyinfo->x_highlight_frame = 0;
14154 dpyinfo->image_cache = make_image_cache ();
14156 /* See if a private colormap is requested. */
14157 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
14159 if (dpyinfo->visual->class == PseudoColor)
14161 Lisp_Object value;
14162 value = display_x_get_resource (dpyinfo,
14163 build_string ("privateColormap"),
14164 build_string ("PrivateColormap"),
14165 Qnil, Qnil);
14166 if (STRINGP (value)
14167 && (!strcmp (XSTRING (value)->data, "true")
14168 || !strcmp (XSTRING (value)->data, "on")))
14169 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
14172 else
14173 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
14174 dpyinfo->visual, AllocNone);
14177 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
14178 double pixels = DisplayHeight (dpyinfo->display, screen_number);
14179 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
14180 dpyinfo->resy = pixels * 25.4 / mm;
14181 pixels = DisplayWidth (dpyinfo->display, screen_number);
14182 mm = DisplayWidthMM (dpyinfo->display, screen_number);
14183 dpyinfo->resx = pixels * 25.4 / mm;
14186 dpyinfo->Xatom_wm_protocols
14187 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
14188 dpyinfo->Xatom_wm_take_focus
14189 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
14190 dpyinfo->Xatom_wm_save_yourself
14191 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
14192 dpyinfo->Xatom_wm_delete_window
14193 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
14194 dpyinfo->Xatom_wm_change_state
14195 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
14196 dpyinfo->Xatom_wm_configure_denied
14197 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
14198 dpyinfo->Xatom_wm_window_moved
14199 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
14200 dpyinfo->Xatom_editres
14201 = XInternAtom (dpyinfo->display, "Editres", False);
14202 dpyinfo->Xatom_CLIPBOARD
14203 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
14204 dpyinfo->Xatom_TIMESTAMP
14205 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
14206 dpyinfo->Xatom_TEXT
14207 = XInternAtom (dpyinfo->display, "TEXT", False);
14208 dpyinfo->Xatom_COMPOUND_TEXT
14209 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
14210 dpyinfo->Xatom_DELETE
14211 = XInternAtom (dpyinfo->display, "DELETE", False);
14212 dpyinfo->Xatom_MULTIPLE
14213 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
14214 dpyinfo->Xatom_INCR
14215 = XInternAtom (dpyinfo->display, "INCR", False);
14216 dpyinfo->Xatom_EMACS_TMP
14217 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
14218 dpyinfo->Xatom_TARGETS
14219 = XInternAtom (dpyinfo->display, "TARGETS", False);
14220 dpyinfo->Xatom_NULL
14221 = XInternAtom (dpyinfo->display, "NULL", False);
14222 dpyinfo->Xatom_ATOM_PAIR
14223 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
14224 /* For properties of font. */
14225 dpyinfo->Xatom_PIXEL_SIZE
14226 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
14227 dpyinfo->Xatom_MULE_BASELINE_OFFSET
14228 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
14229 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
14230 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
14231 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
14232 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
14234 /* Ghostscript support. */
14235 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
14236 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
14238 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
14239 False);
14241 dpyinfo->cut_buffers_initialized = 0;
14243 connection = ConnectionNumber (dpyinfo->display);
14244 dpyinfo->connection = connection;
14247 char null_bits[1];
14249 null_bits[0] = 0x00;
14251 dpyinfo->null_pixel
14252 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14253 null_bits, 1, 1, (long) 0, (long) 0,
14258 extern int gray_bitmap_width, gray_bitmap_height;
14259 extern char *gray_bitmap_bits;
14260 dpyinfo->gray
14261 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14262 gray_bitmap_bits,
14263 gray_bitmap_width, gray_bitmap_height,
14264 (unsigned long) 1, (unsigned long) 0, 1);
14267 #ifdef HAVE_X_I18N
14268 xim_initialize (dpyinfo, resource_name);
14269 #endif
14271 #ifdef subprocesses
14272 /* This is only needed for distinguishing keyboard and process input. */
14273 if (connection != 0)
14274 add_keyboard_wait_descriptor (connection);
14275 #endif
14277 #ifndef F_SETOWN_BUG
14278 #ifdef F_SETOWN
14279 #ifdef F_SETOWN_SOCK_NEG
14280 /* stdin is a socket here */
14281 fcntl (connection, F_SETOWN, -getpid ());
14282 #else /* ! defined (F_SETOWN_SOCK_NEG) */
14283 fcntl (connection, F_SETOWN, getpid ());
14284 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
14285 #endif /* ! defined (F_SETOWN) */
14286 #endif /* F_SETOWN_BUG */
14288 #ifdef SIGIO
14289 if (interrupt_input)
14290 init_sigio (connection);
14291 #endif /* ! defined (SIGIO) */
14293 #ifdef USE_LUCID
14294 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
14295 /* Make sure that we have a valid font for dialog boxes
14296 so that Xt does not crash. */
14298 Display *dpy = dpyinfo->display;
14299 XrmValue d, fr, to;
14300 Font font;
14301 int count;
14303 d.addr = (XPointer)&dpy;
14304 d.size = sizeof (Display *);
14305 fr.addr = XtDefaultFont;
14306 fr.size = sizeof (XtDefaultFont);
14307 to.size = sizeof (Font *);
14308 to.addr = (XPointer)&font;
14309 count = x_catch_errors (dpy);
14310 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
14311 abort ();
14312 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
14313 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
14314 x_uncatch_errors (dpy, count);
14316 #endif
14317 #endif
14319 /* See if we should run in synchronous mode. This is useful
14320 for debugging X code. */
14322 Lisp_Object value;
14323 value = display_x_get_resource (dpyinfo,
14324 build_string ("synchronous"),
14325 build_string ("Synchronous"),
14326 Qnil, Qnil);
14327 if (STRINGP (value)
14328 && (!strcmp (XSTRING (value)->data, "true")
14329 || !strcmp (XSTRING (value)->data, "on")))
14330 XSynchronize (dpyinfo->display, True);
14333 UNBLOCK_INPUT;
14335 return dpyinfo;
14338 /* Get rid of display DPYINFO, assuming all frames are already gone,
14339 and without sending any more commands to the X server. */
14341 void
14342 x_delete_display (dpyinfo)
14343 struct x_display_info *dpyinfo;
14345 delete_keyboard_wait_descriptor (dpyinfo->connection);
14347 /* Discard this display from x_display_name_list and x_display_list.
14348 We can't use Fdelq because that can quit. */
14349 if (! NILP (x_display_name_list)
14350 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
14351 x_display_name_list = XCDR (x_display_name_list);
14352 else
14354 Lisp_Object tail;
14356 tail = x_display_name_list;
14357 while (CONSP (tail) && CONSP (XCDR (tail)))
14359 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
14361 XCDR (tail) = XCDR (XCDR (tail));
14362 break;
14364 tail = XCDR (tail);
14368 if (next_noop_dpyinfo == dpyinfo)
14369 next_noop_dpyinfo = dpyinfo->next;
14371 if (x_display_list == dpyinfo)
14372 x_display_list = dpyinfo->next;
14373 else
14375 struct x_display_info *tail;
14377 for (tail = x_display_list; tail; tail = tail->next)
14378 if (tail->next == dpyinfo)
14379 tail->next = tail->next->next;
14382 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
14383 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
14384 XrmDestroyDatabase (dpyinfo->xrdb);
14385 #endif
14386 #endif
14387 #ifdef MULTI_KBOARD
14388 if (--dpyinfo->kboard->reference_count == 0)
14389 delete_kboard (dpyinfo->kboard);
14390 #endif
14391 #ifdef HAVE_X_I18N
14392 if (dpyinfo->xim)
14393 xim_close_dpy (dpyinfo);
14394 #endif
14396 xfree (dpyinfo->font_table);
14397 xfree (dpyinfo->x_id_name);
14398 xfree (dpyinfo->color_cells);
14399 xfree (dpyinfo);
14403 /* Set up use of X before we make the first connection. */
14405 static struct redisplay_interface x_redisplay_interface =
14407 x_produce_glyphs,
14408 x_write_glyphs,
14409 x_insert_glyphs,
14410 x_clear_end_of_line,
14411 x_scroll_run,
14412 x_after_update_window_line,
14413 x_update_window_begin,
14414 x_update_window_end,
14415 XTcursor_to,
14416 x_flush,
14417 x_clear_mouse_face,
14418 x_get_glyph_overhangs,
14419 x_fix_overlapping_area
14422 void
14423 x_initialize ()
14425 rif = &x_redisplay_interface;
14427 clear_frame_hook = x_clear_frame;
14428 ins_del_lines_hook = x_ins_del_lines;
14429 change_line_highlight_hook = x_change_line_highlight;
14430 delete_glyphs_hook = x_delete_glyphs;
14431 ring_bell_hook = XTring_bell;
14432 reset_terminal_modes_hook = XTreset_terminal_modes;
14433 set_terminal_modes_hook = XTset_terminal_modes;
14434 update_begin_hook = x_update_begin;
14435 update_end_hook = x_update_end;
14436 set_terminal_window_hook = XTset_terminal_window;
14437 read_socket_hook = XTread_socket;
14438 frame_up_to_date_hook = XTframe_up_to_date;
14439 reassert_line_highlight_hook = XTreassert_line_highlight;
14440 mouse_position_hook = XTmouse_position;
14441 frame_rehighlight_hook = XTframe_rehighlight;
14442 frame_raise_lower_hook = XTframe_raise_lower;
14443 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
14444 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
14445 redeem_scroll_bar_hook = XTredeem_scroll_bar;
14446 judge_scroll_bars_hook = XTjudge_scroll_bars;
14447 estimate_mode_line_height_hook = x_estimate_mode_line_height;
14449 scroll_region_ok = 1; /* we'll scroll partial frames */
14450 char_ins_del_ok = 1;
14451 line_ins_del_ok = 1; /* we'll just blt 'em */
14452 fast_clear_end_of_line = 1; /* X does this well */
14453 memory_below_frame = 0; /* we don't remember what scrolls
14454 off the bottom */
14455 baud_rate = 19200;
14457 x_noop_count = 0;
14458 last_tool_bar_item = -1;
14459 any_help_event_p = 0;
14461 /* Try to use interrupt input; if we can't, then start polling. */
14462 Fset_input_mode (Qt, Qnil, Qt, Qnil);
14464 #ifdef USE_X_TOOLKIT
14465 XtToolkitInitialize ();
14467 Xt_app_con = XtCreateApplicationContext ();
14469 /* Register a converter from strings to pixels, which uses
14470 Emacs' color allocation infrastructure. */
14471 XtAppSetTypeConverter (Xt_app_con,
14472 XtRString, XtRPixel, cvt_string_to_pixel,
14473 cvt_string_to_pixel_args,
14474 XtNumber (cvt_string_to_pixel_args),
14475 XtCacheByDisplay, cvt_pixel_dtor);
14477 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
14479 /* Install an asynchronous timer that processes Xt timeout events
14480 every 0.1s. This is necessary because some widget sets use
14481 timeouts internally, for example the LessTif menu bar, or the
14482 Xaw3d scroll bar. When Xt timouts aren't processed, these
14483 widgets don't behave normally. */
14485 EMACS_TIME interval;
14486 EMACS_SET_SECS_USECS (interval, 0, 100000);
14487 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
14489 #endif
14491 #ifdef USE_TOOLKIT_SCROLL_BARS
14492 xaw3d_arrow_scroll = False;
14493 xaw3d_pick_top = True;
14494 #endif
14496 /* Note that there is no real way portable across R3/R4 to get the
14497 original error handler. */
14498 XSetErrorHandler (x_error_handler);
14499 XSetIOErrorHandler (x_io_error_quitter);
14501 /* Disable Window Change signals; they are handled by X events. */
14502 #ifdef SIGWINCH
14503 signal (SIGWINCH, SIG_DFL);
14504 #endif /* ! defined (SIGWINCH) */
14506 signal (SIGPIPE, x_connection_signal);
14510 void
14511 syms_of_xterm ()
14513 staticpro (&x_error_message_string);
14514 x_error_message_string = Qnil;
14516 staticpro (&x_display_name_list);
14517 x_display_name_list = Qnil;
14519 staticpro (&last_mouse_scroll_bar);
14520 last_mouse_scroll_bar = Qnil;
14522 staticpro (&Qvendor_specific_keysyms);
14523 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
14525 staticpro (&last_mouse_press_frame);
14526 last_mouse_press_frame = Qnil;
14528 help_echo = Qnil;
14529 staticpro (&help_echo);
14530 help_echo_object = Qnil;
14531 staticpro (&help_echo_object);
14532 help_echo_window = Qnil;
14533 staticpro (&help_echo_window);
14534 previous_help_echo = Qnil;
14535 staticpro (&previous_help_echo);
14536 help_echo_pos = -1;
14538 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
14539 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
14540 For example, if a block cursor is over a tab, it will be drawn as\n\
14541 wide as that tab on the display.");
14542 x_stretch_cursor_p = 0;
14544 DEFVAR_BOOL ("x-use-underline-position-properties",
14545 &x_use_underline_position_properties,
14546 "*Non-nil means make use of UNDERLINE_POSITION font properties.\n\
14547 Nil means ignore them. If you encounter fonts with bogus\n\
14548 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior\n\
14549 to 4.1, set this to nil.");
14550 x_use_underline_position_properties = 1;
14552 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
14553 "What X toolkit scroll bars Emacs uses.\n\
14554 A value of nil means Emacs doesn't use X toolkit scroll bars.\n\
14555 Otherwise, value is a symbol describing the X toolkit.");
14556 #ifdef USE_TOOLKIT_SCROLL_BARS
14557 #ifdef USE_MOTIF
14558 Vx_toolkit_scroll_bars = intern ("motif");
14559 #elif defined HAVE_XAW3D
14560 Vx_toolkit_scroll_bars = intern ("xaw3d");
14561 #else
14562 Vx_toolkit_scroll_bars = intern ("xaw");
14563 #endif
14564 #else
14565 Vx_toolkit_scroll_bars = Qnil;
14566 #endif
14568 staticpro (&last_mouse_motion_frame);
14569 last_mouse_motion_frame = Qnil;
14572 #endif /* not HAVE_X_WINDOWS */