(x_term_init): Use turn_on_atimers, not start_polling and stop_polling.
[bpt/emacs.git] / 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, 2002
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 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 mouse_autoselect_window;
259
260 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
261 static Lisp_Object last_window;
262
263 /* Non-zero means draw block and hollow cursor as wide as the glyph
264 under it. For example, if a block cursor is over a tab, it will be
265 drawn as wide as that tab on the display. */
266
267 int x_stretch_cursor_p;
268
269 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
270
271 int x_use_underline_position_properties;
272
273 /* This is a chain of structures for all the X displays currently in
274 use. */
275
276 struct x_display_info *x_display_list;
277
278 /* This is a list of cons cells, each of the form (NAME
279 . FONT-LIST-CACHE), one for each element of x_display_list and in
280 the same order. NAME is the name of the frame. FONT-LIST-CACHE
281 records previous values returned by x-list-fonts. */
282
283 Lisp_Object x_display_name_list;
284
285 /* Frame being updated by update_frame. This is declared in term.c.
286 This is set by update_begin and looked at by all the XT functions.
287 It is zero while not inside an update. In that case, the XT
288 functions assume that `selected_frame' is the frame to apply to. */
289
290 extern struct frame *updating_frame;
291
292 /* This is a frame waiting to be auto-raised, within XTread_socket. */
293
294 struct frame *pending_autoraise_frame;
295
296 #ifdef USE_X_TOOLKIT
297 /* The application context for Xt use. */
298 XtAppContext Xt_app_con;
299 static String Xt_default_resources[] = {0};
300 #endif /* USE_X_TOOLKIT */
301
302 /* Nominal cursor position -- where to draw output.
303 HPOS and VPOS are window relative glyph matrix coordinates.
304 X and Y are window relative pixel coordinates. */
305
306 struct cursor_pos output_cursor;
307
308 /* Non-zero means user is interacting with a toolkit scroll bar. */
309
310 static int toolkit_scroll_bar_interaction;
311
312 /* Mouse movement.
313
314 Formerly, we used PointerMotionHintMask (in standard_event_mask)
315 so that we would have to call XQueryPointer after each MotionNotify
316 event to ask for another such event. However, this made mouse tracking
317 slow, and there was a bug that made it eventually stop.
318
319 Simply asking for MotionNotify all the time seems to work better.
320
321 In order to avoid asking for motion events and then throwing most
322 of them away or busy-polling the server for mouse positions, we ask
323 the server for pointer motion hints. This means that we get only
324 one event per group of mouse movements. "Groups" are delimited by
325 other kinds of events (focus changes and button clicks, for
326 example), or by XQueryPointer calls; when one of these happens, we
327 get another MotionNotify event the next time the mouse moves. This
328 is at least as efficient as getting motion events when mouse
329 tracking is on, and I suspect only negligibly worse when tracking
330 is off. */
331
332 /* Where the mouse was last time we reported a mouse event. */
333
334 FRAME_PTR last_mouse_frame;
335 static XRectangle last_mouse_glyph;
336 static Lisp_Object last_mouse_press_frame;
337
338 /* The scroll bar in which the last X motion event occurred.
339
340 If the last X motion event occurred in a scroll bar, we set this so
341 XTmouse_position can know whether to report a scroll bar motion or
342 an ordinary motion.
343
344 If the last X motion event didn't occur in a scroll bar, we set
345 this to Qnil, to tell XTmouse_position to return an ordinary motion
346 event. */
347
348 static Lisp_Object last_mouse_scroll_bar;
349
350 /* This is a hack. We would really prefer that XTmouse_position would
351 return the time associated with the position it returns, but there
352 doesn't seem to be any way to wrest the time-stamp from the server
353 along with the position query. So, we just keep track of the time
354 of the last movement we received, and return that in hopes that
355 it's somewhat accurate. */
356
357 static Time last_mouse_movement_time;
358
359 /* Incremented by XTread_socket whenever it really tries to read
360 events. */
361
362 #ifdef __STDC__
363 static int volatile input_signal_count;
364 #else
365 static int input_signal_count;
366 #endif
367
368 /* Used locally within XTread_socket. */
369
370 static int x_noop_count;
371
372 /* Initial values of argv and argc. */
373
374 extern char **initial_argv;
375 extern int initial_argc;
376
377 extern Lisp_Object Vcommand_line_args, Vsystem_name;
378
379 /* Tells if a window manager is present or not. */
380
381 extern Lisp_Object Vx_no_window_manager;
382
383 extern Lisp_Object Qface, Qmouse_face, Qeql;
384
385 extern int errno;
386
387 /* A mask of extra modifier bits to put into every keyboard char. */
388
389 extern EMACS_INT extra_keyboard_modifiers;
390
391 /* The keysyms to use for the various modifiers. */
392
393 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
394 Lisp_Object Vx_keysym_table;
395 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
396
397 static Lisp_Object Qvendor_specific_keysyms;
398 static Lisp_Object Qlatin_1, Qutf_8;
399
400 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
401 extern Lisp_Object x_icon_type P_ ((struct frame *));
402
403
404 /* Enumeration for overriding/changing the face to use for drawing
405 glyphs in x_draw_glyphs. */
406
407 enum draw_glyphs_face
408 {
409 DRAW_NORMAL_TEXT,
410 DRAW_INVERSE_VIDEO,
411 DRAW_CURSOR,
412 DRAW_MOUSE_FACE,
413 DRAW_IMAGE_RAISED,
414 DRAW_IMAGE_SUNKEN
415 };
416
417 static int cursor_in_mouse_face_p P_ ((struct window *));
418 static int clear_mouse_face P_ ((struct x_display_info *));
419 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
420 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
421 static const XColor *x_color_cells P_ ((Display *, int *));
422 static void x_update_window_end P_ ((struct window *, int, int));
423 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
424 void x_delete_display P_ ((struct x_display_info *));
425 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
426 unsigned));
427 static int fast_find_position P_ ((struct window *, int, int *, int *,
428 int *, int *, Lisp_Object));
429 static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
430 int *, int *, int *, int *, int));
431 static void set_output_cursor P_ ((struct cursor_pos *));
432 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
433 int *, int *, int *, int));
434 static void note_mode_line_or_margin_highlight P_ ((struct window *, int,
435 int, int));
436 static void note_mouse_highlight P_ ((struct frame *, int, int));
437 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
438 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
439 static void show_mouse_face P_ ((struct x_display_info *,
440 enum draw_glyphs_face));
441 static int x_io_error_quitter P_ ((Display *));
442 int x_catch_errors P_ ((Display *));
443 void x_uncatch_errors P_ ((Display *, int));
444 void x_lower_frame P_ ((struct frame *));
445 void x_scroll_bar_clear P_ ((struct frame *));
446 int x_had_errors_p P_ ((Display *));
447 void x_wm_set_size_hint P_ ((struct frame *, long, int));
448 void x_raise_frame P_ ((struct frame *));
449 void x_set_window_size P_ ((struct frame *, int, int, int));
450 void x_wm_set_window_state P_ ((struct frame *, int));
451 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
452 void x_initialize P_ ((void));
453 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
454 static int x_compute_min_glyph_bounds P_ ((struct frame *));
455 static void x_draw_phys_cursor_glyph P_ ((struct window *,
456 struct glyph_row *,
457 enum draw_glyphs_face));
458 static void x_update_end P_ ((struct frame *));
459 static void XTframe_up_to_date P_ ((struct frame *));
460 static void XTset_terminal_modes P_ ((void));
461 static void XTreset_terminal_modes P_ ((void));
462 static void XTcursor_to P_ ((int, int, int, int));
463 static void x_write_glyphs P_ ((struct glyph *, int));
464 static void x_clear_end_of_line P_ ((int));
465 static void x_clear_frame P_ ((void));
466 static void x_clear_cursor P_ ((struct window *));
467 static void frame_highlight P_ ((struct frame *));
468 static void frame_unhighlight P_ ((struct frame *));
469 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
470 static int x_focus_changed P_ ((int,
471 int,
472 struct x_display_info *,
473 struct frame *,
474 struct input_event *,
475 int));
476 static int x_detect_focus_change P_ ((struct x_display_info *,
477 XEvent *,
478 struct input_event *,
479 int));
480 static void XTframe_rehighlight P_ ((struct frame *));
481 static void x_frame_rehighlight P_ ((struct x_display_info *));
482 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
483 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
484 enum text_cursor_kinds));
485 static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
486 XRectangle *));
487 static void expose_frame P_ ((struct frame *, int, int, int, int));
488 static int expose_window_tree P_ ((struct window *, XRectangle *));
489 static void expose_overlaps P_ ((struct window *, struct glyph_row *,
490 struct glyph_row *));
491 static int expose_window P_ ((struct window *, XRectangle *));
492 static void expose_area P_ ((struct window *, struct glyph_row *,
493 XRectangle *, enum glyph_row_area));
494 static int expose_line P_ ((struct window *, struct glyph_row *,
495 XRectangle *));
496 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
497 static void x_update_window_cursor P_ ((struct window *, int));
498 static void x_erase_phys_cursor P_ ((struct window *));
499 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
500 static void x_draw_fringe_bitmap P_ ((struct window *, struct glyph_row *,
501 enum fringe_bitmap_type, int left_p));
502
503 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
504 GC, int));
505 static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
506 static void x_draw_row_fringe_bitmaps P_ ((struct window *, struct glyph_row *));
507 static void notice_overwritten_cursor P_ ((struct window *, enum glyph_row_area,
508 int, int, int, int));
509 static void x_flush P_ ((struct frame *f));
510 static void x_update_begin P_ ((struct frame *));
511 static void x_update_window_begin P_ ((struct window *));
512 static void x_draw_vertical_border P_ ((struct window *));
513 static void x_after_update_window_line P_ ((struct glyph_row *));
514 static INLINE void take_vertical_position_into_account P_ ((struct it *));
515 static void x_produce_stretch_glyph P_ ((struct it *));
516 static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
517 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
518 enum scroll_bar_part *,
519 Lisp_Object *, Lisp_Object *,
520 unsigned long *));
521 static void x_check_fullscreen P_ ((struct frame *));
522 static void x_check_fullscreen_move P_ ((struct frame *));
523
524 /* Flush display of frame F, or of all frames if F is null. */
525
526 static void
527 x_flush (f)
528 struct frame *f;
529 {
530 BLOCK_INPUT;
531 if (f == NULL)
532 {
533 Lisp_Object rest, frame;
534 FOR_EACH_FRAME (rest, frame)
535 x_flush (XFRAME (frame));
536 }
537 else if (FRAME_X_P (f))
538 XFlush (FRAME_X_DISPLAY (f));
539 UNBLOCK_INPUT;
540 }
541
542
543 /* Remove calls to XFlush by defining XFlush to an empty replacement.
544 Calls to XFlush should be unnecessary because the X output buffer
545 is flushed automatically as needed by calls to XPending,
546 XNextEvent, or XWindowEvent according to the XFlush man page.
547 XTread_socket calls XPending. Removing XFlush improves
548 performance. */
549
550 #define XFlush(DISPLAY) (void) 0
551
552 \f
553 /***********************************************************************
554 Debugging
555 ***********************************************************************/
556
557 #if 0
558
559 /* This is a function useful for recording debugging information about
560 the sequence of occurrences in this file. */
561
562 struct record
563 {
564 char *locus;
565 int type;
566 };
567
568 struct record event_record[100];
569
570 int event_record_index;
571
572 record_event (locus, type)
573 char *locus;
574 int type;
575 {
576 if (event_record_index == sizeof (event_record) / sizeof (struct record))
577 event_record_index = 0;
578
579 event_record[event_record_index].locus = locus;
580 event_record[event_record_index].type = type;
581 event_record_index++;
582 }
583
584 #endif /* 0 */
585
586
587 \f
588 /* Return the struct x_display_info corresponding to DPY. */
589
590 struct x_display_info *
591 x_display_info_for_display (dpy)
592 Display *dpy;
593 {
594 struct x_display_info *dpyinfo;
595
596 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
597 if (dpyinfo->display == dpy)
598 return dpyinfo;
599
600 return 0;
601 }
602
603
604 \f
605 /***********************************************************************
606 Starting and ending an update
607 ***********************************************************************/
608
609 /* Start an update of frame F. This function is installed as a hook
610 for update_begin, i.e. it is called when update_begin is called.
611 This function is called prior to calls to x_update_window_begin for
612 each window being updated. Currently, there is nothing to do here
613 because all interesting stuff is done on a window basis. */
614
615 static void
616 x_update_begin (f)
617 struct frame *f;
618 {
619 /* Nothing to do. */
620 }
621
622
623 /* Start update of window W. Set the global variable updated_window
624 to the window being updated and set output_cursor to the cursor
625 position of W. */
626
627 static void
628 x_update_window_begin (w)
629 struct window *w;
630 {
631 struct frame *f = XFRAME (WINDOW_FRAME (w));
632 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
633
634 updated_window = w;
635 set_output_cursor (&w->cursor);
636
637 BLOCK_INPUT;
638
639 if (f == display_info->mouse_face_mouse_frame)
640 {
641 /* Don't do highlighting for mouse motion during the update. */
642 display_info->mouse_face_defer = 1;
643
644 /* If F needs to be redrawn, simply forget about any prior mouse
645 highlighting. */
646 if (FRAME_GARBAGED_P (f))
647 display_info->mouse_face_window = Qnil;
648
649 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
650 their mouse_face_p flag set, which means that they are always
651 unequal to rows in a desired matrix which never have that
652 flag set. So, rows containing mouse-face glyphs are never
653 scrolled, and we don't have to switch the mouse highlight off
654 here to prevent it from being scrolled. */
655
656 /* Can we tell that this update does not affect the window
657 where the mouse highlight is? If so, no need to turn off.
658 Likewise, don't do anything if the frame is garbaged;
659 in that case, the frame's current matrix that we would use
660 is all wrong, and we will redisplay that line anyway. */
661 if (!NILP (display_info->mouse_face_window)
662 && w == XWINDOW (display_info->mouse_face_window))
663 {
664 int i;
665
666 for (i = 0; i < w->desired_matrix->nrows; ++i)
667 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
668 break;
669
670 if (i < w->desired_matrix->nrows)
671 clear_mouse_face (display_info);
672 }
673 #endif /* 0 */
674 }
675
676 UNBLOCK_INPUT;
677 }
678
679
680 /* Draw a vertical window border to the right of window W if W doesn't
681 have vertical scroll bars. */
682
683 static void
684 x_draw_vertical_border (w)
685 struct window *w;
686 {
687 struct frame *f = XFRAME (WINDOW_FRAME (w));
688
689 /* Redraw borders between horizontally adjacent windows. Don't
690 do it for frames with vertical scroll bars because either the
691 right scroll bar of a window, or the left scroll bar of its
692 neighbor will suffice as a border. */
693 if (!WINDOW_RIGHTMOST_P (w)
694 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
695 {
696 int x0, x1, y0, y1;
697
698 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
699 x1 += FRAME_X_RIGHT_FRINGE_WIDTH (f);
700 y1 -= 1;
701
702 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
703 f->output_data.x->normal_gc, x1, y0, x1, y1);
704 }
705 }
706
707
708 /* End update of window W (which is equal to updated_window).
709
710 Draw vertical borders between horizontally adjacent windows, and
711 display W's cursor if CURSOR_ON_P is non-zero.
712
713 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
714 glyphs in mouse-face were overwritten. In that case we have to
715 make sure that the mouse-highlight is properly redrawn.
716
717 W may be a menu bar pseudo-window in case we don't have X toolkit
718 support. Such windows don't have a cursor, so don't display it
719 here. */
720
721 static void
722 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
723 struct window *w;
724 int cursor_on_p, mouse_face_overwritten_p;
725 {
726 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
727
728 if (!w->pseudo_window_p)
729 {
730 BLOCK_INPUT;
731
732 if (cursor_on_p)
733 x_display_and_set_cursor (w, 1, output_cursor.hpos,
734 output_cursor.vpos,
735 output_cursor.x, output_cursor.y);
736
737 x_draw_vertical_border (w);
738 UNBLOCK_INPUT;
739 }
740
741 /* If a row with mouse-face was overwritten, arrange for
742 XTframe_up_to_date to redisplay the mouse highlight. */
743 if (mouse_face_overwritten_p)
744 {
745 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
746 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
747 dpyinfo->mouse_face_window = Qnil;
748 }
749
750 updated_window = NULL;
751 }
752
753
754 /* End update of frame F. This function is installed as a hook in
755 update_end. */
756
757 static void
758 x_update_end (f)
759 struct frame *f;
760 {
761 /* Mouse highlight may be displayed again. */
762 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
763
764 BLOCK_INPUT;
765 XFlush (FRAME_X_DISPLAY (f));
766 UNBLOCK_INPUT;
767 }
768
769
770 /* This function is called from various places in xdisp.c whenever a
771 complete update has been performed. The global variable
772 updated_window is not available here. */
773
774 static void
775 XTframe_up_to_date (f)
776 struct frame *f;
777 {
778 if (FRAME_X_P (f))
779 {
780 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
781
782 if (dpyinfo->mouse_face_deferred_gc
783 || f == dpyinfo->mouse_face_mouse_frame)
784 {
785 BLOCK_INPUT;
786 if (dpyinfo->mouse_face_mouse_frame)
787 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
788 dpyinfo->mouse_face_mouse_x,
789 dpyinfo->mouse_face_mouse_y);
790 dpyinfo->mouse_face_deferred_gc = 0;
791 UNBLOCK_INPUT;
792 }
793 }
794 }
795
796
797 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
798 arrow bitmaps, or clear the fringes if no bitmaps are required
799 before DESIRED_ROW is made current. The window being updated is
800 found in updated_window. This function It is called from
801 update_window_line only if it is known that there are differences
802 between bitmaps to be drawn between current row and DESIRED_ROW. */
803
804 static void
805 x_after_update_window_line (desired_row)
806 struct glyph_row *desired_row;
807 {
808 struct window *w = updated_window;
809 struct frame *f;
810 int width, height;
811
812 xassert (w);
813
814 if (!desired_row->mode_line_p && !w->pseudo_window_p)
815 {
816 BLOCK_INPUT;
817 x_draw_row_fringe_bitmaps (w, desired_row);
818 UNBLOCK_INPUT;
819 }
820
821 /* When a window has disappeared, make sure that no rest of
822 full-width rows stays visible in the internal border. Could
823 check here if updated_window is the leftmost/rightmost window,
824 but I guess it's not worth doing since vertically split windows
825 are almost never used, internal border is rarely set, and the
826 overhead is very small. */
827 if (windows_or_buffers_changed
828 && desired_row->full_width_p
829 && (f = XFRAME (w->frame),
830 width = FRAME_INTERNAL_BORDER_WIDTH (f),
831 width != 0)
832 && (height = desired_row->visible_height,
833 height > 0))
834 {
835 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
836
837 /* Internal border is drawn below the tool bar. */
838 if (WINDOWP (f->tool_bar_window)
839 && w == XWINDOW (f->tool_bar_window))
840 y -= width;
841
842 BLOCK_INPUT;
843 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
844 0, y, width, height, False);
845 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
846 f->output_data.x->pixel_width - width,
847 y, width, height, False);
848 UNBLOCK_INPUT;
849 }
850 }
851
852
853 /* Draw the bitmap WHICH in one of the left or right fringes of
854 window W. ROW is the glyph row for which to display the bitmap; it
855 determines the vertical position at which the bitmap has to be
856 drawn. */
857
858 static void
859 x_draw_fringe_bitmap (w, row, which, left_p)
860 struct window *w;
861 struct glyph_row *row;
862 enum fringe_bitmap_type which;
863 int left_p;
864 {
865 struct frame *f = XFRAME (WINDOW_FRAME (w));
866 Display *display = FRAME_X_DISPLAY (f);
867 Window window = FRAME_X_WINDOW (f);
868 int x, y, wd, h, dy;
869 int b1, b2;
870 unsigned char *bits = NULL;
871 Pixmap pixmap;
872 GC gc = f->output_data.x->normal_gc;
873 struct face *face;
874 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
875
876 /* Must clip because of partially visible lines. */
877 x_clip_to_row (w, row, gc, 1);
878
879 /* Convert row to frame coordinates. */
880 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
881
882 switch (which)
883 {
884 case NO_FRINGE_BITMAP:
885 wd = 0;
886 h = 0;
887 break;
888
889 case LEFT_TRUNCATION_BITMAP:
890 wd = left_width;
891 h = left_height;
892 bits = left_bits;
893 break;
894
895 case OVERLAY_ARROW_BITMAP:
896 wd = ov_width;
897 h = ov_height;
898 bits = ov_bits;
899 break;
900
901 case RIGHT_TRUNCATION_BITMAP:
902 wd = right_width;
903 h = right_height;
904 bits = right_bits;
905 break;
906
907 case CONTINUED_LINE_BITMAP:
908 wd = continued_width;
909 h = continued_height;
910 bits = continued_bits;
911 break;
912
913 case CONTINUATION_LINE_BITMAP:
914 wd = continuation_width;
915 h = continuation_height;
916 bits = continuation_bits;
917 break;
918
919 case ZV_LINE_BITMAP:
920 wd = zv_width;
921 h = zv_height - (y % zv_period);
922 bits = zv_bits + (y % zv_period);
923 break;
924
925 default:
926 abort ();
927 }
928
929 /* Clip bitmap if too high. */
930 if (h > row->height)
931 h = row->height;
932
933 /* Set dy to the offset in the row to start drawing the bitmap. */
934 dy = (row->height - h) / 2;
935
936 face = FACE_FROM_ID (f, FRINGE_FACE_ID);
937 PREPARE_FACE_FOR_DISPLAY (f, face);
938
939 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
940 the fringe. */
941 b1 = b2 = -1;
942 if (left_p)
943 {
944 if (wd > FRAME_X_LEFT_FRINGE_WIDTH (f))
945 wd = FRAME_X_LEFT_FRINGE_WIDTH (f);
946 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
947 - wd
948 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - wd) / 2);
949 if (wd < FRAME_X_LEFT_FRINGE_WIDTH (f) || row->height > h)
950 {
951 /* If W has a vertical border to its left, don't draw over it. */
952 int border = ((XFASTINT (w->left) > 0
953 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
954 ? 1 : 0);
955 b1 = (window_box_left (w, -1)
956 - FRAME_X_LEFT_FRINGE_WIDTH (f)
957 + border);
958 b2 = (FRAME_X_LEFT_FRINGE_WIDTH (f) - border);
959 }
960 }
961 else
962 {
963 if (wd > FRAME_X_RIGHT_FRINGE_WIDTH (f))
964 wd = FRAME_X_RIGHT_FRINGE_WIDTH (f);
965 x = (window_box_right (w, -1)
966 + (FRAME_X_RIGHT_FRINGE_WIDTH (f) - wd) / 2);
967 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
968 the fringe. */
969 if (wd < FRAME_X_RIGHT_FRINGE_WIDTH (f) || row->height > h)
970 {
971 b1 = window_box_right (w, -1);
972 b2 = FRAME_X_RIGHT_FRINGE_WIDTH (f);
973 }
974 }
975
976 if (b1 >= 0)
977 {
978 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
979
980 /* In case the same realized face is used for fringes and
981 for something displayed in the text (e.g. face `region' on
982 mono-displays, the fill style may have been changed to
983 FillSolid in x_draw_glyph_string_background. */
984 if (face->stipple)
985 XSetFillStyle (display, face->gc, FillOpaqueStippled);
986 else
987 XSetForeground (display, face->gc, face->background);
988
989 XFillRectangle (display, window, face->gc,
990 b1,
991 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
992 row->y)),
993 b2,
994 row->visible_height);
995 if (!face->stipple)
996 XSetForeground (display, face->gc, face->foreground);
997 }
998
999 if (which != NO_FRINGE_BITMAP)
1000 {
1001 /* Draw the bitmap. I believe these small pixmaps can be cached
1002 by the server. */
1003 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
1004 face->foreground,
1005 face->background, depth);
1006 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
1007 XFreePixmap (display, pixmap);
1008 }
1009
1010 XSetClipMask (display, gc, None);
1011 }
1012
1013
1014 /* Draw fringe bitmaps for glyph row ROW on window W. Call this
1015 function with input blocked. */
1016
1017 static void
1018 x_draw_row_fringe_bitmaps (w, row)
1019 struct window *w;
1020 struct glyph_row *row;
1021 {
1022 struct frame *f = XFRAME (w->frame);
1023 enum fringe_bitmap_type bitmap;
1024
1025 xassert (interrupt_input_blocked);
1026
1027 /* If row is completely invisible, because of vscrolling, we
1028 don't have to draw anything. */
1029 if (row->visible_height <= 0)
1030 return;
1031
1032 if (FRAME_X_LEFT_FRINGE_WIDTH (f) != 0)
1033 {
1034 /* Decide which bitmap to draw in the left fringe. */
1035 if (row->overlay_arrow_p)
1036 bitmap = OVERLAY_ARROW_BITMAP;
1037 else if (row->truncated_on_left_p)
1038 bitmap = LEFT_TRUNCATION_BITMAP;
1039 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
1040 bitmap = CONTINUATION_LINE_BITMAP;
1041 else if (row->indicate_empty_line_p)
1042 bitmap = ZV_LINE_BITMAP;
1043 else
1044 bitmap = NO_FRINGE_BITMAP;
1045
1046 x_draw_fringe_bitmap (w, row, bitmap, 1);
1047 }
1048
1049 if (FRAME_X_RIGHT_FRINGE_WIDTH (f) != 0)
1050 {
1051 /* Decide which bitmap to draw in the right fringe. */
1052 if (row->truncated_on_right_p)
1053 bitmap = RIGHT_TRUNCATION_BITMAP;
1054 else if (row->continued_p)
1055 bitmap = CONTINUED_LINE_BITMAP;
1056 else if (row->indicate_empty_line_p && FRAME_X_LEFT_FRINGE_WIDTH (f) == 0)
1057 bitmap = ZV_LINE_BITMAP;
1058 else
1059 bitmap = NO_FRINGE_BITMAP;
1060
1061 x_draw_fringe_bitmap (w, row, bitmap, 0);
1062 }
1063 }
1064
1065 \f
1066
1067 /* This is called when starting Emacs and when restarting after
1068 suspend. When starting Emacs, no X window is mapped. And nothing
1069 must be done to Emacs's own window if it is suspended (though that
1070 rarely happens). */
1071
1072 static void
1073 XTset_terminal_modes ()
1074 {
1075 }
1076
1077 /* This is called when exiting or suspending Emacs. Exiting will make
1078 the X-windows go away, and suspending requires no action. */
1079
1080 static void
1081 XTreset_terminal_modes ()
1082 {
1083 }
1084
1085
1086 \f
1087 /***********************************************************************
1088 Output Cursor
1089 ***********************************************************************/
1090
1091 /* Set the global variable output_cursor to CURSOR. All cursor
1092 positions are relative to updated_window. */
1093
1094 static void
1095 set_output_cursor (cursor)
1096 struct cursor_pos *cursor;
1097 {
1098 output_cursor.hpos = cursor->hpos;
1099 output_cursor.vpos = cursor->vpos;
1100 output_cursor.x = cursor->x;
1101 output_cursor.y = cursor->y;
1102 }
1103
1104
1105 /* Set a nominal cursor position.
1106
1107 HPOS and VPOS are column/row positions in a window glyph matrix. X
1108 and Y are window text area relative pixel positions.
1109
1110 If this is done during an update, updated_window will contain the
1111 window that is being updated and the position is the future output
1112 cursor position for that window. If updated_window is null, use
1113 selected_window and display the cursor at the given position. */
1114
1115 static void
1116 XTcursor_to (vpos, hpos, y, x)
1117 int vpos, hpos, y, x;
1118 {
1119 struct window *w;
1120
1121 /* If updated_window is not set, work on selected_window. */
1122 if (updated_window)
1123 w = updated_window;
1124 else
1125 w = XWINDOW (selected_window);
1126
1127 /* Set the output cursor. */
1128 output_cursor.hpos = hpos;
1129 output_cursor.vpos = vpos;
1130 output_cursor.x = x;
1131 output_cursor.y = y;
1132
1133 /* If not called as part of an update, really display the cursor.
1134 This will also set the cursor position of W. */
1135 if (updated_window == NULL)
1136 {
1137 BLOCK_INPUT;
1138 x_display_cursor (w, 1, hpos, vpos, x, y);
1139 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1140 UNBLOCK_INPUT;
1141 }
1142 }
1143
1144
1145 \f
1146 /***********************************************************************
1147 Display Iterator
1148 ***********************************************************************/
1149
1150 /* Function prototypes of this page. */
1151
1152 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1153 struct glyph *,
1154 XChar2b *,
1155 int *));
1156 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1157 int, XChar2b *, int,
1158 int));
1159 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1160 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1161 static void x_append_glyph P_ ((struct it *));
1162 static void x_append_composite_glyph P_ ((struct it *));
1163 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1164 int, int, double));
1165 static void x_produce_glyphs P_ ((struct it *));
1166 static void x_produce_image_glyph P_ ((struct it *it));
1167
1168
1169 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1170 is not contained in the font. */
1171
1172 static INLINE XCharStruct *
1173 x_per_char_metric (font, char2b)
1174 XFontStruct *font;
1175 XChar2b *char2b;
1176 {
1177 /* The result metric information. */
1178 XCharStruct *pcm = NULL;
1179
1180 xassert (font && char2b);
1181
1182 if (font->per_char != NULL)
1183 {
1184 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1185 {
1186 /* min_char_or_byte2 specifies the linear character index
1187 corresponding to the first element of the per_char array,
1188 max_char_or_byte2 is the index of the last character. A
1189 character with non-zero CHAR2B->byte1 is not in the font.
1190 A character with byte2 less than min_char_or_byte2 or
1191 greater max_char_or_byte2 is not in the font. */
1192 if (char2b->byte1 == 0
1193 && char2b->byte2 >= font->min_char_or_byte2
1194 && char2b->byte2 <= font->max_char_or_byte2)
1195 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1196 }
1197 else
1198 {
1199 /* If either min_byte1 or max_byte1 are nonzero, both
1200 min_char_or_byte2 and max_char_or_byte2 are less than
1201 256, and the 2-byte character index values corresponding
1202 to the per_char array element N (counting from 0) are:
1203
1204 byte1 = N/D + min_byte1
1205 byte2 = N\D + min_char_or_byte2
1206
1207 where:
1208
1209 D = max_char_or_byte2 - min_char_or_byte2 + 1
1210 / = integer division
1211 \ = integer modulus */
1212 if (char2b->byte1 >= font->min_byte1
1213 && char2b->byte1 <= font->max_byte1
1214 && char2b->byte2 >= font->min_char_or_byte2
1215 && char2b->byte2 <= font->max_char_or_byte2)
1216 {
1217 pcm = (font->per_char
1218 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1219 * (char2b->byte1 - font->min_byte1))
1220 + (char2b->byte2 - font->min_char_or_byte2));
1221 }
1222 }
1223 }
1224 else
1225 {
1226 /* If the per_char pointer is null, all glyphs between the first
1227 and last character indexes inclusive have the same
1228 information, as given by both min_bounds and max_bounds. */
1229 if (char2b->byte2 >= font->min_char_or_byte2
1230 && char2b->byte2 <= font->max_char_or_byte2)
1231 pcm = &font->max_bounds;
1232 }
1233
1234 return ((pcm == NULL
1235 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1236 ? NULL : pcm);
1237 }
1238
1239
1240 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1241 the two-byte form of C. Encoding is returned in *CHAR2B. */
1242
1243 static INLINE void
1244 x_encode_char (c, char2b, font_info)
1245 int c;
1246 XChar2b *char2b;
1247 struct font_info *font_info;
1248 {
1249 int charset = CHAR_CHARSET (c);
1250 XFontStruct *font = font_info->font;
1251
1252 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1253 This may be either a program in a special encoder language or a
1254 fixed encoding. */
1255 if (font_info->font_encoder)
1256 {
1257 /* It's a program. */
1258 struct ccl_program *ccl = font_info->font_encoder;
1259
1260 if (CHARSET_DIMENSION (charset) == 1)
1261 {
1262 ccl->reg[0] = charset;
1263 ccl->reg[1] = char2b->byte2;
1264 ccl->reg[2] = -1;
1265 }
1266 else
1267 {
1268 ccl->reg[0] = charset;
1269 ccl->reg[1] = char2b->byte1;
1270 ccl->reg[2] = char2b->byte2;
1271 }
1272
1273 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1274
1275 /* We assume that MSBs are appropriately set/reset by CCL
1276 program. */
1277 if (font->max_byte1 == 0) /* 1-byte font */
1278 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1279 else
1280 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1281 }
1282 else if (font_info->encoding[charset])
1283 {
1284 /* Fixed encoding scheme. See fontset.h for the meaning of the
1285 encoding numbers. */
1286 int enc = font_info->encoding[charset];
1287
1288 if ((enc == 1 || enc == 2)
1289 && CHARSET_DIMENSION (charset) == 2)
1290 char2b->byte1 |= 0x80;
1291
1292 if (enc == 1 || enc == 3)
1293 char2b->byte2 |= 0x80;
1294 }
1295 }
1296
1297
1298 /* Get face and two-byte form of character C in face FACE_ID on frame
1299 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1300 means we want to display multibyte text. DISPLAY_P non-zero means
1301 make sure that X resources for the face returned are allocated.
1302 Value is a pointer to a realized face that is ready for display if
1303 DISPLAY_P is non-zero. */
1304
1305 static INLINE struct face *
1306 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
1307 struct frame *f;
1308 int c, face_id;
1309 XChar2b *char2b;
1310 int multibyte_p, display_p;
1311 {
1312 struct face *face = FACE_FROM_ID (f, face_id);
1313
1314 if (!multibyte_p)
1315 {
1316 /* Unibyte case. We don't have to encode, but we have to make
1317 sure to use a face suitable for unibyte. */
1318 char2b->byte1 = 0;
1319 char2b->byte2 = c;
1320 face_id = FACE_FOR_CHAR (f, face, c);
1321 face = FACE_FROM_ID (f, face_id);
1322 }
1323 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1324 {
1325 /* Case of ASCII in a face known to fit ASCII. */
1326 char2b->byte1 = 0;
1327 char2b->byte2 = c;
1328 }
1329 else
1330 {
1331 int c1, c2, charset;
1332
1333 /* Split characters into bytes. If c2 is -1 afterwards, C is
1334 really a one-byte character so that byte1 is zero. */
1335 SPLIT_CHAR (c, charset, c1, c2);
1336 if (c2 > 0)
1337 char2b->byte1 = c1, char2b->byte2 = c2;
1338 else
1339 char2b->byte1 = 0, char2b->byte2 = c1;
1340
1341 /* Maybe encode the character in *CHAR2B. */
1342 if (face->font != NULL)
1343 {
1344 struct font_info *font_info
1345 = FONT_INFO_FROM_ID (f, face->font_info_id);
1346 if (font_info)
1347 x_encode_char (c, char2b, font_info);
1348 }
1349 }
1350
1351 /* Make sure X resources of the face are allocated. */
1352 if (display_p)
1353 {
1354 xassert (face != NULL);
1355 PREPARE_FACE_FOR_DISPLAY (f, face);
1356 }
1357
1358 return face;
1359 }
1360
1361
1362 /* Get face and two-byte form of character glyph GLYPH on frame F.
1363 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1364 a pointer to a realized face that is ready for display. */
1365
1366 static INLINE struct face *
1367 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1368 struct frame *f;
1369 struct glyph *glyph;
1370 XChar2b *char2b;
1371 int *two_byte_p;
1372 {
1373 struct face *face;
1374
1375 xassert (glyph->type == CHAR_GLYPH);
1376 face = FACE_FROM_ID (f, glyph->face_id);
1377
1378 if (two_byte_p)
1379 *two_byte_p = 0;
1380
1381 if (!glyph->multibyte_p)
1382 {
1383 /* Unibyte case. We don't have to encode, but we have to make
1384 sure to use a face suitable for unibyte. */
1385 char2b->byte1 = 0;
1386 char2b->byte2 = glyph->u.ch;
1387 }
1388 else if (glyph->u.ch < 128
1389 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1390 {
1391 /* Case of ASCII in a face known to fit ASCII. */
1392 char2b->byte1 = 0;
1393 char2b->byte2 = glyph->u.ch;
1394 }
1395 else
1396 {
1397 int c1, c2, charset;
1398
1399 /* Split characters into bytes. If c2 is -1 afterwards, C is
1400 really a one-byte character so that byte1 is zero. */
1401 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1402 if (c2 > 0)
1403 char2b->byte1 = c1, char2b->byte2 = c2;
1404 else
1405 char2b->byte1 = 0, char2b->byte2 = c1;
1406
1407 /* Maybe encode the character in *CHAR2B. */
1408 if (charset != CHARSET_ASCII)
1409 {
1410 struct font_info *font_info
1411 = FONT_INFO_FROM_ID (f, face->font_info_id);
1412 if (font_info)
1413 {
1414 x_encode_char (glyph->u.ch, char2b, font_info);
1415 if (two_byte_p)
1416 *two_byte_p
1417 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1418 }
1419 }
1420 }
1421
1422 /* Make sure X resources of the face are allocated. */
1423 xassert (face != NULL);
1424 PREPARE_FACE_FOR_DISPLAY (f, face);
1425 return face;
1426 }
1427
1428
1429 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1430 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1431
1432 static INLINE void
1433 x_append_glyph (it)
1434 struct it *it;
1435 {
1436 struct glyph *glyph;
1437 enum glyph_row_area area = it->area;
1438
1439 xassert (it->glyph_row);
1440 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1441
1442 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1443 if (glyph < it->glyph_row->glyphs[area + 1])
1444 {
1445 glyph->charpos = CHARPOS (it->position);
1446 glyph->object = it->object;
1447 glyph->pixel_width = it->pixel_width;
1448 glyph->voffset = it->voffset;
1449 glyph->type = CHAR_GLYPH;
1450 glyph->multibyte_p = it->multibyte_p;
1451 glyph->left_box_line_p = it->start_of_box_run_p;
1452 glyph->right_box_line_p = it->end_of_box_run_p;
1453 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1454 || it->phys_descent > it->descent);
1455 glyph->padding_p = 0;
1456 glyph->glyph_not_available_p = it->glyph_not_available_p;
1457 glyph->face_id = it->face_id;
1458 glyph->u.ch = it->char_to_display;
1459 ++it->glyph_row->used[area];
1460 }
1461 }
1462
1463 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1464 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1465
1466 static INLINE void
1467 x_append_composite_glyph (it)
1468 struct it *it;
1469 {
1470 struct glyph *glyph;
1471 enum glyph_row_area area = it->area;
1472
1473 xassert (it->glyph_row);
1474
1475 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1476 if (glyph < it->glyph_row->glyphs[area + 1])
1477 {
1478 glyph->charpos = CHARPOS (it->position);
1479 glyph->object = it->object;
1480 glyph->pixel_width = it->pixel_width;
1481 glyph->voffset = it->voffset;
1482 glyph->type = COMPOSITE_GLYPH;
1483 glyph->multibyte_p = it->multibyte_p;
1484 glyph->left_box_line_p = it->start_of_box_run_p;
1485 glyph->right_box_line_p = it->end_of_box_run_p;
1486 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1487 || it->phys_descent > it->descent);
1488 glyph->padding_p = 0;
1489 glyph->glyph_not_available_p = 0;
1490 glyph->face_id = it->face_id;
1491 glyph->u.cmp_id = it->cmp_id;
1492 ++it->glyph_row->used[area];
1493 }
1494 }
1495
1496
1497 /* Change IT->ascent and IT->height according to the setting of
1498 IT->voffset. */
1499
1500 static INLINE void
1501 take_vertical_position_into_account (it)
1502 struct it *it;
1503 {
1504 if (it->voffset)
1505 {
1506 if (it->voffset < 0)
1507 /* Increase the ascent so that we can display the text higher
1508 in the line. */
1509 it->ascent += abs (it->voffset);
1510 else
1511 /* Increase the descent so that we can display the text lower
1512 in the line. */
1513 it->descent += it->voffset;
1514 }
1515 }
1516
1517
1518 /* Produce glyphs/get display metrics for the image IT is loaded with.
1519 See the description of struct display_iterator in dispextern.h for
1520 an overview of struct display_iterator. */
1521
1522 static void
1523 x_produce_image_glyph (it)
1524 struct it *it;
1525 {
1526 struct image *img;
1527 struct face *face;
1528
1529 xassert (it->what == IT_IMAGE);
1530
1531 face = FACE_FROM_ID (it->f, it->face_id);
1532 img = IMAGE_FROM_ID (it->f, it->image_id);
1533 xassert (img);
1534
1535 /* Make sure X resources of the face and image are loaded. */
1536 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1537 prepare_image_for_display (it->f, img);
1538
1539 it->ascent = it->phys_ascent = image_ascent (img, face);
1540 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1541 it->pixel_width = img->width + 2 * img->hmargin;
1542
1543 it->nglyphs = 1;
1544
1545 if (face->box != FACE_NO_BOX)
1546 {
1547 if (face->box_line_width > 0)
1548 {
1549 it->ascent += face->box_line_width;
1550 it->descent += face->box_line_width;
1551 }
1552
1553 if (it->start_of_box_run_p)
1554 it->pixel_width += abs (face->box_line_width);
1555 if (it->end_of_box_run_p)
1556 it->pixel_width += abs (face->box_line_width);
1557 }
1558
1559 take_vertical_position_into_account (it);
1560
1561 if (it->glyph_row)
1562 {
1563 struct glyph *glyph;
1564 enum glyph_row_area area = it->area;
1565
1566 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1567 if (glyph < it->glyph_row->glyphs[area + 1])
1568 {
1569 glyph->charpos = CHARPOS (it->position);
1570 glyph->object = it->object;
1571 glyph->pixel_width = it->pixel_width;
1572 glyph->voffset = it->voffset;
1573 glyph->type = IMAGE_GLYPH;
1574 glyph->multibyte_p = it->multibyte_p;
1575 glyph->left_box_line_p = it->start_of_box_run_p;
1576 glyph->right_box_line_p = it->end_of_box_run_p;
1577 glyph->overlaps_vertically_p = 0;
1578 glyph->padding_p = 0;
1579 glyph->glyph_not_available_p = 0;
1580 glyph->face_id = it->face_id;
1581 glyph->u.img_id = img->id;
1582 ++it->glyph_row->used[area];
1583 }
1584 }
1585 }
1586
1587
1588 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1589 of the glyph, WIDTH and HEIGHT are the width and height of the
1590 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1591 ascent of the glyph (0 <= ASCENT <= 1). */
1592
1593 static void
1594 x_append_stretch_glyph (it, object, width, height, ascent)
1595 struct it *it;
1596 Lisp_Object object;
1597 int width, height;
1598 double ascent;
1599 {
1600 struct glyph *glyph;
1601 enum glyph_row_area area = it->area;
1602
1603 xassert (ascent >= 0 && ascent <= 1);
1604
1605 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1606 if (glyph < it->glyph_row->glyphs[area + 1])
1607 {
1608 glyph->charpos = CHARPOS (it->position);
1609 glyph->object = object;
1610 glyph->pixel_width = width;
1611 glyph->voffset = it->voffset;
1612 glyph->type = STRETCH_GLYPH;
1613 glyph->multibyte_p = it->multibyte_p;
1614 glyph->left_box_line_p = it->start_of_box_run_p;
1615 glyph->right_box_line_p = it->end_of_box_run_p;
1616 glyph->overlaps_vertically_p = 0;
1617 glyph->padding_p = 0;
1618 glyph->glyph_not_available_p = 0;
1619 glyph->face_id = it->face_id;
1620 glyph->u.stretch.ascent = height * ascent;
1621 glyph->u.stretch.height = height;
1622 ++it->glyph_row->used[area];
1623 }
1624 }
1625
1626
1627 /* Produce a stretch glyph for iterator IT. IT->object is the value
1628 of the glyph property displayed. The value must be a list
1629 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1630 being recognized:
1631
1632 1. `:width WIDTH' specifies that the space should be WIDTH *
1633 canonical char width wide. WIDTH may be an integer or floating
1634 point number.
1635
1636 2. `:relative-width FACTOR' specifies that the width of the stretch
1637 should be computed from the width of the first character having the
1638 `glyph' property, and should be FACTOR times that width.
1639
1640 3. `:align-to HPOS' specifies that the space should be wide enough
1641 to reach HPOS, a value in canonical character units.
1642
1643 Exactly one of the above pairs must be present.
1644
1645 4. `:height HEIGHT' specifies that the height of the stretch produced
1646 should be HEIGHT, measured in canonical character units.
1647
1648 5. `:relative-height FACTOR' specifies that the height of the
1649 stretch should be FACTOR times the height of the characters having
1650 the glyph property.
1651
1652 Either none or exactly one of 4 or 5 must be present.
1653
1654 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1655 of the stretch should be used for the ascent of the stretch.
1656 ASCENT must be in the range 0 <= ASCENT <= 100. */
1657
1658 #define NUMVAL(X) \
1659 ((INTEGERP (X) || FLOATP (X)) \
1660 ? XFLOATINT (X) \
1661 : - 1)
1662
1663
1664 static void
1665 x_produce_stretch_glyph (it)
1666 struct it *it;
1667 {
1668 /* (space :width WIDTH :height HEIGHT. */
1669 #if GLYPH_DEBUG
1670 extern Lisp_Object Qspace;
1671 #endif
1672 extern Lisp_Object QCwidth, QCheight, QCascent;
1673 extern Lisp_Object QCrelative_width, QCrelative_height;
1674 extern Lisp_Object QCalign_to;
1675 Lisp_Object prop, plist;
1676 double width = 0, height = 0, ascent = 0;
1677 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1678 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1679
1680 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1681
1682 /* List should start with `space'. */
1683 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1684 plist = XCDR (it->object);
1685
1686 /* Compute the width of the stretch. */
1687 if (prop = Fplist_get (plist, QCwidth),
1688 NUMVAL (prop) > 0)
1689 /* Absolute width `:width WIDTH' specified and valid. */
1690 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1691 else if (prop = Fplist_get (plist, QCrelative_width),
1692 NUMVAL (prop) > 0)
1693 {
1694 /* Relative width `:relative-width FACTOR' specified and valid.
1695 Compute the width of the characters having the `glyph'
1696 property. */
1697 struct it it2;
1698 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1699
1700 it2 = *it;
1701 if (it->multibyte_p)
1702 {
1703 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1704 - IT_BYTEPOS (*it));
1705 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1706 }
1707 else
1708 it2.c = *p, it2.len = 1;
1709
1710 it2.glyph_row = NULL;
1711 it2.what = IT_CHARACTER;
1712 x_produce_glyphs (&it2);
1713 width = NUMVAL (prop) * it2.pixel_width;
1714 }
1715 else if (prop = Fplist_get (plist, QCalign_to),
1716 NUMVAL (prop) > 0)
1717 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1718 else
1719 /* Nothing specified -> width defaults to canonical char width. */
1720 width = CANON_X_UNIT (it->f);
1721
1722 /* Compute height. */
1723 if (prop = Fplist_get (plist, QCheight),
1724 NUMVAL (prop) > 0)
1725 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1726 else if (prop = Fplist_get (plist, QCrelative_height),
1727 NUMVAL (prop) > 0)
1728 height = FONT_HEIGHT (font) * NUMVAL (prop);
1729 else
1730 height = FONT_HEIGHT (font);
1731
1732 /* Compute percentage of height used for ascent. If
1733 `:ascent ASCENT' is present and valid, use that. Otherwise,
1734 derive the ascent from the font in use. */
1735 if (prop = Fplist_get (plist, QCascent),
1736 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1737 ascent = NUMVAL (prop) / 100.0;
1738 else
1739 ascent = (double) font->ascent / FONT_HEIGHT (font);
1740
1741 if (width <= 0)
1742 width = 1;
1743 if (height <= 0)
1744 height = 1;
1745
1746 if (it->glyph_row)
1747 {
1748 Lisp_Object object = it->stack[it->sp - 1].string;
1749 if (!STRINGP (object))
1750 object = it->w->buffer;
1751 x_append_stretch_glyph (it, object, width, height, ascent);
1752 }
1753
1754 it->pixel_width = width;
1755 it->ascent = it->phys_ascent = height * ascent;
1756 it->descent = it->phys_descent = height - it->ascent;
1757 it->nglyphs = 1;
1758
1759 if (face->box != FACE_NO_BOX)
1760 {
1761 if (face->box_line_width > 0)
1762 {
1763 it->ascent += face->box_line_width;
1764 it->descent += face->box_line_width;
1765 }
1766
1767 if (it->start_of_box_run_p)
1768 it->pixel_width += abs (face->box_line_width);
1769 if (it->end_of_box_run_p)
1770 it->pixel_width += abs (face->box_line_width);
1771 }
1772
1773 take_vertical_position_into_account (it);
1774 }
1775
1776 /* Return proper value to be used as baseline offset of font that has
1777 ASCENT and DESCENT to draw characters by the font at the vertical
1778 center of the line of frame F.
1779
1780 Here, out task is to find the value of BOFF in the following figure;
1781
1782 -------------------------+-----------+-
1783 -+-+---------+-+ | |
1784 | | | | | |
1785 | | | | F_ASCENT F_HEIGHT
1786 | | | ASCENT | |
1787 HEIGHT | | | | |
1788 | | |-|-+------+-----------|------- baseline
1789 | | | | BOFF | |
1790 | |---------|-+-+ | |
1791 | | | DESCENT | |
1792 -+-+---------+-+ F_DESCENT |
1793 -------------------------+-----------+-
1794
1795 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1796 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1797 DESCENT = FONT->descent
1798 HEIGHT = FONT_HEIGHT (FONT)
1799 F_DESCENT = (F->output_data.x->font->descent
1800 - F->output_data.x->baseline_offset)
1801 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1802 */
1803
1804 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1805 ((FONT)->descent \
1806 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1807 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1808 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1809
1810 /* Produce glyphs/get display metrics for the display element IT is
1811 loaded with. See the description of struct display_iterator in
1812 dispextern.h for an overview of struct display_iterator. */
1813
1814 static void
1815 x_produce_glyphs (it)
1816 struct it *it;
1817 {
1818 it->glyph_not_available_p = 0;
1819
1820 if (it->what == IT_CHARACTER)
1821 {
1822 XChar2b char2b;
1823 XFontStruct *font;
1824 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1825 XCharStruct *pcm;
1826 int font_not_found_p;
1827 struct font_info *font_info;
1828 int boff; /* baseline offset */
1829 /* We may change it->multibyte_p upon unibyte<->multibyte
1830 conversion. So, save the current value now and restore it
1831 later.
1832
1833 Note: It seems that we don't have to record multibyte_p in
1834 struct glyph because the character code itself tells if or
1835 not the character is multibyte. Thus, in the future, we must
1836 consider eliminating the field `multibyte_p' in the struct
1837 glyph. */
1838 int saved_multibyte_p = it->multibyte_p;
1839
1840 /* Maybe translate single-byte characters to multibyte, or the
1841 other way. */
1842 it->char_to_display = it->c;
1843 if (!ASCII_BYTE_P (it->c))
1844 {
1845 if (unibyte_display_via_language_environment
1846 && SINGLE_BYTE_CHAR_P (it->c)
1847 && (it->c >= 0240
1848 || !NILP (Vnonascii_translation_table)))
1849 {
1850 it->char_to_display = unibyte_char_to_multibyte (it->c);
1851 it->multibyte_p = 1;
1852 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1853 face = FACE_FROM_ID (it->f, it->face_id);
1854 }
1855 else if (!SINGLE_BYTE_CHAR_P (it->c)
1856 && !it->multibyte_p)
1857 {
1858 it->multibyte_p = 1;
1859 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1860 face = FACE_FROM_ID (it->f, it->face_id);
1861 }
1862 }
1863
1864 /* Get font to use. Encode IT->char_to_display. */
1865 x_get_char_face_and_encoding (it->f, it->char_to_display,
1866 it->face_id, &char2b,
1867 it->multibyte_p, 0);
1868 font = face->font;
1869
1870 /* When no suitable font found, use the default font. */
1871 font_not_found_p = font == NULL;
1872 if (font_not_found_p)
1873 {
1874 font = FRAME_FONT (it->f);
1875 boff = it->f->output_data.x->baseline_offset;
1876 font_info = NULL;
1877 }
1878 else
1879 {
1880 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1881 boff = font_info->baseline_offset;
1882 if (font_info->vertical_centering)
1883 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1884 }
1885
1886 if (it->char_to_display >= ' '
1887 && (!it->multibyte_p || it->char_to_display < 128))
1888 {
1889 /* Either unibyte or ASCII. */
1890 int stretched_p;
1891
1892 it->nglyphs = 1;
1893
1894 pcm = x_per_char_metric (font, &char2b);
1895 it->ascent = font->ascent + boff;
1896 it->descent = font->descent - boff;
1897
1898 if (pcm)
1899 {
1900 it->phys_ascent = pcm->ascent + boff;
1901 it->phys_descent = pcm->descent - boff;
1902 it->pixel_width = pcm->width;
1903 }
1904 else
1905 {
1906 it->glyph_not_available_p = 1;
1907 it->phys_ascent = font->ascent + boff;
1908 it->phys_descent = font->descent - boff;
1909 it->pixel_width = FONT_WIDTH (font);
1910 }
1911
1912 /* If this is a space inside a region of text with
1913 `space-width' property, change its width. */
1914 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1915 if (stretched_p)
1916 it->pixel_width *= XFLOATINT (it->space_width);
1917
1918 /* If face has a box, add the box thickness to the character
1919 height. If character has a box line to the left and/or
1920 right, add the box line width to the character's width. */
1921 if (face->box != FACE_NO_BOX)
1922 {
1923 int thick = face->box_line_width;
1924
1925 if (thick > 0)
1926 {
1927 it->ascent += thick;
1928 it->descent += thick;
1929 }
1930 else
1931 thick = -thick;
1932
1933 if (it->start_of_box_run_p)
1934 it->pixel_width += thick;
1935 if (it->end_of_box_run_p)
1936 it->pixel_width += thick;
1937 }
1938
1939 /* If face has an overline, add the height of the overline
1940 (1 pixel) and a 1 pixel margin to the character height. */
1941 if (face->overline_p)
1942 it->ascent += 2;
1943
1944 take_vertical_position_into_account (it);
1945
1946 /* If we have to actually produce glyphs, do it. */
1947 if (it->glyph_row)
1948 {
1949 if (stretched_p)
1950 {
1951 /* Translate a space with a `space-width' property
1952 into a stretch glyph. */
1953 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1954 x_append_stretch_glyph (it, it->object, it->pixel_width,
1955 it->ascent + it->descent, ascent);
1956 }
1957 else
1958 x_append_glyph (it);
1959
1960 /* If characters with lbearing or rbearing are displayed
1961 in this line, record that fact in a flag of the
1962 glyph row. This is used to optimize X output code. */
1963 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1964 it->glyph_row->contains_overlapping_glyphs_p = 1;
1965 }
1966 }
1967 else if (it->char_to_display == '\n')
1968 {
1969 /* A newline has no width but we need the height of the line. */
1970 it->pixel_width = 0;
1971 it->nglyphs = 0;
1972 it->ascent = it->phys_ascent = font->ascent + boff;
1973 it->descent = it->phys_descent = font->descent - boff;
1974
1975 if (face->box != FACE_NO_BOX
1976 && face->box_line_width > 0)
1977 {
1978 it->ascent += face->box_line_width;
1979 it->descent += face->box_line_width;
1980 }
1981 }
1982 else if (it->char_to_display == '\t')
1983 {
1984 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1985 int x = it->current_x + it->continuation_lines_width;
1986 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1987
1988 /* If the distance from the current position to the next tab
1989 stop is less than a canonical character width, use the
1990 tab stop after that. */
1991 if (next_tab_x - x < CANON_X_UNIT (it->f))
1992 next_tab_x += tab_width;
1993
1994 it->pixel_width = next_tab_x - x;
1995 it->nglyphs = 1;
1996 it->ascent = it->phys_ascent = font->ascent + boff;
1997 it->descent = it->phys_descent = font->descent - boff;
1998
1999 if (it->glyph_row)
2000 {
2001 double ascent = (double) it->ascent / (it->ascent + it->descent);
2002 x_append_stretch_glyph (it, it->object, it->pixel_width,
2003 it->ascent + it->descent, ascent);
2004 }
2005 }
2006 else
2007 {
2008 /* A multi-byte character. Assume that the display width of the
2009 character is the width of the character multiplied by the
2010 width of the font. */
2011
2012 /* If we found a font, this font should give us the right
2013 metrics. If we didn't find a font, use the frame's
2014 default font and calculate the width of the character
2015 from the charset width; this is what old redisplay code
2016 did. */
2017 pcm = x_per_char_metric (font, &char2b);
2018 if (font_not_found_p || !pcm)
2019 {
2020 int charset = CHAR_CHARSET (it->char_to_display);
2021
2022 it->glyph_not_available_p = 1;
2023 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
2024 * CHARSET_WIDTH (charset));
2025 it->phys_ascent = font->ascent + boff;
2026 it->phys_descent = font->descent - boff;
2027 }
2028 else
2029 {
2030 it->pixel_width = pcm->width;
2031 it->phys_ascent = pcm->ascent + boff;
2032 it->phys_descent = pcm->descent - boff;
2033 if (it->glyph_row
2034 && (pcm->lbearing < 0
2035 || pcm->rbearing > pcm->width))
2036 it->glyph_row->contains_overlapping_glyphs_p = 1;
2037 }
2038 it->nglyphs = 1;
2039 it->ascent = font->ascent + boff;
2040 it->descent = font->descent - boff;
2041 if (face->box != FACE_NO_BOX)
2042 {
2043 int thick = face->box_line_width;
2044
2045 if (thick > 0)
2046 {
2047 it->ascent += thick;
2048 it->descent += thick;
2049 }
2050 else
2051 thick = - thick;
2052
2053 if (it->start_of_box_run_p)
2054 it->pixel_width += thick;
2055 if (it->end_of_box_run_p)
2056 it->pixel_width += thick;
2057 }
2058
2059 /* If face has an overline, add the height of the overline
2060 (1 pixel) and a 1 pixel margin to the character height. */
2061 if (face->overline_p)
2062 it->ascent += 2;
2063
2064 take_vertical_position_into_account (it);
2065
2066 if (it->glyph_row)
2067 x_append_glyph (it);
2068 }
2069 it->multibyte_p = saved_multibyte_p;
2070 }
2071 else if (it->what == IT_COMPOSITION)
2072 {
2073 /* Note: A composition is represented as one glyph in the
2074 glyph matrix. There are no padding glyphs. */
2075 XChar2b char2b;
2076 XFontStruct *font;
2077 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2078 XCharStruct *pcm;
2079 int font_not_found_p;
2080 struct font_info *font_info;
2081 int boff; /* baseline offset */
2082 struct composition *cmp = composition_table[it->cmp_id];
2083
2084 /* Maybe translate single-byte characters to multibyte. */
2085 it->char_to_display = it->c;
2086 if (unibyte_display_via_language_environment
2087 && SINGLE_BYTE_CHAR_P (it->c)
2088 && (it->c >= 0240
2089 || (it->c >= 0200
2090 && !NILP (Vnonascii_translation_table))))
2091 {
2092 it->char_to_display = unibyte_char_to_multibyte (it->c);
2093 }
2094
2095 /* Get face and font to use. Encode IT->char_to_display. */
2096 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2097 face = FACE_FROM_ID (it->f, it->face_id);
2098 x_get_char_face_and_encoding (it->f, it->char_to_display,
2099 it->face_id, &char2b, it->multibyte_p, 0);
2100 font = face->font;
2101
2102 /* When no suitable font found, use the default font. */
2103 font_not_found_p = font == NULL;
2104 if (font_not_found_p)
2105 {
2106 font = FRAME_FONT (it->f);
2107 boff = it->f->output_data.x->baseline_offset;
2108 font_info = NULL;
2109 }
2110 else
2111 {
2112 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2113 boff = font_info->baseline_offset;
2114 if (font_info->vertical_centering)
2115 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2116 }
2117
2118 /* There are no padding glyphs, so there is only one glyph to
2119 produce for the composition. Important is that pixel_width,
2120 ascent and descent are the values of what is drawn by
2121 draw_glyphs (i.e. the values of the overall glyphs composed). */
2122 it->nglyphs = 1;
2123
2124 /* If we have not yet calculated pixel size data of glyphs of
2125 the composition for the current face font, calculate them
2126 now. Theoretically, we have to check all fonts for the
2127 glyphs, but that requires much time and memory space. So,
2128 here we check only the font of the first glyph. This leads
2129 to incorrect display very rarely, and C-l (recenter) can
2130 correct the display anyway. */
2131 if (cmp->font != (void *) font)
2132 {
2133 /* Ascent and descent of the font of the first character of
2134 this composition (adjusted by baseline offset). Ascent
2135 and descent of overall glyphs should not be less than
2136 them respectively. */
2137 int font_ascent = font->ascent + boff;
2138 int font_descent = font->descent - boff;
2139 /* Bounding box of the overall glyphs. */
2140 int leftmost, rightmost, lowest, highest;
2141 int i, width, ascent, descent;
2142
2143 cmp->font = (void *) font;
2144
2145 /* Initialize the bounding box. */
2146 if (font_info
2147 && (pcm = x_per_char_metric (font, &char2b)))
2148 {
2149 width = pcm->width;
2150 ascent = pcm->ascent;
2151 descent = pcm->descent;
2152 }
2153 else
2154 {
2155 width = FONT_WIDTH (font);
2156 ascent = font->ascent;
2157 descent = font->descent;
2158 }
2159
2160 rightmost = width;
2161 lowest = - descent + boff;
2162 highest = ascent + boff;
2163 leftmost = 0;
2164
2165 if (font_info
2166 && font_info->default_ascent
2167 && CHAR_TABLE_P (Vuse_default_ascent)
2168 && !NILP (Faref (Vuse_default_ascent,
2169 make_number (it->char_to_display))))
2170 highest = font_info->default_ascent + boff;
2171
2172 /* Draw the first glyph at the normal position. It may be
2173 shifted to right later if some other glyphs are drawn at
2174 the left. */
2175 cmp->offsets[0] = 0;
2176 cmp->offsets[1] = boff;
2177
2178 /* Set cmp->offsets for the remaining glyphs. */
2179 for (i = 1; i < cmp->glyph_len; i++)
2180 {
2181 int left, right, btm, top;
2182 int ch = COMPOSITION_GLYPH (cmp, i);
2183 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2184
2185 face = FACE_FROM_ID (it->f, face_id);
2186 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2187 it->multibyte_p, 0);
2188 font = face->font;
2189 if (font == NULL)
2190 {
2191 font = FRAME_FONT (it->f);
2192 boff = it->f->output_data.x->baseline_offset;
2193 font_info = NULL;
2194 }
2195 else
2196 {
2197 font_info
2198 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2199 boff = font_info->baseline_offset;
2200 if (font_info->vertical_centering)
2201 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2202 }
2203
2204 if (font_info
2205 && (pcm = x_per_char_metric (font, &char2b)))
2206 {
2207 width = pcm->width;
2208 ascent = pcm->ascent;
2209 descent = pcm->descent;
2210 }
2211 else
2212 {
2213 width = FONT_WIDTH (font);
2214 ascent = 1;
2215 descent = 0;
2216 }
2217
2218 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2219 {
2220 /* Relative composition with or without
2221 alternate chars. */
2222 left = (leftmost + rightmost - width) / 2;
2223 btm = - descent + boff;
2224 if (font_info && font_info->relative_compose
2225 && (! CHAR_TABLE_P (Vignore_relative_composition)
2226 || NILP (Faref (Vignore_relative_composition,
2227 make_number (ch)))))
2228 {
2229
2230 if (- descent >= font_info->relative_compose)
2231 /* One extra pixel between two glyphs. */
2232 btm = highest + 1;
2233 else if (ascent <= 0)
2234 /* One extra pixel between two glyphs. */
2235 btm = lowest - 1 - ascent - descent;
2236 }
2237 }
2238 else
2239 {
2240 /* A composition rule is specified by an integer
2241 value that encodes global and new reference
2242 points (GREF and NREF). GREF and NREF are
2243 specified by numbers as below:
2244
2245 0---1---2 -- ascent
2246 | |
2247 | |
2248 | |
2249 9--10--11 -- center
2250 | |
2251 ---3---4---5--- baseline
2252 | |
2253 6---7---8 -- descent
2254 */
2255 int rule = COMPOSITION_RULE (cmp, i);
2256 int gref, nref, grefx, grefy, nrefx, nrefy;
2257
2258 COMPOSITION_DECODE_RULE (rule, gref, nref);
2259 grefx = gref % 3, nrefx = nref % 3;
2260 grefy = gref / 3, nrefy = nref / 3;
2261
2262 left = (leftmost
2263 + grefx * (rightmost - leftmost) / 2
2264 - nrefx * width / 2);
2265 btm = ((grefy == 0 ? highest
2266 : grefy == 1 ? 0
2267 : grefy == 2 ? lowest
2268 : (highest + lowest) / 2)
2269 - (nrefy == 0 ? ascent + descent
2270 : nrefy == 1 ? descent - boff
2271 : nrefy == 2 ? 0
2272 : (ascent + descent) / 2));
2273 }
2274
2275 cmp->offsets[i * 2] = left;
2276 cmp->offsets[i * 2 + 1] = btm + descent;
2277
2278 /* Update the bounding box of the overall glyphs. */
2279 right = left + width;
2280 top = btm + descent + ascent;
2281 if (left < leftmost)
2282 leftmost = left;
2283 if (right > rightmost)
2284 rightmost = right;
2285 if (top > highest)
2286 highest = top;
2287 if (btm < lowest)
2288 lowest = btm;
2289 }
2290
2291 /* If there are glyphs whose x-offsets are negative,
2292 shift all glyphs to the right and make all x-offsets
2293 non-negative. */
2294 if (leftmost < 0)
2295 {
2296 for (i = 0; i < cmp->glyph_len; i++)
2297 cmp->offsets[i * 2] -= leftmost;
2298 rightmost -= leftmost;
2299 }
2300
2301 cmp->pixel_width = rightmost;
2302 cmp->ascent = highest;
2303 cmp->descent = - lowest;
2304 if (cmp->ascent < font_ascent)
2305 cmp->ascent = font_ascent;
2306 if (cmp->descent < font_descent)
2307 cmp->descent = font_descent;
2308 }
2309
2310 it->pixel_width = cmp->pixel_width;
2311 it->ascent = it->phys_ascent = cmp->ascent;
2312 it->descent = it->phys_descent = cmp->descent;
2313
2314 if (face->box != FACE_NO_BOX)
2315 {
2316 int thick = face->box_line_width;
2317
2318 if (thick > 0)
2319 {
2320 it->ascent += thick;
2321 it->descent += thick;
2322 }
2323 else
2324 thick = - thick;
2325
2326 if (it->start_of_box_run_p)
2327 it->pixel_width += thick;
2328 if (it->end_of_box_run_p)
2329 it->pixel_width += thick;
2330 }
2331
2332 /* If face has an overline, add the height of the overline
2333 (1 pixel) and a 1 pixel margin to the character height. */
2334 if (face->overline_p)
2335 it->ascent += 2;
2336
2337 take_vertical_position_into_account (it);
2338
2339 if (it->glyph_row)
2340 x_append_composite_glyph (it);
2341 }
2342 else if (it->what == IT_IMAGE)
2343 x_produce_image_glyph (it);
2344 else if (it->what == IT_STRETCH)
2345 x_produce_stretch_glyph (it);
2346
2347 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2348 because this isn't true for images with `:ascent 100'. */
2349 xassert (it->ascent >= 0 && it->descent >= 0);
2350 if (it->area == TEXT_AREA)
2351 it->current_x += it->pixel_width;
2352
2353 it->descent += it->extra_line_spacing;
2354
2355 it->max_ascent = max (it->max_ascent, it->ascent);
2356 it->max_descent = max (it->max_descent, it->descent);
2357 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2358 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2359 }
2360
2361
2362 /* Estimate the pixel height of the mode or top line on frame F.
2363 FACE_ID specifies what line's height to estimate. */
2364
2365 int
2366 x_estimate_mode_line_height (f, face_id)
2367 struct frame *f;
2368 enum face_id face_id;
2369 {
2370 int height = FONT_HEIGHT (FRAME_FONT (f));
2371
2372 /* This function is called so early when Emacs starts that the face
2373 cache and mode line face are not yet initialized. */
2374 if (FRAME_FACE_CACHE (f))
2375 {
2376 struct face *face = FACE_FROM_ID (f, face_id);
2377 if (face)
2378 {
2379 if (face->font)
2380 height = FONT_HEIGHT (face->font);
2381 if (face->box_line_width > 0)
2382 height += 2 * face->box_line_width;
2383 }
2384 }
2385
2386 return height;
2387 }
2388
2389 \f
2390 /***********************************************************************
2391 Glyph display
2392 ***********************************************************************/
2393
2394 /* A sequence of glyphs to be drawn in the same face.
2395
2396 This data structure is not really completely X specific, so it
2397 could possibly, at least partially, be useful for other systems. It
2398 is currently not part of the external redisplay interface because
2399 it's not clear what other systems will need. */
2400
2401 struct glyph_string
2402 {
2403 /* X-origin of the string. */
2404 int x;
2405
2406 /* Y-origin and y-position of the base line of this string. */
2407 int y, ybase;
2408
2409 /* The width of the string, not including a face extension. */
2410 int width;
2411
2412 /* The width of the string, including a face extension. */
2413 int background_width;
2414
2415 /* The height of this string. This is the height of the line this
2416 string is drawn in, and can be different from the height of the
2417 font the string is drawn in. */
2418 int height;
2419
2420 /* Number of pixels this string overwrites in front of its x-origin.
2421 This number is zero if the string has an lbearing >= 0; it is
2422 -lbearing, if the string has an lbearing < 0. */
2423 int left_overhang;
2424
2425 /* Number of pixels this string overwrites past its right-most
2426 nominal x-position, i.e. x + width. Zero if the string's
2427 rbearing is <= its nominal width, rbearing - width otherwise. */
2428 int right_overhang;
2429
2430 /* The frame on which the glyph string is drawn. */
2431 struct frame *f;
2432
2433 /* The window on which the glyph string is drawn. */
2434 struct window *w;
2435
2436 /* X display and window for convenience. */
2437 Display *display;
2438 Window window;
2439
2440 /* The glyph row for which this string was built. It determines the
2441 y-origin and height of the string. */
2442 struct glyph_row *row;
2443
2444 /* The area within row. */
2445 enum glyph_row_area area;
2446
2447 /* Characters to be drawn, and number of characters. */
2448 XChar2b *char2b;
2449 int nchars;
2450
2451 /* A face-override for drawing cursors, mouse face and similar. */
2452 enum draw_glyphs_face hl;
2453
2454 /* Face in which this string is to be drawn. */
2455 struct face *face;
2456
2457 /* Font in which this string is to be drawn. */
2458 XFontStruct *font;
2459
2460 /* Font info for this string. */
2461 struct font_info *font_info;
2462
2463 /* Non-null means this string describes (part of) a composition.
2464 All characters from char2b are drawn composed. */
2465 struct composition *cmp;
2466
2467 /* Index of this glyph string's first character in the glyph
2468 definition of CMP. If this is zero, this glyph string describes
2469 the first character of a composition. */
2470 int gidx;
2471
2472 /* 1 means this glyph strings face has to be drawn to the right end
2473 of the window's drawing area. */
2474 unsigned extends_to_end_of_line_p : 1;
2475
2476 /* 1 means the background of this string has been drawn. */
2477 unsigned background_filled_p : 1;
2478
2479 /* 1 means glyph string must be drawn with 16-bit functions. */
2480 unsigned two_byte_p : 1;
2481
2482 /* 1 means that the original font determined for drawing this glyph
2483 string could not be loaded. The member `font' has been set to
2484 the frame's default font in this case. */
2485 unsigned font_not_found_p : 1;
2486
2487 /* 1 means that the face in which this glyph string is drawn has a
2488 stipple pattern. */
2489 unsigned stippled_p : 1;
2490
2491 /* 1 means only the foreground of this glyph string must be drawn,
2492 and we should use the physical height of the line this glyph
2493 string appears in as clip rect. */
2494 unsigned for_overlaps_p : 1;
2495
2496 /* The GC to use for drawing this glyph string. */
2497 GC gc;
2498
2499 /* A pointer to the first glyph in the string. This glyph
2500 corresponds to char2b[0]. Needed to draw rectangles if
2501 font_not_found_p is 1. */
2502 struct glyph *first_glyph;
2503
2504 /* Image, if any. */
2505 struct image *img;
2506
2507 struct glyph_string *next, *prev;
2508 };
2509
2510
2511 #if GLYPH_DEBUG
2512
2513 static void
2514 x_dump_glyph_string (s)
2515 struct glyph_string *s;
2516 {
2517 fprintf (stderr, "glyph string\n");
2518 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2519 s->x, s->y, s->width, s->height);
2520 fprintf (stderr, " ybase = %d\n", s->ybase);
2521 fprintf (stderr, " hl = %d\n", s->hl);
2522 fprintf (stderr, " left overhang = %d, right = %d\n",
2523 s->left_overhang, s->right_overhang);
2524 fprintf (stderr, " nchars = %d\n", s->nchars);
2525 fprintf (stderr, " extends to end of line = %d\n",
2526 s->extends_to_end_of_line_p);
2527 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2528 fprintf (stderr, " bg width = %d\n", s->background_width);
2529 }
2530
2531 #endif /* GLYPH_DEBUG */
2532
2533
2534
2535 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2536 struct glyph_string **,
2537 struct glyph_string *,
2538 struct glyph_string *));
2539 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2540 struct glyph_string **,
2541 struct glyph_string *,
2542 struct glyph_string *));
2543 static void x_append_glyph_string P_ ((struct glyph_string **,
2544 struct glyph_string **,
2545 struct glyph_string *));
2546 static int x_left_overwritten P_ ((struct glyph_string *));
2547 static int x_left_overwriting P_ ((struct glyph_string *));
2548 static int x_right_overwritten P_ ((struct glyph_string *));
2549 static int x_right_overwriting P_ ((struct glyph_string *));
2550 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2551 int));
2552 static void x_init_glyph_string P_ ((struct glyph_string *,
2553 XChar2b *, struct window *,
2554 struct glyph_row *,
2555 enum glyph_row_area, int,
2556 enum draw_glyphs_face));
2557 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2558 enum glyph_row_area, int, int,
2559 enum draw_glyphs_face, int));
2560 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2561 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2562 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2563 int));
2564 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2565 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2566 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2567 static void x_draw_glyph_string P_ ((struct glyph_string *));
2568 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2569 static void x_set_cursor_gc P_ ((struct glyph_string *));
2570 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2571 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2572 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2573 int *, int *));
2574 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2575 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2576 unsigned long *, double, int));
2577 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2578 double, int, unsigned long));
2579 static void x_setup_relief_colors P_ ((struct glyph_string *));
2580 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2581 static void x_draw_image_relief P_ ((struct glyph_string *));
2582 static void x_draw_image_foreground P_ ((struct glyph_string *));
2583 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2584 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2585 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2586 int, int, int));
2587 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2588 int, int, int, int, XRectangle *));
2589 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2590 int, int, int, XRectangle *));
2591 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2592 enum glyph_row_area));
2593 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2594 struct glyph_row *,
2595 enum glyph_row_area, int, int));
2596
2597 #if GLYPH_DEBUG
2598 static void x_check_font P_ ((struct frame *, XFontStruct *));
2599 #endif
2600
2601
2602 /* Append the list of glyph strings with head H and tail T to the list
2603 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2604
2605 static INLINE void
2606 x_append_glyph_string_lists (head, tail, h, t)
2607 struct glyph_string **head, **tail;
2608 struct glyph_string *h, *t;
2609 {
2610 if (h)
2611 {
2612 if (*head)
2613 (*tail)->next = h;
2614 else
2615 *head = h;
2616 h->prev = *tail;
2617 *tail = t;
2618 }
2619 }
2620
2621
2622 /* Prepend the list of glyph strings with head H and tail T to the
2623 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2624 result. */
2625
2626 static INLINE void
2627 x_prepend_glyph_string_lists (head, tail, h, t)
2628 struct glyph_string **head, **tail;
2629 struct glyph_string *h, *t;
2630 {
2631 if (h)
2632 {
2633 if (*head)
2634 (*head)->prev = t;
2635 else
2636 *tail = t;
2637 t->next = *head;
2638 *head = h;
2639 }
2640 }
2641
2642
2643 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2644 Set *HEAD and *TAIL to the resulting list. */
2645
2646 static INLINE void
2647 x_append_glyph_string (head, tail, s)
2648 struct glyph_string **head, **tail;
2649 struct glyph_string *s;
2650 {
2651 s->next = s->prev = NULL;
2652 x_append_glyph_string_lists (head, tail, s, s);
2653 }
2654
2655
2656 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2657 face. */
2658
2659 static void
2660 x_set_cursor_gc (s)
2661 struct glyph_string *s;
2662 {
2663 if (s->font == FRAME_FONT (s->f)
2664 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2665 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2666 && !s->cmp)
2667 s->gc = s->f->output_data.x->cursor_gc;
2668 else
2669 {
2670 /* Cursor on non-default face: must merge. */
2671 XGCValues xgcv;
2672 unsigned long mask;
2673
2674 xgcv.background = s->f->output_data.x->cursor_pixel;
2675 xgcv.foreground = s->face->background;
2676
2677 /* If the glyph would be invisible, try a different foreground. */
2678 if (xgcv.foreground == xgcv.background)
2679 xgcv.foreground = s->face->foreground;
2680 if (xgcv.foreground == xgcv.background)
2681 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2682 if (xgcv.foreground == xgcv.background)
2683 xgcv.foreground = s->face->foreground;
2684
2685 /* Make sure the cursor is distinct from text in this face. */
2686 if (xgcv.background == s->face->background
2687 && xgcv.foreground == s->face->foreground)
2688 {
2689 xgcv.background = s->face->foreground;
2690 xgcv.foreground = s->face->background;
2691 }
2692
2693 IF_DEBUG (x_check_font (s->f, s->font));
2694 xgcv.font = s->font->fid;
2695 xgcv.graphics_exposures = False;
2696 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2697
2698 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2699 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2700 mask, &xgcv);
2701 else
2702 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2703 = XCreateGC (s->display, s->window, mask, &xgcv);
2704
2705 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2706 }
2707 }
2708
2709
2710 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2711
2712 static void
2713 x_set_mouse_face_gc (s)
2714 struct glyph_string *s;
2715 {
2716 int face_id;
2717 struct face *face;
2718
2719 /* What face has to be used last for the mouse face? */
2720 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2721 face = FACE_FROM_ID (s->f, face_id);
2722 if (face == NULL)
2723 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2724
2725 if (s->first_glyph->type == CHAR_GLYPH)
2726 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2727 else
2728 face_id = FACE_FOR_CHAR (s->f, face, 0);
2729 s->face = FACE_FROM_ID (s->f, face_id);
2730 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2731
2732 /* If font in this face is same as S->font, use it. */
2733 if (s->font == s->face->font)
2734 s->gc = s->face->gc;
2735 else
2736 {
2737 /* Otherwise construct scratch_cursor_gc with values from FACE
2738 but font FONT. */
2739 XGCValues xgcv;
2740 unsigned long mask;
2741
2742 xgcv.background = s->face->background;
2743 xgcv.foreground = s->face->foreground;
2744 IF_DEBUG (x_check_font (s->f, s->font));
2745 xgcv.font = s->font->fid;
2746 xgcv.graphics_exposures = False;
2747 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2748
2749 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2750 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2751 mask, &xgcv);
2752 else
2753 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2754 = XCreateGC (s->display, s->window, mask, &xgcv);
2755
2756 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2757 }
2758
2759 xassert (s->gc != 0);
2760 }
2761
2762
2763 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2764 Faces to use in the mode line have already been computed when the
2765 matrix was built, so there isn't much to do, here. */
2766
2767 static INLINE void
2768 x_set_mode_line_face_gc (s)
2769 struct glyph_string *s;
2770 {
2771 s->gc = s->face->gc;
2772 }
2773
2774
2775 /* Set S->gc of glyph string S for drawing that glyph string. Set
2776 S->stippled_p to a non-zero value if the face of S has a stipple
2777 pattern. */
2778
2779 static INLINE void
2780 x_set_glyph_string_gc (s)
2781 struct glyph_string *s;
2782 {
2783 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2784
2785 if (s->hl == DRAW_NORMAL_TEXT)
2786 {
2787 s->gc = s->face->gc;
2788 s->stippled_p = s->face->stipple != 0;
2789 }
2790 else if (s->hl == DRAW_INVERSE_VIDEO)
2791 {
2792 x_set_mode_line_face_gc (s);
2793 s->stippled_p = s->face->stipple != 0;
2794 }
2795 else if (s->hl == DRAW_CURSOR)
2796 {
2797 x_set_cursor_gc (s);
2798 s->stippled_p = 0;
2799 }
2800 else if (s->hl == DRAW_MOUSE_FACE)
2801 {
2802 x_set_mouse_face_gc (s);
2803 s->stippled_p = s->face->stipple != 0;
2804 }
2805 else if (s->hl == DRAW_IMAGE_RAISED
2806 || s->hl == DRAW_IMAGE_SUNKEN)
2807 {
2808 s->gc = s->face->gc;
2809 s->stippled_p = s->face->stipple != 0;
2810 }
2811 else
2812 {
2813 s->gc = s->face->gc;
2814 s->stippled_p = s->face->stipple != 0;
2815 }
2816
2817 /* GC must have been set. */
2818 xassert (s->gc != 0);
2819 }
2820
2821
2822 /* Return in *R the clipping rectangle for glyph string S. */
2823
2824 static void
2825 x_get_glyph_string_clip_rect (s, r)
2826 struct glyph_string *s;
2827 XRectangle *r;
2828 {
2829 if (s->row->full_width_p)
2830 {
2831 /* Draw full-width. X coordinates are relative to S->w->left. */
2832 int canon_x = CANON_X_UNIT (s->f);
2833
2834 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2835 r->width = XFASTINT (s->w->width) * canon_x;
2836
2837 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2838 {
2839 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2840 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2841 r->x -= width;
2842 }
2843
2844 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2845
2846 /* Unless displaying a mode or menu bar line, which are always
2847 fully visible, clip to the visible part of the row. */
2848 if (s->w->pseudo_window_p)
2849 r->height = s->row->visible_height;
2850 else
2851 r->height = s->height;
2852 }
2853 else
2854 {
2855 /* This is a text line that may be partially visible. */
2856 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2857 r->width = window_box_width (s->w, s->area);
2858 r->height = s->row->visible_height;
2859 }
2860
2861 /* If S draws overlapping rows, it's sufficient to use the top and
2862 bottom of the window for clipping because this glyph string
2863 intentionally draws over other lines. */
2864 if (s->for_overlaps_p)
2865 {
2866 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2867 r->height = window_text_bottom_y (s->w) - r->y;
2868 }
2869 else
2870 {
2871 /* Don't use S->y for clipping because it doesn't take partially
2872 visible lines into account. For example, it can be negative for
2873 partially visible lines at the top of a window. */
2874 if (!s->row->full_width_p
2875 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2876 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2877 else
2878 r->y = max (0, s->row->y);
2879
2880 /* If drawing a tool-bar window, draw it over the internal border
2881 at the top of the window. */
2882 if (s->w == XWINDOW (s->f->tool_bar_window))
2883 r->y -= s->f->output_data.x->internal_border_width;
2884 }
2885
2886 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2887 }
2888
2889
2890 /* Set clipping for output of glyph string S. S may be part of a mode
2891 line or menu if we don't have X toolkit support. */
2892
2893 static INLINE void
2894 x_set_glyph_string_clipping (s)
2895 struct glyph_string *s;
2896 {
2897 XRectangle r;
2898 x_get_glyph_string_clip_rect (s, &r);
2899 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2900 }
2901
2902
2903 /* Compute left and right overhang of glyph string S. If S is a glyph
2904 string for a composition, assume overhangs don't exist. */
2905
2906 static INLINE void
2907 x_compute_glyph_string_overhangs (s)
2908 struct glyph_string *s;
2909 {
2910 if (s->cmp == NULL
2911 && s->first_glyph->type == CHAR_GLYPH)
2912 {
2913 XCharStruct cs;
2914 int direction, font_ascent, font_descent;
2915 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2916 &font_ascent, &font_descent, &cs);
2917 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2918 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2919 }
2920 }
2921
2922
2923 /* Compute overhangs and x-positions for glyph string S and its
2924 predecessors, or successors. X is the starting x-position for S.
2925 BACKWARD_P non-zero means process predecessors. */
2926
2927 static void
2928 x_compute_overhangs_and_x (s, x, backward_p)
2929 struct glyph_string *s;
2930 int x;
2931 int backward_p;
2932 {
2933 if (backward_p)
2934 {
2935 while (s)
2936 {
2937 x_compute_glyph_string_overhangs (s);
2938 x -= s->width;
2939 s->x = x;
2940 s = s->prev;
2941 }
2942 }
2943 else
2944 {
2945 while (s)
2946 {
2947 x_compute_glyph_string_overhangs (s);
2948 s->x = x;
2949 x += s->width;
2950 s = s->next;
2951 }
2952 }
2953 }
2954
2955
2956 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2957 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2958 assumed to be zero. */
2959
2960 static void
2961 x_get_glyph_overhangs (glyph, f, left, right)
2962 struct glyph *glyph;
2963 struct frame *f;
2964 int *left, *right;
2965 {
2966 *left = *right = 0;
2967
2968 if (glyph->type == CHAR_GLYPH)
2969 {
2970 XFontStruct *font;
2971 struct face *face;
2972 struct font_info *font_info;
2973 XChar2b char2b;
2974 XCharStruct *pcm;
2975
2976 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2977 font = face->font;
2978 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2979 if (font
2980 && (pcm = x_per_char_metric (font, &char2b)))
2981 {
2982 if (pcm->rbearing > pcm->width)
2983 *right = pcm->rbearing - pcm->width;
2984 if (pcm->lbearing < 0)
2985 *left = -pcm->lbearing;
2986 }
2987 }
2988 }
2989
2990
2991 /* Return the index of the first glyph preceding glyph string S that
2992 is overwritten by S because of S's left overhang. Value is -1
2993 if no glyphs are overwritten. */
2994
2995 static int
2996 x_left_overwritten (s)
2997 struct glyph_string *s;
2998 {
2999 int k;
3000
3001 if (s->left_overhang)
3002 {
3003 int x = 0, i;
3004 struct glyph *glyphs = s->row->glyphs[s->area];
3005 int first = s->first_glyph - glyphs;
3006
3007 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
3008 x -= glyphs[i].pixel_width;
3009
3010 k = i + 1;
3011 }
3012 else
3013 k = -1;
3014
3015 return k;
3016 }
3017
3018
3019 /* Return the index of the first glyph preceding glyph string S that
3020 is overwriting S because of its right overhang. Value is -1 if no
3021 glyph in front of S overwrites S. */
3022
3023 static int
3024 x_left_overwriting (s)
3025 struct glyph_string *s;
3026 {
3027 int i, k, x;
3028 struct glyph *glyphs = s->row->glyphs[s->area];
3029 int first = s->first_glyph - glyphs;
3030
3031 k = -1;
3032 x = 0;
3033 for (i = first - 1; i >= 0; --i)
3034 {
3035 int left, right;
3036 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3037 if (x + right > 0)
3038 k = i;
3039 x -= glyphs[i].pixel_width;
3040 }
3041
3042 return k;
3043 }
3044
3045
3046 /* Return the index of the last glyph following glyph string S that is
3047 not overwritten by S because of S's right overhang. Value is -1 if
3048 no such glyph is found. */
3049
3050 static int
3051 x_right_overwritten (s)
3052 struct glyph_string *s;
3053 {
3054 int k = -1;
3055
3056 if (s->right_overhang)
3057 {
3058 int x = 0, i;
3059 struct glyph *glyphs = s->row->glyphs[s->area];
3060 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3061 int end = s->row->used[s->area];
3062
3063 for (i = first; i < end && s->right_overhang > x; ++i)
3064 x += glyphs[i].pixel_width;
3065
3066 k = i;
3067 }
3068
3069 return k;
3070 }
3071
3072
3073 /* Return the index of the last glyph following glyph string S that
3074 overwrites S because of its left overhang. Value is negative
3075 if no such glyph is found. */
3076
3077 static int
3078 x_right_overwriting (s)
3079 struct glyph_string *s;
3080 {
3081 int i, k, x;
3082 int end = s->row->used[s->area];
3083 struct glyph *glyphs = s->row->glyphs[s->area];
3084 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3085
3086 k = -1;
3087 x = 0;
3088 for (i = first; i < end; ++i)
3089 {
3090 int left, right;
3091 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3092 if (x - left < 0)
3093 k = i;
3094 x += glyphs[i].pixel_width;
3095 }
3096
3097 return k;
3098 }
3099
3100
3101 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3102
3103 static INLINE void
3104 x_clear_glyph_string_rect (s, x, y, w, h)
3105 struct glyph_string *s;
3106 int x, y, w, h;
3107 {
3108 XGCValues xgcv;
3109 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3110 XSetForeground (s->display, s->gc, xgcv.background);
3111 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3112 XSetForeground (s->display, s->gc, xgcv.foreground);
3113 }
3114
3115
3116 /* Draw the background of glyph_string S. If S->background_filled_p
3117 is non-zero don't draw it. FORCE_P non-zero means draw the
3118 background even if it wouldn't be drawn normally. This is used
3119 when a string preceding S draws into the background of S, or S
3120 contains the first component of a composition. */
3121
3122 static void
3123 x_draw_glyph_string_background (s, force_p)
3124 struct glyph_string *s;
3125 int force_p;
3126 {
3127 /* Nothing to do if background has already been drawn or if it
3128 shouldn't be drawn in the first place. */
3129 if (!s->background_filled_p)
3130 {
3131 int box_line_width = max (s->face->box_line_width, 0);
3132
3133 if (s->stippled_p)
3134 {
3135 /* Fill background with a stipple pattern. */
3136 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3137 XFillRectangle (s->display, s->window, s->gc, s->x,
3138 s->y + box_line_width,
3139 s->background_width,
3140 s->height - 2 * box_line_width);
3141 XSetFillStyle (s->display, s->gc, FillSolid);
3142 s->background_filled_p = 1;
3143 }
3144 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
3145 || s->font_not_found_p
3146 || s->extends_to_end_of_line_p
3147 || force_p)
3148 {
3149 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
3150 s->background_width,
3151 s->height - 2 * box_line_width);
3152 s->background_filled_p = 1;
3153 }
3154 }
3155 }
3156
3157
3158 /* Draw the foreground of glyph string S. */
3159
3160 static void
3161 x_draw_glyph_string_foreground (s)
3162 struct glyph_string *s;
3163 {
3164 int i, x;
3165
3166 /* If first glyph of S has a left box line, start drawing the text
3167 of S to the right of that box line. */
3168 if (s->face->box != FACE_NO_BOX
3169 && s->first_glyph->left_box_line_p)
3170 x = s->x + abs (s->face->box_line_width);
3171 else
3172 x = s->x;
3173
3174 /* Draw characters of S as rectangles if S's font could not be
3175 loaded. */
3176 if (s->font_not_found_p)
3177 {
3178 for (i = 0; i < s->nchars; ++i)
3179 {
3180 struct glyph *g = s->first_glyph + i;
3181 XDrawRectangle (s->display, s->window,
3182 s->gc, x, s->y, g->pixel_width - 1,
3183 s->height - 1);
3184 x += g->pixel_width;
3185 }
3186 }
3187 else
3188 {
3189 char *char1b = (char *) s->char2b;
3190 int boff = s->font_info->baseline_offset;
3191
3192 if (s->font_info->vertical_centering)
3193 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3194
3195 /* If we can use 8-bit functions, condense S->char2b. */
3196 if (!s->two_byte_p)
3197 for (i = 0; i < s->nchars; ++i)
3198 char1b[i] = s->char2b[i].byte2;
3199
3200 /* Draw text with XDrawString if background has already been
3201 filled. Otherwise, use XDrawImageString. (Note that
3202 XDrawImageString is usually faster than XDrawString.) Always
3203 use XDrawImageString when drawing the cursor so that there is
3204 no chance that characters under a box cursor are invisible. */
3205 if (s->for_overlaps_p
3206 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3207 {
3208 /* Draw characters with 16-bit or 8-bit functions. */
3209 if (s->two_byte_p)
3210 XDrawString16 (s->display, s->window, s->gc, x,
3211 s->ybase - boff, s->char2b, s->nchars);
3212 else
3213 XDrawString (s->display, s->window, s->gc, x,
3214 s->ybase - boff, char1b, s->nchars);
3215 }
3216 else
3217 {
3218 if (s->two_byte_p)
3219 XDrawImageString16 (s->display, s->window, s->gc, x,
3220 s->ybase - boff, s->char2b, s->nchars);
3221 else
3222 XDrawImageString (s->display, s->window, s->gc, x,
3223 s->ybase - boff, char1b, s->nchars);
3224 }
3225 }
3226 }
3227
3228 /* Draw the foreground of composite glyph string S. */
3229
3230 static void
3231 x_draw_composite_glyph_string_foreground (s)
3232 struct glyph_string *s;
3233 {
3234 int i, x;
3235
3236 /* If first glyph of S has a left box line, start drawing the text
3237 of S to the right of that box line. */
3238 if (s->face->box != FACE_NO_BOX
3239 && s->first_glyph->left_box_line_p)
3240 x = s->x + abs (s->face->box_line_width);
3241 else
3242 x = s->x;
3243
3244 /* S is a glyph string for a composition. S->gidx is the index of
3245 the first character drawn for glyphs of this composition.
3246 S->gidx == 0 means we are drawing the very first character of
3247 this composition. */
3248
3249 /* Draw a rectangle for the composition if the font for the very
3250 first character of the composition could not be loaded. */
3251 if (s->font_not_found_p)
3252 {
3253 if (s->gidx == 0)
3254 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3255 s->width - 1, s->height - 1);
3256 }
3257 else
3258 {
3259 for (i = 0; i < s->nchars; i++, ++s->gidx)
3260 XDrawString16 (s->display, s->window, s->gc,
3261 x + s->cmp->offsets[s->gidx * 2],
3262 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3263 s->char2b + i, 1);
3264 }
3265 }
3266
3267
3268 #ifdef USE_X_TOOLKIT
3269
3270 static struct frame *x_frame_of_widget P_ ((Widget));
3271 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
3272 XrmValue *, XrmValue *, XtPointer *));
3273 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
3274 XrmValue *, Cardinal *));
3275
3276
3277 /* Return the frame on which widget WIDGET is used.. Abort if frame
3278 cannot be determined. */
3279
3280 static struct frame *
3281 x_frame_of_widget (widget)
3282 Widget widget;
3283 {
3284 struct x_display_info *dpyinfo;
3285 Lisp_Object tail;
3286 struct frame *f;
3287
3288 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3289
3290 /* Find the top-level shell of the widget. Note that this function
3291 can be called when the widget is not yet realized, so XtWindow
3292 (widget) == 0. That's the reason we can't simply use
3293 x_any_window_to_frame. */
3294 while (!XtIsTopLevelShell (widget))
3295 widget = XtParent (widget);
3296
3297 /* Look for a frame with that top-level widget. Allocate the color
3298 on that frame to get the right gamma correction value. */
3299 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3300 if (GC_FRAMEP (XCAR (tail))
3301 && (f = XFRAME (XCAR (tail)),
3302 (f->output_data.nothing != 1
3303 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3304 && f->output_data.x->widget == widget)
3305 return f;
3306
3307 abort ();
3308 }
3309
3310
3311 /* Allocate the color COLOR->pixel on the screen and display of
3312 widget WIDGET in colormap CMAP. If an exact match cannot be
3313 allocated, try the nearest color available. Value is non-zero
3314 if successful. This is called from lwlib. */
3315
3316 int
3317 x_alloc_nearest_color_for_widget (widget, cmap, color)
3318 Widget widget;
3319 Colormap cmap;
3320 XColor *color;
3321 {
3322 struct frame *f = x_frame_of_widget (widget);
3323 return x_alloc_nearest_color (f, cmap, color);
3324 }
3325
3326
3327 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3328 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3329 If this produces the same color as PIXEL, try a color where all RGB
3330 values have DELTA added. Return the allocated color in *PIXEL.
3331 DISPLAY is the X display, CMAP is the colormap to operate on.
3332 Value is non-zero if successful. */
3333
3334 int
3335 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3336 Widget widget;
3337 Display *display;
3338 Colormap cmap;
3339 unsigned long *pixel;
3340 double factor;
3341 int delta;
3342 {
3343 struct frame *f = x_frame_of_widget (widget);
3344 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3345 }
3346
3347
3348 /* Structure specifying which arguments should be passed by Xt to
3349 cvt_string_to_pixel. We want the widget's screen and colormap. */
3350
3351 static XtConvertArgRec cvt_string_to_pixel_args[] =
3352 {
3353 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
3354 sizeof (Screen *)},
3355 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
3356 sizeof (Colormap)}
3357 };
3358
3359
3360 /* The address of this variable is returned by
3361 cvt_string_to_pixel. */
3362
3363 static Pixel cvt_string_to_pixel_value;
3364
3365
3366 /* Convert a color name to a pixel color.
3367
3368 DPY is the display we are working on.
3369
3370 ARGS is an array of *NARGS XrmValue structures holding additional
3371 information about the widget for which the conversion takes place.
3372 The contents of this array are determined by the specification
3373 in cvt_string_to_pixel_args.
3374
3375 FROM is a pointer to an XrmValue which points to the color name to
3376 convert. TO is an XrmValue in which to return the pixel color.
3377
3378 CLOSURE_RET is a pointer to user-data, in which we record if
3379 we allocated the color or not.
3380
3381 Value is True if successful, False otherwise. */
3382
3383 static Boolean
3384 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
3385 Display *dpy;
3386 XrmValue *args;
3387 Cardinal *nargs;
3388 XrmValue *from, *to;
3389 XtPointer *closure_ret;
3390 {
3391 Screen *screen;
3392 Colormap cmap;
3393 Pixel pixel;
3394 String color_name;
3395 XColor color;
3396
3397 if (*nargs != 2)
3398 {
3399 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3400 "wrongParameters", "cvt_string_to_pixel",
3401 "XtToolkitError",
3402 "Screen and colormap args required", NULL, NULL);
3403 return False;
3404 }
3405
3406 screen = *(Screen **) args[0].addr;
3407 cmap = *(Colormap *) args[1].addr;
3408 color_name = (String) from->addr;
3409
3410 if (strcmp (color_name, XtDefaultBackground) == 0)
3411 {
3412 *closure_ret = (XtPointer) False;
3413 pixel = WhitePixelOfScreen (screen);
3414 }
3415 else if (strcmp (color_name, XtDefaultForeground) == 0)
3416 {
3417 *closure_ret = (XtPointer) False;
3418 pixel = BlackPixelOfScreen (screen);
3419 }
3420 else if (XParseColor (dpy, cmap, color_name, &color)
3421 && x_alloc_nearest_color_1 (dpy, cmap, &color))
3422 {
3423 pixel = color.pixel;
3424 *closure_ret = (XtPointer) True;
3425 }
3426 else
3427 {
3428 String params[1];
3429 Cardinal nparams = 1;
3430
3431 params[0] = color_name;
3432 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3433 "badValue", "cvt_string_to_pixel",
3434 "XtToolkitError", "Invalid color `%s'",
3435 params, &nparams);
3436 return False;
3437 }
3438
3439 if (to->addr != NULL)
3440 {
3441 if (to->size < sizeof (Pixel))
3442 {
3443 to->size = sizeof (Pixel);
3444 return False;
3445 }
3446
3447 *(Pixel *) to->addr = pixel;
3448 }
3449 else
3450 {
3451 cvt_string_to_pixel_value = pixel;
3452 to->addr = (XtPointer) &cvt_string_to_pixel_value;
3453 }
3454
3455 to->size = sizeof (Pixel);
3456 return True;
3457 }
3458
3459
3460 /* Free a pixel color which was previously allocated via
3461 cvt_string_to_pixel. This is registered as the destructor
3462 for this type of resource via XtSetTypeConverter.
3463
3464 APP is the application context in which we work.
3465
3466 TO is a pointer to an XrmValue holding the color to free.
3467 CLOSURE is the value we stored in CLOSURE_RET for this color
3468 in cvt_string_to_pixel.
3469
3470 ARGS and NARGS are like for cvt_string_to_pixel. */
3471
3472 static void
3473 cvt_pixel_dtor (app, to, closure, args, nargs)
3474 XtAppContext app;
3475 XrmValuePtr to;
3476 XtPointer closure;
3477 XrmValuePtr args;
3478 Cardinal *nargs;
3479 {
3480 if (*nargs != 2)
3481 {
3482 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
3483 "XtToolkitError",
3484 "Screen and colormap arguments required",
3485 NULL, NULL);
3486 }
3487 else if (closure != NULL)
3488 {
3489 /* We did allocate the pixel, so free it. */
3490 Screen *screen = *(Screen **) args[0].addr;
3491 Colormap cmap = *(Colormap *) args[1].addr;
3492 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
3493 (Pixel *) to->addr, 1);
3494 }
3495 }
3496
3497
3498 #endif /* USE_X_TOOLKIT */
3499
3500
3501 /* Value is an array of XColor structures for the contents of the
3502 color map of display DPY. Set *NCELLS to the size of the array.
3503 Note that this probably shouldn't be called for large color maps,
3504 say a 24-bit TrueColor map. */
3505
3506 static const XColor *
3507 x_color_cells (dpy, ncells)
3508 Display *dpy;
3509 int *ncells;
3510 {
3511 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3512
3513 if (dpyinfo->color_cells == NULL)
3514 {
3515 Screen *screen = dpyinfo->screen;
3516 int i;
3517
3518 dpyinfo->ncolor_cells
3519 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
3520 dpyinfo->color_cells
3521 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3522 * sizeof *dpyinfo->color_cells);
3523
3524 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3525 dpyinfo->color_cells[i].pixel = i;
3526
3527 XQueryColors (dpy, dpyinfo->cmap,
3528 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3529 }
3530
3531 *ncells = dpyinfo->ncolor_cells;
3532 return dpyinfo->color_cells;
3533 }
3534
3535
3536 /* On frame F, translate pixel colors to RGB values for the NCOLORS
3537 colors in COLORS. Use cached information, if available. */
3538
3539 void
3540 x_query_colors (f, colors, ncolors)
3541 struct frame *f;
3542 XColor *colors;
3543 int ncolors;
3544 {
3545 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3546
3547 if (dpyinfo->color_cells)
3548 {
3549 int i;
3550 for (i = 0; i < ncolors; ++i)
3551 {
3552 unsigned long pixel = colors[i].pixel;
3553 xassert (pixel < dpyinfo->ncolor_cells);
3554 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3555 colors[i] = dpyinfo->color_cells[pixel];
3556 }
3557 }
3558 else
3559 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3560 }
3561
3562
3563 /* On frame F, translate pixel color to RGB values for the color in
3564 COLOR. Use cached information, if available. */
3565
3566 void
3567 x_query_color (f, color)
3568 struct frame *f;
3569 XColor *color;
3570 {
3571 x_query_colors (f, color, 1);
3572 }
3573
3574
3575 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
3576 exact match can't be allocated, try the nearest color available.
3577 Value is non-zero if successful. Set *COLOR to the color
3578 allocated. */
3579
3580 static int
3581 x_alloc_nearest_color_1 (dpy, cmap, color)
3582 Display *dpy;
3583 Colormap cmap;
3584 XColor *color;
3585 {
3586 int rc;
3587
3588 rc = XAllocColor (dpy, cmap, color);
3589 if (rc == 0)
3590 {
3591 /* If we got to this point, the colormap is full, so we're going
3592 to try to get the next closest color. The algorithm used is
3593 a least-squares matching, which is what X uses for closest
3594 color matching with StaticColor visuals. */
3595 int nearest, i;
3596 unsigned long nearest_delta = ~0;
3597 int ncells;
3598 const XColor *cells = x_color_cells (dpy, &ncells);
3599
3600 for (nearest = i = 0; i < ncells; ++i)
3601 {
3602 long dred = (color->red >> 8) - (cells[i].red >> 8);
3603 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3604 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3605 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3606
3607 if (delta < nearest_delta)
3608 {
3609 nearest = i;
3610 nearest_delta = delta;
3611 }
3612 }
3613
3614 color->red = cells[nearest].red;
3615 color->green = cells[nearest].green;
3616 color->blue = cells[nearest].blue;
3617 rc = XAllocColor (dpy, cmap, color);
3618 }
3619 else
3620 {
3621 /* If allocation succeeded, and the allocated pixel color is not
3622 equal to a cached pixel color recorded earlier, there was a
3623 change in the colormap, so clear the color cache. */
3624 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3625 XColor *cached_color;
3626
3627 if (dpyinfo->color_cells
3628 && (cached_color = &dpyinfo->color_cells[color->pixel],
3629 (cached_color->red != color->red
3630 || cached_color->blue != color->blue
3631 || cached_color->green != color->green)))
3632 {
3633 xfree (dpyinfo->color_cells);
3634 dpyinfo->color_cells = NULL;
3635 dpyinfo->ncolor_cells = 0;
3636 }
3637 }
3638
3639 #ifdef DEBUG_X_COLORS
3640 if (rc)
3641 register_color (color->pixel);
3642 #endif /* DEBUG_X_COLORS */
3643
3644 return rc;
3645 }
3646
3647
3648 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
3649 exact match can't be allocated, try the nearest color available.
3650 Value is non-zero if successful. Set *COLOR to the color
3651 allocated. */
3652
3653 int
3654 x_alloc_nearest_color (f, cmap, color)
3655 struct frame *f;
3656 Colormap cmap;
3657 XColor *color;
3658 {
3659 gamma_correct (f, color);
3660 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
3661 }
3662
3663
3664 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3665 It's necessary to do this instead of just using PIXEL directly to
3666 get color reference counts right. */
3667
3668 unsigned long
3669 x_copy_color (f, pixel)
3670 struct frame *f;
3671 unsigned long pixel;
3672 {
3673 XColor color;
3674
3675 color.pixel = pixel;
3676 BLOCK_INPUT;
3677 x_query_color (f, &color);
3678 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3679 UNBLOCK_INPUT;
3680 #ifdef DEBUG_X_COLORS
3681 register_color (pixel);
3682 #endif
3683 return color.pixel;
3684 }
3685
3686
3687 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3688 It's necessary to do this instead of just using PIXEL directly to
3689 get color reference counts right. */
3690
3691 unsigned long
3692 x_copy_dpy_color (dpy, cmap, pixel)
3693 Display *dpy;
3694 Colormap cmap;
3695 unsigned long pixel;
3696 {
3697 XColor color;
3698
3699 color.pixel = pixel;
3700 BLOCK_INPUT;
3701 XQueryColor (dpy, cmap, &color);
3702 XAllocColor (dpy, cmap, &color);
3703 UNBLOCK_INPUT;
3704 #ifdef DEBUG_X_COLORS
3705 register_color (pixel);
3706 #endif
3707 return color.pixel;
3708 }
3709
3710
3711 /* Brightness beyond which a color won't have its highlight brightness
3712 boosted.
3713
3714 Nominally, highlight colors for `3d' faces are calculated by
3715 brightening an object's color by a constant scale factor, but this
3716 doesn't yield good results for dark colors, so for colors who's
3717 brightness is less than this value (on a scale of 0-65535) have an
3718 use an additional additive factor.
3719
3720 The value here is set so that the default menu-bar/mode-line color
3721 (grey75) will not have its highlights changed at all. */
3722 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3723
3724
3725 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3726 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3727 If this produces the same color as PIXEL, try a color where all RGB
3728 values have DELTA added. Return the allocated color in *PIXEL.
3729 DISPLAY is the X display, CMAP is the colormap to operate on.
3730 Value is non-zero if successful. */
3731
3732 static int
3733 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3734 struct frame *f;
3735 Display *display;
3736 Colormap cmap;
3737 unsigned long *pixel;
3738 double factor;
3739 int delta;
3740 {
3741 XColor color, new;
3742 long bright;
3743 int success_p;
3744
3745 /* Get RGB color values. */
3746 color.pixel = *pixel;
3747 x_query_color (f, &color);
3748
3749 /* Change RGB values by specified FACTOR. Avoid overflow! */
3750 xassert (factor >= 0);
3751 new.red = min (0xffff, factor * color.red);
3752 new.green = min (0xffff, factor * color.green);
3753 new.blue = min (0xffff, factor * color.blue);
3754
3755 /* Calculate brightness of COLOR. */
3756 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
3757
3758 /* We only boost colors that are darker than
3759 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3760 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3761 /* Make an additive adjustment to NEW, because it's dark enough so
3762 that scaling by FACTOR alone isn't enough. */
3763 {
3764 /* How far below the limit this color is (0 - 1, 1 being darker). */
3765 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3766 /* The additive adjustment. */
3767 int min_delta = delta * dimness * factor / 2;
3768
3769 if (factor < 1)
3770 {
3771 new.red = max (0, new.red - min_delta);
3772 new.green = max (0, new.green - min_delta);
3773 new.blue = max (0, new.blue - min_delta);
3774 }
3775 else
3776 {
3777 new.red = min (0xffff, min_delta + new.red);
3778 new.green = min (0xffff, min_delta + new.green);
3779 new.blue = min (0xffff, min_delta + new.blue);
3780 }
3781 }
3782
3783 /* Try to allocate the color. */
3784 success_p = x_alloc_nearest_color (f, cmap, &new);
3785 if (success_p)
3786 {
3787 if (new.pixel == *pixel)
3788 {
3789 /* If we end up with the same color as before, try adding
3790 delta to the RGB values. */
3791 x_free_colors (f, &new.pixel, 1);
3792
3793 new.red = min (0xffff, delta + color.red);
3794 new.green = min (0xffff, delta + color.green);
3795 new.blue = min (0xffff, delta + color.blue);
3796 success_p = x_alloc_nearest_color (f, cmap, &new);
3797 }
3798 else
3799 success_p = 1;
3800 *pixel = new.pixel;
3801 }
3802
3803 return success_p;
3804 }
3805
3806
3807 /* Set up the foreground color for drawing relief lines of glyph
3808 string S. RELIEF is a pointer to a struct relief containing the GC
3809 with which lines will be drawn. Use a color that is FACTOR or
3810 DELTA lighter or darker than the relief's background which is found
3811 in S->f->output_data.x->relief_background. If such a color cannot
3812 be allocated, use DEFAULT_PIXEL, instead. */
3813
3814 static void
3815 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3816 struct frame *f;
3817 struct relief *relief;
3818 double factor;
3819 int delta;
3820 unsigned long default_pixel;
3821 {
3822 XGCValues xgcv;
3823 struct x_output *di = f->output_data.x;
3824 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3825 unsigned long pixel;
3826 unsigned long background = di->relief_background;
3827 Colormap cmap = FRAME_X_COLORMAP (f);
3828 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3829 Display *dpy = FRAME_X_DISPLAY (f);
3830
3831 xgcv.graphics_exposures = False;
3832 xgcv.line_width = 1;
3833
3834 /* Free previously allocated color. The color cell will be reused
3835 when it has been freed as many times as it was allocated, so this
3836 doesn't affect faces using the same colors. */
3837 if (relief->gc
3838 && relief->allocated_p)
3839 {
3840 x_free_colors (f, &relief->pixel, 1);
3841 relief->allocated_p = 0;
3842 }
3843
3844 /* Allocate new color. */
3845 xgcv.foreground = default_pixel;
3846 pixel = background;
3847 if (dpyinfo->n_planes != 1
3848 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3849 {
3850 relief->allocated_p = 1;
3851 xgcv.foreground = relief->pixel = pixel;
3852 }
3853
3854 if (relief->gc == 0)
3855 {
3856 xgcv.stipple = dpyinfo->gray;
3857 mask |= GCStipple;
3858 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3859 }
3860 else
3861 XChangeGC (dpy, relief->gc, mask, &xgcv);
3862 }
3863
3864
3865 /* Set up colors for the relief lines around glyph string S. */
3866
3867 static void
3868 x_setup_relief_colors (s)
3869 struct glyph_string *s;
3870 {
3871 struct x_output *di = s->f->output_data.x;
3872 unsigned long color;
3873
3874 if (s->face->use_box_color_for_shadows_p)
3875 color = s->face->box_color;
3876 else if (s->first_glyph->type == IMAGE_GLYPH
3877 && s->img->pixmap
3878 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
3879 color = IMAGE_BACKGROUND (s->img, s->f, 0);
3880 else
3881 {
3882 XGCValues xgcv;
3883
3884 /* Get the background color of the face. */
3885 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3886 color = xgcv.background;
3887 }
3888
3889 if (di->white_relief.gc == 0
3890 || color != di->relief_background)
3891 {
3892 di->relief_background = color;
3893 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3894 WHITE_PIX_DEFAULT (s->f));
3895 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3896 BLACK_PIX_DEFAULT (s->f));
3897 }
3898 }
3899
3900
3901 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3902 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3903 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3904 relief. LEFT_P non-zero means draw a relief on the left side of
3905 the rectangle. RIGHT_P non-zero means draw a relief on the right
3906 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3907 when drawing. */
3908
3909 static void
3910 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3911 raised_p, left_p, right_p, clip_rect)
3912 struct frame *f;
3913 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3914 XRectangle *clip_rect;
3915 {
3916 Display *dpy = FRAME_X_DISPLAY (f);
3917 Window window = FRAME_X_WINDOW (f);
3918 int i;
3919 GC gc;
3920
3921 if (raised_p)
3922 gc = f->output_data.x->white_relief.gc;
3923 else
3924 gc = f->output_data.x->black_relief.gc;
3925 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
3926
3927 /* Top. */
3928 for (i = 0; i < width; ++i)
3929 XDrawLine (dpy, window, gc,
3930 left_x + i * left_p, top_y + i,
3931 right_x + 1 - i * right_p, top_y + i);
3932
3933 /* Left. */
3934 if (left_p)
3935 for (i = 0; i < width; ++i)
3936 XDrawLine (dpy, window, gc,
3937 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
3938
3939 XSetClipMask (dpy, gc, None);
3940 if (raised_p)
3941 gc = f->output_data.x->black_relief.gc;
3942 else
3943 gc = f->output_data.x->white_relief.gc;
3944 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
3945
3946 /* Bottom. */
3947 for (i = 0; i < width; ++i)
3948 XDrawLine (dpy, window, gc,
3949 left_x + i * left_p, bottom_y - i,
3950 right_x + 1 - i * right_p, bottom_y - i);
3951
3952 /* Right. */
3953 if (right_p)
3954 for (i = 0; i < width; ++i)
3955 XDrawLine (dpy, window, gc,
3956 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3957
3958 XSetClipMask (dpy, gc, None);
3959 }
3960
3961
3962 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3963 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3964 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3965 left side of the rectangle. RIGHT_P non-zero means draw a line
3966 on the right side of the rectangle. CLIP_RECT is the clipping
3967 rectangle to use when drawing. */
3968
3969 static void
3970 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3971 left_p, right_p, clip_rect)
3972 struct glyph_string *s;
3973 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3974 XRectangle *clip_rect;
3975 {
3976 XGCValues xgcv;
3977
3978 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3979 XSetForeground (s->display, s->gc, s->face->box_color);
3980 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3981
3982 /* Top. */
3983 XFillRectangle (s->display, s->window, s->gc,
3984 left_x, top_y, right_x - left_x + 1, width);
3985
3986 /* Left. */
3987 if (left_p)
3988 XFillRectangle (s->display, s->window, s->gc,
3989 left_x, top_y, width, bottom_y - top_y + 1);
3990
3991 /* Bottom. */
3992 XFillRectangle (s->display, s->window, s->gc,
3993 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3994
3995 /* Right. */
3996 if (right_p)
3997 XFillRectangle (s->display, s->window, s->gc,
3998 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3999
4000 XSetForeground (s->display, s->gc, xgcv.foreground);
4001 XSetClipMask (s->display, s->gc, None);
4002 }
4003
4004
4005 /* Draw a box around glyph string S. */
4006
4007 static void
4008 x_draw_glyph_string_box (s)
4009 struct glyph_string *s;
4010 {
4011 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
4012 int left_p, right_p;
4013 struct glyph *last_glyph;
4014 XRectangle clip_rect;
4015
4016 last_x = window_box_right (s->w, s->area);
4017 if (s->row->full_width_p
4018 && !s->w->pseudo_window_p)
4019 {
4020 last_x += FRAME_X_RIGHT_FRINGE_WIDTH (s->f);
4021 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
4022 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
4023 }
4024
4025 /* The glyph that may have a right box line. */
4026 last_glyph = (s->cmp || s->img
4027 ? s->first_glyph
4028 : s->first_glyph + s->nchars - 1);
4029
4030 width = abs (s->face->box_line_width);
4031 raised_p = s->face->box == FACE_RAISED_BOX;
4032 left_x = s->x;
4033 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
4034 ? last_x - 1
4035 : min (last_x, s->x + s->background_width) - 1);
4036 top_y = s->y;
4037 bottom_y = top_y + s->height - 1;
4038
4039 left_p = (s->first_glyph->left_box_line_p
4040 || (s->hl == DRAW_MOUSE_FACE
4041 && (s->prev == NULL
4042 || s->prev->hl != s->hl)));
4043 right_p = (last_glyph->right_box_line_p
4044 || (s->hl == DRAW_MOUSE_FACE
4045 && (s->next == NULL
4046 || s->next->hl != s->hl)));
4047
4048 x_get_glyph_string_clip_rect (s, &clip_rect);
4049
4050 if (s->face->box == FACE_SIMPLE_BOX)
4051 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
4052 left_p, right_p, &clip_rect);
4053 else
4054 {
4055 x_setup_relief_colors (s);
4056 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
4057 width, raised_p, left_p, right_p, &clip_rect);
4058 }
4059 }
4060
4061
4062 /* Draw foreground of image glyph string S. */
4063
4064 static void
4065 x_draw_image_foreground (s)
4066 struct glyph_string *s;
4067 {
4068 int x;
4069 int y = s->ybase - image_ascent (s->img, s->face);
4070
4071 /* If first glyph of S has a left box line, start drawing it to the
4072 right of that line. */
4073 if (s->face->box != FACE_NO_BOX
4074 && s->first_glyph->left_box_line_p)
4075 x = s->x + abs (s->face->box_line_width);
4076 else
4077 x = s->x;
4078
4079 /* If there is a margin around the image, adjust x- and y-position
4080 by that margin. */
4081 x += s->img->hmargin;
4082 y += s->img->vmargin;
4083
4084 if (s->img->pixmap)
4085 {
4086 if (s->img->mask)
4087 {
4088 /* We can't set both a clip mask and use XSetClipRectangles
4089 because the latter also sets a clip mask. We also can't
4090 trust on the shape extension to be available
4091 (XShapeCombineRegion). So, compute the rectangle to draw
4092 manually. */
4093 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4094 | GCFunction);
4095 XGCValues xgcv;
4096 XRectangle clip_rect, image_rect, r;
4097
4098 xgcv.clip_mask = s->img->mask;
4099 xgcv.clip_x_origin = x;
4100 xgcv.clip_y_origin = y;
4101 xgcv.function = GXcopy;
4102 XChangeGC (s->display, s->gc, mask, &xgcv);
4103
4104 x_get_glyph_string_clip_rect (s, &clip_rect);
4105 image_rect.x = x;
4106 image_rect.y = y;
4107 image_rect.width = s->img->width;
4108 image_rect.height = s->img->height;
4109 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4110 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4111 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4112 }
4113 else
4114 {
4115 XRectangle clip_rect, image_rect, r;
4116
4117 x_get_glyph_string_clip_rect (s, &clip_rect);
4118 image_rect.x = x;
4119 image_rect.y = y;
4120 image_rect.width = s->img->width;
4121 image_rect.height = s->img->height;
4122 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4123 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4124 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4125
4126 /* When the image has a mask, we can expect that at
4127 least part of a mouse highlight or a block cursor will
4128 be visible. If the image doesn't have a mask, make
4129 a block cursor visible by drawing a rectangle around
4130 the image. I believe it's looking better if we do
4131 nothing here for mouse-face. */
4132 if (s->hl == DRAW_CURSOR)
4133 {
4134 int r = s->img->relief;
4135 if (r < 0) r = -r;
4136 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
4137 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
4138 }
4139 }
4140 }
4141 else
4142 /* Draw a rectangle if image could not be loaded. */
4143 XDrawRectangle (s->display, s->window, s->gc, x, y,
4144 s->img->width - 1, s->img->height - 1);
4145 }
4146
4147
4148 /* Draw a relief around the image glyph string S. */
4149
4150 static void
4151 x_draw_image_relief (s)
4152 struct glyph_string *s;
4153 {
4154 int x0, y0, x1, y1, thick, raised_p;
4155 XRectangle r;
4156 int x;
4157 int y = s->ybase - image_ascent (s->img, s->face);
4158
4159 /* If first glyph of S has a left box line, start drawing it to the
4160 right of that line. */
4161 if (s->face->box != FACE_NO_BOX
4162 && s->first_glyph->left_box_line_p)
4163 x = s->x + abs (s->face->box_line_width);
4164 else
4165 x = s->x;
4166
4167 /* If there is a margin around the image, adjust x- and y-position
4168 by that margin. */
4169 x += s->img->hmargin;
4170 y += s->img->vmargin;
4171
4172 if (s->hl == DRAW_IMAGE_SUNKEN
4173 || s->hl == DRAW_IMAGE_RAISED)
4174 {
4175 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
4176 raised_p = s->hl == DRAW_IMAGE_RAISED;
4177 }
4178 else
4179 {
4180 thick = abs (s->img->relief);
4181 raised_p = s->img->relief > 0;
4182 }
4183
4184 x0 = x - thick;
4185 y0 = y - thick;
4186 x1 = x + s->img->width + thick - 1;
4187 y1 = y + s->img->height + thick - 1;
4188
4189 x_setup_relief_colors (s);
4190 x_get_glyph_string_clip_rect (s, &r);
4191 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
4192 }
4193
4194
4195 /* Draw the foreground of image glyph string S to PIXMAP. */
4196
4197 static void
4198 x_draw_image_foreground_1 (s, pixmap)
4199 struct glyph_string *s;
4200 Pixmap pixmap;
4201 {
4202 int x;
4203 int y = s->ybase - s->y - image_ascent (s->img, s->face);
4204
4205 /* If first glyph of S has a left box line, start drawing it to the
4206 right of that line. */
4207 if (s->face->box != FACE_NO_BOX
4208 && s->first_glyph->left_box_line_p)
4209 x = abs (s->face->box_line_width);
4210 else
4211 x = 0;
4212
4213 /* If there is a margin around the image, adjust x- and y-position
4214 by that margin. */
4215 x += s->img->hmargin;
4216 y += s->img->vmargin;
4217
4218 if (s->img->pixmap)
4219 {
4220 if (s->img->mask)
4221 {
4222 /* We can't set both a clip mask and use XSetClipRectangles
4223 because the latter also sets a clip mask. We also can't
4224 trust on the shape extension to be available
4225 (XShapeCombineRegion). So, compute the rectangle to draw
4226 manually. */
4227 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4228 | GCFunction);
4229 XGCValues xgcv;
4230
4231 xgcv.clip_mask = s->img->mask;
4232 xgcv.clip_x_origin = x;
4233 xgcv.clip_y_origin = y;
4234 xgcv.function = GXcopy;
4235 XChangeGC (s->display, s->gc, mask, &xgcv);
4236
4237 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4238 0, 0, s->img->width, s->img->height, x, y);
4239 XSetClipMask (s->display, s->gc, None);
4240 }
4241 else
4242 {
4243 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4244 0, 0, s->img->width, s->img->height, x, y);
4245
4246 /* When the image has a mask, we can expect that at
4247 least part of a mouse highlight or a block cursor will
4248 be visible. If the image doesn't have a mask, make
4249 a block cursor visible by drawing a rectangle around
4250 the image. I believe it's looking better if we do
4251 nothing here for mouse-face. */
4252 if (s->hl == DRAW_CURSOR)
4253 {
4254 int r = s->img->relief;
4255 if (r < 0) r = -r;
4256 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
4257 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
4258 }
4259 }
4260 }
4261 else
4262 /* Draw a rectangle if image could not be loaded. */
4263 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4264 s->img->width - 1, s->img->height - 1);
4265 }
4266
4267
4268 /* Draw part of the background of glyph string S. X, Y, W, and H
4269 give the rectangle to draw. */
4270
4271 static void
4272 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4273 struct glyph_string *s;
4274 int x, y, w, h;
4275 {
4276 if (s->stippled_p)
4277 {
4278 /* Fill background with a stipple pattern. */
4279 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4280 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4281 XSetFillStyle (s->display, s->gc, FillSolid);
4282 }
4283 else
4284 x_clear_glyph_string_rect (s, x, y, w, h);
4285 }
4286
4287
4288 /* Draw image glyph string S.
4289
4290 s->y
4291 s->x +-------------------------
4292 | s->face->box
4293 |
4294 | +-------------------------
4295 | | s->img->margin
4296 | |
4297 | | +-------------------
4298 | | | the image
4299
4300 */
4301
4302 static void
4303 x_draw_image_glyph_string (s)
4304 struct glyph_string *s;
4305 {
4306 int x, y;
4307 int box_line_hwidth = abs (s->face->box_line_width);
4308 int box_line_vwidth = max (s->face->box_line_width, 0);
4309 int height;
4310 Pixmap pixmap = None;
4311
4312 height = s->height - 2 * box_line_vwidth;
4313
4314 /* Fill background with face under the image. Do it only if row is
4315 taller than image or if image has a clip mask to reduce
4316 flickering. */
4317 s->stippled_p = s->face->stipple != 0;
4318 if (height > s->img->height
4319 || s->img->hmargin
4320 || s->img->vmargin
4321 || s->img->mask
4322 || s->img->pixmap == 0
4323 || s->width != s->background_width)
4324 {
4325 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4326 x = s->x + box_line_hwidth;
4327 else
4328 x = s->x;
4329
4330 y = s->y + box_line_vwidth;
4331
4332 if (s->img->mask)
4333 {
4334 /* Create a pixmap as large as the glyph string. Fill it
4335 with the background color. Copy the image to it, using
4336 its mask. Copy the temporary pixmap to the display. */
4337 Screen *screen = FRAME_X_SCREEN (s->f);
4338 int depth = DefaultDepthOfScreen (screen);
4339
4340 /* Create a pixmap as large as the glyph string. */
4341 pixmap = XCreatePixmap (s->display, s->window,
4342 s->background_width,
4343 s->height, depth);
4344
4345 /* Don't clip in the following because we're working on the
4346 pixmap. */
4347 XSetClipMask (s->display, s->gc, None);
4348
4349 /* Fill the pixmap with the background color/stipple. */
4350 if (s->stippled_p)
4351 {
4352 /* Fill background with a stipple pattern. */
4353 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4354 XFillRectangle (s->display, pixmap, s->gc,
4355 0, 0, s->background_width, s->height);
4356 XSetFillStyle (s->display, s->gc, FillSolid);
4357 }
4358 else
4359 {
4360 XGCValues xgcv;
4361 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4362 &xgcv);
4363 XSetForeground (s->display, s->gc, xgcv.background);
4364 XFillRectangle (s->display, pixmap, s->gc,
4365 0, 0, s->background_width, s->height);
4366 XSetForeground (s->display, s->gc, xgcv.foreground);
4367 }
4368 }
4369 else
4370 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4371
4372 s->background_filled_p = 1;
4373 }
4374
4375 /* Draw the foreground. */
4376 if (pixmap != None)
4377 {
4378 x_draw_image_foreground_1 (s, pixmap);
4379 x_set_glyph_string_clipping (s);
4380 XCopyArea (s->display, pixmap, s->window, s->gc,
4381 0, 0, s->background_width, s->height, s->x, s->y);
4382 XFreePixmap (s->display, pixmap);
4383 }
4384 else
4385 x_draw_image_foreground (s);
4386
4387 /* If we must draw a relief around the image, do it. */
4388 if (s->img->relief
4389 || s->hl == DRAW_IMAGE_RAISED
4390 || s->hl == DRAW_IMAGE_SUNKEN)
4391 x_draw_image_relief (s);
4392 }
4393
4394
4395 /* Draw stretch glyph string S. */
4396
4397 static void
4398 x_draw_stretch_glyph_string (s)
4399 struct glyph_string *s;
4400 {
4401 xassert (s->first_glyph->type == STRETCH_GLYPH);
4402 s->stippled_p = s->face->stipple != 0;
4403
4404 if (s->hl == DRAW_CURSOR
4405 && !x_stretch_cursor_p)
4406 {
4407 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4408 as wide as the stretch glyph. */
4409 int width = min (CANON_X_UNIT (s->f), s->background_width);
4410
4411 /* Draw cursor. */
4412 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4413
4414 /* Clear rest using the GC of the original non-cursor face. */
4415 if (width < s->background_width)
4416 {
4417 int x = s->x + width, y = s->y;
4418 int w = s->background_width - width, h = s->height;
4419 XRectangle r;
4420 GC gc;
4421
4422 if (s->row->mouse_face_p
4423 && cursor_in_mouse_face_p (s->w))
4424 {
4425 x_set_mouse_face_gc (s);
4426 gc = s->gc;
4427 }
4428 else
4429 gc = s->face->gc;
4430
4431 x_get_glyph_string_clip_rect (s, &r);
4432 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4433
4434 if (s->face->stipple)
4435 {
4436 /* Fill background with a stipple pattern. */
4437 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4438 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4439 XSetFillStyle (s->display, gc, FillSolid);
4440 }
4441 else
4442 {
4443 XGCValues xgcv;
4444 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4445 XSetForeground (s->display, gc, xgcv.background);
4446 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4447 XSetForeground (s->display, gc, xgcv.foreground);
4448 }
4449 }
4450 }
4451 else if (!s->background_filled_p)
4452 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4453 s->height);
4454
4455 s->background_filled_p = 1;
4456 }
4457
4458
4459 /* Draw glyph string S. */
4460
4461 static void
4462 x_draw_glyph_string (s)
4463 struct glyph_string *s;
4464 {
4465 int relief_drawn_p = 0;
4466
4467 /* If S draws into the background of its successor, draw the
4468 background of the successor first so that S can draw into it.
4469 This makes S->next use XDrawString instead of XDrawImageString. */
4470 if (s->next && s->right_overhang && !s->for_overlaps_p)
4471 {
4472 xassert (s->next->img == NULL);
4473 x_set_glyph_string_gc (s->next);
4474 x_set_glyph_string_clipping (s->next);
4475 x_draw_glyph_string_background (s->next, 1);
4476 }
4477
4478 /* Set up S->gc, set clipping and draw S. */
4479 x_set_glyph_string_gc (s);
4480
4481 /* Draw relief (if any) in advance for char/composition so that the
4482 glyph string can be drawn over it. */
4483 if (!s->for_overlaps_p
4484 && s->face->box != FACE_NO_BOX
4485 && (s->first_glyph->type == CHAR_GLYPH
4486 || s->first_glyph->type == COMPOSITE_GLYPH))
4487
4488 {
4489 x_set_glyph_string_clipping (s);
4490 x_draw_glyph_string_background (s, 1);
4491 x_draw_glyph_string_box (s);
4492 x_set_glyph_string_clipping (s);
4493 relief_drawn_p = 1;
4494 }
4495 else
4496 x_set_glyph_string_clipping (s);
4497
4498 switch (s->first_glyph->type)
4499 {
4500 case IMAGE_GLYPH:
4501 x_draw_image_glyph_string (s);
4502 break;
4503
4504 case STRETCH_GLYPH:
4505 x_draw_stretch_glyph_string (s);
4506 break;
4507
4508 case CHAR_GLYPH:
4509 if (s->for_overlaps_p)
4510 s->background_filled_p = 1;
4511 else
4512 x_draw_glyph_string_background (s, 0);
4513 x_draw_glyph_string_foreground (s);
4514 break;
4515
4516 case COMPOSITE_GLYPH:
4517 if (s->for_overlaps_p || s->gidx > 0)
4518 s->background_filled_p = 1;
4519 else
4520 x_draw_glyph_string_background (s, 1);
4521 x_draw_composite_glyph_string_foreground (s);
4522 break;
4523
4524 default:
4525 abort ();
4526 }
4527
4528 if (!s->for_overlaps_p)
4529 {
4530 /* Draw underline. */
4531 if (s->face->underline_p)
4532 {
4533 unsigned long tem, h;
4534 int y;
4535
4536 /* Get the underline thickness. Default is 1 pixel. */
4537 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4538 h = 1;
4539
4540 /* Get the underline position. This is the recommended
4541 vertical offset in pixels from the baseline to the top of
4542 the underline. This is a signed value according to the
4543 specs, and its default is
4544
4545 ROUND ((maximum descent) / 2), with
4546 ROUND(x) = floor (x + 0.5) */
4547
4548 if (x_use_underline_position_properties
4549 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4550 y = s->ybase + (long) tem;
4551 else if (s->face->font)
4552 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4553 else
4554 y = s->y + s->height - h;
4555
4556 if (s->face->underline_defaulted_p)
4557 XFillRectangle (s->display, s->window, s->gc,
4558 s->x, y, s->width, h);
4559 else
4560 {
4561 XGCValues xgcv;
4562 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4563 XSetForeground (s->display, s->gc, s->face->underline_color);
4564 XFillRectangle (s->display, s->window, s->gc,
4565 s->x, y, s->width, h);
4566 XSetForeground (s->display, s->gc, xgcv.foreground);
4567 }
4568 }
4569
4570 /* Draw overline. */
4571 if (s->face->overline_p)
4572 {
4573 unsigned long dy = 0, h = 1;
4574
4575 if (s->face->overline_color_defaulted_p)
4576 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4577 s->width, h);
4578 else
4579 {
4580 XGCValues xgcv;
4581 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4582 XSetForeground (s->display, s->gc, s->face->overline_color);
4583 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4584 s->width, h);
4585 XSetForeground (s->display, s->gc, xgcv.foreground);
4586 }
4587 }
4588
4589 /* Draw strike-through. */
4590 if (s->face->strike_through_p)
4591 {
4592 unsigned long h = 1;
4593 unsigned long dy = (s->height - h) / 2;
4594
4595 if (s->face->strike_through_color_defaulted_p)
4596 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4597 s->width, h);
4598 else
4599 {
4600 XGCValues xgcv;
4601 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4602 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4603 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4604 s->width, h);
4605 XSetForeground (s->display, s->gc, xgcv.foreground);
4606 }
4607 }
4608
4609 /* Draw relief if not yet drawn. */
4610 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
4611 x_draw_glyph_string_box (s);
4612 }
4613
4614 /* Reset clipping. */
4615 XSetClipMask (s->display, s->gc, None);
4616 }
4617
4618
4619 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4620 struct face **, int));
4621
4622
4623 /* Fill glyph string S with composition components specified by S->cmp.
4624
4625 FACES is an array of faces for all components of this composition.
4626 S->gidx is the index of the first component for S.
4627 OVERLAPS_P non-zero means S should draw the foreground only, and
4628 use its physical height for clipping.
4629
4630 Value is the index of a component not in S. */
4631
4632 static int
4633 x_fill_composite_glyph_string (s, faces, overlaps_p)
4634 struct glyph_string *s;
4635 struct face **faces;
4636 int overlaps_p;
4637 {
4638 int i;
4639
4640 xassert (s);
4641
4642 s->for_overlaps_p = overlaps_p;
4643
4644 s->face = faces[s->gidx];
4645 s->font = s->face->font;
4646 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4647
4648 /* For all glyphs of this composition, starting at the offset
4649 S->gidx, until we reach the end of the definition or encounter a
4650 glyph that requires the different face, add it to S. */
4651 ++s->nchars;
4652 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4653 ++s->nchars;
4654
4655 /* All glyph strings for the same composition has the same width,
4656 i.e. the width set for the first component of the composition. */
4657
4658 s->width = s->first_glyph->pixel_width;
4659
4660 /* If the specified font could not be loaded, use the frame's
4661 default font, but record the fact that we couldn't load it in
4662 the glyph string so that we can draw rectangles for the
4663 characters of the glyph string. */
4664 if (s->font == NULL)
4665 {
4666 s->font_not_found_p = 1;
4667 s->font = FRAME_FONT (s->f);
4668 }
4669
4670 /* Adjust base line for subscript/superscript text. */
4671 s->ybase += s->first_glyph->voffset;
4672
4673 xassert (s->face && s->face->gc);
4674
4675 /* This glyph string must always be drawn with 16-bit functions. */
4676 s->two_byte_p = 1;
4677
4678 return s->gidx + s->nchars;
4679 }
4680
4681
4682 /* Fill glyph string S from a sequence of character glyphs.
4683
4684 FACE_ID is the face id of the string. START is the index of the
4685 first glyph to consider, END is the index of the last + 1.
4686 OVERLAPS_P non-zero means S should draw the foreground only, and
4687 use its physical height for clipping.
4688
4689 Value is the index of the first glyph not in S. */
4690
4691 static int
4692 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4693 struct glyph_string *s;
4694 int face_id;
4695 int start, end, overlaps_p;
4696 {
4697 struct glyph *glyph, *last;
4698 int voffset;
4699 int glyph_not_available_p;
4700
4701 xassert (s->f == XFRAME (s->w->frame));
4702 xassert (s->nchars == 0);
4703 xassert (start >= 0 && end > start);
4704
4705 s->for_overlaps_p = overlaps_p,
4706 glyph = s->row->glyphs[s->area] + start;
4707 last = s->row->glyphs[s->area] + end;
4708 voffset = glyph->voffset;
4709
4710 glyph_not_available_p = glyph->glyph_not_available_p;
4711
4712 while (glyph < last
4713 && glyph->type == CHAR_GLYPH
4714 && glyph->voffset == voffset
4715 /* Same face id implies same font, nowadays. */
4716 && glyph->face_id == face_id
4717 && glyph->glyph_not_available_p == glyph_not_available_p)
4718 {
4719 int two_byte_p;
4720
4721 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4722 s->char2b + s->nchars,
4723 &two_byte_p);
4724 s->two_byte_p = two_byte_p;
4725 ++s->nchars;
4726 xassert (s->nchars <= end - start);
4727 s->width += glyph->pixel_width;
4728 ++glyph;
4729 }
4730
4731 s->font = s->face->font;
4732 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4733
4734 /* If the specified font could not be loaded, use the frame's font,
4735 but record the fact that we couldn't load it in
4736 S->font_not_found_p so that we can draw rectangles for the
4737 characters of the glyph string. */
4738 if (s->font == NULL || glyph_not_available_p)
4739 {
4740 s->font_not_found_p = 1;
4741 s->font = FRAME_FONT (s->f);
4742 }
4743
4744 /* Adjust base line for subscript/superscript text. */
4745 s->ybase += voffset;
4746
4747 xassert (s->face && s->face->gc);
4748 return glyph - s->row->glyphs[s->area];
4749 }
4750
4751
4752 /* Fill glyph string S from image glyph S->first_glyph. */
4753
4754 static void
4755 x_fill_image_glyph_string (s)
4756 struct glyph_string *s;
4757 {
4758 xassert (s->first_glyph->type == IMAGE_GLYPH);
4759 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4760 xassert (s->img);
4761 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4762 s->font = s->face->font;
4763 s->width = s->first_glyph->pixel_width;
4764
4765 /* Adjust base line for subscript/superscript text. */
4766 s->ybase += s->first_glyph->voffset;
4767 }
4768
4769
4770 /* Fill glyph string S from a sequence of stretch glyphs.
4771
4772 ROW is the glyph row in which the glyphs are found, AREA is the
4773 area within the row. START is the index of the first glyph to
4774 consider, END is the index of the last + 1.
4775
4776 Value is the index of the first glyph not in S. */
4777
4778 static int
4779 x_fill_stretch_glyph_string (s, row, area, start, end)
4780 struct glyph_string *s;
4781 struct glyph_row *row;
4782 enum glyph_row_area area;
4783 int start, end;
4784 {
4785 struct glyph *glyph, *last;
4786 int voffset, face_id;
4787
4788 xassert (s->first_glyph->type == STRETCH_GLYPH);
4789
4790 glyph = s->row->glyphs[s->area] + start;
4791 last = s->row->glyphs[s->area] + end;
4792 face_id = glyph->face_id;
4793 s->face = FACE_FROM_ID (s->f, face_id);
4794 s->font = s->face->font;
4795 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4796 s->width = glyph->pixel_width;
4797 voffset = glyph->voffset;
4798
4799 for (++glyph;
4800 (glyph < last
4801 && glyph->type == STRETCH_GLYPH
4802 && glyph->voffset == voffset
4803 && glyph->face_id == face_id);
4804 ++glyph)
4805 s->width += glyph->pixel_width;
4806
4807 /* Adjust base line for subscript/superscript text. */
4808 s->ybase += voffset;
4809
4810 /* The case that face->gc == 0 is handled when drawing the glyph
4811 string by calling PREPARE_FACE_FOR_DISPLAY. */
4812 xassert (s->face);
4813 return glyph - s->row->glyphs[s->area];
4814 }
4815
4816
4817 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4818 of XChar2b structures for S; it can't be allocated in
4819 x_init_glyph_string because it must be allocated via `alloca'. W
4820 is the window on which S is drawn. ROW and AREA are the glyph row
4821 and area within the row from which S is constructed. START is the
4822 index of the first glyph structure covered by S. HL is a
4823 face-override for drawing S. */
4824
4825 static void
4826 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4827 struct glyph_string *s;
4828 XChar2b *char2b;
4829 struct window *w;
4830 struct glyph_row *row;
4831 enum glyph_row_area area;
4832 int start;
4833 enum draw_glyphs_face hl;
4834 {
4835 bzero (s, sizeof *s);
4836 s->w = w;
4837 s->f = XFRAME (w->frame);
4838 s->display = FRAME_X_DISPLAY (s->f);
4839 s->window = FRAME_X_WINDOW (s->f);
4840 s->char2b = char2b;
4841 s->hl = hl;
4842 s->row = row;
4843 s->area = area;
4844 s->first_glyph = row->glyphs[area] + start;
4845 s->height = row->height;
4846 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4847
4848 /* Display the internal border below the tool-bar window. */
4849 if (s->w == XWINDOW (s->f->tool_bar_window))
4850 s->y -= s->f->output_data.x->internal_border_width;
4851
4852 s->ybase = s->y + row->ascent;
4853 }
4854
4855
4856 /* Set background width of glyph string S. START is the index of the
4857 first glyph following S. LAST_X is the right-most x-position + 1
4858 in the drawing area. */
4859
4860 static INLINE void
4861 x_set_glyph_string_background_width (s, start, last_x)
4862 struct glyph_string *s;
4863 int start;
4864 int last_x;
4865 {
4866 /* If the face of this glyph string has to be drawn to the end of
4867 the drawing area, set S->extends_to_end_of_line_p. */
4868 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4869
4870 if (start == s->row->used[s->area]
4871 && s->area == TEXT_AREA
4872 && ((s->hl == DRAW_NORMAL_TEXT
4873 && (s->row->fill_line_p
4874 || s->face->background != default_face->background
4875 || s->face->stipple != default_face->stipple
4876 || s->row->mouse_face_p))
4877 || s->hl == DRAW_MOUSE_FACE
4878 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
4879 && s->row->fill_line_p)))
4880 s->extends_to_end_of_line_p = 1;
4881
4882 /* If S extends its face to the end of the line, set its
4883 background_width to the distance to the right edge of the drawing
4884 area. */
4885 if (s->extends_to_end_of_line_p)
4886 s->background_width = last_x - s->x + 1;
4887 else
4888 s->background_width = s->width;
4889 }
4890
4891
4892 /* Add a glyph string for a stretch glyph to the list of strings
4893 between HEAD and TAIL. START is the index of the stretch glyph in
4894 row area AREA of glyph row ROW. END is the index of the last glyph
4895 in that glyph row area. X is the current output position assigned
4896 to the new glyph string constructed. HL overrides that face of the
4897 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4898 is the right-most x-position of the drawing area. */
4899
4900 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4901 and below -- keep them on one line. */
4902 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4903 do \
4904 { \
4905 s = (struct glyph_string *) alloca (sizeof *s); \
4906 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4907 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4908 x_append_glyph_string (&HEAD, &TAIL, s); \
4909 s->x = (X); \
4910 } \
4911 while (0)
4912
4913
4914 /* Add a glyph string for an image glyph to the list of strings
4915 between HEAD and TAIL. START is the index of the image glyph in
4916 row area AREA of glyph row ROW. END is the index of the last glyph
4917 in that glyph row area. X is the current output position assigned
4918 to the new glyph string constructed. HL overrides that face of the
4919 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4920 is the right-most x-position of the drawing area. */
4921
4922 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4923 do \
4924 { \
4925 s = (struct glyph_string *) alloca (sizeof *s); \
4926 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4927 x_fill_image_glyph_string (s); \
4928 x_append_glyph_string (&HEAD, &TAIL, s); \
4929 ++START; \
4930 s->x = (X); \
4931 } \
4932 while (0)
4933
4934
4935 /* Add a glyph string for a sequence of character glyphs to the list
4936 of strings between HEAD and TAIL. START is the index of the first
4937 glyph in row area AREA of glyph row ROW that is part of the new
4938 glyph string. END is the index of the last glyph in that glyph row
4939 area. X is the current output position assigned to the new glyph
4940 string constructed. HL overrides that face of the glyph; e.g. it
4941 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4942 right-most x-position of the drawing area. */
4943
4944 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4945 do \
4946 { \
4947 int c, face_id; \
4948 XChar2b *char2b; \
4949 \
4950 c = (ROW)->glyphs[AREA][START].u.ch; \
4951 face_id = (ROW)->glyphs[AREA][START].face_id; \
4952 \
4953 s = (struct glyph_string *) alloca (sizeof *s); \
4954 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4955 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4956 x_append_glyph_string (&HEAD, &TAIL, s); \
4957 s->x = (X); \
4958 START = x_fill_glyph_string (s, face_id, START, END, \
4959 OVERLAPS_P); \
4960 } \
4961 while (0)
4962
4963
4964 /* Add a glyph string for a composite sequence to the list of strings
4965 between HEAD and TAIL. START is the index of the first glyph in
4966 row area AREA of glyph row ROW that is part of the new glyph
4967 string. END is the index of the last glyph in that glyph row area.
4968 X is the current output position assigned to the new glyph string
4969 constructed. HL overrides that face of the glyph; e.g. it is
4970 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4971 x-position of the drawing area. */
4972
4973 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4974 do { \
4975 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4976 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4977 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4978 struct composition *cmp = composition_table[cmp_id]; \
4979 int glyph_len = cmp->glyph_len; \
4980 XChar2b *char2b; \
4981 struct face **faces; \
4982 struct glyph_string *first_s = NULL; \
4983 int n; \
4984 \
4985 base_face = base_face->ascii_face; \
4986 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4987 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4988 /* At first, fill in `char2b' and `faces'. */ \
4989 for (n = 0; n < glyph_len; n++) \
4990 { \
4991 int c = COMPOSITION_GLYPH (cmp, n); \
4992 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4993 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4994 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4995 this_face_id, char2b + n, 1, 1); \
4996 } \
4997 \
4998 /* Make glyph_strings for each glyph sequence that is drawable by \
4999 the same face, and append them to HEAD/TAIL. */ \
5000 for (n = 0; n < cmp->glyph_len;) \
5001 { \
5002 s = (struct glyph_string *) alloca (sizeof *s); \
5003 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
5004 x_append_glyph_string (&(HEAD), &(TAIL), s); \
5005 s->cmp = cmp; \
5006 s->gidx = n; \
5007 s->x = (X); \
5008 \
5009 if (n == 0) \
5010 first_s = s; \
5011 \
5012 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
5013 } \
5014 \
5015 ++START; \
5016 s = first_s; \
5017 } while (0)
5018
5019
5020 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
5021 of AREA of glyph row ROW on window W between indices START and END.
5022 HL overrides the face for drawing glyph strings, e.g. it is
5023 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
5024 x-positions of the drawing area.
5025
5026 This is an ugly monster macro construct because we must use alloca
5027 to allocate glyph strings (because x_draw_glyphs can be called
5028 asynchronously). */
5029
5030 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
5031 do \
5032 { \
5033 HEAD = TAIL = NULL; \
5034 while (START < END) \
5035 { \
5036 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
5037 switch (first_glyph->type) \
5038 { \
5039 case CHAR_GLYPH: \
5040 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
5041 TAIL, HL, X, LAST_X, \
5042 OVERLAPS_P); \
5043 break; \
5044 \
5045 case COMPOSITE_GLYPH: \
5046 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
5047 HEAD, TAIL, HL, X, LAST_X,\
5048 OVERLAPS_P); \
5049 break; \
5050 \
5051 case STRETCH_GLYPH: \
5052 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
5053 HEAD, TAIL, HL, X, LAST_X); \
5054 break; \
5055 \
5056 case IMAGE_GLYPH: \
5057 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
5058 TAIL, HL, X, LAST_X); \
5059 break; \
5060 \
5061 default: \
5062 abort (); \
5063 } \
5064 \
5065 x_set_glyph_string_background_width (s, START, LAST_X); \
5066 (X) += s->width; \
5067 } \
5068 } \
5069 while (0)
5070
5071
5072 /* Draw glyphs between START and END in AREA of ROW on window W,
5073 starting at x-position X. X is relative to AREA in W. HL is a
5074 face-override with the following meaning:
5075
5076 DRAW_NORMAL_TEXT draw normally
5077 DRAW_CURSOR draw in cursor face
5078 DRAW_MOUSE_FACE draw in mouse face.
5079 DRAW_INVERSE_VIDEO draw in mode line face
5080 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
5081 DRAW_IMAGE_RAISED draw an image with a raised relief around it
5082
5083 If OVERLAPS_P is non-zero, draw only the foreground of characters
5084 and clip to the physical height of ROW.
5085
5086 Value is the x-position reached, relative to AREA of W. */
5087
5088 static int
5089 x_draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
5090 struct window *w;
5091 int x;
5092 struct glyph_row *row;
5093 enum glyph_row_area area;
5094 int start, end;
5095 enum draw_glyphs_face hl;
5096 int overlaps_p;
5097 {
5098 struct glyph_string *head, *tail;
5099 struct glyph_string *s;
5100 int last_x, area_width;
5101 int x_reached;
5102 int i, j;
5103
5104 /* Let's rather be paranoid than getting a SEGV. */
5105 end = min (end, row->used[area]);
5106 start = max (0, start);
5107 start = min (end, start);
5108
5109 /* Translate X to frame coordinates. Set last_x to the right
5110 end of the drawing area. */
5111 if (row->full_width_p)
5112 {
5113 /* X is relative to the left edge of W, without scroll bars
5114 or fringes. */
5115 struct frame *f = XFRAME (w->frame);
5116 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
5117
5118 x += window_left_x;
5119 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
5120 last_x = window_left_x + area_width;
5121
5122 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5123 {
5124 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5125 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
5126 last_x += width;
5127 else
5128 x -= width;
5129 }
5130
5131 x += FRAME_INTERNAL_BORDER_WIDTH (f);
5132 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
5133 }
5134 else
5135 {
5136 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
5137 area_width = window_box_width (w, area);
5138 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
5139 }
5140
5141 /* Build a doubly-linked list of glyph_string structures between
5142 head and tail from what we have to draw. Note that the macro
5143 BUILD_GLYPH_STRINGS will modify its start parameter. That's
5144 the reason we use a separate variable `i'. */
5145 i = start;
5146 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
5147 overlaps_p);
5148 if (tail)
5149 x_reached = tail->x + tail->background_width;
5150 else
5151 x_reached = x;
5152
5153 /* If there are any glyphs with lbearing < 0 or rbearing > width in
5154 the row, redraw some glyphs in front or following the glyph
5155 strings built above. */
5156 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
5157 {
5158 int dummy_x = 0;
5159 struct glyph_string *h, *t;
5160
5161 /* Compute overhangs for all glyph strings. */
5162 for (s = head; s; s = s->next)
5163 x_compute_glyph_string_overhangs (s);
5164
5165 /* Prepend glyph strings for glyphs in front of the first glyph
5166 string that are overwritten because of the first glyph
5167 string's left overhang. The background of all strings
5168 prepended must be drawn because the first glyph string
5169 draws over it. */
5170 i = x_left_overwritten (head);
5171 if (i >= 0)
5172 {
5173 j = i;
5174 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
5175 DRAW_NORMAL_TEXT, dummy_x, last_x,
5176 overlaps_p);
5177 start = i;
5178 x_compute_overhangs_and_x (t, head->x, 1);
5179 x_prepend_glyph_string_lists (&head, &tail, h, t);
5180 }
5181
5182 /* Prepend glyph strings for glyphs in front of the first glyph
5183 string that overwrite that glyph string because of their
5184 right overhang. For these strings, only the foreground must
5185 be drawn, because it draws over the glyph string at `head'.
5186 The background must not be drawn because this would overwrite
5187 right overhangs of preceding glyphs for which no glyph
5188 strings exist. */
5189 i = x_left_overwriting (head);
5190 if (i >= 0)
5191 {
5192 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
5193 DRAW_NORMAL_TEXT, dummy_x, last_x,
5194 overlaps_p);
5195 for (s = h; s; s = s->next)
5196 s->background_filled_p = 1;
5197 x_compute_overhangs_and_x (t, head->x, 1);
5198 x_prepend_glyph_string_lists (&head, &tail, h, t);
5199 }
5200
5201 /* Append glyphs strings for glyphs following the last glyph
5202 string tail that are overwritten by tail. The background of
5203 these strings has to be drawn because tail's foreground draws
5204 over it. */
5205 i = x_right_overwritten (tail);
5206 if (i >= 0)
5207 {
5208 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5209 DRAW_NORMAL_TEXT, x, last_x,
5210 overlaps_p);
5211 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5212 x_append_glyph_string_lists (&head, &tail, h, t);
5213 }
5214
5215 /* Append glyph strings for glyphs following the last glyph
5216 string tail that overwrite tail. The foreground of such
5217 glyphs has to be drawn because it writes into the background
5218 of tail. The background must not be drawn because it could
5219 paint over the foreground of following glyphs. */
5220 i = x_right_overwriting (tail);
5221 if (i >= 0)
5222 {
5223 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5224 DRAW_NORMAL_TEXT, x, last_x,
5225 overlaps_p);
5226 for (s = h; s; s = s->next)
5227 s->background_filled_p = 1;
5228 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5229 x_append_glyph_string_lists (&head, &tail, h, t);
5230 }
5231 }
5232
5233 /* Draw all strings. */
5234 for (s = head; s; s = s->next)
5235 x_draw_glyph_string (s);
5236
5237 if (area == TEXT_AREA
5238 && !row->full_width_p
5239 /* When drawing overlapping rows, only the glyph strings'
5240 foreground is drawn, which doesn't erase a cursor
5241 completely. */
5242 && !overlaps_p)
5243 {
5244 int x0 = head ? head->x : x;
5245 int x1 = tail ? tail->x + tail->background_width : x;
5246
5247 x0 = FRAME_TO_WINDOW_PIXEL_X (w, x0);
5248 x1 = FRAME_TO_WINDOW_PIXEL_X (w, x1);
5249
5250 if (XFASTINT (w->left_margin_width) != 0)
5251 {
5252 int left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
5253 x0 -= left_area_width;
5254 x1 -= left_area_width;
5255 }
5256
5257 notice_overwritten_cursor (w, area, x0, x1,
5258 row->y, MATRIX_ROW_BOTTOM_Y (row));
5259 }
5260
5261 /* Value is the x-position up to which drawn, relative to AREA of W.
5262 This doesn't include parts drawn because of overhangs. */
5263 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5264 if (!row->full_width_p)
5265 {
5266 if (area > LEFT_MARGIN_AREA && XFASTINT (w->left_margin_width) != 0)
5267 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5268 if (area > TEXT_AREA)
5269 x_reached -= window_box_width (w, TEXT_AREA);
5270 }
5271
5272 return x_reached;
5273 }
5274
5275
5276 /* Fix the display of area AREA of overlapping row ROW in window W. */
5277
5278 static void
5279 x_fix_overlapping_area (w, row, area)
5280 struct window *w;
5281 struct glyph_row *row;
5282 enum glyph_row_area area;
5283 {
5284 int i, x;
5285
5286 BLOCK_INPUT;
5287
5288 if (area == LEFT_MARGIN_AREA)
5289 x = 0;
5290 else if (area == TEXT_AREA)
5291 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5292 else
5293 x = (window_box_width (w, LEFT_MARGIN_AREA)
5294 + window_box_width (w, TEXT_AREA));
5295
5296 for (i = 0; i < row->used[area];)
5297 {
5298 if (row->glyphs[area][i].overlaps_vertically_p)
5299 {
5300 int start = i, start_x = x;
5301
5302 do
5303 {
5304 x += row->glyphs[area][i].pixel_width;
5305 ++i;
5306 }
5307 while (i < row->used[area]
5308 && row->glyphs[area][i].overlaps_vertically_p);
5309
5310 x_draw_glyphs (w, start_x, row, area, start, i,
5311 DRAW_NORMAL_TEXT, 1);
5312 }
5313 else
5314 {
5315 x += row->glyphs[area][i].pixel_width;
5316 ++i;
5317 }
5318 }
5319
5320 UNBLOCK_INPUT;
5321 }
5322
5323
5324 /* Output LEN glyphs starting at START at the nominal cursor position.
5325 Advance the nominal cursor over the text. The global variable
5326 updated_window contains the window being updated, updated_row is
5327 the glyph row being updated, and updated_area is the area of that
5328 row being updated. */
5329
5330 static void
5331 x_write_glyphs (start, len)
5332 struct glyph *start;
5333 int len;
5334 {
5335 int x, hpos;
5336
5337 xassert (updated_window && updated_row);
5338 BLOCK_INPUT;
5339
5340 /* Write glyphs. */
5341
5342 hpos = start - updated_row->glyphs[updated_area];
5343 x = x_draw_glyphs (updated_window, output_cursor.x,
5344 updated_row, updated_area,
5345 hpos, hpos + len,
5346 DRAW_NORMAL_TEXT, 0);
5347
5348 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
5349 if (updated_area == TEXT_AREA
5350 && updated_window->phys_cursor_on_p
5351 && updated_window->phys_cursor.vpos == output_cursor.vpos
5352 && updated_window->phys_cursor.hpos >= hpos
5353 && updated_window->phys_cursor.hpos < hpos + len)
5354 updated_window->phys_cursor_on_p = 0;
5355
5356 UNBLOCK_INPUT;
5357
5358 /* Advance the output cursor. */
5359 output_cursor.hpos += len;
5360 output_cursor.x = x;
5361 }
5362
5363
5364 /* Insert LEN glyphs from START at the nominal cursor position. */
5365
5366 static void
5367 x_insert_glyphs (start, len)
5368 struct glyph *start;
5369 register int len;
5370 {
5371 struct frame *f;
5372 struct window *w;
5373 int line_height, shift_by_width, shifted_region_width;
5374 struct glyph_row *row;
5375 struct glyph *glyph;
5376 int frame_x, frame_y, hpos;
5377
5378 xassert (updated_window && updated_row);
5379 BLOCK_INPUT;
5380 w = updated_window;
5381 f = XFRAME (WINDOW_FRAME (w));
5382
5383 /* Get the height of the line we are in. */
5384 row = updated_row;
5385 line_height = row->height;
5386
5387 /* Get the width of the glyphs to insert. */
5388 shift_by_width = 0;
5389 for (glyph = start; glyph < start + len; ++glyph)
5390 shift_by_width += glyph->pixel_width;
5391
5392 /* Get the width of the region to shift right. */
5393 shifted_region_width = (window_box_width (w, updated_area)
5394 - output_cursor.x
5395 - shift_by_width);
5396
5397 /* Shift right. */
5398 frame_x = window_box_left (w, updated_area) + output_cursor.x;
5399 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5400 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5401 f->output_data.x->normal_gc,
5402 frame_x, frame_y,
5403 shifted_region_width, line_height,
5404 frame_x + shift_by_width, frame_y);
5405
5406 /* Write the glyphs. */
5407 hpos = start - row->glyphs[updated_area];
5408 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5409 DRAW_NORMAL_TEXT, 0);
5410
5411 /* Advance the output cursor. */
5412 output_cursor.hpos += len;
5413 output_cursor.x += shift_by_width;
5414 UNBLOCK_INPUT;
5415 }
5416
5417
5418 /* Delete N glyphs at the nominal cursor position. Not implemented
5419 for X frames. */
5420
5421 static void
5422 x_delete_glyphs (n)
5423 register int n;
5424 {
5425 abort ();
5426 }
5427
5428
5429 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5430 If they are <= 0, this is probably an error. */
5431
5432 void
5433 x_clear_area (dpy, window, x, y, width, height, exposures)
5434 Display *dpy;
5435 Window window;
5436 int x, y;
5437 int width, height;
5438 int exposures;
5439 {
5440 xassert (width > 0 && height > 0);
5441 XClearArea (dpy, window, x, y, width, height, exposures);
5442 }
5443
5444
5445 /* Erase the current text line from the nominal cursor position
5446 (inclusive) to pixel column TO_X (exclusive). The idea is that
5447 everything from TO_X onward is already erased.
5448
5449 TO_X is a pixel position relative to updated_area of
5450 updated_window. TO_X == -1 means clear to the end of this area. */
5451
5452 static void
5453 x_clear_end_of_line (to_x)
5454 int to_x;
5455 {
5456 struct frame *f;
5457 struct window *w = updated_window;
5458 int max_x, min_y, max_y;
5459 int from_x, from_y, to_y;
5460
5461 xassert (updated_window && updated_row);
5462 f = XFRAME (w->frame);
5463
5464 if (updated_row->full_width_p)
5465 {
5466 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5467 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5468 && !w->pseudo_window_p)
5469 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5470 }
5471 else
5472 max_x = window_box_width (w, updated_area);
5473 max_y = window_text_bottom_y (w);
5474
5475 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5476 of window. For TO_X > 0, truncate to end of drawing area. */
5477 if (to_x == 0)
5478 return;
5479 else if (to_x < 0)
5480 to_x = max_x;
5481 else
5482 to_x = min (to_x, max_x);
5483
5484 to_y = min (max_y, output_cursor.y + updated_row->height);
5485
5486 /* Notice if the cursor will be cleared by this operation. */
5487 if (!updated_row->full_width_p)
5488 notice_overwritten_cursor (w, updated_area,
5489 output_cursor.x, -1,
5490 updated_row->y,
5491 MATRIX_ROW_BOTTOM_Y (updated_row));
5492
5493 from_x = output_cursor.x;
5494
5495 /* Translate to frame coordinates. */
5496 if (updated_row->full_width_p)
5497 {
5498 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5499 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5500 }
5501 else
5502 {
5503 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5504 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5505 }
5506
5507 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5508 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5509 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5510
5511 /* Prevent inadvertently clearing to end of the X window. */
5512 if (to_x > from_x && to_y > from_y)
5513 {
5514 BLOCK_INPUT;
5515 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5516 from_x, from_y, to_x - from_x, to_y - from_y,
5517 False);
5518 UNBLOCK_INPUT;
5519 }
5520 }
5521
5522
5523 /* Clear entire frame. If updating_frame is non-null, clear that
5524 frame. Otherwise clear the selected frame. */
5525
5526 static void
5527 x_clear_frame ()
5528 {
5529 struct frame *f;
5530
5531 if (updating_frame)
5532 f = updating_frame;
5533 else
5534 f = SELECTED_FRAME ();
5535
5536 /* Clearing the frame will erase any cursor, so mark them all as no
5537 longer visible. */
5538 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5539 output_cursor.hpos = output_cursor.vpos = 0;
5540 output_cursor.x = -1;
5541
5542 /* We don't set the output cursor here because there will always
5543 follow an explicit cursor_to. */
5544 BLOCK_INPUT;
5545 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5546
5547 /* We have to clear the scroll bars, too. If we have changed
5548 colors or something like that, then they should be notified. */
5549 x_scroll_bar_clear (f);
5550
5551 XFlush (FRAME_X_DISPLAY (f));
5552 UNBLOCK_INPUT;
5553 }
5554
5555
5556 \f
5557 /* Invert the middle quarter of the frame for .15 sec. */
5558
5559 /* We use the select system call to do the waiting, so we have to make
5560 sure it's available. If it isn't, we just won't do visual bells. */
5561
5562 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5563
5564
5565 /* Subtract the `struct timeval' values X and Y, storing the result in
5566 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5567
5568 static int
5569 timeval_subtract (result, x, y)
5570 struct timeval *result, x, y;
5571 {
5572 /* Perform the carry for the later subtraction by updating y. This
5573 is safer because on some systems the tv_sec member is unsigned. */
5574 if (x.tv_usec < y.tv_usec)
5575 {
5576 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5577 y.tv_usec -= 1000000 * nsec;
5578 y.tv_sec += nsec;
5579 }
5580
5581 if (x.tv_usec - y.tv_usec > 1000000)
5582 {
5583 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5584 y.tv_usec += 1000000 * nsec;
5585 y.tv_sec -= nsec;
5586 }
5587
5588 /* Compute the time remaining to wait. tv_usec is certainly
5589 positive. */
5590 result->tv_sec = x.tv_sec - y.tv_sec;
5591 result->tv_usec = x.tv_usec - y.tv_usec;
5592
5593 /* Return indication of whether the result should be considered
5594 negative. */
5595 return x.tv_sec < y.tv_sec;
5596 }
5597
5598 void
5599 XTflash (f)
5600 struct frame *f;
5601 {
5602 BLOCK_INPUT;
5603
5604 {
5605 GC gc;
5606
5607 /* Create a GC that will use the GXxor function to flip foreground
5608 pixels into background pixels. */
5609 {
5610 XGCValues values;
5611
5612 values.function = GXxor;
5613 values.foreground = (f->output_data.x->foreground_pixel
5614 ^ f->output_data.x->background_pixel);
5615
5616 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5617 GCFunction | GCForeground, &values);
5618 }
5619
5620 {
5621 /* Get the height not including a menu bar widget. */
5622 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5623 /* Height of each line to flash. */
5624 int flash_height = FRAME_LINE_HEIGHT (f);
5625 /* These will be the left and right margins of the rectangles. */
5626 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5627 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5628
5629 int width;
5630
5631 /* Don't flash the area between a scroll bar and the frame
5632 edge it is next to. */
5633 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5634 {
5635 case vertical_scroll_bar_left:
5636 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5637 break;
5638
5639 case vertical_scroll_bar_right:
5640 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5641 break;
5642
5643 default:
5644 break;
5645 }
5646
5647 width = flash_right - flash_left;
5648
5649 /* If window is tall, flash top and bottom line. */
5650 if (height > 3 * FRAME_LINE_HEIGHT (f))
5651 {
5652 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5653 flash_left,
5654 (FRAME_INTERNAL_BORDER_WIDTH (f)
5655 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5656 width, flash_height);
5657 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5658 flash_left,
5659 (height - flash_height
5660 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5661 width, flash_height);
5662 }
5663 else
5664 /* If it is short, flash it all. */
5665 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5666 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5667 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5668
5669 x_flush (f);
5670
5671 {
5672 struct timeval wakeup;
5673
5674 EMACS_GET_TIME (wakeup);
5675
5676 /* Compute time to wait until, propagating carry from usecs. */
5677 wakeup.tv_usec += 150000;
5678 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5679 wakeup.tv_usec %= 1000000;
5680
5681 /* Keep waiting until past the time wakeup or any input gets
5682 available. */
5683 while (! detect_input_pending ())
5684 {
5685 struct timeval current;
5686 struct timeval timeout;
5687
5688 EMACS_GET_TIME (current);
5689
5690 /* Break if result would be negative. */
5691 if (timeval_subtract (&current, wakeup, current))
5692 break;
5693
5694 /* How long `select' should wait. */
5695 timeout.tv_sec = 0;
5696 timeout.tv_usec = 10000;
5697
5698 /* Try to wait that long--but we might wake up sooner. */
5699 select (0, NULL, NULL, NULL, &timeout);
5700 }
5701 }
5702
5703 /* If window is tall, flash top and bottom line. */
5704 if (height > 3 * FRAME_LINE_HEIGHT (f))
5705 {
5706 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5707 flash_left,
5708 (FRAME_INTERNAL_BORDER_WIDTH (f)
5709 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5710 width, flash_height);
5711 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5712 flash_left,
5713 (height - flash_height
5714 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5715 width, flash_height);
5716 }
5717 else
5718 /* If it is short, flash it all. */
5719 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5720 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5721 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5722
5723 XFreeGC (FRAME_X_DISPLAY (f), gc);
5724 x_flush (f);
5725 }
5726 }
5727
5728 UNBLOCK_INPUT;
5729 }
5730
5731 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5732
5733
5734 /* Make audible bell. */
5735
5736 void
5737 XTring_bell ()
5738 {
5739 struct frame *f = SELECTED_FRAME ();
5740
5741 if (FRAME_X_DISPLAY (f))
5742 {
5743 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5744 if (visible_bell)
5745 XTflash (f);
5746 else
5747 #endif
5748 {
5749 BLOCK_INPUT;
5750 XBell (FRAME_X_DISPLAY (f), 0);
5751 XFlush (FRAME_X_DISPLAY (f));
5752 UNBLOCK_INPUT;
5753 }
5754 }
5755 }
5756
5757 \f
5758 /* Specify how many text lines, from the top of the window,
5759 should be affected by insert-lines and delete-lines operations.
5760 This, and those operations, are used only within an update
5761 that is bounded by calls to x_update_begin and x_update_end. */
5762
5763 static void
5764 XTset_terminal_window (n)
5765 register int n;
5766 {
5767 /* This function intentionally left blank. */
5768 }
5769
5770
5771 \f
5772 /***********************************************************************
5773 Line Dance
5774 ***********************************************************************/
5775
5776 /* Perform an insert-lines or delete-lines operation, inserting N
5777 lines or deleting -N lines at vertical position VPOS. */
5778
5779 static void
5780 x_ins_del_lines (vpos, n)
5781 int vpos, n;
5782 {
5783 abort ();
5784 }
5785
5786
5787 /* Scroll part of the display as described by RUN. */
5788
5789 static void
5790 x_scroll_run (w, run)
5791 struct window *w;
5792 struct run *run;
5793 {
5794 struct frame *f = XFRAME (w->frame);
5795 int x, y, width, height, from_y, to_y, bottom_y;
5796
5797 /* Get frame-relative bounding box of the text display area of W,
5798 without mode lines. Include in this box the left and right
5799 fringe of W. */
5800 window_box (w, -1, &x, &y, &width, &height);
5801 width += FRAME_X_FRINGE_WIDTH (f);
5802 x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
5803
5804 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5805 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5806 bottom_y = y + height;
5807
5808 if (to_y < from_y)
5809 {
5810 /* Scrolling up. Make sure we don't copy part of the mode
5811 line at the bottom. */
5812 if (from_y + run->height > bottom_y)
5813 height = bottom_y - from_y;
5814 else
5815 height = run->height;
5816 }
5817 else
5818 {
5819 /* Scolling down. Make sure we don't copy over the mode line.
5820 at the bottom. */
5821 if (to_y + run->height > bottom_y)
5822 height = bottom_y - to_y;
5823 else
5824 height = run->height;
5825 }
5826
5827 BLOCK_INPUT;
5828
5829 /* Cursor off. Will be switched on again in x_update_window_end. */
5830 updated_window = w;
5831 x_clear_cursor (w);
5832
5833 XCopyArea (FRAME_X_DISPLAY (f),
5834 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5835 f->output_data.x->normal_gc,
5836 x, from_y,
5837 width, height,
5838 x, to_y);
5839
5840 UNBLOCK_INPUT;
5841 }
5842
5843
5844 \f
5845 /***********************************************************************
5846 Exposure Events
5847 ***********************************************************************/
5848
5849 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5850 corner of the exposed rectangle. W and H are width and height of
5851 the exposed area. All are pixel values. W or H zero means redraw
5852 the entire frame. */
5853
5854 static void
5855 expose_frame (f, x, y, w, h)
5856 struct frame *f;
5857 int x, y, w, h;
5858 {
5859 XRectangle r;
5860 int mouse_face_overwritten_p = 0;
5861
5862 TRACE ((stderr, "expose_frame "));
5863
5864 /* No need to redraw if frame will be redrawn soon. */
5865 if (FRAME_GARBAGED_P (f))
5866 {
5867 TRACE ((stderr, " garbaged\n"));
5868 return;
5869 }
5870
5871 /* If basic faces haven't been realized yet, there is no point in
5872 trying to redraw anything. This can happen when we get an expose
5873 event while Emacs is starting, e.g. by moving another window. */
5874 if (FRAME_FACE_CACHE (f) == NULL
5875 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5876 {
5877 TRACE ((stderr, " no faces\n"));
5878 return;
5879 }
5880
5881 if (w == 0 || h == 0)
5882 {
5883 r.x = r.y = 0;
5884 r.width = CANON_X_UNIT (f) * f->width;
5885 r.height = CANON_Y_UNIT (f) * f->height;
5886 }
5887 else
5888 {
5889 r.x = x;
5890 r.y = y;
5891 r.width = w;
5892 r.height = h;
5893 }
5894
5895 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5896 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
5897
5898 if (WINDOWP (f->tool_bar_window))
5899 mouse_face_overwritten_p
5900 |= expose_window (XWINDOW (f->tool_bar_window), &r);
5901
5902 #ifndef USE_X_TOOLKIT
5903 if (WINDOWP (f->menu_bar_window))
5904 mouse_face_overwritten_p
5905 |= expose_window (XWINDOW (f->menu_bar_window), &r);
5906 #endif /* not USE_X_TOOLKIT */
5907
5908 /* Some window managers support a focus-follows-mouse style with
5909 delayed raising of frames. Imagine a partially obscured frame,
5910 and moving the mouse into partially obscured mouse-face on that
5911 frame. The visible part of the mouse-face will be highlighted,
5912 then the WM raises the obscured frame. With at least one WM, KDE
5913 2.1, Emacs is not getting any event for the raising of the frame
5914 (even tried with SubstructureRedirectMask), only Expose events.
5915 These expose events will draw text normally, i.e. not
5916 highlighted. Which means we must redo the highlight here.
5917 Subsume it under ``we love X''. --gerd 2001-08-15 */
5918 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
5919 {
5920 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5921 if (f == dpyinfo->mouse_face_mouse_frame)
5922 {
5923 int x = dpyinfo->mouse_face_mouse_x;
5924 int y = dpyinfo->mouse_face_mouse_y;
5925 clear_mouse_face (dpyinfo);
5926 note_mouse_highlight (f, x, y);
5927 }
5928 }
5929 }
5930
5931
5932 /* Redraw (parts) of all windows in the window tree rooted at W that
5933 intersect R. R contains frame pixel coordinates. Value is
5934 non-zero if the exposure overwrites mouse-face. */
5935
5936 static int
5937 expose_window_tree (w, r)
5938 struct window *w;
5939 XRectangle *r;
5940 {
5941 struct frame *f = XFRAME (w->frame);
5942 int mouse_face_overwritten_p = 0;
5943
5944 while (w && !FRAME_GARBAGED_P (f))
5945 {
5946 if (!NILP (w->hchild))
5947 mouse_face_overwritten_p
5948 |= expose_window_tree (XWINDOW (w->hchild), r);
5949 else if (!NILP (w->vchild))
5950 mouse_face_overwritten_p
5951 |= expose_window_tree (XWINDOW (w->vchild), r);
5952 else
5953 mouse_face_overwritten_p |= expose_window (w, r);
5954
5955 w = NILP (w->next) ? NULL : XWINDOW (w->next);
5956 }
5957
5958 return mouse_face_overwritten_p;
5959 }
5960
5961
5962 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5963 which intersects rectangle R. R is in window-relative coordinates. */
5964
5965 static void
5966 expose_area (w, row, r, area)
5967 struct window *w;
5968 struct glyph_row *row;
5969 XRectangle *r;
5970 enum glyph_row_area area;
5971 {
5972 struct glyph *first = row->glyphs[area];
5973 struct glyph *end = row->glyphs[area] + row->used[area];
5974 struct glyph *last;
5975 int first_x, start_x, x;
5976
5977 if (area == TEXT_AREA && row->fill_line_p)
5978 /* If row extends face to end of line write the whole line. */
5979 x_draw_glyphs (w, 0, row, area, 0, row->used[area],
5980 DRAW_NORMAL_TEXT, 0);
5981 else
5982 {
5983 /* Set START_X to the window-relative start position for drawing glyphs of
5984 AREA. The first glyph of the text area can be partially visible.
5985 The first glyphs of other areas cannot. */
5986 if (area == LEFT_MARGIN_AREA)
5987 start_x = 0;
5988 else if (area == TEXT_AREA)
5989 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5990 else
5991 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5992 + window_box_width (w, TEXT_AREA));
5993 x = start_x;
5994
5995 /* Find the first glyph that must be redrawn. */
5996 while (first < end
5997 && x + first->pixel_width < r->x)
5998 {
5999 x += first->pixel_width;
6000 ++first;
6001 }
6002
6003 /* Find the last one. */
6004 last = first;
6005 first_x = x;
6006 while (last < end
6007 && x < r->x + r->width)
6008 {
6009 x += last->pixel_width;
6010 ++last;
6011 }
6012
6013 /* Repaint. */
6014 if (last > first)
6015 x_draw_glyphs (w, first_x - start_x, row, area,
6016 first - row->glyphs[area],
6017 last - row->glyphs[area],
6018 DRAW_NORMAL_TEXT, 0);
6019 }
6020 }
6021
6022
6023 /* Redraw the parts of the glyph row ROW on window W intersecting
6024 rectangle R. R is in window-relative coordinates. Value is
6025 non-zero if mouse-face was overwritten. */
6026
6027 static int
6028 expose_line (w, row, r)
6029 struct window *w;
6030 struct glyph_row *row;
6031 XRectangle *r;
6032 {
6033 xassert (row->enabled_p);
6034
6035 if (row->mode_line_p || w->pseudo_window_p)
6036 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
6037 DRAW_NORMAL_TEXT, 0);
6038 else
6039 {
6040 if (row->used[LEFT_MARGIN_AREA])
6041 expose_area (w, row, r, LEFT_MARGIN_AREA);
6042 if (row->used[TEXT_AREA])
6043 expose_area (w, row, r, TEXT_AREA);
6044 if (row->used[RIGHT_MARGIN_AREA])
6045 expose_area (w, row, r, RIGHT_MARGIN_AREA);
6046 x_draw_row_fringe_bitmaps (w, row);
6047 }
6048
6049 return row->mouse_face_p;
6050 }
6051
6052
6053 /* Return non-zero if W's cursor intersects rectangle R. */
6054
6055 static int
6056 x_phys_cursor_in_rect_p (w, r)
6057 struct window *w;
6058 XRectangle *r;
6059 {
6060 XRectangle cr, result;
6061 struct glyph *cursor_glyph;
6062
6063 cursor_glyph = get_phys_cursor_glyph (w);
6064 if (cursor_glyph)
6065 {
6066 cr.x = w->phys_cursor.x;
6067 cr.y = w->phys_cursor.y;
6068 cr.width = cursor_glyph->pixel_width;
6069 cr.height = w->phys_cursor_height;
6070 return x_intersect_rectangles (&cr, r, &result);
6071 }
6072 else
6073 return 0;
6074 }
6075
6076
6077 /* Redraw those parts of glyphs rows during expose event handling that
6078 overlap other rows. Redrawing of an exposed line writes over parts
6079 of lines overlapping that exposed line; this function fixes that.
6080
6081 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
6082 row in W's current matrix that is exposed and overlaps other rows.
6083 LAST_OVERLAPPING_ROW is the last such row. */
6084
6085 static void
6086 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
6087 struct window *w;
6088 struct glyph_row *first_overlapping_row;
6089 struct glyph_row *last_overlapping_row;
6090 {
6091 struct glyph_row *row;
6092
6093 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
6094 if (row->overlapping_p)
6095 {
6096 xassert (row->enabled_p && !row->mode_line_p);
6097
6098 if (row->used[LEFT_MARGIN_AREA])
6099 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
6100
6101 if (row->used[TEXT_AREA])
6102 x_fix_overlapping_area (w, row, TEXT_AREA);
6103
6104 if (row->used[RIGHT_MARGIN_AREA])
6105 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
6106 }
6107 }
6108
6109
6110 /* Redraw the part of window W intersection rectangle FR. Pixel
6111 coordinates in FR are frame-relative. Call this function with
6112 input blocked. Value is non-zero if the exposure overwrites
6113 mouse-face. */
6114
6115 static int
6116 expose_window (w, fr)
6117 struct window *w;
6118 XRectangle *fr;
6119 {
6120 struct frame *f = XFRAME (w->frame);
6121 XRectangle wr, r;
6122 int mouse_face_overwritten_p = 0;
6123
6124 /* If window is not yet fully initialized, do nothing. This can
6125 happen when toolkit scroll bars are used and a window is split.
6126 Reconfiguring the scroll bar will generate an expose for a newly
6127 created window. */
6128 if (w->current_matrix == NULL)
6129 return 0;
6130
6131 /* When we're currently updating the window, display and current
6132 matrix usually don't agree. Arrange for a thorough display
6133 later. */
6134 if (w == updated_window)
6135 {
6136 SET_FRAME_GARBAGED (f);
6137 return 0;
6138 }
6139
6140 /* Frame-relative pixel rectangle of W. */
6141 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
6142 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
6143 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
6144 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
6145
6146 if (x_intersect_rectangles (fr, &wr, &r))
6147 {
6148 int yb = window_text_bottom_y (w);
6149 struct glyph_row *row;
6150 int cursor_cleared_p;
6151 struct glyph_row *first_overlapping_row, *last_overlapping_row;
6152
6153 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
6154 r.x, r.y, r.width, r.height));
6155
6156 /* Convert to window coordinates. */
6157 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
6158 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
6159
6160 /* Turn off the cursor. */
6161 if (!w->pseudo_window_p
6162 && x_phys_cursor_in_rect_p (w, &r))
6163 {
6164 x_clear_cursor (w);
6165 cursor_cleared_p = 1;
6166 }
6167 else
6168 cursor_cleared_p = 0;
6169
6170 /* Update lines intersecting rectangle R. */
6171 first_overlapping_row = last_overlapping_row = NULL;
6172 for (row = w->current_matrix->rows;
6173 row->enabled_p;
6174 ++row)
6175 {
6176 int y0 = row->y;
6177 int y1 = MATRIX_ROW_BOTTOM_Y (row);
6178
6179 if ((y0 >= r.y && y0 < r.y + r.height)
6180 || (y1 > r.y && y1 < r.y + r.height)
6181 || (r.y >= y0 && r.y < y1)
6182 || (r.y + r.height > y0 && r.y + r.height < y1))
6183 {
6184 if (row->overlapping_p)
6185 {
6186 if (first_overlapping_row == NULL)
6187 first_overlapping_row = row;
6188 last_overlapping_row = row;
6189 }
6190
6191 if (expose_line (w, row, &r))
6192 mouse_face_overwritten_p = 1;
6193 }
6194
6195 if (y1 >= yb)
6196 break;
6197 }
6198
6199 /* Display the mode line if there is one. */
6200 if (WINDOW_WANTS_MODELINE_P (w)
6201 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
6202 row->enabled_p)
6203 && row->y < r.y + r.height)
6204 {
6205 if (expose_line (w, row, &r))
6206 mouse_face_overwritten_p = 1;
6207 }
6208
6209 if (!w->pseudo_window_p)
6210 {
6211 /* Fix the display of overlapping rows. */
6212 if (first_overlapping_row)
6213 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
6214
6215 /* Draw border between windows. */
6216 x_draw_vertical_border (w);
6217
6218 /* Turn the cursor on again. */
6219 if (cursor_cleared_p)
6220 x_update_window_cursor (w, 1);
6221 }
6222 }
6223
6224 return mouse_face_overwritten_p;
6225 }
6226
6227
6228 /* Determine the intersection of two rectangles R1 and R2. Return
6229 the intersection in *RESULT. Value is non-zero if RESULT is not
6230 empty. */
6231
6232 static int
6233 x_intersect_rectangles (r1, r2, result)
6234 XRectangle *r1, *r2, *result;
6235 {
6236 XRectangle *left, *right;
6237 XRectangle *upper, *lower;
6238 int intersection_p = 0;
6239
6240 /* Rearrange so that R1 is the left-most rectangle. */
6241 if (r1->x < r2->x)
6242 left = r1, right = r2;
6243 else
6244 left = r2, right = r1;
6245
6246 /* X0 of the intersection is right.x0, if this is inside R1,
6247 otherwise there is no intersection. */
6248 if (right->x <= left->x + left->width)
6249 {
6250 result->x = right->x;
6251
6252 /* The right end of the intersection is the minimum of the
6253 the right ends of left and right. */
6254 result->width = (min (left->x + left->width, right->x + right->width)
6255 - result->x);
6256
6257 /* Same game for Y. */
6258 if (r1->y < r2->y)
6259 upper = r1, lower = r2;
6260 else
6261 upper = r2, lower = r1;
6262
6263 /* The upper end of the intersection is lower.y0, if this is inside
6264 of upper. Otherwise, there is no intersection. */
6265 if (lower->y <= upper->y + upper->height)
6266 {
6267 result->y = lower->y;
6268
6269 /* The lower end of the intersection is the minimum of the lower
6270 ends of upper and lower. */
6271 result->height = (min (lower->y + lower->height,
6272 upper->y + upper->height)
6273 - result->y);
6274 intersection_p = 1;
6275 }
6276 }
6277
6278 return intersection_p;
6279 }
6280
6281
6282
6283
6284 \f
6285 static void
6286 frame_highlight (f)
6287 struct frame *f;
6288 {
6289 /* We used to only do this if Vx_no_window_manager was non-nil, but
6290 the ICCCM (section 4.1.6) says that the window's border pixmap
6291 and border pixel are window attributes which are "private to the
6292 client", so we can always change it to whatever we want. */
6293 BLOCK_INPUT;
6294 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6295 f->output_data.x->border_pixel);
6296 UNBLOCK_INPUT;
6297 x_update_cursor (f, 1);
6298 }
6299
6300 static void
6301 frame_unhighlight (f)
6302 struct frame *f;
6303 {
6304 /* We used to only do this if Vx_no_window_manager was non-nil, but
6305 the ICCCM (section 4.1.6) says that the window's border pixmap
6306 and border pixel are window attributes which are "private to the
6307 client", so we can always change it to whatever we want. */
6308 BLOCK_INPUT;
6309 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6310 f->output_data.x->border_tile);
6311 UNBLOCK_INPUT;
6312 x_update_cursor (f, 1);
6313 }
6314
6315 /* The focus has changed. Update the frames as necessary to reflect
6316 the new situation. Note that we can't change the selected frame
6317 here, because the Lisp code we are interrupting might become confused.
6318 Each event gets marked with the frame in which it occurred, so the
6319 Lisp code can tell when the switch took place by examining the events. */
6320
6321 static void
6322 x_new_focus_frame (dpyinfo, frame)
6323 struct x_display_info *dpyinfo;
6324 struct frame *frame;
6325 {
6326 struct frame *old_focus = dpyinfo->x_focus_frame;
6327
6328 if (frame != dpyinfo->x_focus_frame)
6329 {
6330 /* Set this before calling other routines, so that they see
6331 the correct value of x_focus_frame. */
6332 dpyinfo->x_focus_frame = frame;
6333
6334 if (old_focus && old_focus->auto_lower)
6335 x_lower_frame (old_focus);
6336
6337 #if 0
6338 selected_frame = frame;
6339 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6340 selected_frame);
6341 Fselect_window (selected_frame->selected_window);
6342 choose_minibuf_frame ();
6343 #endif /* ! 0 */
6344
6345 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6346 pending_autoraise_frame = dpyinfo->x_focus_frame;
6347 else
6348 pending_autoraise_frame = 0;
6349 }
6350
6351 x_frame_rehighlight (dpyinfo);
6352 }
6353
6354 /* Handle FocusIn and FocusOut state changes for FRAME.
6355 If FRAME has focus and there exists more than one frame, puts
6356 a FOCUS_IN_EVENT into BUFP.
6357 Returns number of events inserted into BUFP. */
6358
6359 static int
6360 x_focus_changed (type, state, dpyinfo, frame, bufp, numchars)
6361 int type;
6362 int state;
6363 struct x_display_info *dpyinfo;
6364 struct frame *frame;
6365 struct input_event *bufp;
6366 int numchars;
6367 {
6368 int nr_events = 0;
6369
6370 if (type == FocusIn)
6371 {
6372 if (dpyinfo->x_focus_event_frame != frame)
6373 {
6374 x_new_focus_frame (dpyinfo, frame);
6375 dpyinfo->x_focus_event_frame = frame;
6376
6377 /* Don't stop displaying the initial startup message
6378 for a switch-frame event we don't need. */
6379 if (numchars > 0
6380 && GC_NILP (Vterminal_frame)
6381 && GC_CONSP (Vframe_list)
6382 && !GC_NILP (XCDR (Vframe_list)))
6383 {
6384 bufp->kind = FOCUS_IN_EVENT;
6385 XSETFRAME (bufp->frame_or_window, frame);
6386 bufp->arg = Qnil;
6387 ++bufp;
6388 numchars--;
6389 ++nr_events;
6390 }
6391 }
6392
6393 frame->output_data.x->focus_state |= state;
6394
6395 #ifdef HAVE_X_I18N
6396 if (FRAME_XIC (frame))
6397 XSetICFocus (FRAME_XIC (frame));
6398 #endif
6399 }
6400 else if (type == FocusOut)
6401 {
6402 frame->output_data.x->focus_state &= ~state;
6403
6404 if (dpyinfo->x_focus_event_frame == frame)
6405 {
6406 dpyinfo->x_focus_event_frame = 0;
6407 x_new_focus_frame (dpyinfo, 0);
6408 }
6409
6410 #ifdef HAVE_X_I18N
6411 if (FRAME_XIC (frame))
6412 XUnsetICFocus (FRAME_XIC (frame));
6413 #endif
6414 }
6415
6416 return nr_events;
6417 }
6418
6419 /* The focus may have changed. Figure out if it is a real focus change,
6420 by checking both FocusIn/Out and Enter/LeaveNotify events.
6421
6422 Returns number of events inserted into BUFP. */
6423
6424 static int
6425 x_detect_focus_change (dpyinfo, event, bufp, numchars)
6426 struct x_display_info *dpyinfo;
6427 XEvent *event;
6428 struct input_event *bufp;
6429 int numchars;
6430 {
6431 struct frame *frame;
6432 int nr_events = 0;
6433
6434 frame = x_top_window_to_frame (dpyinfo, event->xany.window);
6435 if (! frame) return nr_events;
6436
6437 switch (event->type)
6438 {
6439 case EnterNotify:
6440 case LeaveNotify:
6441 if (event->xcrossing.detail != NotifyInferior
6442 && event->xcrossing.focus
6443 && ! (frame->output_data.x->focus_state & FOCUS_EXPLICIT))
6444 nr_events = x_focus_changed ((event->type == EnterNotify
6445 ? FocusIn : FocusOut),
6446 FOCUS_IMPLICIT,
6447 dpyinfo,
6448 frame,
6449 bufp,
6450 numchars);
6451 break;
6452
6453 case FocusIn:
6454 case FocusOut:
6455 nr_events = x_focus_changed (event->type,
6456 (event->xfocus.detail == NotifyPointer
6457 ? FOCUS_IMPLICIT : FOCUS_EXPLICIT),
6458 dpyinfo,
6459 frame,
6460 bufp,
6461 numchars);
6462 break;
6463 }
6464
6465 return nr_events;
6466 }
6467
6468
6469 /* Handle an event saying the mouse has moved out of an Emacs frame. */
6470
6471 void
6472 x_mouse_leave (dpyinfo)
6473 struct x_display_info *dpyinfo;
6474 {
6475 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
6476 }
6477
6478 /* The focus has changed, or we have redirected a frame's focus to
6479 another frame (this happens when a frame uses a surrogate
6480 mini-buffer frame). Shift the highlight as appropriate.
6481
6482 The FRAME argument doesn't necessarily have anything to do with which
6483 frame is being highlighted or un-highlighted; we only use it to find
6484 the appropriate X display info. */
6485
6486 static void
6487 XTframe_rehighlight (frame)
6488 struct frame *frame;
6489 {
6490 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6491 }
6492
6493 static void
6494 x_frame_rehighlight (dpyinfo)
6495 struct x_display_info *dpyinfo;
6496 {
6497 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6498
6499 if (dpyinfo->x_focus_frame)
6500 {
6501 dpyinfo->x_highlight_frame
6502 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6503 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6504 : dpyinfo->x_focus_frame);
6505 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
6506 {
6507 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6508 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
6509 }
6510 }
6511 else
6512 dpyinfo->x_highlight_frame = 0;
6513
6514 if (dpyinfo->x_highlight_frame != old_highlight)
6515 {
6516 if (old_highlight)
6517 frame_unhighlight (old_highlight);
6518 if (dpyinfo->x_highlight_frame)
6519 frame_highlight (dpyinfo->x_highlight_frame);
6520 }
6521 }
6522
6523
6524 \f
6525 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
6526
6527 /* Initialize mode_switch_bit and modifier_meaning. */
6528 static void
6529 x_find_modifier_meanings (dpyinfo)
6530 struct x_display_info *dpyinfo;
6531 {
6532 int min_code, max_code;
6533 KeySym *syms;
6534 int syms_per_code;
6535 XModifierKeymap *mods;
6536
6537 dpyinfo->meta_mod_mask = 0;
6538 dpyinfo->shift_lock_mask = 0;
6539 dpyinfo->alt_mod_mask = 0;
6540 dpyinfo->super_mod_mask = 0;
6541 dpyinfo->hyper_mod_mask = 0;
6542
6543 #ifdef HAVE_X11R4
6544 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
6545 #else
6546 min_code = dpyinfo->display->min_keycode;
6547 max_code = dpyinfo->display->max_keycode;
6548 #endif
6549
6550 syms = XGetKeyboardMapping (dpyinfo->display,
6551 min_code, max_code - min_code + 1,
6552 &syms_per_code);
6553 mods = XGetModifierMapping (dpyinfo->display);
6554
6555 /* Scan the modifier table to see which modifier bits the Meta and
6556 Alt keysyms are on. */
6557 {
6558 int row, col; /* The row and column in the modifier table. */
6559
6560 for (row = 3; row < 8; row++)
6561 for (col = 0; col < mods->max_keypermod; col++)
6562 {
6563 KeyCode code
6564 = mods->modifiermap[(row * mods->max_keypermod) + col];
6565
6566 /* Zeroes are used for filler. Skip them. */
6567 if (code == 0)
6568 continue;
6569
6570 /* Are any of this keycode's keysyms a meta key? */
6571 {
6572 int code_col;
6573
6574 for (code_col = 0; code_col < syms_per_code; code_col++)
6575 {
6576 int sym = syms[((code - min_code) * syms_per_code) + code_col];
6577
6578 switch (sym)
6579 {
6580 case XK_Meta_L:
6581 case XK_Meta_R:
6582 dpyinfo->meta_mod_mask |= (1 << row);
6583 break;
6584
6585 case XK_Alt_L:
6586 case XK_Alt_R:
6587 dpyinfo->alt_mod_mask |= (1 << row);
6588 break;
6589
6590 case XK_Hyper_L:
6591 case XK_Hyper_R:
6592 dpyinfo->hyper_mod_mask |= (1 << row);
6593 break;
6594
6595 case XK_Super_L:
6596 case XK_Super_R:
6597 dpyinfo->super_mod_mask |= (1 << row);
6598 break;
6599
6600 case XK_Shift_Lock:
6601 /* Ignore this if it's not on the lock modifier. */
6602 if ((1 << row) == LockMask)
6603 dpyinfo->shift_lock_mask = LockMask;
6604 break;
6605 }
6606 }
6607 }
6608 }
6609 }
6610
6611 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
6612 if (! dpyinfo->meta_mod_mask)
6613 {
6614 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6615 dpyinfo->alt_mod_mask = 0;
6616 }
6617
6618 /* If some keys are both alt and meta,
6619 make them just meta, not alt. */
6620 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6621 {
6622 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6623 }
6624
6625 XFree ((char *) syms);
6626 XFreeModifiermap (mods);
6627 }
6628
6629 /* Convert between the modifier bits X uses and the modifier bits
6630 Emacs uses. */
6631
6632 static unsigned int
6633 x_x_to_emacs_modifiers (dpyinfo, state)
6634 struct x_display_info *dpyinfo;
6635 unsigned int state;
6636 {
6637 EMACS_UINT mod_meta = meta_modifier;
6638 EMACS_UINT mod_alt = alt_modifier;
6639 EMACS_UINT mod_hyper = hyper_modifier;
6640 EMACS_UINT mod_super = super_modifier;
6641 Lisp_Object tem;
6642
6643 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6644 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6645 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6646 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6647 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6648 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6649 tem = Fget (Vx_super_keysym, Qmodifier_value);
6650 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
6651
6652
6653 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6654 | ((state & ControlMask) ? ctrl_modifier : 0)
6655 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
6656 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
6657 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
6658 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
6659 }
6660
6661 static unsigned int
6662 x_emacs_to_x_modifiers (dpyinfo, state)
6663 struct x_display_info *dpyinfo;
6664 unsigned int state;
6665 {
6666 EMACS_UINT mod_meta = meta_modifier;
6667 EMACS_UINT mod_alt = alt_modifier;
6668 EMACS_UINT mod_hyper = hyper_modifier;
6669 EMACS_UINT mod_super = super_modifier;
6670
6671 Lisp_Object tem;
6672
6673 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6674 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6675 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6676 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6677 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6678 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6679 tem = Fget (Vx_super_keysym, Qmodifier_value);
6680 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
6681
6682
6683 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
6684 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
6685 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
6686 | ((state & shift_modifier) ? ShiftMask : 0)
6687 | ((state & ctrl_modifier) ? ControlMask : 0)
6688 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
6689 }
6690
6691 /* Convert a keysym to its name. */
6692
6693 char *
6694 x_get_keysym_name (keysym)
6695 KeySym keysym;
6696 {
6697 char *value;
6698
6699 BLOCK_INPUT;
6700 value = XKeysymToString (keysym);
6701 UNBLOCK_INPUT;
6702
6703 return value;
6704 }
6705
6706
6707 \f
6708 /* Mouse clicks and mouse movement. Rah. */
6709
6710 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6711 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6712 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6713 not force the value into range. */
6714
6715 void
6716 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6717 FRAME_PTR f;
6718 register int pix_x, pix_y;
6719 register int *x, *y;
6720 XRectangle *bounds;
6721 int noclip;
6722 {
6723 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6724 even for negative values. */
6725 if (pix_x < 0)
6726 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6727 if (pix_y < 0)
6728 pix_y -= (f)->output_data.x->line_height - 1;
6729
6730 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6731 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6732
6733 if (bounds)
6734 {
6735 bounds->width = FONT_WIDTH (f->output_data.x->font);
6736 bounds->height = f->output_data.x->line_height;
6737 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6738 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6739 }
6740
6741 if (!noclip)
6742 {
6743 if (pix_x < 0)
6744 pix_x = 0;
6745 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6746 pix_x = FRAME_WINDOW_WIDTH (f);
6747
6748 if (pix_y < 0)
6749 pix_y = 0;
6750 else if (pix_y > f->height)
6751 pix_y = f->height;
6752 }
6753
6754 *x = pix_x;
6755 *y = pix_y;
6756 }
6757
6758
6759 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6760 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6761 can't tell the positions because W's display is not up to date,
6762 return 0. */
6763
6764 int
6765 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6766 struct window *w;
6767 int hpos, vpos;
6768 int *frame_x, *frame_y;
6769 {
6770 int success_p;
6771
6772 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6773 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6774
6775 if (display_completed)
6776 {
6777 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6778 struct glyph *glyph = row->glyphs[TEXT_AREA];
6779 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6780
6781 *frame_y = row->y;
6782 *frame_x = row->x;
6783 while (glyph < end)
6784 {
6785 *frame_x += glyph->pixel_width;
6786 ++glyph;
6787 }
6788
6789 success_p = 1;
6790 }
6791 else
6792 {
6793 *frame_y = *frame_x = 0;
6794 success_p = 0;
6795 }
6796
6797 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6798 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6799 return success_p;
6800 }
6801
6802
6803 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6804
6805 If the event is a button press, then note that we have grabbed
6806 the mouse. */
6807
6808 static Lisp_Object
6809 construct_mouse_click (result, event, f)
6810 struct input_event *result;
6811 XButtonEvent *event;
6812 struct frame *f;
6813 {
6814 /* Make the event type NO_EVENT; we'll change that when we decide
6815 otherwise. */
6816 result->kind = MOUSE_CLICK_EVENT;
6817 result->code = event->button - Button1;
6818 result->timestamp = event->time;
6819 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6820 event->state)
6821 | (event->type == ButtonRelease
6822 ? up_modifier
6823 : down_modifier));
6824
6825 XSETINT (result->x, event->x);
6826 XSETINT (result->y, event->y);
6827 XSETFRAME (result->frame_or_window, f);
6828 result->arg = Qnil;
6829 return Qnil;
6830 }
6831
6832 \f
6833 /* Function to report a mouse movement to the mainstream Emacs code.
6834 The input handler calls this.
6835
6836 We have received a mouse movement event, which is given in *event.
6837 If the mouse is over a different glyph than it was last time, tell
6838 the mainstream emacs code by setting mouse_moved. If not, ask for
6839 another motion event, so we can check again the next time it moves. */
6840
6841 static XMotionEvent last_mouse_motion_event;
6842 static Lisp_Object last_mouse_motion_frame;
6843
6844 static void
6845 note_mouse_movement (frame, event)
6846 FRAME_PTR frame;
6847 XMotionEvent *event;
6848 {
6849 last_mouse_movement_time = event->time;
6850 last_mouse_motion_event = *event;
6851 XSETFRAME (last_mouse_motion_frame, frame);
6852
6853 if (event->window != FRAME_X_WINDOW (frame))
6854 {
6855 frame->mouse_moved = 1;
6856 last_mouse_scroll_bar = Qnil;
6857 note_mouse_highlight (frame, -1, -1);
6858 }
6859
6860 /* Has the mouse moved off the glyph it was on at the last sighting? */
6861 else if (event->x < last_mouse_glyph.x
6862 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6863 || event->y < last_mouse_glyph.y
6864 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6865 {
6866 frame->mouse_moved = 1;
6867 last_mouse_scroll_bar = Qnil;
6868 note_mouse_highlight (frame, event->x, event->y);
6869 }
6870 }
6871
6872 \f
6873 /************************************************************************
6874 Mouse Face
6875 ************************************************************************/
6876
6877 /* Find the glyph under window-relative coordinates X/Y in window W.
6878 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6879 strings. Return in *HPOS and *VPOS the row and column number of
6880 the glyph found. Return in *AREA the glyph area containing X.
6881 Value is a pointer to the glyph found or null if X/Y is not on
6882 text, or we can't tell because W's current matrix is not up to
6883 date. */
6884
6885 static struct glyph *
6886 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
6887 struct window *w;
6888 int x, y;
6889 int *hpos, *vpos, *area;
6890 int buffer_only_p;
6891 {
6892 struct glyph *glyph, *end;
6893 struct glyph_row *row = NULL;
6894 int x0, i, left_area_width;
6895
6896 /* Find row containing Y. Give up if some row is not enabled. */
6897 for (i = 0; i < w->current_matrix->nrows; ++i)
6898 {
6899 row = MATRIX_ROW (w->current_matrix, i);
6900 if (!row->enabled_p)
6901 return NULL;
6902 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6903 break;
6904 }
6905
6906 *vpos = i;
6907 *hpos = 0;
6908
6909 /* Give up if Y is not in the window. */
6910 if (i == w->current_matrix->nrows)
6911 return NULL;
6912
6913 /* Get the glyph area containing X. */
6914 if (w->pseudo_window_p)
6915 {
6916 *area = TEXT_AREA;
6917 x0 = 0;
6918 }
6919 else
6920 {
6921 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6922 if (x < left_area_width)
6923 {
6924 *area = LEFT_MARGIN_AREA;
6925 x0 = 0;
6926 }
6927 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6928 {
6929 *area = TEXT_AREA;
6930 x0 = row->x + left_area_width;
6931 }
6932 else
6933 {
6934 *area = RIGHT_MARGIN_AREA;
6935 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6936 }
6937 }
6938
6939 /* Find glyph containing X. */
6940 glyph = row->glyphs[*area];
6941 end = glyph + row->used[*area];
6942 while (glyph < end)
6943 {
6944 if (x < x0 + glyph->pixel_width)
6945 {
6946 if (w->pseudo_window_p)
6947 break;
6948 else if (!buffer_only_p || BUFFERP (glyph->object))
6949 break;
6950 }
6951
6952 x0 += glyph->pixel_width;
6953 ++glyph;
6954 }
6955
6956 if (glyph == end)
6957 return NULL;
6958
6959 *hpos = glyph - row->glyphs[*area];
6960 return glyph;
6961 }
6962
6963
6964 /* Convert frame-relative x/y to coordinates relative to window W.
6965 Takes pseudo-windows into account. */
6966
6967 static void
6968 frame_to_window_pixel_xy (w, x, y)
6969 struct window *w;
6970 int *x, *y;
6971 {
6972 if (w->pseudo_window_p)
6973 {
6974 /* A pseudo-window is always full-width, and starts at the
6975 left edge of the frame, plus a frame border. */
6976 struct frame *f = XFRAME (w->frame);
6977 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6978 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6979 }
6980 else
6981 {
6982 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6983 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6984 }
6985 }
6986
6987
6988 /* Take proper action when mouse has moved to the mode or header line
6989 or marginal area of window W, x-position X and y-position Y. Area
6990 is 1, 3, 6 or 7 for the mode line, header line, left and right
6991 marginal area respectively. X is relative to the start of the text
6992 display area of W, so the width of bitmap areas and scroll bars
6993 must be subtracted to get a position relative to the start of the
6994 mode line. */
6995
6996 static void
6997 note_mode_line_or_margin_highlight (w, x, y, portion)
6998 struct window *w;
6999 int x, y, portion;
7000 {
7001 struct frame *f = XFRAME (w->frame);
7002 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7003 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
7004 int charpos;
7005 Lisp_Object string, help, map, pos;
7006
7007 if (portion == 1 || portion == 3)
7008 string = mode_line_string (w, x, y, portion == 1, &charpos);
7009 else
7010 string = marginal_area_string (w, x, y, portion, &charpos);
7011
7012 if (STRINGP (string))
7013 {
7014 pos = make_number (charpos);
7015
7016 /* If we're on a string with `help-echo' text property, arrange
7017 for the help to be displayed. This is done by setting the
7018 global variable help_echo to the help string. */
7019 help = Fget_text_property (pos, Qhelp_echo, string);
7020 if (!NILP (help))
7021 {
7022 help_echo = help;
7023 XSETWINDOW (help_echo_window, w);
7024 help_echo_object = string;
7025 help_echo_pos = charpos;
7026 }
7027
7028 /* Change the mouse pointer according to what is under X/Y. */
7029 map = Fget_text_property (pos, Qlocal_map, string);
7030 if (!KEYMAPP (map))
7031 map = Fget_text_property (pos, Qkeymap, string);
7032 if (KEYMAPP (map))
7033 cursor = f->output_data.x->nontext_cursor;
7034 }
7035
7036 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7037 }
7038
7039
7040 /* Take proper action when the mouse has moved to position X, Y on
7041 frame F as regards highlighting characters that have mouse-face
7042 properties. Also de-highlighting chars where the mouse was before.
7043 X and Y can be negative or out of range. */
7044
7045 static void
7046 note_mouse_highlight (f, x, y)
7047 struct frame *f;
7048 int x, y;
7049 {
7050 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7051 int portion;
7052 Lisp_Object window;
7053 struct window *w;
7054 Cursor cursor = None;
7055 struct buffer *b;
7056
7057 /* When a menu is active, don't highlight because this looks odd. */
7058 #ifdef USE_X_TOOLKIT
7059 if (popup_activated ())
7060 return;
7061 #endif
7062
7063 if (NILP (Vmouse_highlight)
7064 || !f->glyphs_initialized_p)
7065 return;
7066
7067 dpyinfo->mouse_face_mouse_x = x;
7068 dpyinfo->mouse_face_mouse_y = y;
7069 dpyinfo->mouse_face_mouse_frame = f;
7070
7071 if (dpyinfo->mouse_face_defer)
7072 return;
7073
7074 if (gc_in_progress)
7075 {
7076 dpyinfo->mouse_face_deferred_gc = 1;
7077 return;
7078 }
7079
7080 /* Which window is that in? */
7081 window = window_from_coordinates (f, x, y, &portion, 1);
7082
7083 /* If we were displaying active text in another window, clear that. */
7084 if (! EQ (window, dpyinfo->mouse_face_window))
7085 clear_mouse_face (dpyinfo);
7086
7087 /* Not on a window -> return. */
7088 if (!WINDOWP (window))
7089 return;
7090
7091 /* Convert to window-relative pixel coordinates. */
7092 w = XWINDOW (window);
7093 frame_to_window_pixel_xy (w, &x, &y);
7094
7095 /* Handle tool-bar window differently since it doesn't display a
7096 buffer. */
7097 if (EQ (window, f->tool_bar_window))
7098 {
7099 note_tool_bar_highlight (f, x, y);
7100 return;
7101 }
7102
7103 /* Mouse is on the mode, header line or margin? */
7104 if (portion == 1 || portion == 3 || portion == 6 || portion == 7)
7105 {
7106 note_mode_line_or_margin_highlight (w, x, y, portion);
7107 return;
7108 }
7109
7110 if (portion == 2)
7111 cursor = f->output_data.x->horizontal_drag_cursor;
7112 else
7113 cursor = f->output_data.x->text_cursor;
7114
7115 /* Are we in a window whose display is up to date?
7116 And verify the buffer's text has not changed. */
7117 b = XBUFFER (w->buffer);
7118 if (/* Within text portion of the window. */
7119 portion == 0
7120 && EQ (w->window_end_valid, w->buffer)
7121 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
7122 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
7123 {
7124 int hpos, vpos, pos, i, area;
7125 struct glyph *glyph;
7126 Lisp_Object object;
7127 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
7128 Lisp_Object *overlay_vec = NULL;
7129 int len, noverlays;
7130 struct buffer *obuf;
7131 int obegv, ozv, same_region;
7132
7133 /* Find the glyph under X/Y. */
7134 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
7135
7136 /* Clear mouse face if X/Y not over text. */
7137 if (glyph == NULL
7138 || area != TEXT_AREA
7139 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
7140 {
7141 if (clear_mouse_face (dpyinfo))
7142 cursor = None;
7143 goto set_cursor;
7144 }
7145
7146 pos = glyph->charpos;
7147 object = glyph->object;
7148 if (!STRINGP (object) && !BUFFERP (object))
7149 goto set_cursor;
7150
7151 /* If we get an out-of-range value, return now; avoid an error. */
7152 if (BUFFERP (object) && pos > BUF_Z (b))
7153 goto set_cursor;
7154
7155 /* Make the window's buffer temporarily current for
7156 overlays_at and compute_char_face. */
7157 obuf = current_buffer;
7158 current_buffer = b;
7159 obegv = BEGV;
7160 ozv = ZV;
7161 BEGV = BEG;
7162 ZV = Z;
7163
7164 /* Is this char mouse-active or does it have help-echo? */
7165 position = make_number (pos);
7166
7167 if (BUFFERP (object))
7168 {
7169 /* Put all the overlays we want in a vector in overlay_vec.
7170 Store the length in len. If there are more than 10, make
7171 enough space for all, and try again. */
7172 len = 10;
7173 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7174 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
7175 if (noverlays > len)
7176 {
7177 len = noverlays;
7178 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7179 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
7180 }
7181
7182 /* Sort overlays into increasing priority order. */
7183 noverlays = sort_overlays (overlay_vec, noverlays, w);
7184 }
7185 else
7186 noverlays = 0;
7187
7188 same_region = (EQ (window, dpyinfo->mouse_face_window)
7189 && vpos >= dpyinfo->mouse_face_beg_row
7190 && vpos <= dpyinfo->mouse_face_end_row
7191 && (vpos > dpyinfo->mouse_face_beg_row
7192 || hpos >= dpyinfo->mouse_face_beg_col)
7193 && (vpos < dpyinfo->mouse_face_end_row
7194 || hpos < dpyinfo->mouse_face_end_col
7195 || dpyinfo->mouse_face_past_end));
7196
7197 if (same_region)
7198 cursor = None;
7199
7200 /* Check mouse-face highlighting. */
7201 if (! same_region
7202 /* If there exists an overlay with mouse-face overlapping
7203 the one we are currently highlighting, we have to
7204 check if we enter the overlapping overlay, and then
7205 highlight only that. */
7206 || (OVERLAYP (dpyinfo->mouse_face_overlay)
7207 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
7208 {
7209 /* Find the highest priority overlay that has a mouse-face
7210 property. */
7211 overlay = Qnil;
7212 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
7213 {
7214 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
7215 if (!NILP (mouse_face))
7216 overlay = overlay_vec[i];
7217 }
7218
7219 /* If we're actually highlighting the same overlay as
7220 before, there's no need to do that again. */
7221 if (!NILP (overlay)
7222 && EQ (overlay, dpyinfo->mouse_face_overlay))
7223 goto check_help_echo;
7224
7225 dpyinfo->mouse_face_overlay = overlay;
7226
7227 /* Clear the display of the old active region, if any. */
7228 if (clear_mouse_face (dpyinfo))
7229 cursor = None;
7230
7231 /* If no overlay applies, get a text property. */
7232 if (NILP (overlay))
7233 mouse_face = Fget_text_property (position, Qmouse_face, object);
7234
7235 /* Handle the overlay case. */
7236 if (!NILP (overlay))
7237 {
7238 /* Find the range of text around this char that
7239 should be active. */
7240 Lisp_Object before, after;
7241 int ignore;
7242
7243 before = Foverlay_start (overlay);
7244 after = Foverlay_end (overlay);
7245 /* Record this as the current active region. */
7246 fast_find_position (w, XFASTINT (before),
7247 &dpyinfo->mouse_face_beg_col,
7248 &dpyinfo->mouse_face_beg_row,
7249 &dpyinfo->mouse_face_beg_x,
7250 &dpyinfo->mouse_face_beg_y, Qnil);
7251
7252 dpyinfo->mouse_face_past_end
7253 = !fast_find_position (w, XFASTINT (after),
7254 &dpyinfo->mouse_face_end_col,
7255 &dpyinfo->mouse_face_end_row,
7256 &dpyinfo->mouse_face_end_x,
7257 &dpyinfo->mouse_face_end_y, Qnil);
7258 dpyinfo->mouse_face_window = window;
7259 dpyinfo->mouse_face_face_id
7260 = face_at_buffer_position (w, pos, 0, 0,
7261 &ignore, pos + 1,
7262 !dpyinfo->mouse_face_hidden);
7263
7264 /* Display it as active. */
7265 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7266 cursor = None;
7267 }
7268 /* Handle the text property case. */
7269 else if (!NILP (mouse_face) && BUFFERP (object))
7270 {
7271 /* Find the range of text around this char that
7272 should be active. */
7273 Lisp_Object before, after, beginning, end;
7274 int ignore;
7275
7276 beginning = Fmarker_position (w->start);
7277 end = make_number (BUF_Z (XBUFFER (object))
7278 - XFASTINT (w->window_end_pos));
7279 before
7280 = Fprevious_single_property_change (make_number (pos + 1),
7281 Qmouse_face,
7282 object, beginning);
7283 after
7284 = Fnext_single_property_change (position, Qmouse_face,
7285 object, end);
7286
7287 /* Record this as the current active region. */
7288 fast_find_position (w, XFASTINT (before),
7289 &dpyinfo->mouse_face_beg_col,
7290 &dpyinfo->mouse_face_beg_row,
7291 &dpyinfo->mouse_face_beg_x,
7292 &dpyinfo->mouse_face_beg_y, Qnil);
7293 dpyinfo->mouse_face_past_end
7294 = !fast_find_position (w, XFASTINT (after),
7295 &dpyinfo->mouse_face_end_col,
7296 &dpyinfo->mouse_face_end_row,
7297 &dpyinfo->mouse_face_end_x,
7298 &dpyinfo->mouse_face_end_y, Qnil);
7299 dpyinfo->mouse_face_window = window;
7300
7301 if (BUFFERP (object))
7302 dpyinfo->mouse_face_face_id
7303 = face_at_buffer_position (w, pos, 0, 0,
7304 &ignore, pos + 1,
7305 !dpyinfo->mouse_face_hidden);
7306
7307 /* Display it as active. */
7308 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7309 cursor = None;
7310 }
7311 else if (!NILP (mouse_face) && STRINGP (object))
7312 {
7313 Lisp_Object b, e;
7314 int ignore;
7315
7316 b = Fprevious_single_property_change (make_number (pos + 1),
7317 Qmouse_face,
7318 object, Qnil);
7319 e = Fnext_single_property_change (position, Qmouse_face,
7320 object, Qnil);
7321 if (NILP (b))
7322 b = make_number (0);
7323 if (NILP (e))
7324 e = make_number (SCHARS (object) - 1);
7325 fast_find_string_pos (w, XINT (b), object,
7326 &dpyinfo->mouse_face_beg_col,
7327 &dpyinfo->mouse_face_beg_row,
7328 &dpyinfo->mouse_face_beg_x,
7329 &dpyinfo->mouse_face_beg_y, 0);
7330 fast_find_string_pos (w, XINT (e), object,
7331 &dpyinfo->mouse_face_end_col,
7332 &dpyinfo->mouse_face_end_row,
7333 &dpyinfo->mouse_face_end_x,
7334 &dpyinfo->mouse_face_end_y, 1);
7335 dpyinfo->mouse_face_past_end = 0;
7336 dpyinfo->mouse_face_window = window;
7337 dpyinfo->mouse_face_face_id
7338 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
7339 glyph->face_id, 1);
7340 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7341 cursor = None;
7342 }
7343 else if (STRINGP (object) && NILP (mouse_face))
7344 {
7345 /* A string which doesn't have mouse-face, but
7346 the text ``under'' it might have. */
7347 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
7348 int start = MATRIX_ROW_START_CHARPOS (r);
7349
7350 pos = string_buffer_position (w, object, start);
7351 if (pos > 0)
7352 mouse_face = get_char_property_and_overlay (make_number (pos),
7353 Qmouse_face,
7354 w->buffer,
7355 &overlay);
7356 if (!NILP (mouse_face) && !NILP (overlay))
7357 {
7358 Lisp_Object before = Foverlay_start (overlay);
7359 Lisp_Object after = Foverlay_end (overlay);
7360 int ignore;
7361
7362 /* Note that we might not be able to find position
7363 BEFORE in the glyph matrix if the overlay is
7364 entirely covered by a `display' property. In
7365 this case, we overshoot. So let's stop in
7366 the glyph matrix before glyphs for OBJECT. */
7367 fast_find_position (w, XFASTINT (before),
7368 &dpyinfo->mouse_face_beg_col,
7369 &dpyinfo->mouse_face_beg_row,
7370 &dpyinfo->mouse_face_beg_x,
7371 &dpyinfo->mouse_face_beg_y,
7372 object);
7373
7374 dpyinfo->mouse_face_past_end
7375 = !fast_find_position (w, XFASTINT (after),
7376 &dpyinfo->mouse_face_end_col,
7377 &dpyinfo->mouse_face_end_row,
7378 &dpyinfo->mouse_face_end_x,
7379 &dpyinfo->mouse_face_end_y,
7380 Qnil);
7381 dpyinfo->mouse_face_window = window;
7382 dpyinfo->mouse_face_face_id
7383 = face_at_buffer_position (w, pos, 0, 0,
7384 &ignore, pos + 1,
7385 !dpyinfo->mouse_face_hidden);
7386
7387 /* Display it as active. */
7388 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7389 cursor = None;
7390 }
7391 }
7392 }
7393
7394 check_help_echo:
7395
7396 /* Look for a `help-echo' property. */
7397 {
7398 Lisp_Object help, overlay;
7399
7400 /* Check overlays first. */
7401 help = overlay = Qnil;
7402 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
7403 {
7404 overlay = overlay_vec[i];
7405 help = Foverlay_get (overlay, Qhelp_echo);
7406 }
7407
7408 if (!NILP (help))
7409 {
7410 help_echo = help;
7411 help_echo_window = window;
7412 help_echo_object = overlay;
7413 help_echo_pos = pos;
7414 }
7415 else
7416 {
7417 Lisp_Object object = glyph->object;
7418 int charpos = glyph->charpos;
7419
7420 /* Try text properties. */
7421 if (STRINGP (object)
7422 && charpos >= 0
7423 && charpos < SCHARS (object))
7424 {
7425 help = Fget_text_property (make_number (charpos),
7426 Qhelp_echo, object);
7427 if (NILP (help))
7428 {
7429 /* If the string itself doesn't specify a help-echo,
7430 see if the buffer text ``under'' it does. */
7431 struct glyph_row *r
7432 = MATRIX_ROW (w->current_matrix, vpos);
7433 int start = MATRIX_ROW_START_CHARPOS (r);
7434 int pos = string_buffer_position (w, object, start);
7435 if (pos > 0)
7436 {
7437 help = Fget_char_property (make_number (pos),
7438 Qhelp_echo, w->buffer);
7439 if (!NILP (help))
7440 {
7441 charpos = pos;
7442 object = w->buffer;
7443 }
7444 }
7445 }
7446 }
7447 else if (BUFFERP (object)
7448 && charpos >= BEGV
7449 && charpos < ZV)
7450 help = Fget_text_property (make_number (charpos), Qhelp_echo,
7451 object);
7452
7453 if (!NILP (help))
7454 {
7455 help_echo = help;
7456 help_echo_window = window;
7457 help_echo_object = object;
7458 help_echo_pos = charpos;
7459 }
7460 }
7461 }
7462
7463 BEGV = obegv;
7464 ZV = ozv;
7465 current_buffer = obuf;
7466 }
7467
7468 set_cursor:
7469
7470 if (cursor != None)
7471 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7472 }
7473
7474 static void
7475 redo_mouse_highlight ()
7476 {
7477 if (!NILP (last_mouse_motion_frame)
7478 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
7479 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
7480 last_mouse_motion_event.x,
7481 last_mouse_motion_event.y);
7482 }
7483
7484
7485 \f
7486 /***********************************************************************
7487 Tool-bars
7488 ***********************************************************************/
7489
7490 static int x_tool_bar_item P_ ((struct frame *, int, int,
7491 struct glyph **, int *, int *, int *));
7492
7493 /* Tool-bar item index of the item on which a mouse button was pressed
7494 or -1. */
7495
7496 static int last_tool_bar_item;
7497
7498
7499 /* Get information about the tool-bar item at position X/Y on frame F.
7500 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7501 the current matrix of the tool-bar window of F, or NULL if not
7502 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
7503 item in F->tool_bar_items. Value is
7504
7505 -1 if X/Y is not on a tool-bar item
7506 0 if X/Y is on the same item that was highlighted before.
7507 1 otherwise. */
7508
7509 static int
7510 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
7511 struct frame *f;
7512 int x, y;
7513 struct glyph **glyph;
7514 int *hpos, *vpos, *prop_idx;
7515 {
7516 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7517 struct window *w = XWINDOW (f->tool_bar_window);
7518 int area;
7519
7520 /* Find the glyph under X/Y. */
7521 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
7522 if (*glyph == NULL)
7523 return -1;
7524
7525 /* Get the start of this tool-bar item's properties in
7526 f->tool_bar_items. */
7527 if (!tool_bar_item_info (f, *glyph, prop_idx))
7528 return -1;
7529
7530 /* Is mouse on the highlighted item? */
7531 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
7532 && *vpos >= dpyinfo->mouse_face_beg_row
7533 && *vpos <= dpyinfo->mouse_face_end_row
7534 && (*vpos > dpyinfo->mouse_face_beg_row
7535 || *hpos >= dpyinfo->mouse_face_beg_col)
7536 && (*vpos < dpyinfo->mouse_face_end_row
7537 || *hpos < dpyinfo->mouse_face_end_col
7538 || dpyinfo->mouse_face_past_end))
7539 return 0;
7540
7541 return 1;
7542 }
7543
7544
7545 /* Handle mouse button event on the tool-bar of frame F, at
7546 frame-relative coordinates X/Y. EVENT_TYPE is either ButtonPress
7547 or ButtonRelase. */
7548
7549 static void
7550 x_handle_tool_bar_click (f, button_event)
7551 struct frame *f;
7552 XButtonEvent *button_event;
7553 {
7554 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7555 struct window *w = XWINDOW (f->tool_bar_window);
7556 int hpos, vpos, prop_idx;
7557 struct glyph *glyph;
7558 Lisp_Object enabled_p;
7559 int x = button_event->x;
7560 int y = button_event->y;
7561
7562 /* If not on the highlighted tool-bar item, return. */
7563 frame_to_window_pixel_xy (w, &x, &y);
7564 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
7565 return;
7566
7567 /* If item is disabled, do nothing. */
7568 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7569 if (NILP (enabled_p))
7570 return;
7571
7572 if (button_event->type == ButtonPress)
7573 {
7574 /* Show item in pressed state. */
7575 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7576 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
7577 last_tool_bar_item = prop_idx;
7578 }
7579 else
7580 {
7581 Lisp_Object key, frame;
7582 struct input_event event;
7583
7584 /* Show item in released state. */
7585 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7586 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7587
7588 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
7589
7590 XSETFRAME (frame, f);
7591 event.kind = TOOL_BAR_EVENT;
7592 event.frame_or_window = frame;
7593 event.arg = frame;
7594 kbd_buffer_store_event (&event);
7595
7596 event.kind = TOOL_BAR_EVENT;
7597 event.frame_or_window = frame;
7598 event.arg = key;
7599 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7600 button_event->state);
7601 kbd_buffer_store_event (&event);
7602 last_tool_bar_item = -1;
7603 }
7604 }
7605
7606
7607 /* Possibly highlight a tool-bar item on frame F when mouse moves to
7608 tool-bar window-relative coordinates X/Y. Called from
7609 note_mouse_highlight. */
7610
7611 static void
7612 note_tool_bar_highlight (f, x, y)
7613 struct frame *f;
7614 int x, y;
7615 {
7616 Lisp_Object window = f->tool_bar_window;
7617 struct window *w = XWINDOW (window);
7618 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7619 int hpos, vpos;
7620 struct glyph *glyph;
7621 struct glyph_row *row;
7622 int i;
7623 Lisp_Object enabled_p;
7624 int prop_idx;
7625 enum draw_glyphs_face draw;
7626 int mouse_down_p, rc;
7627
7628 /* Function note_mouse_highlight is called with negative x(y
7629 values when mouse moves outside of the frame. */
7630 if (x <= 0 || y <= 0)
7631 {
7632 clear_mouse_face (dpyinfo);
7633 return;
7634 }
7635
7636 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7637 if (rc < 0)
7638 {
7639 /* Not on tool-bar item. */
7640 clear_mouse_face (dpyinfo);
7641 return;
7642 }
7643 else if (rc == 0)
7644 goto set_help_echo;
7645
7646 clear_mouse_face (dpyinfo);
7647
7648 /* Mouse is down, but on different tool-bar item? */
7649 mouse_down_p = (dpyinfo->grabbed
7650 && f == last_mouse_frame
7651 && FRAME_LIVE_P (f));
7652 if (mouse_down_p
7653 && last_tool_bar_item != prop_idx)
7654 return;
7655
7656 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7657 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7658
7659 /* If tool-bar item is not enabled, don't highlight it. */
7660 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7661 if (!NILP (enabled_p))
7662 {
7663 /* Compute the x-position of the glyph. In front and past the
7664 image is a space. We include this is the highlighted area. */
7665 row = MATRIX_ROW (w->current_matrix, vpos);
7666 for (i = x = 0; i < hpos; ++i)
7667 x += row->glyphs[TEXT_AREA][i].pixel_width;
7668
7669 /* Record this as the current active region. */
7670 dpyinfo->mouse_face_beg_col = hpos;
7671 dpyinfo->mouse_face_beg_row = vpos;
7672 dpyinfo->mouse_face_beg_x = x;
7673 dpyinfo->mouse_face_beg_y = row->y;
7674 dpyinfo->mouse_face_past_end = 0;
7675
7676 dpyinfo->mouse_face_end_col = hpos + 1;
7677 dpyinfo->mouse_face_end_row = vpos;
7678 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7679 dpyinfo->mouse_face_end_y = row->y;
7680 dpyinfo->mouse_face_window = window;
7681 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7682
7683 /* Display it as active. */
7684 show_mouse_face (dpyinfo, draw);
7685 dpyinfo->mouse_face_image_state = draw;
7686 }
7687
7688 set_help_echo:
7689
7690 /* Set help_echo to a help string to display for this tool-bar item.
7691 XTread_socket does the rest. */
7692 help_echo_object = help_echo_window = Qnil;
7693 help_echo_pos = -1;
7694 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
7695 if (NILP (help_echo))
7696 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
7697 }
7698
7699
7700 \f
7701 /* Find the glyph matrix position of buffer position CHARPOS in window
7702 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7703 current glyphs must be up to date. If CHARPOS is above window
7704 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
7705 of last line in W. In the row containing CHARPOS, stop before glyphs
7706 having STOP as object. */
7707
7708 #if 0 /* This is a version of fast_find_position that's more correct
7709 in the presence of hscrolling, for example. I didn't install
7710 it right away because the problem fixed is minor, it failed
7711 in 20.x as well, and I think it's too risky to install
7712 so near the release of 21.1. 2001-09-25 gerd. */
7713
7714 static int
7715 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
7716 struct window *w;
7717 int charpos;
7718 int *hpos, *vpos, *x, *y;
7719 Lisp_Object stop;
7720 {
7721 struct glyph_row *row, *first;
7722 struct glyph *glyph, *end;
7723 int i, past_end = 0;
7724
7725 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7726 row = row_containing_pos (w, charpos, first, NULL, 0);
7727 if (row == NULL)
7728 {
7729 if (charpos < MATRIX_ROW_START_CHARPOS (first))
7730 {
7731 *x = *y = *hpos = *vpos = 0;
7732 return 0;
7733 }
7734 else
7735 {
7736 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
7737 past_end = 1;
7738 }
7739 }
7740
7741 *x = row->x;
7742 *y = row->y;
7743 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7744
7745 glyph = row->glyphs[TEXT_AREA];
7746 end = glyph + row->used[TEXT_AREA];
7747
7748 /* Skip over glyphs not having an object at the start of the row.
7749 These are special glyphs like truncation marks on terminal
7750 frames. */
7751 if (row->displays_text_p)
7752 while (glyph < end
7753 && INTEGERP (glyph->object)
7754 && !EQ (stop, glyph->object)
7755 && glyph->charpos < 0)
7756 {
7757 *x += glyph->pixel_width;
7758 ++glyph;
7759 }
7760
7761 while (glyph < end
7762 && !INTEGERP (glyph->object)
7763 && !EQ (stop, glyph->object)
7764 && (!BUFFERP (glyph->object)
7765 || glyph->charpos < charpos))
7766 {
7767 *x += glyph->pixel_width;
7768 ++glyph;
7769 }
7770
7771 *hpos = glyph - row->glyphs[TEXT_AREA];
7772 return past_end;
7773 }
7774
7775 #else /* not 0 */
7776
7777 static int
7778 fast_find_position (w, pos, hpos, vpos, x, y, stop)
7779 struct window *w;
7780 int pos;
7781 int *hpos, *vpos, *x, *y;
7782 Lisp_Object stop;
7783 {
7784 int i;
7785 int lastcol;
7786 int maybe_next_line_p = 0;
7787 int line_start_position;
7788 int yb = window_text_bottom_y (w);
7789 struct glyph_row *row, *best_row;
7790 int row_vpos, best_row_vpos;
7791 int current_x;
7792
7793 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7794 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7795
7796 while (row->y < yb)
7797 {
7798 if (row->used[TEXT_AREA])
7799 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7800 else
7801 line_start_position = 0;
7802
7803 if (line_start_position > pos)
7804 break;
7805 /* If the position sought is the end of the buffer,
7806 don't include the blank lines at the bottom of the window. */
7807 else if (line_start_position == pos
7808 && pos == BUF_ZV (XBUFFER (w->buffer)))
7809 {
7810 maybe_next_line_p = 1;
7811 break;
7812 }
7813 else if (line_start_position > 0)
7814 {
7815 best_row = row;
7816 best_row_vpos = row_vpos;
7817 }
7818
7819 if (row->y + row->height >= yb)
7820 break;
7821
7822 ++row;
7823 ++row_vpos;
7824 }
7825
7826 /* Find the right column within BEST_ROW. */
7827 lastcol = 0;
7828 current_x = best_row->x;
7829 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
7830 {
7831 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7832 int charpos = glyph->charpos;
7833
7834 if (BUFFERP (glyph->object))
7835 {
7836 if (charpos == pos)
7837 {
7838 *hpos = i;
7839 *vpos = best_row_vpos;
7840 *x = current_x;
7841 *y = best_row->y;
7842 return 1;
7843 }
7844 else if (charpos > pos)
7845 break;
7846 }
7847 else if (EQ (glyph->object, stop))
7848 break;
7849
7850 if (charpos > 0)
7851 lastcol = i;
7852 current_x += glyph->pixel_width;
7853 }
7854
7855 /* If we're looking for the end of the buffer,
7856 and we didn't find it in the line we scanned,
7857 use the start of the following line. */
7858 if (maybe_next_line_p)
7859 {
7860 ++best_row;
7861 ++best_row_vpos;
7862 lastcol = 0;
7863 current_x = best_row->x;
7864 }
7865
7866 *vpos = best_row_vpos;
7867 *hpos = lastcol + 1;
7868 *x = current_x;
7869 *y = best_row->y;
7870 return 0;
7871 }
7872
7873 #endif /* not 0 */
7874
7875
7876 /* Find the position of the glyph for position POS in OBJECT in
7877 window W's current matrix, and return in *X, *Y the pixel
7878 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
7879
7880 RIGHT_P non-zero means return the position of the right edge of the
7881 glyph, RIGHT_P zero means return the left edge position.
7882
7883 If no glyph for POS exists in the matrix, return the position of
7884 the glyph with the next smaller position that is in the matrix, if
7885 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7886 exists in the matrix, return the position of the glyph with the
7887 next larger position in OBJECT.
7888
7889 Value is non-zero if a glyph was found. */
7890
7891 static int
7892 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7893 struct window *w;
7894 int pos;
7895 Lisp_Object object;
7896 int *hpos, *vpos, *x, *y;
7897 int right_p;
7898 {
7899 int yb = window_text_bottom_y (w);
7900 struct glyph_row *r;
7901 struct glyph *best_glyph = NULL;
7902 struct glyph_row *best_row = NULL;
7903 int best_x = 0;
7904
7905 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7906 r->enabled_p && r->y < yb;
7907 ++r)
7908 {
7909 struct glyph *g = r->glyphs[TEXT_AREA];
7910 struct glyph *e = g + r->used[TEXT_AREA];
7911 int gx;
7912
7913 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7914 if (EQ (g->object, object))
7915 {
7916 if (g->charpos == pos)
7917 {
7918 best_glyph = g;
7919 best_x = gx;
7920 best_row = r;
7921 goto found;
7922 }
7923 else if (best_glyph == NULL
7924 || ((abs (g->charpos - pos)
7925 < abs (best_glyph->charpos - pos))
7926 && (right_p
7927 ? g->charpos < pos
7928 : g->charpos > pos)))
7929 {
7930 best_glyph = g;
7931 best_x = gx;
7932 best_row = r;
7933 }
7934 }
7935 }
7936
7937 found:
7938
7939 if (best_glyph)
7940 {
7941 *x = best_x;
7942 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7943
7944 if (right_p)
7945 {
7946 *x += best_glyph->pixel_width;
7947 ++*hpos;
7948 }
7949
7950 *y = best_row->y;
7951 *vpos = best_row - w->current_matrix->rows;
7952 }
7953
7954 return best_glyph != NULL;
7955 }
7956
7957
7958 /* Display the active region described by mouse_face_*
7959 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7960
7961 static void
7962 show_mouse_face (dpyinfo, draw)
7963 struct x_display_info *dpyinfo;
7964 enum draw_glyphs_face draw;
7965 {
7966 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7967 struct frame *f = XFRAME (WINDOW_FRAME (w));
7968
7969 if (/* If window is in the process of being destroyed, don't bother
7970 to do anything. */
7971 w->current_matrix != NULL
7972 /* Don't update mouse highlight if hidden */
7973 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
7974 /* Recognize when we are called to operate on rows that don't exist
7975 anymore. This can happen when a window is split. */
7976 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
7977 {
7978 int phys_cursor_on_p = w->phys_cursor_on_p;
7979 struct glyph_row *row, *first, *last;
7980
7981 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
7982 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
7983
7984 for (row = first; row <= last && row->enabled_p; ++row)
7985 {
7986 int start_hpos, end_hpos, start_x;
7987
7988 /* For all but the first row, the highlight starts at column 0. */
7989 if (row == first)
7990 {
7991 start_hpos = dpyinfo->mouse_face_beg_col;
7992 start_x = dpyinfo->mouse_face_beg_x;
7993 }
7994 else
7995 {
7996 start_hpos = 0;
7997 start_x = 0;
7998 }
7999
8000 if (row == last)
8001 end_hpos = dpyinfo->mouse_face_end_col;
8002 else
8003 end_hpos = row->used[TEXT_AREA];
8004
8005 if (end_hpos > start_hpos)
8006 {
8007 x_draw_glyphs (w, start_x, row, TEXT_AREA,
8008 start_hpos, end_hpos, draw, 0);
8009
8010 row->mouse_face_p
8011 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
8012 }
8013 }
8014
8015 /* When we've written over the cursor, arrange for it to
8016 be displayed again. */
8017 if (phys_cursor_on_p && !w->phys_cursor_on_p)
8018 x_display_cursor (w, 1,
8019 w->phys_cursor.hpos, w->phys_cursor.vpos,
8020 w->phys_cursor.x, w->phys_cursor.y);
8021 }
8022
8023 /* Change the mouse cursor. */
8024 if (draw == DRAW_NORMAL_TEXT)
8025 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8026 f->output_data.x->text_cursor);
8027 else if (draw == DRAW_MOUSE_FACE)
8028 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8029 f->output_data.x->cross_cursor);
8030 else
8031 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8032 f->output_data.x->nontext_cursor);
8033 }
8034
8035 /* Clear out the mouse-highlighted active region.
8036 Redraw it un-highlighted first. Value is non-zero if mouse
8037 face was actually drawn unhighlighted. */
8038
8039 static int
8040 clear_mouse_face (dpyinfo)
8041 struct x_display_info *dpyinfo;
8042 {
8043 int cleared = 0;
8044
8045 if (!NILP (dpyinfo->mouse_face_window))
8046 {
8047 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
8048 cleared = 1;
8049 }
8050
8051 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
8052 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
8053 dpyinfo->mouse_face_window = Qnil;
8054 dpyinfo->mouse_face_overlay = Qnil;
8055 return cleared;
8056 }
8057
8058
8059 /* Clear any mouse-face on window W. This function is part of the
8060 redisplay interface, and is called from try_window_id and similar
8061 functions to ensure the mouse-highlight is off. */
8062
8063 static void
8064 x_clear_mouse_face (w)
8065 struct window *w;
8066 {
8067 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
8068 Lisp_Object window;
8069
8070 BLOCK_INPUT;
8071 XSETWINDOW (window, w);
8072 if (EQ (window, dpyinfo->mouse_face_window))
8073 clear_mouse_face (dpyinfo);
8074 UNBLOCK_INPUT;
8075 }
8076
8077
8078 /* Just discard the mouse face information for frame F, if any.
8079 This is used when the size of F is changed. */
8080
8081 void
8082 cancel_mouse_face (f)
8083 FRAME_PTR f;
8084 {
8085 Lisp_Object window;
8086 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8087
8088 window = dpyinfo->mouse_face_window;
8089 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
8090 {
8091 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
8092 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
8093 dpyinfo->mouse_face_window = Qnil;
8094 }
8095 }
8096
8097 \f
8098 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
8099
8100
8101 /* Try to determine frame pixel position and size of the glyph under
8102 frame pixel coordinates X/Y on frame F . Return the position and
8103 size in *RECT. Value is non-zero if we could compute these
8104 values. */
8105
8106 static int
8107 glyph_rect (f, x, y, rect)
8108 struct frame *f;
8109 int x, y;
8110 XRectangle *rect;
8111 {
8112 Lisp_Object window;
8113 int part, found = 0;
8114
8115 window = window_from_coordinates (f, x, y, &part, 0);
8116 if (!NILP (window))
8117 {
8118 struct window *w = XWINDOW (window);
8119 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
8120 struct glyph_row *end = r + w->current_matrix->nrows - 1;
8121
8122 frame_to_window_pixel_xy (w, &x, &y);
8123
8124 for (; !found && r < end && r->enabled_p; ++r)
8125 if (r->y >= y)
8126 {
8127 struct glyph *g = r->glyphs[TEXT_AREA];
8128 struct glyph *end = g + r->used[TEXT_AREA];
8129 int gx;
8130
8131 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
8132 if (gx >= x)
8133 {
8134 rect->width = g->pixel_width;
8135 rect->height = r->height;
8136 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
8137 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
8138 found = 1;
8139 }
8140 }
8141 }
8142
8143 return found;
8144 }
8145
8146
8147 /* Return the current position of the mouse.
8148 *FP should be a frame which indicates which display to ask about.
8149
8150 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
8151 and *PART to the frame, window, and scroll bar part that the mouse
8152 is over. Set *X and *Y to the portion and whole of the mouse's
8153 position on the scroll bar.
8154
8155 If the mouse movement started elsewhere, set *FP to the frame the
8156 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
8157 the mouse is over.
8158
8159 Set *TIME to the server time-stamp for the time at which the mouse
8160 was at this position.
8161
8162 Don't store anything if we don't have a valid set of values to report.
8163
8164 This clears the mouse_moved flag, so we can wait for the next mouse
8165 movement. */
8166
8167 static void
8168 XTmouse_position (fp, insist, bar_window, part, x, y, time)
8169 FRAME_PTR *fp;
8170 int insist;
8171 Lisp_Object *bar_window;
8172 enum scroll_bar_part *part;
8173 Lisp_Object *x, *y;
8174 unsigned long *time;
8175 {
8176 FRAME_PTR f1;
8177
8178 BLOCK_INPUT;
8179
8180 if (! NILP (last_mouse_scroll_bar) && insist == 0)
8181 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
8182 else
8183 {
8184 Window root;
8185 int root_x, root_y;
8186
8187 Window dummy_window;
8188 int dummy;
8189
8190 Lisp_Object frame, tail;
8191
8192 /* Clear the mouse-moved flag for every frame on this display. */
8193 FOR_EACH_FRAME (tail, frame)
8194 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
8195 XFRAME (frame)->mouse_moved = 0;
8196
8197 last_mouse_scroll_bar = Qnil;
8198
8199 /* Figure out which root window we're on. */
8200 XQueryPointer (FRAME_X_DISPLAY (*fp),
8201 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
8202
8203 /* The root window which contains the pointer. */
8204 &root,
8205
8206 /* Trash which we can't trust if the pointer is on
8207 a different screen. */
8208 &dummy_window,
8209
8210 /* The position on that root window. */
8211 &root_x, &root_y,
8212
8213 /* More trash we can't trust. */
8214 &dummy, &dummy,
8215
8216 /* Modifier keys and pointer buttons, about which
8217 we don't care. */
8218 (unsigned int *) &dummy);
8219
8220 /* Now we have a position on the root; find the innermost window
8221 containing the pointer. */
8222 {
8223 Window win, child;
8224 int win_x, win_y;
8225 int parent_x = 0, parent_y = 0;
8226 int count;
8227
8228 win = root;
8229
8230 /* XTranslateCoordinates can get errors if the window
8231 structure is changing at the same time this function
8232 is running. So at least we must not crash from them. */
8233
8234 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
8235
8236 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
8237 && FRAME_LIVE_P (last_mouse_frame))
8238 {
8239 /* If mouse was grabbed on a frame, give coords for that frame
8240 even if the mouse is now outside it. */
8241 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
8242
8243 /* From-window, to-window. */
8244 root, FRAME_X_WINDOW (last_mouse_frame),
8245
8246 /* From-position, to-position. */
8247 root_x, root_y, &win_x, &win_y,
8248
8249 /* Child of win. */
8250 &child);
8251 f1 = last_mouse_frame;
8252 }
8253 else
8254 {
8255 while (1)
8256 {
8257 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
8258
8259 /* From-window, to-window. */
8260 root, win,
8261
8262 /* From-position, to-position. */
8263 root_x, root_y, &win_x, &win_y,
8264
8265 /* Child of win. */
8266 &child);
8267
8268 if (child == None || child == win)
8269 break;
8270
8271 win = child;
8272 parent_x = win_x;
8273 parent_y = win_y;
8274 }
8275
8276 /* Now we know that:
8277 win is the innermost window containing the pointer
8278 (XTC says it has no child containing the pointer),
8279 win_x and win_y are the pointer's position in it
8280 (XTC did this the last time through), and
8281 parent_x and parent_y are the pointer's position in win's parent.
8282 (They are what win_x and win_y were when win was child.
8283 If win is the root window, it has no parent, and
8284 parent_{x,y} are invalid, but that's okay, because we'll
8285 never use them in that case.) */
8286
8287 /* Is win one of our frames? */
8288 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
8289
8290 #ifdef USE_X_TOOLKIT
8291 /* If we end up with the menu bar window, say it's not
8292 on the frame. */
8293 if (f1 != NULL
8294 && f1->output_data.x->menubar_widget
8295 && win == XtWindow (f1->output_data.x->menubar_widget))
8296 f1 = NULL;
8297 #endif /* USE_X_TOOLKIT */
8298 }
8299
8300 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
8301 f1 = 0;
8302
8303 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
8304
8305 /* If not, is it one of our scroll bars? */
8306 if (! f1)
8307 {
8308 struct scroll_bar *bar = x_window_to_scroll_bar (win);
8309
8310 if (bar)
8311 {
8312 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8313 win_x = parent_x;
8314 win_y = parent_y;
8315 }
8316 }
8317
8318 if (f1 == 0 && insist > 0)
8319 f1 = SELECTED_FRAME ();
8320
8321 if (f1)
8322 {
8323 /* Ok, we found a frame. Store all the values.
8324 last_mouse_glyph is a rectangle used to reduce the
8325 generation of mouse events. To not miss any motion
8326 events, we must divide the frame into rectangles of the
8327 size of the smallest character that could be displayed
8328 on it, i.e. into the same rectangles that matrices on
8329 the frame are divided into. */
8330
8331 int width, height, gx, gy;
8332 XRectangle rect;
8333
8334 if (glyph_rect (f1, win_x, win_y, &rect))
8335 last_mouse_glyph = rect;
8336 else
8337 {
8338 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
8339 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
8340 gx = win_x;
8341 gy = win_y;
8342
8343 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
8344 round down even for negative values. */
8345 if (gx < 0)
8346 gx -= width - 1;
8347 if (gy < 0)
8348 gy -= height - 1;
8349 gx = (gx + width - 1) / width * width;
8350 gy = (gy + height - 1) / height * height;
8351
8352 last_mouse_glyph.width = width;
8353 last_mouse_glyph.height = height;
8354 last_mouse_glyph.x = gx;
8355 last_mouse_glyph.y = gy;
8356 }
8357
8358 *bar_window = Qnil;
8359 *part = 0;
8360 *fp = f1;
8361 XSETINT (*x, win_x);
8362 XSETINT (*y, win_y);
8363 *time = last_mouse_movement_time;
8364 }
8365 }
8366 }
8367
8368 UNBLOCK_INPUT;
8369 }
8370
8371
8372 #ifdef USE_X_TOOLKIT
8373
8374 /* Atimer callback function for TIMER. Called every 0.1s to process
8375 Xt timeouts, if needed. We must avoid calling XtAppPending as
8376 much as possible because that function does an implicit XFlush
8377 that slows us down. */
8378
8379 static void
8380 x_process_timeouts (timer)
8381 struct atimer *timer;
8382 {
8383 if (toolkit_scroll_bar_interaction || popup_activated_flag)
8384 {
8385 BLOCK_INPUT;
8386 while (XtAppPending (Xt_app_con) & XtIMTimer)
8387 XtAppProcessEvent (Xt_app_con, XtIMTimer);
8388 UNBLOCK_INPUT;
8389 }
8390 }
8391
8392 #endif /* USE_X_TOOLKIT */
8393
8394 \f
8395 /* Scroll bar support. */
8396
8397 /* Given an X window ID, find the struct scroll_bar which manages it.
8398 This can be called in GC, so we have to make sure to strip off mark
8399 bits. */
8400
8401 static struct scroll_bar *
8402 x_window_to_scroll_bar (window_id)
8403 Window window_id;
8404 {
8405 Lisp_Object tail;
8406
8407 for (tail = Vframe_list;
8408 XGCTYPE (tail) == Lisp_Cons;
8409 tail = XCDR (tail))
8410 {
8411 Lisp_Object frame, bar, condemned;
8412
8413 frame = XCAR (tail);
8414 /* All elements of Vframe_list should be frames. */
8415 if (! GC_FRAMEP (frame))
8416 abort ();
8417
8418 /* Scan this frame's scroll bar list for a scroll bar with the
8419 right window ID. */
8420 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
8421 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
8422 /* This trick allows us to search both the ordinary and
8423 condemned scroll bar lists with one loop. */
8424 ! GC_NILP (bar) || (bar = condemned,
8425 condemned = Qnil,
8426 ! GC_NILP (bar));
8427 bar = XSCROLL_BAR (bar)->next)
8428 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
8429 return XSCROLL_BAR (bar);
8430 }
8431
8432 return 0;
8433 }
8434
8435
8436 #if defined USE_LUCID
8437
8438 /* Return the Lucid menu bar WINDOW is part of. Return null
8439 if WINDOW is not part of a menu bar. */
8440
8441 static Widget
8442 x_window_to_menu_bar (window)
8443 Window window;
8444 {
8445 Lisp_Object tail;
8446
8447 for (tail = Vframe_list;
8448 XGCTYPE (tail) == Lisp_Cons;
8449 tail = XCDR (tail))
8450 {
8451 Lisp_Object frame = XCAR (tail);
8452 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
8453
8454 if (menu_bar && xlwmenu_window_p (menu_bar, window))
8455 return menu_bar;
8456 }
8457
8458 return NULL;
8459 }
8460
8461 #endif /* USE_LUCID */
8462
8463 \f
8464 /************************************************************************
8465 Toolkit scroll bars
8466 ************************************************************************/
8467
8468 #ifdef USE_TOOLKIT_SCROLL_BARS
8469
8470 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
8471 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
8472 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
8473 struct scroll_bar *));
8474 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
8475 int, int, int));
8476
8477
8478 /* Id of action hook installed for scroll bars. */
8479
8480 static XtActionHookId action_hook_id;
8481
8482 /* Lisp window being scrolled. Set when starting to interact with
8483 a toolkit scroll bar, reset to nil when ending the interaction. */
8484
8485 static Lisp_Object window_being_scrolled;
8486
8487 /* Last scroll bar part sent in xm_scroll_callback. */
8488
8489 static int last_scroll_bar_part;
8490
8491 /* Whether this is an Xaw with arrow-scrollbars. This should imply
8492 that movements of 1/20 of the screen size are mapped to up/down. */
8493
8494 static Boolean xaw3d_arrow_scroll;
8495
8496 /* Whether the drag scrolling maintains the mouse at the top of the
8497 thumb. If not, resizing the thumb needs to be done more carefully
8498 to avoid jerkyness. */
8499
8500 static Boolean xaw3d_pick_top;
8501
8502
8503 /* Action hook installed via XtAppAddActionHook when toolkit scroll
8504 bars are used.. The hook is responsible for detecting when
8505 the user ends an interaction with the scroll bar, and generates
8506 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
8507
8508 static void
8509 xt_action_hook (widget, client_data, action_name, event, params,
8510 num_params)
8511 Widget widget;
8512 XtPointer client_data;
8513 String action_name;
8514 XEvent *event;
8515 String *params;
8516 Cardinal *num_params;
8517 {
8518 int scroll_bar_p;
8519 char *end_action;
8520
8521 #ifdef USE_MOTIF
8522 scroll_bar_p = XmIsScrollBar (widget);
8523 end_action = "Release";
8524 #else /* !USE_MOTIF i.e. use Xaw */
8525 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
8526 end_action = "EndScroll";
8527 #endif /* USE_MOTIF */
8528
8529 if (scroll_bar_p
8530 && strcmp (action_name, end_action) == 0
8531 && WINDOWP (window_being_scrolled))
8532 {
8533 struct window *w;
8534
8535 x_send_scroll_bar_event (window_being_scrolled,
8536 scroll_bar_end_scroll, 0, 0);
8537 w = XWINDOW (window_being_scrolled);
8538 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
8539 window_being_scrolled = Qnil;
8540 last_scroll_bar_part = -1;
8541
8542 /* Xt timeouts no longer needed. */
8543 toolkit_scroll_bar_interaction = 0;
8544 }
8545 }
8546
8547 /* A vector of windows used for communication between
8548 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
8549
8550 static struct window **scroll_bar_windows;
8551 static int scroll_bar_windows_size;
8552
8553
8554 /* Send a client message with message type Xatom_Scrollbar for a
8555 scroll action to the frame of WINDOW. PART is a value identifying
8556 the part of the scroll bar that was clicked on. PORTION is the
8557 amount to scroll of a whole of WHOLE. */
8558
8559 static void
8560 x_send_scroll_bar_event (window, part, portion, whole)
8561 Lisp_Object window;
8562 int part, portion, whole;
8563 {
8564 XEvent event;
8565 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
8566 struct window *w = XWINDOW (window);
8567 struct frame *f = XFRAME (w->frame);
8568 int i;
8569
8570 BLOCK_INPUT;
8571
8572 /* Construct a ClientMessage event to send to the frame. */
8573 ev->type = ClientMessage;
8574 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
8575 ev->display = FRAME_X_DISPLAY (f);
8576 ev->window = FRAME_X_WINDOW (f);
8577 ev->format = 32;
8578
8579 /* We can only transfer 32 bits in the XClientMessageEvent, which is
8580 not enough to store a pointer or Lisp_Object on a 64 bit system.
8581 So, store the window in scroll_bar_windows and pass the index
8582 into that array in the event. */
8583 for (i = 0; i < scroll_bar_windows_size; ++i)
8584 if (scroll_bar_windows[i] == NULL)
8585 break;
8586
8587 if (i == scroll_bar_windows_size)
8588 {
8589 int new_size = max (10, 2 * scroll_bar_windows_size);
8590 size_t nbytes = new_size * sizeof *scroll_bar_windows;
8591 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
8592
8593 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
8594 nbytes);
8595 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
8596 scroll_bar_windows_size = new_size;
8597 }
8598
8599 scroll_bar_windows[i] = w;
8600 ev->data.l[0] = (long) i;
8601 ev->data.l[1] = (long) part;
8602 ev->data.l[2] = (long) 0;
8603 ev->data.l[3] = (long) portion;
8604 ev->data.l[4] = (long) whole;
8605
8606 /* Make Xt timeouts work while the scroll bar is active. */
8607 toolkit_scroll_bar_interaction = 1;
8608
8609 /* Setting the event mask to zero means that the message will
8610 be sent to the client that created the window, and if that
8611 window no longer exists, no event will be sent. */
8612 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
8613 UNBLOCK_INPUT;
8614 }
8615
8616
8617 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
8618 in *IEVENT. */
8619
8620 static void
8621 x_scroll_bar_to_input_event (event, ievent)
8622 XEvent *event;
8623 struct input_event *ievent;
8624 {
8625 XClientMessageEvent *ev = (XClientMessageEvent *) event;
8626 Lisp_Object window;
8627 struct frame *f;
8628 struct window *w;
8629
8630 w = scroll_bar_windows[ev->data.l[0]];
8631 scroll_bar_windows[ev->data.l[0]] = NULL;
8632
8633 XSETWINDOW (window, w);
8634 f = XFRAME (w->frame);
8635
8636 ievent->kind = SCROLL_BAR_CLICK_EVENT;
8637 ievent->frame_or_window = window;
8638 ievent->arg = Qnil;
8639 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
8640 ievent->part = ev->data.l[1];
8641 ievent->code = ev->data.l[2];
8642 ievent->x = make_number ((int) ev->data.l[3]);
8643 ievent->y = make_number ((int) ev->data.l[4]);
8644 ievent->modifiers = 0;
8645 }
8646
8647
8648 #ifdef USE_MOTIF
8649
8650 /* Minimum and maximum values used for Motif scroll bars. */
8651
8652 #define XM_SB_MIN 1
8653 #define XM_SB_MAX 10000000
8654 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
8655
8656
8657 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
8658 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
8659 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
8660
8661 static void
8662 xm_scroll_callback (widget, client_data, call_data)
8663 Widget widget;
8664 XtPointer client_data, call_data;
8665 {
8666 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8667 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
8668 int part = -1, whole = 0, portion = 0;
8669
8670 switch (cs->reason)
8671 {
8672 case XmCR_DECREMENT:
8673 bar->dragging = Qnil;
8674 part = scroll_bar_up_arrow;
8675 break;
8676
8677 case XmCR_INCREMENT:
8678 bar->dragging = Qnil;
8679 part = scroll_bar_down_arrow;
8680 break;
8681
8682 case XmCR_PAGE_DECREMENT:
8683 bar->dragging = Qnil;
8684 part = scroll_bar_above_handle;
8685 break;
8686
8687 case XmCR_PAGE_INCREMENT:
8688 bar->dragging = Qnil;
8689 part = scroll_bar_below_handle;
8690 break;
8691
8692 case XmCR_TO_TOP:
8693 bar->dragging = Qnil;
8694 part = scroll_bar_to_top;
8695 break;
8696
8697 case XmCR_TO_BOTTOM:
8698 bar->dragging = Qnil;
8699 part = scroll_bar_to_bottom;
8700 break;
8701
8702 case XmCR_DRAG:
8703 {
8704 int slider_size;
8705 int dragging_down_p = (INTEGERP (bar->dragging)
8706 && XINT (bar->dragging) <= cs->value);
8707
8708 /* Get the slider size. */
8709 BLOCK_INPUT;
8710 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
8711 UNBLOCK_INPUT;
8712
8713 whole = XM_SB_RANGE - slider_size;
8714 portion = min (cs->value - XM_SB_MIN, whole);
8715 part = scroll_bar_handle;
8716 bar->dragging = make_number (cs->value);
8717 }
8718 break;
8719
8720 case XmCR_VALUE_CHANGED:
8721 break;
8722 };
8723
8724 if (part >= 0)
8725 {
8726 window_being_scrolled = bar->window;
8727 last_scroll_bar_part = part;
8728 x_send_scroll_bar_event (bar->window, part, portion, whole);
8729 }
8730 }
8731
8732
8733 #else /* !USE_MOTIF, i.e. Xaw. */
8734
8735
8736 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
8737 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8738 scroll bar struct. CALL_DATA is a pointer to a float saying where
8739 the thumb is. */
8740
8741 static void
8742 xaw_jump_callback (widget, client_data, call_data)
8743 Widget widget;
8744 XtPointer client_data, call_data;
8745 {
8746 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8747 float top = *(float *) call_data;
8748 float shown;
8749 int whole, portion, height;
8750 int part;
8751
8752 /* Get the size of the thumb, a value between 0 and 1. */
8753 BLOCK_INPUT;
8754 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
8755 UNBLOCK_INPUT;
8756
8757 whole = 10000000;
8758 portion = shown < 1 ? top * whole : 0;
8759
8760 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8761 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8762 the bottom, so we force the scrolling whenever we see that we're
8763 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8764 we try to ensure that we always stay two pixels away from the
8765 bottom). */
8766 part = scroll_bar_down_arrow;
8767 else
8768 part = scroll_bar_handle;
8769
8770 window_being_scrolled = bar->window;
8771 bar->dragging = make_number (portion);
8772 last_scroll_bar_part = part;
8773 x_send_scroll_bar_event (bar->window, part, portion, whole);
8774 }
8775
8776
8777 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
8778 i.e. line or page up or down. WIDGET is the Xaw scroll bar
8779 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8780 the scroll bar. CALL_DATA is an integer specifying the action that
8781 has taken place. Its magnitude is in the range 0..height of the
8782 scroll bar. Negative values mean scroll towards buffer start.
8783 Values < height of scroll bar mean line-wise movement. */
8784
8785 static void
8786 xaw_scroll_callback (widget, client_data, call_data)
8787 Widget widget;
8788 XtPointer client_data, call_data;
8789 {
8790 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8791 /* The position really is stored cast to a pointer. */
8792 int position = (long) call_data;
8793 Dimension height;
8794 int part;
8795
8796 /* Get the height of the scroll bar. */
8797 BLOCK_INPUT;
8798 XtVaGetValues (widget, XtNheight, &height, NULL);
8799 UNBLOCK_INPUT;
8800
8801 if (abs (position) >= height)
8802 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8803
8804 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8805 it maps line-movement to call_data = max(5, height/20). */
8806 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8807 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
8808 else
8809 part = scroll_bar_move_ratio;
8810
8811 window_being_scrolled = bar->window;
8812 bar->dragging = Qnil;
8813 last_scroll_bar_part = part;
8814 x_send_scroll_bar_event (bar->window, part, position, height);
8815 }
8816
8817
8818 #endif /* not USE_MOTIF */
8819
8820
8821 /* Create the widget for scroll bar BAR on frame F. Record the widget
8822 and X window of the scroll bar in BAR. */
8823
8824 static void
8825 x_create_toolkit_scroll_bar (f, bar)
8826 struct frame *f;
8827 struct scroll_bar *bar;
8828 {
8829 Window xwindow;
8830 Widget widget;
8831 Arg av[20];
8832 int ac = 0;
8833 char *scroll_bar_name = "verticalScrollBar";
8834 unsigned long pixel;
8835
8836 BLOCK_INPUT;
8837
8838 #ifdef USE_MOTIF
8839 /* Set resources. Create the widget. */
8840 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8841 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8842 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8843 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8844 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8845 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8846 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8847
8848 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8849 if (pixel != -1)
8850 {
8851 XtSetArg (av[ac], XmNforeground, pixel);
8852 ++ac;
8853 }
8854
8855 pixel = f->output_data.x->scroll_bar_background_pixel;
8856 if (pixel != -1)
8857 {
8858 XtSetArg (av[ac], XmNbackground, pixel);
8859 ++ac;
8860 }
8861
8862 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8863 scroll_bar_name, av, ac);
8864
8865 /* Add one callback for everything that can happen. */
8866 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8867 (XtPointer) bar);
8868 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8869 (XtPointer) bar);
8870 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8871 (XtPointer) bar);
8872 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8873 (XtPointer) bar);
8874 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8875 (XtPointer) bar);
8876 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8877 (XtPointer) bar);
8878 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8879 (XtPointer) bar);
8880
8881 /* Realize the widget. Only after that is the X window created. */
8882 XtRealizeWidget (widget);
8883
8884 /* Set the cursor to an arrow. I didn't find a resource to do that.
8885 And I'm wondering why it hasn't an arrow cursor by default. */
8886 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8887 f->output_data.x->nontext_cursor);
8888
8889 #else /* !USE_MOTIF i.e. use Xaw */
8890
8891 /* Set resources. Create the widget. The background of the
8892 Xaw3d scroll bar widget is a little bit light for my taste.
8893 We don't alter it here to let users change it according
8894 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8895 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8896 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
8897 /* For smoother scrolling with Xaw3d -sm */
8898 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8899
8900 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8901 if (pixel != -1)
8902 {
8903 XtSetArg (av[ac], XtNforeground, pixel);
8904 ++ac;
8905 }
8906
8907 pixel = f->output_data.x->scroll_bar_background_pixel;
8908 if (pixel != -1)
8909 {
8910 XtSetArg (av[ac], XtNbackground, pixel);
8911 ++ac;
8912 }
8913
8914 /* Top/bottom shadow colors. */
8915
8916 /* Allocate them, if necessary. */
8917 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
8918 {
8919 pixel = f->output_data.x->scroll_bar_background_pixel;
8920 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8921 &pixel, 1.2, 0x8000))
8922 pixel = -1;
8923 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
8924 }
8925 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8926 {
8927 pixel = f->output_data.x->scroll_bar_background_pixel;
8928 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8929 &pixel, 0.6, 0x4000))
8930 pixel = -1;
8931 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
8932 }
8933
8934 /* Tell the toolkit about them. */
8935 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
8936 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8937 /* We tried to allocate a color for the top/bottom shadow, and
8938 failed, so tell Xaw3d to use dithering instead. */
8939 {
8940 XtSetArg (av[ac], XtNbeNiceToColormap, True);
8941 ++ac;
8942 }
8943 else
8944 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
8945 be more consistent with other emacs 3d colors, and since Xaw3d is
8946 not good at dealing with allocation failure. */
8947 {
8948 /* This tells Xaw3d to use real colors instead of dithering for
8949 the shadows. */
8950 XtSetArg (av[ac], XtNbeNiceToColormap, False);
8951 ++ac;
8952
8953 /* Specify the colors. */
8954 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
8955 if (pixel != -1)
8956 {
8957 XtSetArg (av[ac], "topShadowPixel", pixel);
8958 ++ac;
8959 }
8960 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
8961 if (pixel != -1)
8962 {
8963 XtSetArg (av[ac], "bottomShadowPixel", pixel);
8964 ++ac;
8965 }
8966 }
8967
8968 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8969 f->output_data.x->edit_widget, av, ac);
8970
8971 {
8972 char *initial = "";
8973 char *val = initial;
8974 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8975 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8976 if (val == initial)
8977 { /* ARROW_SCROLL */
8978 xaw3d_arrow_scroll = True;
8979 /* Isn't that just a personal preference ? -sm */
8980 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8981 }
8982 }
8983
8984 /* Define callbacks. */
8985 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8986 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
8987 (XtPointer) bar);
8988
8989 /* Realize the widget. Only after that is the X window created. */
8990 XtRealizeWidget (widget);
8991
8992 #endif /* !USE_MOTIF */
8993
8994 /* Install an action hook that lets us detect when the user
8995 finishes interacting with a scroll bar. */
8996 if (action_hook_id == 0)
8997 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8998
8999 /* Remember X window and widget in the scroll bar vector. */
9000 SET_SCROLL_BAR_X_WIDGET (bar, widget);
9001 xwindow = XtWindow (widget);
9002 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
9003
9004 UNBLOCK_INPUT;
9005 }
9006
9007
9008 /* Set the thumb size and position of scroll bar BAR. We are currently
9009 displaying PORTION out of a whole WHOLE, and our position POSITION. */
9010
9011 static void
9012 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
9013 struct scroll_bar *bar;
9014 int portion, position, whole;
9015 {
9016 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9017 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
9018 float top, shown;
9019
9020 BLOCK_INPUT;
9021
9022 #ifdef USE_MOTIF
9023
9024 /* We use an estimate of 30 chars per line rather than the real
9025 `portion' value. This has the disadvantage that the thumb size
9026 is not very representative, but it makes our life a lot easier.
9027 Otherwise, we have to constantly adjust the thumb size, which
9028 we can't always do quickly enough: while dragging, the size of
9029 the thumb might prevent the user from dragging the thumb all the
9030 way to the end. but Motif and some versions of Xaw3d don't allow
9031 updating the thumb size while dragging. Also, even if we can update
9032 its size, the update will often happen too late.
9033 If you don't believe it, check out revision 1.650 of xterm.c to see
9034 what hoops we were going through and the still poor behavior we got. */
9035 portion = XFASTINT (XWINDOW (bar->window)->height) * 30;
9036 /* When the thumb is at the bottom, position == whole.
9037 So we need to increase `whole' to make space for the thumb. */
9038 whole += portion;
9039
9040 if (whole <= 0)
9041 top = 0, shown = 1;
9042 else
9043 {
9044 top = (float) position / whole;
9045 shown = (float) portion / whole;
9046 }
9047
9048 if (NILP (bar->dragging))
9049 {
9050 int size, value;
9051
9052 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
9053 is the scroll bar's maximum and MIN is the scroll bar's minimum
9054 value. */
9055 size = shown * XM_SB_RANGE;
9056 size = min (size, XM_SB_RANGE);
9057 size = max (size, 1);
9058
9059 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
9060 value = top * XM_SB_RANGE;
9061 value = min (value, XM_SB_MAX - size);
9062 value = max (value, XM_SB_MIN);
9063
9064 XmScrollBarSetValues (widget, value, size, 0, 0, False);
9065 }
9066 #else /* !USE_MOTIF i.e. use Xaw */
9067
9068 if (whole == 0)
9069 top = 0, shown = 1;
9070 else
9071 {
9072 top = (float) position / whole;
9073 shown = (float) portion / whole;
9074 }
9075
9076 {
9077 float old_top, old_shown;
9078 Dimension height;
9079 XtVaGetValues (widget,
9080 XtNtopOfThumb, &old_top,
9081 XtNshown, &old_shown,
9082 XtNheight, &height,
9083 NULL);
9084
9085 /* Massage the top+shown values. */
9086 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
9087 top = max (0, min (1, top));
9088 else
9089 top = old_top;
9090 /* Keep two pixels available for moving the thumb down. */
9091 shown = max (0, min (1 - top - (2.0 / height), shown));
9092
9093 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
9094 check that your system's configuration file contains a define
9095 for `NARROWPROTO'. See s/freebsd.h for an example. */
9096 if (top != old_top || shown != old_shown)
9097 {
9098 if (NILP (bar->dragging))
9099 XawScrollbarSetThumb (widget, top, shown);
9100 else
9101 {
9102 #ifdef HAVE_XAW3D
9103 ScrollbarWidget sb = (ScrollbarWidget) widget;
9104 int scroll_mode = 0;
9105
9106 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
9107 if (xaw3d_arrow_scroll)
9108 {
9109 /* Xaw3d stupidly ignores resize requests while dragging
9110 so we have to make it believe it's not in dragging mode. */
9111 scroll_mode = sb->scrollbar.scroll_mode;
9112 if (scroll_mode == 2)
9113 sb->scrollbar.scroll_mode = 0;
9114 }
9115 #endif
9116 /* Try to make the scrolling a tad smoother. */
9117 if (!xaw3d_pick_top)
9118 shown = min (shown, old_shown);
9119
9120 XawScrollbarSetThumb (widget, top, shown);
9121
9122 #ifdef HAVE_XAW3D
9123 if (xaw3d_arrow_scroll && scroll_mode == 2)
9124 sb->scrollbar.scroll_mode = scroll_mode;
9125 #endif
9126 }
9127 }
9128 }
9129 #endif /* !USE_MOTIF */
9130
9131 UNBLOCK_INPUT;
9132 }
9133
9134 #endif /* USE_TOOLKIT_SCROLL_BARS */
9135
9136
9137 \f
9138 /************************************************************************
9139 Scroll bars, general
9140 ************************************************************************/
9141
9142 /* Create a scroll bar and return the scroll bar vector for it. W is
9143 the Emacs window on which to create the scroll bar. TOP, LEFT,
9144 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
9145 scroll bar. */
9146
9147 static struct scroll_bar *
9148 x_scroll_bar_create (w, top, left, width, height)
9149 struct window *w;
9150 int top, left, width, height;
9151 {
9152 struct frame *f = XFRAME (w->frame);
9153 struct scroll_bar *bar
9154 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
9155
9156 BLOCK_INPUT;
9157
9158 #ifdef USE_TOOLKIT_SCROLL_BARS
9159 x_create_toolkit_scroll_bar (f, bar);
9160 #else /* not USE_TOOLKIT_SCROLL_BARS */
9161 {
9162 XSetWindowAttributes a;
9163 unsigned long mask;
9164 Window window;
9165
9166 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
9167 if (a.background_pixel == -1)
9168 a.background_pixel = f->output_data.x->background_pixel;
9169
9170 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9171 | ButtonMotionMask | PointerMotionHintMask
9172 | ExposureMask);
9173 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
9174
9175 mask = (CWBackPixel | CWEventMask | CWCursor);
9176
9177 /* Clear the area of W that will serve as a scroll bar. This is
9178 for the case that a window has been split horizontally. In
9179 this case, no clear_frame is generated to reduce flickering. */
9180 if (width > 0 && height > 0)
9181 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9182 left, top, width,
9183 window_box_height (w), False);
9184
9185 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9186 /* Position and size of scroll bar. */
9187 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9188 top,
9189 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9190 height,
9191 /* Border width, depth, class, and visual. */
9192 0,
9193 CopyFromParent,
9194 CopyFromParent,
9195 CopyFromParent,
9196 /* Attributes. */
9197 mask, &a);
9198 SET_SCROLL_BAR_X_WINDOW (bar, window);
9199 }
9200 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9201
9202 XSETWINDOW (bar->window, w);
9203 XSETINT (bar->top, top);
9204 XSETINT (bar->left, left);
9205 XSETINT (bar->width, width);
9206 XSETINT (bar->height, height);
9207 XSETINT (bar->start, 0);
9208 XSETINT (bar->end, 0);
9209 bar->dragging = Qnil;
9210
9211 /* Add bar to its frame's list of scroll bars. */
9212 bar->next = FRAME_SCROLL_BARS (f);
9213 bar->prev = Qnil;
9214 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9215 if (!NILP (bar->next))
9216 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9217
9218 /* Map the window/widget. */
9219 #ifdef USE_TOOLKIT_SCROLL_BARS
9220 {
9221 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
9222 XtConfigureWidget (scroll_bar,
9223 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9224 top,
9225 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9226 max (height, 1), 0);
9227 XtMapWidget (scroll_bar);
9228 }
9229 #else /* not USE_TOOLKIT_SCROLL_BARS */
9230 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9231 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9232
9233 UNBLOCK_INPUT;
9234 return bar;
9235 }
9236
9237
9238 /* Draw BAR's handle in the proper position.
9239
9240 If the handle is already drawn from START to END, don't bother
9241 redrawing it, unless REBUILD is non-zero; in that case, always
9242 redraw it. (REBUILD is handy for drawing the handle after expose
9243 events.)
9244
9245 Normally, we want to constrain the start and end of the handle to
9246 fit inside its rectangle, but if the user is dragging the scroll
9247 bar handle, we want to let them drag it down all the way, so that
9248 the bar's top is as far down as it goes; otherwise, there's no way
9249 to move to the very end of the buffer. */
9250
9251 #ifndef USE_TOOLKIT_SCROLL_BARS
9252
9253 static void
9254 x_scroll_bar_set_handle (bar, start, end, rebuild)
9255 struct scroll_bar *bar;
9256 int start, end;
9257 int rebuild;
9258 {
9259 int dragging = ! NILP (bar->dragging);
9260 Window w = SCROLL_BAR_X_WINDOW (bar);
9261 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9262 GC gc = f->output_data.x->normal_gc;
9263
9264 /* If the display is already accurate, do nothing. */
9265 if (! rebuild
9266 && start == XINT (bar->start)
9267 && end == XINT (bar->end))
9268 return;
9269
9270 BLOCK_INPUT;
9271
9272 {
9273 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
9274 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9275 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9276
9277 /* Make sure the values are reasonable, and try to preserve
9278 the distance between start and end. */
9279 {
9280 int length = end - start;
9281
9282 if (start < 0)
9283 start = 0;
9284 else if (start > top_range)
9285 start = top_range;
9286 end = start + length;
9287
9288 if (end < start)
9289 end = start;
9290 else if (end > top_range && ! dragging)
9291 end = top_range;
9292 }
9293
9294 /* Store the adjusted setting in the scroll bar. */
9295 XSETINT (bar->start, start);
9296 XSETINT (bar->end, end);
9297
9298 /* Clip the end position, just for display. */
9299 if (end > top_range)
9300 end = top_range;
9301
9302 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
9303 below top positions, to make sure the handle is always at least
9304 that many pixels tall. */
9305 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
9306
9307 /* Draw the empty space above the handle. Note that we can't clear
9308 zero-height areas; that means "clear to end of window." */
9309 if (0 < start)
9310 x_clear_area (FRAME_X_DISPLAY (f), w,
9311 /* x, y, width, height, and exposures. */
9312 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9313 VERTICAL_SCROLL_BAR_TOP_BORDER,
9314 inside_width, start,
9315 False);
9316
9317 /* Change to proper foreground color if one is specified. */
9318 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9319 XSetForeground (FRAME_X_DISPLAY (f), gc,
9320 f->output_data.x->scroll_bar_foreground_pixel);
9321
9322 /* Draw the handle itself. */
9323 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
9324 /* x, y, width, height */
9325 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9326 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
9327 inside_width, end - start);
9328
9329 /* Restore the foreground color of the GC if we changed it above. */
9330 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9331 XSetForeground (FRAME_X_DISPLAY (f), gc,
9332 f->output_data.x->foreground_pixel);
9333
9334 /* Draw the empty space below the handle. Note that we can't
9335 clear zero-height areas; that means "clear to end of window." */
9336 if (end < inside_height)
9337 x_clear_area (FRAME_X_DISPLAY (f), w,
9338 /* x, y, width, height, and exposures. */
9339 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9340 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
9341 inside_width, inside_height - end,
9342 False);
9343
9344 }
9345
9346 UNBLOCK_INPUT;
9347 }
9348
9349 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9350
9351 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
9352 nil. */
9353
9354 static void
9355 x_scroll_bar_remove (bar)
9356 struct scroll_bar *bar;
9357 {
9358 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9359 BLOCK_INPUT;
9360
9361 #ifdef USE_TOOLKIT_SCROLL_BARS
9362 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
9363 #else
9364 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9365 #endif
9366
9367 /* Disassociate this scroll bar from its window. */
9368 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
9369
9370 UNBLOCK_INPUT;
9371 }
9372
9373
9374 /* Set the handle of the vertical scroll bar for WINDOW to indicate
9375 that we are displaying PORTION characters out of a total of WHOLE
9376 characters, starting at POSITION. If WINDOW has no scroll bar,
9377 create one. */
9378
9379 static void
9380 XTset_vertical_scroll_bar (w, portion, whole, position)
9381 struct window *w;
9382 int portion, whole, position;
9383 {
9384 struct frame *f = XFRAME (w->frame);
9385 struct scroll_bar *bar;
9386 int top, height, left, sb_left, width, sb_width;
9387 int window_x, window_y, window_width, window_height;
9388
9389 /* Get window dimensions. */
9390 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
9391 top = window_y;
9392 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9393 height = window_height;
9394
9395 /* Compute the left edge of the scroll bar area. */
9396 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9397 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
9398 else
9399 left = XFASTINT (w->left);
9400 left *= CANON_X_UNIT (f);
9401 left += FRAME_INTERNAL_BORDER_WIDTH (f);
9402
9403 /* Compute the width of the scroll bar which might be less than
9404 the width of the area reserved for the scroll bar. */
9405 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
9406 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
9407 else
9408 sb_width = width;
9409
9410 /* Compute the left edge of the scroll bar. */
9411 #ifdef USE_TOOLKIT_SCROLL_BARS
9412 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9413 sb_left = left + width - sb_width - (width - sb_width) / 2;
9414 else
9415 sb_left = left + (width - sb_width) / 2;
9416 #else
9417 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9418 sb_left = left + width - sb_width;
9419 else
9420 sb_left = left;
9421 #endif
9422
9423 /* Does the scroll bar exist yet? */
9424 if (NILP (w->vertical_scroll_bar))
9425 {
9426 if (width > 0 && height > 0)
9427 {
9428 BLOCK_INPUT;
9429 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9430 left, top, width, height, False);
9431 UNBLOCK_INPUT;
9432 }
9433
9434 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
9435 }
9436 else
9437 {
9438 /* It may just need to be moved and resized. */
9439 unsigned int mask = 0;
9440
9441 bar = XSCROLL_BAR (w->vertical_scroll_bar);
9442
9443 BLOCK_INPUT;
9444
9445 if (sb_left != XINT (bar->left))
9446 mask |= CWX;
9447 if (top != XINT (bar->top))
9448 mask |= CWY;
9449 if (sb_width != XINT (bar->width))
9450 mask |= CWWidth;
9451 if (height != XINT (bar->height))
9452 mask |= CWHeight;
9453
9454 #ifdef USE_TOOLKIT_SCROLL_BARS
9455
9456 /* Since toolkit scroll bars are smaller than the space reserved
9457 for them on the frame, we have to clear "under" them. */
9458 if (width > 0 && height > 0)
9459 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9460 left, top, width, height, False);
9461
9462 /* Move/size the scroll bar widget. */
9463 if (mask)
9464 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
9465 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9466 top,
9467 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9468 max (height, 1), 0);
9469
9470 #else /* not USE_TOOLKIT_SCROLL_BARS */
9471
9472 /* Clear areas not covered by the scroll bar because of
9473 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
9474 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
9475 {
9476 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9477 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9478 height, False);
9479 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9480 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9481 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9482 height, False);
9483 }
9484
9485 /* Clear areas not covered by the scroll bar because it's not as
9486 wide as the area reserved for it. This makes sure a
9487 previous mode line display is cleared after C-x 2 C-x 1, for
9488 example. */
9489 {
9490 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9491 int rest = area_width - sb_width;
9492 if (rest > 0 && height > 0)
9493 {
9494 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
9495 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9496 left + area_width - rest, top,
9497 rest, height, False);
9498 else
9499 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9500 left, top, rest, height, False);
9501 }
9502 }
9503
9504 /* Move/size the scroll bar window. */
9505 if (mask)
9506 {
9507 XWindowChanges wc;
9508
9509 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9510 wc.y = top;
9511 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
9512 wc.height = height;
9513 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
9514 mask, &wc);
9515 }
9516
9517 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9518
9519 /* Remember new settings. */
9520 XSETINT (bar->left, sb_left);
9521 XSETINT (bar->top, top);
9522 XSETINT (bar->width, sb_width);
9523 XSETINT (bar->height, height);
9524
9525 UNBLOCK_INPUT;
9526 }
9527
9528 #ifdef USE_TOOLKIT_SCROLL_BARS
9529 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
9530 #else /* not USE_TOOLKIT_SCROLL_BARS */
9531 /* Set the scroll bar's current state, unless we're currently being
9532 dragged. */
9533 if (NILP (bar->dragging))
9534 {
9535 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
9536
9537 if (whole == 0)
9538 x_scroll_bar_set_handle (bar, 0, top_range, 0);
9539 else
9540 {
9541 int start = ((double) position * top_range) / whole;
9542 int end = ((double) (position + portion) * top_range) / whole;
9543 x_scroll_bar_set_handle (bar, start, end, 0);
9544 }
9545 }
9546 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9547
9548 XSETVECTOR (w->vertical_scroll_bar, bar);
9549 }
9550
9551
9552 /* The following three hooks are used when we're doing a thorough
9553 redisplay of the frame. We don't explicitly know which scroll bars
9554 are going to be deleted, because keeping track of when windows go
9555 away is a real pain - "Can you say set-window-configuration, boys
9556 and girls?" Instead, we just assert at the beginning of redisplay
9557 that *all* scroll bars are to be removed, and then save a scroll bar
9558 from the fiery pit when we actually redisplay its window. */
9559
9560 /* Arrange for all scroll bars on FRAME to be removed at the next call
9561 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
9562 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
9563
9564 static void
9565 XTcondemn_scroll_bars (frame)
9566 FRAME_PTR frame;
9567 {
9568 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
9569 while (! NILP (FRAME_SCROLL_BARS (frame)))
9570 {
9571 Lisp_Object bar;
9572 bar = FRAME_SCROLL_BARS (frame);
9573 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
9574 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
9575 XSCROLL_BAR (bar)->prev = Qnil;
9576 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
9577 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
9578 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
9579 }
9580 }
9581
9582
9583 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
9584 Note that WINDOW isn't necessarily condemned at all. */
9585
9586 static void
9587 XTredeem_scroll_bar (window)
9588 struct window *window;
9589 {
9590 struct scroll_bar *bar;
9591 struct frame *f;
9592
9593 /* We can't redeem this window's scroll bar if it doesn't have one. */
9594 if (NILP (window->vertical_scroll_bar))
9595 abort ();
9596
9597 bar = XSCROLL_BAR (window->vertical_scroll_bar);
9598
9599 /* Unlink it from the condemned list. */
9600 f = XFRAME (WINDOW_FRAME (window));
9601 if (NILP (bar->prev))
9602 {
9603 /* If the prev pointer is nil, it must be the first in one of
9604 the lists. */
9605 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
9606 /* It's not condemned. Everything's fine. */
9607 return;
9608 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
9609 window->vertical_scroll_bar))
9610 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
9611 else
9612 /* If its prev pointer is nil, it must be at the front of
9613 one or the other! */
9614 abort ();
9615 }
9616 else
9617 XSCROLL_BAR (bar->prev)->next = bar->next;
9618
9619 if (! NILP (bar->next))
9620 XSCROLL_BAR (bar->next)->prev = bar->prev;
9621
9622 bar->next = FRAME_SCROLL_BARS (f);
9623 bar->prev = Qnil;
9624 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9625 if (! NILP (bar->next))
9626 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9627 }
9628
9629 /* Remove all scroll bars on FRAME that haven't been saved since the
9630 last call to `*condemn_scroll_bars_hook'. */
9631
9632 static void
9633 XTjudge_scroll_bars (f)
9634 FRAME_PTR f;
9635 {
9636 Lisp_Object bar, next;
9637
9638 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
9639
9640 /* Clear out the condemned list now so we won't try to process any
9641 more events on the hapless scroll bars. */
9642 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
9643
9644 for (; ! NILP (bar); bar = next)
9645 {
9646 struct scroll_bar *b = XSCROLL_BAR (bar);
9647
9648 x_scroll_bar_remove (b);
9649
9650 next = b->next;
9651 b->next = b->prev = Qnil;
9652 }
9653
9654 /* Now there should be no references to the condemned scroll bars,
9655 and they should get garbage-collected. */
9656 }
9657
9658
9659 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
9660 is a no-op when using toolkit scroll bars.
9661
9662 This may be called from a signal handler, so we have to ignore GC
9663 mark bits. */
9664
9665 static void
9666 x_scroll_bar_expose (bar, event)
9667 struct scroll_bar *bar;
9668 XEvent *event;
9669 {
9670 #ifndef USE_TOOLKIT_SCROLL_BARS
9671
9672 Window w = SCROLL_BAR_X_WINDOW (bar);
9673 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9674 GC gc = f->output_data.x->normal_gc;
9675 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9676
9677 BLOCK_INPUT;
9678
9679 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
9680
9681 /* Draw a one-pixel border just inside the edges of the scroll bar. */
9682 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
9683
9684 /* x, y, width, height */
9685 0, 0,
9686 XINT (bar->width) - 1 - width_trim - width_trim,
9687 XINT (bar->height) - 1);
9688
9689 UNBLOCK_INPUT;
9690
9691 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9692 }
9693
9694 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
9695 is set to something other than NO_EVENT, it is enqueued.
9696
9697 This may be called from a signal handler, so we have to ignore GC
9698 mark bits. */
9699
9700 #ifndef USE_TOOLKIT_SCROLL_BARS
9701
9702 static void
9703 x_scroll_bar_handle_click (bar, event, emacs_event)
9704 struct scroll_bar *bar;
9705 XEvent *event;
9706 struct input_event *emacs_event;
9707 {
9708 if (! GC_WINDOWP (bar->window))
9709 abort ();
9710
9711 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
9712 emacs_event->code = event->xbutton.button - Button1;
9713 emacs_event->modifiers
9714 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
9715 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
9716 event->xbutton.state)
9717 | (event->type == ButtonRelease
9718 ? up_modifier
9719 : down_modifier));
9720 emacs_event->frame_or_window = bar->window;
9721 emacs_event->arg = Qnil;
9722 emacs_event->timestamp = event->xbutton.time;
9723 {
9724 #if 0
9725 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9726 int internal_height
9727 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9728 #endif
9729 int top_range
9730 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9731 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
9732
9733 if (y < 0) y = 0;
9734 if (y > top_range) y = top_range;
9735
9736 if (y < XINT (bar->start))
9737 emacs_event->part = scroll_bar_above_handle;
9738 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9739 emacs_event->part = scroll_bar_handle;
9740 else
9741 emacs_event->part = scroll_bar_below_handle;
9742
9743 /* Just because the user has clicked on the handle doesn't mean
9744 they want to drag it. Lisp code needs to be able to decide
9745 whether or not we're dragging. */
9746 #if 0
9747 /* If the user has just clicked on the handle, record where they're
9748 holding it. */
9749 if (event->type == ButtonPress
9750 && emacs_event->part == scroll_bar_handle)
9751 XSETINT (bar->dragging, y - XINT (bar->start));
9752 #endif
9753
9754 /* If the user has released the handle, set it to its final position. */
9755 if (event->type == ButtonRelease
9756 && ! NILP (bar->dragging))
9757 {
9758 int new_start = y - XINT (bar->dragging);
9759 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9760
9761 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9762 bar->dragging = Qnil;
9763 }
9764
9765 /* Same deal here as the other #if 0. */
9766 #if 0
9767 /* Clicks on the handle are always reported as occurring at the top of
9768 the handle. */
9769 if (emacs_event->part == scroll_bar_handle)
9770 emacs_event->x = bar->start;
9771 else
9772 XSETINT (emacs_event->x, y);
9773 #else
9774 XSETINT (emacs_event->x, y);
9775 #endif
9776
9777 XSETINT (emacs_event->y, top_range);
9778 }
9779 }
9780
9781 /* Handle some mouse motion while someone is dragging the scroll bar.
9782
9783 This may be called from a signal handler, so we have to ignore GC
9784 mark bits. */
9785
9786 static void
9787 x_scroll_bar_note_movement (bar, event)
9788 struct scroll_bar *bar;
9789 XEvent *event;
9790 {
9791 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9792
9793 last_mouse_movement_time = event->xmotion.time;
9794
9795 f->mouse_moved = 1;
9796 XSETVECTOR (last_mouse_scroll_bar, bar);
9797
9798 /* If we're dragging the bar, display it. */
9799 if (! GC_NILP (bar->dragging))
9800 {
9801 /* Where should the handle be now? */
9802 int new_start = event->xmotion.y - XINT (bar->dragging);
9803
9804 if (new_start != XINT (bar->start))
9805 {
9806 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9807
9808 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9809 }
9810 }
9811 }
9812
9813 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9814
9815 /* Return information to the user about the current position of the mouse
9816 on the scroll bar. */
9817
9818 static void
9819 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9820 FRAME_PTR *fp;
9821 Lisp_Object *bar_window;
9822 enum scroll_bar_part *part;
9823 Lisp_Object *x, *y;
9824 unsigned long *time;
9825 {
9826 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
9827 Window w = SCROLL_BAR_X_WINDOW (bar);
9828 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9829 int win_x, win_y;
9830 Window dummy_window;
9831 int dummy_coord;
9832 unsigned int dummy_mask;
9833
9834 BLOCK_INPUT;
9835
9836 /* Get the mouse's position relative to the scroll bar window, and
9837 report that. */
9838 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
9839
9840 /* Root, child, root x and root y. */
9841 &dummy_window, &dummy_window,
9842 &dummy_coord, &dummy_coord,
9843
9844 /* Position relative to scroll bar. */
9845 &win_x, &win_y,
9846
9847 /* Mouse buttons and modifier keys. */
9848 &dummy_mask))
9849 ;
9850 else
9851 {
9852 #if 0
9853 int inside_height
9854 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9855 #endif
9856 int top_range
9857 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9858
9859 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9860
9861 if (! NILP (bar->dragging))
9862 win_y -= XINT (bar->dragging);
9863
9864 if (win_y < 0)
9865 win_y = 0;
9866 if (win_y > top_range)
9867 win_y = top_range;
9868
9869 *fp = f;
9870 *bar_window = bar->window;
9871
9872 if (! NILP (bar->dragging))
9873 *part = scroll_bar_handle;
9874 else if (win_y < XINT (bar->start))
9875 *part = scroll_bar_above_handle;
9876 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9877 *part = scroll_bar_handle;
9878 else
9879 *part = scroll_bar_below_handle;
9880
9881 XSETINT (*x, win_y);
9882 XSETINT (*y, top_range);
9883
9884 f->mouse_moved = 0;
9885 last_mouse_scroll_bar = Qnil;
9886 }
9887
9888 *time = last_mouse_movement_time;
9889
9890 UNBLOCK_INPUT;
9891 }
9892
9893
9894 /* The screen has been cleared so we may have changed foreground or
9895 background colors, and the scroll bars may need to be redrawn.
9896 Clear out the scroll bars, and ask for expose events, so we can
9897 redraw them. */
9898
9899 void
9900 x_scroll_bar_clear (f)
9901 FRAME_PTR f;
9902 {
9903 #ifndef USE_TOOLKIT_SCROLL_BARS
9904 Lisp_Object bar;
9905
9906 /* We can have scroll bars even if this is 0,
9907 if we just turned off scroll bar mode.
9908 But in that case we should not clear them. */
9909 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9910 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9911 bar = XSCROLL_BAR (bar)->next)
9912 XClearArea (FRAME_X_DISPLAY (f),
9913 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9914 0, 0, 0, 0, True);
9915 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9916 }
9917
9918 /* This processes Expose events from the menu-bar specific X event
9919 loop in xmenu.c. This allows to redisplay the frame if necessary
9920 when handling menu-bar or pop-up items. */
9921
9922 int
9923 process_expose_from_menu (event)
9924 XEvent event;
9925 {
9926 FRAME_PTR f;
9927 struct x_display_info *dpyinfo;
9928 int frame_exposed_p = 0;
9929
9930 BLOCK_INPUT;
9931
9932 dpyinfo = x_display_info_for_display (event.xexpose.display);
9933 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9934 if (f)
9935 {
9936 if (f->async_visible == 0)
9937 {
9938 f->async_visible = 1;
9939 f->async_iconified = 0;
9940 f->output_data.x->has_been_visible = 1;
9941 SET_FRAME_GARBAGED (f);
9942 }
9943 else
9944 {
9945 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9946 event.xexpose.x, event.xexpose.y,
9947 event.xexpose.width, event.xexpose.height);
9948 frame_exposed_p = 1;
9949 }
9950 }
9951 else
9952 {
9953 struct scroll_bar *bar
9954 = x_window_to_scroll_bar (event.xexpose.window);
9955
9956 if (bar)
9957 x_scroll_bar_expose (bar, &event);
9958 }
9959
9960 UNBLOCK_INPUT;
9961 return frame_exposed_p;
9962 }
9963 \f
9964 /* Define a queue to save up SelectionRequest events for later handling. */
9965
9966 struct selection_event_queue
9967 {
9968 XEvent event;
9969 struct selection_event_queue *next;
9970 };
9971
9972 static struct selection_event_queue *queue;
9973
9974 /* Nonzero means queue up certain events--don't process them yet. */
9975
9976 static int x_queue_selection_requests;
9977
9978 /* Queue up an X event *EVENT, to be processed later. */
9979
9980 static void
9981 x_queue_event (f, event)
9982 FRAME_PTR f;
9983 XEvent *event;
9984 {
9985 struct selection_event_queue *queue_tmp
9986 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
9987
9988 if (queue_tmp != NULL)
9989 {
9990 queue_tmp->event = *event;
9991 queue_tmp->next = queue;
9992 queue = queue_tmp;
9993 }
9994 }
9995
9996 /* Take all the queued events and put them back
9997 so that they get processed afresh. */
9998
9999 static void
10000 x_unqueue_events (display)
10001 Display *display;
10002 {
10003 while (queue != NULL)
10004 {
10005 struct selection_event_queue *queue_tmp = queue;
10006 XPutBackEvent (display, &queue_tmp->event);
10007 queue = queue_tmp->next;
10008 xfree ((char *)queue_tmp);
10009 }
10010 }
10011
10012 /* Start queuing SelectionRequest events. */
10013
10014 void
10015 x_start_queuing_selection_requests (display)
10016 Display *display;
10017 {
10018 x_queue_selection_requests++;
10019 }
10020
10021 /* Stop queuing SelectionRequest events. */
10022
10023 void
10024 x_stop_queuing_selection_requests (display)
10025 Display *display;
10026 {
10027 x_queue_selection_requests--;
10028 x_unqueue_events (display);
10029 }
10030 \f
10031 /* The main X event-reading loop - XTread_socket. */
10032
10033 #if 0
10034 /* Time stamp of enter window event. This is only used by XTread_socket,
10035 but we have to put it out here, since static variables within functions
10036 sometimes don't work. */
10037
10038 static Time enter_timestamp;
10039 #endif
10040
10041 /* This holds the state XLookupString needs to implement dead keys
10042 and other tricks known as "compose processing". _X Window System_
10043 says that a portable program can't use this, but Stephen Gildea assures
10044 me that letting the compiler initialize it to zeros will work okay.
10045
10046 This must be defined outside of XTread_socket, for the same reasons
10047 given for enter_timestamp, above. */
10048
10049 static XComposeStatus compose_status;
10050
10051 /* Record the last 100 characters stored
10052 to help debug the loss-of-chars-during-GC problem. */
10053
10054 static int temp_index;
10055 static short temp_buffer[100];
10056
10057 /* Set this to nonzero to fake an "X I/O error"
10058 on a particular display. */
10059
10060 struct x_display_info *XTread_socket_fake_io_error;
10061
10062 /* When we find no input here, we occasionally do a no-op command
10063 to verify that the X server is still running and we can still talk with it.
10064 We try all the open displays, one by one.
10065 This variable is used for cycling thru the displays. */
10066
10067 static struct x_display_info *next_noop_dpyinfo;
10068
10069 #define SET_SAVED_MENU_EVENT(size) \
10070 do \
10071 { \
10072 if (f->output_data.x->saved_menu_event == 0) \
10073 f->output_data.x->saved_menu_event \
10074 = (XEvent *) xmalloc (sizeof (XEvent)); \
10075 bcopy (&event, f->output_data.x->saved_menu_event, size); \
10076 if (numchars >= 1) \
10077 { \
10078 bufp->kind = MENU_BAR_ACTIVATE_EVENT; \
10079 XSETFRAME (bufp->frame_or_window, f); \
10080 bufp->arg = Qnil; \
10081 bufp++; \
10082 count++; \
10083 numchars--; \
10084 } \
10085 } \
10086 while (0)
10087
10088 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
10089 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
10090
10091 /* Read events coming from the X server.
10092 This routine is called by the SIGIO handler.
10093 We return as soon as there are no more events to be read.
10094
10095 Events representing keys are stored in buffer BUFP,
10096 which can hold up to NUMCHARS characters.
10097 We return the number of characters stored into the buffer,
10098 thus pretending to be `read'.
10099
10100 EXPECTED is nonzero if the caller knows input is available. */
10101
10102 static int
10103 XTread_socket (sd, bufp, numchars, expected)
10104 register int sd;
10105 /* register */ struct input_event *bufp;
10106 /* register */ int numchars;
10107 int expected;
10108 {
10109 int count = 0;
10110 int nbytes = 0;
10111 XEvent event;
10112 struct frame *f;
10113 int event_found = 0;
10114 struct x_display_info *dpyinfo;
10115 struct coding_system coding;
10116
10117 if (interrupt_input_blocked)
10118 {
10119 interrupt_input_pending = 1;
10120 return -1;
10121 }
10122
10123 interrupt_input_pending = 0;
10124 BLOCK_INPUT;
10125
10126 /* So people can tell when we have read the available input. */
10127 input_signal_count++;
10128
10129 if (numchars <= 0)
10130 abort (); /* Don't think this happens. */
10131
10132 ++handling_signal;
10133
10134 /* Find the display we are supposed to read input for.
10135 It's the one communicating on descriptor SD. */
10136 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
10137 {
10138 #if 0 /* This ought to be unnecessary; let's verify it. */
10139 #ifdef FIOSNBIO
10140 /* If available, Xlib uses FIOSNBIO to make the socket
10141 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
10142 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
10143 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
10144 fcntl (dpyinfo->connection, F_SETFL, 0);
10145 #endif /* ! defined (FIOSNBIO) */
10146 #endif
10147
10148 #if 0 /* This code can't be made to work, with multiple displays,
10149 and appears not to be used on any system any more.
10150 Also keyboard.c doesn't turn O_NDELAY on and off
10151 for X connections. */
10152 #ifndef SIGIO
10153 #ifndef HAVE_SELECT
10154 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
10155 {
10156 extern int read_alarm_should_throw;
10157 read_alarm_should_throw = 1;
10158 XPeekEvent (dpyinfo->display, &event);
10159 read_alarm_should_throw = 0;
10160 }
10161 #endif /* HAVE_SELECT */
10162 #endif /* SIGIO */
10163 #endif
10164
10165 /* For debugging, this gives a way to fake an I/O error. */
10166 if (dpyinfo == XTread_socket_fake_io_error)
10167 {
10168 XTread_socket_fake_io_error = 0;
10169 x_io_error_quitter (dpyinfo->display);
10170 }
10171
10172 #ifdef HAVE_X_SM
10173 BLOCK_INPUT;
10174 count += x_session_check_input (bufp, &numchars);
10175 UNBLOCK_INPUT;
10176 #endif
10177
10178 while (XPending (dpyinfo->display))
10179 {
10180 XNextEvent (dpyinfo->display, &event);
10181
10182 #ifdef HAVE_X_I18N
10183 {
10184 /* Filter events for the current X input method.
10185 XFilterEvent returns non-zero if the input method has
10186 consumed the event. We pass the frame's X window to
10187 XFilterEvent because that's the one for which the IC
10188 was created. */
10189 struct frame *f1 = x_any_window_to_frame (dpyinfo,
10190 event.xclient.window);
10191 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
10192 break;
10193 }
10194 #endif
10195 event_found = 1;
10196
10197 switch (event.type)
10198 {
10199 case ClientMessage:
10200 {
10201 if (event.xclient.message_type
10202 == dpyinfo->Xatom_wm_protocols
10203 && event.xclient.format == 32)
10204 {
10205 if (event.xclient.data.l[0]
10206 == dpyinfo->Xatom_wm_take_focus)
10207 {
10208 /* Use x_any_window_to_frame because this
10209 could be the shell widget window
10210 if the frame has no title bar. */
10211 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
10212 #ifdef HAVE_X_I18N
10213 /* Not quite sure this is needed -pd */
10214 if (f && FRAME_XIC (f))
10215 XSetICFocus (FRAME_XIC (f));
10216 #endif
10217 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
10218 instructs the WM to set the input focus automatically for
10219 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
10220 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
10221 it has set the focus. So, XSetInputFocus below is not
10222 needed.
10223
10224 The call to XSetInputFocus below has also caused trouble. In
10225 cases where the XSetInputFocus done by the WM and the one
10226 below are temporally close (on a fast machine), the call
10227 below can generate additional FocusIn events which confuse
10228 Emacs. */
10229
10230 /* Since we set WM_TAKE_FOCUS, we must call
10231 XSetInputFocus explicitly. But not if f is null,
10232 since that might be an event for a deleted frame. */
10233 if (f)
10234 {
10235 Display *d = event.xclient.display;
10236 /* Catch and ignore errors, in case window has been
10237 iconified by a window manager such as GWM. */
10238 int count = x_catch_errors (d);
10239 XSetInputFocus (d, event.xclient.window,
10240 /* The ICCCM says this is
10241 the only valid choice. */
10242 RevertToParent,
10243 event.xclient.data.l[1]);
10244 /* This is needed to detect the error
10245 if there is an error. */
10246 XSync (d, False);
10247 x_uncatch_errors (d, count);
10248 }
10249 /* Not certain about handling scroll bars here */
10250 #endif /* 0 */
10251 }
10252 else if (event.xclient.data.l[0]
10253 == dpyinfo->Xatom_wm_save_yourself)
10254 {
10255 /* Save state modify the WM_COMMAND property to
10256 something which can reinstate us. This notifies
10257 the session manager, who's looking for such a
10258 PropertyNotify. Can restart processing when
10259 a keyboard or mouse event arrives. */
10260 /* If we have a session manager, don't set this.
10261 KDE will then start two Emacsen, one for the
10262 session manager and one for this. */
10263 if (numchars > 0
10264 #ifdef HAVE_X_SM
10265 && ! x_session_have_connection ()
10266 #endif
10267 )
10268 {
10269 f = x_top_window_to_frame (dpyinfo,
10270 event.xclient.window);
10271 /* This is just so we only give real data once
10272 for a single Emacs process. */
10273 if (f == SELECTED_FRAME ())
10274 XSetCommand (FRAME_X_DISPLAY (f),
10275 event.xclient.window,
10276 initial_argv, initial_argc);
10277 else if (f)
10278 XSetCommand (FRAME_X_DISPLAY (f),
10279 event.xclient.window,
10280 0, 0);
10281 }
10282 }
10283 else if (event.xclient.data.l[0]
10284 == dpyinfo->Xatom_wm_delete_window)
10285 {
10286 struct frame *f
10287 = x_any_window_to_frame (dpyinfo,
10288 event.xclient.window);
10289
10290 if (f)
10291 {
10292 if (numchars == 0)
10293 abort ();
10294
10295 bufp->kind = DELETE_WINDOW_EVENT;
10296 XSETFRAME (bufp->frame_or_window, f);
10297 bufp->arg = Qnil;
10298 bufp++;
10299
10300 count += 1;
10301 numchars -= 1;
10302 }
10303 }
10304 }
10305 else if (event.xclient.message_type
10306 == dpyinfo->Xatom_wm_configure_denied)
10307 {
10308 }
10309 else if (event.xclient.message_type
10310 == dpyinfo->Xatom_wm_window_moved)
10311 {
10312 int new_x, new_y;
10313 struct frame *f
10314 = x_window_to_frame (dpyinfo, event.xclient.window);
10315
10316 new_x = event.xclient.data.s[0];
10317 new_y = event.xclient.data.s[1];
10318
10319 if (f)
10320 {
10321 f->output_data.x->left_pos = new_x;
10322 f->output_data.x->top_pos = new_y;
10323 }
10324 }
10325 #ifdef HACK_EDITRES
10326 else if (event.xclient.message_type
10327 == dpyinfo->Xatom_editres)
10328 {
10329 struct frame *f
10330 = x_any_window_to_frame (dpyinfo, event.xclient.window);
10331 _XEditResCheckMessages (f->output_data.x->widget, NULL,
10332 &event, NULL);
10333 }
10334 #endif /* HACK_EDITRES */
10335 else if ((event.xclient.message_type
10336 == dpyinfo->Xatom_DONE)
10337 || (event.xclient.message_type
10338 == dpyinfo->Xatom_PAGE))
10339 {
10340 /* Ghostview job completed. Kill it. We could
10341 reply with "Next" if we received "Page", but we
10342 currently never do because we are interested in
10343 images, only, which should have 1 page. */
10344 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
10345 struct frame *f
10346 = x_window_to_frame (dpyinfo, event.xclient.window);
10347 x_kill_gs_process (pixmap, f);
10348 expose_frame (f, 0, 0, 0, 0);
10349 }
10350 #ifdef USE_TOOLKIT_SCROLL_BARS
10351 /* Scroll bar callbacks send a ClientMessage from which
10352 we construct an input_event. */
10353 else if (event.xclient.message_type
10354 == dpyinfo->Xatom_Scrollbar)
10355 {
10356 x_scroll_bar_to_input_event (&event, bufp);
10357 ++bufp, ++count, --numchars;
10358 goto out;
10359 }
10360 #endif /* USE_TOOLKIT_SCROLL_BARS */
10361 else
10362 goto OTHER;
10363 }
10364 break;
10365
10366 case SelectionNotify:
10367 #ifdef USE_X_TOOLKIT
10368 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
10369 goto OTHER;
10370 #endif /* not USE_X_TOOLKIT */
10371 x_handle_selection_notify (&event.xselection);
10372 break;
10373
10374 case SelectionClear: /* Someone has grabbed ownership. */
10375 #ifdef USE_X_TOOLKIT
10376 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
10377 goto OTHER;
10378 #endif /* USE_X_TOOLKIT */
10379 {
10380 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
10381
10382 if (numchars == 0)
10383 abort ();
10384
10385 bufp->kind = SELECTION_CLEAR_EVENT;
10386 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10387 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10388 SELECTION_EVENT_TIME (bufp) = eventp->time;
10389 bufp->frame_or_window = Qnil;
10390 bufp->arg = Qnil;
10391 bufp++;
10392
10393 count += 1;
10394 numchars -= 1;
10395 }
10396 break;
10397
10398 case SelectionRequest: /* Someone wants our selection. */
10399 #ifdef USE_X_TOOLKIT
10400 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
10401 goto OTHER;
10402 #endif /* USE_X_TOOLKIT */
10403 if (x_queue_selection_requests)
10404 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
10405 &event);
10406 else
10407 {
10408 XSelectionRequestEvent *eventp
10409 = (XSelectionRequestEvent *) &event;
10410
10411 if (numchars == 0)
10412 abort ();
10413
10414 bufp->kind = SELECTION_REQUEST_EVENT;
10415 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10416 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
10417 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10418 SELECTION_EVENT_TARGET (bufp) = eventp->target;
10419 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
10420 SELECTION_EVENT_TIME (bufp) = eventp->time;
10421 bufp->frame_or_window = Qnil;
10422 bufp->arg = Qnil;
10423 bufp++;
10424
10425 count += 1;
10426 numchars -= 1;
10427 }
10428 break;
10429
10430 case PropertyNotify:
10431 #if 0 /* This is plain wrong. In the case that we are waiting for a
10432 PropertyNotify used as an ACK in incremental selection
10433 transfer, the property will be on the receiver's window. */
10434 #if defined USE_X_TOOLKIT
10435 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
10436 goto OTHER;
10437 #endif
10438 #endif
10439 x_handle_property_notify (&event.xproperty);
10440 goto OTHER;
10441
10442 case ReparentNotify:
10443 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
10444 if (f)
10445 {
10446 int x, y;
10447 f->output_data.x->parent_desc = event.xreparent.parent;
10448 x_real_positions (f, &x, &y);
10449 f->output_data.x->left_pos = x;
10450 f->output_data.x->top_pos = y;
10451 goto OTHER;
10452 }
10453 break;
10454
10455 case Expose:
10456 f = x_window_to_frame (dpyinfo, event.xexpose.window);
10457 if (f)
10458 {
10459 x_check_fullscreen (f);
10460
10461 if (f->async_visible == 0)
10462 {
10463 f->async_visible = 1;
10464 f->async_iconified = 0;
10465 f->output_data.x->has_been_visible = 1;
10466 SET_FRAME_GARBAGED (f);
10467 }
10468 else
10469 expose_frame (x_window_to_frame (dpyinfo,
10470 event.xexpose.window),
10471 event.xexpose.x, event.xexpose.y,
10472 event.xexpose.width, event.xexpose.height);
10473 }
10474 else
10475 {
10476 #ifndef USE_TOOLKIT_SCROLL_BARS
10477 struct scroll_bar *bar;
10478 #endif
10479 #if defined USE_LUCID
10480 /* Submenus of the Lucid menu bar aren't widgets
10481 themselves, so there's no way to dispatch events
10482 to them. Recognize this case separately. */
10483 {
10484 Widget widget
10485 = x_window_to_menu_bar (event.xexpose.window);
10486 if (widget)
10487 xlwmenu_redisplay (widget);
10488 }
10489 #endif /* USE_LUCID */
10490
10491 #ifdef USE_TOOLKIT_SCROLL_BARS
10492 /* Dispatch event to the widget. */
10493 goto OTHER;
10494 #else /* not USE_TOOLKIT_SCROLL_BARS */
10495 bar = x_window_to_scroll_bar (event.xexpose.window);
10496
10497 if (bar)
10498 x_scroll_bar_expose (bar, &event);
10499 #ifdef USE_X_TOOLKIT
10500 else
10501 goto OTHER;
10502 #endif /* USE_X_TOOLKIT */
10503 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10504 }
10505 break;
10506
10507 case GraphicsExpose: /* This occurs when an XCopyArea's
10508 source area was obscured or not
10509 available. */
10510 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
10511 if (f)
10512 {
10513 expose_frame (f,
10514 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
10515 event.xgraphicsexpose.width,
10516 event.xgraphicsexpose.height);
10517 }
10518 #ifdef USE_X_TOOLKIT
10519 else
10520 goto OTHER;
10521 #endif /* USE_X_TOOLKIT */
10522 break;
10523
10524 case NoExpose: /* This occurs when an XCopyArea's
10525 source area was completely
10526 available. */
10527 break;
10528
10529 case UnmapNotify:
10530 /* Redo the mouse-highlight after the tooltip has gone. */
10531 if (event.xmap.window == tip_window)
10532 {
10533 tip_window = 0;
10534 redo_mouse_highlight ();
10535 }
10536
10537 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
10538 if (f) /* F may no longer exist if
10539 the frame was deleted. */
10540 {
10541 /* While a frame is unmapped, display generation is
10542 disabled; you don't want to spend time updating a
10543 display that won't ever be seen. */
10544 f->async_visible = 0;
10545 /* We can't distinguish, from the event, whether the window
10546 has become iconified or invisible. So assume, if it
10547 was previously visible, than now it is iconified.
10548 But x_make_frame_invisible clears both
10549 the visible flag and the iconified flag;
10550 and that way, we know the window is not iconified now. */
10551 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
10552 {
10553 f->async_iconified = 1;
10554
10555 bufp->kind = ICONIFY_EVENT;
10556 XSETFRAME (bufp->frame_or_window, f);
10557 bufp->arg = Qnil;
10558 bufp++;
10559 count++;
10560 numchars--;
10561 }
10562 }
10563 goto OTHER;
10564
10565 case MapNotify:
10566 if (event.xmap.window == tip_window)
10567 /* The tooltip has been drawn already. Avoid
10568 the SET_FRAME_GARBAGED below. */
10569 goto OTHER;
10570
10571 /* We use x_top_window_to_frame because map events can
10572 come for sub-windows and they don't mean that the
10573 frame is visible. */
10574 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
10575 if (f)
10576 {
10577 f->async_visible = 1;
10578 f->async_iconified = 0;
10579 f->output_data.x->has_been_visible = 1;
10580
10581 /* wait_reading_process_input will notice this and update
10582 the frame's display structures. */
10583 SET_FRAME_GARBAGED (f);
10584
10585 if (f->iconified)
10586 {
10587 bufp->kind = DEICONIFY_EVENT;
10588 XSETFRAME (bufp->frame_or_window, f);
10589 bufp->arg = Qnil;
10590 bufp++;
10591 count++;
10592 numchars--;
10593 }
10594 else if (! NILP (Vframe_list)
10595 && ! NILP (XCDR (Vframe_list)))
10596 /* Force a redisplay sooner or later
10597 to update the frame titles
10598 in case this is the second frame. */
10599 record_asynch_buffer_change ();
10600 }
10601 goto OTHER;
10602
10603 case KeyPress:
10604
10605 /* Dispatch KeyPress events when in menu. */
10606 if (popup_activated_flag)
10607 goto OTHER;
10608
10609 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
10610
10611 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
10612 {
10613 dpyinfo->mouse_face_hidden = 1;
10614 clear_mouse_face (dpyinfo);
10615 }
10616
10617 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
10618 if (f == 0)
10619 {
10620 /* Scroll bars consume key events, but we want
10621 the keys to go to the scroll bar's frame. */
10622 Widget widget = XtWindowToWidget (dpyinfo->display,
10623 event.xkey.window);
10624 if (widget && XmIsScrollBar (widget))
10625 {
10626 widget = XtParent (widget);
10627 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
10628 }
10629 }
10630 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
10631
10632 if (f != 0)
10633 {
10634 KeySym keysym, orig_keysym;
10635 /* al%imercury@uunet.uu.net says that making this 81
10636 instead of 80 fixed a bug whereby meta chars made
10637 his Emacs hang.
10638
10639 It seems that some version of XmbLookupString has
10640 a bug of not returning XBufferOverflow in
10641 status_return even if the input is too long to
10642 fit in 81 bytes. So, we must prepare sufficient
10643 bytes for copy_buffer. 513 bytes (256 chars for
10644 two-byte character set) seems to be a fairly good
10645 approximation. -- 2000.8.10 handa@etl.go.jp */
10646 unsigned char copy_buffer[513];
10647 unsigned char *copy_bufptr = copy_buffer;
10648 int copy_bufsiz = sizeof (copy_buffer);
10649 int modifiers;
10650 Lisp_Object coding_system = Qlatin_1;
10651
10652 event.xkey.state
10653 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
10654 extra_keyboard_modifiers);
10655 modifiers = event.xkey.state;
10656
10657 /* This will have to go some day... */
10658
10659 /* make_lispy_event turns chars into control chars.
10660 Don't do it here because XLookupString is too eager. */
10661 event.xkey.state &= ~ControlMask;
10662 event.xkey.state &= ~(dpyinfo->meta_mod_mask
10663 | dpyinfo->super_mod_mask
10664 | dpyinfo->hyper_mod_mask
10665 | dpyinfo->alt_mod_mask);
10666
10667 /* In case Meta is ComposeCharacter,
10668 clear its status. According to Markus Ehrnsperger
10669 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
10670 this enables ComposeCharacter to work whether or
10671 not it is combined with Meta. */
10672 if (modifiers & dpyinfo->meta_mod_mask)
10673 bzero (&compose_status, sizeof (compose_status));
10674
10675 #ifdef HAVE_X_I18N
10676 if (FRAME_XIC (f))
10677 {
10678 Status status_return;
10679
10680 coding_system = Vlocale_coding_system;
10681 nbytes = XmbLookupString (FRAME_XIC (f),
10682 &event.xkey, copy_bufptr,
10683 copy_bufsiz, &keysym,
10684 &status_return);
10685 if (status_return == XBufferOverflow)
10686 {
10687 copy_bufsiz = nbytes + 1;
10688 copy_bufptr = (char *) alloca (copy_bufsiz);
10689 nbytes = XmbLookupString (FRAME_XIC (f),
10690 &event.xkey, copy_bufptr,
10691 copy_bufsiz, &keysym,
10692 &status_return);
10693 }
10694 /* Xutf8LookupString is a new but already deprecated interface. -stef */
10695 #if 0 && defined X_HAVE_UTF8_STRING
10696 else if (status_return == XLookupKeySym)
10697 { /* Try again but with utf-8. */
10698 coding_system = Qutf_8;
10699 nbytes = Xutf8LookupString (FRAME_XIC (f),
10700 &event.xkey, copy_bufptr,
10701 copy_bufsiz, &keysym,
10702 &status_return);
10703 if (status_return == XBufferOverflow)
10704 {
10705 copy_bufsiz = nbytes + 1;
10706 copy_bufptr = (char *) alloca (copy_bufsiz);
10707 nbytes = Xutf8LookupString (FRAME_XIC (f),
10708 &event.xkey,
10709 copy_bufptr,
10710 copy_bufsiz, &keysym,
10711 &status_return);
10712 }
10713 }
10714 #endif
10715
10716 if (status_return == XLookupNone)
10717 break;
10718 else if (status_return == XLookupChars)
10719 {
10720 keysym = NoSymbol;
10721 modifiers = 0;
10722 }
10723 else if (status_return != XLookupKeySym
10724 && status_return != XLookupBoth)
10725 abort ();
10726 }
10727 else
10728 nbytes = XLookupString (&event.xkey, copy_bufptr,
10729 copy_bufsiz, &keysym,
10730 &compose_status);
10731 #else
10732 nbytes = XLookupString (&event.xkey, copy_bufptr,
10733 copy_bufsiz, &keysym,
10734 &compose_status);
10735 #endif
10736
10737 orig_keysym = keysym;
10738
10739 if (numchars > 1)
10740 {
10741 Lisp_Object c;
10742
10743 /* First deal with keysyms which have defined
10744 translations to characters. */
10745 if (keysym >= 32 && keysym < 128)
10746 /* Avoid explicitly decoding each ASCII character. */
10747 {
10748 bufp->kind = ASCII_KEYSTROKE_EVENT;
10749 bufp->code = keysym;
10750 XSETFRAME (bufp->frame_or_window, f);
10751 bufp->arg = Qnil;
10752 bufp->modifiers
10753 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10754 modifiers);
10755 bufp->timestamp = event.xkey.time;
10756 bufp++;
10757 count++;
10758 numchars--;
10759 }
10760 /* Now non-ASCII. */
10761 else if (HASH_TABLE_P (Vx_keysym_table)
10762 && (NATNUMP (c = Fgethash (make_number (keysym),
10763 Vx_keysym_table,
10764 Qnil))))
10765 {
10766 bufp->kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
10767 ? ASCII_KEYSTROKE_EVENT
10768 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
10769 bufp->code = XFASTINT (c);
10770 XSETFRAME (bufp->frame_or_window, f);
10771 bufp->arg = Qnil;
10772 bufp->modifiers
10773 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10774 modifiers);
10775 bufp->timestamp = event.xkey.time;
10776 bufp++;
10777 count++;
10778 numchars--;
10779 }
10780 /* Random non-modifier sorts of keysyms. */
10781 else if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
10782 || keysym == XK_Delete
10783 #ifdef XK_ISO_Left_Tab
10784 || (keysym >= XK_ISO_Left_Tab
10785 && keysym <= XK_ISO_Enter)
10786 #endif
10787 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
10788 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
10789 #ifdef HPUX
10790 /* This recognizes the "extended function
10791 keys". It seems there's no cleaner way.
10792 Test IsModifierKey to avoid handling
10793 mode_switch incorrectly. */
10794 || ((unsigned) (keysym) >= XK_Select
10795 && (unsigned)(keysym) < XK_KP_Space)
10796 #endif
10797 #ifdef XK_dead_circumflex
10798 || orig_keysym == XK_dead_circumflex
10799 #endif
10800 #ifdef XK_dead_grave
10801 || orig_keysym == XK_dead_grave
10802 #endif
10803 #ifdef XK_dead_tilde
10804 || orig_keysym == XK_dead_tilde
10805 #endif
10806 #ifdef XK_dead_diaeresis
10807 || orig_keysym == XK_dead_diaeresis
10808 #endif
10809 #ifdef XK_dead_macron
10810 || orig_keysym == XK_dead_macron
10811 #endif
10812 #ifdef XK_dead_degree
10813 || orig_keysym == XK_dead_degree
10814 #endif
10815 #ifdef XK_dead_acute
10816 || orig_keysym == XK_dead_acute
10817 #endif
10818 #ifdef XK_dead_cedilla
10819 || orig_keysym == XK_dead_cedilla
10820 #endif
10821 #ifdef XK_dead_breve
10822 || orig_keysym == XK_dead_breve
10823 #endif
10824 #ifdef XK_dead_ogonek
10825 || orig_keysym == XK_dead_ogonek
10826 #endif
10827 #ifdef XK_dead_caron
10828 || orig_keysym == XK_dead_caron
10829 #endif
10830 #ifdef XK_dead_doubleacute
10831 || orig_keysym == XK_dead_doubleacute
10832 #endif
10833 #ifdef XK_dead_abovedot
10834 || orig_keysym == XK_dead_abovedot
10835 #endif
10836 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10837 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10838 /* Any "vendor-specific" key is ok. */
10839 || (orig_keysym & (1 << 28))
10840 || (keysym != NoSymbol && nbytes == 0))
10841 && ! (IsModifierKey (orig_keysym)
10842 #ifndef HAVE_X11R5
10843 #ifdef XK_Mode_switch
10844 || ((unsigned)(orig_keysym) == XK_Mode_switch)
10845 #endif
10846 #ifdef XK_Num_Lock
10847 || ((unsigned)(orig_keysym) == XK_Num_Lock)
10848 #endif
10849 #endif /* not HAVE_X11R5 */
10850 /* The symbols from XK_ISO_Lock
10851 to XK_ISO_Last_Group_Lock
10852 don't have real modifiers but
10853 should be treated similarly to
10854 Mode_switch by Emacs. */
10855 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
10856 || ((unsigned)(orig_keysym)
10857 >= XK_ISO_Lock
10858 && (unsigned)(orig_keysym)
10859 <= XK_ISO_Last_Group_Lock)
10860 #endif
10861 ))
10862 {
10863 if (temp_index == sizeof temp_buffer / sizeof (short))
10864 temp_index = 0;
10865 temp_buffer[temp_index++] = keysym;
10866 /* make_lispy_event will convert this to a symbolic
10867 key. */
10868 bufp->kind = NON_ASCII_KEYSTROKE_EVENT;
10869 bufp->code = keysym;
10870 XSETFRAME (bufp->frame_or_window, f);
10871 bufp->arg = Qnil;
10872 bufp->modifiers
10873 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10874 modifiers);
10875 bufp->timestamp = event.xkey.time;
10876 bufp++;
10877 count++;
10878 numchars--;
10879 }
10880 else if (numchars > nbytes)
10881 { /* Raw bytes, not keysym. */
10882 register int i;
10883 register int c;
10884 int nchars, len;
10885
10886 /* The input should be decoded with `coding_system'
10887 which depends on which X*LookupString function
10888 we used just above and the locale. */
10889 setup_coding_system (coding_system, &coding);
10890 coding.src_multibyte = 0;
10891 coding.dst_multibyte = 1;
10892 /* The input is converted to events, thus we can't
10893 handle composition. Anyway, there's no XIM that
10894 gives us composition information. */
10895 coding.composing = COMPOSITION_DISABLED;
10896
10897 for (i = 0; i < nbytes; i++)
10898 {
10899 if (temp_index == (sizeof temp_buffer
10900 / sizeof (short)))
10901 temp_index = 0;
10902 temp_buffer[temp_index++] = copy_bufptr[i];
10903 }
10904
10905 {
10906 /* Decode the input data. */
10907 int require;
10908 unsigned char *p;
10909
10910 require = decoding_buffer_size (&coding, nbytes);
10911 p = (unsigned char *) alloca (require);
10912 coding.mode |= CODING_MODE_LAST_BLOCK;
10913 /* We explicitely disable composition
10914 handling because key data should
10915 not contain any composition
10916 sequence. */
10917 coding.composing = COMPOSITION_DISABLED;
10918 decode_coding (&coding, copy_bufptr, p,
10919 nbytes, require);
10920 nbytes = coding.produced;
10921 nchars = coding.produced_char;
10922 copy_bufptr = p;
10923 }
10924
10925 /* Convert the input data to a sequence of
10926 character events. */
10927 for (i = 0; i < nbytes; i += len)
10928 {
10929 if (nchars == nbytes)
10930 c = copy_bufptr[i], len = 1;
10931 else
10932 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10933 nbytes - i, len);
10934
10935 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
10936 ? ASCII_KEYSTROKE_EVENT
10937 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
10938 bufp->code = c;
10939 XSETFRAME (bufp->frame_or_window, f);
10940 bufp->arg = Qnil;
10941 bufp->modifiers
10942 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10943 modifiers);
10944 bufp->timestamp = event.xkey.time;
10945 bufp++;
10946 }
10947
10948 count += nchars;
10949 numchars -= nchars;
10950
10951 if (keysym == NoSymbol)
10952 break;
10953 }
10954 else
10955 abort ();
10956 }
10957 else
10958 abort ();
10959 }
10960 #ifdef HAVE_X_I18N
10961 /* Don't dispatch this event since XtDispatchEvent calls
10962 XFilterEvent, and two calls in a row may freeze the
10963 client. */
10964 break;
10965 #else
10966 goto OTHER;
10967 #endif
10968
10969 case KeyRelease:
10970 #ifdef HAVE_X_I18N
10971 /* Don't dispatch this event since XtDispatchEvent calls
10972 XFilterEvent, and two calls in a row may freeze the
10973 client. */
10974 break;
10975 #else
10976 goto OTHER;
10977 #endif
10978
10979 case EnterNotify:
10980 {
10981 int n;
10982
10983 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
10984 if (n > 0)
10985 {
10986 bufp += n, count += n, numchars -= n;
10987 }
10988
10989 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10990
10991 #if 0
10992 if (event.xcrossing.focus)
10993 {
10994 /* Avoid nasty pop/raise loops. */
10995 if (f && (!(f->auto_raise)
10996 || !(f->auto_lower)
10997 || (event.xcrossing.time - enter_timestamp) > 500))
10998 {
10999 x_new_focus_frame (dpyinfo, f);
11000 enter_timestamp = event.xcrossing.time;
11001 }
11002 }
11003 else if (f == dpyinfo->x_focus_frame)
11004 x_new_focus_frame (dpyinfo, 0);
11005 #endif
11006
11007 /* EnterNotify counts as mouse movement,
11008 so update things that depend on mouse position. */
11009 if (f && !f->output_data.x->hourglass_p)
11010 note_mouse_movement (f, &event.xmotion);
11011 goto OTHER;
11012 }
11013
11014 case FocusIn:
11015 {
11016 int n;
11017
11018 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
11019 if (n > 0)
11020 {
11021 bufp += n, count += n, numchars -= n;
11022 }
11023 }
11024
11025 goto OTHER;
11026
11027 case LeaveNotify:
11028 {
11029 int n;
11030
11031 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
11032 if (n > 0)
11033 {
11034 bufp += n, count += n, numchars -= n;
11035 }
11036 }
11037
11038 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
11039 if (f)
11040 {
11041 if (f == dpyinfo->mouse_face_mouse_frame)
11042 {
11043 /* If we move outside the frame, then we're
11044 certainly no longer on any text in the frame. */
11045 clear_mouse_face (dpyinfo);
11046 dpyinfo->mouse_face_mouse_frame = 0;
11047 }
11048
11049 /* Generate a nil HELP_EVENT to cancel a help-echo.
11050 Do it only if there's something to cancel.
11051 Otherwise, the startup message is cleared when
11052 the mouse leaves the frame. */
11053 if (any_help_event_p)
11054 {
11055 Lisp_Object frame;
11056 int n;
11057
11058 XSETFRAME (frame, f);
11059 help_echo = Qnil;
11060 n = gen_help_event (bufp, numchars,
11061 Qnil, frame, Qnil, Qnil, 0);
11062 bufp += n, count += n, numchars -= n;
11063 }
11064
11065 }
11066 goto OTHER;
11067
11068 case FocusOut:
11069 {
11070 int n;
11071
11072 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
11073 if (n > 0)
11074 {
11075 bufp += n, count += n, numchars -= n;
11076 }
11077 }
11078
11079 goto OTHER;
11080
11081 case MotionNotify:
11082 {
11083 previous_help_echo = help_echo;
11084 help_echo = help_echo_object = help_echo_window = Qnil;
11085 help_echo_pos = -1;
11086
11087 if (dpyinfo->grabbed && last_mouse_frame
11088 && FRAME_LIVE_P (last_mouse_frame))
11089 f = last_mouse_frame;
11090 else
11091 f = x_window_to_frame (dpyinfo, event.xmotion.window);
11092
11093 if (dpyinfo->mouse_face_hidden)
11094 {
11095 dpyinfo->mouse_face_hidden = 0;
11096 clear_mouse_face (dpyinfo);
11097 }
11098
11099 if (f)
11100 {
11101
11102 /* Generate SELECT_WINDOW_EVENTs when needed. */
11103 if (mouse_autoselect_window)
11104 {
11105 Lisp_Object window;
11106 int area;
11107
11108 window = window_from_coordinates (f,
11109 event.xmotion.x, event.xmotion.y,
11110 &area, 0);
11111
11112 /* Window will be selected only when it is not selected now and
11113 last mouse movement event was not in it. Minibuffer window
11114 will be selected iff it is active. */
11115 if (WINDOWP(window)
11116 && !EQ (window, last_window)
11117 && !EQ (window, selected_window)
11118 && numchars > 0)
11119 {
11120 bufp->kind = SELECT_WINDOW_EVENT;
11121 bufp->frame_or_window = window;
11122 bufp->arg = Qnil;
11123 ++bufp, ++count, --numchars;
11124 }
11125
11126 last_window=window;
11127 }
11128 note_mouse_movement (f, &event.xmotion);
11129 }
11130 else
11131 {
11132 #ifndef USE_TOOLKIT_SCROLL_BARS
11133 struct scroll_bar *bar
11134 = x_window_to_scroll_bar (event.xmotion.window);
11135
11136 if (bar)
11137 x_scroll_bar_note_movement (bar, &event);
11138 #endif /* USE_TOOLKIT_SCROLL_BARS */
11139
11140 /* If we move outside the frame, then we're
11141 certainly no longer on any text in the frame. */
11142 clear_mouse_face (dpyinfo);
11143 }
11144
11145 /* If the contents of the global variable help_echo
11146 has changed, generate a HELP_EVENT. */
11147 if (!NILP (help_echo)
11148 || !NILP (previous_help_echo))
11149 {
11150 Lisp_Object frame;
11151 int n;
11152
11153 if (f)
11154 XSETFRAME (frame, f);
11155 else
11156 frame = Qnil;
11157
11158 any_help_event_p = 1;
11159 n = gen_help_event (bufp, numchars, help_echo, frame,
11160 help_echo_window, help_echo_object,
11161 help_echo_pos);
11162 bufp += n, count += n, numchars -= n;
11163 }
11164
11165 goto OTHER;
11166 }
11167
11168 case ConfigureNotify:
11169 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
11170 if (f)
11171 {
11172 #ifndef USE_X_TOOLKIT
11173 /* If there is a pending resize for fullscreen, don't
11174 do this one, the right one will come later.
11175 The toolkit version doesn't seem to need this, but we
11176 need to reset it below. */
11177 int dont_resize =
11178 ((f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
11179 && FRAME_NEW_WIDTH (f) != 0);
11180 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
11181 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
11182 if (dont_resize)
11183 goto OTHER;
11184
11185 /* In the toolkit version, change_frame_size
11186 is called by the code that handles resizing
11187 of the EmacsFrame widget. */
11188
11189 /* Even if the number of character rows and columns has
11190 not changed, the font size may have changed, so we need
11191 to check the pixel dimensions as well. */
11192 if (columns != f->width
11193 || rows != f->height
11194 || event.xconfigure.width != f->output_data.x->pixel_width
11195 || event.xconfigure.height != f->output_data.x->pixel_height)
11196 {
11197 change_frame_size (f, rows, columns, 0, 1, 0);
11198 SET_FRAME_GARBAGED (f);
11199 cancel_mouse_face (f);
11200 }
11201 #endif
11202
11203 f->output_data.x->pixel_width = event.xconfigure.width;
11204 f->output_data.x->pixel_height = event.xconfigure.height;
11205
11206 /* What we have now is the position of Emacs's own window.
11207 Convert that to the position of the window manager window. */
11208 x_real_positions (f, &f->output_data.x->left_pos,
11209 &f->output_data.x->top_pos);
11210
11211 x_check_fullscreen_move(f);
11212 if (f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
11213 f->output_data.x->want_fullscreen &=
11214 ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
11215 #ifdef HAVE_X_I18N
11216 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
11217 xic_set_statusarea (f);
11218 #endif
11219
11220 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
11221 {
11222 /* Since the WM decorations come below top_pos now,
11223 we must put them below top_pos in the future. */
11224 f->output_data.x->win_gravity = NorthWestGravity;
11225 x_wm_set_size_hint (f, (long) 0, 0);
11226 }
11227 }
11228 goto OTHER;
11229
11230 case ButtonPress:
11231 case ButtonRelease:
11232 {
11233 /* If we decide we want to generate an event to be seen
11234 by the rest of Emacs, we put it here. */
11235 struct input_event emacs_event;
11236 int tool_bar_p = 0;
11237
11238 emacs_event.kind = NO_EVENT;
11239 bzero (&compose_status, sizeof (compose_status));
11240
11241 if (dpyinfo->grabbed
11242 && last_mouse_frame
11243 && FRAME_LIVE_P (last_mouse_frame))
11244 f = last_mouse_frame;
11245 else
11246 f = x_window_to_frame (dpyinfo, event.xbutton.window);
11247
11248 if (f)
11249 {
11250 /* Is this in the tool-bar? */
11251 if (WINDOWP (f->tool_bar_window)
11252 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
11253 {
11254 Lisp_Object window;
11255 int p, x, y;
11256
11257 x = event.xbutton.x;
11258 y = event.xbutton.y;
11259
11260 /* Set x and y. */
11261 window = window_from_coordinates (f, x, y, &p, 1);
11262 if (EQ (window, f->tool_bar_window))
11263 {
11264 x_handle_tool_bar_click (f, &event.xbutton);
11265 tool_bar_p = 1;
11266 }
11267 }
11268
11269 if (!tool_bar_p)
11270 if (!dpyinfo->x_focus_frame
11271 || f == dpyinfo->x_focus_frame)
11272 construct_mouse_click (&emacs_event, &event, f);
11273 }
11274 else
11275 {
11276 #ifndef USE_TOOLKIT_SCROLL_BARS
11277 struct scroll_bar *bar
11278 = x_window_to_scroll_bar (event.xbutton.window);
11279
11280 if (bar)
11281 x_scroll_bar_handle_click (bar, &event, &emacs_event);
11282 #endif /* not USE_TOOLKIT_SCROLL_BARS */
11283 }
11284
11285 if (event.type == ButtonPress)
11286 {
11287 dpyinfo->grabbed |= (1 << event.xbutton.button);
11288 last_mouse_frame = f;
11289 /* Ignore any mouse motion that happened
11290 before this event; any subsequent mouse-movement
11291 Emacs events should reflect only motion after
11292 the ButtonPress. */
11293 if (f != 0)
11294 f->mouse_moved = 0;
11295
11296 if (!tool_bar_p)
11297 last_tool_bar_item = -1;
11298 }
11299 else
11300 {
11301 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
11302 }
11303
11304 if (numchars >= 1 && emacs_event.kind != NO_EVENT)
11305 {
11306 bcopy (&emacs_event, bufp, sizeof (struct input_event));
11307 bufp++;
11308 count++;
11309 numchars--;
11310 }
11311
11312 #ifdef USE_X_TOOLKIT
11313 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
11314 /* For a down-event in the menu bar,
11315 don't pass it to Xt right now.
11316 Instead, save it away
11317 and we will pass it to Xt from kbd_buffer_get_event.
11318 That way, we can run some Lisp code first. */
11319 if (f && event.type == ButtonPress
11320 /* Verify the event is really within the menu bar
11321 and not just sent to it due to grabbing. */
11322 && event.xbutton.x >= 0
11323 && event.xbutton.x < f->output_data.x->pixel_width
11324 && event.xbutton.y >= 0
11325 && event.xbutton.y < f->output_data.x->menubar_height
11326 && event.xbutton.same_screen)
11327 {
11328 SET_SAVED_BUTTON_EVENT;
11329 XSETFRAME (last_mouse_press_frame, f);
11330 }
11331 else if (event.type == ButtonPress)
11332 {
11333 last_mouse_press_frame = Qnil;
11334 goto OTHER;
11335 }
11336
11337 #ifdef USE_MOTIF /* This should do not harm for Lucid,
11338 but I am trying to be cautious. */
11339 else if (event.type == ButtonRelease)
11340 {
11341 if (!NILP (last_mouse_press_frame))
11342 {
11343 f = XFRAME (last_mouse_press_frame);
11344 if (f->output_data.x)
11345 SET_SAVED_BUTTON_EVENT;
11346 }
11347 else
11348 goto OTHER;
11349 }
11350 #endif /* USE_MOTIF */
11351 else
11352 goto OTHER;
11353 #endif /* USE_X_TOOLKIT */
11354 }
11355 break;
11356
11357 case CirculateNotify:
11358 goto OTHER;
11359
11360 case CirculateRequest:
11361 goto OTHER;
11362
11363 case VisibilityNotify:
11364 goto OTHER;
11365
11366 case MappingNotify:
11367 /* Someone has changed the keyboard mapping - update the
11368 local cache. */
11369 switch (event.xmapping.request)
11370 {
11371 case MappingModifier:
11372 x_find_modifier_meanings (dpyinfo);
11373 /* This is meant to fall through. */
11374 case MappingKeyboard:
11375 XRefreshKeyboardMapping (&event.xmapping);
11376 }
11377 goto OTHER;
11378
11379 default:
11380 OTHER:
11381 #ifdef USE_X_TOOLKIT
11382 BLOCK_INPUT;
11383 XtDispatchEvent (&event);
11384 UNBLOCK_INPUT;
11385 #endif /* USE_X_TOOLKIT */
11386 break;
11387 }
11388 }
11389 }
11390
11391 out:;
11392
11393 /* On some systems, an X bug causes Emacs to get no more events
11394 when the window is destroyed. Detect that. (1994.) */
11395 if (! event_found)
11396 {
11397 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
11398 One XNOOP in 100 loops will make Emacs terminate.
11399 B. Bretthauer, 1994 */
11400 x_noop_count++;
11401 if (x_noop_count >= 100)
11402 {
11403 x_noop_count=0;
11404
11405 if (next_noop_dpyinfo == 0)
11406 next_noop_dpyinfo = x_display_list;
11407
11408 XNoOp (next_noop_dpyinfo->display);
11409
11410 /* Each time we get here, cycle through the displays now open. */
11411 next_noop_dpyinfo = next_noop_dpyinfo->next;
11412 }
11413 }
11414
11415 /* If the focus was just given to an auto-raising frame,
11416 raise it now. */
11417 /* ??? This ought to be able to handle more than one such frame. */
11418 if (pending_autoraise_frame)
11419 {
11420 x_raise_frame (pending_autoraise_frame);
11421 pending_autoraise_frame = 0;
11422 }
11423
11424 UNBLOCK_INPUT;
11425 --handling_signal;
11426 return count;
11427 }
11428
11429
11430
11431 \f
11432 /***********************************************************************
11433 Text Cursor
11434 ***********************************************************************/
11435
11436 /* Notice when the text cursor of window W has been completely
11437 overwritten by a drawing operation that outputs glyphs in AREA
11438 starting at X0 and ending at X1 in the line starting at Y0 and
11439 ending at Y1. X coordinates are area-relative. X1 < 0 means all
11440 the rest of the line after X0 has been written. Y coordinates
11441 are window-relative. */
11442
11443 static void
11444 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
11445 struct window *w;
11446 enum glyph_row_area area;
11447 int x0, y0, x1, y1;
11448 {
11449 if (area == TEXT_AREA && w->phys_cursor_on_p)
11450 {
11451 int cx0 = w->phys_cursor.x;
11452 int cx1 = cx0 + w->phys_cursor_width;
11453 int cy0 = w->phys_cursor.y;
11454 int cy1 = cy0 + w->phys_cursor_height;
11455
11456 if (x0 <= cx0 && (x1 < 0 || x1 >= cx1))
11457 {
11458 /* The cursor image will be completely removed from the
11459 screen if the output area intersects the cursor area in
11460 y-direction. When we draw in [y0 y1[, and some part of
11461 the cursor is at y < y0, that part must have been drawn
11462 before. When scrolling, the cursor is erased before
11463 actually scrolling, so we don't come here. When not
11464 scrolling, the rows above the old cursor row must have
11465 changed, and in this case these rows must have written
11466 over the cursor image.
11467
11468 Likewise if part of the cursor is below y1, with the
11469 exception of the cursor being in the first blank row at
11470 the buffer and window end because update_text_area
11471 doesn't draw that row. (Except when it does, but
11472 that's handled in update_text_area.) */
11473
11474 if (((y0 >= cy0 && y0 < cy1) || (y1 > cy0 && y1 < cy1))
11475 && w->current_matrix->rows[w->phys_cursor.vpos].displays_text_p)
11476 w->phys_cursor_on_p = 0;
11477 }
11478 }
11479 }
11480
11481
11482 /* Set clipping for output in glyph row ROW. W is the window in which
11483 we operate. GC is the graphics context to set clipping in.
11484 WHOLE_LINE_P non-zero means include the areas used for truncation
11485 mark display and alike in the clipping rectangle.
11486
11487 ROW may be a text row or, e.g., a mode line. Text rows must be
11488 clipped to the interior of the window dedicated to text display,
11489 mode lines must be clipped to the whole window. */
11490
11491 static void
11492 x_clip_to_row (w, row, gc, whole_line_p)
11493 struct window *w;
11494 struct glyph_row *row;
11495 GC gc;
11496 int whole_line_p;
11497 {
11498 struct frame *f = XFRAME (WINDOW_FRAME (w));
11499 XRectangle clip_rect;
11500 int window_x, window_y, window_width, window_height;
11501
11502 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
11503
11504 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
11505 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
11506 clip_rect.y = max (clip_rect.y, window_y);
11507 clip_rect.width = window_width;
11508 clip_rect.height = row->visible_height;
11509
11510 /* If clipping to the whole line, including trunc marks, extend
11511 the rectangle to the left and increase its width. */
11512 if (whole_line_p)
11513 {
11514 clip_rect.x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
11515 clip_rect.width += FRAME_X_FRINGE_WIDTH (f);
11516 }
11517
11518 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
11519 }
11520
11521
11522 /* Draw a hollow box cursor on window W in glyph row ROW. */
11523
11524 static void
11525 x_draw_hollow_cursor (w, row)
11526 struct window *w;
11527 struct glyph_row *row;
11528 {
11529 struct frame *f = XFRAME (WINDOW_FRAME (w));
11530 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11531 Display *dpy = FRAME_X_DISPLAY (f);
11532 int x, y, wd, h;
11533 XGCValues xgcv;
11534 struct glyph *cursor_glyph;
11535 GC gc;
11536
11537 /* Compute frame-relative coordinates from window-relative
11538 coordinates. */
11539 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11540 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
11541 + row->ascent - w->phys_cursor_ascent);
11542 h = row->height - 1;
11543
11544 /* Get the glyph the cursor is on. If we can't tell because
11545 the current matrix is invalid or such, give up. */
11546 cursor_glyph = get_phys_cursor_glyph (w);
11547 if (cursor_glyph == NULL)
11548 return;
11549
11550 /* Compute the width of the rectangle to draw. If on a stretch
11551 glyph, and `x-stretch-block-cursor' is nil, don't draw a
11552 rectangle as wide as the glyph, but use a canonical character
11553 width instead. */
11554 wd = cursor_glyph->pixel_width - 1;
11555 if (cursor_glyph->type == STRETCH_GLYPH
11556 && !x_stretch_cursor_p)
11557 wd = min (CANON_X_UNIT (f), wd);
11558 w->phys_cursor_width = wd;
11559
11560 /* The foreground of cursor_gc is typically the same as the normal
11561 background color, which can cause the cursor box to be invisible. */
11562 xgcv.foreground = f->output_data.x->cursor_pixel;
11563 if (dpyinfo->scratch_cursor_gc)
11564 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
11565 else
11566 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
11567 GCForeground, &xgcv);
11568 gc = dpyinfo->scratch_cursor_gc;
11569
11570 /* Set clipping, draw the rectangle, and reset clipping again. */
11571 x_clip_to_row (w, row, gc, 0);
11572 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
11573 XSetClipMask (dpy, gc, None);
11574 }
11575
11576
11577 /* Draw a bar cursor on window W in glyph row ROW.
11578
11579 Implementation note: One would like to draw a bar cursor with an
11580 angle equal to the one given by the font property XA_ITALIC_ANGLE.
11581 Unfortunately, I didn't find a font yet that has this property set.
11582 --gerd. */
11583
11584 static void
11585 x_draw_bar_cursor (w, row, width, kind)
11586 struct window *w;
11587 struct glyph_row *row;
11588 int width;
11589 enum text_cursor_kinds kind;
11590 {
11591 struct frame *f = XFRAME (w->frame);
11592 struct glyph *cursor_glyph;
11593
11594 /* If cursor is out of bounds, don't draw garbage. This can happen
11595 in mini-buffer windows when switching between echo area glyphs
11596 and mini-buffer. */
11597 cursor_glyph = get_phys_cursor_glyph (w);
11598 if (cursor_glyph == NULL)
11599 return;
11600
11601 /* If on an image, draw like a normal cursor. That's usually better
11602 visible than drawing a bar, esp. if the image is large so that
11603 the bar might not be in the window. */
11604 if (cursor_glyph->type == IMAGE_GLYPH)
11605 {
11606 struct glyph_row *row;
11607 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
11608 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
11609 }
11610 else
11611 {
11612 Display *dpy = FRAME_X_DISPLAY (f);
11613 Window window = FRAME_X_WINDOW (f);
11614 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
11615 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
11616 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
11617 XGCValues xgcv;
11618
11619 /* If the glyph's background equals the color we normally draw
11620 the bar cursor in, the bar cursor in its normal color is
11621 invisible. Use the glyph's foreground color instead in this
11622 case, on the assumption that the glyph's colors are chosen so
11623 that the glyph is legible. */
11624 if (face->background == f->output_data.x->cursor_pixel)
11625 xgcv.background = xgcv.foreground = face->foreground;
11626 else
11627 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
11628 xgcv.graphics_exposures = 0;
11629
11630 if (gc)
11631 XChangeGC (dpy, gc, mask, &xgcv);
11632 else
11633 {
11634 gc = XCreateGC (dpy, window, mask, &xgcv);
11635 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
11636 }
11637
11638 if (width < 0)
11639 width = FRAME_CURSOR_WIDTH (f);
11640 width = min (cursor_glyph->pixel_width, width);
11641
11642 w->phys_cursor_width = width;
11643 x_clip_to_row (w, row, gc, 0);
11644
11645 if (kind == BAR_CURSOR)
11646 XFillRectangle (dpy, window, gc,
11647 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11648 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
11649 width, row->height);
11650 else
11651 XFillRectangle (dpy, window, gc,
11652 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11653 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
11654 row->height - width),
11655 cursor_glyph->pixel_width,
11656 width);
11657
11658 XSetClipMask (dpy, gc, None);
11659 }
11660 }
11661
11662
11663 /* Clear the cursor of window W to background color, and mark the
11664 cursor as not shown. This is used when the text where the cursor
11665 is is about to be rewritten. */
11666
11667 static void
11668 x_clear_cursor (w)
11669 struct window *w;
11670 {
11671 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
11672 x_update_window_cursor (w, 0);
11673 }
11674
11675
11676 /* Draw the cursor glyph of window W in glyph row ROW. See the
11677 comment of x_draw_glyphs for the meaning of HL. */
11678
11679 static void
11680 x_draw_phys_cursor_glyph (w, row, hl)
11681 struct window *w;
11682 struct glyph_row *row;
11683 enum draw_glyphs_face hl;
11684 {
11685 /* If cursor hpos is out of bounds, don't draw garbage. This can
11686 happen in mini-buffer windows when switching between echo area
11687 glyphs and mini-buffer. */
11688 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
11689 {
11690 int on_p = w->phys_cursor_on_p;
11691 int x1;
11692
11693 x1 = x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
11694 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
11695 hl, 0);
11696 w->phys_cursor_on_p = on_p;
11697
11698 if (hl == DRAW_CURSOR)
11699 w->phys_cursor_width = x1 - w->phys_cursor.x;
11700
11701 /* When we erase the cursor, and ROW is overlapped by other
11702 rows, make sure that these overlapping parts of other rows
11703 are redrawn. */
11704 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
11705 {
11706 if (row > w->current_matrix->rows
11707 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
11708 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
11709
11710 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
11711 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
11712 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
11713 }
11714 }
11715 }
11716
11717
11718 /* Erase the image of a cursor of window W from the screen. */
11719
11720 static void
11721 x_erase_phys_cursor (w)
11722 struct window *w;
11723 {
11724 struct frame *f = XFRAME (w->frame);
11725 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11726 int hpos = w->phys_cursor.hpos;
11727 int vpos = w->phys_cursor.vpos;
11728 int mouse_face_here_p = 0;
11729 struct glyph_matrix *active_glyphs = w->current_matrix;
11730 struct glyph_row *cursor_row;
11731 struct glyph *cursor_glyph;
11732 enum draw_glyphs_face hl;
11733
11734 /* No cursor displayed or row invalidated => nothing to do on the
11735 screen. */
11736 if (w->phys_cursor_type == NO_CURSOR)
11737 goto mark_cursor_off;
11738
11739 /* VPOS >= active_glyphs->nrows means that window has been resized.
11740 Don't bother to erase the cursor. */
11741 if (vpos >= active_glyphs->nrows)
11742 goto mark_cursor_off;
11743
11744 /* If row containing cursor is marked invalid, there is nothing we
11745 can do. */
11746 cursor_row = MATRIX_ROW (active_glyphs, vpos);
11747 if (!cursor_row->enabled_p)
11748 goto mark_cursor_off;
11749
11750 /* If row is completely invisible, don't attempt to delete a cursor which
11751 isn't there. This can happen if cursor is at top of a window, and
11752 we switch to a buffer with a header line in that window. */
11753 if (cursor_row->visible_height <= 0)
11754 goto mark_cursor_off;
11755
11756 /* This can happen when the new row is shorter than the old one.
11757 In this case, either x_draw_glyphs or clear_end_of_line
11758 should have cleared the cursor. Note that we wouldn't be
11759 able to erase the cursor in this case because we don't have a
11760 cursor glyph at hand. */
11761 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
11762 goto mark_cursor_off;
11763
11764 /* If the cursor is in the mouse face area, redisplay that when
11765 we clear the cursor. */
11766 if (! NILP (dpyinfo->mouse_face_window)
11767 && w == XWINDOW (dpyinfo->mouse_face_window)
11768 && (vpos > dpyinfo->mouse_face_beg_row
11769 || (vpos == dpyinfo->mouse_face_beg_row
11770 && hpos >= dpyinfo->mouse_face_beg_col))
11771 && (vpos < dpyinfo->mouse_face_end_row
11772 || (vpos == dpyinfo->mouse_face_end_row
11773 && hpos < dpyinfo->mouse_face_end_col))
11774 /* Don't redraw the cursor's spot in mouse face if it is at the
11775 end of a line (on a newline). The cursor appears there, but
11776 mouse highlighting does not. */
11777 && cursor_row->used[TEXT_AREA] > hpos)
11778 mouse_face_here_p = 1;
11779
11780 /* Maybe clear the display under the cursor. */
11781 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
11782 {
11783 int x;
11784 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11785
11786 cursor_glyph = get_phys_cursor_glyph (w);
11787 if (cursor_glyph == NULL)
11788 goto mark_cursor_off;
11789
11790 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11791
11792 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11793 x,
11794 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
11795 cursor_row->y)),
11796 cursor_glyph->pixel_width,
11797 cursor_row->visible_height,
11798 False);
11799 }
11800
11801 /* Erase the cursor by redrawing the character underneath it. */
11802 if (mouse_face_here_p)
11803 hl = DRAW_MOUSE_FACE;
11804 else
11805 hl = DRAW_NORMAL_TEXT;
11806 x_draw_phys_cursor_glyph (w, cursor_row, hl);
11807
11808 mark_cursor_off:
11809 w->phys_cursor_on_p = 0;
11810 w->phys_cursor_type = NO_CURSOR;
11811 }
11812
11813
11814 /* Non-zero if physical cursor of window W is within mouse face. */
11815
11816 static int
11817 cursor_in_mouse_face_p (w)
11818 struct window *w;
11819 {
11820 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
11821 int in_mouse_face = 0;
11822
11823 if (WINDOWP (dpyinfo->mouse_face_window)
11824 && XWINDOW (dpyinfo->mouse_face_window) == w)
11825 {
11826 int hpos = w->phys_cursor.hpos;
11827 int vpos = w->phys_cursor.vpos;
11828
11829 if (vpos >= dpyinfo->mouse_face_beg_row
11830 && vpos <= dpyinfo->mouse_face_end_row
11831 && (vpos > dpyinfo->mouse_face_beg_row
11832 || hpos >= dpyinfo->mouse_face_beg_col)
11833 && (vpos < dpyinfo->mouse_face_end_row
11834 || hpos < dpyinfo->mouse_face_end_col
11835 || dpyinfo->mouse_face_past_end))
11836 in_mouse_face = 1;
11837 }
11838
11839 return in_mouse_face;
11840 }
11841
11842
11843 /* Display or clear cursor of window W. If ON is zero, clear the
11844 cursor. If it is non-zero, display the cursor. If ON is nonzero,
11845 where to put the cursor is specified by HPOS, VPOS, X and Y. */
11846
11847 void
11848 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
11849 struct window *w;
11850 int on, hpos, vpos, x, y;
11851 {
11852 struct frame *f = XFRAME (w->frame);
11853 int new_cursor_type;
11854 int new_cursor_width;
11855 int active_cursor;
11856 struct glyph_matrix *current_glyphs;
11857 struct glyph_row *glyph_row;
11858 struct glyph *glyph;
11859
11860 /* This is pointless on invisible frames, and dangerous on garbaged
11861 windows and frames; in the latter case, the frame or window may
11862 be in the midst of changing its size, and x and y may be off the
11863 window. */
11864 if (! FRAME_VISIBLE_P (f)
11865 || FRAME_GARBAGED_P (f)
11866 || vpos >= w->current_matrix->nrows
11867 || hpos >= w->current_matrix->matrix_w)
11868 return;
11869
11870 /* If cursor is off and we want it off, return quickly. */
11871 if (!on && !w->phys_cursor_on_p)
11872 return;
11873
11874 current_glyphs = w->current_matrix;
11875 glyph_row = MATRIX_ROW (current_glyphs, vpos);
11876 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
11877
11878 /* If cursor row is not enabled, we don't really know where to
11879 display the cursor. */
11880 if (!glyph_row->enabled_p)
11881 {
11882 w->phys_cursor_on_p = 0;
11883 return;
11884 }
11885
11886 xassert (interrupt_input_blocked);
11887
11888 /* Set new_cursor_type to the cursor we want to be displayed. */
11889 new_cursor_type = get_window_cursor_type (w, &new_cursor_width, &active_cursor);
11890
11891 /* If cursor is currently being shown and we don't want it to be or
11892 it is in the wrong place, or the cursor type is not what we want,
11893 erase it. */
11894 if (w->phys_cursor_on_p
11895 && (!on
11896 || w->phys_cursor.x != x
11897 || w->phys_cursor.y != y
11898 || new_cursor_type != w->phys_cursor_type
11899 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
11900 && new_cursor_width != w->phys_cursor_width)))
11901 x_erase_phys_cursor (w);
11902
11903 /* Don't check phys_cursor_on_p here because that flag is only set
11904 to zero in some cases where we know that the cursor has been
11905 completely erased, to avoid the extra work of erasing the cursor
11906 twice. In other words, phys_cursor_on_p can be 1 and the cursor
11907 still not be visible, or it has only been partly erased. */
11908 if (on)
11909 {
11910 w->phys_cursor_ascent = glyph_row->ascent;
11911 w->phys_cursor_height = glyph_row->height;
11912
11913 /* Set phys_cursor_.* before x_draw_.* is called because some
11914 of them may need the information. */
11915 w->phys_cursor.x = x;
11916 w->phys_cursor.y = glyph_row->y;
11917 w->phys_cursor.hpos = hpos;
11918 w->phys_cursor.vpos = vpos;
11919 w->phys_cursor_type = new_cursor_type;
11920 w->phys_cursor_on_p = 1;
11921
11922 switch (new_cursor_type)
11923 {
11924 case HOLLOW_BOX_CURSOR:
11925 x_draw_hollow_cursor (w, glyph_row);
11926 break;
11927
11928 case FILLED_BOX_CURSOR:
11929 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
11930 break;
11931
11932 case BAR_CURSOR:
11933 x_draw_bar_cursor (w, glyph_row, new_cursor_width, BAR_CURSOR);
11934 break;
11935
11936 case HBAR_CURSOR:
11937 x_draw_bar_cursor (w, glyph_row, new_cursor_width, HBAR_CURSOR);
11938 break;
11939
11940 case NO_CURSOR:
11941 w->phys_cursor_width = 0;
11942 break;
11943
11944 default:
11945 abort ();
11946 }
11947
11948 #ifdef HAVE_X_I18N
11949 if (w == XWINDOW (f->selected_window))
11950 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
11951 xic_set_preeditarea (w, x, y);
11952 #endif
11953 }
11954
11955 #ifndef XFlush
11956 if (updating_frame != f)
11957 XFlush (FRAME_X_DISPLAY (f));
11958 #endif
11959 }
11960
11961
11962 /* Display the cursor on window W, or clear it. X and Y are window
11963 relative pixel coordinates. HPOS and VPOS are glyph matrix
11964 positions. If W is not the selected window, display a hollow
11965 cursor. ON non-zero means display the cursor at X, Y which
11966 correspond to HPOS, VPOS, otherwise it is cleared. */
11967
11968 void
11969 x_display_cursor (w, on, hpos, vpos, x, y)
11970 struct window *w;
11971 int on, hpos, vpos, x, y;
11972 {
11973 BLOCK_INPUT;
11974 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
11975 UNBLOCK_INPUT;
11976 }
11977
11978
11979 /* Display the cursor on window W, or clear it, according to ON_P.
11980 Don't change the cursor's position. */
11981
11982 void
11983 x_update_cursor (f, on_p)
11984 struct frame *f;
11985 {
11986 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11987 }
11988
11989
11990 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
11991 in the window tree rooted at W. */
11992
11993 static void
11994 x_update_cursor_in_window_tree (w, on_p)
11995 struct window *w;
11996 int on_p;
11997 {
11998 while (w)
11999 {
12000 if (!NILP (w->hchild))
12001 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
12002 else if (!NILP (w->vchild))
12003 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
12004 else
12005 x_update_window_cursor (w, on_p);
12006
12007 w = NILP (w->next) ? 0 : XWINDOW (w->next);
12008 }
12009 }
12010
12011
12012 /* Switch the display of W's cursor on or off, according to the value
12013 of ON. */
12014
12015 static void
12016 x_update_window_cursor (w, on)
12017 struct window *w;
12018 int on;
12019 {
12020 /* Don't update cursor in windows whose frame is in the process
12021 of being deleted. */
12022 if (w->current_matrix)
12023 {
12024 BLOCK_INPUT;
12025 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
12026 w->phys_cursor.x, w->phys_cursor.y);
12027 UNBLOCK_INPUT;
12028 }
12029 }
12030
12031
12032
12033 \f
12034 /* Icons. */
12035
12036 /* Make the x-window of frame F use the gnu icon bitmap. */
12037
12038 int
12039 x_bitmap_icon (f, file)
12040 struct frame *f;
12041 Lisp_Object file;
12042 {
12043 int bitmap_id;
12044
12045 if (FRAME_X_WINDOW (f) == 0)
12046 return 1;
12047
12048 /* Free up our existing icon bitmap if any. */
12049 if (f->output_data.x->icon_bitmap > 0)
12050 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
12051 f->output_data.x->icon_bitmap = 0;
12052
12053 if (STRINGP (file))
12054 bitmap_id = x_create_bitmap_from_file (f, file);
12055 else
12056 {
12057 /* Create the GNU bitmap if necessary. */
12058 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
12059 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
12060 = x_create_bitmap_from_data (f, gnu_bits,
12061 gnu_width, gnu_height);
12062
12063 /* The first time we create the GNU bitmap,
12064 this increments the ref-count one extra time.
12065 As a result, the GNU bitmap is never freed.
12066 That way, we don't have to worry about allocating it again. */
12067 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
12068
12069 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
12070 }
12071
12072 x_wm_set_icon_pixmap (f, bitmap_id);
12073 f->output_data.x->icon_bitmap = bitmap_id;
12074
12075 return 0;
12076 }
12077
12078
12079 /* Make the x-window of frame F use a rectangle with text.
12080 Use ICON_NAME as the text. */
12081
12082 int
12083 x_text_icon (f, icon_name)
12084 struct frame *f;
12085 char *icon_name;
12086 {
12087 if (FRAME_X_WINDOW (f) == 0)
12088 return 1;
12089
12090 #ifdef HAVE_X11R4
12091 {
12092 XTextProperty text;
12093 text.value = (unsigned char *) icon_name;
12094 text.encoding = XA_STRING;
12095 text.format = 8;
12096 text.nitems = strlen (icon_name);
12097 #ifdef USE_X_TOOLKIT
12098 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
12099 &text);
12100 #else /* not USE_X_TOOLKIT */
12101 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
12102 #endif /* not USE_X_TOOLKIT */
12103 }
12104 #else /* not HAVE_X11R4 */
12105 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
12106 #endif /* not HAVE_X11R4 */
12107
12108 if (f->output_data.x->icon_bitmap > 0)
12109 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
12110 f->output_data.x->icon_bitmap = 0;
12111 x_wm_set_icon_pixmap (f, 0);
12112
12113 return 0;
12114 }
12115 \f
12116 #define X_ERROR_MESSAGE_SIZE 200
12117
12118 /* If non-nil, this should be a string.
12119 It means catch X errors and store the error message in this string. */
12120
12121 static Lisp_Object x_error_message_string;
12122
12123 /* An X error handler which stores the error message in
12124 x_error_message_string. This is called from x_error_handler if
12125 x_catch_errors is in effect. */
12126
12127 static void
12128 x_error_catcher (display, error)
12129 Display *display;
12130 XErrorEvent *error;
12131 {
12132 XGetErrorText (display, error->error_code,
12133 SDATA (x_error_message_string),
12134 X_ERROR_MESSAGE_SIZE);
12135 }
12136
12137 /* Begin trapping X errors for display DPY. Actually we trap X errors
12138 for all displays, but DPY should be the display you are actually
12139 operating on.
12140
12141 After calling this function, X protocol errors no longer cause
12142 Emacs to exit; instead, they are recorded in the string
12143 stored in x_error_message_string.
12144
12145 Calling x_check_errors signals an Emacs error if an X error has
12146 occurred since the last call to x_catch_errors or x_check_errors.
12147
12148 Calling x_uncatch_errors resumes the normal error handling. */
12149
12150 void x_check_errors ();
12151 static Lisp_Object x_catch_errors_unwind ();
12152
12153 int
12154 x_catch_errors (dpy)
12155 Display *dpy;
12156 {
12157 int count = SPECPDL_INDEX ();
12158
12159 /* Make sure any errors from previous requests have been dealt with. */
12160 XSync (dpy, False);
12161
12162 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
12163
12164 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
12165 SSET (x_error_message_string, 0, 0);
12166
12167 return count;
12168 }
12169
12170 /* Unbind the binding that we made to check for X errors. */
12171
12172 static Lisp_Object
12173 x_catch_errors_unwind (old_val)
12174 Lisp_Object old_val;
12175 {
12176 x_error_message_string = old_val;
12177 return Qnil;
12178 }
12179
12180 /* If any X protocol errors have arrived since the last call to
12181 x_catch_errors or x_check_errors, signal an Emacs error using
12182 sprintf (a buffer, FORMAT, the x error message text) as the text. */
12183
12184 void
12185 x_check_errors (dpy, format)
12186 Display *dpy;
12187 char *format;
12188 {
12189 /* Make sure to catch any errors incurred so far. */
12190 XSync (dpy, False);
12191
12192 if (SREF (x_error_message_string, 0))
12193 error (format, SDATA (x_error_message_string));
12194 }
12195
12196 /* Nonzero if we had any X protocol errors
12197 since we did x_catch_errors on DPY. */
12198
12199 int
12200 x_had_errors_p (dpy)
12201 Display *dpy;
12202 {
12203 /* Make sure to catch any errors incurred so far. */
12204 XSync (dpy, False);
12205
12206 return SREF (x_error_message_string, 0) != 0;
12207 }
12208
12209 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
12210
12211 void
12212 x_clear_errors (dpy)
12213 Display *dpy;
12214 {
12215 SSET (x_error_message_string, 0, 0);
12216 }
12217
12218 /* Stop catching X protocol errors and let them make Emacs die.
12219 DPY should be the display that was passed to x_catch_errors.
12220 COUNT should be the value that was returned by
12221 the corresponding call to x_catch_errors. */
12222
12223 void
12224 x_uncatch_errors (dpy, count)
12225 Display *dpy;
12226 int count;
12227 {
12228 unbind_to (count, Qnil);
12229 }
12230
12231 #if 0
12232 static unsigned int x_wire_count;
12233 x_trace_wire ()
12234 {
12235 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
12236 }
12237 #endif /* ! 0 */
12238
12239 \f
12240 /* Handle SIGPIPE, which can happen when the connection to a server
12241 simply goes away. SIGPIPE is handled by x_connection_signal.
12242 Don't need to do anything, because the write which caused the
12243 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
12244 which will do the appropriate cleanup for us. */
12245
12246 static SIGTYPE
12247 x_connection_signal (signalnum) /* If we don't have an argument, */
12248 int signalnum; /* some compilers complain in signal calls. */
12249 {
12250 #ifdef USG
12251 /* USG systems forget handlers when they are used;
12252 must reestablish each time */
12253 signal (signalnum, x_connection_signal);
12254 #endif /* USG */
12255 }
12256
12257 \f
12258 /************************************************************************
12259 Handling X errors
12260 ************************************************************************/
12261
12262 /* Error message passed to x_connection_closed. */
12263
12264 static char *error_msg;
12265
12266 /* Function installed as fatal_error_signal_hook in
12267 x_connection_closed. Print the X error message, and exit normally,
12268 instead of dumping core when XtCloseDisplay fails. */
12269
12270 static void
12271 x_fatal_error_signal ()
12272 {
12273 fprintf (stderr, "%s\n", error_msg);
12274 exit (70);
12275 }
12276
12277 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
12278 the text of an error message that lead to the connection loss. */
12279
12280 static SIGTYPE
12281 x_connection_closed (dpy, error_message)
12282 Display *dpy;
12283 char *error_message;
12284 {
12285 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
12286 Lisp_Object frame, tail;
12287 int count;
12288
12289 error_msg = (char *) alloca (strlen (error_message) + 1);
12290 strcpy (error_msg, error_message);
12291 handling_signal = 0;
12292
12293 /* Prevent being called recursively because of an error condition
12294 below. Otherwise, we might end up with printing ``can't find per
12295 display information'' in the recursive call instead of printing
12296 the original message here. */
12297 count = x_catch_errors (dpy);
12298
12299 /* We have to close the display to inform Xt that it doesn't
12300 exist anymore. If we don't, Xt will continue to wait for
12301 events from the display. As a consequence, a sequence of
12302
12303 M-x make-frame-on-display RET :1 RET
12304 ...kill the new frame, so that we get an IO error...
12305 M-x make-frame-on-display RET :1 RET
12306
12307 will indefinitely wait in Xt for events for display `:1', opened
12308 in the first class to make-frame-on-display.
12309
12310 Closing the display is reported to lead to a bus error on
12311 OpenWindows in certain situations. I suspect that is a bug
12312 in OpenWindows. I don't know how to cicumvent it here. */
12313
12314 #ifdef USE_X_TOOLKIT
12315 /* If DPYINFO is null, this means we didn't open the display
12316 in the first place, so don't try to close it. */
12317 if (dpyinfo)
12318 {
12319 extern void (*fatal_error_signal_hook) P_ ((void));
12320 fatal_error_signal_hook = x_fatal_error_signal;
12321 XtCloseDisplay (dpy);
12322 fatal_error_signal_hook = NULL;
12323 }
12324 #endif
12325
12326 /* Indicate that this display is dead. */
12327 if (dpyinfo)
12328 dpyinfo->display = 0;
12329
12330 /* First delete frames whose mini-buffers are on frames
12331 that are on the dead display. */
12332 FOR_EACH_FRAME (tail, frame)
12333 {
12334 Lisp_Object minibuf_frame;
12335 minibuf_frame
12336 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
12337 if (FRAME_X_P (XFRAME (frame))
12338 && FRAME_X_P (XFRAME (minibuf_frame))
12339 && ! EQ (frame, minibuf_frame)
12340 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
12341 Fdelete_frame (frame, Qt);
12342 }
12343
12344 /* Now delete all remaining frames on the dead display.
12345 We are now sure none of these is used as the mini-buffer
12346 for another frame that we need to delete. */
12347 FOR_EACH_FRAME (tail, frame)
12348 if (FRAME_X_P (XFRAME (frame))
12349 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
12350 {
12351 /* Set this to t so that Fdelete_frame won't get confused
12352 trying to find a replacement. */
12353 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
12354 Fdelete_frame (frame, Qt);
12355 }
12356
12357 if (dpyinfo)
12358 x_delete_display (dpyinfo);
12359
12360 x_uncatch_errors (dpy, count);
12361
12362 if (x_display_list == 0)
12363 {
12364 fprintf (stderr, "%s\n", error_msg);
12365 shut_down_emacs (0, 0, Qnil);
12366 exit (70);
12367 }
12368
12369 /* Ordinary stack unwind doesn't deal with these. */
12370 #ifdef SIGIO
12371 sigunblock (sigmask (SIGIO));
12372 #endif
12373 sigunblock (sigmask (SIGALRM));
12374 TOTALLY_UNBLOCK_INPUT;
12375
12376 clear_waiting_for_input ();
12377 error ("%s", error_msg);
12378 }
12379
12380
12381 /* This is the usual handler for X protocol errors.
12382 It kills all frames on the display that we got the error for.
12383 If that was the only one, it prints an error message and kills Emacs. */
12384
12385 static void
12386 x_error_quitter (display, error)
12387 Display *display;
12388 XErrorEvent *error;
12389 {
12390 char buf[256], buf1[356];
12391
12392 /* Note that there is no real way portable across R3/R4 to get the
12393 original error handler. */
12394
12395 XGetErrorText (display, error->error_code, buf, sizeof (buf));
12396 sprintf (buf1, "X protocol error: %s on protocol request %d",
12397 buf, error->request_code);
12398 x_connection_closed (display, buf1);
12399 }
12400
12401
12402 /* This is the first-level handler for X protocol errors.
12403 It calls x_error_quitter or x_error_catcher. */
12404
12405 static int
12406 x_error_handler (display, error)
12407 Display *display;
12408 XErrorEvent *error;
12409 {
12410 if (! NILP (x_error_message_string))
12411 x_error_catcher (display, error);
12412 else
12413 x_error_quitter (display, error);
12414 return 0;
12415 }
12416
12417 /* This is the handler for X IO errors, always.
12418 It kills all frames on the display that we lost touch with.
12419 If that was the only one, it prints an error message and kills Emacs. */
12420
12421 static int
12422 x_io_error_quitter (display)
12423 Display *display;
12424 {
12425 char buf[256];
12426
12427 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
12428 x_connection_closed (display, buf);
12429 return 0;
12430 }
12431 \f
12432 /* Changing the font of the frame. */
12433
12434 /* Give frame F the font named FONTNAME as its default font, and
12435 return the full name of that font. FONTNAME may be a wildcard
12436 pattern; in that case, we choose some font that fits the pattern.
12437 The return value shows which font we chose. */
12438
12439 Lisp_Object
12440 x_new_font (f, fontname)
12441 struct frame *f;
12442 register char *fontname;
12443 {
12444 struct font_info *fontp
12445 = FS_LOAD_FONT (f, 0, fontname, -1);
12446
12447 if (!fontp)
12448 return Qnil;
12449
12450 f->output_data.x->font = (XFontStruct *) (fontp->font);
12451 f->output_data.x->baseline_offset = fontp->baseline_offset;
12452 f->output_data.x->fontset = -1;
12453
12454 x_compute_fringe_widths (f, 1);
12455
12456 /* Compute the scroll bar width in character columns. */
12457 if (f->scroll_bar_pixel_width > 0)
12458 {
12459 int wid = FONT_WIDTH (f->output_data.x->font);
12460 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
12461 }
12462 else
12463 {
12464 int wid = FONT_WIDTH (f->output_data.x->font);
12465 f->scroll_bar_cols = (14 + wid - 1) / wid;
12466 }
12467
12468 /* Now make the frame display the given font. */
12469 if (FRAME_X_WINDOW (f) != 0)
12470 {
12471 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
12472 f->output_data.x->font->fid);
12473 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
12474 f->output_data.x->font->fid);
12475 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
12476 f->output_data.x->font->fid);
12477
12478 frame_update_line_height (f);
12479
12480 /* Don't change the size of a tip frame; there's no point in
12481 doing it because it's done in Fx_show_tip, and it leads to
12482 problems because the tip frame has no widget. */
12483 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
12484 x_set_window_size (f, 0, f->width, f->height);
12485 }
12486 else
12487 /* If we are setting a new frame's font for the first time,
12488 there are no faces yet, so this font's height is the line height. */
12489 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
12490
12491 return build_string (fontp->full_name);
12492 }
12493
12494 /* Give frame F the fontset named FONTSETNAME as its default font, and
12495 return the full name of that fontset. FONTSETNAME may be a wildcard
12496 pattern; in that case, we choose some fontset that fits the pattern.
12497 The return value shows which fontset we chose. */
12498
12499 Lisp_Object
12500 x_new_fontset (f, fontsetname)
12501 struct frame *f;
12502 char *fontsetname;
12503 {
12504 int fontset = fs_query_fontset (build_string (fontsetname), 0);
12505 Lisp_Object result;
12506
12507 if (fontset < 0)
12508 return Qnil;
12509
12510 if (f->output_data.x->fontset == fontset)
12511 /* This fontset is already set in frame F. There's nothing more
12512 to do. */
12513 return fontset_name (fontset);
12514
12515 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
12516
12517 if (!STRINGP (result))
12518 /* Can't load ASCII font. */
12519 return Qnil;
12520
12521 /* Since x_new_font doesn't update any fontset information, do it now. */
12522 f->output_data.x->fontset = fontset;
12523
12524 #ifdef HAVE_X_I18N
12525 if (FRAME_XIC (f)
12526 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
12527 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
12528 #endif
12529
12530 return build_string (fontsetname);
12531 }
12532
12533 /* Compute actual fringe widths */
12534
12535 void
12536 x_compute_fringe_widths (f, redraw)
12537 struct frame *f;
12538 int redraw;
12539 {
12540 int o_left = f->output_data.x->left_fringe_width;
12541 int o_right = f->output_data.x->right_fringe_width;
12542 int o_cols = f->output_data.x->fringe_cols;
12543
12544 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
12545 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
12546 int left_fringe_width, right_fringe_width;
12547
12548 if (!NILP (left_fringe))
12549 left_fringe = Fcdr (left_fringe);
12550 if (!NILP (right_fringe))
12551 right_fringe = Fcdr (right_fringe);
12552
12553 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
12554 XINT (left_fringe));
12555 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
12556 XINT (right_fringe));
12557
12558 if (left_fringe_width || right_fringe_width)
12559 {
12560 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
12561 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
12562 int conf_wid = left_wid + right_wid;
12563 int font_wid = FONT_WIDTH (f->output_data.x->font);
12564 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
12565 int real_wid = cols * font_wid;
12566 if (left_wid && right_wid)
12567 {
12568 if (left_fringe_width < 0)
12569 {
12570 /* Left fringe width is fixed, adjust right fringe if necessary */
12571 f->output_data.x->left_fringe_width = left_wid;
12572 f->output_data.x->right_fringe_width = real_wid - left_wid;
12573 }
12574 else if (right_fringe_width < 0)
12575 {
12576 /* Right fringe width is fixed, adjust left fringe if necessary */
12577 f->output_data.x->left_fringe_width = real_wid - right_wid;
12578 f->output_data.x->right_fringe_width = right_wid;
12579 }
12580 else
12581 {
12582 /* Adjust both fringes with an equal amount.
12583 Note that we are doing integer arithmetic here, so don't
12584 lose a pixel if the total width is an odd number. */
12585 int fill = real_wid - conf_wid;
12586 f->output_data.x->left_fringe_width = left_wid + fill/2;
12587 f->output_data.x->right_fringe_width = right_wid + fill - fill/2;
12588 }
12589 }
12590 else if (left_fringe_width)
12591 {
12592 f->output_data.x->left_fringe_width = real_wid;
12593 f->output_data.x->right_fringe_width = 0;
12594 }
12595 else
12596 {
12597 f->output_data.x->left_fringe_width = 0;
12598 f->output_data.x->right_fringe_width = real_wid;
12599 }
12600 f->output_data.x->fringe_cols = cols;
12601 f->output_data.x->fringes_extra = real_wid;
12602 }
12603 else
12604 {
12605 f->output_data.x->left_fringe_width = 0;
12606 f->output_data.x->right_fringe_width = 0;
12607 f->output_data.x->fringe_cols = 0;
12608 f->output_data.x->fringes_extra = 0;
12609 }
12610
12611 if (redraw && FRAME_VISIBLE_P (f))
12612 if (o_left != f->output_data.x->left_fringe_width ||
12613 o_right != f->output_data.x->right_fringe_width ||
12614 o_cols != f->output_data.x->fringe_cols)
12615 redraw_frame (f);
12616 }
12617 \f
12618 /***********************************************************************
12619 X Input Methods
12620 ***********************************************************************/
12621
12622 #ifdef HAVE_X_I18N
12623
12624 #ifdef HAVE_X11R6
12625
12626 /* XIM destroy callback function, which is called whenever the
12627 connection to input method XIM dies. CLIENT_DATA contains a
12628 pointer to the x_display_info structure corresponding to XIM. */
12629
12630 static void
12631 xim_destroy_callback (xim, client_data, call_data)
12632 XIM xim;
12633 XPointer client_data;
12634 XPointer call_data;
12635 {
12636 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
12637 Lisp_Object frame, tail;
12638
12639 BLOCK_INPUT;
12640
12641 /* No need to call XDestroyIC.. */
12642 FOR_EACH_FRAME (tail, frame)
12643 {
12644 struct frame *f = XFRAME (frame);
12645 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
12646 {
12647 FRAME_XIC (f) = NULL;
12648 if (FRAME_XIC_FONTSET (f))
12649 {
12650 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
12651 FRAME_XIC_FONTSET (f) = NULL;
12652 }
12653 }
12654 }
12655
12656 /* No need to call XCloseIM. */
12657 dpyinfo->xim = NULL;
12658 XFree (dpyinfo->xim_styles);
12659 UNBLOCK_INPUT;
12660 }
12661
12662 #endif /* HAVE_X11R6 */
12663
12664 #ifdef HAVE_X11R6
12665 /* This isn't prototyped in OSF 5.0 or 5.1a. */
12666 extern char *XSetIMValues P_ ((XIM, ...));
12667 #endif
12668
12669 /* Open the connection to the XIM server on display DPYINFO.
12670 RESOURCE_NAME is the resource name Emacs uses. */
12671
12672 static void
12673 xim_open_dpy (dpyinfo, resource_name)
12674 struct x_display_info *dpyinfo;
12675 char *resource_name;
12676 {
12677 #ifdef USE_XIM
12678 XIM xim;
12679
12680 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
12681 dpyinfo->xim = xim;
12682
12683 if (xim)
12684 {
12685 #ifdef HAVE_X11R6
12686 XIMCallback destroy;
12687 #endif
12688
12689 /* Get supported styles and XIM values. */
12690 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
12691
12692 #ifdef HAVE_X11R6
12693 destroy.callback = xim_destroy_callback;
12694 destroy.client_data = (XPointer)dpyinfo;
12695 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
12696 #endif
12697 }
12698
12699 #else /* not USE_XIM */
12700 dpyinfo->xim = NULL;
12701 #endif /* not USE_XIM */
12702 }
12703
12704
12705 #ifdef HAVE_X11R6_XIM
12706
12707 struct xim_inst_t
12708 {
12709 struct x_display_info *dpyinfo;
12710 char *resource_name;
12711 };
12712
12713 /* XIM instantiate callback function, which is called whenever an XIM
12714 server is available. DISPLAY is teh display of the XIM.
12715 CLIENT_DATA contains a pointer to an xim_inst_t structure created
12716 when the callback was registered. */
12717
12718 static void
12719 xim_instantiate_callback (display, client_data, call_data)
12720 Display *display;
12721 XPointer client_data;
12722 XPointer call_data;
12723 {
12724 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
12725 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
12726
12727 /* We don't support multiple XIM connections. */
12728 if (dpyinfo->xim)
12729 return;
12730
12731 xim_open_dpy (dpyinfo, xim_inst->resource_name);
12732
12733 /* Create XIC for the existing frames on the same display, as long
12734 as they have no XIC. */
12735 if (dpyinfo->xim && dpyinfo->reference_count > 0)
12736 {
12737 Lisp_Object tail, frame;
12738
12739 BLOCK_INPUT;
12740 FOR_EACH_FRAME (tail, frame)
12741 {
12742 struct frame *f = XFRAME (frame);
12743
12744 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
12745 if (FRAME_XIC (f) == NULL)
12746 {
12747 create_frame_xic (f);
12748 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
12749 xic_set_statusarea (f);
12750 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
12751 {
12752 struct window *w = XWINDOW (f->selected_window);
12753 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
12754 }
12755 }
12756 }
12757
12758 UNBLOCK_INPUT;
12759 }
12760 }
12761
12762 #endif /* HAVE_X11R6_XIM */
12763
12764
12765 /* Open a connection to the XIM server on display DPYINFO.
12766 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
12767 connection only at the first time. On X11R6, open the connection
12768 in the XIM instantiate callback function. */
12769
12770 static void
12771 xim_initialize (dpyinfo, resource_name)
12772 struct x_display_info *dpyinfo;
12773 char *resource_name;
12774 {
12775 #ifdef USE_XIM
12776 #ifdef HAVE_X11R6_XIM
12777 struct xim_inst_t *xim_inst;
12778 int len;
12779
12780 dpyinfo->xim = NULL;
12781 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
12782 xim_inst->dpyinfo = dpyinfo;
12783 len = strlen (resource_name);
12784 xim_inst->resource_name = (char *) xmalloc (len + 1);
12785 bcopy (resource_name, xim_inst->resource_name, len + 1);
12786 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12787 resource_name, EMACS_CLASS,
12788 xim_instantiate_callback,
12789 /* Fixme: This is XPointer in
12790 XFree86 but (XPointer *) on
12791 Tru64, at least. */
12792 (XPointer) xim_inst);
12793 #else /* not HAVE_X11R6_XIM */
12794 dpyinfo->xim = NULL;
12795 xim_open_dpy (dpyinfo, resource_name);
12796 #endif /* not HAVE_X11R6_XIM */
12797
12798 #else /* not USE_XIM */
12799 dpyinfo->xim = NULL;
12800 #endif /* not USE_XIM */
12801 }
12802
12803
12804 /* Close the connection to the XIM server on display DPYINFO. */
12805
12806 static void
12807 xim_close_dpy (dpyinfo)
12808 struct x_display_info *dpyinfo;
12809 {
12810 #ifdef USE_XIM
12811 #ifdef HAVE_X11R6_XIM
12812 if (dpyinfo->display)
12813 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12814 NULL, EMACS_CLASS,
12815 xim_instantiate_callback, NULL);
12816 #endif /* not HAVE_X11R6_XIM */
12817 if (dpyinfo->display)
12818 XCloseIM (dpyinfo->xim);
12819 dpyinfo->xim = NULL;
12820 XFree (dpyinfo->xim_styles);
12821 #endif /* USE_XIM */
12822 }
12823
12824 #endif /* not HAVE_X11R6_XIM */
12825
12826
12827 \f
12828 /* Calculate the absolute position in frame F
12829 from its current recorded position values and gravity. */
12830
12831 void
12832 x_calc_absolute_position (f)
12833 struct frame *f;
12834 {
12835 Window child;
12836 int win_x = 0, win_y = 0;
12837 int flags = f->output_data.x->size_hint_flags;
12838 int this_window;
12839
12840 /* We have nothing to do if the current position
12841 is already for the top-left corner. */
12842 if (! ((flags & XNegative) || (flags & YNegative)))
12843 return;
12844
12845 #ifdef USE_X_TOOLKIT
12846 this_window = XtWindow (f->output_data.x->widget);
12847 #else
12848 this_window = FRAME_X_WINDOW (f);
12849 #endif
12850
12851 /* Find the position of the outside upper-left corner of
12852 the inner window, with respect to the outer window.
12853 But do this only if we will need the results. */
12854 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
12855 {
12856 int count;
12857
12858 BLOCK_INPUT;
12859 count = x_catch_errors (FRAME_X_DISPLAY (f));
12860 while (1)
12861 {
12862 x_clear_errors (FRAME_X_DISPLAY (f));
12863 XTranslateCoordinates (FRAME_X_DISPLAY (f),
12864
12865 /* From-window, to-window. */
12866 this_window,
12867 f->output_data.x->parent_desc,
12868
12869 /* From-position, to-position. */
12870 0, 0, &win_x, &win_y,
12871
12872 /* Child of win. */
12873 &child);
12874 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12875 {
12876 Window newroot, newparent = 0xdeadbeef;
12877 Window *newchildren;
12878 unsigned int nchildren;
12879
12880 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
12881 &newparent, &newchildren, &nchildren))
12882 break;
12883
12884 XFree ((char *) newchildren);
12885
12886 f->output_data.x->parent_desc = newparent;
12887 }
12888 else
12889 break;
12890 }
12891
12892 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12893 UNBLOCK_INPUT;
12894 }
12895
12896 /* Treat negative positions as relative to the leftmost bottommost
12897 position that fits on the screen. */
12898 if (flags & XNegative)
12899 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
12900 - 2 * f->output_data.x->border_width - win_x
12901 - PIXEL_WIDTH (f)
12902 + f->output_data.x->left_pos);
12903
12904 {
12905 int height = PIXEL_HEIGHT (f);
12906
12907 #if defined USE_X_TOOLKIT && defined USE_MOTIF
12908 /* Something is fishy here. When using Motif, starting Emacs with
12909 `-g -0-0', the frame appears too low by a few pixels.
12910
12911 This seems to be so because initially, while Emacs is starting,
12912 the column widget's height and the frame's pixel height are
12913 different. The column widget's height is the right one. In
12914 later invocations, when Emacs is up, the frame's pixel height
12915 is right, though.
12916
12917 It's not obvious where the initial small difference comes from.
12918 2000-12-01, gerd. */
12919
12920 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
12921 #endif
12922
12923 if (flags & YNegative)
12924 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
12925 - 2 * f->output_data.x->border_width
12926 - win_y
12927 - height
12928 + f->output_data.x->top_pos);
12929 }
12930
12931 /* The left_pos and top_pos
12932 are now relative to the top and left screen edges,
12933 so the flags should correspond. */
12934 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12935 }
12936
12937 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
12938 to really change the position, and 0 when calling from
12939 x_make_frame_visible (in that case, XOFF and YOFF are the current
12940 position values). It is -1 when calling from x_set_frame_parameters,
12941 which means, do adjust for borders but don't change the gravity. */
12942
12943 void
12944 x_set_offset (f, xoff, yoff, change_gravity)
12945 struct frame *f;
12946 register int xoff, yoff;
12947 int change_gravity;
12948 {
12949 int modified_top, modified_left;
12950
12951 if (change_gravity > 0)
12952 {
12953 f->output_data.x->top_pos = yoff;
12954 f->output_data.x->left_pos = xoff;
12955 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12956 if (xoff < 0)
12957 f->output_data.x->size_hint_flags |= XNegative;
12958 if (yoff < 0)
12959 f->output_data.x->size_hint_flags |= YNegative;
12960 f->output_data.x->win_gravity = NorthWestGravity;
12961 }
12962 x_calc_absolute_position (f);
12963
12964 BLOCK_INPUT;
12965 x_wm_set_size_hint (f, (long) 0, 0);
12966
12967 modified_left = f->output_data.x->left_pos;
12968 modified_top = f->output_data.x->top_pos;
12969 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
12970 this seems to be unnecessary and incorrect. rms, 4/17/97. */
12971 /* It is a mystery why we need to add the border_width here
12972 when the frame is already visible, but experiment says we do. */
12973 if (change_gravity != 0)
12974 {
12975 modified_left += f->output_data.x->border_width;
12976 modified_top += f->output_data.x->border_width;
12977 }
12978 #endif
12979
12980 #ifdef USE_X_TOOLKIT
12981 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
12982 modified_left, modified_top);
12983 #else /* not USE_X_TOOLKIT */
12984 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12985 modified_left, modified_top);
12986 #endif /* not USE_X_TOOLKIT */
12987 UNBLOCK_INPUT;
12988 }
12989
12990 /* Check if we need to resize the frame due to a fullscreen request.
12991 If so needed, resize the frame. */
12992 static void
12993 x_check_fullscreen (f)
12994 struct frame *f;
12995 {
12996 if (f->output_data.x->want_fullscreen & FULLSCREEN_BOTH)
12997 {
12998 int width, height, ign;
12999
13000 x_real_positions (f, &f->output_data.x->left_pos,
13001 &f->output_data.x->top_pos);
13002
13003 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
13004
13005 /* We do not need to move the window, it shall be taken care of
13006 when setting WM manager hints.
13007 If the frame is visible already, the position is checked by
13008 x_check_fullscreen_move. */
13009 if (f->width != width || f->height != height)
13010 {
13011 change_frame_size (f, height, width, 0, 1, 0);
13012 SET_FRAME_GARBAGED (f);
13013 cancel_mouse_face (f);
13014
13015 /* Wait for the change of frame size to occur */
13016 f->output_data.x->want_fullscreen |= FULLSCREEN_WAIT;
13017
13018 }
13019 }
13020 }
13021
13022 /* If frame parameters are set after the frame is mapped, we need to move
13023 the window. This is done in xfns.c.
13024 Some window managers moves the window to the right position, some
13025 moves the outer window manager window to the specified position.
13026 Here we check that we are in the right spot. If not, make a second
13027 move, assuming we are dealing with the second kind of window manager. */
13028 static void
13029 x_check_fullscreen_move (f)
13030 struct frame *f;
13031 {
13032 if (f->output_data.x->want_fullscreen & FULLSCREEN_MOVE_WAIT)
13033 {
13034 int expect_top = f->output_data.x->top_pos;
13035 int expect_left = f->output_data.x->left_pos;
13036
13037 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
13038 expect_top = 0;
13039 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
13040 expect_left = 0;
13041
13042 if (expect_top != f->output_data.x->top_pos
13043 || expect_left != f->output_data.x->left_pos)
13044 x_set_offset (f, expect_left, expect_top, 1);
13045
13046 /* Just do this once */
13047 f->output_data.x->want_fullscreen &= ~FULLSCREEN_MOVE_WAIT;
13048 }
13049 }
13050
13051
13052 /* Calculate fullscreen size. Return in *TOP_POS and *LEFT_POS the
13053 wanted positions of the WM window (not emacs window).
13054 Return in *WIDTH and *HEIGHT the wanted width and height of Emacs
13055 window (FRAME_X_WINDOW).
13056 */
13057 void
13058 x_fullscreen_adjust (f, width, height, top_pos, left_pos)
13059 struct frame *f;
13060 int *width;
13061 int *height;
13062 int *top_pos;
13063 int *left_pos;
13064 {
13065 int newwidth = f->width, newheight = f->height;
13066
13067 *top_pos = f->output_data.x->top_pos;
13068 *left_pos = f->output_data.x->left_pos;
13069
13070 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
13071 {
13072 int ph;
13073
13074 ph = FRAME_X_DISPLAY_INFO (f)->height;
13075 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
13076 ph = CHAR_TO_PIXEL_HEIGHT (f, newheight)
13077 - f->output_data.x->y_pixels_diff;
13078 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
13079 *top_pos = 0;
13080 }
13081
13082 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
13083 {
13084 int pw;
13085
13086 pw = FRAME_X_DISPLAY_INFO (f)->width;
13087 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
13088 pw = CHAR_TO_PIXEL_WIDTH (f, newwidth)
13089 - f->output_data.x->x_pixels_diff;
13090 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
13091 *left_pos = 0;
13092 }
13093
13094 *width = newwidth;
13095 *height = newheight;
13096 }
13097
13098
13099 /* Change the size of frame F's X window to COLS/ROWS in the case F
13100 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
13101 top-left-corner window gravity for this size change and subsequent
13102 size changes. Otherwise we leave the window gravity unchanged. */
13103
13104 static void
13105 x_set_window_size_1 (f, change_gravity, cols, rows)
13106 struct frame *f;
13107 int change_gravity;
13108 int cols, rows;
13109 {
13110 int pixelwidth, pixelheight;
13111
13112 check_frame_size (f, &rows, &cols);
13113 f->output_data.x->vertical_scroll_bar_extra
13114 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
13115 ? 0
13116 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
13117 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
13118 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
13119
13120 x_compute_fringe_widths (f, 0);
13121
13122 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
13123 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
13124
13125 f->output_data.x->win_gravity = NorthWestGravity;
13126 x_wm_set_size_hint (f, (long) 0, 0);
13127
13128 XSync (FRAME_X_DISPLAY (f), False);
13129 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
13130 pixelwidth, pixelheight);
13131
13132 /* Now, strictly speaking, we can't be sure that this is accurate,
13133 but the window manager will get around to dealing with the size
13134 change request eventually, and we'll hear how it went when the
13135 ConfigureNotify event gets here.
13136
13137 We could just not bother storing any of this information here,
13138 and let the ConfigureNotify event set everything up, but that
13139 might be kind of confusing to the Lisp code, since size changes
13140 wouldn't be reported in the frame parameters until some random
13141 point in the future when the ConfigureNotify event arrives.
13142
13143 We pass 1 for DELAY since we can't run Lisp code inside of
13144 a BLOCK_INPUT. */
13145 change_frame_size (f, rows, cols, 0, 1, 0);
13146 PIXEL_WIDTH (f) = pixelwidth;
13147 PIXEL_HEIGHT (f) = pixelheight;
13148
13149 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
13150 receive in the ConfigureNotify event; if we get what we asked
13151 for, then the event won't cause the screen to become garbaged, so
13152 we have to make sure to do it here. */
13153 SET_FRAME_GARBAGED (f);
13154
13155 XFlush (FRAME_X_DISPLAY (f));
13156 }
13157
13158
13159 /* Call this to change the size of frame F's x-window.
13160 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
13161 for this size change and subsequent size changes.
13162 Otherwise we leave the window gravity unchanged. */
13163
13164 void
13165 x_set_window_size (f, change_gravity, cols, rows)
13166 struct frame *f;
13167 int change_gravity;
13168 int cols, rows;
13169 {
13170 BLOCK_INPUT;
13171
13172 #ifdef USE_X_TOOLKIT
13173
13174 if (f->output_data.x->widget != NULL)
13175 {
13176 /* The x and y position of the widget is clobbered by the
13177 call to XtSetValues within EmacsFrameSetCharSize.
13178 This is a real kludge, but I don't understand Xt so I can't
13179 figure out a correct fix. Can anyone else tell me? -- rms. */
13180 int xpos = f->output_data.x->widget->core.x;
13181 int ypos = f->output_data.x->widget->core.y;
13182 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
13183 f->output_data.x->widget->core.x = xpos;
13184 f->output_data.x->widget->core.y = ypos;
13185 }
13186 else
13187 x_set_window_size_1 (f, change_gravity, cols, rows);
13188
13189 #else /* not USE_X_TOOLKIT */
13190
13191 x_set_window_size_1 (f, change_gravity, cols, rows);
13192
13193 #endif /* not USE_X_TOOLKIT */
13194
13195 /* If cursor was outside the new size, mark it as off. */
13196 mark_window_cursors_off (XWINDOW (f->root_window));
13197
13198 /* Clear out any recollection of where the mouse highlighting was,
13199 since it might be in a place that's outside the new frame size.
13200 Actually checking whether it is outside is a pain in the neck,
13201 so don't try--just let the highlighting be done afresh with new size. */
13202 cancel_mouse_face (f);
13203
13204 UNBLOCK_INPUT;
13205 }
13206 \f
13207 /* Mouse warping. */
13208
13209 void
13210 x_set_mouse_position (f, x, y)
13211 struct frame *f;
13212 int x, y;
13213 {
13214 int pix_x, pix_y;
13215
13216 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
13217 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
13218
13219 if (pix_x < 0) pix_x = 0;
13220 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
13221
13222 if (pix_y < 0) pix_y = 0;
13223 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
13224
13225 BLOCK_INPUT;
13226
13227 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13228 0, 0, 0, 0, pix_x, pix_y);
13229 UNBLOCK_INPUT;
13230 }
13231
13232 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
13233
13234 void
13235 x_set_mouse_pixel_position (f, pix_x, pix_y)
13236 struct frame *f;
13237 int pix_x, pix_y;
13238 {
13239 BLOCK_INPUT;
13240
13241 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13242 0, 0, 0, 0, pix_x, pix_y);
13243 UNBLOCK_INPUT;
13244 }
13245 \f
13246 /* focus shifting, raising and lowering. */
13247
13248 void
13249 x_focus_on_frame (f)
13250 struct frame *f;
13251 {
13252 #if 0 /* This proves to be unpleasant. */
13253 x_raise_frame (f);
13254 #endif
13255 #if 0
13256 /* I don't think that the ICCCM allows programs to do things like this
13257 without the interaction of the window manager. Whatever you end up
13258 doing with this code, do it to x_unfocus_frame too. */
13259 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
13260 RevertToPointerRoot, CurrentTime);
13261 #endif /* ! 0 */
13262 }
13263
13264 void
13265 x_unfocus_frame (f)
13266 struct frame *f;
13267 {
13268 #if 0
13269 /* Look at the remarks in x_focus_on_frame. */
13270 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
13271 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
13272 RevertToPointerRoot, CurrentTime);
13273 #endif /* ! 0 */
13274 }
13275
13276 /* Raise frame F. */
13277
13278 void
13279 x_raise_frame (f)
13280 struct frame *f;
13281 {
13282 if (f->async_visible)
13283 {
13284 BLOCK_INPUT;
13285 #ifdef USE_X_TOOLKIT
13286 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
13287 #else /* not USE_X_TOOLKIT */
13288 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13289 #endif /* not USE_X_TOOLKIT */
13290 XFlush (FRAME_X_DISPLAY (f));
13291 UNBLOCK_INPUT;
13292 }
13293 }
13294
13295 /* Lower frame F. */
13296
13297 void
13298 x_lower_frame (f)
13299 struct frame *f;
13300 {
13301 if (f->async_visible)
13302 {
13303 BLOCK_INPUT;
13304 #ifdef USE_X_TOOLKIT
13305 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
13306 #else /* not USE_X_TOOLKIT */
13307 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13308 #endif /* not USE_X_TOOLKIT */
13309 XFlush (FRAME_X_DISPLAY (f));
13310 UNBLOCK_INPUT;
13311 }
13312 }
13313
13314 static void
13315 XTframe_raise_lower (f, raise_flag)
13316 FRAME_PTR f;
13317 int raise_flag;
13318 {
13319 if (raise_flag)
13320 x_raise_frame (f);
13321 else
13322 x_lower_frame (f);
13323 }
13324 \f
13325 /* Change of visibility. */
13326
13327 /* This tries to wait until the frame is really visible.
13328 However, if the window manager asks the user where to position
13329 the frame, this will return before the user finishes doing that.
13330 The frame will not actually be visible at that time,
13331 but it will become visible later when the window manager
13332 finishes with it. */
13333
13334 void
13335 x_make_frame_visible (f)
13336 struct frame *f;
13337 {
13338 Lisp_Object type;
13339 int original_top, original_left;
13340 int retry_count = 2;
13341
13342 retry:
13343
13344 BLOCK_INPUT;
13345
13346 type = x_icon_type (f);
13347 if (!NILP (type))
13348 x_bitmap_icon (f, type);
13349
13350 if (! FRAME_VISIBLE_P (f))
13351 {
13352 /* We test FRAME_GARBAGED_P here to make sure we don't
13353 call x_set_offset a second time
13354 if we get to x_make_frame_visible a second time
13355 before the window gets really visible. */
13356 if (! FRAME_ICONIFIED_P (f)
13357 && ! f->output_data.x->asked_for_visible)
13358 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13359
13360 f->output_data.x->asked_for_visible = 1;
13361
13362 if (! EQ (Vx_no_window_manager, Qt))
13363 x_wm_set_window_state (f, NormalState);
13364 #ifdef USE_X_TOOLKIT
13365 /* This was XtPopup, but that did nothing for an iconified frame. */
13366 XtMapWidget (f->output_data.x->widget);
13367 #else /* not USE_X_TOOLKIT */
13368 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13369 #endif /* not USE_X_TOOLKIT */
13370 #if 0 /* This seems to bring back scroll bars in the wrong places
13371 if the window configuration has changed. They seem
13372 to come back ok without this. */
13373 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
13374 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13375 #endif
13376 }
13377
13378 XFlush (FRAME_X_DISPLAY (f));
13379
13380 /* Synchronize to ensure Emacs knows the frame is visible
13381 before we do anything else. We do this loop with input not blocked
13382 so that incoming events are handled. */
13383 {
13384 Lisp_Object frame;
13385 int count;
13386 /* This must be before UNBLOCK_INPUT
13387 since events that arrive in response to the actions above
13388 will set it when they are handled. */
13389 int previously_visible = f->output_data.x->has_been_visible;
13390
13391 original_left = f->output_data.x->left_pos;
13392 original_top = f->output_data.x->top_pos;
13393
13394 /* This must come after we set COUNT. */
13395 UNBLOCK_INPUT;
13396
13397 /* We unblock here so that arriving X events are processed. */
13398
13399 /* Now move the window back to where it was "supposed to be".
13400 But don't do it if the gravity is negative.
13401 When the gravity is negative, this uses a position
13402 that is 3 pixels too low. Perhaps that's really the border width.
13403
13404 Don't do this if the window has never been visible before,
13405 because the window manager may choose the position
13406 and we don't want to override it. */
13407
13408 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
13409 && f->output_data.x->win_gravity == NorthWestGravity
13410 && previously_visible)
13411 {
13412 Drawable rootw;
13413 int x, y;
13414 unsigned int width, height, border, depth;
13415
13416 BLOCK_INPUT;
13417
13418 /* On some window managers (such as FVWM) moving an existing
13419 window, even to the same place, causes the window manager
13420 to introduce an offset. This can cause the window to move
13421 to an unexpected location. Check the geometry (a little
13422 slow here) and then verify that the window is in the right
13423 place. If the window is not in the right place, move it
13424 there, and take the potential window manager hit. */
13425 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13426 &rootw, &x, &y, &width, &height, &border, &depth);
13427
13428 if (original_left != x || original_top != y)
13429 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13430 original_left, original_top);
13431
13432 UNBLOCK_INPUT;
13433 }
13434
13435 XSETFRAME (frame, f);
13436
13437 /* Wait until the frame is visible. Process X events until a
13438 MapNotify event has been seen, or until we think we won't get a
13439 MapNotify at all.. */
13440 for (count = input_signal_count + 10;
13441 input_signal_count < count && !FRAME_VISIBLE_P (f);)
13442 {
13443 /* Force processing of queued events. */
13444 x_sync (f);
13445
13446 /* Machines that do polling rather than SIGIO have been
13447 observed to go into a busy-wait here. So we'll fake an
13448 alarm signal to let the handler know that there's something
13449 to be read. We used to raise a real alarm, but it seems
13450 that the handler isn't always enabled here. This is
13451 probably a bug. */
13452 if (input_polling_used ())
13453 {
13454 /* It could be confusing if a real alarm arrives while
13455 processing the fake one. Turn it off and let the
13456 handler reset it. */
13457 extern void poll_for_input_1 P_ ((void));
13458 int old_poll_suppress_count = poll_suppress_count;
13459 poll_suppress_count = 1;
13460 poll_for_input_1 ();
13461 poll_suppress_count = old_poll_suppress_count;
13462 }
13463
13464 /* See if a MapNotify event has been processed. */
13465 FRAME_SAMPLE_VISIBILITY (f);
13466 }
13467
13468 /* 2000-09-28: In
13469
13470 (let ((f (selected-frame)))
13471 (iconify-frame f)
13472 (raise-frame f))
13473
13474 the frame is not raised with various window managers on
13475 FreeBSD, Linux and Solaris. It turns out that, for some
13476 unknown reason, the call to XtMapWidget is completely ignored.
13477 Mapping the widget a second time works. */
13478
13479 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
13480 goto retry;
13481 }
13482 }
13483
13484 /* Change from mapped state to withdrawn state. */
13485
13486 /* Make the frame visible (mapped and not iconified). */
13487
13488 void
13489 x_make_frame_invisible (f)
13490 struct frame *f;
13491 {
13492 Window window;
13493
13494 #ifdef USE_X_TOOLKIT
13495 /* Use the frame's outermost window, not the one we normally draw on. */
13496 window = XtWindow (f->output_data.x->widget);
13497 #else /* not USE_X_TOOLKIT */
13498 window = FRAME_X_WINDOW (f);
13499 #endif /* not USE_X_TOOLKIT */
13500
13501 /* Don't keep the highlight on an invisible frame. */
13502 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13503 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
13504
13505 #if 0/* This might add unreliability; I don't trust it -- rms. */
13506 if (! f->async_visible && ! f->async_iconified)
13507 return;
13508 #endif
13509
13510 BLOCK_INPUT;
13511
13512 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
13513 that the current position of the window is user-specified, rather than
13514 program-specified, so that when the window is mapped again, it will be
13515 placed at the same location, without forcing the user to position it
13516 by hand again (they have already done that once for this window.) */
13517 x_wm_set_size_hint (f, (long) 0, 1);
13518
13519 #ifdef HAVE_X11R4
13520
13521 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
13522 DefaultScreen (FRAME_X_DISPLAY (f))))
13523 {
13524 UNBLOCK_INPUT_RESIGNAL;
13525 error ("Can't notify window manager of window withdrawal");
13526 }
13527 #else /* ! defined (HAVE_X11R4) */
13528
13529 /* Tell the window manager what we're going to do. */
13530 if (! EQ (Vx_no_window_manager, Qt))
13531 {
13532 XEvent unmap;
13533
13534 unmap.xunmap.type = UnmapNotify;
13535 unmap.xunmap.window = window;
13536 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
13537 unmap.xunmap.from_configure = False;
13538 if (! XSendEvent (FRAME_X_DISPLAY (f),
13539 DefaultRootWindow (FRAME_X_DISPLAY (f)),
13540 False,
13541 SubstructureRedirectMaskSubstructureNotifyMask,
13542 &unmap))
13543 {
13544 UNBLOCK_INPUT_RESIGNAL;
13545 error ("Can't notify window manager of withdrawal");
13546 }
13547 }
13548
13549 /* Unmap the window ourselves. Cheeky! */
13550 XUnmapWindow (FRAME_X_DISPLAY (f), window);
13551 #endif /* ! defined (HAVE_X11R4) */
13552
13553 /* We can't distinguish this from iconification
13554 just by the event that we get from the server.
13555 So we can't win using the usual strategy of letting
13556 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
13557 and synchronize with the server to make sure we agree. */
13558 f->visible = 0;
13559 FRAME_ICONIFIED_P (f) = 0;
13560 f->async_visible = 0;
13561 f->async_iconified = 0;
13562
13563 x_sync (f);
13564
13565 UNBLOCK_INPUT;
13566 }
13567
13568 /* Change window state from mapped to iconified. */
13569
13570 void
13571 x_iconify_frame (f)
13572 struct frame *f;
13573 {
13574 int result;
13575 Lisp_Object type;
13576
13577 /* Don't keep the highlight on an invisible frame. */
13578 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13579 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
13580
13581 if (f->async_iconified)
13582 return;
13583
13584 BLOCK_INPUT;
13585
13586 FRAME_SAMPLE_VISIBILITY (f);
13587
13588 type = x_icon_type (f);
13589 if (!NILP (type))
13590 x_bitmap_icon (f, type);
13591
13592 #ifdef USE_X_TOOLKIT
13593
13594 if (! FRAME_VISIBLE_P (f))
13595 {
13596 if (! EQ (Vx_no_window_manager, Qt))
13597 x_wm_set_window_state (f, IconicState);
13598 /* This was XtPopup, but that did nothing for an iconified frame. */
13599 XtMapWidget (f->output_data.x->widget);
13600 /* The server won't give us any event to indicate
13601 that an invisible frame was changed to an icon,
13602 so we have to record it here. */
13603 f->iconified = 1;
13604 f->visible = 1;
13605 f->async_iconified = 1;
13606 f->async_visible = 0;
13607 UNBLOCK_INPUT;
13608 return;
13609 }
13610
13611 result = XIconifyWindow (FRAME_X_DISPLAY (f),
13612 XtWindow (f->output_data.x->widget),
13613 DefaultScreen (FRAME_X_DISPLAY (f)));
13614 UNBLOCK_INPUT;
13615
13616 if (!result)
13617 error ("Can't notify window manager of iconification");
13618
13619 f->async_iconified = 1;
13620 f->async_visible = 0;
13621
13622
13623 BLOCK_INPUT;
13624 XFlush (FRAME_X_DISPLAY (f));
13625 UNBLOCK_INPUT;
13626 #else /* not USE_X_TOOLKIT */
13627
13628 /* Make sure the X server knows where the window should be positioned,
13629 in case the user deiconifies with the window manager. */
13630 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
13631 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13632
13633 /* Since we don't know which revision of X we're running, we'll use both
13634 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
13635
13636 /* X11R4: send a ClientMessage to the window manager using the
13637 WM_CHANGE_STATE type. */
13638 {
13639 XEvent message;
13640
13641 message.xclient.window = FRAME_X_WINDOW (f);
13642 message.xclient.type = ClientMessage;
13643 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
13644 message.xclient.format = 32;
13645 message.xclient.data.l[0] = IconicState;
13646
13647 if (! XSendEvent (FRAME_X_DISPLAY (f),
13648 DefaultRootWindow (FRAME_X_DISPLAY (f)),
13649 False,
13650 SubstructureRedirectMask | SubstructureNotifyMask,
13651 &message))
13652 {
13653 UNBLOCK_INPUT_RESIGNAL;
13654 error ("Can't notify window manager of iconification");
13655 }
13656 }
13657
13658 /* X11R3: set the initial_state field of the window manager hints to
13659 IconicState. */
13660 x_wm_set_window_state (f, IconicState);
13661
13662 if (!FRAME_VISIBLE_P (f))
13663 {
13664 /* If the frame was withdrawn, before, we must map it. */
13665 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13666 }
13667
13668 f->async_iconified = 1;
13669 f->async_visible = 0;
13670
13671 XFlush (FRAME_X_DISPLAY (f));
13672 UNBLOCK_INPUT;
13673 #endif /* not USE_X_TOOLKIT */
13674 }
13675
13676 \f
13677 /* Free X resources of frame F. */
13678
13679 void
13680 x_free_frame_resources (f)
13681 struct frame *f;
13682 {
13683 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13684 Lisp_Object bar;
13685 struct scroll_bar *b;
13686
13687 BLOCK_INPUT;
13688
13689 /* If a display connection is dead, don't try sending more
13690 commands to the X server. */
13691 if (dpyinfo->display)
13692 {
13693 if (f->output_data.x->icon_desc)
13694 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
13695
13696 #ifdef USE_X_TOOLKIT
13697 /* Explicitly destroy the scroll bars of the frame. Without
13698 this, we get "BadDrawable" errors from the toolkit later on,
13699 presumably from expose events generated for the disappearing
13700 toolkit scroll bars. */
13701 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
13702 {
13703 b = XSCROLL_BAR (bar);
13704 x_scroll_bar_remove (b);
13705 }
13706 #endif
13707
13708 #ifdef HAVE_X_I18N
13709 if (FRAME_XIC (f))
13710 free_frame_xic (f);
13711 #endif
13712
13713 #ifdef USE_X_TOOLKIT
13714 if (f->output_data.x->widget)
13715 {
13716 XtDestroyWidget (f->output_data.x->widget);
13717 f->output_data.x->widget = NULL;
13718 }
13719 /* Tooltips don't have widgets, only a simple X window, even if
13720 we are using a toolkit. */
13721 else if (FRAME_X_WINDOW (f))
13722 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13723
13724 free_frame_menubar (f);
13725 #else /* !USE_X_TOOLKIT */
13726 if (FRAME_X_WINDOW (f))
13727 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13728 #endif /* !USE_X_TOOLKIT */
13729
13730 unload_color (f, f->output_data.x->foreground_pixel);
13731 unload_color (f, f->output_data.x->background_pixel);
13732 unload_color (f, f->output_data.x->cursor_pixel);
13733 unload_color (f, f->output_data.x->cursor_foreground_pixel);
13734 unload_color (f, f->output_data.x->border_pixel);
13735 unload_color (f, f->output_data.x->mouse_pixel);
13736
13737 if (f->output_data.x->scroll_bar_background_pixel != -1)
13738 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
13739 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
13740 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
13741 #ifdef USE_TOOLKIT_SCROLL_BARS
13742 /* Scrollbar shadow colors. */
13743 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
13744 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
13745 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
13746 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
13747 #endif /* USE_TOOLKIT_SCROLL_BARS */
13748 if (f->output_data.x->white_relief.allocated_p)
13749 unload_color (f, f->output_data.x->white_relief.pixel);
13750 if (f->output_data.x->black_relief.allocated_p)
13751 unload_color (f, f->output_data.x->black_relief.pixel);
13752
13753 if (FRAME_FACE_CACHE (f))
13754 free_frame_faces (f);
13755
13756 x_free_gcs (f);
13757 XFlush (FRAME_X_DISPLAY (f));
13758 }
13759
13760 if (f->output_data.x->saved_menu_event)
13761 xfree (f->output_data.x->saved_menu_event);
13762
13763 xfree (f->output_data.x);
13764 f->output_data.x = NULL;
13765
13766 if (f == dpyinfo->x_focus_frame)
13767 dpyinfo->x_focus_frame = 0;
13768 if (f == dpyinfo->x_focus_event_frame)
13769 dpyinfo->x_focus_event_frame = 0;
13770 if (f == dpyinfo->x_highlight_frame)
13771 dpyinfo->x_highlight_frame = 0;
13772
13773 if (f == dpyinfo->mouse_face_mouse_frame)
13774 {
13775 dpyinfo->mouse_face_beg_row
13776 = dpyinfo->mouse_face_beg_col = -1;
13777 dpyinfo->mouse_face_end_row
13778 = dpyinfo->mouse_face_end_col = -1;
13779 dpyinfo->mouse_face_window = Qnil;
13780 dpyinfo->mouse_face_deferred_gc = 0;
13781 dpyinfo->mouse_face_mouse_frame = 0;
13782 }
13783
13784 UNBLOCK_INPUT;
13785 }
13786
13787
13788 /* Destroy the X window of frame F. */
13789
13790 void
13791 x_destroy_window (f)
13792 struct frame *f;
13793 {
13794 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13795
13796 /* If a display connection is dead, don't try sending more
13797 commands to the X server. */
13798 if (dpyinfo->display != 0)
13799 x_free_frame_resources (f);
13800
13801 dpyinfo->reference_count--;
13802 }
13803
13804 \f
13805 /* Setting window manager hints. */
13806
13807 /* Set the normal size hints for the window manager, for frame F.
13808 FLAGS is the flags word to use--or 0 meaning preserve the flags
13809 that the window now has.
13810 If USER_POSITION is nonzero, we set the USPosition
13811 flag (this is useful when FLAGS is 0). */
13812
13813 void
13814 x_wm_set_size_hint (f, flags, user_position)
13815 struct frame *f;
13816 long flags;
13817 int user_position;
13818 {
13819 XSizeHints size_hints;
13820
13821 #ifdef USE_X_TOOLKIT
13822 Arg al[2];
13823 int ac = 0;
13824 Dimension widget_width, widget_height;
13825 Window window = XtWindow (f->output_data.x->widget);
13826 #else /* not USE_X_TOOLKIT */
13827 Window window = FRAME_X_WINDOW (f);
13828 #endif /* not USE_X_TOOLKIT */
13829
13830 /* Setting PMaxSize caused various problems. */
13831 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
13832
13833 size_hints.x = f->output_data.x->left_pos;
13834 size_hints.y = f->output_data.x->top_pos;
13835
13836 #ifdef USE_X_TOOLKIT
13837 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
13838 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
13839 XtGetValues (f->output_data.x->widget, al, ac);
13840 size_hints.height = widget_height;
13841 size_hints.width = widget_width;
13842 #else /* not USE_X_TOOLKIT */
13843 size_hints.height = PIXEL_HEIGHT (f);
13844 size_hints.width = PIXEL_WIDTH (f);
13845 #endif /* not USE_X_TOOLKIT */
13846
13847 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
13848 size_hints.height_inc = f->output_data.x->line_height;
13849 size_hints.max_width
13850 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
13851 size_hints.max_height
13852 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
13853
13854 /* Calculate the base and minimum sizes.
13855
13856 (When we use the X toolkit, we don't do it here.
13857 Instead we copy the values that the widgets are using, below.) */
13858 #ifndef USE_X_TOOLKIT
13859 {
13860 int base_width, base_height;
13861 int min_rows = 0, min_cols = 0;
13862
13863 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
13864 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
13865
13866 check_frame_size (f, &min_rows, &min_cols);
13867
13868 /* The window manager uses the base width hints to calculate the
13869 current number of rows and columns in the frame while
13870 resizing; min_width and min_height aren't useful for this
13871 purpose, since they might not give the dimensions for a
13872 zero-row, zero-column frame.
13873
13874 We use the base_width and base_height members if we have
13875 them; otherwise, we set the min_width and min_height members
13876 to the size for a zero x zero frame. */
13877
13878 #ifdef HAVE_X11R4
13879 size_hints.flags |= PBaseSize;
13880 size_hints.base_width = base_width;
13881 size_hints.base_height = base_height;
13882 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
13883 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
13884 #else
13885 size_hints.min_width = base_width;
13886 size_hints.min_height = base_height;
13887 #endif
13888 }
13889
13890 /* If we don't need the old flags, we don't need the old hint at all. */
13891 if (flags)
13892 {
13893 size_hints.flags |= flags;
13894 goto no_read;
13895 }
13896 #endif /* not USE_X_TOOLKIT */
13897
13898 {
13899 XSizeHints hints; /* Sometimes I hate X Windows... */
13900 long supplied_return;
13901 int value;
13902
13903 #ifdef HAVE_X11R4
13904 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
13905 &supplied_return);
13906 #else
13907 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
13908 #endif
13909
13910 #ifdef USE_X_TOOLKIT
13911 size_hints.base_height = hints.base_height;
13912 size_hints.base_width = hints.base_width;
13913 size_hints.min_height = hints.min_height;
13914 size_hints.min_width = hints.min_width;
13915 #endif
13916
13917 if (flags)
13918 size_hints.flags |= flags;
13919 else
13920 {
13921 if (value == 0)
13922 hints.flags = 0;
13923 if (hints.flags & PSize)
13924 size_hints.flags |= PSize;
13925 if (hints.flags & PPosition)
13926 size_hints.flags |= PPosition;
13927 if (hints.flags & USPosition)
13928 size_hints.flags |= USPosition;
13929 if (hints.flags & USSize)
13930 size_hints.flags |= USSize;
13931 }
13932 }
13933
13934 #ifndef USE_X_TOOLKIT
13935 no_read:
13936 #endif
13937
13938 #ifdef PWinGravity
13939 size_hints.win_gravity = f->output_data.x->win_gravity;
13940 size_hints.flags |= PWinGravity;
13941
13942 if (user_position)
13943 {
13944 size_hints.flags &= ~ PPosition;
13945 size_hints.flags |= USPosition;
13946 }
13947 #endif /* PWinGravity */
13948
13949 #ifdef HAVE_X11R4
13950 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13951 #else
13952 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13953 #endif
13954 }
13955
13956 /* Used for IconicState or NormalState */
13957
13958 void
13959 x_wm_set_window_state (f, state)
13960 struct frame *f;
13961 int state;
13962 {
13963 #ifdef USE_X_TOOLKIT
13964 Arg al[1];
13965
13966 XtSetArg (al[0], XtNinitialState, state);
13967 XtSetValues (f->output_data.x->widget, al, 1);
13968 #else /* not USE_X_TOOLKIT */
13969 Window window = FRAME_X_WINDOW (f);
13970
13971 f->output_data.x->wm_hints.flags |= StateHint;
13972 f->output_data.x->wm_hints.initial_state = state;
13973
13974 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13975 #endif /* not USE_X_TOOLKIT */
13976 }
13977
13978 void
13979 x_wm_set_icon_pixmap (f, pixmap_id)
13980 struct frame *f;
13981 int pixmap_id;
13982 {
13983 Pixmap icon_pixmap;
13984
13985 #ifndef USE_X_TOOLKIT
13986 Window window = FRAME_X_WINDOW (f);
13987 #endif
13988
13989 if (pixmap_id > 0)
13990 {
13991 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
13992 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
13993 }
13994 else
13995 {
13996 /* It seems there is no way to turn off use of an icon pixmap.
13997 The following line does it, only if no icon has yet been created,
13998 for some window managers. But with mwm it crashes.
13999 Some people say it should clear the IconPixmapHint bit in this case,
14000 but that doesn't work, and the X consortium said it isn't the
14001 right thing at all. Since there is no way to win,
14002 best to explicitly give up. */
14003 #if 0
14004 f->output_data.x->wm_hints.icon_pixmap = None;
14005 #else
14006 return;
14007 #endif
14008 }
14009
14010 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
14011
14012 {
14013 Arg al[1];
14014 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
14015 XtSetValues (f->output_data.x->widget, al, 1);
14016 }
14017
14018 #else /* not USE_X_TOOLKIT */
14019
14020 f->output_data.x->wm_hints.flags |= IconPixmapHint;
14021 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
14022
14023 #endif /* not USE_X_TOOLKIT */
14024 }
14025
14026 void
14027 x_wm_set_icon_position (f, icon_x, icon_y)
14028 struct frame *f;
14029 int icon_x, icon_y;
14030 {
14031 #ifdef USE_X_TOOLKIT
14032 Window window = XtWindow (f->output_data.x->widget);
14033 #else
14034 Window window = FRAME_X_WINDOW (f);
14035 #endif
14036
14037 f->output_data.x->wm_hints.flags |= IconPositionHint;
14038 f->output_data.x->wm_hints.icon_x = icon_x;
14039 f->output_data.x->wm_hints.icon_y = icon_y;
14040
14041 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
14042 }
14043
14044 \f
14045 /***********************************************************************
14046 Fonts
14047 ***********************************************************************/
14048
14049 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
14050
14051 struct font_info *
14052 x_get_font_info (f, font_idx)
14053 FRAME_PTR f;
14054 int font_idx;
14055 {
14056 return (FRAME_X_FONT_TABLE (f) + font_idx);
14057 }
14058
14059
14060 /* Return a list of names of available fonts matching PATTERN on frame F.
14061
14062 If SIZE is > 0, it is the size (maximum bounds width) of fonts
14063 to be listed.
14064
14065 SIZE < 0 means include scalable fonts.
14066
14067 Frame F null means we have not yet created any frame on X, and
14068 consult the first display in x_display_list. MAXNAMES sets a limit
14069 on how many fonts to match. */
14070
14071 Lisp_Object
14072 x_list_fonts (f, pattern, size, maxnames)
14073 struct frame *f;
14074 Lisp_Object pattern;
14075 int size;
14076 int maxnames;
14077 {
14078 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
14079 Lisp_Object tem, second_best;
14080 struct x_display_info *dpyinfo
14081 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
14082 Display *dpy = dpyinfo->display;
14083 int try_XLoadQueryFont = 0;
14084 int count;
14085 int allow_scalable_fonts_p = 0;
14086
14087 if (size < 0)
14088 {
14089 allow_scalable_fonts_p = 1;
14090 size = 0;
14091 }
14092
14093 patterns = Fassoc (pattern, Valternate_fontname_alist);
14094 if (NILP (patterns))
14095 patterns = Fcons (pattern, Qnil);
14096
14097 if (maxnames == 1 && !size)
14098 /* We can return any single font matching PATTERN. */
14099 try_XLoadQueryFont = 1;
14100
14101 for (; CONSP (patterns); patterns = XCDR (patterns))
14102 {
14103 int num_fonts;
14104 char **names = NULL;
14105
14106 pattern = XCAR (patterns);
14107 /* See if we cached the result for this particular query.
14108 The cache is an alist of the form:
14109 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
14110 tem = XCDR (dpyinfo->name_list_element);
14111 key = Fcons (Fcons (pattern, make_number (maxnames)),
14112 allow_scalable_fonts_p ? Qt : Qnil);
14113 list = Fassoc (key, tem);
14114 if (!NILP (list))
14115 {
14116 list = Fcdr_safe (list);
14117 /* We have a cashed list. Don't have to get the list again. */
14118 goto label_cached;
14119 }
14120
14121 /* At first, put PATTERN in the cache. */
14122
14123 BLOCK_INPUT;
14124 count = x_catch_errors (dpy);
14125
14126 if (try_XLoadQueryFont)
14127 {
14128 XFontStruct *font;
14129 unsigned long value;
14130
14131 font = XLoadQueryFont (dpy, SDATA (pattern));
14132 if (x_had_errors_p (dpy))
14133 {
14134 /* This error is perhaps due to insufficient memory on X
14135 server. Let's just ignore it. */
14136 font = NULL;
14137 x_clear_errors (dpy);
14138 }
14139
14140 if (font
14141 && XGetFontProperty (font, XA_FONT, &value))
14142 {
14143 char *name = (char *) XGetAtomName (dpy, (Atom) value);
14144 int len = strlen (name);
14145 char *tmp;
14146
14147 /* If DXPC (a Differential X Protocol Compressor)
14148 Ver.3.7 is running, XGetAtomName will return null
14149 string. We must avoid such a name. */
14150 if (len == 0)
14151 try_XLoadQueryFont = 0;
14152 else
14153 {
14154 num_fonts = 1;
14155 names = (char **) alloca (sizeof (char *));
14156 /* Some systems only allow alloca assigned to a
14157 simple var. */
14158 tmp = (char *) alloca (len + 1); names[0] = tmp;
14159 bcopy (name, names[0], len + 1);
14160 XFree (name);
14161 }
14162 }
14163 else
14164 try_XLoadQueryFont = 0;
14165
14166 if (font)
14167 XFreeFont (dpy, font);
14168 }
14169
14170 if (!try_XLoadQueryFont)
14171 {
14172 /* We try at least 10 fonts because XListFonts will return
14173 auto-scaled fonts at the head. */
14174 names = XListFonts (dpy, SDATA (pattern), max (maxnames, 10),
14175 &num_fonts);
14176 if (x_had_errors_p (dpy))
14177 {
14178 /* This error is perhaps due to insufficient memory on X
14179 server. Let's just ignore it. */
14180 names = NULL;
14181 x_clear_errors (dpy);
14182 }
14183 }
14184
14185 x_uncatch_errors (dpy, count);
14186 UNBLOCK_INPUT;
14187
14188 if (names)
14189 {
14190 int i;
14191
14192 /* Make a list of all the fonts we got back.
14193 Store that in the font cache for the display. */
14194 for (i = 0; i < num_fonts; i++)
14195 {
14196 int width = 0;
14197 char *p = names[i];
14198 int average_width = -1, dashes = 0;
14199
14200 /* Count the number of dashes in NAMES[I]. If there are
14201 14 dashes, and the field value following 12th dash
14202 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
14203 is usually too ugly to be used for editing. Let's
14204 ignore it. */
14205 while (*p)
14206 if (*p++ == '-')
14207 {
14208 dashes++;
14209 if (dashes == 7) /* PIXEL_SIZE field */
14210 width = atoi (p);
14211 else if (dashes == 12) /* AVERAGE_WIDTH field */
14212 average_width = atoi (p);
14213 }
14214
14215 if (allow_scalable_fonts_p
14216 || dashes < 14 || average_width != 0)
14217 {
14218 tem = build_string (names[i]);
14219 if (NILP (Fassoc (tem, list)))
14220 {
14221 if (STRINGP (Vx_pixel_size_width_font_regexp)
14222 && ((fast_c_string_match_ignore_case
14223 (Vx_pixel_size_width_font_regexp, names[i]))
14224 >= 0))
14225 /* We can set the value of PIXEL_SIZE to the
14226 width of this font. */
14227 list = Fcons (Fcons (tem, make_number (width)), list);
14228 else
14229 /* For the moment, width is not known. */
14230 list = Fcons (Fcons (tem, Qnil), list);
14231 }
14232 }
14233 }
14234
14235 if (!try_XLoadQueryFont)
14236 {
14237 BLOCK_INPUT;
14238 XFreeFontNames (names);
14239 UNBLOCK_INPUT;
14240 }
14241 }
14242
14243 /* Now store the result in the cache. */
14244 XSETCDR (dpyinfo->name_list_element,
14245 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
14246
14247 label_cached:
14248 if (NILP (list)) continue; /* Try the remaining alternatives. */
14249
14250 newlist = second_best = Qnil;
14251 /* Make a list of the fonts that have the right width. */
14252 for (; CONSP (list); list = XCDR (list))
14253 {
14254 int found_size;
14255
14256 tem = XCAR (list);
14257
14258 if (!CONSP (tem) || NILP (XCAR (tem)))
14259 continue;
14260 if (!size)
14261 {
14262 newlist = Fcons (XCAR (tem), newlist);
14263 continue;
14264 }
14265
14266 if (!INTEGERP (XCDR (tem)))
14267 {
14268 /* Since we have not yet known the size of this font, we
14269 must try slow function call XLoadQueryFont. */
14270 XFontStruct *thisinfo;
14271
14272 BLOCK_INPUT;
14273 count = x_catch_errors (dpy);
14274 thisinfo = XLoadQueryFont (dpy,
14275 SDATA (XCAR (tem)));
14276 if (x_had_errors_p (dpy))
14277 {
14278 /* This error is perhaps due to insufficient memory on X
14279 server. Let's just ignore it. */
14280 thisinfo = NULL;
14281 x_clear_errors (dpy);
14282 }
14283 x_uncatch_errors (dpy, count);
14284 UNBLOCK_INPUT;
14285
14286 if (thisinfo)
14287 {
14288 XSETCDR (tem,
14289 (thisinfo->min_bounds.width == 0
14290 ? make_number (0)
14291 : make_number (thisinfo->max_bounds.width)));
14292 BLOCK_INPUT;
14293 XFreeFont (dpy, thisinfo);
14294 UNBLOCK_INPUT;
14295 }
14296 else
14297 /* For unknown reason, the previous call of XListFont had
14298 returned a font which can't be opened. Record the size
14299 as 0 not to try to open it again. */
14300 XSETCDR (tem, make_number (0));
14301 }
14302
14303 found_size = XINT (XCDR (tem));
14304 if (found_size == size)
14305 newlist = Fcons (XCAR (tem), newlist);
14306 else if (found_size > 0)
14307 {
14308 if (NILP (second_best))
14309 second_best = tem;
14310 else if (found_size < size)
14311 {
14312 if (XINT (XCDR (second_best)) > size
14313 || XINT (XCDR (second_best)) < found_size)
14314 second_best = tem;
14315 }
14316 else
14317 {
14318 if (XINT (XCDR (second_best)) > size
14319 && XINT (XCDR (second_best)) > found_size)
14320 second_best = tem;
14321 }
14322 }
14323 }
14324 if (!NILP (newlist))
14325 break;
14326 else if (!NILP (second_best))
14327 {
14328 newlist = Fcons (XCAR (second_best), Qnil);
14329 break;
14330 }
14331 }
14332
14333 return newlist;
14334 }
14335
14336
14337 #if GLYPH_DEBUG
14338
14339 /* Check that FONT is valid on frame F. It is if it can be found in F's
14340 font table. */
14341
14342 static void
14343 x_check_font (f, font)
14344 struct frame *f;
14345 XFontStruct *font;
14346 {
14347 int i;
14348 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14349
14350 xassert (font != NULL);
14351
14352 for (i = 0; i < dpyinfo->n_fonts; i++)
14353 if (dpyinfo->font_table[i].name
14354 && font == dpyinfo->font_table[i].font)
14355 break;
14356
14357 xassert (i < dpyinfo->n_fonts);
14358 }
14359
14360 #endif /* GLYPH_DEBUG != 0 */
14361
14362 /* Set *W to the minimum width, *H to the minimum font height of FONT.
14363 Note: There are (broken) X fonts out there with invalid XFontStruct
14364 min_bounds contents. For example, handa@etl.go.jp reports that
14365 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
14366 have font->min_bounds.width == 0. */
14367
14368 static INLINE void
14369 x_font_min_bounds (font, w, h)
14370 XFontStruct *font;
14371 int *w, *h;
14372 {
14373 *h = FONT_HEIGHT (font);
14374 *w = font->min_bounds.width;
14375
14376 /* Try to handle the case where FONT->min_bounds has invalid
14377 contents. Since the only font known to have invalid min_bounds
14378 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
14379 if (*w <= 0)
14380 *w = font->max_bounds.width;
14381 }
14382
14383
14384 /* Compute the smallest character width and smallest font height over
14385 all fonts available on frame F. Set the members smallest_char_width
14386 and smallest_font_height in F's x_display_info structure to
14387 the values computed. Value is non-zero if smallest_font_height or
14388 smallest_char_width become smaller than they were before. */
14389
14390 static int
14391 x_compute_min_glyph_bounds (f)
14392 struct frame *f;
14393 {
14394 int i;
14395 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14396 XFontStruct *font;
14397 int old_width = dpyinfo->smallest_char_width;
14398 int old_height = dpyinfo->smallest_font_height;
14399
14400 dpyinfo->smallest_font_height = 100000;
14401 dpyinfo->smallest_char_width = 100000;
14402
14403 for (i = 0; i < dpyinfo->n_fonts; ++i)
14404 if (dpyinfo->font_table[i].name)
14405 {
14406 struct font_info *fontp = dpyinfo->font_table + i;
14407 int w, h;
14408
14409 font = (XFontStruct *) fontp->font;
14410 xassert (font != (XFontStruct *) ~0);
14411 x_font_min_bounds (font, &w, &h);
14412
14413 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
14414 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
14415 }
14416
14417 xassert (dpyinfo->smallest_char_width > 0
14418 && dpyinfo->smallest_font_height > 0);
14419
14420 return (dpyinfo->n_fonts == 1
14421 || dpyinfo->smallest_char_width < old_width
14422 || dpyinfo->smallest_font_height < old_height);
14423 }
14424
14425
14426 /* Load font named FONTNAME of the size SIZE for frame F, and return a
14427 pointer to the structure font_info while allocating it dynamically.
14428 If SIZE is 0, load any size of font.
14429 If loading is failed, return NULL. */
14430
14431 struct font_info *
14432 x_load_font (f, fontname, size)
14433 struct frame *f;
14434 register char *fontname;
14435 int size;
14436 {
14437 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14438 Lisp_Object font_names;
14439 int count;
14440
14441 /* Get a list of all the fonts that match this name. Once we
14442 have a list of matching fonts, we compare them against the fonts
14443 we already have by comparing names. */
14444 font_names = x_list_fonts (f, build_string (fontname), size, 1);
14445
14446 if (!NILP (font_names))
14447 {
14448 Lisp_Object tail;
14449 int i;
14450
14451 for (i = 0; i < dpyinfo->n_fonts; i++)
14452 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
14453 if (dpyinfo->font_table[i].name
14454 && (!strcmp (dpyinfo->font_table[i].name,
14455 SDATA (XCAR (tail)))
14456 || !strcmp (dpyinfo->font_table[i].full_name,
14457 SDATA (XCAR (tail)))))
14458 return (dpyinfo->font_table + i);
14459 }
14460
14461 /* Load the font and add it to the table. */
14462 {
14463 char *full_name;
14464 XFontStruct *font;
14465 struct font_info *fontp;
14466 unsigned long value;
14467 int i;
14468
14469 /* If we have found fonts by x_list_font, load one of them. If
14470 not, we still try to load a font by the name given as FONTNAME
14471 because XListFonts (called in x_list_font) of some X server has
14472 a bug of not finding a font even if the font surely exists and
14473 is loadable by XLoadQueryFont. */
14474 if (size > 0 && !NILP (font_names))
14475 fontname = (char *) SDATA (XCAR (font_names));
14476
14477 BLOCK_INPUT;
14478 count = x_catch_errors (FRAME_X_DISPLAY (f));
14479 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
14480 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
14481 {
14482 /* This error is perhaps due to insufficient memory on X
14483 server. Let's just ignore it. */
14484 font = NULL;
14485 x_clear_errors (FRAME_X_DISPLAY (f));
14486 }
14487 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
14488 UNBLOCK_INPUT;
14489 if (!font)
14490 return NULL;
14491
14492 /* Find a free slot in the font table. */
14493 for (i = 0; i < dpyinfo->n_fonts; ++i)
14494 if (dpyinfo->font_table[i].name == NULL)
14495 break;
14496
14497 /* If no free slot found, maybe enlarge the font table. */
14498 if (i == dpyinfo->n_fonts
14499 && dpyinfo->n_fonts == dpyinfo->font_table_size)
14500 {
14501 int sz;
14502 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
14503 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
14504 dpyinfo->font_table
14505 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
14506 }
14507
14508 fontp = dpyinfo->font_table + i;
14509 if (i == dpyinfo->n_fonts)
14510 ++dpyinfo->n_fonts;
14511
14512 /* Now fill in the slots of *FONTP. */
14513 BLOCK_INPUT;
14514 fontp->font = font;
14515 fontp->font_idx = i;
14516 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
14517 bcopy (fontname, fontp->name, strlen (fontname) + 1);
14518
14519 /* Try to get the full name of FONT. Put it in FULL_NAME. */
14520 full_name = 0;
14521 if (XGetFontProperty (font, XA_FONT, &value))
14522 {
14523 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
14524 char *p = name;
14525 int dashes = 0;
14526
14527 /* Count the number of dashes in the "full name".
14528 If it is too few, this isn't really the font's full name,
14529 so don't use it.
14530 In X11R4, the fonts did not come with their canonical names
14531 stored in them. */
14532 while (*p)
14533 {
14534 if (*p == '-')
14535 dashes++;
14536 p++;
14537 }
14538
14539 if (dashes >= 13)
14540 {
14541 full_name = (char *) xmalloc (p - name + 1);
14542 bcopy (name, full_name, p - name + 1);
14543 }
14544
14545 XFree (name);
14546 }
14547
14548 if (full_name != 0)
14549 fontp->full_name = full_name;
14550 else
14551 fontp->full_name = fontp->name;
14552
14553 fontp->size = font->max_bounds.width;
14554 fontp->height = FONT_HEIGHT (font);
14555
14556 if (NILP (font_names))
14557 {
14558 /* We come here because of a bug of XListFonts mentioned at
14559 the head of this block. Let's store this information in
14560 the cache for x_list_fonts. */
14561 Lisp_Object lispy_name = build_string (fontname);
14562 Lisp_Object lispy_full_name = build_string (fontp->full_name);
14563 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
14564 Qnil);
14565
14566 XSETCDR (dpyinfo->name_list_element,
14567 Fcons (Fcons (key,
14568 Fcons (Fcons (lispy_full_name,
14569 make_number (fontp->size)),
14570 Qnil)),
14571 XCDR (dpyinfo->name_list_element)));
14572 if (full_name)
14573 {
14574 key = Fcons (Fcons (lispy_full_name, make_number (256)),
14575 Qnil);
14576 XSETCDR (dpyinfo->name_list_element,
14577 Fcons (Fcons (key,
14578 Fcons (Fcons (lispy_full_name,
14579 make_number (fontp->size)),
14580 Qnil)),
14581 XCDR (dpyinfo->name_list_element)));
14582 }
14583 }
14584
14585 /* The slot `encoding' specifies how to map a character
14586 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
14587 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
14588 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
14589 2:0xA020..0xFF7F). For the moment, we don't know which charset
14590 uses this font. So, we set information in fontp->encoding[1]
14591 which is never used by any charset. If mapping can't be
14592 decided, set FONT_ENCODING_NOT_DECIDED. */
14593 fontp->encoding[1]
14594 = (font->max_byte1 == 0
14595 /* 1-byte font */
14596 ? (font->min_char_or_byte2 < 0x80
14597 ? (font->max_char_or_byte2 < 0x80
14598 ? 0 /* 0x20..0x7F */
14599 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
14600 : 1) /* 0xA0..0xFF */
14601 /* 2-byte font */
14602 : (font->min_byte1 < 0x80
14603 ? (font->max_byte1 < 0x80
14604 ? (font->min_char_or_byte2 < 0x80
14605 ? (font->max_char_or_byte2 < 0x80
14606 ? 0 /* 0x2020..0x7F7F */
14607 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
14608 : 3) /* 0x20A0..0x7FFF */
14609 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
14610 : (font->min_char_or_byte2 < 0x80
14611 ? (font->max_char_or_byte2 < 0x80
14612 ? 2 /* 0xA020..0xFF7F */
14613 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
14614 : 1))); /* 0xA0A0..0xFFFF */
14615
14616 fontp->baseline_offset
14617 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
14618 ? (long) value : 0);
14619 fontp->relative_compose
14620 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
14621 ? (long) value : 0);
14622 fontp->default_ascent
14623 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
14624 ? (long) value : 0);
14625
14626 /* Set global flag fonts_changed_p to non-zero if the font loaded
14627 has a character with a smaller width than any other character
14628 before, or if the font loaded has a smaller height than any
14629 other font loaded before. If this happens, it will make a
14630 glyph matrix reallocation necessary. */
14631 fonts_changed_p |= x_compute_min_glyph_bounds (f);
14632 UNBLOCK_INPUT;
14633 return fontp;
14634 }
14635 }
14636
14637
14638 /* Return a pointer to struct font_info of a font named FONTNAME for
14639 frame F. If no such font is loaded, return NULL. */
14640
14641 struct font_info *
14642 x_query_font (f, fontname)
14643 struct frame *f;
14644 register char *fontname;
14645 {
14646 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14647 int i;
14648
14649 for (i = 0; i < dpyinfo->n_fonts; i++)
14650 if (dpyinfo->font_table[i].name
14651 && (!strcmp (dpyinfo->font_table[i].name, fontname)
14652 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
14653 return (dpyinfo->font_table + i);
14654 return NULL;
14655 }
14656
14657
14658 /* Find a CCL program for a font specified by FONTP, and set the member
14659 `encoder' of the structure. */
14660
14661 void
14662 x_find_ccl_program (fontp)
14663 struct font_info *fontp;
14664 {
14665 Lisp_Object list, elt;
14666
14667 elt = Qnil;
14668 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
14669 {
14670 elt = XCAR (list);
14671 if (CONSP (elt)
14672 && STRINGP (XCAR (elt))
14673 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
14674 >= 0)
14675 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
14676 >= 0)))
14677 break;
14678 }
14679
14680 if (! NILP (list))
14681 {
14682 struct ccl_program *ccl
14683 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
14684
14685 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
14686 xfree (ccl);
14687 else
14688 fontp->font_encoder = ccl;
14689 }
14690 }
14691
14692
14693 \f
14694 /***********************************************************************
14695 Initialization
14696 ***********************************************************************/
14697
14698 #ifdef USE_X_TOOLKIT
14699 static XrmOptionDescRec emacs_options[] = {
14700 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
14701 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
14702
14703 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
14704 XrmoptionSepArg, NULL},
14705 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
14706
14707 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14708 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14709 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14710 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14711 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14712 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
14713 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
14714 };
14715 #endif /* USE_X_TOOLKIT */
14716
14717 static int x_initialized;
14718
14719 #ifdef MULTI_KBOARD
14720 /* Test whether two display-name strings agree up to the dot that separates
14721 the screen number from the server number. */
14722 static int
14723 same_x_server (name1, name2)
14724 const char *name1, *name2;
14725 {
14726 int seen_colon = 0;
14727 const unsigned char *system_name = SDATA (Vsystem_name);
14728 int system_name_length = strlen (system_name);
14729 int length_until_period = 0;
14730
14731 while (system_name[length_until_period] != 0
14732 && system_name[length_until_period] != '.')
14733 length_until_period++;
14734
14735 /* Treat `unix' like an empty host name. */
14736 if (! strncmp (name1, "unix:", 5))
14737 name1 += 4;
14738 if (! strncmp (name2, "unix:", 5))
14739 name2 += 4;
14740 /* Treat this host's name like an empty host name. */
14741 if (! strncmp (name1, system_name, system_name_length)
14742 && name1[system_name_length] == ':')
14743 name1 += system_name_length;
14744 if (! strncmp (name2, system_name, system_name_length)
14745 && name2[system_name_length] == ':')
14746 name2 += system_name_length;
14747 /* Treat this host's domainless name like an empty host name. */
14748 if (! strncmp (name1, system_name, length_until_period)
14749 && name1[length_until_period] == ':')
14750 name1 += length_until_period;
14751 if (! strncmp (name2, system_name, length_until_period)
14752 && name2[length_until_period] == ':')
14753 name2 += length_until_period;
14754
14755 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
14756 {
14757 if (*name1 == ':')
14758 seen_colon++;
14759 if (seen_colon && *name1 == '.')
14760 return 1;
14761 }
14762 return (seen_colon
14763 && (*name1 == '.' || *name1 == '\0')
14764 && (*name2 == '.' || *name2 == '\0'));
14765 }
14766 #endif
14767
14768 struct x_display_info *
14769 x_term_init (display_name, xrm_option, resource_name)
14770 Lisp_Object display_name;
14771 char *xrm_option;
14772 char *resource_name;
14773 {
14774 int connection;
14775 Display *dpy;
14776 struct x_display_info *dpyinfo;
14777 XrmDatabase xrdb;
14778
14779 BLOCK_INPUT;
14780
14781 if (!x_initialized)
14782 {
14783 x_initialize ();
14784 x_initialized = 1;
14785 }
14786
14787 #ifdef USE_X_TOOLKIT
14788 /* weiner@footloose.sps.mot.com reports that this causes
14789 errors with X11R5:
14790 X protocol error: BadAtom (invalid Atom parameter)
14791 on protocol request 18skiloaf.
14792 So let's not use it until R6. */
14793 #ifdef HAVE_X11XTR6
14794 XtSetLanguageProc (NULL, NULL, NULL);
14795 #endif
14796
14797 {
14798 int argc = 0;
14799 char *argv[3];
14800
14801 argv[0] = "";
14802 argc = 1;
14803 if (xrm_option)
14804 {
14805 argv[argc++] = "-xrm";
14806 argv[argc++] = xrm_option;
14807 }
14808 turn_on_atimers (0);
14809 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
14810 resource_name, EMACS_CLASS,
14811 emacs_options, XtNumber (emacs_options),
14812 &argc, argv);
14813 turn_on_atimers (1);
14814
14815 #ifdef HAVE_X11XTR6
14816 /* I think this is to compensate for XtSetLanguageProc. */
14817 fixup_locale ();
14818 #endif
14819 }
14820
14821 #else /* not USE_X_TOOLKIT */
14822 #ifdef HAVE_X11R5
14823 XSetLocaleModifiers ("");
14824 #endif
14825 dpy = XOpenDisplay (SDATA (display_name));
14826 #endif /* not USE_X_TOOLKIT */
14827
14828 /* Detect failure. */
14829 if (dpy == 0)
14830 {
14831 UNBLOCK_INPUT;
14832 return 0;
14833 }
14834
14835 /* We have definitely succeeded. Record the new connection. */
14836
14837 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
14838 bzero (dpyinfo, sizeof *dpyinfo);
14839
14840 #ifdef MULTI_KBOARD
14841 {
14842 struct x_display_info *share;
14843 Lisp_Object tail;
14844
14845 for (share = x_display_list, tail = x_display_name_list; share;
14846 share = share->next, tail = XCDR (tail))
14847 if (same_x_server (SDATA (XCAR (XCAR (tail))),
14848 SDATA (display_name)))
14849 break;
14850 if (share)
14851 dpyinfo->kboard = share->kboard;
14852 else
14853 {
14854 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
14855 init_kboard (dpyinfo->kboard);
14856 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
14857 {
14858 char *vendor = ServerVendor (dpy);
14859 UNBLOCK_INPUT;
14860 dpyinfo->kboard->Vsystem_key_alist
14861 = call1 (Qvendor_specific_keysyms,
14862 build_string (vendor ? vendor : ""));
14863 BLOCK_INPUT;
14864 }
14865
14866 dpyinfo->kboard->next_kboard = all_kboards;
14867 all_kboards = dpyinfo->kboard;
14868 /* Don't let the initial kboard remain current longer than necessary.
14869 That would cause problems if a file loaded on startup tries to
14870 prompt in the mini-buffer. */
14871 if (current_kboard == initial_kboard)
14872 current_kboard = dpyinfo->kboard;
14873 }
14874 dpyinfo->kboard->reference_count++;
14875 }
14876 #endif
14877
14878 /* Put this display on the chain. */
14879 dpyinfo->next = x_display_list;
14880 x_display_list = dpyinfo;
14881
14882 /* Put it on x_display_name_list as well, to keep them parallel. */
14883 x_display_name_list = Fcons (Fcons (display_name, Qnil),
14884 x_display_name_list);
14885 dpyinfo->name_list_element = XCAR (x_display_name_list);
14886
14887 dpyinfo->display = dpy;
14888
14889 #if 0
14890 XSetAfterFunction (x_current_display, x_trace_wire);
14891 #endif /* ! 0 */
14892
14893 dpyinfo->x_id_name
14894 = (char *) xmalloc (SBYTES (Vinvocation_name)
14895 + SBYTES (Vsystem_name)
14896 + 2);
14897 sprintf (dpyinfo->x_id_name, "%s@%s",
14898 SDATA (Vinvocation_name), SDATA (Vsystem_name));
14899
14900 /* Figure out which modifier bits mean what. */
14901 x_find_modifier_meanings (dpyinfo);
14902
14903 /* Get the scroll bar cursor. */
14904 dpyinfo->vertical_scroll_bar_cursor
14905 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
14906
14907 xrdb = x_load_resources (dpyinfo->display, xrm_option,
14908 resource_name, EMACS_CLASS);
14909 #ifdef HAVE_XRMSETDATABASE
14910 XrmSetDatabase (dpyinfo->display, xrdb);
14911 #else
14912 dpyinfo->display->db = xrdb;
14913 #endif
14914 /* Put the rdb where we can find it in a way that works on
14915 all versions. */
14916 dpyinfo->xrdb = xrdb;
14917
14918 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
14919 DefaultScreen (dpyinfo->display));
14920 select_visual (dpyinfo);
14921 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
14922 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
14923 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
14924 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
14925 dpyinfo->grabbed = 0;
14926 dpyinfo->reference_count = 0;
14927 dpyinfo->icon_bitmap_id = -1;
14928 dpyinfo->font_table = NULL;
14929 dpyinfo->n_fonts = 0;
14930 dpyinfo->font_table_size = 0;
14931 dpyinfo->bitmaps = 0;
14932 dpyinfo->bitmaps_size = 0;
14933 dpyinfo->bitmaps_last = 0;
14934 dpyinfo->scratch_cursor_gc = 0;
14935 dpyinfo->mouse_face_mouse_frame = 0;
14936 dpyinfo->mouse_face_deferred_gc = 0;
14937 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
14938 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
14939 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
14940 dpyinfo->mouse_face_window = Qnil;
14941 dpyinfo->mouse_face_overlay = Qnil;
14942 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
14943 dpyinfo->mouse_face_defer = 0;
14944 dpyinfo->mouse_face_hidden = 0;
14945 dpyinfo->x_focus_frame = 0;
14946 dpyinfo->x_focus_event_frame = 0;
14947 dpyinfo->x_highlight_frame = 0;
14948 dpyinfo->image_cache = make_image_cache ();
14949
14950 /* See if a private colormap is requested. */
14951 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
14952 {
14953 if (dpyinfo->visual->class == PseudoColor)
14954 {
14955 Lisp_Object value;
14956 value = display_x_get_resource (dpyinfo,
14957 build_string ("privateColormap"),
14958 build_string ("PrivateColormap"),
14959 Qnil, Qnil);
14960 if (STRINGP (value)
14961 && (!strcmp (SDATA (value), "true")
14962 || !strcmp (SDATA (value), "on")))
14963 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
14964 }
14965 }
14966 else
14967 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
14968 dpyinfo->visual, AllocNone);
14969
14970 {
14971 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
14972 double pixels = DisplayHeight (dpyinfo->display, screen_number);
14973 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
14974 dpyinfo->resy = pixels * 25.4 / mm;
14975 pixels = DisplayWidth (dpyinfo->display, screen_number);
14976 mm = DisplayWidthMM (dpyinfo->display, screen_number);
14977 dpyinfo->resx = pixels * 25.4 / mm;
14978 }
14979
14980 dpyinfo->Xatom_wm_protocols
14981 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
14982 dpyinfo->Xatom_wm_take_focus
14983 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
14984 dpyinfo->Xatom_wm_save_yourself
14985 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
14986 dpyinfo->Xatom_wm_delete_window
14987 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
14988 dpyinfo->Xatom_wm_change_state
14989 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
14990 dpyinfo->Xatom_wm_configure_denied
14991 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
14992 dpyinfo->Xatom_wm_window_moved
14993 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
14994 dpyinfo->Xatom_editres
14995 = XInternAtom (dpyinfo->display, "Editres", False);
14996 dpyinfo->Xatom_CLIPBOARD
14997 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
14998 dpyinfo->Xatom_TIMESTAMP
14999 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
15000 dpyinfo->Xatom_TEXT
15001 = XInternAtom (dpyinfo->display, "TEXT", False);
15002 dpyinfo->Xatom_COMPOUND_TEXT
15003 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
15004 dpyinfo->Xatom_UTF8_STRING
15005 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
15006 dpyinfo->Xatom_DELETE
15007 = XInternAtom (dpyinfo->display, "DELETE", False);
15008 dpyinfo->Xatom_MULTIPLE
15009 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
15010 dpyinfo->Xatom_INCR
15011 = XInternAtom (dpyinfo->display, "INCR", False);
15012 dpyinfo->Xatom_EMACS_TMP
15013 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
15014 dpyinfo->Xatom_TARGETS
15015 = XInternAtom (dpyinfo->display, "TARGETS", False);
15016 dpyinfo->Xatom_NULL
15017 = XInternAtom (dpyinfo->display, "NULL", False);
15018 dpyinfo->Xatom_ATOM_PAIR
15019 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
15020 /* For properties of font. */
15021 dpyinfo->Xatom_PIXEL_SIZE
15022 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
15023 dpyinfo->Xatom_MULE_BASELINE_OFFSET
15024 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
15025 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
15026 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
15027 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
15028 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
15029
15030 /* Ghostscript support. */
15031 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
15032 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
15033
15034 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
15035 False);
15036
15037 dpyinfo->cut_buffers_initialized = 0;
15038
15039 connection = ConnectionNumber (dpyinfo->display);
15040 dpyinfo->connection = connection;
15041
15042 {
15043 char null_bits[1];
15044
15045 null_bits[0] = 0x00;
15046
15047 dpyinfo->null_pixel
15048 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
15049 null_bits, 1, 1, (long) 0, (long) 0,
15050 1);
15051 }
15052
15053 {
15054 extern int gray_bitmap_width, gray_bitmap_height;
15055 extern char *gray_bitmap_bits;
15056 dpyinfo->gray
15057 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
15058 gray_bitmap_bits,
15059 gray_bitmap_width, gray_bitmap_height,
15060 (unsigned long) 1, (unsigned long) 0, 1);
15061 }
15062
15063 #ifdef HAVE_X_I18N
15064 xim_initialize (dpyinfo, resource_name);
15065 #endif
15066
15067 #ifdef subprocesses
15068 /* This is only needed for distinguishing keyboard and process input. */
15069 if (connection != 0)
15070 add_keyboard_wait_descriptor (connection);
15071 #endif
15072
15073 #ifndef F_SETOWN_BUG
15074 #ifdef F_SETOWN
15075 #ifdef F_SETOWN_SOCK_NEG
15076 /* stdin is a socket here */
15077 fcntl (connection, F_SETOWN, -getpid ());
15078 #else /* ! defined (F_SETOWN_SOCK_NEG) */
15079 fcntl (connection, F_SETOWN, getpid ());
15080 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
15081 #endif /* ! defined (F_SETOWN) */
15082 #endif /* F_SETOWN_BUG */
15083
15084 #ifdef SIGIO
15085 if (interrupt_input)
15086 init_sigio (connection);
15087 #endif /* ! defined (SIGIO) */
15088
15089 #ifdef USE_LUCID
15090 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
15091 /* Make sure that we have a valid font for dialog boxes
15092 so that Xt does not crash. */
15093 {
15094 Display *dpy = dpyinfo->display;
15095 XrmValue d, fr, to;
15096 Font font;
15097 int count;
15098
15099 d.addr = (XPointer)&dpy;
15100 d.size = sizeof (Display *);
15101 fr.addr = XtDefaultFont;
15102 fr.size = sizeof (XtDefaultFont);
15103 to.size = sizeof (Font *);
15104 to.addr = (XPointer)&font;
15105 count = x_catch_errors (dpy);
15106 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
15107 abort ();
15108 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
15109 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
15110 x_uncatch_errors (dpy, count);
15111 }
15112 #endif
15113 #endif
15114
15115 /* See if we should run in synchronous mode. This is useful
15116 for debugging X code. */
15117 {
15118 Lisp_Object value;
15119 value = display_x_get_resource (dpyinfo,
15120 build_string ("synchronous"),
15121 build_string ("Synchronous"),
15122 Qnil, Qnil);
15123 if (STRINGP (value)
15124 && (!strcmp (SDATA (value), "true")
15125 || !strcmp (SDATA (value), "on")))
15126 XSynchronize (dpyinfo->display, True);
15127 }
15128
15129 UNBLOCK_INPUT;
15130
15131 return dpyinfo;
15132 }
15133 \f
15134 /* Get rid of display DPYINFO, assuming all frames are already gone,
15135 and without sending any more commands to the X server. */
15136
15137 void
15138 x_delete_display (dpyinfo)
15139 struct x_display_info *dpyinfo;
15140 {
15141 delete_keyboard_wait_descriptor (dpyinfo->connection);
15142
15143 /* Discard this display from x_display_name_list and x_display_list.
15144 We can't use Fdelq because that can quit. */
15145 if (! NILP (x_display_name_list)
15146 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
15147 x_display_name_list = XCDR (x_display_name_list);
15148 else
15149 {
15150 Lisp_Object tail;
15151
15152 tail = x_display_name_list;
15153 while (CONSP (tail) && CONSP (XCDR (tail)))
15154 {
15155 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
15156 {
15157 XSETCDR (tail, XCDR (XCDR (tail)));
15158 break;
15159 }
15160 tail = XCDR (tail);
15161 }
15162 }
15163
15164 if (next_noop_dpyinfo == dpyinfo)
15165 next_noop_dpyinfo = dpyinfo->next;
15166
15167 if (x_display_list == dpyinfo)
15168 x_display_list = dpyinfo->next;
15169 else
15170 {
15171 struct x_display_info *tail;
15172
15173 for (tail = x_display_list; tail; tail = tail->next)
15174 if (tail->next == dpyinfo)
15175 tail->next = tail->next->next;
15176 }
15177
15178 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
15179 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
15180 XrmDestroyDatabase (dpyinfo->xrdb);
15181 #endif
15182 #endif
15183 #ifdef MULTI_KBOARD
15184 if (--dpyinfo->kboard->reference_count == 0)
15185 delete_kboard (dpyinfo->kboard);
15186 #endif
15187 #ifdef HAVE_X_I18N
15188 if (dpyinfo->xim)
15189 xim_close_dpy (dpyinfo);
15190 #endif
15191
15192 xfree (dpyinfo->font_table);
15193 xfree (dpyinfo->x_id_name);
15194 xfree (dpyinfo->color_cells);
15195 xfree (dpyinfo);
15196 }
15197
15198 \f
15199 /* Set up use of X before we make the first connection. */
15200
15201 static struct redisplay_interface x_redisplay_interface =
15202 {
15203 x_produce_glyphs,
15204 x_write_glyphs,
15205 x_insert_glyphs,
15206 x_clear_end_of_line,
15207 x_scroll_run,
15208 x_after_update_window_line,
15209 x_update_window_begin,
15210 x_update_window_end,
15211 XTcursor_to,
15212 x_flush,
15213 x_clear_mouse_face,
15214 x_get_glyph_overhangs,
15215 x_fix_overlapping_area
15216 };
15217
15218 void
15219 x_initialize ()
15220 {
15221 rif = &x_redisplay_interface;
15222
15223 clear_frame_hook = x_clear_frame;
15224 ins_del_lines_hook = x_ins_del_lines;
15225 delete_glyphs_hook = x_delete_glyphs;
15226 ring_bell_hook = XTring_bell;
15227 reset_terminal_modes_hook = XTreset_terminal_modes;
15228 set_terminal_modes_hook = XTset_terminal_modes;
15229 update_begin_hook = x_update_begin;
15230 update_end_hook = x_update_end;
15231 set_terminal_window_hook = XTset_terminal_window;
15232 read_socket_hook = XTread_socket;
15233 frame_up_to_date_hook = XTframe_up_to_date;
15234 mouse_position_hook = XTmouse_position;
15235 frame_rehighlight_hook = XTframe_rehighlight;
15236 frame_raise_lower_hook = XTframe_raise_lower;
15237 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
15238 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
15239 redeem_scroll_bar_hook = XTredeem_scroll_bar;
15240 judge_scroll_bars_hook = XTjudge_scroll_bars;
15241 estimate_mode_line_height_hook = x_estimate_mode_line_height;
15242
15243 scroll_region_ok = 1; /* we'll scroll partial frames */
15244 char_ins_del_ok = 1;
15245 line_ins_del_ok = 1; /* we'll just blt 'em */
15246 fast_clear_end_of_line = 1; /* X does this well */
15247 memory_below_frame = 0; /* we don't remember what scrolls
15248 off the bottom */
15249 baud_rate = 19200;
15250
15251 x_noop_count = 0;
15252 last_tool_bar_item = -1;
15253 any_help_event_p = 0;
15254
15255 /* Try to use interrupt input; if we can't, then start polling. */
15256 Fset_input_mode (Qt, Qnil, Qt, Qnil);
15257
15258 #ifdef USE_X_TOOLKIT
15259 XtToolkitInitialize ();
15260
15261 Xt_app_con = XtCreateApplicationContext ();
15262
15263 /* Register a converter from strings to pixels, which uses
15264 Emacs' color allocation infrastructure. */
15265 XtAppSetTypeConverter (Xt_app_con,
15266 XtRString, XtRPixel, cvt_string_to_pixel,
15267 cvt_string_to_pixel_args,
15268 XtNumber (cvt_string_to_pixel_args),
15269 XtCacheByDisplay, cvt_pixel_dtor);
15270
15271 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
15272
15273 /* Install an asynchronous timer that processes Xt timeout events
15274 every 0.1s. This is necessary because some widget sets use
15275 timeouts internally, for example the LessTif menu bar, or the
15276 Xaw3d scroll bar. When Xt timouts aren't processed, these
15277 widgets don't behave normally. */
15278 {
15279 EMACS_TIME interval;
15280 EMACS_SET_SECS_USECS (interval, 0, 100000);
15281 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
15282 }
15283 #endif
15284
15285 #ifdef USE_TOOLKIT_SCROLL_BARS
15286 xaw3d_arrow_scroll = False;
15287 xaw3d_pick_top = True;
15288 #endif
15289
15290 /* Note that there is no real way portable across R3/R4 to get the
15291 original error handler. */
15292 XSetErrorHandler (x_error_handler);
15293 XSetIOErrorHandler (x_io_error_quitter);
15294
15295 /* Disable Window Change signals; they are handled by X events. */
15296 #ifdef SIGWINCH
15297 signal (SIGWINCH, SIG_DFL);
15298 #endif /* SIGWINCH */
15299
15300 signal (SIGPIPE, x_connection_signal);
15301
15302 #ifdef HAVE_X_SM
15303 x_session_initialize ();
15304 #endif
15305 }
15306
15307
15308 void
15309 syms_of_xterm ()
15310 {
15311 staticpro (&x_error_message_string);
15312 x_error_message_string = Qnil;
15313
15314 staticpro (&x_display_name_list);
15315 x_display_name_list = Qnil;
15316
15317 staticpro (&last_mouse_scroll_bar);
15318 last_mouse_scroll_bar = Qnil;
15319
15320 staticpro (&Qvendor_specific_keysyms);
15321 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
15322
15323 staticpro (&Qutf_8);
15324 Qutf_8 = intern ("utf-8");
15325 staticpro (&Qlatin_1);
15326 Qlatin_1 = intern ("latin-1");
15327
15328 staticpro (&last_mouse_press_frame);
15329 last_mouse_press_frame = Qnil;
15330
15331 help_echo = Qnil;
15332 staticpro (&help_echo);
15333 help_echo_object = Qnil;
15334 staticpro (&help_echo_object);
15335 help_echo_window = Qnil;
15336 staticpro (&help_echo_window);
15337 previous_help_echo = Qnil;
15338 staticpro (&previous_help_echo);
15339 help_echo_pos = -1;
15340
15341 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
15342 doc: /* *Non-nil means autoselect window with mouse pointer. */);
15343 mouse_autoselect_window = 0;
15344
15345 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
15346 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
15347 For example, if a block cursor is over a tab, it will be drawn as
15348 wide as that tab on the display. */);
15349 x_stretch_cursor_p = 0;
15350
15351 DEFVAR_BOOL ("x-use-underline-position-properties",
15352 &x_use_underline_position_properties,
15353 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
15354 nil means ignore them. If you encounter fonts with bogus
15355 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
15356 to 4.1, set this to nil. */);
15357 x_use_underline_position_properties = 1;
15358
15359 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
15360 doc: /* What X toolkit scroll bars Emacs uses.
15361 A value of nil means Emacs doesn't use X toolkit scroll bars.
15362 Otherwise, value is a symbol describing the X toolkit. */);
15363 #ifdef USE_TOOLKIT_SCROLL_BARS
15364 #ifdef USE_MOTIF
15365 Vx_toolkit_scroll_bars = intern ("motif");
15366 #elif defined HAVE_XAW3D
15367 Vx_toolkit_scroll_bars = intern ("xaw3d");
15368 #else
15369 Vx_toolkit_scroll_bars = intern ("xaw");
15370 #endif
15371 #else
15372 Vx_toolkit_scroll_bars = Qnil;
15373 #endif
15374
15375 staticpro (&last_mouse_motion_frame);
15376 last_mouse_motion_frame = Qnil;
15377
15378 Qmodifier_value = intern ("modifier-value");
15379 Qalt = intern ("alt");
15380 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
15381 Qhyper = intern ("hyper");
15382 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
15383 Qmeta = intern ("meta");
15384 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
15385 Qsuper = intern ("super");
15386 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
15387
15388 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
15389 doc: /* Which keys Emacs uses for the alt modifier.
15390 This should be one of the symbols `alt', `hyper', `meta', `super'.
15391 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
15392 is nil, which is the same as `alt'. */);
15393 Vx_alt_keysym = Qnil;
15394
15395 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
15396 doc: /* Which keys Emacs uses for the hyper modifier.
15397 This should be one of the symbols `alt', `hyper', `meta', `super'.
15398 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
15399 default is nil, which is the same as `hyper'. */);
15400 Vx_hyper_keysym = Qnil;
15401
15402 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
15403 doc: /* Which keys Emacs uses for the meta modifier.
15404 This should be one of the symbols `alt', `hyper', `meta', `super'.
15405 For example, `meta' means use the Meta_L and Meta_R keysyms. The
15406 default is nil, which is the same as `meta'. */);
15407 Vx_meta_keysym = Qnil;
15408
15409 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
15410 doc: /* Which keys Emacs uses for the super modifier.
15411 This should be one of the symbols `alt', `hyper', `meta', `super'.
15412 For example, `super' means use the Super_L and Super_R keysyms. The
15413 default is nil, which is the same as `super'. */);
15414 Vx_super_keysym = Qnil;
15415
15416 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
15417 doc: /* Hash table of character codes indexed by X keysym codes. */);
15418 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
15419 make_float (DEFAULT_REHASH_SIZE),
15420 make_float (DEFAULT_REHASH_THRESHOLD),
15421 Qnil, Qnil, Qnil);
15422 }
15423
15424 #endif /* HAVE_X_WINDOWS */