]> code.delx.au - gnu-emacs/blob - src/xterm.c
(clear_mouse_face): Return 1 if text with mouse face was
[gnu-emacs] / src / xterm.c
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
23 /* Xt features made by Fred Pierresteguy. */
24
25 #include <config.h>
26
27 /* On 4.3 these lose if they come after xterm.h. */
28 /* Putting these at the beginning seems to be standard for other .c files. */
29 #include <signal.h>
30
31 #include <stdio.h>
32
33 #ifdef HAVE_X_WINDOWS
34
35 #include "lisp.h"
36 #include "blockinput.h"
37
38 /* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40 #include "syssignal.h"
41
42 /* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44 #include "xterm.h"
45 #include <X11/cursorfont.h>
46
47 /* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49 #ifndef makedev
50 #include <sys/types.h>
51 #endif /* makedev */
52
53 #ifdef BSD_SYSTEM
54 #include <sys/ioctl.h>
55 #endif /* ! defined (BSD_SYSTEM) */
56
57 #include "systty.h"
58 #include "systime.h"
59
60 #ifndef INCLUDED_FCNTL
61 #include <fcntl.h>
62 #endif
63 #include <ctype.h>
64 #include <errno.h>
65 #include <setjmp.h>
66 #include <sys/stat.h>
67 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68 /* #include <sys/param.h> */
69
70 #include "charset.h"
71 #include "coding.h"
72 #include "ccl.h"
73 #include "frame.h"
74 #include "dispextern.h"
75 #include "fontset.h"
76 #include "termhooks.h"
77 #include "termopts.h"
78 #include "termchar.h"
79 #if 0
80 #include "sink.h"
81 #include "sinkmask.h"
82 #endif /* ! 0 */
83 #include "gnu.h"
84 #include "disptab.h"
85 #include "buffer.h"
86 #include "window.h"
87 #include "keyboard.h"
88 #include "intervals.h"
89 #include "process.h"
90 #include "atimer.h"
91
92 #ifdef USE_X_TOOLKIT
93 #include <X11/Shell.h>
94 #endif
95
96 #ifdef HAVE_SYS_TIME_H
97 #include <sys/time.h>
98 #endif
99 #ifdef HAVE_UNISTD_H
100 #include <unistd.h>
101 #endif
102
103 #ifdef USE_X_TOOLKIT
104
105 extern void free_frame_menubar P_ ((struct frame *));
106 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
107 int));
108
109 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
110 #define HACK_EDITRES
111 extern void _XEditResCheckMessages ();
112 #endif /* not NO_EDITRES */
113
114 /* Include toolkit specific headers for the scroll bar widget. */
115
116 #ifdef USE_TOOLKIT_SCROLL_BARS
117 #if defined USE_MOTIF
118 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
119 #include <Xm/ScrollBar.h>
120 #else /* !USE_MOTIF i.e. use Xaw */
121
122 #ifdef HAVE_XAW3D
123 #include <X11/Xaw3d/Simple.h>
124 #include <X11/Xaw3d/Scrollbar.h>
125 #define ARROW_SCROLLBAR
126 #include <X11/Xaw3d/ScrollbarP.h>
127 #else /* !HAVE_XAW3D */
128 #include <X11/Xaw/Simple.h>
129 #include <X11/Xaw/Scrollbar.h>
130 #endif /* !HAVE_XAW3D */
131 #ifndef XtNpickTop
132 #define XtNpickTop "pickTop"
133 #endif /* !XtNpickTop */
134 #endif /* !USE_MOTIF */
135 #endif /* USE_TOOLKIT_SCROLL_BARS */
136
137 #endif /* USE_X_TOOLKIT */
138
139 #ifndef USE_X_TOOLKIT
140 #define x_any_window_to_frame x_window_to_frame
141 #define x_top_window_to_frame x_window_to_frame
142 #endif
143
144 #ifdef USE_X_TOOLKIT
145 #include "widget.h"
146 #ifndef XtNinitialState
147 #define XtNinitialState "initialState"
148 #endif
149 #endif
150
151 #ifndef min
152 #define min(a,b) ((a) < (b) ? (a) : (b))
153 #endif
154 #ifndef max
155 #define max(a,b) ((a) > (b) ? (a) : (b))
156 #endif
157
158 #define abs(x) ((x) < 0 ? -(x) : (x))
159
160 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
161
162 \f
163 /* Bitmaps for truncated lines. */
164
165 enum bitmap_type
166 {
167 NO_BITMAP,
168 LEFT_TRUNCATION_BITMAP,
169 RIGHT_TRUNCATION_BITMAP,
170 OVERLAY_ARROW_BITMAP,
171 CONTINUED_LINE_BITMAP,
172 CONTINUATION_LINE_BITMAP,
173 ZV_LINE_BITMAP
174 };
175
176 /* Bitmap drawn to indicate lines not displaying text if
177 `indicate-empty-lines' is non-nil. */
178
179 #define zv_width 8
180 #define zv_height 8
181 static unsigned char zv_bits[] = {
182 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
183
184 /* An arrow like this: `<-'. */
185
186 #define left_width 8
187 #define left_height 8
188 static unsigned char left_bits[] = {
189 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
190
191 /* Right truncation arrow bitmap `->'. */
192
193 #define right_width 8
194 #define right_height 8
195 static unsigned char right_bits[] = {
196 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
197
198 /* Marker for continued lines. */
199
200 #define continued_width 8
201 #define continued_height 8
202 static unsigned char continued_bits[] = {
203 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
204
205 /* Marker for continuation lines. */
206
207 #define continuation_width 8
208 #define continuation_height 8
209 static unsigned char continuation_bits[] = {
210 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
211
212 /* Overlay arrow bitmap. */
213
214 #if 0
215 /* A bomb. */
216 #define ov_width 8
217 #define ov_height 8
218 static unsigned char ov_bits[] = {
219 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
220 #else
221 /* A triangular arrow. */
222 #define ov_width 8
223 #define ov_height 8
224 static unsigned char ov_bits[] = {
225 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
226
227 #endif
228
229 extern Lisp_Object Qhelp_echo;
230
231 \f
232 /* Non-nil means Emacs uses toolkit scroll bars. */
233
234 Lisp_Object Vx_toolkit_scroll_bars;
235
236 /* If a string, XTread_socket generates an event to display that string.
237 (The display is done in read_char.) */
238
239 static Lisp_Object help_echo;
240 static Lisp_Object help_echo_window;
241 static Lisp_Object help_echo_object;
242 static int help_echo_pos;
243
244 /* Temporary variable for XTread_socket. */
245
246 static Lisp_Object previous_help_echo;
247
248 /* Non-zero means that a HELP_EVENT has been generated since Emacs
249 start. */
250
251 static int any_help_event_p;
252
253 /* Non-zero means draw block and hollow cursor as wide as the glyph
254 under it. For example, if a block cursor is over a tab, it will be
255 drawn as wide as that tab on the display. */
256
257 int x_stretch_cursor_p;
258
259 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
260
261 int x_use_underline_position_properties;
262
263 /* This is a chain of structures for all the X displays currently in
264 use. */
265
266 struct x_display_info *x_display_list;
267
268 /* This is a list of cons cells, each of the form (NAME
269 . FONT-LIST-CACHE), one for each element of x_display_list and in
270 the same order. NAME is the name of the frame. FONT-LIST-CACHE
271 records previous values returned by x-list-fonts. */
272
273 Lisp_Object x_display_name_list;
274
275 /* Frame being updated by update_frame. This is declared in term.c.
276 This is set by update_begin and looked at by all the XT functions.
277 It is zero while not inside an update. In that case, the XT
278 functions assume that `selected_frame' is the frame to apply to. */
279
280 extern struct frame *updating_frame;
281
282 extern int waiting_for_input;
283
284 /* This is a frame waiting to be auto-raised, within XTread_socket. */
285
286 struct frame *pending_autoraise_frame;
287
288 #ifdef USE_X_TOOLKIT
289 /* The application context for Xt use. */
290 XtAppContext Xt_app_con;
291 static String Xt_default_resources[] = {0};
292 #endif /* USE_X_TOOLKIT */
293
294 /* Nominal cursor position -- where to draw output.
295 HPOS and VPOS are window relative glyph matrix coordinates.
296 X and Y are window relative pixel coordinates. */
297
298 struct cursor_pos output_cursor;
299
300 /* Non-zero means user is interacting with a toolkit scroll bar. */
301
302 static int toolkit_scroll_bar_interaction;
303
304 /* Mouse movement.
305
306 Formerly, we used PointerMotionHintMask (in standard_event_mask)
307 so that we would have to call XQueryPointer after each MotionNotify
308 event to ask for another such event. However, this made mouse tracking
309 slow, and there was a bug that made it eventually stop.
310
311 Simply asking for MotionNotify all the time seems to work better.
312
313 In order to avoid asking for motion events and then throwing most
314 of them away or busy-polling the server for mouse positions, we ask
315 the server for pointer motion hints. This means that we get only
316 one event per group of mouse movements. "Groups" are delimited by
317 other kinds of events (focus changes and button clicks, for
318 example), or by XQueryPointer calls; when one of these happens, we
319 get another MotionNotify event the next time the mouse moves. This
320 is at least as efficient as getting motion events when mouse
321 tracking is on, and I suspect only negligibly worse when tracking
322 is off. */
323
324 /* Where the mouse was last time we reported a mouse event. */
325
326 FRAME_PTR last_mouse_frame;
327 static XRectangle last_mouse_glyph;
328 static Lisp_Object last_mouse_press_frame;
329
330 /* The scroll bar in which the last X motion event occurred.
331
332 If the last X motion event occurred in a scroll bar, we set this so
333 XTmouse_position can know whether to report a scroll bar motion or
334 an ordinary motion.
335
336 If the last X motion event didn't occur in a scroll bar, we set
337 this to Qnil, to tell XTmouse_position to return an ordinary motion
338 event. */
339
340 static Lisp_Object last_mouse_scroll_bar;
341
342 /* This is a hack. We would really prefer that XTmouse_position would
343 return the time associated with the position it returns, but there
344 doesn't seem to be any way to wrest the time-stamp from the server
345 along with the position query. So, we just keep track of the time
346 of the last movement we received, and return that in hopes that
347 it's somewhat accurate. */
348
349 static Time last_mouse_movement_time;
350
351 /* Incremented by XTread_socket whenever it really tries to read
352 events. */
353
354 #ifdef __STDC__
355 static int volatile input_signal_count;
356 #else
357 static int input_signal_count;
358 #endif
359
360 /* Used locally within XTread_socket. */
361
362 static int x_noop_count;
363
364 /* Initial values of argv and argc. */
365
366 extern char **initial_argv;
367 extern int initial_argc;
368
369 extern Lisp_Object Vcommand_line_args, Vsystem_name;
370
371 /* Tells if a window manager is present or not. */
372
373 extern Lisp_Object Vx_no_window_manager;
374
375 extern Lisp_Object Qface, Qmouse_face;
376
377 extern int errno;
378
379 /* A mask of extra modifier bits to put into every keyboard char. */
380
381 extern int extra_keyboard_modifiers;
382
383 static Lisp_Object Qvendor_specific_keysyms;
384
385 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
386 extern Lisp_Object x_icon_type P_ ((struct frame *));
387
388
389 /* Enumeration for overriding/changing the face to use for drawing
390 glyphs in x_draw_glyphs. */
391
392 enum draw_glyphs_face
393 {
394 DRAW_NORMAL_TEXT,
395 DRAW_INVERSE_VIDEO,
396 DRAW_CURSOR,
397 DRAW_MOUSE_FACE,
398 DRAW_IMAGE_RAISED,
399 DRAW_IMAGE_SUNKEN
400 };
401
402 static int clear_mouse_face P_ ((struct x_display_info *));
403 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
404 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
405 static const XColor *x_color_cells P_ ((Display *, int *));
406 static void x_update_window_end P_ ((struct window *, int, int));
407 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
408 void x_delete_display P_ ((struct x_display_info *));
409 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
410 unsigned));
411 static int fast_find_position P_ ((struct window *, int, int *, int *,
412 int *, int *));
413 static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
414 int *, int *, int *, int *, int));
415 static void set_output_cursor P_ ((struct cursor_pos *));
416 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
417 int *, int *, int *, int));
418 static void note_mode_line_highlight P_ ((struct window *, int, int));
419 static void note_mouse_highlight P_ ((struct frame *, int, int));
420 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
421 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
422 static void show_mouse_face P_ ((struct x_display_info *,
423 enum draw_glyphs_face));
424 static int x_io_error_quitter P_ ((Display *));
425 int x_catch_errors P_ ((Display *));
426 void x_uncatch_errors P_ ((Display *, int));
427 void x_lower_frame P_ ((struct frame *));
428 void x_scroll_bar_clear P_ ((struct frame *));
429 int x_had_errors_p P_ ((Display *));
430 void x_wm_set_size_hint P_ ((struct frame *, long, int));
431 void x_raise_frame P_ ((struct frame *));
432 void x_set_window_size P_ ((struct frame *, int, int, int));
433 void x_wm_set_window_state P_ ((struct frame *, int));
434 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
435 void x_initialize P_ ((void));
436 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
437 static int x_compute_min_glyph_bounds P_ ((struct frame *));
438 static void x_draw_phys_cursor_glyph P_ ((struct window *,
439 struct glyph_row *,
440 enum draw_glyphs_face));
441 static void x_update_end P_ ((struct frame *));
442 static void XTframe_up_to_date P_ ((struct frame *));
443 static void XTreassert_line_highlight P_ ((int, int));
444 static void x_change_line_highlight P_ ((int, int, int, int));
445 static void XTset_terminal_modes P_ ((void));
446 static void XTreset_terminal_modes P_ ((void));
447 static void XTcursor_to P_ ((int, int, int, int));
448 static void x_write_glyphs P_ ((struct glyph *, int));
449 static void x_clear_end_of_line P_ ((int));
450 static void x_clear_frame P_ ((void));
451 static void x_clear_cursor P_ ((struct window *));
452 static void frame_highlight P_ ((struct frame *));
453 static void frame_unhighlight P_ ((struct frame *));
454 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
455 static void XTframe_rehighlight P_ ((struct frame *));
456 static void x_frame_rehighlight P_ ((struct x_display_info *));
457 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
458 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
459 static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
460 XRectangle *));
461 static void expose_frame P_ ((struct frame *, int, int, int, int));
462 static void expose_window_tree P_ ((struct window *, XRectangle *));
463 static int expose_window P_ ((struct window *, XRectangle *));
464 static void expose_area P_ ((struct window *, struct glyph_row *,
465 XRectangle *, enum glyph_row_area));
466 static void expose_line P_ ((struct window *, struct glyph_row *,
467 XRectangle *));
468 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
469 static void x_update_window_cursor P_ ((struct window *, int));
470 static void x_erase_phys_cursor P_ ((struct window *));
471 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
472 static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
473 enum bitmap_type));
474
475 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
476 GC, int));
477 static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
478 static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
479 static void note_overwritten_text_cursor P_ ((struct window *, int, int));
480 static void x_flush P_ ((struct frame *f));
481 static void x_update_begin P_ ((struct frame *));
482 static void x_update_window_begin P_ ((struct window *));
483 static void x_draw_vertical_border P_ ((struct window *));
484 static void x_after_update_window_line P_ ((struct glyph_row *));
485 static INLINE void take_vertical_position_into_account P_ ((struct it *));
486 static void x_produce_stretch_glyph P_ ((struct it *));
487 static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
488 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
489 enum scroll_bar_part *,
490 Lisp_Object *, Lisp_Object *,
491 unsigned long *));
492
493 /* Flush display of frame F, or of all frames if F is null. */
494
495 static void
496 x_flush (f)
497 struct frame *f;
498 {
499 BLOCK_INPUT;
500 if (f == NULL)
501 {
502 Lisp_Object rest, frame;
503 FOR_EACH_FRAME (rest, frame)
504 x_flush (XFRAME (frame));
505 }
506 else if (FRAME_X_P (f))
507 XFlush (FRAME_X_DISPLAY (f));
508 UNBLOCK_INPUT;
509 }
510
511
512 /* Remove calls to XFlush by defining XFlush to an empty replacement.
513 Calls to XFlush should be unnecessary because the X output buffer
514 is flushed automatically as needed by calls to XPending,
515 XNextEvent, or XWindowEvent according to the XFlush man page.
516 XTread_socket calls XPending. Removing XFlush improves
517 performance. */
518
519 #define XFlush(DISPLAY) (void) 0
520
521 \f
522 /***********************************************************************
523 Debugging
524 ***********************************************************************/
525
526 #if 0
527
528 /* This is a function useful for recording debugging information about
529 the sequence of occurrences in this file. */
530
531 struct record
532 {
533 char *locus;
534 int type;
535 };
536
537 struct record event_record[100];
538
539 int event_record_index;
540
541 record_event (locus, type)
542 char *locus;
543 int type;
544 {
545 if (event_record_index == sizeof (event_record) / sizeof (struct record))
546 event_record_index = 0;
547
548 event_record[event_record_index].locus = locus;
549 event_record[event_record_index].type = type;
550 event_record_index++;
551 }
552
553 #endif /* 0 */
554
555
556 \f
557 /* Return the struct x_display_info corresponding to DPY. */
558
559 struct x_display_info *
560 x_display_info_for_display (dpy)
561 Display *dpy;
562 {
563 struct x_display_info *dpyinfo;
564
565 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
566 if (dpyinfo->display == dpy)
567 return dpyinfo;
568
569 return 0;
570 }
571
572
573 \f
574 /***********************************************************************
575 Starting and ending an update
576 ***********************************************************************/
577
578 /* Start an update of frame F. This function is installed as a hook
579 for update_begin, i.e. it is called when update_begin is called.
580 This function is called prior to calls to x_update_window_begin for
581 each window being updated. Currently, there is nothing to do here
582 because all interesting stuff is done on a window basis. */
583
584 static void
585 x_update_begin (f)
586 struct frame *f;
587 {
588 /* Nothing to do. */
589 }
590
591
592 /* Start update of window W. Set the global variable updated_window
593 to the window being updated and set output_cursor to the cursor
594 position of W. */
595
596 static void
597 x_update_window_begin (w)
598 struct window *w;
599 {
600 struct frame *f = XFRAME (WINDOW_FRAME (w));
601 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
602
603 updated_window = w;
604 set_output_cursor (&w->cursor);
605
606 BLOCK_INPUT;
607
608 if (f == display_info->mouse_face_mouse_frame)
609 {
610 /* Don't do highlighting for mouse motion during the update. */
611 display_info->mouse_face_defer = 1;
612
613 /* If F needs to be redrawn, simply forget about any prior mouse
614 highlighting. */
615 if (FRAME_GARBAGED_P (f))
616 display_info->mouse_face_window = Qnil;
617
618 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
619 their mouse_face_p flag set, which means that they are always
620 unequal to rows in a desired matrix which never have that
621 flag set. So, rows containing mouse-face glyphs are never
622 scrolled, and we don't have to switch the mouse highlight off
623 here to prevent it from being scrolled. */
624
625 /* Can we tell that this update does not affect the window
626 where the mouse highlight is? If so, no need to turn off.
627 Likewise, don't do anything if the frame is garbaged;
628 in that case, the frame's current matrix that we would use
629 is all wrong, and we will redisplay that line anyway. */
630 if (!NILP (display_info->mouse_face_window)
631 && w == XWINDOW (display_info->mouse_face_window))
632 {
633 int i;
634
635 for (i = 0; i < w->desired_matrix->nrows; ++i)
636 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
637 break;
638
639 if (i < w->desired_matrix->nrows)
640 clear_mouse_face (display_info);
641 }
642 #endif /* 0 */
643 }
644
645 UNBLOCK_INPUT;
646 }
647
648
649 /* Draw a vertical window border to the right of window W if W doesn't
650 have vertical scroll bars. */
651
652 static void
653 x_draw_vertical_border (w)
654 struct window *w;
655 {
656 struct frame *f = XFRAME (WINDOW_FRAME (w));
657
658 /* Redraw borders between horizontally adjacent windows. Don't
659 do it for frames with vertical scroll bars because either the
660 right scroll bar of a window, or the left scroll bar of its
661 neighbor will suffice as a border. */
662 if (!WINDOW_RIGHTMOST_P (w)
663 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
664 {
665 int x0, x1, y0, y1;
666
667 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
668 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
669 y1 -= 1;
670
671 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
672 f->output_data.x->normal_gc, x1, y0, x1, y1);
673 }
674 }
675
676
677 /* End update of window W (which is equal to updated_window).
678
679 Draw vertical borders between horizontally adjacent windows, and
680 display W's cursor if CURSOR_ON_P is non-zero.
681
682 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
683 glyphs in mouse-face were overwritten. In that case we have to
684 make sure that the mouse-highlight is properly redrawn.
685
686 W may be a menu bar pseudo-window in case we don't have X toolkit
687 support. Such windows don't have a cursor, so don't display it
688 here. */
689
690 static void
691 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
692 struct window *w;
693 int cursor_on_p, mouse_face_overwritten_p;
694 {
695 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
696
697 if (!w->pseudo_window_p)
698 {
699 BLOCK_INPUT;
700
701 if (cursor_on_p)
702 x_display_and_set_cursor (w, 1, output_cursor.hpos,
703 output_cursor.vpos,
704 output_cursor.x, output_cursor.y);
705
706 x_draw_vertical_border (w);
707 UNBLOCK_INPUT;
708 }
709
710 /* If a row with mouse-face was overwritten, arrange for
711 XTframe_up_to_date to redisplay the mouse highlight. */
712 if (mouse_face_overwritten_p)
713 {
714 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
715 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
716 dpyinfo->mouse_face_window = Qnil;
717 }
718
719 updated_window = NULL;
720 }
721
722
723 /* End update of frame F. This function is installed as a hook in
724 update_end. */
725
726 static void
727 x_update_end (f)
728 struct frame *f;
729 {
730 /* Mouse highlight may be displayed again. */
731 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
732
733 BLOCK_INPUT;
734 XFlush (FRAME_X_DISPLAY (f));
735 UNBLOCK_INPUT;
736 }
737
738
739 /* This function is called from various places in xdisp.c whenever a
740 complete update has been performed. The global variable
741 updated_window is not available here. */
742
743 static void
744 XTframe_up_to_date (f)
745 struct frame *f;
746 {
747 if (FRAME_X_P (f))
748 {
749 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
750
751 if (dpyinfo->mouse_face_deferred_gc
752 || f == dpyinfo->mouse_face_mouse_frame)
753 {
754 BLOCK_INPUT;
755 if (dpyinfo->mouse_face_mouse_frame)
756 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
757 dpyinfo->mouse_face_mouse_x,
758 dpyinfo->mouse_face_mouse_y);
759 dpyinfo->mouse_face_deferred_gc = 0;
760 UNBLOCK_INPUT;
761 }
762 }
763 }
764
765
766 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
767 arrow bitmaps, or clear the areas where they would be displayed
768 before DESIRED_ROW is made current. The window being updated is
769 found in updated_window. This function It is called from
770 update_window_line only if it is known that there are differences
771 between bitmaps to be drawn between current row and DESIRED_ROW. */
772
773 static void
774 x_after_update_window_line (desired_row)
775 struct glyph_row *desired_row;
776 {
777 struct window *w = updated_window;
778
779 xassert (w);
780
781 if (!desired_row->mode_line_p && !w->pseudo_window_p)
782 {
783 struct frame *f;
784 int width;
785
786 BLOCK_INPUT;
787 x_draw_row_bitmaps (w, desired_row);
788
789 /* When a window has disappeared, make sure that no rest of
790 full-width rows stays visible in the internal border. */
791 if (windows_or_buffers_changed
792 && (f = XFRAME (w->frame),
793 width = FRAME_INTERNAL_BORDER_WIDTH (f),
794 width != 0))
795 {
796 int height = desired_row->visible_height;
797 int x = (window_box_right (w, -1)
798 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
799 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
800
801 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
802 x, y, width, height, False);
803 }
804
805 UNBLOCK_INPUT;
806 }
807 }
808
809
810 /* Draw the bitmap WHICH in one of the areas to the left or right of
811 window W. ROW is the glyph row for which to display the bitmap; it
812 determines the vertical position at which the bitmap has to be
813 drawn. */
814
815 static void
816 x_draw_bitmap (w, row, which)
817 struct window *w;
818 struct glyph_row *row;
819 enum bitmap_type which;
820 {
821 struct frame *f = XFRAME (WINDOW_FRAME (w));
822 Display *display = FRAME_X_DISPLAY (f);
823 Window window = FRAME_X_WINDOW (f);
824 int x, y, wd, h, dy;
825 unsigned char *bits;
826 Pixmap pixmap;
827 GC gc = f->output_data.x->normal_gc;
828 struct face *face;
829 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
830
831 /* Must clip because of partially visible lines. */
832 x_clip_to_row (w, row, gc, 1);
833
834 switch (which)
835 {
836 case LEFT_TRUNCATION_BITMAP:
837 wd = left_width;
838 h = left_height;
839 bits = left_bits;
840 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
841 - wd
842 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
843 break;
844
845 case OVERLAY_ARROW_BITMAP:
846 wd = left_width;
847 h = left_height;
848 bits = ov_bits;
849 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
850 - wd
851 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
852 break;
853
854 case RIGHT_TRUNCATION_BITMAP:
855 wd = right_width;
856 h = right_height;
857 bits = right_bits;
858 x = window_box_right (w, -1);
859 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
860 break;
861
862 case CONTINUED_LINE_BITMAP:
863 wd = right_width;
864 h = right_height;
865 bits = continued_bits;
866 x = window_box_right (w, -1);
867 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
868 break;
869
870 case CONTINUATION_LINE_BITMAP:
871 wd = continuation_width;
872 h = continuation_height;
873 bits = continuation_bits;
874 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
875 - wd
876 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
877 break;
878
879 case ZV_LINE_BITMAP:
880 wd = zv_width;
881 h = zv_height;
882 bits = zv_bits;
883 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
884 - wd
885 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
886 break;
887
888 default:
889 abort ();
890 }
891
892 /* Convert to frame coordinates. Set dy to the offset in the row to
893 start drawing the bitmap. */
894 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
895 dy = (row->height - h) / 2;
896
897 /* Draw the bitmap. I believe these small pixmaps can be cached
898 by the server. */
899 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
900 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
901 face->foreground,
902 face->background, depth);
903 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
904 XFreePixmap (display, pixmap);
905 XSetClipMask (display, gc, None);
906 }
907
908
909 /* Draw flags bitmaps for glyph row ROW on window W. Call this
910 function with input blocked. */
911
912 static void
913 x_draw_row_bitmaps (w, row)
914 struct window *w;
915 struct glyph_row *row;
916 {
917 struct frame *f = XFRAME (w->frame);
918 enum bitmap_type bitmap;
919 struct face *face;
920 int header_line_height = -1;
921
922 xassert (interrupt_input_blocked);
923
924 /* If row is completely invisible, because of vscrolling, we
925 don't have to draw anything. */
926 if (row->visible_height <= 0)
927 return;
928
929 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
930 PREPARE_FACE_FOR_DISPLAY (f, face);
931
932 /* Decide which bitmap to draw at the left side. */
933 if (row->overlay_arrow_p)
934 bitmap = OVERLAY_ARROW_BITMAP;
935 else if (row->truncated_on_left_p)
936 bitmap = LEFT_TRUNCATION_BITMAP;
937 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
938 bitmap = CONTINUATION_LINE_BITMAP;
939 else if (row->indicate_empty_line_p)
940 bitmap = ZV_LINE_BITMAP;
941 else
942 bitmap = NO_BITMAP;
943
944 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
945 the flags area. */
946 if (bitmap == NO_BITMAP
947 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
948 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
949 {
950 /* If W has a vertical border to its left, don't draw over it. */
951 int border = ((XFASTINT (w->left) > 0
952 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
953 ? 1 : 0);
954 int left = window_box_left (w, -1);
955
956 if (header_line_height < 0)
957 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
958
959 /* In case the same realized face is used for bitmap areas and
960 for something displayed in the text (e.g. face `region' on
961 mono-displays, the fill style may have been changed to
962 FillSolid in x_draw_glyph_string_background. */
963 if (face->stipple)
964 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
965 else
966 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
967
968 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
969 face->gc,
970 (left
971 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
972 + border),
973 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
974 row->y)),
975 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
976 row->visible_height);
977 if (!face->stipple)
978 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
979 }
980
981 /* Draw the left bitmap. */
982 if (bitmap != NO_BITMAP)
983 x_draw_bitmap (w, row, bitmap);
984
985 /* Decide which bitmap to draw at the right side. */
986 if (row->truncated_on_right_p)
987 bitmap = RIGHT_TRUNCATION_BITMAP;
988 else if (row->continued_p)
989 bitmap = CONTINUED_LINE_BITMAP;
990 else
991 bitmap = NO_BITMAP;
992
993 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
994 the flags area. */
995 if (bitmap == NO_BITMAP
996 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
997 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
998 {
999 int right = window_box_right (w, -1);
1000
1001 if (header_line_height < 0)
1002 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1003
1004 /* In case the same realized face is used for bitmap areas and
1005 for something displayed in the text (e.g. face `region' on
1006 mono-displays, the fill style may have been changed to
1007 FillSolid in x_draw_glyph_string_background. */
1008 if (face->stipple)
1009 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
1010 else
1011 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
1012 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1013 face->gc,
1014 right,
1015 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
1016 row->y)),
1017 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
1018 row->visible_height);
1019 if (!face->stipple)
1020 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
1021 }
1022
1023 /* Draw the right bitmap. */
1024 if (bitmap != NO_BITMAP)
1025 x_draw_bitmap (w, row, bitmap);
1026 }
1027
1028 \f
1029 /***********************************************************************
1030 Line Highlighting
1031 ***********************************************************************/
1032
1033 /* External interface to control of standout mode. Not used for X
1034 frames. Aborts when called. */
1035
1036 static void
1037 XTreassert_line_highlight (new, vpos)
1038 int new, vpos;
1039 {
1040 abort ();
1041 }
1042
1043
1044 /* Call this when about to modify line at position VPOS and change
1045 whether it is highlighted. Not used for X frames. Aborts when
1046 called. */
1047
1048 static void
1049 x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1050 int new_highlight, vpos, y, first_unused_hpos;
1051 {
1052 abort ();
1053 }
1054
1055
1056 /* This is called when starting Emacs and when restarting after
1057 suspend. When starting Emacs, no X window is mapped. And nothing
1058 must be done to Emacs's own window if it is suspended (though that
1059 rarely happens). */
1060
1061 static void
1062 XTset_terminal_modes ()
1063 {
1064 }
1065
1066 /* This is called when exiting or suspending Emacs. Exiting will make
1067 the X-windows go away, and suspending requires no action. */
1068
1069 static void
1070 XTreset_terminal_modes ()
1071 {
1072 }
1073
1074
1075 \f
1076 /***********************************************************************
1077 Output Cursor
1078 ***********************************************************************/
1079
1080 /* Set the global variable output_cursor to CURSOR. All cursor
1081 positions are relative to updated_window. */
1082
1083 static void
1084 set_output_cursor (cursor)
1085 struct cursor_pos *cursor;
1086 {
1087 output_cursor.hpos = cursor->hpos;
1088 output_cursor.vpos = cursor->vpos;
1089 output_cursor.x = cursor->x;
1090 output_cursor.y = cursor->y;
1091 }
1092
1093
1094 /* Set a nominal cursor position.
1095
1096 HPOS and VPOS are column/row positions in a window glyph matrix. X
1097 and Y are window text area relative pixel positions.
1098
1099 If this is done during an update, updated_window will contain the
1100 window that is being updated and the position is the future output
1101 cursor position for that window. If updated_window is null, use
1102 selected_window and display the cursor at the given position. */
1103
1104 static void
1105 XTcursor_to (vpos, hpos, y, x)
1106 int vpos, hpos, y, x;
1107 {
1108 struct window *w;
1109
1110 /* If updated_window is not set, work on selected_window. */
1111 if (updated_window)
1112 w = updated_window;
1113 else
1114 w = XWINDOW (selected_window);
1115
1116 /* Set the output cursor. */
1117 output_cursor.hpos = hpos;
1118 output_cursor.vpos = vpos;
1119 output_cursor.x = x;
1120 output_cursor.y = y;
1121
1122 /* If not called as part of an update, really display the cursor.
1123 This will also set the cursor position of W. */
1124 if (updated_window == NULL)
1125 {
1126 BLOCK_INPUT;
1127 x_display_cursor (w, 1, hpos, vpos, x, y);
1128 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1129 UNBLOCK_INPUT;
1130 }
1131 }
1132
1133
1134 \f
1135 /***********************************************************************
1136 Display Iterator
1137 ***********************************************************************/
1138
1139 /* Function prototypes of this page. */
1140
1141 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1142 struct glyph *,
1143 XChar2b *,
1144 int *));
1145 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1146 int, XChar2b *, int));
1147 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1148 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1149 static void x_append_glyph P_ ((struct it *));
1150 static void x_append_composite_glyph P_ ((struct it *));
1151 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1152 int, int, double));
1153 static void x_produce_glyphs P_ ((struct it *));
1154 static void x_produce_image_glyph P_ ((struct it *it));
1155
1156
1157 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1158 is not contained in the font. */
1159
1160 static INLINE XCharStruct *
1161 x_per_char_metric (font, char2b)
1162 XFontStruct *font;
1163 XChar2b *char2b;
1164 {
1165 /* The result metric information. */
1166 XCharStruct *pcm = NULL;
1167
1168 xassert (font && char2b);
1169
1170 if (font->per_char != NULL)
1171 {
1172 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1173 {
1174 /* min_char_or_byte2 specifies the linear character index
1175 corresponding to the first element of the per_char array,
1176 max_char_or_byte2 is the index of the last character. A
1177 character with non-zero CHAR2B->byte1 is not in the font.
1178 A character with byte2 less than min_char_or_byte2 or
1179 greater max_char_or_byte2 is not in the font. */
1180 if (char2b->byte1 == 0
1181 && char2b->byte2 >= font->min_char_or_byte2
1182 && char2b->byte2 <= font->max_char_or_byte2)
1183 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1184 }
1185 else
1186 {
1187 /* If either min_byte1 or max_byte1 are nonzero, both
1188 min_char_or_byte2 and max_char_or_byte2 are less than
1189 256, and the 2-byte character index values corresponding
1190 to the per_char array element N (counting from 0) are:
1191
1192 byte1 = N/D + min_byte1
1193 byte2 = N\D + min_char_or_byte2
1194
1195 where:
1196
1197 D = max_char_or_byte2 - min_char_or_byte2 + 1
1198 / = integer division
1199 \ = integer modulus */
1200 if (char2b->byte1 >= font->min_byte1
1201 && char2b->byte1 <= font->max_byte1
1202 && char2b->byte2 >= font->min_char_or_byte2
1203 && char2b->byte2 <= font->max_char_or_byte2)
1204 {
1205 pcm = (font->per_char
1206 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1207 * (char2b->byte1 - font->min_byte1))
1208 + (char2b->byte2 - font->min_char_or_byte2));
1209 }
1210 }
1211 }
1212 else
1213 {
1214 /* If the per_char pointer is null, all glyphs between the first
1215 and last character indexes inclusive have the same
1216 information, as given by both min_bounds and max_bounds. */
1217 if (char2b->byte2 >= font->min_char_or_byte2
1218 && char2b->byte2 <= font->max_char_or_byte2)
1219 pcm = &font->max_bounds;
1220 }
1221
1222 return ((pcm == NULL
1223 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1224 ? NULL : pcm);
1225 }
1226
1227
1228 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1229 the two-byte form of C. Encoding is returned in *CHAR2B. */
1230
1231 static INLINE void
1232 x_encode_char (c, char2b, font_info)
1233 int c;
1234 XChar2b *char2b;
1235 struct font_info *font_info;
1236 {
1237 int charset = CHAR_CHARSET (c);
1238 XFontStruct *font = font_info->font;
1239
1240 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1241 This may be either a program in a special encoder language or a
1242 fixed encoding. */
1243 if (font_info->font_encoder)
1244 {
1245 /* It's a program. */
1246 struct ccl_program *ccl = font_info->font_encoder;
1247
1248 if (CHARSET_DIMENSION (charset) == 1)
1249 {
1250 ccl->reg[0] = charset;
1251 ccl->reg[1] = char2b->byte2;
1252 }
1253 else
1254 {
1255 ccl->reg[0] = charset;
1256 ccl->reg[1] = char2b->byte1;
1257 ccl->reg[2] = char2b->byte2;
1258 }
1259
1260 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1261
1262 /* We assume that MSBs are appropriately set/reset by CCL
1263 program. */
1264 if (font->max_byte1 == 0) /* 1-byte font */
1265 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1266 else
1267 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1268 }
1269 else if (font_info->encoding[charset])
1270 {
1271 /* Fixed encoding scheme. See fontset.h for the meaning of the
1272 encoding numbers. */
1273 int enc = font_info->encoding[charset];
1274
1275 if ((enc == 1 || enc == 2)
1276 && CHARSET_DIMENSION (charset) == 2)
1277 char2b->byte1 |= 0x80;
1278
1279 if (enc == 1 || enc == 3)
1280 char2b->byte2 |= 0x80;
1281 }
1282 }
1283
1284
1285 /* Get face and two-byte form of character C in face FACE_ID on frame
1286 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1287 means we want to display multibyte text. Value is a pointer to a
1288 realized face that is ready for display. */
1289
1290 static INLINE struct face *
1291 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1292 struct frame *f;
1293 int c, face_id;
1294 XChar2b *char2b;
1295 int multibyte_p;
1296 {
1297 struct face *face = FACE_FROM_ID (f, face_id);
1298
1299 if (!multibyte_p)
1300 {
1301 /* Unibyte case. We don't have to encode, but we have to make
1302 sure to use a face suitable for unibyte. */
1303 char2b->byte1 = 0;
1304 char2b->byte2 = c;
1305 face_id = FACE_FOR_CHAR (f, face, c);
1306 face = FACE_FROM_ID (f, face_id);
1307 }
1308 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1309 {
1310 /* Case of ASCII in a face known to fit ASCII. */
1311 char2b->byte1 = 0;
1312 char2b->byte2 = c;
1313 }
1314 else
1315 {
1316 int c1, c2, charset;
1317
1318 /* Split characters into bytes. If c2 is -1 afterwards, C is
1319 really a one-byte character so that byte1 is zero. */
1320 SPLIT_CHAR (c, charset, c1, c2);
1321 if (c2 > 0)
1322 char2b->byte1 = c1, char2b->byte2 = c2;
1323 else
1324 char2b->byte1 = 0, char2b->byte2 = c1;
1325
1326 /* Maybe encode the character in *CHAR2B. */
1327 if (face->font != NULL)
1328 {
1329 struct font_info *font_info
1330 = FONT_INFO_FROM_ID (f, face->font_info_id);
1331 if (font_info)
1332 x_encode_char (c, char2b, font_info);
1333 }
1334 }
1335
1336 /* Make sure X resources of the face are allocated. */
1337 xassert (face != NULL);
1338 PREPARE_FACE_FOR_DISPLAY (f, face);
1339
1340 return face;
1341 }
1342
1343
1344 /* Get face and two-byte form of character glyph GLYPH on frame F.
1345 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1346 a pointer to a realized face that is ready for display. */
1347
1348 static INLINE struct face *
1349 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1350 struct frame *f;
1351 struct glyph *glyph;
1352 XChar2b *char2b;
1353 int *two_byte_p;
1354 {
1355 struct face *face;
1356
1357 xassert (glyph->type == CHAR_GLYPH);
1358 face = FACE_FROM_ID (f, glyph->face_id);
1359
1360 if (two_byte_p)
1361 *two_byte_p = 0;
1362
1363 if (!glyph->multibyte_p)
1364 {
1365 /* Unibyte case. We don't have to encode, but we have to make
1366 sure to use a face suitable for unibyte. */
1367 char2b->byte1 = 0;
1368 char2b->byte2 = glyph->u.ch;
1369 }
1370 else if (glyph->u.ch < 128
1371 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1372 {
1373 /* Case of ASCII in a face known to fit ASCII. */
1374 char2b->byte1 = 0;
1375 char2b->byte2 = glyph->u.ch;
1376 }
1377 else
1378 {
1379 int c1, c2, charset;
1380
1381 /* Split characters into bytes. If c2 is -1 afterwards, C is
1382 really a one-byte character so that byte1 is zero. */
1383 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1384 if (c2 > 0)
1385 char2b->byte1 = c1, char2b->byte2 = c2;
1386 else
1387 char2b->byte1 = 0, char2b->byte2 = c1;
1388
1389 /* Maybe encode the character in *CHAR2B. */
1390 if (charset != CHARSET_ASCII)
1391 {
1392 struct font_info *font_info
1393 = FONT_INFO_FROM_ID (f, face->font_info_id);
1394 if (font_info)
1395 {
1396 x_encode_char (glyph->u.ch, char2b, font_info);
1397 if (two_byte_p)
1398 *two_byte_p
1399 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1400 }
1401 }
1402 }
1403
1404 /* Make sure X resources of the face are allocated. */
1405 xassert (face != NULL);
1406 PREPARE_FACE_FOR_DISPLAY (f, face);
1407 return face;
1408 }
1409
1410
1411 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1412 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1413
1414 static INLINE void
1415 x_append_glyph (it)
1416 struct it *it;
1417 {
1418 struct glyph *glyph;
1419 enum glyph_row_area area = it->area;
1420
1421 xassert (it->glyph_row);
1422 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1423
1424 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1425 if (glyph < it->glyph_row->glyphs[area + 1])
1426 {
1427 glyph->charpos = CHARPOS (it->position);
1428 glyph->object = it->object;
1429 glyph->pixel_width = it->pixel_width;
1430 glyph->voffset = it->voffset;
1431 glyph->type = CHAR_GLYPH;
1432 glyph->multibyte_p = it->multibyte_p;
1433 glyph->left_box_line_p = it->start_of_box_run_p;
1434 glyph->right_box_line_p = it->end_of_box_run_p;
1435 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1436 || it->phys_descent > it->descent);
1437 glyph->padding_p = 0;
1438 glyph->glyph_not_available_p = it->glyph_not_available_p;
1439 glyph->face_id = it->face_id;
1440 glyph->u.ch = it->char_to_display;
1441 ++it->glyph_row->used[area];
1442 }
1443 }
1444
1445 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1446 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1447
1448 static INLINE void
1449 x_append_composite_glyph (it)
1450 struct it *it;
1451 {
1452 struct glyph *glyph;
1453 enum glyph_row_area area = it->area;
1454
1455 xassert (it->glyph_row);
1456
1457 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1458 if (glyph < it->glyph_row->glyphs[area + 1])
1459 {
1460 glyph->charpos = CHARPOS (it->position);
1461 glyph->object = it->object;
1462 glyph->pixel_width = it->pixel_width;
1463 glyph->voffset = it->voffset;
1464 glyph->type = COMPOSITE_GLYPH;
1465 glyph->multibyte_p = it->multibyte_p;
1466 glyph->left_box_line_p = it->start_of_box_run_p;
1467 glyph->right_box_line_p = it->end_of_box_run_p;
1468 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1469 || it->phys_descent > it->descent);
1470 glyph->padding_p = 0;
1471 glyph->glyph_not_available_p = 0;
1472 glyph->face_id = it->face_id;
1473 glyph->u.cmp_id = it->cmp_id;
1474 ++it->glyph_row->used[area];
1475 }
1476 }
1477
1478
1479 /* Change IT->ascent and IT->height according to the setting of
1480 IT->voffset. */
1481
1482 static INLINE void
1483 take_vertical_position_into_account (it)
1484 struct it *it;
1485 {
1486 if (it->voffset)
1487 {
1488 if (it->voffset < 0)
1489 /* Increase the ascent so that we can display the text higher
1490 in the line. */
1491 it->ascent += abs (it->voffset);
1492 else
1493 /* Increase the descent so that we can display the text lower
1494 in the line. */
1495 it->descent += it->voffset;
1496 }
1497 }
1498
1499
1500 /* Produce glyphs/get display metrics for the image IT is loaded with.
1501 See the description of struct display_iterator in dispextern.h for
1502 an overview of struct display_iterator. */
1503
1504 static void
1505 x_produce_image_glyph (it)
1506 struct it *it;
1507 {
1508 struct image *img;
1509 struct face *face;
1510
1511 xassert (it->what == IT_IMAGE);
1512
1513 face = FACE_FROM_ID (it->f, it->face_id);
1514 img = IMAGE_FROM_ID (it->f, it->image_id);
1515 xassert (img);
1516
1517 /* Make sure X resources of the face and image are loaded. */
1518 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1519 prepare_image_for_display (it->f, img);
1520
1521 it->ascent = it->phys_ascent = image_ascent (img, face);
1522 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1523 it->pixel_width = img->width + 2 * img->hmargin;
1524
1525 it->nglyphs = 1;
1526
1527 if (face->box != FACE_NO_BOX)
1528 {
1529 if (face->box_line_width > 0)
1530 {
1531 it->ascent += face->box_line_width;
1532 it->descent += face->box_line_width;
1533 }
1534
1535 if (it->start_of_box_run_p)
1536 it->pixel_width += abs (face->box_line_width);
1537 if (it->end_of_box_run_p)
1538 it->pixel_width += abs (face->box_line_width);
1539 }
1540
1541 take_vertical_position_into_account (it);
1542
1543 if (it->glyph_row)
1544 {
1545 struct glyph *glyph;
1546 enum glyph_row_area area = it->area;
1547
1548 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1549 if (glyph < it->glyph_row->glyphs[area + 1])
1550 {
1551 glyph->charpos = CHARPOS (it->position);
1552 glyph->object = it->object;
1553 glyph->pixel_width = it->pixel_width;
1554 glyph->voffset = it->voffset;
1555 glyph->type = IMAGE_GLYPH;
1556 glyph->multibyte_p = it->multibyte_p;
1557 glyph->left_box_line_p = it->start_of_box_run_p;
1558 glyph->right_box_line_p = it->end_of_box_run_p;
1559 glyph->overlaps_vertically_p = 0;
1560 glyph->padding_p = 0;
1561 glyph->glyph_not_available_p = 0;
1562 glyph->face_id = it->face_id;
1563 glyph->u.img_id = img->id;
1564 ++it->glyph_row->used[area];
1565 }
1566 }
1567 }
1568
1569
1570 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1571 of the glyph, WIDTH and HEIGHT are the width and height of the
1572 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1573 ascent of the glyph (0 <= ASCENT <= 1). */
1574
1575 static void
1576 x_append_stretch_glyph (it, object, width, height, ascent)
1577 struct it *it;
1578 Lisp_Object object;
1579 int width, height;
1580 double ascent;
1581 {
1582 struct glyph *glyph;
1583 enum glyph_row_area area = it->area;
1584
1585 xassert (ascent >= 0 && ascent <= 1);
1586
1587 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1588 if (glyph < it->glyph_row->glyphs[area + 1])
1589 {
1590 glyph->charpos = CHARPOS (it->position);
1591 glyph->object = object;
1592 glyph->pixel_width = width;
1593 glyph->voffset = it->voffset;
1594 glyph->type = STRETCH_GLYPH;
1595 glyph->multibyte_p = it->multibyte_p;
1596 glyph->left_box_line_p = it->start_of_box_run_p;
1597 glyph->right_box_line_p = it->end_of_box_run_p;
1598 glyph->overlaps_vertically_p = 0;
1599 glyph->padding_p = 0;
1600 glyph->glyph_not_available_p = 0;
1601 glyph->face_id = it->face_id;
1602 glyph->u.stretch.ascent = height * ascent;
1603 glyph->u.stretch.height = height;
1604 ++it->glyph_row->used[area];
1605 }
1606 }
1607
1608
1609 /* Produce a stretch glyph for iterator IT. IT->object is the value
1610 of the glyph property displayed. The value must be a list
1611 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1612 being recognized:
1613
1614 1. `:width WIDTH' specifies that the space should be WIDTH *
1615 canonical char width wide. WIDTH may be an integer or floating
1616 point number.
1617
1618 2. `:relative-width FACTOR' specifies that the width of the stretch
1619 should be computed from the width of the first character having the
1620 `glyph' property, and should be FACTOR times that width.
1621
1622 3. `:align-to HPOS' specifies that the space should be wide enough
1623 to reach HPOS, a value in canonical character units.
1624
1625 Exactly one of the above pairs must be present.
1626
1627 4. `:height HEIGHT' specifies that the height of the stretch produced
1628 should be HEIGHT, measured in canonical character units.
1629
1630 5. `:relative-height FACTOR' specifies that the height of the the
1631 stretch should be FACTOR times the height of the characters having
1632 the glyph property.
1633
1634 Either none or exactly one of 4 or 5 must be present.
1635
1636 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1637 of the stretch should be used for the ascent of the stretch.
1638 ASCENT must be in the range 0 <= ASCENT <= 100. */
1639
1640 #define NUMVAL(X) \
1641 ((INTEGERP (X) || FLOATP (X)) \
1642 ? XFLOATINT (X) \
1643 : - 1)
1644
1645
1646 static void
1647 x_produce_stretch_glyph (it)
1648 struct it *it;
1649 {
1650 /* (space :width WIDTH :height HEIGHT. */
1651 #if GLYPH_DEBUG
1652 extern Lisp_Object Qspace;
1653 #endif
1654 extern Lisp_Object QCwidth, QCheight, QCascent;
1655 extern Lisp_Object QCrelative_width, QCrelative_height;
1656 extern Lisp_Object QCalign_to;
1657 Lisp_Object prop, plist;
1658 double width = 0, height = 0, ascent = 0;
1659 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1660 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1661
1662 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1663
1664 /* List should start with `space'. */
1665 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1666 plist = XCDR (it->object);
1667
1668 /* Compute the width of the stretch. */
1669 if (prop = Fplist_get (plist, QCwidth),
1670 NUMVAL (prop) > 0)
1671 /* Absolute width `:width WIDTH' specified and valid. */
1672 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1673 else if (prop = Fplist_get (plist, QCrelative_width),
1674 NUMVAL (prop) > 0)
1675 {
1676 /* Relative width `:relative-width FACTOR' specified and valid.
1677 Compute the width of the characters having the `glyph'
1678 property. */
1679 struct it it2;
1680 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1681
1682 it2 = *it;
1683 if (it->multibyte_p)
1684 {
1685 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1686 - IT_BYTEPOS (*it));
1687 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1688 }
1689 else
1690 it2.c = *p, it2.len = 1;
1691
1692 it2.glyph_row = NULL;
1693 it2.what = IT_CHARACTER;
1694 x_produce_glyphs (&it2);
1695 width = NUMVAL (prop) * it2.pixel_width;
1696 }
1697 else if (prop = Fplist_get (plist, QCalign_to),
1698 NUMVAL (prop) > 0)
1699 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1700 else
1701 /* Nothing specified -> width defaults to canonical char width. */
1702 width = CANON_X_UNIT (it->f);
1703
1704 /* Compute height. */
1705 if (prop = Fplist_get (plist, QCheight),
1706 NUMVAL (prop) > 0)
1707 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1708 else if (prop = Fplist_get (plist, QCrelative_height),
1709 NUMVAL (prop) > 0)
1710 height = FONT_HEIGHT (font) * NUMVAL (prop);
1711 else
1712 height = FONT_HEIGHT (font);
1713
1714 /* Compute percentage of height used for ascent. If
1715 `:ascent ASCENT' is present and valid, use that. Otherwise,
1716 derive the ascent from the font in use. */
1717 if (prop = Fplist_get (plist, QCascent),
1718 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1719 ascent = NUMVAL (prop) / 100.0;
1720 else
1721 ascent = (double) font->ascent / FONT_HEIGHT (font);
1722
1723 if (width <= 0)
1724 width = 1;
1725 if (height <= 0)
1726 height = 1;
1727
1728 if (it->glyph_row)
1729 {
1730 Lisp_Object object = it->stack[it->sp - 1].string;
1731 if (!STRINGP (object))
1732 object = it->w->buffer;
1733 x_append_stretch_glyph (it, object, width, height, ascent);
1734 }
1735
1736 it->pixel_width = width;
1737 it->ascent = it->phys_ascent = height * ascent;
1738 it->descent = it->phys_descent = height - it->ascent;
1739 it->nglyphs = 1;
1740
1741 if (face->box != FACE_NO_BOX)
1742 {
1743 if (face->box_line_width > 0)
1744 {
1745 it->ascent += face->box_line_width;
1746 it->descent += face->box_line_width;
1747 }
1748
1749 if (it->start_of_box_run_p)
1750 it->pixel_width += abs (face->box_line_width);
1751 if (it->end_of_box_run_p)
1752 it->pixel_width += abs (face->box_line_width);
1753 }
1754
1755 take_vertical_position_into_account (it);
1756 }
1757
1758 /* Return proper value to be used as baseline offset of font that has
1759 ASCENT and DESCENT to draw characters by the font at the vertical
1760 center of the line of frame F.
1761
1762 Here, out task is to find the value of BOFF in the following figure;
1763
1764 -------------------------+-----------+-
1765 -+-+---------+-+ | |
1766 | | | | | |
1767 | | | | F_ASCENT F_HEIGHT
1768 | | | ASCENT | |
1769 HEIGHT | | | | |
1770 | | |-|-+------+-----------|------- baseline
1771 | | | | BOFF | |
1772 | |---------|-+-+ | |
1773 | | | DESCENT | |
1774 -+-+---------+-+ F_DESCENT |
1775 -------------------------+-----------+-
1776
1777 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1778 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1779 DESCENT = FONT->descent
1780 HEIGHT = FONT_HEIGHT (FONT)
1781 F_DESCENT = (F->output_data.x->font->descent
1782 - F->output_data.x->baseline_offset)
1783 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1784 */
1785
1786 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1787 ((FONT)->descent \
1788 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1789 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1790 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1791
1792 /* Produce glyphs/get display metrics for the display element IT is
1793 loaded with. See the description of struct display_iterator in
1794 dispextern.h for an overview of struct display_iterator. */
1795
1796 static void
1797 x_produce_glyphs (it)
1798 struct it *it;
1799 {
1800 it->glyph_not_available_p = 0;
1801
1802 if (it->what == IT_CHARACTER)
1803 {
1804 XChar2b char2b;
1805 XFontStruct *font;
1806 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1807 XCharStruct *pcm;
1808 int font_not_found_p;
1809 struct font_info *font_info;
1810 int boff; /* baseline offset */
1811 /* We may change it->multibyte_p upon unibyte<->multibyte
1812 conversion. So, save the current value now and restore it
1813 later.
1814
1815 Note: It seems that we don't have to record multibyte_p in
1816 struct glyph because the character code itself tells if or
1817 not the character is multibyte. Thus, in the future, we must
1818 consider eliminating the field `multibyte_p' in the struct
1819 glyph.
1820 */
1821 int saved_multibyte_p = it->multibyte_p;
1822
1823 /* Maybe translate single-byte characters to multibyte, or the
1824 other way. */
1825 it->char_to_display = it->c;
1826 if (!ASCII_BYTE_P (it->c))
1827 {
1828 if (unibyte_display_via_language_environment
1829 && SINGLE_BYTE_CHAR_P (it->c)
1830 && (it->c >= 0240
1831 || !NILP (Vnonascii_translation_table)))
1832 {
1833 it->char_to_display = unibyte_char_to_multibyte (it->c);
1834 it->multibyte_p = 1;
1835 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1836 face = FACE_FROM_ID (it->f, it->face_id);
1837 }
1838 else if (!SINGLE_BYTE_CHAR_P (it->c)
1839 && !it->multibyte_p)
1840 {
1841 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
1842 it->multibyte_p = 0;
1843 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1844 face = FACE_FROM_ID (it->f, it->face_id);
1845 }
1846 }
1847
1848 /* Get font to use. Encode IT->char_to_display. */
1849 x_get_char_face_and_encoding (it->f, it->char_to_display,
1850 it->face_id, &char2b,
1851 it->multibyte_p);
1852 font = face->font;
1853
1854 /* When no suitable font found, use the default font. */
1855 font_not_found_p = font == NULL;
1856 if (font_not_found_p)
1857 {
1858 font = FRAME_FONT (it->f);
1859 boff = it->f->output_data.x->baseline_offset;
1860 font_info = NULL;
1861 }
1862 else
1863 {
1864 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1865 boff = font_info->baseline_offset;
1866 if (font_info->vertical_centering)
1867 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1868 }
1869
1870 if (it->char_to_display >= ' '
1871 && (!it->multibyte_p || it->char_to_display < 128))
1872 {
1873 /* Either unibyte or ASCII. */
1874 int stretched_p;
1875
1876 it->nglyphs = 1;
1877
1878 pcm = x_per_char_metric (font, &char2b);
1879 it->ascent = font->ascent + boff;
1880 it->descent = font->descent - boff;
1881
1882 if (pcm)
1883 {
1884 it->phys_ascent = pcm->ascent + boff;
1885 it->phys_descent = pcm->descent - boff;
1886 it->pixel_width = pcm->width;
1887 }
1888 else
1889 {
1890 it->glyph_not_available_p = 1;
1891 it->phys_ascent = font->ascent + boff;
1892 it->phys_descent = font->descent - boff;
1893 it->pixel_width = FONT_WIDTH (font);
1894 }
1895
1896 /* If this is a space inside a region of text with
1897 `space-width' property, change its width. */
1898 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1899 if (stretched_p)
1900 it->pixel_width *= XFLOATINT (it->space_width);
1901
1902 /* If face has a box, add the box thickness to the character
1903 height. If character has a box line to the left and/or
1904 right, add the box line width to the character's width. */
1905 if (face->box != FACE_NO_BOX)
1906 {
1907 int thick = face->box_line_width;
1908
1909 if (thick > 0)
1910 {
1911 it->ascent += thick;
1912 it->descent += thick;
1913 }
1914 else
1915 thick = -thick;
1916
1917 if (it->start_of_box_run_p)
1918 it->pixel_width += thick;
1919 if (it->end_of_box_run_p)
1920 it->pixel_width += thick;
1921 }
1922
1923 /* If face has an overline, add the height of the overline
1924 (1 pixel) and a 1 pixel margin to the character height. */
1925 if (face->overline_p)
1926 it->ascent += 2;
1927
1928 take_vertical_position_into_account (it);
1929
1930 /* If we have to actually produce glyphs, do it. */
1931 if (it->glyph_row)
1932 {
1933 if (stretched_p)
1934 {
1935 /* Translate a space with a `space-width' property
1936 into a stretch glyph. */
1937 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1938 x_append_stretch_glyph (it, it->object, it->pixel_width,
1939 it->ascent + it->descent, ascent);
1940 }
1941 else
1942 x_append_glyph (it);
1943
1944 /* If characters with lbearing or rbearing are displayed
1945 in this line, record that fact in a flag of the
1946 glyph row. This is used to optimize X output code. */
1947 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1948 it->glyph_row->contains_overlapping_glyphs_p = 1;
1949 }
1950 }
1951 else if (it->char_to_display == '\n')
1952 {
1953 /* A newline has no width but we need the height of the line. */
1954 it->pixel_width = 0;
1955 it->nglyphs = 0;
1956 it->ascent = it->phys_ascent = font->ascent + boff;
1957 it->descent = it->phys_descent = font->descent - boff;
1958
1959 if (face->box != FACE_NO_BOX
1960 && face->box_line_width > 0)
1961 {
1962 it->ascent += face->box_line_width;
1963 it->descent += face->box_line_width;
1964 }
1965 }
1966 else if (it->char_to_display == '\t')
1967 {
1968 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1969 int x = it->current_x + it->continuation_lines_width;
1970 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1971
1972 /* If the distance from the current position to the next tab
1973 stop is less than a canonical character width, use the
1974 tab stop after that. */
1975 if (next_tab_x - x < CANON_X_UNIT (it->f))
1976 next_tab_x += tab_width;
1977
1978 it->pixel_width = next_tab_x - x;
1979 it->nglyphs = 1;
1980 it->ascent = it->phys_ascent = font->ascent + boff;
1981 it->descent = it->phys_descent = font->descent - boff;
1982
1983 if (it->glyph_row)
1984 {
1985 double ascent = (double) it->ascent / (it->ascent + it->descent);
1986 x_append_stretch_glyph (it, it->object, it->pixel_width,
1987 it->ascent + it->descent, ascent);
1988 }
1989 }
1990 else
1991 {
1992 /* A multi-byte character. Assume that the display width of the
1993 character is the width of the character multiplied by the
1994 width of the font. */
1995
1996 /* If we found a font, this font should give us the right
1997 metrics. If we didn't find a font, use the frame's
1998 default font and calculate the width of the character
1999 from the charset width; this is what old redisplay code
2000 did. */
2001 pcm = x_per_char_metric (font, &char2b);
2002 if (font_not_found_p || !pcm)
2003 {
2004 int charset = CHAR_CHARSET (it->char_to_display);
2005
2006 it->glyph_not_available_p = 1;
2007 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
2008 * CHARSET_WIDTH (charset));
2009 it->phys_ascent = font->ascent + boff;
2010 it->phys_descent = font->descent - boff;
2011 }
2012 else
2013 {
2014 it->pixel_width = pcm->width;
2015 it->phys_ascent = pcm->ascent + boff;
2016 it->phys_descent = pcm->descent - boff;
2017 if (it->glyph_row
2018 && (pcm->lbearing < 0
2019 || pcm->rbearing > pcm->width))
2020 it->glyph_row->contains_overlapping_glyphs_p = 1;
2021 }
2022 it->nglyphs = 1;
2023 it->ascent = font->ascent + boff;
2024 it->descent = font->descent - boff;
2025 if (face->box != FACE_NO_BOX)
2026 {
2027 int thick = face->box_line_width;
2028
2029 if (thick > 0)
2030 {
2031 it->ascent += thick;
2032 it->descent += thick;
2033 }
2034 else
2035 thick = - thick;
2036
2037 if (it->start_of_box_run_p)
2038 it->pixel_width += thick;
2039 if (it->end_of_box_run_p)
2040 it->pixel_width += thick;
2041 }
2042
2043 /* If face has an overline, add the height of the overline
2044 (1 pixel) and a 1 pixel margin to the character height. */
2045 if (face->overline_p)
2046 it->ascent += 2;
2047
2048 take_vertical_position_into_account (it);
2049
2050 if (it->glyph_row)
2051 x_append_glyph (it);
2052 }
2053 it->multibyte_p = saved_multibyte_p;
2054 }
2055 else if (it->what == IT_COMPOSITION)
2056 {
2057 /* Note: A composition is represented as one glyph in the
2058 glyph matrix. There are no padding glyphs. */
2059 XChar2b char2b;
2060 XFontStruct *font;
2061 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2062 XCharStruct *pcm;
2063 int font_not_found_p;
2064 struct font_info *font_info;
2065 int boff; /* baseline offset */
2066 struct composition *cmp = composition_table[it->cmp_id];
2067
2068 /* Maybe translate single-byte characters to multibyte. */
2069 it->char_to_display = it->c;
2070 if (unibyte_display_via_language_environment
2071 && SINGLE_BYTE_CHAR_P (it->c)
2072 && (it->c >= 0240
2073 || (it->c >= 0200
2074 && !NILP (Vnonascii_translation_table))))
2075 {
2076 it->char_to_display = unibyte_char_to_multibyte (it->c);
2077 }
2078
2079 /* Get face and font to use. Encode IT->char_to_display. */
2080 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2081 face = FACE_FROM_ID (it->f, it->face_id);
2082 x_get_char_face_and_encoding (it->f, it->char_to_display,
2083 it->face_id, &char2b, it->multibyte_p);
2084 font = face->font;
2085
2086 /* When no suitable font found, use the default font. */
2087 font_not_found_p = font == NULL;
2088 if (font_not_found_p)
2089 {
2090 font = FRAME_FONT (it->f);
2091 boff = it->f->output_data.x->baseline_offset;
2092 font_info = NULL;
2093 }
2094 else
2095 {
2096 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2097 boff = font_info->baseline_offset;
2098 if (font_info->vertical_centering)
2099 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2100 }
2101
2102 /* There are no padding glyphs, so there is only one glyph to
2103 produce for the composition. Important is that pixel_width,
2104 ascent and descent are the values of what is drawn by
2105 draw_glyphs (i.e. the values of the overall glyphs composed). */
2106 it->nglyphs = 1;
2107
2108 /* If we have not yet calculated pixel size data of glyphs of
2109 the composition for the current face font, calculate them
2110 now. Theoretically, we have to check all fonts for the
2111 glyphs, but that requires much time and memory space. So,
2112 here we check only the font of the first glyph. This leads
2113 to incorrect display very rarely, and C-l (recenter) can
2114 correct the display anyway. */
2115 if (cmp->font != (void *) font)
2116 {
2117 /* Ascent and descent of the font of the first character of
2118 this composition (adjusted by baseline offset). Ascent
2119 and descent of overall glyphs should not be less than
2120 them respectively. */
2121 int font_ascent = font->ascent + boff;
2122 int font_descent = font->descent - boff;
2123 /* Bounding box of the overall glyphs. */
2124 int leftmost, rightmost, lowest, highest;
2125 int i, width, ascent, descent;
2126
2127 cmp->font = (void *) font;
2128
2129 /* Initialize the bounding box. */
2130 if (font_info
2131 && (pcm = x_per_char_metric (font, &char2b)))
2132 {
2133 width = pcm->width;
2134 ascent = pcm->ascent;
2135 descent = pcm->descent;
2136 }
2137 else
2138 {
2139 width = FONT_WIDTH (font);
2140 ascent = font->ascent;
2141 descent = font->descent;
2142 }
2143
2144 rightmost = width;
2145 lowest = - descent + boff;
2146 highest = ascent + boff;
2147 leftmost = 0;
2148
2149 if (font_info
2150 && font_info->default_ascent
2151 && CHAR_TABLE_P (Vuse_default_ascent)
2152 && !NILP (Faref (Vuse_default_ascent,
2153 make_number (it->char_to_display))))
2154 highest = font_info->default_ascent + boff;
2155
2156 /* Draw the first glyph at the normal position. It may be
2157 shifted to right later if some other glyphs are drawn at
2158 the left. */
2159 cmp->offsets[0] = 0;
2160 cmp->offsets[1] = boff;
2161
2162 /* Set cmp->offsets for the remaining glyphs. */
2163 for (i = 1; i < cmp->glyph_len; i++)
2164 {
2165 int left, right, btm, top;
2166 int ch = COMPOSITION_GLYPH (cmp, i);
2167 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2168
2169 face = FACE_FROM_ID (it->f, face_id);
2170 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2171 it->multibyte_p);
2172 font = face->font;
2173 if (font == NULL)
2174 {
2175 font = FRAME_FONT (it->f);
2176 boff = it->f->output_data.x->baseline_offset;
2177 font_info = NULL;
2178 }
2179 else
2180 {
2181 font_info
2182 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2183 boff = font_info->baseline_offset;
2184 if (font_info->vertical_centering)
2185 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2186 }
2187
2188 if (font_info
2189 && (pcm = x_per_char_metric (font, &char2b)))
2190 {
2191 width = pcm->width;
2192 ascent = pcm->ascent;
2193 descent = pcm->descent;
2194 }
2195 else
2196 {
2197 width = FONT_WIDTH (font);
2198 ascent = 1;
2199 descent = 0;
2200 }
2201
2202 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2203 {
2204 /* Relative composition with or without
2205 alternate chars. */
2206 left = (leftmost + rightmost - width) / 2;
2207 btm = - descent + boff;
2208 if (font_info && font_info->relative_compose
2209 && (! CHAR_TABLE_P (Vignore_relative_composition)
2210 || NILP (Faref (Vignore_relative_composition,
2211 make_number (ch)))))
2212 {
2213
2214 if (- descent >= font_info->relative_compose)
2215 /* One extra pixel between two glyphs. */
2216 btm = highest + 1;
2217 else if (ascent <= 0)
2218 /* One extra pixel between two glyphs. */
2219 btm = lowest - 1 - ascent - descent;
2220 }
2221 }
2222 else
2223 {
2224 /* A composition rule is specified by an integer
2225 value that encodes global and new reference
2226 points (GREF and NREF). GREF and NREF are
2227 specified by numbers as below:
2228
2229 0---1---2 -- ascent
2230 | |
2231 | |
2232 | |
2233 9--10--11 -- center
2234 | |
2235 ---3---4---5--- baseline
2236 | |
2237 6---7---8 -- descent
2238 */
2239 int rule = COMPOSITION_RULE (cmp, i);
2240 int gref, nref, grefx, grefy, nrefx, nrefy;
2241
2242 COMPOSITION_DECODE_RULE (rule, gref, nref);
2243 grefx = gref % 3, nrefx = nref % 3;
2244 grefy = gref / 3, nrefy = nref / 3;
2245
2246 left = (leftmost
2247 + grefx * (rightmost - leftmost) / 2
2248 - nrefx * width / 2);
2249 btm = ((grefy == 0 ? highest
2250 : grefy == 1 ? 0
2251 : grefy == 2 ? lowest
2252 : (highest + lowest) / 2)
2253 - (nrefy == 0 ? ascent + descent
2254 : nrefy == 1 ? descent - boff
2255 : nrefy == 2 ? 0
2256 : (ascent + descent) / 2));
2257 }
2258
2259 cmp->offsets[i * 2] = left;
2260 cmp->offsets[i * 2 + 1] = btm + descent;
2261
2262 /* Update the bounding box of the overall glyphs. */
2263 right = left + width;
2264 top = btm + descent + ascent;
2265 if (left < leftmost)
2266 leftmost = left;
2267 if (right > rightmost)
2268 rightmost = right;
2269 if (top > highest)
2270 highest = top;
2271 if (btm < lowest)
2272 lowest = btm;
2273 }
2274
2275 /* If there are glyphs whose x-offsets are negative,
2276 shift all glyphs to the right and make all x-offsets
2277 non-negative. */
2278 if (leftmost < 0)
2279 {
2280 for (i = 0; i < cmp->glyph_len; i++)
2281 cmp->offsets[i * 2] -= leftmost;
2282 rightmost -= leftmost;
2283 }
2284
2285 cmp->pixel_width = rightmost;
2286 cmp->ascent = highest;
2287 cmp->descent = - lowest;
2288 if (cmp->ascent < font_ascent)
2289 cmp->ascent = font_ascent;
2290 if (cmp->descent < font_descent)
2291 cmp->descent = font_descent;
2292 }
2293
2294 it->pixel_width = cmp->pixel_width;
2295 it->ascent = it->phys_ascent = cmp->ascent;
2296 it->descent = it->phys_descent = cmp->descent;
2297
2298 if (face->box != FACE_NO_BOX)
2299 {
2300 int thick = face->box_line_width;
2301
2302 if (thick > 0)
2303 {
2304 it->ascent += thick;
2305 it->descent += thick;
2306 }
2307 else
2308 thick = - thick;
2309
2310 if (it->start_of_box_run_p)
2311 it->pixel_width += thick;
2312 if (it->end_of_box_run_p)
2313 it->pixel_width += thick;
2314 }
2315
2316 /* If face has an overline, add the height of the overline
2317 (1 pixel) and a 1 pixel margin to the character height. */
2318 if (face->overline_p)
2319 it->ascent += 2;
2320
2321 take_vertical_position_into_account (it);
2322
2323 if (it->glyph_row)
2324 x_append_composite_glyph (it);
2325 }
2326 else if (it->what == IT_IMAGE)
2327 x_produce_image_glyph (it);
2328 else if (it->what == IT_STRETCH)
2329 x_produce_stretch_glyph (it);
2330
2331 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2332 because this isn't true for images with `:ascent 100'. */
2333 xassert (it->ascent >= 0 && it->descent >= 0);
2334 if (it->area == TEXT_AREA)
2335 it->current_x += it->pixel_width;
2336
2337 it->descent += it->extra_line_spacing;
2338
2339 it->max_ascent = max (it->max_ascent, it->ascent);
2340 it->max_descent = max (it->max_descent, it->descent);
2341 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2342 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2343 }
2344
2345
2346 /* Estimate the pixel height of the mode or top line on frame F.
2347 FACE_ID specifies what line's height to estimate. */
2348
2349 int
2350 x_estimate_mode_line_height (f, face_id)
2351 struct frame *f;
2352 enum face_id face_id;
2353 {
2354 int height = FONT_HEIGHT (FRAME_FONT (f));
2355
2356 /* This function is called so early when Emacs starts that the face
2357 cache and mode line face are not yet initialized. */
2358 if (FRAME_FACE_CACHE (f))
2359 {
2360 struct face *face = FACE_FROM_ID (f, face_id);
2361 if (face)
2362 {
2363 if (face->font)
2364 height = FONT_HEIGHT (face->font);
2365 if (face->box_line_width > 0)
2366 height += 2 * face->box_line_width;
2367 }
2368 }
2369
2370 return height;
2371 }
2372
2373 \f
2374 /***********************************************************************
2375 Glyph display
2376 ***********************************************************************/
2377
2378 /* A sequence of glyphs to be drawn in the same face.
2379
2380 This data structure is not really completely X specific, so it
2381 could possibly, at least partially, be useful for other systems. It
2382 is currently not part of the external redisplay interface because
2383 it's not clear what other systems will need. */
2384
2385 struct glyph_string
2386 {
2387 /* X-origin of the string. */
2388 int x;
2389
2390 /* Y-origin and y-position of the base line of this string. */
2391 int y, ybase;
2392
2393 /* The width of the string, not including a face extension. */
2394 int width;
2395
2396 /* The width of the string, including a face extension. */
2397 int background_width;
2398
2399 /* The height of this string. This is the height of the line this
2400 string is drawn in, and can be different from the height of the
2401 font the string is drawn in. */
2402 int height;
2403
2404 /* Number of pixels this string overwrites in front of its x-origin.
2405 This number is zero if the string has an lbearing >= 0; it is
2406 -lbearing, if the string has an lbearing < 0. */
2407 int left_overhang;
2408
2409 /* Number of pixels this string overwrites past its right-most
2410 nominal x-position, i.e. x + width. Zero if the string's
2411 rbearing is <= its nominal width, rbearing - width otherwise. */
2412 int right_overhang;
2413
2414 /* The frame on which the glyph string is drawn. */
2415 struct frame *f;
2416
2417 /* The window on which the glyph string is drawn. */
2418 struct window *w;
2419
2420 /* X display and window for convenience. */
2421 Display *display;
2422 Window window;
2423
2424 /* The glyph row for which this string was built. It determines the
2425 y-origin and height of the string. */
2426 struct glyph_row *row;
2427
2428 /* The area within row. */
2429 enum glyph_row_area area;
2430
2431 /* Characters to be drawn, and number of characters. */
2432 XChar2b *char2b;
2433 int nchars;
2434
2435 /* A face-override for drawing cursors, mouse face and similar. */
2436 enum draw_glyphs_face hl;
2437
2438 /* Face in which this string is to be drawn. */
2439 struct face *face;
2440
2441 /* Font in which this string is to be drawn. */
2442 XFontStruct *font;
2443
2444 /* Font info for this string. */
2445 struct font_info *font_info;
2446
2447 /* Non-null means this string describes (part of) a composition.
2448 All characters from char2b are drawn composed. */
2449 struct composition *cmp;
2450
2451 /* Index of this glyph string's first character in the glyph
2452 definition of CMP. If this is zero, this glyph string describes
2453 the first character of a composition. */
2454 int gidx;
2455
2456 /* 1 means this glyph strings face has to be drawn to the right end
2457 of the window's drawing area. */
2458 unsigned extends_to_end_of_line_p : 1;
2459
2460 /* 1 means the background of this string has been drawn. */
2461 unsigned background_filled_p : 1;
2462
2463 /* 1 means glyph string must be drawn with 16-bit functions. */
2464 unsigned two_byte_p : 1;
2465
2466 /* 1 means that the original font determined for drawing this glyph
2467 string could not be loaded. The member `font' has been set to
2468 the frame's default font in this case. */
2469 unsigned font_not_found_p : 1;
2470
2471 /* 1 means that the face in which this glyph string is drawn has a
2472 stipple pattern. */
2473 unsigned stippled_p : 1;
2474
2475 /* 1 means only the foreground of this glyph string must be drawn,
2476 and we should use the physical height of the line this glyph
2477 string appears in as clip rect. */
2478 unsigned for_overlaps_p : 1;
2479
2480 /* The GC to use for drawing this glyph string. */
2481 GC gc;
2482
2483 /* A pointer to the first glyph in the string. This glyph
2484 corresponds to char2b[0]. Needed to draw rectangles if
2485 font_not_found_p is 1. */
2486 struct glyph *first_glyph;
2487
2488 /* Image, if any. */
2489 struct image *img;
2490
2491 struct glyph_string *next, *prev;
2492 };
2493
2494
2495 #if 1
2496
2497 static void
2498 x_dump_glyph_string (s)
2499 struct glyph_string *s;
2500 {
2501 fprintf (stderr, "glyph string\n");
2502 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2503 s->x, s->y, s->width, s->height);
2504 fprintf (stderr, " ybase = %d\n", s->ybase);
2505 fprintf (stderr, " hl = %d\n", s->hl);
2506 fprintf (stderr, " left overhang = %d, right = %d\n",
2507 s->left_overhang, s->right_overhang);
2508 fprintf (stderr, " nchars = %d\n", s->nchars);
2509 fprintf (stderr, " extends to end of line = %d\n",
2510 s->extends_to_end_of_line_p);
2511 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2512 fprintf (stderr, " bg width = %d\n", s->background_width);
2513 }
2514
2515 #endif /* GLYPH_DEBUG */
2516
2517
2518
2519 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2520 struct glyph_string **,
2521 struct glyph_string *,
2522 struct glyph_string *));
2523 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2524 struct glyph_string **,
2525 struct glyph_string *,
2526 struct glyph_string *));
2527 static void x_append_glyph_string P_ ((struct glyph_string **,
2528 struct glyph_string **,
2529 struct glyph_string *));
2530 static int x_left_overwritten P_ ((struct glyph_string *));
2531 static int x_left_overwriting P_ ((struct glyph_string *));
2532 static int x_right_overwritten P_ ((struct glyph_string *));
2533 static int x_right_overwriting P_ ((struct glyph_string *));
2534 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2535 int));
2536 static void x_init_glyph_string P_ ((struct glyph_string *,
2537 XChar2b *, struct window *,
2538 struct glyph_row *,
2539 enum glyph_row_area, int,
2540 enum draw_glyphs_face));
2541 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2542 enum glyph_row_area, int, int,
2543 enum draw_glyphs_face, int *, int *, int));
2544 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2545 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2546 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2547 int));
2548 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2549 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2550 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2551 static void x_draw_glyph_string P_ ((struct glyph_string *));
2552 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2553 static void x_set_cursor_gc P_ ((struct glyph_string *));
2554 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2555 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2556 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2557 int *, int *));
2558 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2559 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2560 unsigned long *, double, int));
2561 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2562 double, int, unsigned long));
2563 static void x_setup_relief_colors P_ ((struct glyph_string *));
2564 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2565 static void x_draw_image_relief P_ ((struct glyph_string *));
2566 static void x_draw_image_foreground P_ ((struct glyph_string *));
2567 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2568 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2569 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2570 int, int, int));
2571 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2572 int, int, int, int, XRectangle *));
2573 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2574 int, int, int, XRectangle *));
2575 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2576 enum glyph_row_area));
2577 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2578 struct glyph_row *,
2579 enum glyph_row_area, int, int));
2580
2581 #if GLYPH_DEBUG
2582 static void x_check_font P_ ((struct frame *, XFontStruct *));
2583 #endif
2584
2585
2586 /* Append the list of glyph strings with head H and tail T to the list
2587 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2588
2589 static INLINE void
2590 x_append_glyph_string_lists (head, tail, h, t)
2591 struct glyph_string **head, **tail;
2592 struct glyph_string *h, *t;
2593 {
2594 if (h)
2595 {
2596 if (*head)
2597 (*tail)->next = h;
2598 else
2599 *head = h;
2600 h->prev = *tail;
2601 *tail = t;
2602 }
2603 }
2604
2605
2606 /* Prepend the list of glyph strings with head H and tail T to the
2607 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2608 result. */
2609
2610 static INLINE void
2611 x_prepend_glyph_string_lists (head, tail, h, t)
2612 struct glyph_string **head, **tail;
2613 struct glyph_string *h, *t;
2614 {
2615 if (h)
2616 {
2617 if (*head)
2618 (*head)->prev = t;
2619 else
2620 *tail = t;
2621 t->next = *head;
2622 *head = h;
2623 }
2624 }
2625
2626
2627 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2628 Set *HEAD and *TAIL to the resulting list. */
2629
2630 static INLINE void
2631 x_append_glyph_string (head, tail, s)
2632 struct glyph_string **head, **tail;
2633 struct glyph_string *s;
2634 {
2635 s->next = s->prev = NULL;
2636 x_append_glyph_string_lists (head, tail, s, s);
2637 }
2638
2639
2640 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2641 face. */
2642
2643 static void
2644 x_set_cursor_gc (s)
2645 struct glyph_string *s;
2646 {
2647 if (s->font == FRAME_FONT (s->f)
2648 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2649 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2650 && !s->cmp)
2651 s->gc = s->f->output_data.x->cursor_gc;
2652 else
2653 {
2654 /* Cursor on non-default face: must merge. */
2655 XGCValues xgcv;
2656 unsigned long mask;
2657
2658 xgcv.background = s->f->output_data.x->cursor_pixel;
2659 xgcv.foreground = s->face->background;
2660
2661 /* If the glyph would be invisible, try a different foreground. */
2662 if (xgcv.foreground == xgcv.background)
2663 xgcv.foreground = s->face->foreground;
2664 if (xgcv.foreground == xgcv.background)
2665 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2666 if (xgcv.foreground == xgcv.background)
2667 xgcv.foreground = s->face->foreground;
2668
2669 /* Make sure the cursor is distinct from text in this face. */
2670 if (xgcv.background == s->face->background
2671 && xgcv.foreground == s->face->foreground)
2672 {
2673 xgcv.background = s->face->foreground;
2674 xgcv.foreground = s->face->background;
2675 }
2676
2677 IF_DEBUG (x_check_font (s->f, s->font));
2678 xgcv.font = s->font->fid;
2679 xgcv.graphics_exposures = False;
2680 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2681
2682 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2683 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2684 mask, &xgcv);
2685 else
2686 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2687 = XCreateGC (s->display, s->window, mask, &xgcv);
2688
2689 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2690 }
2691 }
2692
2693
2694 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2695
2696 static void
2697 x_set_mouse_face_gc (s)
2698 struct glyph_string *s;
2699 {
2700 int face_id;
2701 struct face *face;
2702
2703 /* What face has to be used last for the mouse face? */
2704 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2705 face = FACE_FROM_ID (s->f, face_id);
2706 if (face == NULL)
2707 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2708
2709 if (s->first_glyph->type == CHAR_GLYPH)
2710 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2711 else
2712 face_id = FACE_FOR_CHAR (s->f, face, 0);
2713 s->face = FACE_FROM_ID (s->f, face_id);
2714 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2715
2716 /* If font in this face is same as S->font, use it. */
2717 if (s->font == s->face->font)
2718 s->gc = s->face->gc;
2719 else
2720 {
2721 /* Otherwise construct scratch_cursor_gc with values from FACE
2722 but font FONT. */
2723 XGCValues xgcv;
2724 unsigned long mask;
2725
2726 xgcv.background = s->face->background;
2727 xgcv.foreground = s->face->foreground;
2728 IF_DEBUG (x_check_font (s->f, s->font));
2729 xgcv.font = s->font->fid;
2730 xgcv.graphics_exposures = False;
2731 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2732
2733 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2734 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2735 mask, &xgcv);
2736 else
2737 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2738 = XCreateGC (s->display, s->window, mask, &xgcv);
2739
2740 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2741 }
2742
2743 xassert (s->gc != 0);
2744 }
2745
2746
2747 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2748 Faces to use in the mode line have already been computed when the
2749 matrix was built, so there isn't much to do, here. */
2750
2751 static INLINE void
2752 x_set_mode_line_face_gc (s)
2753 struct glyph_string *s;
2754 {
2755 s->gc = s->face->gc;
2756 }
2757
2758
2759 /* Set S->gc of glyph string S for drawing that glyph string. Set
2760 S->stippled_p to a non-zero value if the face of S has a stipple
2761 pattern. */
2762
2763 static INLINE void
2764 x_set_glyph_string_gc (s)
2765 struct glyph_string *s;
2766 {
2767 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2768
2769 if (s->hl == DRAW_NORMAL_TEXT)
2770 {
2771 s->gc = s->face->gc;
2772 s->stippled_p = s->face->stipple != 0;
2773 }
2774 else if (s->hl == DRAW_INVERSE_VIDEO)
2775 {
2776 x_set_mode_line_face_gc (s);
2777 s->stippled_p = s->face->stipple != 0;
2778 }
2779 else if (s->hl == DRAW_CURSOR)
2780 {
2781 x_set_cursor_gc (s);
2782 s->stippled_p = 0;
2783 }
2784 else if (s->hl == DRAW_MOUSE_FACE)
2785 {
2786 x_set_mouse_face_gc (s);
2787 s->stippled_p = s->face->stipple != 0;
2788 }
2789 else if (s->hl == DRAW_IMAGE_RAISED
2790 || s->hl == DRAW_IMAGE_SUNKEN)
2791 {
2792 s->gc = s->face->gc;
2793 s->stippled_p = s->face->stipple != 0;
2794 }
2795 else
2796 {
2797 s->gc = s->face->gc;
2798 s->stippled_p = s->face->stipple != 0;
2799 }
2800
2801 /* GC must have been set. */
2802 xassert (s->gc != 0);
2803 }
2804
2805
2806 /* Return in *R the clipping rectangle for glyph string S. */
2807
2808 static void
2809 x_get_glyph_string_clip_rect (s, r)
2810 struct glyph_string *s;
2811 XRectangle *r;
2812 {
2813 if (s->row->full_width_p)
2814 {
2815 /* Draw full-width. X coordinates are relative to S->w->left. */
2816 int canon_x = CANON_X_UNIT (s->f);
2817
2818 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2819 r->width = XFASTINT (s->w->width) * canon_x;
2820
2821 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2822 {
2823 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2824 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2825 r->x -= width;
2826 }
2827
2828 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2829
2830 /* Unless displaying a mode or menu bar line, which are always
2831 fully visible, clip to the visible part of the row. */
2832 if (s->w->pseudo_window_p)
2833 r->height = s->row->visible_height;
2834 else
2835 r->height = s->height;
2836 }
2837 else
2838 {
2839 /* This is a text line that may be partially visible. */
2840 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2841 r->width = window_box_width (s->w, s->area);
2842 r->height = s->row->visible_height;
2843 }
2844
2845 /* Don't use S->y for clipping because it doesn't take partially
2846 visible lines into account. For example, it can be negative for
2847 partially visible lines at the top of a window. */
2848 if (!s->row->full_width_p
2849 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2850 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2851 else
2852 r->y = max (0, s->row->y);
2853
2854 /* If drawing a tool-bar window, draw it over the internal border
2855 at the top of the window. */
2856 if (s->w == XWINDOW (s->f->tool_bar_window))
2857 r->y -= s->f->output_data.x->internal_border_width;
2858
2859 /* If S draws overlapping rows, it's sufficient to use the top and
2860 bottom of the window for clipping because this glyph string
2861 intentionally draws over other lines. */
2862 if (s->for_overlaps_p)
2863 {
2864 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2865 r->height = window_text_bottom_y (s->w) - r->y;
2866 }
2867
2868 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2869 }
2870
2871
2872 /* Set clipping for output of glyph string S. S may be part of a mode
2873 line or menu if we don't have X toolkit support. */
2874
2875 static INLINE void
2876 x_set_glyph_string_clipping (s)
2877 struct glyph_string *s;
2878 {
2879 XRectangle r;
2880 x_get_glyph_string_clip_rect (s, &r);
2881 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2882 }
2883
2884
2885 /* Compute left and right overhang of glyph string S. If S is a glyph
2886 string for a composition, assume overhangs don't exist. */
2887
2888 static INLINE void
2889 x_compute_glyph_string_overhangs (s)
2890 struct glyph_string *s;
2891 {
2892 if (s->cmp == NULL
2893 && s->first_glyph->type == CHAR_GLYPH)
2894 {
2895 XCharStruct cs;
2896 int direction, font_ascent, font_descent;
2897 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2898 &font_ascent, &font_descent, &cs);
2899 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2900 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2901 }
2902 }
2903
2904
2905 /* Compute overhangs and x-positions for glyph string S and its
2906 predecessors, or successors. X is the starting x-position for S.
2907 BACKWARD_P non-zero means process predecessors. */
2908
2909 static void
2910 x_compute_overhangs_and_x (s, x, backward_p)
2911 struct glyph_string *s;
2912 int x;
2913 int backward_p;
2914 {
2915 if (backward_p)
2916 {
2917 while (s)
2918 {
2919 x_compute_glyph_string_overhangs (s);
2920 x -= s->width;
2921 s->x = x;
2922 s = s->prev;
2923 }
2924 }
2925 else
2926 {
2927 while (s)
2928 {
2929 x_compute_glyph_string_overhangs (s);
2930 s->x = x;
2931 x += s->width;
2932 s = s->next;
2933 }
2934 }
2935 }
2936
2937
2938 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2939 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2940 assumed to be zero. */
2941
2942 static void
2943 x_get_glyph_overhangs (glyph, f, left, right)
2944 struct glyph *glyph;
2945 struct frame *f;
2946 int *left, *right;
2947 {
2948 *left = *right = 0;
2949
2950 if (glyph->type == CHAR_GLYPH)
2951 {
2952 XFontStruct *font;
2953 struct face *face;
2954 struct font_info *font_info;
2955 XChar2b char2b;
2956 XCharStruct *pcm;
2957
2958 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2959 font = face->font;
2960 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2961 if (font
2962 && (pcm = x_per_char_metric (font, &char2b)))
2963 {
2964 if (pcm->rbearing > pcm->width)
2965 *right = pcm->rbearing - pcm->width;
2966 if (pcm->lbearing < 0)
2967 *left = -pcm->lbearing;
2968 }
2969 }
2970 }
2971
2972
2973 /* Return the index of the first glyph preceding glyph string S that
2974 is overwritten by S because of S's left overhang. Value is -1
2975 if no glyphs are overwritten. */
2976
2977 static int
2978 x_left_overwritten (s)
2979 struct glyph_string *s;
2980 {
2981 int k;
2982
2983 if (s->left_overhang)
2984 {
2985 int x = 0, i;
2986 struct glyph *glyphs = s->row->glyphs[s->area];
2987 int first = s->first_glyph - glyphs;
2988
2989 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2990 x -= glyphs[i].pixel_width;
2991
2992 k = i + 1;
2993 }
2994 else
2995 k = -1;
2996
2997 return k;
2998 }
2999
3000
3001 /* Return the index of the first glyph preceding glyph string S that
3002 is overwriting S because of its right overhang. Value is -1 if no
3003 glyph in front of S overwrites S. */
3004
3005 static int
3006 x_left_overwriting (s)
3007 struct glyph_string *s;
3008 {
3009 int i, k, x;
3010 struct glyph *glyphs = s->row->glyphs[s->area];
3011 int first = s->first_glyph - glyphs;
3012
3013 k = -1;
3014 x = 0;
3015 for (i = first - 1; i >= 0; --i)
3016 {
3017 int left, right;
3018 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3019 if (x + right > 0)
3020 k = i;
3021 x -= glyphs[i].pixel_width;
3022 }
3023
3024 return k;
3025 }
3026
3027
3028 /* Return the index of the last glyph following glyph string S that is
3029 not overwritten by S because of S's right overhang. Value is -1 if
3030 no such glyph is found. */
3031
3032 static int
3033 x_right_overwritten (s)
3034 struct glyph_string *s;
3035 {
3036 int k = -1;
3037
3038 if (s->right_overhang)
3039 {
3040 int x = 0, i;
3041 struct glyph *glyphs = s->row->glyphs[s->area];
3042 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3043 int end = s->row->used[s->area];
3044
3045 for (i = first; i < end && s->right_overhang > x; ++i)
3046 x += glyphs[i].pixel_width;
3047
3048 k = i;
3049 }
3050
3051 return k;
3052 }
3053
3054
3055 /* Return the index of the last glyph following glyph string S that
3056 overwrites S because of its left overhang. Value is negative
3057 if no such glyph is found. */
3058
3059 static int
3060 x_right_overwriting (s)
3061 struct glyph_string *s;
3062 {
3063 int i, k, x;
3064 int end = s->row->used[s->area];
3065 struct glyph *glyphs = s->row->glyphs[s->area];
3066 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3067
3068 k = -1;
3069 x = 0;
3070 for (i = first; i < end; ++i)
3071 {
3072 int left, right;
3073 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3074 if (x - left < 0)
3075 k = i;
3076 x += glyphs[i].pixel_width;
3077 }
3078
3079 return k;
3080 }
3081
3082
3083 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3084
3085 static INLINE void
3086 x_clear_glyph_string_rect (s, x, y, w, h)
3087 struct glyph_string *s;
3088 int x, y, w, h;
3089 {
3090 XGCValues xgcv;
3091 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3092 XSetForeground (s->display, s->gc, xgcv.background);
3093 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3094 XSetForeground (s->display, s->gc, xgcv.foreground);
3095 }
3096
3097
3098 /* Draw the background of glyph_string S. If S->background_filled_p
3099 is non-zero don't draw it. FORCE_P non-zero means draw the
3100 background even if it wouldn't be drawn normally. This is used
3101 when a string preceding S draws into the background of S, or S
3102 contains the first component of a composition. */
3103
3104 static void
3105 x_draw_glyph_string_background (s, force_p)
3106 struct glyph_string *s;
3107 int force_p;
3108 {
3109 /* Nothing to do if background has already been drawn or if it
3110 shouldn't be drawn in the first place. */
3111 if (!s->background_filled_p)
3112 {
3113 int box_line_width = max (s->face->box_line_width, 0);
3114
3115 if (s->stippled_p)
3116 {
3117 /* Fill background with a stipple pattern. */
3118 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3119 XFillRectangle (s->display, s->window, s->gc, s->x,
3120 s->y + box_line_width,
3121 s->background_width,
3122 s->height - 2 * box_line_width);
3123 XSetFillStyle (s->display, s->gc, FillSolid);
3124 s->background_filled_p = 1;
3125 }
3126 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
3127 || s->font_not_found_p
3128 || s->extends_to_end_of_line_p
3129 || force_p)
3130 {
3131 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
3132 s->background_width,
3133 s->height - 2 * box_line_width);
3134 s->background_filled_p = 1;
3135 }
3136 }
3137 }
3138
3139
3140 /* Draw the foreground of glyph string S. */
3141
3142 static void
3143 x_draw_glyph_string_foreground (s)
3144 struct glyph_string *s;
3145 {
3146 int i, x;
3147
3148 /* If first glyph of S has a left box line, start drawing the text
3149 of S to the right of that box line. */
3150 if (s->face->box != FACE_NO_BOX
3151 && s->first_glyph->left_box_line_p)
3152 x = s->x + abs (s->face->box_line_width);
3153 else
3154 x = s->x;
3155
3156 /* Draw characters of S as rectangles if S's font could not be
3157 loaded. */
3158 if (s->font_not_found_p)
3159 {
3160 for (i = 0; i < s->nchars; ++i)
3161 {
3162 struct glyph *g = s->first_glyph + i;
3163 XDrawRectangle (s->display, s->window,
3164 s->gc, x, s->y, g->pixel_width - 1,
3165 s->height - 1);
3166 x += g->pixel_width;
3167 }
3168 }
3169 else
3170 {
3171 char *char1b = (char *) s->char2b;
3172 int boff = s->font_info->baseline_offset;
3173
3174 if (s->font_info->vertical_centering)
3175 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3176
3177 /* If we can use 8-bit functions, condense S->char2b. */
3178 if (!s->two_byte_p)
3179 for (i = 0; i < s->nchars; ++i)
3180 char1b[i] = s->char2b[i].byte2;
3181
3182 /* Draw text with XDrawString if background has already been
3183 filled. Otherwise, use XDrawImageString. (Note that
3184 XDrawImageString is usually faster than XDrawString.) Always
3185 use XDrawImageString when drawing the cursor so that there is
3186 no chance that characters under a box cursor are invisible. */
3187 if (s->for_overlaps_p
3188 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3189 {
3190 /* Draw characters with 16-bit or 8-bit functions. */
3191 if (s->two_byte_p)
3192 XDrawString16 (s->display, s->window, s->gc, x,
3193 s->ybase - boff, s->char2b, s->nchars);
3194 else
3195 XDrawString (s->display, s->window, s->gc, x,
3196 s->ybase - boff, char1b, s->nchars);
3197 }
3198 else
3199 {
3200 if (s->two_byte_p)
3201 XDrawImageString16 (s->display, s->window, s->gc, x,
3202 s->ybase - boff, s->char2b, s->nchars);
3203 else
3204 XDrawImageString (s->display, s->window, s->gc, x,
3205 s->ybase - boff, char1b, s->nchars);
3206 }
3207 }
3208 }
3209
3210 /* Draw the foreground of composite glyph string S. */
3211
3212 static void
3213 x_draw_composite_glyph_string_foreground (s)
3214 struct glyph_string *s;
3215 {
3216 int i, x;
3217
3218 /* If first glyph of S has a left box line, start drawing the text
3219 of S to the right of that box line. */
3220 if (s->face->box != FACE_NO_BOX
3221 && s->first_glyph->left_box_line_p)
3222 x = s->x + abs (s->face->box_line_width);
3223 else
3224 x = s->x;
3225
3226 /* S is a glyph string for a composition. S->gidx is the index of
3227 the first character drawn for glyphs of this composition.
3228 S->gidx == 0 means we are drawing the very first character of
3229 this composition. */
3230
3231 /* Draw a rectangle for the composition if the font for the very
3232 first character of the composition could not be loaded. */
3233 if (s->font_not_found_p)
3234 {
3235 if (s->gidx == 0)
3236 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3237 s->width - 1, s->height - 1);
3238 }
3239 else
3240 {
3241 for (i = 0; i < s->nchars; i++, ++s->gidx)
3242 XDrawString16 (s->display, s->window, s->gc,
3243 x + s->cmp->offsets[s->gidx * 2],
3244 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3245 s->char2b + i, 1);
3246 }
3247 }
3248
3249
3250 #ifdef USE_X_TOOLKIT
3251
3252 static struct frame *x_frame_of_widget P_ ((Widget));
3253 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
3254 XrmValue *, XrmValue *, XtPointer *));
3255 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
3256 XrmValue *, Cardinal *));
3257
3258
3259 /* Return the frame on which widget WIDGET is used.. Abort if frame
3260 cannot be determined. */
3261
3262 static struct frame *
3263 x_frame_of_widget (widget)
3264 Widget widget;
3265 {
3266 struct x_display_info *dpyinfo;
3267 Lisp_Object tail;
3268 struct frame *f;
3269
3270 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3271
3272 /* Find the top-level shell of the widget. Note that this function
3273 can be called when the widget is not yet realized, so XtWindow
3274 (widget) == 0. That's the reason we can't simply use
3275 x_any_window_to_frame. */
3276 while (!XtIsTopLevelShell (widget))
3277 widget = XtParent (widget);
3278
3279 /* Look for a frame with that top-level widget. Allocate the color
3280 on that frame to get the right gamma correction value. */
3281 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3282 if (GC_FRAMEP (XCAR (tail))
3283 && (f = XFRAME (XCAR (tail)),
3284 (f->output_data.nothing != 1
3285 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3286 && f->output_data.x->widget == widget)
3287 return f;
3288
3289 abort ();
3290 }
3291
3292
3293 /* Allocate the color COLOR->pixel on the screen and display of
3294 widget WIDGET in colormap CMAP. If an exact match cannot be
3295 allocated, try the nearest color available. Value is non-zero
3296 if successful. This is called from lwlib. */
3297
3298 int
3299 x_alloc_nearest_color_for_widget (widget, cmap, color)
3300 Widget widget;
3301 Colormap cmap;
3302 XColor *color;
3303 {
3304 struct frame *f = x_frame_of_widget (widget);
3305 return x_alloc_nearest_color (f, cmap, color);
3306 }
3307
3308
3309 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3310 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3311 If this produces the same color as PIXEL, try a color where all RGB
3312 values have DELTA added. Return the allocated color in *PIXEL.
3313 DISPLAY is the X display, CMAP is the colormap to operate on.
3314 Value is non-zero if successful. */
3315
3316 int
3317 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3318 Widget widget;
3319 Display *display;
3320 Colormap cmap;
3321 unsigned long *pixel;
3322 double factor;
3323 int delta;
3324 {
3325 struct frame *f = x_frame_of_widget (widget);
3326 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3327 }
3328
3329
3330 /* Structure specifying which arguments should be passed by Xt to
3331 cvt_string_to_pixel. We want the widget's screen and colormap. */
3332
3333 static XtConvertArgRec cvt_string_to_pixel_args[] =
3334 {
3335 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
3336 sizeof (Screen *)},
3337 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
3338 sizeof (Colormap)}
3339 };
3340
3341
3342 /* The address of this variable is returned by
3343 cvt_string_to_pixel. */
3344
3345 static Pixel cvt_string_to_pixel_value;
3346
3347
3348 /* Convert a color name to a pixel color.
3349
3350 DPY is the display we are working on.
3351
3352 ARGS is an array of *NARGS XrmValue structures holding additional
3353 information about the widget for which the conversion takes place.
3354 The contents of this array are determined by the specification
3355 in cvt_string_to_pixel_args.
3356
3357 FROM is a pointer to an XrmValue which points to the color name to
3358 convert. TO is an XrmValue in which to return the pixel color.
3359
3360 CLOSURE_RET is a pointer to user-data, in which we record if
3361 we allocated the color or not.
3362
3363 Value is True if successful, False otherwise. */
3364
3365 static Boolean
3366 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
3367 Display *dpy;
3368 XrmValue *args;
3369 Cardinal *nargs;
3370 XrmValue *from, *to;
3371 XtPointer *closure_ret;
3372 {
3373 Screen *screen;
3374 Colormap cmap;
3375 Pixel pixel;
3376 String color_name;
3377 XColor color;
3378
3379 if (*nargs != 2)
3380 {
3381 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3382 "wrongParameters", "cvt_string_to_pixel",
3383 "XtToolkitError",
3384 "Screen and colormap args required", NULL, NULL);
3385 return False;
3386 }
3387
3388 screen = *(Screen **) args[0].addr;
3389 cmap = *(Colormap *) args[1].addr;
3390 color_name = (String) from->addr;
3391
3392 if (strcmp (color_name, XtDefaultBackground) == 0)
3393 {
3394 *closure_ret = (XtPointer) False;
3395 pixel = WhitePixelOfScreen (screen);
3396 }
3397 else if (strcmp (color_name, XtDefaultForeground) == 0)
3398 {
3399 *closure_ret = (XtPointer) False;
3400 pixel = BlackPixelOfScreen (screen);
3401 }
3402 else if (XParseColor (dpy, cmap, color_name, &color)
3403 && x_alloc_nearest_color_1 (dpy, cmap, &color))
3404 {
3405 pixel = color.pixel;
3406 *closure_ret = (XtPointer) True;
3407 }
3408 else
3409 {
3410 String params[1];
3411 Cardinal nparams = 1;
3412
3413 params[0] = color_name;
3414 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3415 "badValue", "cvt_string_to_pixel",
3416 "XtToolkitError", "Invalid color `%s'",
3417 params, &nparams);
3418 return False;
3419 }
3420
3421 if (to->addr != NULL)
3422 {
3423 if (to->size < sizeof (Pixel))
3424 {
3425 to->size = sizeof (Pixel);
3426 return False;
3427 }
3428
3429 *(Pixel *) to->addr = pixel;
3430 }
3431 else
3432 {
3433 cvt_string_to_pixel_value = pixel;
3434 to->addr = (XtPointer) &cvt_string_to_pixel_value;
3435 }
3436
3437 to->size = sizeof (Pixel);
3438 return True;
3439 }
3440
3441
3442 /* Free a pixel color which was previously allocated via
3443 cvt_string_to_pixel. This is registered as the destructor
3444 for this type of resource via XtSetTypeConverter.
3445
3446 APP is the application context in which we work.
3447
3448 TO is a pointer to an XrmValue holding the color to free.
3449 CLOSURE is the value we stored in CLOSURE_RET for this color
3450 in cvt_string_to_pixel.
3451
3452 ARGS and NARGS are like for cvt_string_to_pixel. */
3453
3454 static void
3455 cvt_pixel_dtor (app, to, closure, args, nargs)
3456 XtAppContext app;
3457 XrmValuePtr to;
3458 XtPointer closure;
3459 XrmValuePtr args;
3460 Cardinal *nargs;
3461 {
3462 if (*nargs != 2)
3463 {
3464 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
3465 "XtToolkitError",
3466 "Screen and colormap arguments required",
3467 NULL, NULL);
3468 }
3469 else if (closure != NULL)
3470 {
3471 /* We did allocate the pixel, so free it. */
3472 Screen *screen = *(Screen **) args[0].addr;
3473 Colormap cmap = *(Colormap *) args[1].addr;
3474 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
3475 (Pixel *) to->addr, 1, 0);
3476 }
3477 }
3478
3479
3480 #endif /* USE_X_TOOLKIT */
3481
3482
3483 /* Value is an array of XColor structures for the contents of the
3484 color map of display DPY. Set *NCELLS to the size of the array.
3485 Note that this probably shouldn't be called for large color maps,
3486 say a 24-bit TrueColor map. */
3487
3488 static const XColor *
3489 x_color_cells (dpy, ncells)
3490 Display *dpy;
3491 int *ncells;
3492 {
3493 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3494
3495 if (dpyinfo->color_cells == NULL)
3496 {
3497 Screen *screen = dpyinfo->screen;
3498 int i;
3499
3500 dpyinfo->ncolor_cells
3501 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
3502 dpyinfo->color_cells
3503 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3504 * sizeof *dpyinfo->color_cells);
3505
3506 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3507 dpyinfo->color_cells[i].pixel = i;
3508
3509 XQueryColors (dpy, dpyinfo->cmap,
3510 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3511 }
3512
3513 *ncells = dpyinfo->ncolor_cells;
3514 return dpyinfo->color_cells;
3515 }
3516
3517
3518 /* On frame F, translate pixel colors to RGB values for the NCOLORS
3519 colors in COLORS. Use cached information, if available. */
3520
3521 void
3522 x_query_colors (f, colors, ncolors)
3523 struct frame *f;
3524 XColor *colors;
3525 int ncolors;
3526 {
3527 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3528
3529 if (dpyinfo->color_cells)
3530 {
3531 int i;
3532 for (i = 0; i < ncolors; ++i)
3533 {
3534 unsigned long pixel = colors[i].pixel;
3535 xassert (pixel < dpyinfo->ncolor_cells);
3536 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3537 colors[i] = dpyinfo->color_cells[pixel];
3538 }
3539 }
3540 else
3541 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3542 }
3543
3544
3545 /* On frame F, translate pixel color to RGB values for the color in
3546 COLOR. Use cached information, if available. */
3547
3548 void
3549 x_query_color (f, color)
3550 struct frame *f;
3551 XColor *color;
3552 {
3553 x_query_colors (f, color, 1);
3554 }
3555
3556
3557 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
3558 exact match can't be allocated, try the nearest color available.
3559 Value is non-zero if successful. Set *COLOR to the color
3560 allocated. */
3561
3562 static int
3563 x_alloc_nearest_color_1 (dpy, cmap, color)
3564 Display *dpy;
3565 Colormap cmap;
3566 XColor *color;
3567 {
3568 int rc;
3569
3570 rc = XAllocColor (dpy, cmap, color);
3571 if (rc == 0)
3572 {
3573 /* If we got to this point, the colormap is full, so we're going
3574 to try to get the next closest color. The algorithm used is
3575 a least-squares matching, which is what X uses for closest
3576 color matching with StaticColor visuals. */
3577 int nearest, i;
3578 unsigned long nearest_delta = ~0;
3579 int ncells;
3580 const XColor *cells = x_color_cells (dpy, &ncells);
3581
3582 for (nearest = i = 0; i < ncells; ++i)
3583 {
3584 long dred = (color->red >> 8) - (cells[i].red >> 8);
3585 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3586 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3587 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3588
3589 if (delta < nearest_delta)
3590 {
3591 nearest = i;
3592 nearest_delta = delta;
3593 }
3594 }
3595
3596 color->red = cells[nearest].red;
3597 color->green = cells[nearest].green;
3598 color->blue = cells[nearest].blue;
3599 rc = XAllocColor (dpy, cmap, color);
3600 }
3601 else
3602 {
3603 /* If allocation succeeded, and the allocated pixel color is not
3604 equal to a cached pixel color recorded earlier, there was a
3605 change in the colormap, so clear the color cache. */
3606 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3607 XColor *cached_color;
3608
3609 if (dpyinfo->color_cells
3610 && (cached_color = &dpyinfo->color_cells[color->pixel],
3611 (cached_color->red != color->red
3612 || cached_color->blue != color->blue
3613 || cached_color->green != color->green)))
3614 {
3615 xfree (dpyinfo->color_cells);
3616 dpyinfo->color_cells = NULL;
3617 dpyinfo->ncolor_cells = 0;
3618 }
3619 }
3620
3621 #ifdef DEBUG_X_COLORS
3622 if (rc)
3623 register_color (color->pixel);
3624 #endif /* DEBUG_X_COLORS */
3625
3626 return rc;
3627 }
3628
3629
3630 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
3631 exact match can't be allocated, try the nearest color available.
3632 Value is non-zero if successful. Set *COLOR to the color
3633 allocated. */
3634
3635 int
3636 x_alloc_nearest_color (f, cmap, color)
3637 struct frame *f;
3638 Colormap cmap;
3639 XColor *color;
3640 {
3641 gamma_correct (f, color);
3642 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
3643 }
3644
3645
3646 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3647 It's necessary to do this instead of just using PIXEL directly to
3648 get color reference counts right. */
3649
3650 unsigned long
3651 x_copy_color (f, pixel)
3652 struct frame *f;
3653 unsigned long pixel;
3654 {
3655 XColor color;
3656
3657 color.pixel = pixel;
3658 BLOCK_INPUT;
3659 x_query_color (f, &color);
3660 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3661 UNBLOCK_INPUT;
3662 #ifdef DEBUG_X_COLORS
3663 register_color (pixel);
3664 #endif
3665 return color.pixel;
3666 }
3667
3668
3669 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3670 It's necessary to do this instead of just using PIXEL directly to
3671 get color reference counts right. */
3672
3673 unsigned long
3674 x_copy_dpy_color (dpy, cmap, pixel)
3675 Display *dpy;
3676 Colormap cmap;
3677 unsigned long pixel;
3678 {
3679 XColor color;
3680
3681 color.pixel = pixel;
3682 BLOCK_INPUT;
3683 XQueryColor (dpy, cmap, &color);
3684 XAllocColor (dpy, cmap, &color);
3685 UNBLOCK_INPUT;
3686 #ifdef DEBUG_X_COLORS
3687 register_color (pixel);
3688 #endif
3689 return color.pixel;
3690 }
3691
3692
3693 /* Brightness beyond which a color won't have its highlight brightness
3694 boosted.
3695
3696 Nominally, highlight colors for `3d' faces are calculated by
3697 brightening an object's color by a constant scale factor, but this
3698 doesn't yield good results for dark colors, so for colors who's
3699 brightness is less than this value (on a scale of 0-65535) have an
3700 use an additional additive factor.
3701
3702 The value here is set so that the default menu-bar/mode-line color
3703 (grey75) will not have its highlights changed at all. */
3704 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3705
3706
3707 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3708 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3709 If this produces the same color as PIXEL, try a color where all RGB
3710 values have DELTA added. Return the allocated color in *PIXEL.
3711 DISPLAY is the X display, CMAP is the colormap to operate on.
3712 Value is non-zero if successful. */
3713
3714 static int
3715 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3716 struct frame *f;
3717 Display *display;
3718 Colormap cmap;
3719 unsigned long *pixel;
3720 double factor;
3721 int delta;
3722 {
3723 XColor color, new;
3724 long bright;
3725 int success_p;
3726
3727 /* Get RGB color values. */
3728 color.pixel = *pixel;
3729 x_query_color (f, &color);
3730
3731 /* Change RGB values by specified FACTOR. Avoid overflow! */
3732 xassert (factor >= 0);
3733 new.red = min (0xffff, factor * color.red);
3734 new.green = min (0xffff, factor * color.green);
3735 new.blue = min (0xffff, factor * color.blue);
3736
3737 /* Calculate brightness of COLOR. */
3738 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
3739
3740 /* We only boost colors that are darker than
3741 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3742 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3743 /* Make an additive adjustment to NEW, because it's dark enough so
3744 that scaling by FACTOR alone isn't enough. */
3745 {
3746 /* How far below the limit this color is (0 - 1, 1 being darker). */
3747 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3748 /* The additive adjustment. */
3749 int min_delta = delta * dimness * factor / 2;
3750
3751 if (factor < 1)
3752 {
3753 new.red = max (0, new.red - min_delta);
3754 new.green = max (0, new.green - min_delta);
3755 new.blue = max (0, new.blue - min_delta);
3756 }
3757 else
3758 {
3759 new.red = min (0xffff, min_delta + new.red);
3760 new.green = min (0xffff, min_delta + new.green);
3761 new.blue = min (0xffff, min_delta + new.blue);
3762 }
3763 }
3764
3765 /* Try to allocate the color. */
3766 success_p = x_alloc_nearest_color (f, cmap, &new);
3767 if (success_p)
3768 {
3769 if (new.pixel == *pixel)
3770 {
3771 /* If we end up with the same color as before, try adding
3772 delta to the RGB values. */
3773 x_free_colors (f, &new.pixel, 1);
3774
3775 new.red = min (0xffff, delta + color.red);
3776 new.green = min (0xffff, delta + color.green);
3777 new.blue = min (0xffff, delta + color.blue);
3778 success_p = x_alloc_nearest_color (f, cmap, &new);
3779 }
3780 else
3781 success_p = 1;
3782 *pixel = new.pixel;
3783 }
3784
3785 return success_p;
3786 }
3787
3788
3789 /* Set up the foreground color for drawing relief lines of glyph
3790 string S. RELIEF is a pointer to a struct relief containing the GC
3791 with which lines will be drawn. Use a color that is FACTOR or
3792 DELTA lighter or darker than the relief's background which is found
3793 in S->f->output_data.x->relief_background. If such a color cannot
3794 be allocated, use DEFAULT_PIXEL, instead. */
3795
3796 static void
3797 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3798 struct frame *f;
3799 struct relief *relief;
3800 double factor;
3801 int delta;
3802 unsigned long default_pixel;
3803 {
3804 XGCValues xgcv;
3805 struct x_output *di = f->output_data.x;
3806 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3807 unsigned long pixel;
3808 unsigned long background = di->relief_background;
3809 Colormap cmap = FRAME_X_COLORMAP (f);
3810 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3811 Display *dpy = FRAME_X_DISPLAY (f);
3812
3813 xgcv.graphics_exposures = False;
3814 xgcv.line_width = 1;
3815
3816 /* Free previously allocated color. The color cell will be reused
3817 when it has been freed as many times as it was allocated, so this
3818 doesn't affect faces using the same colors. */
3819 if (relief->gc
3820 && relief->allocated_p)
3821 {
3822 x_free_colors (f, &relief->pixel, 1);
3823 relief->allocated_p = 0;
3824 }
3825
3826 /* Allocate new color. */
3827 xgcv.foreground = default_pixel;
3828 pixel = background;
3829 if (dpyinfo->n_planes != 1
3830 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3831 {
3832 relief->allocated_p = 1;
3833 xgcv.foreground = relief->pixel = pixel;
3834 }
3835
3836 if (relief->gc == 0)
3837 {
3838 xgcv.stipple = dpyinfo->gray;
3839 mask |= GCStipple;
3840 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3841 }
3842 else
3843 XChangeGC (dpy, relief->gc, mask, &xgcv);
3844 }
3845
3846
3847 /* Set up colors for the relief lines around glyph string S. */
3848
3849 static void
3850 x_setup_relief_colors (s)
3851 struct glyph_string *s;
3852 {
3853 struct x_output *di = s->f->output_data.x;
3854 unsigned long color;
3855
3856 if (s->face->use_box_color_for_shadows_p)
3857 color = s->face->box_color;
3858 else
3859 {
3860 XGCValues xgcv;
3861
3862 /* Get the background color of the face. */
3863 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3864 color = xgcv.background;
3865 }
3866
3867 if (di->white_relief.gc == 0
3868 || color != di->relief_background)
3869 {
3870 di->relief_background = color;
3871 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3872 WHITE_PIX_DEFAULT (s->f));
3873 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3874 BLACK_PIX_DEFAULT (s->f));
3875 }
3876 }
3877
3878
3879 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3880 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3881 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3882 relief. LEFT_P non-zero means draw a relief on the left side of
3883 the rectangle. RIGHT_P non-zero means draw a relief on the right
3884 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3885 when drawing. */
3886
3887 static void
3888 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3889 raised_p, left_p, right_p, clip_rect)
3890 struct frame *f;
3891 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3892 XRectangle *clip_rect;
3893 {
3894 int i;
3895 GC gc;
3896
3897 if (raised_p)
3898 gc = f->output_data.x->white_relief.gc;
3899 else
3900 gc = f->output_data.x->black_relief.gc;
3901 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3902
3903 /* Top. */
3904 for (i = 0; i < width; ++i)
3905 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3906 left_x + i * left_p, top_y + i,
3907 right_x + 1 - i * right_p, top_y + i);
3908
3909 /* Left. */
3910 if (left_p)
3911 for (i = 0; i < width; ++i)
3912 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3913 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
3914
3915 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3916 if (raised_p)
3917 gc = f->output_data.x->black_relief.gc;
3918 else
3919 gc = f->output_data.x->white_relief.gc;
3920 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3921
3922 /* Bottom. */
3923 for (i = 0; i < width; ++i)
3924 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3925 left_x + i * left_p + 1, bottom_y - i,
3926 right_x + 1 - i * right_p, bottom_y - i);
3927
3928 /* Right. */
3929 if (right_p)
3930 for (i = 0; i < width; ++i)
3931 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3932 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3933
3934 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3935 }
3936
3937
3938 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3939 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3940 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3941 left side of the rectangle. RIGHT_P non-zero means draw a line
3942 on the right side of the rectangle. CLIP_RECT is the clipping
3943 rectangle to use when drawing. */
3944
3945 static void
3946 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3947 left_p, right_p, clip_rect)
3948 struct glyph_string *s;
3949 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3950 XRectangle *clip_rect;
3951 {
3952 XGCValues xgcv;
3953
3954 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3955 XSetForeground (s->display, s->gc, s->face->box_color);
3956 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3957
3958 /* Top. */
3959 XFillRectangle (s->display, s->window, s->gc,
3960 left_x, top_y, right_x - left_x + 1, width);
3961
3962 /* Left. */
3963 if (left_p)
3964 XFillRectangle (s->display, s->window, s->gc,
3965 left_x, top_y, width, bottom_y - top_y + 1);
3966
3967 /* Bottom. */
3968 XFillRectangle (s->display, s->window, s->gc,
3969 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3970
3971 /* Right. */
3972 if (right_p)
3973 XFillRectangle (s->display, s->window, s->gc,
3974 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3975
3976 XSetForeground (s->display, s->gc, xgcv.foreground);
3977 XSetClipMask (s->display, s->gc, None);
3978 }
3979
3980
3981 /* Draw a box around glyph string S. */
3982
3983 static void
3984 x_draw_glyph_string_box (s)
3985 struct glyph_string *s;
3986 {
3987 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3988 int left_p, right_p;
3989 struct glyph *last_glyph;
3990 XRectangle clip_rect;
3991
3992 last_x = window_box_right (s->w, s->area);
3993 if (s->row->full_width_p
3994 && !s->w->pseudo_window_p)
3995 {
3996 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3997 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3998 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3999 }
4000
4001 /* The glyph that may have a right box line. */
4002 last_glyph = (s->cmp || s->img
4003 ? s->first_glyph
4004 : s->first_glyph + s->nchars - 1);
4005
4006 width = abs (s->face->box_line_width);
4007 raised_p = s->face->box == FACE_RAISED_BOX;
4008 left_x = s->x;
4009 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
4010 ? last_x - 1
4011 : min (last_x, s->x + s->background_width) - 1);
4012 top_y = s->y;
4013 bottom_y = top_y + s->height - 1;
4014
4015 left_p = (s->first_glyph->left_box_line_p
4016 || (s->hl == DRAW_MOUSE_FACE
4017 && (s->prev == NULL
4018 || s->prev->hl != s->hl)));
4019 right_p = (last_glyph->right_box_line_p
4020 || (s->hl == DRAW_MOUSE_FACE
4021 && (s->next == NULL
4022 || s->next->hl != s->hl)));
4023
4024 x_get_glyph_string_clip_rect (s, &clip_rect);
4025
4026 if (s->face->box == FACE_SIMPLE_BOX)
4027 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
4028 left_p, right_p, &clip_rect);
4029 else
4030 {
4031 x_setup_relief_colors (s);
4032 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
4033 width, raised_p, left_p, right_p, &clip_rect);
4034 }
4035 }
4036
4037
4038 /* Draw foreground of image glyph string S. */
4039
4040 static void
4041 x_draw_image_foreground (s)
4042 struct glyph_string *s;
4043 {
4044 int x;
4045 int y = s->ybase - image_ascent (s->img, s->face);
4046
4047 /* If first glyph of S has a left box line, start drawing it to the
4048 right of that line. */
4049 if (s->face->box != FACE_NO_BOX
4050 && s->first_glyph->left_box_line_p)
4051 x = s->x + abs (s->face->box_line_width);
4052 else
4053 x = s->x;
4054
4055 /* If there is a margin around the image, adjust x- and y-position
4056 by that margin. */
4057 x += s->img->hmargin;
4058 y += s->img->vmargin;
4059
4060 if (s->img->pixmap)
4061 {
4062 if (s->img->mask)
4063 {
4064 /* We can't set both a clip mask and use XSetClipRectangles
4065 because the latter also sets a clip mask. We also can't
4066 trust on the shape extension to be available
4067 (XShapeCombineRegion). So, compute the rectangle to draw
4068 manually. */
4069 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4070 | GCFunction);
4071 XGCValues xgcv;
4072 XRectangle clip_rect, image_rect, r;
4073
4074 xgcv.clip_mask = s->img->mask;
4075 xgcv.clip_x_origin = x;
4076 xgcv.clip_y_origin = y;
4077 xgcv.function = GXcopy;
4078 XChangeGC (s->display, s->gc, mask, &xgcv);
4079
4080 x_get_glyph_string_clip_rect (s, &clip_rect);
4081 image_rect.x = x;
4082 image_rect.y = y;
4083 image_rect.width = s->img->width;
4084 image_rect.height = s->img->height;
4085 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4086 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4087 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4088 }
4089 else
4090 {
4091 unsigned long mask = GCClipXOrigin | GCClipYOrigin | GCFunction;
4092 XGCValues xgcv;
4093 XRectangle clip_rect, image_rect, r;
4094
4095 x_get_glyph_string_clip_rect (s, &clip_rect);
4096 image_rect.x = x;
4097 image_rect.y = y;
4098 image_rect.width = s->img->width;
4099 image_rect.height = s->img->height;
4100 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4101 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4102 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4103
4104 /* When the image has a mask, we can expect that at
4105 least part of a mouse highlight or a block cursor will
4106 be visible. If the image doesn't have a mask, make
4107 a block cursor visible by drawing a rectangle around
4108 the image. I believe it's looking better if we do
4109 nothing here for mouse-face. */
4110 if (s->hl == DRAW_CURSOR)
4111 XDrawRectangle (s->display, s->window, s->gc, x, y,
4112 s->img->width - 1, s->img->height - 1);
4113 }
4114 }
4115 else
4116 /* Draw a rectangle if image could not be loaded. */
4117 XDrawRectangle (s->display, s->window, s->gc, x, y,
4118 s->img->width - 1, s->img->height - 1);
4119 }
4120
4121
4122 /* Draw a relief around the image glyph string S. */
4123
4124 static void
4125 x_draw_image_relief (s)
4126 struct glyph_string *s;
4127 {
4128 int x0, y0, x1, y1, thick, raised_p;
4129 XRectangle r;
4130 int x;
4131 int y = s->ybase - image_ascent (s->img, s->face);
4132
4133 /* If first glyph of S has a left box line, start drawing it to the
4134 right of that line. */
4135 if (s->face->box != FACE_NO_BOX
4136 && s->first_glyph->left_box_line_p)
4137 x = s->x + abs (s->face->box_line_width);
4138 else
4139 x = s->x;
4140
4141 /* If there is a margin around the image, adjust x- and y-position
4142 by that margin. */
4143 x += s->img->hmargin;
4144 y += s->img->vmargin;
4145
4146 if (s->hl == DRAW_IMAGE_SUNKEN
4147 || s->hl == DRAW_IMAGE_RAISED)
4148 {
4149 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
4150 raised_p = s->hl == DRAW_IMAGE_RAISED;
4151 }
4152 else
4153 {
4154 thick = abs (s->img->relief);
4155 raised_p = s->img->relief > 0;
4156 }
4157
4158 x0 = x - thick;
4159 y0 = y - thick;
4160 x1 = x + s->img->width + thick - 1;
4161 y1 = y + s->img->height + thick - 1;
4162
4163 x_setup_relief_colors (s);
4164 x_get_glyph_string_clip_rect (s, &r);
4165 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
4166 }
4167
4168
4169 /* Draw the foreground of image glyph string S to PIXMAP. */
4170
4171 static void
4172 x_draw_image_foreground_1 (s, pixmap)
4173 struct glyph_string *s;
4174 Pixmap pixmap;
4175 {
4176 int x;
4177 int y = s->ybase - s->y - image_ascent (s->img, s->face);
4178
4179 /* If first glyph of S has a left box line, start drawing it to the
4180 right of that line. */
4181 if (s->face->box != FACE_NO_BOX
4182 && s->first_glyph->left_box_line_p)
4183 x = abs (s->face->box_line_width);
4184 else
4185 x = 0;
4186
4187 /* If there is a margin around the image, adjust x- and y-position
4188 by that margin. */
4189 x += s->img->hmargin;
4190 y += s->img->vmargin;
4191
4192 if (s->img->pixmap)
4193 {
4194 if (s->img->mask)
4195 {
4196 /* We can't set both a clip mask and use XSetClipRectangles
4197 because the latter also sets a clip mask. We also can't
4198 trust on the shape extension to be available
4199 (XShapeCombineRegion). So, compute the rectangle to draw
4200 manually. */
4201 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4202 | GCFunction);
4203 XGCValues xgcv;
4204
4205 xgcv.clip_mask = s->img->mask;
4206 xgcv.clip_x_origin = x;
4207 xgcv.clip_y_origin = y;
4208 xgcv.function = GXcopy;
4209 XChangeGC (s->display, s->gc, mask, &xgcv);
4210
4211 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4212 0, 0, s->img->width, s->img->height, x, y);
4213 XSetClipMask (s->display, s->gc, None);
4214 }
4215 else
4216 {
4217 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4218 0, 0, s->img->width, s->img->height, x, y);
4219
4220 /* When the image has a mask, we can expect that at
4221 least part of a mouse highlight or a block cursor will
4222 be visible. If the image doesn't have a mask, make
4223 a block cursor visible by drawing a rectangle around
4224 the image. I believe it's looking better if we do
4225 nothing here for mouse-face. */
4226 if (s->hl == DRAW_CURSOR)
4227 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4228 s->img->width - 1, s->img->height - 1);
4229 }
4230 }
4231 else
4232 /* Draw a rectangle if image could not be loaded. */
4233 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4234 s->img->width - 1, s->img->height - 1);
4235 }
4236
4237
4238 /* Draw part of the background of glyph string S. X, Y, W, and H
4239 give the rectangle to draw. */
4240
4241 static void
4242 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4243 struct glyph_string *s;
4244 int x, y, w, h;
4245 {
4246 if (s->stippled_p)
4247 {
4248 /* Fill background with a stipple pattern. */
4249 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4250 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4251 XSetFillStyle (s->display, s->gc, FillSolid);
4252 }
4253 else
4254 x_clear_glyph_string_rect (s, x, y, w, h);
4255 }
4256
4257
4258 /* Draw image glyph string S.
4259
4260 s->y
4261 s->x +-------------------------
4262 | s->face->box
4263 |
4264 | +-------------------------
4265 | | s->img->margin
4266 | |
4267 | | +-------------------
4268 | | | the image
4269
4270 */
4271
4272 static void
4273 x_draw_image_glyph_string (s)
4274 struct glyph_string *s;
4275 {
4276 int x, y;
4277 int box_line_hwidth = abs (s->face->box_line_width);
4278 int box_line_vwidth = max (s->face->box_line_width, 0);
4279 int height;
4280 Pixmap pixmap = None;
4281
4282 height = s->height - 2 * box_line_vwidth;
4283
4284 /* Fill background with face under the image. Do it only if row is
4285 taller than image or if image has a clip mask to reduce
4286 flickering. */
4287 s->stippled_p = s->face->stipple != 0;
4288 if (height > s->img->height
4289 || s->img->hmargin
4290 || s->img->vmargin
4291 || s->img->mask
4292 || s->img->pixmap == 0
4293 || s->width != s->background_width)
4294 {
4295 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4296 x = s->x + box_line_hwidth;
4297 else
4298 x = s->x;
4299
4300 y = s->y + box_line_vwidth;
4301
4302 if (s->img->mask)
4303 {
4304 /* Create a pixmap as large as the glyph string. Fill it
4305 with the background color. Copy the image to it, using
4306 its mask. Copy the temporary pixmap to the display. */
4307 Screen *screen = FRAME_X_SCREEN (s->f);
4308 int depth = DefaultDepthOfScreen (screen);
4309
4310 /* Create a pixmap as large as the glyph string. */
4311 pixmap = XCreatePixmap (s->display, s->window,
4312 s->background_width,
4313 s->height, depth);
4314
4315 /* Don't clip in the following because we're working on the
4316 pixmap. */
4317 XSetClipMask (s->display, s->gc, None);
4318
4319 /* Fill the pixmap with the background color/stipple. */
4320 if (s->stippled_p)
4321 {
4322 /* Fill background with a stipple pattern. */
4323 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4324 XFillRectangle (s->display, pixmap, s->gc,
4325 0, 0, s->background_width, s->height);
4326 XSetFillStyle (s->display, s->gc, FillSolid);
4327 }
4328 else
4329 {
4330 XGCValues xgcv;
4331 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4332 &xgcv);
4333 XSetForeground (s->display, s->gc, xgcv.background);
4334 XFillRectangle (s->display, pixmap, s->gc,
4335 0, 0, s->background_width, s->height);
4336 XSetForeground (s->display, s->gc, xgcv.foreground);
4337 }
4338 }
4339 else
4340 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4341
4342 s->background_filled_p = 1;
4343 }
4344
4345 /* Draw the foreground. */
4346 if (pixmap != None)
4347 {
4348 x_draw_image_foreground_1 (s, pixmap);
4349 x_set_glyph_string_clipping (s);
4350 XCopyArea (s->display, pixmap, s->window, s->gc,
4351 0, 0, s->background_width, s->height, s->x, s->y);
4352 XFreePixmap (s->display, pixmap);
4353 }
4354 else
4355 x_draw_image_foreground (s);
4356
4357 /* If we must draw a relief around the image, do it. */
4358 if (s->img->relief
4359 || s->hl == DRAW_IMAGE_RAISED
4360 || s->hl == DRAW_IMAGE_SUNKEN)
4361 x_draw_image_relief (s);
4362 }
4363
4364
4365 /* Draw stretch glyph string S. */
4366
4367 static void
4368 x_draw_stretch_glyph_string (s)
4369 struct glyph_string *s;
4370 {
4371 xassert (s->first_glyph->type == STRETCH_GLYPH);
4372 s->stippled_p = s->face->stipple != 0;
4373
4374 if (s->hl == DRAW_CURSOR
4375 && !x_stretch_cursor_p)
4376 {
4377 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4378 as wide as the stretch glyph. */
4379 int width = min (CANON_X_UNIT (s->f), s->background_width);
4380
4381 /* Draw cursor. */
4382 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4383
4384 /* Clear rest using the GC of the original non-cursor face. */
4385 if (width < s->background_width)
4386 {
4387 GC gc = s->face->gc;
4388 int x = s->x + width, y = s->y;
4389 int w = s->background_width - width, h = s->height;
4390 XRectangle r;
4391
4392 x_get_glyph_string_clip_rect (s, &r);
4393 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4394
4395 if (s->face->stipple)
4396 {
4397 /* Fill background with a stipple pattern. */
4398 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4399 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4400 XSetFillStyle (s->display, gc, FillSolid);
4401 }
4402 else
4403 {
4404 XGCValues xgcv;
4405 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4406 XSetForeground (s->display, gc, xgcv.background);
4407 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4408 XSetForeground (s->display, gc, xgcv.foreground);
4409 }
4410 }
4411 }
4412 else if (!s->background_filled_p)
4413 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4414 s->height);
4415
4416 s->background_filled_p = 1;
4417 }
4418
4419
4420 /* Draw glyph string S. */
4421
4422 static void
4423 x_draw_glyph_string (s)
4424 struct glyph_string *s;
4425 {
4426 int relief_drawn_p = 0;
4427
4428 /* If S draws into the background of its successor, draw the
4429 background of the successor first so that S can draw into it.
4430 This makes S->next use XDrawString instead of XDrawImageString. */
4431 if (s->next && s->right_overhang && !s->for_overlaps_p)
4432 {
4433 xassert (s->next->img == NULL);
4434 x_set_glyph_string_gc (s->next);
4435 x_set_glyph_string_clipping (s->next);
4436 x_draw_glyph_string_background (s->next, 1);
4437 }
4438
4439 /* Set up S->gc, set clipping and draw S. */
4440 x_set_glyph_string_gc (s);
4441 x_set_glyph_string_clipping (s);
4442
4443 /* Draw relief (if any) in advance for char/composition so that the
4444 glyph string can be drawn over it. */
4445 if (!s->for_overlaps_p
4446 && s->face->box != FACE_NO_BOX
4447 && (s->first_glyph->type == CHAR_GLYPH
4448 || s->first_glyph->type == COMPOSITE_GLYPH))
4449
4450 {
4451 x_draw_glyph_string_background (s, 1);
4452 x_draw_glyph_string_box (s);
4453 relief_drawn_p = 1;
4454 }
4455
4456 switch (s->first_glyph->type)
4457 {
4458 case IMAGE_GLYPH:
4459 x_draw_image_glyph_string (s);
4460 break;
4461
4462 case STRETCH_GLYPH:
4463 x_draw_stretch_glyph_string (s);
4464 break;
4465
4466 case CHAR_GLYPH:
4467 if (s->for_overlaps_p)
4468 s->background_filled_p = 1;
4469 else
4470 x_draw_glyph_string_background (s, 0);
4471 x_draw_glyph_string_foreground (s);
4472 break;
4473
4474 case COMPOSITE_GLYPH:
4475 if (s->for_overlaps_p || s->gidx > 0)
4476 s->background_filled_p = 1;
4477 else
4478 x_draw_glyph_string_background (s, 1);
4479 x_draw_composite_glyph_string_foreground (s);
4480 break;
4481
4482 default:
4483 abort ();
4484 }
4485
4486 if (!s->for_overlaps_p)
4487 {
4488 /* Draw underline. */
4489 if (s->face->underline_p)
4490 {
4491 unsigned long tem, h;
4492 int y;
4493
4494 /* Get the underline thickness. Default is 1 pixel. */
4495 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4496 h = 1;
4497
4498 /* Get the underline position. This is the recommended
4499 vertical offset in pixels from the baseline to the top of
4500 the underline. This is a signed value according to the
4501 specs, and its default is
4502
4503 ROUND ((maximum descent) / 2), with
4504 ROUND(x) = floor (x + 0.5) */
4505
4506 if (x_use_underline_position_properties
4507 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4508 y = s->ybase + (long) tem;
4509 else if (s->face->font)
4510 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4511 else
4512 y = s->y + s->height - h;
4513
4514 if (s->face->underline_defaulted_p)
4515 XFillRectangle (s->display, s->window, s->gc,
4516 s->x, y, s->width, h);
4517 else
4518 {
4519 XGCValues xgcv;
4520 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4521 XSetForeground (s->display, s->gc, s->face->underline_color);
4522 XFillRectangle (s->display, s->window, s->gc,
4523 s->x, y, s->width, h);
4524 XSetForeground (s->display, s->gc, xgcv.foreground);
4525 }
4526 }
4527
4528 /* Draw overline. */
4529 if (s->face->overline_p)
4530 {
4531 unsigned long dy = 0, h = 1;
4532
4533 if (s->face->overline_color_defaulted_p)
4534 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4535 s->width, h);
4536 else
4537 {
4538 XGCValues xgcv;
4539 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4540 XSetForeground (s->display, s->gc, s->face->overline_color);
4541 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4542 s->width, h);
4543 XSetForeground (s->display, s->gc, xgcv.foreground);
4544 }
4545 }
4546
4547 /* Draw strike-through. */
4548 if (s->face->strike_through_p)
4549 {
4550 unsigned long h = 1;
4551 unsigned long dy = (s->height - h) / 2;
4552
4553 if (s->face->strike_through_color_defaulted_p)
4554 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4555 s->width, h);
4556 else
4557 {
4558 XGCValues xgcv;
4559 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4560 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4561 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4562 s->width, h);
4563 XSetForeground (s->display, s->gc, xgcv.foreground);
4564 }
4565 }
4566
4567 /* Draw relief if not yet drawn. */
4568 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
4569 x_draw_glyph_string_box (s);
4570 }
4571
4572 /* Reset clipping. */
4573 XSetClipMask (s->display, s->gc, None);
4574 }
4575
4576
4577 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4578 struct face **, int));
4579
4580
4581 /* Fill glyph string S with composition components specified by S->cmp.
4582
4583 FACES is an array of faces for all components of this composition.
4584 S->gidx is the index of the first component for S.
4585 OVERLAPS_P non-zero means S should draw the foreground only, and
4586 use its physical height for clipping.
4587
4588 Value is the index of a component not in S. */
4589
4590 static int
4591 x_fill_composite_glyph_string (s, faces, overlaps_p)
4592 struct glyph_string *s;
4593 struct face **faces;
4594 int overlaps_p;
4595 {
4596 int i;
4597
4598 xassert (s);
4599
4600 s->for_overlaps_p = overlaps_p;
4601
4602 s->face = faces[s->gidx];
4603 s->font = s->face->font;
4604 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4605
4606 /* For all glyphs of this composition, starting at the offset
4607 S->gidx, until we reach the end of the definition or encounter a
4608 glyph that requires the different face, add it to S. */
4609 ++s->nchars;
4610 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4611 ++s->nchars;
4612
4613 /* All glyph strings for the same composition has the same width,
4614 i.e. the width set for the first component of the composition. */
4615
4616 s->width = s->first_glyph->pixel_width;
4617
4618 /* If the specified font could not be loaded, use the frame's
4619 default font, but record the fact that we couldn't load it in
4620 the glyph string so that we can draw rectangles for the
4621 characters of the glyph string. */
4622 if (s->font == NULL)
4623 {
4624 s->font_not_found_p = 1;
4625 s->font = FRAME_FONT (s->f);
4626 }
4627
4628 /* Adjust base line for subscript/superscript text. */
4629 s->ybase += s->first_glyph->voffset;
4630
4631 xassert (s->face && s->face->gc);
4632
4633 /* This glyph string must always be drawn with 16-bit functions. */
4634 s->two_byte_p = 1;
4635
4636 return s->gidx + s->nchars;
4637 }
4638
4639
4640 /* Fill glyph string S from a sequence of character glyphs.
4641
4642 FACE_ID is the face id of the string. START is the index of the
4643 first glyph to consider, END is the index of the last + 1.
4644 OVERLAPS_P non-zero means S should draw the foreground only, and
4645 use its physical height for clipping.
4646
4647 Value is the index of the first glyph not in S. */
4648
4649 static int
4650 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4651 struct glyph_string *s;
4652 int face_id;
4653 int start, end, overlaps_p;
4654 {
4655 struct glyph *glyph, *last;
4656 int voffset;
4657 int glyph_not_available_p;
4658
4659 xassert (s->f == XFRAME (s->w->frame));
4660 xassert (s->nchars == 0);
4661 xassert (start >= 0 && end > start);
4662
4663 s->for_overlaps_p = overlaps_p,
4664 glyph = s->row->glyphs[s->area] + start;
4665 last = s->row->glyphs[s->area] + end;
4666 voffset = glyph->voffset;
4667
4668 glyph_not_available_p = glyph->glyph_not_available_p;
4669
4670 while (glyph < last
4671 && glyph->type == CHAR_GLYPH
4672 && glyph->voffset == voffset
4673 /* Same face id implies same font, nowadays. */
4674 && glyph->face_id == face_id
4675 && glyph->glyph_not_available_p == glyph_not_available_p)
4676 {
4677 int two_byte_p;
4678
4679 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4680 s->char2b + s->nchars,
4681 &two_byte_p);
4682 s->two_byte_p = two_byte_p;
4683 ++s->nchars;
4684 xassert (s->nchars <= end - start);
4685 s->width += glyph->pixel_width;
4686 ++glyph;
4687 }
4688
4689 s->font = s->face->font;
4690 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4691
4692 /* If the specified font could not be loaded, use the frame's font,
4693 but record the fact that we couldn't load it in
4694 S->font_not_found_p so that we can draw rectangles for the
4695 characters of the glyph string. */
4696 if (s->font == NULL || glyph_not_available_p)
4697 {
4698 s->font_not_found_p = 1;
4699 s->font = FRAME_FONT (s->f);
4700 }
4701
4702 /* Adjust base line for subscript/superscript text. */
4703 s->ybase += voffset;
4704
4705 xassert (s->face && s->face->gc);
4706 return glyph - s->row->glyphs[s->area];
4707 }
4708
4709
4710 /* Fill glyph string S from image glyph S->first_glyph. */
4711
4712 static void
4713 x_fill_image_glyph_string (s)
4714 struct glyph_string *s;
4715 {
4716 xassert (s->first_glyph->type == IMAGE_GLYPH);
4717 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4718 xassert (s->img);
4719 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4720 s->font = s->face->font;
4721 s->width = s->first_glyph->pixel_width;
4722
4723 /* Adjust base line for subscript/superscript text. */
4724 s->ybase += s->first_glyph->voffset;
4725 }
4726
4727
4728 /* Fill glyph string S from a sequence of stretch glyphs.
4729
4730 ROW is the glyph row in which the glyphs are found, AREA is the
4731 area within the row. START is the index of the first glyph to
4732 consider, END is the index of the last + 1.
4733
4734 Value is the index of the first glyph not in S. */
4735
4736 static int
4737 x_fill_stretch_glyph_string (s, row, area, start, end)
4738 struct glyph_string *s;
4739 struct glyph_row *row;
4740 enum glyph_row_area area;
4741 int start, end;
4742 {
4743 struct glyph *glyph, *last;
4744 int voffset, face_id;
4745
4746 xassert (s->first_glyph->type == STRETCH_GLYPH);
4747
4748 glyph = s->row->glyphs[s->area] + start;
4749 last = s->row->glyphs[s->area] + end;
4750 face_id = glyph->face_id;
4751 s->face = FACE_FROM_ID (s->f, face_id);
4752 s->font = s->face->font;
4753 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4754 s->width = glyph->pixel_width;
4755 voffset = glyph->voffset;
4756
4757 for (++glyph;
4758 (glyph < last
4759 && glyph->type == STRETCH_GLYPH
4760 && glyph->voffset == voffset
4761 && glyph->face_id == face_id);
4762 ++glyph)
4763 s->width += glyph->pixel_width;
4764
4765 /* Adjust base line for subscript/superscript text. */
4766 s->ybase += voffset;
4767
4768 xassert (s->face && s->face->gc);
4769 return glyph - s->row->glyphs[s->area];
4770 }
4771
4772
4773 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4774 of XChar2b structures for S; it can't be allocated in
4775 x_init_glyph_string because it must be allocated via `alloca'. W
4776 is the window on which S is drawn. ROW and AREA are the glyph row
4777 and area within the row from which S is constructed. START is the
4778 index of the first glyph structure covered by S. HL is a
4779 face-override for drawing S. */
4780
4781 static void
4782 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4783 struct glyph_string *s;
4784 XChar2b *char2b;
4785 struct window *w;
4786 struct glyph_row *row;
4787 enum glyph_row_area area;
4788 int start;
4789 enum draw_glyphs_face hl;
4790 {
4791 bzero (s, sizeof *s);
4792 s->w = w;
4793 s->f = XFRAME (w->frame);
4794 s->display = FRAME_X_DISPLAY (s->f);
4795 s->window = FRAME_X_WINDOW (s->f);
4796 s->char2b = char2b;
4797 s->hl = hl;
4798 s->row = row;
4799 s->area = area;
4800 s->first_glyph = row->glyphs[area] + start;
4801 s->height = row->height;
4802 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4803
4804 /* Display the internal border below the tool-bar window. */
4805 if (s->w == XWINDOW (s->f->tool_bar_window))
4806 s->y -= s->f->output_data.x->internal_border_width;
4807
4808 s->ybase = s->y + row->ascent;
4809 }
4810
4811
4812 /* Set background width of glyph string S. START is the index of the
4813 first glyph following S. LAST_X is the right-most x-position + 1
4814 in the drawing area. */
4815
4816 static INLINE void
4817 x_set_glyph_string_background_width (s, start, last_x)
4818 struct glyph_string *s;
4819 int start;
4820 int last_x;
4821 {
4822 /* If the face of this glyph string has to be drawn to the end of
4823 the drawing area, set S->extends_to_end_of_line_p. */
4824 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4825
4826 if (start == s->row->used[s->area]
4827 && s->hl == DRAW_NORMAL_TEXT
4828 && s->area == TEXT_AREA
4829 && (s->row->fill_line_p
4830 || s->face->background != default_face->background
4831 || s->face->stipple != default_face->stipple))
4832 s->extends_to_end_of_line_p = 1;
4833
4834 /* If S extends its face to the end of the line, set its
4835 background_width to the distance to the right edge of the drawing
4836 area. */
4837 if (s->extends_to_end_of_line_p)
4838 s->background_width = last_x - s->x + 1;
4839 else
4840 s->background_width = s->width;
4841 }
4842
4843
4844 /* Add a glyph string for a stretch glyph to the list of strings
4845 between HEAD and TAIL. START is the index of the stretch glyph in
4846 row area AREA of glyph row ROW. END is the index of the last glyph
4847 in that glyph row area. X is the current output position assigned
4848 to the new glyph string constructed. HL overrides that face of the
4849 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4850 is the right-most x-position of the drawing area. */
4851
4852 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4853 and below -- keep them on one line. */
4854 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4855 do \
4856 { \
4857 s = (struct glyph_string *) alloca (sizeof *s); \
4858 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4859 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4860 x_append_glyph_string (&HEAD, &TAIL, s); \
4861 s->x = (X); \
4862 } \
4863 while (0)
4864
4865
4866 /* Add a glyph string for an image glyph to the list of strings
4867 between HEAD and TAIL. START is the index of the image glyph in
4868 row area AREA of glyph row ROW. END is the index of the last glyph
4869 in that glyph row area. X is the current output position assigned
4870 to the new glyph string constructed. HL overrides that face of the
4871 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4872 is the right-most x-position of the drawing area. */
4873
4874 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4875 do \
4876 { \
4877 s = (struct glyph_string *) alloca (sizeof *s); \
4878 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4879 x_fill_image_glyph_string (s); \
4880 x_append_glyph_string (&HEAD, &TAIL, s); \
4881 ++START; \
4882 s->x = (X); \
4883 } \
4884 while (0)
4885
4886
4887 /* Add a glyph string for a sequence of character glyphs to the list
4888 of strings between HEAD and TAIL. START is the index of the first
4889 glyph in row area AREA of glyph row ROW that is part of the new
4890 glyph string. END is the index of the last glyph in that glyph row
4891 area. X is the current output position assigned to the new glyph
4892 string constructed. HL overrides that face of the glyph; e.g. it
4893 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4894 right-most x-position of the drawing area. */
4895
4896 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4897 do \
4898 { \
4899 int c, face_id; \
4900 XChar2b *char2b; \
4901 \
4902 c = (ROW)->glyphs[AREA][START].u.ch; \
4903 face_id = (ROW)->glyphs[AREA][START].face_id; \
4904 \
4905 s = (struct glyph_string *) alloca (sizeof *s); \
4906 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4907 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4908 x_append_glyph_string (&HEAD, &TAIL, s); \
4909 s->x = (X); \
4910 START = x_fill_glyph_string (s, face_id, START, END, \
4911 OVERLAPS_P); \
4912 } \
4913 while (0)
4914
4915
4916 /* Add a glyph string for a composite sequence to the list of strings
4917 between HEAD and TAIL. START is the index of the first glyph in
4918 row area AREA of glyph row ROW that is part of the new glyph
4919 string. END is the index of the last glyph in that glyph row area.
4920 X is the current output position assigned to the new glyph string
4921 constructed. HL overrides that face of the glyph; e.g. it is
4922 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4923 x-position of the drawing area. */
4924
4925 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4926 do { \
4927 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4928 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4929 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4930 struct composition *cmp = composition_table[cmp_id]; \
4931 int glyph_len = cmp->glyph_len; \
4932 XChar2b *char2b; \
4933 struct face **faces; \
4934 struct glyph_string *first_s = NULL; \
4935 int n; \
4936 \
4937 base_face = base_face->ascii_face; \
4938 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4939 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4940 /* At first, fill in `char2b' and `faces'. */ \
4941 for (n = 0; n < glyph_len; n++) \
4942 { \
4943 int c = COMPOSITION_GLYPH (cmp, n); \
4944 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4945 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4946 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4947 this_face_id, char2b + n, 1); \
4948 } \
4949 \
4950 /* Make glyph_strings for each glyph sequence that is drawable by \
4951 the same face, and append them to HEAD/TAIL. */ \
4952 for (n = 0; n < cmp->glyph_len;) \
4953 { \
4954 s = (struct glyph_string *) alloca (sizeof *s); \
4955 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4956 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4957 s->cmp = cmp; \
4958 s->gidx = n; \
4959 s->x = (X); \
4960 \
4961 if (n == 0) \
4962 first_s = s; \
4963 \
4964 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4965 } \
4966 \
4967 ++START; \
4968 s = first_s; \
4969 } while (0)
4970
4971
4972 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4973 of AREA of glyph row ROW on window W between indices START and END.
4974 HL overrides the face for drawing glyph strings, e.g. it is
4975 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4976 x-positions of the drawing area.
4977
4978 This is an ugly monster macro construct because we must use alloca
4979 to allocate glyph strings (because x_draw_glyphs can be called
4980 asynchronously). */
4981
4982 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4983 do \
4984 { \
4985 HEAD = TAIL = NULL; \
4986 while (START < END) \
4987 { \
4988 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4989 switch (first_glyph->type) \
4990 { \
4991 case CHAR_GLYPH: \
4992 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
4993 TAIL, HL, X, LAST_X, \
4994 OVERLAPS_P); \
4995 break; \
4996 \
4997 case COMPOSITE_GLYPH: \
4998 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4999 HEAD, TAIL, HL, X, LAST_X,\
5000 OVERLAPS_P); \
5001 break; \
5002 \
5003 case STRETCH_GLYPH: \
5004 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
5005 HEAD, TAIL, HL, X, LAST_X); \
5006 break; \
5007 \
5008 case IMAGE_GLYPH: \
5009 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
5010 TAIL, HL, X, LAST_X); \
5011 break; \
5012 \
5013 default: \
5014 abort (); \
5015 } \
5016 \
5017 x_set_glyph_string_background_width (s, START, LAST_X); \
5018 (X) += s->width; \
5019 } \
5020 } \
5021 while (0)
5022
5023
5024 /* Draw glyphs between START and END in AREA of ROW on window W,
5025 starting at x-position X. X is relative to AREA in W. HL is a
5026 face-override with the following meaning:
5027
5028 DRAW_NORMAL_TEXT draw normally
5029 DRAW_CURSOR draw in cursor face
5030 DRAW_MOUSE_FACE draw in mouse face.
5031 DRAW_INVERSE_VIDEO draw in mode line face
5032 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
5033 DRAW_IMAGE_RAISED draw an image with a raised relief around it
5034
5035 If REAL_START is non-null, return in *REAL_START the real starting
5036 position for display. This can be different from START in case
5037 overlapping glyphs must be displayed. If REAL_END is non-null,
5038 return in *REAL_END the real end position for display. This can be
5039 different from END in case overlapping glyphs must be displayed.
5040
5041 If OVERLAPS_P is non-zero, draw only the foreground of characters
5042 and clip to the physical height of ROW.
5043
5044 Value is the x-position reached, relative to AREA of W. */
5045
5046 static int
5047 x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
5048 overlaps_p)
5049 struct window *w;
5050 int x;
5051 struct glyph_row *row;
5052 enum glyph_row_area area;
5053 int start, end;
5054 enum draw_glyphs_face hl;
5055 int *real_start, *real_end;
5056 int overlaps_p;
5057 {
5058 struct glyph_string *head, *tail;
5059 struct glyph_string *s;
5060 int last_x, area_width;
5061 int x_reached;
5062 int i, j;
5063
5064 /* Let's rather be paranoid than getting a SEGV. */
5065 end = min (end, row->used[area]);
5066 start = max (0, start);
5067 start = min (end, start);
5068 if (real_start)
5069 *real_start = start;
5070 if (real_end)
5071 *real_end = end;
5072
5073 /* Translate X to frame coordinates. Set last_x to the right
5074 end of the drawing area. */
5075 if (row->full_width_p)
5076 {
5077 /* X is relative to the left edge of W, without scroll bars
5078 or flag areas. */
5079 struct frame *f = XFRAME (w->frame);
5080 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
5081 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
5082
5083 x += window_left_x;
5084 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
5085 last_x = window_left_x + area_width;
5086
5087 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5088 {
5089 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5090 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
5091 last_x += width;
5092 else
5093 x -= width;
5094 }
5095
5096 x += FRAME_INTERNAL_BORDER_WIDTH (f);
5097 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
5098 }
5099 else
5100 {
5101 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
5102 area_width = window_box_width (w, area);
5103 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
5104 }
5105
5106 /* Build a doubly-linked list of glyph_string structures between
5107 head and tail from what we have to draw. Note that the macro
5108 BUILD_GLYPH_STRINGS will modify its start parameter. That's
5109 the reason we use a separate variable `i'. */
5110 i = start;
5111 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
5112 overlaps_p);
5113 if (tail)
5114 x_reached = tail->x + tail->background_width;
5115 else
5116 x_reached = x;
5117
5118 /* If there are any glyphs with lbearing < 0 or rbearing > width in
5119 the row, redraw some glyphs in front or following the glyph
5120 strings built above. */
5121 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
5122 {
5123 int dummy_x = 0;
5124 struct glyph_string *h, *t;
5125
5126 /* Compute overhangs for all glyph strings. */
5127 for (s = head; s; s = s->next)
5128 x_compute_glyph_string_overhangs (s);
5129
5130 /* Prepend glyph strings for glyphs in front of the first glyph
5131 string that are overwritten because of the first glyph
5132 string's left overhang. The background of all strings
5133 prepended must be drawn because the first glyph string
5134 draws over it. */
5135 i = x_left_overwritten (head);
5136 if (i >= 0)
5137 {
5138 j = i;
5139 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
5140 DRAW_NORMAL_TEXT, dummy_x, last_x,
5141 overlaps_p);
5142 start = i;
5143 if (real_start)
5144 *real_start = start;
5145 x_compute_overhangs_and_x (t, head->x, 1);
5146 x_prepend_glyph_string_lists (&head, &tail, h, t);
5147 }
5148
5149 /* Prepend glyph strings for glyphs in front of the first glyph
5150 string that overwrite that glyph string because of their
5151 right overhang. For these strings, only the foreground must
5152 be drawn, because it draws over the glyph string at `head'.
5153 The background must not be drawn because this would overwrite
5154 right overhangs of preceding glyphs for which no glyph
5155 strings exist. */
5156 i = x_left_overwriting (head);
5157 if (i >= 0)
5158 {
5159 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
5160 DRAW_NORMAL_TEXT, dummy_x, last_x,
5161 overlaps_p);
5162 for (s = h; s; s = s->next)
5163 s->background_filled_p = 1;
5164 if (real_start)
5165 *real_start = i;
5166 x_compute_overhangs_and_x (t, head->x, 1);
5167 x_prepend_glyph_string_lists (&head, &tail, h, t);
5168 }
5169
5170 /* Append glyphs strings for glyphs following the last glyph
5171 string tail that are overwritten by tail. The background of
5172 these strings has to be drawn because tail's foreground draws
5173 over it. */
5174 i = x_right_overwritten (tail);
5175 if (i >= 0)
5176 {
5177 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5178 DRAW_NORMAL_TEXT, x, last_x,
5179 overlaps_p);
5180 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5181 x_append_glyph_string_lists (&head, &tail, h, t);
5182 if (real_end)
5183 *real_end = i;
5184 }
5185
5186 /* Append glyph strings for glyphs following the last glyph
5187 string tail that overwrite tail. The foreground of such
5188 glyphs has to be drawn because it writes into the background
5189 of tail. The background must not be drawn because it could
5190 paint over the foreground of following glyphs. */
5191 i = x_right_overwriting (tail);
5192 if (i >= 0)
5193 {
5194 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5195 DRAW_NORMAL_TEXT, x, last_x,
5196 overlaps_p);
5197 for (s = h; s; s = s->next)
5198 s->background_filled_p = 1;
5199 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5200 x_append_glyph_string_lists (&head, &tail, h, t);
5201 if (real_end)
5202 *real_end = i;
5203 }
5204 }
5205
5206 /* Draw all strings. */
5207 for (s = head; s; s = s->next)
5208 x_draw_glyph_string (s);
5209
5210 /* Value is the x-position up to which drawn, relative to AREA of W.
5211 This doesn't include parts drawn because of overhangs. */
5212 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5213 if (!row->full_width_p)
5214 {
5215 if (area > LEFT_MARGIN_AREA)
5216 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5217 if (area > TEXT_AREA)
5218 x_reached -= window_box_width (w, TEXT_AREA);
5219 }
5220
5221 return x_reached;
5222 }
5223
5224
5225 /* Fix the display of area AREA of overlapping row ROW in window W. */
5226
5227 static void
5228 x_fix_overlapping_area (w, row, area)
5229 struct window *w;
5230 struct glyph_row *row;
5231 enum glyph_row_area area;
5232 {
5233 int i, x;
5234
5235 BLOCK_INPUT;
5236
5237 if (area == LEFT_MARGIN_AREA)
5238 x = 0;
5239 else if (area == TEXT_AREA)
5240 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5241 else
5242 x = (window_box_width (w, LEFT_MARGIN_AREA)
5243 + window_box_width (w, TEXT_AREA));
5244
5245 for (i = 0; i < row->used[area];)
5246 {
5247 if (row->glyphs[area][i].overlaps_vertically_p)
5248 {
5249 int start = i, start_x = x;
5250
5251 do
5252 {
5253 x += row->glyphs[area][i].pixel_width;
5254 ++i;
5255 }
5256 while (i < row->used[area]
5257 && row->glyphs[area][i].overlaps_vertically_p);
5258
5259 x_draw_glyphs (w, start_x, row, area, start, i,
5260 (row->inverse_p
5261 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5262 NULL, NULL, 1);
5263 }
5264 else
5265 {
5266 x += row->glyphs[area][i].pixel_width;
5267 ++i;
5268 }
5269 }
5270
5271 UNBLOCK_INPUT;
5272 }
5273
5274
5275 /* Output LEN glyphs starting at START at the nominal cursor position.
5276 Advance the nominal cursor over the text. The global variable
5277 updated_window contains the window being updated, updated_row is
5278 the glyph row being updated, and updated_area is the area of that
5279 row being updated. */
5280
5281 static void
5282 x_write_glyphs (start, len)
5283 struct glyph *start;
5284 int len;
5285 {
5286 int x, hpos, real_start, real_end;
5287
5288 xassert (updated_window && updated_row);
5289 BLOCK_INPUT;
5290
5291 /* Write glyphs. */
5292
5293 hpos = start - updated_row->glyphs[updated_area];
5294 x = x_draw_glyphs (updated_window, output_cursor.x,
5295 updated_row, updated_area,
5296 hpos, hpos + len,
5297 (updated_row->inverse_p
5298 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5299 &real_start, &real_end, 0);
5300
5301 /* If we drew over the cursor, note that it is not visible any more. */
5302 note_overwritten_text_cursor (updated_window, real_start,
5303 real_end - real_start);
5304
5305 UNBLOCK_INPUT;
5306
5307 /* Advance the output cursor. */
5308 output_cursor.hpos += len;
5309 output_cursor.x = x;
5310 }
5311
5312
5313 /* Insert LEN glyphs from START at the nominal cursor position. */
5314
5315 static void
5316 x_insert_glyphs (start, len)
5317 struct glyph *start;
5318 register int len;
5319 {
5320 struct frame *f;
5321 struct window *w;
5322 int line_height, shift_by_width, shifted_region_width;
5323 struct glyph_row *row;
5324 struct glyph *glyph;
5325 int frame_x, frame_y, hpos, real_start, real_end;
5326
5327 xassert (updated_window && updated_row);
5328 BLOCK_INPUT;
5329 w = updated_window;
5330 f = XFRAME (WINDOW_FRAME (w));
5331
5332 /* Get the height of the line we are in. */
5333 row = updated_row;
5334 line_height = row->height;
5335
5336 /* Get the width of the glyphs to insert. */
5337 shift_by_width = 0;
5338 for (glyph = start; glyph < start + len; ++glyph)
5339 shift_by_width += glyph->pixel_width;
5340
5341 /* Get the width of the region to shift right. */
5342 shifted_region_width = (window_box_width (w, updated_area)
5343 - output_cursor.x
5344 - shift_by_width);
5345
5346 /* Shift right. */
5347 frame_x = window_box_left (w, updated_area) + output_cursor.x;
5348 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5349 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5350 f->output_data.x->normal_gc,
5351 frame_x, frame_y,
5352 shifted_region_width, line_height,
5353 frame_x + shift_by_width, frame_y);
5354
5355 /* Write the glyphs. */
5356 hpos = start - row->glyphs[updated_area];
5357 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5358 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
5359 note_overwritten_text_cursor (w, real_start, real_end - real_start);
5360
5361 /* Advance the output cursor. */
5362 output_cursor.hpos += len;
5363 output_cursor.x += shift_by_width;
5364 UNBLOCK_INPUT;
5365 }
5366
5367
5368 /* Delete N glyphs at the nominal cursor position. Not implemented
5369 for X frames. */
5370
5371 static void
5372 x_delete_glyphs (n)
5373 register int n;
5374 {
5375 abort ();
5376 }
5377
5378
5379 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5380 If they are <= 0, this is probably an error. */
5381
5382 void
5383 x_clear_area (dpy, window, x, y, width, height, exposures)
5384 Display *dpy;
5385 Window window;
5386 int x, y;
5387 int width, height;
5388 int exposures;
5389 {
5390 xassert (width > 0 && height > 0);
5391 XClearArea (dpy, window, x, y, width, height, exposures);
5392 }
5393
5394
5395 /* Erase the current text line from the nominal cursor position
5396 (inclusive) to pixel column TO_X (exclusive). The idea is that
5397 everything from TO_X onward is already erased.
5398
5399 TO_X is a pixel position relative to updated_area of
5400 updated_window. TO_X == -1 means clear to the end of this area. */
5401
5402 static void
5403 x_clear_end_of_line (to_x)
5404 int to_x;
5405 {
5406 struct frame *f;
5407 struct window *w = updated_window;
5408 int max_x, min_y, max_y;
5409 int from_x, from_y, to_y;
5410
5411 xassert (updated_window && updated_row);
5412 f = XFRAME (w->frame);
5413
5414 if (updated_row->full_width_p)
5415 {
5416 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5417 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5418 && !w->pseudo_window_p)
5419 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5420 }
5421 else
5422 max_x = window_box_width (w, updated_area);
5423 max_y = window_text_bottom_y (w);
5424
5425 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5426 of window. For TO_X > 0, truncate to end of drawing area. */
5427 if (to_x == 0)
5428 return;
5429 else if (to_x < 0)
5430 to_x = max_x;
5431 else
5432 to_x = min (to_x, max_x);
5433
5434 to_y = min (max_y, output_cursor.y + updated_row->height);
5435
5436 /* Notice if the cursor will be cleared by this operation. */
5437 if (!updated_row->full_width_p)
5438 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
5439
5440 from_x = output_cursor.x;
5441
5442 /* Translate to frame coordinates. */
5443 if (updated_row->full_width_p)
5444 {
5445 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5446 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5447 }
5448 else
5449 {
5450 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5451 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5452 }
5453
5454 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5455 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5456 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5457
5458 /* Prevent inadvertently clearing to end of the X window. */
5459 if (to_x > from_x && to_y > from_y)
5460 {
5461 BLOCK_INPUT;
5462 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5463 from_x, from_y, to_x - from_x, to_y - from_y,
5464 False);
5465 UNBLOCK_INPUT;
5466 }
5467 }
5468
5469
5470 /* Clear entire frame. If updating_frame is non-null, clear that
5471 frame. Otherwise clear the selected frame. */
5472
5473 static void
5474 x_clear_frame ()
5475 {
5476 struct frame *f;
5477
5478 if (updating_frame)
5479 f = updating_frame;
5480 else
5481 f = SELECTED_FRAME ();
5482
5483 /* Clearing the frame will erase any cursor, so mark them all as no
5484 longer visible. */
5485 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5486 output_cursor.hpos = output_cursor.vpos = 0;
5487 output_cursor.x = -1;
5488
5489 /* We don't set the output cursor here because there will always
5490 follow an explicit cursor_to. */
5491 BLOCK_INPUT;
5492 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5493
5494 /* We have to clear the scroll bars, too. If we have changed
5495 colors or something like that, then they should be notified. */
5496 x_scroll_bar_clear (f);
5497
5498 XFlush (FRAME_X_DISPLAY (f));
5499 UNBLOCK_INPUT;
5500 }
5501
5502
5503 \f
5504 /* Invert the middle quarter of the frame for .15 sec. */
5505
5506 /* We use the select system call to do the waiting, so we have to make
5507 sure it's available. If it isn't, we just won't do visual bells. */
5508
5509 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5510
5511
5512 /* Subtract the `struct timeval' values X and Y, storing the result in
5513 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5514
5515 static int
5516 timeval_subtract (result, x, y)
5517 struct timeval *result, x, y;
5518 {
5519 /* Perform the carry for the later subtraction by updating y. This
5520 is safer because on some systems the tv_sec member is unsigned. */
5521 if (x.tv_usec < y.tv_usec)
5522 {
5523 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5524 y.tv_usec -= 1000000 * nsec;
5525 y.tv_sec += nsec;
5526 }
5527
5528 if (x.tv_usec - y.tv_usec > 1000000)
5529 {
5530 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5531 y.tv_usec += 1000000 * nsec;
5532 y.tv_sec -= nsec;
5533 }
5534
5535 /* Compute the time remaining to wait. tv_usec is certainly
5536 positive. */
5537 result->tv_sec = x.tv_sec - y.tv_sec;
5538 result->tv_usec = x.tv_usec - y.tv_usec;
5539
5540 /* Return indication of whether the result should be considered
5541 negative. */
5542 return x.tv_sec < y.tv_sec;
5543 }
5544
5545 void
5546 XTflash (f)
5547 struct frame *f;
5548 {
5549 BLOCK_INPUT;
5550
5551 {
5552 GC gc;
5553
5554 /* Create a GC that will use the GXxor function to flip foreground
5555 pixels into background pixels. */
5556 {
5557 XGCValues values;
5558
5559 values.function = GXxor;
5560 values.foreground = (f->output_data.x->foreground_pixel
5561 ^ f->output_data.x->background_pixel);
5562
5563 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5564 GCFunction | GCForeground, &values);
5565 }
5566
5567 {
5568 /* Get the height not including a menu bar widget. */
5569 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5570 /* Height of each line to flash. */
5571 int flash_height = FRAME_LINE_HEIGHT (f);
5572 /* These will be the left and right margins of the rectangles. */
5573 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5574 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5575
5576 int width;
5577
5578 /* Don't flash the area between a scroll bar and the frame
5579 edge it is next to. */
5580 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5581 {
5582 case vertical_scroll_bar_left:
5583 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5584 break;
5585
5586 case vertical_scroll_bar_right:
5587 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5588 break;
5589
5590 default:
5591 break;
5592 }
5593
5594 width = flash_right - flash_left;
5595
5596 /* If window is tall, flash top and bottom line. */
5597 if (height > 3 * FRAME_LINE_HEIGHT (f))
5598 {
5599 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5600 flash_left,
5601 (FRAME_INTERNAL_BORDER_WIDTH (f)
5602 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5603 width, flash_height);
5604 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5605 flash_left,
5606 (height - flash_height
5607 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5608 width, flash_height);
5609 }
5610 else
5611 /* If it is short, flash it all. */
5612 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5613 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5614 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5615
5616 x_flush (f);
5617
5618 {
5619 struct timeval wakeup;
5620
5621 EMACS_GET_TIME (wakeup);
5622
5623 /* Compute time to wait until, propagating carry from usecs. */
5624 wakeup.tv_usec += 150000;
5625 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5626 wakeup.tv_usec %= 1000000;
5627
5628 /* Keep waiting until past the time wakeup or any input gets
5629 available. */
5630 while (! detect_input_pending ())
5631 {
5632 struct timeval current;
5633 struct timeval timeout;
5634
5635 EMACS_GET_TIME (current);
5636
5637 /* Break if result would be negative. */
5638 if (timeval_subtract (&current, wakeup, current))
5639 break;
5640
5641 /* How long `select' should wait. */
5642 timeout.tv_sec = 0;
5643 timeout.tv_usec = 10000;
5644
5645 /* Try to wait that long--but we might wake up sooner. */
5646 select (0, NULL, NULL, NULL, &timeout);
5647 }
5648 }
5649
5650 /* If window is tall, flash top and bottom line. */
5651 if (height > 3 * FRAME_LINE_HEIGHT (f))
5652 {
5653 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5654 flash_left,
5655 (FRAME_INTERNAL_BORDER_WIDTH (f)
5656 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5657 width, flash_height);
5658 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5659 flash_left,
5660 (height - flash_height
5661 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5662 width, flash_height);
5663 }
5664 else
5665 /* If it is short, flash it all. */
5666 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5667 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5668 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5669
5670 XFreeGC (FRAME_X_DISPLAY (f), gc);
5671 x_flush (f);
5672 }
5673 }
5674
5675 UNBLOCK_INPUT;
5676 }
5677
5678 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5679
5680
5681 /* Make audible bell. */
5682
5683 void
5684 XTring_bell ()
5685 {
5686 struct frame *f = SELECTED_FRAME ();
5687
5688 if (FRAME_X_DISPLAY (f))
5689 {
5690 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5691 if (visible_bell)
5692 XTflash (f);
5693 else
5694 #endif
5695 {
5696 BLOCK_INPUT;
5697 XBell (FRAME_X_DISPLAY (f), 0);
5698 XFlush (FRAME_X_DISPLAY (f));
5699 UNBLOCK_INPUT;
5700 }
5701 }
5702 }
5703
5704 \f
5705 /* Specify how many text lines, from the top of the window,
5706 should be affected by insert-lines and delete-lines operations.
5707 This, and those operations, are used only within an update
5708 that is bounded by calls to x_update_begin and x_update_end. */
5709
5710 static void
5711 XTset_terminal_window (n)
5712 register int n;
5713 {
5714 /* This function intentionally left blank. */
5715 }
5716
5717
5718 \f
5719 /***********************************************************************
5720 Line Dance
5721 ***********************************************************************/
5722
5723 /* Perform an insert-lines or delete-lines operation, inserting N
5724 lines or deleting -N lines at vertical position VPOS. */
5725
5726 static void
5727 x_ins_del_lines (vpos, n)
5728 int vpos, n;
5729 {
5730 abort ();
5731 }
5732
5733
5734 /* Scroll part of the display as described by RUN. */
5735
5736 static void
5737 x_scroll_run (w, run)
5738 struct window *w;
5739 struct run *run;
5740 {
5741 struct frame *f = XFRAME (w->frame);
5742 int x, y, width, height, from_y, to_y, bottom_y;
5743
5744 /* Get frame-relative bounding box of the text display area of W,
5745 without mode lines. Include in this box the flags areas to the
5746 left and right of W. */
5747 window_box (w, -1, &x, &y, &width, &height);
5748 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5749 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
5750
5751 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5752 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5753 bottom_y = y + height;
5754
5755 if (to_y < from_y)
5756 {
5757 /* Scrolling up. Make sure we don't copy part of the mode
5758 line at the bottom. */
5759 if (from_y + run->height > bottom_y)
5760 height = bottom_y - from_y;
5761 else
5762 height = run->height;
5763 }
5764 else
5765 {
5766 /* Scolling down. Make sure we don't copy over the mode line.
5767 at the bottom. */
5768 if (to_y + run->height > bottom_y)
5769 height = bottom_y - to_y;
5770 else
5771 height = run->height;
5772 }
5773
5774 BLOCK_INPUT;
5775
5776 /* Cursor off. Will be switched on again in x_update_window_end. */
5777 updated_window = w;
5778 x_clear_cursor (w);
5779
5780 XCopyArea (FRAME_X_DISPLAY (f),
5781 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5782 f->output_data.x->normal_gc,
5783 x, from_y,
5784 width, height,
5785 x, to_y);
5786
5787 UNBLOCK_INPUT;
5788 }
5789
5790
5791 \f
5792 /***********************************************************************
5793 Exposure Events
5794 ***********************************************************************/
5795
5796 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5797 corner of the exposed rectangle. W and H are width and height of
5798 the exposed area. All are pixel values. W or H zero means redraw
5799 the entire frame. */
5800
5801 static void
5802 expose_frame (f, x, y, w, h)
5803 struct frame *f;
5804 int x, y, w, h;
5805 {
5806 XRectangle r;
5807
5808 TRACE ((stderr, "expose_frame "));
5809
5810 /* No need to redraw if frame will be redrawn soon. */
5811 if (FRAME_GARBAGED_P (f))
5812 {
5813 TRACE ((stderr, " garbaged\n"));
5814 return;
5815 }
5816
5817 /* If basic faces haven't been realized yet, there is no point in
5818 trying to redraw anything. This can happen when we get an expose
5819 event while Emacs is starting, e.g. by moving another window. */
5820 if (FRAME_FACE_CACHE (f) == NULL
5821 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5822 {
5823 TRACE ((stderr, " no faces\n"));
5824 return;
5825 }
5826
5827 if (w == 0 || h == 0)
5828 {
5829 r.x = r.y = 0;
5830 r.width = CANON_X_UNIT (f) * f->width;
5831 r.height = CANON_Y_UNIT (f) * f->height;
5832 }
5833 else
5834 {
5835 r.x = x;
5836 r.y = y;
5837 r.width = w;
5838 r.height = h;
5839 }
5840
5841 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5842 expose_window_tree (XWINDOW (f->root_window), &r);
5843
5844 if (WINDOWP (f->tool_bar_window))
5845 expose_window (XWINDOW (f->tool_bar_window), &r);
5846
5847 #ifndef USE_X_TOOLKIT
5848 if (WINDOWP (f->menu_bar_window))
5849 expose_window (XWINDOW (f->menu_bar_window), &r);
5850 #endif /* not USE_X_TOOLKIT */
5851 }
5852
5853
5854 /* Redraw (parts) of all windows in the window tree rooted at W that
5855 intersect R. R contains frame pixel coordinates. */
5856
5857 static void
5858 expose_window_tree (w, r)
5859 struct window *w;
5860 XRectangle *r;
5861 {
5862 while (w)
5863 {
5864 if (!NILP (w->hchild))
5865 expose_window_tree (XWINDOW (w->hchild), r);
5866 else if (!NILP (w->vchild))
5867 expose_window_tree (XWINDOW (w->vchild), r);
5868 else if (expose_window (w, r) == 0)
5869 break;
5870 w = NILP (w->next) ? NULL : XWINDOW (w->next);
5871 }
5872 }
5873
5874
5875 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5876 which intersects rectangle R. R is in window-relative coordinates. */
5877
5878 static void
5879 expose_area (w, row, r, area)
5880 struct window *w;
5881 struct glyph_row *row;
5882 XRectangle *r;
5883 enum glyph_row_area area;
5884 {
5885 struct glyph *first = row->glyphs[area];
5886 struct glyph *end = row->glyphs[area] + row->used[area];
5887 struct glyph *last;
5888 int first_x, start_x, x;
5889
5890 if (area == TEXT_AREA && row->fill_line_p)
5891 /* If row extends face to end of line write the whole line. */
5892 x_draw_glyphs (w, 0, row, area,
5893 0, row->used[area],
5894 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5895 NULL, NULL, 0);
5896 else
5897 {
5898 /* Set START_X to the window-relative start position for drawing glyphs of
5899 AREA. The first glyph of the text area can be partially visible.
5900 The first glyphs of other areas cannot. */
5901 if (area == LEFT_MARGIN_AREA)
5902 start_x = 0;
5903 else if (area == TEXT_AREA)
5904 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5905 else
5906 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5907 + window_box_width (w, TEXT_AREA));
5908 x = start_x;
5909
5910 /* Find the first glyph that must be redrawn. */
5911 while (first < end
5912 && x + first->pixel_width < r->x)
5913 {
5914 x += first->pixel_width;
5915 ++first;
5916 }
5917
5918 /* Find the last one. */
5919 last = first;
5920 first_x = x;
5921 while (last < end
5922 && x < r->x + r->width)
5923 {
5924 x += last->pixel_width;
5925 ++last;
5926 }
5927
5928 /* Repaint. */
5929 if (last > first)
5930 x_draw_glyphs (w, first_x - start_x, row, area,
5931 first - row->glyphs[area],
5932 last - row->glyphs[area],
5933 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5934 NULL, NULL, 0);
5935 }
5936 }
5937
5938
5939 /* Redraw the parts of the glyph row ROW on window W intersecting
5940 rectangle R. R is in window-relative coordinates. */
5941
5942 static void
5943 expose_line (w, row, r)
5944 struct window *w;
5945 struct glyph_row *row;
5946 XRectangle *r;
5947 {
5948 xassert (row->enabled_p);
5949
5950 if (row->mode_line_p || w->pseudo_window_p)
5951 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5952 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5953 NULL, NULL, 0);
5954 else
5955 {
5956 if (row->used[LEFT_MARGIN_AREA])
5957 expose_area (w, row, r, LEFT_MARGIN_AREA);
5958 if (row->used[TEXT_AREA])
5959 expose_area (w, row, r, TEXT_AREA);
5960 if (row->used[RIGHT_MARGIN_AREA])
5961 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5962 x_draw_row_bitmaps (w, row);
5963 }
5964 }
5965
5966
5967 /* Return non-zero if W's cursor intersects rectangle R. */
5968
5969 static int
5970 x_phys_cursor_in_rect_p (w, r)
5971 struct window *w;
5972 XRectangle *r;
5973 {
5974 XRectangle cr, result;
5975 struct glyph *cursor_glyph;
5976
5977 cursor_glyph = get_phys_cursor_glyph (w);
5978 if (cursor_glyph)
5979 {
5980 cr.x = w->phys_cursor.x;
5981 cr.y = w->phys_cursor.y;
5982 cr.width = cursor_glyph->pixel_width;
5983 cr.height = w->phys_cursor_height;
5984 return x_intersect_rectangles (&cr, r, &result);
5985 }
5986 else
5987 return 0;
5988 }
5989
5990
5991 /* Redraw the part of window W intersection rectangle FR. Pixel
5992 coordinates in FR are frame-relative. Call this function with
5993 input blocked. */
5994
5995 static int
5996 expose_window (w, fr)
5997 struct window *w;
5998 XRectangle *fr;
5999 {
6000 struct frame *f = XFRAME (w->frame);
6001 XRectangle wr, r;
6002
6003 /* If window is not yet fully initialized, do nothing. This can
6004 happen when toolkit scroll bars are used and a window is split.
6005 Reconfiguring the scroll bar will generate an expose for a newly
6006 created window. */
6007 if (w->current_matrix == NULL)
6008 return 1;
6009
6010 /* When we're currently updating the window, display and current
6011 matrix usually don't agree. Arrange for a thorough display
6012 later. */
6013 if (w == updated_window)
6014 {
6015 SET_FRAME_GARBAGED (f);
6016 return 0;
6017 }
6018
6019 /* Frame-relative pixel rectangle of W. */
6020 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
6021 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
6022 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
6023 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
6024
6025 if (x_intersect_rectangles (fr, &wr, &r))
6026 {
6027 int yb = window_text_bottom_y (w);
6028 struct glyph_row *row;
6029 int cursor_cleared_p;
6030
6031 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
6032 r.x, r.y, r.width, r.height));
6033
6034 /* Convert to window coordinates. */
6035 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
6036 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
6037
6038 /* Turn off the cursor. */
6039 if (!w->pseudo_window_p
6040 && x_phys_cursor_in_rect_p (w, &r))
6041 {
6042 x_clear_cursor (w);
6043 cursor_cleared_p = 1;
6044 }
6045 else
6046 cursor_cleared_p = 0;
6047
6048 /* Find the first row intersecting the rectangle R. */
6049 for (row = w->current_matrix->rows;
6050 row->enabled_p;
6051 ++row)
6052 {
6053 int y0 = row->y;
6054 int y1 = MATRIX_ROW_BOTTOM_Y (row);
6055
6056 if ((y0 >= r.y && y0 < r.y + r.height)
6057 || (y1 > r.y && y1 < r.y + r.height)
6058 || (r.y >= y0 && r.y < y1)
6059 || (r.y + r.height > y0 && r.y + r.height < y1))
6060 expose_line (w, row, &r);
6061
6062 if (y1 >= yb)
6063 break;
6064 }
6065
6066 /* Display the mode line if there is one. */
6067 if (WINDOW_WANTS_MODELINE_P (w)
6068 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
6069 row->enabled_p)
6070 && row->y < r.y + r.height)
6071 expose_line (w, row, &r);
6072
6073 if (!w->pseudo_window_p)
6074 {
6075 /* Draw border between windows. */
6076 x_draw_vertical_border (w);
6077
6078 /* Turn the cursor on again. */
6079 if (cursor_cleared_p)
6080 x_update_window_cursor (w, 1);
6081 }
6082 }
6083
6084 return 1;
6085 }
6086
6087
6088 /* Determine the intersection of two rectangles R1 and R2. Return
6089 the intersection in *RESULT. Value is non-zero if RESULT is not
6090 empty. */
6091
6092 static int
6093 x_intersect_rectangles (r1, r2, result)
6094 XRectangle *r1, *r2, *result;
6095 {
6096 XRectangle *left, *right;
6097 XRectangle *upper, *lower;
6098 int intersection_p = 0;
6099
6100 /* Rearrange so that R1 is the left-most rectangle. */
6101 if (r1->x < r2->x)
6102 left = r1, right = r2;
6103 else
6104 left = r2, right = r1;
6105
6106 /* X0 of the intersection is right.x0, if this is inside R1,
6107 otherwise there is no intersection. */
6108 if (right->x <= left->x + left->width)
6109 {
6110 result->x = right->x;
6111
6112 /* The right end of the intersection is the minimum of the
6113 the right ends of left and right. */
6114 result->width = (min (left->x + left->width, right->x + right->width)
6115 - result->x);
6116
6117 /* Same game for Y. */
6118 if (r1->y < r2->y)
6119 upper = r1, lower = r2;
6120 else
6121 upper = r2, lower = r1;
6122
6123 /* The upper end of the intersection is lower.y0, if this is inside
6124 of upper. Otherwise, there is no intersection. */
6125 if (lower->y <= upper->y + upper->height)
6126 {
6127 result->y = lower->y;
6128
6129 /* The lower end of the intersection is the minimum of the lower
6130 ends of upper and lower. */
6131 result->height = (min (lower->y + lower->height,
6132 upper->y + upper->height)
6133 - result->y);
6134 intersection_p = 1;
6135 }
6136 }
6137
6138 return intersection_p;
6139 }
6140
6141
6142
6143
6144 \f
6145 static void
6146 frame_highlight (f)
6147 struct frame *f;
6148 {
6149 /* We used to only do this if Vx_no_window_manager was non-nil, but
6150 the ICCCM (section 4.1.6) says that the window's border pixmap
6151 and border pixel are window attributes which are "private to the
6152 client", so we can always change it to whatever we want. */
6153 BLOCK_INPUT;
6154 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6155 f->output_data.x->border_pixel);
6156 UNBLOCK_INPUT;
6157 x_update_cursor (f, 1);
6158 }
6159
6160 static void
6161 frame_unhighlight (f)
6162 struct frame *f;
6163 {
6164 /* We used to only do this if Vx_no_window_manager was non-nil, but
6165 the ICCCM (section 4.1.6) says that the window's border pixmap
6166 and border pixel are window attributes which are "private to the
6167 client", so we can always change it to whatever we want. */
6168 BLOCK_INPUT;
6169 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6170 f->output_data.x->border_tile);
6171 UNBLOCK_INPUT;
6172 x_update_cursor (f, 1);
6173 }
6174
6175 /* The focus has changed. Update the frames as necessary to reflect
6176 the new situation. Note that we can't change the selected frame
6177 here, because the Lisp code we are interrupting might become confused.
6178 Each event gets marked with the frame in which it occurred, so the
6179 Lisp code can tell when the switch took place by examining the events. */
6180
6181 static void
6182 x_new_focus_frame (dpyinfo, frame)
6183 struct x_display_info *dpyinfo;
6184 struct frame *frame;
6185 {
6186 struct frame *old_focus = dpyinfo->x_focus_frame;
6187
6188 if (frame != dpyinfo->x_focus_frame)
6189 {
6190 /* Set this before calling other routines, so that they see
6191 the correct value of x_focus_frame. */
6192 dpyinfo->x_focus_frame = frame;
6193
6194 if (old_focus && old_focus->auto_lower)
6195 x_lower_frame (old_focus);
6196
6197 #if 0
6198 selected_frame = frame;
6199 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6200 selected_frame);
6201 Fselect_window (selected_frame->selected_window);
6202 choose_minibuf_frame ();
6203 #endif /* ! 0 */
6204
6205 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6206 pending_autoraise_frame = dpyinfo->x_focus_frame;
6207 else
6208 pending_autoraise_frame = 0;
6209 }
6210
6211 x_frame_rehighlight (dpyinfo);
6212 }
6213
6214 /* Handle an event saying the mouse has moved out of an Emacs frame. */
6215
6216 void
6217 x_mouse_leave (dpyinfo)
6218 struct x_display_info *dpyinfo;
6219 {
6220 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
6221 }
6222
6223 /* The focus has changed, or we have redirected a frame's focus to
6224 another frame (this happens when a frame uses a surrogate
6225 mini-buffer frame). Shift the highlight as appropriate.
6226
6227 The FRAME argument doesn't necessarily have anything to do with which
6228 frame is being highlighted or un-highlighted; we only use it to find
6229 the appropriate X display info. */
6230
6231 static void
6232 XTframe_rehighlight (frame)
6233 struct frame *frame;
6234 {
6235 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6236 }
6237
6238 static void
6239 x_frame_rehighlight (dpyinfo)
6240 struct x_display_info *dpyinfo;
6241 {
6242 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6243
6244 if (dpyinfo->x_focus_frame)
6245 {
6246 dpyinfo->x_highlight_frame
6247 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6248 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6249 : dpyinfo->x_focus_frame);
6250 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
6251 {
6252 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6253 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
6254 }
6255 }
6256 else
6257 dpyinfo->x_highlight_frame = 0;
6258
6259 if (dpyinfo->x_highlight_frame != old_highlight)
6260 {
6261 if (old_highlight)
6262 frame_unhighlight (old_highlight);
6263 if (dpyinfo->x_highlight_frame)
6264 frame_highlight (dpyinfo->x_highlight_frame);
6265 }
6266 }
6267
6268
6269 \f
6270 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
6271
6272 /* Initialize mode_switch_bit and modifier_meaning. */
6273 static void
6274 x_find_modifier_meanings (dpyinfo)
6275 struct x_display_info *dpyinfo;
6276 {
6277 int min_code, max_code;
6278 KeySym *syms;
6279 int syms_per_code;
6280 XModifierKeymap *mods;
6281
6282 dpyinfo->meta_mod_mask = 0;
6283 dpyinfo->shift_lock_mask = 0;
6284 dpyinfo->alt_mod_mask = 0;
6285 dpyinfo->super_mod_mask = 0;
6286 dpyinfo->hyper_mod_mask = 0;
6287
6288 #ifdef HAVE_X11R4
6289 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
6290 #else
6291 min_code = dpyinfo->display->min_keycode;
6292 max_code = dpyinfo->display->max_keycode;
6293 #endif
6294
6295 syms = XGetKeyboardMapping (dpyinfo->display,
6296 min_code, max_code - min_code + 1,
6297 &syms_per_code);
6298 mods = XGetModifierMapping (dpyinfo->display);
6299
6300 /* Scan the modifier table to see which modifier bits the Meta and
6301 Alt keysyms are on. */
6302 {
6303 int row, col; /* The row and column in the modifier table. */
6304
6305 for (row = 3; row < 8; row++)
6306 for (col = 0; col < mods->max_keypermod; col++)
6307 {
6308 KeyCode code
6309 = mods->modifiermap[(row * mods->max_keypermod) + col];
6310
6311 /* Zeroes are used for filler. Skip them. */
6312 if (code == 0)
6313 continue;
6314
6315 /* Are any of this keycode's keysyms a meta key? */
6316 {
6317 int code_col;
6318
6319 for (code_col = 0; code_col < syms_per_code; code_col++)
6320 {
6321 int sym = syms[((code - min_code) * syms_per_code) + code_col];
6322
6323 switch (sym)
6324 {
6325 case XK_Meta_L:
6326 case XK_Meta_R:
6327 dpyinfo->meta_mod_mask |= (1 << row);
6328 break;
6329
6330 case XK_Alt_L:
6331 case XK_Alt_R:
6332 dpyinfo->alt_mod_mask |= (1 << row);
6333 break;
6334
6335 case XK_Hyper_L:
6336 case XK_Hyper_R:
6337 dpyinfo->hyper_mod_mask |= (1 << row);
6338 break;
6339
6340 case XK_Super_L:
6341 case XK_Super_R:
6342 dpyinfo->super_mod_mask |= (1 << row);
6343 break;
6344
6345 case XK_Shift_Lock:
6346 /* Ignore this if it's not on the lock modifier. */
6347 if ((1 << row) == LockMask)
6348 dpyinfo->shift_lock_mask = LockMask;
6349 break;
6350 }
6351 }
6352 }
6353 }
6354 }
6355
6356 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
6357 if (! dpyinfo->meta_mod_mask)
6358 {
6359 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6360 dpyinfo->alt_mod_mask = 0;
6361 }
6362
6363 /* If some keys are both alt and meta,
6364 make them just meta, not alt. */
6365 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6366 {
6367 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6368 }
6369
6370 XFree ((char *) syms);
6371 XFreeModifiermap (mods);
6372 }
6373
6374 /* Convert between the modifier bits X uses and the modifier bits
6375 Emacs uses. */
6376
6377 static unsigned int
6378 x_x_to_emacs_modifiers (dpyinfo, state)
6379 struct x_display_info *dpyinfo;
6380 unsigned int state;
6381 {
6382 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6383 | ((state & ControlMask) ? ctrl_modifier : 0)
6384 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6385 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6386 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6387 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
6388 }
6389
6390 static unsigned int
6391 x_emacs_to_x_modifiers (dpyinfo, state)
6392 struct x_display_info *dpyinfo;
6393 unsigned int state;
6394 {
6395 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6396 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6397 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6398 | ((state & shift_modifier) ? ShiftMask : 0)
6399 | ((state & ctrl_modifier) ? ControlMask : 0)
6400 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
6401 }
6402
6403 /* Convert a keysym to its name. */
6404
6405 char *
6406 x_get_keysym_name (keysym)
6407 KeySym keysym;
6408 {
6409 char *value;
6410
6411 BLOCK_INPUT;
6412 value = XKeysymToString (keysym);
6413 UNBLOCK_INPUT;
6414
6415 return value;
6416 }
6417
6418
6419 \f
6420 /* Mouse clicks and mouse movement. Rah. */
6421
6422 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6423 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6424 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6425 not force the value into range. */
6426
6427 void
6428 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6429 FRAME_PTR f;
6430 register int pix_x, pix_y;
6431 register int *x, *y;
6432 XRectangle *bounds;
6433 int noclip;
6434 {
6435 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6436 even for negative values. */
6437 if (pix_x < 0)
6438 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6439 if (pix_y < 0)
6440 pix_y -= (f)->output_data.x->line_height - 1;
6441
6442 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6443 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6444
6445 if (bounds)
6446 {
6447 bounds->width = FONT_WIDTH (f->output_data.x->font);
6448 bounds->height = f->output_data.x->line_height;
6449 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6450 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6451 }
6452
6453 if (!noclip)
6454 {
6455 if (pix_x < 0)
6456 pix_x = 0;
6457 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6458 pix_x = FRAME_WINDOW_WIDTH (f);
6459
6460 if (pix_y < 0)
6461 pix_y = 0;
6462 else if (pix_y > f->height)
6463 pix_y = f->height;
6464 }
6465
6466 *x = pix_x;
6467 *y = pix_y;
6468 }
6469
6470
6471 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6472 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6473 can't tell the positions because W's display is not up to date,
6474 return 0. */
6475
6476 int
6477 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6478 struct window *w;
6479 int hpos, vpos;
6480 int *frame_x, *frame_y;
6481 {
6482 int success_p;
6483
6484 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6485 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6486
6487 if (display_completed)
6488 {
6489 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6490 struct glyph *glyph = row->glyphs[TEXT_AREA];
6491 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6492
6493 *frame_y = row->y;
6494 *frame_x = row->x;
6495 while (glyph < end)
6496 {
6497 *frame_x += glyph->pixel_width;
6498 ++glyph;
6499 }
6500
6501 success_p = 1;
6502 }
6503 else
6504 {
6505 *frame_y = *frame_x = 0;
6506 success_p = 0;
6507 }
6508
6509 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6510 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6511 return success_p;
6512 }
6513
6514
6515 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6516
6517 If the event is a button press, then note that we have grabbed
6518 the mouse. */
6519
6520 static Lisp_Object
6521 construct_mouse_click (result, event, f)
6522 struct input_event *result;
6523 XButtonEvent *event;
6524 struct frame *f;
6525 {
6526 /* Make the event type no_event; we'll change that when we decide
6527 otherwise. */
6528 result->kind = mouse_click;
6529 result->code = event->button - Button1;
6530 result->timestamp = event->time;
6531 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6532 event->state)
6533 | (event->type == ButtonRelease
6534 ? up_modifier
6535 : down_modifier));
6536
6537 XSETINT (result->x, event->x);
6538 XSETINT (result->y, event->y);
6539 XSETFRAME (result->frame_or_window, f);
6540 result->arg = Qnil;
6541 return Qnil;
6542 }
6543
6544 \f
6545 /* Function to report a mouse movement to the mainstream Emacs code.
6546 The input handler calls this.
6547
6548 We have received a mouse movement event, which is given in *event.
6549 If the mouse is over a different glyph than it was last time, tell
6550 the mainstream emacs code by setting mouse_moved. If not, ask for
6551 another motion event, so we can check again the next time it moves. */
6552
6553 static XMotionEvent last_mouse_motion_event;
6554 static Lisp_Object last_mouse_motion_frame;
6555
6556 static void
6557 note_mouse_movement (frame, event)
6558 FRAME_PTR frame;
6559 XMotionEvent *event;
6560 {
6561 last_mouse_movement_time = event->time;
6562 last_mouse_motion_event = *event;
6563 XSETFRAME (last_mouse_motion_frame, frame);
6564
6565 if (event->window != FRAME_X_WINDOW (frame))
6566 {
6567 frame->mouse_moved = 1;
6568 last_mouse_scroll_bar = Qnil;
6569 note_mouse_highlight (frame, -1, -1);
6570 }
6571
6572 /* Has the mouse moved off the glyph it was on at the last sighting? */
6573 else if (event->x < last_mouse_glyph.x
6574 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6575 || event->y < last_mouse_glyph.y
6576 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6577 {
6578 frame->mouse_moved = 1;
6579 last_mouse_scroll_bar = Qnil;
6580 note_mouse_highlight (frame, event->x, event->y);
6581 }
6582 }
6583
6584 /* This is used for debugging, to turn off note_mouse_highlight. */
6585
6586 int disable_mouse_highlight;
6587
6588
6589 \f
6590 /************************************************************************
6591 Mouse Face
6592 ************************************************************************/
6593
6594 /* Find the glyph under window-relative coordinates X/Y in window W.
6595 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6596 strings. Return in *HPOS and *VPOS the row and column number of
6597 the glyph found. Return in *AREA the glyph area containing X.
6598 Value is a pointer to the glyph found or null if X/Y is not on
6599 text, or we can't tell because W's current matrix is not up to
6600 date. */
6601
6602 static struct glyph *
6603 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
6604 struct window *w;
6605 int x, y;
6606 int *hpos, *vpos, *area;
6607 int buffer_only_p;
6608 {
6609 struct glyph *glyph, *end;
6610 struct glyph_row *row = NULL;
6611 int x0, i, left_area_width;
6612
6613 /* Find row containing Y. Give up if some row is not enabled. */
6614 for (i = 0; i < w->current_matrix->nrows; ++i)
6615 {
6616 row = MATRIX_ROW (w->current_matrix, i);
6617 if (!row->enabled_p)
6618 return NULL;
6619 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6620 break;
6621 }
6622
6623 *vpos = i;
6624 *hpos = 0;
6625
6626 /* Give up if Y is not in the window. */
6627 if (i == w->current_matrix->nrows)
6628 return NULL;
6629
6630 /* Get the glyph area containing X. */
6631 if (w->pseudo_window_p)
6632 {
6633 *area = TEXT_AREA;
6634 x0 = 0;
6635 }
6636 else
6637 {
6638 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6639 if (x < left_area_width)
6640 {
6641 *area = LEFT_MARGIN_AREA;
6642 x0 = 0;
6643 }
6644 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6645 {
6646 *area = TEXT_AREA;
6647 x0 = row->x + left_area_width;
6648 }
6649 else
6650 {
6651 *area = RIGHT_MARGIN_AREA;
6652 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6653 }
6654 }
6655
6656 /* Find glyph containing X. */
6657 glyph = row->glyphs[*area];
6658 end = glyph + row->used[*area];
6659 while (glyph < end)
6660 {
6661 if (x < x0 + glyph->pixel_width)
6662 {
6663 if (w->pseudo_window_p)
6664 break;
6665 else if (!buffer_only_p || BUFFERP (glyph->object))
6666 break;
6667 }
6668
6669 x0 += glyph->pixel_width;
6670 ++glyph;
6671 }
6672
6673 if (glyph == end)
6674 return NULL;
6675
6676 *hpos = glyph - row->glyphs[*area];
6677 return glyph;
6678 }
6679
6680
6681 /* Convert frame-relative x/y to coordinates relative to window W.
6682 Takes pseudo-windows into account. */
6683
6684 static void
6685 frame_to_window_pixel_xy (w, x, y)
6686 struct window *w;
6687 int *x, *y;
6688 {
6689 if (w->pseudo_window_p)
6690 {
6691 /* A pseudo-window is always full-width, and starts at the
6692 left edge of the frame, plus a frame border. */
6693 struct frame *f = XFRAME (w->frame);
6694 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6695 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6696 }
6697 else
6698 {
6699 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6700 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6701 }
6702 }
6703
6704
6705 /* Take proper action when mouse has moved to the mode or header line of
6706 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6707 mode line. X is relative to the start of the text display area of
6708 W, so the width of bitmap areas and scroll bars must be subtracted
6709 to get a position relative to the start of the mode line. */
6710
6711 static void
6712 note_mode_line_highlight (w, x, mode_line_p)
6713 struct window *w;
6714 int x, mode_line_p;
6715 {
6716 struct frame *f = XFRAME (w->frame);
6717 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6718 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6719 struct glyph_row *row;
6720
6721 if (mode_line_p)
6722 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6723 else
6724 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6725
6726 if (row->enabled_p)
6727 {
6728 struct glyph *glyph, *end;
6729 Lisp_Object help, map;
6730 int x0;
6731
6732 /* Find the glyph under X. */
6733 glyph = row->glyphs[TEXT_AREA];
6734 end = glyph + row->used[TEXT_AREA];
6735 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6736 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
6737
6738 while (glyph < end
6739 && x >= x0 + glyph->pixel_width)
6740 {
6741 x0 += glyph->pixel_width;
6742 ++glyph;
6743 }
6744
6745 if (glyph < end
6746 && STRINGP (glyph->object)
6747 && XSTRING (glyph->object)->intervals
6748 && glyph->charpos >= 0
6749 && glyph->charpos < XSTRING (glyph->object)->size)
6750 {
6751 /* If we're on a string with `help-echo' text property,
6752 arrange for the help to be displayed. This is done by
6753 setting the global variable help_echo to the help string. */
6754 help = Fget_text_property (make_number (glyph->charpos),
6755 Qhelp_echo, glyph->object);
6756 if (!NILP (help))
6757 {
6758 help_echo = help;
6759 XSETWINDOW (help_echo_window, w);
6760 help_echo_object = glyph->object;
6761 help_echo_pos = glyph->charpos;
6762 }
6763
6764 /* Change the mouse pointer according to what is under X/Y. */
6765 map = Fget_text_property (make_number (glyph->charpos),
6766 Qlocal_map, glyph->object);
6767 if (KEYMAPP (map))
6768 cursor = f->output_data.x->nontext_cursor;
6769 else
6770 {
6771 map = Fget_text_property (make_number (glyph->charpos),
6772 Qkeymap, glyph->object);
6773 if (KEYMAPP (map))
6774 cursor = f->output_data.x->nontext_cursor;
6775 }
6776 }
6777 }
6778
6779 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6780 }
6781
6782
6783 /* Take proper action when the mouse has moved to position X, Y on
6784 frame F as regards highlighting characters that have mouse-face
6785 properties. Also de-highlighting chars where the mouse was before.
6786 X and Y can be negative or out of range. */
6787
6788 static void
6789 note_mouse_highlight (f, x, y)
6790 struct frame *f;
6791 int x, y;
6792 {
6793 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6794 int portion;
6795 Lisp_Object window;
6796 struct window *w;
6797 Cursor cursor = None;
6798 struct buffer *b;
6799
6800 /* When a menu is active, don't highlight because this looks odd. */
6801 #ifdef USE_X_TOOLKIT
6802 if (popup_activated ())
6803 return;
6804 #endif
6805
6806 if (disable_mouse_highlight
6807 || !f->glyphs_initialized_p)
6808 return;
6809
6810 dpyinfo->mouse_face_mouse_x = x;
6811 dpyinfo->mouse_face_mouse_y = y;
6812 dpyinfo->mouse_face_mouse_frame = f;
6813
6814 if (dpyinfo->mouse_face_defer)
6815 return;
6816
6817 if (gc_in_progress)
6818 {
6819 dpyinfo->mouse_face_deferred_gc = 1;
6820 return;
6821 }
6822
6823 /* Which window is that in? */
6824 window = window_from_coordinates (f, x, y, &portion, 1);
6825
6826 /* If we were displaying active text in another window, clear that. */
6827 if (! EQ (window, dpyinfo->mouse_face_window))
6828 clear_mouse_face (dpyinfo);
6829
6830 /* Not on a window -> return. */
6831 if (!WINDOWP (window))
6832 return;
6833
6834 /* Convert to window-relative pixel coordinates. */
6835 w = XWINDOW (window);
6836 frame_to_window_pixel_xy (w, &x, &y);
6837
6838 /* Handle tool-bar window differently since it doesn't display a
6839 buffer. */
6840 if (EQ (window, f->tool_bar_window))
6841 {
6842 note_tool_bar_highlight (f, x, y);
6843 return;
6844 }
6845
6846 /* Mouse is on the mode or header line? */
6847 if (portion == 1 || portion == 3)
6848 {
6849 note_mode_line_highlight (w, x, portion == 1);
6850 return;
6851 }
6852
6853 if (portion == 2)
6854 cursor = f->output_data.x->horizontal_drag_cursor;
6855 else
6856 cursor = f->output_data.x->text_cursor;
6857
6858 /* Are we in a window whose display is up to date?
6859 And verify the buffer's text has not changed. */
6860 b = XBUFFER (w->buffer);
6861 if (/* Within text portion of the window. */
6862 portion == 0
6863 && EQ (w->window_end_valid, w->buffer)
6864 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
6865 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
6866 {
6867 int hpos, vpos, pos, i, area;
6868 struct glyph *glyph;
6869 Lisp_Object object;
6870 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
6871 Lisp_Object *overlay_vec = NULL;
6872 int len, noverlays;
6873 struct buffer *obuf;
6874 int obegv, ozv, same_region;
6875
6876 /* Find the glyph under X/Y. */
6877 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
6878
6879 /* Clear mouse face if X/Y not over text. */
6880 if (glyph == NULL
6881 || area != TEXT_AREA
6882 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6883 {
6884 if (clear_mouse_face (dpyinfo))
6885 cursor = None;
6886 goto set_cursor;
6887 }
6888
6889 pos = glyph->charpos;
6890 object = glyph->object;
6891 if (!STRINGP (object) && !BUFFERP (object))
6892 goto set_cursor;
6893
6894 /* If we get an out-of-range value, return now; avoid an error. */
6895 if (BUFFERP (object) && pos > BUF_Z (b))
6896 goto set_cursor;
6897
6898 /* Make the window's buffer temporarily current for
6899 overlays_at and compute_char_face. */
6900 obuf = current_buffer;
6901 current_buffer = b;
6902 obegv = BEGV;
6903 ozv = ZV;
6904 BEGV = BEG;
6905 ZV = Z;
6906
6907 /* Is this char mouse-active or does it have help-echo? */
6908 position = make_number (pos);
6909
6910 if (BUFFERP (object))
6911 {
6912 /* Put all the overlays we want in a vector in overlay_vec.
6913 Store the length in len. If there are more than 10, make
6914 enough space for all, and try again. */
6915 len = 10;
6916 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6917 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6918 if (noverlays > len)
6919 {
6920 len = noverlays;
6921 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6922 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6923 }
6924
6925 /* Sort overlays into increasing priority order. */
6926 noverlays = sort_overlays (overlay_vec, noverlays, w);
6927 }
6928 else
6929 noverlays = 0;
6930
6931 same_region = (EQ (window, dpyinfo->mouse_face_window)
6932 && vpos >= dpyinfo->mouse_face_beg_row
6933 && vpos <= dpyinfo->mouse_face_end_row
6934 && (vpos > dpyinfo->mouse_face_beg_row
6935 || hpos >= dpyinfo->mouse_face_beg_col)
6936 && (vpos < dpyinfo->mouse_face_end_row
6937 || hpos < dpyinfo->mouse_face_end_col
6938 || dpyinfo->mouse_face_past_end));
6939
6940 if (same_region)
6941 cursor = None;
6942
6943 /* Check mouse-face highlighting. */
6944 if (! same_region
6945 /* If there exists an overlay with mouse-face overlapping
6946 the one we are currently highlighting, we have to
6947 check if we enter the overlapping overlay, and then
6948 highlight only that. */
6949 || (OVERLAYP (dpyinfo->mouse_face_overlay)
6950 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
6951 {
6952 /* Clear the display of the old active region, if any. */
6953 if (clear_mouse_face (dpyinfo))
6954 cursor = None;
6955
6956 /* Find the highest priority overlay that has a mouse-face
6957 property. */
6958 overlay = Qnil;
6959 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
6960 {
6961 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6962 if (!NILP (mouse_face))
6963 overlay = overlay_vec[i];
6964 }
6965 dpyinfo->mouse_face_overlay = overlay;
6966
6967 /* If no overlay applies, get a text property. */
6968 if (NILP (overlay))
6969 mouse_face = Fget_text_property (position, Qmouse_face, object);
6970
6971 /* Handle the overlay case. */
6972 if (!NILP (overlay))
6973 {
6974 /* Find the range of text around this char that
6975 should be active. */
6976 Lisp_Object before, after;
6977 int ignore;
6978
6979 before = Foverlay_start (overlay);
6980 after = Foverlay_end (overlay);
6981 /* Record this as the current active region. */
6982 fast_find_position (w, XFASTINT (before),
6983 &dpyinfo->mouse_face_beg_col,
6984 &dpyinfo->mouse_face_beg_row,
6985 &dpyinfo->mouse_face_beg_x,
6986 &dpyinfo->mouse_face_beg_y);
6987 dpyinfo->mouse_face_past_end
6988 = !fast_find_position (w, XFASTINT (after),
6989 &dpyinfo->mouse_face_end_col,
6990 &dpyinfo->mouse_face_end_row,
6991 &dpyinfo->mouse_face_end_x,
6992 &dpyinfo->mouse_face_end_y);
6993 dpyinfo->mouse_face_window = window;
6994 dpyinfo->mouse_face_face_id
6995 = face_at_buffer_position (w, pos, 0, 0,
6996 &ignore, pos + 1, 1);
6997
6998 /* Display it as active. */
6999 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7000 }
7001 /* Handle the text property case. */
7002 else if (!NILP (mouse_face) && BUFFERP (object))
7003 {
7004 /* Find the range of text around this char that
7005 should be active. */
7006 Lisp_Object before, after, beginning, end;
7007 int ignore;
7008
7009 beginning = Fmarker_position (w->start);
7010 end = make_number (BUF_Z (XBUFFER (object))
7011 - XFASTINT (w->window_end_pos));
7012 before
7013 = Fprevious_single_property_change (make_number (pos + 1),
7014 Qmouse_face,
7015 object, beginning);
7016 after
7017 = Fnext_single_property_change (position, Qmouse_face,
7018 object, end);
7019
7020 /* Record this as the current active region. */
7021 fast_find_position (w, XFASTINT (before),
7022 &dpyinfo->mouse_face_beg_col,
7023 &dpyinfo->mouse_face_beg_row,
7024 &dpyinfo->mouse_face_beg_x,
7025 &dpyinfo->mouse_face_beg_y);
7026 dpyinfo->mouse_face_past_end
7027 = !fast_find_position (w, XFASTINT (after),
7028 &dpyinfo->mouse_face_end_col,
7029 &dpyinfo->mouse_face_end_row,
7030 &dpyinfo->mouse_face_end_x,
7031 &dpyinfo->mouse_face_end_y);
7032 dpyinfo->mouse_face_window = window;
7033
7034 if (BUFFERP (object))
7035 dpyinfo->mouse_face_face_id
7036 = face_at_buffer_position (w, pos, 0, 0,
7037 &ignore, pos + 1, 1);
7038
7039 /* Display it as active. */
7040 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7041 }
7042 else if (!NILP (mouse_face) && STRINGP (object))
7043 {
7044 Lisp_Object b, e;
7045 int ignore;
7046
7047 b = Fprevious_single_property_change (make_number (pos + 1),
7048 Qmouse_face,
7049 object, Qnil);
7050 e = Fnext_single_property_change (position, Qmouse_face,
7051 object, Qnil);
7052 if (NILP (b))
7053 b = make_number (0);
7054 if (NILP (e))
7055 e = make_number (XSTRING (object)->size - 1);
7056 fast_find_string_pos (w, XINT (b), object,
7057 &dpyinfo->mouse_face_beg_col,
7058 &dpyinfo->mouse_face_beg_row,
7059 &dpyinfo->mouse_face_beg_x,
7060 &dpyinfo->mouse_face_beg_y, 0);
7061 fast_find_string_pos (w, XINT (e), object,
7062 &dpyinfo->mouse_face_end_col,
7063 &dpyinfo->mouse_face_end_row,
7064 &dpyinfo->mouse_face_end_x,
7065 &dpyinfo->mouse_face_end_y, 1);
7066 dpyinfo->mouse_face_past_end = 0;
7067 dpyinfo->mouse_face_window = window;
7068 dpyinfo->mouse_face_face_id
7069 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
7070 glyph->face_id, 1);
7071 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7072 }
7073 }
7074
7075 /* Look for a `help-echo' property. */
7076 {
7077 Lisp_Object help, overlay;
7078
7079 /* Check overlays first. */
7080 help = overlay = Qnil;
7081 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
7082 {
7083 overlay = overlay_vec[i];
7084 help = Foverlay_get (overlay, Qhelp_echo);
7085 }
7086
7087 if (!NILP (help))
7088 {
7089 help_echo = help;
7090 help_echo_window = window;
7091 help_echo_object = overlay;
7092 help_echo_pos = pos;
7093 }
7094 else
7095 {
7096 Lisp_Object object = glyph->object;
7097 int charpos = glyph->charpos;
7098
7099 /* Try text properties. */
7100 if (STRINGP (object)
7101 && charpos >= 0
7102 && charpos < XSTRING (object)->size)
7103 {
7104 help = Fget_text_property (make_number (charpos),
7105 Qhelp_echo, object);
7106 if (NILP (help))
7107 {
7108 /* If the string itself doesn't specify a help-echo,
7109 see if the buffer text ``under'' it does. */
7110 struct glyph_row *r
7111 = MATRIX_ROW (w->current_matrix, vpos);
7112 int start = MATRIX_ROW_START_CHARPOS (r);
7113 int pos = string_buffer_position (w, object, start);
7114 if (pos > 0)
7115 {
7116 help = Fget_text_property (make_number (pos),
7117 Qhelp_echo, w->buffer);
7118 if (!NILP (help))
7119 {
7120 charpos = pos;
7121 object = w->buffer;
7122 }
7123 }
7124 }
7125 }
7126 else if (BUFFERP (object)
7127 && charpos >= BEGV
7128 && charpos < ZV)
7129 help = Fget_text_property (make_number (charpos), Qhelp_echo,
7130 object);
7131
7132 if (!NILP (help))
7133 {
7134 help_echo = help;
7135 help_echo_window = window;
7136 help_echo_object = object;
7137 help_echo_pos = charpos;
7138 }
7139 }
7140 }
7141
7142 BEGV = obegv;
7143 ZV = ozv;
7144 current_buffer = obuf;
7145 }
7146
7147 set_cursor:
7148
7149 if (cursor != None)
7150 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7151 }
7152
7153 static void
7154 redo_mouse_highlight ()
7155 {
7156 if (!NILP (last_mouse_motion_frame)
7157 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
7158 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
7159 last_mouse_motion_event.x,
7160 last_mouse_motion_event.y);
7161 }
7162
7163
7164 \f
7165 /***********************************************************************
7166 Tool-bars
7167 ***********************************************************************/
7168
7169 static int x_tool_bar_item P_ ((struct frame *, int, int,
7170 struct glyph **, int *, int *, int *));
7171
7172 /* Tool-bar item index of the item on which a mouse button was pressed
7173 or -1. */
7174
7175 static int last_tool_bar_item;
7176
7177
7178 /* Get information about the tool-bar item at position X/Y on frame F.
7179 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7180 the current matrix of the tool-bar window of F, or NULL if not
7181 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
7182 item in F->tool_bar_items. Value is
7183
7184 -1 if X/Y is not on a tool-bar item
7185 0 if X/Y is on the same item that was highlighted before.
7186 1 otherwise. */
7187
7188 static int
7189 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
7190 struct frame *f;
7191 int x, y;
7192 struct glyph **glyph;
7193 int *hpos, *vpos, *prop_idx;
7194 {
7195 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7196 struct window *w = XWINDOW (f->tool_bar_window);
7197 int area;
7198
7199 /* Find the glyph under X/Y. */
7200 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
7201 if (*glyph == NULL)
7202 return -1;
7203
7204 /* Get the start of this tool-bar item's properties in
7205 f->tool_bar_items. */
7206 if (!tool_bar_item_info (f, *glyph, prop_idx))
7207 return -1;
7208
7209 /* Is mouse on the highlighted item? */
7210 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
7211 && *vpos >= dpyinfo->mouse_face_beg_row
7212 && *vpos <= dpyinfo->mouse_face_end_row
7213 && (*vpos > dpyinfo->mouse_face_beg_row
7214 || *hpos >= dpyinfo->mouse_face_beg_col)
7215 && (*vpos < dpyinfo->mouse_face_end_row
7216 || *hpos < dpyinfo->mouse_face_end_col
7217 || dpyinfo->mouse_face_past_end))
7218 return 0;
7219
7220 return 1;
7221 }
7222
7223
7224 /* Handle mouse button event on the tool-bar of frame F, at
7225 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
7226 or ButtonRelase. */
7227
7228 static void
7229 x_handle_tool_bar_click (f, button_event)
7230 struct frame *f;
7231 XButtonEvent *button_event;
7232 {
7233 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7234 struct window *w = XWINDOW (f->tool_bar_window);
7235 int hpos, vpos, prop_idx;
7236 struct glyph *glyph;
7237 Lisp_Object enabled_p;
7238 int x = button_event->x;
7239 int y = button_event->y;
7240
7241 /* If not on the highlighted tool-bar item, return. */
7242 frame_to_window_pixel_xy (w, &x, &y);
7243 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
7244 return;
7245
7246 /* If item is disabled, do nothing. */
7247 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7248 if (NILP (enabled_p))
7249 return;
7250
7251 if (button_event->type == ButtonPress)
7252 {
7253 /* Show item in pressed state. */
7254 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7255 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
7256 last_tool_bar_item = prop_idx;
7257 }
7258 else
7259 {
7260 Lisp_Object key, frame;
7261 struct input_event event;
7262
7263 /* Show item in released state. */
7264 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7265 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7266
7267 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
7268
7269 XSETFRAME (frame, f);
7270 event.kind = TOOL_BAR_EVENT;
7271 event.frame_or_window = frame;
7272 event.arg = frame;
7273 kbd_buffer_store_event (&event);
7274
7275 event.kind = TOOL_BAR_EVENT;
7276 event.frame_or_window = frame;
7277 event.arg = key;
7278 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7279 button_event->state);
7280 kbd_buffer_store_event (&event);
7281 last_tool_bar_item = -1;
7282 }
7283 }
7284
7285
7286 /* Possibly highlight a tool-bar item on frame F when mouse moves to
7287 tool-bar window-relative coordinates X/Y. Called from
7288 note_mouse_highlight. */
7289
7290 static void
7291 note_tool_bar_highlight (f, x, y)
7292 struct frame *f;
7293 int x, y;
7294 {
7295 Lisp_Object window = f->tool_bar_window;
7296 struct window *w = XWINDOW (window);
7297 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7298 int hpos, vpos;
7299 struct glyph *glyph;
7300 struct glyph_row *row;
7301 int i;
7302 Lisp_Object enabled_p;
7303 int prop_idx;
7304 enum draw_glyphs_face draw;
7305 int mouse_down_p, rc;
7306
7307 /* Function note_mouse_highlight is called with negative x(y
7308 values when mouse moves outside of the frame. */
7309 if (x <= 0 || y <= 0)
7310 {
7311 clear_mouse_face (dpyinfo);
7312 return;
7313 }
7314
7315 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7316 if (rc < 0)
7317 {
7318 /* Not on tool-bar item. */
7319 clear_mouse_face (dpyinfo);
7320 return;
7321 }
7322 else if (rc == 0)
7323 goto set_help_echo;
7324
7325 clear_mouse_face (dpyinfo);
7326
7327 /* Mouse is down, but on different tool-bar item? */
7328 mouse_down_p = (dpyinfo->grabbed
7329 && f == last_mouse_frame
7330 && FRAME_LIVE_P (f));
7331 if (mouse_down_p
7332 && last_tool_bar_item != prop_idx)
7333 return;
7334
7335 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7336 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7337
7338 /* If tool-bar item is not enabled, don't highlight it. */
7339 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7340 if (!NILP (enabled_p))
7341 {
7342 /* Compute the x-position of the glyph. In front and past the
7343 image is a space. We include this is the highlighted area. */
7344 row = MATRIX_ROW (w->current_matrix, vpos);
7345 for (i = x = 0; i < hpos; ++i)
7346 x += row->glyphs[TEXT_AREA][i].pixel_width;
7347
7348 /* Record this as the current active region. */
7349 dpyinfo->mouse_face_beg_col = hpos;
7350 dpyinfo->mouse_face_beg_row = vpos;
7351 dpyinfo->mouse_face_beg_x = x;
7352 dpyinfo->mouse_face_beg_y = row->y;
7353 dpyinfo->mouse_face_past_end = 0;
7354
7355 dpyinfo->mouse_face_end_col = hpos + 1;
7356 dpyinfo->mouse_face_end_row = vpos;
7357 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7358 dpyinfo->mouse_face_end_y = row->y;
7359 dpyinfo->mouse_face_window = window;
7360 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7361
7362 /* Display it as active. */
7363 show_mouse_face (dpyinfo, draw);
7364 dpyinfo->mouse_face_image_state = draw;
7365 }
7366
7367 set_help_echo:
7368
7369 /* Set help_echo to a help string.to display for this tool-bar item.
7370 XTread_socket does the rest. */
7371 help_echo_object = help_echo_window = Qnil;
7372 help_echo_pos = -1;
7373 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
7374 if (NILP (help_echo))
7375 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
7376 }
7377
7378
7379 \f
7380 /* Find the glyph matrix position of buffer position POS in window W.
7381 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7382 current glyphs must be up to date. If POS is above window start
7383 return (0, 0, 0, 0). If POS is after end of W, return end of
7384 last line in W. */
7385
7386 static int
7387 fast_find_position (w, pos, hpos, vpos, x, y)
7388 struct window *w;
7389 int pos;
7390 int *hpos, *vpos, *x, *y;
7391 {
7392 int i;
7393 int lastcol;
7394 int maybe_next_line_p = 0;
7395 int line_start_position;
7396 int yb = window_text_bottom_y (w);
7397 struct glyph_row *row, *best_row;
7398 int row_vpos, best_row_vpos;
7399 int current_x;
7400
7401 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7402 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7403
7404 while (row->y < yb)
7405 {
7406 if (row->used[TEXT_AREA])
7407 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7408 else
7409 line_start_position = 0;
7410
7411 if (line_start_position > pos)
7412 break;
7413 /* If the position sought is the end of the buffer,
7414 don't include the blank lines at the bottom of the window. */
7415 else if (line_start_position == pos
7416 && pos == BUF_ZV (XBUFFER (w->buffer)))
7417 {
7418 maybe_next_line_p = 1;
7419 break;
7420 }
7421 else if (line_start_position > 0)
7422 {
7423 best_row = row;
7424 best_row_vpos = row_vpos;
7425 }
7426
7427 if (row->y + row->height >= yb)
7428 break;
7429
7430 ++row;
7431 ++row_vpos;
7432 }
7433
7434 /* Find the right column within BEST_ROW. */
7435 lastcol = 0;
7436 current_x = best_row->x;
7437 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
7438 {
7439 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7440 int charpos;
7441
7442 charpos = glyph->charpos;
7443 if (charpos == pos)
7444 {
7445 *hpos = i;
7446 *vpos = best_row_vpos;
7447 *x = current_x;
7448 *y = best_row->y;
7449 return 1;
7450 }
7451 else if (charpos > pos)
7452 break;
7453 else if (charpos > 0)
7454 lastcol = i;
7455
7456 current_x += glyph->pixel_width;
7457 }
7458
7459 /* If we're looking for the end of the buffer,
7460 and we didn't find it in the line we scanned,
7461 use the start of the following line. */
7462 if (maybe_next_line_p)
7463 {
7464 ++best_row;
7465 ++best_row_vpos;
7466 lastcol = 0;
7467 current_x = best_row->x;
7468 }
7469
7470 *vpos = best_row_vpos;
7471 *hpos = lastcol + 1;
7472 *x = current_x;
7473 *y = best_row->y;
7474 return 0;
7475 }
7476
7477
7478 /* Find the position of the the glyph for position POS in OBJECT in
7479 window W's current matrix, and return in *X/*Y the pixel
7480 coordinates, and return in *HPOS/*VPOS the column/row of the glyph.
7481
7482 RIGHT_P non-zero means return the position of the right edge of the
7483 glyph, RIGHT_P zero means return the left edge position.
7484
7485 If no glyph for POS exists in the matrix, return the position of
7486 the glyph with the next smaller position that is in the matrix, if
7487 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7488 exists in the matrix, return the position of the glyph with the
7489 next larger position in OBJECT.
7490
7491 Value is non-zero if a glyph was found. */
7492
7493 static int
7494 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7495 struct window *w;
7496 int pos;
7497 Lisp_Object object;
7498 int *hpos, *vpos, *x, *y;
7499 int right_p;
7500 {
7501 int yb = window_text_bottom_y (w);
7502 struct glyph_row *r;
7503 struct glyph *best_glyph = NULL;
7504 struct glyph_row *best_row = NULL;
7505 int best_x = 0;
7506
7507 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7508 r->enabled_p && r->y < yb;
7509 ++r)
7510 {
7511 struct glyph *g = r->glyphs[TEXT_AREA];
7512 struct glyph *e = g + r->used[TEXT_AREA];
7513 int gx;
7514
7515 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7516 if (EQ (g->object, object))
7517 {
7518 if (g->charpos == pos)
7519 {
7520 best_glyph = g;
7521 best_x = gx;
7522 best_row = r;
7523 goto found;
7524 }
7525 else if (best_glyph == NULL
7526 || ((abs (g->charpos - pos)
7527 < abs (best_glyph->charpos - pos))
7528 && (right_p
7529 ? g->charpos < pos
7530 : g->charpos > pos)))
7531 {
7532 best_glyph = g;
7533 best_x = gx;
7534 best_row = r;
7535 }
7536 }
7537 }
7538
7539 found:
7540
7541 if (best_glyph)
7542 {
7543 *x = best_x;
7544 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7545
7546 if (right_p)
7547 {
7548 *x += best_glyph->pixel_width;
7549 ++*hpos;
7550 }
7551
7552 *y = best_row->y;
7553 *vpos = best_row - w->current_matrix->rows;
7554 }
7555
7556 return best_glyph != NULL;
7557 }
7558
7559
7560 /* Display the active region described by mouse_face_*
7561 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7562
7563 static void
7564 show_mouse_face (dpyinfo, draw)
7565 struct x_display_info *dpyinfo;
7566 enum draw_glyphs_face draw;
7567 {
7568 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7569 struct frame *f = XFRAME (WINDOW_FRAME (w));
7570 int i;
7571 int cursor_off_p = 0;
7572 struct cursor_pos saved_cursor;
7573
7574 saved_cursor = output_cursor;
7575
7576 /* If window is in the process of being destroyed, don't bother
7577 to do anything. */
7578 if (w->current_matrix == NULL)
7579 goto set_x_cursor;
7580
7581 /* Recognize when we are called to operate on rows that don't exist
7582 anymore. This can happen when a window is split. */
7583 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7584 goto set_x_cursor;
7585
7586 set_output_cursor (&w->phys_cursor);
7587
7588 /* Note that mouse_face_beg_row etc. are window relative. */
7589 for (i = dpyinfo->mouse_face_beg_row;
7590 i <= dpyinfo->mouse_face_end_row;
7591 i++)
7592 {
7593 int start_hpos, end_hpos, start_x;
7594 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7595
7596 /* Don't do anything if row doesn't have valid contents. */
7597 if (!row->enabled_p)
7598 continue;
7599
7600 /* For all but the first row, the highlight starts at column 0. */
7601 if (i == dpyinfo->mouse_face_beg_row)
7602 {
7603 start_hpos = dpyinfo->mouse_face_beg_col;
7604 start_x = dpyinfo->mouse_face_beg_x;
7605 }
7606 else
7607 {
7608 start_hpos = 0;
7609 start_x = 0;
7610 }
7611
7612 if (i == dpyinfo->mouse_face_end_row)
7613 end_hpos = dpyinfo->mouse_face_end_col;
7614 else
7615 end_hpos = row->used[TEXT_AREA];
7616
7617 /* If the cursor's in the text we are about to rewrite, turn the
7618 cursor off. */
7619 if (!w->pseudo_window_p
7620 && i == output_cursor.vpos
7621 && output_cursor.hpos >= start_hpos - 1
7622 && output_cursor.hpos <= end_hpos)
7623 {
7624 x_update_window_cursor (w, 0);
7625 cursor_off_p = 1;
7626 }
7627
7628 if (end_hpos > start_hpos)
7629 {
7630 row->mouse_face_p = draw == DRAW_MOUSE_FACE || DRAW_IMAGE_RAISED;
7631 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7632 start_hpos, end_hpos, draw, NULL, NULL, 0);
7633 }
7634 }
7635
7636 /* If we turned the cursor off, turn it back on. */
7637 if (cursor_off_p)
7638 x_display_cursor (w, 1,
7639 output_cursor.hpos, output_cursor.vpos,
7640 output_cursor.x, output_cursor.y);
7641
7642 output_cursor = saved_cursor;
7643
7644 set_x_cursor:
7645
7646 /* Change the mouse cursor. */
7647 if (draw == DRAW_NORMAL_TEXT)
7648 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7649 f->output_data.x->text_cursor);
7650 else if (draw == DRAW_MOUSE_FACE)
7651 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7652 f->output_data.x->cross_cursor);
7653 else
7654 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7655 f->output_data.x->nontext_cursor);
7656 }
7657
7658 /* Clear out the mouse-highlighted active region.
7659 Redraw it un-highlighted first. Value is non-zero if mouse
7660 face was actually drawn unhighlighted. */
7661
7662 static int
7663 clear_mouse_face (dpyinfo)
7664 struct x_display_info *dpyinfo;
7665 {
7666 int cleared = 0;
7667
7668 if (!NILP (dpyinfo->mouse_face_window))
7669 {
7670 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7671 cleared = 1;
7672 }
7673
7674 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7675 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7676 dpyinfo->mouse_face_window = Qnil;
7677 return cleared;
7678 }
7679
7680
7681 /* Clear any mouse-face on window W. This function is part of the
7682 redisplay interface, and is called from try_window_id and similar
7683 functions to ensure the mouse-highlight is off. */
7684
7685 static void
7686 x_clear_mouse_face (w)
7687 struct window *w;
7688 {
7689 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7690 Lisp_Object window;
7691
7692 BLOCK_INPUT;
7693 XSETWINDOW (window, w);
7694 if (EQ (window, dpyinfo->mouse_face_window))
7695 clear_mouse_face (dpyinfo);
7696 UNBLOCK_INPUT;
7697 }
7698
7699
7700 /* Just discard the mouse face information for frame F, if any.
7701 This is used when the size of F is changed. */
7702
7703 void
7704 cancel_mouse_face (f)
7705 FRAME_PTR f;
7706 {
7707 Lisp_Object window;
7708 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7709
7710 window = dpyinfo->mouse_face_window;
7711 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7712 {
7713 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7714 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7715 dpyinfo->mouse_face_window = Qnil;
7716 }
7717 }
7718
7719 \f
7720 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
7721
7722
7723 /* Try to determine frame pixel position and size of the glyph under
7724 frame pixel coordinates X/Y on frame F . Return the position and
7725 size in *RECT. Value is non-zero if we could compute these
7726 values. */
7727
7728 static int
7729 glyph_rect (f, x, y, rect)
7730 struct frame *f;
7731 int x, y;
7732 XRectangle *rect;
7733 {
7734 Lisp_Object window;
7735 int part, found = 0;
7736
7737 window = window_from_coordinates (f, x, y, &part, 0);
7738 if (!NILP (window))
7739 {
7740 struct window *w = XWINDOW (window);
7741 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7742 struct glyph_row *end = r + w->current_matrix->nrows - 1;
7743 int area;
7744
7745 frame_to_window_pixel_xy (w, &x, &y);
7746
7747 for (; !found && r < end && r->enabled_p; ++r)
7748 if (r->y >= y)
7749 {
7750 struct glyph *g = r->glyphs[TEXT_AREA];
7751 struct glyph *end = g + r->used[TEXT_AREA];
7752 int gx;
7753
7754 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
7755 if (gx >= x)
7756 {
7757 rect->width = g->pixel_width;
7758 rect->height = r->height;
7759 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
7760 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
7761 found = 1;
7762 }
7763 }
7764 }
7765
7766 return found;
7767 }
7768
7769
7770 /* Return the current position of the mouse.
7771 *FP should be a frame which indicates which display to ask about.
7772
7773 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
7774 and *PART to the frame, window, and scroll bar part that the mouse
7775 is over. Set *X and *Y to the portion and whole of the mouse's
7776 position on the scroll bar.
7777
7778 If the mouse movement started elsewhere, set *FP to the frame the
7779 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
7780 the mouse is over.
7781
7782 Set *TIME to the server time-stamp for the time at which the mouse
7783 was at this position.
7784
7785 Don't store anything if we don't have a valid set of values to report.
7786
7787 This clears the mouse_moved flag, so we can wait for the next mouse
7788 movement. */
7789
7790 static void
7791 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7792 FRAME_PTR *fp;
7793 int insist;
7794 Lisp_Object *bar_window;
7795 enum scroll_bar_part *part;
7796 Lisp_Object *x, *y;
7797 unsigned long *time;
7798 {
7799 FRAME_PTR f1;
7800
7801 BLOCK_INPUT;
7802
7803 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7804 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7805 else
7806 {
7807 Window root;
7808 int root_x, root_y;
7809
7810 Window dummy_window;
7811 int dummy;
7812
7813 Lisp_Object frame, tail;
7814
7815 /* Clear the mouse-moved flag for every frame on this display. */
7816 FOR_EACH_FRAME (tail, frame)
7817 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7818 XFRAME (frame)->mouse_moved = 0;
7819
7820 last_mouse_scroll_bar = Qnil;
7821
7822 /* Figure out which root window we're on. */
7823 XQueryPointer (FRAME_X_DISPLAY (*fp),
7824 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
7825
7826 /* The root window which contains the pointer. */
7827 &root,
7828
7829 /* Trash which we can't trust if the pointer is on
7830 a different screen. */
7831 &dummy_window,
7832
7833 /* The position on that root window. */
7834 &root_x, &root_y,
7835
7836 /* More trash we can't trust. */
7837 &dummy, &dummy,
7838
7839 /* Modifier keys and pointer buttons, about which
7840 we don't care. */
7841 (unsigned int *) &dummy);
7842
7843 /* Now we have a position on the root; find the innermost window
7844 containing the pointer. */
7845 {
7846 Window win, child;
7847 int win_x, win_y;
7848 int parent_x = 0, parent_y = 0;
7849 int count;
7850
7851 win = root;
7852
7853 /* XTranslateCoordinates can get errors if the window
7854 structure is changing at the same time this function
7855 is running. So at least we must not crash from them. */
7856
7857 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
7858
7859 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
7860 && FRAME_LIVE_P (last_mouse_frame))
7861 {
7862 /* If mouse was grabbed on a frame, give coords for that frame
7863 even if the mouse is now outside it. */
7864 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7865
7866 /* From-window, to-window. */
7867 root, FRAME_X_WINDOW (last_mouse_frame),
7868
7869 /* From-position, to-position. */
7870 root_x, root_y, &win_x, &win_y,
7871
7872 /* Child of win. */
7873 &child);
7874 f1 = last_mouse_frame;
7875 }
7876 else
7877 {
7878 while (1)
7879 {
7880 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7881
7882 /* From-window, to-window. */
7883 root, win,
7884
7885 /* From-position, to-position. */
7886 root_x, root_y, &win_x, &win_y,
7887
7888 /* Child of win. */
7889 &child);
7890
7891 if (child == None || child == win)
7892 break;
7893
7894 win = child;
7895 parent_x = win_x;
7896 parent_y = win_y;
7897 }
7898
7899 /* Now we know that:
7900 win is the innermost window containing the pointer
7901 (XTC says it has no child containing the pointer),
7902 win_x and win_y are the pointer's position in it
7903 (XTC did this the last time through), and
7904 parent_x and parent_y are the pointer's position in win's parent.
7905 (They are what win_x and win_y were when win was child.
7906 If win is the root window, it has no parent, and
7907 parent_{x,y} are invalid, but that's okay, because we'll
7908 never use them in that case.) */
7909
7910 /* Is win one of our frames? */
7911 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
7912
7913 #ifdef USE_X_TOOLKIT
7914 /* If we end up with the menu bar window, say it's not
7915 on the frame. */
7916 if (f1 != NULL
7917 && f1->output_data.x->menubar_widget
7918 && win == XtWindow (f1->output_data.x->menubar_widget))
7919 f1 = NULL;
7920 #endif /* USE_X_TOOLKIT */
7921 }
7922
7923 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7924 f1 = 0;
7925
7926 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
7927
7928 /* If not, is it one of our scroll bars? */
7929 if (! f1)
7930 {
7931 struct scroll_bar *bar = x_window_to_scroll_bar (win);
7932
7933 if (bar)
7934 {
7935 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7936 win_x = parent_x;
7937 win_y = parent_y;
7938 }
7939 }
7940
7941 if (f1 == 0 && insist > 0)
7942 f1 = SELECTED_FRAME ();
7943
7944 if (f1)
7945 {
7946 /* Ok, we found a frame. Store all the values.
7947 last_mouse_glyph is a rectangle used to reduce the
7948 generation of mouse events. To not miss any motion
7949 events, we must divide the frame into rectangles of the
7950 size of the smallest character that could be displayed
7951 on it, i.e. into the same rectangles that matrices on
7952 the frame are divided into. */
7953
7954 int width, height, gx, gy;
7955 XRectangle rect;
7956
7957 if (glyph_rect (f1, win_x, win_y, &rect))
7958 last_mouse_glyph = rect;
7959 else
7960 {
7961 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7962 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7963 gx = win_x;
7964 gy = win_y;
7965
7966 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7967 round down even for negative values. */
7968 if (gx < 0)
7969 gx -= width - 1;
7970 if (gy < 0)
7971 gy -= height - 1;
7972 gx = (gx + width - 1) / width * width;
7973 gy = (gy + height - 1) / height * height;
7974
7975 last_mouse_glyph.width = width;
7976 last_mouse_glyph.height = height;
7977 last_mouse_glyph.x = gx;
7978 last_mouse_glyph.y = gy;
7979 }
7980
7981 *bar_window = Qnil;
7982 *part = 0;
7983 *fp = f1;
7984 XSETINT (*x, win_x);
7985 XSETINT (*y, win_y);
7986 *time = last_mouse_movement_time;
7987 }
7988 }
7989 }
7990
7991 UNBLOCK_INPUT;
7992 }
7993
7994
7995 #ifdef USE_X_TOOLKIT
7996
7997 /* Atimer callback function for TIMER. Called every 0.1s to process
7998 Xt timeouts, if needed. We must avoid calling XtAppPending as
7999 much as possible because that function does an implicit XFlush
8000 that slows us down. */
8001
8002 static void
8003 x_process_timeouts (timer)
8004 struct atimer *timer;
8005 {
8006 if (toolkit_scroll_bar_interaction || popup_activated_flag)
8007 {
8008 BLOCK_INPUT;
8009 while (XtAppPending (Xt_app_con) & XtIMTimer)
8010 XtAppProcessEvent (Xt_app_con, XtIMTimer);
8011 UNBLOCK_INPUT;
8012 }
8013 }
8014
8015 #endif /* USE_X_TOOLKIT */
8016
8017 \f
8018 /* Scroll bar support. */
8019
8020 /* Given an X window ID, find the struct scroll_bar which manages it.
8021 This can be called in GC, so we have to make sure to strip off mark
8022 bits. */
8023
8024 static struct scroll_bar *
8025 x_window_to_scroll_bar (window_id)
8026 Window window_id;
8027 {
8028 Lisp_Object tail;
8029
8030 for (tail = Vframe_list;
8031 XGCTYPE (tail) == Lisp_Cons;
8032 tail = XCDR (tail))
8033 {
8034 Lisp_Object frame, bar, condemned;
8035
8036 frame = XCAR (tail);
8037 /* All elements of Vframe_list should be frames. */
8038 if (! GC_FRAMEP (frame))
8039 abort ();
8040
8041 /* Scan this frame's scroll bar list for a scroll bar with the
8042 right window ID. */
8043 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
8044 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
8045 /* This trick allows us to search both the ordinary and
8046 condemned scroll bar lists with one loop. */
8047 ! GC_NILP (bar) || (bar = condemned,
8048 condemned = Qnil,
8049 ! GC_NILP (bar));
8050 bar = XSCROLL_BAR (bar)->next)
8051 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
8052 return XSCROLL_BAR (bar);
8053 }
8054
8055 return 0;
8056 }
8057
8058
8059 #if defined USE_X_TOOLKIT && defined USE_LUCID
8060
8061 /* Return the Lucid menu bar WINDOW is part of. Return null
8062 if WINDOW is not part of a menu bar. */
8063
8064 static Widget
8065 x_window_to_menu_bar (window)
8066 Window window;
8067 {
8068 Lisp_Object tail;
8069
8070 for (tail = Vframe_list;
8071 XGCTYPE (tail) == Lisp_Cons;
8072 tail = XCDR (tail))
8073 {
8074 Lisp_Object frame = XCAR (tail);
8075 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
8076
8077 if (menu_bar && xlwmenu_window_p (menu_bar, window))
8078 return menu_bar;
8079 }
8080
8081 return NULL;
8082 }
8083
8084 #endif /* USE_X_TOOLKIT && USE_LUCID */
8085
8086 \f
8087 /************************************************************************
8088 Toolkit scroll bars
8089 ************************************************************************/
8090
8091 #ifdef USE_TOOLKIT_SCROLL_BARS
8092
8093 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
8094 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
8095 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
8096 struct scroll_bar *));
8097 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
8098 int, int, int));
8099
8100
8101 /* Id of action hook installed for scroll bars. */
8102
8103 static XtActionHookId action_hook_id;
8104
8105 /* Lisp window being scrolled. Set when starting to interact with
8106 a toolkit scroll bar, reset to nil when ending the interaction. */
8107
8108 static Lisp_Object window_being_scrolled;
8109
8110 /* Last scroll bar part sent in xm_scroll_callback. */
8111
8112 static int last_scroll_bar_part;
8113
8114 /* Whether this is an Xaw with arrow-scrollbars. This should imply
8115 that movements of 1/20 of the screen size are mapped to up/down. */
8116
8117 static Boolean xaw3d_arrow_scroll;
8118
8119 /* Whether the drag scrolling maintains the mouse at the top of the
8120 thumb. If not, resizing the thumb needs to be done more carefully
8121 to avoid jerkyness. */
8122
8123 static Boolean xaw3d_pick_top;
8124
8125
8126 /* Action hook installed via XtAppAddActionHook when toolkit scroll
8127 bars are used.. The hook is responsible for detecting when
8128 the user ends an interaction with the scroll bar, and generates
8129 a `end-scroll' scroll_bar_click' event if so. */
8130
8131 static void
8132 xt_action_hook (widget, client_data, action_name, event, params,
8133 num_params)
8134 Widget widget;
8135 XtPointer client_data;
8136 String action_name;
8137 XEvent *event;
8138 String *params;
8139 Cardinal *num_params;
8140 {
8141 int scroll_bar_p;
8142 char *end_action;
8143
8144 #ifdef USE_MOTIF
8145 scroll_bar_p = XmIsScrollBar (widget);
8146 end_action = "Release";
8147 #else /* !USE_MOTIF i.e. use Xaw */
8148 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
8149 end_action = "EndScroll";
8150 #endif /* USE_MOTIF */
8151
8152 if (scroll_bar_p
8153 && strcmp (action_name, end_action) == 0
8154 && WINDOWP (window_being_scrolled))
8155 {
8156 struct window *w;
8157
8158 x_send_scroll_bar_event (window_being_scrolled,
8159 scroll_bar_end_scroll, 0, 0);
8160 w = XWINDOW (window_being_scrolled);
8161 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
8162 window_being_scrolled = Qnil;
8163 last_scroll_bar_part = -1;
8164
8165 /* Xt timeouts no longer needed. */
8166 toolkit_scroll_bar_interaction = 0;
8167 }
8168 }
8169
8170 /* A vector of windows used for communication between
8171 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
8172
8173 static struct window **scroll_bar_windows;
8174 static int scroll_bar_windows_size;
8175
8176
8177 /* Send a client message with message type Xatom_Scrollbar for a
8178 scroll action to the frame of WINDOW. PART is a value identifying
8179 the part of the scroll bar that was clicked on. PORTION is the
8180 amount to scroll of a whole of WHOLE. */
8181
8182 static void
8183 x_send_scroll_bar_event (window, part, portion, whole)
8184 Lisp_Object window;
8185 int part, portion, whole;
8186 {
8187 XEvent event;
8188 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
8189 struct window *w = XWINDOW (window);
8190 struct frame *f = XFRAME (w->frame);
8191 int i;
8192
8193 BLOCK_INPUT;
8194
8195 /* Construct a ClientMessage event to send to the frame. */
8196 ev->type = ClientMessage;
8197 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
8198 ev->display = FRAME_X_DISPLAY (f);
8199 ev->window = FRAME_X_WINDOW (f);
8200 ev->format = 32;
8201
8202 /* We can only transfer 32 bits in the XClientMessageEvent, which is
8203 not enough to store a pointer or Lisp_Object on a 64 bit system.
8204 So, store the window in scroll_bar_windows and pass the index
8205 into that array in the event. */
8206 for (i = 0; i < scroll_bar_windows_size; ++i)
8207 if (scroll_bar_windows[i] == NULL)
8208 break;
8209
8210 if (i == scroll_bar_windows_size)
8211 {
8212 int new_size = max (10, 2 * scroll_bar_windows_size);
8213 size_t nbytes = new_size * sizeof *scroll_bar_windows;
8214 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
8215
8216 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
8217 nbytes);
8218 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
8219 scroll_bar_windows_size = new_size;
8220 }
8221
8222 scroll_bar_windows[i] = w;
8223 ev->data.l[0] = (long) i;
8224 ev->data.l[1] = (long) part;
8225 ev->data.l[2] = (long) 0;
8226 ev->data.l[3] = (long) portion;
8227 ev->data.l[4] = (long) whole;
8228
8229 /* Make Xt timeouts work while the scroll bar is active. */
8230 toolkit_scroll_bar_interaction = 1;
8231
8232 /* Setting the event mask to zero means that the message will
8233 be sent to the client that created the window, and if that
8234 window no longer exists, no event will be sent. */
8235 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
8236 UNBLOCK_INPUT;
8237 }
8238
8239
8240 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
8241 in *IEVENT. */
8242
8243 static void
8244 x_scroll_bar_to_input_event (event, ievent)
8245 XEvent *event;
8246 struct input_event *ievent;
8247 {
8248 XClientMessageEvent *ev = (XClientMessageEvent *) event;
8249 Lisp_Object window;
8250 struct frame *f;
8251 struct window *w;
8252
8253 w = scroll_bar_windows[ev->data.l[0]];
8254 scroll_bar_windows[ev->data.l[0]] = NULL;
8255
8256 XSETWINDOW (window, w);
8257 f = XFRAME (w->frame);
8258
8259 ievent->kind = scroll_bar_click;
8260 ievent->frame_or_window = window;
8261 ievent->arg = Qnil;
8262 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
8263 ievent->part = ev->data.l[1];
8264 ievent->code = ev->data.l[2];
8265 ievent->x = make_number ((int) ev->data.l[3]);
8266 ievent->y = make_number ((int) ev->data.l[4]);
8267 ievent->modifiers = 0;
8268 }
8269
8270
8271 #ifdef USE_MOTIF
8272
8273 /* Minimum and maximum values used for Motif scroll bars. */
8274
8275 #define XM_SB_MIN 1
8276 #define XM_SB_MAX 10000000
8277 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
8278
8279
8280 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
8281 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
8282 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
8283
8284 static void
8285 xm_scroll_callback (widget, client_data, call_data)
8286 Widget widget;
8287 XtPointer client_data, call_data;
8288 {
8289 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8290 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
8291 double percent;
8292 int part = -1, whole = 0, portion = 0;
8293
8294 switch (cs->reason)
8295 {
8296 case XmCR_DECREMENT:
8297 bar->dragging = Qnil;
8298 part = scroll_bar_up_arrow;
8299 break;
8300
8301 case XmCR_INCREMENT:
8302 bar->dragging = Qnil;
8303 part = scroll_bar_down_arrow;
8304 break;
8305
8306 case XmCR_PAGE_DECREMENT:
8307 bar->dragging = Qnil;
8308 part = scroll_bar_above_handle;
8309 break;
8310
8311 case XmCR_PAGE_INCREMENT:
8312 bar->dragging = Qnil;
8313 part = scroll_bar_below_handle;
8314 break;
8315
8316 case XmCR_TO_TOP:
8317 bar->dragging = Qnil;
8318 part = scroll_bar_to_top;
8319 break;
8320
8321 case XmCR_TO_BOTTOM:
8322 bar->dragging = Qnil;
8323 part = scroll_bar_to_bottom;
8324 break;
8325
8326 case XmCR_DRAG:
8327 {
8328 int slider_size;
8329 int dragging_down_p = (INTEGERP (bar->dragging)
8330 && XINT (bar->dragging) <= cs->value);
8331
8332 /* Get the slider size. */
8333 BLOCK_INPUT;
8334 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
8335 UNBLOCK_INPUT;
8336
8337 /* At the max position of the scroll bar, do a line-wise
8338 movement. Without doing anything, we would be called with
8339 the same cs->value again and again. If we want to make
8340 sure that we can reach the end of the buffer, we have to do
8341 something.
8342
8343 Implementation note: setting bar->dragging always to
8344 cs->value gives a smoother movement at the max position.
8345 Setting it to nil when doing line-wise movement gives
8346 a better slider behavior. */
8347
8348 if (cs->value + slider_size == XM_SB_MAX
8349 || (dragging_down_p
8350 && last_scroll_bar_part == scroll_bar_down_arrow))
8351 {
8352 part = scroll_bar_down_arrow;
8353 bar->dragging = Qnil;
8354 }
8355 else
8356 {
8357 whole = XM_SB_RANGE;
8358 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
8359 part = scroll_bar_handle;
8360 bar->dragging = make_number (cs->value);
8361 }
8362 }
8363 break;
8364
8365 case XmCR_VALUE_CHANGED:
8366 break;
8367 };
8368
8369 if (part >= 0)
8370 {
8371 window_being_scrolled = bar->window;
8372 last_scroll_bar_part = part;
8373 x_send_scroll_bar_event (bar->window, part, portion, whole);
8374 }
8375 }
8376
8377
8378 #else /* !USE_MOTIF, i.e. Xaw. */
8379
8380
8381 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
8382 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8383 scroll bar struct. CALL_DATA is a pointer to a float saying where
8384 the thumb is. */
8385
8386 static void
8387 xaw_jump_callback (widget, client_data, call_data)
8388 Widget widget;
8389 XtPointer client_data, call_data;
8390 {
8391 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8392 float top = *(float *) call_data;
8393 float shown;
8394 int whole, portion, height;
8395 int part;
8396
8397 /* Get the size of the thumb, a value between 0 and 1. */
8398 BLOCK_INPUT;
8399 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
8400 UNBLOCK_INPUT;
8401
8402 whole = 10000000;
8403 portion = shown < 1 ? top * whole : 0;
8404
8405 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8406 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8407 the bottom, so we force the scrolling whenever we see that we're
8408 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8409 we try to ensure that we always stay two pixels away from the
8410 bottom). */
8411 part = scroll_bar_down_arrow;
8412 else
8413 part = scroll_bar_handle;
8414
8415 window_being_scrolled = bar->window;
8416 bar->dragging = make_number (portion);
8417 last_scroll_bar_part = part;
8418 x_send_scroll_bar_event (bar->window, part, portion, whole);
8419 }
8420
8421
8422 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
8423 i.e. line or page up or down. WIDGET is the Xaw scroll bar
8424 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8425 the scroll bar. CALL_DATA is an integer specifying the action that
8426 has taken place. It's magnitude is in the range 0..height of the
8427 scroll bar. Negative values mean scroll towards buffer start.
8428 Values < height of scroll bar mean line-wise movement. */
8429
8430 static void
8431 xaw_scroll_callback (widget, client_data, call_data)
8432 Widget widget;
8433 XtPointer client_data, call_data;
8434 {
8435 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8436 int position = (int) call_data;
8437 Dimension height;
8438 int part;
8439
8440 /* Get the height of the scroll bar. */
8441 BLOCK_INPUT;
8442 XtVaGetValues (widget, XtNheight, &height, NULL);
8443 UNBLOCK_INPUT;
8444
8445 if (abs (position) >= height)
8446 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8447
8448 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8449 it maps line-movement to call_data = max(5, height/20). */
8450 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8451 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
8452 else
8453 part = scroll_bar_move_ratio;
8454
8455 window_being_scrolled = bar->window;
8456 bar->dragging = Qnil;
8457 last_scroll_bar_part = part;
8458 x_send_scroll_bar_event (bar->window, part, position, height);
8459 }
8460
8461
8462 #endif /* not USE_MOTIF */
8463
8464
8465 /* Create the widget for scroll bar BAR on frame F. Record the widget
8466 and X window of the scroll bar in BAR. */
8467
8468 static void
8469 x_create_toolkit_scroll_bar (f, bar)
8470 struct frame *f;
8471 struct scroll_bar *bar;
8472 {
8473 Window xwindow;
8474 Widget widget;
8475 Arg av[20];
8476 int ac = 0;
8477 char *scroll_bar_name = "verticalScrollBar";
8478 unsigned long pixel;
8479
8480 BLOCK_INPUT;
8481
8482 #ifdef USE_MOTIF
8483 /* Set resources. Create the widget. */
8484 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8485 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8486 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8487 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8488 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8489 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8490 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8491
8492 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8493 if (pixel != -1)
8494 {
8495 XtSetArg (av[ac], XmNforeground, pixel);
8496 ++ac;
8497 }
8498
8499 pixel = f->output_data.x->scroll_bar_background_pixel;
8500 if (pixel != -1)
8501 {
8502 XtSetArg (av[ac], XmNbackground, pixel);
8503 ++ac;
8504 }
8505
8506 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8507 scroll_bar_name, av, ac);
8508
8509 /* Add one callback for everything that can happen. */
8510 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8511 (XtPointer) bar);
8512 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8513 (XtPointer) bar);
8514 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8515 (XtPointer) bar);
8516 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8517 (XtPointer) bar);
8518 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8519 (XtPointer) bar);
8520 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8521 (XtPointer) bar);
8522 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8523 (XtPointer) bar);
8524
8525 /* Realize the widget. Only after that is the X window created. */
8526 XtRealizeWidget (widget);
8527
8528 /* Set the cursor to an arrow. I didn't find a resource to do that.
8529 And I'm wondering why it hasn't an arrow cursor by default. */
8530 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8531 f->output_data.x->nontext_cursor);
8532
8533 #else /* !USE_MOTIF i.e. use Xaw */
8534
8535 /* Set resources. Create the widget. The background of the
8536 Xaw3d scroll bar widget is a little bit light for my taste.
8537 We don't alter it here to let users change it according
8538 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8539 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8540 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
8541 /* For smoother scrolling with Xaw3d -sm */
8542 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8543 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
8544
8545 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8546 if (pixel != -1)
8547 {
8548 XtSetArg (av[ac], XtNforeground, pixel);
8549 ++ac;
8550 }
8551
8552 pixel = f->output_data.x->scroll_bar_background_pixel;
8553 if (pixel != -1)
8554 {
8555 XtSetArg (av[ac], XtNbackground, pixel);
8556 ++ac;
8557 }
8558
8559 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8560 f->output_data.x->edit_widget, av, ac);
8561
8562 {
8563 char *initial = "";
8564 char *val = initial;
8565 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8566 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8567 if (val == initial)
8568 { /* ARROW_SCROLL */
8569 xaw3d_arrow_scroll = True;
8570 /* Isn't that just a personal preference ? -sm */
8571 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8572 }
8573 }
8574
8575 /* Define callbacks. */
8576 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8577 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
8578 (XtPointer) bar);
8579
8580 /* Realize the widget. Only after that is the X window created. */
8581 XtRealizeWidget (widget);
8582
8583 #endif /* !USE_MOTIF */
8584
8585 /* Install an action hook that let's us detect when the user
8586 finishes interacting with a scroll bar. */
8587 if (action_hook_id == 0)
8588 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8589
8590 /* Remember X window and widget in the scroll bar vector. */
8591 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8592 xwindow = XtWindow (widget);
8593 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8594
8595 UNBLOCK_INPUT;
8596 }
8597
8598
8599 /* Set the thumb size and position of scroll bar BAR. We are currently
8600 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8601
8602 static void
8603 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8604 struct scroll_bar *bar;
8605 int portion, position, whole;
8606 {
8607 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8608 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8609 float top, shown;
8610
8611 if (whole == 0)
8612 top = 0, shown = 1;
8613 else
8614 {
8615 top = (float) position / whole;
8616 shown = (float) portion / whole;
8617 }
8618
8619 BLOCK_INPUT;
8620
8621 #ifdef USE_MOTIF
8622 {
8623 int size, value;
8624 XmScrollBarWidget sb;
8625
8626 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
8627 is the scroll bar's maximum and MIN is the scroll bar's minimum
8628 value. */
8629 size = shown * XM_SB_RANGE;
8630 size = min (size, XM_SB_RANGE);
8631 size = max (size, 1);
8632
8633 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8634 value = top * XM_SB_RANGE;
8635 value = min (value, XM_SB_MAX - size);
8636 value = max (value, XM_SB_MIN);
8637
8638 if (NILP (bar->dragging))
8639 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8640 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8641 /* This has the negative side effect that the slider value is
8642 not what it would be if we scrolled here using line-wise or
8643 page-wise movement. */
8644 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8645 else
8646 {
8647 /* If currently dragging, only update the slider size.
8648 This reduces flicker effects. */
8649 int old_value, old_size, increment, page_increment;
8650
8651 XmScrollBarGetValues (widget, &old_value, &old_size,
8652 &increment, &page_increment);
8653 XmScrollBarSetValues (widget, old_value,
8654 min (size, XM_SB_RANGE - old_value),
8655 0, 0, False);
8656 }
8657 }
8658 #else /* !USE_MOTIF i.e. use Xaw */
8659 {
8660 float old_top, old_shown;
8661 Dimension height;
8662 XtVaGetValues (widget,
8663 XtNtopOfThumb, &old_top,
8664 XtNshown, &old_shown,
8665 XtNheight, &height,
8666 NULL);
8667
8668 /* Massage the top+shown values. */
8669 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8670 top = max (0, min (1, top));
8671 else
8672 top = old_top;
8673 /* Keep two pixels available for moving the thumb down. */
8674 shown = max (0, min (1 - top - (2.0 / height), shown));
8675
8676 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8677 check that your system's configuration file contains a define
8678 for `NARROWPROTO'. See s/freebsd.h for an example. */
8679 if (top != old_top || shown != old_shown)
8680 {
8681 if (NILP (bar->dragging))
8682 XawScrollbarSetThumb (widget, top, shown);
8683 else
8684 {
8685 #ifdef HAVE_XAW3D
8686 ScrollbarWidget sb = (ScrollbarWidget) widget;
8687 int scroll_mode = 0;
8688
8689 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8690 if (xaw3d_arrow_scroll)
8691 {
8692 /* Xaw3d stupidly ignores resize requests while dragging
8693 so we have to make it believe it's not in dragging mode. */
8694 scroll_mode = sb->scrollbar.scroll_mode;
8695 if (scroll_mode == 2)
8696 sb->scrollbar.scroll_mode = 0;
8697 }
8698 #endif
8699 /* Try to make the scrolling a tad smoother. */
8700 if (!xaw3d_pick_top)
8701 shown = min (shown, old_shown);
8702
8703 XawScrollbarSetThumb (widget, top, shown);
8704
8705 #ifdef HAVE_XAW3D
8706 if (xaw3d_arrow_scroll && scroll_mode == 2)
8707 sb->scrollbar.scroll_mode = scroll_mode;
8708 #endif
8709 }
8710 }
8711 }
8712 #endif /* !USE_MOTIF */
8713
8714 UNBLOCK_INPUT;
8715 }
8716
8717 #endif /* USE_TOOLKIT_SCROLL_BARS */
8718
8719
8720 \f
8721 /************************************************************************
8722 Scroll bars, general
8723 ************************************************************************/
8724
8725 /* Create a scroll bar and return the scroll bar vector for it. W is
8726 the Emacs window on which to create the scroll bar. TOP, LEFT,
8727 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8728 scroll bar. */
8729
8730 static struct scroll_bar *
8731 x_scroll_bar_create (w, top, left, width, height)
8732 struct window *w;
8733 int top, left, width, height;
8734 {
8735 struct frame *f = XFRAME (w->frame);
8736 struct scroll_bar *bar
8737 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8738
8739 BLOCK_INPUT;
8740
8741 #ifdef USE_TOOLKIT_SCROLL_BARS
8742 x_create_toolkit_scroll_bar (f, bar);
8743 #else /* not USE_TOOLKIT_SCROLL_BARS */
8744 {
8745 XSetWindowAttributes a;
8746 unsigned long mask;
8747 Window window;
8748
8749 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8750 if (a.background_pixel == -1)
8751 a.background_pixel = f->output_data.x->background_pixel;
8752
8753 a.event_mask = (ButtonPressMask | ButtonReleaseMask
8754 | ButtonMotionMask | PointerMotionHintMask
8755 | ExposureMask);
8756 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
8757
8758 mask = (CWBackPixel | CWEventMask | CWCursor);
8759
8760 /* Clear the area of W that will serve as a scroll bar. This is
8761 for the case that a window has been split horizontally. In
8762 this case, no clear_frame is generated to reduce flickering. */
8763 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8764 left, top, width,
8765 window_box_height (w), False);
8766
8767 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8768 /* Position and size of scroll bar. */
8769 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8770 top,
8771 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8772 height,
8773 /* Border width, depth, class, and visual. */
8774 0,
8775 CopyFromParent,
8776 CopyFromParent,
8777 CopyFromParent,
8778 /* Attributes. */
8779 mask, &a);
8780 SET_SCROLL_BAR_X_WINDOW (bar, window);
8781 }
8782 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8783
8784 XSETWINDOW (bar->window, w);
8785 XSETINT (bar->top, top);
8786 XSETINT (bar->left, left);
8787 XSETINT (bar->width, width);
8788 XSETINT (bar->height, height);
8789 XSETINT (bar->start, 0);
8790 XSETINT (bar->end, 0);
8791 bar->dragging = Qnil;
8792
8793 /* Add bar to its frame's list of scroll bars. */
8794 bar->next = FRAME_SCROLL_BARS (f);
8795 bar->prev = Qnil;
8796 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8797 if (!NILP (bar->next))
8798 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8799
8800 /* Map the window/widget. */
8801 #ifdef USE_TOOLKIT_SCROLL_BARS
8802 {
8803 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8804 XtConfigureWidget (scroll_bar,
8805 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8806 top,
8807 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8808 max (height, 1), 0);
8809 XtMapWidget (scroll_bar);
8810 }
8811 #else /* not USE_TOOLKIT_SCROLL_BARS */
8812 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8813 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8814
8815 UNBLOCK_INPUT;
8816 return bar;
8817 }
8818
8819
8820 /* Draw BAR's handle in the proper position.
8821
8822 If the handle is already drawn from START to END, don't bother
8823 redrawing it, unless REBUILD is non-zero; in that case, always
8824 redraw it. (REBUILD is handy for drawing the handle after expose
8825 events.)
8826
8827 Normally, we want to constrain the start and end of the handle to
8828 fit inside its rectangle, but if the user is dragging the scroll
8829 bar handle, we want to let them drag it down all the way, so that
8830 the bar's top is as far down as it goes; otherwise, there's no way
8831 to move to the very end of the buffer. */
8832
8833 #ifndef USE_TOOLKIT_SCROLL_BARS
8834
8835 static void
8836 x_scroll_bar_set_handle (bar, start, end, rebuild)
8837 struct scroll_bar *bar;
8838 int start, end;
8839 int rebuild;
8840 {
8841 int dragging = ! NILP (bar->dragging);
8842 Window w = SCROLL_BAR_X_WINDOW (bar);
8843 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8844 GC gc = f->output_data.x->normal_gc;
8845
8846 /* If the display is already accurate, do nothing. */
8847 if (! rebuild
8848 && start == XINT (bar->start)
8849 && end == XINT (bar->end))
8850 return;
8851
8852 BLOCK_INPUT;
8853
8854 {
8855 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8856 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8857 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8858
8859 /* Make sure the values are reasonable, and try to preserve
8860 the distance between start and end. */
8861 {
8862 int length = end - start;
8863
8864 if (start < 0)
8865 start = 0;
8866 else if (start > top_range)
8867 start = top_range;
8868 end = start + length;
8869
8870 if (end < start)
8871 end = start;
8872 else if (end > top_range && ! dragging)
8873 end = top_range;
8874 }
8875
8876 /* Store the adjusted setting in the scroll bar. */
8877 XSETINT (bar->start, start);
8878 XSETINT (bar->end, end);
8879
8880 /* Clip the end position, just for display. */
8881 if (end > top_range)
8882 end = top_range;
8883
8884 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
8885 below top positions, to make sure the handle is always at least
8886 that many pixels tall. */
8887 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
8888
8889 /* Draw the empty space above the handle. Note that we can't clear
8890 zero-height areas; that means "clear to end of window." */
8891 if (0 < start)
8892 x_clear_area (FRAME_X_DISPLAY (f), w,
8893 /* x, y, width, height, and exposures. */
8894 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8895 VERTICAL_SCROLL_BAR_TOP_BORDER,
8896 inside_width, start,
8897 False);
8898
8899 /* Change to proper foreground color if one is specified. */
8900 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8901 XSetForeground (FRAME_X_DISPLAY (f), gc,
8902 f->output_data.x->scroll_bar_foreground_pixel);
8903
8904 /* Draw the handle itself. */
8905 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
8906 /* x, y, width, height */
8907 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8908 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
8909 inside_width, end - start);
8910
8911 /* Restore the foreground color of the GC if we changed it above. */
8912 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8913 XSetForeground (FRAME_X_DISPLAY (f), gc,
8914 f->output_data.x->foreground_pixel);
8915
8916 /* Draw the empty space below the handle. Note that we can't
8917 clear zero-height areas; that means "clear to end of window." */
8918 if (end < inside_height)
8919 x_clear_area (FRAME_X_DISPLAY (f), w,
8920 /* x, y, width, height, and exposures. */
8921 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8922 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
8923 inside_width, inside_height - end,
8924 False);
8925
8926 }
8927
8928 UNBLOCK_INPUT;
8929 }
8930
8931 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8932
8933 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8934 nil. */
8935
8936 static void
8937 x_scroll_bar_remove (bar)
8938 struct scroll_bar *bar;
8939 {
8940 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8941 BLOCK_INPUT;
8942
8943 #ifdef USE_TOOLKIT_SCROLL_BARS
8944 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
8945 #else
8946 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8947 #endif
8948
8949 /* Disassociate this scroll bar from its window. */
8950 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
8951
8952 UNBLOCK_INPUT;
8953 }
8954
8955
8956 /* Set the handle of the vertical scroll bar for WINDOW to indicate
8957 that we are displaying PORTION characters out of a total of WHOLE
8958 characters, starting at POSITION. If WINDOW has no scroll bar,
8959 create one. */
8960
8961 static void
8962 XTset_vertical_scroll_bar (w, portion, whole, position)
8963 struct window *w;
8964 int portion, whole, position;
8965 {
8966 struct frame *f = XFRAME (w->frame);
8967 struct scroll_bar *bar;
8968 int top, height, left, sb_left, width, sb_width;
8969 int window_x, window_y, window_width, window_height;
8970
8971 /* Get window dimensions. */
8972 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8973 top = window_y;
8974 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8975 height = window_height;
8976
8977 /* Compute the left edge of the scroll bar area. */
8978 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8979 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8980 else
8981 left = XFASTINT (w->left);
8982 left *= CANON_X_UNIT (f);
8983 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8984
8985 /* Compute the width of the scroll bar which might be less than
8986 the width of the area reserved for the scroll bar. */
8987 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8988 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
8989 else
8990 sb_width = width;
8991
8992 /* Compute the left edge of the scroll bar. */
8993 #ifdef USE_TOOLKIT_SCROLL_BARS
8994 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8995 sb_left = left + width - sb_width - (width - sb_width) / 2;
8996 else
8997 sb_left = left + (width - sb_width) / 2;
8998 #else
8999 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9000 sb_left = left + width - sb_width;
9001 else
9002 sb_left = left;
9003 #endif
9004
9005 /* Does the scroll bar exist yet? */
9006 if (NILP (w->vertical_scroll_bar))
9007 {
9008 BLOCK_INPUT;
9009 if (width && height)
9010 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9011 left, top, width, height, False);
9012 UNBLOCK_INPUT;
9013 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
9014 }
9015 else
9016 {
9017 /* It may just need to be moved and resized. */
9018 unsigned int mask = 0;
9019
9020 bar = XSCROLL_BAR (w->vertical_scroll_bar);
9021
9022 BLOCK_INPUT;
9023
9024 if (sb_left != XINT (bar->left))
9025 mask |= CWX;
9026 if (top != XINT (bar->top))
9027 mask |= CWY;
9028 if (sb_width != XINT (bar->width))
9029 mask |= CWWidth;
9030 if (height != XINT (bar->height))
9031 mask |= CWHeight;
9032
9033 #ifdef USE_TOOLKIT_SCROLL_BARS
9034
9035 /* Since toolkit scroll bars are smaller than the space reserved
9036 for them on the frame, we have to clear "under" them. */
9037 if (width && height)
9038 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9039 left, top, width, height, False);
9040
9041 /* Move/size the scroll bar widget. */
9042 if (mask)
9043 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
9044 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9045 top,
9046 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9047 max (height, 1), 0);
9048
9049 #else /* not USE_TOOLKIT_SCROLL_BARS */
9050
9051 /* Clear areas not covered by the scroll bar because of
9052 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
9053 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
9054 {
9055 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9056 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9057 height, False);
9058 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9059 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9060 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9061 height, False);
9062 }
9063
9064 /* Clear areas not covered by the scroll bar because it's not as
9065 wide as the area reserved for it . This makes sure a
9066 previous mode line display is cleared after C-x 2 C-x 1, for
9067 example. */
9068 {
9069 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9070 int rest = area_width - sb_width;
9071 if (rest > 0)
9072 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9073 left + area_width - rest, 0,
9074 rest, max (height, 1), False);
9075 }
9076
9077 /* Move/size the scroll bar window. */
9078 if (mask)
9079 {
9080 XWindowChanges wc;
9081
9082 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9083 wc.y = top;
9084 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
9085 wc.height = height;
9086 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
9087 mask, &wc);
9088 }
9089
9090 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9091
9092 /* Remember new settings. */
9093 XSETINT (bar->left, sb_left);
9094 XSETINT (bar->top, top);
9095 XSETINT (bar->width, sb_width);
9096 XSETINT (bar->height, height);
9097
9098 UNBLOCK_INPUT;
9099 }
9100
9101 #ifdef USE_TOOLKIT_SCROLL_BARS
9102 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
9103 #else /* not USE_TOOLKIT_SCROLL_BARS */
9104 /* Set the scroll bar's current state, unless we're currently being
9105 dragged. */
9106 if (NILP (bar->dragging))
9107 {
9108 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
9109
9110 if (whole == 0)
9111 x_scroll_bar_set_handle (bar, 0, top_range, 0);
9112 else
9113 {
9114 int start = ((double) position * top_range) / whole;
9115 int end = ((double) (position + portion) * top_range) / whole;
9116 x_scroll_bar_set_handle (bar, start, end, 0);
9117 }
9118 }
9119 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9120
9121 XSETVECTOR (w->vertical_scroll_bar, bar);
9122 }
9123
9124
9125 /* The following three hooks are used when we're doing a thorough
9126 redisplay of the frame. We don't explicitly know which scroll bars
9127 are going to be deleted, because keeping track of when windows go
9128 away is a real pain - "Can you say set-window-configuration, boys
9129 and girls?" Instead, we just assert at the beginning of redisplay
9130 that *all* scroll bars are to be removed, and then save a scroll bar
9131 from the fiery pit when we actually redisplay its window. */
9132
9133 /* Arrange for all scroll bars on FRAME to be removed at the next call
9134 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
9135 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
9136
9137 static void
9138 XTcondemn_scroll_bars (frame)
9139 FRAME_PTR frame;
9140 {
9141 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
9142 while (! NILP (FRAME_SCROLL_BARS (frame)))
9143 {
9144 Lisp_Object bar;
9145 bar = FRAME_SCROLL_BARS (frame);
9146 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
9147 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
9148 XSCROLL_BAR (bar)->prev = Qnil;
9149 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
9150 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
9151 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
9152 }
9153 }
9154
9155
9156 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
9157 Note that WINDOW isn't necessarily condemned at all. */
9158
9159 static void
9160 XTredeem_scroll_bar (window)
9161 struct window *window;
9162 {
9163 struct scroll_bar *bar;
9164 struct frame *f;
9165
9166 /* We can't redeem this window's scroll bar if it doesn't have one. */
9167 if (NILP (window->vertical_scroll_bar))
9168 abort ();
9169
9170 bar = XSCROLL_BAR (window->vertical_scroll_bar);
9171
9172 /* Unlink it from the condemned list. */
9173 f = XFRAME (WINDOW_FRAME (window));
9174 if (NILP (bar->prev))
9175 {
9176 /* If the prev pointer is nil, it must be the first in one of
9177 the lists. */
9178 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
9179 /* It's not condemned. Everything's fine. */
9180 return;
9181 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
9182 window->vertical_scroll_bar))
9183 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
9184 else
9185 /* If its prev pointer is nil, it must be at the front of
9186 one or the other! */
9187 abort ();
9188 }
9189 else
9190 XSCROLL_BAR (bar->prev)->next = bar->next;
9191
9192 if (! NILP (bar->next))
9193 XSCROLL_BAR (bar->next)->prev = bar->prev;
9194
9195 bar->next = FRAME_SCROLL_BARS (f);
9196 bar->prev = Qnil;
9197 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9198 if (! NILP (bar->next))
9199 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9200 }
9201
9202 /* Remove all scroll bars on FRAME that haven't been saved since the
9203 last call to `*condemn_scroll_bars_hook'. */
9204
9205 static void
9206 XTjudge_scroll_bars (f)
9207 FRAME_PTR f;
9208 {
9209 Lisp_Object bar, next;
9210
9211 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
9212
9213 /* Clear out the condemned list now so we won't try to process any
9214 more events on the hapless scroll bars. */
9215 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
9216
9217 for (; ! NILP (bar); bar = next)
9218 {
9219 struct scroll_bar *b = XSCROLL_BAR (bar);
9220
9221 x_scroll_bar_remove (b);
9222
9223 next = b->next;
9224 b->next = b->prev = Qnil;
9225 }
9226
9227 /* Now there should be no references to the condemned scroll bars,
9228 and they should get garbage-collected. */
9229 }
9230
9231
9232 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
9233 is a no-op when using toolkit scroll bars.
9234
9235 This may be called from a signal handler, so we have to ignore GC
9236 mark bits. */
9237
9238 static void
9239 x_scroll_bar_expose (bar, event)
9240 struct scroll_bar *bar;
9241 XEvent *event;
9242 {
9243 #ifndef USE_TOOLKIT_SCROLL_BARS
9244
9245 Window w = SCROLL_BAR_X_WINDOW (bar);
9246 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9247 GC gc = f->output_data.x->normal_gc;
9248 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9249
9250 BLOCK_INPUT;
9251
9252 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
9253
9254 /* Draw a one-pixel border just inside the edges of the scroll bar. */
9255 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
9256
9257 /* x, y, width, height */
9258 0, 0,
9259 XINT (bar->width) - 1 - width_trim - width_trim,
9260 XINT (bar->height) - 1);
9261
9262 UNBLOCK_INPUT;
9263
9264 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9265 }
9266
9267 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
9268 is set to something other than no_event, it is enqueued.
9269
9270 This may be called from a signal handler, so we have to ignore GC
9271 mark bits. */
9272
9273 #ifndef USE_TOOLKIT_SCROLL_BARS
9274
9275 static void
9276 x_scroll_bar_handle_click (bar, event, emacs_event)
9277 struct scroll_bar *bar;
9278 XEvent *event;
9279 struct input_event *emacs_event;
9280 {
9281 if (! GC_WINDOWP (bar->window))
9282 abort ();
9283
9284 emacs_event->kind = scroll_bar_click;
9285 emacs_event->code = event->xbutton.button - Button1;
9286 emacs_event->modifiers
9287 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
9288 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
9289 event->xbutton.state)
9290 | (event->type == ButtonRelease
9291 ? up_modifier
9292 : down_modifier));
9293 emacs_event->frame_or_window = bar->window;
9294 emacs_event->arg = Qnil;
9295 emacs_event->timestamp = event->xbutton.time;
9296 {
9297 #if 0
9298 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9299 int internal_height
9300 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9301 #endif
9302 int top_range
9303 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9304 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
9305
9306 if (y < 0) y = 0;
9307 if (y > top_range) y = top_range;
9308
9309 if (y < XINT (bar->start))
9310 emacs_event->part = scroll_bar_above_handle;
9311 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9312 emacs_event->part = scroll_bar_handle;
9313 else
9314 emacs_event->part = scroll_bar_below_handle;
9315
9316 /* Just because the user has clicked on the handle doesn't mean
9317 they want to drag it. Lisp code needs to be able to decide
9318 whether or not we're dragging. */
9319 #if 0
9320 /* If the user has just clicked on the handle, record where they're
9321 holding it. */
9322 if (event->type == ButtonPress
9323 && emacs_event->part == scroll_bar_handle)
9324 XSETINT (bar->dragging, y - XINT (bar->start));
9325 #endif
9326
9327 /* If the user has released the handle, set it to its final position. */
9328 if (event->type == ButtonRelease
9329 && ! NILP (bar->dragging))
9330 {
9331 int new_start = y - XINT (bar->dragging);
9332 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9333
9334 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9335 bar->dragging = Qnil;
9336 }
9337
9338 /* Same deal here as the other #if 0. */
9339 #if 0
9340 /* Clicks on the handle are always reported as occurring at the top of
9341 the handle. */
9342 if (emacs_event->part == scroll_bar_handle)
9343 emacs_event->x = bar->start;
9344 else
9345 XSETINT (emacs_event->x, y);
9346 #else
9347 XSETINT (emacs_event->x, y);
9348 #endif
9349
9350 XSETINT (emacs_event->y, top_range);
9351 }
9352 }
9353
9354 /* Handle some mouse motion while someone is dragging the scroll bar.
9355
9356 This may be called from a signal handler, so we have to ignore GC
9357 mark bits. */
9358
9359 static void
9360 x_scroll_bar_note_movement (bar, event)
9361 struct scroll_bar *bar;
9362 XEvent *event;
9363 {
9364 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9365
9366 last_mouse_movement_time = event->xmotion.time;
9367
9368 f->mouse_moved = 1;
9369 XSETVECTOR (last_mouse_scroll_bar, bar);
9370
9371 /* If we're dragging the bar, display it. */
9372 if (! GC_NILP (bar->dragging))
9373 {
9374 /* Where should the handle be now? */
9375 int new_start = event->xmotion.y - XINT (bar->dragging);
9376
9377 if (new_start != XINT (bar->start))
9378 {
9379 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9380
9381 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9382 }
9383 }
9384 }
9385
9386 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9387
9388 /* Return information to the user about the current position of the mouse
9389 on the scroll bar. */
9390
9391 static void
9392 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9393 FRAME_PTR *fp;
9394 Lisp_Object *bar_window;
9395 enum scroll_bar_part *part;
9396 Lisp_Object *x, *y;
9397 unsigned long *time;
9398 {
9399 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
9400 Window w = SCROLL_BAR_X_WINDOW (bar);
9401 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9402 int win_x, win_y;
9403 Window dummy_window;
9404 int dummy_coord;
9405 unsigned int dummy_mask;
9406
9407 BLOCK_INPUT;
9408
9409 /* Get the mouse's position relative to the scroll bar window, and
9410 report that. */
9411 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
9412
9413 /* Root, child, root x and root y. */
9414 &dummy_window, &dummy_window,
9415 &dummy_coord, &dummy_coord,
9416
9417 /* Position relative to scroll bar. */
9418 &win_x, &win_y,
9419
9420 /* Mouse buttons and modifier keys. */
9421 &dummy_mask))
9422 ;
9423 else
9424 {
9425 #if 0
9426 int inside_height
9427 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9428 #endif
9429 int top_range
9430 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9431
9432 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9433
9434 if (! NILP (bar->dragging))
9435 win_y -= XINT (bar->dragging);
9436
9437 if (win_y < 0)
9438 win_y = 0;
9439 if (win_y > top_range)
9440 win_y = top_range;
9441
9442 *fp = f;
9443 *bar_window = bar->window;
9444
9445 if (! NILP (bar->dragging))
9446 *part = scroll_bar_handle;
9447 else if (win_y < XINT (bar->start))
9448 *part = scroll_bar_above_handle;
9449 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9450 *part = scroll_bar_handle;
9451 else
9452 *part = scroll_bar_below_handle;
9453
9454 XSETINT (*x, win_y);
9455 XSETINT (*y, top_range);
9456
9457 f->mouse_moved = 0;
9458 last_mouse_scroll_bar = Qnil;
9459 }
9460
9461 *time = last_mouse_movement_time;
9462
9463 UNBLOCK_INPUT;
9464 }
9465
9466
9467 /* The screen has been cleared so we may have changed foreground or
9468 background colors, and the scroll bars may need to be redrawn.
9469 Clear out the scroll bars, and ask for expose events, so we can
9470 redraw them. */
9471
9472 void
9473 x_scroll_bar_clear (f)
9474 FRAME_PTR f;
9475 {
9476 #ifndef USE_TOOLKIT_SCROLL_BARS
9477 Lisp_Object bar;
9478
9479 /* We can have scroll bars even if this is 0,
9480 if we just turned off scroll bar mode.
9481 But in that case we should not clear them. */
9482 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9483 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9484 bar = XSCROLL_BAR (bar)->next)
9485 XClearArea (FRAME_X_DISPLAY (f),
9486 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9487 0, 0, 0, 0, True);
9488 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9489 }
9490
9491 /* This processes Expose events from the menu-bar specific X event
9492 loop in xmenu.c. This allows to redisplay the frame if necessary
9493 when handling menu-bar or pop-up items. */
9494
9495 int
9496 process_expose_from_menu (event)
9497 XEvent event;
9498 {
9499 FRAME_PTR f;
9500 struct x_display_info *dpyinfo;
9501 int frame_exposed_p = 0;
9502
9503 BLOCK_INPUT;
9504
9505 dpyinfo = x_display_info_for_display (event.xexpose.display);
9506 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9507 if (f)
9508 {
9509 if (f->async_visible == 0)
9510 {
9511 f->async_visible = 1;
9512 f->async_iconified = 0;
9513 f->output_data.x->has_been_visible = 1;
9514 SET_FRAME_GARBAGED (f);
9515 }
9516 else
9517 {
9518 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9519 event.xexpose.x, event.xexpose.y,
9520 event.xexpose.width, event.xexpose.height);
9521 frame_exposed_p = 1;
9522 }
9523 }
9524 else
9525 {
9526 struct scroll_bar *bar
9527 = x_window_to_scroll_bar (event.xexpose.window);
9528
9529 if (bar)
9530 x_scroll_bar_expose (bar, &event);
9531 }
9532
9533 UNBLOCK_INPUT;
9534 return frame_exposed_p;
9535 }
9536 \f
9537 /* Define a queue to save up SelectionRequest events for later handling. */
9538
9539 struct selection_event_queue
9540 {
9541 XEvent event;
9542 struct selection_event_queue *next;
9543 };
9544
9545 static struct selection_event_queue *queue;
9546
9547 /* Nonzero means queue up certain events--don't process them yet. */
9548
9549 static int x_queue_selection_requests;
9550
9551 /* Queue up an X event *EVENT, to be processed later. */
9552
9553 static void
9554 x_queue_event (f, event)
9555 FRAME_PTR f;
9556 XEvent *event;
9557 {
9558 struct selection_event_queue *queue_tmp
9559 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
9560
9561 if (queue_tmp != NULL)
9562 {
9563 queue_tmp->event = *event;
9564 queue_tmp->next = queue;
9565 queue = queue_tmp;
9566 }
9567 }
9568
9569 /* Take all the queued events and put them back
9570 so that they get processed afresh. */
9571
9572 static void
9573 x_unqueue_events (display)
9574 Display *display;
9575 {
9576 while (queue != NULL)
9577 {
9578 struct selection_event_queue *queue_tmp = queue;
9579 XPutBackEvent (display, &queue_tmp->event);
9580 queue = queue_tmp->next;
9581 xfree ((char *)queue_tmp);
9582 }
9583 }
9584
9585 /* Start queuing SelectionRequest events. */
9586
9587 void
9588 x_start_queuing_selection_requests (display)
9589 Display *display;
9590 {
9591 x_queue_selection_requests++;
9592 }
9593
9594 /* Stop queuing SelectionRequest events. */
9595
9596 void
9597 x_stop_queuing_selection_requests (display)
9598 Display *display;
9599 {
9600 x_queue_selection_requests--;
9601 x_unqueue_events (display);
9602 }
9603 \f
9604 /* The main X event-reading loop - XTread_socket. */
9605
9606 /* Time stamp of enter window event. This is only used by XTread_socket,
9607 but we have to put it out here, since static variables within functions
9608 sometimes don't work. */
9609
9610 static Time enter_timestamp;
9611
9612 /* This holds the state XLookupString needs to implement dead keys
9613 and other tricks known as "compose processing". _X Window System_
9614 says that a portable program can't use this, but Stephen Gildea assures
9615 me that letting the compiler initialize it to zeros will work okay.
9616
9617 This must be defined outside of XTread_socket, for the same reasons
9618 given for enter_time stamp, above. */
9619
9620 static XComposeStatus compose_status;
9621
9622 /* Record the last 100 characters stored
9623 to help debug the loss-of-chars-during-GC problem. */
9624
9625 static int temp_index;
9626 static short temp_buffer[100];
9627
9628 /* Set this to nonzero to fake an "X I/O error"
9629 on a particular display. */
9630
9631 struct x_display_info *XTread_socket_fake_io_error;
9632
9633 /* When we find no input here, we occasionally do a no-op command
9634 to verify that the X server is still running and we can still talk with it.
9635 We try all the open displays, one by one.
9636 This variable is used for cycling thru the displays. */
9637
9638 static struct x_display_info *next_noop_dpyinfo;
9639
9640 #define SET_SAVED_MENU_EVENT(size) \
9641 do \
9642 { \
9643 if (f->output_data.x->saved_menu_event == 0) \
9644 f->output_data.x->saved_menu_event \
9645 = (XEvent *) xmalloc (sizeof (XEvent)); \
9646 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9647 if (numchars >= 1) \
9648 { \
9649 bufp->kind = menu_bar_activate_event; \
9650 XSETFRAME (bufp->frame_or_window, f); \
9651 bufp->arg = Qnil; \
9652 bufp++; \
9653 count++; \
9654 numchars--; \
9655 } \
9656 } \
9657 while (0)
9658
9659 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9660 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9661
9662 /* Read events coming from the X server.
9663 This routine is called by the SIGIO handler.
9664 We return as soon as there are no more events to be read.
9665
9666 Events representing keys are stored in buffer BUFP,
9667 which can hold up to NUMCHARS characters.
9668 We return the number of characters stored into the buffer,
9669 thus pretending to be `read'.
9670
9671 EXPECTED is nonzero if the caller knows input is available. */
9672
9673 int
9674 XTread_socket (sd, bufp, numchars, expected)
9675 register int sd;
9676 /* register */ struct input_event *bufp;
9677 /* register */ int numchars;
9678 int expected;
9679 {
9680 int count = 0;
9681 int nbytes = 0;
9682 XEvent event;
9683 struct frame *f;
9684 int event_found = 0;
9685 struct x_display_info *dpyinfo;
9686 struct coding_system coding;
9687
9688 if (interrupt_input_blocked)
9689 {
9690 interrupt_input_pending = 1;
9691 return -1;
9692 }
9693
9694 interrupt_input_pending = 0;
9695 BLOCK_INPUT;
9696
9697 /* So people can tell when we have read the available input. */
9698 input_signal_count++;
9699
9700 if (numchars <= 0)
9701 abort (); /* Don't think this happens. */
9702
9703 ++handling_signal;
9704
9705 /* The input should be decoded if it is from XIM. Currently the
9706 locale of XIM is the same as that of the system. So, we can use
9707 Vlocale_coding_system which is initialized properly at Emacs
9708 startup time. */
9709 setup_coding_system (Vlocale_coding_system, &coding);
9710 coding.src_multibyte = 0;
9711 coding.dst_multibyte = 1;
9712 /* The input is converted to events, thus we can't handle
9713 composition. Anyway, there's no XIM that gives us composition
9714 information. */
9715 coding.composing = COMPOSITION_DISABLED;
9716
9717 /* Find the display we are supposed to read input for.
9718 It's the one communicating on descriptor SD. */
9719 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9720 {
9721 #if 0 /* This ought to be unnecessary; let's verify it. */
9722 #ifdef FIOSNBIO
9723 /* If available, Xlib uses FIOSNBIO to make the socket
9724 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
9725 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
9726 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
9727 fcntl (dpyinfo->connection, F_SETFL, 0);
9728 #endif /* ! defined (FIOSNBIO) */
9729 #endif
9730
9731 #if 0 /* This code can't be made to work, with multiple displays,
9732 and appears not to be used on any system any more.
9733 Also keyboard.c doesn't turn O_NDELAY on and off
9734 for X connections. */
9735 #ifndef SIGIO
9736 #ifndef HAVE_SELECT
9737 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9738 {
9739 extern int read_alarm_should_throw;
9740 read_alarm_should_throw = 1;
9741 XPeekEvent (dpyinfo->display, &event);
9742 read_alarm_should_throw = 0;
9743 }
9744 #endif /* HAVE_SELECT */
9745 #endif /* SIGIO */
9746 #endif
9747
9748 /* For debugging, this gives a way to fake an I/O error. */
9749 if (dpyinfo == XTread_socket_fake_io_error)
9750 {
9751 XTread_socket_fake_io_error = 0;
9752 x_io_error_quitter (dpyinfo->display);
9753 }
9754
9755 while (XPending (dpyinfo->display))
9756 {
9757 XNextEvent (dpyinfo->display, &event);
9758
9759 #ifdef HAVE_X_I18N
9760 {
9761 /* Filter events for the current X input method.
9762 XFilterEvent returns non-zero if the input method has
9763 consumed the event. We pass the frame's X window to
9764 XFilterEvent because that's the one for which the IC
9765 was created. */
9766 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9767 event.xclient.window);
9768 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
9769 break;
9770 }
9771 #endif
9772 event_found = 1;
9773
9774 switch (event.type)
9775 {
9776 case ClientMessage:
9777 {
9778 if (event.xclient.message_type
9779 == dpyinfo->Xatom_wm_protocols
9780 && event.xclient.format == 32)
9781 {
9782 if (event.xclient.data.l[0]
9783 == dpyinfo->Xatom_wm_take_focus)
9784 {
9785 /* Use x_any_window_to_frame because this
9786 could be the shell widget window
9787 if the frame has no title bar. */
9788 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
9789 #ifdef HAVE_X_I18N
9790 /* Not quite sure this is needed -pd */
9791 if (f && FRAME_XIC (f))
9792 XSetICFocus (FRAME_XIC (f));
9793 #endif
9794 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9795 instructs the WM to set the input focus automatically for
9796 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9797 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9798 it has set the focus. So, XSetInputFocus below is not
9799 needed.
9800
9801 The call to XSetInputFocus below has also caused trouble. In
9802 cases where the XSetInputFocus done by the WM and the one
9803 below are temporally close (on a fast machine), the call
9804 below can generate additional FocusIn events which confuse
9805 Emacs. */
9806
9807 /* Since we set WM_TAKE_FOCUS, we must call
9808 XSetInputFocus explicitly. But not if f is null,
9809 since that might be an event for a deleted frame. */
9810 if (f)
9811 {
9812 Display *d = event.xclient.display;
9813 /* Catch and ignore errors, in case window has been
9814 iconified by a window manager such as GWM. */
9815 int count = x_catch_errors (d);
9816 XSetInputFocus (d, event.xclient.window,
9817 /* The ICCCM says this is
9818 the only valid choice. */
9819 RevertToParent,
9820 event.xclient.data.l[1]);
9821 /* This is needed to detect the error
9822 if there is an error. */
9823 XSync (d, False);
9824 x_uncatch_errors (d, count);
9825 }
9826 /* Not certain about handling scroll bars here */
9827 #endif /* 0 */
9828 }
9829 else if (event.xclient.data.l[0]
9830 == dpyinfo->Xatom_wm_save_yourself)
9831 {
9832 /* Save state modify the WM_COMMAND property to
9833 something which can reinstate us. This notifies
9834 the session manager, who's looking for such a
9835 PropertyNotify. Can restart processing when
9836 a keyboard or mouse event arrives. */
9837 if (numchars > 0)
9838 {
9839 f = x_top_window_to_frame (dpyinfo,
9840 event.xclient.window);
9841
9842 /* This is just so we only give real data once
9843 for a single Emacs process. */
9844 if (f == SELECTED_FRAME ())
9845 XSetCommand (FRAME_X_DISPLAY (f),
9846 event.xclient.window,
9847 initial_argv, initial_argc);
9848 else if (f)
9849 XSetCommand (FRAME_X_DISPLAY (f),
9850 event.xclient.window,
9851 0, 0);
9852 }
9853 }
9854 else if (event.xclient.data.l[0]
9855 == dpyinfo->Xatom_wm_delete_window)
9856 {
9857 struct frame *f
9858 = x_any_window_to_frame (dpyinfo,
9859 event.xclient.window);
9860
9861 if (f)
9862 {
9863 if (numchars == 0)
9864 abort ();
9865
9866 bufp->kind = delete_window_event;
9867 XSETFRAME (bufp->frame_or_window, f);
9868 bufp->arg = Qnil;
9869 bufp++;
9870
9871 count += 1;
9872 numchars -= 1;
9873 }
9874 }
9875 }
9876 else if (event.xclient.message_type
9877 == dpyinfo->Xatom_wm_configure_denied)
9878 {
9879 }
9880 else if (event.xclient.message_type
9881 == dpyinfo->Xatom_wm_window_moved)
9882 {
9883 int new_x, new_y;
9884 struct frame *f
9885 = x_window_to_frame (dpyinfo, event.xclient.window);
9886
9887 new_x = event.xclient.data.s[0];
9888 new_y = event.xclient.data.s[1];
9889
9890 if (f)
9891 {
9892 f->output_data.x->left_pos = new_x;
9893 f->output_data.x->top_pos = new_y;
9894 }
9895 }
9896 #ifdef HACK_EDITRES
9897 else if (event.xclient.message_type
9898 == dpyinfo->Xatom_editres)
9899 {
9900 struct frame *f
9901 = x_any_window_to_frame (dpyinfo, event.xclient.window);
9902 _XEditResCheckMessages (f->output_data.x->widget, NULL,
9903 &event, NULL);
9904 }
9905 #endif /* HACK_EDITRES */
9906 else if ((event.xclient.message_type
9907 == dpyinfo->Xatom_DONE)
9908 || (event.xclient.message_type
9909 == dpyinfo->Xatom_PAGE))
9910 {
9911 /* Ghostview job completed. Kill it. We could
9912 reply with "Next" if we received "Page", but we
9913 currently never do because we are interested in
9914 images, only, which should have 1 page. */
9915 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9916 struct frame *f
9917 = x_window_to_frame (dpyinfo, event.xclient.window);
9918 x_kill_gs_process (pixmap, f);
9919 expose_frame (f, 0, 0, 0, 0);
9920 }
9921 #ifdef USE_TOOLKIT_SCROLL_BARS
9922 /* Scroll bar callbacks send a ClientMessage from which
9923 we construct an input_event. */
9924 else if (event.xclient.message_type
9925 == dpyinfo->Xatom_Scrollbar)
9926 {
9927 x_scroll_bar_to_input_event (&event, bufp);
9928 ++bufp, ++count, --numchars;
9929 goto out;
9930 }
9931 #endif /* USE_TOOLKIT_SCROLL_BARS */
9932 else
9933 goto OTHER;
9934 }
9935 break;
9936
9937 case SelectionNotify:
9938 #ifdef USE_X_TOOLKIT
9939 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
9940 goto OTHER;
9941 #endif /* not USE_X_TOOLKIT */
9942 x_handle_selection_notify (&event.xselection);
9943 break;
9944
9945 case SelectionClear: /* Someone has grabbed ownership. */
9946 #ifdef USE_X_TOOLKIT
9947 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
9948 goto OTHER;
9949 #endif /* USE_X_TOOLKIT */
9950 {
9951 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
9952
9953 if (numchars == 0)
9954 abort ();
9955
9956 bufp->kind = selection_clear_event;
9957 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9958 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9959 SELECTION_EVENT_TIME (bufp) = eventp->time;
9960 bufp->frame_or_window = Qnil;
9961 bufp->arg = Qnil;
9962 bufp++;
9963
9964 count += 1;
9965 numchars -= 1;
9966 }
9967 break;
9968
9969 case SelectionRequest: /* Someone wants our selection. */
9970 #ifdef USE_X_TOOLKIT
9971 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
9972 goto OTHER;
9973 #endif /* USE_X_TOOLKIT */
9974 if (x_queue_selection_requests)
9975 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
9976 &event);
9977 else
9978 {
9979 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
9980
9981 if (numchars == 0)
9982 abort ();
9983
9984 bufp->kind = selection_request_event;
9985 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9986 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9987 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9988 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9989 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9990 SELECTION_EVENT_TIME (bufp) = eventp->time;
9991 bufp->frame_or_window = Qnil;
9992 bufp->arg = Qnil;
9993 bufp++;
9994
9995 count += 1;
9996 numchars -= 1;
9997 }
9998 break;
9999
10000 case PropertyNotify:
10001 #ifdef USE_X_TOOLKIT
10002 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
10003 goto OTHER;
10004 #endif /* not USE_X_TOOLKIT */
10005 x_handle_property_notify (&event.xproperty);
10006 break;
10007
10008 case ReparentNotify:
10009 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
10010 if (f)
10011 {
10012 int x, y;
10013 f->output_data.x->parent_desc = event.xreparent.parent;
10014 x_real_positions (f, &x, &y);
10015 f->output_data.x->left_pos = x;
10016 f->output_data.x->top_pos = y;
10017 }
10018 break;
10019
10020 case Expose:
10021 f = x_window_to_frame (dpyinfo, event.xexpose.window);
10022 if (f)
10023 {
10024 if (f->async_visible == 0)
10025 {
10026 f->async_visible = 1;
10027 f->async_iconified = 0;
10028 f->output_data.x->has_been_visible = 1;
10029 SET_FRAME_GARBAGED (f);
10030 }
10031 else
10032 expose_frame (x_window_to_frame (dpyinfo,
10033 event.xexpose.window),
10034 event.xexpose.x, event.xexpose.y,
10035 event.xexpose.width, event.xexpose.height);
10036 }
10037 else
10038 {
10039 #ifndef USE_TOOLKIT_SCROLL_BARS
10040 struct scroll_bar *bar;
10041 #endif
10042 #if defined USE_X_TOOLKIT && defined USE_LUCID
10043 /* Submenus of the Lucid menu bar aren't widgets
10044 themselves, so there's no way to dispatch events
10045 to them. Recognize this case separately. */
10046 {
10047 Widget widget
10048 = x_window_to_menu_bar (event.xexpose.window);
10049 if (widget)
10050 xlwmenu_redisplay (widget);
10051 }
10052 #endif /* USE_X_TOOLKIT && USE_LUCID */
10053
10054 #ifdef USE_TOOLKIT_SCROLL_BARS
10055 /* Dispatch event to the widget. */
10056 goto OTHER;
10057 #else /* not USE_TOOLKIT_SCROLL_BARS */
10058 bar = x_window_to_scroll_bar (event.xexpose.window);
10059
10060 if (bar)
10061 x_scroll_bar_expose (bar, &event);
10062 #ifdef USE_X_TOOLKIT
10063 else
10064 goto OTHER;
10065 #endif /* USE_X_TOOLKIT */
10066 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10067 }
10068 break;
10069
10070 case GraphicsExpose: /* This occurs when an XCopyArea's
10071 source area was obscured or not
10072 available.*/
10073 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
10074 if (f)
10075 {
10076 expose_frame (f,
10077 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
10078 event.xgraphicsexpose.width,
10079 event.xgraphicsexpose.height);
10080 }
10081 #ifdef USE_X_TOOLKIT
10082 else
10083 goto OTHER;
10084 #endif /* USE_X_TOOLKIT */
10085 break;
10086
10087 case NoExpose: /* This occurs when an XCopyArea's
10088 source area was completely
10089 available */
10090 break;
10091
10092 case UnmapNotify:
10093 /* Redo the mouse-highlight after the tooltip has gone. */
10094 if (event.xmap.window == tip_window)
10095 {
10096 tip_window = 0;
10097 redo_mouse_highlight ();
10098 }
10099
10100 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
10101 if (f) /* F may no longer exist if
10102 the frame was deleted. */
10103 {
10104 /* While a frame is unmapped, display generation is
10105 disabled; you don't want to spend time updating a
10106 display that won't ever be seen. */
10107 f->async_visible = 0;
10108 /* We can't distinguish, from the event, whether the window
10109 has become iconified or invisible. So assume, if it
10110 was previously visible, than now it is iconified.
10111 But x_make_frame_invisible clears both
10112 the visible flag and the iconified flag;
10113 and that way, we know the window is not iconified now. */
10114 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
10115 {
10116 f->async_iconified = 1;
10117
10118 bufp->kind = iconify_event;
10119 XSETFRAME (bufp->frame_or_window, f);
10120 bufp->arg = Qnil;
10121 bufp++;
10122 count++;
10123 numchars--;
10124 }
10125 }
10126 goto OTHER;
10127
10128 case MapNotify:
10129 if (event.xmap.window == tip_window)
10130 /* The tooltip has been drawn already. Avoid
10131 the SET_FRAME_GARBAGED below. */
10132 goto OTHER;
10133
10134 /* We use x_top_window_to_frame because map events can
10135 come for sub-windows and they don't mean that the
10136 frame is visible. */
10137 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
10138 if (f)
10139 {
10140 f->async_visible = 1;
10141 f->async_iconified = 0;
10142 f->output_data.x->has_been_visible = 1;
10143
10144 /* wait_reading_process_input will notice this and update
10145 the frame's display structures. */
10146 SET_FRAME_GARBAGED (f);
10147
10148 if (f->iconified)
10149 {
10150 bufp->kind = deiconify_event;
10151 XSETFRAME (bufp->frame_or_window, f);
10152 bufp->arg = Qnil;
10153 bufp++;
10154 count++;
10155 numchars--;
10156 }
10157 else if (! NILP (Vframe_list)
10158 && ! NILP (XCDR (Vframe_list)))
10159 /* Force a redisplay sooner or later
10160 to update the frame titles
10161 in case this is the second frame. */
10162 record_asynch_buffer_change ();
10163 }
10164 goto OTHER;
10165
10166 case KeyPress:
10167 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
10168
10169 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
10170 if (f == 0)
10171 {
10172 /* Scroll bars consume key events, but we want
10173 the keys to go to the scroll bar's frame. */
10174 Widget widget = XtWindowToWidget (dpyinfo->display,
10175 event.xkey.window);
10176 if (widget && XmIsScrollBar (widget))
10177 {
10178 widget = XtParent (widget);
10179 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
10180 }
10181 }
10182 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
10183
10184 if (f != 0)
10185 {
10186 KeySym keysym, orig_keysym;
10187 /* al%imercury@uunet.uu.net says that making this 81
10188 instead of 80 fixed a bug whereby meta chars made
10189 his Emacs hang.
10190
10191 It seems that some version of XmbLookupString has
10192 a bug of not returning XBufferOverflow in
10193 status_return even if the input is too long to
10194 fit in 81 bytes. So, we must prepare sufficient
10195 bytes for copy_buffer. 513 bytes (256 chars for
10196 two-byte character set) seems to be a faily good
10197 approximation. -- 2000.8.10 handa@etl.go.jp */
10198 unsigned char copy_buffer[513];
10199 unsigned char *copy_bufptr = copy_buffer;
10200 int copy_bufsiz = sizeof (copy_buffer);
10201 int modifiers;
10202
10203 event.xkey.state
10204 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
10205 extra_keyboard_modifiers);
10206 modifiers = event.xkey.state;
10207
10208 /* This will have to go some day... */
10209
10210 /* make_lispy_event turns chars into control chars.
10211 Don't do it here because XLookupString is too eager. */
10212 event.xkey.state &= ~ControlMask;
10213 event.xkey.state &= ~(dpyinfo->meta_mod_mask
10214 | dpyinfo->super_mod_mask
10215 | dpyinfo->hyper_mod_mask
10216 | dpyinfo->alt_mod_mask);
10217
10218 /* In case Meta is ComposeCharacter,
10219 clear its status. According to Markus Ehrnsperger
10220 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
10221 this enables ComposeCharacter to work whether or
10222 not it is combined with Meta. */
10223 if (modifiers & dpyinfo->meta_mod_mask)
10224 bzero (&compose_status, sizeof (compose_status));
10225
10226 #ifdef HAVE_X_I18N
10227 if (FRAME_XIC (f))
10228 {
10229 Status status_return;
10230
10231 nbytes = XmbLookupString (FRAME_XIC (f),
10232 &event.xkey, copy_bufptr,
10233 copy_bufsiz, &keysym,
10234 &status_return);
10235 if (status_return == XBufferOverflow)
10236 {
10237 copy_bufsiz = nbytes + 1;
10238 copy_bufptr = (char *) alloca (copy_bufsiz);
10239 nbytes = XmbLookupString (FRAME_XIC (f),
10240 &event.xkey, copy_bufptr,
10241 copy_bufsiz, &keysym,
10242 &status_return);
10243 }
10244
10245 if (status_return == XLookupNone)
10246 break;
10247 else if (status_return == XLookupChars)
10248 {
10249 keysym = NoSymbol;
10250 modifiers = 0;
10251 }
10252 else if (status_return != XLookupKeySym
10253 && status_return != XLookupBoth)
10254 abort ();
10255 }
10256 else
10257 nbytes = XLookupString (&event.xkey, copy_bufptr,
10258 copy_bufsiz, &keysym,
10259 &compose_status);
10260 #else
10261 nbytes = XLookupString (&event.xkey, copy_bufptr,
10262 copy_bufsiz, &keysym,
10263 &compose_status);
10264 #endif
10265
10266 orig_keysym = keysym;
10267
10268 if (numchars > 1)
10269 {
10270 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
10271 || keysym == XK_Delete
10272 #ifdef XK_ISO_Left_Tab
10273 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
10274 #endif
10275 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
10276 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
10277 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
10278 #ifdef HPUX
10279 /* This recognizes the "extended function keys".
10280 It seems there's no cleaner way.
10281 Test IsModifierKey to avoid handling mode_switch
10282 incorrectly. */
10283 || ((unsigned) (keysym) >= XK_Select
10284 && (unsigned)(keysym) < XK_KP_Space)
10285 #endif
10286 #ifdef XK_dead_circumflex
10287 || orig_keysym == XK_dead_circumflex
10288 #endif
10289 #ifdef XK_dead_grave
10290 || orig_keysym == XK_dead_grave
10291 #endif
10292 #ifdef XK_dead_tilde
10293 || orig_keysym == XK_dead_tilde
10294 #endif
10295 #ifdef XK_dead_diaeresis
10296 || orig_keysym == XK_dead_diaeresis
10297 #endif
10298 #ifdef XK_dead_macron
10299 || orig_keysym == XK_dead_macron
10300 #endif
10301 #ifdef XK_dead_degree
10302 || orig_keysym == XK_dead_degree
10303 #endif
10304 #ifdef XK_dead_acute
10305 || orig_keysym == XK_dead_acute
10306 #endif
10307 #ifdef XK_dead_cedilla
10308 || orig_keysym == XK_dead_cedilla
10309 #endif
10310 #ifdef XK_dead_breve
10311 || orig_keysym == XK_dead_breve
10312 #endif
10313 #ifdef XK_dead_ogonek
10314 || orig_keysym == XK_dead_ogonek
10315 #endif
10316 #ifdef XK_dead_caron
10317 || orig_keysym == XK_dead_caron
10318 #endif
10319 #ifdef XK_dead_doubleacute
10320 || orig_keysym == XK_dead_doubleacute
10321 #endif
10322 #ifdef XK_dead_abovedot
10323 || orig_keysym == XK_dead_abovedot
10324 #endif
10325 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10326 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10327 /* Any "vendor-specific" key is ok. */
10328 || (orig_keysym & (1 << 28)))
10329 && ! (IsModifierKey (orig_keysym)
10330 #ifndef HAVE_X11R5
10331 #ifdef XK_Mode_switch
10332 || ((unsigned)(orig_keysym) == XK_Mode_switch)
10333 #endif
10334 #ifdef XK_Num_Lock
10335 || ((unsigned)(orig_keysym) == XK_Num_Lock)
10336 #endif
10337 #endif /* not HAVE_X11R5 */
10338 ))
10339 {
10340 if (temp_index == sizeof temp_buffer / sizeof (short))
10341 temp_index = 0;
10342 temp_buffer[temp_index++] = keysym;
10343 bufp->kind = non_ascii_keystroke;
10344 bufp->code = keysym;
10345 XSETFRAME (bufp->frame_or_window, f);
10346 bufp->arg = Qnil;
10347 bufp->modifiers
10348 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10349 modifiers);
10350 bufp->timestamp = event.xkey.time;
10351 bufp++;
10352 count++;
10353 numchars--;
10354 }
10355 else if (numchars > nbytes)
10356 {
10357 register int i;
10358 register int c;
10359 int nchars, len;
10360
10361 for (i = 0; i < nbytes; i++)
10362 {
10363 if (temp_index == (sizeof temp_buffer
10364 / sizeof (short)))
10365 temp_index = 0;
10366 temp_buffer[temp_index++] = copy_bufptr[i];
10367 }
10368
10369 if (/* If the event is not from XIM, */
10370 event.xkey.keycode != 0
10371 /* or the current locale doesn't request
10372 decoding of the intup data, ... */
10373 || coding.type == coding_type_raw_text
10374 || coding.type == coding_type_no_conversion)
10375 {
10376 /* ... we can use the input data as is. */
10377 nchars = nbytes;
10378 }
10379 else
10380 {
10381 /* We have to decode the input data. */
10382 int require;
10383 unsigned char *p;
10384
10385 require = decoding_buffer_size (&coding, nbytes);
10386 p = (unsigned char *) alloca (require);
10387 coding.mode |= CODING_MODE_LAST_BLOCK;
10388 decode_coding (&coding, copy_bufptr, p,
10389 nbytes, require);
10390 nbytes = coding.produced;
10391 nchars = coding.produced_char;
10392 copy_bufptr = p;
10393 }
10394
10395 /* Convert the input data to a sequence of
10396 character events. */
10397 for (i = 0; i < nbytes; i += len)
10398 {
10399 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10400 nbytes - i, len);
10401 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
10402 ? ascii_keystroke
10403 : multibyte_char_keystroke);
10404 bufp->code = c;
10405 XSETFRAME (bufp->frame_or_window, f);
10406 bufp->arg = Qnil;
10407 bufp->modifiers
10408 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10409 modifiers);
10410 bufp->timestamp = event.xkey.time;
10411 bufp++;
10412 }
10413
10414 count += nchars;
10415 numchars -= nchars;
10416
10417 if (keysym == NoSymbol)
10418 break;
10419 }
10420 else
10421 abort ();
10422 }
10423 else
10424 abort ();
10425 }
10426 #ifdef HAVE_X_I18N
10427 /* Don't dispatch this event since XtDispatchEvent calls
10428 XFilterEvent, and two calls in a row may freeze the
10429 client. */
10430 break;
10431 #else
10432 goto OTHER;
10433 #endif
10434
10435 case KeyRelease:
10436 #ifdef HAVE_X_I18N
10437 /* Don't dispatch this event since XtDispatchEvent calls
10438 XFilterEvent, and two calls in a row may freeze the
10439 client. */
10440 break;
10441 #else
10442 goto OTHER;
10443 #endif
10444
10445 /* Here's a possible interpretation of the whole
10446 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
10447 you get a FocusIn event, you have to get a FocusOut
10448 event before you relinquish the focus. If you
10449 haven't received a FocusIn event, then a mere
10450 LeaveNotify is enough to free you. */
10451
10452 case EnterNotify:
10453 {
10454 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10455
10456 if (event.xcrossing.focus)
10457 {
10458 /* Avoid nasty pop/raise loops. */
10459 if (f && (!(f->auto_raise)
10460 || !(f->auto_lower)
10461 || (event.xcrossing.time - enter_timestamp) > 500))
10462 {
10463 x_new_focus_frame (dpyinfo, f);
10464 enter_timestamp = event.xcrossing.time;
10465 }
10466 }
10467 else if (f == dpyinfo->x_focus_frame)
10468 x_new_focus_frame (dpyinfo, 0);
10469
10470 /* EnterNotify counts as mouse movement,
10471 so update things that depend on mouse position. */
10472 if (f && !f->output_data.x->hourglass_p)
10473 note_mouse_movement (f, &event.xmotion);
10474 goto OTHER;
10475 }
10476
10477 case FocusIn:
10478 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10479 if (event.xfocus.detail != NotifyPointer)
10480 dpyinfo->x_focus_event_frame = f;
10481 if (f)
10482 {
10483 x_new_focus_frame (dpyinfo, f);
10484
10485 /* Don't stop displaying the initial startup message
10486 for a switch-frame event we don't need. */
10487 if (GC_NILP (Vterminal_frame)
10488 && GC_CONSP (Vframe_list)
10489 && !GC_NILP (XCDR (Vframe_list)))
10490 {
10491 bufp->kind = FOCUS_IN_EVENT;
10492 XSETFRAME (bufp->frame_or_window, f);
10493 bufp->arg = Qnil;
10494 ++bufp, ++count, --numchars;
10495 }
10496 }
10497
10498 #ifdef HAVE_X_I18N
10499 if (f && FRAME_XIC (f))
10500 XSetICFocus (FRAME_XIC (f));
10501 #endif
10502
10503 goto OTHER;
10504
10505 case LeaveNotify:
10506 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
10507 if (f)
10508 {
10509 if (f == dpyinfo->mouse_face_mouse_frame)
10510 {
10511 /* If we move outside the frame, then we're
10512 certainly no longer on any text in the frame. */
10513 clear_mouse_face (dpyinfo);
10514 dpyinfo->mouse_face_mouse_frame = 0;
10515 }
10516
10517 /* Generate a nil HELP_EVENT to cancel a help-echo.
10518 Do it only if there's something to cancel.
10519 Otherwise, the startup message is cleared when
10520 the mouse leaves the frame. */
10521 if (any_help_event_p)
10522 {
10523 Lisp_Object frame;
10524 int n;
10525
10526 XSETFRAME (frame, f);
10527 help_echo = Qnil;
10528 n = gen_help_event (bufp, numchars,
10529 Qnil, frame, Qnil, Qnil, 0);
10530 bufp += n, count += n, numchars -= n;
10531 }
10532
10533 if (event.xcrossing.focus)
10534 x_mouse_leave (dpyinfo);
10535 else
10536 {
10537 if (f == dpyinfo->x_focus_event_frame)
10538 dpyinfo->x_focus_event_frame = 0;
10539 if (f == dpyinfo->x_focus_frame)
10540 x_new_focus_frame (dpyinfo, 0);
10541 }
10542 }
10543 goto OTHER;
10544
10545 case FocusOut:
10546 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10547 if (event.xfocus.detail != NotifyPointer
10548 && f == dpyinfo->x_focus_event_frame)
10549 dpyinfo->x_focus_event_frame = 0;
10550 if (f && f == dpyinfo->x_focus_frame)
10551 x_new_focus_frame (dpyinfo, 0);
10552
10553 #ifdef HAVE_X_I18N
10554 if (f && FRAME_XIC (f))
10555 XUnsetICFocus (FRAME_XIC (f));
10556 #endif
10557
10558 goto OTHER;
10559
10560 case MotionNotify:
10561 {
10562 previous_help_echo = help_echo;
10563 help_echo = help_echo_object = help_echo_window = Qnil;
10564 help_echo_pos = -1;
10565
10566 if (dpyinfo->grabbed && last_mouse_frame
10567 && FRAME_LIVE_P (last_mouse_frame))
10568 f = last_mouse_frame;
10569 else
10570 f = x_window_to_frame (dpyinfo, event.xmotion.window);
10571
10572 if (f)
10573 note_mouse_movement (f, &event.xmotion);
10574 else
10575 {
10576 #ifndef USE_TOOLKIT_SCROLL_BARS
10577 struct scroll_bar *bar
10578 = x_window_to_scroll_bar (event.xmotion.window);
10579
10580 if (bar)
10581 x_scroll_bar_note_movement (bar, &event);
10582 #endif /* USE_TOOLKIT_SCROLL_BARS */
10583
10584 /* If we move outside the frame, then we're
10585 certainly no longer on any text in the frame. */
10586 clear_mouse_face (dpyinfo);
10587 }
10588
10589 /* If the contents of the global variable help_echo
10590 has changed, generate a HELP_EVENT. */
10591 if (!NILP (help_echo)
10592 || !NILP (previous_help_echo))
10593 {
10594 Lisp_Object frame;
10595 int n;
10596
10597 if (f)
10598 XSETFRAME (frame, f);
10599 else
10600 frame = Qnil;
10601
10602 any_help_event_p = 1;
10603 n = gen_help_event (bufp, numchars, help_echo, frame,
10604 help_echo_window, help_echo_object,
10605 help_echo_pos);
10606 bufp += n, count += n, numchars -= n;
10607 }
10608
10609 goto OTHER;
10610 }
10611
10612 case ConfigureNotify:
10613 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10614 if (f)
10615 {
10616 #ifndef USE_X_TOOLKIT
10617 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10618 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
10619
10620 /* In the toolkit version, change_frame_size
10621 is called by the code that handles resizing
10622 of the EmacsFrame widget. */
10623
10624 /* Even if the number of character rows and columns has
10625 not changed, the font size may have changed, so we need
10626 to check the pixel dimensions as well. */
10627 if (columns != f->width
10628 || rows != f->height
10629 || event.xconfigure.width != f->output_data.x->pixel_width
10630 || event.xconfigure.height != f->output_data.x->pixel_height)
10631 {
10632 change_frame_size (f, rows, columns, 0, 1, 0);
10633 SET_FRAME_GARBAGED (f);
10634 cancel_mouse_face (f);
10635 }
10636 #endif
10637
10638 f->output_data.x->pixel_width = event.xconfigure.width;
10639 f->output_data.x->pixel_height = event.xconfigure.height;
10640
10641 /* What we have now is the position of Emacs's own window.
10642 Convert that to the position of the window manager window. */
10643 x_real_positions (f, &f->output_data.x->left_pos,
10644 &f->output_data.x->top_pos);
10645
10646 #ifdef HAVE_X_I18N
10647 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10648 xic_set_statusarea (f);
10649 #endif
10650
10651 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10652 {
10653 /* Since the WM decorations come below top_pos now,
10654 we must put them below top_pos in the future. */
10655 f->output_data.x->win_gravity = NorthWestGravity;
10656 x_wm_set_size_hint (f, (long) 0, 0);
10657 }
10658 #ifdef USE_MOTIF
10659 /* Some window managers pass (0,0) as the location of
10660 the window, and the Motif event handler stores it
10661 in the emacs widget, which messes up Motif menus. */
10662 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10663 {
10664 event.xconfigure.x = f->output_data.x->widget->core.x;
10665 event.xconfigure.y = f->output_data.x->widget->core.y;
10666 }
10667 #endif /* USE_MOTIF */
10668 }
10669 goto OTHER;
10670
10671 case ButtonPress:
10672 case ButtonRelease:
10673 {
10674 /* If we decide we want to generate an event to be seen
10675 by the rest of Emacs, we put it here. */
10676 struct input_event emacs_event;
10677 int tool_bar_p = 0;
10678
10679 emacs_event.kind = no_event;
10680 bzero (&compose_status, sizeof (compose_status));
10681
10682 if (dpyinfo->grabbed
10683 && last_mouse_frame
10684 && FRAME_LIVE_P (last_mouse_frame))
10685 f = last_mouse_frame;
10686 else
10687 f = x_window_to_frame (dpyinfo, event.xbutton.window);
10688
10689 if (f)
10690 {
10691 /* Is this in the tool-bar? */
10692 if (WINDOWP (f->tool_bar_window)
10693 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
10694 {
10695 Lisp_Object window;
10696 int p, x, y;
10697
10698 x = event.xbutton.x;
10699 y = event.xbutton.y;
10700
10701 /* Set x and y. */
10702 window = window_from_coordinates (f, x, y, &p, 1);
10703 if (EQ (window, f->tool_bar_window))
10704 {
10705 x_handle_tool_bar_click (f, &event.xbutton);
10706 tool_bar_p = 1;
10707 }
10708 }
10709
10710 if (!tool_bar_p)
10711 if (!dpyinfo->x_focus_frame
10712 || f == dpyinfo->x_focus_frame)
10713 construct_mouse_click (&emacs_event, &event, f);
10714 }
10715 else
10716 {
10717 #ifndef USE_TOOLKIT_SCROLL_BARS
10718 struct scroll_bar *bar
10719 = x_window_to_scroll_bar (event.xbutton.window);
10720
10721 if (bar)
10722 x_scroll_bar_handle_click (bar, &event, &emacs_event);
10723 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10724 }
10725
10726 if (event.type == ButtonPress)
10727 {
10728 dpyinfo->grabbed |= (1 << event.xbutton.button);
10729 last_mouse_frame = f;
10730 /* Ignore any mouse motion that happened
10731 before this event; any subsequent mouse-movement
10732 Emacs events should reflect only motion after
10733 the ButtonPress. */
10734 if (f != 0)
10735 f->mouse_moved = 0;
10736
10737 if (!tool_bar_p)
10738 last_tool_bar_item = -1;
10739 }
10740 else
10741 {
10742 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
10743 }
10744
10745 if (numchars >= 1 && emacs_event.kind != no_event)
10746 {
10747 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10748 bufp++;
10749 count++;
10750 numchars--;
10751 }
10752
10753 #ifdef USE_X_TOOLKIT
10754 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10755 /* For a down-event in the menu bar,
10756 don't pass it to Xt right now.
10757 Instead, save it away
10758 and we will pass it to Xt from kbd_buffer_get_event.
10759 That way, we can run some Lisp code first. */
10760 if (f && event.type == ButtonPress
10761 /* Verify the event is really within the menu bar
10762 and not just sent to it due to grabbing. */
10763 && event.xbutton.x >= 0
10764 && event.xbutton.x < f->output_data.x->pixel_width
10765 && event.xbutton.y >= 0
10766 && event.xbutton.y < f->output_data.x->menubar_height
10767 && event.xbutton.same_screen)
10768 {
10769 SET_SAVED_BUTTON_EVENT;
10770 XSETFRAME (last_mouse_press_frame, f);
10771 }
10772 else if (event.type == ButtonPress)
10773 {
10774 last_mouse_press_frame = Qnil;
10775 goto OTHER;
10776 }
10777
10778 #ifdef USE_MOTIF /* This should do not harm for Lucid,
10779 but I am trying to be cautious. */
10780 else if (event.type == ButtonRelease)
10781 {
10782 if (!NILP (last_mouse_press_frame))
10783 {
10784 f = XFRAME (last_mouse_press_frame);
10785 if (f->output_data.x)
10786 SET_SAVED_BUTTON_EVENT;
10787 }
10788 else
10789 goto OTHER;
10790 }
10791 #endif /* USE_MOTIF */
10792 else
10793 goto OTHER;
10794 #endif /* USE_X_TOOLKIT */
10795 }
10796 break;
10797
10798 case CirculateNotify:
10799 goto OTHER;
10800
10801 case CirculateRequest:
10802 goto OTHER;
10803
10804 case VisibilityNotify:
10805 goto OTHER;
10806
10807 case MappingNotify:
10808 /* Someone has changed the keyboard mapping - update the
10809 local cache. */
10810 switch (event.xmapping.request)
10811 {
10812 case MappingModifier:
10813 x_find_modifier_meanings (dpyinfo);
10814 /* This is meant to fall through. */
10815 case MappingKeyboard:
10816 XRefreshKeyboardMapping (&event.xmapping);
10817 }
10818 goto OTHER;
10819
10820 default:
10821 OTHER:
10822 #ifdef USE_X_TOOLKIT
10823 BLOCK_INPUT;
10824 XtDispatchEvent (&event);
10825 UNBLOCK_INPUT;
10826 #endif /* USE_X_TOOLKIT */
10827 break;
10828 }
10829 }
10830 }
10831
10832 out:;
10833
10834 /* On some systems, an X bug causes Emacs to get no more events
10835 when the window is destroyed. Detect that. (1994.) */
10836 if (! event_found)
10837 {
10838 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10839 One XNOOP in 100 loops will make Emacs terminate.
10840 B. Bretthauer, 1994 */
10841 x_noop_count++;
10842 if (x_noop_count >= 100)
10843 {
10844 x_noop_count=0;
10845
10846 if (next_noop_dpyinfo == 0)
10847 next_noop_dpyinfo = x_display_list;
10848
10849 XNoOp (next_noop_dpyinfo->display);
10850
10851 /* Each time we get here, cycle through the displays now open. */
10852 next_noop_dpyinfo = next_noop_dpyinfo->next;
10853 }
10854 }
10855
10856 /* If the focus was just given to an auto-raising frame,
10857 raise it now. */
10858 /* ??? This ought to be able to handle more than one such frame. */
10859 if (pending_autoraise_frame)
10860 {
10861 x_raise_frame (pending_autoraise_frame);
10862 pending_autoraise_frame = 0;
10863 }
10864
10865 UNBLOCK_INPUT;
10866 --handling_signal;
10867 return count;
10868 }
10869
10870
10871
10872 \f
10873 /***********************************************************************
10874 Text Cursor
10875 ***********************************************************************/
10876
10877 /* Note if the text cursor of window W has been overwritten by a
10878 drawing operation that outputs N glyphs starting at HPOS in the
10879 line given by output_cursor.vpos. N < 0 means all the rest of the
10880 line after HPOS has been written. */
10881
10882 static void
10883 note_overwritten_text_cursor (w, hpos, n)
10884 struct window *w;
10885 int hpos, n;
10886 {
10887 if (updated_area == TEXT_AREA
10888 && output_cursor.vpos == w->phys_cursor.vpos
10889 && hpos <= w->phys_cursor.hpos
10890 && (n < 0
10891 || hpos + n > w->phys_cursor.hpos))
10892 w->phys_cursor_on_p = 0;
10893 }
10894
10895
10896 /* Set clipping for output in glyph row ROW. W is the window in which
10897 we operate. GC is the graphics context to set clipping in.
10898 WHOLE_LINE_P non-zero means include the areas used for truncation
10899 mark display and alike in the clipping rectangle.
10900
10901 ROW may be a text row or, e.g., a mode line. Text rows must be
10902 clipped to the interior of the window dedicated to text display,
10903 mode lines must be clipped to the whole window. */
10904
10905 static void
10906 x_clip_to_row (w, row, gc, whole_line_p)
10907 struct window *w;
10908 struct glyph_row *row;
10909 GC gc;
10910 int whole_line_p;
10911 {
10912 struct frame *f = XFRAME (WINDOW_FRAME (w));
10913 XRectangle clip_rect;
10914 int window_x, window_y, window_width, window_height;
10915
10916 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
10917
10918 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10919 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10920 clip_rect.y = max (clip_rect.y, window_y);
10921 clip_rect.width = window_width;
10922 clip_rect.height = row->visible_height;
10923
10924 /* If clipping to the whole line, including trunc marks, extend
10925 the rectangle to the left and increase its width. */
10926 if (whole_line_p)
10927 {
10928 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10929 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
10930 }
10931
10932 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
10933 }
10934
10935
10936 /* Draw a hollow box cursor on window W in glyph row ROW. */
10937
10938 static void
10939 x_draw_hollow_cursor (w, row)
10940 struct window *w;
10941 struct glyph_row *row;
10942 {
10943 struct frame *f = XFRAME (WINDOW_FRAME (w));
10944 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10945 Display *dpy = FRAME_X_DISPLAY (f);
10946 int x, y, wd, h;
10947 XGCValues xgcv;
10948 struct glyph *cursor_glyph;
10949 GC gc;
10950
10951 /* Compute frame-relative coordinates from window-relative
10952 coordinates. */
10953 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10954 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10955 + row->ascent - w->phys_cursor_ascent);
10956 h = row->height - 1;
10957
10958 /* Get the glyph the cursor is on. If we can't tell because
10959 the current matrix is invalid or such, give up. */
10960 cursor_glyph = get_phys_cursor_glyph (w);
10961 if (cursor_glyph == NULL)
10962 return;
10963
10964 /* Compute the width of the rectangle to draw. If on a stretch
10965 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10966 rectangle as wide as the glyph, but use a canonical character
10967 width instead. */
10968 wd = cursor_glyph->pixel_width - 1;
10969 if (cursor_glyph->type == STRETCH_GLYPH
10970 && !x_stretch_cursor_p)
10971 wd = min (CANON_X_UNIT (f), wd);
10972
10973 /* The foreground of cursor_gc is typically the same as the normal
10974 background color, which can cause the cursor box to be invisible. */
10975 xgcv.foreground = f->output_data.x->cursor_pixel;
10976 if (dpyinfo->scratch_cursor_gc)
10977 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10978 else
10979 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10980 GCForeground, &xgcv);
10981 gc = dpyinfo->scratch_cursor_gc;
10982
10983 /* Set clipping, draw the rectangle, and reset clipping again. */
10984 x_clip_to_row (w, row, gc, 0);
10985 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10986 XSetClipMask (dpy, gc, None);
10987 }
10988
10989
10990 /* Draw a bar cursor on window W in glyph row ROW.
10991
10992 Implementation note: One would like to draw a bar cursor with an
10993 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10994 Unfortunately, I didn't find a font yet that has this property set.
10995 --gerd. */
10996
10997 static void
10998 x_draw_bar_cursor (w, row, width)
10999 struct window *w;
11000 struct glyph_row *row;
11001 int width;
11002 {
11003 struct frame *f = XFRAME (w->frame);
11004 struct glyph *cursor_glyph;
11005 GC gc;
11006 int x;
11007 unsigned long mask;
11008 XGCValues xgcv;
11009 Display *dpy;
11010 Window window;
11011
11012 /* If cursor is out of bounds, don't draw garbage. This can happen
11013 in mini-buffer windows when switching between echo area glyphs
11014 and mini-buffer. */
11015 cursor_glyph = get_phys_cursor_glyph (w);
11016 if (cursor_glyph == NULL)
11017 return;
11018
11019 /* If on an image, draw like a normal cursor. That's usually better
11020 visible than drawing a bar, esp. if the image is large so that
11021 the bar might not be in the window. */
11022 if (cursor_glyph->type == IMAGE_GLYPH)
11023 {
11024 struct glyph_row *row;
11025 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
11026 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
11027 }
11028 else
11029 {
11030 xgcv.background = f->output_data.x->cursor_pixel;
11031 xgcv.foreground = f->output_data.x->cursor_pixel;
11032 xgcv.graphics_exposures = 0;
11033 mask = GCForeground | GCBackground | GCGraphicsExposures;
11034 dpy = FRAME_X_DISPLAY (f);
11035 window = FRAME_X_WINDOW (f);
11036 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
11037
11038 if (gc)
11039 XChangeGC (dpy, gc, mask, &xgcv);
11040 else
11041 {
11042 gc = XCreateGC (dpy, window, mask, &xgcv);
11043 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
11044 }
11045
11046 if (width < 0)
11047 width = f->output_data.x->cursor_width;
11048
11049 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11050 x_clip_to_row (w, row, gc, 0);
11051 XFillRectangle (dpy, window, gc,
11052 x,
11053 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
11054 min (cursor_glyph->pixel_width, width),
11055 row->height);
11056 XSetClipMask (dpy, gc, None);
11057 }
11058 }
11059
11060
11061 /* Clear the cursor of window W to background color, and mark the
11062 cursor as not shown. This is used when the text where the cursor
11063 is is about to be rewritten. */
11064
11065 static void
11066 x_clear_cursor (w)
11067 struct window *w;
11068 {
11069 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
11070 x_update_window_cursor (w, 0);
11071 }
11072
11073
11074 /* Draw the cursor glyph of window W in glyph row ROW. See the
11075 comment of x_draw_glyphs for the meaning of HL. */
11076
11077 static void
11078 x_draw_phys_cursor_glyph (w, row, hl)
11079 struct window *w;
11080 struct glyph_row *row;
11081 enum draw_glyphs_face hl;
11082 {
11083 /* If cursor hpos is out of bounds, don't draw garbage. This can
11084 happen in mini-buffer windows when switching between echo area
11085 glyphs and mini-buffer. */
11086 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
11087 {
11088 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
11089 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
11090 hl, 0, 0, 0);
11091
11092 /* When we erase the cursor, and ROW is overlapped by other
11093 rows, make sure that these overlapping parts of other rows
11094 are redrawn. */
11095 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
11096 {
11097 if (row > w->current_matrix->rows
11098 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
11099 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
11100
11101 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
11102 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
11103 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
11104 }
11105 }
11106 }
11107
11108
11109 /* Erase the image of a cursor of window W from the screen. */
11110
11111 static void
11112 x_erase_phys_cursor (w)
11113 struct window *w;
11114 {
11115 struct frame *f = XFRAME (w->frame);
11116 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11117 int hpos = w->phys_cursor.hpos;
11118 int vpos = w->phys_cursor.vpos;
11119 int mouse_face_here_p = 0;
11120 struct glyph_matrix *active_glyphs = w->current_matrix;
11121 struct glyph_row *cursor_row;
11122 struct glyph *cursor_glyph;
11123 enum draw_glyphs_face hl;
11124
11125 /* No cursor displayed or row invalidated => nothing to do on the
11126 screen. */
11127 if (w->phys_cursor_type == NO_CURSOR)
11128 goto mark_cursor_off;
11129
11130 /* VPOS >= active_glyphs->nrows means that window has been resized.
11131 Don't bother to erase the cursor. */
11132 if (vpos >= active_glyphs->nrows)
11133 goto mark_cursor_off;
11134
11135 /* If row containing cursor is marked invalid, there is nothing we
11136 can do. */
11137 cursor_row = MATRIX_ROW (active_glyphs, vpos);
11138 if (!cursor_row->enabled_p)
11139 goto mark_cursor_off;
11140
11141 /* This can happen when the new row is shorter than the old one.
11142 In this case, either x_draw_glyphs or clear_end_of_line
11143 should have cleared the cursor. Note that we wouldn't be
11144 able to erase the cursor in this case because we don't have a
11145 cursor glyph at hand. */
11146 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
11147 goto mark_cursor_off;
11148
11149 /* If the cursor is in the mouse face area, redisplay that when
11150 we clear the cursor. */
11151 if (! NILP (dpyinfo->mouse_face_window)
11152 && w == XWINDOW (dpyinfo->mouse_face_window)
11153 && (vpos > dpyinfo->mouse_face_beg_row
11154 || (vpos == dpyinfo->mouse_face_beg_row
11155 && hpos >= dpyinfo->mouse_face_beg_col))
11156 && (vpos < dpyinfo->mouse_face_end_row
11157 || (vpos == dpyinfo->mouse_face_end_row
11158 && hpos < dpyinfo->mouse_face_end_col))
11159 /* Don't redraw the cursor's spot in mouse face if it is at the
11160 end of a line (on a newline). The cursor appears there, but
11161 mouse highlighting does not. */
11162 && cursor_row->used[TEXT_AREA] > hpos)
11163 mouse_face_here_p = 1;
11164
11165 /* Maybe clear the display under the cursor. */
11166 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
11167 {
11168 int x;
11169 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11170
11171 cursor_glyph = get_phys_cursor_glyph (w);
11172 if (cursor_glyph == NULL)
11173 goto mark_cursor_off;
11174
11175 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11176
11177 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11178 x,
11179 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
11180 cursor_row->y)),
11181 cursor_glyph->pixel_width,
11182 cursor_row->visible_height,
11183 False);
11184 }
11185
11186 /* Erase the cursor by redrawing the character underneath it. */
11187 if (mouse_face_here_p)
11188 hl = DRAW_MOUSE_FACE;
11189 else if (cursor_row->inverse_p)
11190 hl = DRAW_INVERSE_VIDEO;
11191 else
11192 hl = DRAW_NORMAL_TEXT;
11193 x_draw_phys_cursor_glyph (w, cursor_row, hl);
11194
11195 mark_cursor_off:
11196 w->phys_cursor_on_p = 0;
11197 w->phys_cursor_type = NO_CURSOR;
11198 }
11199
11200
11201 /* Display or clear cursor of window W. If ON is zero, clear the
11202 cursor. If it is non-zero, display the cursor. If ON is nonzero,
11203 where to put the cursor is specified by HPOS, VPOS, X and Y. */
11204
11205 void
11206 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
11207 struct window *w;
11208 int on, hpos, vpos, x, y;
11209 {
11210 struct frame *f = XFRAME (w->frame);
11211 int new_cursor_type;
11212 int new_cursor_width;
11213 struct glyph_matrix *current_glyphs;
11214 struct glyph_row *glyph_row;
11215 struct glyph *glyph;
11216
11217 /* This is pointless on invisible frames, and dangerous on garbaged
11218 windows and frames; in the latter case, the frame or window may
11219 be in the midst of changing its size, and x and y may be off the
11220 window. */
11221 if (! FRAME_VISIBLE_P (f)
11222 || FRAME_GARBAGED_P (f)
11223 || vpos >= w->current_matrix->nrows
11224 || hpos >= w->current_matrix->matrix_w)
11225 return;
11226
11227 /* If cursor is off and we want it off, return quickly. */
11228 if (!on && !w->phys_cursor_on_p)
11229 return;
11230
11231 current_glyphs = w->current_matrix;
11232 glyph_row = MATRIX_ROW (current_glyphs, vpos);
11233 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
11234
11235 /* If cursor row is not enabled, we don't really know where to
11236 display the cursor. */
11237 if (!glyph_row->enabled_p)
11238 {
11239 w->phys_cursor_on_p = 0;
11240 return;
11241 }
11242
11243 xassert (interrupt_input_blocked);
11244
11245 /* Set new_cursor_type to the cursor we want to be displayed. In a
11246 mini-buffer window, we want the cursor only to appear if we are
11247 reading input from this window. For the selected window, we want
11248 the cursor type given by the frame parameter. If explicitly
11249 marked off, draw no cursor. In all other cases, we want a hollow
11250 box cursor. */
11251 new_cursor_width = -1;
11252 if (cursor_in_echo_area
11253 && FRAME_HAS_MINIBUF_P (f)
11254 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
11255 {
11256 if (w == XWINDOW (echo_area_window))
11257 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11258 else
11259 new_cursor_type = HOLLOW_BOX_CURSOR;
11260 }
11261 else
11262 {
11263 if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
11264 || w != XWINDOW (f->selected_window))
11265 {
11266 extern int cursor_in_non_selected_windows;
11267
11268 if (MINI_WINDOW_P (w)
11269 || !cursor_in_non_selected_windows
11270 || NILP (XBUFFER (w->buffer)->cursor_type))
11271 new_cursor_type = NO_CURSOR;
11272 else
11273 new_cursor_type = HOLLOW_BOX_CURSOR;
11274 }
11275 else if (w->cursor_off_p)
11276 new_cursor_type = NO_CURSOR;
11277 else
11278 {
11279 struct buffer *b = XBUFFER (w->buffer);
11280
11281 if (EQ (b->cursor_type, Qt))
11282 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11283 else
11284 new_cursor_type = x_specified_cursor_type (b->cursor_type,
11285 &new_cursor_width);
11286 }
11287 }
11288
11289 /* If cursor is currently being shown and we don't want it to be or
11290 it is in the wrong place, or the cursor type is not what we want,
11291 erase it. */
11292 if (w->phys_cursor_on_p
11293 && (!on
11294 || w->phys_cursor.x != x
11295 || w->phys_cursor.y != y
11296 || new_cursor_type != w->phys_cursor_type))
11297 x_erase_phys_cursor (w);
11298
11299 /* If the cursor is now invisible and we want it to be visible,
11300 display it. */
11301 if (on && !w->phys_cursor_on_p)
11302 {
11303 w->phys_cursor_ascent = glyph_row->ascent;
11304 w->phys_cursor_height = glyph_row->height;
11305
11306 /* Set phys_cursor_.* before x_draw_.* is called because some
11307 of them may need the information. */
11308 w->phys_cursor.x = x;
11309 w->phys_cursor.y = glyph_row->y;
11310 w->phys_cursor.hpos = hpos;
11311 w->phys_cursor.vpos = vpos;
11312 w->phys_cursor_type = new_cursor_type;
11313 w->phys_cursor_on_p = 1;
11314
11315 switch (new_cursor_type)
11316 {
11317 case HOLLOW_BOX_CURSOR:
11318 x_draw_hollow_cursor (w, glyph_row);
11319 break;
11320
11321 case FILLED_BOX_CURSOR:
11322 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
11323 break;
11324
11325 case BAR_CURSOR:
11326 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
11327 break;
11328
11329 case NO_CURSOR:
11330 break;
11331
11332 default:
11333 abort ();
11334 }
11335
11336 #ifdef HAVE_X_I18N
11337 if (w == XWINDOW (f->selected_window))
11338 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
11339 xic_set_preeditarea (w, x, y);
11340 #endif
11341 }
11342
11343 #ifndef XFlush
11344 if (updating_frame != f)
11345 XFlush (FRAME_X_DISPLAY (f));
11346 #endif
11347 }
11348
11349
11350 /* Display the cursor on window W, or clear it. X and Y are window
11351 relative pixel coordinates. HPOS and VPOS are glyph matrix
11352 positions. If W is not the selected window, display a hollow
11353 cursor. ON non-zero means display the cursor at X, Y which
11354 correspond to HPOS, VPOS, otherwise it is cleared. */
11355
11356 void
11357 x_display_cursor (w, on, hpos, vpos, x, y)
11358 struct window *w;
11359 int on, hpos, vpos, x, y;
11360 {
11361 BLOCK_INPUT;
11362 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
11363 UNBLOCK_INPUT;
11364 }
11365
11366
11367 /* Display the cursor on window W, or clear it, according to ON_P.
11368 Don't change the cursor's position. */
11369
11370 void
11371 x_update_cursor (f, on_p)
11372 struct frame *f;
11373 {
11374 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11375 }
11376
11377
11378 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
11379 in the window tree rooted at W. */
11380
11381 static void
11382 x_update_cursor_in_window_tree (w, on_p)
11383 struct window *w;
11384 int on_p;
11385 {
11386 while (w)
11387 {
11388 if (!NILP (w->hchild))
11389 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11390 else if (!NILP (w->vchild))
11391 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11392 else
11393 x_update_window_cursor (w, on_p);
11394
11395 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11396 }
11397 }
11398
11399
11400 /* Switch the display of W's cursor on or off, according to the value
11401 of ON. */
11402
11403 static void
11404 x_update_window_cursor (w, on)
11405 struct window *w;
11406 int on;
11407 {
11408 /* Don't update cursor in windows whose frame is in the process
11409 of being deleted. */
11410 if (w->current_matrix)
11411 {
11412 BLOCK_INPUT;
11413 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11414 w->phys_cursor.x, w->phys_cursor.y);
11415 UNBLOCK_INPUT;
11416 }
11417 }
11418
11419
11420
11421 \f
11422 /* Icons. */
11423
11424 /* Refresh bitmap kitchen sink icon for frame F
11425 when we get an expose event for it. */
11426
11427 void
11428 refreshicon (f)
11429 struct frame *f;
11430 {
11431 /* Normally, the window manager handles this function. */
11432 }
11433
11434 /* Make the x-window of frame F use the gnu icon bitmap. */
11435
11436 int
11437 x_bitmap_icon (f, file)
11438 struct frame *f;
11439 Lisp_Object file;
11440 {
11441 int bitmap_id;
11442
11443 if (FRAME_X_WINDOW (f) == 0)
11444 return 1;
11445
11446 /* Free up our existing icon bitmap if any. */
11447 if (f->output_data.x->icon_bitmap > 0)
11448 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11449 f->output_data.x->icon_bitmap = 0;
11450
11451 if (STRINGP (file))
11452 bitmap_id = x_create_bitmap_from_file (f, file);
11453 else
11454 {
11455 /* Create the GNU bitmap if necessary. */
11456 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
11457 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11458 = x_create_bitmap_from_data (f, gnu_bits,
11459 gnu_width, gnu_height);
11460
11461 /* The first time we create the GNU bitmap,
11462 this increments the ref-count one extra time.
11463 As a result, the GNU bitmap is never freed.
11464 That way, we don't have to worry about allocating it again. */
11465 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
11466
11467 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
11468 }
11469
11470 x_wm_set_icon_pixmap (f, bitmap_id);
11471 f->output_data.x->icon_bitmap = bitmap_id;
11472
11473 return 0;
11474 }
11475
11476
11477 /* Make the x-window of frame F use a rectangle with text.
11478 Use ICON_NAME as the text. */
11479
11480 int
11481 x_text_icon (f, icon_name)
11482 struct frame *f;
11483 char *icon_name;
11484 {
11485 if (FRAME_X_WINDOW (f) == 0)
11486 return 1;
11487
11488 #ifdef HAVE_X11R4
11489 {
11490 XTextProperty text;
11491 text.value = (unsigned char *) icon_name;
11492 text.encoding = XA_STRING;
11493 text.format = 8;
11494 text.nitems = strlen (icon_name);
11495 #ifdef USE_X_TOOLKIT
11496 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11497 &text);
11498 #else /* not USE_X_TOOLKIT */
11499 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11500 #endif /* not USE_X_TOOLKIT */
11501 }
11502 #else /* not HAVE_X11R4 */
11503 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11504 #endif /* not HAVE_X11R4 */
11505
11506 if (f->output_data.x->icon_bitmap > 0)
11507 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11508 f->output_data.x->icon_bitmap = 0;
11509 x_wm_set_icon_pixmap (f, 0);
11510
11511 return 0;
11512 }
11513 \f
11514 #define X_ERROR_MESSAGE_SIZE 200
11515
11516 /* If non-nil, this should be a string.
11517 It means catch X errors and store the error message in this string. */
11518
11519 static Lisp_Object x_error_message_string;
11520
11521 /* An X error handler which stores the error message in
11522 x_error_message_string. This is called from x_error_handler if
11523 x_catch_errors is in effect. */
11524
11525 static void
11526 x_error_catcher (display, error)
11527 Display *display;
11528 XErrorEvent *error;
11529 {
11530 XGetErrorText (display, error->error_code,
11531 XSTRING (x_error_message_string)->data,
11532 X_ERROR_MESSAGE_SIZE);
11533 }
11534
11535 /* Begin trapping X errors for display DPY. Actually we trap X errors
11536 for all displays, but DPY should be the display you are actually
11537 operating on.
11538
11539 After calling this function, X protocol errors no longer cause
11540 Emacs to exit; instead, they are recorded in the string
11541 stored in x_error_message_string.
11542
11543 Calling x_check_errors signals an Emacs error if an X error has
11544 occurred since the last call to x_catch_errors or x_check_errors.
11545
11546 Calling x_uncatch_errors resumes the normal error handling. */
11547
11548 void x_check_errors ();
11549 static Lisp_Object x_catch_errors_unwind ();
11550
11551 int
11552 x_catch_errors (dpy)
11553 Display *dpy;
11554 {
11555 int count = specpdl_ptr - specpdl;
11556
11557 /* Make sure any errors from previous requests have been dealt with. */
11558 XSync (dpy, False);
11559
11560 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11561
11562 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11563 XSTRING (x_error_message_string)->data[0] = 0;
11564
11565 return count;
11566 }
11567
11568 /* Unbind the binding that we made to check for X errors. */
11569
11570 static Lisp_Object
11571 x_catch_errors_unwind (old_val)
11572 Lisp_Object old_val;
11573 {
11574 x_error_message_string = old_val;
11575 return Qnil;
11576 }
11577
11578 /* If any X protocol errors have arrived since the last call to
11579 x_catch_errors or x_check_errors, signal an Emacs error using
11580 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11581
11582 void
11583 x_check_errors (dpy, format)
11584 Display *dpy;
11585 char *format;
11586 {
11587 /* Make sure to catch any errors incurred so far. */
11588 XSync (dpy, False);
11589
11590 if (XSTRING (x_error_message_string)->data[0])
11591 error (format, XSTRING (x_error_message_string)->data);
11592 }
11593
11594 /* Nonzero if we had any X protocol errors
11595 since we did x_catch_errors on DPY. */
11596
11597 int
11598 x_had_errors_p (dpy)
11599 Display *dpy;
11600 {
11601 /* Make sure to catch any errors incurred so far. */
11602 XSync (dpy, False);
11603
11604 return XSTRING (x_error_message_string)->data[0] != 0;
11605 }
11606
11607 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11608
11609 void
11610 x_clear_errors (dpy)
11611 Display *dpy;
11612 {
11613 XSTRING (x_error_message_string)->data[0] = 0;
11614 }
11615
11616 /* Stop catching X protocol errors and let them make Emacs die.
11617 DPY should be the display that was passed to x_catch_errors.
11618 COUNT should be the value that was returned by
11619 the corresponding call to x_catch_errors. */
11620
11621 void
11622 x_uncatch_errors (dpy, count)
11623 Display *dpy;
11624 int count;
11625 {
11626 unbind_to (count, Qnil);
11627 }
11628
11629 #if 0
11630 static unsigned int x_wire_count;
11631 x_trace_wire ()
11632 {
11633 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11634 }
11635 #endif /* ! 0 */
11636
11637 \f
11638 /* Handle SIGPIPE, which can happen when the connection to a server
11639 simply goes away. SIGPIPE is handled by x_connection_signal.
11640 Don't need to do anything, because the write which caused the
11641 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
11642 which will do the appropriate cleanup for us. */
11643
11644 static SIGTYPE
11645 x_connection_signal (signalnum) /* If we don't have an argument, */
11646 int signalnum; /* some compilers complain in signal calls. */
11647 {
11648 #ifdef USG
11649 /* USG systems forget handlers when they are used;
11650 must reestablish each time */
11651 signal (signalnum, x_connection_signal);
11652 #endif /* USG */
11653 }
11654
11655 \f
11656 /************************************************************************
11657 Handling X errors
11658 ************************************************************************/
11659
11660 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
11661 the text of an error message that lead to the connection loss. */
11662
11663 static SIGTYPE
11664 x_connection_closed (dpy, error_message)
11665 Display *dpy;
11666 char *error_message;
11667 {
11668 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
11669 Lisp_Object frame, tail;
11670 int count;
11671 char *msg;
11672
11673 msg = (char *) alloca (strlen (error_message) + 1);
11674 strcpy (msg, error_message);
11675 handling_signal = 0;
11676
11677 /* Prevent being called recursively because of an error condition
11678 below. Otherwise, we might end up with printing ``can't find per
11679 display information'' in the recursive call instead of printing
11680 the original message here. */
11681 count = x_catch_errors (dpy);
11682
11683 /* We have to close the display to inform Xt that it doesn't
11684 exist anymore. If we don't, Xt will continue to wait for
11685 events from the display. As a consequence, a sequence of
11686
11687 M-x make-frame-on-display RET :1 RET
11688 ...kill the new frame, so that we get an IO error...
11689 M-x make-frame-on-display RET :1 RET
11690
11691 will indefinitely wait in Xt for events for display `:1', opened
11692 in the first class to make-frame-on-display.
11693
11694 Closing the display is reported to lead to a bus error on
11695 OpenWindows in certain situations. I suspect that is a bug
11696 in OpenWindows. I don't know how to cicumvent it here. */
11697
11698 #ifdef USE_X_TOOLKIT
11699 /* If DPYINFO is null, this means we didn't open the display
11700 in the first place, so don't try to close it. */
11701 if (dpyinfo)
11702 XtCloseDisplay (dpy);
11703 #endif
11704
11705 /* Indicate that this display is dead. */
11706 if (dpyinfo)
11707 dpyinfo->display = 0;
11708
11709 /* First delete frames whose mini-buffers are on frames
11710 that are on the dead display. */
11711 FOR_EACH_FRAME (tail, frame)
11712 {
11713 Lisp_Object minibuf_frame;
11714 minibuf_frame
11715 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
11716 if (FRAME_X_P (XFRAME (frame))
11717 && FRAME_X_P (XFRAME (minibuf_frame))
11718 && ! EQ (frame, minibuf_frame)
11719 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
11720 Fdelete_frame (frame, Qt);
11721 }
11722
11723 /* Now delete all remaining frames on the dead display.
11724 We are now sure none of these is used as the mini-buffer
11725 for another frame that we need to delete. */
11726 FOR_EACH_FRAME (tail, frame)
11727 if (FRAME_X_P (XFRAME (frame))
11728 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
11729 {
11730 /* Set this to t so that Fdelete_frame won't get confused
11731 trying to find a replacement. */
11732 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11733 Fdelete_frame (frame, Qt);
11734 }
11735
11736 if (dpyinfo)
11737 x_delete_display (dpyinfo);
11738
11739 x_uncatch_errors (dpy, count);
11740
11741 if (x_display_list == 0)
11742 {
11743 fprintf (stderr, "%s\n", msg);
11744 shut_down_emacs (0, 0, Qnil);
11745 exit (70);
11746 }
11747
11748 /* Ordinary stack unwind doesn't deal with these. */
11749 #ifdef SIGIO
11750 sigunblock (sigmask (SIGIO));
11751 #endif
11752 sigunblock (sigmask (SIGALRM));
11753 TOTALLY_UNBLOCK_INPUT;
11754
11755 clear_waiting_for_input ();
11756 error ("%s", msg);
11757 }
11758
11759
11760 /* This is the usual handler for X protocol errors.
11761 It kills all frames on the display that we got the error for.
11762 If that was the only one, it prints an error message and kills Emacs. */
11763
11764 static void
11765 x_error_quitter (display, error)
11766 Display *display;
11767 XErrorEvent *error;
11768 {
11769 char buf[256], buf1[356];
11770
11771 /* Note that there is no real way portable across R3/R4 to get the
11772 original error handler. */
11773
11774 XGetErrorText (display, error->error_code, buf, sizeof (buf));
11775 sprintf (buf1, "X protocol error: %s on protocol request %d",
11776 buf, error->request_code);
11777 x_connection_closed (display, buf1);
11778 }
11779
11780
11781 /* This is the first-level handler for X protocol errors.
11782 It calls x_error_quitter or x_error_catcher. */
11783
11784 static int
11785 x_error_handler (display, error)
11786 Display *display;
11787 XErrorEvent *error;
11788 {
11789 if (! NILP (x_error_message_string))
11790 x_error_catcher (display, error);
11791 else
11792 x_error_quitter (display, error);
11793 return 0;
11794 }
11795
11796 /* This is the handler for X IO errors, always.
11797 It kills all frames on the display that we lost touch with.
11798 If that was the only one, it prints an error message and kills Emacs. */
11799
11800 static int
11801 x_io_error_quitter (display)
11802 Display *display;
11803 {
11804 char buf[256];
11805
11806 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11807 x_connection_closed (display, buf);
11808 return 0;
11809 }
11810 \f
11811 /* Changing the font of the frame. */
11812
11813 /* Give frame F the font named FONTNAME as its default font, and
11814 return the full name of that font. FONTNAME may be a wildcard
11815 pattern; in that case, we choose some font that fits the pattern.
11816 The return value shows which font we chose. */
11817
11818 Lisp_Object
11819 x_new_font (f, fontname)
11820 struct frame *f;
11821 register char *fontname;
11822 {
11823 struct font_info *fontp
11824 = FS_LOAD_FONT (f, 0, fontname, -1);
11825
11826 if (!fontp)
11827 return Qnil;
11828
11829 f->output_data.x->font = (XFontStruct *) (fontp->font);
11830 f->output_data.x->baseline_offset = fontp->baseline_offset;
11831 f->output_data.x->fontset = -1;
11832
11833 /* Compute the scroll bar width in character columns. */
11834 if (f->scroll_bar_pixel_width > 0)
11835 {
11836 int wid = FONT_WIDTH (f->output_data.x->font);
11837 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11838 }
11839 else
11840 {
11841 int wid = FONT_WIDTH (f->output_data.x->font);
11842 f->scroll_bar_cols = (14 + wid - 1) / wid;
11843 }
11844
11845 /* Now make the frame display the given font. */
11846 if (FRAME_X_WINDOW (f) != 0)
11847 {
11848 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11849 f->output_data.x->font->fid);
11850 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11851 f->output_data.x->font->fid);
11852 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11853 f->output_data.x->font->fid);
11854
11855 frame_update_line_height (f);
11856
11857 /* Don't change the size of a tip frame; there's no point in
11858 doing it because it's done in Fx_show_tip, and it leads to
11859 problems because the tip frame has no widget. */
11860 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
11861 x_set_window_size (f, 0, f->width, f->height);
11862 }
11863 else
11864 /* If we are setting a new frame's font for the first time,
11865 there are no faces yet, so this font's height is the line height. */
11866 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
11867
11868 return build_string (fontp->full_name);
11869 }
11870
11871 /* Give frame F the fontset named FONTSETNAME as its default font, and
11872 return the full name of that fontset. FONTSETNAME may be a wildcard
11873 pattern; in that case, we choose some fontset that fits the pattern.
11874 The return value shows which fontset we chose. */
11875
11876 Lisp_Object
11877 x_new_fontset (f, fontsetname)
11878 struct frame *f;
11879 char *fontsetname;
11880 {
11881 int fontset = fs_query_fontset (build_string (fontsetname), 0);
11882 Lisp_Object result;
11883
11884 if (fontset < 0)
11885 return Qnil;
11886
11887 if (f->output_data.x->fontset == fontset)
11888 /* This fontset is already set in frame F. There's nothing more
11889 to do. */
11890 return fontset_name (fontset);
11891
11892 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
11893
11894 if (!STRINGP (result))
11895 /* Can't load ASCII font. */
11896 return Qnil;
11897
11898 /* Since x_new_font doesn't update any fontset information, do it now. */
11899 f->output_data.x->fontset = fontset;
11900
11901 #ifdef HAVE_X_I18N
11902 if (FRAME_XIC (f)
11903 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
11904 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
11905 #endif
11906
11907 return build_string (fontsetname);
11908 }
11909
11910 \f
11911 /***********************************************************************
11912 X Input Methods
11913 ***********************************************************************/
11914
11915 #ifdef HAVE_X_I18N
11916
11917 #ifdef HAVE_X11R6
11918
11919 /* XIM destroy callback function, which is called whenever the
11920 connection to input method XIM dies. CLIENT_DATA contains a
11921 pointer to the x_display_info structure corresponding to XIM. */
11922
11923 static void
11924 xim_destroy_callback (xim, client_data, call_data)
11925 XIM xim;
11926 XPointer client_data;
11927 XPointer call_data;
11928 {
11929 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11930 Lisp_Object frame, tail;
11931
11932 BLOCK_INPUT;
11933
11934 /* No need to call XDestroyIC.. */
11935 FOR_EACH_FRAME (tail, frame)
11936 {
11937 struct frame *f = XFRAME (frame);
11938 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11939 {
11940 FRAME_XIC (f) = NULL;
11941 if (FRAME_XIC_FONTSET (f))
11942 {
11943 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11944 FRAME_XIC_FONTSET (f) = NULL;
11945 }
11946 }
11947 }
11948
11949 /* No need to call XCloseIM. */
11950 dpyinfo->xim = NULL;
11951 XFree (dpyinfo->xim_styles);
11952 UNBLOCK_INPUT;
11953 }
11954
11955 #endif /* HAVE_X11R6 */
11956
11957 /* Open the connection to the XIM server on display DPYINFO.
11958 RESOURCE_NAME is the resource name Emacs uses. */
11959
11960 static void
11961 xim_open_dpy (dpyinfo, resource_name)
11962 struct x_display_info *dpyinfo;
11963 char *resource_name;
11964 {
11965 #ifdef USE_XIM
11966 XIM xim;
11967
11968 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11969 dpyinfo->xim = xim;
11970
11971 if (xim)
11972 {
11973 #ifdef HAVE_X11R6
11974 XIMCallback destroy;
11975 #endif
11976
11977 /* Get supported styles and XIM values. */
11978 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11979
11980 #ifdef HAVE_X11R6
11981 destroy.callback = xim_destroy_callback;
11982 destroy.client_data = (XPointer)dpyinfo;
11983 /* This isn't prptotyped in OSF 5.0. */
11984 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11985 #endif
11986 }
11987
11988 #else /* not USE_XIM */
11989 dpyinfo->xim = NULL;
11990 #endif /* not USE_XIM */
11991 }
11992
11993
11994 #ifdef HAVE_X11R6_XIM
11995
11996 struct xim_inst_t
11997 {
11998 struct x_display_info *dpyinfo;
11999 char *resource_name;
12000 };
12001
12002 /* XIM instantiate callback function, which is called whenever an XIM
12003 server is available. DISPLAY is teh display of the XIM.
12004 CLIENT_DATA contains a pointer to an xim_inst_t structure created
12005 when the callback was registered. */
12006
12007 static void
12008 xim_instantiate_callback (display, client_data, call_data)
12009 Display *display;
12010 XPointer client_data;
12011 XPointer call_data;
12012 {
12013 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
12014 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
12015
12016 /* We don't support multiple XIM connections. */
12017 if (dpyinfo->xim)
12018 return;
12019
12020 xim_open_dpy (dpyinfo, xim_inst->resource_name);
12021
12022 /* Create XIC for the existing frames on the same display, as long
12023 as they have no XIC. */
12024 if (dpyinfo->xim && dpyinfo->reference_count > 0)
12025 {
12026 Lisp_Object tail, frame;
12027
12028 BLOCK_INPUT;
12029 FOR_EACH_FRAME (tail, frame)
12030 {
12031 struct frame *f = XFRAME (frame);
12032
12033 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
12034 if (FRAME_XIC (f) == NULL)
12035 {
12036 create_frame_xic (f);
12037 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
12038 xic_set_statusarea (f);
12039 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
12040 {
12041 struct window *w = XWINDOW (f->selected_window);
12042 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
12043 }
12044 }
12045 }
12046
12047 UNBLOCK_INPUT;
12048 }
12049 }
12050
12051 #endif /* HAVE_X11R6_XIM */
12052
12053
12054 /* Open a connection to the XIM server on display DPYINFO.
12055 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
12056 connection only at the first time. On X11R6, open the connection
12057 in the XIM instantiate callback function. */
12058
12059 static void
12060 xim_initialize (dpyinfo, resource_name)
12061 struct x_display_info *dpyinfo;
12062 char *resource_name;
12063 {
12064 #ifdef USE_XIM
12065 #ifdef HAVE_X11R6_XIM
12066 struct xim_inst_t *xim_inst;
12067 int len;
12068
12069 dpyinfo->xim = NULL;
12070 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
12071 xim_inst->dpyinfo = dpyinfo;
12072 len = strlen (resource_name);
12073 xim_inst->resource_name = (char *) xmalloc (len + 1);
12074 bcopy (resource_name, xim_inst->resource_name, len + 1);
12075 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12076 resource_name, EMACS_CLASS,
12077 xim_instantiate_callback,
12078 /* Fixme: This is XPointer in
12079 XFree86 but (XPointer *) on
12080 Tru64, at least. */
12081 (XPointer) xim_inst);
12082 #else /* not HAVE_X11R6_XIM */
12083 dpyinfo->xim = NULL;
12084 xim_open_dpy (dpyinfo, resource_name);
12085 #endif /* not HAVE_X11R6_XIM */
12086
12087 #else /* not USE_XIM */
12088 dpyinfo->xim = NULL;
12089 #endif /* not USE_XIM */
12090 }
12091
12092
12093 /* Close the connection to the XIM server on display DPYINFO. */
12094
12095 static void
12096 xim_close_dpy (dpyinfo)
12097 struct x_display_info *dpyinfo;
12098 {
12099 #ifdef USE_XIM
12100 #ifdef HAVE_X11R6_XIM
12101 if (dpyinfo->display)
12102 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12103 NULL, EMACS_CLASS,
12104 xim_instantiate_callback, NULL);
12105 #endif /* not HAVE_X11R6_XIM */
12106 if (dpyinfo->display)
12107 XCloseIM (dpyinfo->xim);
12108 dpyinfo->xim = NULL;
12109 XFree (dpyinfo->xim_styles);
12110 #endif /* USE_XIM */
12111 }
12112
12113 #endif /* not HAVE_X11R6_XIM */
12114
12115
12116 \f
12117 /* Calculate the absolute position in frame F
12118 from its current recorded position values and gravity. */
12119
12120 void
12121 x_calc_absolute_position (f)
12122 struct frame *f;
12123 {
12124 Window child;
12125 int win_x = 0, win_y = 0;
12126 int flags = f->output_data.x->size_hint_flags;
12127 int this_window;
12128
12129 /* We have nothing to do if the current position
12130 is already for the top-left corner. */
12131 if (! ((flags & XNegative) || (flags & YNegative)))
12132 return;
12133
12134 #ifdef USE_X_TOOLKIT
12135 this_window = XtWindow (f->output_data.x->widget);
12136 #else
12137 this_window = FRAME_X_WINDOW (f);
12138 #endif
12139
12140 /* Find the position of the outside upper-left corner of
12141 the inner window, with respect to the outer window.
12142 But do this only if we will need the results. */
12143 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
12144 {
12145 int count;
12146
12147 BLOCK_INPUT;
12148 count = x_catch_errors (FRAME_X_DISPLAY (f));
12149 while (1)
12150 {
12151 x_clear_errors (FRAME_X_DISPLAY (f));
12152 XTranslateCoordinates (FRAME_X_DISPLAY (f),
12153
12154 /* From-window, to-window. */
12155 this_window,
12156 f->output_data.x->parent_desc,
12157
12158 /* From-position, to-position. */
12159 0, 0, &win_x, &win_y,
12160
12161 /* Child of win. */
12162 &child);
12163 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12164 {
12165 Window newroot, newparent = 0xdeadbeef;
12166 Window *newchildren;
12167 unsigned int nchildren;
12168
12169 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
12170 &newparent, &newchildren, &nchildren))
12171 break;
12172
12173 XFree ((char *) newchildren);
12174
12175 f->output_data.x->parent_desc = newparent;
12176 }
12177 else
12178 break;
12179 }
12180
12181 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12182 UNBLOCK_INPUT;
12183 }
12184
12185 /* Treat negative positions as relative to the leftmost bottommost
12186 position that fits on the screen. */
12187 if (flags & XNegative)
12188 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
12189 - 2 * f->output_data.x->border_width - win_x
12190 - PIXEL_WIDTH (f)
12191 + f->output_data.x->left_pos);
12192
12193 {
12194 int height = PIXEL_HEIGHT (f);
12195
12196 #if defined USE_X_TOOLKIT && defined USE_MOTIF
12197 /* Something is fishy here. When using Motif, starting Emacs with
12198 `-g -0-0', the frame appears too low by a few pixels.
12199
12200 This seems to be so because initially, while Emacs is starting,
12201 the column widget's height and the frame's pixel height are
12202 different. The column widget's height is the right one. In
12203 later invocations, when Emacs is up, the frame's pixel height
12204 is right, though.
12205
12206 It's not obvious where the initial small difference comes from.
12207 2000-12-01, gerd. */
12208
12209 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
12210 #endif
12211
12212 if (flags & YNegative)
12213 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
12214 - 2 * f->output_data.x->border_width
12215 - win_y
12216 - height
12217 + f->output_data.x->top_pos);
12218 }
12219
12220 /* The left_pos and top_pos
12221 are now relative to the top and left screen edges,
12222 so the flags should correspond. */
12223 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12224 }
12225
12226 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
12227 to really change the position, and 0 when calling from
12228 x_make_frame_visible (in that case, XOFF and YOFF are the current
12229 position values). It is -1 when calling from x_set_frame_parameters,
12230 which means, do adjust for borders but don't change the gravity. */
12231
12232 void
12233 x_set_offset (f, xoff, yoff, change_gravity)
12234 struct frame *f;
12235 register int xoff, yoff;
12236 int change_gravity;
12237 {
12238 int modified_top, modified_left;
12239
12240 if (change_gravity > 0)
12241 {
12242 f->output_data.x->top_pos = yoff;
12243 f->output_data.x->left_pos = xoff;
12244 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12245 if (xoff < 0)
12246 f->output_data.x->size_hint_flags |= XNegative;
12247 if (yoff < 0)
12248 f->output_data.x->size_hint_flags |= YNegative;
12249 f->output_data.x->win_gravity = NorthWestGravity;
12250 }
12251 x_calc_absolute_position (f);
12252
12253 BLOCK_INPUT;
12254 x_wm_set_size_hint (f, (long) 0, 0);
12255
12256 modified_left = f->output_data.x->left_pos;
12257 modified_top = f->output_data.x->top_pos;
12258 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
12259 this seems to be unnecessary and incorrect. rms, 4/17/97. */
12260 /* It is a mystery why we need to add the border_width here
12261 when the frame is already visible, but experiment says we do. */
12262 if (change_gravity != 0)
12263 {
12264 modified_left += f->output_data.x->border_width;
12265 modified_top += f->output_data.x->border_width;
12266 }
12267 #endif
12268
12269 #ifdef USE_X_TOOLKIT
12270 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
12271 modified_left, modified_top);
12272 #else /* not USE_X_TOOLKIT */
12273 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12274 modified_left, modified_top);
12275 #endif /* not USE_X_TOOLKIT */
12276 UNBLOCK_INPUT;
12277 }
12278
12279
12280 /* Change the size of frame F's X window to COLS/ROWS in the case F
12281 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
12282 top-left-corner window gravity for this size change and subsequent
12283 size changes. Otherwise we leave the window gravity unchanged. */
12284
12285 static void
12286 x_set_window_size_1 (f, change_gravity, cols, rows)
12287 struct frame *f;
12288 int change_gravity;
12289 int cols, rows;
12290 {
12291 int pixelwidth, pixelheight;
12292
12293 check_frame_size (f, &rows, &cols);
12294 f->output_data.x->vertical_scroll_bar_extra
12295 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
12296 ? 0
12297 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
12298 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
12299 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
12300 f->output_data.x->flags_areas_extra
12301 = FRAME_FLAGS_AREA_WIDTH (f);
12302 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
12303 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
12304
12305 f->output_data.x->win_gravity = NorthWestGravity;
12306 x_wm_set_size_hint (f, (long) 0, 0);
12307
12308 XSync (FRAME_X_DISPLAY (f), False);
12309 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12310 pixelwidth, pixelheight);
12311
12312 /* Now, strictly speaking, we can't be sure that this is accurate,
12313 but the window manager will get around to dealing with the size
12314 change request eventually, and we'll hear how it went when the
12315 ConfigureNotify event gets here.
12316
12317 We could just not bother storing any of this information here,
12318 and let the ConfigureNotify event set everything up, but that
12319 might be kind of confusing to the Lisp code, since size changes
12320 wouldn't be reported in the frame parameters until some random
12321 point in the future when the ConfigureNotify event arrives.
12322
12323 We pass 1 for DELAY since we can't run Lisp code inside of
12324 a BLOCK_INPUT. */
12325 change_frame_size (f, rows, cols, 0, 1, 0);
12326 PIXEL_WIDTH (f) = pixelwidth;
12327 PIXEL_HEIGHT (f) = pixelheight;
12328
12329 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
12330 receive in the ConfigureNotify event; if we get what we asked
12331 for, then the event won't cause the screen to become garbaged, so
12332 we have to make sure to do it here. */
12333 SET_FRAME_GARBAGED (f);
12334
12335 XFlush (FRAME_X_DISPLAY (f));
12336 }
12337
12338
12339 /* Call this to change the size of frame F's x-window.
12340 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
12341 for this size change and subsequent size changes.
12342 Otherwise we leave the window gravity unchanged. */
12343
12344 void
12345 x_set_window_size (f, change_gravity, cols, rows)
12346 struct frame *f;
12347 int change_gravity;
12348 int cols, rows;
12349 {
12350 BLOCK_INPUT;
12351
12352 #ifdef USE_X_TOOLKIT
12353
12354 if (f->output_data.x->widget != NULL)
12355 {
12356 /* The x and y position of the widget is clobbered by the
12357 call to XtSetValues within EmacsFrameSetCharSize.
12358 This is a real kludge, but I don't understand Xt so I can't
12359 figure out a correct fix. Can anyone else tell me? -- rms. */
12360 int xpos = f->output_data.x->widget->core.x;
12361 int ypos = f->output_data.x->widget->core.y;
12362 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
12363 f->output_data.x->widget->core.x = xpos;
12364 f->output_data.x->widget->core.y = ypos;
12365 }
12366 else
12367 x_set_window_size_1 (f, change_gravity, cols, rows);
12368
12369 #else /* not USE_X_TOOLKIT */
12370
12371 x_set_window_size_1 (f, change_gravity, cols, rows);
12372
12373 #endif /* not USE_X_TOOLKIT */
12374
12375 /* If cursor was outside the new size, mark it as off. */
12376 mark_window_cursors_off (XWINDOW (f->root_window));
12377
12378 /* Clear out any recollection of where the mouse highlighting was,
12379 since it might be in a place that's outside the new frame size.
12380 Actually checking whether it is outside is a pain in the neck,
12381 so don't try--just let the highlighting be done afresh with new size. */
12382 cancel_mouse_face (f);
12383
12384 UNBLOCK_INPUT;
12385 }
12386 \f
12387 /* Mouse warping. */
12388
12389 void
12390 x_set_mouse_position (f, x, y)
12391 struct frame *f;
12392 int x, y;
12393 {
12394 int pix_x, pix_y;
12395
12396 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
12397 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
12398
12399 if (pix_x < 0) pix_x = 0;
12400 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
12401
12402 if (pix_y < 0) pix_y = 0;
12403 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
12404
12405 BLOCK_INPUT;
12406
12407 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12408 0, 0, 0, 0, pix_x, pix_y);
12409 UNBLOCK_INPUT;
12410 }
12411
12412 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
12413
12414 void
12415 x_set_mouse_pixel_position (f, pix_x, pix_y)
12416 struct frame *f;
12417 int pix_x, pix_y;
12418 {
12419 BLOCK_INPUT;
12420
12421 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12422 0, 0, 0, 0, pix_x, pix_y);
12423 UNBLOCK_INPUT;
12424 }
12425 \f
12426 /* focus shifting, raising and lowering. */
12427
12428 void
12429 x_focus_on_frame (f)
12430 struct frame *f;
12431 {
12432 #if 0 /* This proves to be unpleasant. */
12433 x_raise_frame (f);
12434 #endif
12435 #if 0
12436 /* I don't think that the ICCCM allows programs to do things like this
12437 without the interaction of the window manager. Whatever you end up
12438 doing with this code, do it to x_unfocus_frame too. */
12439 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12440 RevertToPointerRoot, CurrentTime);
12441 #endif /* ! 0 */
12442 }
12443
12444 void
12445 x_unfocus_frame (f)
12446 struct frame *f;
12447 {
12448 #if 0
12449 /* Look at the remarks in x_focus_on_frame. */
12450 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
12451 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
12452 RevertToPointerRoot, CurrentTime);
12453 #endif /* ! 0 */
12454 }
12455
12456 /* Raise frame F. */
12457
12458 void
12459 x_raise_frame (f)
12460 struct frame *f;
12461 {
12462 if (f->async_visible)
12463 {
12464 BLOCK_INPUT;
12465 #ifdef USE_X_TOOLKIT
12466 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12467 #else /* not USE_X_TOOLKIT */
12468 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12469 #endif /* not USE_X_TOOLKIT */
12470 XFlush (FRAME_X_DISPLAY (f));
12471 UNBLOCK_INPUT;
12472 }
12473 }
12474
12475 /* Lower frame F. */
12476
12477 void
12478 x_lower_frame (f)
12479 struct frame *f;
12480 {
12481 if (f->async_visible)
12482 {
12483 BLOCK_INPUT;
12484 #ifdef USE_X_TOOLKIT
12485 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12486 #else /* not USE_X_TOOLKIT */
12487 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12488 #endif /* not USE_X_TOOLKIT */
12489 XFlush (FRAME_X_DISPLAY (f));
12490 UNBLOCK_INPUT;
12491 }
12492 }
12493
12494 static void
12495 XTframe_raise_lower (f, raise_flag)
12496 FRAME_PTR f;
12497 int raise_flag;
12498 {
12499 if (raise_flag)
12500 x_raise_frame (f);
12501 else
12502 x_lower_frame (f);
12503 }
12504 \f
12505 /* Change of visibility. */
12506
12507 /* This tries to wait until the frame is really visible.
12508 However, if the window manager asks the user where to position
12509 the frame, this will return before the user finishes doing that.
12510 The frame will not actually be visible at that time,
12511 but it will become visible later when the window manager
12512 finishes with it. */
12513
12514 void
12515 x_make_frame_visible (f)
12516 struct frame *f;
12517 {
12518 Lisp_Object type;
12519 int original_top, original_left;
12520 int retry_count = 2;
12521
12522 retry:
12523
12524 BLOCK_INPUT;
12525
12526 type = x_icon_type (f);
12527 if (!NILP (type))
12528 x_bitmap_icon (f, type);
12529
12530 if (! FRAME_VISIBLE_P (f))
12531 {
12532 /* We test FRAME_GARBAGED_P here to make sure we don't
12533 call x_set_offset a second time
12534 if we get to x_make_frame_visible a second time
12535 before the window gets really visible. */
12536 if (! FRAME_ICONIFIED_P (f)
12537 && ! f->output_data.x->asked_for_visible)
12538 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12539
12540 f->output_data.x->asked_for_visible = 1;
12541
12542 if (! EQ (Vx_no_window_manager, Qt))
12543 x_wm_set_window_state (f, NormalState);
12544 #ifdef USE_X_TOOLKIT
12545 /* This was XtPopup, but that did nothing for an iconified frame. */
12546 XtMapWidget (f->output_data.x->widget);
12547 #else /* not USE_X_TOOLKIT */
12548 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12549 #endif /* not USE_X_TOOLKIT */
12550 #if 0 /* This seems to bring back scroll bars in the wrong places
12551 if the window configuration has changed. They seem
12552 to come back ok without this. */
12553 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
12554 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12555 #endif
12556 }
12557
12558 XFlush (FRAME_X_DISPLAY (f));
12559
12560 /* Synchronize to ensure Emacs knows the frame is visible
12561 before we do anything else. We do this loop with input not blocked
12562 so that incoming events are handled. */
12563 {
12564 Lisp_Object frame;
12565 int count;
12566 /* This must be before UNBLOCK_INPUT
12567 since events that arrive in response to the actions above
12568 will set it when they are handled. */
12569 int previously_visible = f->output_data.x->has_been_visible;
12570
12571 original_left = f->output_data.x->left_pos;
12572 original_top = f->output_data.x->top_pos;
12573
12574 /* This must come after we set COUNT. */
12575 UNBLOCK_INPUT;
12576
12577 /* We unblock here so that arriving X events are processed. */
12578
12579 /* Now move the window back to where it was "supposed to be".
12580 But don't do it if the gravity is negative.
12581 When the gravity is negative, this uses a position
12582 that is 3 pixels too low. Perhaps that's really the border width.
12583
12584 Don't do this if the window has never been visible before,
12585 because the window manager may choose the position
12586 and we don't want to override it. */
12587
12588 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
12589 && f->output_data.x->win_gravity == NorthWestGravity
12590 && previously_visible)
12591 {
12592 Drawable rootw;
12593 int x, y;
12594 unsigned int width, height, border, depth;
12595
12596 BLOCK_INPUT;
12597
12598 /* On some window managers (such as FVWM) moving an existing
12599 window, even to the same place, causes the window manager
12600 to introduce an offset. This can cause the window to move
12601 to an unexpected location. Check the geometry (a little
12602 slow here) and then verify that the window is in the right
12603 place. If the window is not in the right place, move it
12604 there, and take the potential window manager hit. */
12605 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12606 &rootw, &x, &y, &width, &height, &border, &depth);
12607
12608 if (original_left != x || original_top != y)
12609 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12610 original_left, original_top);
12611
12612 UNBLOCK_INPUT;
12613 }
12614
12615 XSETFRAME (frame, f);
12616
12617 /* Wait until the frame is visible. Process X events until a
12618 MapNotify event has been seen, or until we think we won't get a
12619 MapNotify at all.. */
12620 for (count = input_signal_count + 10;
12621 input_signal_count < count && !FRAME_VISIBLE_P (f);)
12622 {
12623 /* Force processing of queued events. */
12624 x_sync (f);
12625
12626 /* Machines that do polling rather than SIGIO have been
12627 observed to go into a busy-wait here. So we'll fake an
12628 alarm signal to let the handler know that there's something
12629 to be read. We used to raise a real alarm, but it seems
12630 that the handler isn't always enabled here. This is
12631 probably a bug. */
12632 if (input_polling_used ())
12633 {
12634 /* It could be confusing if a real alarm arrives while
12635 processing the fake one. Turn it off and let the
12636 handler reset it. */
12637 extern void poll_for_input_1 P_ ((void));
12638 int old_poll_suppress_count = poll_suppress_count;
12639 poll_suppress_count = 1;
12640 poll_for_input_1 ();
12641 poll_suppress_count = old_poll_suppress_count;
12642 }
12643
12644 /* See if a MapNotify event has been processed. */
12645 FRAME_SAMPLE_VISIBILITY (f);
12646 }
12647
12648 /* 2000-09-28: In
12649
12650 (let ((f (selected-frame)))
12651 (iconify-frame f)
12652 (raise-frame f))
12653
12654 the frame is not raised with various window managers on
12655 FreeBSD, Linux and Solaris. It turns out that, for some
12656 unknown reason, the call to XtMapWidget is completely ignored.
12657 Mapping the widget a second time works. */
12658
12659 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
12660 goto retry;
12661 }
12662 }
12663
12664 /* Change from mapped state to withdrawn state. */
12665
12666 /* Make the frame visible (mapped and not iconified). */
12667
12668 void
12669 x_make_frame_invisible (f)
12670 struct frame *f;
12671 {
12672 Window window;
12673
12674 #ifdef USE_X_TOOLKIT
12675 /* Use the frame's outermost window, not the one we normally draw on. */
12676 window = XtWindow (f->output_data.x->widget);
12677 #else /* not USE_X_TOOLKIT */
12678 window = FRAME_X_WINDOW (f);
12679 #endif /* not USE_X_TOOLKIT */
12680
12681 /* Don't keep the highlight on an invisible frame. */
12682 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12683 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12684
12685 #if 0/* This might add unreliability; I don't trust it -- rms. */
12686 if (! f->async_visible && ! f->async_iconified)
12687 return;
12688 #endif
12689
12690 BLOCK_INPUT;
12691
12692 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
12693 that the current position of the window is user-specified, rather than
12694 program-specified, so that when the window is mapped again, it will be
12695 placed at the same location, without forcing the user to position it
12696 by hand again (they have already done that once for this window.) */
12697 x_wm_set_size_hint (f, (long) 0, 1);
12698
12699 #ifdef HAVE_X11R4
12700
12701 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
12702 DefaultScreen (FRAME_X_DISPLAY (f))))
12703 {
12704 UNBLOCK_INPUT_RESIGNAL;
12705 error ("Can't notify window manager of window withdrawal");
12706 }
12707 #else /* ! defined (HAVE_X11R4) */
12708
12709 /* Tell the window manager what we're going to do. */
12710 if (! EQ (Vx_no_window_manager, Qt))
12711 {
12712 XEvent unmap;
12713
12714 unmap.xunmap.type = UnmapNotify;
12715 unmap.xunmap.window = window;
12716 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
12717 unmap.xunmap.from_configure = False;
12718 if (! XSendEvent (FRAME_X_DISPLAY (f),
12719 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12720 False,
12721 SubstructureRedirectMaskSubstructureNotifyMask,
12722 &unmap))
12723 {
12724 UNBLOCK_INPUT_RESIGNAL;
12725 error ("Can't notify window manager of withdrawal");
12726 }
12727 }
12728
12729 /* Unmap the window ourselves. Cheeky! */
12730 XUnmapWindow (FRAME_X_DISPLAY (f), window);
12731 #endif /* ! defined (HAVE_X11R4) */
12732
12733 /* We can't distinguish this from iconification
12734 just by the event that we get from the server.
12735 So we can't win using the usual strategy of letting
12736 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
12737 and synchronize with the server to make sure we agree. */
12738 f->visible = 0;
12739 FRAME_ICONIFIED_P (f) = 0;
12740 f->async_visible = 0;
12741 f->async_iconified = 0;
12742
12743 x_sync (f);
12744
12745 UNBLOCK_INPUT;
12746 }
12747
12748 /* Change window state from mapped to iconified. */
12749
12750 void
12751 x_iconify_frame (f)
12752 struct frame *f;
12753 {
12754 int result;
12755 Lisp_Object type;
12756
12757 /* Don't keep the highlight on an invisible frame. */
12758 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12759 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12760
12761 if (f->async_iconified)
12762 return;
12763
12764 BLOCK_INPUT;
12765
12766 FRAME_SAMPLE_VISIBILITY (f);
12767
12768 type = x_icon_type (f);
12769 if (!NILP (type))
12770 x_bitmap_icon (f, type);
12771
12772 #ifdef USE_X_TOOLKIT
12773
12774 if (! FRAME_VISIBLE_P (f))
12775 {
12776 if (! EQ (Vx_no_window_manager, Qt))
12777 x_wm_set_window_state (f, IconicState);
12778 /* This was XtPopup, but that did nothing for an iconified frame. */
12779 XtMapWidget (f->output_data.x->widget);
12780 /* The server won't give us any event to indicate
12781 that an invisible frame was changed to an icon,
12782 so we have to record it here. */
12783 f->iconified = 1;
12784 f->visible = 1;
12785 f->async_iconified = 1;
12786 f->async_visible = 0;
12787 UNBLOCK_INPUT;
12788 return;
12789 }
12790
12791 result = XIconifyWindow (FRAME_X_DISPLAY (f),
12792 XtWindow (f->output_data.x->widget),
12793 DefaultScreen (FRAME_X_DISPLAY (f)));
12794 UNBLOCK_INPUT;
12795
12796 if (!result)
12797 error ("Can't notify window manager of iconification");
12798
12799 f->async_iconified = 1;
12800 f->async_visible = 0;
12801
12802
12803 BLOCK_INPUT;
12804 XFlush (FRAME_X_DISPLAY (f));
12805 UNBLOCK_INPUT;
12806 #else /* not USE_X_TOOLKIT */
12807
12808 /* Make sure the X server knows where the window should be positioned,
12809 in case the user deiconifies with the window manager. */
12810 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
12811 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12812
12813 /* Since we don't know which revision of X we're running, we'll use both
12814 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
12815
12816 /* X11R4: send a ClientMessage to the window manager using the
12817 WM_CHANGE_STATE type. */
12818 {
12819 XEvent message;
12820
12821 message.xclient.window = FRAME_X_WINDOW (f);
12822 message.xclient.type = ClientMessage;
12823 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
12824 message.xclient.format = 32;
12825 message.xclient.data.l[0] = IconicState;
12826
12827 if (! XSendEvent (FRAME_X_DISPLAY (f),
12828 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12829 False,
12830 SubstructureRedirectMask | SubstructureNotifyMask,
12831 &message))
12832 {
12833 UNBLOCK_INPUT_RESIGNAL;
12834 error ("Can't notify window manager of iconification");
12835 }
12836 }
12837
12838 /* X11R3: set the initial_state field of the window manager hints to
12839 IconicState. */
12840 x_wm_set_window_state (f, IconicState);
12841
12842 if (!FRAME_VISIBLE_P (f))
12843 {
12844 /* If the frame was withdrawn, before, we must map it. */
12845 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12846 }
12847
12848 f->async_iconified = 1;
12849 f->async_visible = 0;
12850
12851 XFlush (FRAME_X_DISPLAY (f));
12852 UNBLOCK_INPUT;
12853 #endif /* not USE_X_TOOLKIT */
12854 }
12855
12856 \f
12857 /* Free X resources of frame F. */
12858
12859 void
12860 x_free_frame_resources (f)
12861 struct frame *f;
12862 {
12863 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12864
12865 BLOCK_INPUT;
12866
12867 /* If a display connection is dead, don't try sending more
12868 commands to the X server. */
12869 if (dpyinfo->display)
12870 {
12871 if (f->output_data.x->icon_desc)
12872 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
12873
12874 #ifdef HAVE_X_I18N
12875 if (FRAME_XIC (f))
12876 free_frame_xic (f);
12877 #endif
12878
12879 if (FRAME_X_WINDOW (f))
12880 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12881
12882 #ifdef USE_X_TOOLKIT
12883 if (f->output_data.x->widget)
12884 {
12885 XtDestroyWidget (f->output_data.x->widget);
12886 f->output_data.x->widget = NULL;
12887 }
12888 free_frame_menubar (f);
12889 #endif /* USE_X_TOOLKIT */
12890
12891 unload_color (f, f->output_data.x->foreground_pixel);
12892 unload_color (f, f->output_data.x->background_pixel);
12893 unload_color (f, f->output_data.x->cursor_pixel);
12894 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12895 unload_color (f, f->output_data.x->border_pixel);
12896 unload_color (f, f->output_data.x->mouse_pixel);
12897
12898 if (f->output_data.x->scroll_bar_background_pixel != -1)
12899 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
12900 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
12901 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
12902 if (f->output_data.x->white_relief.allocated_p)
12903 unload_color (f, f->output_data.x->white_relief.pixel);
12904 if (f->output_data.x->black_relief.allocated_p)
12905 unload_color (f, f->output_data.x->black_relief.pixel);
12906
12907 if (FRAME_FACE_CACHE (f))
12908 free_frame_faces (f);
12909
12910 x_free_gcs (f);
12911 XFlush (FRAME_X_DISPLAY (f));
12912 }
12913
12914 if (f->output_data.x->saved_menu_event)
12915 xfree (f->output_data.x->saved_menu_event);
12916
12917 xfree (f->output_data.x);
12918 f->output_data.x = NULL;
12919
12920 if (f == dpyinfo->x_focus_frame)
12921 dpyinfo->x_focus_frame = 0;
12922 if (f == dpyinfo->x_focus_event_frame)
12923 dpyinfo->x_focus_event_frame = 0;
12924 if (f == dpyinfo->x_highlight_frame)
12925 dpyinfo->x_highlight_frame = 0;
12926
12927 if (f == dpyinfo->mouse_face_mouse_frame)
12928 {
12929 dpyinfo->mouse_face_beg_row
12930 = dpyinfo->mouse_face_beg_col = -1;
12931 dpyinfo->mouse_face_end_row
12932 = dpyinfo->mouse_face_end_col = -1;
12933 dpyinfo->mouse_face_window = Qnil;
12934 dpyinfo->mouse_face_deferred_gc = 0;
12935 dpyinfo->mouse_face_mouse_frame = 0;
12936 }
12937
12938 UNBLOCK_INPUT;
12939 }
12940
12941
12942 /* Destroy the X window of frame F. */
12943
12944 void
12945 x_destroy_window (f)
12946 struct frame *f;
12947 {
12948 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12949
12950 /* If a display connection is dead, don't try sending more
12951 commands to the X server. */
12952 if (dpyinfo->display != 0)
12953 x_free_frame_resources (f);
12954
12955 dpyinfo->reference_count--;
12956 }
12957
12958 \f
12959 /* Setting window manager hints. */
12960
12961 /* Set the normal size hints for the window manager, for frame F.
12962 FLAGS is the flags word to use--or 0 meaning preserve the flags
12963 that the window now has.
12964 If USER_POSITION is nonzero, we set the USPosition
12965 flag (this is useful when FLAGS is 0). */
12966
12967 void
12968 x_wm_set_size_hint (f, flags, user_position)
12969 struct frame *f;
12970 long flags;
12971 int user_position;
12972 {
12973 XSizeHints size_hints;
12974
12975 #ifdef USE_X_TOOLKIT
12976 Arg al[2];
12977 int ac = 0;
12978 Dimension widget_width, widget_height;
12979 Window window = XtWindow (f->output_data.x->widget);
12980 #else /* not USE_X_TOOLKIT */
12981 Window window = FRAME_X_WINDOW (f);
12982 #endif /* not USE_X_TOOLKIT */
12983
12984 /* Setting PMaxSize caused various problems. */
12985 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
12986
12987 size_hints.x = f->output_data.x->left_pos;
12988 size_hints.y = f->output_data.x->top_pos;
12989
12990 #ifdef USE_X_TOOLKIT
12991 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12992 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
12993 XtGetValues (f->output_data.x->widget, al, ac);
12994 size_hints.height = widget_height;
12995 size_hints.width = widget_width;
12996 #else /* not USE_X_TOOLKIT */
12997 size_hints.height = PIXEL_HEIGHT (f);
12998 size_hints.width = PIXEL_WIDTH (f);
12999 #endif /* not USE_X_TOOLKIT */
13000
13001 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
13002 size_hints.height_inc = f->output_data.x->line_height;
13003 size_hints.max_width
13004 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
13005 size_hints.max_height
13006 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
13007
13008 /* Calculate the base and minimum sizes.
13009
13010 (When we use the X toolkit, we don't do it here.
13011 Instead we copy the values that the widgets are using, below.) */
13012 #ifndef USE_X_TOOLKIT
13013 {
13014 int base_width, base_height;
13015 int min_rows = 0, min_cols = 0;
13016
13017 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
13018 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
13019
13020 check_frame_size (f, &min_rows, &min_cols);
13021
13022 /* The window manager uses the base width hints to calculate the
13023 current number of rows and columns in the frame while
13024 resizing; min_width and min_height aren't useful for this
13025 purpose, since they might not give the dimensions for a
13026 zero-row, zero-column frame.
13027
13028 We use the base_width and base_height members if we have
13029 them; otherwise, we set the min_width and min_height members
13030 to the size for a zero x zero frame. */
13031
13032 #ifdef HAVE_X11R4
13033 size_hints.flags |= PBaseSize;
13034 size_hints.base_width = base_width;
13035 size_hints.base_height = base_height;
13036 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
13037 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
13038 #else
13039 size_hints.min_width = base_width;
13040 size_hints.min_height = base_height;
13041 #endif
13042 }
13043
13044 /* If we don't need the old flags, we don't need the old hint at all. */
13045 if (flags)
13046 {
13047 size_hints.flags |= flags;
13048 goto no_read;
13049 }
13050 #endif /* not USE_X_TOOLKIT */
13051
13052 {
13053 XSizeHints hints; /* Sometimes I hate X Windows... */
13054 long supplied_return;
13055 int value;
13056
13057 #ifdef HAVE_X11R4
13058 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
13059 &supplied_return);
13060 #else
13061 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
13062 #endif
13063
13064 #ifdef USE_X_TOOLKIT
13065 size_hints.base_height = hints.base_height;
13066 size_hints.base_width = hints.base_width;
13067 size_hints.min_height = hints.min_height;
13068 size_hints.min_width = hints.min_width;
13069 #endif
13070
13071 if (flags)
13072 size_hints.flags |= flags;
13073 else
13074 {
13075 if (value == 0)
13076 hints.flags = 0;
13077 if (hints.flags & PSize)
13078 size_hints.flags |= PSize;
13079 if (hints.flags & PPosition)
13080 size_hints.flags |= PPosition;
13081 if (hints.flags & USPosition)
13082 size_hints.flags |= USPosition;
13083 if (hints.flags & USSize)
13084 size_hints.flags |= USSize;
13085 }
13086 }
13087
13088 #ifndef USE_X_TOOLKIT
13089 no_read:
13090 #endif
13091
13092 #ifdef PWinGravity
13093 size_hints.win_gravity = f->output_data.x->win_gravity;
13094 size_hints.flags |= PWinGravity;
13095
13096 if (user_position)
13097 {
13098 size_hints.flags &= ~ PPosition;
13099 size_hints.flags |= USPosition;
13100 }
13101 #endif /* PWinGravity */
13102
13103 #ifdef HAVE_X11R4
13104 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13105 #else
13106 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13107 #endif
13108 }
13109
13110 /* Used for IconicState or NormalState */
13111
13112 void
13113 x_wm_set_window_state (f, state)
13114 struct frame *f;
13115 int state;
13116 {
13117 #ifdef USE_X_TOOLKIT
13118 Arg al[1];
13119
13120 XtSetArg (al[0], XtNinitialState, state);
13121 XtSetValues (f->output_data.x->widget, al, 1);
13122 #else /* not USE_X_TOOLKIT */
13123 Window window = FRAME_X_WINDOW (f);
13124
13125 f->output_data.x->wm_hints.flags |= StateHint;
13126 f->output_data.x->wm_hints.initial_state = state;
13127
13128 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13129 #endif /* not USE_X_TOOLKIT */
13130 }
13131
13132 void
13133 x_wm_set_icon_pixmap (f, pixmap_id)
13134 struct frame *f;
13135 int pixmap_id;
13136 {
13137 Pixmap icon_pixmap;
13138
13139 #ifndef USE_X_TOOLKIT
13140 Window window = FRAME_X_WINDOW (f);
13141 #endif
13142
13143 if (pixmap_id > 0)
13144 {
13145 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
13146 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
13147 }
13148 else
13149 {
13150 /* It seems there is no way to turn off use of an icon pixmap.
13151 The following line does it, only if no icon has yet been created,
13152 for some window managers. But with mwm it crashes.
13153 Some people say it should clear the IconPixmapHint bit in this case,
13154 but that doesn't work, and the X consortium said it isn't the
13155 right thing at all. Since there is no way to win,
13156 best to explicitly give up. */
13157 #if 0
13158 f->output_data.x->wm_hints.icon_pixmap = None;
13159 #else
13160 return;
13161 #endif
13162 }
13163
13164 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
13165
13166 {
13167 Arg al[1];
13168 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
13169 XtSetValues (f->output_data.x->widget, al, 1);
13170 }
13171
13172 #else /* not USE_X_TOOLKIT */
13173
13174 f->output_data.x->wm_hints.flags |= IconPixmapHint;
13175 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13176
13177 #endif /* not USE_X_TOOLKIT */
13178 }
13179
13180 void
13181 x_wm_set_icon_position (f, icon_x, icon_y)
13182 struct frame *f;
13183 int icon_x, icon_y;
13184 {
13185 #ifdef USE_X_TOOLKIT
13186 Window window = XtWindow (f->output_data.x->widget);
13187 #else
13188 Window window = FRAME_X_WINDOW (f);
13189 #endif
13190
13191 f->output_data.x->wm_hints.flags |= IconPositionHint;
13192 f->output_data.x->wm_hints.icon_x = icon_x;
13193 f->output_data.x->wm_hints.icon_y = icon_y;
13194
13195 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13196 }
13197
13198 \f
13199 /***********************************************************************
13200 Fonts
13201 ***********************************************************************/
13202
13203 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
13204
13205 struct font_info *
13206 x_get_font_info (f, font_idx)
13207 FRAME_PTR f;
13208 int font_idx;
13209 {
13210 return (FRAME_X_FONT_TABLE (f) + font_idx);
13211 }
13212
13213
13214 /* Return a list of names of available fonts matching PATTERN on frame F.
13215
13216 If SIZE is > 0, it is the size (maximum bounds width) of fonts
13217 to be listed.
13218
13219 SIZE < 0 means include scalable fonts.
13220
13221 Frame F null means we have not yet created any frame on X, and
13222 consult the first display in x_display_list. MAXNAMES sets a limit
13223 on how many fonts to match. */
13224
13225 Lisp_Object
13226 x_list_fonts (f, pattern, size, maxnames)
13227 struct frame *f;
13228 Lisp_Object pattern;
13229 int size;
13230 int maxnames;
13231 {
13232 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
13233 Lisp_Object tem, second_best;
13234 struct x_display_info *dpyinfo
13235 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
13236 Display *dpy = dpyinfo->display;
13237 int try_XLoadQueryFont = 0;
13238 int count;
13239 int allow_scalable_fonts_p = 0;
13240
13241 if (size < 0)
13242 {
13243 allow_scalable_fonts_p = 1;
13244 size = 0;
13245 }
13246
13247 patterns = Fassoc (pattern, Valternate_fontname_alist);
13248 if (NILP (patterns))
13249 patterns = Fcons (pattern, Qnil);
13250
13251 if (maxnames == 1 && !size)
13252 /* We can return any single font matching PATTERN. */
13253 try_XLoadQueryFont = 1;
13254
13255 for (; CONSP (patterns); patterns = XCDR (patterns))
13256 {
13257 int num_fonts;
13258 char **names = NULL;
13259
13260 pattern = XCAR (patterns);
13261 /* See if we cached the result for this particular query.
13262 The cache is an alist of the form:
13263 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
13264 tem = XCDR (dpyinfo->name_list_element);
13265 key = Fcons (Fcons (pattern, make_number (maxnames)),
13266 allow_scalable_fonts_p ? Qt : Qnil);
13267 list = Fassoc (key, tem);
13268 if (!NILP (list))
13269 {
13270 list = Fcdr_safe (list);
13271 /* We have a cashed list. Don't have to get the list again. */
13272 goto label_cached;
13273 }
13274
13275 /* At first, put PATTERN in the cache. */
13276
13277 BLOCK_INPUT;
13278 count = x_catch_errors (dpy);
13279
13280 if (try_XLoadQueryFont)
13281 {
13282 XFontStruct *font;
13283 unsigned long value;
13284
13285 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
13286 if (x_had_errors_p (dpy))
13287 {
13288 /* This error is perhaps due to insufficient memory on X
13289 server. Let's just ignore it. */
13290 font = NULL;
13291 x_clear_errors (dpy);
13292 }
13293
13294 if (font
13295 && XGetFontProperty (font, XA_FONT, &value))
13296 {
13297 char *name = (char *) XGetAtomName (dpy, (Atom) value);
13298 int len = strlen (name);
13299 char *tmp;
13300
13301 /* If DXPC (a Differential X Protocol Compressor)
13302 Ver.3.7 is running, XGetAtomName will return null
13303 string. We must avoid such a name. */
13304 if (len == 0)
13305 try_XLoadQueryFont = 0;
13306 else
13307 {
13308 num_fonts = 1;
13309 names = (char **) alloca (sizeof (char *));
13310 /* Some systems only allow alloca assigned to a
13311 simple var. */
13312 tmp = (char *) alloca (len + 1); names[0] = tmp;
13313 bcopy (name, names[0], len + 1);
13314 XFree (name);
13315 }
13316 }
13317 else
13318 try_XLoadQueryFont = 0;
13319
13320 if (font)
13321 XFreeFont (dpy, font);
13322 }
13323
13324 if (!try_XLoadQueryFont)
13325 {
13326 /* We try at least 10 fonts because XListFonts will return
13327 auto-scaled fonts at the head. */
13328 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
13329 &num_fonts);
13330 if (x_had_errors_p (dpy))
13331 {
13332 /* This error is perhaps due to insufficient memory on X
13333 server. Let's just ignore it. */
13334 names = NULL;
13335 x_clear_errors (dpy);
13336 }
13337 }
13338
13339 x_uncatch_errors (dpy, count);
13340 UNBLOCK_INPUT;
13341
13342 if (names)
13343 {
13344 int i;
13345
13346 /* Make a list of all the fonts we got back.
13347 Store that in the font cache for the display. */
13348 for (i = 0; i < num_fonts; i++)
13349 {
13350 int width = 0;
13351 char *p = names[i];
13352 int average_width = -1, dashes = 0;
13353
13354 /* Count the number of dashes in NAMES[I]. If there are
13355 14 dashes, and the field value following 12th dash
13356 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
13357 is usually too ugly to be used for editing. Let's
13358 ignore it. */
13359 while (*p)
13360 if (*p++ == '-')
13361 {
13362 dashes++;
13363 if (dashes == 7) /* PIXEL_SIZE field */
13364 width = atoi (p);
13365 else if (dashes == 12) /* AVERAGE_WIDTH field */
13366 average_width = atoi (p);
13367 }
13368
13369 if (allow_scalable_fonts_p
13370 || dashes < 14 || average_width != 0)
13371 {
13372 tem = build_string (names[i]);
13373 if (NILP (Fassoc (tem, list)))
13374 {
13375 if (STRINGP (Vx_pixel_size_width_font_regexp)
13376 && ((fast_c_string_match_ignore_case
13377 (Vx_pixel_size_width_font_regexp, names[i]))
13378 >= 0))
13379 /* We can set the value of PIXEL_SIZE to the
13380 width of this font. */
13381 list = Fcons (Fcons (tem, make_number (width)), list);
13382 else
13383 /* For the moment, width is not known. */
13384 list = Fcons (Fcons (tem, Qnil), list);
13385 }
13386 }
13387 }
13388 if (!try_XLoadQueryFont)
13389 XFreeFontNames (names);
13390 }
13391
13392 /* Now store the result in the cache. */
13393 XCDR (dpyinfo->name_list_element)
13394 = Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element));
13395
13396 label_cached:
13397 if (NILP (list)) continue; /* Try the remaining alternatives. */
13398
13399 newlist = second_best = Qnil;
13400 /* Make a list of the fonts that have the right width. */
13401 for (; CONSP (list); list = XCDR (list))
13402 {
13403 int found_size;
13404
13405 tem = XCAR (list);
13406
13407 if (!CONSP (tem) || NILP (XCAR (tem)))
13408 continue;
13409 if (!size)
13410 {
13411 newlist = Fcons (XCAR (tem), newlist);
13412 continue;
13413 }
13414
13415 if (!INTEGERP (XCDR (tem)))
13416 {
13417 /* Since we have not yet known the size of this font, we
13418 must try slow function call XLoadQueryFont. */
13419 XFontStruct *thisinfo;
13420
13421 BLOCK_INPUT;
13422 count = x_catch_errors (dpy);
13423 thisinfo = XLoadQueryFont (dpy,
13424 XSTRING (XCAR (tem))->data);
13425 if (x_had_errors_p (dpy))
13426 {
13427 /* This error is perhaps due to insufficient memory on X
13428 server. Let's just ignore it. */
13429 thisinfo = NULL;
13430 x_clear_errors (dpy);
13431 }
13432 x_uncatch_errors (dpy, count);
13433 UNBLOCK_INPUT;
13434
13435 if (thisinfo)
13436 {
13437 XCDR (tem)
13438 = (thisinfo->min_bounds.width == 0
13439 ? make_number (0)
13440 : make_number (thisinfo->max_bounds.width));
13441 XFreeFont (dpy, thisinfo);
13442 }
13443 else
13444 /* For unknown reason, the previous call of XListFont had
13445 returned a font which can't be opened. Record the size
13446 as 0 not to try to open it again. */
13447 XCDR (tem) = make_number (0);
13448 }
13449
13450 found_size = XINT (XCDR (tem));
13451 if (found_size == size)
13452 newlist = Fcons (XCAR (tem), newlist);
13453 else if (found_size > 0)
13454 {
13455 if (NILP (second_best))
13456 second_best = tem;
13457 else if (found_size < size)
13458 {
13459 if (XINT (XCDR (second_best)) > size
13460 || XINT (XCDR (second_best)) < found_size)
13461 second_best = tem;
13462 }
13463 else
13464 {
13465 if (XINT (XCDR (second_best)) > size
13466 && XINT (XCDR (second_best)) > found_size)
13467 second_best = tem;
13468 }
13469 }
13470 }
13471 if (!NILP (newlist))
13472 break;
13473 else if (!NILP (second_best))
13474 {
13475 newlist = Fcons (XCAR (second_best), Qnil);
13476 break;
13477 }
13478 }
13479
13480 return newlist;
13481 }
13482
13483
13484 #if GLYPH_DEBUG
13485
13486 /* Check that FONT is valid on frame F. It is if it can be found in F's
13487 font table. */
13488
13489 static void
13490 x_check_font (f, font)
13491 struct frame *f;
13492 XFontStruct *font;
13493 {
13494 int i;
13495 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13496
13497 xassert (font != NULL);
13498
13499 for (i = 0; i < dpyinfo->n_fonts; i++)
13500 if (dpyinfo->font_table[i].name
13501 && font == dpyinfo->font_table[i].font)
13502 break;
13503
13504 xassert (i < dpyinfo->n_fonts);
13505 }
13506
13507 #endif /* GLYPH_DEBUG != 0 */
13508
13509 /* Set *W to the minimum width, *H to the minimum font height of FONT.
13510 Note: There are (broken) X fonts out there with invalid XFontStruct
13511 min_bounds contents. For example, handa@etl.go.jp reports that
13512 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
13513 have font->min_bounds.width == 0. */
13514
13515 static INLINE void
13516 x_font_min_bounds (font, w, h)
13517 XFontStruct *font;
13518 int *w, *h;
13519 {
13520 *h = FONT_HEIGHT (font);
13521 *w = font->min_bounds.width;
13522
13523 /* Try to handle the case where FONT->min_bounds has invalid
13524 contents. Since the only font known to have invalid min_bounds
13525 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
13526 if (*w <= 0)
13527 *w = font->max_bounds.width;
13528 }
13529
13530
13531 /* Compute the smallest character width and smallest font height over
13532 all fonts available on frame F. Set the members smallest_char_width
13533 and smallest_font_height in F's x_display_info structure to
13534 the values computed. Value is non-zero if smallest_font_height or
13535 smallest_char_width become smaller than they were before. */
13536
13537 static int
13538 x_compute_min_glyph_bounds (f)
13539 struct frame *f;
13540 {
13541 int i;
13542 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13543 XFontStruct *font;
13544 int old_width = dpyinfo->smallest_char_width;
13545 int old_height = dpyinfo->smallest_font_height;
13546
13547 dpyinfo->smallest_font_height = 100000;
13548 dpyinfo->smallest_char_width = 100000;
13549
13550 for (i = 0; i < dpyinfo->n_fonts; ++i)
13551 if (dpyinfo->font_table[i].name)
13552 {
13553 struct font_info *fontp = dpyinfo->font_table + i;
13554 int w, h;
13555
13556 font = (XFontStruct *) fontp->font;
13557 xassert (font != (XFontStruct *) ~0);
13558 x_font_min_bounds (font, &w, &h);
13559
13560 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
13561 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
13562 }
13563
13564 xassert (dpyinfo->smallest_char_width > 0
13565 && dpyinfo->smallest_font_height > 0);
13566
13567 return (dpyinfo->n_fonts == 1
13568 || dpyinfo->smallest_char_width < old_width
13569 || dpyinfo->smallest_font_height < old_height);
13570 }
13571
13572
13573 /* Load font named FONTNAME of the size SIZE for frame F, and return a
13574 pointer to the structure font_info while allocating it dynamically.
13575 If SIZE is 0, load any size of font.
13576 If loading is failed, return NULL. */
13577
13578 struct font_info *
13579 x_load_font (f, fontname, size)
13580 struct frame *f;
13581 register char *fontname;
13582 int size;
13583 {
13584 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13585 Lisp_Object font_names;
13586 int count;
13587
13588 /* Get a list of all the fonts that match this name. Once we
13589 have a list of matching fonts, we compare them against the fonts
13590 we already have by comparing names. */
13591 font_names = x_list_fonts (f, build_string (fontname), size, 1);
13592
13593 if (!NILP (font_names))
13594 {
13595 Lisp_Object tail;
13596 int i;
13597
13598 for (i = 0; i < dpyinfo->n_fonts; i++)
13599 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
13600 if (dpyinfo->font_table[i].name
13601 && (!strcmp (dpyinfo->font_table[i].name,
13602 XSTRING (XCAR (tail))->data)
13603 || !strcmp (dpyinfo->font_table[i].full_name,
13604 XSTRING (XCAR (tail))->data)))
13605 return (dpyinfo->font_table + i);
13606 }
13607
13608 /* Load the font and add it to the table. */
13609 {
13610 char *full_name;
13611 XFontStruct *font;
13612 struct font_info *fontp;
13613 unsigned long value;
13614 int i;
13615
13616 /* If we have found fonts by x_list_font, load one of them. If
13617 not, we still try to load a font by the name given as FONTNAME
13618 because XListFonts (called in x_list_font) of some X server has
13619 a bug of not finding a font even if the font surely exists and
13620 is loadable by XLoadQueryFont. */
13621 if (size > 0 && !NILP (font_names))
13622 fontname = (char *) XSTRING (XCAR (font_names))->data;
13623
13624 BLOCK_INPUT;
13625 count = x_catch_errors (FRAME_X_DISPLAY (f));
13626 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
13627 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
13628 {
13629 /* This error is perhaps due to insufficient memory on X
13630 server. Let's just ignore it. */
13631 font = NULL;
13632 x_clear_errors (FRAME_X_DISPLAY (f));
13633 }
13634 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
13635 UNBLOCK_INPUT;
13636 if (!font)
13637 return NULL;
13638
13639 /* Find a free slot in the font table. */
13640 for (i = 0; i < dpyinfo->n_fonts; ++i)
13641 if (dpyinfo->font_table[i].name == NULL)
13642 break;
13643
13644 /* If no free slot found, maybe enlarge the font table. */
13645 if (i == dpyinfo->n_fonts
13646 && dpyinfo->n_fonts == dpyinfo->font_table_size)
13647 {
13648 int sz;
13649 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
13650 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
13651 dpyinfo->font_table
13652 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
13653 }
13654
13655 fontp = dpyinfo->font_table + i;
13656 if (i == dpyinfo->n_fonts)
13657 ++dpyinfo->n_fonts;
13658
13659 /* Now fill in the slots of *FONTP. */
13660 BLOCK_INPUT;
13661 fontp->font = font;
13662 fontp->font_idx = i;
13663 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
13664 bcopy (fontname, fontp->name, strlen (fontname) + 1);
13665
13666 /* Try to get the full name of FONT. Put it in FULL_NAME. */
13667 full_name = 0;
13668 if (XGetFontProperty (font, XA_FONT, &value))
13669 {
13670 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
13671 char *p = name;
13672 int dashes = 0;
13673
13674 /* Count the number of dashes in the "full name".
13675 If it is too few, this isn't really the font's full name,
13676 so don't use it.
13677 In X11R4, the fonts did not come with their canonical names
13678 stored in them. */
13679 while (*p)
13680 {
13681 if (*p == '-')
13682 dashes++;
13683 p++;
13684 }
13685
13686 if (dashes >= 13)
13687 {
13688 full_name = (char *) xmalloc (p - name + 1);
13689 bcopy (name, full_name, p - name + 1);
13690 }
13691
13692 XFree (name);
13693 }
13694
13695 if (full_name != 0)
13696 fontp->full_name = full_name;
13697 else
13698 fontp->full_name = fontp->name;
13699
13700 fontp->size = font->max_bounds.width;
13701 fontp->height = FONT_HEIGHT (font);
13702
13703 if (NILP (font_names))
13704 {
13705 /* We come here because of a bug of XListFonts mentioned at
13706 the head of this block. Let's store this information in
13707 the cache for x_list_fonts. */
13708 Lisp_Object lispy_name = build_string (fontname);
13709 Lisp_Object lispy_full_name = build_string (fontp->full_name);
13710 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
13711 Qnil);
13712
13713 XCDR (dpyinfo->name_list_element)
13714 = Fcons (Fcons (key,
13715 Fcons (Fcons (lispy_full_name,
13716 make_number (fontp->size)),
13717 Qnil)),
13718 XCDR (dpyinfo->name_list_element));
13719 if (full_name)
13720 {
13721 key = Fcons (Fcons (lispy_full_name, make_number (256)),
13722 Qnil);
13723 XCDR (dpyinfo->name_list_element)
13724 = Fcons (Fcons (key,
13725 Fcons (Fcons (lispy_full_name,
13726 make_number (fontp->size)),
13727 Qnil)),
13728 XCDR (dpyinfo->name_list_element));
13729 }
13730 }
13731
13732 /* The slot `encoding' specifies how to map a character
13733 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
13734 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
13735 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
13736 2:0xA020..0xFF7F). For the moment, we don't know which charset
13737 uses this font. So, we set information in fontp->encoding[1]
13738 which is never used by any charset. If mapping can't be
13739 decided, set FONT_ENCODING_NOT_DECIDED. */
13740 fontp->encoding[1]
13741 = (font->max_byte1 == 0
13742 /* 1-byte font */
13743 ? (font->min_char_or_byte2 < 0x80
13744 ? (font->max_char_or_byte2 < 0x80
13745 ? 0 /* 0x20..0x7F */
13746 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
13747 : 1) /* 0xA0..0xFF */
13748 /* 2-byte font */
13749 : (font->min_byte1 < 0x80
13750 ? (font->max_byte1 < 0x80
13751 ? (font->min_char_or_byte2 < 0x80
13752 ? (font->max_char_or_byte2 < 0x80
13753 ? 0 /* 0x2020..0x7F7F */
13754 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
13755 : 3) /* 0x20A0..0x7FFF */
13756 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
13757 : (font->min_char_or_byte2 < 0x80
13758 ? (font->max_char_or_byte2 < 0x80
13759 ? 2 /* 0xA020..0xFF7F */
13760 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
13761 : 1))); /* 0xA0A0..0xFFFF */
13762
13763 fontp->baseline_offset
13764 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
13765 ? (long) value : 0);
13766 fontp->relative_compose
13767 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
13768 ? (long) value : 0);
13769 fontp->default_ascent
13770 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
13771 ? (long) value : 0);
13772
13773 /* Set global flag fonts_changed_p to non-zero if the font loaded
13774 has a character with a smaller width than any other character
13775 before, or if the font loaded has a smalle>r height than any
13776 other font loaded before. If this happens, it will make a
13777 glyph matrix reallocation necessary. */
13778 fonts_changed_p = x_compute_min_glyph_bounds (f);
13779 UNBLOCK_INPUT;
13780 return fontp;
13781 }
13782 }
13783
13784
13785 /* Return a pointer to struct font_info of a font named FONTNAME for
13786 frame F. If no such font is loaded, return NULL. */
13787
13788 struct font_info *
13789 x_query_font (f, fontname)
13790 struct frame *f;
13791 register char *fontname;
13792 {
13793 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13794 int i;
13795
13796 for (i = 0; i < dpyinfo->n_fonts; i++)
13797 if (dpyinfo->font_table[i].name
13798 && (!strcmp (dpyinfo->font_table[i].name, fontname)
13799 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
13800 return (dpyinfo->font_table + i);
13801 return NULL;
13802 }
13803
13804
13805 /* Find a CCL program for a font specified by FONTP, and set the member
13806 `encoder' of the structure. */
13807
13808 void
13809 x_find_ccl_program (fontp)
13810 struct font_info *fontp;
13811 {
13812 Lisp_Object list, elt;
13813
13814 elt = Qnil;
13815 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
13816 {
13817 elt = XCAR (list);
13818 if (CONSP (elt)
13819 && STRINGP (XCAR (elt))
13820 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
13821 >= 0)
13822 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
13823 >= 0)))
13824 break;
13825 }
13826
13827 if (! NILP (list))
13828 {
13829 struct ccl_program *ccl
13830 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
13831
13832 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
13833 xfree (ccl);
13834 else
13835 fontp->font_encoder = ccl;
13836 }
13837 }
13838
13839
13840 \f
13841 /***********************************************************************
13842 Initialization
13843 ***********************************************************************/
13844
13845 #ifdef USE_X_TOOLKIT
13846 static XrmOptionDescRec emacs_options[] = {
13847 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13848 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13849
13850 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13851 XrmoptionSepArg, NULL},
13852 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13853
13854 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13855 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13856 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13857 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13858 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13859 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13860 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13861 };
13862 #endif /* USE_X_TOOLKIT */
13863
13864 static int x_initialized;
13865
13866 #ifdef MULTI_KBOARD
13867 /* Test whether two display-name strings agree up to the dot that separates
13868 the screen number from the server number. */
13869 static int
13870 same_x_server (name1, name2)
13871 char *name1, *name2;
13872 {
13873 int seen_colon = 0;
13874 unsigned char *system_name = XSTRING (Vsystem_name)->data;
13875 int system_name_length = strlen (system_name);
13876 int length_until_period = 0;
13877
13878 while (system_name[length_until_period] != 0
13879 && system_name[length_until_period] != '.')
13880 length_until_period++;
13881
13882 /* Treat `unix' like an empty host name. */
13883 if (! strncmp (name1, "unix:", 5))
13884 name1 += 4;
13885 if (! strncmp (name2, "unix:", 5))
13886 name2 += 4;
13887 /* Treat this host's name like an empty host name. */
13888 if (! strncmp (name1, system_name, system_name_length)
13889 && name1[system_name_length] == ':')
13890 name1 += system_name_length;
13891 if (! strncmp (name2, system_name, system_name_length)
13892 && name2[system_name_length] == ':')
13893 name2 += system_name_length;
13894 /* Treat this host's domainless name like an empty host name. */
13895 if (! strncmp (name1, system_name, length_until_period)
13896 && name1[length_until_period] == ':')
13897 name1 += length_until_period;
13898 if (! strncmp (name2, system_name, length_until_period)
13899 && name2[length_until_period] == ':')
13900 name2 += length_until_period;
13901
13902 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13903 {
13904 if (*name1 == ':')
13905 seen_colon++;
13906 if (seen_colon && *name1 == '.')
13907 return 1;
13908 }
13909 return (seen_colon
13910 && (*name1 == '.' || *name1 == '\0')
13911 && (*name2 == '.' || *name2 == '\0'));
13912 }
13913 #endif
13914
13915 struct x_display_info *
13916 x_term_init (display_name, xrm_option, resource_name)
13917 Lisp_Object display_name;
13918 char *xrm_option;
13919 char *resource_name;
13920 {
13921 int connection;
13922 Display *dpy;
13923 struct x_display_info *dpyinfo;
13924 XrmDatabase xrdb;
13925
13926 BLOCK_INPUT;
13927
13928 if (!x_initialized)
13929 {
13930 x_initialize ();
13931 x_initialized = 1;
13932 }
13933
13934 #ifdef USE_X_TOOLKIT
13935 /* weiner@footloose.sps.mot.com reports that this causes
13936 errors with X11R5:
13937 X protocol error: BadAtom (invalid Atom parameter)
13938 on protocol request 18skiloaf.
13939 So let's not use it until R6. */
13940 #ifdef HAVE_X11XTR6
13941 XtSetLanguageProc (NULL, NULL, NULL);
13942 #endif
13943
13944 {
13945 int argc = 0;
13946 char *argv[3];
13947
13948 argv[0] = "";
13949 argc = 1;
13950 if (xrm_option)
13951 {
13952 argv[argc++] = "-xrm";
13953 argv[argc++] = xrm_option;
13954 }
13955 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
13956 resource_name, EMACS_CLASS,
13957 emacs_options, XtNumber (emacs_options),
13958 &argc, argv);
13959
13960 #ifdef HAVE_X11XTR6
13961 /* I think this is to compensate for XtSetLanguageProc. */
13962 fixup_locale ();
13963 #endif
13964 }
13965
13966 #else /* not USE_X_TOOLKIT */
13967 #ifdef HAVE_X11R5
13968 XSetLocaleModifiers ("");
13969 #endif
13970 dpy = XOpenDisplay (XSTRING (display_name)->data);
13971 #endif /* not USE_X_TOOLKIT */
13972
13973 /* Detect failure. */
13974 if (dpy == 0)
13975 {
13976 UNBLOCK_INPUT;
13977 return 0;
13978 }
13979
13980 /* We have definitely succeeded. Record the new connection. */
13981
13982 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
13983 bzero (dpyinfo, sizeof *dpyinfo);
13984
13985 #ifdef MULTI_KBOARD
13986 {
13987 struct x_display_info *share;
13988 Lisp_Object tail;
13989
13990 for (share = x_display_list, tail = x_display_name_list; share;
13991 share = share->next, tail = XCDR (tail))
13992 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
13993 XSTRING (display_name)->data))
13994 break;
13995 if (share)
13996 dpyinfo->kboard = share->kboard;
13997 else
13998 {
13999 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
14000 init_kboard (dpyinfo->kboard);
14001 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
14002 {
14003 char *vendor = ServerVendor (dpy);
14004 UNBLOCK_INPUT;
14005 dpyinfo->kboard->Vsystem_key_alist
14006 = call1 (Qvendor_specific_keysyms,
14007 build_string (vendor ? vendor : ""));
14008 BLOCK_INPUT;
14009 }
14010
14011 dpyinfo->kboard->next_kboard = all_kboards;
14012 all_kboards = dpyinfo->kboard;
14013 /* Don't let the initial kboard remain current longer than necessary.
14014 That would cause problems if a file loaded on startup tries to
14015 prompt in the mini-buffer. */
14016 if (current_kboard == initial_kboard)
14017 current_kboard = dpyinfo->kboard;
14018 }
14019 dpyinfo->kboard->reference_count++;
14020 }
14021 #endif
14022
14023 /* Put this display on the chain. */
14024 dpyinfo->next = x_display_list;
14025 x_display_list = dpyinfo;
14026
14027 /* Put it on x_display_name_list as well, to keep them parallel. */
14028 x_display_name_list = Fcons (Fcons (display_name, Qnil),
14029 x_display_name_list);
14030 dpyinfo->name_list_element = XCAR (x_display_name_list);
14031
14032 dpyinfo->display = dpy;
14033
14034 #if 0
14035 XSetAfterFunction (x_current_display, x_trace_wire);
14036 #endif /* ! 0 */
14037
14038 dpyinfo->x_id_name
14039 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
14040 + STRING_BYTES (XSTRING (Vsystem_name))
14041 + 2);
14042 sprintf (dpyinfo->x_id_name, "%s@%s",
14043 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
14044
14045 /* Figure out which modifier bits mean what. */
14046 x_find_modifier_meanings (dpyinfo);
14047
14048 /* Get the scroll bar cursor. */
14049 dpyinfo->vertical_scroll_bar_cursor
14050 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
14051
14052 xrdb = x_load_resources (dpyinfo->display, xrm_option,
14053 resource_name, EMACS_CLASS);
14054 #ifdef HAVE_XRMSETDATABASE
14055 XrmSetDatabase (dpyinfo->display, xrdb);
14056 #else
14057 dpyinfo->display->db = xrdb;
14058 #endif
14059 /* Put the rdb where we can find it in a way that works on
14060 all versions. */
14061 dpyinfo->xrdb = xrdb;
14062
14063 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
14064 DefaultScreen (dpyinfo->display));
14065 select_visual (dpyinfo);
14066 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
14067 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
14068 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
14069 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
14070 dpyinfo->grabbed = 0;
14071 dpyinfo->reference_count = 0;
14072 dpyinfo->icon_bitmap_id = -1;
14073 dpyinfo->font_table = NULL;
14074 dpyinfo->n_fonts = 0;
14075 dpyinfo->font_table_size = 0;
14076 dpyinfo->bitmaps = 0;
14077 dpyinfo->bitmaps_size = 0;
14078 dpyinfo->bitmaps_last = 0;
14079 dpyinfo->scratch_cursor_gc = 0;
14080 dpyinfo->mouse_face_mouse_frame = 0;
14081 dpyinfo->mouse_face_deferred_gc = 0;
14082 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
14083 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
14084 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
14085 dpyinfo->mouse_face_window = Qnil;
14086 dpyinfo->mouse_face_overlay = Qnil;
14087 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
14088 dpyinfo->mouse_face_defer = 0;
14089 dpyinfo->x_focus_frame = 0;
14090 dpyinfo->x_focus_event_frame = 0;
14091 dpyinfo->x_highlight_frame = 0;
14092 dpyinfo->image_cache = make_image_cache ();
14093
14094 /* See if a private colormap is requested. */
14095 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
14096 {
14097 if (dpyinfo->visual->class == PseudoColor)
14098 {
14099 Lisp_Object value;
14100 value = display_x_get_resource (dpyinfo,
14101 build_string ("privateColormap"),
14102 build_string ("PrivateColormap"),
14103 Qnil, Qnil);
14104 if (STRINGP (value)
14105 && (!strcmp (XSTRING (value)->data, "true")
14106 || !strcmp (XSTRING (value)->data, "on")))
14107 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
14108 }
14109 }
14110 else
14111 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
14112 dpyinfo->visual, AllocNone);
14113
14114 {
14115 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
14116 double pixels = DisplayHeight (dpyinfo->display, screen_number);
14117 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
14118 dpyinfo->resy = pixels * 25.4 / mm;
14119 pixels = DisplayWidth (dpyinfo->display, screen_number);
14120 mm = DisplayWidthMM (dpyinfo->display, screen_number);
14121 dpyinfo->resx = pixels * 25.4 / mm;
14122 }
14123
14124 dpyinfo->Xatom_wm_protocols
14125 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
14126 dpyinfo->Xatom_wm_take_focus
14127 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
14128 dpyinfo->Xatom_wm_save_yourself
14129 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
14130 dpyinfo->Xatom_wm_delete_window
14131 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
14132 dpyinfo->Xatom_wm_change_state
14133 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
14134 dpyinfo->Xatom_wm_configure_denied
14135 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
14136 dpyinfo->Xatom_wm_window_moved
14137 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
14138 dpyinfo->Xatom_editres
14139 = XInternAtom (dpyinfo->display, "Editres", False);
14140 dpyinfo->Xatom_CLIPBOARD
14141 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
14142 dpyinfo->Xatom_TIMESTAMP
14143 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
14144 dpyinfo->Xatom_TEXT
14145 = XInternAtom (dpyinfo->display, "TEXT", False);
14146 dpyinfo->Xatom_COMPOUND_TEXT
14147 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
14148 dpyinfo->Xatom_DELETE
14149 = XInternAtom (dpyinfo->display, "DELETE", False);
14150 dpyinfo->Xatom_MULTIPLE
14151 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
14152 dpyinfo->Xatom_INCR
14153 = XInternAtom (dpyinfo->display, "INCR", False);
14154 dpyinfo->Xatom_EMACS_TMP
14155 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
14156 dpyinfo->Xatom_TARGETS
14157 = XInternAtom (dpyinfo->display, "TARGETS", False);
14158 dpyinfo->Xatom_NULL
14159 = XInternAtom (dpyinfo->display, "NULL", False);
14160 dpyinfo->Xatom_ATOM_PAIR
14161 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
14162 /* For properties of font. */
14163 dpyinfo->Xatom_PIXEL_SIZE
14164 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
14165 dpyinfo->Xatom_MULE_BASELINE_OFFSET
14166 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
14167 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
14168 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
14169 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
14170 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
14171
14172 /* Ghostscript support. */
14173 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
14174 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
14175
14176 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
14177 False);
14178
14179 dpyinfo->cut_buffers_initialized = 0;
14180
14181 connection = ConnectionNumber (dpyinfo->display);
14182 dpyinfo->connection = connection;
14183
14184 {
14185 char null_bits[1];
14186
14187 null_bits[0] = 0x00;
14188
14189 dpyinfo->null_pixel
14190 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14191 null_bits, 1, 1, (long) 0, (long) 0,
14192 1);
14193 }
14194
14195 {
14196 extern int gray_bitmap_width, gray_bitmap_height;
14197 extern char *gray_bitmap_bits;
14198 dpyinfo->gray
14199 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14200 gray_bitmap_bits,
14201 gray_bitmap_width, gray_bitmap_height,
14202 (unsigned long) 1, (unsigned long) 0, 1);
14203 }
14204
14205 #ifdef HAVE_X_I18N
14206 xim_initialize (dpyinfo, resource_name);
14207 #endif
14208
14209 #ifdef subprocesses
14210 /* This is only needed for distinguishing keyboard and process input. */
14211 if (connection != 0)
14212 add_keyboard_wait_descriptor (connection);
14213 #endif
14214
14215 #ifndef F_SETOWN_BUG
14216 #ifdef F_SETOWN
14217 #ifdef F_SETOWN_SOCK_NEG
14218 /* stdin is a socket here */
14219 fcntl (connection, F_SETOWN, -getpid ());
14220 #else /* ! defined (F_SETOWN_SOCK_NEG) */
14221 fcntl (connection, F_SETOWN, getpid ());
14222 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
14223 #endif /* ! defined (F_SETOWN) */
14224 #endif /* F_SETOWN_BUG */
14225
14226 #ifdef SIGIO
14227 if (interrupt_input)
14228 init_sigio (connection);
14229 #endif /* ! defined (SIGIO) */
14230
14231 #ifdef USE_LUCID
14232 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
14233 /* Make sure that we have a valid font for dialog boxes
14234 so that Xt does not crash. */
14235 {
14236 Display *dpy = dpyinfo->display;
14237 XrmValue d, fr, to;
14238 Font font;
14239 int count;
14240
14241 d.addr = (XPointer)&dpy;
14242 d.size = sizeof (Display *);
14243 fr.addr = XtDefaultFont;
14244 fr.size = sizeof (XtDefaultFont);
14245 to.size = sizeof (Font *);
14246 to.addr = (XPointer)&font;
14247 count = x_catch_errors (dpy);
14248 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
14249 abort ();
14250 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
14251 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
14252 x_uncatch_errors (dpy, count);
14253 }
14254 #endif
14255 #endif
14256
14257 /* See if we should run in synchronous mode. This is useful
14258 for debugging X code. */
14259 {
14260 Lisp_Object value;
14261 value = display_x_get_resource (dpyinfo,
14262 build_string ("synchronous"),
14263 build_string ("Synchronous"),
14264 Qnil, Qnil);
14265 if (STRINGP (value)
14266 && (!strcmp (XSTRING (value)->data, "true")
14267 || !strcmp (XSTRING (value)->data, "on")))
14268 XSynchronize (dpyinfo->display, True);
14269 }
14270
14271 UNBLOCK_INPUT;
14272
14273 return dpyinfo;
14274 }
14275 \f
14276 /* Get rid of display DPYINFO, assuming all frames are already gone,
14277 and without sending any more commands to the X server. */
14278
14279 void
14280 x_delete_display (dpyinfo)
14281 struct x_display_info *dpyinfo;
14282 {
14283 delete_keyboard_wait_descriptor (dpyinfo->connection);
14284
14285 /* Discard this display from x_display_name_list and x_display_list.
14286 We can't use Fdelq because that can quit. */
14287 if (! NILP (x_display_name_list)
14288 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
14289 x_display_name_list = XCDR (x_display_name_list);
14290 else
14291 {
14292 Lisp_Object tail;
14293
14294 tail = x_display_name_list;
14295 while (CONSP (tail) && CONSP (XCDR (tail)))
14296 {
14297 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
14298 {
14299 XCDR (tail) = XCDR (XCDR (tail));
14300 break;
14301 }
14302 tail = XCDR (tail);
14303 }
14304 }
14305
14306 if (next_noop_dpyinfo == dpyinfo)
14307 next_noop_dpyinfo = dpyinfo->next;
14308
14309 if (x_display_list == dpyinfo)
14310 x_display_list = dpyinfo->next;
14311 else
14312 {
14313 struct x_display_info *tail;
14314
14315 for (tail = x_display_list; tail; tail = tail->next)
14316 if (tail->next == dpyinfo)
14317 tail->next = tail->next->next;
14318 }
14319
14320 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
14321 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
14322 XrmDestroyDatabase (dpyinfo->xrdb);
14323 #endif
14324 #endif
14325 #ifdef MULTI_KBOARD
14326 if (--dpyinfo->kboard->reference_count == 0)
14327 delete_kboard (dpyinfo->kboard);
14328 #endif
14329 #ifdef HAVE_X_I18N
14330 if (dpyinfo->xim)
14331 xim_close_dpy (dpyinfo);
14332 #endif
14333
14334 xfree (dpyinfo->font_table);
14335 xfree (dpyinfo->x_id_name);
14336 xfree (dpyinfo->color_cells);
14337 xfree (dpyinfo);
14338 }
14339
14340 \f
14341 /* Set up use of X before we make the first connection. */
14342
14343 static struct redisplay_interface x_redisplay_interface =
14344 {
14345 x_produce_glyphs,
14346 x_write_glyphs,
14347 x_insert_glyphs,
14348 x_clear_end_of_line,
14349 x_scroll_run,
14350 x_after_update_window_line,
14351 x_update_window_begin,
14352 x_update_window_end,
14353 XTcursor_to,
14354 x_flush,
14355 x_clear_mouse_face,
14356 x_get_glyph_overhangs,
14357 x_fix_overlapping_area
14358 };
14359
14360 void
14361 x_initialize ()
14362 {
14363 rif = &x_redisplay_interface;
14364
14365 clear_frame_hook = x_clear_frame;
14366 ins_del_lines_hook = x_ins_del_lines;
14367 change_line_highlight_hook = x_change_line_highlight;
14368 delete_glyphs_hook = x_delete_glyphs;
14369 ring_bell_hook = XTring_bell;
14370 reset_terminal_modes_hook = XTreset_terminal_modes;
14371 set_terminal_modes_hook = XTset_terminal_modes;
14372 update_begin_hook = x_update_begin;
14373 update_end_hook = x_update_end;
14374 set_terminal_window_hook = XTset_terminal_window;
14375 read_socket_hook = XTread_socket;
14376 frame_up_to_date_hook = XTframe_up_to_date;
14377 reassert_line_highlight_hook = XTreassert_line_highlight;
14378 mouse_position_hook = XTmouse_position;
14379 frame_rehighlight_hook = XTframe_rehighlight;
14380 frame_raise_lower_hook = XTframe_raise_lower;
14381 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
14382 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
14383 redeem_scroll_bar_hook = XTredeem_scroll_bar;
14384 judge_scroll_bars_hook = XTjudge_scroll_bars;
14385 estimate_mode_line_height_hook = x_estimate_mode_line_height;
14386
14387 scroll_region_ok = 1; /* we'll scroll partial frames */
14388 char_ins_del_ok = 1;
14389 line_ins_del_ok = 1; /* we'll just blt 'em */
14390 fast_clear_end_of_line = 1; /* X does this well */
14391 memory_below_frame = 0; /* we don't remember what scrolls
14392 off the bottom */
14393 baud_rate = 19200;
14394
14395 x_noop_count = 0;
14396 last_tool_bar_item = -1;
14397 any_help_event_p = 0;
14398
14399 /* Try to use interrupt input; if we can't, then start polling. */
14400 Fset_input_mode (Qt, Qnil, Qt, Qnil);
14401
14402 #ifdef USE_X_TOOLKIT
14403 XtToolkitInitialize ();
14404
14405 Xt_app_con = XtCreateApplicationContext ();
14406
14407 /* Register a converter from strings to pixels, which uses
14408 Emacs' color allocation infrastructure. */
14409 XtAppSetTypeConverter (Xt_app_con,
14410 XtRString, XtRPixel, cvt_string_to_pixel,
14411 cvt_string_to_pixel_args,
14412 XtNumber (cvt_string_to_pixel_args),
14413 XtCacheByDisplay, cvt_pixel_dtor);
14414
14415 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
14416
14417 /* Install an asynchronous timer that processes Xt timeout events
14418 every 0.1s. This is necessary because some widget sets use
14419 timeouts internally, for example the LessTif menu bar, or the
14420 Xaw3d scroll bar. When Xt timouts aren't processed, these
14421 widgets don't behave normally. */
14422 {
14423 EMACS_TIME interval;
14424 EMACS_SET_SECS_USECS (interval, 0, 100000);
14425 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
14426 }
14427 #endif
14428
14429 #ifdef USE_TOOLKIT_SCROLL_BARS
14430 xaw3d_arrow_scroll = False;
14431 xaw3d_pick_top = True;
14432 #endif
14433
14434 /* Note that there is no real way portable across R3/R4 to get the
14435 original error handler. */
14436 XSetErrorHandler (x_error_handler);
14437 XSetIOErrorHandler (x_io_error_quitter);
14438
14439 /* Disable Window Change signals; they are handled by X events. */
14440 #ifdef SIGWINCH
14441 signal (SIGWINCH, SIG_DFL);
14442 #endif /* ! defined (SIGWINCH) */
14443
14444 signal (SIGPIPE, x_connection_signal);
14445 }
14446
14447
14448 void
14449 syms_of_xterm ()
14450 {
14451 staticpro (&x_error_message_string);
14452 x_error_message_string = Qnil;
14453
14454 staticpro (&x_display_name_list);
14455 x_display_name_list = Qnil;
14456
14457 staticpro (&last_mouse_scroll_bar);
14458 last_mouse_scroll_bar = Qnil;
14459
14460 staticpro (&Qvendor_specific_keysyms);
14461 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
14462
14463 staticpro (&last_mouse_press_frame);
14464 last_mouse_press_frame = Qnil;
14465
14466 help_echo = Qnil;
14467 staticpro (&help_echo);
14468 help_echo_object = Qnil;
14469 staticpro (&help_echo_object);
14470 help_echo_window = Qnil;
14471 staticpro (&help_echo_window);
14472 previous_help_echo = Qnil;
14473 staticpro (&previous_help_echo);
14474 help_echo_pos = -1;
14475
14476 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
14477 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
14478 For example, if a block cursor is over a tab, it will be drawn as\n\
14479 wide as that tab on the display.");
14480 x_stretch_cursor_p = 0;
14481
14482 DEFVAR_BOOL ("x-use-underline-position-properties",
14483 &x_use_underline_position_properties,
14484 "*Non-nil means make use of UNDERLINE_POSITION font properties.\n\
14485 Nil means ignore them. If you encounter fonts with bogus\n\
14486 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior\n\
14487 to 4.1, set this to nil.");
14488 x_use_underline_position_properties = 1;
14489
14490 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
14491 "What X toolkit scroll bars Emacs uses.\n\
14492 A value of nil means Emacs doesn't use X toolkit scroll bars.\n\
14493 Otherwise, value is a symbol describing the X toolkit.");
14494 #ifdef USE_TOOLKIT_SCROLL_BARS
14495 #ifdef USE_MOTIF
14496 Vx_toolkit_scroll_bars = intern ("motif");
14497 #elif defined HAVE_XAW3D
14498 Vx_toolkit_scroll_bars = intern ("xaw3d");
14499 #else
14500 Vx_toolkit_scroll_bars = intern ("xaw");
14501 #endif
14502 #else
14503 Vx_toolkit_scroll_bars = Qnil;
14504 #endif
14505
14506 staticpro (&last_mouse_motion_frame);
14507 last_mouse_motion_frame = Qnil;
14508 }
14509
14510 #endif /* not HAVE_X_WINDOWS */