]> code.delx.au - gnu-emacs/blob - src/xterm.c
(note_mouse_movement): Put code for
[gnu-emacs] / src / xterm.c
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.
4
5 This file is part of GNU Emacs.
6
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.
11
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.
16
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. */
21
22 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
23 /* Xt features made by Fred Pierresteguy. */
24
25 #include <config.h>
26
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>
30
31 #include <stdio.h>
32
33 #ifdef HAVE_X_WINDOWS
34
35 #include "lisp.h"
36 #include "blockinput.h"
37
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"
41
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>
46
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 */
52
53 #ifdef BSD_SYSTEM
54 #include <sys/ioctl.h>
55 #endif /* ! defined (BSD_SYSTEM) */
56
57 #include "systty.h"
58 #include "systime.h"
59
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> */
69
70 #include "charset.h"
71 #include "coding.h"
72 #include "ccl.h"
73 #include "frame.h"
74 #include "dispextern.h"
75 #include "fontset.h"
76 #include "termhooks.h"
77 #include "termopts.h"
78 #include "termchar.h"
79 #include "gnu.h"
80 #include "disptab.h"
81 #include "buffer.h"
82 #include "window.h"
83 #include "keyboard.h"
84 #include "intervals.h"
85 #include "process.h"
86 #include "atimer.h"
87 #include "keymap.h"
88
89 #ifdef USE_X_TOOLKIT
90 #include <X11/Shell.h>
91 #endif
92
93 #ifdef HAVE_SYS_TIME_H
94 #include <sys/time.h>
95 #endif
96 #ifdef HAVE_UNISTD_H
97 #include <unistd.h>
98 #endif
99
100 #ifdef USE_LUCID
101 extern int xlwmenu_window_p (Widget w, Window window);
102 extern void xlwmenu_redisplay P_ ((Widget));
103 #endif
104
105 #ifdef USE_X_TOOLKIT
106
107 extern void free_frame_menubar P_ ((struct frame *));
108 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
109 int));
110
111 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
112 #define HACK_EDITRES
113 extern void _XEditResCheckMessages ();
114 #endif /* not NO_EDITRES */
115
116 /* Include toolkit specific headers for the scroll bar widget. */
117
118 #ifdef USE_TOOLKIT_SCROLL_BARS
119 #if defined USE_MOTIF
120 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
121 #include <Xm/ScrollBar.h>
122 #else /* !USE_MOTIF i.e. use Xaw */
123
124 #ifdef HAVE_XAW3D
125 #include <X11/Xaw3d/Simple.h>
126 #include <X11/Xaw3d/Scrollbar.h>
127 #define ARROW_SCROLLBAR
128 #include <X11/Xaw3d/ScrollbarP.h>
129 #else /* !HAVE_XAW3D */
130 #include <X11/Xaw/Simple.h>
131 #include <X11/Xaw/Scrollbar.h>
132 #endif /* !HAVE_XAW3D */
133 #ifndef XtNpickTop
134 #define XtNpickTop "pickTop"
135 #endif /* !XtNpickTop */
136 #endif /* !USE_MOTIF */
137 #endif /* USE_TOOLKIT_SCROLL_BARS */
138
139 #endif /* USE_X_TOOLKIT */
140
141 #ifndef USE_X_TOOLKIT
142 #define x_any_window_to_frame x_window_to_frame
143 #define x_top_window_to_frame x_window_to_frame
144 #endif
145
146 #ifdef USE_X_TOOLKIT
147 #include "widget.h"
148 #ifndef XtNinitialState
149 #define XtNinitialState "initialState"
150 #endif
151 #endif
152
153 #define abs(x) ((x) < 0 ? -(x) : (x))
154
155 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
156
157 \f
158 /* Fringe bitmaps. */
159
160 enum fringe_bitmap_type
161 {
162 NO_FRINGE_BITMAP,
163 LEFT_TRUNCATION_BITMAP,
164 RIGHT_TRUNCATION_BITMAP,
165 OVERLAY_ARROW_BITMAP,
166 CONTINUED_LINE_BITMAP,
167 CONTINUATION_LINE_BITMAP,
168 ZV_LINE_BITMAP
169 };
170
171 /* Bitmap drawn to indicate lines not displaying text if
172 `indicate-empty-lines' is non-nil. */
173
174 #define zv_width 8
175 #define zv_height 72
176 #define zv_period 3
177 static unsigned char zv_bits[] = {
178 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
179 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
180 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
181 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
182 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
183 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
184 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
185 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
186
187 /* An arrow like this: `<-'. */
188
189 #define left_width 8
190 #define left_height 8
191 static unsigned char left_bits[] = {
192 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
193
194 /* Right truncation arrow bitmap `->'. */
195
196 #define right_width 8
197 #define right_height 8
198 static unsigned char right_bits[] = {
199 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
200
201 /* Marker for continued lines. */
202
203 #define continued_width 8
204 #define continued_height 8
205 static unsigned char continued_bits[] = {
206 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
207
208 /* Marker for continuation lines. */
209
210 #define continuation_width 8
211 #define continuation_height 8
212 static unsigned char continuation_bits[] = {
213 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
214
215 /* Overlay arrow bitmap. */
216
217 #if 0
218 /* A bomb. */
219 #define ov_width 8
220 #define ov_height 8
221 static unsigned char ov_bits[] = {
222 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
223 #else
224 /* A triangular arrow. */
225 #define ov_width 8
226 #define ov_height 8
227 static unsigned char ov_bits[] = {
228 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
229
230 #endif
231
232 extern Lisp_Object Qhelp_echo;
233
234 \f
235 /* Non-nil means Emacs uses toolkit scroll bars. */
236
237 Lisp_Object Vx_toolkit_scroll_bars;
238
239 /* If a string, XTread_socket generates an event to display that string.
240 (The display is done in read_char.) */
241
242 static Lisp_Object help_echo;
243 static Lisp_Object help_echo_window;
244 static Lisp_Object help_echo_object;
245 static int help_echo_pos;
246
247 /* Temporary variable for XTread_socket. */
248
249 static Lisp_Object previous_help_echo;
250
251 /* Non-zero means that a HELP_EVENT has been generated since Emacs
252 start. */
253
254 static int any_help_event_p;
255
256 /* Non-zero means autoselect window with the mouse cursor. */
257
258 int x_autoselect_window_p;
259
260 /* Non-zero means draw block and hollow cursor as wide as the glyph
261 under it. For example, if a block cursor is over a tab, it will be
262 drawn as wide as that tab on the display. */
263
264 int x_stretch_cursor_p;
265
266 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
267
268 int x_use_underline_position_properties;
269
270 /* This is a chain of structures for all the X displays currently in
271 use. */
272
273 struct x_display_info *x_display_list;
274
275 /* This is a list of cons cells, each of the form (NAME
276 . FONT-LIST-CACHE), one for each element of x_display_list and in
277 the same order. NAME is the name of the frame. FONT-LIST-CACHE
278 records previous values returned by x-list-fonts. */
279
280 Lisp_Object x_display_name_list;
281
282 /* Frame being updated by update_frame. This is declared in term.c.
283 This is set by update_begin and looked at by all the XT functions.
284 It is zero while not inside an update. In that case, the XT
285 functions assume that `selected_frame' is the frame to apply to. */
286
287 extern struct frame *updating_frame;
288
289 /* This is a frame waiting to be auto-raised, within XTread_socket. */
290
291 struct frame *pending_autoraise_frame;
292
293 #ifdef USE_X_TOOLKIT
294 /* The application context for Xt use. */
295 XtAppContext Xt_app_con;
296 static String Xt_default_resources[] = {0};
297 #endif /* USE_X_TOOLKIT */
298
299 /* Nominal cursor position -- where to draw output.
300 HPOS and VPOS are window relative glyph matrix coordinates.
301 X and Y are window relative pixel coordinates. */
302
303 struct cursor_pos output_cursor;
304
305 /* Non-zero means user is interacting with a toolkit scroll bar. */
306
307 static int toolkit_scroll_bar_interaction;
308
309 /* Mouse movement.
310
311 Formerly, we used PointerMotionHintMask (in standard_event_mask)
312 so that we would have to call XQueryPointer after each MotionNotify
313 event to ask for another such event. However, this made mouse tracking
314 slow, and there was a bug that made it eventually stop.
315
316 Simply asking for MotionNotify all the time seems to work better.
317
318 In order to avoid asking for motion events and then throwing most
319 of them away or busy-polling the server for mouse positions, we ask
320 the server for pointer motion hints. This means that we get only
321 one event per group of mouse movements. "Groups" are delimited by
322 other kinds of events (focus changes and button clicks, for
323 example), or by XQueryPointer calls; when one of these happens, we
324 get another MotionNotify event the next time the mouse moves. This
325 is at least as efficient as getting motion events when mouse
326 tracking is on, and I suspect only negligibly worse when tracking
327 is off. */
328
329 /* Where the mouse was last time we reported a mouse event. */
330
331 FRAME_PTR last_mouse_frame;
332 static XRectangle last_mouse_glyph;
333 static Lisp_Object last_mouse_press_frame;
334
335 /* The scroll bar in which the last X motion event occurred.
336
337 If the last X motion event occurred in a scroll bar, we set this so
338 XTmouse_position can know whether to report a scroll bar motion or
339 an ordinary motion.
340
341 If the last X motion event didn't occur in a scroll bar, we set
342 this to Qnil, to tell XTmouse_position to return an ordinary motion
343 event. */
344
345 static Lisp_Object last_mouse_scroll_bar;
346
347 /* This is a hack. We would really prefer that XTmouse_position would
348 return the time associated with the position it returns, but there
349 doesn't seem to be any way to wrest the time-stamp from the server
350 along with the position query. So, we just keep track of the time
351 of the last movement we received, and return that in hopes that
352 it's somewhat accurate. */
353
354 static Time last_mouse_movement_time;
355
356 /* Incremented by XTread_socket whenever it really tries to read
357 events. */
358
359 #ifdef __STDC__
360 static int volatile input_signal_count;
361 #else
362 static int input_signal_count;
363 #endif
364
365 /* Used locally within XTread_socket. */
366
367 static int x_noop_count;
368
369 /* Initial values of argv and argc. */
370
371 extern char **initial_argv;
372 extern int initial_argc;
373
374 extern Lisp_Object Vcommand_line_args, Vsystem_name;
375
376 /* Tells if a window manager is present or not. */
377
378 extern Lisp_Object Vx_no_window_manager;
379
380 extern Lisp_Object Qface, Qmouse_face;
381
382 extern int errno;
383
384 /* A mask of extra modifier bits to put into every keyboard char. */
385
386 extern EMACS_INT extra_keyboard_modifiers;
387
388 /* The keysyms to use for the various modifiers. */
389
390 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
391 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
392
393 static Lisp_Object Qvendor_specific_keysyms;
394
395 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
396 extern Lisp_Object x_icon_type P_ ((struct frame *));
397
398
399 /* Enumeration for overriding/changing the face to use for drawing
400 glyphs in x_draw_glyphs. */
401
402 enum draw_glyphs_face
403 {
404 DRAW_NORMAL_TEXT,
405 DRAW_INVERSE_VIDEO,
406 DRAW_CURSOR,
407 DRAW_MOUSE_FACE,
408 DRAW_IMAGE_RAISED,
409 DRAW_IMAGE_SUNKEN
410 };
411
412 static int cursor_in_mouse_face_p P_ ((struct window *));
413 static int clear_mouse_face P_ ((struct x_display_info *));
414 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
415 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
416 static const XColor *x_color_cells P_ ((Display *, int *));
417 static void x_update_window_end P_ ((struct window *, int, int));
418 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
419 void x_delete_display P_ ((struct x_display_info *));
420 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
421 unsigned));
422 static int fast_find_position P_ ((struct window *, int, int *, int *,
423 int *, int *, Lisp_Object));
424 static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
425 int *, int *, int *, int *, int));
426 static void set_output_cursor P_ ((struct cursor_pos *));
427 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
428 int *, int *, int *, int));
429 static void note_mode_line_highlight P_ ((struct window *, int, int));
430 static void note_mouse_highlight P_ ((struct frame *, int, int));
431 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
432 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
433 static void show_mouse_face P_ ((struct x_display_info *,
434 enum draw_glyphs_face));
435 static int x_io_error_quitter P_ ((Display *));
436 int x_catch_errors P_ ((Display *));
437 void x_uncatch_errors P_ ((Display *, int));
438 void x_lower_frame P_ ((struct frame *));
439 void x_scroll_bar_clear P_ ((struct frame *));
440 int x_had_errors_p P_ ((Display *));
441 void x_wm_set_size_hint P_ ((struct frame *, long, int));
442 void x_raise_frame P_ ((struct frame *));
443 void x_set_window_size P_ ((struct frame *, int, int, int));
444 void x_wm_set_window_state P_ ((struct frame *, int));
445 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
446 void x_initialize P_ ((void));
447 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
448 static int x_compute_min_glyph_bounds P_ ((struct frame *));
449 static void x_draw_phys_cursor_glyph P_ ((struct window *,
450 struct glyph_row *,
451 enum draw_glyphs_face));
452 static void x_update_end P_ ((struct frame *));
453 static void XTframe_up_to_date P_ ((struct frame *));
454 static void XTset_terminal_modes P_ ((void));
455 static void XTreset_terminal_modes P_ ((void));
456 static void XTcursor_to P_ ((int, int, int, int));
457 static void x_write_glyphs P_ ((struct glyph *, int));
458 static void x_clear_end_of_line P_ ((int));
459 static void x_clear_frame P_ ((void));
460 static void x_clear_cursor P_ ((struct window *));
461 static void frame_highlight P_ ((struct frame *));
462 static void frame_unhighlight P_ ((struct frame *));
463 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
464 static void XTframe_rehighlight P_ ((struct frame *));
465 static void x_frame_rehighlight P_ ((struct x_display_info *));
466 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
467 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
468 static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
469 XRectangle *));
470 static void expose_frame P_ ((struct frame *, int, int, int, int));
471 static int expose_window_tree P_ ((struct window *, XRectangle *));
472 static int expose_window P_ ((struct window *, XRectangle *));
473 static void expose_area P_ ((struct window *, struct glyph_row *,
474 XRectangle *, enum glyph_row_area));
475 static int expose_line P_ ((struct window *, struct glyph_row *,
476 XRectangle *));
477 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
478 static void x_update_window_cursor P_ ((struct window *, int));
479 static void x_erase_phys_cursor P_ ((struct window *));
480 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
481 static void x_draw_fringe_bitmap P_ ((struct window *, struct glyph_row *,
482 enum fringe_bitmap_type, int left_p));
483
484 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
485 GC, int));
486 static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
487 static void x_draw_row_fringe_bitmaps P_ ((struct window *, struct glyph_row *));
488 static void notice_overwritten_cursor P_ ((struct window *, enum glyph_row_area,
489 int, int, int, int));
490 static void x_flush P_ ((struct frame *f));
491 static void x_update_begin P_ ((struct frame *));
492 static void x_update_window_begin P_ ((struct window *));
493 static void x_draw_vertical_border P_ ((struct window *));
494 static void x_after_update_window_line P_ ((struct glyph_row *));
495 static INLINE void take_vertical_position_into_account P_ ((struct it *));
496 static void x_produce_stretch_glyph P_ ((struct it *));
497 static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
498 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
499 enum scroll_bar_part *,
500 Lisp_Object *, Lisp_Object *,
501 unsigned long *));
502 static void x_check_fullscreen P_ ((struct frame *));
503 static void x_check_fullscreen_move P_ ((struct frame *));
504
505 /* Flush display of frame F, or of all frames if F is null. */
506
507 static void
508 x_flush (f)
509 struct frame *f;
510 {
511 BLOCK_INPUT;
512 if (f == NULL)
513 {
514 Lisp_Object rest, frame;
515 FOR_EACH_FRAME (rest, frame)
516 x_flush (XFRAME (frame));
517 }
518 else if (FRAME_X_P (f))
519 XFlush (FRAME_X_DISPLAY (f));
520 UNBLOCK_INPUT;
521 }
522
523
524 /* Remove calls to XFlush by defining XFlush to an empty replacement.
525 Calls to XFlush should be unnecessary because the X output buffer
526 is flushed automatically as needed by calls to XPending,
527 XNextEvent, or XWindowEvent according to the XFlush man page.
528 XTread_socket calls XPending. Removing XFlush improves
529 performance. */
530
531 #define XFlush(DISPLAY) (void) 0
532
533 \f
534 /***********************************************************************
535 Debugging
536 ***********************************************************************/
537
538 #if 0
539
540 /* This is a function useful for recording debugging information about
541 the sequence of occurrences in this file. */
542
543 struct record
544 {
545 char *locus;
546 int type;
547 };
548
549 struct record event_record[100];
550
551 int event_record_index;
552
553 record_event (locus, type)
554 char *locus;
555 int type;
556 {
557 if (event_record_index == sizeof (event_record) / sizeof (struct record))
558 event_record_index = 0;
559
560 event_record[event_record_index].locus = locus;
561 event_record[event_record_index].type = type;
562 event_record_index++;
563 }
564
565 #endif /* 0 */
566
567
568 \f
569 /* Return the struct x_display_info corresponding to DPY. */
570
571 struct x_display_info *
572 x_display_info_for_display (dpy)
573 Display *dpy;
574 {
575 struct x_display_info *dpyinfo;
576
577 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
578 if (dpyinfo->display == dpy)
579 return dpyinfo;
580
581 return 0;
582 }
583
584
585 \f
586 /***********************************************************************
587 Starting and ending an update
588 ***********************************************************************/
589
590 /* Start an update of frame F. This function is installed as a hook
591 for update_begin, i.e. it is called when update_begin is called.
592 This function is called prior to calls to x_update_window_begin for
593 each window being updated. Currently, there is nothing to do here
594 because all interesting stuff is done on a window basis. */
595
596 static void
597 x_update_begin (f)
598 struct frame *f;
599 {
600 /* Nothing to do. */
601 }
602
603
604 /* Start update of window W. Set the global variable updated_window
605 to the window being updated and set output_cursor to the cursor
606 position of W. */
607
608 static void
609 x_update_window_begin (w)
610 struct window *w;
611 {
612 struct frame *f = XFRAME (WINDOW_FRAME (w));
613 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
614
615 updated_window = w;
616 set_output_cursor (&w->cursor);
617
618 BLOCK_INPUT;
619
620 if (f == display_info->mouse_face_mouse_frame)
621 {
622 /* Don't do highlighting for mouse motion during the update. */
623 display_info->mouse_face_defer = 1;
624
625 /* If F needs to be redrawn, simply forget about any prior mouse
626 highlighting. */
627 if (FRAME_GARBAGED_P (f))
628 display_info->mouse_face_window = Qnil;
629
630 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
631 their mouse_face_p flag set, which means that they are always
632 unequal to rows in a desired matrix which never have that
633 flag set. So, rows containing mouse-face glyphs are never
634 scrolled, and we don't have to switch the mouse highlight off
635 here to prevent it from being scrolled. */
636
637 /* Can we tell that this update does not affect the window
638 where the mouse highlight is? If so, no need to turn off.
639 Likewise, don't do anything if the frame is garbaged;
640 in that case, the frame's current matrix that we would use
641 is all wrong, and we will redisplay that line anyway. */
642 if (!NILP (display_info->mouse_face_window)
643 && w == XWINDOW (display_info->mouse_face_window))
644 {
645 int i;
646
647 for (i = 0; i < w->desired_matrix->nrows; ++i)
648 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
649 break;
650
651 if (i < w->desired_matrix->nrows)
652 clear_mouse_face (display_info);
653 }
654 #endif /* 0 */
655 }
656
657 UNBLOCK_INPUT;
658 }
659
660
661 /* Draw a vertical window border to the right of window W if W doesn't
662 have vertical scroll bars. */
663
664 static void
665 x_draw_vertical_border (w)
666 struct window *w;
667 {
668 struct frame *f = XFRAME (WINDOW_FRAME (w));
669
670 /* Redraw borders between horizontally adjacent windows. Don't
671 do it for frames with vertical scroll bars because either the
672 right scroll bar of a window, or the left scroll bar of its
673 neighbor will suffice as a border. */
674 if (!WINDOW_RIGHTMOST_P (w)
675 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
676 {
677 int x0, x1, y0, y1;
678
679 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
680 x1 += FRAME_X_RIGHT_FRINGE_WIDTH (f);
681 y1 -= 1;
682
683 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
684 f->output_data.x->normal_gc, x1, y0, x1, y1);
685 }
686 }
687
688
689 /* End update of window W (which is equal to updated_window).
690
691 Draw vertical borders between horizontally adjacent windows, and
692 display W's cursor if CURSOR_ON_P is non-zero.
693
694 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
695 glyphs in mouse-face were overwritten. In that case we have to
696 make sure that the mouse-highlight is properly redrawn.
697
698 W may be a menu bar pseudo-window in case we don't have X toolkit
699 support. Such windows don't have a cursor, so don't display it
700 here. */
701
702 static void
703 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
704 struct window *w;
705 int cursor_on_p, mouse_face_overwritten_p;
706 {
707 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
708
709 if (!w->pseudo_window_p)
710 {
711 BLOCK_INPUT;
712
713 if (cursor_on_p)
714 x_display_and_set_cursor (w, 1, output_cursor.hpos,
715 output_cursor.vpos,
716 output_cursor.x, output_cursor.y);
717
718 x_draw_vertical_border (w);
719 UNBLOCK_INPUT;
720 }
721
722 /* If a row with mouse-face was overwritten, arrange for
723 XTframe_up_to_date to redisplay the mouse highlight. */
724 if (mouse_face_overwritten_p)
725 {
726 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
727 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
728 dpyinfo->mouse_face_window = Qnil;
729 }
730
731 updated_window = NULL;
732 }
733
734
735 /* End update of frame F. This function is installed as a hook in
736 update_end. */
737
738 static void
739 x_update_end (f)
740 struct frame *f;
741 {
742 /* Mouse highlight may be displayed again. */
743 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
744
745 BLOCK_INPUT;
746 XFlush (FRAME_X_DISPLAY (f));
747 UNBLOCK_INPUT;
748 }
749
750
751 /* This function is called from various places in xdisp.c whenever a
752 complete update has been performed. The global variable
753 updated_window is not available here. */
754
755 static void
756 XTframe_up_to_date (f)
757 struct frame *f;
758 {
759 if (FRAME_X_P (f))
760 {
761 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
762
763 if (dpyinfo->mouse_face_deferred_gc
764 || f == dpyinfo->mouse_face_mouse_frame)
765 {
766 BLOCK_INPUT;
767 if (dpyinfo->mouse_face_mouse_frame)
768 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
769 dpyinfo->mouse_face_mouse_x,
770 dpyinfo->mouse_face_mouse_y);
771 dpyinfo->mouse_face_deferred_gc = 0;
772 UNBLOCK_INPUT;
773 }
774 }
775 }
776
777
778 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
779 arrow bitmaps, or clear the fringes if no bitmaps are required
780 before DESIRED_ROW is made current. The window being updated is
781 found in updated_window. This function It is called from
782 update_window_line only if it is known that there are differences
783 between bitmaps to be drawn between current row and DESIRED_ROW. */
784
785 static void
786 x_after_update_window_line (desired_row)
787 struct glyph_row *desired_row;
788 {
789 struct window *w = updated_window;
790 struct frame *f;
791 int width, height;
792
793 xassert (w);
794
795 if (!desired_row->mode_line_p && !w->pseudo_window_p)
796 {
797 BLOCK_INPUT;
798 x_draw_row_fringe_bitmaps (w, desired_row);
799 UNBLOCK_INPUT;
800 }
801
802 /* When a window has disappeared, make sure that no rest of
803 full-width rows stays visible in the internal border. Could
804 check here if updated_window is the leftmost/rightmost window,
805 but I guess it's not worth doing since vertically split windows
806 are almost never used, internal border is rarely set, and the
807 overhead is very small. */
808 if (windows_or_buffers_changed
809 && desired_row->full_width_p
810 && (f = XFRAME (w->frame),
811 width = FRAME_INTERNAL_BORDER_WIDTH (f),
812 width != 0)
813 && (height = desired_row->visible_height,
814 height > 0))
815 {
816 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
817
818 /* Internal border is drawn below the tool bar. */
819 if (WINDOWP (f->tool_bar_window)
820 && w == XWINDOW (f->tool_bar_window))
821 y -= width;
822
823 BLOCK_INPUT;
824 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
825 0, y, width, height, False);
826 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
827 f->output_data.x->pixel_width - width,
828 y, width, height, False);
829 UNBLOCK_INPUT;
830 }
831 }
832
833
834 /* Draw the bitmap WHICH in one of the left or right fringes of
835 window W. ROW is the glyph row for which to display the bitmap; it
836 determines the vertical position at which the bitmap has to be
837 drawn. */
838
839 static void
840 x_draw_fringe_bitmap (w, row, which, left_p)
841 struct window *w;
842 struct glyph_row *row;
843 enum fringe_bitmap_type which;
844 int left_p;
845 {
846 struct frame *f = XFRAME (WINDOW_FRAME (w));
847 Display *display = FRAME_X_DISPLAY (f);
848 Window window = FRAME_X_WINDOW (f);
849 int x, y, wd, h, dy;
850 int b1, b2;
851 unsigned char *bits;
852 Pixmap pixmap;
853 GC gc = f->output_data.x->normal_gc;
854 struct face *face;
855 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
856
857 /* Must clip because of partially visible lines. */
858 x_clip_to_row (w, row, gc, 1);
859
860 /* Convert row to frame coordinates. */
861 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
862
863 switch (which)
864 {
865 case NO_FRINGE_BITMAP:
866 wd = 0;
867 h = 0;
868 break;
869
870 case LEFT_TRUNCATION_BITMAP:
871 wd = left_width;
872 h = left_height;
873 bits = left_bits;
874 break;
875
876 case OVERLAY_ARROW_BITMAP:
877 wd = ov_width;
878 h = ov_height;
879 bits = ov_bits;
880 break;
881
882 case RIGHT_TRUNCATION_BITMAP:
883 wd = right_width;
884 h = right_height;
885 bits = right_bits;
886 break;
887
888 case CONTINUED_LINE_BITMAP:
889 wd = continued_width;
890 h = continued_height;
891 bits = continued_bits;
892 break;
893
894 case CONTINUATION_LINE_BITMAP:
895 wd = continuation_width;
896 h = continuation_height;
897 bits = continuation_bits;
898 break;
899
900 case ZV_LINE_BITMAP:
901 wd = zv_width;
902 h = zv_height - (y % zv_period);
903 bits = zv_bits + (y % zv_period);
904 break;
905
906 default:
907 abort ();
908 }
909
910 /* Clip bitmap if too high. */
911 if (h > row->height)
912 h = row->height;
913
914 /* Set dy to the offset in the row to start drawing the bitmap. */
915 dy = (row->height - h) / 2;
916
917 face = FACE_FROM_ID (f, FRINGE_FACE_ID);
918 PREPARE_FACE_FOR_DISPLAY (f, face);
919
920 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
921 the fringe. */
922 b1 = b2 = -1;
923 if (left_p)
924 {
925 if (wd > FRAME_X_LEFT_FRINGE_WIDTH (f))
926 wd = FRAME_X_LEFT_FRINGE_WIDTH (f);
927 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
928 - wd
929 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - wd) / 2);
930 if (wd < FRAME_X_LEFT_FRINGE_WIDTH (f) || row->height > h)
931 {
932 /* If W has a vertical border to its left, don't draw over it. */
933 int border = ((XFASTINT (w->left) > 0
934 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
935 ? 1 : 0);
936 b1 = (window_box_left (w, -1)
937 - FRAME_X_LEFT_FRINGE_WIDTH (f)
938 + border);
939 b2 = (FRAME_X_LEFT_FRINGE_WIDTH (f) - border);
940 }
941 }
942 else
943 {
944 if (wd > FRAME_X_RIGHT_FRINGE_WIDTH (f))
945 wd = FRAME_X_RIGHT_FRINGE_WIDTH (f);
946 x = (window_box_right (w, -1)
947 + (FRAME_X_RIGHT_FRINGE_WIDTH (f) - wd) / 2);
948 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
949 the fringe. */
950 if (wd < FRAME_X_RIGHT_FRINGE_WIDTH (f) || row->height > h)
951 {
952 b1 = window_box_right (w, -1);
953 b2 = FRAME_X_RIGHT_FRINGE_WIDTH (f);
954 }
955 }
956
957 if (b1 >= 0)
958 {
959 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
960
961 /* In case the same realized face is used for fringes and
962 for something displayed in the text (e.g. face `region' on
963 mono-displays, the fill style may have been changed to
964 FillSolid in x_draw_glyph_string_background. */
965 if (face->stipple)
966 XSetFillStyle (display, face->gc, FillOpaqueStippled);
967 else
968 XSetForeground (display, face->gc, face->background);
969
970 XFillRectangle (display, window, face->gc,
971 b1,
972 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
973 row->y)),
974 b2,
975 row->visible_height);
976 if (!face->stipple)
977 XSetForeground (display, face->gc, face->foreground);
978 }
979
980 if (which != NO_FRINGE_BITMAP)
981 {
982 /* Draw the bitmap. I believe these small pixmaps can be cached
983 by the server. */
984 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
985 face->foreground,
986 face->background, depth);
987 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
988 XFreePixmap (display, pixmap);
989 }
990
991 XSetClipMask (display, gc, None);
992 }
993
994
995 /* Draw fringe bitmaps for glyph row ROW on window W. Call this
996 function with input blocked. */
997
998 static void
999 x_draw_row_fringe_bitmaps (w, row)
1000 struct window *w;
1001 struct glyph_row *row;
1002 {
1003 struct frame *f = XFRAME (w->frame);
1004 enum fringe_bitmap_type bitmap;
1005
1006 xassert (interrupt_input_blocked);
1007
1008 /* If row is completely invisible, because of vscrolling, we
1009 don't have to draw anything. */
1010 if (row->visible_height <= 0)
1011 return;
1012
1013 if (FRAME_X_LEFT_FRINGE_WIDTH (f) != 0)
1014 {
1015 /* Decide which bitmap to draw in the left fringe. */
1016 if (row->overlay_arrow_p)
1017 bitmap = OVERLAY_ARROW_BITMAP;
1018 else if (row->truncated_on_left_p)
1019 bitmap = LEFT_TRUNCATION_BITMAP;
1020 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
1021 bitmap = CONTINUATION_LINE_BITMAP;
1022 else if (row->indicate_empty_line_p)
1023 bitmap = ZV_LINE_BITMAP;
1024 else
1025 bitmap = NO_FRINGE_BITMAP;
1026
1027 x_draw_fringe_bitmap (w, row, bitmap, 1);
1028 }
1029
1030 if (FRAME_X_RIGHT_FRINGE_WIDTH (f) != 0)
1031 {
1032 /* Decide which bitmap to draw in the right fringe. */
1033 if (row->truncated_on_right_p)
1034 bitmap = RIGHT_TRUNCATION_BITMAP;
1035 else if (row->continued_p)
1036 bitmap = CONTINUED_LINE_BITMAP;
1037 else if (row->indicate_empty_line_p && FRAME_X_LEFT_FRINGE_WIDTH (f) == 0)
1038 bitmap = ZV_LINE_BITMAP;
1039 else
1040 bitmap = NO_FRINGE_BITMAP;
1041
1042 x_draw_fringe_bitmap (w, row, bitmap, 0);
1043 }
1044 }
1045
1046 \f
1047
1048 /* This is called when starting Emacs and when restarting after
1049 suspend. When starting Emacs, no X window is mapped. And nothing
1050 must be done to Emacs's own window if it is suspended (though that
1051 rarely happens). */
1052
1053 static void
1054 XTset_terminal_modes ()
1055 {
1056 }
1057
1058 /* This is called when exiting or suspending Emacs. Exiting will make
1059 the X-windows go away, and suspending requires no action. */
1060
1061 static void
1062 XTreset_terminal_modes ()
1063 {
1064 }
1065
1066
1067 \f
1068 /***********************************************************************
1069 Output Cursor
1070 ***********************************************************************/
1071
1072 /* Set the global variable output_cursor to CURSOR. All cursor
1073 positions are relative to updated_window. */
1074
1075 static void
1076 set_output_cursor (cursor)
1077 struct cursor_pos *cursor;
1078 {
1079 output_cursor.hpos = cursor->hpos;
1080 output_cursor.vpos = cursor->vpos;
1081 output_cursor.x = cursor->x;
1082 output_cursor.y = cursor->y;
1083 }
1084
1085
1086 /* Set a nominal cursor position.
1087
1088 HPOS and VPOS are column/row positions in a window glyph matrix. X
1089 and Y are window text area relative pixel positions.
1090
1091 If this is done during an update, updated_window will contain the
1092 window that is being updated and the position is the future output
1093 cursor position for that window. If updated_window is null, use
1094 selected_window and display the cursor at the given position. */
1095
1096 static void
1097 XTcursor_to (vpos, hpos, y, x)
1098 int vpos, hpos, y, x;
1099 {
1100 struct window *w;
1101
1102 /* If updated_window is not set, work on selected_window. */
1103 if (updated_window)
1104 w = updated_window;
1105 else
1106 w = XWINDOW (selected_window);
1107
1108 /* Set the output cursor. */
1109 output_cursor.hpos = hpos;
1110 output_cursor.vpos = vpos;
1111 output_cursor.x = x;
1112 output_cursor.y = y;
1113
1114 /* If not called as part of an update, really display the cursor.
1115 This will also set the cursor position of W. */
1116 if (updated_window == NULL)
1117 {
1118 BLOCK_INPUT;
1119 x_display_cursor (w, 1, hpos, vpos, x, y);
1120 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1121 UNBLOCK_INPUT;
1122 }
1123 }
1124
1125
1126 \f
1127 /***********************************************************************
1128 Display Iterator
1129 ***********************************************************************/
1130
1131 /* Function prototypes of this page. */
1132
1133 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1134 struct glyph *,
1135 XChar2b *,
1136 int *));
1137 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1138 int, XChar2b *, int));
1139 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1140 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1141 static void x_append_glyph P_ ((struct it *));
1142 static void x_append_composite_glyph P_ ((struct it *));
1143 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1144 int, int, double));
1145 static void x_produce_glyphs P_ ((struct it *));
1146 static void x_produce_image_glyph P_ ((struct it *it));
1147
1148
1149 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1150 is not contained in the font. */
1151
1152 static INLINE XCharStruct *
1153 x_per_char_metric (font, char2b)
1154 XFontStruct *font;
1155 XChar2b *char2b;
1156 {
1157 /* The result metric information. */
1158 XCharStruct *pcm = NULL;
1159
1160 xassert (font && char2b);
1161
1162 if (font->per_char != NULL)
1163 {
1164 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1165 {
1166 /* min_char_or_byte2 specifies the linear character index
1167 corresponding to the first element of the per_char array,
1168 max_char_or_byte2 is the index of the last character. A
1169 character with non-zero CHAR2B->byte1 is not in the font.
1170 A character with byte2 less than min_char_or_byte2 or
1171 greater max_char_or_byte2 is not in the font. */
1172 if (char2b->byte1 == 0
1173 && char2b->byte2 >= font->min_char_or_byte2
1174 && char2b->byte2 <= font->max_char_or_byte2)
1175 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1176 }
1177 else
1178 {
1179 /* If either min_byte1 or max_byte1 are nonzero, both
1180 min_char_or_byte2 and max_char_or_byte2 are less than
1181 256, and the 2-byte character index values corresponding
1182 to the per_char array element N (counting from 0) are:
1183
1184 byte1 = N/D + min_byte1
1185 byte2 = N\D + min_char_or_byte2
1186
1187 where:
1188
1189 D = max_char_or_byte2 - min_char_or_byte2 + 1
1190 / = integer division
1191 \ = integer modulus */
1192 if (char2b->byte1 >= font->min_byte1
1193 && char2b->byte1 <= font->max_byte1
1194 && char2b->byte2 >= font->min_char_or_byte2
1195 && char2b->byte2 <= font->max_char_or_byte2)
1196 {
1197 pcm = (font->per_char
1198 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1199 * (char2b->byte1 - font->min_byte1))
1200 + (char2b->byte2 - font->min_char_or_byte2));
1201 }
1202 }
1203 }
1204 else
1205 {
1206 /* If the per_char pointer is null, all glyphs between the first
1207 and last character indexes inclusive have the same
1208 information, as given by both min_bounds and max_bounds. */
1209 if (char2b->byte2 >= font->min_char_or_byte2
1210 && char2b->byte2 <= font->max_char_or_byte2)
1211 pcm = &font->max_bounds;
1212 }
1213
1214 return ((pcm == NULL
1215 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1216 ? NULL : pcm);
1217 }
1218
1219
1220 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1221 the two-byte form of C. Encoding is returned in *CHAR2B. */
1222
1223 static INLINE void
1224 x_encode_char (c, char2b, font_info)
1225 int c;
1226 XChar2b *char2b;
1227 struct font_info *font_info;
1228 {
1229 int charset = CHAR_CHARSET (c);
1230 XFontStruct *font = font_info->font;
1231
1232 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1233 This may be either a program in a special encoder language or a
1234 fixed encoding. */
1235 if (font_info->font_encoder)
1236 {
1237 /* It's a program. */
1238 struct ccl_program *ccl = font_info->font_encoder;
1239
1240 if (CHARSET_DIMENSION (charset) == 1)
1241 {
1242 ccl->reg[0] = charset;
1243 ccl->reg[1] = char2b->byte2;
1244 }
1245 else
1246 {
1247 ccl->reg[0] = charset;
1248 ccl->reg[1] = char2b->byte1;
1249 ccl->reg[2] = char2b->byte2;
1250 }
1251
1252 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1253
1254 /* We assume that MSBs are appropriately set/reset by CCL
1255 program. */
1256 if (font->max_byte1 == 0) /* 1-byte font */
1257 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1258 else
1259 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1260 }
1261 else if (font_info->encoding[charset])
1262 {
1263 /* Fixed encoding scheme. See fontset.h for the meaning of the
1264 encoding numbers. */
1265 int enc = font_info->encoding[charset];
1266
1267 if ((enc == 1 || enc == 2)
1268 && CHARSET_DIMENSION (charset) == 2)
1269 char2b->byte1 |= 0x80;
1270
1271 if (enc == 1 || enc == 3)
1272 char2b->byte2 |= 0x80;
1273 }
1274 }
1275
1276
1277 /* Get face and two-byte form of character C in face FACE_ID on frame
1278 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1279 means we want to display multibyte text. Value is a pointer to a
1280 realized face that is ready for display. */
1281
1282 static INLINE struct face *
1283 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1284 struct frame *f;
1285 int c, face_id;
1286 XChar2b *char2b;
1287 int multibyte_p;
1288 {
1289 struct face *face = FACE_FROM_ID (f, face_id);
1290
1291 if (!multibyte_p)
1292 {
1293 /* Unibyte case. We don't have to encode, but we have to make
1294 sure to use a face suitable for unibyte. */
1295 char2b->byte1 = 0;
1296 char2b->byte2 = c;
1297 face_id = FACE_FOR_CHAR (f, face, c);
1298 face = FACE_FROM_ID (f, face_id);
1299 }
1300 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1301 {
1302 /* Case of ASCII in a face known to fit ASCII. */
1303 char2b->byte1 = 0;
1304 char2b->byte2 = c;
1305 }
1306 else
1307 {
1308 int c1, c2, charset;
1309
1310 /* Split characters into bytes. If c2 is -1 afterwards, C is
1311 really a one-byte character so that byte1 is zero. */
1312 SPLIT_CHAR (c, charset, c1, c2);
1313 if (c2 > 0)
1314 char2b->byte1 = c1, char2b->byte2 = c2;
1315 else
1316 char2b->byte1 = 0, char2b->byte2 = c1;
1317
1318 /* Maybe encode the character in *CHAR2B. */
1319 if (face->font != NULL)
1320 {
1321 struct font_info *font_info
1322 = FONT_INFO_FROM_ID (f, face->font_info_id);
1323 if (font_info)
1324 x_encode_char (c, char2b, font_info);
1325 }
1326 }
1327
1328 /* Make sure X resources of the face are allocated. */
1329 xassert (face != NULL);
1330 PREPARE_FACE_FOR_DISPLAY (f, face);
1331
1332 return face;
1333 }
1334
1335
1336 /* Get face and two-byte form of character glyph GLYPH on frame F.
1337 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1338 a pointer to a realized face that is ready for display. */
1339
1340 static INLINE struct face *
1341 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1342 struct frame *f;
1343 struct glyph *glyph;
1344 XChar2b *char2b;
1345 int *two_byte_p;
1346 {
1347 struct face *face;
1348
1349 xassert (glyph->type == CHAR_GLYPH);
1350 face = FACE_FROM_ID (f, glyph->face_id);
1351
1352 if (two_byte_p)
1353 *two_byte_p = 0;
1354
1355 if (!glyph->multibyte_p)
1356 {
1357 /* Unibyte case. We don't have to encode, but we have to make
1358 sure to use a face suitable for unibyte. */
1359 char2b->byte1 = 0;
1360 char2b->byte2 = glyph->u.ch;
1361 }
1362 else if (glyph->u.ch < 128
1363 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1364 {
1365 /* Case of ASCII in a face known to fit ASCII. */
1366 char2b->byte1 = 0;
1367 char2b->byte2 = glyph->u.ch;
1368 }
1369 else
1370 {
1371 int c1, c2, charset;
1372
1373 /* Split characters into bytes. If c2 is -1 afterwards, C is
1374 really a one-byte character so that byte1 is zero. */
1375 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1376 if (c2 > 0)
1377 char2b->byte1 = c1, char2b->byte2 = c2;
1378 else
1379 char2b->byte1 = 0, char2b->byte2 = c1;
1380
1381 /* Maybe encode the character in *CHAR2B. */
1382 if (charset != CHARSET_ASCII)
1383 {
1384 struct font_info *font_info
1385 = FONT_INFO_FROM_ID (f, face->font_info_id);
1386 if (font_info)
1387 {
1388 x_encode_char (glyph->u.ch, char2b, font_info);
1389 if (two_byte_p)
1390 *two_byte_p
1391 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1392 }
1393 }
1394 }
1395
1396 /* Make sure X resources of the face are allocated. */
1397 xassert (face != NULL);
1398 PREPARE_FACE_FOR_DISPLAY (f, face);
1399 return face;
1400 }
1401
1402
1403 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1404 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1405
1406 static INLINE void
1407 x_append_glyph (it)
1408 struct it *it;
1409 {
1410 struct glyph *glyph;
1411 enum glyph_row_area area = it->area;
1412
1413 xassert (it->glyph_row);
1414 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1415
1416 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1417 if (glyph < it->glyph_row->glyphs[area + 1])
1418 {
1419 glyph->charpos = CHARPOS (it->position);
1420 glyph->object = it->object;
1421 glyph->pixel_width = it->pixel_width;
1422 glyph->voffset = it->voffset;
1423 glyph->type = CHAR_GLYPH;
1424 glyph->multibyte_p = it->multibyte_p;
1425 glyph->left_box_line_p = it->start_of_box_run_p;
1426 glyph->right_box_line_p = it->end_of_box_run_p;
1427 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1428 || it->phys_descent > it->descent);
1429 glyph->padding_p = 0;
1430 glyph->glyph_not_available_p = it->glyph_not_available_p;
1431 glyph->face_id = it->face_id;
1432 glyph->u.ch = it->char_to_display;
1433 ++it->glyph_row->used[area];
1434 }
1435 }
1436
1437 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1438 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1439
1440 static INLINE void
1441 x_append_composite_glyph (it)
1442 struct it *it;
1443 {
1444 struct glyph *glyph;
1445 enum glyph_row_area area = it->area;
1446
1447 xassert (it->glyph_row);
1448
1449 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1450 if (glyph < it->glyph_row->glyphs[area + 1])
1451 {
1452 glyph->charpos = CHARPOS (it->position);
1453 glyph->object = it->object;
1454 glyph->pixel_width = it->pixel_width;
1455 glyph->voffset = it->voffset;
1456 glyph->type = COMPOSITE_GLYPH;
1457 glyph->multibyte_p = it->multibyte_p;
1458 glyph->left_box_line_p = it->start_of_box_run_p;
1459 glyph->right_box_line_p = it->end_of_box_run_p;
1460 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1461 || it->phys_descent > it->descent);
1462 glyph->padding_p = 0;
1463 glyph->glyph_not_available_p = 0;
1464 glyph->face_id = it->face_id;
1465 glyph->u.cmp_id = it->cmp_id;
1466 ++it->glyph_row->used[area];
1467 }
1468 }
1469
1470
1471 /* Change IT->ascent and IT->height according to the setting of
1472 IT->voffset. */
1473
1474 static INLINE void
1475 take_vertical_position_into_account (it)
1476 struct it *it;
1477 {
1478 if (it->voffset)
1479 {
1480 if (it->voffset < 0)
1481 /* Increase the ascent so that we can display the text higher
1482 in the line. */
1483 it->ascent += abs (it->voffset);
1484 else
1485 /* Increase the descent so that we can display the text lower
1486 in the line. */
1487 it->descent += it->voffset;
1488 }
1489 }
1490
1491
1492 /* Produce glyphs/get display metrics for the image IT is loaded with.
1493 See the description of struct display_iterator in dispextern.h for
1494 an overview of struct display_iterator. */
1495
1496 static void
1497 x_produce_image_glyph (it)
1498 struct it *it;
1499 {
1500 struct image *img;
1501 struct face *face;
1502
1503 xassert (it->what == IT_IMAGE);
1504
1505 face = FACE_FROM_ID (it->f, it->face_id);
1506 img = IMAGE_FROM_ID (it->f, it->image_id);
1507 xassert (img);
1508
1509 /* Make sure X resources of the face and image are loaded. */
1510 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1511 prepare_image_for_display (it->f, img);
1512
1513 it->ascent = it->phys_ascent = image_ascent (img, face);
1514 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1515 it->pixel_width = img->width + 2 * img->hmargin;
1516
1517 it->nglyphs = 1;
1518
1519 if (face->box != FACE_NO_BOX)
1520 {
1521 if (face->box_line_width > 0)
1522 {
1523 it->ascent += face->box_line_width;
1524 it->descent += face->box_line_width;
1525 }
1526
1527 if (it->start_of_box_run_p)
1528 it->pixel_width += abs (face->box_line_width);
1529 if (it->end_of_box_run_p)
1530 it->pixel_width += abs (face->box_line_width);
1531 }
1532
1533 take_vertical_position_into_account (it);
1534
1535 if (it->glyph_row)
1536 {
1537 struct glyph *glyph;
1538 enum glyph_row_area area = it->area;
1539
1540 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1541 if (glyph < it->glyph_row->glyphs[area + 1])
1542 {
1543 glyph->charpos = CHARPOS (it->position);
1544 glyph->object = it->object;
1545 glyph->pixel_width = it->pixel_width;
1546 glyph->voffset = it->voffset;
1547 glyph->type = IMAGE_GLYPH;
1548 glyph->multibyte_p = it->multibyte_p;
1549 glyph->left_box_line_p = it->start_of_box_run_p;
1550 glyph->right_box_line_p = it->end_of_box_run_p;
1551 glyph->overlaps_vertically_p = 0;
1552 glyph->padding_p = 0;
1553 glyph->glyph_not_available_p = 0;
1554 glyph->face_id = it->face_id;
1555 glyph->u.img_id = img->id;
1556 ++it->glyph_row->used[area];
1557 }
1558 }
1559 }
1560
1561
1562 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1563 of the glyph, WIDTH and HEIGHT are the width and height of the
1564 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1565 ascent of the glyph (0 <= ASCENT <= 1). */
1566
1567 static void
1568 x_append_stretch_glyph (it, object, width, height, ascent)
1569 struct it *it;
1570 Lisp_Object object;
1571 int width, height;
1572 double ascent;
1573 {
1574 struct glyph *glyph;
1575 enum glyph_row_area area = it->area;
1576
1577 xassert (ascent >= 0 && ascent <= 1);
1578
1579 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1580 if (glyph < it->glyph_row->glyphs[area + 1])
1581 {
1582 glyph->charpos = CHARPOS (it->position);
1583 glyph->object = object;
1584 glyph->pixel_width = width;
1585 glyph->voffset = it->voffset;
1586 glyph->type = STRETCH_GLYPH;
1587 glyph->multibyte_p = it->multibyte_p;
1588 glyph->left_box_line_p = it->start_of_box_run_p;
1589 glyph->right_box_line_p = it->end_of_box_run_p;
1590 glyph->overlaps_vertically_p = 0;
1591 glyph->padding_p = 0;
1592 glyph->glyph_not_available_p = 0;
1593 glyph->face_id = it->face_id;
1594 glyph->u.stretch.ascent = height * ascent;
1595 glyph->u.stretch.height = height;
1596 ++it->glyph_row->used[area];
1597 }
1598 }
1599
1600
1601 /* Produce a stretch glyph for iterator IT. IT->object is the value
1602 of the glyph property displayed. The value must be a list
1603 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1604 being recognized:
1605
1606 1. `:width WIDTH' specifies that the space should be WIDTH *
1607 canonical char width wide. WIDTH may be an integer or floating
1608 point number.
1609
1610 2. `:relative-width FACTOR' specifies that the width of the stretch
1611 should be computed from the width of the first character having the
1612 `glyph' property, and should be FACTOR times that width.
1613
1614 3. `:align-to HPOS' specifies that the space should be wide enough
1615 to reach HPOS, a value in canonical character units.
1616
1617 Exactly one of the above pairs must be present.
1618
1619 4. `:height HEIGHT' specifies that the height of the stretch produced
1620 should be HEIGHT, measured in canonical character units.
1621
1622 5. `:relative-height FACTOR' specifies that the height of the
1623 stretch should be FACTOR times the height of the characters having
1624 the glyph property.
1625
1626 Either none or exactly one of 4 or 5 must be present.
1627
1628 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1629 of the stretch should be used for the ascent of the stretch.
1630 ASCENT must be in the range 0 <= ASCENT <= 100. */
1631
1632 #define NUMVAL(X) \
1633 ((INTEGERP (X) || FLOATP (X)) \
1634 ? XFLOATINT (X) \
1635 : - 1)
1636
1637
1638 static void
1639 x_produce_stretch_glyph (it)
1640 struct it *it;
1641 {
1642 /* (space :width WIDTH :height HEIGHT. */
1643 #if GLYPH_DEBUG
1644 extern Lisp_Object Qspace;
1645 #endif
1646 extern Lisp_Object QCwidth, QCheight, QCascent;
1647 extern Lisp_Object QCrelative_width, QCrelative_height;
1648 extern Lisp_Object QCalign_to;
1649 Lisp_Object prop, plist;
1650 double width = 0, height = 0, ascent = 0;
1651 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1652 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1653
1654 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1655
1656 /* List should start with `space'. */
1657 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1658 plist = XCDR (it->object);
1659
1660 /* Compute the width of the stretch. */
1661 if (prop = Fplist_get (plist, QCwidth),
1662 NUMVAL (prop) > 0)
1663 /* Absolute width `:width WIDTH' specified and valid. */
1664 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1665 else if (prop = Fplist_get (plist, QCrelative_width),
1666 NUMVAL (prop) > 0)
1667 {
1668 /* Relative width `:relative-width FACTOR' specified and valid.
1669 Compute the width of the characters having the `glyph'
1670 property. */
1671 struct it it2;
1672 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1673
1674 it2 = *it;
1675 if (it->multibyte_p)
1676 {
1677 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1678 - IT_BYTEPOS (*it));
1679 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1680 }
1681 else
1682 it2.c = *p, it2.len = 1;
1683
1684 it2.glyph_row = NULL;
1685 it2.what = IT_CHARACTER;
1686 x_produce_glyphs (&it2);
1687 width = NUMVAL (prop) * it2.pixel_width;
1688 }
1689 else if (prop = Fplist_get (plist, QCalign_to),
1690 NUMVAL (prop) > 0)
1691 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1692 else
1693 /* Nothing specified -> width defaults to canonical char width. */
1694 width = CANON_X_UNIT (it->f);
1695
1696 /* Compute height. */
1697 if (prop = Fplist_get (plist, QCheight),
1698 NUMVAL (prop) > 0)
1699 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1700 else if (prop = Fplist_get (plist, QCrelative_height),
1701 NUMVAL (prop) > 0)
1702 height = FONT_HEIGHT (font) * NUMVAL (prop);
1703 else
1704 height = FONT_HEIGHT (font);
1705
1706 /* Compute percentage of height used for ascent. If
1707 `:ascent ASCENT' is present and valid, use that. Otherwise,
1708 derive the ascent from the font in use. */
1709 if (prop = Fplist_get (plist, QCascent),
1710 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1711 ascent = NUMVAL (prop) / 100.0;
1712 else
1713 ascent = (double) font->ascent / FONT_HEIGHT (font);
1714
1715 if (width <= 0)
1716 width = 1;
1717 if (height <= 0)
1718 height = 1;
1719
1720 if (it->glyph_row)
1721 {
1722 Lisp_Object object = it->stack[it->sp - 1].string;
1723 if (!STRINGP (object))
1724 object = it->w->buffer;
1725 x_append_stretch_glyph (it, object, width, height, ascent);
1726 }
1727
1728 it->pixel_width = width;
1729 it->ascent = it->phys_ascent = height * ascent;
1730 it->descent = it->phys_descent = height - it->ascent;
1731 it->nglyphs = 1;
1732
1733 if (face->box != FACE_NO_BOX)
1734 {
1735 if (face->box_line_width > 0)
1736 {
1737 it->ascent += face->box_line_width;
1738 it->descent += face->box_line_width;
1739 }
1740
1741 if (it->start_of_box_run_p)
1742 it->pixel_width += abs (face->box_line_width);
1743 if (it->end_of_box_run_p)
1744 it->pixel_width += abs (face->box_line_width);
1745 }
1746
1747 take_vertical_position_into_account (it);
1748 }
1749
1750 /* Return proper value to be used as baseline offset of font that has
1751 ASCENT and DESCENT to draw characters by the font at the vertical
1752 center of the line of frame F.
1753
1754 Here, out task is to find the value of BOFF in the following figure;
1755
1756 -------------------------+-----------+-
1757 -+-+---------+-+ | |
1758 | | | | | |
1759 | | | | F_ASCENT F_HEIGHT
1760 | | | ASCENT | |
1761 HEIGHT | | | | |
1762 | | |-|-+------+-----------|------- baseline
1763 | | | | BOFF | |
1764 | |---------|-+-+ | |
1765 | | | DESCENT | |
1766 -+-+---------+-+ F_DESCENT |
1767 -------------------------+-----------+-
1768
1769 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1770 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1771 DESCENT = FONT->descent
1772 HEIGHT = FONT_HEIGHT (FONT)
1773 F_DESCENT = (F->output_data.x->font->descent
1774 - F->output_data.x->baseline_offset)
1775 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1776 */
1777
1778 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1779 ((FONT)->descent \
1780 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1781 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1782 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1783
1784 /* Produce glyphs/get display metrics for the display element IT is
1785 loaded with. See the description of struct display_iterator in
1786 dispextern.h for an overview of struct display_iterator. */
1787
1788 static void
1789 x_produce_glyphs (it)
1790 struct it *it;
1791 {
1792 it->glyph_not_available_p = 0;
1793
1794 if (it->what == IT_CHARACTER)
1795 {
1796 XChar2b char2b;
1797 XFontStruct *font;
1798 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1799 XCharStruct *pcm;
1800 int font_not_found_p;
1801 struct font_info *font_info;
1802 int boff; /* baseline offset */
1803 /* We may change it->multibyte_p upon unibyte<->multibyte
1804 conversion. So, save the current value now and restore it
1805 later.
1806
1807 Note: It seems that we don't have to record multibyte_p in
1808 struct glyph because the character code itself tells if or
1809 not the character is multibyte. Thus, in the future, we must
1810 consider eliminating the field `multibyte_p' in the struct
1811 glyph. */
1812 int saved_multibyte_p = it->multibyte_p;
1813
1814 /* Maybe translate single-byte characters to multibyte, or the
1815 other way. */
1816 it->char_to_display = it->c;
1817 if (!ASCII_BYTE_P (it->c))
1818 {
1819 if (unibyte_display_via_language_environment
1820 && SINGLE_BYTE_CHAR_P (it->c)
1821 && (it->c >= 0240
1822 || !NILP (Vnonascii_translation_table)))
1823 {
1824 it->char_to_display = unibyte_char_to_multibyte (it->c);
1825 it->multibyte_p = 1;
1826 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1827 face = FACE_FROM_ID (it->f, it->face_id);
1828 }
1829 else if (!SINGLE_BYTE_CHAR_P (it->c)
1830 && !it->multibyte_p)
1831 {
1832 it->multibyte_p = 1;
1833 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1834 face = FACE_FROM_ID (it->f, it->face_id);
1835 }
1836 }
1837
1838 /* Get font to use. Encode IT->char_to_display. */
1839 x_get_char_face_and_encoding (it->f, it->char_to_display,
1840 it->face_id, &char2b,
1841 it->multibyte_p);
1842 font = face->font;
1843
1844 /* When no suitable font found, use the default font. */
1845 font_not_found_p = font == NULL;
1846 if (font_not_found_p)
1847 {
1848 font = FRAME_FONT (it->f);
1849 boff = it->f->output_data.x->baseline_offset;
1850 font_info = NULL;
1851 }
1852 else
1853 {
1854 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1855 boff = font_info->baseline_offset;
1856 if (font_info->vertical_centering)
1857 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1858 }
1859
1860 if (it->char_to_display >= ' '
1861 && (!it->multibyte_p || it->char_to_display < 128))
1862 {
1863 /* Either unibyte or ASCII. */
1864 int stretched_p;
1865
1866 it->nglyphs = 1;
1867
1868 pcm = x_per_char_metric (font, &char2b);
1869 it->ascent = font->ascent + boff;
1870 it->descent = font->descent - boff;
1871
1872 if (pcm)
1873 {
1874 it->phys_ascent = pcm->ascent + boff;
1875 it->phys_descent = pcm->descent - boff;
1876 it->pixel_width = pcm->width;
1877 }
1878 else
1879 {
1880 it->glyph_not_available_p = 1;
1881 it->phys_ascent = font->ascent + boff;
1882 it->phys_descent = font->descent - boff;
1883 it->pixel_width = FONT_WIDTH (font);
1884 }
1885
1886 /* If this is a space inside a region of text with
1887 `space-width' property, change its width. */
1888 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1889 if (stretched_p)
1890 it->pixel_width *= XFLOATINT (it->space_width);
1891
1892 /* If face has a box, add the box thickness to the character
1893 height. If character has a box line to the left and/or
1894 right, add the box line width to the character's width. */
1895 if (face->box != FACE_NO_BOX)
1896 {
1897 int thick = face->box_line_width;
1898
1899 if (thick > 0)
1900 {
1901 it->ascent += thick;
1902 it->descent += thick;
1903 }
1904 else
1905 thick = -thick;
1906
1907 if (it->start_of_box_run_p)
1908 it->pixel_width += thick;
1909 if (it->end_of_box_run_p)
1910 it->pixel_width += thick;
1911 }
1912
1913 /* If face has an overline, add the height of the overline
1914 (1 pixel) and a 1 pixel margin to the character height. */
1915 if (face->overline_p)
1916 it->ascent += 2;
1917
1918 take_vertical_position_into_account (it);
1919
1920 /* If we have to actually produce glyphs, do it. */
1921 if (it->glyph_row)
1922 {
1923 if (stretched_p)
1924 {
1925 /* Translate a space with a `space-width' property
1926 into a stretch glyph. */
1927 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1928 x_append_stretch_glyph (it, it->object, it->pixel_width,
1929 it->ascent + it->descent, ascent);
1930 }
1931 else
1932 x_append_glyph (it);
1933
1934 /* If characters with lbearing or rbearing are displayed
1935 in this line, record that fact in a flag of the
1936 glyph row. This is used to optimize X output code. */
1937 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1938 it->glyph_row->contains_overlapping_glyphs_p = 1;
1939 }
1940 }
1941 else if (it->char_to_display == '\n')
1942 {
1943 /* A newline has no width but we need the height of the line. */
1944 it->pixel_width = 0;
1945 it->nglyphs = 0;
1946 it->ascent = it->phys_ascent = font->ascent + boff;
1947 it->descent = it->phys_descent = font->descent - boff;
1948
1949 if (face->box != FACE_NO_BOX
1950 && face->box_line_width > 0)
1951 {
1952 it->ascent += face->box_line_width;
1953 it->descent += face->box_line_width;
1954 }
1955 }
1956 else if (it->char_to_display == '\t')
1957 {
1958 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1959 int x = it->current_x + it->continuation_lines_width;
1960 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1961
1962 /* If the distance from the current position to the next tab
1963 stop is less than a canonical character width, use the
1964 tab stop after that. */
1965 if (next_tab_x - x < CANON_X_UNIT (it->f))
1966 next_tab_x += tab_width;
1967
1968 it->pixel_width = next_tab_x - x;
1969 it->nglyphs = 1;
1970 it->ascent = it->phys_ascent = font->ascent + boff;
1971 it->descent = it->phys_descent = font->descent - boff;
1972
1973 if (it->glyph_row)
1974 {
1975 double ascent = (double) it->ascent / (it->ascent + it->descent);
1976 x_append_stretch_glyph (it, it->object, it->pixel_width,
1977 it->ascent + it->descent, ascent);
1978 }
1979 }
1980 else
1981 {
1982 /* A multi-byte character. Assume that the display width of the
1983 character is the width of the character multiplied by the
1984 width of the font. */
1985
1986 /* If we found a font, this font should give us the right
1987 metrics. If we didn't find a font, use the frame's
1988 default font and calculate the width of the character
1989 from the charset width; this is what old redisplay code
1990 did. */
1991 pcm = x_per_char_metric (font, &char2b);
1992 if (font_not_found_p || !pcm)
1993 {
1994 int charset = CHAR_CHARSET (it->char_to_display);
1995
1996 it->glyph_not_available_p = 1;
1997 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1998 * CHARSET_WIDTH (charset));
1999 it->phys_ascent = font->ascent + boff;
2000 it->phys_descent = font->descent - boff;
2001 }
2002 else
2003 {
2004 it->pixel_width = pcm->width;
2005 it->phys_ascent = pcm->ascent + boff;
2006 it->phys_descent = pcm->descent - boff;
2007 if (it->glyph_row
2008 && (pcm->lbearing < 0
2009 || pcm->rbearing > pcm->width))
2010 it->glyph_row->contains_overlapping_glyphs_p = 1;
2011 }
2012 it->nglyphs = 1;
2013 it->ascent = font->ascent + boff;
2014 it->descent = font->descent - boff;
2015 if (face->box != FACE_NO_BOX)
2016 {
2017 int thick = face->box_line_width;
2018
2019 if (thick > 0)
2020 {
2021 it->ascent += thick;
2022 it->descent += thick;
2023 }
2024 else
2025 thick = - thick;
2026
2027 if (it->start_of_box_run_p)
2028 it->pixel_width += thick;
2029 if (it->end_of_box_run_p)
2030 it->pixel_width += thick;
2031 }
2032
2033 /* If face has an overline, add the height of the overline
2034 (1 pixel) and a 1 pixel margin to the character height. */
2035 if (face->overline_p)
2036 it->ascent += 2;
2037
2038 take_vertical_position_into_account (it);
2039
2040 if (it->glyph_row)
2041 x_append_glyph (it);
2042 }
2043 it->multibyte_p = saved_multibyte_p;
2044 }
2045 else if (it->what == IT_COMPOSITION)
2046 {
2047 /* Note: A composition is represented as one glyph in the
2048 glyph matrix. There are no padding glyphs. */
2049 XChar2b char2b;
2050 XFontStruct *font;
2051 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2052 XCharStruct *pcm;
2053 int font_not_found_p;
2054 struct font_info *font_info;
2055 int boff; /* baseline offset */
2056 struct composition *cmp = composition_table[it->cmp_id];
2057
2058 /* Maybe translate single-byte characters to multibyte. */
2059 it->char_to_display = it->c;
2060 if (unibyte_display_via_language_environment
2061 && SINGLE_BYTE_CHAR_P (it->c)
2062 && (it->c >= 0240
2063 || (it->c >= 0200
2064 && !NILP (Vnonascii_translation_table))))
2065 {
2066 it->char_to_display = unibyte_char_to_multibyte (it->c);
2067 }
2068
2069 /* Get face and font to use. Encode IT->char_to_display. */
2070 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2071 face = FACE_FROM_ID (it->f, it->face_id);
2072 x_get_char_face_and_encoding (it->f, it->char_to_display,
2073 it->face_id, &char2b, it->multibyte_p);
2074 font = face->font;
2075
2076 /* When no suitable font found, use the default font. */
2077 font_not_found_p = font == NULL;
2078 if (font_not_found_p)
2079 {
2080 font = FRAME_FONT (it->f);
2081 boff = it->f->output_data.x->baseline_offset;
2082 font_info = NULL;
2083 }
2084 else
2085 {
2086 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2087 boff = font_info->baseline_offset;
2088 if (font_info->vertical_centering)
2089 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2090 }
2091
2092 /* There are no padding glyphs, so there is only one glyph to
2093 produce for the composition. Important is that pixel_width,
2094 ascent and descent are the values of what is drawn by
2095 draw_glyphs (i.e. the values of the overall glyphs composed). */
2096 it->nglyphs = 1;
2097
2098 /* If we have not yet calculated pixel size data of glyphs of
2099 the composition for the current face font, calculate them
2100 now. Theoretically, we have to check all fonts for the
2101 glyphs, but that requires much time and memory space. So,
2102 here we check only the font of the first glyph. This leads
2103 to incorrect display very rarely, and C-l (recenter) can
2104 correct the display anyway. */
2105 if (cmp->font != (void *) font)
2106 {
2107 /* Ascent and descent of the font of the first character of
2108 this composition (adjusted by baseline offset). Ascent
2109 and descent of overall glyphs should not be less than
2110 them respectively. */
2111 int font_ascent = font->ascent + boff;
2112 int font_descent = font->descent - boff;
2113 /* Bounding box of the overall glyphs. */
2114 int leftmost, rightmost, lowest, highest;
2115 int i, width, ascent, descent;
2116
2117 cmp->font = (void *) font;
2118
2119 /* Initialize the bounding box. */
2120 if (font_info
2121 && (pcm = x_per_char_metric (font, &char2b)))
2122 {
2123 width = pcm->width;
2124 ascent = pcm->ascent;
2125 descent = pcm->descent;
2126 }
2127 else
2128 {
2129 width = FONT_WIDTH (font);
2130 ascent = font->ascent;
2131 descent = font->descent;
2132 }
2133
2134 rightmost = width;
2135 lowest = - descent + boff;
2136 highest = ascent + boff;
2137 leftmost = 0;
2138
2139 if (font_info
2140 && font_info->default_ascent
2141 && CHAR_TABLE_P (Vuse_default_ascent)
2142 && !NILP (Faref (Vuse_default_ascent,
2143 make_number (it->char_to_display))))
2144 highest = font_info->default_ascent + boff;
2145
2146 /* Draw the first glyph at the normal position. It may be
2147 shifted to right later if some other glyphs are drawn at
2148 the left. */
2149 cmp->offsets[0] = 0;
2150 cmp->offsets[1] = boff;
2151
2152 /* Set cmp->offsets for the remaining glyphs. */
2153 for (i = 1; i < cmp->glyph_len; i++)
2154 {
2155 int left, right, btm, top;
2156 int ch = COMPOSITION_GLYPH (cmp, i);
2157 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2158
2159 face = FACE_FROM_ID (it->f, face_id);
2160 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2161 it->multibyte_p);
2162 font = face->font;
2163 if (font == NULL)
2164 {
2165 font = FRAME_FONT (it->f);
2166 boff = it->f->output_data.x->baseline_offset;
2167 font_info = NULL;
2168 }
2169 else
2170 {
2171 font_info
2172 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2173 boff = font_info->baseline_offset;
2174 if (font_info->vertical_centering)
2175 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2176 }
2177
2178 if (font_info
2179 && (pcm = x_per_char_metric (font, &char2b)))
2180 {
2181 width = pcm->width;
2182 ascent = pcm->ascent;
2183 descent = pcm->descent;
2184 }
2185 else
2186 {
2187 width = FONT_WIDTH (font);
2188 ascent = 1;
2189 descent = 0;
2190 }
2191
2192 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2193 {
2194 /* Relative composition with or without
2195 alternate chars. */
2196 left = (leftmost + rightmost - width) / 2;
2197 btm = - descent + boff;
2198 if (font_info && font_info->relative_compose
2199 && (! CHAR_TABLE_P (Vignore_relative_composition)
2200 || NILP (Faref (Vignore_relative_composition,
2201 make_number (ch)))))
2202 {
2203
2204 if (- descent >= font_info->relative_compose)
2205 /* One extra pixel between two glyphs. */
2206 btm = highest + 1;
2207 else if (ascent <= 0)
2208 /* One extra pixel between two glyphs. */
2209 btm = lowest - 1 - ascent - descent;
2210 }
2211 }
2212 else
2213 {
2214 /* A composition rule is specified by an integer
2215 value that encodes global and new reference
2216 points (GREF and NREF). GREF and NREF are
2217 specified by numbers as below:
2218
2219 0---1---2 -- ascent
2220 | |
2221 | |
2222 | |
2223 9--10--11 -- center
2224 | |
2225 ---3---4---5--- baseline
2226 | |
2227 6---7---8 -- descent
2228 */
2229 int rule = COMPOSITION_RULE (cmp, i);
2230 int gref, nref, grefx, grefy, nrefx, nrefy;
2231
2232 COMPOSITION_DECODE_RULE (rule, gref, nref);
2233 grefx = gref % 3, nrefx = nref % 3;
2234 grefy = gref / 3, nrefy = nref / 3;
2235
2236 left = (leftmost
2237 + grefx * (rightmost - leftmost) / 2
2238 - nrefx * width / 2);
2239 btm = ((grefy == 0 ? highest
2240 : grefy == 1 ? 0
2241 : grefy == 2 ? lowest
2242 : (highest + lowest) / 2)
2243 - (nrefy == 0 ? ascent + descent
2244 : nrefy == 1 ? descent - boff
2245 : nrefy == 2 ? 0
2246 : (ascent + descent) / 2));
2247 }
2248
2249 cmp->offsets[i * 2] = left;
2250 cmp->offsets[i * 2 + 1] = btm + descent;
2251
2252 /* Update the bounding box of the overall glyphs. */
2253 right = left + width;
2254 top = btm + descent + ascent;
2255 if (left < leftmost)
2256 leftmost = left;
2257 if (right > rightmost)
2258 rightmost = right;
2259 if (top > highest)
2260 highest = top;
2261 if (btm < lowest)
2262 lowest = btm;
2263 }
2264
2265 /* If there are glyphs whose x-offsets are negative,
2266 shift all glyphs to the right and make all x-offsets
2267 non-negative. */
2268 if (leftmost < 0)
2269 {
2270 for (i = 0; i < cmp->glyph_len; i++)
2271 cmp->offsets[i * 2] -= leftmost;
2272 rightmost -= leftmost;
2273 }
2274
2275 cmp->pixel_width = rightmost;
2276 cmp->ascent = highest;
2277 cmp->descent = - lowest;
2278 if (cmp->ascent < font_ascent)
2279 cmp->ascent = font_ascent;
2280 if (cmp->descent < font_descent)
2281 cmp->descent = font_descent;
2282 }
2283
2284 it->pixel_width = cmp->pixel_width;
2285 it->ascent = it->phys_ascent = cmp->ascent;
2286 it->descent = it->phys_descent = cmp->descent;
2287
2288 if (face->box != FACE_NO_BOX)
2289 {
2290 int thick = face->box_line_width;
2291
2292 if (thick > 0)
2293 {
2294 it->ascent += thick;
2295 it->descent += thick;
2296 }
2297 else
2298 thick = - thick;
2299
2300 if (it->start_of_box_run_p)
2301 it->pixel_width += thick;
2302 if (it->end_of_box_run_p)
2303 it->pixel_width += thick;
2304 }
2305
2306 /* If face has an overline, add the height of the overline
2307 (1 pixel) and a 1 pixel margin to the character height. */
2308 if (face->overline_p)
2309 it->ascent += 2;
2310
2311 take_vertical_position_into_account (it);
2312
2313 if (it->glyph_row)
2314 x_append_composite_glyph (it);
2315 }
2316 else if (it->what == IT_IMAGE)
2317 x_produce_image_glyph (it);
2318 else if (it->what == IT_STRETCH)
2319 x_produce_stretch_glyph (it);
2320
2321 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2322 because this isn't true for images with `:ascent 100'. */
2323 xassert (it->ascent >= 0 && it->descent >= 0);
2324 if (it->area == TEXT_AREA)
2325 it->current_x += it->pixel_width;
2326
2327 it->descent += it->extra_line_spacing;
2328
2329 it->max_ascent = max (it->max_ascent, it->ascent);
2330 it->max_descent = max (it->max_descent, it->descent);
2331 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2332 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2333 }
2334
2335
2336 /* Estimate the pixel height of the mode or top line on frame F.
2337 FACE_ID specifies what line's height to estimate. */
2338
2339 int
2340 x_estimate_mode_line_height (f, face_id)
2341 struct frame *f;
2342 enum face_id face_id;
2343 {
2344 int height = FONT_HEIGHT (FRAME_FONT (f));
2345
2346 /* This function is called so early when Emacs starts that the face
2347 cache and mode line face are not yet initialized. */
2348 if (FRAME_FACE_CACHE (f))
2349 {
2350 struct face *face = FACE_FROM_ID (f, face_id);
2351 if (face)
2352 {
2353 if (face->font)
2354 height = FONT_HEIGHT (face->font);
2355 if (face->box_line_width > 0)
2356 height += 2 * face->box_line_width;
2357 }
2358 }
2359
2360 return height;
2361 }
2362
2363 \f
2364 /***********************************************************************
2365 Glyph display
2366 ***********************************************************************/
2367
2368 /* A sequence of glyphs to be drawn in the same face.
2369
2370 This data structure is not really completely X specific, so it
2371 could possibly, at least partially, be useful for other systems. It
2372 is currently not part of the external redisplay interface because
2373 it's not clear what other systems will need. */
2374
2375 struct glyph_string
2376 {
2377 /* X-origin of the string. */
2378 int x;
2379
2380 /* Y-origin and y-position of the base line of this string. */
2381 int y, ybase;
2382
2383 /* The width of the string, not including a face extension. */
2384 int width;
2385
2386 /* The width of the string, including a face extension. */
2387 int background_width;
2388
2389 /* The height of this string. This is the height of the line this
2390 string is drawn in, and can be different from the height of the
2391 font the string is drawn in. */
2392 int height;
2393
2394 /* Number of pixels this string overwrites in front of its x-origin.
2395 This number is zero if the string has an lbearing >= 0; it is
2396 -lbearing, if the string has an lbearing < 0. */
2397 int left_overhang;
2398
2399 /* Number of pixels this string overwrites past its right-most
2400 nominal x-position, i.e. x + width. Zero if the string's
2401 rbearing is <= its nominal width, rbearing - width otherwise. */
2402 int right_overhang;
2403
2404 /* The frame on which the glyph string is drawn. */
2405 struct frame *f;
2406
2407 /* The window on which the glyph string is drawn. */
2408 struct window *w;
2409
2410 /* X display and window for convenience. */
2411 Display *display;
2412 Window window;
2413
2414 /* The glyph row for which this string was built. It determines the
2415 y-origin and height of the string. */
2416 struct glyph_row *row;
2417
2418 /* The area within row. */
2419 enum glyph_row_area area;
2420
2421 /* Characters to be drawn, and number of characters. */
2422 XChar2b *char2b;
2423 int nchars;
2424
2425 /* A face-override for drawing cursors, mouse face and similar. */
2426 enum draw_glyphs_face hl;
2427
2428 /* Face in which this string is to be drawn. */
2429 struct face *face;
2430
2431 /* Font in which this string is to be drawn. */
2432 XFontStruct *font;
2433
2434 /* Font info for this string. */
2435 struct font_info *font_info;
2436
2437 /* Non-null means this string describes (part of) a composition.
2438 All characters from char2b are drawn composed. */
2439 struct composition *cmp;
2440
2441 /* Index of this glyph string's first character in the glyph
2442 definition of CMP. If this is zero, this glyph string describes
2443 the first character of a composition. */
2444 int gidx;
2445
2446 /* 1 means this glyph strings face has to be drawn to the right end
2447 of the window's drawing area. */
2448 unsigned extends_to_end_of_line_p : 1;
2449
2450 /* 1 means the background of this string has been drawn. */
2451 unsigned background_filled_p : 1;
2452
2453 /* 1 means glyph string must be drawn with 16-bit functions. */
2454 unsigned two_byte_p : 1;
2455
2456 /* 1 means that the original font determined for drawing this glyph
2457 string could not be loaded. The member `font' has been set to
2458 the frame's default font in this case. */
2459 unsigned font_not_found_p : 1;
2460
2461 /* 1 means that the face in which this glyph string is drawn has a
2462 stipple pattern. */
2463 unsigned stippled_p : 1;
2464
2465 /* 1 means only the foreground of this glyph string must be drawn,
2466 and we should use the physical height of the line this glyph
2467 string appears in as clip rect. */
2468 unsigned for_overlaps_p : 1;
2469
2470 /* The GC to use for drawing this glyph string. */
2471 GC gc;
2472
2473 /* A pointer to the first glyph in the string. This glyph
2474 corresponds to char2b[0]. Needed to draw rectangles if
2475 font_not_found_p is 1. */
2476 struct glyph *first_glyph;
2477
2478 /* Image, if any. */
2479 struct image *img;
2480
2481 struct glyph_string *next, *prev;
2482 };
2483
2484
2485 #if GLYPH_DEBUG
2486
2487 static void
2488 x_dump_glyph_string (s)
2489 struct glyph_string *s;
2490 {
2491 fprintf (stderr, "glyph string\n");
2492 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2493 s->x, s->y, s->width, s->height);
2494 fprintf (stderr, " ybase = %d\n", s->ybase);
2495 fprintf (stderr, " hl = %d\n", s->hl);
2496 fprintf (stderr, " left overhang = %d, right = %d\n",
2497 s->left_overhang, s->right_overhang);
2498 fprintf (stderr, " nchars = %d\n", s->nchars);
2499 fprintf (stderr, " extends to end of line = %d\n",
2500 s->extends_to_end_of_line_p);
2501 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2502 fprintf (stderr, " bg width = %d\n", s->background_width);
2503 }
2504
2505 #endif /* GLYPH_DEBUG */
2506
2507
2508
2509 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2510 struct glyph_string **,
2511 struct glyph_string *,
2512 struct glyph_string *));
2513 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2514 struct glyph_string **,
2515 struct glyph_string *,
2516 struct glyph_string *));
2517 static void x_append_glyph_string P_ ((struct glyph_string **,
2518 struct glyph_string **,
2519 struct glyph_string *));
2520 static int x_left_overwritten P_ ((struct glyph_string *));
2521 static int x_left_overwriting P_ ((struct glyph_string *));
2522 static int x_right_overwritten P_ ((struct glyph_string *));
2523 static int x_right_overwriting P_ ((struct glyph_string *));
2524 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2525 int));
2526 static void x_init_glyph_string P_ ((struct glyph_string *,
2527 XChar2b *, struct window *,
2528 struct glyph_row *,
2529 enum glyph_row_area, int,
2530 enum draw_glyphs_face));
2531 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2532 enum glyph_row_area, int, int,
2533 enum draw_glyphs_face, int));
2534 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2535 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2536 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2537 int));
2538 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2539 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2540 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2541 static void x_draw_glyph_string P_ ((struct glyph_string *));
2542 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2543 static void x_set_cursor_gc P_ ((struct glyph_string *));
2544 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2545 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2546 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2547 int *, int *));
2548 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2549 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2550 unsigned long *, double, int));
2551 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2552 double, int, unsigned long));
2553 static void x_setup_relief_colors P_ ((struct glyph_string *));
2554 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2555 static void x_draw_image_relief P_ ((struct glyph_string *));
2556 static void x_draw_image_foreground P_ ((struct glyph_string *));
2557 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2558 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2559 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2560 int, int, int));
2561 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2562 int, int, int, int, XRectangle *));
2563 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2564 int, int, int, XRectangle *));
2565 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2566 enum glyph_row_area));
2567 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2568 struct glyph_row *,
2569 enum glyph_row_area, int, int));
2570
2571 #if GLYPH_DEBUG
2572 static void x_check_font P_ ((struct frame *, XFontStruct *));
2573 #endif
2574
2575
2576 /* Append the list of glyph strings with head H and tail T to the list
2577 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2578
2579 static INLINE void
2580 x_append_glyph_string_lists (head, tail, h, t)
2581 struct glyph_string **head, **tail;
2582 struct glyph_string *h, *t;
2583 {
2584 if (h)
2585 {
2586 if (*head)
2587 (*tail)->next = h;
2588 else
2589 *head = h;
2590 h->prev = *tail;
2591 *tail = t;
2592 }
2593 }
2594
2595
2596 /* Prepend the list of glyph strings with head H and tail T to the
2597 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2598 result. */
2599
2600 static INLINE void
2601 x_prepend_glyph_string_lists (head, tail, h, t)
2602 struct glyph_string **head, **tail;
2603 struct glyph_string *h, *t;
2604 {
2605 if (h)
2606 {
2607 if (*head)
2608 (*head)->prev = t;
2609 else
2610 *tail = t;
2611 t->next = *head;
2612 *head = h;
2613 }
2614 }
2615
2616
2617 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2618 Set *HEAD and *TAIL to the resulting list. */
2619
2620 static INLINE void
2621 x_append_glyph_string (head, tail, s)
2622 struct glyph_string **head, **tail;
2623 struct glyph_string *s;
2624 {
2625 s->next = s->prev = NULL;
2626 x_append_glyph_string_lists (head, tail, s, s);
2627 }
2628
2629
2630 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2631 face. */
2632
2633 static void
2634 x_set_cursor_gc (s)
2635 struct glyph_string *s;
2636 {
2637 if (s->font == FRAME_FONT (s->f)
2638 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2639 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2640 && !s->cmp)
2641 s->gc = s->f->output_data.x->cursor_gc;
2642 else
2643 {
2644 /* Cursor on non-default face: must merge. */
2645 XGCValues xgcv;
2646 unsigned long mask;
2647
2648 xgcv.background = s->f->output_data.x->cursor_pixel;
2649 xgcv.foreground = s->face->background;
2650
2651 /* If the glyph would be invisible, try a different foreground. */
2652 if (xgcv.foreground == xgcv.background)
2653 xgcv.foreground = s->face->foreground;
2654 if (xgcv.foreground == xgcv.background)
2655 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2656 if (xgcv.foreground == xgcv.background)
2657 xgcv.foreground = s->face->foreground;
2658
2659 /* Make sure the cursor is distinct from text in this face. */
2660 if (xgcv.background == s->face->background
2661 && xgcv.foreground == s->face->foreground)
2662 {
2663 xgcv.background = s->face->foreground;
2664 xgcv.foreground = s->face->background;
2665 }
2666
2667 IF_DEBUG (x_check_font (s->f, s->font));
2668 xgcv.font = s->font->fid;
2669 xgcv.graphics_exposures = False;
2670 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2671
2672 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2673 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2674 mask, &xgcv);
2675 else
2676 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2677 = XCreateGC (s->display, s->window, mask, &xgcv);
2678
2679 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2680 }
2681 }
2682
2683
2684 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2685
2686 static void
2687 x_set_mouse_face_gc (s)
2688 struct glyph_string *s;
2689 {
2690 int face_id;
2691 struct face *face;
2692
2693 /* What face has to be used last for the mouse face? */
2694 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2695 face = FACE_FROM_ID (s->f, face_id);
2696 if (face == NULL)
2697 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2698
2699 if (s->first_glyph->type == CHAR_GLYPH)
2700 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2701 else
2702 face_id = FACE_FOR_CHAR (s->f, face, 0);
2703 s->face = FACE_FROM_ID (s->f, face_id);
2704 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2705
2706 /* If font in this face is same as S->font, use it. */
2707 if (s->font == s->face->font)
2708 s->gc = s->face->gc;
2709 else
2710 {
2711 /* Otherwise construct scratch_cursor_gc with values from FACE
2712 but font FONT. */
2713 XGCValues xgcv;
2714 unsigned long mask;
2715
2716 xgcv.background = s->face->background;
2717 xgcv.foreground = s->face->foreground;
2718 IF_DEBUG (x_check_font (s->f, s->font));
2719 xgcv.font = s->font->fid;
2720 xgcv.graphics_exposures = False;
2721 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2722
2723 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2724 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2725 mask, &xgcv);
2726 else
2727 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2728 = XCreateGC (s->display, s->window, mask, &xgcv);
2729
2730 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2731 }
2732
2733 xassert (s->gc != 0);
2734 }
2735
2736
2737 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2738 Faces to use in the mode line have already been computed when the
2739 matrix was built, so there isn't much to do, here. */
2740
2741 static INLINE void
2742 x_set_mode_line_face_gc (s)
2743 struct glyph_string *s;
2744 {
2745 s->gc = s->face->gc;
2746 }
2747
2748
2749 /* Set S->gc of glyph string S for drawing that glyph string. Set
2750 S->stippled_p to a non-zero value if the face of S has a stipple
2751 pattern. */
2752
2753 static INLINE void
2754 x_set_glyph_string_gc (s)
2755 struct glyph_string *s;
2756 {
2757 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2758
2759 if (s->hl == DRAW_NORMAL_TEXT)
2760 {
2761 s->gc = s->face->gc;
2762 s->stippled_p = s->face->stipple != 0;
2763 }
2764 else if (s->hl == DRAW_INVERSE_VIDEO)
2765 {
2766 x_set_mode_line_face_gc (s);
2767 s->stippled_p = s->face->stipple != 0;
2768 }
2769 else if (s->hl == DRAW_CURSOR)
2770 {
2771 x_set_cursor_gc (s);
2772 s->stippled_p = 0;
2773 }
2774 else if (s->hl == DRAW_MOUSE_FACE)
2775 {
2776 x_set_mouse_face_gc (s);
2777 s->stippled_p = s->face->stipple != 0;
2778 }
2779 else if (s->hl == DRAW_IMAGE_RAISED
2780 || s->hl == DRAW_IMAGE_SUNKEN)
2781 {
2782 s->gc = s->face->gc;
2783 s->stippled_p = s->face->stipple != 0;
2784 }
2785 else
2786 {
2787 s->gc = s->face->gc;
2788 s->stippled_p = s->face->stipple != 0;
2789 }
2790
2791 /* GC must have been set. */
2792 xassert (s->gc != 0);
2793 }
2794
2795
2796 /* Return in *R the clipping rectangle for glyph string S. */
2797
2798 static void
2799 x_get_glyph_string_clip_rect (s, r)
2800 struct glyph_string *s;
2801 XRectangle *r;
2802 {
2803 if (s->row->full_width_p)
2804 {
2805 /* Draw full-width. X coordinates are relative to S->w->left. */
2806 int canon_x = CANON_X_UNIT (s->f);
2807
2808 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2809 r->width = XFASTINT (s->w->width) * canon_x;
2810
2811 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2812 {
2813 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2814 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2815 r->x -= width;
2816 }
2817
2818 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2819
2820 /* Unless displaying a mode or menu bar line, which are always
2821 fully visible, clip to the visible part of the row. */
2822 if (s->w->pseudo_window_p)
2823 r->height = s->row->visible_height;
2824 else
2825 r->height = s->height;
2826 }
2827 else
2828 {
2829 /* This is a text line that may be partially visible. */
2830 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2831 r->width = window_box_width (s->w, s->area);
2832 r->height = s->row->visible_height;
2833 }
2834
2835 /* If S draws overlapping rows, it's sufficient to use the top and
2836 bottom of the window for clipping because this glyph string
2837 intentionally draws over other lines. */
2838 if (s->for_overlaps_p)
2839 {
2840 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2841 r->height = window_text_bottom_y (s->w) - r->y;
2842 }
2843 else
2844 {
2845 /* Don't use S->y for clipping because it doesn't take partially
2846 visible lines into account. For example, it can be negative for
2847 partially visible lines at the top of a window. */
2848 if (!s->row->full_width_p
2849 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2850 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2851 else
2852 r->y = max (0, s->row->y);
2853
2854 /* If drawing a tool-bar window, draw it over the internal border
2855 at the top of the window. */
2856 if (s->w == XWINDOW (s->f->tool_bar_window))
2857 r->y -= s->f->output_data.x->internal_border_width;
2858 }
2859
2860 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2861 }
2862
2863
2864 /* Set clipping for output of glyph string S. S may be part of a mode
2865 line or menu if we don't have X toolkit support. */
2866
2867 static INLINE void
2868 x_set_glyph_string_clipping (s)
2869 struct glyph_string *s;
2870 {
2871 XRectangle r;
2872 x_get_glyph_string_clip_rect (s, &r);
2873 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2874 }
2875
2876
2877 /* Compute left and right overhang of glyph string S. If S is a glyph
2878 string for a composition, assume overhangs don't exist. */
2879
2880 static INLINE void
2881 x_compute_glyph_string_overhangs (s)
2882 struct glyph_string *s;
2883 {
2884 if (s->cmp == NULL
2885 && s->first_glyph->type == CHAR_GLYPH)
2886 {
2887 XCharStruct cs;
2888 int direction, font_ascent, font_descent;
2889 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2890 &font_ascent, &font_descent, &cs);
2891 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2892 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2893 }
2894 }
2895
2896
2897 /* Compute overhangs and x-positions for glyph string S and its
2898 predecessors, or successors. X is the starting x-position for S.
2899 BACKWARD_P non-zero means process predecessors. */
2900
2901 static void
2902 x_compute_overhangs_and_x (s, x, backward_p)
2903 struct glyph_string *s;
2904 int x;
2905 int backward_p;
2906 {
2907 if (backward_p)
2908 {
2909 while (s)
2910 {
2911 x_compute_glyph_string_overhangs (s);
2912 x -= s->width;
2913 s->x = x;
2914 s = s->prev;
2915 }
2916 }
2917 else
2918 {
2919 while (s)
2920 {
2921 x_compute_glyph_string_overhangs (s);
2922 s->x = x;
2923 x += s->width;
2924 s = s->next;
2925 }
2926 }
2927 }
2928
2929
2930 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2931 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2932 assumed to be zero. */
2933
2934 static void
2935 x_get_glyph_overhangs (glyph, f, left, right)
2936 struct glyph *glyph;
2937 struct frame *f;
2938 int *left, *right;
2939 {
2940 *left = *right = 0;
2941
2942 if (glyph->type == CHAR_GLYPH)
2943 {
2944 XFontStruct *font;
2945 struct face *face;
2946 struct font_info *font_info;
2947 XChar2b char2b;
2948 XCharStruct *pcm;
2949
2950 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2951 font = face->font;
2952 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2953 if (font
2954 && (pcm = x_per_char_metric (font, &char2b)))
2955 {
2956 if (pcm->rbearing > pcm->width)
2957 *right = pcm->rbearing - pcm->width;
2958 if (pcm->lbearing < 0)
2959 *left = -pcm->lbearing;
2960 }
2961 }
2962 }
2963
2964
2965 /* Return the index of the first glyph preceding glyph string S that
2966 is overwritten by S because of S's left overhang. Value is -1
2967 if no glyphs are overwritten. */
2968
2969 static int
2970 x_left_overwritten (s)
2971 struct glyph_string *s;
2972 {
2973 int k;
2974
2975 if (s->left_overhang)
2976 {
2977 int x = 0, i;
2978 struct glyph *glyphs = s->row->glyphs[s->area];
2979 int first = s->first_glyph - glyphs;
2980
2981 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2982 x -= glyphs[i].pixel_width;
2983
2984 k = i + 1;
2985 }
2986 else
2987 k = -1;
2988
2989 return k;
2990 }
2991
2992
2993 /* Return the index of the first glyph preceding glyph string S that
2994 is overwriting S because of its right overhang. Value is -1 if no
2995 glyph in front of S overwrites S. */
2996
2997 static int
2998 x_left_overwriting (s)
2999 struct glyph_string *s;
3000 {
3001 int i, k, x;
3002 struct glyph *glyphs = s->row->glyphs[s->area];
3003 int first = s->first_glyph - glyphs;
3004
3005 k = -1;
3006 x = 0;
3007 for (i = first - 1; i >= 0; --i)
3008 {
3009 int left, right;
3010 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3011 if (x + right > 0)
3012 k = i;
3013 x -= glyphs[i].pixel_width;
3014 }
3015
3016 return k;
3017 }
3018
3019
3020 /* Return the index of the last glyph following glyph string S that is
3021 not overwritten by S because of S's right overhang. Value is -1 if
3022 no such glyph is found. */
3023
3024 static int
3025 x_right_overwritten (s)
3026 struct glyph_string *s;
3027 {
3028 int k = -1;
3029
3030 if (s->right_overhang)
3031 {
3032 int x = 0, i;
3033 struct glyph *glyphs = s->row->glyphs[s->area];
3034 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3035 int end = s->row->used[s->area];
3036
3037 for (i = first; i < end && s->right_overhang > x; ++i)
3038 x += glyphs[i].pixel_width;
3039
3040 k = i;
3041 }
3042
3043 return k;
3044 }
3045
3046
3047 /* Return the index of the last glyph following glyph string S that
3048 overwrites S because of its left overhang. Value is negative
3049 if no such glyph is found. */
3050
3051 static int
3052 x_right_overwriting (s)
3053 struct glyph_string *s;
3054 {
3055 int i, k, x;
3056 int end = s->row->used[s->area];
3057 struct glyph *glyphs = s->row->glyphs[s->area];
3058 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3059
3060 k = -1;
3061 x = 0;
3062 for (i = first; i < end; ++i)
3063 {
3064 int left, right;
3065 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3066 if (x - left < 0)
3067 k = i;
3068 x += glyphs[i].pixel_width;
3069 }
3070
3071 return k;
3072 }
3073
3074
3075 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3076
3077 static INLINE void
3078 x_clear_glyph_string_rect (s, x, y, w, h)
3079 struct glyph_string *s;
3080 int x, y, w, h;
3081 {
3082 XGCValues xgcv;
3083 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3084 XSetForeground (s->display, s->gc, xgcv.background);
3085 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3086 XSetForeground (s->display, s->gc, xgcv.foreground);
3087 }
3088
3089
3090 /* Draw the background of glyph_string S. If S->background_filled_p
3091 is non-zero don't draw it. FORCE_P non-zero means draw the
3092 background even if it wouldn't be drawn normally. This is used
3093 when a string preceding S draws into the background of S, or S
3094 contains the first component of a composition. */
3095
3096 static void
3097 x_draw_glyph_string_background (s, force_p)
3098 struct glyph_string *s;
3099 int force_p;
3100 {
3101 /* Nothing to do if background has already been drawn or if it
3102 shouldn't be drawn in the first place. */
3103 if (!s->background_filled_p)
3104 {
3105 int box_line_width = max (s->face->box_line_width, 0);
3106
3107 if (s->stippled_p)
3108 {
3109 /* Fill background with a stipple pattern. */
3110 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3111 XFillRectangle (s->display, s->window, s->gc, s->x,
3112 s->y + box_line_width,
3113 s->background_width,
3114 s->height - 2 * box_line_width);
3115 XSetFillStyle (s->display, s->gc, FillSolid);
3116 s->background_filled_p = 1;
3117 }
3118 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
3119 || s->font_not_found_p
3120 || s->extends_to_end_of_line_p
3121 || force_p)
3122 {
3123 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
3124 s->background_width,
3125 s->height - 2 * box_line_width);
3126 s->background_filled_p = 1;
3127 }
3128 }
3129 }
3130
3131
3132 /* Draw the foreground of glyph string S. */
3133
3134 static void
3135 x_draw_glyph_string_foreground (s)
3136 struct glyph_string *s;
3137 {
3138 int i, x;
3139
3140 /* If first glyph of S has a left box line, start drawing the text
3141 of S to the right of that box line. */
3142 if (s->face->box != FACE_NO_BOX
3143 && s->first_glyph->left_box_line_p)
3144 x = s->x + abs (s->face->box_line_width);
3145 else
3146 x = s->x;
3147
3148 /* Draw characters of S as rectangles if S's font could not be
3149 loaded. */
3150 if (s->font_not_found_p)
3151 {
3152 for (i = 0; i < s->nchars; ++i)
3153 {
3154 struct glyph *g = s->first_glyph + i;
3155 XDrawRectangle (s->display, s->window,
3156 s->gc, x, s->y, g->pixel_width - 1,
3157 s->height - 1);
3158 x += g->pixel_width;
3159 }
3160 }
3161 else
3162 {
3163 char *char1b = (char *) s->char2b;
3164 int boff = s->font_info->baseline_offset;
3165
3166 if (s->font_info->vertical_centering)
3167 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3168
3169 /* If we can use 8-bit functions, condense S->char2b. */
3170 if (!s->two_byte_p)
3171 for (i = 0; i < s->nchars; ++i)
3172 char1b[i] = s->char2b[i].byte2;
3173
3174 /* Draw text with XDrawString if background has already been
3175 filled. Otherwise, use XDrawImageString. (Note that
3176 XDrawImageString is usually faster than XDrawString.) Always
3177 use XDrawImageString when drawing the cursor so that there is
3178 no chance that characters under a box cursor are invisible. */
3179 if (s->for_overlaps_p
3180 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3181 {
3182 /* Draw characters with 16-bit or 8-bit functions. */
3183 if (s->two_byte_p)
3184 XDrawString16 (s->display, s->window, s->gc, x,
3185 s->ybase - boff, s->char2b, s->nchars);
3186 else
3187 XDrawString (s->display, s->window, s->gc, x,
3188 s->ybase - boff, char1b, s->nchars);
3189 }
3190 else
3191 {
3192 if (s->two_byte_p)
3193 XDrawImageString16 (s->display, s->window, s->gc, x,
3194 s->ybase - boff, s->char2b, s->nchars);
3195 else
3196 XDrawImageString (s->display, s->window, s->gc, x,
3197 s->ybase - boff, char1b, s->nchars);
3198 }
3199 }
3200 }
3201
3202 /* Draw the foreground of composite glyph string S. */
3203
3204 static void
3205 x_draw_composite_glyph_string_foreground (s)
3206 struct glyph_string *s;
3207 {
3208 int i, x;
3209
3210 /* If first glyph of S has a left box line, start drawing the text
3211 of S to the right of that box line. */
3212 if (s->face->box != FACE_NO_BOX
3213 && s->first_glyph->left_box_line_p)
3214 x = s->x + abs (s->face->box_line_width);
3215 else
3216 x = s->x;
3217
3218 /* S is a glyph string for a composition. S->gidx is the index of
3219 the first character drawn for glyphs of this composition.
3220 S->gidx == 0 means we are drawing the very first character of
3221 this composition. */
3222
3223 /* Draw a rectangle for the composition if the font for the very
3224 first character of the composition could not be loaded. */
3225 if (s->font_not_found_p)
3226 {
3227 if (s->gidx == 0)
3228 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3229 s->width - 1, s->height - 1);
3230 }
3231 else
3232 {
3233 for (i = 0; i < s->nchars; i++, ++s->gidx)
3234 XDrawString16 (s->display, s->window, s->gc,
3235 x + s->cmp->offsets[s->gidx * 2],
3236 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3237 s->char2b + i, 1);
3238 }
3239 }
3240
3241
3242 #ifdef USE_X_TOOLKIT
3243
3244 static struct frame *x_frame_of_widget P_ ((Widget));
3245 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
3246 XrmValue *, XrmValue *, XtPointer *));
3247 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
3248 XrmValue *, Cardinal *));
3249
3250
3251 /* Return the frame on which widget WIDGET is used.. Abort if frame
3252 cannot be determined. */
3253
3254 static struct frame *
3255 x_frame_of_widget (widget)
3256 Widget widget;
3257 {
3258 struct x_display_info *dpyinfo;
3259 Lisp_Object tail;
3260 struct frame *f;
3261
3262 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3263
3264 /* Find the top-level shell of the widget. Note that this function
3265 can be called when the widget is not yet realized, so XtWindow
3266 (widget) == 0. That's the reason we can't simply use
3267 x_any_window_to_frame. */
3268 while (!XtIsTopLevelShell (widget))
3269 widget = XtParent (widget);
3270
3271 /* Look for a frame with that top-level widget. Allocate the color
3272 on that frame to get the right gamma correction value. */
3273 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3274 if (GC_FRAMEP (XCAR (tail))
3275 && (f = XFRAME (XCAR (tail)),
3276 (f->output_data.nothing != 1
3277 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3278 && f->output_data.x->widget == widget)
3279 return f;
3280
3281 abort ();
3282 }
3283
3284
3285 /* Allocate the color COLOR->pixel on the screen and display of
3286 widget WIDGET in colormap CMAP. If an exact match cannot be
3287 allocated, try the nearest color available. Value is non-zero
3288 if successful. This is called from lwlib. */
3289
3290 int
3291 x_alloc_nearest_color_for_widget (widget, cmap, color)
3292 Widget widget;
3293 Colormap cmap;
3294 XColor *color;
3295 {
3296 struct frame *f = x_frame_of_widget (widget);
3297 return x_alloc_nearest_color (f, cmap, color);
3298 }
3299
3300
3301 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3302 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3303 If this produces the same color as PIXEL, try a color where all RGB
3304 values have DELTA added. Return the allocated color in *PIXEL.
3305 DISPLAY is the X display, CMAP is the colormap to operate on.
3306 Value is non-zero if successful. */
3307
3308 int
3309 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3310 Widget widget;
3311 Display *display;
3312 Colormap cmap;
3313 unsigned long *pixel;
3314 double factor;
3315 int delta;
3316 {
3317 struct frame *f = x_frame_of_widget (widget);
3318 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3319 }
3320
3321
3322 /* Structure specifying which arguments should be passed by Xt to
3323 cvt_string_to_pixel. We want the widget's screen and colormap. */
3324
3325 static XtConvertArgRec cvt_string_to_pixel_args[] =
3326 {
3327 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
3328 sizeof (Screen *)},
3329 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
3330 sizeof (Colormap)}
3331 };
3332
3333
3334 /* The address of this variable is returned by
3335 cvt_string_to_pixel. */
3336
3337 static Pixel cvt_string_to_pixel_value;
3338
3339
3340 /* Convert a color name to a pixel color.
3341
3342 DPY is the display we are working on.
3343
3344 ARGS is an array of *NARGS XrmValue structures holding additional
3345 information about the widget for which the conversion takes place.
3346 The contents of this array are determined by the specification
3347 in cvt_string_to_pixel_args.
3348
3349 FROM is a pointer to an XrmValue which points to the color name to
3350 convert. TO is an XrmValue in which to return the pixel color.
3351
3352 CLOSURE_RET is a pointer to user-data, in which we record if
3353 we allocated the color or not.
3354
3355 Value is True if successful, False otherwise. */
3356
3357 static Boolean
3358 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
3359 Display *dpy;
3360 XrmValue *args;
3361 Cardinal *nargs;
3362 XrmValue *from, *to;
3363 XtPointer *closure_ret;
3364 {
3365 Screen *screen;
3366 Colormap cmap;
3367 Pixel pixel;
3368 String color_name;
3369 XColor color;
3370
3371 if (*nargs != 2)
3372 {
3373 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3374 "wrongParameters", "cvt_string_to_pixel",
3375 "XtToolkitError",
3376 "Screen and colormap args required", NULL, NULL);
3377 return False;
3378 }
3379
3380 screen = *(Screen **) args[0].addr;
3381 cmap = *(Colormap *) args[1].addr;
3382 color_name = (String) from->addr;
3383
3384 if (strcmp (color_name, XtDefaultBackground) == 0)
3385 {
3386 *closure_ret = (XtPointer) False;
3387 pixel = WhitePixelOfScreen (screen);
3388 }
3389 else if (strcmp (color_name, XtDefaultForeground) == 0)
3390 {
3391 *closure_ret = (XtPointer) False;
3392 pixel = BlackPixelOfScreen (screen);
3393 }
3394 else if (XParseColor (dpy, cmap, color_name, &color)
3395 && x_alloc_nearest_color_1 (dpy, cmap, &color))
3396 {
3397 pixel = color.pixel;
3398 *closure_ret = (XtPointer) True;
3399 }
3400 else
3401 {
3402 String params[1];
3403 Cardinal nparams = 1;
3404
3405 params[0] = color_name;
3406 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3407 "badValue", "cvt_string_to_pixel",
3408 "XtToolkitError", "Invalid color `%s'",
3409 params, &nparams);
3410 return False;
3411 }
3412
3413 if (to->addr != NULL)
3414 {
3415 if (to->size < sizeof (Pixel))
3416 {
3417 to->size = sizeof (Pixel);
3418 return False;
3419 }
3420
3421 *(Pixel *) to->addr = pixel;
3422 }
3423 else
3424 {
3425 cvt_string_to_pixel_value = pixel;
3426 to->addr = (XtPointer) &cvt_string_to_pixel_value;
3427 }
3428
3429 to->size = sizeof (Pixel);
3430 return True;
3431 }
3432
3433
3434 /* Free a pixel color which was previously allocated via
3435 cvt_string_to_pixel. This is registered as the destructor
3436 for this type of resource via XtSetTypeConverter.
3437
3438 APP is the application context in which we work.
3439
3440 TO is a pointer to an XrmValue holding the color to free.
3441 CLOSURE is the value we stored in CLOSURE_RET for this color
3442 in cvt_string_to_pixel.
3443
3444 ARGS and NARGS are like for cvt_string_to_pixel. */
3445
3446 static void
3447 cvt_pixel_dtor (app, to, closure, args, nargs)
3448 XtAppContext app;
3449 XrmValuePtr to;
3450 XtPointer closure;
3451 XrmValuePtr args;
3452 Cardinal *nargs;
3453 {
3454 if (*nargs != 2)
3455 {
3456 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
3457 "XtToolkitError",
3458 "Screen and colormap arguments required",
3459 NULL, NULL);
3460 }
3461 else if (closure != NULL)
3462 {
3463 /* We did allocate the pixel, so free it. */
3464 Screen *screen = *(Screen **) args[0].addr;
3465 Colormap cmap = *(Colormap *) args[1].addr;
3466 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
3467 (Pixel *) to->addr, 1);
3468 }
3469 }
3470
3471
3472 #endif /* USE_X_TOOLKIT */
3473
3474
3475 /* Value is an array of XColor structures for the contents of the
3476 color map of display DPY. Set *NCELLS to the size of the array.
3477 Note that this probably shouldn't be called for large color maps,
3478 say a 24-bit TrueColor map. */
3479
3480 static const XColor *
3481 x_color_cells (dpy, ncells)
3482 Display *dpy;
3483 int *ncells;
3484 {
3485 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3486
3487 if (dpyinfo->color_cells == NULL)
3488 {
3489 Screen *screen = dpyinfo->screen;
3490 int i;
3491
3492 dpyinfo->ncolor_cells
3493 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
3494 dpyinfo->color_cells
3495 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3496 * sizeof *dpyinfo->color_cells);
3497
3498 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3499 dpyinfo->color_cells[i].pixel = i;
3500
3501 XQueryColors (dpy, dpyinfo->cmap,
3502 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3503 }
3504
3505 *ncells = dpyinfo->ncolor_cells;
3506 return dpyinfo->color_cells;
3507 }
3508
3509
3510 /* On frame F, translate pixel colors to RGB values for the NCOLORS
3511 colors in COLORS. Use cached information, if available. */
3512
3513 void
3514 x_query_colors (f, colors, ncolors)
3515 struct frame *f;
3516 XColor *colors;
3517 int ncolors;
3518 {
3519 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3520
3521 if (dpyinfo->color_cells)
3522 {
3523 int i;
3524 for (i = 0; i < ncolors; ++i)
3525 {
3526 unsigned long pixel = colors[i].pixel;
3527 xassert (pixel < dpyinfo->ncolor_cells);
3528 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3529 colors[i] = dpyinfo->color_cells[pixel];
3530 }
3531 }
3532 else
3533 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3534 }
3535
3536
3537 /* On frame F, translate pixel color to RGB values for the color in
3538 COLOR. Use cached information, if available. */
3539
3540 void
3541 x_query_color (f, color)
3542 struct frame *f;
3543 XColor *color;
3544 {
3545 x_query_colors (f, color, 1);
3546 }
3547
3548
3549 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
3550 exact match can't be allocated, try the nearest color available.
3551 Value is non-zero if successful. Set *COLOR to the color
3552 allocated. */
3553
3554 static int
3555 x_alloc_nearest_color_1 (dpy, cmap, color)
3556 Display *dpy;
3557 Colormap cmap;
3558 XColor *color;
3559 {
3560 int rc;
3561
3562 rc = XAllocColor (dpy, cmap, color);
3563 if (rc == 0)
3564 {
3565 /* If we got to this point, the colormap is full, so we're going
3566 to try to get the next closest color. The algorithm used is
3567 a least-squares matching, which is what X uses for closest
3568 color matching with StaticColor visuals. */
3569 int nearest, i;
3570 unsigned long nearest_delta = ~0;
3571 int ncells;
3572 const XColor *cells = x_color_cells (dpy, &ncells);
3573
3574 for (nearest = i = 0; i < ncells; ++i)
3575 {
3576 long dred = (color->red >> 8) - (cells[i].red >> 8);
3577 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3578 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3579 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3580
3581 if (delta < nearest_delta)
3582 {
3583 nearest = i;
3584 nearest_delta = delta;
3585 }
3586 }
3587
3588 color->red = cells[nearest].red;
3589 color->green = cells[nearest].green;
3590 color->blue = cells[nearest].blue;
3591 rc = XAllocColor (dpy, cmap, color);
3592 }
3593 else
3594 {
3595 /* If allocation succeeded, and the allocated pixel color is not
3596 equal to a cached pixel color recorded earlier, there was a
3597 change in the colormap, so clear the color cache. */
3598 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3599 XColor *cached_color;
3600
3601 if (dpyinfo->color_cells
3602 && (cached_color = &dpyinfo->color_cells[color->pixel],
3603 (cached_color->red != color->red
3604 || cached_color->blue != color->blue
3605 || cached_color->green != color->green)))
3606 {
3607 xfree (dpyinfo->color_cells);
3608 dpyinfo->color_cells = NULL;
3609 dpyinfo->ncolor_cells = 0;
3610 }
3611 }
3612
3613 #ifdef DEBUG_X_COLORS
3614 if (rc)
3615 register_color (color->pixel);
3616 #endif /* DEBUG_X_COLORS */
3617
3618 return rc;
3619 }
3620
3621
3622 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
3623 exact match can't be allocated, try the nearest color available.
3624 Value is non-zero if successful. Set *COLOR to the color
3625 allocated. */
3626
3627 int
3628 x_alloc_nearest_color (f, cmap, color)
3629 struct frame *f;
3630 Colormap cmap;
3631 XColor *color;
3632 {
3633 gamma_correct (f, color);
3634 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
3635 }
3636
3637
3638 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3639 It's necessary to do this instead of just using PIXEL directly to
3640 get color reference counts right. */
3641
3642 unsigned long
3643 x_copy_color (f, pixel)
3644 struct frame *f;
3645 unsigned long pixel;
3646 {
3647 XColor color;
3648
3649 color.pixel = pixel;
3650 BLOCK_INPUT;
3651 x_query_color (f, &color);
3652 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3653 UNBLOCK_INPUT;
3654 #ifdef DEBUG_X_COLORS
3655 register_color (pixel);
3656 #endif
3657 return color.pixel;
3658 }
3659
3660
3661 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3662 It's necessary to do this instead of just using PIXEL directly to
3663 get color reference counts right. */
3664
3665 unsigned long
3666 x_copy_dpy_color (dpy, cmap, pixel)
3667 Display *dpy;
3668 Colormap cmap;
3669 unsigned long pixel;
3670 {
3671 XColor color;
3672
3673 color.pixel = pixel;
3674 BLOCK_INPUT;
3675 XQueryColor (dpy, cmap, &color);
3676 XAllocColor (dpy, cmap, &color);
3677 UNBLOCK_INPUT;
3678 #ifdef DEBUG_X_COLORS
3679 register_color (pixel);
3680 #endif
3681 return color.pixel;
3682 }
3683
3684
3685 /* Brightness beyond which a color won't have its highlight brightness
3686 boosted.
3687
3688 Nominally, highlight colors for `3d' faces are calculated by
3689 brightening an object's color by a constant scale factor, but this
3690 doesn't yield good results for dark colors, so for colors who's
3691 brightness is less than this value (on a scale of 0-65535) have an
3692 use an additional additive factor.
3693
3694 The value here is set so that the default menu-bar/mode-line color
3695 (grey75) will not have its highlights changed at all. */
3696 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3697
3698
3699 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3700 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3701 If this produces the same color as PIXEL, try a color where all RGB
3702 values have DELTA added. Return the allocated color in *PIXEL.
3703 DISPLAY is the X display, CMAP is the colormap to operate on.
3704 Value is non-zero if successful. */
3705
3706 static int
3707 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3708 struct frame *f;
3709 Display *display;
3710 Colormap cmap;
3711 unsigned long *pixel;
3712 double factor;
3713 int delta;
3714 {
3715 XColor color, new;
3716 long bright;
3717 int success_p;
3718
3719 /* Get RGB color values. */
3720 color.pixel = *pixel;
3721 x_query_color (f, &color);
3722
3723 /* Change RGB values by specified FACTOR. Avoid overflow! */
3724 xassert (factor >= 0);
3725 new.red = min (0xffff, factor * color.red);
3726 new.green = min (0xffff, factor * color.green);
3727 new.blue = min (0xffff, factor * color.blue);
3728
3729 /* Calculate brightness of COLOR. */
3730 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
3731
3732 /* We only boost colors that are darker than
3733 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3734 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3735 /* Make an additive adjustment to NEW, because it's dark enough so
3736 that scaling by FACTOR alone isn't enough. */
3737 {
3738 /* How far below the limit this color is (0 - 1, 1 being darker). */
3739 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3740 /* The additive adjustment. */
3741 int min_delta = delta * dimness * factor / 2;
3742
3743 if (factor < 1)
3744 {
3745 new.red = max (0, new.red - min_delta);
3746 new.green = max (0, new.green - min_delta);
3747 new.blue = max (0, new.blue - min_delta);
3748 }
3749 else
3750 {
3751 new.red = min (0xffff, min_delta + new.red);
3752 new.green = min (0xffff, min_delta + new.green);
3753 new.blue = min (0xffff, min_delta + new.blue);
3754 }
3755 }
3756
3757 /* Try to allocate the color. */
3758 success_p = x_alloc_nearest_color (f, cmap, &new);
3759 if (success_p)
3760 {
3761 if (new.pixel == *pixel)
3762 {
3763 /* If we end up with the same color as before, try adding
3764 delta to the RGB values. */
3765 x_free_colors (f, &new.pixel, 1);
3766
3767 new.red = min (0xffff, delta + color.red);
3768 new.green = min (0xffff, delta + color.green);
3769 new.blue = min (0xffff, delta + color.blue);
3770 success_p = x_alloc_nearest_color (f, cmap, &new);
3771 }
3772 else
3773 success_p = 1;
3774 *pixel = new.pixel;
3775 }
3776
3777 return success_p;
3778 }
3779
3780
3781 /* Set up the foreground color for drawing relief lines of glyph
3782 string S. RELIEF is a pointer to a struct relief containing the GC
3783 with which lines will be drawn. Use a color that is FACTOR or
3784 DELTA lighter or darker than the relief's background which is found
3785 in S->f->output_data.x->relief_background. If such a color cannot
3786 be allocated, use DEFAULT_PIXEL, instead. */
3787
3788 static void
3789 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3790 struct frame *f;
3791 struct relief *relief;
3792 double factor;
3793 int delta;
3794 unsigned long default_pixel;
3795 {
3796 XGCValues xgcv;
3797 struct x_output *di = f->output_data.x;
3798 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3799 unsigned long pixel;
3800 unsigned long background = di->relief_background;
3801 Colormap cmap = FRAME_X_COLORMAP (f);
3802 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3803 Display *dpy = FRAME_X_DISPLAY (f);
3804
3805 xgcv.graphics_exposures = False;
3806 xgcv.line_width = 1;
3807
3808 /* Free previously allocated color. The color cell will be reused
3809 when it has been freed as many times as it was allocated, so this
3810 doesn't affect faces using the same colors. */
3811 if (relief->gc
3812 && relief->allocated_p)
3813 {
3814 x_free_colors (f, &relief->pixel, 1);
3815 relief->allocated_p = 0;
3816 }
3817
3818 /* Allocate new color. */
3819 xgcv.foreground = default_pixel;
3820 pixel = background;
3821 if (dpyinfo->n_planes != 1
3822 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3823 {
3824 relief->allocated_p = 1;
3825 xgcv.foreground = relief->pixel = pixel;
3826 }
3827
3828 if (relief->gc == 0)
3829 {
3830 xgcv.stipple = dpyinfo->gray;
3831 mask |= GCStipple;
3832 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3833 }
3834 else
3835 XChangeGC (dpy, relief->gc, mask, &xgcv);
3836 }
3837
3838
3839 /* Set up colors for the relief lines around glyph string S. */
3840
3841 static void
3842 x_setup_relief_colors (s)
3843 struct glyph_string *s;
3844 {
3845 struct x_output *di = s->f->output_data.x;
3846 unsigned long color;
3847
3848 if (s->face->use_box_color_for_shadows_p)
3849 color = s->face->box_color;
3850 else if (s->first_glyph->type == IMAGE_GLYPH
3851 && s->img->pixmap
3852 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
3853 color = IMAGE_BACKGROUND (s->img, s->f, 0);
3854 else
3855 {
3856 XGCValues xgcv;
3857
3858 /* Get the background color of the face. */
3859 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3860 color = xgcv.background;
3861 }
3862
3863 if (di->white_relief.gc == 0
3864 || color != di->relief_background)
3865 {
3866 di->relief_background = color;
3867 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3868 WHITE_PIX_DEFAULT (s->f));
3869 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3870 BLACK_PIX_DEFAULT (s->f));
3871 }
3872 }
3873
3874
3875 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3876 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3877 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3878 relief. LEFT_P non-zero means draw a relief on the left side of
3879 the rectangle. RIGHT_P non-zero means draw a relief on the right
3880 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3881 when drawing. */
3882
3883 static void
3884 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3885 raised_p, left_p, right_p, clip_rect)
3886 struct frame *f;
3887 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3888 XRectangle *clip_rect;
3889 {
3890 Display *dpy = FRAME_X_DISPLAY (f);
3891 Window window = FRAME_X_WINDOW (f);
3892 int i;
3893 GC gc;
3894
3895 if (raised_p)
3896 gc = f->output_data.x->white_relief.gc;
3897 else
3898 gc = f->output_data.x->black_relief.gc;
3899 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
3900
3901 /* Top. */
3902 for (i = 0; i < width; ++i)
3903 XDrawLine (dpy, window, gc,
3904 left_x + i * left_p, top_y + i,
3905 right_x + 1 - i * right_p, top_y + i);
3906
3907 /* Left. */
3908 if (left_p)
3909 for (i = 0; i < width; ++i)
3910 XDrawLine (dpy, window, gc,
3911 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
3912
3913 XSetClipMask (dpy, gc, None);
3914 if (raised_p)
3915 gc = f->output_data.x->black_relief.gc;
3916 else
3917 gc = f->output_data.x->white_relief.gc;
3918 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
3919
3920 /* Bottom. */
3921 for (i = 0; i < width; ++i)
3922 XDrawLine (dpy, window, gc,
3923 left_x + i * left_p, bottom_y - i,
3924 right_x + 1 - i * right_p, bottom_y - i);
3925
3926 /* Right. */
3927 if (right_p)
3928 for (i = 0; i < width; ++i)
3929 XDrawLine (dpy, window, gc,
3930 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3931
3932 XSetClipMask (dpy, gc, None);
3933 }
3934
3935
3936 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3937 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3938 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3939 left side of the rectangle. RIGHT_P non-zero means draw a line
3940 on the right side of the rectangle. CLIP_RECT is the clipping
3941 rectangle to use when drawing. */
3942
3943 static void
3944 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3945 left_p, right_p, clip_rect)
3946 struct glyph_string *s;
3947 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3948 XRectangle *clip_rect;
3949 {
3950 XGCValues xgcv;
3951
3952 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3953 XSetForeground (s->display, s->gc, s->face->box_color);
3954 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3955
3956 /* Top. */
3957 XFillRectangle (s->display, s->window, s->gc,
3958 left_x, top_y, right_x - left_x + 1, width);
3959
3960 /* Left. */
3961 if (left_p)
3962 XFillRectangle (s->display, s->window, s->gc,
3963 left_x, top_y, width, bottom_y - top_y + 1);
3964
3965 /* Bottom. */
3966 XFillRectangle (s->display, s->window, s->gc,
3967 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3968
3969 /* Right. */
3970 if (right_p)
3971 XFillRectangle (s->display, s->window, s->gc,
3972 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3973
3974 XSetForeground (s->display, s->gc, xgcv.foreground);
3975 XSetClipMask (s->display, s->gc, None);
3976 }
3977
3978
3979 /* Draw a box around glyph string S. */
3980
3981 static void
3982 x_draw_glyph_string_box (s)
3983 struct glyph_string *s;
3984 {
3985 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3986 int left_p, right_p;
3987 struct glyph *last_glyph;
3988 XRectangle clip_rect;
3989
3990 last_x = window_box_right (s->w, s->area);
3991 if (s->row->full_width_p
3992 && !s->w->pseudo_window_p)
3993 {
3994 last_x += FRAME_X_RIGHT_FRINGE_WIDTH (s->f);
3995 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3996 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3997 }
3998
3999 /* The glyph that may have a right box line. */
4000 last_glyph = (s->cmp || s->img
4001 ? s->first_glyph
4002 : s->first_glyph + s->nchars - 1);
4003
4004 width = abs (s->face->box_line_width);
4005 raised_p = s->face->box == FACE_RAISED_BOX;
4006 left_x = s->x;
4007 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
4008 ? last_x - 1
4009 : min (last_x, s->x + s->background_width) - 1);
4010 top_y = s->y;
4011 bottom_y = top_y + s->height - 1;
4012
4013 left_p = (s->first_glyph->left_box_line_p
4014 || (s->hl == DRAW_MOUSE_FACE
4015 && (s->prev == NULL
4016 || s->prev->hl != s->hl)));
4017 right_p = (last_glyph->right_box_line_p
4018 || (s->hl == DRAW_MOUSE_FACE
4019 && (s->next == NULL
4020 || s->next->hl != s->hl)));
4021
4022 x_get_glyph_string_clip_rect (s, &clip_rect);
4023
4024 if (s->face->box == FACE_SIMPLE_BOX)
4025 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
4026 left_p, right_p, &clip_rect);
4027 else
4028 {
4029 x_setup_relief_colors (s);
4030 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
4031 width, raised_p, left_p, right_p, &clip_rect);
4032 }
4033 }
4034
4035
4036 /* Draw foreground of image glyph string S. */
4037
4038 static void
4039 x_draw_image_foreground (s)
4040 struct glyph_string *s;
4041 {
4042 int x;
4043 int y = s->ybase - image_ascent (s->img, s->face);
4044
4045 /* If first glyph of S has a left box line, start drawing it to the
4046 right of that line. */
4047 if (s->face->box != FACE_NO_BOX
4048 && s->first_glyph->left_box_line_p)
4049 x = s->x + abs (s->face->box_line_width);
4050 else
4051 x = s->x;
4052
4053 /* If there is a margin around the image, adjust x- and y-position
4054 by that margin. */
4055 x += s->img->hmargin;
4056 y += s->img->vmargin;
4057
4058 if (s->img->pixmap)
4059 {
4060 if (s->img->mask)
4061 {
4062 /* We can't set both a clip mask and use XSetClipRectangles
4063 because the latter also sets a clip mask. We also can't
4064 trust on the shape extension to be available
4065 (XShapeCombineRegion). So, compute the rectangle to draw
4066 manually. */
4067 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4068 | GCFunction);
4069 XGCValues xgcv;
4070 XRectangle clip_rect, image_rect, r;
4071
4072 xgcv.clip_mask = s->img->mask;
4073 xgcv.clip_x_origin = x;
4074 xgcv.clip_y_origin = y;
4075 xgcv.function = GXcopy;
4076 XChangeGC (s->display, s->gc, mask, &xgcv);
4077
4078 x_get_glyph_string_clip_rect (s, &clip_rect);
4079 image_rect.x = x;
4080 image_rect.y = y;
4081 image_rect.width = s->img->width;
4082 image_rect.height = s->img->height;
4083 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4084 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4085 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4086 }
4087 else
4088 {
4089 XRectangle clip_rect, image_rect, r;
4090
4091 x_get_glyph_string_clip_rect (s, &clip_rect);
4092 image_rect.x = x;
4093 image_rect.y = y;
4094 image_rect.width = s->img->width;
4095 image_rect.height = s->img->height;
4096 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4097 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4098 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4099
4100 /* When the image has a mask, we can expect that at
4101 least part of a mouse highlight or a block cursor will
4102 be visible. If the image doesn't have a mask, make
4103 a block cursor visible by drawing a rectangle around
4104 the image. I believe it's looking better if we do
4105 nothing here for mouse-face. */
4106 if (s->hl == DRAW_CURSOR)
4107 XDrawRectangle (s->display, s->window, s->gc, x, y,
4108 s->img->width - 1, s->img->height - 1);
4109 }
4110 }
4111 else
4112 /* Draw a rectangle if image could not be loaded. */
4113 XDrawRectangle (s->display, s->window, s->gc, x, y,
4114 s->img->width - 1, s->img->height - 1);
4115 }
4116
4117
4118 /* Draw a relief around the image glyph string S. */
4119
4120 static void
4121 x_draw_image_relief (s)
4122 struct glyph_string *s;
4123 {
4124 int x0, y0, x1, y1, thick, raised_p;
4125 XRectangle r;
4126 int x;
4127 int y = s->ybase - image_ascent (s->img, s->face);
4128
4129 /* If first glyph of S has a left box line, start drawing it to the
4130 right of that line. */
4131 if (s->face->box != FACE_NO_BOX
4132 && s->first_glyph->left_box_line_p)
4133 x = s->x + abs (s->face->box_line_width);
4134 else
4135 x = s->x;
4136
4137 /* If there is a margin around the image, adjust x- and y-position
4138 by that margin. */
4139 x += s->img->hmargin;
4140 y += s->img->vmargin;
4141
4142 if (s->hl == DRAW_IMAGE_SUNKEN
4143 || s->hl == DRAW_IMAGE_RAISED)
4144 {
4145 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
4146 raised_p = s->hl == DRAW_IMAGE_RAISED;
4147 }
4148 else
4149 {
4150 thick = abs (s->img->relief);
4151 raised_p = s->img->relief > 0;
4152 }
4153
4154 x0 = x - thick;
4155 y0 = y - thick;
4156 x1 = x + s->img->width + thick - 1;
4157 y1 = y + s->img->height + thick - 1;
4158
4159 x_setup_relief_colors (s);
4160 x_get_glyph_string_clip_rect (s, &r);
4161 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
4162 }
4163
4164
4165 /* Draw the foreground of image glyph string S to PIXMAP. */
4166
4167 static void
4168 x_draw_image_foreground_1 (s, pixmap)
4169 struct glyph_string *s;
4170 Pixmap pixmap;
4171 {
4172 int x;
4173 int y = s->ybase - s->y - image_ascent (s->img, s->face);
4174
4175 /* If first glyph of S has a left box line, start drawing it to the
4176 right of that line. */
4177 if (s->face->box != FACE_NO_BOX
4178 && s->first_glyph->left_box_line_p)
4179 x = abs (s->face->box_line_width);
4180 else
4181 x = 0;
4182
4183 /* If there is a margin around the image, adjust x- and y-position
4184 by that margin. */
4185 x += s->img->hmargin;
4186 y += s->img->vmargin;
4187
4188 if (s->img->pixmap)
4189 {
4190 if (s->img->mask)
4191 {
4192 /* We can't set both a clip mask and use XSetClipRectangles
4193 because the latter also sets a clip mask. We also can't
4194 trust on the shape extension to be available
4195 (XShapeCombineRegion). So, compute the rectangle to draw
4196 manually. */
4197 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4198 | GCFunction);
4199 XGCValues xgcv;
4200
4201 xgcv.clip_mask = s->img->mask;
4202 xgcv.clip_x_origin = x;
4203 xgcv.clip_y_origin = y;
4204 xgcv.function = GXcopy;
4205 XChangeGC (s->display, s->gc, mask, &xgcv);
4206
4207 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4208 0, 0, s->img->width, s->img->height, x, y);
4209 XSetClipMask (s->display, s->gc, None);
4210 }
4211 else
4212 {
4213 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4214 0, 0, s->img->width, s->img->height, x, y);
4215
4216 /* When the image has a mask, we can expect that at
4217 least part of a mouse highlight or a block cursor will
4218 be visible. If the image doesn't have a mask, make
4219 a block cursor visible by drawing a rectangle around
4220 the image. I believe it's looking better if we do
4221 nothing here for mouse-face. */
4222 if (s->hl == DRAW_CURSOR)
4223 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4224 s->img->width - 1, s->img->height - 1);
4225 }
4226 }
4227 else
4228 /* Draw a rectangle if image could not be loaded. */
4229 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4230 s->img->width - 1, s->img->height - 1);
4231 }
4232
4233
4234 /* Draw part of the background of glyph string S. X, Y, W, and H
4235 give the rectangle to draw. */
4236
4237 static void
4238 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4239 struct glyph_string *s;
4240 int x, y, w, h;
4241 {
4242 if (s->stippled_p)
4243 {
4244 /* Fill background with a stipple pattern. */
4245 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4246 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4247 XSetFillStyle (s->display, s->gc, FillSolid);
4248 }
4249 else
4250 x_clear_glyph_string_rect (s, x, y, w, h);
4251 }
4252
4253
4254 /* Draw image glyph string S.
4255
4256 s->y
4257 s->x +-------------------------
4258 | s->face->box
4259 |
4260 | +-------------------------
4261 | | s->img->margin
4262 | |
4263 | | +-------------------
4264 | | | the image
4265
4266 */
4267
4268 static void
4269 x_draw_image_glyph_string (s)
4270 struct glyph_string *s;
4271 {
4272 int x, y;
4273 int box_line_hwidth = abs (s->face->box_line_width);
4274 int box_line_vwidth = max (s->face->box_line_width, 0);
4275 int height;
4276 Pixmap pixmap = None;
4277
4278 height = s->height - 2 * box_line_vwidth;
4279
4280 /* Fill background with face under the image. Do it only if row is
4281 taller than image or if image has a clip mask to reduce
4282 flickering. */
4283 s->stippled_p = s->face->stipple != 0;
4284 if (height > s->img->height
4285 || s->img->hmargin
4286 || s->img->vmargin
4287 || s->img->mask
4288 || s->img->pixmap == 0
4289 || s->width != s->background_width)
4290 {
4291 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4292 x = s->x + box_line_hwidth;
4293 else
4294 x = s->x;
4295
4296 y = s->y + box_line_vwidth;
4297
4298 if (s->img->mask)
4299 {
4300 /* Create a pixmap as large as the glyph string. Fill it
4301 with the background color. Copy the image to it, using
4302 its mask. Copy the temporary pixmap to the display. */
4303 Screen *screen = FRAME_X_SCREEN (s->f);
4304 int depth = DefaultDepthOfScreen (screen);
4305
4306 /* Create a pixmap as large as the glyph string. */
4307 pixmap = XCreatePixmap (s->display, s->window,
4308 s->background_width,
4309 s->height, depth);
4310
4311 /* Don't clip in the following because we're working on the
4312 pixmap. */
4313 XSetClipMask (s->display, s->gc, None);
4314
4315 /* Fill the pixmap with the background color/stipple. */
4316 if (s->stippled_p)
4317 {
4318 /* Fill background with a stipple pattern. */
4319 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4320 XFillRectangle (s->display, pixmap, s->gc,
4321 0, 0, s->background_width, s->height);
4322 XSetFillStyle (s->display, s->gc, FillSolid);
4323 }
4324 else
4325 {
4326 XGCValues xgcv;
4327 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4328 &xgcv);
4329 XSetForeground (s->display, s->gc, xgcv.background);
4330 XFillRectangle (s->display, pixmap, s->gc,
4331 0, 0, s->background_width, s->height);
4332 XSetForeground (s->display, s->gc, xgcv.foreground);
4333 }
4334 }
4335 else
4336 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4337
4338 s->background_filled_p = 1;
4339 }
4340
4341 /* Draw the foreground. */
4342 if (pixmap != None)
4343 {
4344 x_draw_image_foreground_1 (s, pixmap);
4345 x_set_glyph_string_clipping (s);
4346 XCopyArea (s->display, pixmap, s->window, s->gc,
4347 0, 0, s->background_width, s->height, s->x, s->y);
4348 XFreePixmap (s->display, pixmap);
4349 }
4350 else
4351 x_draw_image_foreground (s);
4352
4353 /* If we must draw a relief around the image, do it. */
4354 if (s->img->relief
4355 || s->hl == DRAW_IMAGE_RAISED
4356 || s->hl == DRAW_IMAGE_SUNKEN)
4357 x_draw_image_relief (s);
4358 }
4359
4360
4361 /* Draw stretch glyph string S. */
4362
4363 static void
4364 x_draw_stretch_glyph_string (s)
4365 struct glyph_string *s;
4366 {
4367 xassert (s->first_glyph->type == STRETCH_GLYPH);
4368 s->stippled_p = s->face->stipple != 0;
4369
4370 if (s->hl == DRAW_CURSOR
4371 && !x_stretch_cursor_p)
4372 {
4373 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4374 as wide as the stretch glyph. */
4375 int width = min (CANON_X_UNIT (s->f), s->background_width);
4376
4377 /* Draw cursor. */
4378 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4379
4380 /* Clear rest using the GC of the original non-cursor face. */
4381 if (width < s->background_width)
4382 {
4383 int x = s->x + width, y = s->y;
4384 int w = s->background_width - width, h = s->height;
4385 XRectangle r;
4386 GC gc;
4387
4388 if (s->row->mouse_face_p
4389 && cursor_in_mouse_face_p (s->w))
4390 {
4391 x_set_mouse_face_gc (s);
4392 gc = s->gc;
4393 }
4394 else
4395 gc = s->face->gc;
4396
4397 x_get_glyph_string_clip_rect (s, &r);
4398 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4399
4400 if (s->face->stipple)
4401 {
4402 /* Fill background with a stipple pattern. */
4403 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4404 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4405 XSetFillStyle (s->display, gc, FillSolid);
4406 }
4407 else
4408 {
4409 XGCValues xgcv;
4410 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4411 XSetForeground (s->display, gc, xgcv.background);
4412 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4413 XSetForeground (s->display, gc, xgcv.foreground);
4414 }
4415 }
4416 }
4417 else if (!s->background_filled_p)
4418 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4419 s->height);
4420
4421 s->background_filled_p = 1;
4422 }
4423
4424
4425 /* Draw glyph string S. */
4426
4427 static void
4428 x_draw_glyph_string (s)
4429 struct glyph_string *s;
4430 {
4431 int relief_drawn_p = 0;
4432
4433 /* If S draws into the background of its successor, draw the
4434 background of the successor first so that S can draw into it.
4435 This makes S->next use XDrawString instead of XDrawImageString. */
4436 if (s->next && s->right_overhang && !s->for_overlaps_p)
4437 {
4438 xassert (s->next->img == NULL);
4439 x_set_glyph_string_gc (s->next);
4440 x_set_glyph_string_clipping (s->next);
4441 x_draw_glyph_string_background (s->next, 1);
4442 }
4443
4444 /* Set up S->gc, set clipping and draw S. */
4445 x_set_glyph_string_gc (s);
4446
4447 /* Draw relief (if any) in advance for char/composition so that the
4448 glyph string can be drawn over it. */
4449 if (!s->for_overlaps_p
4450 && s->face->box != FACE_NO_BOX
4451 && (s->first_glyph->type == CHAR_GLYPH
4452 || s->first_glyph->type == COMPOSITE_GLYPH))
4453
4454 {
4455 x_set_glyph_string_clipping (s);
4456 x_draw_glyph_string_background (s, 1);
4457 x_draw_glyph_string_box (s);
4458 x_set_glyph_string_clipping (s);
4459 relief_drawn_p = 1;
4460 }
4461 else
4462 x_set_glyph_string_clipping (s);
4463
4464 switch (s->first_glyph->type)
4465 {
4466 case IMAGE_GLYPH:
4467 x_draw_image_glyph_string (s);
4468 break;
4469
4470 case STRETCH_GLYPH:
4471 x_draw_stretch_glyph_string (s);
4472 break;
4473
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;
4481
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;
4489
4490 default:
4491 abort ();
4492 }
4493
4494 if (!s->for_overlaps_p)
4495 {
4496 /* Draw underline. */
4497 if (s->face->underline_p)
4498 {
4499 unsigned long tem, h;
4500 int y;
4501
4502 /* Get the underline thickness. Default is 1 pixel. */
4503 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4504 h = 1;
4505
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
4510
4511 ROUND ((maximum descent) / 2), with
4512 ROUND(x) = floor (x + 0.5) */
4513
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;
4521
4522 if (s->face->underline_defaulted_p)
4523 XFillRectangle (s->display, s->window, s->gc,
4524 s->x, y, s->width, h);
4525 else
4526 {
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);
4533 }
4534 }
4535
4536 /* Draw overline. */
4537 if (s->face->overline_p)
4538 {
4539 unsigned long dy = 0, h = 1;
4540
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
4545 {
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);
4552 }
4553 }
4554
4555 /* Draw strike-through. */
4556 if (s->face->strike_through_p)
4557 {
4558 unsigned long h = 1;
4559 unsigned long dy = (s->height - h) / 2;
4560
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
4565 {
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);
4572 }
4573 }
4574
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);
4578 }
4579
4580 /* Reset clipping. */
4581 XSetClipMask (s->display, s->gc, None);
4582 }
4583
4584
4585 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4586 struct face **, int));
4587
4588
4589 /* Fill glyph string S with composition components specified by S->cmp.
4590
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.
4595
4596 Value is the index of a component not in S. */
4597
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;
4603 {
4604 int i;
4605
4606 xassert (s);
4607
4608 s->for_overlaps_p = overlaps_p;
4609
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);
4613
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;
4620
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. */
4623
4624 s->width = s->first_glyph->pixel_width;
4625
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)
4631 {
4632 s->font_not_found_p = 1;
4633 s->font = FRAME_FONT (s->f);
4634 }
4635
4636 /* Adjust base line for subscript/superscript text. */
4637 s->ybase += s->first_glyph->voffset;
4638
4639 xassert (s->face && s->face->gc);
4640
4641 /* This glyph string must always be drawn with 16-bit functions. */
4642 s->two_byte_p = 1;
4643
4644 return s->gidx + s->nchars;
4645 }
4646
4647
4648 /* Fill glyph string S from a sequence of character glyphs.
4649
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.
4654
4655 Value is the index of the first glyph not in S. */
4656
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;
4662 {
4663 struct glyph *glyph, *last;
4664 int voffset;
4665 int glyph_not_available_p;
4666
4667 xassert (s->f == XFRAME (s->w->frame));
4668 xassert (s->nchars == 0);
4669 xassert (start >= 0 && end > start);
4670
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;
4675
4676 glyph_not_available_p = glyph->glyph_not_available_p;
4677
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)
4684 {
4685 int two_byte_p;
4686
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;
4695 }
4696
4697 s->font = s->face->font;
4698 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4699
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)
4705 {
4706 s->font_not_found_p = 1;
4707 s->font = FRAME_FONT (s->f);
4708 }
4709
4710 /* Adjust base line for subscript/superscript text. */
4711 s->ybase += voffset;
4712
4713 xassert (s->face && s->face->gc);
4714 return glyph - s->row->glyphs[s->area];
4715 }
4716
4717
4718 /* Fill glyph string S from image glyph S->first_glyph. */
4719
4720 static void
4721 x_fill_image_glyph_string (s)
4722 struct glyph_string *s;
4723 {
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;
4730
4731 /* Adjust base line for subscript/superscript text. */
4732 s->ybase += s->first_glyph->voffset;
4733 }
4734
4735
4736 /* Fill glyph string S from a sequence of stretch glyphs.
4737
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.
4741
4742 Value is the index of the first glyph not in S. */
4743
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;
4750 {
4751 struct glyph *glyph, *last;
4752 int voffset, face_id;
4753
4754 xassert (s->first_glyph->type == STRETCH_GLYPH);
4755
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;
4764
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;
4772
4773 /* Adjust base line for subscript/superscript text. */
4774 s->ybase += voffset;
4775
4776 /* The case that face->gc == 0 is handled when drawing the glyph
4777 string by calling PREPARE_FACE_FOR_DISPLAY. */
4778 xassert (s->face);
4779 return glyph - s->row->glyphs[s->area];
4780 }
4781
4782
4783 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4784 of XChar2b structures for S; it can't be allocated in
4785 x_init_glyph_string because it must be allocated via `alloca'. W
4786 is the window on which S is drawn. ROW and AREA are the glyph row
4787 and area within the row from which S is constructed. START is the
4788 index of the first glyph structure covered by S. HL is a
4789 face-override for drawing S. */
4790
4791 static void
4792 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4793 struct glyph_string *s;
4794 XChar2b *char2b;
4795 struct window *w;
4796 struct glyph_row *row;
4797 enum glyph_row_area area;
4798 int start;
4799 enum draw_glyphs_face hl;
4800 {
4801 bzero (s, sizeof *s);
4802 s->w = w;
4803 s->f = XFRAME (w->frame);
4804 s->display = FRAME_X_DISPLAY (s->f);
4805 s->window = FRAME_X_WINDOW (s->f);
4806 s->char2b = char2b;
4807 s->hl = hl;
4808 s->row = row;
4809 s->area = area;
4810 s->first_glyph = row->glyphs[area] + start;
4811 s->height = row->height;
4812 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4813
4814 /* Display the internal border below the tool-bar window. */
4815 if (s->w == XWINDOW (s->f->tool_bar_window))
4816 s->y -= s->f->output_data.x->internal_border_width;
4817
4818 s->ybase = s->y + row->ascent;
4819 }
4820
4821
4822 /* Set background width of glyph string S. START is the index of the
4823 first glyph following S. LAST_X is the right-most x-position + 1
4824 in the drawing area. */
4825
4826 static INLINE void
4827 x_set_glyph_string_background_width (s, start, last_x)
4828 struct glyph_string *s;
4829 int start;
4830 int last_x;
4831 {
4832 /* If the face of this glyph string has to be drawn to the end of
4833 the drawing area, set S->extends_to_end_of_line_p. */
4834 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4835
4836 if (start == s->row->used[s->area]
4837 && s->area == TEXT_AREA
4838 && ((s->hl == DRAW_NORMAL_TEXT
4839 && (s->row->fill_line_p
4840 || s->face->background != default_face->background
4841 || s->face->stipple != default_face->stipple
4842 || s->row->mouse_face_p))
4843 || s->hl == DRAW_MOUSE_FACE
4844 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
4845 && s->row->fill_line_p)))
4846 s->extends_to_end_of_line_p = 1;
4847
4848 /* If S extends its face to the end of the line, set its
4849 background_width to the distance to the right edge of the drawing
4850 area. */
4851 if (s->extends_to_end_of_line_p)
4852 s->background_width = last_x - s->x + 1;
4853 else
4854 s->background_width = s->width;
4855 }
4856
4857
4858 /* Add a glyph string for a stretch glyph to the list of strings
4859 between HEAD and TAIL. START is the index of the stretch glyph in
4860 row area AREA of glyph row ROW. END is the index of the last glyph
4861 in that glyph row area. X is the current output position assigned
4862 to the new glyph string constructed. HL overrides that face of the
4863 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4864 is the right-most x-position of the drawing area. */
4865
4866 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4867 and below -- keep them on one line. */
4868 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4869 do \
4870 { \
4871 s = (struct glyph_string *) alloca (sizeof *s); \
4872 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4873 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4874 x_append_glyph_string (&HEAD, &TAIL, s); \
4875 s->x = (X); \
4876 } \
4877 while (0)
4878
4879
4880 /* Add a glyph string for an image glyph to the list of strings
4881 between HEAD and TAIL. START is the index of the image glyph in
4882 row area AREA of glyph row ROW. END is the index of the last glyph
4883 in that glyph row area. X is the current output position assigned
4884 to the new glyph string constructed. HL overrides that face of the
4885 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4886 is the right-most x-position of the drawing area. */
4887
4888 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4889 do \
4890 { \
4891 s = (struct glyph_string *) alloca (sizeof *s); \
4892 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4893 x_fill_image_glyph_string (s); \
4894 x_append_glyph_string (&HEAD, &TAIL, s); \
4895 ++START; \
4896 s->x = (X); \
4897 } \
4898 while (0)
4899
4900
4901 /* Add a glyph string for a sequence of character glyphs to the list
4902 of strings between HEAD and TAIL. START is the index of the first
4903 glyph in row area AREA of glyph row ROW that is part of the new
4904 glyph string. END is the index of the last glyph in that glyph row
4905 area. X is the current output position assigned to the new glyph
4906 string constructed. HL overrides that face of the glyph; e.g. it
4907 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4908 right-most x-position of the drawing area. */
4909
4910 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4911 do \
4912 { \
4913 int c, face_id; \
4914 XChar2b *char2b; \
4915 \
4916 c = (ROW)->glyphs[AREA][START].u.ch; \
4917 face_id = (ROW)->glyphs[AREA][START].face_id; \
4918 \
4919 s = (struct glyph_string *) alloca (sizeof *s); \
4920 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4921 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4922 x_append_glyph_string (&HEAD, &TAIL, s); \
4923 s->x = (X); \
4924 START = x_fill_glyph_string (s, face_id, START, END, \
4925 OVERLAPS_P); \
4926 } \
4927 while (0)
4928
4929
4930 /* Add a glyph string for a composite sequence to the list of strings
4931 between HEAD and TAIL. START is the index of the first glyph in
4932 row area AREA of glyph row ROW that is part of the new glyph
4933 string. END is the index of the last glyph in that glyph row area.
4934 X is the current output position assigned to the new glyph string
4935 constructed. HL overrides that face of the glyph; e.g. it is
4936 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4937 x-position of the drawing area. */
4938
4939 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4940 do { \
4941 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4942 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4943 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4944 struct composition *cmp = composition_table[cmp_id]; \
4945 int glyph_len = cmp->glyph_len; \
4946 XChar2b *char2b; \
4947 struct face **faces; \
4948 struct glyph_string *first_s = NULL; \
4949 int n; \
4950 \
4951 base_face = base_face->ascii_face; \
4952 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4953 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4954 /* At first, fill in `char2b' and `faces'. */ \
4955 for (n = 0; n < glyph_len; n++) \
4956 { \
4957 int c = COMPOSITION_GLYPH (cmp, n); \
4958 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4959 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4960 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4961 this_face_id, char2b + n, 1); \
4962 } \
4963 \
4964 /* Make glyph_strings for each glyph sequence that is drawable by \
4965 the same face, and append them to HEAD/TAIL. */ \
4966 for (n = 0; n < cmp->glyph_len;) \
4967 { \
4968 s = (struct glyph_string *) alloca (sizeof *s); \
4969 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4970 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4971 s->cmp = cmp; \
4972 s->gidx = n; \
4973 s->x = (X); \
4974 \
4975 if (n == 0) \
4976 first_s = s; \
4977 \
4978 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4979 } \
4980 \
4981 ++START; \
4982 s = first_s; \
4983 } while (0)
4984
4985
4986 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4987 of AREA of glyph row ROW on window W between indices START and END.
4988 HL overrides the face for drawing glyph strings, e.g. it is
4989 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4990 x-positions of the drawing area.
4991
4992 This is an ugly monster macro construct because we must use alloca
4993 to allocate glyph strings (because x_draw_glyphs can be called
4994 asynchronously). */
4995
4996 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4997 do \
4998 { \
4999 HEAD = TAIL = NULL; \
5000 while (START < END) \
5001 { \
5002 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
5003 switch (first_glyph->type) \
5004 { \
5005 case CHAR_GLYPH: \
5006 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
5007 TAIL, HL, X, LAST_X, \
5008 OVERLAPS_P); \
5009 break; \
5010 \
5011 case COMPOSITE_GLYPH: \
5012 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
5013 HEAD, TAIL, HL, X, LAST_X,\
5014 OVERLAPS_P); \
5015 break; \
5016 \
5017 case STRETCH_GLYPH: \
5018 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
5019 HEAD, TAIL, HL, X, LAST_X); \
5020 break; \
5021 \
5022 case IMAGE_GLYPH: \
5023 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
5024 TAIL, HL, X, LAST_X); \
5025 break; \
5026 \
5027 default: \
5028 abort (); \
5029 } \
5030 \
5031 x_set_glyph_string_background_width (s, START, LAST_X); \
5032 (X) += s->width; \
5033 } \
5034 } \
5035 while (0)
5036
5037
5038 /* Draw glyphs between START and END in AREA of ROW on window W,
5039 starting at x-position X. X is relative to AREA in W. HL is a
5040 face-override with the following meaning:
5041
5042 DRAW_NORMAL_TEXT draw normally
5043 DRAW_CURSOR draw in cursor face
5044 DRAW_MOUSE_FACE draw in mouse face.
5045 DRAW_INVERSE_VIDEO draw in mode line face
5046 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
5047 DRAW_IMAGE_RAISED draw an image with a raised relief around it
5048
5049 If OVERLAPS_P is non-zero, draw only the foreground of characters
5050 and clip to the physical height of ROW.
5051
5052 Value is the x-position reached, relative to AREA of W. */
5053
5054 static int
5055 x_draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
5056 struct window *w;
5057 int x;
5058 struct glyph_row *row;
5059 enum glyph_row_area area;
5060 int start, end;
5061 enum draw_glyphs_face hl;
5062 int overlaps_p;
5063 {
5064 struct glyph_string *head, *tail;
5065 struct glyph_string *s;
5066 int last_x, area_width;
5067 int x_reached;
5068 int i, j;
5069
5070 /* Let's rather be paranoid than getting a SEGV. */
5071 end = min (end, row->used[area]);
5072 start = max (0, start);
5073 start = min (end, start);
5074
5075 /* Translate X to frame coordinates. Set last_x to the right
5076 end of the drawing area. */
5077 if (row->full_width_p)
5078 {
5079 /* X is relative to the left edge of W, without scroll bars
5080 or fringes. */
5081 struct frame *f = XFRAME (w->frame);
5082 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
5083
5084 x += window_left_x;
5085 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
5086 last_x = window_left_x + area_width;
5087
5088 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5089 {
5090 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5091 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
5092 last_x += width;
5093 else
5094 x -= width;
5095 }
5096
5097 x += FRAME_INTERNAL_BORDER_WIDTH (f);
5098 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
5099 }
5100 else
5101 {
5102 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
5103 area_width = window_box_width (w, area);
5104 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
5105 }
5106
5107 /* Build a doubly-linked list of glyph_string structures between
5108 head and tail from what we have to draw. Note that the macro
5109 BUILD_GLYPH_STRINGS will modify its start parameter. That's
5110 the reason we use a separate variable `i'. */
5111 i = start;
5112 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
5113 overlaps_p);
5114 if (tail)
5115 x_reached = tail->x + tail->background_width;
5116 else
5117 x_reached = x;
5118
5119 /* If there are any glyphs with lbearing < 0 or rbearing > width in
5120 the row, redraw some glyphs in front or following the glyph
5121 strings built above. */
5122 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
5123 {
5124 int dummy_x = 0;
5125 struct glyph_string *h, *t;
5126
5127 /* Compute overhangs for all glyph strings. */
5128 for (s = head; s; s = s->next)
5129 x_compute_glyph_string_overhangs (s);
5130
5131 /* Prepend glyph strings for glyphs in front of the first glyph
5132 string that are overwritten because of the first glyph
5133 string's left overhang. The background of all strings
5134 prepended must be drawn because the first glyph string
5135 draws over it. */
5136 i = x_left_overwritten (head);
5137 if (i >= 0)
5138 {
5139 j = i;
5140 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
5141 DRAW_NORMAL_TEXT, dummy_x, last_x,
5142 overlaps_p);
5143 start = i;
5144 x_compute_overhangs_and_x (t, head->x, 1);
5145 x_prepend_glyph_string_lists (&head, &tail, h, t);
5146 }
5147
5148 /* Prepend glyph strings for glyphs in front of the first glyph
5149 string that overwrite that glyph string because of their
5150 right overhang. For these strings, only the foreground must
5151 be drawn, because it draws over the glyph string at `head'.
5152 The background must not be drawn because this would overwrite
5153 right overhangs of preceding glyphs for which no glyph
5154 strings exist. */
5155 i = x_left_overwriting (head);
5156 if (i >= 0)
5157 {
5158 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
5159 DRAW_NORMAL_TEXT, dummy_x, last_x,
5160 overlaps_p);
5161 for (s = h; s; s = s->next)
5162 s->background_filled_p = 1;
5163 x_compute_overhangs_and_x (t, head->x, 1);
5164 x_prepend_glyph_string_lists (&head, &tail, h, t);
5165 }
5166
5167 /* Append glyphs strings for glyphs following the last glyph
5168 string tail that are overwritten by tail. The background of
5169 these strings has to be drawn because tail's foreground draws
5170 over it. */
5171 i = x_right_overwritten (tail);
5172 if (i >= 0)
5173 {
5174 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5175 DRAW_NORMAL_TEXT, x, last_x,
5176 overlaps_p);
5177 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5178 x_append_glyph_string_lists (&head, &tail, h, t);
5179 }
5180
5181 /* Append glyph strings for glyphs following the last glyph
5182 string tail that overwrite tail. The foreground of such
5183 glyphs has to be drawn because it writes into the background
5184 of tail. The background must not be drawn because it could
5185 paint over the foreground of following glyphs. */
5186 i = x_right_overwriting (tail);
5187 if (i >= 0)
5188 {
5189 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5190 DRAW_NORMAL_TEXT, x, last_x,
5191 overlaps_p);
5192 for (s = h; s; s = s->next)
5193 s->background_filled_p = 1;
5194 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5195 x_append_glyph_string_lists (&head, &tail, h, t);
5196 }
5197 }
5198
5199 /* Draw all strings. */
5200 for (s = head; s; s = s->next)
5201 x_draw_glyph_string (s);
5202
5203 if (area == TEXT_AREA
5204 && !row->full_width_p
5205 /* When drawing overlapping rows, only the glyph strings'
5206 foreground is drawn, which doesn't erase a cursor
5207 completely. */
5208 && !overlaps_p)
5209 {
5210 int x0 = head ? head->x : x;
5211 int x1 = tail ? tail->x + tail->background_width : x;
5212
5213 x0 = FRAME_TO_WINDOW_PIXEL_X (w, x0);
5214 x1 = FRAME_TO_WINDOW_PIXEL_X (w, x1);
5215
5216 if (XFASTINT (w->left_margin_width) != 0)
5217 {
5218 int left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
5219 x0 -= left_area_width;
5220 x1 -= left_area_width;
5221 }
5222
5223 notice_overwritten_cursor (w, area, x0, x1,
5224 row->y, MATRIX_ROW_BOTTOM_Y (row));
5225 }
5226
5227 /* Value is the x-position up to which drawn, relative to AREA of W.
5228 This doesn't include parts drawn because of overhangs. */
5229 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5230 if (!row->full_width_p)
5231 {
5232 if (area > LEFT_MARGIN_AREA && XFASTINT (w->left_margin_width) != 0)
5233 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5234 if (area > TEXT_AREA)
5235 x_reached -= window_box_width (w, TEXT_AREA);
5236 }
5237
5238 return x_reached;
5239 }
5240
5241
5242 /* Fix the display of area AREA of overlapping row ROW in window W. */
5243
5244 static void
5245 x_fix_overlapping_area (w, row, area)
5246 struct window *w;
5247 struct glyph_row *row;
5248 enum glyph_row_area area;
5249 {
5250 int i, x;
5251
5252 BLOCK_INPUT;
5253
5254 if (area == LEFT_MARGIN_AREA)
5255 x = 0;
5256 else if (area == TEXT_AREA)
5257 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5258 else
5259 x = (window_box_width (w, LEFT_MARGIN_AREA)
5260 + window_box_width (w, TEXT_AREA));
5261
5262 for (i = 0; i < row->used[area];)
5263 {
5264 if (row->glyphs[area][i].overlaps_vertically_p)
5265 {
5266 int start = i, start_x = x;
5267
5268 do
5269 {
5270 x += row->glyphs[area][i].pixel_width;
5271 ++i;
5272 }
5273 while (i < row->used[area]
5274 && row->glyphs[area][i].overlaps_vertically_p);
5275
5276 x_draw_glyphs (w, start_x, row, area, start, i,
5277 DRAW_NORMAL_TEXT, 1);
5278 }
5279 else
5280 {
5281 x += row->glyphs[area][i].pixel_width;
5282 ++i;
5283 }
5284 }
5285
5286 UNBLOCK_INPUT;
5287 }
5288
5289
5290 /* Output LEN glyphs starting at START at the nominal cursor position.
5291 Advance the nominal cursor over the text. The global variable
5292 updated_window contains the window being updated, updated_row is
5293 the glyph row being updated, and updated_area is the area of that
5294 row being updated. */
5295
5296 static void
5297 x_write_glyphs (start, len)
5298 struct glyph *start;
5299 int len;
5300 {
5301 int x, hpos;
5302
5303 xassert (updated_window && updated_row);
5304 BLOCK_INPUT;
5305
5306 /* Write glyphs. */
5307
5308 hpos = start - updated_row->glyphs[updated_area];
5309 x = x_draw_glyphs (updated_window, output_cursor.x,
5310 updated_row, updated_area,
5311 hpos, hpos + len,
5312 DRAW_NORMAL_TEXT, 0);
5313
5314 UNBLOCK_INPUT;
5315
5316 /* Advance the output cursor. */
5317 output_cursor.hpos += len;
5318 output_cursor.x = x;
5319 }
5320
5321
5322 /* Insert LEN glyphs from START at the nominal cursor position. */
5323
5324 static void
5325 x_insert_glyphs (start, len)
5326 struct glyph *start;
5327 register int len;
5328 {
5329 struct frame *f;
5330 struct window *w;
5331 int line_height, shift_by_width, shifted_region_width;
5332 struct glyph_row *row;
5333 struct glyph *glyph;
5334 int frame_x, frame_y, hpos;
5335
5336 xassert (updated_window && updated_row);
5337 BLOCK_INPUT;
5338 w = updated_window;
5339 f = XFRAME (WINDOW_FRAME (w));
5340
5341 /* Get the height of the line we are in. */
5342 row = updated_row;
5343 line_height = row->height;
5344
5345 /* Get the width of the glyphs to insert. */
5346 shift_by_width = 0;
5347 for (glyph = start; glyph < start + len; ++glyph)
5348 shift_by_width += glyph->pixel_width;
5349
5350 /* Get the width of the region to shift right. */
5351 shifted_region_width = (window_box_width (w, updated_area)
5352 - output_cursor.x
5353 - shift_by_width);
5354
5355 /* Shift right. */
5356 frame_x = window_box_left (w, updated_area) + output_cursor.x;
5357 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5358 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5359 f->output_data.x->normal_gc,
5360 frame_x, frame_y,
5361 shifted_region_width, line_height,
5362 frame_x + shift_by_width, frame_y);
5363
5364 /* Write the glyphs. */
5365 hpos = start - row->glyphs[updated_area];
5366 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5367 DRAW_NORMAL_TEXT, 0);
5368
5369 /* Advance the output cursor. */
5370 output_cursor.hpos += len;
5371 output_cursor.x += shift_by_width;
5372 UNBLOCK_INPUT;
5373 }
5374
5375
5376 /* Delete N glyphs at the nominal cursor position. Not implemented
5377 for X frames. */
5378
5379 static void
5380 x_delete_glyphs (n)
5381 register int n;
5382 {
5383 abort ();
5384 }
5385
5386
5387 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5388 If they are <= 0, this is probably an error. */
5389
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;
5397 {
5398 xassert (width > 0 && height > 0);
5399 XClearArea (dpy, window, x, y, width, height, exposures);
5400 }
5401
5402
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.
5406
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. */
5409
5410 static void
5411 x_clear_end_of_line (to_x)
5412 int to_x;
5413 {
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;
5418
5419 xassert (updated_window && updated_row);
5420 f = XFRAME (w->frame);
5421
5422 if (updated_row->full_width_p)
5423 {
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);
5428 }
5429 else
5430 max_x = window_box_width (w, updated_area);
5431 max_y = window_text_bottom_y (w);
5432
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);
5441
5442 to_y = min (max_y, output_cursor.y + updated_row->height);
5443
5444 /* Notice if the cursor will be cleared by this operation. */
5445 if (!updated_row->full_width_p)
5446 notice_overwritten_cursor (w, updated_area,
5447 output_cursor.x, -1,
5448 updated_row->y,
5449 MATRIX_ROW_BOTTOM_Y (updated_row));
5450
5451 from_x = output_cursor.x;
5452
5453 /* Translate to frame coordinates. */
5454 if (updated_row->full_width_p)
5455 {
5456 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5457 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5458 }
5459 else
5460 {
5461 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5462 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5463 }
5464
5465 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5466 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5467 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5468
5469 /* Prevent inadvertently clearing to end of the X window. */
5470 if (to_x > from_x && to_y > from_y)
5471 {
5472 BLOCK_INPUT;
5473 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5474 from_x, from_y, to_x - from_x, to_y - from_y,
5475 False);
5476 UNBLOCK_INPUT;
5477 }
5478 }
5479
5480
5481 /* Clear entire frame. If updating_frame is non-null, clear that
5482 frame. Otherwise clear the selected frame. */
5483
5484 static void
5485 x_clear_frame ()
5486 {
5487 struct frame *f;
5488
5489 if (updating_frame)
5490 f = updating_frame;
5491 else
5492 f = SELECTED_FRAME ();
5493
5494 /* Clearing the frame will erase any cursor, so mark them all as no
5495 longer visible. */
5496 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5497 output_cursor.hpos = output_cursor.vpos = 0;
5498 output_cursor.x = -1;
5499
5500 /* We don't set the output cursor here because there will always
5501 follow an explicit cursor_to. */
5502 BLOCK_INPUT;
5503 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5504
5505 /* We have to clear the scroll bars, too. If we have changed
5506 colors or something like that, then they should be notified. */
5507 x_scroll_bar_clear (f);
5508
5509 XFlush (FRAME_X_DISPLAY (f));
5510 UNBLOCK_INPUT;
5511 }
5512
5513
5514 \f
5515 /* Invert the middle quarter of the frame for .15 sec. */
5516
5517 /* We use the select system call to do the waiting, so we have to make
5518 sure it's available. If it isn't, we just won't do visual bells. */
5519
5520 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5521
5522
5523 /* Subtract the `struct timeval' values X and Y, storing the result in
5524 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5525
5526 static int
5527 timeval_subtract (result, x, y)
5528 struct timeval *result, x, y;
5529 {
5530 /* Perform the carry for the later subtraction by updating y. This
5531 is safer because on some systems the tv_sec member is unsigned. */
5532 if (x.tv_usec < y.tv_usec)
5533 {
5534 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5535 y.tv_usec -= 1000000 * nsec;
5536 y.tv_sec += nsec;
5537 }
5538
5539 if (x.tv_usec - y.tv_usec > 1000000)
5540 {
5541 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5542 y.tv_usec += 1000000 * nsec;
5543 y.tv_sec -= nsec;
5544 }
5545
5546 /* Compute the time remaining to wait. tv_usec is certainly
5547 positive. */
5548 result->tv_sec = x.tv_sec - y.tv_sec;
5549 result->tv_usec = x.tv_usec - y.tv_usec;
5550
5551 /* Return indication of whether the result should be considered
5552 negative. */
5553 return x.tv_sec < y.tv_sec;
5554 }
5555
5556 void
5557 XTflash (f)
5558 struct frame *f;
5559 {
5560 BLOCK_INPUT;
5561
5562 {
5563 GC gc;
5564
5565 /* Create a GC that will use the GXxor function to flip foreground
5566 pixels into background pixels. */
5567 {
5568 XGCValues values;
5569
5570 values.function = GXxor;
5571 values.foreground = (f->output_data.x->foreground_pixel
5572 ^ f->output_data.x->background_pixel);
5573
5574 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5575 GCFunction | GCForeground, &values);
5576 }
5577
5578 {
5579 /* Get the height not including a menu bar widget. */
5580 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5581 /* Height of each line to flash. */
5582 int flash_height = FRAME_LINE_HEIGHT (f);
5583 /* These will be the left and right margins of the rectangles. */
5584 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5585 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5586
5587 int width;
5588
5589 /* Don't flash the area between a scroll bar and the frame
5590 edge it is next to. */
5591 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5592 {
5593 case vertical_scroll_bar_left:
5594 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5595 break;
5596
5597 case vertical_scroll_bar_right:
5598 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5599 break;
5600
5601 default:
5602 break;
5603 }
5604
5605 width = flash_right - flash_left;
5606
5607 /* If window is tall, flash top and bottom line. */
5608 if (height > 3 * FRAME_LINE_HEIGHT (f))
5609 {
5610 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5611 flash_left,
5612 (FRAME_INTERNAL_BORDER_WIDTH (f)
5613 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5614 width, flash_height);
5615 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5616 flash_left,
5617 (height - flash_height
5618 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5619 width, flash_height);
5620 }
5621 else
5622 /* If it is short, flash it all. */
5623 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5624 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5625 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5626
5627 x_flush (f);
5628
5629 {
5630 struct timeval wakeup;
5631
5632 EMACS_GET_TIME (wakeup);
5633
5634 /* Compute time to wait until, propagating carry from usecs. */
5635 wakeup.tv_usec += 150000;
5636 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5637 wakeup.tv_usec %= 1000000;
5638
5639 /* Keep waiting until past the time wakeup or any input gets
5640 available. */
5641 while (! detect_input_pending ())
5642 {
5643 struct timeval current;
5644 struct timeval timeout;
5645
5646 EMACS_GET_TIME (current);
5647
5648 /* Break if result would be negative. */
5649 if (timeval_subtract (&current, wakeup, current))
5650 break;
5651
5652 /* How long `select' should wait. */
5653 timeout.tv_sec = 0;
5654 timeout.tv_usec = 10000;
5655
5656 /* Try to wait that long--but we might wake up sooner. */
5657 select (0, NULL, NULL, NULL, &timeout);
5658 }
5659 }
5660
5661 /* If window is tall, flash top and bottom line. */
5662 if (height > 3 * FRAME_LINE_HEIGHT (f))
5663 {
5664 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5665 flash_left,
5666 (FRAME_INTERNAL_BORDER_WIDTH (f)
5667 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5668 width, flash_height);
5669 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5670 flash_left,
5671 (height - flash_height
5672 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5673 width, flash_height);
5674 }
5675 else
5676 /* If it is short, flash it all. */
5677 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5678 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5679 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5680
5681 XFreeGC (FRAME_X_DISPLAY (f), gc);
5682 x_flush (f);
5683 }
5684 }
5685
5686 UNBLOCK_INPUT;
5687 }
5688
5689 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5690
5691
5692 /* Make audible bell. */
5693
5694 void
5695 XTring_bell ()
5696 {
5697 struct frame *f = SELECTED_FRAME ();
5698
5699 if (FRAME_X_DISPLAY (f))
5700 {
5701 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5702 if (visible_bell)
5703 XTflash (f);
5704 else
5705 #endif
5706 {
5707 BLOCK_INPUT;
5708 XBell (FRAME_X_DISPLAY (f), 0);
5709 XFlush (FRAME_X_DISPLAY (f));
5710 UNBLOCK_INPUT;
5711 }
5712 }
5713 }
5714
5715 \f
5716 /* Specify how many text lines, from the top of the window,
5717 should be affected by insert-lines and delete-lines operations.
5718 This, and those operations, are used only within an update
5719 that is bounded by calls to x_update_begin and x_update_end. */
5720
5721 static void
5722 XTset_terminal_window (n)
5723 register int n;
5724 {
5725 /* This function intentionally left blank. */
5726 }
5727
5728
5729 \f
5730 /***********************************************************************
5731 Line Dance
5732 ***********************************************************************/
5733
5734 /* Perform an insert-lines or delete-lines operation, inserting N
5735 lines or deleting -N lines at vertical position VPOS. */
5736
5737 static void
5738 x_ins_del_lines (vpos, n)
5739 int vpos, n;
5740 {
5741 abort ();
5742 }
5743
5744
5745 /* Scroll part of the display as described by RUN. */
5746
5747 static void
5748 x_scroll_run (w, run)
5749 struct window *w;
5750 struct run *run;
5751 {
5752 struct frame *f = XFRAME (w->frame);
5753 int x, y, width, height, from_y, to_y, bottom_y;
5754
5755 /* Get frame-relative bounding box of the text display area of W,
5756 without mode lines. Include in this box the left and right
5757 fringe of W. */
5758 window_box (w, -1, &x, &y, &width, &height);
5759 width += FRAME_X_FRINGE_WIDTH (f);
5760 x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
5761
5762 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5763 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5764 bottom_y = y + height;
5765
5766 if (to_y < from_y)
5767 {
5768 /* Scrolling up. Make sure we don't copy part of the mode
5769 line at the bottom. */
5770 if (from_y + run->height > bottom_y)
5771 height = bottom_y - from_y;
5772 else
5773 height = run->height;
5774 }
5775 else
5776 {
5777 /* Scolling down. Make sure we don't copy over the mode line.
5778 at the bottom. */
5779 if (to_y + run->height > bottom_y)
5780 height = bottom_y - to_y;
5781 else
5782 height = run->height;
5783 }
5784
5785 BLOCK_INPUT;
5786
5787 /* Cursor off. Will be switched on again in x_update_window_end. */
5788 updated_window = w;
5789 x_clear_cursor (w);
5790
5791 XCopyArea (FRAME_X_DISPLAY (f),
5792 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5793 f->output_data.x->normal_gc,
5794 x, from_y,
5795 width, height,
5796 x, to_y);
5797
5798 UNBLOCK_INPUT;
5799 }
5800
5801
5802 \f
5803 /***********************************************************************
5804 Exposure Events
5805 ***********************************************************************/
5806
5807 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5808 corner of the exposed rectangle. W and H are width and height of
5809 the exposed area. All are pixel values. W or H zero means redraw
5810 the entire frame. */
5811
5812 static void
5813 expose_frame (f, x, y, w, h)
5814 struct frame *f;
5815 int x, y, w, h;
5816 {
5817 XRectangle r;
5818 int mouse_face_overwritten_p = 0;
5819
5820 TRACE ((stderr, "expose_frame "));
5821
5822 /* No need to redraw if frame will be redrawn soon. */
5823 if (FRAME_GARBAGED_P (f))
5824 {
5825 TRACE ((stderr, " garbaged\n"));
5826 return;
5827 }
5828
5829 /* If basic faces haven't been realized yet, there is no point in
5830 trying to redraw anything. This can happen when we get an expose
5831 event while Emacs is starting, e.g. by moving another window. */
5832 if (FRAME_FACE_CACHE (f) == NULL
5833 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5834 {
5835 TRACE ((stderr, " no faces\n"));
5836 return;
5837 }
5838
5839 if (w == 0 || h == 0)
5840 {
5841 r.x = r.y = 0;
5842 r.width = CANON_X_UNIT (f) * f->width;
5843 r.height = CANON_Y_UNIT (f) * f->height;
5844 }
5845 else
5846 {
5847 r.x = x;
5848 r.y = y;
5849 r.width = w;
5850 r.height = h;
5851 }
5852
5853 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5854 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
5855
5856 if (WINDOWP (f->tool_bar_window))
5857 mouse_face_overwritten_p
5858 |= expose_window (XWINDOW (f->tool_bar_window), &r);
5859
5860 #ifndef USE_X_TOOLKIT
5861 if (WINDOWP (f->menu_bar_window))
5862 mouse_face_overwritten_p
5863 |= expose_window (XWINDOW (f->menu_bar_window), &r);
5864 #endif /* not USE_X_TOOLKIT */
5865
5866 /* Some window managers support a focus-follows-mouse style with
5867 delayed raising of frames. Imagine a partially obscured frame,
5868 and moving the mouse into partially obscured mouse-face on that
5869 frame. The visible part of the mouse-face will be highlighted,
5870 then the WM raises the obscured frame. With at least one WM, KDE
5871 2.1, Emacs is not getting any event for the raising of the frame
5872 (even tried with SubstructureRedirectMask), only Expose events.
5873 These expose events will draw text normally, i.e. not
5874 highlighted. Which means we must redo the highlight here.
5875 Subsume it under ``we love X''. --gerd 2001-08-15 */
5876 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
5877 {
5878 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5879 if (f == dpyinfo->mouse_face_mouse_frame)
5880 {
5881 int x = dpyinfo->mouse_face_mouse_x;
5882 int y = dpyinfo->mouse_face_mouse_y;
5883 clear_mouse_face (dpyinfo);
5884 note_mouse_highlight (f, x, y);
5885 }
5886 }
5887 }
5888
5889
5890 /* Redraw (parts) of all windows in the window tree rooted at W that
5891 intersect R. R contains frame pixel coordinates. Value is
5892 non-zero if the exposure overwrites mouse-face. */
5893
5894 static int
5895 expose_window_tree (w, r)
5896 struct window *w;
5897 XRectangle *r;
5898 {
5899 struct frame *f = XFRAME (w->frame);
5900 int mouse_face_overwritten_p = 0;
5901
5902 while (w && !FRAME_GARBAGED_P (f))
5903 {
5904 if (!NILP (w->hchild))
5905 mouse_face_overwritten_p
5906 |= expose_window_tree (XWINDOW (w->hchild), r);
5907 else if (!NILP (w->vchild))
5908 mouse_face_overwritten_p
5909 |= expose_window_tree (XWINDOW (w->vchild), r);
5910 else
5911 mouse_face_overwritten_p |= expose_window (w, r);
5912
5913 w = NILP (w->next) ? NULL : XWINDOW (w->next);
5914 }
5915
5916 return mouse_face_overwritten_p;
5917 }
5918
5919
5920 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5921 which intersects rectangle R. R is in window-relative coordinates. */
5922
5923 static void
5924 expose_area (w, row, r, area)
5925 struct window *w;
5926 struct glyph_row *row;
5927 XRectangle *r;
5928 enum glyph_row_area area;
5929 {
5930 struct glyph *first = row->glyphs[area];
5931 struct glyph *end = row->glyphs[area] + row->used[area];
5932 struct glyph *last;
5933 int first_x, start_x, x;
5934
5935 if (area == TEXT_AREA && row->fill_line_p)
5936 /* If row extends face to end of line write the whole line. */
5937 x_draw_glyphs (w, 0, row, area, 0, row->used[area],
5938 DRAW_NORMAL_TEXT, 0);
5939 else
5940 {
5941 /* Set START_X to the window-relative start position for drawing glyphs of
5942 AREA. The first glyph of the text area can be partially visible.
5943 The first glyphs of other areas cannot. */
5944 if (area == LEFT_MARGIN_AREA)
5945 start_x = 0;
5946 else if (area == TEXT_AREA)
5947 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5948 else
5949 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5950 + window_box_width (w, TEXT_AREA));
5951 x = start_x;
5952
5953 /* Find the first glyph that must be redrawn. */
5954 while (first < end
5955 && x + first->pixel_width < r->x)
5956 {
5957 x += first->pixel_width;
5958 ++first;
5959 }
5960
5961 /* Find the last one. */
5962 last = first;
5963 first_x = x;
5964 while (last < end
5965 && x < r->x + r->width)
5966 {
5967 x += last->pixel_width;
5968 ++last;
5969 }
5970
5971 /* Repaint. */
5972 if (last > first)
5973 x_draw_glyphs (w, first_x - start_x, row, area,
5974 first - row->glyphs[area],
5975 last - row->glyphs[area],
5976 DRAW_NORMAL_TEXT, 0);
5977 }
5978 }
5979
5980
5981 /* Redraw the parts of the glyph row ROW on window W intersecting
5982 rectangle R. R is in window-relative coordinates. Value is
5983 non-zero if mouse-face was overwritten. */
5984
5985 static int
5986 expose_line (w, row, r)
5987 struct window *w;
5988 struct glyph_row *row;
5989 XRectangle *r;
5990 {
5991 xassert (row->enabled_p);
5992
5993 if (row->mode_line_p || w->pseudo_window_p)
5994 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5995 DRAW_NORMAL_TEXT, 0);
5996 else
5997 {
5998 if (row->used[LEFT_MARGIN_AREA])
5999 expose_area (w, row, r, LEFT_MARGIN_AREA);
6000 if (row->used[TEXT_AREA])
6001 expose_area (w, row, r, TEXT_AREA);
6002 if (row->used[RIGHT_MARGIN_AREA])
6003 expose_area (w, row, r, RIGHT_MARGIN_AREA);
6004 x_draw_row_fringe_bitmaps (w, row);
6005 }
6006
6007 return row->mouse_face_p;
6008 }
6009
6010
6011 /* Return non-zero if W's cursor intersects rectangle R. */
6012
6013 static int
6014 x_phys_cursor_in_rect_p (w, r)
6015 struct window *w;
6016 XRectangle *r;
6017 {
6018 XRectangle cr, result;
6019 struct glyph *cursor_glyph;
6020
6021 cursor_glyph = get_phys_cursor_glyph (w);
6022 if (cursor_glyph)
6023 {
6024 cr.x = w->phys_cursor.x;
6025 cr.y = w->phys_cursor.y;
6026 cr.width = cursor_glyph->pixel_width;
6027 cr.height = w->phys_cursor_height;
6028 return x_intersect_rectangles (&cr, r, &result);
6029 }
6030 else
6031 return 0;
6032 }
6033
6034
6035 /* Redraw the part of window W intersection rectangle FR. Pixel
6036 coordinates in FR are frame-relative. Call this function with
6037 input blocked. Value is non-zero if the exposure overwrites
6038 mouse-face. */
6039
6040 static int
6041 expose_window (w, fr)
6042 struct window *w;
6043 XRectangle *fr;
6044 {
6045 struct frame *f = XFRAME (w->frame);
6046 XRectangle wr, r;
6047 int mouse_face_overwritten_p = 0;
6048
6049 /* If window is not yet fully initialized, do nothing. This can
6050 happen when toolkit scroll bars are used and a window is split.
6051 Reconfiguring the scroll bar will generate an expose for a newly
6052 created window. */
6053 if (w->current_matrix == NULL)
6054 return 0;
6055
6056 /* When we're currently updating the window, display and current
6057 matrix usually don't agree. Arrange for a thorough display
6058 later. */
6059 if (w == updated_window)
6060 {
6061 SET_FRAME_GARBAGED (f);
6062 return 0;
6063 }
6064
6065 /* Frame-relative pixel rectangle of W. */
6066 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
6067 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
6068 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
6069 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
6070
6071 if (x_intersect_rectangles (fr, &wr, &r))
6072 {
6073 int yb = window_text_bottom_y (w);
6074 struct glyph_row *row;
6075 int cursor_cleared_p;
6076
6077 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
6078 r.x, r.y, r.width, r.height));
6079
6080 /* Convert to window coordinates. */
6081 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
6082 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
6083
6084 /* Turn off the cursor. */
6085 if (!w->pseudo_window_p
6086 && x_phys_cursor_in_rect_p (w, &r))
6087 {
6088 x_clear_cursor (w);
6089 cursor_cleared_p = 1;
6090 }
6091 else
6092 cursor_cleared_p = 0;
6093
6094 /* Find the first row intersecting the rectangle R. */
6095 for (row = w->current_matrix->rows;
6096 row->enabled_p;
6097 ++row)
6098 {
6099 int y0 = row->y;
6100 int y1 = MATRIX_ROW_BOTTOM_Y (row);
6101
6102 if ((y0 >= r.y && y0 < r.y + r.height)
6103 || (y1 > r.y && y1 < r.y + r.height)
6104 || (r.y >= y0 && r.y < y1)
6105 || (r.y + r.height > y0 && r.y + r.height < y1))
6106 {
6107 if (expose_line (w, row, &r))
6108 mouse_face_overwritten_p = 1;
6109 }
6110
6111 if (y1 >= yb)
6112 break;
6113 }
6114
6115 /* Display the mode line if there is one. */
6116 if (WINDOW_WANTS_MODELINE_P (w)
6117 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
6118 row->enabled_p)
6119 && row->y < r.y + r.height)
6120 {
6121 if (expose_line (w, row, &r))
6122 mouse_face_overwritten_p = 1;
6123 }
6124
6125 if (!w->pseudo_window_p)
6126 {
6127 /* Draw border between windows. */
6128 x_draw_vertical_border (w);
6129
6130 /* Turn the cursor on again. */
6131 if (cursor_cleared_p)
6132 x_update_window_cursor (w, 1);
6133 }
6134 }
6135
6136 return mouse_face_overwritten_p;
6137 }
6138
6139
6140 /* Determine the intersection of two rectangles R1 and R2. Return
6141 the intersection in *RESULT. Value is non-zero if RESULT is not
6142 empty. */
6143
6144 static int
6145 x_intersect_rectangles (r1, r2, result)
6146 XRectangle *r1, *r2, *result;
6147 {
6148 XRectangle *left, *right;
6149 XRectangle *upper, *lower;
6150 int intersection_p = 0;
6151
6152 /* Rearrange so that R1 is the left-most rectangle. */
6153 if (r1->x < r2->x)
6154 left = r1, right = r2;
6155 else
6156 left = r2, right = r1;
6157
6158 /* X0 of the intersection is right.x0, if this is inside R1,
6159 otherwise there is no intersection. */
6160 if (right->x <= left->x + left->width)
6161 {
6162 result->x = right->x;
6163
6164 /* The right end of the intersection is the minimum of the
6165 the right ends of left and right. */
6166 result->width = (min (left->x + left->width, right->x + right->width)
6167 - result->x);
6168
6169 /* Same game for Y. */
6170 if (r1->y < r2->y)
6171 upper = r1, lower = r2;
6172 else
6173 upper = r2, lower = r1;
6174
6175 /* The upper end of the intersection is lower.y0, if this is inside
6176 of upper. Otherwise, there is no intersection. */
6177 if (lower->y <= upper->y + upper->height)
6178 {
6179 result->y = lower->y;
6180
6181 /* The lower end of the intersection is the minimum of the lower
6182 ends of upper and lower. */
6183 result->height = (min (lower->y + lower->height,
6184 upper->y + upper->height)
6185 - result->y);
6186 intersection_p = 1;
6187 }
6188 }
6189
6190 return intersection_p;
6191 }
6192
6193
6194
6195
6196 \f
6197 static void
6198 frame_highlight (f)
6199 struct frame *f;
6200 {
6201 /* We used to only do this if Vx_no_window_manager was non-nil, but
6202 the ICCCM (section 4.1.6) says that the window's border pixmap
6203 and border pixel are window attributes which are "private to the
6204 client", so we can always change it to whatever we want. */
6205 BLOCK_INPUT;
6206 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6207 f->output_data.x->border_pixel);
6208 UNBLOCK_INPUT;
6209 x_update_cursor (f, 1);
6210 }
6211
6212 static void
6213 frame_unhighlight (f)
6214 struct frame *f;
6215 {
6216 /* We used to only do this if Vx_no_window_manager was non-nil, but
6217 the ICCCM (section 4.1.6) says that the window's border pixmap
6218 and border pixel are window attributes which are "private to the
6219 client", so we can always change it to whatever we want. */
6220 BLOCK_INPUT;
6221 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6222 f->output_data.x->border_tile);
6223 UNBLOCK_INPUT;
6224 x_update_cursor (f, 1);
6225 }
6226
6227 /* The focus has changed. Update the frames as necessary to reflect
6228 the new situation. Note that we can't change the selected frame
6229 here, because the Lisp code we are interrupting might become confused.
6230 Each event gets marked with the frame in which it occurred, so the
6231 Lisp code can tell when the switch took place by examining the events. */
6232
6233 static void
6234 x_new_focus_frame (dpyinfo, frame)
6235 struct x_display_info *dpyinfo;
6236 struct frame *frame;
6237 {
6238 struct frame *old_focus = dpyinfo->x_focus_frame;
6239
6240 if (frame != dpyinfo->x_focus_frame)
6241 {
6242 /* Set this before calling other routines, so that they see
6243 the correct value of x_focus_frame. */
6244 dpyinfo->x_focus_frame = frame;
6245
6246 if (old_focus && old_focus->auto_lower)
6247 x_lower_frame (old_focus);
6248
6249 #if 0
6250 selected_frame = frame;
6251 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6252 selected_frame);
6253 Fselect_window (selected_frame->selected_window);
6254 choose_minibuf_frame ();
6255 #endif /* ! 0 */
6256
6257 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6258 pending_autoraise_frame = dpyinfo->x_focus_frame;
6259 else
6260 pending_autoraise_frame = 0;
6261 }
6262
6263 x_frame_rehighlight (dpyinfo);
6264 }
6265
6266 /* Handle an event saying the mouse has moved out of an Emacs frame. */
6267
6268 void
6269 x_mouse_leave (dpyinfo)
6270 struct x_display_info *dpyinfo;
6271 {
6272 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
6273 }
6274
6275 /* The focus has changed, or we have redirected a frame's focus to
6276 another frame (this happens when a frame uses a surrogate
6277 mini-buffer frame). Shift the highlight as appropriate.
6278
6279 The FRAME argument doesn't necessarily have anything to do with which
6280 frame is being highlighted or un-highlighted; we only use it to find
6281 the appropriate X display info. */
6282
6283 static void
6284 XTframe_rehighlight (frame)
6285 struct frame *frame;
6286 {
6287 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6288 }
6289
6290 static void
6291 x_frame_rehighlight (dpyinfo)
6292 struct x_display_info *dpyinfo;
6293 {
6294 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6295
6296 if (dpyinfo->x_focus_frame)
6297 {
6298 dpyinfo->x_highlight_frame
6299 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6300 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6301 : dpyinfo->x_focus_frame);
6302 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
6303 {
6304 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6305 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
6306 }
6307 }
6308 else
6309 dpyinfo->x_highlight_frame = 0;
6310
6311 if (dpyinfo->x_highlight_frame != old_highlight)
6312 {
6313 if (old_highlight)
6314 frame_unhighlight (old_highlight);
6315 if (dpyinfo->x_highlight_frame)
6316 frame_highlight (dpyinfo->x_highlight_frame);
6317 }
6318 }
6319
6320
6321 \f
6322 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
6323
6324 /* Initialize mode_switch_bit and modifier_meaning. */
6325 static void
6326 x_find_modifier_meanings (dpyinfo)
6327 struct x_display_info *dpyinfo;
6328 {
6329 int min_code, max_code;
6330 KeySym *syms;
6331 int syms_per_code;
6332 XModifierKeymap *mods;
6333
6334 dpyinfo->meta_mod_mask = 0;
6335 dpyinfo->shift_lock_mask = 0;
6336 dpyinfo->alt_mod_mask = 0;
6337 dpyinfo->super_mod_mask = 0;
6338 dpyinfo->hyper_mod_mask = 0;
6339
6340 #ifdef HAVE_X11R4
6341 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
6342 #else
6343 min_code = dpyinfo->display->min_keycode;
6344 max_code = dpyinfo->display->max_keycode;
6345 #endif
6346
6347 syms = XGetKeyboardMapping (dpyinfo->display,
6348 min_code, max_code - min_code + 1,
6349 &syms_per_code);
6350 mods = XGetModifierMapping (dpyinfo->display);
6351
6352 /* Scan the modifier table to see which modifier bits the Meta and
6353 Alt keysyms are on. */
6354 {
6355 int row, col; /* The row and column in the modifier table. */
6356
6357 for (row = 3; row < 8; row++)
6358 for (col = 0; col < mods->max_keypermod; col++)
6359 {
6360 KeyCode code
6361 = mods->modifiermap[(row * mods->max_keypermod) + col];
6362
6363 /* Zeroes are used for filler. Skip them. */
6364 if (code == 0)
6365 continue;
6366
6367 /* Are any of this keycode's keysyms a meta key? */
6368 {
6369 int code_col;
6370
6371 for (code_col = 0; code_col < syms_per_code; code_col++)
6372 {
6373 int sym = syms[((code - min_code) * syms_per_code) + code_col];
6374
6375 switch (sym)
6376 {
6377 case XK_Meta_L:
6378 case XK_Meta_R:
6379 dpyinfo->meta_mod_mask |= (1 << row);
6380 break;
6381
6382 case XK_Alt_L:
6383 case XK_Alt_R:
6384 dpyinfo->alt_mod_mask |= (1 << row);
6385 break;
6386
6387 case XK_Hyper_L:
6388 case XK_Hyper_R:
6389 dpyinfo->hyper_mod_mask |= (1 << row);
6390 break;
6391
6392 case XK_Super_L:
6393 case XK_Super_R:
6394 dpyinfo->super_mod_mask |= (1 << row);
6395 break;
6396
6397 case XK_Shift_Lock:
6398 /* Ignore this if it's not on the lock modifier. */
6399 if ((1 << row) == LockMask)
6400 dpyinfo->shift_lock_mask = LockMask;
6401 break;
6402 }
6403 }
6404 }
6405 }
6406 }
6407
6408 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
6409 if (! dpyinfo->meta_mod_mask)
6410 {
6411 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6412 dpyinfo->alt_mod_mask = 0;
6413 }
6414
6415 /* If some keys are both alt and meta,
6416 make them just meta, not alt. */
6417 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6418 {
6419 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6420 }
6421
6422 XFree ((char *) syms);
6423 XFreeModifiermap (mods);
6424 }
6425
6426 /* Convert between the modifier bits X uses and the modifier bits
6427 Emacs uses. */
6428
6429 static unsigned int
6430 x_x_to_emacs_modifiers (dpyinfo, state)
6431 struct x_display_info *dpyinfo;
6432 unsigned int state;
6433 {
6434 EMACS_UINT mod_meta = meta_modifier;
6435 EMACS_UINT mod_alt = alt_modifier;
6436 EMACS_UINT mod_hyper = hyper_modifier;
6437 EMACS_UINT mod_super = super_modifier;
6438 Lisp_Object tem;
6439
6440 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6441 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6442 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6443 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6444 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6445 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6446 tem = Fget (Vx_super_keysym, Qmodifier_value);
6447 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
6448
6449
6450 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6451 | ((state & ControlMask) ? ctrl_modifier : 0)
6452 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
6453 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
6454 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
6455 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
6456 }
6457
6458 static unsigned int
6459 x_emacs_to_x_modifiers (dpyinfo, state)
6460 struct x_display_info *dpyinfo;
6461 unsigned int state;
6462 {
6463 EMACS_UINT mod_meta = meta_modifier;
6464 EMACS_UINT mod_alt = alt_modifier;
6465 EMACS_UINT mod_hyper = hyper_modifier;
6466 EMACS_UINT mod_super = super_modifier;
6467
6468 Lisp_Object tem;
6469
6470 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6471 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6472 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6473 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6474 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6475 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6476 tem = Fget (Vx_super_keysym, Qmodifier_value);
6477 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
6478
6479
6480 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
6481 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
6482 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
6483 | ((state & shift_modifier) ? ShiftMask : 0)
6484 | ((state & ctrl_modifier) ? ControlMask : 0)
6485 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
6486 }
6487
6488 /* Convert a keysym to its name. */
6489
6490 char *
6491 x_get_keysym_name (keysym)
6492 KeySym keysym;
6493 {
6494 char *value;
6495
6496 BLOCK_INPUT;
6497 value = XKeysymToString (keysym);
6498 UNBLOCK_INPUT;
6499
6500 return value;
6501 }
6502
6503
6504 \f
6505 /* Mouse clicks and mouse movement. Rah. */
6506
6507 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6508 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6509 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6510 not force the value into range. */
6511
6512 void
6513 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6514 FRAME_PTR f;
6515 register int pix_x, pix_y;
6516 register int *x, *y;
6517 XRectangle *bounds;
6518 int noclip;
6519 {
6520 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6521 even for negative values. */
6522 if (pix_x < 0)
6523 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6524 if (pix_y < 0)
6525 pix_y -= (f)->output_data.x->line_height - 1;
6526
6527 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6528 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6529
6530 if (bounds)
6531 {
6532 bounds->width = FONT_WIDTH (f->output_data.x->font);
6533 bounds->height = f->output_data.x->line_height;
6534 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6535 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6536 }
6537
6538 if (!noclip)
6539 {
6540 if (pix_x < 0)
6541 pix_x = 0;
6542 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6543 pix_x = FRAME_WINDOW_WIDTH (f);
6544
6545 if (pix_y < 0)
6546 pix_y = 0;
6547 else if (pix_y > f->height)
6548 pix_y = f->height;
6549 }
6550
6551 *x = pix_x;
6552 *y = pix_y;
6553 }
6554
6555
6556 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6557 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6558 can't tell the positions because W's display is not up to date,
6559 return 0. */
6560
6561 int
6562 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6563 struct window *w;
6564 int hpos, vpos;
6565 int *frame_x, *frame_y;
6566 {
6567 int success_p;
6568
6569 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6570 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6571
6572 if (display_completed)
6573 {
6574 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6575 struct glyph *glyph = row->glyphs[TEXT_AREA];
6576 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6577
6578 *frame_y = row->y;
6579 *frame_x = row->x;
6580 while (glyph < end)
6581 {
6582 *frame_x += glyph->pixel_width;
6583 ++glyph;
6584 }
6585
6586 success_p = 1;
6587 }
6588 else
6589 {
6590 *frame_y = *frame_x = 0;
6591 success_p = 0;
6592 }
6593
6594 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6595 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6596 return success_p;
6597 }
6598
6599
6600 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6601
6602 If the event is a button press, then note that we have grabbed
6603 the mouse. */
6604
6605 static Lisp_Object
6606 construct_mouse_click (result, event, f)
6607 struct input_event *result;
6608 XButtonEvent *event;
6609 struct frame *f;
6610 {
6611 /* Make the event type no_event; we'll change that when we decide
6612 otherwise. */
6613 result->kind = mouse_click;
6614 result->code = event->button - Button1;
6615 result->timestamp = event->time;
6616 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6617 event->state)
6618 | (event->type == ButtonRelease
6619 ? up_modifier
6620 : down_modifier));
6621
6622 XSETINT (result->x, event->x);
6623 XSETINT (result->y, event->y);
6624 XSETFRAME (result->frame_or_window, f);
6625 result->arg = Qnil;
6626 return Qnil;
6627 }
6628
6629 \f
6630 /* Function to report a mouse movement to the mainstream Emacs code.
6631 The input handler calls this.
6632
6633 We have received a mouse movement event, which is given in *event.
6634 If the mouse is over a different glyph than it was last time, tell
6635 the mainstream emacs code by setting mouse_moved. If not, ask for
6636 another motion event, so we can check again the next time it moves. */
6637
6638 static XMotionEvent last_mouse_motion_event;
6639 static Lisp_Object last_mouse_motion_frame;
6640
6641 static void
6642 note_mouse_movement (frame, event)
6643 FRAME_PTR frame;
6644 XMotionEvent *event;
6645 {
6646 last_mouse_movement_time = event->time;
6647 last_mouse_motion_event = *event;
6648 XSETFRAME (last_mouse_motion_frame, frame);
6649
6650 #if 0 /* Lisp must not be called asynchronously, so this must
6651 not be done. */
6652 if (x_autoselect_window_p)
6653 {
6654 int area;
6655 Lisp_Object window;
6656 static Lisp_Object last_window;
6657
6658 window = window_from_coordinates (frame, XINT (event->x), XINT (event->y), &area, 0);
6659
6660 /* Window will be selected only when it is not selected now and
6661 last mouse movement event was not in it. Minubuffer window
6662 will be selected iff it is active. */
6663 if (!EQ (window, last_window)
6664 && !EQ (window, selected_window)
6665 && (!MINI_WINDOW_P (XWINDOW (window))
6666 || (EQ (window, minibuf_window) && minibuf_level > 0)))
6667 Fselect_window (window);
6668
6669 last_window=window;
6670 }
6671 #endif
6672
6673 if (event->window != FRAME_X_WINDOW (frame))
6674 {
6675 frame->mouse_moved = 1;
6676 last_mouse_scroll_bar = Qnil;
6677 note_mouse_highlight (frame, -1, -1);
6678 }
6679
6680 /* Has the mouse moved off the glyph it was on at the last sighting? */
6681 else if (event->x < last_mouse_glyph.x
6682 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6683 || event->y < last_mouse_glyph.y
6684 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6685 {
6686 frame->mouse_moved = 1;
6687 last_mouse_scroll_bar = Qnil;
6688 note_mouse_highlight (frame, event->x, event->y);
6689 }
6690 }
6691
6692 \f
6693 /************************************************************************
6694 Mouse Face
6695 ************************************************************************/
6696
6697 /* Find the glyph under window-relative coordinates X/Y in window W.
6698 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6699 strings. Return in *HPOS and *VPOS the row and column number of
6700 the glyph found. Return in *AREA the glyph area containing X.
6701 Value is a pointer to the glyph found or null if X/Y is not on
6702 text, or we can't tell because W's current matrix is not up to
6703 date. */
6704
6705 static struct glyph *
6706 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
6707 struct window *w;
6708 int x, y;
6709 int *hpos, *vpos, *area;
6710 int buffer_only_p;
6711 {
6712 struct glyph *glyph, *end;
6713 struct glyph_row *row = NULL;
6714 int x0, i, left_area_width;
6715
6716 /* Find row containing Y. Give up if some row is not enabled. */
6717 for (i = 0; i < w->current_matrix->nrows; ++i)
6718 {
6719 row = MATRIX_ROW (w->current_matrix, i);
6720 if (!row->enabled_p)
6721 return NULL;
6722 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6723 break;
6724 }
6725
6726 *vpos = i;
6727 *hpos = 0;
6728
6729 /* Give up if Y is not in the window. */
6730 if (i == w->current_matrix->nrows)
6731 return NULL;
6732
6733 /* Get the glyph area containing X. */
6734 if (w->pseudo_window_p)
6735 {
6736 *area = TEXT_AREA;
6737 x0 = 0;
6738 }
6739 else
6740 {
6741 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6742 if (x < left_area_width)
6743 {
6744 *area = LEFT_MARGIN_AREA;
6745 x0 = 0;
6746 }
6747 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6748 {
6749 *area = TEXT_AREA;
6750 x0 = row->x + left_area_width;
6751 }
6752 else
6753 {
6754 *area = RIGHT_MARGIN_AREA;
6755 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6756 }
6757 }
6758
6759 /* Find glyph containing X. */
6760 glyph = row->glyphs[*area];
6761 end = glyph + row->used[*area];
6762 while (glyph < end)
6763 {
6764 if (x < x0 + glyph->pixel_width)
6765 {
6766 if (w->pseudo_window_p)
6767 break;
6768 else if (!buffer_only_p || BUFFERP (glyph->object))
6769 break;
6770 }
6771
6772 x0 += glyph->pixel_width;
6773 ++glyph;
6774 }
6775
6776 if (glyph == end)
6777 return NULL;
6778
6779 *hpos = glyph - row->glyphs[*area];
6780 return glyph;
6781 }
6782
6783
6784 /* Convert frame-relative x/y to coordinates relative to window W.
6785 Takes pseudo-windows into account. */
6786
6787 static void
6788 frame_to_window_pixel_xy (w, x, y)
6789 struct window *w;
6790 int *x, *y;
6791 {
6792 if (w->pseudo_window_p)
6793 {
6794 /* A pseudo-window is always full-width, and starts at the
6795 left edge of the frame, plus a frame border. */
6796 struct frame *f = XFRAME (w->frame);
6797 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6798 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6799 }
6800 else
6801 {
6802 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6803 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6804 }
6805 }
6806
6807
6808 /* Take proper action when mouse has moved to the mode or header line of
6809 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6810 mode line. X is relative to the start of the text display area of
6811 W, so the width of fringes and scroll bars must be subtracted
6812 to get a position relative to the start of the mode line. */
6813
6814 static void
6815 note_mode_line_highlight (w, x, mode_line_p)
6816 struct window *w;
6817 int x, mode_line_p;
6818 {
6819 struct frame *f = XFRAME (w->frame);
6820 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6821 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6822 struct glyph_row *row;
6823
6824 if (mode_line_p)
6825 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6826 else
6827 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6828
6829 if (row->enabled_p)
6830 {
6831 struct glyph *glyph, *end;
6832 Lisp_Object help, map;
6833 int x0;
6834
6835 /* Find the glyph under X. */
6836 glyph = row->glyphs[TEXT_AREA];
6837 end = glyph + row->used[TEXT_AREA];
6838 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6839 + FRAME_X_LEFT_FRINGE_WIDTH (f));
6840
6841 while (glyph < end
6842 && x >= x0 + glyph->pixel_width)
6843 {
6844 x0 += glyph->pixel_width;
6845 ++glyph;
6846 }
6847
6848 if (glyph < end
6849 && STRINGP (glyph->object)
6850 && XSTRING (glyph->object)->intervals
6851 && glyph->charpos >= 0
6852 && glyph->charpos < XSTRING (glyph->object)->size)
6853 {
6854 /* If we're on a string with `help-echo' text property,
6855 arrange for the help to be displayed. This is done by
6856 setting the global variable help_echo to the help string. */
6857 help = Fget_text_property (make_number (glyph->charpos),
6858 Qhelp_echo, glyph->object);
6859 if (!NILP (help))
6860 {
6861 help_echo = help;
6862 XSETWINDOW (help_echo_window, w);
6863 help_echo_object = glyph->object;
6864 help_echo_pos = glyph->charpos;
6865 }
6866
6867 /* Change the mouse pointer according to what is under X/Y. */
6868 map = Fget_text_property (make_number (glyph->charpos),
6869 Qlocal_map, glyph->object);
6870 if (KEYMAPP (map))
6871 cursor = f->output_data.x->nontext_cursor;
6872 else
6873 {
6874 map = Fget_text_property (make_number (glyph->charpos),
6875 Qkeymap, glyph->object);
6876 if (KEYMAPP (map))
6877 cursor = f->output_data.x->nontext_cursor;
6878 }
6879 }
6880 }
6881
6882 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6883 }
6884
6885
6886 /* Take proper action when the mouse has moved to position X, Y on
6887 frame F as regards highlighting characters that have mouse-face
6888 properties. Also de-highlighting chars where the mouse was before.
6889 X and Y can be negative or out of range. */
6890
6891 static void
6892 note_mouse_highlight (f, x, y)
6893 struct frame *f;
6894 int x, y;
6895 {
6896 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6897 int portion;
6898 Lisp_Object window;
6899 struct window *w;
6900 Cursor cursor = None;
6901 struct buffer *b;
6902
6903 /* When a menu is active, don't highlight because this looks odd. */
6904 #ifdef USE_X_TOOLKIT
6905 if (popup_activated ())
6906 return;
6907 #endif
6908
6909 if (NILP (Vmouse_highlight)
6910 || !f->glyphs_initialized_p)
6911 return;
6912
6913 dpyinfo->mouse_face_mouse_x = x;
6914 dpyinfo->mouse_face_mouse_y = y;
6915 dpyinfo->mouse_face_mouse_frame = f;
6916
6917 if (dpyinfo->mouse_face_defer)
6918 return;
6919
6920 if (gc_in_progress)
6921 {
6922 dpyinfo->mouse_face_deferred_gc = 1;
6923 return;
6924 }
6925
6926 /* Which window is that in? */
6927 window = window_from_coordinates (f, x, y, &portion, 1);
6928
6929 /* If we were displaying active text in another window, clear that. */
6930 if (! EQ (window, dpyinfo->mouse_face_window))
6931 clear_mouse_face (dpyinfo);
6932
6933 /* Not on a window -> return. */
6934 if (!WINDOWP (window))
6935 return;
6936
6937 /* Convert to window-relative pixel coordinates. */
6938 w = XWINDOW (window);
6939 frame_to_window_pixel_xy (w, &x, &y);
6940
6941 /* Handle tool-bar window differently since it doesn't display a
6942 buffer. */
6943 if (EQ (window, f->tool_bar_window))
6944 {
6945 note_tool_bar_highlight (f, x, y);
6946 return;
6947 }
6948
6949 /* Mouse is on the mode or header line? */
6950 if (portion == 1 || portion == 3)
6951 {
6952 note_mode_line_highlight (w, x, portion == 1);
6953 return;
6954 }
6955
6956 if (portion == 2)
6957 cursor = f->output_data.x->horizontal_drag_cursor;
6958 else
6959 cursor = f->output_data.x->text_cursor;
6960
6961 /* Are we in a window whose display is up to date?
6962 And verify the buffer's text has not changed. */
6963 b = XBUFFER (w->buffer);
6964 if (/* Within text portion of the window. */
6965 portion == 0
6966 && EQ (w->window_end_valid, w->buffer)
6967 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
6968 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
6969 {
6970 int hpos, vpos, pos, i, area;
6971 struct glyph *glyph;
6972 Lisp_Object object;
6973 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
6974 Lisp_Object *overlay_vec = NULL;
6975 int len, noverlays;
6976 struct buffer *obuf;
6977 int obegv, ozv, same_region;
6978
6979 /* Find the glyph under X/Y. */
6980 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
6981
6982 /* Clear mouse face if X/Y not over text. */
6983 if (glyph == NULL
6984 || area != TEXT_AREA
6985 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6986 {
6987 if (clear_mouse_face (dpyinfo))
6988 cursor = None;
6989 goto set_cursor;
6990 }
6991
6992 pos = glyph->charpos;
6993 object = glyph->object;
6994 if (!STRINGP (object) && !BUFFERP (object))
6995 goto set_cursor;
6996
6997 /* If we get an out-of-range value, return now; avoid an error. */
6998 if (BUFFERP (object) && pos > BUF_Z (b))
6999 goto set_cursor;
7000
7001 /* Make the window's buffer temporarily current for
7002 overlays_at and compute_char_face. */
7003 obuf = current_buffer;
7004 current_buffer = b;
7005 obegv = BEGV;
7006 ozv = ZV;
7007 BEGV = BEG;
7008 ZV = Z;
7009
7010 /* Is this char mouse-active or does it have help-echo? */
7011 position = make_number (pos);
7012
7013 if (BUFFERP (object))
7014 {
7015 /* Put all the overlays we want in a vector in overlay_vec.
7016 Store the length in len. If there are more than 10, make
7017 enough space for all, and try again. */
7018 len = 10;
7019 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7020 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
7021 if (noverlays > len)
7022 {
7023 len = noverlays;
7024 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7025 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
7026 }
7027
7028 /* Sort overlays into increasing priority order. */
7029 noverlays = sort_overlays (overlay_vec, noverlays, w);
7030 }
7031 else
7032 noverlays = 0;
7033
7034 same_region = (EQ (window, dpyinfo->mouse_face_window)
7035 && vpos >= dpyinfo->mouse_face_beg_row
7036 && vpos <= dpyinfo->mouse_face_end_row
7037 && (vpos > dpyinfo->mouse_face_beg_row
7038 || hpos >= dpyinfo->mouse_face_beg_col)
7039 && (vpos < dpyinfo->mouse_face_end_row
7040 || hpos < dpyinfo->mouse_face_end_col
7041 || dpyinfo->mouse_face_past_end));
7042
7043 if (same_region)
7044 cursor = None;
7045
7046 /* Check mouse-face highlighting. */
7047 if (! same_region
7048 /* If there exists an overlay with mouse-face overlapping
7049 the one we are currently highlighting, we have to
7050 check if we enter the overlapping overlay, and then
7051 highlight only that. */
7052 || (OVERLAYP (dpyinfo->mouse_face_overlay)
7053 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
7054 {
7055 /* Find the highest priority overlay that has a mouse-face
7056 property. */
7057 overlay = Qnil;
7058 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
7059 {
7060 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
7061 if (!NILP (mouse_face))
7062 overlay = overlay_vec[i];
7063 }
7064
7065 /* If we're actually highlighting the same overlay as
7066 before, there's no need to do that again. */
7067 if (!NILP (overlay)
7068 && EQ (overlay, dpyinfo->mouse_face_overlay))
7069 goto check_help_echo;
7070
7071 dpyinfo->mouse_face_overlay = overlay;
7072
7073 /* Clear the display of the old active region, if any. */
7074 if (clear_mouse_face (dpyinfo))
7075 cursor = None;
7076
7077 /* If no overlay applies, get a text property. */
7078 if (NILP (overlay))
7079 mouse_face = Fget_text_property (position, Qmouse_face, object);
7080
7081 /* Handle the overlay case. */
7082 if (!NILP (overlay))
7083 {
7084 /* Find the range of text around this char that
7085 should be active. */
7086 Lisp_Object before, after;
7087 int ignore;
7088
7089 before = Foverlay_start (overlay);
7090 after = Foverlay_end (overlay);
7091 /* Record this as the current active region. */
7092 fast_find_position (w, XFASTINT (before),
7093 &dpyinfo->mouse_face_beg_col,
7094 &dpyinfo->mouse_face_beg_row,
7095 &dpyinfo->mouse_face_beg_x,
7096 &dpyinfo->mouse_face_beg_y, Qnil);
7097
7098 dpyinfo->mouse_face_past_end
7099 = !fast_find_position (w, XFASTINT (after),
7100 &dpyinfo->mouse_face_end_col,
7101 &dpyinfo->mouse_face_end_row,
7102 &dpyinfo->mouse_face_end_x,
7103 &dpyinfo->mouse_face_end_y, Qnil);
7104 dpyinfo->mouse_face_window = window;
7105 dpyinfo->mouse_face_face_id
7106 = face_at_buffer_position (w, pos, 0, 0,
7107 &ignore, pos + 1, 1);
7108
7109 /* Display it as active. */
7110 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7111 cursor = None;
7112 }
7113 /* Handle the text property case. */
7114 else if (!NILP (mouse_face) && BUFFERP (object))
7115 {
7116 /* Find the range of text around this char that
7117 should be active. */
7118 Lisp_Object before, after, beginning, end;
7119 int ignore;
7120
7121 beginning = Fmarker_position (w->start);
7122 end = make_number (BUF_Z (XBUFFER (object))
7123 - XFASTINT (w->window_end_pos));
7124 before
7125 = Fprevious_single_property_change (make_number (pos + 1),
7126 Qmouse_face,
7127 object, beginning);
7128 after
7129 = Fnext_single_property_change (position, Qmouse_face,
7130 object, end);
7131
7132 /* Record this as the current active region. */
7133 fast_find_position (w, XFASTINT (before),
7134 &dpyinfo->mouse_face_beg_col,
7135 &dpyinfo->mouse_face_beg_row,
7136 &dpyinfo->mouse_face_beg_x,
7137 &dpyinfo->mouse_face_beg_y, Qnil);
7138 dpyinfo->mouse_face_past_end
7139 = !fast_find_position (w, XFASTINT (after),
7140 &dpyinfo->mouse_face_end_col,
7141 &dpyinfo->mouse_face_end_row,
7142 &dpyinfo->mouse_face_end_x,
7143 &dpyinfo->mouse_face_end_y, Qnil);
7144 dpyinfo->mouse_face_window = window;
7145
7146 if (BUFFERP (object))
7147 dpyinfo->mouse_face_face_id
7148 = face_at_buffer_position (w, pos, 0, 0,
7149 &ignore, pos + 1, 1);
7150
7151 /* Display it as active. */
7152 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7153 cursor = None;
7154 }
7155 else if (!NILP (mouse_face) && STRINGP (object))
7156 {
7157 Lisp_Object b, e;
7158 int ignore;
7159
7160 b = Fprevious_single_property_change (make_number (pos + 1),
7161 Qmouse_face,
7162 object, Qnil);
7163 e = Fnext_single_property_change (position, Qmouse_face,
7164 object, Qnil);
7165 if (NILP (b))
7166 b = make_number (0);
7167 if (NILP (e))
7168 e = make_number (XSTRING (object)->size - 1);
7169 fast_find_string_pos (w, XINT (b), object,
7170 &dpyinfo->mouse_face_beg_col,
7171 &dpyinfo->mouse_face_beg_row,
7172 &dpyinfo->mouse_face_beg_x,
7173 &dpyinfo->mouse_face_beg_y, 0);
7174 fast_find_string_pos (w, XINT (e), object,
7175 &dpyinfo->mouse_face_end_col,
7176 &dpyinfo->mouse_face_end_row,
7177 &dpyinfo->mouse_face_end_x,
7178 &dpyinfo->mouse_face_end_y, 1);
7179 dpyinfo->mouse_face_past_end = 0;
7180 dpyinfo->mouse_face_window = window;
7181 dpyinfo->mouse_face_face_id
7182 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
7183 glyph->face_id, 1);
7184 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7185 cursor = None;
7186 }
7187 else if (STRINGP (object) && NILP (mouse_face))
7188 {
7189 /* A string which doesn't have mouse-face, but
7190 the text ``under'' it might have. */
7191 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
7192 int start = MATRIX_ROW_START_CHARPOS (r);
7193
7194 pos = string_buffer_position (w, object, start);
7195 if (pos > 0)
7196 mouse_face = get_char_property_and_overlay (make_number (pos),
7197 Qmouse_face,
7198 w->buffer,
7199 &overlay);
7200 if (!NILP (mouse_face) && !NILP (overlay))
7201 {
7202 Lisp_Object before = Foverlay_start (overlay);
7203 Lisp_Object after = Foverlay_end (overlay);
7204 int ignore;
7205
7206 /* Note that we might not be able to find position
7207 BEFORE in the glyph matrix if the overlay is
7208 entirely covered by a `display' property. In
7209 this case, we overshoot. So let's stop in
7210 the glyph matrix before glyphs for OBJECT. */
7211 fast_find_position (w, XFASTINT (before),
7212 &dpyinfo->mouse_face_beg_col,
7213 &dpyinfo->mouse_face_beg_row,
7214 &dpyinfo->mouse_face_beg_x,
7215 &dpyinfo->mouse_face_beg_y,
7216 object);
7217
7218 dpyinfo->mouse_face_past_end
7219 = !fast_find_position (w, XFASTINT (after),
7220 &dpyinfo->mouse_face_end_col,
7221 &dpyinfo->mouse_face_end_row,
7222 &dpyinfo->mouse_face_end_x,
7223 &dpyinfo->mouse_face_end_y,
7224 Qnil);
7225 dpyinfo->mouse_face_window = window;
7226 dpyinfo->mouse_face_face_id
7227 = face_at_buffer_position (w, pos, 0, 0,
7228 &ignore, pos + 1, 1);
7229
7230 /* Display it as active. */
7231 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7232 cursor = None;
7233 }
7234 }
7235 }
7236
7237 check_help_echo:
7238
7239 /* Look for a `help-echo' property. */
7240 {
7241 Lisp_Object help, overlay;
7242
7243 /* Check overlays first. */
7244 help = overlay = Qnil;
7245 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
7246 {
7247 overlay = overlay_vec[i];
7248 help = Foverlay_get (overlay, Qhelp_echo);
7249 }
7250
7251 if (!NILP (help))
7252 {
7253 help_echo = help;
7254 help_echo_window = window;
7255 help_echo_object = overlay;
7256 help_echo_pos = pos;
7257 }
7258 else
7259 {
7260 Lisp_Object object = glyph->object;
7261 int charpos = glyph->charpos;
7262
7263 /* Try text properties. */
7264 if (STRINGP (object)
7265 && charpos >= 0
7266 && charpos < XSTRING (object)->size)
7267 {
7268 help = Fget_text_property (make_number (charpos),
7269 Qhelp_echo, object);
7270 if (NILP (help))
7271 {
7272 /* If the string itself doesn't specify a help-echo,
7273 see if the buffer text ``under'' it does. */
7274 struct glyph_row *r
7275 = MATRIX_ROW (w->current_matrix, vpos);
7276 int start = MATRIX_ROW_START_CHARPOS (r);
7277 int pos = string_buffer_position (w, object, start);
7278 if (pos > 0)
7279 {
7280 help = Fget_char_property (make_number (pos),
7281 Qhelp_echo, w->buffer);
7282 if (!NILP (help))
7283 {
7284 charpos = pos;
7285 object = w->buffer;
7286 }
7287 }
7288 }
7289 }
7290 else if (BUFFERP (object)
7291 && charpos >= BEGV
7292 && charpos < ZV)
7293 help = Fget_text_property (make_number (charpos), Qhelp_echo,
7294 object);
7295
7296 if (!NILP (help))
7297 {
7298 help_echo = help;
7299 help_echo_window = window;
7300 help_echo_object = object;
7301 help_echo_pos = charpos;
7302 }
7303 }
7304 }
7305
7306 BEGV = obegv;
7307 ZV = ozv;
7308 current_buffer = obuf;
7309 }
7310
7311 set_cursor:
7312
7313 if (cursor != None)
7314 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7315 }
7316
7317 static void
7318 redo_mouse_highlight ()
7319 {
7320 if (!NILP (last_mouse_motion_frame)
7321 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
7322 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
7323 last_mouse_motion_event.x,
7324 last_mouse_motion_event.y);
7325 }
7326
7327
7328 \f
7329 /***********************************************************************
7330 Tool-bars
7331 ***********************************************************************/
7332
7333 static int x_tool_bar_item P_ ((struct frame *, int, int,
7334 struct glyph **, int *, int *, int *));
7335
7336 /* Tool-bar item index of the item on which a mouse button was pressed
7337 or -1. */
7338
7339 static int last_tool_bar_item;
7340
7341
7342 /* Get information about the tool-bar item at position X/Y on frame F.
7343 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7344 the current matrix of the tool-bar window of F, or NULL if not
7345 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
7346 item in F->tool_bar_items. Value is
7347
7348 -1 if X/Y is not on a tool-bar item
7349 0 if X/Y is on the same item that was highlighted before.
7350 1 otherwise. */
7351
7352 static int
7353 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
7354 struct frame *f;
7355 int x, y;
7356 struct glyph **glyph;
7357 int *hpos, *vpos, *prop_idx;
7358 {
7359 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7360 struct window *w = XWINDOW (f->tool_bar_window);
7361 int area;
7362
7363 /* Find the glyph under X/Y. */
7364 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
7365 if (*glyph == NULL)
7366 return -1;
7367
7368 /* Get the start of this tool-bar item's properties in
7369 f->tool_bar_items. */
7370 if (!tool_bar_item_info (f, *glyph, prop_idx))
7371 return -1;
7372
7373 /* Is mouse on the highlighted item? */
7374 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
7375 && *vpos >= dpyinfo->mouse_face_beg_row
7376 && *vpos <= dpyinfo->mouse_face_end_row
7377 && (*vpos > dpyinfo->mouse_face_beg_row
7378 || *hpos >= dpyinfo->mouse_face_beg_col)
7379 && (*vpos < dpyinfo->mouse_face_end_row
7380 || *hpos < dpyinfo->mouse_face_end_col
7381 || dpyinfo->mouse_face_past_end))
7382 return 0;
7383
7384 return 1;
7385 }
7386
7387
7388 /* Handle mouse button event on the tool-bar of frame F, at
7389 frame-relative coordinates X/Y. EVENT_TYPE is either ButtonPress
7390 or ButtonRelase. */
7391
7392 static void
7393 x_handle_tool_bar_click (f, button_event)
7394 struct frame *f;
7395 XButtonEvent *button_event;
7396 {
7397 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7398 struct window *w = XWINDOW (f->tool_bar_window);
7399 int hpos, vpos, prop_idx;
7400 struct glyph *glyph;
7401 Lisp_Object enabled_p;
7402 int x = button_event->x;
7403 int y = button_event->y;
7404
7405 /* If not on the highlighted tool-bar item, return. */
7406 frame_to_window_pixel_xy (w, &x, &y);
7407 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
7408 return;
7409
7410 /* If item is disabled, do nothing. */
7411 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7412 if (NILP (enabled_p))
7413 return;
7414
7415 if (button_event->type == ButtonPress)
7416 {
7417 /* Show item in pressed state. */
7418 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7419 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
7420 last_tool_bar_item = prop_idx;
7421 }
7422 else
7423 {
7424 Lisp_Object key, frame;
7425 struct input_event event;
7426
7427 /* Show item in released state. */
7428 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7429 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7430
7431 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
7432
7433 XSETFRAME (frame, f);
7434 event.kind = TOOL_BAR_EVENT;
7435 event.frame_or_window = frame;
7436 event.arg = frame;
7437 kbd_buffer_store_event (&event);
7438
7439 event.kind = TOOL_BAR_EVENT;
7440 event.frame_or_window = frame;
7441 event.arg = key;
7442 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7443 button_event->state);
7444 kbd_buffer_store_event (&event);
7445 last_tool_bar_item = -1;
7446 }
7447 }
7448
7449
7450 /* Possibly highlight a tool-bar item on frame F when mouse moves to
7451 tool-bar window-relative coordinates X/Y. Called from
7452 note_mouse_highlight. */
7453
7454 static void
7455 note_tool_bar_highlight (f, x, y)
7456 struct frame *f;
7457 int x, y;
7458 {
7459 Lisp_Object window = f->tool_bar_window;
7460 struct window *w = XWINDOW (window);
7461 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7462 int hpos, vpos;
7463 struct glyph *glyph;
7464 struct glyph_row *row;
7465 int i;
7466 Lisp_Object enabled_p;
7467 int prop_idx;
7468 enum draw_glyphs_face draw;
7469 int mouse_down_p, rc;
7470
7471 /* Function note_mouse_highlight is called with negative x(y
7472 values when mouse moves outside of the frame. */
7473 if (x <= 0 || y <= 0)
7474 {
7475 clear_mouse_face (dpyinfo);
7476 return;
7477 }
7478
7479 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7480 if (rc < 0)
7481 {
7482 /* Not on tool-bar item. */
7483 clear_mouse_face (dpyinfo);
7484 return;
7485 }
7486 else if (rc == 0)
7487 goto set_help_echo;
7488
7489 clear_mouse_face (dpyinfo);
7490
7491 /* Mouse is down, but on different tool-bar item? */
7492 mouse_down_p = (dpyinfo->grabbed
7493 && f == last_mouse_frame
7494 && FRAME_LIVE_P (f));
7495 if (mouse_down_p
7496 && last_tool_bar_item != prop_idx)
7497 return;
7498
7499 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7500 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7501
7502 /* If tool-bar item is not enabled, don't highlight it. */
7503 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7504 if (!NILP (enabled_p))
7505 {
7506 /* Compute the x-position of the glyph. In front and past the
7507 image is a space. We include this is the highlighted area. */
7508 row = MATRIX_ROW (w->current_matrix, vpos);
7509 for (i = x = 0; i < hpos; ++i)
7510 x += row->glyphs[TEXT_AREA][i].pixel_width;
7511
7512 /* Record this as the current active region. */
7513 dpyinfo->mouse_face_beg_col = hpos;
7514 dpyinfo->mouse_face_beg_row = vpos;
7515 dpyinfo->mouse_face_beg_x = x;
7516 dpyinfo->mouse_face_beg_y = row->y;
7517 dpyinfo->mouse_face_past_end = 0;
7518
7519 dpyinfo->mouse_face_end_col = hpos + 1;
7520 dpyinfo->mouse_face_end_row = vpos;
7521 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7522 dpyinfo->mouse_face_end_y = row->y;
7523 dpyinfo->mouse_face_window = window;
7524 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7525
7526 /* Display it as active. */
7527 show_mouse_face (dpyinfo, draw);
7528 dpyinfo->mouse_face_image_state = draw;
7529 }
7530
7531 set_help_echo:
7532
7533 /* Set help_echo to a help string to display for this tool-bar item.
7534 XTread_socket does the rest. */
7535 help_echo_object = help_echo_window = Qnil;
7536 help_echo_pos = -1;
7537 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
7538 if (NILP (help_echo))
7539 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
7540 }
7541
7542
7543 \f
7544 /* Find the glyph matrix position of buffer position CHARPOS in window
7545 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7546 current glyphs must be up to date. If CHARPOS is above window
7547 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
7548 of last line in W. In the row containing CHARPOS, stop before glyphs
7549 having STOP as object. */
7550
7551 #if 0 /* This is a version of fast_find_position that's more correct
7552 in the presence of hscrolling, for example. I didn't install
7553 it right away because the problem fixed is minor, it failed
7554 in 20.x as well, and I think it's too risky to install
7555 so near the release of 21.1. 2001-09-25 gerd. */
7556
7557 static int
7558 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
7559 struct window *w;
7560 int charpos;
7561 int *hpos, *vpos, *x, *y;
7562 Lisp_Object stop;
7563 {
7564 struct glyph_row *row, *first;
7565 struct glyph *glyph, *end;
7566 int i, past_end = 0;
7567
7568 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7569 row = row_containing_pos (w, charpos, first, NULL, 0);
7570 if (row == NULL)
7571 {
7572 if (charpos < MATRIX_ROW_START_CHARPOS (first))
7573 {
7574 *x = *y = *hpos = *vpos = 0;
7575 return 0;
7576 }
7577 else
7578 {
7579 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
7580 past_end = 1;
7581 }
7582 }
7583
7584 *x = row->x;
7585 *y = row->y;
7586 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7587
7588 glyph = row->glyphs[TEXT_AREA];
7589 end = glyph + row->used[TEXT_AREA];
7590
7591 /* Skip over glyphs not having an object at the start of the row.
7592 These are special glyphs like truncation marks on terminal
7593 frames. */
7594 if (row->displays_text_p)
7595 while (glyph < end
7596 && INTEGERP (glyph->object)
7597 && !EQ (stop, glyph->object)
7598 && glyph->charpos < 0)
7599 {
7600 *x += glyph->pixel_width;
7601 ++glyph;
7602 }
7603
7604 while (glyph < end
7605 && !INTEGERP (glyph->object)
7606 && !EQ (stop, glyph->object)
7607 && (!BUFFERP (glyph->object)
7608 || glyph->charpos < charpos))
7609 {
7610 *x += glyph->pixel_width;
7611 ++glyph;
7612 }
7613
7614 *hpos = glyph - row->glyphs[TEXT_AREA];
7615 return past_end;
7616 }
7617
7618 #else /* not 0 */
7619
7620 static int
7621 fast_find_position (w, pos, hpos, vpos, x, y, stop)
7622 struct window *w;
7623 int pos;
7624 int *hpos, *vpos, *x, *y;
7625 Lisp_Object stop;
7626 {
7627 int i;
7628 int lastcol;
7629 int maybe_next_line_p = 0;
7630 int line_start_position;
7631 int yb = window_text_bottom_y (w);
7632 struct glyph_row *row, *best_row;
7633 int row_vpos, best_row_vpos;
7634 int current_x;
7635
7636 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7637 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7638
7639 while (row->y < yb)
7640 {
7641 if (row->used[TEXT_AREA])
7642 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7643 else
7644 line_start_position = 0;
7645
7646 if (line_start_position > pos)
7647 break;
7648 /* If the position sought is the end of the buffer,
7649 don't include the blank lines at the bottom of the window. */
7650 else if (line_start_position == pos
7651 && pos == BUF_ZV (XBUFFER (w->buffer)))
7652 {
7653 maybe_next_line_p = 1;
7654 break;
7655 }
7656 else if (line_start_position > 0)
7657 {
7658 best_row = row;
7659 best_row_vpos = row_vpos;
7660 }
7661
7662 if (row->y + row->height >= yb)
7663 break;
7664
7665 ++row;
7666 ++row_vpos;
7667 }
7668
7669 /* Find the right column within BEST_ROW. */
7670 lastcol = 0;
7671 current_x = best_row->x;
7672 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
7673 {
7674 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7675 int charpos = glyph->charpos;
7676
7677 if (BUFFERP (glyph->object))
7678 {
7679 if (charpos == pos)
7680 {
7681 *hpos = i;
7682 *vpos = best_row_vpos;
7683 *x = current_x;
7684 *y = best_row->y;
7685 return 1;
7686 }
7687 else if (charpos > pos)
7688 break;
7689 }
7690 else if (EQ (glyph->object, stop))
7691 break;
7692
7693 if (charpos > 0)
7694 lastcol = i;
7695 current_x += glyph->pixel_width;
7696 }
7697
7698 /* If we're looking for the end of the buffer,
7699 and we didn't find it in the line we scanned,
7700 use the start of the following line. */
7701 if (maybe_next_line_p)
7702 {
7703 ++best_row;
7704 ++best_row_vpos;
7705 lastcol = 0;
7706 current_x = best_row->x;
7707 }
7708
7709 *vpos = best_row_vpos;
7710 *hpos = lastcol + 1;
7711 *x = current_x;
7712 *y = best_row->y;
7713 return 0;
7714 }
7715
7716 #endif /* not 0 */
7717
7718
7719 /* Find the position of the glyph for position POS in OBJECT in
7720 window W's current matrix, and return in *X, *Y the pixel
7721 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
7722
7723 RIGHT_P non-zero means return the position of the right edge of the
7724 glyph, RIGHT_P zero means return the left edge position.
7725
7726 If no glyph for POS exists in the matrix, return the position of
7727 the glyph with the next smaller position that is in the matrix, if
7728 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7729 exists in the matrix, return the position of the glyph with the
7730 next larger position in OBJECT.
7731
7732 Value is non-zero if a glyph was found. */
7733
7734 static int
7735 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7736 struct window *w;
7737 int pos;
7738 Lisp_Object object;
7739 int *hpos, *vpos, *x, *y;
7740 int right_p;
7741 {
7742 int yb = window_text_bottom_y (w);
7743 struct glyph_row *r;
7744 struct glyph *best_glyph = NULL;
7745 struct glyph_row *best_row = NULL;
7746 int best_x = 0;
7747
7748 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7749 r->enabled_p && r->y < yb;
7750 ++r)
7751 {
7752 struct glyph *g = r->glyphs[TEXT_AREA];
7753 struct glyph *e = g + r->used[TEXT_AREA];
7754 int gx;
7755
7756 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7757 if (EQ (g->object, object))
7758 {
7759 if (g->charpos == pos)
7760 {
7761 best_glyph = g;
7762 best_x = gx;
7763 best_row = r;
7764 goto found;
7765 }
7766 else if (best_glyph == NULL
7767 || ((abs (g->charpos - pos)
7768 < abs (best_glyph->charpos - pos))
7769 && (right_p
7770 ? g->charpos < pos
7771 : g->charpos > pos)))
7772 {
7773 best_glyph = g;
7774 best_x = gx;
7775 best_row = r;
7776 }
7777 }
7778 }
7779
7780 found:
7781
7782 if (best_glyph)
7783 {
7784 *x = best_x;
7785 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7786
7787 if (right_p)
7788 {
7789 *x += best_glyph->pixel_width;
7790 ++*hpos;
7791 }
7792
7793 *y = best_row->y;
7794 *vpos = best_row - w->current_matrix->rows;
7795 }
7796
7797 return best_glyph != NULL;
7798 }
7799
7800
7801 /* Display the active region described by mouse_face_*
7802 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7803
7804 static void
7805 show_mouse_face (dpyinfo, draw)
7806 struct x_display_info *dpyinfo;
7807 enum draw_glyphs_face draw;
7808 {
7809 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7810 struct frame *f = XFRAME (WINDOW_FRAME (w));
7811
7812 if (/* If window is in the process of being destroyed, don't bother
7813 to do anything. */
7814 w->current_matrix != NULL
7815 /* Don't update mouse highlight if hidden */
7816 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
7817 /* Recognize when we are called to operate on rows that don't exist
7818 anymore. This can happen when a window is split. */
7819 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
7820 {
7821 int phys_cursor_on_p = w->phys_cursor_on_p;
7822 struct glyph_row *row, *first, *last;
7823
7824 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
7825 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
7826
7827 for (row = first; row <= last && row->enabled_p; ++row)
7828 {
7829 int start_hpos, end_hpos, start_x;
7830
7831 /* For all but the first row, the highlight starts at column 0. */
7832 if (row == first)
7833 {
7834 start_hpos = dpyinfo->mouse_face_beg_col;
7835 start_x = dpyinfo->mouse_face_beg_x;
7836 }
7837 else
7838 {
7839 start_hpos = 0;
7840 start_x = 0;
7841 }
7842
7843 if (row == last)
7844 end_hpos = dpyinfo->mouse_face_end_col;
7845 else
7846 end_hpos = row->used[TEXT_AREA];
7847
7848 if (end_hpos > start_hpos)
7849 {
7850 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7851 start_hpos, end_hpos, draw, 0);
7852
7853 row->mouse_face_p
7854 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
7855 }
7856 }
7857
7858 /* When we've written over the cursor, arrange for it to
7859 be displayed again. */
7860 if (phys_cursor_on_p && !w->phys_cursor_on_p)
7861 x_display_cursor (w, 1,
7862 w->phys_cursor.hpos, w->phys_cursor.vpos,
7863 w->phys_cursor.x, w->phys_cursor.y);
7864 }
7865
7866 /* Change the mouse cursor. */
7867 if (draw == DRAW_NORMAL_TEXT)
7868 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7869 f->output_data.x->text_cursor);
7870 else if (draw == DRAW_MOUSE_FACE)
7871 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7872 f->output_data.x->cross_cursor);
7873 else
7874 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7875 f->output_data.x->nontext_cursor);
7876 }
7877
7878 /* Clear out the mouse-highlighted active region.
7879 Redraw it un-highlighted first. Value is non-zero if mouse
7880 face was actually drawn unhighlighted. */
7881
7882 static int
7883 clear_mouse_face (dpyinfo)
7884 struct x_display_info *dpyinfo;
7885 {
7886 int cleared = 0;
7887
7888 if (!NILP (dpyinfo->mouse_face_window))
7889 {
7890 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7891 cleared = 1;
7892 }
7893
7894 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7895 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7896 dpyinfo->mouse_face_window = Qnil;
7897 dpyinfo->mouse_face_overlay = Qnil;
7898 return cleared;
7899 }
7900
7901
7902 /* Clear any mouse-face on window W. This function is part of the
7903 redisplay interface, and is called from try_window_id and similar
7904 functions to ensure the mouse-highlight is off. */
7905
7906 static void
7907 x_clear_mouse_face (w)
7908 struct window *w;
7909 {
7910 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7911 Lisp_Object window;
7912
7913 BLOCK_INPUT;
7914 XSETWINDOW (window, w);
7915 if (EQ (window, dpyinfo->mouse_face_window))
7916 clear_mouse_face (dpyinfo);
7917 UNBLOCK_INPUT;
7918 }
7919
7920
7921 /* Just discard the mouse face information for frame F, if any.
7922 This is used when the size of F is changed. */
7923
7924 void
7925 cancel_mouse_face (f)
7926 FRAME_PTR f;
7927 {
7928 Lisp_Object window;
7929 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7930
7931 window = dpyinfo->mouse_face_window;
7932 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7933 {
7934 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7935 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7936 dpyinfo->mouse_face_window = Qnil;
7937 }
7938 }
7939
7940 \f
7941 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
7942
7943
7944 /* Try to determine frame pixel position and size of the glyph under
7945 frame pixel coordinates X/Y on frame F . Return the position and
7946 size in *RECT. Value is non-zero if we could compute these
7947 values. */
7948
7949 static int
7950 glyph_rect (f, x, y, rect)
7951 struct frame *f;
7952 int x, y;
7953 XRectangle *rect;
7954 {
7955 Lisp_Object window;
7956 int part, found = 0;
7957
7958 window = window_from_coordinates (f, x, y, &part, 0);
7959 if (!NILP (window))
7960 {
7961 struct window *w = XWINDOW (window);
7962 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7963 struct glyph_row *end = r + w->current_matrix->nrows - 1;
7964
7965 frame_to_window_pixel_xy (w, &x, &y);
7966
7967 for (; !found && r < end && r->enabled_p; ++r)
7968 if (r->y >= y)
7969 {
7970 struct glyph *g = r->glyphs[TEXT_AREA];
7971 struct glyph *end = g + r->used[TEXT_AREA];
7972 int gx;
7973
7974 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
7975 if (gx >= x)
7976 {
7977 rect->width = g->pixel_width;
7978 rect->height = r->height;
7979 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
7980 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
7981 found = 1;
7982 }
7983 }
7984 }
7985
7986 return found;
7987 }
7988
7989
7990 /* Return the current position of the mouse.
7991 *FP should be a frame which indicates which display to ask about.
7992
7993 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
7994 and *PART to the frame, window, and scroll bar part that the mouse
7995 is over. Set *X and *Y to the portion and whole of the mouse's
7996 position on the scroll bar.
7997
7998 If the mouse movement started elsewhere, set *FP to the frame the
7999 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
8000 the mouse is over.
8001
8002 Set *TIME to the server time-stamp for the time at which the mouse
8003 was at this position.
8004
8005 Don't store anything if we don't have a valid set of values to report.
8006
8007 This clears the mouse_moved flag, so we can wait for the next mouse
8008 movement. */
8009
8010 static void
8011 XTmouse_position (fp, insist, bar_window, part, x, y, time)
8012 FRAME_PTR *fp;
8013 int insist;
8014 Lisp_Object *bar_window;
8015 enum scroll_bar_part *part;
8016 Lisp_Object *x, *y;
8017 unsigned long *time;
8018 {
8019 FRAME_PTR f1;
8020
8021 BLOCK_INPUT;
8022
8023 if (! NILP (last_mouse_scroll_bar) && insist == 0)
8024 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
8025 else
8026 {
8027 Window root;
8028 int root_x, root_y;
8029
8030 Window dummy_window;
8031 int dummy;
8032
8033 Lisp_Object frame, tail;
8034
8035 /* Clear the mouse-moved flag for every frame on this display. */
8036 FOR_EACH_FRAME (tail, frame)
8037 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
8038 XFRAME (frame)->mouse_moved = 0;
8039
8040 last_mouse_scroll_bar = Qnil;
8041
8042 /* Figure out which root window we're on. */
8043 XQueryPointer (FRAME_X_DISPLAY (*fp),
8044 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
8045
8046 /* The root window which contains the pointer. */
8047 &root,
8048
8049 /* Trash which we can't trust if the pointer is on
8050 a different screen. */
8051 &dummy_window,
8052
8053 /* The position on that root window. */
8054 &root_x, &root_y,
8055
8056 /* More trash we can't trust. */
8057 &dummy, &dummy,
8058
8059 /* Modifier keys and pointer buttons, about which
8060 we don't care. */
8061 (unsigned int *) &dummy);
8062
8063 /* Now we have a position on the root; find the innermost window
8064 containing the pointer. */
8065 {
8066 Window win, child;
8067 int win_x, win_y;
8068 int parent_x = 0, parent_y = 0;
8069 int count;
8070
8071 win = root;
8072
8073 /* XTranslateCoordinates can get errors if the window
8074 structure is changing at the same time this function
8075 is running. So at least we must not crash from them. */
8076
8077 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
8078
8079 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
8080 && FRAME_LIVE_P (last_mouse_frame))
8081 {
8082 /* If mouse was grabbed on a frame, give coords for that frame
8083 even if the mouse is now outside it. */
8084 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
8085
8086 /* From-window, to-window. */
8087 root, FRAME_X_WINDOW (last_mouse_frame),
8088
8089 /* From-position, to-position. */
8090 root_x, root_y, &win_x, &win_y,
8091
8092 /* Child of win. */
8093 &child);
8094 f1 = last_mouse_frame;
8095 }
8096 else
8097 {
8098 while (1)
8099 {
8100 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
8101
8102 /* From-window, to-window. */
8103 root, win,
8104
8105 /* From-position, to-position. */
8106 root_x, root_y, &win_x, &win_y,
8107
8108 /* Child of win. */
8109 &child);
8110
8111 if (child == None || child == win)
8112 break;
8113
8114 win = child;
8115 parent_x = win_x;
8116 parent_y = win_y;
8117 }
8118
8119 /* Now we know that:
8120 win is the innermost window containing the pointer
8121 (XTC says it has no child containing the pointer),
8122 win_x and win_y are the pointer's position in it
8123 (XTC did this the last time through), and
8124 parent_x and parent_y are the pointer's position in win's parent.
8125 (They are what win_x and win_y were when win was child.
8126 If win is the root window, it has no parent, and
8127 parent_{x,y} are invalid, but that's okay, because we'll
8128 never use them in that case.) */
8129
8130 /* Is win one of our frames? */
8131 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
8132
8133 #ifdef USE_X_TOOLKIT
8134 /* If we end up with the menu bar window, say it's not
8135 on the frame. */
8136 if (f1 != NULL
8137 && f1->output_data.x->menubar_widget
8138 && win == XtWindow (f1->output_data.x->menubar_widget))
8139 f1 = NULL;
8140 #endif /* USE_X_TOOLKIT */
8141 }
8142
8143 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
8144 f1 = 0;
8145
8146 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
8147
8148 /* If not, is it one of our scroll bars? */
8149 if (! f1)
8150 {
8151 struct scroll_bar *bar = x_window_to_scroll_bar (win);
8152
8153 if (bar)
8154 {
8155 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8156 win_x = parent_x;
8157 win_y = parent_y;
8158 }
8159 }
8160
8161 if (f1 == 0 && insist > 0)
8162 f1 = SELECTED_FRAME ();
8163
8164 if (f1)
8165 {
8166 /* Ok, we found a frame. Store all the values.
8167 last_mouse_glyph is a rectangle used to reduce the
8168 generation of mouse events. To not miss any motion
8169 events, we must divide the frame into rectangles of the
8170 size of the smallest character that could be displayed
8171 on it, i.e. into the same rectangles that matrices on
8172 the frame are divided into. */
8173
8174 int width, height, gx, gy;
8175 XRectangle rect;
8176
8177 if (glyph_rect (f1, win_x, win_y, &rect))
8178 last_mouse_glyph = rect;
8179 else
8180 {
8181 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
8182 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
8183 gx = win_x;
8184 gy = win_y;
8185
8186 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
8187 round down even for negative values. */
8188 if (gx < 0)
8189 gx -= width - 1;
8190 if (gy < 0)
8191 gy -= height - 1;
8192 gx = (gx + width - 1) / width * width;
8193 gy = (gy + height - 1) / height * height;
8194
8195 last_mouse_glyph.width = width;
8196 last_mouse_glyph.height = height;
8197 last_mouse_glyph.x = gx;
8198 last_mouse_glyph.y = gy;
8199 }
8200
8201 *bar_window = Qnil;
8202 *part = 0;
8203 *fp = f1;
8204 XSETINT (*x, win_x);
8205 XSETINT (*y, win_y);
8206 *time = last_mouse_movement_time;
8207 }
8208 }
8209 }
8210
8211 UNBLOCK_INPUT;
8212 }
8213
8214
8215 #ifdef USE_X_TOOLKIT
8216
8217 /* Atimer callback function for TIMER. Called every 0.1s to process
8218 Xt timeouts, if needed. We must avoid calling XtAppPending as
8219 much as possible because that function does an implicit XFlush
8220 that slows us down. */
8221
8222 static void
8223 x_process_timeouts (timer)
8224 struct atimer *timer;
8225 {
8226 if (toolkit_scroll_bar_interaction || popup_activated_flag)
8227 {
8228 BLOCK_INPUT;
8229 while (XtAppPending (Xt_app_con) & XtIMTimer)
8230 XtAppProcessEvent (Xt_app_con, XtIMTimer);
8231 UNBLOCK_INPUT;
8232 }
8233 }
8234
8235 #endif /* USE_X_TOOLKIT */
8236
8237 \f
8238 /* Scroll bar support. */
8239
8240 /* Given an X window ID, find the struct scroll_bar which manages it.
8241 This can be called in GC, so we have to make sure to strip off mark
8242 bits. */
8243
8244 static struct scroll_bar *
8245 x_window_to_scroll_bar (window_id)
8246 Window window_id;
8247 {
8248 Lisp_Object tail;
8249
8250 for (tail = Vframe_list;
8251 XGCTYPE (tail) == Lisp_Cons;
8252 tail = XCDR (tail))
8253 {
8254 Lisp_Object frame, bar, condemned;
8255
8256 frame = XCAR (tail);
8257 /* All elements of Vframe_list should be frames. */
8258 if (! GC_FRAMEP (frame))
8259 abort ();
8260
8261 /* Scan this frame's scroll bar list for a scroll bar with the
8262 right window ID. */
8263 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
8264 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
8265 /* This trick allows us to search both the ordinary and
8266 condemned scroll bar lists with one loop. */
8267 ! GC_NILP (bar) || (bar = condemned,
8268 condemned = Qnil,
8269 ! GC_NILP (bar));
8270 bar = XSCROLL_BAR (bar)->next)
8271 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
8272 return XSCROLL_BAR (bar);
8273 }
8274
8275 return 0;
8276 }
8277
8278
8279 #if defined USE_LUCID
8280
8281 /* Return the Lucid menu bar WINDOW is part of. Return null
8282 if WINDOW is not part of a menu bar. */
8283
8284 static Widget
8285 x_window_to_menu_bar (window)
8286 Window window;
8287 {
8288 Lisp_Object tail;
8289
8290 for (tail = Vframe_list;
8291 XGCTYPE (tail) == Lisp_Cons;
8292 tail = XCDR (tail))
8293 {
8294 Lisp_Object frame = XCAR (tail);
8295 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
8296
8297 if (menu_bar && xlwmenu_window_p (menu_bar, window))
8298 return menu_bar;
8299 }
8300
8301 return NULL;
8302 }
8303
8304 #endif /* USE_LUCID */
8305
8306 \f
8307 /************************************************************************
8308 Toolkit scroll bars
8309 ************************************************************************/
8310
8311 #ifdef USE_TOOLKIT_SCROLL_BARS
8312
8313 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
8314 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
8315 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
8316 struct scroll_bar *));
8317 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
8318 int, int, int));
8319
8320
8321 /* Id of action hook installed for scroll bars. */
8322
8323 static XtActionHookId action_hook_id;
8324
8325 /* Lisp window being scrolled. Set when starting to interact with
8326 a toolkit scroll bar, reset to nil when ending the interaction. */
8327
8328 static Lisp_Object window_being_scrolled;
8329
8330 /* Last scroll bar part sent in xm_scroll_callback. */
8331
8332 static int last_scroll_bar_part;
8333
8334 /* Whether this is an Xaw with arrow-scrollbars. This should imply
8335 that movements of 1/20 of the screen size are mapped to up/down. */
8336
8337 static Boolean xaw3d_arrow_scroll;
8338
8339 /* Whether the drag scrolling maintains the mouse at the top of the
8340 thumb. If not, resizing the thumb needs to be done more carefully
8341 to avoid jerkyness. */
8342
8343 static Boolean xaw3d_pick_top;
8344
8345
8346 /* Action hook installed via XtAppAddActionHook when toolkit scroll
8347 bars are used.. The hook is responsible for detecting when
8348 the user ends an interaction with the scroll bar, and generates
8349 a `end-scroll' scroll_bar_click' event if so. */
8350
8351 static void
8352 xt_action_hook (widget, client_data, action_name, event, params,
8353 num_params)
8354 Widget widget;
8355 XtPointer client_data;
8356 String action_name;
8357 XEvent *event;
8358 String *params;
8359 Cardinal *num_params;
8360 {
8361 int scroll_bar_p;
8362 char *end_action;
8363
8364 #ifdef USE_MOTIF
8365 scroll_bar_p = XmIsScrollBar (widget);
8366 end_action = "Release";
8367 #else /* !USE_MOTIF i.e. use Xaw */
8368 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
8369 end_action = "EndScroll";
8370 #endif /* USE_MOTIF */
8371
8372 if (scroll_bar_p
8373 && strcmp (action_name, end_action) == 0
8374 && WINDOWP (window_being_scrolled))
8375 {
8376 struct window *w;
8377
8378 x_send_scroll_bar_event (window_being_scrolled,
8379 scroll_bar_end_scroll, 0, 0);
8380 w = XWINDOW (window_being_scrolled);
8381 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
8382 window_being_scrolled = Qnil;
8383 last_scroll_bar_part = -1;
8384
8385 /* Xt timeouts no longer needed. */
8386 toolkit_scroll_bar_interaction = 0;
8387 }
8388 }
8389
8390 /* A vector of windows used for communication between
8391 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
8392
8393 static struct window **scroll_bar_windows;
8394 static int scroll_bar_windows_size;
8395
8396
8397 /* Send a client message with message type Xatom_Scrollbar for a
8398 scroll action to the frame of WINDOW. PART is a value identifying
8399 the part of the scroll bar that was clicked on. PORTION is the
8400 amount to scroll of a whole of WHOLE. */
8401
8402 static void
8403 x_send_scroll_bar_event (window, part, portion, whole)
8404 Lisp_Object window;
8405 int part, portion, whole;
8406 {
8407 XEvent event;
8408 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
8409 struct window *w = XWINDOW (window);
8410 struct frame *f = XFRAME (w->frame);
8411 int i;
8412
8413 BLOCK_INPUT;
8414
8415 /* Construct a ClientMessage event to send to the frame. */
8416 ev->type = ClientMessage;
8417 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
8418 ev->display = FRAME_X_DISPLAY (f);
8419 ev->window = FRAME_X_WINDOW (f);
8420 ev->format = 32;
8421
8422 /* We can only transfer 32 bits in the XClientMessageEvent, which is
8423 not enough to store a pointer or Lisp_Object on a 64 bit system.
8424 So, store the window in scroll_bar_windows and pass the index
8425 into that array in the event. */
8426 for (i = 0; i < scroll_bar_windows_size; ++i)
8427 if (scroll_bar_windows[i] == NULL)
8428 break;
8429
8430 if (i == scroll_bar_windows_size)
8431 {
8432 int new_size = max (10, 2 * scroll_bar_windows_size);
8433 size_t nbytes = new_size * sizeof *scroll_bar_windows;
8434 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
8435
8436 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
8437 nbytes);
8438 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
8439 scroll_bar_windows_size = new_size;
8440 }
8441
8442 scroll_bar_windows[i] = w;
8443 ev->data.l[0] = (long) i;
8444 ev->data.l[1] = (long) part;
8445 ev->data.l[2] = (long) 0;
8446 ev->data.l[3] = (long) portion;
8447 ev->data.l[4] = (long) whole;
8448
8449 /* Make Xt timeouts work while the scroll bar is active. */
8450 toolkit_scroll_bar_interaction = 1;
8451
8452 /* Setting the event mask to zero means that the message will
8453 be sent to the client that created the window, and if that
8454 window no longer exists, no event will be sent. */
8455 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
8456 UNBLOCK_INPUT;
8457 }
8458
8459
8460 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
8461 in *IEVENT. */
8462
8463 static void
8464 x_scroll_bar_to_input_event (event, ievent)
8465 XEvent *event;
8466 struct input_event *ievent;
8467 {
8468 XClientMessageEvent *ev = (XClientMessageEvent *) event;
8469 Lisp_Object window;
8470 struct frame *f;
8471 struct window *w;
8472
8473 w = scroll_bar_windows[ev->data.l[0]];
8474 scroll_bar_windows[ev->data.l[0]] = NULL;
8475
8476 XSETWINDOW (window, w);
8477 f = XFRAME (w->frame);
8478
8479 ievent->kind = scroll_bar_click;
8480 ievent->frame_or_window = window;
8481 ievent->arg = Qnil;
8482 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
8483 ievent->part = ev->data.l[1];
8484 ievent->code = ev->data.l[2];
8485 ievent->x = make_number ((int) ev->data.l[3]);
8486 ievent->y = make_number ((int) ev->data.l[4]);
8487 ievent->modifiers = 0;
8488 }
8489
8490
8491 #ifdef USE_MOTIF
8492
8493 /* Minimum and maximum values used for Motif scroll bars. */
8494
8495 #define XM_SB_MIN 1
8496 #define XM_SB_MAX 10000000
8497 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
8498
8499
8500 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
8501 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
8502 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
8503
8504 static void
8505 xm_scroll_callback (widget, client_data, call_data)
8506 Widget widget;
8507 XtPointer client_data, call_data;
8508 {
8509 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8510 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
8511 int part = -1, whole = 0, portion = 0;
8512
8513 switch (cs->reason)
8514 {
8515 case XmCR_DECREMENT:
8516 bar->dragging = Qnil;
8517 part = scroll_bar_up_arrow;
8518 break;
8519
8520 case XmCR_INCREMENT:
8521 bar->dragging = Qnil;
8522 part = scroll_bar_down_arrow;
8523 break;
8524
8525 case XmCR_PAGE_DECREMENT:
8526 bar->dragging = Qnil;
8527 part = scroll_bar_above_handle;
8528 break;
8529
8530 case XmCR_PAGE_INCREMENT:
8531 bar->dragging = Qnil;
8532 part = scroll_bar_below_handle;
8533 break;
8534
8535 case XmCR_TO_TOP:
8536 bar->dragging = Qnil;
8537 part = scroll_bar_to_top;
8538 break;
8539
8540 case XmCR_TO_BOTTOM:
8541 bar->dragging = Qnil;
8542 part = scroll_bar_to_bottom;
8543 break;
8544
8545 case XmCR_DRAG:
8546 {
8547 int slider_size;
8548 int dragging_down_p = (INTEGERP (bar->dragging)
8549 && XINT (bar->dragging) <= cs->value);
8550
8551 /* Get the slider size. */
8552 BLOCK_INPUT;
8553 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
8554 UNBLOCK_INPUT;
8555
8556 /* At the max position of the scroll bar, do a line-wise
8557 movement. Without doing anything, we would be called with
8558 the same cs->value again and again. If we want to make
8559 sure that we can reach the end of the buffer, we have to do
8560 something.
8561
8562 Implementation note: setting bar->dragging always to
8563 cs->value gives a smoother movement at the max position.
8564 Setting it to nil when doing line-wise movement gives
8565 a better slider behavior. */
8566
8567 if (cs->value + slider_size == XM_SB_MAX
8568 || (dragging_down_p
8569 && last_scroll_bar_part == scroll_bar_down_arrow))
8570 {
8571 part = scroll_bar_down_arrow;
8572 bar->dragging = Qnil;
8573 }
8574 else
8575 {
8576 whole = XM_SB_RANGE;
8577 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
8578 part = scroll_bar_handle;
8579 bar->dragging = make_number (cs->value);
8580 }
8581 }
8582 break;
8583
8584 case XmCR_VALUE_CHANGED:
8585 break;
8586 };
8587
8588 if (part >= 0)
8589 {
8590 window_being_scrolled = bar->window;
8591 last_scroll_bar_part = part;
8592 x_send_scroll_bar_event (bar->window, part, portion, whole);
8593 }
8594 }
8595
8596
8597 #else /* !USE_MOTIF, i.e. Xaw. */
8598
8599
8600 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
8601 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8602 scroll bar struct. CALL_DATA is a pointer to a float saying where
8603 the thumb is. */
8604
8605 static void
8606 xaw_jump_callback (widget, client_data, call_data)
8607 Widget widget;
8608 XtPointer client_data, call_data;
8609 {
8610 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8611 float top = *(float *) call_data;
8612 float shown;
8613 int whole, portion, height;
8614 int part;
8615
8616 /* Get the size of the thumb, a value between 0 and 1. */
8617 BLOCK_INPUT;
8618 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
8619 UNBLOCK_INPUT;
8620
8621 whole = 10000000;
8622 portion = shown < 1 ? top * whole : 0;
8623
8624 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8625 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8626 the bottom, so we force the scrolling whenever we see that we're
8627 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8628 we try to ensure that we always stay two pixels away from the
8629 bottom). */
8630 part = scroll_bar_down_arrow;
8631 else
8632 part = scroll_bar_handle;
8633
8634 window_being_scrolled = bar->window;
8635 bar->dragging = make_number (portion);
8636 last_scroll_bar_part = part;
8637 x_send_scroll_bar_event (bar->window, part, portion, whole);
8638 }
8639
8640
8641 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
8642 i.e. line or page up or down. WIDGET is the Xaw scroll bar
8643 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8644 the scroll bar. CALL_DATA is an integer specifying the action that
8645 has taken place. It's magnitude is in the range 0..height of the
8646 scroll bar. Negative values mean scroll towards buffer start.
8647 Values < height of scroll bar mean line-wise movement. */
8648
8649 static void
8650 xaw_scroll_callback (widget, client_data, call_data)
8651 Widget widget;
8652 XtPointer client_data, call_data;
8653 {
8654 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8655 int position = (int) call_data;
8656 Dimension height;
8657 int part;
8658
8659 /* Get the height of the scroll bar. */
8660 BLOCK_INPUT;
8661 XtVaGetValues (widget, XtNheight, &height, NULL);
8662 UNBLOCK_INPUT;
8663
8664 if (abs (position) >= height)
8665 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8666
8667 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8668 it maps line-movement to call_data = max(5, height/20). */
8669 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8670 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
8671 else
8672 part = scroll_bar_move_ratio;
8673
8674 window_being_scrolled = bar->window;
8675 bar->dragging = Qnil;
8676 last_scroll_bar_part = part;
8677 x_send_scroll_bar_event (bar->window, part, position, height);
8678 }
8679
8680
8681 #endif /* not USE_MOTIF */
8682
8683
8684 /* Create the widget for scroll bar BAR on frame F. Record the widget
8685 and X window of the scroll bar in BAR. */
8686
8687 static void
8688 x_create_toolkit_scroll_bar (f, bar)
8689 struct frame *f;
8690 struct scroll_bar *bar;
8691 {
8692 Window xwindow;
8693 Widget widget;
8694 Arg av[20];
8695 int ac = 0;
8696 char *scroll_bar_name = "verticalScrollBar";
8697 unsigned long pixel;
8698
8699 BLOCK_INPUT;
8700
8701 #ifdef USE_MOTIF
8702 /* Set resources. Create the widget. */
8703 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8704 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8705 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8706 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8707 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8708 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8709 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8710
8711 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8712 if (pixel != -1)
8713 {
8714 XtSetArg (av[ac], XmNforeground, pixel);
8715 ++ac;
8716 }
8717
8718 pixel = f->output_data.x->scroll_bar_background_pixel;
8719 if (pixel != -1)
8720 {
8721 XtSetArg (av[ac], XmNbackground, pixel);
8722 ++ac;
8723 }
8724
8725 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8726 scroll_bar_name, av, ac);
8727
8728 /* Add one callback for everything that can happen. */
8729 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8730 (XtPointer) bar);
8731 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8732 (XtPointer) bar);
8733 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8734 (XtPointer) bar);
8735 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8736 (XtPointer) bar);
8737 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8738 (XtPointer) bar);
8739 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8740 (XtPointer) bar);
8741 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8742 (XtPointer) bar);
8743
8744 /* Realize the widget. Only after that is the X window created. */
8745 XtRealizeWidget (widget);
8746
8747 /* Set the cursor to an arrow. I didn't find a resource to do that.
8748 And I'm wondering why it hasn't an arrow cursor by default. */
8749 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8750 f->output_data.x->nontext_cursor);
8751
8752 #else /* !USE_MOTIF i.e. use Xaw */
8753
8754 /* Set resources. Create the widget. The background of the
8755 Xaw3d scroll bar widget is a little bit light for my taste.
8756 We don't alter it here to let users change it according
8757 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8758 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8759 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
8760 /* For smoother scrolling with Xaw3d -sm */
8761 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8762
8763 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8764 if (pixel != -1)
8765 {
8766 XtSetArg (av[ac], XtNforeground, pixel);
8767 ++ac;
8768 }
8769
8770 pixel = f->output_data.x->scroll_bar_background_pixel;
8771 if (pixel != -1)
8772 {
8773 XtSetArg (av[ac], XtNbackground, pixel);
8774 ++ac;
8775 }
8776
8777 /* Top/bottom shadow colors. */
8778
8779 /* Allocate them, if necessary. */
8780 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
8781 {
8782 pixel = f->output_data.x->scroll_bar_background_pixel;
8783 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8784 &pixel, 1.2, 0x8000))
8785 pixel = -1;
8786 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
8787 }
8788 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8789 {
8790 pixel = f->output_data.x->scroll_bar_background_pixel;
8791 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8792 &pixel, 0.6, 0x4000))
8793 pixel = -1;
8794 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
8795 }
8796
8797 /* Tell the toolkit about them. */
8798 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
8799 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8800 /* We tried to allocate a color for the top/bottom shadow, and
8801 failed, so tell Xaw3d to use dithering instead. */
8802 {
8803 XtSetArg (av[ac], XtNbeNiceToColormap, True);
8804 ++ac;
8805 }
8806 else
8807 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
8808 be more consistent with other emacs 3d colors, and since Xaw3d is
8809 not good at dealing with allocation failure. */
8810 {
8811 /* This tells Xaw3d to use real colors instead of dithering for
8812 the shadows. */
8813 XtSetArg (av[ac], XtNbeNiceToColormap, False);
8814 ++ac;
8815
8816 /* Specify the colors. */
8817 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
8818 if (pixel != -1)
8819 {
8820 XtSetArg (av[ac], "topShadowPixel", pixel);
8821 ++ac;
8822 }
8823 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
8824 if (pixel != -1)
8825 {
8826 XtSetArg (av[ac], "bottomShadowPixel", pixel);
8827 ++ac;
8828 }
8829 }
8830
8831 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8832 f->output_data.x->edit_widget, av, ac);
8833
8834 {
8835 char *initial = "";
8836 char *val = initial;
8837 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8838 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8839 if (val == initial)
8840 { /* ARROW_SCROLL */
8841 xaw3d_arrow_scroll = True;
8842 /* Isn't that just a personal preference ? -sm */
8843 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8844 }
8845 }
8846
8847 /* Define callbacks. */
8848 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8849 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
8850 (XtPointer) bar);
8851
8852 /* Realize the widget. Only after that is the X window created. */
8853 XtRealizeWidget (widget);
8854
8855 #endif /* !USE_MOTIF */
8856
8857 /* Install an action hook that let's us detect when the user
8858 finishes interacting with a scroll bar. */
8859 if (action_hook_id == 0)
8860 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8861
8862 /* Remember X window and widget in the scroll bar vector. */
8863 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8864 xwindow = XtWindow (widget);
8865 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8866
8867 UNBLOCK_INPUT;
8868 }
8869
8870
8871 /* Set the thumb size and position of scroll bar BAR. We are currently
8872 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8873
8874 static void
8875 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8876 struct scroll_bar *bar;
8877 int portion, position, whole;
8878 {
8879 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8880 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8881 float top, shown;
8882
8883 if (whole == 0)
8884 top = 0, shown = 1;
8885 else
8886 {
8887 top = (float) position / whole;
8888 shown = (float) portion / whole;
8889 }
8890
8891 BLOCK_INPUT;
8892
8893 #ifdef USE_MOTIF
8894 {
8895 int size, value;
8896
8897 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
8898 is the scroll bar's maximum and MIN is the scroll bar's minimum
8899 value. */
8900 size = shown * XM_SB_RANGE;
8901 size = min (size, XM_SB_RANGE);
8902 size = max (size, 1);
8903
8904 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8905 value = top * XM_SB_RANGE;
8906 value = min (value, XM_SB_MAX - size);
8907 value = max (value, XM_SB_MIN);
8908
8909 if (NILP (bar->dragging))
8910 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8911 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8912 /* This has the negative side effect that the slider value is
8913 not what it would be if we scrolled here using line-wise or
8914 page-wise movement. */
8915 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8916 else
8917 {
8918 /* If currently dragging, only update the slider size.
8919 This reduces flicker effects. */
8920 int old_value, old_size, increment, page_increment;
8921
8922 XmScrollBarGetValues (widget, &old_value, &old_size,
8923 &increment, &page_increment);
8924 XmScrollBarSetValues (widget, old_value,
8925 min (size, XM_SB_RANGE - old_value),
8926 0, 0, False);
8927 }
8928 }
8929 #else /* !USE_MOTIF i.e. use Xaw */
8930 {
8931 float old_top, old_shown;
8932 Dimension height;
8933 XtVaGetValues (widget,
8934 XtNtopOfThumb, &old_top,
8935 XtNshown, &old_shown,
8936 XtNheight, &height,
8937 NULL);
8938
8939 /* Massage the top+shown values. */
8940 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8941 top = max (0, min (1, top));
8942 else
8943 top = old_top;
8944 /* Keep two pixels available for moving the thumb down. */
8945 shown = max (0, min (1 - top - (2.0 / height), shown));
8946
8947 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8948 check that your system's configuration file contains a define
8949 for `NARROWPROTO'. See s/freebsd.h for an example. */
8950 if (top != old_top || shown != old_shown)
8951 {
8952 if (NILP (bar->dragging))
8953 XawScrollbarSetThumb (widget, top, shown);
8954 else
8955 {
8956 #ifdef HAVE_XAW3D
8957 ScrollbarWidget sb = (ScrollbarWidget) widget;
8958 int scroll_mode = 0;
8959
8960 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8961 if (xaw3d_arrow_scroll)
8962 {
8963 /* Xaw3d stupidly ignores resize requests while dragging
8964 so we have to make it believe it's not in dragging mode. */
8965 scroll_mode = sb->scrollbar.scroll_mode;
8966 if (scroll_mode == 2)
8967 sb->scrollbar.scroll_mode = 0;
8968 }
8969 #endif
8970 /* Try to make the scrolling a tad smoother. */
8971 if (!xaw3d_pick_top)
8972 shown = min (shown, old_shown);
8973
8974 XawScrollbarSetThumb (widget, top, shown);
8975
8976 #ifdef HAVE_XAW3D
8977 if (xaw3d_arrow_scroll && scroll_mode == 2)
8978 sb->scrollbar.scroll_mode = scroll_mode;
8979 #endif
8980 }
8981 }
8982 }
8983 #endif /* !USE_MOTIF */
8984
8985 UNBLOCK_INPUT;
8986 }
8987
8988 #endif /* USE_TOOLKIT_SCROLL_BARS */
8989
8990
8991 \f
8992 /************************************************************************
8993 Scroll bars, general
8994 ************************************************************************/
8995
8996 /* Create a scroll bar and return the scroll bar vector for it. W is
8997 the Emacs window on which to create the scroll bar. TOP, LEFT,
8998 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
8999 scroll bar. */
9000
9001 static struct scroll_bar *
9002 x_scroll_bar_create (w, top, left, width, height)
9003 struct window *w;
9004 int top, left, width, height;
9005 {
9006 struct frame *f = XFRAME (w->frame);
9007 struct scroll_bar *bar
9008 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
9009
9010 BLOCK_INPUT;
9011
9012 #ifdef USE_TOOLKIT_SCROLL_BARS
9013 x_create_toolkit_scroll_bar (f, bar);
9014 #else /* not USE_TOOLKIT_SCROLL_BARS */
9015 {
9016 XSetWindowAttributes a;
9017 unsigned long mask;
9018 Window window;
9019
9020 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
9021 if (a.background_pixel == -1)
9022 a.background_pixel = f->output_data.x->background_pixel;
9023
9024 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9025 | ButtonMotionMask | PointerMotionHintMask
9026 | ExposureMask);
9027 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
9028
9029 mask = (CWBackPixel | CWEventMask | CWCursor);
9030
9031 /* Clear the area of W that will serve as a scroll bar. This is
9032 for the case that a window has been split horizontally. In
9033 this case, no clear_frame is generated to reduce flickering. */
9034 if (width > 0 && height > 0)
9035 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9036 left, top, width,
9037 window_box_height (w), False);
9038
9039 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9040 /* Position and size of scroll bar. */
9041 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9042 top,
9043 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9044 height,
9045 /* Border width, depth, class, and visual. */
9046 0,
9047 CopyFromParent,
9048 CopyFromParent,
9049 CopyFromParent,
9050 /* Attributes. */
9051 mask, &a);
9052 SET_SCROLL_BAR_X_WINDOW (bar, window);
9053 }
9054 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9055
9056 XSETWINDOW (bar->window, w);
9057 XSETINT (bar->top, top);
9058 XSETINT (bar->left, left);
9059 XSETINT (bar->width, width);
9060 XSETINT (bar->height, height);
9061 XSETINT (bar->start, 0);
9062 XSETINT (bar->end, 0);
9063 bar->dragging = Qnil;
9064
9065 /* Add bar to its frame's list of scroll bars. */
9066 bar->next = FRAME_SCROLL_BARS (f);
9067 bar->prev = Qnil;
9068 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9069 if (!NILP (bar->next))
9070 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9071
9072 /* Map the window/widget. */
9073 #ifdef USE_TOOLKIT_SCROLL_BARS
9074 {
9075 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
9076 XtConfigureWidget (scroll_bar,
9077 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9078 top,
9079 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9080 max (height, 1), 0);
9081 XtMapWidget (scroll_bar);
9082 }
9083 #else /* not USE_TOOLKIT_SCROLL_BARS */
9084 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9085 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9086
9087 UNBLOCK_INPUT;
9088 return bar;
9089 }
9090
9091
9092 /* Draw BAR's handle in the proper position.
9093
9094 If the handle is already drawn from START to END, don't bother
9095 redrawing it, unless REBUILD is non-zero; in that case, always
9096 redraw it. (REBUILD is handy for drawing the handle after expose
9097 events.)
9098
9099 Normally, we want to constrain the start and end of the handle to
9100 fit inside its rectangle, but if the user is dragging the scroll
9101 bar handle, we want to let them drag it down all the way, so that
9102 the bar's top is as far down as it goes; otherwise, there's no way
9103 to move to the very end of the buffer. */
9104
9105 #ifndef USE_TOOLKIT_SCROLL_BARS
9106
9107 static void
9108 x_scroll_bar_set_handle (bar, start, end, rebuild)
9109 struct scroll_bar *bar;
9110 int start, end;
9111 int rebuild;
9112 {
9113 int dragging = ! NILP (bar->dragging);
9114 Window w = SCROLL_BAR_X_WINDOW (bar);
9115 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9116 GC gc = f->output_data.x->normal_gc;
9117
9118 /* If the display is already accurate, do nothing. */
9119 if (! rebuild
9120 && start == XINT (bar->start)
9121 && end == XINT (bar->end))
9122 return;
9123
9124 BLOCK_INPUT;
9125
9126 {
9127 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
9128 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9129 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9130
9131 /* Make sure the values are reasonable, and try to preserve
9132 the distance between start and end. */
9133 {
9134 int length = end - start;
9135
9136 if (start < 0)
9137 start = 0;
9138 else if (start > top_range)
9139 start = top_range;
9140 end = start + length;
9141
9142 if (end < start)
9143 end = start;
9144 else if (end > top_range && ! dragging)
9145 end = top_range;
9146 }
9147
9148 /* Store the adjusted setting in the scroll bar. */
9149 XSETINT (bar->start, start);
9150 XSETINT (bar->end, end);
9151
9152 /* Clip the end position, just for display. */
9153 if (end > top_range)
9154 end = top_range;
9155
9156 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
9157 below top positions, to make sure the handle is always at least
9158 that many pixels tall. */
9159 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
9160
9161 /* Draw the empty space above the handle. Note that we can't clear
9162 zero-height areas; that means "clear to end of window." */
9163 if (0 < start)
9164 x_clear_area (FRAME_X_DISPLAY (f), w,
9165 /* x, y, width, height, and exposures. */
9166 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9167 VERTICAL_SCROLL_BAR_TOP_BORDER,
9168 inside_width, start,
9169 False);
9170
9171 /* Change to proper foreground color if one is specified. */
9172 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9173 XSetForeground (FRAME_X_DISPLAY (f), gc,
9174 f->output_data.x->scroll_bar_foreground_pixel);
9175
9176 /* Draw the handle itself. */
9177 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
9178 /* x, y, width, height */
9179 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9180 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
9181 inside_width, end - start);
9182
9183 /* Restore the foreground color of the GC if we changed it above. */
9184 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9185 XSetForeground (FRAME_X_DISPLAY (f), gc,
9186 f->output_data.x->foreground_pixel);
9187
9188 /* Draw the empty space below the handle. Note that we can't
9189 clear zero-height areas; that means "clear to end of window." */
9190 if (end < inside_height)
9191 x_clear_area (FRAME_X_DISPLAY (f), w,
9192 /* x, y, width, height, and exposures. */
9193 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9194 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
9195 inside_width, inside_height - end,
9196 False);
9197
9198 }
9199
9200 UNBLOCK_INPUT;
9201 }
9202
9203 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9204
9205 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
9206 nil. */
9207
9208 static void
9209 x_scroll_bar_remove (bar)
9210 struct scroll_bar *bar;
9211 {
9212 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9213 BLOCK_INPUT;
9214
9215 #ifdef USE_TOOLKIT_SCROLL_BARS
9216 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
9217 #else
9218 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9219 #endif
9220
9221 /* Disassociate this scroll bar from its window. */
9222 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
9223
9224 UNBLOCK_INPUT;
9225 }
9226
9227
9228 /* Set the handle of the vertical scroll bar for WINDOW to indicate
9229 that we are displaying PORTION characters out of a total of WHOLE
9230 characters, starting at POSITION. If WINDOW has no scroll bar,
9231 create one. */
9232
9233 static void
9234 XTset_vertical_scroll_bar (w, portion, whole, position)
9235 struct window *w;
9236 int portion, whole, position;
9237 {
9238 struct frame *f = XFRAME (w->frame);
9239 struct scroll_bar *bar;
9240 int top, height, left, sb_left, width, sb_width;
9241 int window_x, window_y, window_width, window_height;
9242
9243 /* Get window dimensions. */
9244 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
9245 top = window_y;
9246 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9247 height = window_height;
9248
9249 /* Compute the left edge of the scroll bar area. */
9250 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9251 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
9252 else
9253 left = XFASTINT (w->left);
9254 left *= CANON_X_UNIT (f);
9255 left += FRAME_INTERNAL_BORDER_WIDTH (f);
9256
9257 /* Compute the width of the scroll bar which might be less than
9258 the width of the area reserved for the scroll bar. */
9259 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
9260 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
9261 else
9262 sb_width = width;
9263
9264 /* Compute the left edge of the scroll bar. */
9265 #ifdef USE_TOOLKIT_SCROLL_BARS
9266 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9267 sb_left = left + width - sb_width - (width - sb_width) / 2;
9268 else
9269 sb_left = left + (width - sb_width) / 2;
9270 #else
9271 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9272 sb_left = left + width - sb_width;
9273 else
9274 sb_left = left;
9275 #endif
9276
9277 /* Does the scroll bar exist yet? */
9278 if (NILP (w->vertical_scroll_bar))
9279 {
9280 if (width > 0 && height > 0)
9281 {
9282 BLOCK_INPUT;
9283 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9284 left, top, width, height, False);
9285 UNBLOCK_INPUT;
9286 }
9287
9288 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
9289 }
9290 else
9291 {
9292 /* It may just need to be moved and resized. */
9293 unsigned int mask = 0;
9294
9295 bar = XSCROLL_BAR (w->vertical_scroll_bar);
9296
9297 BLOCK_INPUT;
9298
9299 if (sb_left != XINT (bar->left))
9300 mask |= CWX;
9301 if (top != XINT (bar->top))
9302 mask |= CWY;
9303 if (sb_width != XINT (bar->width))
9304 mask |= CWWidth;
9305 if (height != XINT (bar->height))
9306 mask |= CWHeight;
9307
9308 #ifdef USE_TOOLKIT_SCROLL_BARS
9309
9310 /* Since toolkit scroll bars are smaller than the space reserved
9311 for them on the frame, we have to clear "under" them. */
9312 if (width > 0 && height > 0)
9313 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9314 left, top, width, height, False);
9315
9316 /* Move/size the scroll bar widget. */
9317 if (mask)
9318 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
9319 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9320 top,
9321 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9322 max (height, 1), 0);
9323
9324 #else /* not USE_TOOLKIT_SCROLL_BARS */
9325
9326 /* Clear areas not covered by the scroll bar because of
9327 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
9328 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
9329 {
9330 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9331 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9332 height, False);
9333 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9334 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9335 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9336 height, False);
9337 }
9338
9339 /* Clear areas not covered by the scroll bar because it's not as
9340 wide as the area reserved for it. This makes sure a
9341 previous mode line display is cleared after C-x 2 C-x 1, for
9342 example. */
9343 {
9344 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9345 int rest = area_width - sb_width;
9346 if (rest > 0 && height > 0)
9347 {
9348 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
9349 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9350 left + area_width - rest, top,
9351 rest, height, False);
9352 else
9353 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9354 left, top, rest, height, False);
9355 }
9356 }
9357
9358 /* Move/size the scroll bar window. */
9359 if (mask)
9360 {
9361 XWindowChanges wc;
9362
9363 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9364 wc.y = top;
9365 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
9366 wc.height = height;
9367 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
9368 mask, &wc);
9369 }
9370
9371 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9372
9373 /* Remember new settings. */
9374 XSETINT (bar->left, sb_left);
9375 XSETINT (bar->top, top);
9376 XSETINT (bar->width, sb_width);
9377 XSETINT (bar->height, height);
9378
9379 UNBLOCK_INPUT;
9380 }
9381
9382 #ifdef USE_TOOLKIT_SCROLL_BARS
9383 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
9384 #else /* not USE_TOOLKIT_SCROLL_BARS */
9385 /* Set the scroll bar's current state, unless we're currently being
9386 dragged. */
9387 if (NILP (bar->dragging))
9388 {
9389 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
9390
9391 if (whole == 0)
9392 x_scroll_bar_set_handle (bar, 0, top_range, 0);
9393 else
9394 {
9395 int start = ((double) position * top_range) / whole;
9396 int end = ((double) (position + portion) * top_range) / whole;
9397 x_scroll_bar_set_handle (bar, start, end, 0);
9398 }
9399 }
9400 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9401
9402 XSETVECTOR (w->vertical_scroll_bar, bar);
9403 }
9404
9405
9406 /* The following three hooks are used when we're doing a thorough
9407 redisplay of the frame. We don't explicitly know which scroll bars
9408 are going to be deleted, because keeping track of when windows go
9409 away is a real pain - "Can you say set-window-configuration, boys
9410 and girls?" Instead, we just assert at the beginning of redisplay
9411 that *all* scroll bars are to be removed, and then save a scroll bar
9412 from the fiery pit when we actually redisplay its window. */
9413
9414 /* Arrange for all scroll bars on FRAME to be removed at the next call
9415 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
9416 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
9417
9418 static void
9419 XTcondemn_scroll_bars (frame)
9420 FRAME_PTR frame;
9421 {
9422 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
9423 while (! NILP (FRAME_SCROLL_BARS (frame)))
9424 {
9425 Lisp_Object bar;
9426 bar = FRAME_SCROLL_BARS (frame);
9427 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
9428 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
9429 XSCROLL_BAR (bar)->prev = Qnil;
9430 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
9431 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
9432 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
9433 }
9434 }
9435
9436
9437 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
9438 Note that WINDOW isn't necessarily condemned at all. */
9439
9440 static void
9441 XTredeem_scroll_bar (window)
9442 struct window *window;
9443 {
9444 struct scroll_bar *bar;
9445 struct frame *f;
9446
9447 /* We can't redeem this window's scroll bar if it doesn't have one. */
9448 if (NILP (window->vertical_scroll_bar))
9449 abort ();
9450
9451 bar = XSCROLL_BAR (window->vertical_scroll_bar);
9452
9453 /* Unlink it from the condemned list. */
9454 f = XFRAME (WINDOW_FRAME (window));
9455 if (NILP (bar->prev))
9456 {
9457 /* If the prev pointer is nil, it must be the first in one of
9458 the lists. */
9459 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
9460 /* It's not condemned. Everything's fine. */
9461 return;
9462 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
9463 window->vertical_scroll_bar))
9464 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
9465 else
9466 /* If its prev pointer is nil, it must be at the front of
9467 one or the other! */
9468 abort ();
9469 }
9470 else
9471 XSCROLL_BAR (bar->prev)->next = bar->next;
9472
9473 if (! NILP (bar->next))
9474 XSCROLL_BAR (bar->next)->prev = bar->prev;
9475
9476 bar->next = FRAME_SCROLL_BARS (f);
9477 bar->prev = Qnil;
9478 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9479 if (! NILP (bar->next))
9480 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9481 }
9482
9483 /* Remove all scroll bars on FRAME that haven't been saved since the
9484 last call to `*condemn_scroll_bars_hook'. */
9485
9486 static void
9487 XTjudge_scroll_bars (f)
9488 FRAME_PTR f;
9489 {
9490 Lisp_Object bar, next;
9491
9492 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
9493
9494 /* Clear out the condemned list now so we won't try to process any
9495 more events on the hapless scroll bars. */
9496 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
9497
9498 for (; ! NILP (bar); bar = next)
9499 {
9500 struct scroll_bar *b = XSCROLL_BAR (bar);
9501
9502 x_scroll_bar_remove (b);
9503
9504 next = b->next;
9505 b->next = b->prev = Qnil;
9506 }
9507
9508 /* Now there should be no references to the condemned scroll bars,
9509 and they should get garbage-collected. */
9510 }
9511
9512
9513 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
9514 is a no-op when using toolkit scroll bars.
9515
9516 This may be called from a signal handler, so we have to ignore GC
9517 mark bits. */
9518
9519 static void
9520 x_scroll_bar_expose (bar, event)
9521 struct scroll_bar *bar;
9522 XEvent *event;
9523 {
9524 #ifndef USE_TOOLKIT_SCROLL_BARS
9525
9526 Window w = SCROLL_BAR_X_WINDOW (bar);
9527 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9528 GC gc = f->output_data.x->normal_gc;
9529 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9530
9531 BLOCK_INPUT;
9532
9533 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
9534
9535 /* Draw a one-pixel border just inside the edges of the scroll bar. */
9536 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
9537
9538 /* x, y, width, height */
9539 0, 0,
9540 XINT (bar->width) - 1 - width_trim - width_trim,
9541 XINT (bar->height) - 1);
9542
9543 UNBLOCK_INPUT;
9544
9545 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9546 }
9547
9548 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
9549 is set to something other than no_event, it is enqueued.
9550
9551 This may be called from a signal handler, so we have to ignore GC
9552 mark bits. */
9553
9554 #ifndef USE_TOOLKIT_SCROLL_BARS
9555
9556 static void
9557 x_scroll_bar_handle_click (bar, event, emacs_event)
9558 struct scroll_bar *bar;
9559 XEvent *event;
9560 struct input_event *emacs_event;
9561 {
9562 if (! GC_WINDOWP (bar->window))
9563 abort ();
9564
9565 emacs_event->kind = scroll_bar_click;
9566 emacs_event->code = event->xbutton.button - Button1;
9567 emacs_event->modifiers
9568 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
9569 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
9570 event->xbutton.state)
9571 | (event->type == ButtonRelease
9572 ? up_modifier
9573 : down_modifier));
9574 emacs_event->frame_or_window = bar->window;
9575 emacs_event->arg = Qnil;
9576 emacs_event->timestamp = event->xbutton.time;
9577 {
9578 #if 0
9579 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9580 int internal_height
9581 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9582 #endif
9583 int top_range
9584 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9585 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
9586
9587 if (y < 0) y = 0;
9588 if (y > top_range) y = top_range;
9589
9590 if (y < XINT (bar->start))
9591 emacs_event->part = scroll_bar_above_handle;
9592 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9593 emacs_event->part = scroll_bar_handle;
9594 else
9595 emacs_event->part = scroll_bar_below_handle;
9596
9597 /* Just because the user has clicked on the handle doesn't mean
9598 they want to drag it. Lisp code needs to be able to decide
9599 whether or not we're dragging. */
9600 #if 0
9601 /* If the user has just clicked on the handle, record where they're
9602 holding it. */
9603 if (event->type == ButtonPress
9604 && emacs_event->part == scroll_bar_handle)
9605 XSETINT (bar->dragging, y - XINT (bar->start));
9606 #endif
9607
9608 /* If the user has released the handle, set it to its final position. */
9609 if (event->type == ButtonRelease
9610 && ! NILP (bar->dragging))
9611 {
9612 int new_start = y - XINT (bar->dragging);
9613 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9614
9615 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9616 bar->dragging = Qnil;
9617 }
9618
9619 /* Same deal here as the other #if 0. */
9620 #if 0
9621 /* Clicks on the handle are always reported as occurring at the top of
9622 the handle. */
9623 if (emacs_event->part == scroll_bar_handle)
9624 emacs_event->x = bar->start;
9625 else
9626 XSETINT (emacs_event->x, y);
9627 #else
9628 XSETINT (emacs_event->x, y);
9629 #endif
9630
9631 XSETINT (emacs_event->y, top_range);
9632 }
9633 }
9634
9635 /* Handle some mouse motion while someone is dragging the scroll bar.
9636
9637 This may be called from a signal handler, so we have to ignore GC
9638 mark bits. */
9639
9640 static void
9641 x_scroll_bar_note_movement (bar, event)
9642 struct scroll_bar *bar;
9643 XEvent *event;
9644 {
9645 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9646
9647 last_mouse_movement_time = event->xmotion.time;
9648
9649 f->mouse_moved = 1;
9650 XSETVECTOR (last_mouse_scroll_bar, bar);
9651
9652 /* If we're dragging the bar, display it. */
9653 if (! GC_NILP (bar->dragging))
9654 {
9655 /* Where should the handle be now? */
9656 int new_start = event->xmotion.y - XINT (bar->dragging);
9657
9658 if (new_start != XINT (bar->start))
9659 {
9660 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9661
9662 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9663 }
9664 }
9665 }
9666
9667 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9668
9669 /* Return information to the user about the current position of the mouse
9670 on the scroll bar. */
9671
9672 static void
9673 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9674 FRAME_PTR *fp;
9675 Lisp_Object *bar_window;
9676 enum scroll_bar_part *part;
9677 Lisp_Object *x, *y;
9678 unsigned long *time;
9679 {
9680 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
9681 Window w = SCROLL_BAR_X_WINDOW (bar);
9682 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9683 int win_x, win_y;
9684 Window dummy_window;
9685 int dummy_coord;
9686 unsigned int dummy_mask;
9687
9688 BLOCK_INPUT;
9689
9690 /* Get the mouse's position relative to the scroll bar window, and
9691 report that. */
9692 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
9693
9694 /* Root, child, root x and root y. */
9695 &dummy_window, &dummy_window,
9696 &dummy_coord, &dummy_coord,
9697
9698 /* Position relative to scroll bar. */
9699 &win_x, &win_y,
9700
9701 /* Mouse buttons and modifier keys. */
9702 &dummy_mask))
9703 ;
9704 else
9705 {
9706 #if 0
9707 int inside_height
9708 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9709 #endif
9710 int top_range
9711 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9712
9713 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9714
9715 if (! NILP (bar->dragging))
9716 win_y -= XINT (bar->dragging);
9717
9718 if (win_y < 0)
9719 win_y = 0;
9720 if (win_y > top_range)
9721 win_y = top_range;
9722
9723 *fp = f;
9724 *bar_window = bar->window;
9725
9726 if (! NILP (bar->dragging))
9727 *part = scroll_bar_handle;
9728 else if (win_y < XINT (bar->start))
9729 *part = scroll_bar_above_handle;
9730 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9731 *part = scroll_bar_handle;
9732 else
9733 *part = scroll_bar_below_handle;
9734
9735 XSETINT (*x, win_y);
9736 XSETINT (*y, top_range);
9737
9738 f->mouse_moved = 0;
9739 last_mouse_scroll_bar = Qnil;
9740 }
9741
9742 *time = last_mouse_movement_time;
9743
9744 UNBLOCK_INPUT;
9745 }
9746
9747
9748 /* The screen has been cleared so we may have changed foreground or
9749 background colors, and the scroll bars may need to be redrawn.
9750 Clear out the scroll bars, and ask for expose events, so we can
9751 redraw them. */
9752
9753 void
9754 x_scroll_bar_clear (f)
9755 FRAME_PTR f;
9756 {
9757 #ifndef USE_TOOLKIT_SCROLL_BARS
9758 Lisp_Object bar;
9759
9760 /* We can have scroll bars even if this is 0,
9761 if we just turned off scroll bar mode.
9762 But in that case we should not clear them. */
9763 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9764 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9765 bar = XSCROLL_BAR (bar)->next)
9766 XClearArea (FRAME_X_DISPLAY (f),
9767 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9768 0, 0, 0, 0, True);
9769 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9770 }
9771
9772 /* This processes Expose events from the menu-bar specific X event
9773 loop in xmenu.c. This allows to redisplay the frame if necessary
9774 when handling menu-bar or pop-up items. */
9775
9776 int
9777 process_expose_from_menu (event)
9778 XEvent event;
9779 {
9780 FRAME_PTR f;
9781 struct x_display_info *dpyinfo;
9782 int frame_exposed_p = 0;
9783
9784 BLOCK_INPUT;
9785
9786 dpyinfo = x_display_info_for_display (event.xexpose.display);
9787 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9788 if (f)
9789 {
9790 if (f->async_visible == 0)
9791 {
9792 f->async_visible = 1;
9793 f->async_iconified = 0;
9794 f->output_data.x->has_been_visible = 1;
9795 SET_FRAME_GARBAGED (f);
9796 }
9797 else
9798 {
9799 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9800 event.xexpose.x, event.xexpose.y,
9801 event.xexpose.width, event.xexpose.height);
9802 frame_exposed_p = 1;
9803 }
9804 }
9805 else
9806 {
9807 struct scroll_bar *bar
9808 = x_window_to_scroll_bar (event.xexpose.window);
9809
9810 if (bar)
9811 x_scroll_bar_expose (bar, &event);
9812 }
9813
9814 UNBLOCK_INPUT;
9815 return frame_exposed_p;
9816 }
9817 \f
9818 /* Define a queue to save up SelectionRequest events for later handling. */
9819
9820 struct selection_event_queue
9821 {
9822 XEvent event;
9823 struct selection_event_queue *next;
9824 };
9825
9826 static struct selection_event_queue *queue;
9827
9828 /* Nonzero means queue up certain events--don't process them yet. */
9829
9830 static int x_queue_selection_requests;
9831
9832 /* Queue up an X event *EVENT, to be processed later. */
9833
9834 static void
9835 x_queue_event (f, event)
9836 FRAME_PTR f;
9837 XEvent *event;
9838 {
9839 struct selection_event_queue *queue_tmp
9840 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
9841
9842 if (queue_tmp != NULL)
9843 {
9844 queue_tmp->event = *event;
9845 queue_tmp->next = queue;
9846 queue = queue_tmp;
9847 }
9848 }
9849
9850 /* Take all the queued events and put them back
9851 so that they get processed afresh. */
9852
9853 static void
9854 x_unqueue_events (display)
9855 Display *display;
9856 {
9857 while (queue != NULL)
9858 {
9859 struct selection_event_queue *queue_tmp = queue;
9860 XPutBackEvent (display, &queue_tmp->event);
9861 queue = queue_tmp->next;
9862 xfree ((char *)queue_tmp);
9863 }
9864 }
9865
9866 /* Start queuing SelectionRequest events. */
9867
9868 void
9869 x_start_queuing_selection_requests (display)
9870 Display *display;
9871 {
9872 x_queue_selection_requests++;
9873 }
9874
9875 /* Stop queuing SelectionRequest events. */
9876
9877 void
9878 x_stop_queuing_selection_requests (display)
9879 Display *display;
9880 {
9881 x_queue_selection_requests--;
9882 x_unqueue_events (display);
9883 }
9884 \f
9885 /* The main X event-reading loop - XTread_socket. */
9886
9887 #if 0
9888 /* Time stamp of enter window event. This is only used by XTread_socket,
9889 but we have to put it out here, since static variables within functions
9890 sometimes don't work. */
9891
9892 static Time enter_timestamp;
9893 #endif
9894
9895 /* This holds the state XLookupString needs to implement dead keys
9896 and other tricks known as "compose processing". _X Window System_
9897 says that a portable program can't use this, but Stephen Gildea assures
9898 me that letting the compiler initialize it to zeros will work okay.
9899
9900 This must be defined outside of XTread_socket, for the same reasons
9901 given for enter_timestamp, above. */
9902
9903 static XComposeStatus compose_status;
9904
9905 /* Record the last 100 characters stored
9906 to help debug the loss-of-chars-during-GC problem. */
9907
9908 static int temp_index;
9909 static short temp_buffer[100];
9910
9911 /* Set this to nonzero to fake an "X I/O error"
9912 on a particular display. */
9913
9914 struct x_display_info *XTread_socket_fake_io_error;
9915
9916 /* When we find no input here, we occasionally do a no-op command
9917 to verify that the X server is still running and we can still talk with it.
9918 We try all the open displays, one by one.
9919 This variable is used for cycling thru the displays. */
9920
9921 static struct x_display_info *next_noop_dpyinfo;
9922
9923 #define SET_SAVED_MENU_EVENT(size) \
9924 do \
9925 { \
9926 if (f->output_data.x->saved_menu_event == 0) \
9927 f->output_data.x->saved_menu_event \
9928 = (XEvent *) xmalloc (sizeof (XEvent)); \
9929 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9930 if (numchars >= 1) \
9931 { \
9932 bufp->kind = menu_bar_activate_event; \
9933 XSETFRAME (bufp->frame_or_window, f); \
9934 bufp->arg = Qnil; \
9935 bufp++; \
9936 count++; \
9937 numchars--; \
9938 } \
9939 } \
9940 while (0)
9941
9942 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9943 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9944
9945 /* Read events coming from the X server.
9946 This routine is called by the SIGIO handler.
9947 We return as soon as there are no more events to be read.
9948
9949 Events representing keys are stored in buffer BUFP,
9950 which can hold up to NUMCHARS characters.
9951 We return the number of characters stored into the buffer,
9952 thus pretending to be `read'.
9953
9954 EXPECTED is nonzero if the caller knows input is available. */
9955
9956 int
9957 XTread_socket (sd, bufp, numchars, expected)
9958 register int sd;
9959 /* register */ struct input_event *bufp;
9960 /* register */ int numchars;
9961 int expected;
9962 {
9963 int count = 0;
9964 int nbytes = 0;
9965 XEvent event;
9966 struct frame *f;
9967 int event_found = 0;
9968 struct x_display_info *dpyinfo;
9969 struct coding_system coding;
9970
9971 if (interrupt_input_blocked)
9972 {
9973 interrupt_input_pending = 1;
9974 return -1;
9975 }
9976
9977 interrupt_input_pending = 0;
9978 BLOCK_INPUT;
9979
9980 /* So people can tell when we have read the available input. */
9981 input_signal_count++;
9982
9983 if (numchars <= 0)
9984 abort (); /* Don't think this happens. */
9985
9986 ++handling_signal;
9987
9988 /* The input should be decoded if it is from XIM. Currently the
9989 locale of XIM is the same as that of the system. So, we can use
9990 Vlocale_coding_system which is initialized properly at Emacs
9991 startup time. */
9992 setup_coding_system (Vlocale_coding_system, &coding);
9993 coding.src_multibyte = 0;
9994 coding.dst_multibyte = 1;
9995 /* The input is converted to events, thus we can't handle
9996 composition. Anyway, there's no XIM that gives us composition
9997 information. */
9998 coding.composing = COMPOSITION_DISABLED;
9999
10000 /* Find the display we are supposed to read input for.
10001 It's the one communicating on descriptor SD. */
10002 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
10003 {
10004 #if 0 /* This ought to be unnecessary; let's verify it. */
10005 #ifdef FIOSNBIO
10006 /* If available, Xlib uses FIOSNBIO to make the socket
10007 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
10008 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
10009 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
10010 fcntl (dpyinfo->connection, F_SETFL, 0);
10011 #endif /* ! defined (FIOSNBIO) */
10012 #endif
10013
10014 #if 0 /* This code can't be made to work, with multiple displays,
10015 and appears not to be used on any system any more.
10016 Also keyboard.c doesn't turn O_NDELAY on and off
10017 for X connections. */
10018 #ifndef SIGIO
10019 #ifndef HAVE_SELECT
10020 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
10021 {
10022 extern int read_alarm_should_throw;
10023 read_alarm_should_throw = 1;
10024 XPeekEvent (dpyinfo->display, &event);
10025 read_alarm_should_throw = 0;
10026 }
10027 #endif /* HAVE_SELECT */
10028 #endif /* SIGIO */
10029 #endif
10030
10031 /* For debugging, this gives a way to fake an I/O error. */
10032 if (dpyinfo == XTread_socket_fake_io_error)
10033 {
10034 XTread_socket_fake_io_error = 0;
10035 x_io_error_quitter (dpyinfo->display);
10036 }
10037
10038 #ifdef HAVE_X_SM
10039 BLOCK_INPUT;
10040 count += x_session_check_input (bufp, &numchars);
10041 UNBLOCK_INPUT;
10042 #endif
10043
10044 while (XPending (dpyinfo->display))
10045 {
10046 XNextEvent (dpyinfo->display, &event);
10047
10048 #ifdef HAVE_X_I18N
10049 {
10050 /* Filter events for the current X input method.
10051 XFilterEvent returns non-zero if the input method has
10052 consumed the event. We pass the frame's X window to
10053 XFilterEvent because that's the one for which the IC
10054 was created. */
10055 struct frame *f1 = x_any_window_to_frame (dpyinfo,
10056 event.xclient.window);
10057 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
10058 break;
10059 }
10060 #endif
10061 event_found = 1;
10062
10063 switch (event.type)
10064 {
10065 case ClientMessage:
10066 {
10067 if (event.xclient.message_type
10068 == dpyinfo->Xatom_wm_protocols
10069 && event.xclient.format == 32)
10070 {
10071 if (event.xclient.data.l[0]
10072 == dpyinfo->Xatom_wm_take_focus)
10073 {
10074 /* Use x_any_window_to_frame because this
10075 could be the shell widget window
10076 if the frame has no title bar. */
10077 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
10078 #ifdef HAVE_X_I18N
10079 /* Not quite sure this is needed -pd */
10080 if (f && FRAME_XIC (f))
10081 XSetICFocus (FRAME_XIC (f));
10082 #endif
10083 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
10084 instructs the WM to set the input focus automatically for
10085 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
10086 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
10087 it has set the focus. So, XSetInputFocus below is not
10088 needed.
10089
10090 The call to XSetInputFocus below has also caused trouble. In
10091 cases where the XSetInputFocus done by the WM and the one
10092 below are temporally close (on a fast machine), the call
10093 below can generate additional FocusIn events which confuse
10094 Emacs. */
10095
10096 /* Since we set WM_TAKE_FOCUS, we must call
10097 XSetInputFocus explicitly. But not if f is null,
10098 since that might be an event for a deleted frame. */
10099 if (f)
10100 {
10101 Display *d = event.xclient.display;
10102 /* Catch and ignore errors, in case window has been
10103 iconified by a window manager such as GWM. */
10104 int count = x_catch_errors (d);
10105 XSetInputFocus (d, event.xclient.window,
10106 /* The ICCCM says this is
10107 the only valid choice. */
10108 RevertToParent,
10109 event.xclient.data.l[1]);
10110 /* This is needed to detect the error
10111 if there is an error. */
10112 XSync (d, False);
10113 x_uncatch_errors (d, count);
10114 }
10115 /* Not certain about handling scroll bars here */
10116 #endif /* 0 */
10117 }
10118 else if (event.xclient.data.l[0]
10119 == dpyinfo->Xatom_wm_save_yourself)
10120 {
10121 /* Save state modify the WM_COMMAND property to
10122 something which can reinstate us. This notifies
10123 the session manager, who's looking for such a
10124 PropertyNotify. Can restart processing when
10125 a keyboard or mouse event arrives. */
10126 /* If we have a session manager, don't set this.
10127 KDE will then start two Emacsen, one for the
10128 session manager and one for this. */
10129 if (numchars > 0
10130 #ifdef HAVE_X_SM
10131 && ! x_session_have_connection ()
10132 #endif
10133 )
10134 {
10135 f = x_top_window_to_frame (dpyinfo,
10136 event.xclient.window);
10137 /* This is just so we only give real data once
10138 for a single Emacs process. */
10139 if (f == SELECTED_FRAME ())
10140 XSetCommand (FRAME_X_DISPLAY (f),
10141 event.xclient.window,
10142 initial_argv, initial_argc);
10143 else if (f)
10144 XSetCommand (FRAME_X_DISPLAY (f),
10145 event.xclient.window,
10146 0, 0);
10147 }
10148 }
10149 else if (event.xclient.data.l[0]
10150 == dpyinfo->Xatom_wm_delete_window)
10151 {
10152 struct frame *f
10153 = x_any_window_to_frame (dpyinfo,
10154 event.xclient.window);
10155
10156 if (f)
10157 {
10158 if (numchars == 0)
10159 abort ();
10160
10161 bufp->kind = delete_window_event;
10162 XSETFRAME (bufp->frame_or_window, f);
10163 bufp->arg = Qnil;
10164 bufp++;
10165
10166 count += 1;
10167 numchars -= 1;
10168 }
10169 }
10170 }
10171 else if (event.xclient.message_type
10172 == dpyinfo->Xatom_wm_configure_denied)
10173 {
10174 }
10175 else if (event.xclient.message_type
10176 == dpyinfo->Xatom_wm_window_moved)
10177 {
10178 int new_x, new_y;
10179 struct frame *f
10180 = x_window_to_frame (dpyinfo, event.xclient.window);
10181
10182 new_x = event.xclient.data.s[0];
10183 new_y = event.xclient.data.s[1];
10184
10185 if (f)
10186 {
10187 f->output_data.x->left_pos = new_x;
10188 f->output_data.x->top_pos = new_y;
10189 }
10190 }
10191 #ifdef HACK_EDITRES
10192 else if (event.xclient.message_type
10193 == dpyinfo->Xatom_editres)
10194 {
10195 struct frame *f
10196 = x_any_window_to_frame (dpyinfo, event.xclient.window);
10197 _XEditResCheckMessages (f->output_data.x->widget, NULL,
10198 &event, NULL);
10199 }
10200 #endif /* HACK_EDITRES */
10201 else if ((event.xclient.message_type
10202 == dpyinfo->Xatom_DONE)
10203 || (event.xclient.message_type
10204 == dpyinfo->Xatom_PAGE))
10205 {
10206 /* Ghostview job completed. Kill it. We could
10207 reply with "Next" if we received "Page", but we
10208 currently never do because we are interested in
10209 images, only, which should have 1 page. */
10210 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
10211 struct frame *f
10212 = x_window_to_frame (dpyinfo, event.xclient.window);
10213 x_kill_gs_process (pixmap, f);
10214 expose_frame (f, 0, 0, 0, 0);
10215 }
10216 #ifdef USE_TOOLKIT_SCROLL_BARS
10217 /* Scroll bar callbacks send a ClientMessage from which
10218 we construct an input_event. */
10219 else if (event.xclient.message_type
10220 == dpyinfo->Xatom_Scrollbar)
10221 {
10222 x_scroll_bar_to_input_event (&event, bufp);
10223 ++bufp, ++count, --numchars;
10224 goto out;
10225 }
10226 #endif /* USE_TOOLKIT_SCROLL_BARS */
10227 else
10228 goto OTHER;
10229 }
10230 break;
10231
10232 case SelectionNotify:
10233 #ifdef USE_X_TOOLKIT
10234 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
10235 goto OTHER;
10236 #endif /* not USE_X_TOOLKIT */
10237 x_handle_selection_notify (&event.xselection);
10238 break;
10239
10240 case SelectionClear: /* Someone has grabbed ownership. */
10241 #ifdef USE_X_TOOLKIT
10242 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
10243 goto OTHER;
10244 #endif /* USE_X_TOOLKIT */
10245 {
10246 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
10247
10248 if (numchars == 0)
10249 abort ();
10250
10251 bufp->kind = selection_clear_event;
10252 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10253 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10254 SELECTION_EVENT_TIME (bufp) = eventp->time;
10255 bufp->frame_or_window = Qnil;
10256 bufp->arg = Qnil;
10257 bufp++;
10258
10259 count += 1;
10260 numchars -= 1;
10261 }
10262 break;
10263
10264 case SelectionRequest: /* Someone wants our selection. */
10265 #ifdef USE_X_TOOLKIT
10266 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
10267 goto OTHER;
10268 #endif /* USE_X_TOOLKIT */
10269 if (x_queue_selection_requests)
10270 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
10271 &event);
10272 else
10273 {
10274 XSelectionRequestEvent *eventp
10275 = (XSelectionRequestEvent *) &event;
10276
10277 if (numchars == 0)
10278 abort ();
10279
10280 bufp->kind = selection_request_event;
10281 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10282 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
10283 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10284 SELECTION_EVENT_TARGET (bufp) = eventp->target;
10285 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
10286 SELECTION_EVENT_TIME (bufp) = eventp->time;
10287 bufp->frame_or_window = Qnil;
10288 bufp->arg = Qnil;
10289 bufp++;
10290
10291 count += 1;
10292 numchars -= 1;
10293 }
10294 break;
10295
10296 case PropertyNotify:
10297 #if 0 /* This is plain wrong. In the case that we are waiting for a
10298 PropertyNotify used as an ACK in incremental selection
10299 transfer, the property will be on the receiver's window. */
10300 #if defined USE_X_TOOLKIT
10301 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
10302 goto OTHER;
10303 #endif
10304 #endif
10305 x_handle_property_notify (&event.xproperty);
10306 goto OTHER;
10307
10308 case ReparentNotify:
10309 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
10310 if (f)
10311 {
10312 int x, y;
10313 f->output_data.x->parent_desc = event.xreparent.parent;
10314 x_real_positions (f, &x, &y);
10315 f->output_data.x->left_pos = x;
10316 f->output_data.x->top_pos = y;
10317 }
10318 break;
10319
10320 case Expose:
10321 f = x_window_to_frame (dpyinfo, event.xexpose.window);
10322 if (f)
10323 {
10324 x_check_fullscreen (f);
10325
10326 if (f->async_visible == 0)
10327 {
10328 f->async_visible = 1;
10329 f->async_iconified = 0;
10330 f->output_data.x->has_been_visible = 1;
10331 SET_FRAME_GARBAGED (f);
10332 }
10333 else
10334 expose_frame (x_window_to_frame (dpyinfo,
10335 event.xexpose.window),
10336 event.xexpose.x, event.xexpose.y,
10337 event.xexpose.width, event.xexpose.height);
10338 }
10339 else
10340 {
10341 #ifndef USE_TOOLKIT_SCROLL_BARS
10342 struct scroll_bar *bar;
10343 #endif
10344 #if defined USE_LUCID
10345 /* Submenus of the Lucid menu bar aren't widgets
10346 themselves, so there's no way to dispatch events
10347 to them. Recognize this case separately. */
10348 {
10349 Widget widget
10350 = x_window_to_menu_bar (event.xexpose.window);
10351 if (widget)
10352 xlwmenu_redisplay (widget);
10353 }
10354 #endif /* USE_LUCID */
10355
10356 #ifdef USE_TOOLKIT_SCROLL_BARS
10357 /* Dispatch event to the widget. */
10358 goto OTHER;
10359 #else /* not USE_TOOLKIT_SCROLL_BARS */
10360 bar = x_window_to_scroll_bar (event.xexpose.window);
10361
10362 if (bar)
10363 x_scroll_bar_expose (bar, &event);
10364 #ifdef USE_X_TOOLKIT
10365 else
10366 goto OTHER;
10367 #endif /* USE_X_TOOLKIT */
10368 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10369 }
10370 break;
10371
10372 case GraphicsExpose: /* This occurs when an XCopyArea's
10373 source area was obscured or not
10374 available. */
10375 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
10376 if (f)
10377 {
10378 expose_frame (f,
10379 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
10380 event.xgraphicsexpose.width,
10381 event.xgraphicsexpose.height);
10382 }
10383 #ifdef USE_X_TOOLKIT
10384 else
10385 goto OTHER;
10386 #endif /* USE_X_TOOLKIT */
10387 break;
10388
10389 case NoExpose: /* This occurs when an XCopyArea's
10390 source area was completely
10391 available. */
10392 break;
10393
10394 case UnmapNotify:
10395 /* Redo the mouse-highlight after the tooltip has gone. */
10396 if (event.xmap.window == tip_window)
10397 {
10398 tip_window = 0;
10399 redo_mouse_highlight ();
10400 }
10401
10402 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
10403 if (f) /* F may no longer exist if
10404 the frame was deleted. */
10405 {
10406 /* While a frame is unmapped, display generation is
10407 disabled; you don't want to spend time updating a
10408 display that won't ever be seen. */
10409 f->async_visible = 0;
10410 /* We can't distinguish, from the event, whether the window
10411 has become iconified or invisible. So assume, if it
10412 was previously visible, than now it is iconified.
10413 But x_make_frame_invisible clears both
10414 the visible flag and the iconified flag;
10415 and that way, we know the window is not iconified now. */
10416 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
10417 {
10418 f->async_iconified = 1;
10419
10420 bufp->kind = iconify_event;
10421 XSETFRAME (bufp->frame_or_window, f);
10422 bufp->arg = Qnil;
10423 bufp++;
10424 count++;
10425 numchars--;
10426 }
10427 }
10428 goto OTHER;
10429
10430 case MapNotify:
10431 if (event.xmap.window == tip_window)
10432 /* The tooltip has been drawn already. Avoid
10433 the SET_FRAME_GARBAGED below. */
10434 goto OTHER;
10435
10436 /* We use x_top_window_to_frame because map events can
10437 come for sub-windows and they don't mean that the
10438 frame is visible. */
10439 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
10440 if (f)
10441 {
10442 f->async_visible = 1;
10443 f->async_iconified = 0;
10444 f->output_data.x->has_been_visible = 1;
10445
10446 /* wait_reading_process_input will notice this and update
10447 the frame's display structures. */
10448 SET_FRAME_GARBAGED (f);
10449
10450 if (f->iconified)
10451 {
10452 bufp->kind = deiconify_event;
10453 XSETFRAME (bufp->frame_or_window, f);
10454 bufp->arg = Qnil;
10455 bufp++;
10456 count++;
10457 numchars--;
10458 }
10459 else if (! NILP (Vframe_list)
10460 && ! NILP (XCDR (Vframe_list)))
10461 /* Force a redisplay sooner or later
10462 to update the frame titles
10463 in case this is the second frame. */
10464 record_asynch_buffer_change ();
10465 }
10466 goto OTHER;
10467
10468 case KeyPress:
10469 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
10470
10471 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
10472 {
10473 dpyinfo->mouse_face_hidden = 1;
10474 clear_mouse_face (dpyinfo);
10475 }
10476
10477 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
10478 if (f == 0)
10479 {
10480 /* Scroll bars consume key events, but we want
10481 the keys to go to the scroll bar's frame. */
10482 Widget widget = XtWindowToWidget (dpyinfo->display,
10483 event.xkey.window);
10484 if (widget && XmIsScrollBar (widget))
10485 {
10486 widget = XtParent (widget);
10487 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
10488 }
10489 }
10490 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
10491
10492 if (f != 0)
10493 {
10494 KeySym keysym, orig_keysym;
10495 /* al%imercury@uunet.uu.net says that making this 81
10496 instead of 80 fixed a bug whereby meta chars made
10497 his Emacs hang.
10498
10499 It seems that some version of XmbLookupString has
10500 a bug of not returning XBufferOverflow in
10501 status_return even if the input is too long to
10502 fit in 81 bytes. So, we must prepare sufficient
10503 bytes for copy_buffer. 513 bytes (256 chars for
10504 two-byte character set) seems to be a fairly good
10505 approximation. -- 2000.8.10 handa@etl.go.jp */
10506 unsigned char copy_buffer[513];
10507 unsigned char *copy_bufptr = copy_buffer;
10508 int copy_bufsiz = sizeof (copy_buffer);
10509 int modifiers;
10510
10511 event.xkey.state
10512 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
10513 extra_keyboard_modifiers);
10514 modifiers = event.xkey.state;
10515
10516 /* This will have to go some day... */
10517
10518 /* make_lispy_event turns chars into control chars.
10519 Don't do it here because XLookupString is too eager. */
10520 event.xkey.state &= ~ControlMask;
10521 event.xkey.state &= ~(dpyinfo->meta_mod_mask
10522 | dpyinfo->super_mod_mask
10523 | dpyinfo->hyper_mod_mask
10524 | dpyinfo->alt_mod_mask);
10525
10526 /* In case Meta is ComposeCharacter,
10527 clear its status. According to Markus Ehrnsperger
10528 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
10529 this enables ComposeCharacter to work whether or
10530 not it is combined with Meta. */
10531 if (modifiers & dpyinfo->meta_mod_mask)
10532 bzero (&compose_status, sizeof (compose_status));
10533
10534 #ifdef HAVE_X_I18N
10535 if (FRAME_XIC (f))
10536 {
10537 Status status_return;
10538
10539 nbytes = XmbLookupString (FRAME_XIC (f),
10540 &event.xkey, copy_bufptr,
10541 copy_bufsiz, &keysym,
10542 &status_return);
10543 if (status_return == XBufferOverflow)
10544 {
10545 copy_bufsiz = nbytes + 1;
10546 copy_bufptr = (char *) alloca (copy_bufsiz);
10547 nbytes = XmbLookupString (FRAME_XIC (f),
10548 &event.xkey, copy_bufptr,
10549 copy_bufsiz, &keysym,
10550 &status_return);
10551 }
10552
10553 if (status_return == XLookupNone)
10554 break;
10555 else if (status_return == XLookupChars)
10556 {
10557 keysym = NoSymbol;
10558 modifiers = 0;
10559 }
10560 else if (status_return != XLookupKeySym
10561 && status_return != XLookupBoth)
10562 abort ();
10563 }
10564 else
10565 nbytes = XLookupString (&event.xkey, copy_bufptr,
10566 copy_bufsiz, &keysym,
10567 &compose_status);
10568 #else
10569 nbytes = XLookupString (&event.xkey, copy_bufptr,
10570 copy_bufsiz, &keysym,
10571 &compose_status);
10572 #endif
10573
10574 orig_keysym = keysym;
10575
10576 if (numchars > 1)
10577 {
10578 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
10579 || keysym == XK_Delete
10580 #ifdef XK_ISO_Left_Tab
10581 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
10582 #endif
10583 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
10584 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
10585 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
10586 #ifdef HPUX
10587 /* This recognizes the "extended function keys".
10588 It seems there's no cleaner way.
10589 Test IsModifierKey to avoid handling mode_switch
10590 incorrectly. */
10591 || ((unsigned) (keysym) >= XK_Select
10592 && (unsigned)(keysym) < XK_KP_Space)
10593 #endif
10594 #ifdef XK_dead_circumflex
10595 || orig_keysym == XK_dead_circumflex
10596 #endif
10597 #ifdef XK_dead_grave
10598 || orig_keysym == XK_dead_grave
10599 #endif
10600 #ifdef XK_dead_tilde
10601 || orig_keysym == XK_dead_tilde
10602 #endif
10603 #ifdef XK_dead_diaeresis
10604 || orig_keysym == XK_dead_diaeresis
10605 #endif
10606 #ifdef XK_dead_macron
10607 || orig_keysym == XK_dead_macron
10608 #endif
10609 #ifdef XK_dead_degree
10610 || orig_keysym == XK_dead_degree
10611 #endif
10612 #ifdef XK_dead_acute
10613 || orig_keysym == XK_dead_acute
10614 #endif
10615 #ifdef XK_dead_cedilla
10616 || orig_keysym == XK_dead_cedilla
10617 #endif
10618 #ifdef XK_dead_breve
10619 || orig_keysym == XK_dead_breve
10620 #endif
10621 #ifdef XK_dead_ogonek
10622 || orig_keysym == XK_dead_ogonek
10623 #endif
10624 #ifdef XK_dead_caron
10625 || orig_keysym == XK_dead_caron
10626 #endif
10627 #ifdef XK_dead_doubleacute
10628 || orig_keysym == XK_dead_doubleacute
10629 #endif
10630 #ifdef XK_dead_abovedot
10631 || orig_keysym == XK_dead_abovedot
10632 #endif
10633 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10634 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10635 /* Any "vendor-specific" key is ok. */
10636 || (orig_keysym & (1 << 28))
10637 || (keysym != NoSymbol && nbytes == 0))
10638 && ! (IsModifierKey (orig_keysym)
10639 #ifndef HAVE_X11R5
10640 #ifdef XK_Mode_switch
10641 || ((unsigned)(orig_keysym) == XK_Mode_switch)
10642 #endif
10643 #ifdef XK_Num_Lock
10644 || ((unsigned)(orig_keysym) == XK_Num_Lock)
10645 #endif
10646 #endif /* not HAVE_X11R5 */
10647 ))
10648 {
10649 if (temp_index == sizeof temp_buffer / sizeof (short))
10650 temp_index = 0;
10651 temp_buffer[temp_index++] = keysym;
10652 bufp->kind = non_ascii_keystroke;
10653 bufp->code = keysym;
10654 XSETFRAME (bufp->frame_or_window, f);
10655 bufp->arg = Qnil;
10656 bufp->modifiers
10657 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10658 modifiers);
10659 bufp->timestamp = event.xkey.time;
10660 bufp++;
10661 count++;
10662 numchars--;
10663 }
10664 else if (numchars > nbytes)
10665 {
10666 register int i;
10667 register int c;
10668 int nchars, len;
10669
10670 for (i = 0; i < nbytes; i++)
10671 {
10672 if (temp_index == (sizeof temp_buffer
10673 / sizeof (short)))
10674 temp_index = 0;
10675 temp_buffer[temp_index++] = copy_bufptr[i];
10676 }
10677
10678 if (/* If the event is not from XIM, */
10679 event.xkey.keycode != 0
10680 /* or the current locale doesn't request
10681 decoding of the intup data, ... */
10682 || coding.type == coding_type_raw_text
10683 || coding.type == coding_type_no_conversion)
10684 {
10685 /* ... we can use the input data as is. */
10686 nchars = nbytes;
10687 }
10688 else
10689 {
10690 /* We have to decode the input data. */
10691 int require;
10692 unsigned char *p;
10693
10694 require = decoding_buffer_size (&coding, nbytes);
10695 p = (unsigned char *) alloca (require);
10696 coding.mode |= CODING_MODE_LAST_BLOCK;
10697 decode_coding (&coding, copy_bufptr, p,
10698 nbytes, require);
10699 nbytes = coding.produced;
10700 nchars = coding.produced_char;
10701 copy_bufptr = p;
10702 }
10703
10704 /* Convert the input data to a sequence of
10705 character events. */
10706 for (i = 0; i < nbytes; i += len)
10707 {
10708 if (nchars == nbytes)
10709 c = copy_bufptr[i], len = 1;
10710 else
10711 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10712 nbytes - i, len);
10713
10714 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
10715 ? ascii_keystroke
10716 : multibyte_char_keystroke);
10717 bufp->code = c;
10718 XSETFRAME (bufp->frame_or_window, f);
10719 bufp->arg = Qnil;
10720 bufp->modifiers
10721 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10722 modifiers);
10723 bufp->timestamp = event.xkey.time;
10724 bufp++;
10725 }
10726
10727 count += nchars;
10728 numchars -= nchars;
10729
10730 if (keysym == NoSymbol)
10731 break;
10732 }
10733 else
10734 abort ();
10735 }
10736 else
10737 abort ();
10738 }
10739 #ifdef HAVE_X_I18N
10740 /* Don't dispatch this event since XtDispatchEvent calls
10741 XFilterEvent, and two calls in a row may freeze the
10742 client. */
10743 break;
10744 #else
10745 goto OTHER;
10746 #endif
10747
10748 case KeyRelease:
10749 #ifdef HAVE_X_I18N
10750 /* Don't dispatch this event since XtDispatchEvent calls
10751 XFilterEvent, and two calls in a row may freeze the
10752 client. */
10753 break;
10754 #else
10755 goto OTHER;
10756 #endif
10757
10758 /* Here's a possible interpretation of the whole
10759 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
10760 you get a FocusIn event, you have to get a FocusOut
10761 event before you relinquish the focus. If you
10762 haven't received a FocusIn event, then a mere
10763 LeaveNotify is enough to free you. */
10764
10765 case EnterNotify:
10766 {
10767 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10768
10769 #if 0
10770 if (event.xcrossing.focus)
10771 {
10772 /* Avoid nasty pop/raise loops. */
10773 if (f && (!(f->auto_raise)
10774 || !(f->auto_lower)
10775 || (event.xcrossing.time - enter_timestamp) > 500))
10776 {
10777 x_new_focus_frame (dpyinfo, f);
10778 enter_timestamp = event.xcrossing.time;
10779 }
10780 }
10781 else if (f == dpyinfo->x_focus_frame)
10782 x_new_focus_frame (dpyinfo, 0);
10783 #endif
10784
10785 /* EnterNotify counts as mouse movement,
10786 so update things that depend on mouse position. */
10787 if (f && !f->output_data.x->hourglass_p)
10788 note_mouse_movement (f, &event.xmotion);
10789 goto OTHER;
10790 }
10791
10792 case FocusIn:
10793 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10794 if (event.xfocus.detail != NotifyPointer)
10795 dpyinfo->x_focus_event_frame = f;
10796 if (f)
10797 {
10798 x_new_focus_frame (dpyinfo, f);
10799
10800 /* Don't stop displaying the initial startup message
10801 for a switch-frame event we don't need. */
10802 if (GC_NILP (Vterminal_frame)
10803 && GC_CONSP (Vframe_list)
10804 && !GC_NILP (XCDR (Vframe_list)))
10805 {
10806 bufp->kind = FOCUS_IN_EVENT;
10807 XSETFRAME (bufp->frame_or_window, f);
10808 bufp->arg = Qnil;
10809 ++bufp, ++count, --numchars;
10810 }
10811 }
10812
10813 #ifdef HAVE_X_I18N
10814 if (f && FRAME_XIC (f))
10815 XSetICFocus (FRAME_XIC (f));
10816 #endif
10817
10818 goto OTHER;
10819
10820 case LeaveNotify:
10821 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
10822 if (f)
10823 {
10824 if (f == dpyinfo->mouse_face_mouse_frame)
10825 {
10826 /* If we move outside the frame, then we're
10827 certainly no longer on any text in the frame. */
10828 clear_mouse_face (dpyinfo);
10829 dpyinfo->mouse_face_mouse_frame = 0;
10830 }
10831
10832 /* Generate a nil HELP_EVENT to cancel a help-echo.
10833 Do it only if there's something to cancel.
10834 Otherwise, the startup message is cleared when
10835 the mouse leaves the frame. */
10836 if (any_help_event_p)
10837 {
10838 Lisp_Object frame;
10839 int n;
10840
10841 XSETFRAME (frame, f);
10842 help_echo = Qnil;
10843 n = gen_help_event (bufp, numchars,
10844 Qnil, frame, Qnil, Qnil, 0);
10845 bufp += n, count += n, numchars -= n;
10846 }
10847
10848 #if 0
10849 if (event.xcrossing.focus)
10850 x_mouse_leave (dpyinfo);
10851 else
10852 {
10853 if (f == dpyinfo->x_focus_event_frame)
10854 dpyinfo->x_focus_event_frame = 0;
10855 if (f == dpyinfo->x_focus_frame)
10856 x_new_focus_frame (dpyinfo, 0);
10857 }
10858 #endif
10859 }
10860 goto OTHER;
10861
10862 case FocusOut:
10863 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10864 if (event.xfocus.detail != NotifyPointer
10865 && f == dpyinfo->x_focus_event_frame)
10866 dpyinfo->x_focus_event_frame = 0;
10867 if (f && f == dpyinfo->x_focus_frame)
10868 x_new_focus_frame (dpyinfo, 0);
10869
10870 #ifdef HAVE_X_I18N
10871 if (f && FRAME_XIC (f))
10872 XUnsetICFocus (FRAME_XIC (f));
10873 #endif
10874
10875 goto OTHER;
10876
10877 case MotionNotify:
10878 {
10879 previous_help_echo = help_echo;
10880 help_echo = help_echo_object = help_echo_window = Qnil;
10881 help_echo_pos = -1;
10882
10883 if (dpyinfo->grabbed && last_mouse_frame
10884 && FRAME_LIVE_P (last_mouse_frame))
10885 f = last_mouse_frame;
10886 else
10887 f = x_window_to_frame (dpyinfo, event.xmotion.window);
10888
10889 if (dpyinfo->mouse_face_hidden)
10890 {
10891 dpyinfo->mouse_face_hidden = 0;
10892 clear_mouse_face (dpyinfo);
10893 }
10894
10895 if (f)
10896 note_mouse_movement (f, &event.xmotion);
10897 else
10898 {
10899 #ifndef USE_TOOLKIT_SCROLL_BARS
10900 struct scroll_bar *bar
10901 = x_window_to_scroll_bar (event.xmotion.window);
10902
10903 if (bar)
10904 x_scroll_bar_note_movement (bar, &event);
10905 #endif /* USE_TOOLKIT_SCROLL_BARS */
10906
10907 /* If we move outside the frame, then we're
10908 certainly no longer on any text in the frame. */
10909 clear_mouse_face (dpyinfo);
10910 }
10911
10912 /* If the contents of the global variable help_echo
10913 has changed, generate a HELP_EVENT. */
10914 if (!NILP (help_echo)
10915 || !NILP (previous_help_echo))
10916 {
10917 Lisp_Object frame;
10918 int n;
10919
10920 if (f)
10921 XSETFRAME (frame, f);
10922 else
10923 frame = Qnil;
10924
10925 any_help_event_p = 1;
10926 n = gen_help_event (bufp, numchars, help_echo, frame,
10927 help_echo_window, help_echo_object,
10928 help_echo_pos);
10929 bufp += n, count += n, numchars -= n;
10930 }
10931
10932 goto OTHER;
10933 }
10934
10935 case ConfigureNotify:
10936 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10937 if (f)
10938 {
10939 #ifndef USE_X_TOOLKIT
10940 /* If there is a pending resize for fullscreen, don't
10941 do this one, the right one will come later.
10942 The toolkit version doesn't seem to need this, but we
10943 need to reset it below. */
10944 int dont_resize =
10945 ((f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
10946 && FRAME_NEW_WIDTH (f) != 0);
10947 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10948 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
10949 if (dont_resize)
10950 goto OTHER;
10951
10952 /* In the toolkit version, change_frame_size
10953 is called by the code that handles resizing
10954 of the EmacsFrame widget. */
10955
10956 /* Even if the number of character rows and columns has
10957 not changed, the font size may have changed, so we need
10958 to check the pixel dimensions as well. */
10959 if (columns != f->width
10960 || rows != f->height
10961 || event.xconfigure.width != f->output_data.x->pixel_width
10962 || event.xconfigure.height != f->output_data.x->pixel_height)
10963 {
10964 change_frame_size (f, rows, columns, 0, 1, 0);
10965 SET_FRAME_GARBAGED (f);
10966 cancel_mouse_face (f);
10967 }
10968 #endif
10969
10970 f->output_data.x->pixel_width = event.xconfigure.width;
10971 f->output_data.x->pixel_height = event.xconfigure.height;
10972
10973 /* What we have now is the position of Emacs's own window.
10974 Convert that to the position of the window manager window. */
10975 x_real_positions (f, &f->output_data.x->left_pos,
10976 &f->output_data.x->top_pos);
10977
10978 x_check_fullscreen_move(f);
10979 if (f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
10980 f->output_data.x->want_fullscreen &=
10981 ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
10982 #ifdef HAVE_X_I18N
10983 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10984 xic_set_statusarea (f);
10985 #endif
10986
10987 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10988 {
10989 /* Since the WM decorations come below top_pos now,
10990 we must put them below top_pos in the future. */
10991 f->output_data.x->win_gravity = NorthWestGravity;
10992 x_wm_set_size_hint (f, (long) 0, 0);
10993 }
10994 #ifdef USE_MOTIF
10995 /* Some window managers pass (0,0) as the location of
10996 the window, and the Motif event handler stores it
10997 in the emacs widget, which messes up Motif menus. */
10998 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10999 {
11000 event.xconfigure.x = f->output_data.x->widget->core.x;
11001 event.xconfigure.y = f->output_data.x->widget->core.y;
11002 }
11003 #endif /* USE_MOTIF */
11004 }
11005 goto OTHER;
11006
11007 case ButtonPress:
11008 case ButtonRelease:
11009 {
11010 /* If we decide we want to generate an event to be seen
11011 by the rest of Emacs, we put it here. */
11012 struct input_event emacs_event;
11013 int tool_bar_p = 0;
11014
11015 emacs_event.kind = no_event;
11016 bzero (&compose_status, sizeof (compose_status));
11017
11018 if (dpyinfo->grabbed
11019 && last_mouse_frame
11020 && FRAME_LIVE_P (last_mouse_frame))
11021 f = last_mouse_frame;
11022 else
11023 f = x_window_to_frame (dpyinfo, event.xbutton.window);
11024
11025 if (f)
11026 {
11027 /* Is this in the tool-bar? */
11028 if (WINDOWP (f->tool_bar_window)
11029 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
11030 {
11031 Lisp_Object window;
11032 int p, x, y;
11033
11034 x = event.xbutton.x;
11035 y = event.xbutton.y;
11036
11037 /* Set x and y. */
11038 window = window_from_coordinates (f, x, y, &p, 1);
11039 if (EQ (window, f->tool_bar_window))
11040 {
11041 x_handle_tool_bar_click (f, &event.xbutton);
11042 tool_bar_p = 1;
11043 }
11044 }
11045
11046 if (!tool_bar_p)
11047 if (!dpyinfo->x_focus_frame
11048 || f == dpyinfo->x_focus_frame)
11049 construct_mouse_click (&emacs_event, &event, f);
11050 }
11051 else
11052 {
11053 #ifndef USE_TOOLKIT_SCROLL_BARS
11054 struct scroll_bar *bar
11055 = x_window_to_scroll_bar (event.xbutton.window);
11056
11057 if (bar)
11058 x_scroll_bar_handle_click (bar, &event, &emacs_event);
11059 #endif /* not USE_TOOLKIT_SCROLL_BARS */
11060 }
11061
11062 if (event.type == ButtonPress)
11063 {
11064 dpyinfo->grabbed |= (1 << event.xbutton.button);
11065 last_mouse_frame = f;
11066 /* Ignore any mouse motion that happened
11067 before this event; any subsequent mouse-movement
11068 Emacs events should reflect only motion after
11069 the ButtonPress. */
11070 if (f != 0)
11071 f->mouse_moved = 0;
11072
11073 if (!tool_bar_p)
11074 last_tool_bar_item = -1;
11075 }
11076 else
11077 {
11078 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
11079 }
11080
11081 if (numchars >= 1 && emacs_event.kind != no_event)
11082 {
11083 bcopy (&emacs_event, bufp, sizeof (struct input_event));
11084 bufp++;
11085 count++;
11086 numchars--;
11087 }
11088
11089 #ifdef USE_X_TOOLKIT
11090 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
11091 /* For a down-event in the menu bar,
11092 don't pass it to Xt right now.
11093 Instead, save it away
11094 and we will pass it to Xt from kbd_buffer_get_event.
11095 That way, we can run some Lisp code first. */
11096 if (f && event.type == ButtonPress
11097 /* Verify the event is really within the menu bar
11098 and not just sent to it due to grabbing. */
11099 && event.xbutton.x >= 0
11100 && event.xbutton.x < f->output_data.x->pixel_width
11101 && event.xbutton.y >= 0
11102 && event.xbutton.y < f->output_data.x->menubar_height
11103 && event.xbutton.same_screen)
11104 {
11105 SET_SAVED_BUTTON_EVENT;
11106 XSETFRAME (last_mouse_press_frame, f);
11107 }
11108 else if (event.type == ButtonPress)
11109 {
11110 last_mouse_press_frame = Qnil;
11111 goto OTHER;
11112 }
11113
11114 #ifdef USE_MOTIF /* This should do not harm for Lucid,
11115 but I am trying to be cautious. */
11116 else if (event.type == ButtonRelease)
11117 {
11118 if (!NILP (last_mouse_press_frame))
11119 {
11120 f = XFRAME (last_mouse_press_frame);
11121 if (f->output_data.x)
11122 SET_SAVED_BUTTON_EVENT;
11123 }
11124 else
11125 goto OTHER;
11126 }
11127 #endif /* USE_MOTIF */
11128 else
11129 goto OTHER;
11130 #endif /* USE_X_TOOLKIT */
11131 }
11132 break;
11133
11134 case CirculateNotify:
11135 goto OTHER;
11136
11137 case CirculateRequest:
11138 goto OTHER;
11139
11140 case VisibilityNotify:
11141 goto OTHER;
11142
11143 case MappingNotify:
11144 /* Someone has changed the keyboard mapping - update the
11145 local cache. */
11146 switch (event.xmapping.request)
11147 {
11148 case MappingModifier:
11149 x_find_modifier_meanings (dpyinfo);
11150 /* This is meant to fall through. */
11151 case MappingKeyboard:
11152 XRefreshKeyboardMapping (&event.xmapping);
11153 }
11154 goto OTHER;
11155
11156 default:
11157 OTHER:
11158 #ifdef USE_X_TOOLKIT
11159 BLOCK_INPUT;
11160 XtDispatchEvent (&event);
11161 UNBLOCK_INPUT;
11162 #endif /* USE_X_TOOLKIT */
11163 break;
11164 }
11165 }
11166 }
11167
11168 out:;
11169
11170 /* On some systems, an X bug causes Emacs to get no more events
11171 when the window is destroyed. Detect that. (1994.) */
11172 if (! event_found)
11173 {
11174 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
11175 One XNOOP in 100 loops will make Emacs terminate.
11176 B. Bretthauer, 1994 */
11177 x_noop_count++;
11178 if (x_noop_count >= 100)
11179 {
11180 x_noop_count=0;
11181
11182 if (next_noop_dpyinfo == 0)
11183 next_noop_dpyinfo = x_display_list;
11184
11185 XNoOp (next_noop_dpyinfo->display);
11186
11187 /* Each time we get here, cycle through the displays now open. */
11188 next_noop_dpyinfo = next_noop_dpyinfo->next;
11189 }
11190 }
11191
11192 /* If the focus was just given to an auto-raising frame,
11193 raise it now. */
11194 /* ??? This ought to be able to handle more than one such frame. */
11195 if (pending_autoraise_frame)
11196 {
11197 x_raise_frame (pending_autoraise_frame);
11198 pending_autoraise_frame = 0;
11199 }
11200
11201 UNBLOCK_INPUT;
11202 --handling_signal;
11203 return count;
11204 }
11205
11206
11207
11208 \f
11209 /***********************************************************************
11210 Text Cursor
11211 ***********************************************************************/
11212
11213 /* Notice when the text cursor of window W has been completely
11214 overwritten by a drawing operation that outputs glyphs in AREA
11215 starting at X0 and ending at X1 in the line starting at Y0 and
11216 ending at Y1. X coordinates are area-relative. X1 < 0 means all
11217 the rest of the line after X0 has been written. Y coordinates
11218 are window-relative. */
11219
11220 static void
11221 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
11222 struct window *w;
11223 enum glyph_row_area area;
11224 int x0, y0, x1, y1;
11225 {
11226 if (area == TEXT_AREA
11227 && w->phys_cursor_on_p
11228 && y0 <= w->phys_cursor.y
11229 && y1 >= w->phys_cursor.y + w->phys_cursor_height
11230 && x0 <= w->phys_cursor.x
11231 && (x1 < 0 || x1 > w->phys_cursor.x))
11232 w->phys_cursor_on_p = 0;
11233 }
11234
11235
11236 /* Set clipping for output in glyph row ROW. W is the window in which
11237 we operate. GC is the graphics context to set clipping in.
11238 WHOLE_LINE_P non-zero means include the areas used for truncation
11239 mark display and alike in the clipping rectangle.
11240
11241 ROW may be a text row or, e.g., a mode line. Text rows must be
11242 clipped to the interior of the window dedicated to text display,
11243 mode lines must be clipped to the whole window. */
11244
11245 static void
11246 x_clip_to_row (w, row, gc, whole_line_p)
11247 struct window *w;
11248 struct glyph_row *row;
11249 GC gc;
11250 int whole_line_p;
11251 {
11252 struct frame *f = XFRAME (WINDOW_FRAME (w));
11253 XRectangle clip_rect;
11254 int window_x, window_y, window_width, window_height;
11255
11256 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
11257
11258 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
11259 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
11260 clip_rect.y = max (clip_rect.y, window_y);
11261 clip_rect.width = window_width;
11262 clip_rect.height = row->visible_height;
11263
11264 /* If clipping to the whole line, including trunc marks, extend
11265 the rectangle to the left and increase its width. */
11266 if (whole_line_p)
11267 {
11268 clip_rect.x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
11269 clip_rect.width += FRAME_X_FRINGE_WIDTH (f);
11270 }
11271
11272 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
11273 }
11274
11275
11276 /* Draw a hollow box cursor on window W in glyph row ROW. */
11277
11278 static void
11279 x_draw_hollow_cursor (w, row)
11280 struct window *w;
11281 struct glyph_row *row;
11282 {
11283 struct frame *f = XFRAME (WINDOW_FRAME (w));
11284 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11285 Display *dpy = FRAME_X_DISPLAY (f);
11286 int x, y, wd, h;
11287 XGCValues xgcv;
11288 struct glyph *cursor_glyph;
11289 GC gc;
11290
11291 /* Compute frame-relative coordinates from window-relative
11292 coordinates. */
11293 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11294 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
11295 + row->ascent - w->phys_cursor_ascent);
11296 h = row->height - 1;
11297
11298 /* Get the glyph the cursor is on. If we can't tell because
11299 the current matrix is invalid or such, give up. */
11300 cursor_glyph = get_phys_cursor_glyph (w);
11301 if (cursor_glyph == NULL)
11302 return;
11303
11304 /* Compute the width of the rectangle to draw. If on a stretch
11305 glyph, and `x-stretch-block-cursor' is nil, don't draw a
11306 rectangle as wide as the glyph, but use a canonical character
11307 width instead. */
11308 wd = cursor_glyph->pixel_width - 1;
11309 if (cursor_glyph->type == STRETCH_GLYPH
11310 && !x_stretch_cursor_p)
11311 wd = min (CANON_X_UNIT (f), wd);
11312
11313 /* The foreground of cursor_gc is typically the same as the normal
11314 background color, which can cause the cursor box to be invisible. */
11315 xgcv.foreground = f->output_data.x->cursor_pixel;
11316 if (dpyinfo->scratch_cursor_gc)
11317 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
11318 else
11319 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
11320 GCForeground, &xgcv);
11321 gc = dpyinfo->scratch_cursor_gc;
11322
11323 /* Set clipping, draw the rectangle, and reset clipping again. */
11324 x_clip_to_row (w, row, gc, 0);
11325 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
11326 XSetClipMask (dpy, gc, None);
11327 }
11328
11329
11330 /* Draw a bar cursor on window W in glyph row ROW.
11331
11332 Implementation note: One would like to draw a bar cursor with an
11333 angle equal to the one given by the font property XA_ITALIC_ANGLE.
11334 Unfortunately, I didn't find a font yet that has this property set.
11335 --gerd. */
11336
11337 static void
11338 x_draw_bar_cursor (w, row, width)
11339 struct window *w;
11340 struct glyph_row *row;
11341 int width;
11342 {
11343 struct frame *f = XFRAME (w->frame);
11344 struct glyph *cursor_glyph;
11345
11346 /* If cursor is out of bounds, don't draw garbage. This can happen
11347 in mini-buffer windows when switching between echo area glyphs
11348 and mini-buffer. */
11349 cursor_glyph = get_phys_cursor_glyph (w);
11350 if (cursor_glyph == NULL)
11351 return;
11352
11353 /* If on an image, draw like a normal cursor. That's usually better
11354 visible than drawing a bar, esp. if the image is large so that
11355 the bar might not be in the window. */
11356 if (cursor_glyph->type == IMAGE_GLYPH)
11357 {
11358 struct glyph_row *row;
11359 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
11360 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
11361 }
11362 else
11363 {
11364 Display *dpy = FRAME_X_DISPLAY (f);
11365 Window window = FRAME_X_WINDOW (f);
11366 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
11367 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
11368 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
11369 XGCValues xgcv;
11370
11371 /* If the glyph's background equals the color we normally draw
11372 the bar cursor in, the bar cursor in its normal color is
11373 invisible. Use the glyph's foreground color instead in this
11374 case, on the assumption that the glyph's colors are chosen so
11375 that the glyph is legible. */
11376 if (face->background == f->output_data.x->cursor_pixel)
11377 xgcv.background = xgcv.foreground = face->foreground;
11378 else
11379 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
11380 xgcv.graphics_exposures = 0;
11381
11382 if (gc)
11383 XChangeGC (dpy, gc, mask, &xgcv);
11384 else
11385 {
11386 gc = XCreateGC (dpy, window, mask, &xgcv);
11387 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
11388 }
11389
11390 if (width < 0)
11391 width = f->output_data.x->cursor_width;
11392 width = min (cursor_glyph->pixel_width, width);
11393
11394 x_clip_to_row (w, row, gc, 0);
11395 XFillRectangle (dpy, window, gc,
11396 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11397 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
11398 width, row->height);
11399 XSetClipMask (dpy, gc, None);
11400 }
11401 }
11402
11403
11404 /* Clear the cursor of window W to background color, and mark the
11405 cursor as not shown. This is used when the text where the cursor
11406 is is about to be rewritten. */
11407
11408 static void
11409 x_clear_cursor (w)
11410 struct window *w;
11411 {
11412 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
11413 x_update_window_cursor (w, 0);
11414 }
11415
11416
11417 /* Draw the cursor glyph of window W in glyph row ROW. See the
11418 comment of x_draw_glyphs for the meaning of HL. */
11419
11420 static void
11421 x_draw_phys_cursor_glyph (w, row, hl)
11422 struct window *w;
11423 struct glyph_row *row;
11424 enum draw_glyphs_face hl;
11425 {
11426 /* If cursor hpos is out of bounds, don't draw garbage. This can
11427 happen in mini-buffer windows when switching between echo area
11428 glyphs and mini-buffer. */
11429 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
11430 {
11431 int on_p = w->phys_cursor_on_p;
11432
11433 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
11434 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
11435 hl, 0);
11436 w->phys_cursor_on_p = on_p;
11437
11438 /* When we erase the cursor, and ROW is overlapped by other
11439 rows, make sure that these overlapping parts of other rows
11440 are redrawn. */
11441 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
11442 {
11443 if (row > w->current_matrix->rows
11444 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
11445 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
11446
11447 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
11448 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
11449 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
11450 }
11451 }
11452 }
11453
11454
11455 /* Erase the image of a cursor of window W from the screen. */
11456
11457 static void
11458 x_erase_phys_cursor (w)
11459 struct window *w;
11460 {
11461 struct frame *f = XFRAME (w->frame);
11462 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11463 int hpos = w->phys_cursor.hpos;
11464 int vpos = w->phys_cursor.vpos;
11465 int mouse_face_here_p = 0;
11466 struct glyph_matrix *active_glyphs = w->current_matrix;
11467 struct glyph_row *cursor_row;
11468 struct glyph *cursor_glyph;
11469 enum draw_glyphs_face hl;
11470
11471 /* No cursor displayed or row invalidated => nothing to do on the
11472 screen. */
11473 if (w->phys_cursor_type == NO_CURSOR)
11474 goto mark_cursor_off;
11475
11476 /* VPOS >= active_glyphs->nrows means that window has been resized.
11477 Don't bother to erase the cursor. */
11478 if (vpos >= active_glyphs->nrows)
11479 goto mark_cursor_off;
11480
11481 /* If row containing cursor is marked invalid, there is nothing we
11482 can do. */
11483 cursor_row = MATRIX_ROW (active_glyphs, vpos);
11484 if (!cursor_row->enabled_p)
11485 goto mark_cursor_off;
11486
11487 /* If row is completely invisible, don't attempt to delete a cursor which
11488 isn't there. This can happen if cursor is at top of a window, and
11489 we switch to a buffer with a header line in that window. */
11490 if (cursor_row->visible_height <= 0)
11491 goto mark_cursor_off;
11492
11493 /* This can happen when the new row is shorter than the old one.
11494 In this case, either x_draw_glyphs or clear_end_of_line
11495 should have cleared the cursor. Note that we wouldn't be
11496 able to erase the cursor in this case because we don't have a
11497 cursor glyph at hand. */
11498 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
11499 goto mark_cursor_off;
11500
11501 /* If the cursor is in the mouse face area, redisplay that when
11502 we clear the cursor. */
11503 if (! NILP (dpyinfo->mouse_face_window)
11504 && w == XWINDOW (dpyinfo->mouse_face_window)
11505 && (vpos > dpyinfo->mouse_face_beg_row
11506 || (vpos == dpyinfo->mouse_face_beg_row
11507 && hpos >= dpyinfo->mouse_face_beg_col))
11508 && (vpos < dpyinfo->mouse_face_end_row
11509 || (vpos == dpyinfo->mouse_face_end_row
11510 && hpos < dpyinfo->mouse_face_end_col))
11511 /* Don't redraw the cursor's spot in mouse face if it is at the
11512 end of a line (on a newline). The cursor appears there, but
11513 mouse highlighting does not. */
11514 && cursor_row->used[TEXT_AREA] > hpos)
11515 mouse_face_here_p = 1;
11516
11517 /* Maybe clear the display under the cursor. */
11518 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
11519 {
11520 int x;
11521 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11522
11523 cursor_glyph = get_phys_cursor_glyph (w);
11524 if (cursor_glyph == NULL)
11525 goto mark_cursor_off;
11526
11527 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11528
11529 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11530 x,
11531 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
11532 cursor_row->y)),
11533 cursor_glyph->pixel_width,
11534 cursor_row->visible_height,
11535 False);
11536 }
11537
11538 /* Erase the cursor by redrawing the character underneath it. */
11539 if (mouse_face_here_p)
11540 hl = DRAW_MOUSE_FACE;
11541 else
11542 hl = DRAW_NORMAL_TEXT;
11543 x_draw_phys_cursor_glyph (w, cursor_row, hl);
11544
11545 mark_cursor_off:
11546 w->phys_cursor_on_p = 0;
11547 w->phys_cursor_type = NO_CURSOR;
11548 }
11549
11550
11551 /* Non-zero if physical cursor of window W is within mouse face. */
11552
11553 static int
11554 cursor_in_mouse_face_p (w)
11555 struct window *w;
11556 {
11557 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
11558 int in_mouse_face = 0;
11559
11560 if (WINDOWP (dpyinfo->mouse_face_window)
11561 && XWINDOW (dpyinfo->mouse_face_window) == w)
11562 {
11563 int hpos = w->phys_cursor.hpos;
11564 int vpos = w->phys_cursor.vpos;
11565
11566 if (vpos >= dpyinfo->mouse_face_beg_row
11567 && vpos <= dpyinfo->mouse_face_end_row
11568 && (vpos > dpyinfo->mouse_face_beg_row
11569 || hpos >= dpyinfo->mouse_face_beg_col)
11570 && (vpos < dpyinfo->mouse_face_end_row
11571 || hpos < dpyinfo->mouse_face_end_col
11572 || dpyinfo->mouse_face_past_end))
11573 in_mouse_face = 1;
11574 }
11575
11576 return in_mouse_face;
11577 }
11578
11579
11580 /* Display or clear cursor of window W. If ON is zero, clear the
11581 cursor. If it is non-zero, display the cursor. If ON is nonzero,
11582 where to put the cursor is specified by HPOS, VPOS, X and Y. */
11583
11584 void
11585 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
11586 struct window *w;
11587 int on, hpos, vpos, x, y;
11588 {
11589 struct frame *f = XFRAME (w->frame);
11590 int new_cursor_type;
11591 int new_cursor_width;
11592 struct glyph_matrix *current_glyphs;
11593 struct glyph_row *glyph_row;
11594 struct glyph *glyph;
11595 int cursor_non_selected;
11596
11597 /* This is pointless on invisible frames, and dangerous on garbaged
11598 windows and frames; in the latter case, the frame or window may
11599 be in the midst of changing its size, and x and y may be off the
11600 window. */
11601 if (! FRAME_VISIBLE_P (f)
11602 || FRAME_GARBAGED_P (f)
11603 || vpos >= w->current_matrix->nrows
11604 || hpos >= w->current_matrix->matrix_w)
11605 return;
11606
11607 /* If cursor is off and we want it off, return quickly. */
11608 if (!on && !w->phys_cursor_on_p)
11609 return;
11610
11611 current_glyphs = w->current_matrix;
11612 glyph_row = MATRIX_ROW (current_glyphs, vpos);
11613 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
11614
11615 /* If cursor row is not enabled, we don't really know where to
11616 display the cursor. */
11617 if (!glyph_row->enabled_p)
11618 {
11619 w->phys_cursor_on_p = 0;
11620 return;
11621 }
11622
11623 xassert (interrupt_input_blocked);
11624
11625 /* Set new_cursor_type to the cursor we want to be displayed. In a
11626 mini-buffer window, we want the cursor only to appear if we are
11627 reading input from this window. For the selected window, we want
11628 the cursor type given by the frame parameter. If explicitly
11629 marked off, draw no cursor. In all other cases, we want a hollow
11630 box cursor. */
11631 cursor_non_selected
11632 = !NILP (Fbuffer_local_value (Qcursor_in_non_selected_windows,
11633 w->buffer));
11634 new_cursor_width = -1;
11635 if (cursor_in_echo_area
11636 && FRAME_HAS_MINIBUF_P (f)
11637 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
11638 {
11639 if (w == XWINDOW (echo_area_window))
11640 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11641 else if (cursor_non_selected)
11642 new_cursor_type = HOLLOW_BOX_CURSOR;
11643 else
11644 new_cursor_type = NO_CURSOR;
11645 }
11646 else
11647 {
11648 if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
11649 || w != XWINDOW (f->selected_window))
11650 {
11651 if ((MINI_WINDOW_P (w) && minibuf_level == 0)
11652 || !cursor_non_selected
11653 || NILP (XBUFFER (w->buffer)->cursor_type))
11654 new_cursor_type = NO_CURSOR;
11655 else
11656 new_cursor_type = HOLLOW_BOX_CURSOR;
11657 }
11658 else
11659 {
11660 struct buffer *b = XBUFFER (w->buffer);
11661
11662 if (EQ (b->cursor_type, Qt))
11663 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11664 else
11665 new_cursor_type = x_specified_cursor_type (b->cursor_type,
11666 &new_cursor_width);
11667 if (w->cursor_off_p)
11668 {
11669 if (new_cursor_type == FILLED_BOX_CURSOR)
11670 new_cursor_type = HOLLOW_BOX_CURSOR;
11671 else if (new_cursor_type == BAR_CURSOR && new_cursor_width > 1)
11672 new_cursor_width = 1;
11673 else
11674 new_cursor_type = NO_CURSOR;
11675 }
11676 }
11677 }
11678
11679 /* If cursor is currently being shown and we don't want it to be or
11680 it is in the wrong place, or the cursor type is not what we want,
11681 erase it. */
11682 if (w->phys_cursor_on_p
11683 && (!on
11684 || w->phys_cursor.x != x
11685 || w->phys_cursor.y != y
11686 || new_cursor_type != w->phys_cursor_type
11687 || (new_cursor_type == BAR_CURSOR
11688 && new_cursor_width != w->phys_cursor_width)))
11689 x_erase_phys_cursor (w);
11690
11691 /* If the cursor is now invisible and we want it to be visible,
11692 display it. */
11693 if (on && !w->phys_cursor_on_p)
11694 {
11695 w->phys_cursor_ascent = glyph_row->ascent;
11696 w->phys_cursor_height = glyph_row->height;
11697
11698 /* Set phys_cursor_.* before x_draw_.* is called because some
11699 of them may need the information. */
11700 w->phys_cursor.x = x;
11701 w->phys_cursor.y = glyph_row->y;
11702 w->phys_cursor.hpos = hpos;
11703 w->phys_cursor.vpos = vpos;
11704 w->phys_cursor_type = new_cursor_type;
11705 w->phys_cursor_width = new_cursor_width;
11706 w->phys_cursor_on_p = 1;
11707
11708 switch (new_cursor_type)
11709 {
11710 case HOLLOW_BOX_CURSOR:
11711 x_draw_hollow_cursor (w, glyph_row);
11712 break;
11713
11714 case FILLED_BOX_CURSOR:
11715 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
11716 break;
11717
11718 case BAR_CURSOR:
11719 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
11720 break;
11721
11722 case NO_CURSOR:
11723 break;
11724
11725 default:
11726 abort ();
11727 }
11728
11729 #ifdef HAVE_X_I18N
11730 if (w == XWINDOW (f->selected_window))
11731 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
11732 xic_set_preeditarea (w, x, y);
11733 #endif
11734 }
11735
11736 #ifndef XFlush
11737 if (updating_frame != f)
11738 XFlush (FRAME_X_DISPLAY (f));
11739 #endif
11740 }
11741
11742
11743 /* Display the cursor on window W, or clear it. X and Y are window
11744 relative pixel coordinates. HPOS and VPOS are glyph matrix
11745 positions. If W is not the selected window, display a hollow
11746 cursor. ON non-zero means display the cursor at X, Y which
11747 correspond to HPOS, VPOS, otherwise it is cleared. */
11748
11749 void
11750 x_display_cursor (w, on, hpos, vpos, x, y)
11751 struct window *w;
11752 int on, hpos, vpos, x, y;
11753 {
11754 BLOCK_INPUT;
11755 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
11756 UNBLOCK_INPUT;
11757 }
11758
11759
11760 /* Display the cursor on window W, or clear it, according to ON_P.
11761 Don't change the cursor's position. */
11762
11763 void
11764 x_update_cursor (f, on_p)
11765 struct frame *f;
11766 {
11767 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11768 }
11769
11770
11771 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
11772 in the window tree rooted at W. */
11773
11774 static void
11775 x_update_cursor_in_window_tree (w, on_p)
11776 struct window *w;
11777 int on_p;
11778 {
11779 while (w)
11780 {
11781 if (!NILP (w->hchild))
11782 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11783 else if (!NILP (w->vchild))
11784 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11785 else
11786 x_update_window_cursor (w, on_p);
11787
11788 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11789 }
11790 }
11791
11792
11793 /* Switch the display of W's cursor on or off, according to the value
11794 of ON. */
11795
11796 static void
11797 x_update_window_cursor (w, on)
11798 struct window *w;
11799 int on;
11800 {
11801 /* Don't update cursor in windows whose frame is in the process
11802 of being deleted. */
11803 if (w->current_matrix)
11804 {
11805 BLOCK_INPUT;
11806 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11807 w->phys_cursor.x, w->phys_cursor.y);
11808 UNBLOCK_INPUT;
11809 }
11810 }
11811
11812
11813
11814 \f
11815 /* Icons. */
11816
11817 /* Make the x-window of frame F use the gnu icon bitmap. */
11818
11819 int
11820 x_bitmap_icon (f, file)
11821 struct frame *f;
11822 Lisp_Object file;
11823 {
11824 int bitmap_id;
11825
11826 if (FRAME_X_WINDOW (f) == 0)
11827 return 1;
11828
11829 /* Free up our existing icon bitmap if any. */
11830 if (f->output_data.x->icon_bitmap > 0)
11831 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11832 f->output_data.x->icon_bitmap = 0;
11833
11834 if (STRINGP (file))
11835 bitmap_id = x_create_bitmap_from_file (f, file);
11836 else
11837 {
11838 /* Create the GNU bitmap if necessary. */
11839 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
11840 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11841 = x_create_bitmap_from_data (f, gnu_bits,
11842 gnu_width, gnu_height);
11843
11844 /* The first time we create the GNU bitmap,
11845 this increments the ref-count one extra time.
11846 As a result, the GNU bitmap is never freed.
11847 That way, we don't have to worry about allocating it again. */
11848 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
11849
11850 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
11851 }
11852
11853 x_wm_set_icon_pixmap (f, bitmap_id);
11854 f->output_data.x->icon_bitmap = bitmap_id;
11855
11856 return 0;
11857 }
11858
11859
11860 /* Make the x-window of frame F use a rectangle with text.
11861 Use ICON_NAME as the text. */
11862
11863 int
11864 x_text_icon (f, icon_name)
11865 struct frame *f;
11866 char *icon_name;
11867 {
11868 if (FRAME_X_WINDOW (f) == 0)
11869 return 1;
11870
11871 #ifdef HAVE_X11R4
11872 {
11873 XTextProperty text;
11874 text.value = (unsigned char *) icon_name;
11875 text.encoding = XA_STRING;
11876 text.format = 8;
11877 text.nitems = strlen (icon_name);
11878 #ifdef USE_X_TOOLKIT
11879 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11880 &text);
11881 #else /* not USE_X_TOOLKIT */
11882 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11883 #endif /* not USE_X_TOOLKIT */
11884 }
11885 #else /* not HAVE_X11R4 */
11886 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11887 #endif /* not HAVE_X11R4 */
11888
11889 if (f->output_data.x->icon_bitmap > 0)
11890 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11891 f->output_data.x->icon_bitmap = 0;
11892 x_wm_set_icon_pixmap (f, 0);
11893
11894 return 0;
11895 }
11896 \f
11897 #define X_ERROR_MESSAGE_SIZE 200
11898
11899 /* If non-nil, this should be a string.
11900 It means catch X errors and store the error message in this string. */
11901
11902 static Lisp_Object x_error_message_string;
11903
11904 /* An X error handler which stores the error message in
11905 x_error_message_string. This is called from x_error_handler if
11906 x_catch_errors is in effect. */
11907
11908 static void
11909 x_error_catcher (display, error)
11910 Display *display;
11911 XErrorEvent *error;
11912 {
11913 XGetErrorText (display, error->error_code,
11914 XSTRING (x_error_message_string)->data,
11915 X_ERROR_MESSAGE_SIZE);
11916 }
11917
11918 /* Begin trapping X errors for display DPY. Actually we trap X errors
11919 for all displays, but DPY should be the display you are actually
11920 operating on.
11921
11922 After calling this function, X protocol errors no longer cause
11923 Emacs to exit; instead, they are recorded in the string
11924 stored in x_error_message_string.
11925
11926 Calling x_check_errors signals an Emacs error if an X error has
11927 occurred since the last call to x_catch_errors or x_check_errors.
11928
11929 Calling x_uncatch_errors resumes the normal error handling. */
11930
11931 void x_check_errors ();
11932 static Lisp_Object x_catch_errors_unwind ();
11933
11934 int
11935 x_catch_errors (dpy)
11936 Display *dpy;
11937 {
11938 int count = specpdl_ptr - specpdl;
11939
11940 /* Make sure any errors from previous requests have been dealt with. */
11941 XSync (dpy, False);
11942
11943 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11944
11945 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11946 XSTRING (x_error_message_string)->data[0] = 0;
11947
11948 return count;
11949 }
11950
11951 /* Unbind the binding that we made to check for X errors. */
11952
11953 static Lisp_Object
11954 x_catch_errors_unwind (old_val)
11955 Lisp_Object old_val;
11956 {
11957 x_error_message_string = old_val;
11958 return Qnil;
11959 }
11960
11961 /* If any X protocol errors have arrived since the last call to
11962 x_catch_errors or x_check_errors, signal an Emacs error using
11963 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11964
11965 void
11966 x_check_errors (dpy, format)
11967 Display *dpy;
11968 char *format;
11969 {
11970 /* Make sure to catch any errors incurred so far. */
11971 XSync (dpy, False);
11972
11973 if (XSTRING (x_error_message_string)->data[0])
11974 error (format, XSTRING (x_error_message_string)->data);
11975 }
11976
11977 /* Nonzero if we had any X protocol errors
11978 since we did x_catch_errors on DPY. */
11979
11980 int
11981 x_had_errors_p (dpy)
11982 Display *dpy;
11983 {
11984 /* Make sure to catch any errors incurred so far. */
11985 XSync (dpy, False);
11986
11987 return XSTRING (x_error_message_string)->data[0] != 0;
11988 }
11989
11990 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11991
11992 void
11993 x_clear_errors (dpy)
11994 Display *dpy;
11995 {
11996 XSTRING (x_error_message_string)->data[0] = 0;
11997 }
11998
11999 /* Stop catching X protocol errors and let them make Emacs die.
12000 DPY should be the display that was passed to x_catch_errors.
12001 COUNT should be the value that was returned by
12002 the corresponding call to x_catch_errors. */
12003
12004 void
12005 x_uncatch_errors (dpy, count)
12006 Display *dpy;
12007 int count;
12008 {
12009 unbind_to (count, Qnil);
12010 }
12011
12012 #if 0
12013 static unsigned int x_wire_count;
12014 x_trace_wire ()
12015 {
12016 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
12017 }
12018 #endif /* ! 0 */
12019
12020 \f
12021 /* Handle SIGPIPE, which can happen when the connection to a server
12022 simply goes away. SIGPIPE is handled by x_connection_signal.
12023 Don't need to do anything, because the write which caused the
12024 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
12025 which will do the appropriate cleanup for us. */
12026
12027 static SIGTYPE
12028 x_connection_signal (signalnum) /* If we don't have an argument, */
12029 int signalnum; /* some compilers complain in signal calls. */
12030 {
12031 #ifdef USG
12032 /* USG systems forget handlers when they are used;
12033 must reestablish each time */
12034 signal (signalnum, x_connection_signal);
12035 #endif /* USG */
12036 }
12037
12038 \f
12039 /************************************************************************
12040 Handling X errors
12041 ************************************************************************/
12042
12043 /* Error message passed to x_connection_closed. */
12044
12045 static char *error_msg;
12046
12047 /* Function installed as fatal_error_signal_hook in
12048 x_connection_closed. Print the X error message, and exit normally,
12049 instead of dumping core when XtCloseDisplay fails. */
12050
12051 static void
12052 x_fatal_error_signal ()
12053 {
12054 fprintf (stderr, "%s\n", error_msg);
12055 exit (70);
12056 }
12057
12058 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
12059 the text of an error message that lead to the connection loss. */
12060
12061 static SIGTYPE
12062 x_connection_closed (dpy, error_message)
12063 Display *dpy;
12064 char *error_message;
12065 {
12066 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
12067 Lisp_Object frame, tail;
12068 int count;
12069
12070 error_msg = (char *) alloca (strlen (error_message) + 1);
12071 strcpy (error_msg, error_message);
12072 handling_signal = 0;
12073
12074 /* Prevent being called recursively because of an error condition
12075 below. Otherwise, we might end up with printing ``can't find per
12076 display information'' in the recursive call instead of printing
12077 the original message here. */
12078 count = x_catch_errors (dpy);
12079
12080 /* We have to close the display to inform Xt that it doesn't
12081 exist anymore. If we don't, Xt will continue to wait for
12082 events from the display. As a consequence, a sequence of
12083
12084 M-x make-frame-on-display RET :1 RET
12085 ...kill the new frame, so that we get an IO error...
12086 M-x make-frame-on-display RET :1 RET
12087
12088 will indefinitely wait in Xt for events for display `:1', opened
12089 in the first class to make-frame-on-display.
12090
12091 Closing the display is reported to lead to a bus error on
12092 OpenWindows in certain situations. I suspect that is a bug
12093 in OpenWindows. I don't know how to cicumvent it here. */
12094
12095 #ifdef USE_X_TOOLKIT
12096 /* If DPYINFO is null, this means we didn't open the display
12097 in the first place, so don't try to close it. */
12098 if (dpyinfo)
12099 {
12100 extern void (*fatal_error_signal_hook) P_ ((void));
12101 fatal_error_signal_hook = x_fatal_error_signal;
12102 XtCloseDisplay (dpy);
12103 fatal_error_signal_hook = NULL;
12104 }
12105 #endif
12106
12107 /* Indicate that this display is dead. */
12108 if (dpyinfo)
12109 dpyinfo->display = 0;
12110
12111 /* First delete frames whose mini-buffers are on frames
12112 that are on the dead display. */
12113 FOR_EACH_FRAME (tail, frame)
12114 {
12115 Lisp_Object minibuf_frame;
12116 minibuf_frame
12117 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
12118 if (FRAME_X_P (XFRAME (frame))
12119 && FRAME_X_P (XFRAME (minibuf_frame))
12120 && ! EQ (frame, minibuf_frame)
12121 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
12122 Fdelete_frame (frame, Qt);
12123 }
12124
12125 /* Now delete all remaining frames on the dead display.
12126 We are now sure none of these is used as the mini-buffer
12127 for another frame that we need to delete. */
12128 FOR_EACH_FRAME (tail, frame)
12129 if (FRAME_X_P (XFRAME (frame))
12130 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
12131 {
12132 /* Set this to t so that Fdelete_frame won't get confused
12133 trying to find a replacement. */
12134 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
12135 Fdelete_frame (frame, Qt);
12136 }
12137
12138 if (dpyinfo)
12139 x_delete_display (dpyinfo);
12140
12141 x_uncatch_errors (dpy, count);
12142
12143 if (x_display_list == 0)
12144 {
12145 fprintf (stderr, "%s\n", error_msg);
12146 shut_down_emacs (0, 0, Qnil);
12147 exit (70);
12148 }
12149
12150 /* Ordinary stack unwind doesn't deal with these. */
12151 #ifdef SIGIO
12152 sigunblock (sigmask (SIGIO));
12153 #endif
12154 sigunblock (sigmask (SIGALRM));
12155 TOTALLY_UNBLOCK_INPUT;
12156
12157 clear_waiting_for_input ();
12158 error ("%s", error_msg);
12159 }
12160
12161
12162 /* This is the usual handler for X protocol errors.
12163 It kills all frames on the display that we got the error for.
12164 If that was the only one, it prints an error message and kills Emacs. */
12165
12166 static void
12167 x_error_quitter (display, error)
12168 Display *display;
12169 XErrorEvent *error;
12170 {
12171 char buf[256], buf1[356];
12172
12173 /* Note that there is no real way portable across R3/R4 to get the
12174 original error handler. */
12175
12176 XGetErrorText (display, error->error_code, buf, sizeof (buf));
12177 sprintf (buf1, "X protocol error: %s on protocol request %d",
12178 buf, error->request_code);
12179 x_connection_closed (display, buf1);
12180 }
12181
12182
12183 /* This is the first-level handler for X protocol errors.
12184 It calls x_error_quitter or x_error_catcher. */
12185
12186 static int
12187 x_error_handler (display, error)
12188 Display *display;
12189 XErrorEvent *error;
12190 {
12191 if (! NILP (x_error_message_string))
12192 x_error_catcher (display, error);
12193 else
12194 x_error_quitter (display, error);
12195 return 0;
12196 }
12197
12198 /* This is the handler for X IO errors, always.
12199 It kills all frames on the display that we lost touch with.
12200 If that was the only one, it prints an error message and kills Emacs. */
12201
12202 static int
12203 x_io_error_quitter (display)
12204 Display *display;
12205 {
12206 char buf[256];
12207
12208 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
12209 x_connection_closed (display, buf);
12210 return 0;
12211 }
12212 \f
12213 /* Changing the font of the frame. */
12214
12215 /* Give frame F the font named FONTNAME as its default font, and
12216 return the full name of that font. FONTNAME may be a wildcard
12217 pattern; in that case, we choose some font that fits the pattern.
12218 The return value shows which font we chose. */
12219
12220 Lisp_Object
12221 x_new_font (f, fontname)
12222 struct frame *f;
12223 register char *fontname;
12224 {
12225 struct font_info *fontp
12226 = FS_LOAD_FONT (f, 0, fontname, -1);
12227
12228 if (!fontp)
12229 return Qnil;
12230
12231 f->output_data.x->font = (XFontStruct *) (fontp->font);
12232 f->output_data.x->baseline_offset = fontp->baseline_offset;
12233 f->output_data.x->fontset = -1;
12234
12235 x_compute_fringe_widths (f, 1);
12236
12237 /* Compute the scroll bar width in character columns. */
12238 if (f->scroll_bar_pixel_width > 0)
12239 {
12240 int wid = FONT_WIDTH (f->output_data.x->font);
12241 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
12242 }
12243 else
12244 {
12245 int wid = FONT_WIDTH (f->output_data.x->font);
12246 f->scroll_bar_cols = (14 + wid - 1) / wid;
12247 }
12248
12249 /* Now make the frame display the given font. */
12250 if (FRAME_X_WINDOW (f) != 0)
12251 {
12252 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
12253 f->output_data.x->font->fid);
12254 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
12255 f->output_data.x->font->fid);
12256 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
12257 f->output_data.x->font->fid);
12258
12259 frame_update_line_height (f);
12260
12261 /* Don't change the size of a tip frame; there's no point in
12262 doing it because it's done in Fx_show_tip, and it leads to
12263 problems because the tip frame has no widget. */
12264 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
12265 x_set_window_size (f, 0, f->width, f->height);
12266 }
12267 else
12268 /* If we are setting a new frame's font for the first time,
12269 there are no faces yet, so this font's height is the line height. */
12270 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
12271
12272 return build_string (fontp->full_name);
12273 }
12274
12275 /* Give frame F the fontset named FONTSETNAME as its default font, and
12276 return the full name of that fontset. FONTSETNAME may be a wildcard
12277 pattern; in that case, we choose some fontset that fits the pattern.
12278 The return value shows which fontset we chose. */
12279
12280 Lisp_Object
12281 x_new_fontset (f, fontsetname)
12282 struct frame *f;
12283 char *fontsetname;
12284 {
12285 int fontset = fs_query_fontset (build_string (fontsetname), 0);
12286 Lisp_Object result;
12287
12288 if (fontset < 0)
12289 return Qnil;
12290
12291 if (f->output_data.x->fontset == fontset)
12292 /* This fontset is already set in frame F. There's nothing more
12293 to do. */
12294 return fontset_name (fontset);
12295
12296 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
12297
12298 if (!STRINGP (result))
12299 /* Can't load ASCII font. */
12300 return Qnil;
12301
12302 /* Since x_new_font doesn't update any fontset information, do it now. */
12303 f->output_data.x->fontset = fontset;
12304
12305 #ifdef HAVE_X_I18N
12306 if (FRAME_XIC (f)
12307 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
12308 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
12309 #endif
12310
12311 return build_string (fontsetname);
12312 }
12313
12314 /* Compute actual fringe widths */
12315
12316 void
12317 x_compute_fringe_widths (f, redraw)
12318 struct frame *f;
12319 int redraw;
12320 {
12321 int o_left = f->output_data.x->left_fringe_width;
12322 int o_right = f->output_data.x->right_fringe_width;
12323 int o_cols = f->output_data.x->fringe_cols;
12324
12325 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
12326 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
12327 int left_fringe_width, right_fringe_width;
12328
12329 if (!NILP (left_fringe))
12330 left_fringe = Fcdr (left_fringe);
12331 if (!NILP (right_fringe))
12332 right_fringe = Fcdr (right_fringe);
12333
12334 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
12335 XINT (left_fringe));
12336 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
12337 XINT (right_fringe));
12338
12339 if (left_fringe_width || right_fringe_width)
12340 {
12341 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
12342 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
12343 int conf_wid = left_wid + right_wid;
12344 int font_wid = FONT_WIDTH (f->output_data.x->font);
12345 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
12346 int real_wid = cols * font_wid;
12347 if (left_wid && right_wid)
12348 {
12349 if (left_fringe_width < 0)
12350 {
12351 /* Left fringe width is fixed, adjust right fringe if necessary */
12352 f->output_data.x->left_fringe_width = left_wid;
12353 f->output_data.x->right_fringe_width = real_wid - left_wid;
12354 }
12355 else if (right_fringe_width < 0)
12356 {
12357 /* Right fringe width is fixed, adjust left fringe if necessary */
12358 f->output_data.x->left_fringe_width = real_wid - right_wid;
12359 f->output_data.x->right_fringe_width = right_wid;
12360 }
12361 else
12362 {
12363 /* Adjust both fringes with an equal amount.
12364 Note that we are doing integer arithmetic here, so don't
12365 lose a pixel if the total width is an odd number. */
12366 int fill = real_wid - conf_wid;
12367 f->output_data.x->left_fringe_width = left_wid + fill/2;
12368 f->output_data.x->right_fringe_width = right_wid + fill - fill/2;
12369 }
12370 }
12371 else if (left_fringe_width)
12372 {
12373 f->output_data.x->left_fringe_width = real_wid;
12374 f->output_data.x->right_fringe_width = 0;
12375 }
12376 else
12377 {
12378 f->output_data.x->left_fringe_width = 0;
12379 f->output_data.x->right_fringe_width = real_wid;
12380 }
12381 f->output_data.x->fringe_cols = cols;
12382 f->output_data.x->fringes_extra = real_wid;
12383 }
12384 else
12385 {
12386 f->output_data.x->left_fringe_width = 0;
12387 f->output_data.x->right_fringe_width = 0;
12388 f->output_data.x->fringe_cols = 0;
12389 f->output_data.x->fringes_extra = 0;
12390 }
12391
12392 if (redraw && FRAME_VISIBLE_P (f))
12393 if (o_left != f->output_data.x->left_fringe_width ||
12394 o_right != f->output_data.x->right_fringe_width ||
12395 o_cols != f->output_data.x->fringe_cols)
12396 redraw_frame (f);
12397 }
12398 \f
12399 /***********************************************************************
12400 X Input Methods
12401 ***********************************************************************/
12402
12403 #ifdef HAVE_X_I18N
12404
12405 #ifdef HAVE_X11R6
12406
12407 /* XIM destroy callback function, which is called whenever the
12408 connection to input method XIM dies. CLIENT_DATA contains a
12409 pointer to the x_display_info structure corresponding to XIM. */
12410
12411 static void
12412 xim_destroy_callback (xim, client_data, call_data)
12413 XIM xim;
12414 XPointer client_data;
12415 XPointer call_data;
12416 {
12417 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
12418 Lisp_Object frame, tail;
12419
12420 BLOCK_INPUT;
12421
12422 /* No need to call XDestroyIC.. */
12423 FOR_EACH_FRAME (tail, frame)
12424 {
12425 struct frame *f = XFRAME (frame);
12426 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
12427 {
12428 FRAME_XIC (f) = NULL;
12429 if (FRAME_XIC_FONTSET (f))
12430 {
12431 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
12432 FRAME_XIC_FONTSET (f) = NULL;
12433 }
12434 }
12435 }
12436
12437 /* No need to call XCloseIM. */
12438 dpyinfo->xim = NULL;
12439 XFree (dpyinfo->xim_styles);
12440 UNBLOCK_INPUT;
12441 }
12442
12443 #endif /* HAVE_X11R6 */
12444
12445 /* Open the connection to the XIM server on display DPYINFO.
12446 RESOURCE_NAME is the resource name Emacs uses. */
12447
12448 static void
12449 xim_open_dpy (dpyinfo, resource_name)
12450 struct x_display_info *dpyinfo;
12451 char *resource_name;
12452 {
12453 #ifdef USE_XIM
12454 XIM xim;
12455
12456 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
12457 dpyinfo->xim = xim;
12458
12459 if (xim)
12460 {
12461 #ifdef HAVE_X11R6
12462 XIMCallback destroy;
12463 #endif
12464
12465 /* Get supported styles and XIM values. */
12466 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
12467
12468 #ifdef HAVE_X11R6
12469 destroy.callback = xim_destroy_callback;
12470 destroy.client_data = (XPointer)dpyinfo;
12471 /* This isn't prototyped in OSF 5.0. */
12472 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
12473 #endif
12474 }
12475
12476 #else /* not USE_XIM */
12477 dpyinfo->xim = NULL;
12478 #endif /* not USE_XIM */
12479 }
12480
12481
12482 #ifdef HAVE_X11R6_XIM
12483
12484 struct xim_inst_t
12485 {
12486 struct x_display_info *dpyinfo;
12487 char *resource_name;
12488 };
12489
12490 /* XIM instantiate callback function, which is called whenever an XIM
12491 server is available. DISPLAY is teh display of the XIM.
12492 CLIENT_DATA contains a pointer to an xim_inst_t structure created
12493 when the callback was registered. */
12494
12495 static void
12496 xim_instantiate_callback (display, client_data, call_data)
12497 Display *display;
12498 XPointer client_data;
12499 XPointer call_data;
12500 {
12501 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
12502 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
12503
12504 /* We don't support multiple XIM connections. */
12505 if (dpyinfo->xim)
12506 return;
12507
12508 xim_open_dpy (dpyinfo, xim_inst->resource_name);
12509
12510 /* Create XIC for the existing frames on the same display, as long
12511 as they have no XIC. */
12512 if (dpyinfo->xim && dpyinfo->reference_count > 0)
12513 {
12514 Lisp_Object tail, frame;
12515
12516 BLOCK_INPUT;
12517 FOR_EACH_FRAME (tail, frame)
12518 {
12519 struct frame *f = XFRAME (frame);
12520
12521 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
12522 if (FRAME_XIC (f) == NULL)
12523 {
12524 create_frame_xic (f);
12525 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
12526 xic_set_statusarea (f);
12527 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
12528 {
12529 struct window *w = XWINDOW (f->selected_window);
12530 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
12531 }
12532 }
12533 }
12534
12535 UNBLOCK_INPUT;
12536 }
12537 }
12538
12539 #endif /* HAVE_X11R6_XIM */
12540
12541
12542 /* Open a connection to the XIM server on display DPYINFO.
12543 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
12544 connection only at the first time. On X11R6, open the connection
12545 in the XIM instantiate callback function. */
12546
12547 static void
12548 xim_initialize (dpyinfo, resource_name)
12549 struct x_display_info *dpyinfo;
12550 char *resource_name;
12551 {
12552 #ifdef USE_XIM
12553 #ifdef HAVE_X11R6_XIM
12554 struct xim_inst_t *xim_inst;
12555 int len;
12556
12557 dpyinfo->xim = NULL;
12558 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
12559 xim_inst->dpyinfo = dpyinfo;
12560 len = strlen (resource_name);
12561 xim_inst->resource_name = (char *) xmalloc (len + 1);
12562 bcopy (resource_name, xim_inst->resource_name, len + 1);
12563 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12564 resource_name, EMACS_CLASS,
12565 xim_instantiate_callback,
12566 /* Fixme: This is XPointer in
12567 XFree86 but (XPointer *) on
12568 Tru64, at least. */
12569 (XPointer) xim_inst);
12570 #else /* not HAVE_X11R6_XIM */
12571 dpyinfo->xim = NULL;
12572 xim_open_dpy (dpyinfo, resource_name);
12573 #endif /* not HAVE_X11R6_XIM */
12574
12575 #else /* not USE_XIM */
12576 dpyinfo->xim = NULL;
12577 #endif /* not USE_XIM */
12578 }
12579
12580
12581 /* Close the connection to the XIM server on display DPYINFO. */
12582
12583 static void
12584 xim_close_dpy (dpyinfo)
12585 struct x_display_info *dpyinfo;
12586 {
12587 #ifdef USE_XIM
12588 #ifdef HAVE_X11R6_XIM
12589 if (dpyinfo->display)
12590 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12591 NULL, EMACS_CLASS,
12592 xim_instantiate_callback, NULL);
12593 #endif /* not HAVE_X11R6_XIM */
12594 if (dpyinfo->display)
12595 XCloseIM (dpyinfo->xim);
12596 dpyinfo->xim = NULL;
12597 XFree (dpyinfo->xim_styles);
12598 #endif /* USE_XIM */
12599 }
12600
12601 #endif /* not HAVE_X11R6_XIM */
12602
12603
12604 \f
12605 /* Calculate the absolute position in frame F
12606 from its current recorded position values and gravity. */
12607
12608 void
12609 x_calc_absolute_position (f)
12610 struct frame *f;
12611 {
12612 Window child;
12613 int win_x = 0, win_y = 0;
12614 int flags = f->output_data.x->size_hint_flags;
12615 int this_window;
12616
12617 /* We have nothing to do if the current position
12618 is already for the top-left corner. */
12619 if (! ((flags & XNegative) || (flags & YNegative)))
12620 return;
12621
12622 #ifdef USE_X_TOOLKIT
12623 this_window = XtWindow (f->output_data.x->widget);
12624 #else
12625 this_window = FRAME_X_WINDOW (f);
12626 #endif
12627
12628 /* Find the position of the outside upper-left corner of
12629 the inner window, with respect to the outer window.
12630 But do this only if we will need the results. */
12631 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
12632 {
12633 int count;
12634
12635 BLOCK_INPUT;
12636 count = x_catch_errors (FRAME_X_DISPLAY (f));
12637 while (1)
12638 {
12639 x_clear_errors (FRAME_X_DISPLAY (f));
12640 XTranslateCoordinates (FRAME_X_DISPLAY (f),
12641
12642 /* From-window, to-window. */
12643 this_window,
12644 f->output_data.x->parent_desc,
12645
12646 /* From-position, to-position. */
12647 0, 0, &win_x, &win_y,
12648
12649 /* Child of win. */
12650 &child);
12651 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12652 {
12653 Window newroot, newparent = 0xdeadbeef;
12654 Window *newchildren;
12655 unsigned int nchildren;
12656
12657 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
12658 &newparent, &newchildren, &nchildren))
12659 break;
12660
12661 XFree ((char *) newchildren);
12662
12663 f->output_data.x->parent_desc = newparent;
12664 }
12665 else
12666 break;
12667 }
12668
12669 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12670 UNBLOCK_INPUT;
12671 }
12672
12673 /* Treat negative positions as relative to the leftmost bottommost
12674 position that fits on the screen. */
12675 if (flags & XNegative)
12676 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
12677 - 2 * f->output_data.x->border_width - win_x
12678 - PIXEL_WIDTH (f)
12679 + f->output_data.x->left_pos);
12680
12681 {
12682 int height = PIXEL_HEIGHT (f);
12683
12684 #if defined USE_X_TOOLKIT && defined USE_MOTIF
12685 /* Something is fishy here. When using Motif, starting Emacs with
12686 `-g -0-0', the frame appears too low by a few pixels.
12687
12688 This seems to be so because initially, while Emacs is starting,
12689 the column widget's height and the frame's pixel height are
12690 different. The column widget's height is the right one. In
12691 later invocations, when Emacs is up, the frame's pixel height
12692 is right, though.
12693
12694 It's not obvious where the initial small difference comes from.
12695 2000-12-01, gerd. */
12696
12697 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
12698 #endif
12699
12700 if (flags & YNegative)
12701 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
12702 - 2 * f->output_data.x->border_width
12703 - win_y
12704 - height
12705 + f->output_data.x->top_pos);
12706 }
12707
12708 /* The left_pos and top_pos
12709 are now relative to the top and left screen edges,
12710 so the flags should correspond. */
12711 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12712 }
12713
12714 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
12715 to really change the position, and 0 when calling from
12716 x_make_frame_visible (in that case, XOFF and YOFF are the current
12717 position values). It is -1 when calling from x_set_frame_parameters,
12718 which means, do adjust for borders but don't change the gravity. */
12719
12720 void
12721 x_set_offset (f, xoff, yoff, change_gravity)
12722 struct frame *f;
12723 register int xoff, yoff;
12724 int change_gravity;
12725 {
12726 int modified_top, modified_left;
12727
12728 if (change_gravity > 0)
12729 {
12730 f->output_data.x->top_pos = yoff;
12731 f->output_data.x->left_pos = xoff;
12732 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12733 if (xoff < 0)
12734 f->output_data.x->size_hint_flags |= XNegative;
12735 if (yoff < 0)
12736 f->output_data.x->size_hint_flags |= YNegative;
12737 f->output_data.x->win_gravity = NorthWestGravity;
12738 }
12739 x_calc_absolute_position (f);
12740
12741 BLOCK_INPUT;
12742 x_wm_set_size_hint (f, (long) 0, 0);
12743
12744 modified_left = f->output_data.x->left_pos;
12745 modified_top = f->output_data.x->top_pos;
12746 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
12747 this seems to be unnecessary and incorrect. rms, 4/17/97. */
12748 /* It is a mystery why we need to add the border_width here
12749 when the frame is already visible, but experiment says we do. */
12750 if (change_gravity != 0)
12751 {
12752 modified_left += f->output_data.x->border_width;
12753 modified_top += f->output_data.x->border_width;
12754 }
12755 #endif
12756
12757 #ifdef USE_X_TOOLKIT
12758 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
12759 modified_left, modified_top);
12760 #else /* not USE_X_TOOLKIT */
12761 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12762 modified_left, modified_top);
12763 #endif /* not USE_X_TOOLKIT */
12764 UNBLOCK_INPUT;
12765 }
12766
12767 /* Check if we need to resize the frame due to a fullscreen request.
12768 If so needed, resize the frame. */
12769 static void
12770 x_check_fullscreen (f)
12771 struct frame *f;
12772 {
12773 if (f->output_data.x->want_fullscreen & FULLSCREEN_BOTH)
12774 {
12775 int width, height, ign;
12776
12777 x_real_positions (f, &f->output_data.x->left_pos,
12778 &f->output_data.x->top_pos);
12779
12780 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
12781
12782 /* We do not need to move the window, it shall be taken care of
12783 when setting WM manager hints.
12784 If the frame is visible already, the position is checked by
12785 x_check_fullscreen_move. */
12786 if (f->width != width || f->height != height)
12787 {
12788 change_frame_size (f, height, width, 0, 1, 0);
12789 SET_FRAME_GARBAGED (f);
12790 cancel_mouse_face (f);
12791
12792 /* Wait for the change of frame size to occur */
12793 f->output_data.x->want_fullscreen |= FULLSCREEN_WAIT;
12794
12795 }
12796 }
12797 }
12798
12799 /* If frame parameters are set after the frame is mapped, we need to move
12800 the window. This is done in xfns.c.
12801 Some window managers moves the window to the right position, some
12802 moves the outer window manager window to the specified position.
12803 Here we check that we are in the right spot. If not, make a second
12804 move, assuming we are dealing with the second kind of window manager. */
12805 static void
12806 x_check_fullscreen_move (f)
12807 struct frame *f;
12808 {
12809 if (f->output_data.x->want_fullscreen & FULLSCREEN_MOVE_WAIT)
12810 {
12811 int expect_top = f->output_data.x->top_pos;
12812 int expect_left = f->output_data.x->left_pos;
12813
12814 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
12815 expect_top = 0;
12816 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
12817 expect_left = 0;
12818
12819 if (expect_top != f->output_data.x->top_pos
12820 || expect_left != f->output_data.x->left_pos)
12821 x_set_offset (f, expect_left, expect_top, 1);
12822
12823 /* Just do this once */
12824 f->output_data.x->want_fullscreen &= ~FULLSCREEN_MOVE_WAIT;
12825 }
12826 }
12827
12828
12829 /* Calculate fullscreen size. Return in *TOP_POS and *LEFT_POS the
12830 wanted positions of the WM window (not emacs window).
12831 Return in *WIDTH and *HEIGHT the wanted width and height of Emacs
12832 window (FRAME_X_WINDOW).
12833 */
12834 void
12835 x_fullscreen_adjust (f, width, height, top_pos, left_pos)
12836 struct frame *f;
12837 int *width;
12838 int *height;
12839 int *top_pos;
12840 int *left_pos;
12841 {
12842 int newwidth = f->width, newheight = f->height;
12843
12844 *top_pos = f->output_data.x->top_pos;
12845 *left_pos = f->output_data.x->left_pos;
12846
12847 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
12848 {
12849 int ph;
12850
12851 ph = FRAME_X_DISPLAY_INFO (f)->height;
12852 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
12853 ph = CHAR_TO_PIXEL_HEIGHT (f, newheight)
12854 - f->output_data.x->y_pixels_diff;
12855 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
12856 *top_pos = 0;
12857 }
12858
12859 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
12860 {
12861 int pw;
12862
12863 pw = FRAME_X_DISPLAY_INFO (f)->width;
12864 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
12865 pw = CHAR_TO_PIXEL_WIDTH (f, newwidth)
12866 - f->output_data.x->x_pixels_diff;
12867 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
12868 *left_pos = 0;
12869 }
12870
12871 *width = newwidth;
12872 *height = newheight;
12873 }
12874
12875
12876 /* Change the size of frame F's X window to COLS/ROWS in the case F
12877 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
12878 top-left-corner window gravity for this size change and subsequent
12879 size changes. Otherwise we leave the window gravity unchanged. */
12880
12881 static void
12882 x_set_window_size_1 (f, change_gravity, cols, rows)
12883 struct frame *f;
12884 int change_gravity;
12885 int cols, rows;
12886 {
12887 int pixelwidth, pixelheight;
12888
12889 check_frame_size (f, &rows, &cols);
12890 f->output_data.x->vertical_scroll_bar_extra
12891 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
12892 ? 0
12893 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
12894 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
12895 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
12896
12897 x_compute_fringe_widths (f, 0);
12898
12899 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
12900 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
12901
12902 f->output_data.x->win_gravity = NorthWestGravity;
12903 x_wm_set_size_hint (f, (long) 0, 0);
12904
12905 XSync (FRAME_X_DISPLAY (f), False);
12906 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12907 pixelwidth, pixelheight);
12908
12909 /* Now, strictly speaking, we can't be sure that this is accurate,
12910 but the window manager will get around to dealing with the size
12911 change request eventually, and we'll hear how it went when the
12912 ConfigureNotify event gets here.
12913
12914 We could just not bother storing any of this information here,
12915 and let the ConfigureNotify event set everything up, but that
12916 might be kind of confusing to the Lisp code, since size changes
12917 wouldn't be reported in the frame parameters until some random
12918 point in the future when the ConfigureNotify event arrives.
12919
12920 We pass 1 for DELAY since we can't run Lisp code inside of
12921 a BLOCK_INPUT. */
12922 change_frame_size (f, rows, cols, 0, 1, 0);
12923 PIXEL_WIDTH (f) = pixelwidth;
12924 PIXEL_HEIGHT (f) = pixelheight;
12925
12926 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
12927 receive in the ConfigureNotify event; if we get what we asked
12928 for, then the event won't cause the screen to become garbaged, so
12929 we have to make sure to do it here. */
12930 SET_FRAME_GARBAGED (f);
12931
12932 XFlush (FRAME_X_DISPLAY (f));
12933 }
12934
12935
12936 /* Call this to change the size of frame F's x-window.
12937 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
12938 for this size change and subsequent size changes.
12939 Otherwise we leave the window gravity unchanged. */
12940
12941 void
12942 x_set_window_size (f, change_gravity, cols, rows)
12943 struct frame *f;
12944 int change_gravity;
12945 int cols, rows;
12946 {
12947 BLOCK_INPUT;
12948
12949 #ifdef USE_X_TOOLKIT
12950
12951 if (f->output_data.x->widget != NULL)
12952 {
12953 /* The x and y position of the widget is clobbered by the
12954 call to XtSetValues within EmacsFrameSetCharSize.
12955 This is a real kludge, but I don't understand Xt so I can't
12956 figure out a correct fix. Can anyone else tell me? -- rms. */
12957 int xpos = f->output_data.x->widget->core.x;
12958 int ypos = f->output_data.x->widget->core.y;
12959 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
12960 f->output_data.x->widget->core.x = xpos;
12961 f->output_data.x->widget->core.y = ypos;
12962 }
12963 else
12964 x_set_window_size_1 (f, change_gravity, cols, rows);
12965
12966 #else /* not USE_X_TOOLKIT */
12967
12968 x_set_window_size_1 (f, change_gravity, cols, rows);
12969
12970 #endif /* not USE_X_TOOLKIT */
12971
12972 /* If cursor was outside the new size, mark it as off. */
12973 mark_window_cursors_off (XWINDOW (f->root_window));
12974
12975 /* Clear out any recollection of where the mouse highlighting was,
12976 since it might be in a place that's outside the new frame size.
12977 Actually checking whether it is outside is a pain in the neck,
12978 so don't try--just let the highlighting be done afresh with new size. */
12979 cancel_mouse_face (f);
12980
12981 UNBLOCK_INPUT;
12982 }
12983 \f
12984 /* Mouse warping. */
12985
12986 void
12987 x_set_mouse_position (f, x, y)
12988 struct frame *f;
12989 int x, y;
12990 {
12991 int pix_x, pix_y;
12992
12993 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
12994 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
12995
12996 if (pix_x < 0) pix_x = 0;
12997 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
12998
12999 if (pix_y < 0) pix_y = 0;
13000 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
13001
13002 BLOCK_INPUT;
13003
13004 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13005 0, 0, 0, 0, pix_x, pix_y);
13006 UNBLOCK_INPUT;
13007 }
13008
13009 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
13010
13011 void
13012 x_set_mouse_pixel_position (f, pix_x, pix_y)
13013 struct frame *f;
13014 int pix_x, pix_y;
13015 {
13016 BLOCK_INPUT;
13017
13018 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13019 0, 0, 0, 0, pix_x, pix_y);
13020 UNBLOCK_INPUT;
13021 }
13022 \f
13023 /* focus shifting, raising and lowering. */
13024
13025 void
13026 x_focus_on_frame (f)
13027 struct frame *f;
13028 {
13029 #if 0 /* This proves to be unpleasant. */
13030 x_raise_frame (f);
13031 #endif
13032 #if 0
13033 /* I don't think that the ICCCM allows programs to do things like this
13034 without the interaction of the window manager. Whatever you end up
13035 doing with this code, do it to x_unfocus_frame too. */
13036 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
13037 RevertToPointerRoot, CurrentTime);
13038 #endif /* ! 0 */
13039 }
13040
13041 void
13042 x_unfocus_frame (f)
13043 struct frame *f;
13044 {
13045 #if 0
13046 /* Look at the remarks in x_focus_on_frame. */
13047 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
13048 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
13049 RevertToPointerRoot, CurrentTime);
13050 #endif /* ! 0 */
13051 }
13052
13053 /* Raise frame F. */
13054
13055 void
13056 x_raise_frame (f)
13057 struct frame *f;
13058 {
13059 if (f->async_visible)
13060 {
13061 BLOCK_INPUT;
13062 #ifdef USE_X_TOOLKIT
13063 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
13064 #else /* not USE_X_TOOLKIT */
13065 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13066 #endif /* not USE_X_TOOLKIT */
13067 XFlush (FRAME_X_DISPLAY (f));
13068 UNBLOCK_INPUT;
13069 }
13070 }
13071
13072 /* Lower frame F. */
13073
13074 void
13075 x_lower_frame (f)
13076 struct frame *f;
13077 {
13078 if (f->async_visible)
13079 {
13080 BLOCK_INPUT;
13081 #ifdef USE_X_TOOLKIT
13082 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
13083 #else /* not USE_X_TOOLKIT */
13084 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13085 #endif /* not USE_X_TOOLKIT */
13086 XFlush (FRAME_X_DISPLAY (f));
13087 UNBLOCK_INPUT;
13088 }
13089 }
13090
13091 static void
13092 XTframe_raise_lower (f, raise_flag)
13093 FRAME_PTR f;
13094 int raise_flag;
13095 {
13096 if (raise_flag)
13097 x_raise_frame (f);
13098 else
13099 x_lower_frame (f);
13100 }
13101 \f
13102 /* Change of visibility. */
13103
13104 /* This tries to wait until the frame is really visible.
13105 However, if the window manager asks the user where to position
13106 the frame, this will return before the user finishes doing that.
13107 The frame will not actually be visible at that time,
13108 but it will become visible later when the window manager
13109 finishes with it. */
13110
13111 void
13112 x_make_frame_visible (f)
13113 struct frame *f;
13114 {
13115 Lisp_Object type;
13116 int original_top, original_left;
13117 int retry_count = 2;
13118
13119 retry:
13120
13121 BLOCK_INPUT;
13122
13123 type = x_icon_type (f);
13124 if (!NILP (type))
13125 x_bitmap_icon (f, type);
13126
13127 if (! FRAME_VISIBLE_P (f))
13128 {
13129 /* We test FRAME_GARBAGED_P here to make sure we don't
13130 call x_set_offset a second time
13131 if we get to x_make_frame_visible a second time
13132 before the window gets really visible. */
13133 if (! FRAME_ICONIFIED_P (f)
13134 && ! f->output_data.x->asked_for_visible)
13135 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13136
13137 f->output_data.x->asked_for_visible = 1;
13138
13139 if (! EQ (Vx_no_window_manager, Qt))
13140 x_wm_set_window_state (f, NormalState);
13141 #ifdef USE_X_TOOLKIT
13142 /* This was XtPopup, but that did nothing for an iconified frame. */
13143 XtMapWidget (f->output_data.x->widget);
13144 #else /* not USE_X_TOOLKIT */
13145 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13146 #endif /* not USE_X_TOOLKIT */
13147 #if 0 /* This seems to bring back scroll bars in the wrong places
13148 if the window configuration has changed. They seem
13149 to come back ok without this. */
13150 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
13151 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13152 #endif
13153 }
13154
13155 XFlush (FRAME_X_DISPLAY (f));
13156
13157 /* Synchronize to ensure Emacs knows the frame is visible
13158 before we do anything else. We do this loop with input not blocked
13159 so that incoming events are handled. */
13160 {
13161 Lisp_Object frame;
13162 int count;
13163 /* This must be before UNBLOCK_INPUT
13164 since events that arrive in response to the actions above
13165 will set it when they are handled. */
13166 int previously_visible = f->output_data.x->has_been_visible;
13167
13168 original_left = f->output_data.x->left_pos;
13169 original_top = f->output_data.x->top_pos;
13170
13171 /* This must come after we set COUNT. */
13172 UNBLOCK_INPUT;
13173
13174 /* We unblock here so that arriving X events are processed. */
13175
13176 /* Now move the window back to where it was "supposed to be".
13177 But don't do it if the gravity is negative.
13178 When the gravity is negative, this uses a position
13179 that is 3 pixels too low. Perhaps that's really the border width.
13180
13181 Don't do this if the window has never been visible before,
13182 because the window manager may choose the position
13183 and we don't want to override it. */
13184
13185 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
13186 && f->output_data.x->win_gravity == NorthWestGravity
13187 && previously_visible)
13188 {
13189 Drawable rootw;
13190 int x, y;
13191 unsigned int width, height, border, depth;
13192
13193 BLOCK_INPUT;
13194
13195 /* On some window managers (such as FVWM) moving an existing
13196 window, even to the same place, causes the window manager
13197 to introduce an offset. This can cause the window to move
13198 to an unexpected location. Check the geometry (a little
13199 slow here) and then verify that the window is in the right
13200 place. If the window is not in the right place, move it
13201 there, and take the potential window manager hit. */
13202 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13203 &rootw, &x, &y, &width, &height, &border, &depth);
13204
13205 if (original_left != x || original_top != y)
13206 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13207 original_left, original_top);
13208
13209 UNBLOCK_INPUT;
13210 }
13211
13212 XSETFRAME (frame, f);
13213
13214 /* Wait until the frame is visible. Process X events until a
13215 MapNotify event has been seen, or until we think we won't get a
13216 MapNotify at all.. */
13217 for (count = input_signal_count + 10;
13218 input_signal_count < count && !FRAME_VISIBLE_P (f);)
13219 {
13220 /* Force processing of queued events. */
13221 x_sync (f);
13222
13223 /* Machines that do polling rather than SIGIO have been
13224 observed to go into a busy-wait here. So we'll fake an
13225 alarm signal to let the handler know that there's something
13226 to be read. We used to raise a real alarm, but it seems
13227 that the handler isn't always enabled here. This is
13228 probably a bug. */
13229 if (input_polling_used ())
13230 {
13231 /* It could be confusing if a real alarm arrives while
13232 processing the fake one. Turn it off and let the
13233 handler reset it. */
13234 extern void poll_for_input_1 P_ ((void));
13235 int old_poll_suppress_count = poll_suppress_count;
13236 poll_suppress_count = 1;
13237 poll_for_input_1 ();
13238 poll_suppress_count = old_poll_suppress_count;
13239 }
13240
13241 /* See if a MapNotify event has been processed. */
13242 FRAME_SAMPLE_VISIBILITY (f);
13243 }
13244
13245 /* 2000-09-28: In
13246
13247 (let ((f (selected-frame)))
13248 (iconify-frame f)
13249 (raise-frame f))
13250
13251 the frame is not raised with various window managers on
13252 FreeBSD, Linux and Solaris. It turns out that, for some
13253 unknown reason, the call to XtMapWidget is completely ignored.
13254 Mapping the widget a second time works. */
13255
13256 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
13257 goto retry;
13258 }
13259 }
13260
13261 /* Change from mapped state to withdrawn state. */
13262
13263 /* Make the frame visible (mapped and not iconified). */
13264
13265 void
13266 x_make_frame_invisible (f)
13267 struct frame *f;
13268 {
13269 Window window;
13270
13271 #ifdef USE_X_TOOLKIT
13272 /* Use the frame's outermost window, not the one we normally draw on. */
13273 window = XtWindow (f->output_data.x->widget);
13274 #else /* not USE_X_TOOLKIT */
13275 window = FRAME_X_WINDOW (f);
13276 #endif /* not USE_X_TOOLKIT */
13277
13278 /* Don't keep the highlight on an invisible frame. */
13279 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13280 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
13281
13282 #if 0/* This might add unreliability; I don't trust it -- rms. */
13283 if (! f->async_visible && ! f->async_iconified)
13284 return;
13285 #endif
13286
13287 BLOCK_INPUT;
13288
13289 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
13290 that the current position of the window is user-specified, rather than
13291 program-specified, so that when the window is mapped again, it will be
13292 placed at the same location, without forcing the user to position it
13293 by hand again (they have already done that once for this window.) */
13294 x_wm_set_size_hint (f, (long) 0, 1);
13295
13296 #ifdef HAVE_X11R4
13297
13298 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
13299 DefaultScreen (FRAME_X_DISPLAY (f))))
13300 {
13301 UNBLOCK_INPUT_RESIGNAL;
13302 error ("Can't notify window manager of window withdrawal");
13303 }
13304 #else /* ! defined (HAVE_X11R4) */
13305
13306 /* Tell the window manager what we're going to do. */
13307 if (! EQ (Vx_no_window_manager, Qt))
13308 {
13309 XEvent unmap;
13310
13311 unmap.xunmap.type = UnmapNotify;
13312 unmap.xunmap.window = window;
13313 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
13314 unmap.xunmap.from_configure = False;
13315 if (! XSendEvent (FRAME_X_DISPLAY (f),
13316 DefaultRootWindow (FRAME_X_DISPLAY (f)),
13317 False,
13318 SubstructureRedirectMaskSubstructureNotifyMask,
13319 &unmap))
13320 {
13321 UNBLOCK_INPUT_RESIGNAL;
13322 error ("Can't notify window manager of withdrawal");
13323 }
13324 }
13325
13326 /* Unmap the window ourselves. Cheeky! */
13327 XUnmapWindow (FRAME_X_DISPLAY (f), window);
13328 #endif /* ! defined (HAVE_X11R4) */
13329
13330 /* We can't distinguish this from iconification
13331 just by the event that we get from the server.
13332 So we can't win using the usual strategy of letting
13333 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
13334 and synchronize with the server to make sure we agree. */
13335 f->visible = 0;
13336 FRAME_ICONIFIED_P (f) = 0;
13337 f->async_visible = 0;
13338 f->async_iconified = 0;
13339
13340 x_sync (f);
13341
13342 UNBLOCK_INPUT;
13343 }
13344
13345 /* Change window state from mapped to iconified. */
13346
13347 void
13348 x_iconify_frame (f)
13349 struct frame *f;
13350 {
13351 int result;
13352 Lisp_Object type;
13353
13354 /* Don't keep the highlight on an invisible frame. */
13355 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13356 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
13357
13358 if (f->async_iconified)
13359 return;
13360
13361 BLOCK_INPUT;
13362
13363 FRAME_SAMPLE_VISIBILITY (f);
13364
13365 type = x_icon_type (f);
13366 if (!NILP (type))
13367 x_bitmap_icon (f, type);
13368
13369 #ifdef USE_X_TOOLKIT
13370
13371 if (! FRAME_VISIBLE_P (f))
13372 {
13373 if (! EQ (Vx_no_window_manager, Qt))
13374 x_wm_set_window_state (f, IconicState);
13375 /* This was XtPopup, but that did nothing for an iconified frame. */
13376 XtMapWidget (f->output_data.x->widget);
13377 /* The server won't give us any event to indicate
13378 that an invisible frame was changed to an icon,
13379 so we have to record it here. */
13380 f->iconified = 1;
13381 f->visible = 1;
13382 f->async_iconified = 1;
13383 f->async_visible = 0;
13384 UNBLOCK_INPUT;
13385 return;
13386 }
13387
13388 result = XIconifyWindow (FRAME_X_DISPLAY (f),
13389 XtWindow (f->output_data.x->widget),
13390 DefaultScreen (FRAME_X_DISPLAY (f)));
13391 UNBLOCK_INPUT;
13392
13393 if (!result)
13394 error ("Can't notify window manager of iconification");
13395
13396 f->async_iconified = 1;
13397 f->async_visible = 0;
13398
13399
13400 BLOCK_INPUT;
13401 XFlush (FRAME_X_DISPLAY (f));
13402 UNBLOCK_INPUT;
13403 #else /* not USE_X_TOOLKIT */
13404
13405 /* Make sure the X server knows where the window should be positioned,
13406 in case the user deiconifies with the window manager. */
13407 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
13408 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13409
13410 /* Since we don't know which revision of X we're running, we'll use both
13411 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
13412
13413 /* X11R4: send a ClientMessage to the window manager using the
13414 WM_CHANGE_STATE type. */
13415 {
13416 XEvent message;
13417
13418 message.xclient.window = FRAME_X_WINDOW (f);
13419 message.xclient.type = ClientMessage;
13420 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
13421 message.xclient.format = 32;
13422 message.xclient.data.l[0] = IconicState;
13423
13424 if (! XSendEvent (FRAME_X_DISPLAY (f),
13425 DefaultRootWindow (FRAME_X_DISPLAY (f)),
13426 False,
13427 SubstructureRedirectMask | SubstructureNotifyMask,
13428 &message))
13429 {
13430 UNBLOCK_INPUT_RESIGNAL;
13431 error ("Can't notify window manager of iconification");
13432 }
13433 }
13434
13435 /* X11R3: set the initial_state field of the window manager hints to
13436 IconicState. */
13437 x_wm_set_window_state (f, IconicState);
13438
13439 if (!FRAME_VISIBLE_P (f))
13440 {
13441 /* If the frame was withdrawn, before, we must map it. */
13442 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13443 }
13444
13445 f->async_iconified = 1;
13446 f->async_visible = 0;
13447
13448 XFlush (FRAME_X_DISPLAY (f));
13449 UNBLOCK_INPUT;
13450 #endif /* not USE_X_TOOLKIT */
13451 }
13452
13453 \f
13454 /* Free X resources of frame F. */
13455
13456 void
13457 x_free_frame_resources (f)
13458 struct frame *f;
13459 {
13460 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13461 Lisp_Object bar;
13462 struct scroll_bar *b;
13463
13464 BLOCK_INPUT;
13465
13466 /* If a display connection is dead, don't try sending more
13467 commands to the X server. */
13468 if (dpyinfo->display)
13469 {
13470 if (f->output_data.x->icon_desc)
13471 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
13472
13473 #ifdef USE_X_TOOLKIT
13474 /* Explicitly destroy the scroll bars of the frame. Without
13475 this, we get "BadDrawable" errors from the toolkit later on,
13476 presumably from expose events generated for the disappearing
13477 toolkit scroll bars. */
13478 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
13479 {
13480 b = XSCROLL_BAR (bar);
13481 x_scroll_bar_remove (b);
13482 }
13483 #endif
13484
13485 #ifdef HAVE_X_I18N
13486 if (FRAME_XIC (f))
13487 free_frame_xic (f);
13488 #endif
13489
13490 #ifdef USE_X_TOOLKIT
13491 if (f->output_data.x->widget)
13492 {
13493 XtDestroyWidget (f->output_data.x->widget);
13494 f->output_data.x->widget = NULL;
13495 }
13496 /* Tooltips don't have widgets, only a simple X window, even if
13497 we are using a toolkit. */
13498 else if (FRAME_X_WINDOW (f))
13499 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13500
13501 free_frame_menubar (f);
13502 #else /* !USE_X_TOOLKIT */
13503 if (FRAME_X_WINDOW (f))
13504 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13505 #endif /* !USE_X_TOOLKIT */
13506
13507 unload_color (f, f->output_data.x->foreground_pixel);
13508 unload_color (f, f->output_data.x->background_pixel);
13509 unload_color (f, f->output_data.x->cursor_pixel);
13510 unload_color (f, f->output_data.x->cursor_foreground_pixel);
13511 unload_color (f, f->output_data.x->border_pixel);
13512 unload_color (f, f->output_data.x->mouse_pixel);
13513
13514 if (f->output_data.x->scroll_bar_background_pixel != -1)
13515 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
13516 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
13517 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
13518 #ifdef USE_TOOLKIT_SCROLL_BARS
13519 /* Scrollbar shadow colors. */
13520 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
13521 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
13522 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
13523 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
13524 #endif /* USE_TOOLKIT_SCROLL_BARS */
13525 if (f->output_data.x->white_relief.allocated_p)
13526 unload_color (f, f->output_data.x->white_relief.pixel);
13527 if (f->output_data.x->black_relief.allocated_p)
13528 unload_color (f, f->output_data.x->black_relief.pixel);
13529
13530 if (FRAME_FACE_CACHE (f))
13531 free_frame_faces (f);
13532
13533 x_free_gcs (f);
13534 XFlush (FRAME_X_DISPLAY (f));
13535 }
13536
13537 if (f->output_data.x->saved_menu_event)
13538 xfree (f->output_data.x->saved_menu_event);
13539
13540 xfree (f->output_data.x);
13541 f->output_data.x = NULL;
13542
13543 if (f == dpyinfo->x_focus_frame)
13544 dpyinfo->x_focus_frame = 0;
13545 if (f == dpyinfo->x_focus_event_frame)
13546 dpyinfo->x_focus_event_frame = 0;
13547 if (f == dpyinfo->x_highlight_frame)
13548 dpyinfo->x_highlight_frame = 0;
13549
13550 if (f == dpyinfo->mouse_face_mouse_frame)
13551 {
13552 dpyinfo->mouse_face_beg_row
13553 = dpyinfo->mouse_face_beg_col = -1;
13554 dpyinfo->mouse_face_end_row
13555 = dpyinfo->mouse_face_end_col = -1;
13556 dpyinfo->mouse_face_window = Qnil;
13557 dpyinfo->mouse_face_deferred_gc = 0;
13558 dpyinfo->mouse_face_mouse_frame = 0;
13559 }
13560
13561 UNBLOCK_INPUT;
13562 }
13563
13564
13565 /* Destroy the X window of frame F. */
13566
13567 void
13568 x_destroy_window (f)
13569 struct frame *f;
13570 {
13571 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13572
13573 /* If a display connection is dead, don't try sending more
13574 commands to the X server. */
13575 if (dpyinfo->display != 0)
13576 x_free_frame_resources (f);
13577
13578 dpyinfo->reference_count--;
13579 }
13580
13581 \f
13582 /* Setting window manager hints. */
13583
13584 /* Set the normal size hints for the window manager, for frame F.
13585 FLAGS is the flags word to use--or 0 meaning preserve the flags
13586 that the window now has.
13587 If USER_POSITION is nonzero, we set the USPosition
13588 flag (this is useful when FLAGS is 0). */
13589
13590 void
13591 x_wm_set_size_hint (f, flags, user_position)
13592 struct frame *f;
13593 long flags;
13594 int user_position;
13595 {
13596 XSizeHints size_hints;
13597
13598 #ifdef USE_X_TOOLKIT
13599 Arg al[2];
13600 int ac = 0;
13601 Dimension widget_width, widget_height;
13602 Window window = XtWindow (f->output_data.x->widget);
13603 #else /* not USE_X_TOOLKIT */
13604 Window window = FRAME_X_WINDOW (f);
13605 #endif /* not USE_X_TOOLKIT */
13606
13607 /* Setting PMaxSize caused various problems. */
13608 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
13609
13610 size_hints.x = f->output_data.x->left_pos;
13611 size_hints.y = f->output_data.x->top_pos;
13612
13613 #ifdef USE_X_TOOLKIT
13614 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
13615 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
13616 XtGetValues (f->output_data.x->widget, al, ac);
13617 size_hints.height = widget_height;
13618 size_hints.width = widget_width;
13619 #else /* not USE_X_TOOLKIT */
13620 size_hints.height = PIXEL_HEIGHT (f);
13621 size_hints.width = PIXEL_WIDTH (f);
13622 #endif /* not USE_X_TOOLKIT */
13623
13624 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
13625 size_hints.height_inc = f->output_data.x->line_height;
13626 size_hints.max_width
13627 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
13628 size_hints.max_height
13629 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
13630
13631 /* Calculate the base and minimum sizes.
13632
13633 (When we use the X toolkit, we don't do it here.
13634 Instead we copy the values that the widgets are using, below.) */
13635 #ifndef USE_X_TOOLKIT
13636 {
13637 int base_width, base_height;
13638 int min_rows = 0, min_cols = 0;
13639
13640 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
13641 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
13642
13643 check_frame_size (f, &min_rows, &min_cols);
13644
13645 /* The window manager uses the base width hints to calculate the
13646 current number of rows and columns in the frame while
13647 resizing; min_width and min_height aren't useful for this
13648 purpose, since they might not give the dimensions for a
13649 zero-row, zero-column frame.
13650
13651 We use the base_width and base_height members if we have
13652 them; otherwise, we set the min_width and min_height members
13653 to the size for a zero x zero frame. */
13654
13655 #ifdef HAVE_X11R4
13656 size_hints.flags |= PBaseSize;
13657 size_hints.base_width = base_width;
13658 size_hints.base_height = base_height;
13659 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
13660 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
13661 #else
13662 size_hints.min_width = base_width;
13663 size_hints.min_height = base_height;
13664 #endif
13665 }
13666
13667 /* If we don't need the old flags, we don't need the old hint at all. */
13668 if (flags)
13669 {
13670 size_hints.flags |= flags;
13671 goto no_read;
13672 }
13673 #endif /* not USE_X_TOOLKIT */
13674
13675 {
13676 XSizeHints hints; /* Sometimes I hate X Windows... */
13677 long supplied_return;
13678 int value;
13679
13680 #ifdef HAVE_X11R4
13681 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
13682 &supplied_return);
13683 #else
13684 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
13685 #endif
13686
13687 #ifdef USE_X_TOOLKIT
13688 size_hints.base_height = hints.base_height;
13689 size_hints.base_width = hints.base_width;
13690 size_hints.min_height = hints.min_height;
13691 size_hints.min_width = hints.min_width;
13692 #endif
13693
13694 if (flags)
13695 size_hints.flags |= flags;
13696 else
13697 {
13698 if (value == 0)
13699 hints.flags = 0;
13700 if (hints.flags & PSize)
13701 size_hints.flags |= PSize;
13702 if (hints.flags & PPosition)
13703 size_hints.flags |= PPosition;
13704 if (hints.flags & USPosition)
13705 size_hints.flags |= USPosition;
13706 if (hints.flags & USSize)
13707 size_hints.flags |= USSize;
13708 }
13709 }
13710
13711 #ifndef USE_X_TOOLKIT
13712 no_read:
13713 #endif
13714
13715 #ifdef PWinGravity
13716 size_hints.win_gravity = f->output_data.x->win_gravity;
13717 size_hints.flags |= PWinGravity;
13718
13719 if (user_position)
13720 {
13721 size_hints.flags &= ~ PPosition;
13722 size_hints.flags |= USPosition;
13723 }
13724 #endif /* PWinGravity */
13725
13726 #ifdef HAVE_X11R4
13727 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13728 #else
13729 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13730 #endif
13731 }
13732
13733 /* Used for IconicState or NormalState */
13734
13735 void
13736 x_wm_set_window_state (f, state)
13737 struct frame *f;
13738 int state;
13739 {
13740 #ifdef USE_X_TOOLKIT
13741 Arg al[1];
13742
13743 XtSetArg (al[0], XtNinitialState, state);
13744 XtSetValues (f->output_data.x->widget, al, 1);
13745 #else /* not USE_X_TOOLKIT */
13746 Window window = FRAME_X_WINDOW (f);
13747
13748 f->output_data.x->wm_hints.flags |= StateHint;
13749 f->output_data.x->wm_hints.initial_state = state;
13750
13751 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13752 #endif /* not USE_X_TOOLKIT */
13753 }
13754
13755 void
13756 x_wm_set_icon_pixmap (f, pixmap_id)
13757 struct frame *f;
13758 int pixmap_id;
13759 {
13760 Pixmap icon_pixmap;
13761
13762 #ifndef USE_X_TOOLKIT
13763 Window window = FRAME_X_WINDOW (f);
13764 #endif
13765
13766 if (pixmap_id > 0)
13767 {
13768 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
13769 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
13770 }
13771 else
13772 {
13773 /* It seems there is no way to turn off use of an icon pixmap.
13774 The following line does it, only if no icon has yet been created,
13775 for some window managers. But with mwm it crashes.
13776 Some people say it should clear the IconPixmapHint bit in this case,
13777 but that doesn't work, and the X consortium said it isn't the
13778 right thing at all. Since there is no way to win,
13779 best to explicitly give up. */
13780 #if 0
13781 f->output_data.x->wm_hints.icon_pixmap = None;
13782 #else
13783 return;
13784 #endif
13785 }
13786
13787 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
13788
13789 {
13790 Arg al[1];
13791 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
13792 XtSetValues (f->output_data.x->widget, al, 1);
13793 }
13794
13795 #else /* not USE_X_TOOLKIT */
13796
13797 f->output_data.x->wm_hints.flags |= IconPixmapHint;
13798 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13799
13800 #endif /* not USE_X_TOOLKIT */
13801 }
13802
13803 void
13804 x_wm_set_icon_position (f, icon_x, icon_y)
13805 struct frame *f;
13806 int icon_x, icon_y;
13807 {
13808 #ifdef USE_X_TOOLKIT
13809 Window window = XtWindow (f->output_data.x->widget);
13810 #else
13811 Window window = FRAME_X_WINDOW (f);
13812 #endif
13813
13814 f->output_data.x->wm_hints.flags |= IconPositionHint;
13815 f->output_data.x->wm_hints.icon_x = icon_x;
13816 f->output_data.x->wm_hints.icon_y = icon_y;
13817
13818 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13819 }
13820
13821 \f
13822 /***********************************************************************
13823 Fonts
13824 ***********************************************************************/
13825
13826 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
13827
13828 struct font_info *
13829 x_get_font_info (f, font_idx)
13830 FRAME_PTR f;
13831 int font_idx;
13832 {
13833 return (FRAME_X_FONT_TABLE (f) + font_idx);
13834 }
13835
13836
13837 /* Return a list of names of available fonts matching PATTERN on frame F.
13838
13839 If SIZE is > 0, it is the size (maximum bounds width) of fonts
13840 to be listed.
13841
13842 SIZE < 0 means include scalable fonts.
13843
13844 Frame F null means we have not yet created any frame on X, and
13845 consult the first display in x_display_list. MAXNAMES sets a limit
13846 on how many fonts to match. */
13847
13848 Lisp_Object
13849 x_list_fonts (f, pattern, size, maxnames)
13850 struct frame *f;
13851 Lisp_Object pattern;
13852 int size;
13853 int maxnames;
13854 {
13855 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
13856 Lisp_Object tem, second_best;
13857 struct x_display_info *dpyinfo
13858 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
13859 Display *dpy = dpyinfo->display;
13860 int try_XLoadQueryFont = 0;
13861 int count;
13862 int allow_scalable_fonts_p = 0;
13863
13864 if (size < 0)
13865 {
13866 allow_scalable_fonts_p = 1;
13867 size = 0;
13868 }
13869
13870 patterns = Fassoc (pattern, Valternate_fontname_alist);
13871 if (NILP (patterns))
13872 patterns = Fcons (pattern, Qnil);
13873
13874 if (maxnames == 1 && !size)
13875 /* We can return any single font matching PATTERN. */
13876 try_XLoadQueryFont = 1;
13877
13878 for (; CONSP (patterns); patterns = XCDR (patterns))
13879 {
13880 int num_fonts;
13881 char **names = NULL;
13882
13883 pattern = XCAR (patterns);
13884 /* See if we cached the result for this particular query.
13885 The cache is an alist of the form:
13886 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
13887 tem = XCDR (dpyinfo->name_list_element);
13888 key = Fcons (Fcons (pattern, make_number (maxnames)),
13889 allow_scalable_fonts_p ? Qt : Qnil);
13890 list = Fassoc (key, tem);
13891 if (!NILP (list))
13892 {
13893 list = Fcdr_safe (list);
13894 /* We have a cashed list. Don't have to get the list again. */
13895 goto label_cached;
13896 }
13897
13898 /* At first, put PATTERN in the cache. */
13899
13900 BLOCK_INPUT;
13901 count = x_catch_errors (dpy);
13902
13903 if (try_XLoadQueryFont)
13904 {
13905 XFontStruct *font;
13906 unsigned long value;
13907
13908 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
13909 if (x_had_errors_p (dpy))
13910 {
13911 /* This error is perhaps due to insufficient memory on X
13912 server. Let's just ignore it. */
13913 font = NULL;
13914 x_clear_errors (dpy);
13915 }
13916
13917 if (font
13918 && XGetFontProperty (font, XA_FONT, &value))
13919 {
13920 char *name = (char *) XGetAtomName (dpy, (Atom) value);
13921 int len = strlen (name);
13922 char *tmp;
13923
13924 /* If DXPC (a Differential X Protocol Compressor)
13925 Ver.3.7 is running, XGetAtomName will return null
13926 string. We must avoid such a name. */
13927 if (len == 0)
13928 try_XLoadQueryFont = 0;
13929 else
13930 {
13931 num_fonts = 1;
13932 names = (char **) alloca (sizeof (char *));
13933 /* Some systems only allow alloca assigned to a
13934 simple var. */
13935 tmp = (char *) alloca (len + 1); names[0] = tmp;
13936 bcopy (name, names[0], len + 1);
13937 XFree (name);
13938 }
13939 }
13940 else
13941 try_XLoadQueryFont = 0;
13942
13943 if (font)
13944 XFreeFont (dpy, font);
13945 }
13946
13947 if (!try_XLoadQueryFont)
13948 {
13949 /* We try at least 10 fonts because XListFonts will return
13950 auto-scaled fonts at the head. */
13951 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
13952 &num_fonts);
13953 if (x_had_errors_p (dpy))
13954 {
13955 /* This error is perhaps due to insufficient memory on X
13956 server. Let's just ignore it. */
13957 names = NULL;
13958 x_clear_errors (dpy);
13959 }
13960 }
13961
13962 x_uncatch_errors (dpy, count);
13963 UNBLOCK_INPUT;
13964
13965 if (names)
13966 {
13967 int i;
13968
13969 /* Make a list of all the fonts we got back.
13970 Store that in the font cache for the display. */
13971 for (i = 0; i < num_fonts; i++)
13972 {
13973 int width = 0;
13974 char *p = names[i];
13975 int average_width = -1, dashes = 0;
13976
13977 /* Count the number of dashes in NAMES[I]. If there are
13978 14 dashes, and the field value following 12th dash
13979 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
13980 is usually too ugly to be used for editing. Let's
13981 ignore it. */
13982 while (*p)
13983 if (*p++ == '-')
13984 {
13985 dashes++;
13986 if (dashes == 7) /* PIXEL_SIZE field */
13987 width = atoi (p);
13988 else if (dashes == 12) /* AVERAGE_WIDTH field */
13989 average_width = atoi (p);
13990 }
13991
13992 if (allow_scalable_fonts_p
13993 || dashes < 14 || average_width != 0)
13994 {
13995 tem = build_string (names[i]);
13996 if (NILP (Fassoc (tem, list)))
13997 {
13998 if (STRINGP (Vx_pixel_size_width_font_regexp)
13999 && ((fast_c_string_match_ignore_case
14000 (Vx_pixel_size_width_font_regexp, names[i]))
14001 >= 0))
14002 /* We can set the value of PIXEL_SIZE to the
14003 width of this font. */
14004 list = Fcons (Fcons (tem, make_number (width)), list);
14005 else
14006 /* For the moment, width is not known. */
14007 list = Fcons (Fcons (tem, Qnil), list);
14008 }
14009 }
14010 }
14011
14012 if (!try_XLoadQueryFont)
14013 {
14014 BLOCK_INPUT;
14015 XFreeFontNames (names);
14016 UNBLOCK_INPUT;
14017 }
14018 }
14019
14020 /* Now store the result in the cache. */
14021 XSETCDR (dpyinfo->name_list_element,
14022 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
14023
14024 label_cached:
14025 if (NILP (list)) continue; /* Try the remaining alternatives. */
14026
14027 newlist = second_best = Qnil;
14028 /* Make a list of the fonts that have the right width. */
14029 for (; CONSP (list); list = XCDR (list))
14030 {
14031 int found_size;
14032
14033 tem = XCAR (list);
14034
14035 if (!CONSP (tem) || NILP (XCAR (tem)))
14036 continue;
14037 if (!size)
14038 {
14039 newlist = Fcons (XCAR (tem), newlist);
14040 continue;
14041 }
14042
14043 if (!INTEGERP (XCDR (tem)))
14044 {
14045 /* Since we have not yet known the size of this font, we
14046 must try slow function call XLoadQueryFont. */
14047 XFontStruct *thisinfo;
14048
14049 BLOCK_INPUT;
14050 count = x_catch_errors (dpy);
14051 thisinfo = XLoadQueryFont (dpy,
14052 XSTRING (XCAR (tem))->data);
14053 if (x_had_errors_p (dpy))
14054 {
14055 /* This error is perhaps due to insufficient memory on X
14056 server. Let's just ignore it. */
14057 thisinfo = NULL;
14058 x_clear_errors (dpy);
14059 }
14060 x_uncatch_errors (dpy, count);
14061 UNBLOCK_INPUT;
14062
14063 if (thisinfo)
14064 {
14065 XSETCDR (tem,
14066 (thisinfo->min_bounds.width == 0
14067 ? make_number (0)
14068 : make_number (thisinfo->max_bounds.width)));
14069 BLOCK_INPUT;
14070 XFreeFont (dpy, thisinfo);
14071 UNBLOCK_INPUT;
14072 }
14073 else
14074 /* For unknown reason, the previous call of XListFont had
14075 returned a font which can't be opened. Record the size
14076 as 0 not to try to open it again. */
14077 XSETCDR (tem, make_number (0));
14078 }
14079
14080 found_size = XINT (XCDR (tem));
14081 if (found_size == size)
14082 newlist = Fcons (XCAR (tem), newlist);
14083 else if (found_size > 0)
14084 {
14085 if (NILP (second_best))
14086 second_best = tem;
14087 else if (found_size < size)
14088 {
14089 if (XINT (XCDR (second_best)) > size
14090 || XINT (XCDR (second_best)) < found_size)
14091 second_best = tem;
14092 }
14093 else
14094 {
14095 if (XINT (XCDR (second_best)) > size
14096 && XINT (XCDR (second_best)) > found_size)
14097 second_best = tem;
14098 }
14099 }
14100 }
14101 if (!NILP (newlist))
14102 break;
14103 else if (!NILP (second_best))
14104 {
14105 newlist = Fcons (XCAR (second_best), Qnil);
14106 break;
14107 }
14108 }
14109
14110 return newlist;
14111 }
14112
14113
14114 #if GLYPH_DEBUG
14115
14116 /* Check that FONT is valid on frame F. It is if it can be found in F's
14117 font table. */
14118
14119 static void
14120 x_check_font (f, font)
14121 struct frame *f;
14122 XFontStruct *font;
14123 {
14124 int i;
14125 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14126
14127 xassert (font != NULL);
14128
14129 for (i = 0; i < dpyinfo->n_fonts; i++)
14130 if (dpyinfo->font_table[i].name
14131 && font == dpyinfo->font_table[i].font)
14132 break;
14133
14134 xassert (i < dpyinfo->n_fonts);
14135 }
14136
14137 #endif /* GLYPH_DEBUG != 0 */
14138
14139 /* Set *W to the minimum width, *H to the minimum font height of FONT.
14140 Note: There are (broken) X fonts out there with invalid XFontStruct
14141 min_bounds contents. For example, handa@etl.go.jp reports that
14142 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
14143 have font->min_bounds.width == 0. */
14144
14145 static INLINE void
14146 x_font_min_bounds (font, w, h)
14147 XFontStruct *font;
14148 int *w, *h;
14149 {
14150 *h = FONT_HEIGHT (font);
14151 *w = font->min_bounds.width;
14152
14153 /* Try to handle the case where FONT->min_bounds has invalid
14154 contents. Since the only font known to have invalid min_bounds
14155 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
14156 if (*w <= 0)
14157 *w = font->max_bounds.width;
14158 }
14159
14160
14161 /* Compute the smallest character width and smallest font height over
14162 all fonts available on frame F. Set the members smallest_char_width
14163 and smallest_font_height in F's x_display_info structure to
14164 the values computed. Value is non-zero if smallest_font_height or
14165 smallest_char_width become smaller than they were before. */
14166
14167 static int
14168 x_compute_min_glyph_bounds (f)
14169 struct frame *f;
14170 {
14171 int i;
14172 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14173 XFontStruct *font;
14174 int old_width = dpyinfo->smallest_char_width;
14175 int old_height = dpyinfo->smallest_font_height;
14176
14177 dpyinfo->smallest_font_height = 100000;
14178 dpyinfo->smallest_char_width = 100000;
14179
14180 for (i = 0; i < dpyinfo->n_fonts; ++i)
14181 if (dpyinfo->font_table[i].name)
14182 {
14183 struct font_info *fontp = dpyinfo->font_table + i;
14184 int w, h;
14185
14186 font = (XFontStruct *) fontp->font;
14187 xassert (font != (XFontStruct *) ~0);
14188 x_font_min_bounds (font, &w, &h);
14189
14190 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
14191 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
14192 }
14193
14194 xassert (dpyinfo->smallest_char_width > 0
14195 && dpyinfo->smallest_font_height > 0);
14196
14197 return (dpyinfo->n_fonts == 1
14198 || dpyinfo->smallest_char_width < old_width
14199 || dpyinfo->smallest_font_height < old_height);
14200 }
14201
14202
14203 /* Load font named FONTNAME of the size SIZE for frame F, and return a
14204 pointer to the structure font_info while allocating it dynamically.
14205 If SIZE is 0, load any size of font.
14206 If loading is failed, return NULL. */
14207
14208 struct font_info *
14209 x_load_font (f, fontname, size)
14210 struct frame *f;
14211 register char *fontname;
14212 int size;
14213 {
14214 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14215 Lisp_Object font_names;
14216 int count;
14217
14218 /* Get a list of all the fonts that match this name. Once we
14219 have a list of matching fonts, we compare them against the fonts
14220 we already have by comparing names. */
14221 font_names = x_list_fonts (f, build_string (fontname), size, 1);
14222
14223 if (!NILP (font_names))
14224 {
14225 Lisp_Object tail;
14226 int i;
14227
14228 for (i = 0; i < dpyinfo->n_fonts; i++)
14229 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
14230 if (dpyinfo->font_table[i].name
14231 && (!strcmp (dpyinfo->font_table[i].name,
14232 XSTRING (XCAR (tail))->data)
14233 || !strcmp (dpyinfo->font_table[i].full_name,
14234 XSTRING (XCAR (tail))->data)))
14235 return (dpyinfo->font_table + i);
14236 }
14237
14238 /* Load the font and add it to the table. */
14239 {
14240 char *full_name;
14241 XFontStruct *font;
14242 struct font_info *fontp;
14243 unsigned long value;
14244 int i;
14245
14246 /* If we have found fonts by x_list_font, load one of them. If
14247 not, we still try to load a font by the name given as FONTNAME
14248 because XListFonts (called in x_list_font) of some X server has
14249 a bug of not finding a font even if the font surely exists and
14250 is loadable by XLoadQueryFont. */
14251 if (size > 0 && !NILP (font_names))
14252 fontname = (char *) XSTRING (XCAR (font_names))->data;
14253
14254 BLOCK_INPUT;
14255 count = x_catch_errors (FRAME_X_DISPLAY (f));
14256 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
14257 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
14258 {
14259 /* This error is perhaps due to insufficient memory on X
14260 server. Let's just ignore it. */
14261 font = NULL;
14262 x_clear_errors (FRAME_X_DISPLAY (f));
14263 }
14264 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
14265 UNBLOCK_INPUT;
14266 if (!font)
14267 return NULL;
14268
14269 /* Find a free slot in the font table. */
14270 for (i = 0; i < dpyinfo->n_fonts; ++i)
14271 if (dpyinfo->font_table[i].name == NULL)
14272 break;
14273
14274 /* If no free slot found, maybe enlarge the font table. */
14275 if (i == dpyinfo->n_fonts
14276 && dpyinfo->n_fonts == dpyinfo->font_table_size)
14277 {
14278 int sz;
14279 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
14280 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
14281 dpyinfo->font_table
14282 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
14283 }
14284
14285 fontp = dpyinfo->font_table + i;
14286 if (i == dpyinfo->n_fonts)
14287 ++dpyinfo->n_fonts;
14288
14289 /* Now fill in the slots of *FONTP. */
14290 BLOCK_INPUT;
14291 fontp->font = font;
14292 fontp->font_idx = i;
14293 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
14294 bcopy (fontname, fontp->name, strlen (fontname) + 1);
14295
14296 /* Try to get the full name of FONT. Put it in FULL_NAME. */
14297 full_name = 0;
14298 if (XGetFontProperty (font, XA_FONT, &value))
14299 {
14300 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
14301 char *p = name;
14302 int dashes = 0;
14303
14304 /* Count the number of dashes in the "full name".
14305 If it is too few, this isn't really the font's full name,
14306 so don't use it.
14307 In X11R4, the fonts did not come with their canonical names
14308 stored in them. */
14309 while (*p)
14310 {
14311 if (*p == '-')
14312 dashes++;
14313 p++;
14314 }
14315
14316 if (dashes >= 13)
14317 {
14318 full_name = (char *) xmalloc (p - name + 1);
14319 bcopy (name, full_name, p - name + 1);
14320 }
14321
14322 XFree (name);
14323 }
14324
14325 if (full_name != 0)
14326 fontp->full_name = full_name;
14327 else
14328 fontp->full_name = fontp->name;
14329
14330 fontp->size = font->max_bounds.width;
14331 fontp->height = FONT_HEIGHT (font);
14332
14333 if (NILP (font_names))
14334 {
14335 /* We come here because of a bug of XListFonts mentioned at
14336 the head of this block. Let's store this information in
14337 the cache for x_list_fonts. */
14338 Lisp_Object lispy_name = build_string (fontname);
14339 Lisp_Object lispy_full_name = build_string (fontp->full_name);
14340 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
14341 Qnil);
14342
14343 XSETCDR (dpyinfo->name_list_element,
14344 Fcons (Fcons (key,
14345 Fcons (Fcons (lispy_full_name,
14346 make_number (fontp->size)),
14347 Qnil)),
14348 XCDR (dpyinfo->name_list_element)));
14349 if (full_name)
14350 {
14351 key = Fcons (Fcons (lispy_full_name, make_number (256)),
14352 Qnil);
14353 XSETCDR (dpyinfo->name_list_element,
14354 Fcons (Fcons (key,
14355 Fcons (Fcons (lispy_full_name,
14356 make_number (fontp->size)),
14357 Qnil)),
14358 XCDR (dpyinfo->name_list_element)));
14359 }
14360 }
14361
14362 /* The slot `encoding' specifies how to map a character
14363 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
14364 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
14365 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
14366 2:0xA020..0xFF7F). For the moment, we don't know which charset
14367 uses this font. So, we set information in fontp->encoding[1]
14368 which is never used by any charset. If mapping can't be
14369 decided, set FONT_ENCODING_NOT_DECIDED. */
14370 fontp->encoding[1]
14371 = (font->max_byte1 == 0
14372 /* 1-byte font */
14373 ? (font->min_char_or_byte2 < 0x80
14374 ? (font->max_char_or_byte2 < 0x80
14375 ? 0 /* 0x20..0x7F */
14376 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
14377 : 1) /* 0xA0..0xFF */
14378 /* 2-byte font */
14379 : (font->min_byte1 < 0x80
14380 ? (font->max_byte1 < 0x80
14381 ? (font->min_char_or_byte2 < 0x80
14382 ? (font->max_char_or_byte2 < 0x80
14383 ? 0 /* 0x2020..0x7F7F */
14384 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
14385 : 3) /* 0x20A0..0x7FFF */
14386 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
14387 : (font->min_char_or_byte2 < 0x80
14388 ? (font->max_char_or_byte2 < 0x80
14389 ? 2 /* 0xA020..0xFF7F */
14390 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
14391 : 1))); /* 0xA0A0..0xFFFF */
14392
14393 fontp->baseline_offset
14394 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
14395 ? (long) value : 0);
14396 fontp->relative_compose
14397 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
14398 ? (long) value : 0);
14399 fontp->default_ascent
14400 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
14401 ? (long) value : 0);
14402
14403 /* Set global flag fonts_changed_p to non-zero if the font loaded
14404 has a character with a smaller width than any other character
14405 before, or if the font loaded has a smaller height than any
14406 other font loaded before. If this happens, it will make a
14407 glyph matrix reallocation necessary. */
14408 fonts_changed_p |= x_compute_min_glyph_bounds (f);
14409 UNBLOCK_INPUT;
14410 return fontp;
14411 }
14412 }
14413
14414
14415 /* Return a pointer to struct font_info of a font named FONTNAME for
14416 frame F. If no such font is loaded, return NULL. */
14417
14418 struct font_info *
14419 x_query_font (f, fontname)
14420 struct frame *f;
14421 register char *fontname;
14422 {
14423 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14424 int i;
14425
14426 for (i = 0; i < dpyinfo->n_fonts; i++)
14427 if (dpyinfo->font_table[i].name
14428 && (!strcmp (dpyinfo->font_table[i].name, fontname)
14429 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
14430 return (dpyinfo->font_table + i);
14431 return NULL;
14432 }
14433
14434
14435 /* Find a CCL program for a font specified by FONTP, and set the member
14436 `encoder' of the structure. */
14437
14438 void
14439 x_find_ccl_program (fontp)
14440 struct font_info *fontp;
14441 {
14442 Lisp_Object list, elt;
14443
14444 elt = Qnil;
14445 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
14446 {
14447 elt = XCAR (list);
14448 if (CONSP (elt)
14449 && STRINGP (XCAR (elt))
14450 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
14451 >= 0)
14452 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
14453 >= 0)))
14454 break;
14455 }
14456
14457 if (! NILP (list))
14458 {
14459 struct ccl_program *ccl
14460 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
14461
14462 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
14463 xfree (ccl);
14464 else
14465 fontp->font_encoder = ccl;
14466 }
14467 }
14468
14469
14470 \f
14471 /***********************************************************************
14472 Initialization
14473 ***********************************************************************/
14474
14475 #ifdef USE_X_TOOLKIT
14476 static XrmOptionDescRec emacs_options[] = {
14477 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
14478 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
14479
14480 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
14481 XrmoptionSepArg, NULL},
14482 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
14483
14484 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14485 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14486 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14487 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14488 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14489 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
14490 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
14491 };
14492 #endif /* USE_X_TOOLKIT */
14493
14494 static int x_initialized;
14495
14496 #ifdef MULTI_KBOARD
14497 /* Test whether two display-name strings agree up to the dot that separates
14498 the screen number from the server number. */
14499 static int
14500 same_x_server (name1, name2)
14501 char *name1, *name2;
14502 {
14503 int seen_colon = 0;
14504 unsigned char *system_name = XSTRING (Vsystem_name)->data;
14505 int system_name_length = strlen (system_name);
14506 int length_until_period = 0;
14507
14508 while (system_name[length_until_period] != 0
14509 && system_name[length_until_period] != '.')
14510 length_until_period++;
14511
14512 /* Treat `unix' like an empty host name. */
14513 if (! strncmp (name1, "unix:", 5))
14514 name1 += 4;
14515 if (! strncmp (name2, "unix:", 5))
14516 name2 += 4;
14517 /* Treat this host's name like an empty host name. */
14518 if (! strncmp (name1, system_name, system_name_length)
14519 && name1[system_name_length] == ':')
14520 name1 += system_name_length;
14521 if (! strncmp (name2, system_name, system_name_length)
14522 && name2[system_name_length] == ':')
14523 name2 += system_name_length;
14524 /* Treat this host's domainless name like an empty host name. */
14525 if (! strncmp (name1, system_name, length_until_period)
14526 && name1[length_until_period] == ':')
14527 name1 += length_until_period;
14528 if (! strncmp (name2, system_name, length_until_period)
14529 && name2[length_until_period] == ':')
14530 name2 += length_until_period;
14531
14532 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
14533 {
14534 if (*name1 == ':')
14535 seen_colon++;
14536 if (seen_colon && *name1 == '.')
14537 return 1;
14538 }
14539 return (seen_colon
14540 && (*name1 == '.' || *name1 == '\0')
14541 && (*name2 == '.' || *name2 == '\0'));
14542 }
14543 #endif
14544
14545 struct x_display_info *
14546 x_term_init (display_name, xrm_option, resource_name)
14547 Lisp_Object display_name;
14548 char *xrm_option;
14549 char *resource_name;
14550 {
14551 int connection;
14552 Display *dpy;
14553 struct x_display_info *dpyinfo;
14554 XrmDatabase xrdb;
14555
14556 BLOCK_INPUT;
14557
14558 if (!x_initialized)
14559 {
14560 x_initialize ();
14561 x_initialized = 1;
14562 }
14563
14564 #ifdef USE_X_TOOLKIT
14565 /* weiner@footloose.sps.mot.com reports that this causes
14566 errors with X11R5:
14567 X protocol error: BadAtom (invalid Atom parameter)
14568 on protocol request 18skiloaf.
14569 So let's not use it until R6. */
14570 #ifdef HAVE_X11XTR6
14571 XtSetLanguageProc (NULL, NULL, NULL);
14572 #endif
14573
14574 {
14575 int argc = 0;
14576 char *argv[3];
14577
14578 argv[0] = "";
14579 argc = 1;
14580 if (xrm_option)
14581 {
14582 argv[argc++] = "-xrm";
14583 argv[argc++] = xrm_option;
14584 }
14585 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
14586 resource_name, EMACS_CLASS,
14587 emacs_options, XtNumber (emacs_options),
14588 &argc, argv);
14589
14590 #ifdef HAVE_X11XTR6
14591 /* I think this is to compensate for XtSetLanguageProc. */
14592 fixup_locale ();
14593 #endif
14594 }
14595
14596 #else /* not USE_X_TOOLKIT */
14597 #ifdef HAVE_X11R5
14598 XSetLocaleModifiers ("");
14599 #endif
14600 dpy = XOpenDisplay (XSTRING (display_name)->data);
14601 #endif /* not USE_X_TOOLKIT */
14602
14603 /* Detect failure. */
14604 if (dpy == 0)
14605 {
14606 UNBLOCK_INPUT;
14607 return 0;
14608 }
14609
14610 /* We have definitely succeeded. Record the new connection. */
14611
14612 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
14613 bzero (dpyinfo, sizeof *dpyinfo);
14614
14615 #ifdef MULTI_KBOARD
14616 {
14617 struct x_display_info *share;
14618 Lisp_Object tail;
14619
14620 for (share = x_display_list, tail = x_display_name_list; share;
14621 share = share->next, tail = XCDR (tail))
14622 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
14623 XSTRING (display_name)->data))
14624 break;
14625 if (share)
14626 dpyinfo->kboard = share->kboard;
14627 else
14628 {
14629 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
14630 init_kboard (dpyinfo->kboard);
14631 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
14632 {
14633 char *vendor = ServerVendor (dpy);
14634 UNBLOCK_INPUT;
14635 dpyinfo->kboard->Vsystem_key_alist
14636 = call1 (Qvendor_specific_keysyms,
14637 build_string (vendor ? vendor : ""));
14638 BLOCK_INPUT;
14639 }
14640
14641 dpyinfo->kboard->next_kboard = all_kboards;
14642 all_kboards = dpyinfo->kboard;
14643 /* Don't let the initial kboard remain current longer than necessary.
14644 That would cause problems if a file loaded on startup tries to
14645 prompt in the mini-buffer. */
14646 if (current_kboard == initial_kboard)
14647 current_kboard = dpyinfo->kboard;
14648 }
14649 dpyinfo->kboard->reference_count++;
14650 }
14651 #endif
14652
14653 /* Put this display on the chain. */
14654 dpyinfo->next = x_display_list;
14655 x_display_list = dpyinfo;
14656
14657 /* Put it on x_display_name_list as well, to keep them parallel. */
14658 x_display_name_list = Fcons (Fcons (display_name, Qnil),
14659 x_display_name_list);
14660 dpyinfo->name_list_element = XCAR (x_display_name_list);
14661
14662 dpyinfo->display = dpy;
14663
14664 #if 0
14665 XSetAfterFunction (x_current_display, x_trace_wire);
14666 #endif /* ! 0 */
14667
14668 dpyinfo->x_id_name
14669 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
14670 + STRING_BYTES (XSTRING (Vsystem_name))
14671 + 2);
14672 sprintf (dpyinfo->x_id_name, "%s@%s",
14673 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
14674
14675 /* Figure out which modifier bits mean what. */
14676 x_find_modifier_meanings (dpyinfo);
14677
14678 /* Get the scroll bar cursor. */
14679 dpyinfo->vertical_scroll_bar_cursor
14680 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
14681
14682 xrdb = x_load_resources (dpyinfo->display, xrm_option,
14683 resource_name, EMACS_CLASS);
14684 #ifdef HAVE_XRMSETDATABASE
14685 XrmSetDatabase (dpyinfo->display, xrdb);
14686 #else
14687 dpyinfo->display->db = xrdb;
14688 #endif
14689 /* Put the rdb where we can find it in a way that works on
14690 all versions. */
14691 dpyinfo->xrdb = xrdb;
14692
14693 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
14694 DefaultScreen (dpyinfo->display));
14695 select_visual (dpyinfo);
14696 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
14697 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
14698 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
14699 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
14700 dpyinfo->grabbed = 0;
14701 dpyinfo->reference_count = 0;
14702 dpyinfo->icon_bitmap_id = -1;
14703 dpyinfo->font_table = NULL;
14704 dpyinfo->n_fonts = 0;
14705 dpyinfo->font_table_size = 0;
14706 dpyinfo->bitmaps = 0;
14707 dpyinfo->bitmaps_size = 0;
14708 dpyinfo->bitmaps_last = 0;
14709 dpyinfo->scratch_cursor_gc = 0;
14710 dpyinfo->mouse_face_mouse_frame = 0;
14711 dpyinfo->mouse_face_deferred_gc = 0;
14712 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
14713 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
14714 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
14715 dpyinfo->mouse_face_window = Qnil;
14716 dpyinfo->mouse_face_overlay = Qnil;
14717 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
14718 dpyinfo->mouse_face_defer = 0;
14719 dpyinfo->mouse_face_hidden = 0;
14720 dpyinfo->x_focus_frame = 0;
14721 dpyinfo->x_focus_event_frame = 0;
14722 dpyinfo->x_highlight_frame = 0;
14723 dpyinfo->image_cache = make_image_cache ();
14724
14725 /* See if a private colormap is requested. */
14726 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
14727 {
14728 if (dpyinfo->visual->class == PseudoColor)
14729 {
14730 Lisp_Object value;
14731 value = display_x_get_resource (dpyinfo,
14732 build_string ("privateColormap"),
14733 build_string ("PrivateColormap"),
14734 Qnil, Qnil);
14735 if (STRINGP (value)
14736 && (!strcmp (XSTRING (value)->data, "true")
14737 || !strcmp (XSTRING (value)->data, "on")))
14738 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
14739 }
14740 }
14741 else
14742 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
14743 dpyinfo->visual, AllocNone);
14744
14745 {
14746 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
14747 double pixels = DisplayHeight (dpyinfo->display, screen_number);
14748 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
14749 dpyinfo->resy = pixels * 25.4 / mm;
14750 pixels = DisplayWidth (dpyinfo->display, screen_number);
14751 mm = DisplayWidthMM (dpyinfo->display, screen_number);
14752 dpyinfo->resx = pixels * 25.4 / mm;
14753 }
14754
14755 dpyinfo->Xatom_wm_protocols
14756 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
14757 dpyinfo->Xatom_wm_take_focus
14758 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
14759 dpyinfo->Xatom_wm_save_yourself
14760 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
14761 dpyinfo->Xatom_wm_delete_window
14762 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
14763 dpyinfo->Xatom_wm_change_state
14764 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
14765 dpyinfo->Xatom_wm_configure_denied
14766 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
14767 dpyinfo->Xatom_wm_window_moved
14768 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
14769 dpyinfo->Xatom_editres
14770 = XInternAtom (dpyinfo->display, "Editres", False);
14771 dpyinfo->Xatom_CLIPBOARD
14772 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
14773 dpyinfo->Xatom_TIMESTAMP
14774 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
14775 dpyinfo->Xatom_TEXT
14776 = XInternAtom (dpyinfo->display, "TEXT", False);
14777 dpyinfo->Xatom_COMPOUND_TEXT
14778 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
14779 dpyinfo->Xatom_DELETE
14780 = XInternAtom (dpyinfo->display, "DELETE", False);
14781 dpyinfo->Xatom_MULTIPLE
14782 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
14783 dpyinfo->Xatom_INCR
14784 = XInternAtom (dpyinfo->display, "INCR", False);
14785 dpyinfo->Xatom_EMACS_TMP
14786 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
14787 dpyinfo->Xatom_TARGETS
14788 = XInternAtom (dpyinfo->display, "TARGETS", False);
14789 dpyinfo->Xatom_NULL
14790 = XInternAtom (dpyinfo->display, "NULL", False);
14791 dpyinfo->Xatom_ATOM_PAIR
14792 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
14793 /* For properties of font. */
14794 dpyinfo->Xatom_PIXEL_SIZE
14795 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
14796 dpyinfo->Xatom_MULE_BASELINE_OFFSET
14797 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
14798 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
14799 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
14800 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
14801 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
14802
14803 /* Ghostscript support. */
14804 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
14805 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
14806
14807 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
14808 False);
14809
14810 dpyinfo->cut_buffers_initialized = 0;
14811
14812 connection = ConnectionNumber (dpyinfo->display);
14813 dpyinfo->connection = connection;
14814
14815 {
14816 char null_bits[1];
14817
14818 null_bits[0] = 0x00;
14819
14820 dpyinfo->null_pixel
14821 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14822 null_bits, 1, 1, (long) 0, (long) 0,
14823 1);
14824 }
14825
14826 {
14827 extern int gray_bitmap_width, gray_bitmap_height;
14828 extern char *gray_bitmap_bits;
14829 dpyinfo->gray
14830 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14831 gray_bitmap_bits,
14832 gray_bitmap_width, gray_bitmap_height,
14833 (unsigned long) 1, (unsigned long) 0, 1);
14834 }
14835
14836 #ifdef HAVE_X_I18N
14837 xim_initialize (dpyinfo, resource_name);
14838 #endif
14839
14840 #ifdef subprocesses
14841 /* This is only needed for distinguishing keyboard and process input. */
14842 if (connection != 0)
14843 add_keyboard_wait_descriptor (connection);
14844 #endif
14845
14846 #ifndef F_SETOWN_BUG
14847 #ifdef F_SETOWN
14848 #ifdef F_SETOWN_SOCK_NEG
14849 /* stdin is a socket here */
14850 fcntl (connection, F_SETOWN, -getpid ());
14851 #else /* ! defined (F_SETOWN_SOCK_NEG) */
14852 fcntl (connection, F_SETOWN, getpid ());
14853 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
14854 #endif /* ! defined (F_SETOWN) */
14855 #endif /* F_SETOWN_BUG */
14856
14857 #ifdef SIGIO
14858 if (interrupt_input)
14859 init_sigio (connection);
14860 #endif /* ! defined (SIGIO) */
14861
14862 #ifdef USE_LUCID
14863 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
14864 /* Make sure that we have a valid font for dialog boxes
14865 so that Xt does not crash. */
14866 {
14867 Display *dpy = dpyinfo->display;
14868 XrmValue d, fr, to;
14869 Font font;
14870 int count;
14871
14872 d.addr = (XPointer)&dpy;
14873 d.size = sizeof (Display *);
14874 fr.addr = XtDefaultFont;
14875 fr.size = sizeof (XtDefaultFont);
14876 to.size = sizeof (Font *);
14877 to.addr = (XPointer)&font;
14878 count = x_catch_errors (dpy);
14879 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
14880 abort ();
14881 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
14882 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
14883 x_uncatch_errors (dpy, count);
14884 }
14885 #endif
14886 #endif
14887
14888 /* See if we should run in synchronous mode. This is useful
14889 for debugging X code. */
14890 {
14891 Lisp_Object value;
14892 value = display_x_get_resource (dpyinfo,
14893 build_string ("synchronous"),
14894 build_string ("Synchronous"),
14895 Qnil, Qnil);
14896 if (STRINGP (value)
14897 && (!strcmp (XSTRING (value)->data, "true")
14898 || !strcmp (XSTRING (value)->data, "on")))
14899 XSynchronize (dpyinfo->display, True);
14900 }
14901
14902 UNBLOCK_INPUT;
14903
14904 return dpyinfo;
14905 }
14906 \f
14907 /* Get rid of display DPYINFO, assuming all frames are already gone,
14908 and without sending any more commands to the X server. */
14909
14910 void
14911 x_delete_display (dpyinfo)
14912 struct x_display_info *dpyinfo;
14913 {
14914 delete_keyboard_wait_descriptor (dpyinfo->connection);
14915
14916 /* Discard this display from x_display_name_list and x_display_list.
14917 We can't use Fdelq because that can quit. */
14918 if (! NILP (x_display_name_list)
14919 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
14920 x_display_name_list = XCDR (x_display_name_list);
14921 else
14922 {
14923 Lisp_Object tail;
14924
14925 tail = x_display_name_list;
14926 while (CONSP (tail) && CONSP (XCDR (tail)))
14927 {
14928 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
14929 {
14930 XSETCDR (tail, XCDR (XCDR (tail)));
14931 break;
14932 }
14933 tail = XCDR (tail);
14934 }
14935 }
14936
14937 if (next_noop_dpyinfo == dpyinfo)
14938 next_noop_dpyinfo = dpyinfo->next;
14939
14940 if (x_display_list == dpyinfo)
14941 x_display_list = dpyinfo->next;
14942 else
14943 {
14944 struct x_display_info *tail;
14945
14946 for (tail = x_display_list; tail; tail = tail->next)
14947 if (tail->next == dpyinfo)
14948 tail->next = tail->next->next;
14949 }
14950
14951 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
14952 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
14953 XrmDestroyDatabase (dpyinfo->xrdb);
14954 #endif
14955 #endif
14956 #ifdef MULTI_KBOARD
14957 if (--dpyinfo->kboard->reference_count == 0)
14958 delete_kboard (dpyinfo->kboard);
14959 #endif
14960 #ifdef HAVE_X_I18N
14961 if (dpyinfo->xim)
14962 xim_close_dpy (dpyinfo);
14963 #endif
14964
14965 xfree (dpyinfo->font_table);
14966 xfree (dpyinfo->x_id_name);
14967 xfree (dpyinfo->color_cells);
14968 xfree (dpyinfo);
14969 }
14970
14971 \f
14972 /* Set up use of X before we make the first connection. */
14973
14974 static struct redisplay_interface x_redisplay_interface =
14975 {
14976 x_produce_glyphs,
14977 x_write_glyphs,
14978 x_insert_glyphs,
14979 x_clear_end_of_line,
14980 x_scroll_run,
14981 x_after_update_window_line,
14982 x_update_window_begin,
14983 x_update_window_end,
14984 XTcursor_to,
14985 x_flush,
14986 x_clear_mouse_face,
14987 x_get_glyph_overhangs,
14988 x_fix_overlapping_area
14989 };
14990
14991 void
14992 x_initialize ()
14993 {
14994 rif = &x_redisplay_interface;
14995
14996 clear_frame_hook = x_clear_frame;
14997 ins_del_lines_hook = x_ins_del_lines;
14998 delete_glyphs_hook = x_delete_glyphs;
14999 ring_bell_hook = XTring_bell;
15000 reset_terminal_modes_hook = XTreset_terminal_modes;
15001 set_terminal_modes_hook = XTset_terminal_modes;
15002 update_begin_hook = x_update_begin;
15003 update_end_hook = x_update_end;
15004 set_terminal_window_hook = XTset_terminal_window;
15005 read_socket_hook = XTread_socket;
15006 frame_up_to_date_hook = XTframe_up_to_date;
15007 mouse_position_hook = XTmouse_position;
15008 frame_rehighlight_hook = XTframe_rehighlight;
15009 frame_raise_lower_hook = XTframe_raise_lower;
15010 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
15011 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
15012 redeem_scroll_bar_hook = XTredeem_scroll_bar;
15013 judge_scroll_bars_hook = XTjudge_scroll_bars;
15014 estimate_mode_line_height_hook = x_estimate_mode_line_height;
15015
15016 scroll_region_ok = 1; /* we'll scroll partial frames */
15017 char_ins_del_ok = 1;
15018 line_ins_del_ok = 1; /* we'll just blt 'em */
15019 fast_clear_end_of_line = 1; /* X does this well */
15020 memory_below_frame = 0; /* we don't remember what scrolls
15021 off the bottom */
15022 baud_rate = 19200;
15023
15024 x_noop_count = 0;
15025 last_tool_bar_item = -1;
15026 any_help_event_p = 0;
15027
15028 /* Try to use interrupt input; if we can't, then start polling. */
15029 Fset_input_mode (Qt, Qnil, Qt, Qnil);
15030
15031 #ifdef USE_X_TOOLKIT
15032 XtToolkitInitialize ();
15033
15034 Xt_app_con = XtCreateApplicationContext ();
15035
15036 /* Register a converter from strings to pixels, which uses
15037 Emacs' color allocation infrastructure. */
15038 XtAppSetTypeConverter (Xt_app_con,
15039 XtRString, XtRPixel, cvt_string_to_pixel,
15040 cvt_string_to_pixel_args,
15041 XtNumber (cvt_string_to_pixel_args),
15042 XtCacheByDisplay, cvt_pixel_dtor);
15043
15044 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
15045
15046 /* Install an asynchronous timer that processes Xt timeout events
15047 every 0.1s. This is necessary because some widget sets use
15048 timeouts internally, for example the LessTif menu bar, or the
15049 Xaw3d scroll bar. When Xt timouts aren't processed, these
15050 widgets don't behave normally. */
15051 {
15052 EMACS_TIME interval;
15053 EMACS_SET_SECS_USECS (interval, 0, 100000);
15054 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
15055 }
15056 #endif
15057
15058 #ifdef USE_TOOLKIT_SCROLL_BARS
15059 xaw3d_arrow_scroll = False;
15060 xaw3d_pick_top = True;
15061 #endif
15062
15063 /* Note that there is no real way portable across R3/R4 to get the
15064 original error handler. */
15065 XSetErrorHandler (x_error_handler);
15066 XSetIOErrorHandler (x_io_error_quitter);
15067
15068 /* Disable Window Change signals; they are handled by X events. */
15069 #ifdef SIGWINCH
15070 signal (SIGWINCH, SIG_DFL);
15071 #endif /* ! defined (SIGWINCH) */
15072
15073 signal (SIGPIPE, x_connection_signal);
15074
15075 #ifdef HAVE_X_SM
15076 x_session_initialize ();
15077 #endif
15078 }
15079
15080
15081 void
15082 syms_of_xterm ()
15083 {
15084 staticpro (&x_error_message_string);
15085 x_error_message_string = Qnil;
15086
15087 staticpro (&x_display_name_list);
15088 x_display_name_list = Qnil;
15089
15090 staticpro (&last_mouse_scroll_bar);
15091 last_mouse_scroll_bar = Qnil;
15092
15093 staticpro (&Qvendor_specific_keysyms);
15094 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
15095
15096 staticpro (&last_mouse_press_frame);
15097 last_mouse_press_frame = Qnil;
15098
15099 help_echo = Qnil;
15100 staticpro (&help_echo);
15101 help_echo_object = Qnil;
15102 staticpro (&help_echo_object);
15103 help_echo_window = Qnil;
15104 staticpro (&help_echo_window);
15105 previous_help_echo = Qnil;
15106 staticpro (&previous_help_echo);
15107 help_echo_pos = -1;
15108
15109 DEFVAR_BOOL ("x-autoselect-window", &x_autoselect_window_p,
15110 doc: /* *Non-nil means autoselect window with mouse pointer. */);
15111 x_autoselect_window_p = 0;
15112
15113 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
15114 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
15115 For example, if a block cursor is over a tab, it will be drawn as
15116 wide as that tab on the display. */);
15117 x_stretch_cursor_p = 0;
15118
15119 DEFVAR_BOOL ("x-use-underline-position-properties",
15120 &x_use_underline_position_properties,
15121 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
15122 nil means ignore them. If you encounter fonts with bogus
15123 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
15124 to 4.1, set this to nil. */);
15125 x_use_underline_position_properties = 1;
15126
15127 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
15128 doc: /* What X toolkit scroll bars Emacs uses.
15129 A value of nil means Emacs doesn't use X toolkit scroll bars.
15130 Otherwise, value is a symbol describing the X toolkit. */);
15131 #ifdef USE_TOOLKIT_SCROLL_BARS
15132 #ifdef USE_MOTIF
15133 Vx_toolkit_scroll_bars = intern ("motif");
15134 #elif defined HAVE_XAW3D
15135 Vx_toolkit_scroll_bars = intern ("xaw3d");
15136 #else
15137 Vx_toolkit_scroll_bars = intern ("xaw");
15138 #endif
15139 #else
15140 Vx_toolkit_scroll_bars = Qnil;
15141 #endif
15142
15143 staticpro (&last_mouse_motion_frame);
15144 last_mouse_motion_frame = Qnil;
15145
15146 Qmodifier_value = intern ("modifier-value");
15147 Qalt = intern ("alt");
15148 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
15149 Qhyper = intern ("hyper");
15150 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
15151 Qmeta = intern ("meta");
15152 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
15153 Qsuper = intern ("super");
15154 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
15155
15156 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
15157 doc: /* Which keys Emacs uses for the alt modifier.
15158 This should be one of the symbols `alt', `hyper', `meta', `super'.
15159 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
15160 is nil, which is the same as `alt'. */);
15161 Vx_alt_keysym = Qnil;
15162
15163 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
15164 doc: /* Which keys Emacs uses for the hyper modifier.
15165 This should be one of the symbols `alt', `hyper', `meta', `super'.
15166 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
15167 default is nil, which is the same as `hyper'. */);
15168 Vx_hyper_keysym = Qnil;
15169
15170 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
15171 doc: /* Which keys Emacs uses for the meta modifier.
15172 This should be one of the symbols `alt', `hyper', `meta', `super'.
15173 For example, `meta' means use the Meta_L and Meta_R keysyms. The
15174 default is nil, which is the same as `meta'. */);
15175 Vx_meta_keysym = Qnil;
15176
15177 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
15178 doc: /* Which keys Emacs uses for the super modifier.
15179 This should be one of the symbols `alt', `hyper', `meta', `super'.
15180 For example, `super' means use the Super_L and Super_R keysyms. The
15181 default is nil, which is the same as `super'. */);
15182 Vx_super_keysym = Qnil;
15183
15184 }
15185
15186 #endif /* HAVE_X_WINDOWS */