]> code.delx.au - gnu-emacs/blob - src/xterm.c
Put doc strings in comments.
[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 #define DOC_STRINGS_IN_COMMENTS
36 #include "lisp.h"
37 #include "blockinput.h"
38
39 /* Need syssignal.h for various externs and definitions that may be required
40 by some configurations for calls to signal later in this source file. */
41 #include "syssignal.h"
42
43 /* This may include sys/types.h, and that somehow loses
44 if this is not done before the other system files. */
45 #include "xterm.h"
46 #include <X11/cursorfont.h>
47
48 /* Load sys/types.h if not already loaded.
49 In some systems loading it twice is suicidal. */
50 #ifndef makedev
51 #include <sys/types.h>
52 #endif /* makedev */
53
54 #ifdef BSD_SYSTEM
55 #include <sys/ioctl.h>
56 #endif /* ! defined (BSD_SYSTEM) */
57
58 #include "systty.h"
59 #include "systime.h"
60
61 #ifndef INCLUDED_FCNTL
62 #include <fcntl.h>
63 #endif
64 #include <ctype.h>
65 #include <errno.h>
66 #include <setjmp.h>
67 #include <sys/stat.h>
68 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
69 /* #include <sys/param.h> */
70
71 #include "charset.h"
72 #include "coding.h"
73 #include "ccl.h"
74 #include "frame.h"
75 #include "dispextern.h"
76 #include "fontset.h"
77 #include "termhooks.h"
78 #include "termopts.h"
79 #include "termchar.h"
80 #include "gnu.h"
81 #include "disptab.h"
82 #include "buffer.h"
83 #include "window.h"
84 #include "keyboard.h"
85 #include "intervals.h"
86 #include "process.h"
87 #include "atimer.h"
88 #include "keymap.h"
89
90 #ifdef USE_X_TOOLKIT
91 #include <X11/Shell.h>
92 #endif
93
94 #ifdef HAVE_SYS_TIME_H
95 #include <sys/time.h>
96 #endif
97 #ifdef HAVE_UNISTD_H
98 #include <unistd.h>
99 #endif
100
101 #ifdef USE_X_TOOLKIT
102
103 extern void free_frame_menubar P_ ((struct frame *));
104 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
105 int));
106
107 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
108 #define HACK_EDITRES
109 extern void _XEditResCheckMessages ();
110 #endif /* not NO_EDITRES */
111
112 /* Include toolkit specific headers for the scroll bar widget. */
113
114 #ifdef USE_TOOLKIT_SCROLL_BARS
115 #if defined USE_MOTIF
116 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
117 #include <Xm/ScrollBar.h>
118 #else /* !USE_MOTIF i.e. use Xaw */
119
120 #ifdef HAVE_XAW3D
121 #include <X11/Xaw3d/Simple.h>
122 #include <X11/Xaw3d/Scrollbar.h>
123 #define ARROW_SCROLLBAR
124 #include <X11/Xaw3d/ScrollbarP.h>
125 #else /* !HAVE_XAW3D */
126 #include <X11/Xaw/Simple.h>
127 #include <X11/Xaw/Scrollbar.h>
128 #endif /* !HAVE_XAW3D */
129 #ifndef XtNpickTop
130 #define XtNpickTop "pickTop"
131 #endif /* !XtNpickTop */
132 #endif /* !USE_MOTIF */
133 #endif /* USE_TOOLKIT_SCROLL_BARS */
134
135 #endif /* USE_X_TOOLKIT */
136
137 #ifndef USE_X_TOOLKIT
138 #define x_any_window_to_frame x_window_to_frame
139 #define x_top_window_to_frame x_window_to_frame
140 #endif
141
142 #ifdef USE_X_TOOLKIT
143 #include "widget.h"
144 #ifndef XtNinitialState
145 #define XtNinitialState "initialState"
146 #endif
147 #endif
148
149 #define abs(x) ((x) < 0 ? -(x) : (x))
150
151 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
152
153 \f
154 /* Bitmaps for truncated lines. */
155
156 enum bitmap_type
157 {
158 NO_BITMAP,
159 LEFT_TRUNCATION_BITMAP,
160 RIGHT_TRUNCATION_BITMAP,
161 OVERLAY_ARROW_BITMAP,
162 CONTINUED_LINE_BITMAP,
163 CONTINUATION_LINE_BITMAP,
164 ZV_LINE_BITMAP
165 };
166
167 /* Bitmap drawn to indicate lines not displaying text if
168 `indicate-empty-lines' is non-nil. */
169
170 #define zv_width 8
171 #define zv_height 8
172 static unsigned char zv_bits[] = {
173 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
174
175 /* An arrow like this: `<-'. */
176
177 #define left_width 8
178 #define left_height 8
179 static unsigned char left_bits[] = {
180 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
181
182 /* Right truncation arrow bitmap `->'. */
183
184 #define right_width 8
185 #define right_height 8
186 static unsigned char right_bits[] = {
187 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
188
189 /* Marker for continued lines. */
190
191 #define continued_width 8
192 #define continued_height 8
193 static unsigned char continued_bits[] = {
194 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
195
196 /* Marker for continuation lines. */
197
198 #define continuation_width 8
199 #define continuation_height 8
200 static unsigned char continuation_bits[] = {
201 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
202
203 /* Overlay arrow bitmap. */
204
205 #if 0
206 /* A bomb. */
207 #define ov_width 8
208 #define ov_height 8
209 static unsigned char ov_bits[] = {
210 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
211 #else
212 /* A triangular arrow. */
213 #define ov_width 8
214 #define ov_height 8
215 static unsigned char ov_bits[] = {
216 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
217
218 #endif
219
220 extern Lisp_Object Qhelp_echo;
221
222 \f
223 /* Non-nil means Emacs uses toolkit scroll bars. */
224
225 Lisp_Object Vx_toolkit_scroll_bars;
226
227 /* If a string, XTread_socket generates an event to display that string.
228 (The display is done in read_char.) */
229
230 static Lisp_Object help_echo;
231 static Lisp_Object help_echo_window;
232 static Lisp_Object help_echo_object;
233 static int help_echo_pos;
234
235 /* Temporary variable for XTread_socket. */
236
237 static Lisp_Object previous_help_echo;
238
239 /* Non-zero means that a HELP_EVENT has been generated since Emacs
240 start. */
241
242 static int any_help_event_p;
243
244 /* Non-zero means draw block and hollow cursor as wide as the glyph
245 under it. For example, if a block cursor is over a tab, it will be
246 drawn as wide as that tab on the display. */
247
248 int x_stretch_cursor_p;
249
250 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
251
252 int x_use_underline_position_properties;
253
254 /* This is a chain of structures for all the X displays currently in
255 use. */
256
257 struct x_display_info *x_display_list;
258
259 /* This is a list of cons cells, each of the form (NAME
260 . FONT-LIST-CACHE), one for each element of x_display_list and in
261 the same order. NAME is the name of the frame. FONT-LIST-CACHE
262 records previous values returned by x-list-fonts. */
263
264 Lisp_Object x_display_name_list;
265
266 /* Frame being updated by update_frame. This is declared in term.c.
267 This is set by update_begin and looked at by all the XT functions.
268 It is zero while not inside an update. In that case, the XT
269 functions assume that `selected_frame' is the frame to apply to. */
270
271 extern struct frame *updating_frame;
272
273 extern int waiting_for_input;
274
275 /* This is a frame waiting to be auto-raised, within XTread_socket. */
276
277 struct frame *pending_autoraise_frame;
278
279 #ifdef USE_X_TOOLKIT
280 /* The application context for Xt use. */
281 XtAppContext Xt_app_con;
282 static String Xt_default_resources[] = {0};
283 #endif /* USE_X_TOOLKIT */
284
285 /* Nominal cursor position -- where to draw output.
286 HPOS and VPOS are window relative glyph matrix coordinates.
287 X and Y are window relative pixel coordinates. */
288
289 struct cursor_pos output_cursor;
290
291 /* Non-zero means user is interacting with a toolkit scroll bar. */
292
293 static int toolkit_scroll_bar_interaction;
294
295 /* Mouse movement.
296
297 Formerly, we used PointerMotionHintMask (in standard_event_mask)
298 so that we would have to call XQueryPointer after each MotionNotify
299 event to ask for another such event. However, this made mouse tracking
300 slow, and there was a bug that made it eventually stop.
301
302 Simply asking for MotionNotify all the time seems to work better.
303
304 In order to avoid asking for motion events and then throwing most
305 of them away or busy-polling the server for mouse positions, we ask
306 the server for pointer motion hints. This means that we get only
307 one event per group of mouse movements. "Groups" are delimited by
308 other kinds of events (focus changes and button clicks, for
309 example), or by XQueryPointer calls; when one of these happens, we
310 get another MotionNotify event the next time the mouse moves. This
311 is at least as efficient as getting motion events when mouse
312 tracking is on, and I suspect only negligibly worse when tracking
313 is off. */
314
315 /* Where the mouse was last time we reported a mouse event. */
316
317 FRAME_PTR last_mouse_frame;
318 static XRectangle last_mouse_glyph;
319 static Lisp_Object last_mouse_press_frame;
320
321 /* The scroll bar in which the last X motion event occurred.
322
323 If the last X motion event occurred in a scroll bar, we set this so
324 XTmouse_position can know whether to report a scroll bar motion or
325 an ordinary motion.
326
327 If the last X motion event didn't occur in a scroll bar, we set
328 this to Qnil, to tell XTmouse_position to return an ordinary motion
329 event. */
330
331 static Lisp_Object last_mouse_scroll_bar;
332
333 /* This is a hack. We would really prefer that XTmouse_position would
334 return the time associated with the position it returns, but there
335 doesn't seem to be any way to wrest the time-stamp from the server
336 along with the position query. So, we just keep track of the time
337 of the last movement we received, and return that in hopes that
338 it's somewhat accurate. */
339
340 static Time last_mouse_movement_time;
341
342 /* Incremented by XTread_socket whenever it really tries to read
343 events. */
344
345 #ifdef __STDC__
346 static int volatile input_signal_count;
347 #else
348 static int input_signal_count;
349 #endif
350
351 /* Used locally within XTread_socket. */
352
353 static int x_noop_count;
354
355 /* Initial values of argv and argc. */
356
357 extern char **initial_argv;
358 extern int initial_argc;
359
360 extern Lisp_Object Vcommand_line_args, Vsystem_name;
361
362 /* Tells if a window manager is present or not. */
363
364 extern Lisp_Object Vx_no_window_manager;
365
366 extern Lisp_Object Qface, Qmouse_face;
367
368 extern int errno;
369
370 /* A mask of extra modifier bits to put into every keyboard char. */
371
372 extern int extra_keyboard_modifiers;
373
374 static Lisp_Object Qvendor_specific_keysyms;
375
376 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
377 extern Lisp_Object x_icon_type P_ ((struct frame *));
378
379
380 /* Enumeration for overriding/changing the face to use for drawing
381 glyphs in x_draw_glyphs. */
382
383 enum draw_glyphs_face
384 {
385 DRAW_NORMAL_TEXT,
386 DRAW_INVERSE_VIDEO,
387 DRAW_CURSOR,
388 DRAW_MOUSE_FACE,
389 DRAW_IMAGE_RAISED,
390 DRAW_IMAGE_SUNKEN
391 };
392
393 static int cursor_in_mouse_face_p P_ ((struct window *));
394 static int clear_mouse_face P_ ((struct x_display_info *));
395 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
396 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
397 static const XColor *x_color_cells P_ ((Display *, int *));
398 static void x_update_window_end P_ ((struct window *, int, int));
399 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
400 void x_delete_display P_ ((struct x_display_info *));
401 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
402 unsigned));
403 static int fast_find_position P_ ((struct window *, int, int *, int *,
404 int *, int *, Lisp_Object));
405 static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
406 int *, int *, int *, int *, int));
407 static void set_output_cursor P_ ((struct cursor_pos *));
408 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
409 int *, int *, int *, int));
410 static void note_mode_line_highlight P_ ((struct window *, int, int));
411 static void note_mouse_highlight P_ ((struct frame *, int, int));
412 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
413 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
414 static void show_mouse_face P_ ((struct x_display_info *,
415 enum draw_glyphs_face));
416 static int x_io_error_quitter P_ ((Display *));
417 int x_catch_errors P_ ((Display *));
418 void x_uncatch_errors P_ ((Display *, int));
419 void x_lower_frame P_ ((struct frame *));
420 void x_scroll_bar_clear P_ ((struct frame *));
421 int x_had_errors_p P_ ((Display *));
422 void x_wm_set_size_hint P_ ((struct frame *, long, int));
423 void x_raise_frame P_ ((struct frame *));
424 void x_set_window_size P_ ((struct frame *, int, int, int));
425 void x_wm_set_window_state P_ ((struct frame *, int));
426 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
427 void x_initialize P_ ((void));
428 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
429 static int x_compute_min_glyph_bounds P_ ((struct frame *));
430 static void x_draw_phys_cursor_glyph P_ ((struct window *,
431 struct glyph_row *,
432 enum draw_glyphs_face));
433 static void x_update_end P_ ((struct frame *));
434 static void XTframe_up_to_date P_ ((struct frame *));
435 static void XTreassert_line_highlight P_ ((int, int));
436 static void x_change_line_highlight P_ ((int, int, int, int));
437 static void XTset_terminal_modes P_ ((void));
438 static void XTreset_terminal_modes P_ ((void));
439 static void XTcursor_to P_ ((int, int, int, int));
440 static void x_write_glyphs P_ ((struct glyph *, int));
441 static void x_clear_end_of_line P_ ((int));
442 static void x_clear_frame P_ ((void));
443 static void x_clear_cursor P_ ((struct window *));
444 static void frame_highlight P_ ((struct frame *));
445 static void frame_unhighlight P_ ((struct frame *));
446 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
447 static void XTframe_rehighlight P_ ((struct frame *));
448 static void x_frame_rehighlight P_ ((struct x_display_info *));
449 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
450 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
451 static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
452 XRectangle *));
453 static void expose_frame P_ ((struct frame *, int, int, int, int));
454 static int expose_window_tree P_ ((struct window *, XRectangle *));
455 static int expose_window P_ ((struct window *, XRectangle *));
456 static void expose_area P_ ((struct window *, struct glyph_row *,
457 XRectangle *, enum glyph_row_area));
458 static int expose_line P_ ((struct window *, struct glyph_row *,
459 XRectangle *));
460 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
461 static void x_update_window_cursor P_ ((struct window *, int));
462 static void x_erase_phys_cursor P_ ((struct window *));
463 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
464 static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
465 enum bitmap_type));
466
467 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
468 GC, int));
469 static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
470 static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
471 static void notice_overwritten_cursor P_ ((struct window *, int, int));
472 static void x_flush P_ ((struct frame *f));
473 static void x_update_begin P_ ((struct frame *));
474 static void x_update_window_begin P_ ((struct window *));
475 static void x_draw_vertical_border P_ ((struct window *));
476 static void x_after_update_window_line P_ ((struct glyph_row *));
477 static INLINE void take_vertical_position_into_account P_ ((struct it *));
478 static void x_produce_stretch_glyph P_ ((struct it *));
479 static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
480 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
481 enum scroll_bar_part *,
482 Lisp_Object *, Lisp_Object *,
483 unsigned long *));
484
485 /* Flush display of frame F, or of all frames if F is null. */
486
487 static void
488 x_flush (f)
489 struct frame *f;
490 {
491 BLOCK_INPUT;
492 if (f == NULL)
493 {
494 Lisp_Object rest, frame;
495 FOR_EACH_FRAME (rest, frame)
496 x_flush (XFRAME (frame));
497 }
498 else if (FRAME_X_P (f))
499 XFlush (FRAME_X_DISPLAY (f));
500 UNBLOCK_INPUT;
501 }
502
503
504 /* Remove calls to XFlush by defining XFlush to an empty replacement.
505 Calls to XFlush should be unnecessary because the X output buffer
506 is flushed automatically as needed by calls to XPending,
507 XNextEvent, or XWindowEvent according to the XFlush man page.
508 XTread_socket calls XPending. Removing XFlush improves
509 performance. */
510
511 #define XFlush(DISPLAY) (void) 0
512
513 \f
514 /***********************************************************************
515 Debugging
516 ***********************************************************************/
517
518 #if 0
519
520 /* This is a function useful for recording debugging information about
521 the sequence of occurrences in this file. */
522
523 struct record
524 {
525 char *locus;
526 int type;
527 };
528
529 struct record event_record[100];
530
531 int event_record_index;
532
533 record_event (locus, type)
534 char *locus;
535 int type;
536 {
537 if (event_record_index == sizeof (event_record) / sizeof (struct record))
538 event_record_index = 0;
539
540 event_record[event_record_index].locus = locus;
541 event_record[event_record_index].type = type;
542 event_record_index++;
543 }
544
545 #endif /* 0 */
546
547
548 \f
549 /* Return the struct x_display_info corresponding to DPY. */
550
551 struct x_display_info *
552 x_display_info_for_display (dpy)
553 Display *dpy;
554 {
555 struct x_display_info *dpyinfo;
556
557 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
558 if (dpyinfo->display == dpy)
559 return dpyinfo;
560
561 return 0;
562 }
563
564
565 \f
566 /***********************************************************************
567 Starting and ending an update
568 ***********************************************************************/
569
570 /* Start an update of frame F. This function is installed as a hook
571 for update_begin, i.e. it is called when update_begin is called.
572 This function is called prior to calls to x_update_window_begin for
573 each window being updated. Currently, there is nothing to do here
574 because all interesting stuff is done on a window basis. */
575
576 static void
577 x_update_begin (f)
578 struct frame *f;
579 {
580 /* Nothing to do. */
581 }
582
583
584 /* Start update of window W. Set the global variable updated_window
585 to the window being updated and set output_cursor to the cursor
586 position of W. */
587
588 static void
589 x_update_window_begin (w)
590 struct window *w;
591 {
592 struct frame *f = XFRAME (WINDOW_FRAME (w));
593 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
594
595 updated_window = w;
596 set_output_cursor (&w->cursor);
597
598 BLOCK_INPUT;
599
600 if (f == display_info->mouse_face_mouse_frame)
601 {
602 /* Don't do highlighting for mouse motion during the update. */
603 display_info->mouse_face_defer = 1;
604
605 /* If F needs to be redrawn, simply forget about any prior mouse
606 highlighting. */
607 if (FRAME_GARBAGED_P (f))
608 display_info->mouse_face_window = Qnil;
609
610 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
611 their mouse_face_p flag set, which means that they are always
612 unequal to rows in a desired matrix which never have that
613 flag set. So, rows containing mouse-face glyphs are never
614 scrolled, and we don't have to switch the mouse highlight off
615 here to prevent it from being scrolled. */
616
617 /* Can we tell that this update does not affect the window
618 where the mouse highlight is? If so, no need to turn off.
619 Likewise, don't do anything if the frame is garbaged;
620 in that case, the frame's current matrix that we would use
621 is all wrong, and we will redisplay that line anyway. */
622 if (!NILP (display_info->mouse_face_window)
623 && w == XWINDOW (display_info->mouse_face_window))
624 {
625 int i;
626
627 for (i = 0; i < w->desired_matrix->nrows; ++i)
628 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
629 break;
630
631 if (i < w->desired_matrix->nrows)
632 clear_mouse_face (display_info);
633 }
634 #endif /* 0 */
635 }
636
637 UNBLOCK_INPUT;
638 }
639
640
641 /* Draw a vertical window border to the right of window W if W doesn't
642 have vertical scroll bars. */
643
644 static void
645 x_draw_vertical_border (w)
646 struct window *w;
647 {
648 struct frame *f = XFRAME (WINDOW_FRAME (w));
649
650 /* Redraw borders between horizontally adjacent windows. Don't
651 do it for frames with vertical scroll bars because either the
652 right scroll bar of a window, or the left scroll bar of its
653 neighbor will suffice as a border. */
654 if (!WINDOW_RIGHTMOST_P (w)
655 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
656 {
657 int x0, x1, y0, y1;
658
659 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
660 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
661 y1 -= 1;
662
663 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
664 f->output_data.x->normal_gc, x1, y0, x1, y1);
665 }
666 }
667
668
669 /* End update of window W (which is equal to updated_window).
670
671 Draw vertical borders between horizontally adjacent windows, and
672 display W's cursor if CURSOR_ON_P is non-zero.
673
674 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
675 glyphs in mouse-face were overwritten. In that case we have to
676 make sure that the mouse-highlight is properly redrawn.
677
678 W may be a menu bar pseudo-window in case we don't have X toolkit
679 support. Such windows don't have a cursor, so don't display it
680 here. */
681
682 static void
683 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
684 struct window *w;
685 int cursor_on_p, mouse_face_overwritten_p;
686 {
687 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
688
689 if (!w->pseudo_window_p)
690 {
691 BLOCK_INPUT;
692
693 if (cursor_on_p)
694 x_display_and_set_cursor (w, 1, output_cursor.hpos,
695 output_cursor.vpos,
696 output_cursor.x, output_cursor.y);
697
698 x_draw_vertical_border (w);
699 UNBLOCK_INPUT;
700 }
701
702 /* If a row with mouse-face was overwritten, arrange for
703 XTframe_up_to_date to redisplay the mouse highlight. */
704 if (mouse_face_overwritten_p)
705 {
706 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
707 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
708 dpyinfo->mouse_face_window = Qnil;
709 }
710
711 updated_window = NULL;
712 }
713
714
715 /* End update of frame F. This function is installed as a hook in
716 update_end. */
717
718 static void
719 x_update_end (f)
720 struct frame *f;
721 {
722 /* Mouse highlight may be displayed again. */
723 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
724
725 BLOCK_INPUT;
726 XFlush (FRAME_X_DISPLAY (f));
727 UNBLOCK_INPUT;
728 }
729
730
731 /* This function is called from various places in xdisp.c whenever a
732 complete update has been performed. The global variable
733 updated_window is not available here. */
734
735 static void
736 XTframe_up_to_date (f)
737 struct frame *f;
738 {
739 if (FRAME_X_P (f))
740 {
741 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
742
743 if (dpyinfo->mouse_face_deferred_gc
744 || f == dpyinfo->mouse_face_mouse_frame)
745 {
746 BLOCK_INPUT;
747 if (dpyinfo->mouse_face_mouse_frame)
748 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
749 dpyinfo->mouse_face_mouse_x,
750 dpyinfo->mouse_face_mouse_y);
751 dpyinfo->mouse_face_deferred_gc = 0;
752 UNBLOCK_INPUT;
753 }
754 }
755 }
756
757
758 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
759 arrow bitmaps, or clear the areas where they would be displayed
760 before DESIRED_ROW is made current. The window being updated is
761 found in updated_window. This function It is called from
762 update_window_line only if it is known that there are differences
763 between bitmaps to be drawn between current row and DESIRED_ROW. */
764
765 static void
766 x_after_update_window_line (desired_row)
767 struct glyph_row *desired_row;
768 {
769 struct window *w = updated_window;
770
771 xassert (w);
772
773 if (!desired_row->mode_line_p && !w->pseudo_window_p)
774 {
775 struct frame *f;
776 int width;
777
778 BLOCK_INPUT;
779 x_draw_row_bitmaps (w, desired_row);
780
781 /* When a window has disappeared, make sure that no rest of
782 full-width rows stays visible in the internal border. */
783 if (windows_or_buffers_changed
784 && (f = XFRAME (w->frame),
785 width = FRAME_INTERNAL_BORDER_WIDTH (f),
786 width != 0))
787 {
788 int height = desired_row->visible_height;
789 int x = (window_box_right (w, -1)
790 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
791 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
792
793 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
794 x, y, width, height, False);
795 }
796
797 UNBLOCK_INPUT;
798 }
799 }
800
801
802 /* Draw the bitmap WHICH in one of the areas to the left or right of
803 window W. ROW is the glyph row for which to display the bitmap; it
804 determines the vertical position at which the bitmap has to be
805 drawn. */
806
807 static void
808 x_draw_bitmap (w, row, which)
809 struct window *w;
810 struct glyph_row *row;
811 enum bitmap_type which;
812 {
813 struct frame *f = XFRAME (WINDOW_FRAME (w));
814 Display *display = FRAME_X_DISPLAY (f);
815 Window window = FRAME_X_WINDOW (f);
816 int x, y, wd, h, dy;
817 unsigned char *bits;
818 Pixmap pixmap;
819 GC gc = f->output_data.x->normal_gc;
820 struct face *face;
821 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
822
823 /* Must clip because of partially visible lines. */
824 x_clip_to_row (w, row, gc, 1);
825
826 switch (which)
827 {
828 case LEFT_TRUNCATION_BITMAP:
829 wd = left_width;
830 h = left_height;
831 bits = left_bits;
832 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
833 - wd
834 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
835 break;
836
837 case OVERLAY_ARROW_BITMAP:
838 wd = left_width;
839 h = left_height;
840 bits = ov_bits;
841 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
842 - wd
843 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
844 break;
845
846 case RIGHT_TRUNCATION_BITMAP:
847 wd = right_width;
848 h = right_height;
849 bits = right_bits;
850 x = window_box_right (w, -1);
851 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
852 break;
853
854 case CONTINUED_LINE_BITMAP:
855 wd = right_width;
856 h = right_height;
857 bits = continued_bits;
858 x = window_box_right (w, -1);
859 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
860 break;
861
862 case CONTINUATION_LINE_BITMAP:
863 wd = continuation_width;
864 h = continuation_height;
865 bits = continuation_bits;
866 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
867 - wd
868 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
869 break;
870
871 case ZV_LINE_BITMAP:
872 wd = zv_width;
873 h = zv_height;
874 bits = zv_bits;
875 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
876 - wd
877 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
878 break;
879
880 default:
881 abort ();
882 }
883
884 /* Convert to frame coordinates. Set dy to the offset in the row to
885 start drawing the bitmap. */
886 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
887 dy = (row->height - h) / 2;
888
889 /* Draw the bitmap. I believe these small pixmaps can be cached
890 by the server. */
891 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
892 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
893 face->foreground,
894 face->background, depth);
895 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
896 XFreePixmap (display, pixmap);
897 XSetClipMask (display, gc, None);
898 }
899
900
901 /* Draw flags bitmaps for glyph row ROW on window W. Call this
902 function with input blocked. */
903
904 static void
905 x_draw_row_bitmaps (w, row)
906 struct window *w;
907 struct glyph_row *row;
908 {
909 struct frame *f = XFRAME (w->frame);
910 enum bitmap_type bitmap;
911 struct face *face;
912 int header_line_height = -1;
913
914 xassert (interrupt_input_blocked);
915
916 /* If row is completely invisible, because of vscrolling, we
917 don't have to draw anything. */
918 if (row->visible_height <= 0)
919 return;
920
921 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
922 PREPARE_FACE_FOR_DISPLAY (f, face);
923
924 /* Decide which bitmap to draw at the left side. */
925 if (row->overlay_arrow_p)
926 bitmap = OVERLAY_ARROW_BITMAP;
927 else if (row->truncated_on_left_p)
928 bitmap = LEFT_TRUNCATION_BITMAP;
929 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
930 bitmap = CONTINUATION_LINE_BITMAP;
931 else if (row->indicate_empty_line_p)
932 bitmap = ZV_LINE_BITMAP;
933 else
934 bitmap = NO_BITMAP;
935
936 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
937 the flags area. */
938 if (bitmap == NO_BITMAP
939 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
940 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
941 {
942 /* If W has a vertical border to its left, don't draw over it. */
943 int border = ((XFASTINT (w->left) > 0
944 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
945 ? 1 : 0);
946 int left = window_box_left (w, -1);
947
948 if (header_line_height < 0)
949 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
950
951 /* In case the same realized face is used for bitmap areas and
952 for something displayed in the text (e.g. face `region' on
953 mono-displays, the fill style may have been changed to
954 FillSolid in x_draw_glyph_string_background. */
955 if (face->stipple)
956 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
957 else
958 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
959
960 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
961 face->gc,
962 (left
963 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
964 + border),
965 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
966 row->y)),
967 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
968 row->visible_height);
969 if (!face->stipple)
970 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
971 }
972
973 /* Draw the left bitmap. */
974 if (bitmap != NO_BITMAP)
975 x_draw_bitmap (w, row, bitmap);
976
977 /* Decide which bitmap to draw at the right side. */
978 if (row->truncated_on_right_p)
979 bitmap = RIGHT_TRUNCATION_BITMAP;
980 else if (row->continued_p)
981 bitmap = CONTINUED_LINE_BITMAP;
982 else
983 bitmap = NO_BITMAP;
984
985 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
986 the flags area. */
987 if (bitmap == NO_BITMAP
988 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
989 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
990 {
991 int right = window_box_right (w, -1);
992
993 if (header_line_height < 0)
994 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
995
996 /* In case the same realized face is used for bitmap areas and
997 for something displayed in the text (e.g. face `region' on
998 mono-displays, the fill style may have been changed to
999 FillSolid in x_draw_glyph_string_background. */
1000 if (face->stipple)
1001 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
1002 else
1003 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
1004 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1005 face->gc,
1006 right,
1007 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
1008 row->y)),
1009 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
1010 row->visible_height);
1011 if (!face->stipple)
1012 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
1013 }
1014
1015 /* Draw the right bitmap. */
1016 if (bitmap != NO_BITMAP)
1017 x_draw_bitmap (w, row, bitmap);
1018 }
1019
1020 \f
1021 /***********************************************************************
1022 Line Highlighting
1023 ***********************************************************************/
1024
1025 /* External interface to control of standout mode. Not used for X
1026 frames. Aborts when called. */
1027
1028 static void
1029 XTreassert_line_highlight (new, vpos)
1030 int new, vpos;
1031 {
1032 abort ();
1033 }
1034
1035
1036 /* Call this when about to modify line at position VPOS and change
1037 whether it is highlighted. Not used for X frames. Aborts when
1038 called. */
1039
1040 static void
1041 x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1042 int new_highlight, vpos, y, first_unused_hpos;
1043 {
1044 abort ();
1045 }
1046
1047
1048 /* This is called when starting Emacs and when restarting after
1049 suspend. When starting Emacs, no X window is mapped. And nothing
1050 must be done to Emacs's own window if it is suspended (though that
1051 rarely happens). */
1052
1053 static void
1054 XTset_terminal_modes ()
1055 {
1056 }
1057
1058 /* This is called when exiting or suspending Emacs. Exiting will make
1059 the X-windows go away, and suspending requires no action. */
1060
1061 static void
1062 XTreset_terminal_modes ()
1063 {
1064 }
1065
1066
1067 \f
1068 /***********************************************************************
1069 Output Cursor
1070 ***********************************************************************/
1071
1072 /* Set the global variable output_cursor to CURSOR. All cursor
1073 positions are relative to updated_window. */
1074
1075 static void
1076 set_output_cursor (cursor)
1077 struct cursor_pos *cursor;
1078 {
1079 output_cursor.hpos = cursor->hpos;
1080 output_cursor.vpos = cursor->vpos;
1081 output_cursor.x = cursor->x;
1082 output_cursor.y = cursor->y;
1083 }
1084
1085
1086 /* Set a nominal cursor position.
1087
1088 HPOS and VPOS are column/row positions in a window glyph matrix. X
1089 and Y are window text area relative pixel positions.
1090
1091 If this is done during an update, updated_window will contain the
1092 window that is being updated and the position is the future output
1093 cursor position for that window. If updated_window is null, use
1094 selected_window and display the cursor at the given position. */
1095
1096 static void
1097 XTcursor_to (vpos, hpos, y, x)
1098 int vpos, hpos, y, x;
1099 {
1100 struct window *w;
1101
1102 /* If updated_window is not set, work on selected_window. */
1103 if (updated_window)
1104 w = updated_window;
1105 else
1106 w = XWINDOW (selected_window);
1107
1108 /* Set the output cursor. */
1109 output_cursor.hpos = hpos;
1110 output_cursor.vpos = vpos;
1111 output_cursor.x = x;
1112 output_cursor.y = y;
1113
1114 /* If not called as part of an update, really display the cursor.
1115 This will also set the cursor position of W. */
1116 if (updated_window == NULL)
1117 {
1118 BLOCK_INPUT;
1119 x_display_cursor (w, 1, hpos, vpos, x, y);
1120 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1121 UNBLOCK_INPUT;
1122 }
1123 }
1124
1125
1126 \f
1127 /***********************************************************************
1128 Display Iterator
1129 ***********************************************************************/
1130
1131 /* Function prototypes of this page. */
1132
1133 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1134 struct glyph *,
1135 XChar2b *,
1136 int *));
1137 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1138 int, XChar2b *, int));
1139 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1140 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1141 static void x_append_glyph P_ ((struct it *));
1142 static void x_append_composite_glyph P_ ((struct it *));
1143 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1144 int, int, double));
1145 static void x_produce_glyphs P_ ((struct it *));
1146 static void x_produce_image_glyph P_ ((struct it *it));
1147
1148
1149 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1150 is not contained in the font. */
1151
1152 static INLINE XCharStruct *
1153 x_per_char_metric (font, char2b)
1154 XFontStruct *font;
1155 XChar2b *char2b;
1156 {
1157 /* The result metric information. */
1158 XCharStruct *pcm = NULL;
1159
1160 xassert (font && char2b);
1161
1162 if (font->per_char != NULL)
1163 {
1164 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1165 {
1166 /* min_char_or_byte2 specifies the linear character index
1167 corresponding to the first element of the per_char array,
1168 max_char_or_byte2 is the index of the last character. A
1169 character with non-zero CHAR2B->byte1 is not in the font.
1170 A character with byte2 less than min_char_or_byte2 or
1171 greater max_char_or_byte2 is not in the font. */
1172 if (char2b->byte1 == 0
1173 && char2b->byte2 >= font->min_char_or_byte2
1174 && char2b->byte2 <= font->max_char_or_byte2)
1175 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1176 }
1177 else
1178 {
1179 /* If either min_byte1 or max_byte1 are nonzero, both
1180 min_char_or_byte2 and max_char_or_byte2 are less than
1181 256, and the 2-byte character index values corresponding
1182 to the per_char array element N (counting from 0) are:
1183
1184 byte1 = N/D + min_byte1
1185 byte2 = N\D + min_char_or_byte2
1186
1187 where:
1188
1189 D = max_char_or_byte2 - min_char_or_byte2 + 1
1190 / = integer division
1191 \ = integer modulus */
1192 if (char2b->byte1 >= font->min_byte1
1193 && char2b->byte1 <= font->max_byte1
1194 && char2b->byte2 >= font->min_char_or_byte2
1195 && char2b->byte2 <= font->max_char_or_byte2)
1196 {
1197 pcm = (font->per_char
1198 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1199 * (char2b->byte1 - font->min_byte1))
1200 + (char2b->byte2 - font->min_char_or_byte2));
1201 }
1202 }
1203 }
1204 else
1205 {
1206 /* If the per_char pointer is null, all glyphs between the first
1207 and last character indexes inclusive have the same
1208 information, as given by both min_bounds and max_bounds. */
1209 if (char2b->byte2 >= font->min_char_or_byte2
1210 && char2b->byte2 <= font->max_char_or_byte2)
1211 pcm = &font->max_bounds;
1212 }
1213
1214 return ((pcm == NULL
1215 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1216 ? NULL : pcm);
1217 }
1218
1219
1220 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1221 the two-byte form of C. Encoding is returned in *CHAR2B. */
1222
1223 static INLINE void
1224 x_encode_char (c, char2b, font_info)
1225 int c;
1226 XChar2b *char2b;
1227 struct font_info *font_info;
1228 {
1229 int charset = CHAR_CHARSET (c);
1230 XFontStruct *font = font_info->font;
1231
1232 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1233 This may be either a program in a special encoder language or a
1234 fixed encoding. */
1235 if (font_info->font_encoder)
1236 {
1237 /* It's a program. */
1238 struct ccl_program *ccl = font_info->font_encoder;
1239
1240 if (CHARSET_DIMENSION (charset) == 1)
1241 {
1242 ccl->reg[0] = charset;
1243 ccl->reg[1] = char2b->byte2;
1244 }
1245 else
1246 {
1247 ccl->reg[0] = charset;
1248 ccl->reg[1] = char2b->byte1;
1249 ccl->reg[2] = char2b->byte2;
1250 }
1251
1252 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1253
1254 /* We assume that MSBs are appropriately set/reset by CCL
1255 program. */
1256 if (font->max_byte1 == 0) /* 1-byte font */
1257 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1258 else
1259 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1260 }
1261 else if (font_info->encoding[charset])
1262 {
1263 /* Fixed encoding scheme. See fontset.h for the meaning of the
1264 encoding numbers. */
1265 int enc = font_info->encoding[charset];
1266
1267 if ((enc == 1 || enc == 2)
1268 && CHARSET_DIMENSION (charset) == 2)
1269 char2b->byte1 |= 0x80;
1270
1271 if (enc == 1 || enc == 3)
1272 char2b->byte2 |= 0x80;
1273 }
1274 }
1275
1276
1277 /* Get face and two-byte form of character C in face FACE_ID on frame
1278 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1279 means we want to display multibyte text. Value is a pointer to a
1280 realized face that is ready for display. */
1281
1282 static INLINE struct face *
1283 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1284 struct frame *f;
1285 int c, face_id;
1286 XChar2b *char2b;
1287 int multibyte_p;
1288 {
1289 struct face *face = FACE_FROM_ID (f, face_id);
1290
1291 if (!multibyte_p)
1292 {
1293 /* Unibyte case. We don't have to encode, but we have to make
1294 sure to use a face suitable for unibyte. */
1295 char2b->byte1 = 0;
1296 char2b->byte2 = c;
1297 face_id = FACE_FOR_CHAR (f, face, c);
1298 face = FACE_FROM_ID (f, face_id);
1299 }
1300 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1301 {
1302 /* Case of ASCII in a face known to fit ASCII. */
1303 char2b->byte1 = 0;
1304 char2b->byte2 = c;
1305 }
1306 else
1307 {
1308 int c1, c2, charset;
1309
1310 /* Split characters into bytes. If c2 is -1 afterwards, C is
1311 really a one-byte character so that byte1 is zero. */
1312 SPLIT_CHAR (c, charset, c1, c2);
1313 if (c2 > 0)
1314 char2b->byte1 = c1, char2b->byte2 = c2;
1315 else
1316 char2b->byte1 = 0, char2b->byte2 = c1;
1317
1318 /* Maybe encode the character in *CHAR2B. */
1319 if (face->font != NULL)
1320 {
1321 struct font_info *font_info
1322 = FONT_INFO_FROM_ID (f, face->font_info_id);
1323 if (font_info)
1324 x_encode_char (c, char2b, font_info);
1325 }
1326 }
1327
1328 /* Make sure X resources of the face are allocated. */
1329 xassert (face != NULL);
1330 PREPARE_FACE_FOR_DISPLAY (f, face);
1331
1332 return face;
1333 }
1334
1335
1336 /* Get face and two-byte form of character glyph GLYPH on frame F.
1337 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1338 a pointer to a realized face that is ready for display. */
1339
1340 static INLINE struct face *
1341 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1342 struct frame *f;
1343 struct glyph *glyph;
1344 XChar2b *char2b;
1345 int *two_byte_p;
1346 {
1347 struct face *face;
1348
1349 xassert (glyph->type == CHAR_GLYPH);
1350 face = FACE_FROM_ID (f, glyph->face_id);
1351
1352 if (two_byte_p)
1353 *two_byte_p = 0;
1354
1355 if (!glyph->multibyte_p)
1356 {
1357 /* Unibyte case. We don't have to encode, but we have to make
1358 sure to use a face suitable for unibyte. */
1359 char2b->byte1 = 0;
1360 char2b->byte2 = glyph->u.ch;
1361 }
1362 else if (glyph->u.ch < 128
1363 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1364 {
1365 /* Case of ASCII in a face known to fit ASCII. */
1366 char2b->byte1 = 0;
1367 char2b->byte2 = glyph->u.ch;
1368 }
1369 else
1370 {
1371 int c1, c2, charset;
1372
1373 /* Split characters into bytes. If c2 is -1 afterwards, C is
1374 really a one-byte character so that byte1 is zero. */
1375 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1376 if (c2 > 0)
1377 char2b->byte1 = c1, char2b->byte2 = c2;
1378 else
1379 char2b->byte1 = 0, char2b->byte2 = c1;
1380
1381 /* Maybe encode the character in *CHAR2B. */
1382 if (charset != CHARSET_ASCII)
1383 {
1384 struct font_info *font_info
1385 = FONT_INFO_FROM_ID (f, face->font_info_id);
1386 if (font_info)
1387 {
1388 x_encode_char (glyph->u.ch, char2b, font_info);
1389 if (two_byte_p)
1390 *two_byte_p
1391 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1392 }
1393 }
1394 }
1395
1396 /* Make sure X resources of the face are allocated. */
1397 xassert (face != NULL);
1398 PREPARE_FACE_FOR_DISPLAY (f, face);
1399 return face;
1400 }
1401
1402
1403 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1404 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1405
1406 static INLINE void
1407 x_append_glyph (it)
1408 struct it *it;
1409 {
1410 struct glyph *glyph;
1411 enum glyph_row_area area = it->area;
1412
1413 xassert (it->glyph_row);
1414 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1415
1416 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1417 if (glyph < it->glyph_row->glyphs[area + 1])
1418 {
1419 glyph->charpos = CHARPOS (it->position);
1420 glyph->object = it->object;
1421 glyph->pixel_width = it->pixel_width;
1422 glyph->voffset = it->voffset;
1423 glyph->type = CHAR_GLYPH;
1424 glyph->multibyte_p = it->multibyte_p;
1425 glyph->left_box_line_p = it->start_of_box_run_p;
1426 glyph->right_box_line_p = it->end_of_box_run_p;
1427 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1428 || it->phys_descent > it->descent);
1429 glyph->padding_p = 0;
1430 glyph->glyph_not_available_p = it->glyph_not_available_p;
1431 glyph->face_id = it->face_id;
1432 glyph->u.ch = it->char_to_display;
1433 ++it->glyph_row->used[area];
1434 }
1435 }
1436
1437 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1438 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1439
1440 static INLINE void
1441 x_append_composite_glyph (it)
1442 struct it *it;
1443 {
1444 struct glyph *glyph;
1445 enum glyph_row_area area = it->area;
1446
1447 xassert (it->glyph_row);
1448
1449 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1450 if (glyph < it->glyph_row->glyphs[area + 1])
1451 {
1452 glyph->charpos = CHARPOS (it->position);
1453 glyph->object = it->object;
1454 glyph->pixel_width = it->pixel_width;
1455 glyph->voffset = it->voffset;
1456 glyph->type = COMPOSITE_GLYPH;
1457 glyph->multibyte_p = it->multibyte_p;
1458 glyph->left_box_line_p = it->start_of_box_run_p;
1459 glyph->right_box_line_p = it->end_of_box_run_p;
1460 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1461 || it->phys_descent > it->descent);
1462 glyph->padding_p = 0;
1463 glyph->glyph_not_available_p = 0;
1464 glyph->face_id = it->face_id;
1465 glyph->u.cmp_id = it->cmp_id;
1466 ++it->glyph_row->used[area];
1467 }
1468 }
1469
1470
1471 /* Change IT->ascent and IT->height according to the setting of
1472 IT->voffset. */
1473
1474 static INLINE void
1475 take_vertical_position_into_account (it)
1476 struct it *it;
1477 {
1478 if (it->voffset)
1479 {
1480 if (it->voffset < 0)
1481 /* Increase the ascent so that we can display the text higher
1482 in the line. */
1483 it->ascent += abs (it->voffset);
1484 else
1485 /* Increase the descent so that we can display the text lower
1486 in the line. */
1487 it->descent += it->voffset;
1488 }
1489 }
1490
1491
1492 /* Produce glyphs/get display metrics for the image IT is loaded with.
1493 See the description of struct display_iterator in dispextern.h for
1494 an overview of struct display_iterator. */
1495
1496 static void
1497 x_produce_image_glyph (it)
1498 struct it *it;
1499 {
1500 struct image *img;
1501 struct face *face;
1502
1503 xassert (it->what == IT_IMAGE);
1504
1505 face = FACE_FROM_ID (it->f, it->face_id);
1506 img = IMAGE_FROM_ID (it->f, it->image_id);
1507 xassert (img);
1508
1509 /* Make sure X resources of the face and image are loaded. */
1510 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1511 prepare_image_for_display (it->f, img);
1512
1513 it->ascent = it->phys_ascent = image_ascent (img, face);
1514 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1515 it->pixel_width = img->width + 2 * img->hmargin;
1516
1517 it->nglyphs = 1;
1518
1519 if (face->box != FACE_NO_BOX)
1520 {
1521 if (face->box_line_width > 0)
1522 {
1523 it->ascent += face->box_line_width;
1524 it->descent += face->box_line_width;
1525 }
1526
1527 if (it->start_of_box_run_p)
1528 it->pixel_width += abs (face->box_line_width);
1529 if (it->end_of_box_run_p)
1530 it->pixel_width += abs (face->box_line_width);
1531 }
1532
1533 take_vertical_position_into_account (it);
1534
1535 if (it->glyph_row)
1536 {
1537 struct glyph *glyph;
1538 enum glyph_row_area area = it->area;
1539
1540 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1541 if (glyph < it->glyph_row->glyphs[area + 1])
1542 {
1543 glyph->charpos = CHARPOS (it->position);
1544 glyph->object = it->object;
1545 glyph->pixel_width = it->pixel_width;
1546 glyph->voffset = it->voffset;
1547 glyph->type = IMAGE_GLYPH;
1548 glyph->multibyte_p = it->multibyte_p;
1549 glyph->left_box_line_p = it->start_of_box_run_p;
1550 glyph->right_box_line_p = it->end_of_box_run_p;
1551 glyph->overlaps_vertically_p = 0;
1552 glyph->padding_p = 0;
1553 glyph->glyph_not_available_p = 0;
1554 glyph->face_id = it->face_id;
1555 glyph->u.img_id = img->id;
1556 ++it->glyph_row->used[area];
1557 }
1558 }
1559 }
1560
1561
1562 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1563 of the glyph, WIDTH and HEIGHT are the width and height of the
1564 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1565 ascent of the glyph (0 <= ASCENT <= 1). */
1566
1567 static void
1568 x_append_stretch_glyph (it, object, width, height, ascent)
1569 struct it *it;
1570 Lisp_Object object;
1571 int width, height;
1572 double ascent;
1573 {
1574 struct glyph *glyph;
1575 enum glyph_row_area area = it->area;
1576
1577 xassert (ascent >= 0 && ascent <= 1);
1578
1579 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1580 if (glyph < it->glyph_row->glyphs[area + 1])
1581 {
1582 glyph->charpos = CHARPOS (it->position);
1583 glyph->object = object;
1584 glyph->pixel_width = width;
1585 glyph->voffset = it->voffset;
1586 glyph->type = STRETCH_GLYPH;
1587 glyph->multibyte_p = it->multibyte_p;
1588 glyph->left_box_line_p = it->start_of_box_run_p;
1589 glyph->right_box_line_p = it->end_of_box_run_p;
1590 glyph->overlaps_vertically_p = 0;
1591 glyph->padding_p = 0;
1592 glyph->glyph_not_available_p = 0;
1593 glyph->face_id = it->face_id;
1594 glyph->u.stretch.ascent = height * ascent;
1595 glyph->u.stretch.height = height;
1596 ++it->glyph_row->used[area];
1597 }
1598 }
1599
1600
1601 /* Produce a stretch glyph for iterator IT. IT->object is the value
1602 of the glyph property displayed. The value must be a list
1603 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1604 being recognized:
1605
1606 1. `:width WIDTH' specifies that the space should be WIDTH *
1607 canonical char width wide. WIDTH may be an integer or floating
1608 point number.
1609
1610 2. `:relative-width FACTOR' specifies that the width of the stretch
1611 should be computed from the width of the first character having the
1612 `glyph' property, and should be FACTOR times that width.
1613
1614 3. `:align-to HPOS' specifies that the space should be wide enough
1615 to reach HPOS, a value in canonical character units.
1616
1617 Exactly one of the above pairs must be present.
1618
1619 4. `:height HEIGHT' specifies that the height of the stretch produced
1620 should be HEIGHT, measured in canonical character units.
1621
1622 5. `:relative-height FACTOR' specifies that the height of the the
1623 stretch should be FACTOR times the height of the characters having
1624 the glyph property.
1625
1626 Either none or exactly one of 4 or 5 must be present.
1627
1628 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1629 of the stretch should be used for the ascent of the stretch.
1630 ASCENT must be in the range 0 <= ASCENT <= 100. */
1631
1632 #define NUMVAL(X) \
1633 ((INTEGERP (X) || FLOATP (X)) \
1634 ? XFLOATINT (X) \
1635 : - 1)
1636
1637
1638 static void
1639 x_produce_stretch_glyph (it)
1640 struct it *it;
1641 {
1642 /* (space :width WIDTH :height HEIGHT. */
1643 #if GLYPH_DEBUG
1644 extern Lisp_Object Qspace;
1645 #endif
1646 extern Lisp_Object QCwidth, QCheight, QCascent;
1647 extern Lisp_Object QCrelative_width, QCrelative_height;
1648 extern Lisp_Object QCalign_to;
1649 Lisp_Object prop, plist;
1650 double width = 0, height = 0, ascent = 0;
1651 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1652 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1653
1654 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1655
1656 /* List should start with `space'. */
1657 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1658 plist = XCDR (it->object);
1659
1660 /* Compute the width of the stretch. */
1661 if (prop = Fplist_get (plist, QCwidth),
1662 NUMVAL (prop) > 0)
1663 /* Absolute width `:width WIDTH' specified and valid. */
1664 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1665 else if (prop = Fplist_get (plist, QCrelative_width),
1666 NUMVAL (prop) > 0)
1667 {
1668 /* Relative width `:relative-width FACTOR' specified and valid.
1669 Compute the width of the characters having the `glyph'
1670 property. */
1671 struct it it2;
1672 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1673
1674 it2 = *it;
1675 if (it->multibyte_p)
1676 {
1677 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1678 - IT_BYTEPOS (*it));
1679 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1680 }
1681 else
1682 it2.c = *p, it2.len = 1;
1683
1684 it2.glyph_row = NULL;
1685 it2.what = IT_CHARACTER;
1686 x_produce_glyphs (&it2);
1687 width = NUMVAL (prop) * it2.pixel_width;
1688 }
1689 else if (prop = Fplist_get (plist, QCalign_to),
1690 NUMVAL (prop) > 0)
1691 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1692 else
1693 /* Nothing specified -> width defaults to canonical char width. */
1694 width = CANON_X_UNIT (it->f);
1695
1696 /* Compute height. */
1697 if (prop = Fplist_get (plist, QCheight),
1698 NUMVAL (prop) > 0)
1699 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1700 else if (prop = Fplist_get (plist, QCrelative_height),
1701 NUMVAL (prop) > 0)
1702 height = FONT_HEIGHT (font) * NUMVAL (prop);
1703 else
1704 height = FONT_HEIGHT (font);
1705
1706 /* Compute percentage of height used for ascent. If
1707 `:ascent ASCENT' is present and valid, use that. Otherwise,
1708 derive the ascent from the font in use. */
1709 if (prop = Fplist_get (plist, QCascent),
1710 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1711 ascent = NUMVAL (prop) / 100.0;
1712 else
1713 ascent = (double) font->ascent / FONT_HEIGHT (font);
1714
1715 if (width <= 0)
1716 width = 1;
1717 if (height <= 0)
1718 height = 1;
1719
1720 if (it->glyph_row)
1721 {
1722 Lisp_Object object = it->stack[it->sp - 1].string;
1723 if (!STRINGP (object))
1724 object = it->w->buffer;
1725 x_append_stretch_glyph (it, object, width, height, ascent);
1726 }
1727
1728 it->pixel_width = width;
1729 it->ascent = it->phys_ascent = height * ascent;
1730 it->descent = it->phys_descent = height - it->ascent;
1731 it->nglyphs = 1;
1732
1733 if (face->box != FACE_NO_BOX)
1734 {
1735 if (face->box_line_width > 0)
1736 {
1737 it->ascent += face->box_line_width;
1738 it->descent += face->box_line_width;
1739 }
1740
1741 if (it->start_of_box_run_p)
1742 it->pixel_width += abs (face->box_line_width);
1743 if (it->end_of_box_run_p)
1744 it->pixel_width += abs (face->box_line_width);
1745 }
1746
1747 take_vertical_position_into_account (it);
1748 }
1749
1750 /* Return proper value to be used as baseline offset of font that has
1751 ASCENT and DESCENT to draw characters by the font at the vertical
1752 center of the line of frame F.
1753
1754 Here, out task is to find the value of BOFF in the following figure;
1755
1756 -------------------------+-----------+-
1757 -+-+---------+-+ | |
1758 | | | | | |
1759 | | | | F_ASCENT F_HEIGHT
1760 | | | ASCENT | |
1761 HEIGHT | | | | |
1762 | | |-|-+------+-----------|------- baseline
1763 | | | | BOFF | |
1764 | |---------|-+-+ | |
1765 | | | DESCENT | |
1766 -+-+---------+-+ F_DESCENT |
1767 -------------------------+-----------+-
1768
1769 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1770 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1771 DESCENT = FONT->descent
1772 HEIGHT = FONT_HEIGHT (FONT)
1773 F_DESCENT = (F->output_data.x->font->descent
1774 - F->output_data.x->baseline_offset)
1775 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1776 */
1777
1778 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1779 ((FONT)->descent \
1780 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1781 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1782 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1783
1784 /* Produce glyphs/get display metrics for the display element IT is
1785 loaded with. See the description of struct display_iterator in
1786 dispextern.h for an overview of struct display_iterator. */
1787
1788 static void
1789 x_produce_glyphs (it)
1790 struct it *it;
1791 {
1792 it->glyph_not_available_p = 0;
1793
1794 if (it->what == IT_CHARACTER)
1795 {
1796 XChar2b char2b;
1797 XFontStruct *font;
1798 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1799 XCharStruct *pcm;
1800 int font_not_found_p;
1801 struct font_info *font_info;
1802 int boff; /* baseline offset */
1803 /* We may change it->multibyte_p upon unibyte<->multibyte
1804 conversion. So, save the current value now and restore it
1805 later.
1806
1807 Note: It seems that we don't have to record multibyte_p in
1808 struct glyph because the character code itself tells if or
1809 not the character is multibyte. Thus, in the future, we must
1810 consider eliminating the field `multibyte_p' in the struct
1811 glyph. */
1812 int saved_multibyte_p = it->multibyte_p;
1813
1814 /* Maybe translate single-byte characters to multibyte, or the
1815 other way. */
1816 it->char_to_display = it->c;
1817 if (!ASCII_BYTE_P (it->c))
1818 {
1819 if (unibyte_display_via_language_environment
1820 && SINGLE_BYTE_CHAR_P (it->c)
1821 && (it->c >= 0240
1822 || !NILP (Vnonascii_translation_table)))
1823 {
1824 it->char_to_display = unibyte_char_to_multibyte (it->c);
1825 it->multibyte_p = 1;
1826 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1827 face = FACE_FROM_ID (it->f, it->face_id);
1828 }
1829 else if (!SINGLE_BYTE_CHAR_P (it->c)
1830 && !it->multibyte_p)
1831 {
1832 it->multibyte_p = 1;
1833 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1834 face = FACE_FROM_ID (it->f, it->face_id);
1835 }
1836 }
1837
1838 /* Get font to use. Encode IT->char_to_display. */
1839 x_get_char_face_and_encoding (it->f, it->char_to_display,
1840 it->face_id, &char2b,
1841 it->multibyte_p);
1842 font = face->font;
1843
1844 /* When no suitable font found, use the default font. */
1845 font_not_found_p = font == NULL;
1846 if (font_not_found_p)
1847 {
1848 font = FRAME_FONT (it->f);
1849 boff = it->f->output_data.x->baseline_offset;
1850 font_info = NULL;
1851 }
1852 else
1853 {
1854 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1855 boff = font_info->baseline_offset;
1856 if (font_info->vertical_centering)
1857 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1858 }
1859
1860 if (it->char_to_display >= ' '
1861 && (!it->multibyte_p || it->char_to_display < 128))
1862 {
1863 /* Either unibyte or ASCII. */
1864 int stretched_p;
1865
1866 it->nglyphs = 1;
1867
1868 pcm = x_per_char_metric (font, &char2b);
1869 it->ascent = font->ascent + boff;
1870 it->descent = font->descent - boff;
1871
1872 if (pcm)
1873 {
1874 it->phys_ascent = pcm->ascent + boff;
1875 it->phys_descent = pcm->descent - boff;
1876 it->pixel_width = pcm->width;
1877 }
1878 else
1879 {
1880 it->glyph_not_available_p = 1;
1881 it->phys_ascent = font->ascent + boff;
1882 it->phys_descent = font->descent - boff;
1883 it->pixel_width = FONT_WIDTH (font);
1884 }
1885
1886 /* If this is a space inside a region of text with
1887 `space-width' property, change its width. */
1888 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1889 if (stretched_p)
1890 it->pixel_width *= XFLOATINT (it->space_width);
1891
1892 /* If face has a box, add the box thickness to the character
1893 height. If character has a box line to the left and/or
1894 right, add the box line width to the character's width. */
1895 if (face->box != FACE_NO_BOX)
1896 {
1897 int thick = face->box_line_width;
1898
1899 if (thick > 0)
1900 {
1901 it->ascent += thick;
1902 it->descent += thick;
1903 }
1904 else
1905 thick = -thick;
1906
1907 if (it->start_of_box_run_p)
1908 it->pixel_width += thick;
1909 if (it->end_of_box_run_p)
1910 it->pixel_width += thick;
1911 }
1912
1913 /* If face has an overline, add the height of the overline
1914 (1 pixel) and a 1 pixel margin to the character height. */
1915 if (face->overline_p)
1916 it->ascent += 2;
1917
1918 take_vertical_position_into_account (it);
1919
1920 /* If we have to actually produce glyphs, do it. */
1921 if (it->glyph_row)
1922 {
1923 if (stretched_p)
1924 {
1925 /* Translate a space with a `space-width' property
1926 into a stretch glyph. */
1927 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1928 x_append_stretch_glyph (it, it->object, it->pixel_width,
1929 it->ascent + it->descent, ascent);
1930 }
1931 else
1932 x_append_glyph (it);
1933
1934 /* If characters with lbearing or rbearing are displayed
1935 in this line, record that fact in a flag of the
1936 glyph row. This is used to optimize X output code. */
1937 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1938 it->glyph_row->contains_overlapping_glyphs_p = 1;
1939 }
1940 }
1941 else if (it->char_to_display == '\n')
1942 {
1943 /* A newline has no width but we need the height of the line. */
1944 it->pixel_width = 0;
1945 it->nglyphs = 0;
1946 it->ascent = it->phys_ascent = font->ascent + boff;
1947 it->descent = it->phys_descent = font->descent - boff;
1948
1949 if (face->box != FACE_NO_BOX
1950 && face->box_line_width > 0)
1951 {
1952 it->ascent += face->box_line_width;
1953 it->descent += face->box_line_width;
1954 }
1955 }
1956 else if (it->char_to_display == '\t')
1957 {
1958 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1959 int x = it->current_x + it->continuation_lines_width;
1960 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1961
1962 /* If the distance from the current position to the next tab
1963 stop is less than a canonical character width, use the
1964 tab stop after that. */
1965 if (next_tab_x - x < CANON_X_UNIT (it->f))
1966 next_tab_x += tab_width;
1967
1968 it->pixel_width = next_tab_x - x;
1969 it->nglyphs = 1;
1970 it->ascent = it->phys_ascent = font->ascent + boff;
1971 it->descent = it->phys_descent = font->descent - boff;
1972
1973 if (it->glyph_row)
1974 {
1975 double ascent = (double) it->ascent / (it->ascent + it->descent);
1976 x_append_stretch_glyph (it, it->object, it->pixel_width,
1977 it->ascent + it->descent, ascent);
1978 }
1979 }
1980 else
1981 {
1982 /* A multi-byte character. Assume that the display width of the
1983 character is the width of the character multiplied by the
1984 width of the font. */
1985
1986 /* If we found a font, this font should give us the right
1987 metrics. If we didn't find a font, use the frame's
1988 default font and calculate the width of the character
1989 from the charset width; this is what old redisplay code
1990 did. */
1991 pcm = x_per_char_metric (font, &char2b);
1992 if (font_not_found_p || !pcm)
1993 {
1994 int charset = CHAR_CHARSET (it->char_to_display);
1995
1996 it->glyph_not_available_p = 1;
1997 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1998 * CHARSET_WIDTH (charset));
1999 it->phys_ascent = font->ascent + boff;
2000 it->phys_descent = font->descent - boff;
2001 }
2002 else
2003 {
2004 it->pixel_width = pcm->width;
2005 it->phys_ascent = pcm->ascent + boff;
2006 it->phys_descent = pcm->descent - boff;
2007 if (it->glyph_row
2008 && (pcm->lbearing < 0
2009 || pcm->rbearing > pcm->width))
2010 it->glyph_row->contains_overlapping_glyphs_p = 1;
2011 }
2012 it->nglyphs = 1;
2013 it->ascent = font->ascent + boff;
2014 it->descent = font->descent - boff;
2015 if (face->box != FACE_NO_BOX)
2016 {
2017 int thick = face->box_line_width;
2018
2019 if (thick > 0)
2020 {
2021 it->ascent += thick;
2022 it->descent += thick;
2023 }
2024 else
2025 thick = - thick;
2026
2027 if (it->start_of_box_run_p)
2028 it->pixel_width += thick;
2029 if (it->end_of_box_run_p)
2030 it->pixel_width += thick;
2031 }
2032
2033 /* If face has an overline, add the height of the overline
2034 (1 pixel) and a 1 pixel margin to the character height. */
2035 if (face->overline_p)
2036 it->ascent += 2;
2037
2038 take_vertical_position_into_account (it);
2039
2040 if (it->glyph_row)
2041 x_append_glyph (it);
2042 }
2043 it->multibyte_p = saved_multibyte_p;
2044 }
2045 else if (it->what == IT_COMPOSITION)
2046 {
2047 /* Note: A composition is represented as one glyph in the
2048 glyph matrix. There are no padding glyphs. */
2049 XChar2b char2b;
2050 XFontStruct *font;
2051 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2052 XCharStruct *pcm;
2053 int font_not_found_p;
2054 struct font_info *font_info;
2055 int boff; /* baseline offset */
2056 struct composition *cmp = composition_table[it->cmp_id];
2057
2058 /* Maybe translate single-byte characters to multibyte. */
2059 it->char_to_display = it->c;
2060 if (unibyte_display_via_language_environment
2061 && SINGLE_BYTE_CHAR_P (it->c)
2062 && (it->c >= 0240
2063 || (it->c >= 0200
2064 && !NILP (Vnonascii_translation_table))))
2065 {
2066 it->char_to_display = unibyte_char_to_multibyte (it->c);
2067 }
2068
2069 /* Get face and font to use. Encode IT->char_to_display. */
2070 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2071 face = FACE_FROM_ID (it->f, it->face_id);
2072 x_get_char_face_and_encoding (it->f, it->char_to_display,
2073 it->face_id, &char2b, it->multibyte_p);
2074 font = face->font;
2075
2076 /* When no suitable font found, use the default font. */
2077 font_not_found_p = font == NULL;
2078 if (font_not_found_p)
2079 {
2080 font = FRAME_FONT (it->f);
2081 boff = it->f->output_data.x->baseline_offset;
2082 font_info = NULL;
2083 }
2084 else
2085 {
2086 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2087 boff = font_info->baseline_offset;
2088 if (font_info->vertical_centering)
2089 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2090 }
2091
2092 /* There are no padding glyphs, so there is only one glyph to
2093 produce for the composition. Important is that pixel_width,
2094 ascent and descent are the values of what is drawn by
2095 draw_glyphs (i.e. the values of the overall glyphs composed). */
2096 it->nglyphs = 1;
2097
2098 /* If we have not yet calculated pixel size data of glyphs of
2099 the composition for the current face font, calculate them
2100 now. Theoretically, we have to check all fonts for the
2101 glyphs, but that requires much time and memory space. So,
2102 here we check only the font of the first glyph. This leads
2103 to incorrect display very rarely, and C-l (recenter) can
2104 correct the display anyway. */
2105 if (cmp->font != (void *) font)
2106 {
2107 /* Ascent and descent of the font of the first character of
2108 this composition (adjusted by baseline offset). Ascent
2109 and descent of overall glyphs should not be less than
2110 them respectively. */
2111 int font_ascent = font->ascent + boff;
2112 int font_descent = font->descent - boff;
2113 /* Bounding box of the overall glyphs. */
2114 int leftmost, rightmost, lowest, highest;
2115 int i, width, ascent, descent;
2116
2117 cmp->font = (void *) font;
2118
2119 /* Initialize the bounding box. */
2120 if (font_info
2121 && (pcm = x_per_char_metric (font, &char2b)))
2122 {
2123 width = pcm->width;
2124 ascent = pcm->ascent;
2125 descent = pcm->descent;
2126 }
2127 else
2128 {
2129 width = FONT_WIDTH (font);
2130 ascent = font->ascent;
2131 descent = font->descent;
2132 }
2133
2134 rightmost = width;
2135 lowest = - descent + boff;
2136 highest = ascent + boff;
2137 leftmost = 0;
2138
2139 if (font_info
2140 && font_info->default_ascent
2141 && CHAR_TABLE_P (Vuse_default_ascent)
2142 && !NILP (Faref (Vuse_default_ascent,
2143 make_number (it->char_to_display))))
2144 highest = font_info->default_ascent + boff;
2145
2146 /* Draw the first glyph at the normal position. It may be
2147 shifted to right later if some other glyphs are drawn at
2148 the left. */
2149 cmp->offsets[0] = 0;
2150 cmp->offsets[1] = boff;
2151
2152 /* Set cmp->offsets for the remaining glyphs. */
2153 for (i = 1; i < cmp->glyph_len; i++)
2154 {
2155 int left, right, btm, top;
2156 int ch = COMPOSITION_GLYPH (cmp, i);
2157 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2158
2159 face = FACE_FROM_ID (it->f, face_id);
2160 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2161 it->multibyte_p);
2162 font = face->font;
2163 if (font == NULL)
2164 {
2165 font = FRAME_FONT (it->f);
2166 boff = it->f->output_data.x->baseline_offset;
2167 font_info = NULL;
2168 }
2169 else
2170 {
2171 font_info
2172 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2173 boff = font_info->baseline_offset;
2174 if (font_info->vertical_centering)
2175 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2176 }
2177
2178 if (font_info
2179 && (pcm = x_per_char_metric (font, &char2b)))
2180 {
2181 width = pcm->width;
2182 ascent = pcm->ascent;
2183 descent = pcm->descent;
2184 }
2185 else
2186 {
2187 width = FONT_WIDTH (font);
2188 ascent = 1;
2189 descent = 0;
2190 }
2191
2192 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2193 {
2194 /* Relative composition with or without
2195 alternate chars. */
2196 left = (leftmost + rightmost - width) / 2;
2197 btm = - descent + boff;
2198 if (font_info && font_info->relative_compose
2199 && (! CHAR_TABLE_P (Vignore_relative_composition)
2200 || NILP (Faref (Vignore_relative_composition,
2201 make_number (ch)))))
2202 {
2203
2204 if (- descent >= font_info->relative_compose)
2205 /* One extra pixel between two glyphs. */
2206 btm = highest + 1;
2207 else if (ascent <= 0)
2208 /* One extra pixel between two glyphs. */
2209 btm = lowest - 1 - ascent - descent;
2210 }
2211 }
2212 else
2213 {
2214 /* A composition rule is specified by an integer
2215 value that encodes global and new reference
2216 points (GREF and NREF). GREF and NREF are
2217 specified by numbers as below:
2218
2219 0---1---2 -- ascent
2220 | |
2221 | |
2222 | |
2223 9--10--11 -- center
2224 | |
2225 ---3---4---5--- baseline
2226 | |
2227 6---7---8 -- descent
2228 */
2229 int rule = COMPOSITION_RULE (cmp, i);
2230 int gref, nref, grefx, grefy, nrefx, nrefy;
2231
2232 COMPOSITION_DECODE_RULE (rule, gref, nref);
2233 grefx = gref % 3, nrefx = nref % 3;
2234 grefy = gref / 3, nrefy = nref / 3;
2235
2236 left = (leftmost
2237 + grefx * (rightmost - leftmost) / 2
2238 - nrefx * width / 2);
2239 btm = ((grefy == 0 ? highest
2240 : grefy == 1 ? 0
2241 : grefy == 2 ? lowest
2242 : (highest + lowest) / 2)
2243 - (nrefy == 0 ? ascent + descent
2244 : nrefy == 1 ? descent - boff
2245 : nrefy == 2 ? 0
2246 : (ascent + descent) / 2));
2247 }
2248
2249 cmp->offsets[i * 2] = left;
2250 cmp->offsets[i * 2 + 1] = btm + descent;
2251
2252 /* Update the bounding box of the overall glyphs. */
2253 right = left + width;
2254 top = btm + descent + ascent;
2255 if (left < leftmost)
2256 leftmost = left;
2257 if (right > rightmost)
2258 rightmost = right;
2259 if (top > highest)
2260 highest = top;
2261 if (btm < lowest)
2262 lowest = btm;
2263 }
2264
2265 /* If there are glyphs whose x-offsets are negative,
2266 shift all glyphs to the right and make all x-offsets
2267 non-negative. */
2268 if (leftmost < 0)
2269 {
2270 for (i = 0; i < cmp->glyph_len; i++)
2271 cmp->offsets[i * 2] -= leftmost;
2272 rightmost -= leftmost;
2273 }
2274
2275 cmp->pixel_width = rightmost;
2276 cmp->ascent = highest;
2277 cmp->descent = - lowest;
2278 if (cmp->ascent < font_ascent)
2279 cmp->ascent = font_ascent;
2280 if (cmp->descent < font_descent)
2281 cmp->descent = font_descent;
2282 }
2283
2284 it->pixel_width = cmp->pixel_width;
2285 it->ascent = it->phys_ascent = cmp->ascent;
2286 it->descent = it->phys_descent = cmp->descent;
2287
2288 if (face->box != FACE_NO_BOX)
2289 {
2290 int thick = face->box_line_width;
2291
2292 if (thick > 0)
2293 {
2294 it->ascent += thick;
2295 it->descent += thick;
2296 }
2297 else
2298 thick = - thick;
2299
2300 if (it->start_of_box_run_p)
2301 it->pixel_width += thick;
2302 if (it->end_of_box_run_p)
2303 it->pixel_width += thick;
2304 }
2305
2306 /* If face has an overline, add the height of the overline
2307 (1 pixel) and a 1 pixel margin to the character height. */
2308 if (face->overline_p)
2309 it->ascent += 2;
2310
2311 take_vertical_position_into_account (it);
2312
2313 if (it->glyph_row)
2314 x_append_composite_glyph (it);
2315 }
2316 else if (it->what == IT_IMAGE)
2317 x_produce_image_glyph (it);
2318 else if (it->what == IT_STRETCH)
2319 x_produce_stretch_glyph (it);
2320
2321 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2322 because this isn't true for images with `:ascent 100'. */
2323 xassert (it->ascent >= 0 && it->descent >= 0);
2324 if (it->area == TEXT_AREA)
2325 it->current_x += it->pixel_width;
2326
2327 it->descent += it->extra_line_spacing;
2328
2329 it->max_ascent = max (it->max_ascent, it->ascent);
2330 it->max_descent = max (it->max_descent, it->descent);
2331 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2332 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2333 }
2334
2335
2336 /* Estimate the pixel height of the mode or top line on frame F.
2337 FACE_ID specifies what line's height to estimate. */
2338
2339 int
2340 x_estimate_mode_line_height (f, face_id)
2341 struct frame *f;
2342 enum face_id face_id;
2343 {
2344 int height = FONT_HEIGHT (FRAME_FONT (f));
2345
2346 /* This function is called so early when Emacs starts that the face
2347 cache and mode line face are not yet initialized. */
2348 if (FRAME_FACE_CACHE (f))
2349 {
2350 struct face *face = FACE_FROM_ID (f, face_id);
2351 if (face)
2352 {
2353 if (face->font)
2354 height = FONT_HEIGHT (face->font);
2355 if (face->box_line_width > 0)
2356 height += 2 * face->box_line_width;
2357 }
2358 }
2359
2360 return height;
2361 }
2362
2363 \f
2364 /***********************************************************************
2365 Glyph display
2366 ***********************************************************************/
2367
2368 /* A sequence of glyphs to be drawn in the same face.
2369
2370 This data structure is not really completely X specific, so it
2371 could possibly, at least partially, be useful for other systems. It
2372 is currently not part of the external redisplay interface because
2373 it's not clear what other systems will need. */
2374
2375 struct glyph_string
2376 {
2377 /* X-origin of the string. */
2378 int x;
2379
2380 /* Y-origin and y-position of the base line of this string. */
2381 int y, ybase;
2382
2383 /* The width of the string, not including a face extension. */
2384 int width;
2385
2386 /* The width of the string, including a face extension. */
2387 int background_width;
2388
2389 /* The height of this string. This is the height of the line this
2390 string is drawn in, and can be different from the height of the
2391 font the string is drawn in. */
2392 int height;
2393
2394 /* Number of pixels this string overwrites in front of its x-origin.
2395 This number is zero if the string has an lbearing >= 0; it is
2396 -lbearing, if the string has an lbearing < 0. */
2397 int left_overhang;
2398
2399 /* Number of pixels this string overwrites past its right-most
2400 nominal x-position, i.e. x + width. Zero if the string's
2401 rbearing is <= its nominal width, rbearing - width otherwise. */
2402 int right_overhang;
2403
2404 /* The frame on which the glyph string is drawn. */
2405 struct frame *f;
2406
2407 /* The window on which the glyph string is drawn. */
2408 struct window *w;
2409
2410 /* X display and window for convenience. */
2411 Display *display;
2412 Window window;
2413
2414 /* The glyph row for which this string was built. It determines the
2415 y-origin and height of the string. */
2416 struct glyph_row *row;
2417
2418 /* The area within row. */
2419 enum glyph_row_area area;
2420
2421 /* Characters to be drawn, and number of characters. */
2422 XChar2b *char2b;
2423 int nchars;
2424
2425 /* A face-override for drawing cursors, mouse face and similar. */
2426 enum draw_glyphs_face hl;
2427
2428 /* Face in which this string is to be drawn. */
2429 struct face *face;
2430
2431 /* Font in which this string is to be drawn. */
2432 XFontStruct *font;
2433
2434 /* Font info for this string. */
2435 struct font_info *font_info;
2436
2437 /* Non-null means this string describes (part of) a composition.
2438 All characters from char2b are drawn composed. */
2439 struct composition *cmp;
2440
2441 /* Index of this glyph string's first character in the glyph
2442 definition of CMP. If this is zero, this glyph string describes
2443 the first character of a composition. */
2444 int gidx;
2445
2446 /* 1 means this glyph strings face has to be drawn to the right end
2447 of the window's drawing area. */
2448 unsigned extends_to_end_of_line_p : 1;
2449
2450 /* 1 means the background of this string has been drawn. */
2451 unsigned background_filled_p : 1;
2452
2453 /* 1 means glyph string must be drawn with 16-bit functions. */
2454 unsigned two_byte_p : 1;
2455
2456 /* 1 means that the original font determined for drawing this glyph
2457 string could not be loaded. The member `font' has been set to
2458 the frame's default font in this case. */
2459 unsigned font_not_found_p : 1;
2460
2461 /* 1 means that the face in which this glyph string is drawn has a
2462 stipple pattern. */
2463 unsigned stippled_p : 1;
2464
2465 /* 1 means only the foreground of this glyph string must be drawn,
2466 and we should use the physical height of the line this glyph
2467 string appears in as clip rect. */
2468 unsigned for_overlaps_p : 1;
2469
2470 /* The GC to use for drawing this glyph string. */
2471 GC gc;
2472
2473 /* A pointer to the first glyph in the string. This glyph
2474 corresponds to char2b[0]. Needed to draw rectangles if
2475 font_not_found_p is 1. */
2476 struct glyph *first_glyph;
2477
2478 /* Image, if any. */
2479 struct image *img;
2480
2481 struct glyph_string *next, *prev;
2482 };
2483
2484
2485 #if GLYPH_DEBUG
2486
2487 static void
2488 x_dump_glyph_string (s)
2489 struct glyph_string *s;
2490 {
2491 fprintf (stderr, "glyph string\n");
2492 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2493 s->x, s->y, s->width, s->height);
2494 fprintf (stderr, " ybase = %d\n", s->ybase);
2495 fprintf (stderr, " hl = %d\n", s->hl);
2496 fprintf (stderr, " left overhang = %d, right = %d\n",
2497 s->left_overhang, s->right_overhang);
2498 fprintf (stderr, " nchars = %d\n", s->nchars);
2499 fprintf (stderr, " extends to end of line = %d\n",
2500 s->extends_to_end_of_line_p);
2501 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2502 fprintf (stderr, " bg width = %d\n", s->background_width);
2503 }
2504
2505 #endif /* GLYPH_DEBUG */
2506
2507
2508
2509 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2510 struct glyph_string **,
2511 struct glyph_string *,
2512 struct glyph_string *));
2513 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2514 struct glyph_string **,
2515 struct glyph_string *,
2516 struct glyph_string *));
2517 static void x_append_glyph_string P_ ((struct glyph_string **,
2518 struct glyph_string **,
2519 struct glyph_string *));
2520 static int x_left_overwritten P_ ((struct glyph_string *));
2521 static int x_left_overwriting P_ ((struct glyph_string *));
2522 static int x_right_overwritten P_ ((struct glyph_string *));
2523 static int x_right_overwriting P_ ((struct glyph_string *));
2524 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2525 int));
2526 static void x_init_glyph_string P_ ((struct glyph_string *,
2527 XChar2b *, struct window *,
2528 struct glyph_row *,
2529 enum glyph_row_area, int,
2530 enum draw_glyphs_face));
2531 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2532 enum glyph_row_area, int, int,
2533 enum draw_glyphs_face, int *, int *, int));
2534 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2535 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2536 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2537 int));
2538 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2539 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2540 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2541 static void x_draw_glyph_string P_ ((struct glyph_string *));
2542 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2543 static void x_set_cursor_gc P_ ((struct glyph_string *));
2544 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2545 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2546 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2547 int *, int *));
2548 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2549 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2550 unsigned long *, double, int));
2551 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2552 double, int, unsigned long));
2553 static void x_setup_relief_colors P_ ((struct glyph_string *));
2554 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2555 static void x_draw_image_relief P_ ((struct glyph_string *));
2556 static void x_draw_image_foreground P_ ((struct glyph_string *));
2557 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2558 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2559 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2560 int, int, int));
2561 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2562 int, int, int, int, XRectangle *));
2563 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2564 int, int, int, XRectangle *));
2565 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2566 enum glyph_row_area));
2567 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2568 struct glyph_row *,
2569 enum glyph_row_area, int, int));
2570
2571 #if GLYPH_DEBUG
2572 static void x_check_font P_ ((struct frame *, XFontStruct *));
2573 #endif
2574
2575
2576 /* Append the list of glyph strings with head H and tail T to the list
2577 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2578
2579 static INLINE void
2580 x_append_glyph_string_lists (head, tail, h, t)
2581 struct glyph_string **head, **tail;
2582 struct glyph_string *h, *t;
2583 {
2584 if (h)
2585 {
2586 if (*head)
2587 (*tail)->next = h;
2588 else
2589 *head = h;
2590 h->prev = *tail;
2591 *tail = t;
2592 }
2593 }
2594
2595
2596 /* Prepend the list of glyph strings with head H and tail T to the
2597 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2598 result. */
2599
2600 static INLINE void
2601 x_prepend_glyph_string_lists (head, tail, h, t)
2602 struct glyph_string **head, **tail;
2603 struct glyph_string *h, *t;
2604 {
2605 if (h)
2606 {
2607 if (*head)
2608 (*head)->prev = t;
2609 else
2610 *tail = t;
2611 t->next = *head;
2612 *head = h;
2613 }
2614 }
2615
2616
2617 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2618 Set *HEAD and *TAIL to the resulting list. */
2619
2620 static INLINE void
2621 x_append_glyph_string (head, tail, s)
2622 struct glyph_string **head, **tail;
2623 struct glyph_string *s;
2624 {
2625 s->next = s->prev = NULL;
2626 x_append_glyph_string_lists (head, tail, s, s);
2627 }
2628
2629
2630 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2631 face. */
2632
2633 static void
2634 x_set_cursor_gc (s)
2635 struct glyph_string *s;
2636 {
2637 if (s->font == FRAME_FONT (s->f)
2638 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2639 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2640 && !s->cmp)
2641 s->gc = s->f->output_data.x->cursor_gc;
2642 else
2643 {
2644 /* Cursor on non-default face: must merge. */
2645 XGCValues xgcv;
2646 unsigned long mask;
2647
2648 xgcv.background = s->f->output_data.x->cursor_pixel;
2649 xgcv.foreground = s->face->background;
2650
2651 /* If the glyph would be invisible, try a different foreground. */
2652 if (xgcv.foreground == xgcv.background)
2653 xgcv.foreground = s->face->foreground;
2654 if (xgcv.foreground == xgcv.background)
2655 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2656 if (xgcv.foreground == xgcv.background)
2657 xgcv.foreground = s->face->foreground;
2658
2659 /* Make sure the cursor is distinct from text in this face. */
2660 if (xgcv.background == s->face->background
2661 && xgcv.foreground == s->face->foreground)
2662 {
2663 xgcv.background = s->face->foreground;
2664 xgcv.foreground = s->face->background;
2665 }
2666
2667 IF_DEBUG (x_check_font (s->f, s->font));
2668 xgcv.font = s->font->fid;
2669 xgcv.graphics_exposures = False;
2670 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2671
2672 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2673 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2674 mask, &xgcv);
2675 else
2676 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2677 = XCreateGC (s->display, s->window, mask, &xgcv);
2678
2679 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2680 }
2681 }
2682
2683
2684 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2685
2686 static void
2687 x_set_mouse_face_gc (s)
2688 struct glyph_string *s;
2689 {
2690 int face_id;
2691 struct face *face;
2692
2693 /* What face has to be used last for the mouse face? */
2694 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2695 face = FACE_FROM_ID (s->f, face_id);
2696 if (face == NULL)
2697 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2698
2699 if (s->first_glyph->type == CHAR_GLYPH)
2700 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2701 else
2702 face_id = FACE_FOR_CHAR (s->f, face, 0);
2703 s->face = FACE_FROM_ID (s->f, face_id);
2704 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2705
2706 /* If font in this face is same as S->font, use it. */
2707 if (s->font == s->face->font)
2708 s->gc = s->face->gc;
2709 else
2710 {
2711 /* Otherwise construct scratch_cursor_gc with values from FACE
2712 but font FONT. */
2713 XGCValues xgcv;
2714 unsigned long mask;
2715
2716 xgcv.background = s->face->background;
2717 xgcv.foreground = s->face->foreground;
2718 IF_DEBUG (x_check_font (s->f, s->font));
2719 xgcv.font = s->font->fid;
2720 xgcv.graphics_exposures = False;
2721 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2722
2723 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2724 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2725 mask, &xgcv);
2726 else
2727 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2728 = XCreateGC (s->display, s->window, mask, &xgcv);
2729
2730 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2731 }
2732
2733 xassert (s->gc != 0);
2734 }
2735
2736
2737 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2738 Faces to use in the mode line have already been computed when the
2739 matrix was built, so there isn't much to do, here. */
2740
2741 static INLINE void
2742 x_set_mode_line_face_gc (s)
2743 struct glyph_string *s;
2744 {
2745 s->gc = s->face->gc;
2746 }
2747
2748
2749 /* Set S->gc of glyph string S for drawing that glyph string. Set
2750 S->stippled_p to a non-zero value if the face of S has a stipple
2751 pattern. */
2752
2753 static INLINE void
2754 x_set_glyph_string_gc (s)
2755 struct glyph_string *s;
2756 {
2757 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2758
2759 if (s->hl == DRAW_NORMAL_TEXT)
2760 {
2761 s->gc = s->face->gc;
2762 s->stippled_p = s->face->stipple != 0;
2763 }
2764 else if (s->hl == DRAW_INVERSE_VIDEO)
2765 {
2766 x_set_mode_line_face_gc (s);
2767 s->stippled_p = s->face->stipple != 0;
2768 }
2769 else if (s->hl == DRAW_CURSOR)
2770 {
2771 x_set_cursor_gc (s);
2772 s->stippled_p = 0;
2773 }
2774 else if (s->hl == DRAW_MOUSE_FACE)
2775 {
2776 x_set_mouse_face_gc (s);
2777 s->stippled_p = s->face->stipple != 0;
2778 }
2779 else if (s->hl == DRAW_IMAGE_RAISED
2780 || s->hl == DRAW_IMAGE_SUNKEN)
2781 {
2782 s->gc = s->face->gc;
2783 s->stippled_p = s->face->stipple != 0;
2784 }
2785 else
2786 {
2787 s->gc = s->face->gc;
2788 s->stippled_p = s->face->stipple != 0;
2789 }
2790
2791 /* GC must have been set. */
2792 xassert (s->gc != 0);
2793 }
2794
2795
2796 /* Return in *R the clipping rectangle for glyph string S. */
2797
2798 static void
2799 x_get_glyph_string_clip_rect (s, r)
2800 struct glyph_string *s;
2801 XRectangle *r;
2802 {
2803 if (s->row->full_width_p)
2804 {
2805 /* Draw full-width. X coordinates are relative to S->w->left. */
2806 int canon_x = CANON_X_UNIT (s->f);
2807
2808 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2809 r->width = XFASTINT (s->w->width) * canon_x;
2810
2811 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2812 {
2813 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2814 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2815 r->x -= width;
2816 }
2817
2818 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2819
2820 /* Unless displaying a mode or menu bar line, which are always
2821 fully visible, clip to the visible part of the row. */
2822 if (s->w->pseudo_window_p)
2823 r->height = s->row->visible_height;
2824 else
2825 r->height = s->height;
2826 }
2827 else
2828 {
2829 /* This is a text line that may be partially visible. */
2830 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2831 r->width = window_box_width (s->w, s->area);
2832 r->height = s->row->visible_height;
2833 }
2834
2835 /* If S draws overlapping rows, it's sufficient to use the top and
2836 bottom of the window for clipping because this glyph string
2837 intentionally draws over other lines. */
2838 if (s->for_overlaps_p)
2839 {
2840 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2841 r->height = window_text_bottom_y (s->w) - r->y;
2842 }
2843 else
2844 {
2845 /* Don't use S->y for clipping because it doesn't take partially
2846 visible lines into account. For example, it can be negative for
2847 partially visible lines at the top of a window. */
2848 if (!s->row->full_width_p
2849 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2850 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2851 else
2852 r->y = max (0, s->row->y);
2853
2854 /* If drawing a tool-bar window, draw it over the internal border
2855 at the top of the window. */
2856 if (s->w == XWINDOW (s->f->tool_bar_window))
2857 r->y -= s->f->output_data.x->internal_border_width;
2858 }
2859
2860 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2861 }
2862
2863
2864 /* Set clipping for output of glyph string S. S may be part of a mode
2865 line or menu if we don't have X toolkit support. */
2866
2867 static INLINE void
2868 x_set_glyph_string_clipping (s)
2869 struct glyph_string *s;
2870 {
2871 XRectangle r;
2872 x_get_glyph_string_clip_rect (s, &r);
2873 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2874 }
2875
2876
2877 /* Compute left and right overhang of glyph string S. If S is a glyph
2878 string for a composition, assume overhangs don't exist. */
2879
2880 static INLINE void
2881 x_compute_glyph_string_overhangs (s)
2882 struct glyph_string *s;
2883 {
2884 if (s->cmp == NULL
2885 && s->first_glyph->type == CHAR_GLYPH)
2886 {
2887 XCharStruct cs;
2888 int direction, font_ascent, font_descent;
2889 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2890 &font_ascent, &font_descent, &cs);
2891 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2892 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2893 }
2894 }
2895
2896
2897 /* Compute overhangs and x-positions for glyph string S and its
2898 predecessors, or successors. X is the starting x-position for S.
2899 BACKWARD_P non-zero means process predecessors. */
2900
2901 static void
2902 x_compute_overhangs_and_x (s, x, backward_p)
2903 struct glyph_string *s;
2904 int x;
2905 int backward_p;
2906 {
2907 if (backward_p)
2908 {
2909 while (s)
2910 {
2911 x_compute_glyph_string_overhangs (s);
2912 x -= s->width;
2913 s->x = x;
2914 s = s->prev;
2915 }
2916 }
2917 else
2918 {
2919 while (s)
2920 {
2921 x_compute_glyph_string_overhangs (s);
2922 s->x = x;
2923 x += s->width;
2924 s = s->next;
2925 }
2926 }
2927 }
2928
2929
2930 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2931 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2932 assumed to be zero. */
2933
2934 static void
2935 x_get_glyph_overhangs (glyph, f, left, right)
2936 struct glyph *glyph;
2937 struct frame *f;
2938 int *left, *right;
2939 {
2940 *left = *right = 0;
2941
2942 if (glyph->type == CHAR_GLYPH)
2943 {
2944 XFontStruct *font;
2945 struct face *face;
2946 struct font_info *font_info;
2947 XChar2b char2b;
2948 XCharStruct *pcm;
2949
2950 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2951 font = face->font;
2952 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2953 if (font
2954 && (pcm = x_per_char_metric (font, &char2b)))
2955 {
2956 if (pcm->rbearing > pcm->width)
2957 *right = pcm->rbearing - pcm->width;
2958 if (pcm->lbearing < 0)
2959 *left = -pcm->lbearing;
2960 }
2961 }
2962 }
2963
2964
2965 /* Return the index of the first glyph preceding glyph string S that
2966 is overwritten by S because of S's left overhang. Value is -1
2967 if no glyphs are overwritten. */
2968
2969 static int
2970 x_left_overwritten (s)
2971 struct glyph_string *s;
2972 {
2973 int k;
2974
2975 if (s->left_overhang)
2976 {
2977 int x = 0, i;
2978 struct glyph *glyphs = s->row->glyphs[s->area];
2979 int first = s->first_glyph - glyphs;
2980
2981 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2982 x -= glyphs[i].pixel_width;
2983
2984 k = i + 1;
2985 }
2986 else
2987 k = -1;
2988
2989 return k;
2990 }
2991
2992
2993 /* Return the index of the first glyph preceding glyph string S that
2994 is overwriting S because of its right overhang. Value is -1 if no
2995 glyph in front of S overwrites S. */
2996
2997 static int
2998 x_left_overwriting (s)
2999 struct glyph_string *s;
3000 {
3001 int i, k, x;
3002 struct glyph *glyphs = s->row->glyphs[s->area];
3003 int first = s->first_glyph - glyphs;
3004
3005 k = -1;
3006 x = 0;
3007 for (i = first - 1; i >= 0; --i)
3008 {
3009 int left, right;
3010 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3011 if (x + right > 0)
3012 k = i;
3013 x -= glyphs[i].pixel_width;
3014 }
3015
3016 return k;
3017 }
3018
3019
3020 /* Return the index of the last glyph following glyph string S that is
3021 not overwritten by S because of S's right overhang. Value is -1 if
3022 no such glyph is found. */
3023
3024 static int
3025 x_right_overwritten (s)
3026 struct glyph_string *s;
3027 {
3028 int k = -1;
3029
3030 if (s->right_overhang)
3031 {
3032 int x = 0, i;
3033 struct glyph *glyphs = s->row->glyphs[s->area];
3034 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3035 int end = s->row->used[s->area];
3036
3037 for (i = first; i < end && s->right_overhang > x; ++i)
3038 x += glyphs[i].pixel_width;
3039
3040 k = i;
3041 }
3042
3043 return k;
3044 }
3045
3046
3047 /* Return the index of the last glyph following glyph string S that
3048 overwrites S because of its left overhang. Value is negative
3049 if no such glyph is found. */
3050
3051 static int
3052 x_right_overwriting (s)
3053 struct glyph_string *s;
3054 {
3055 int i, k, x;
3056 int end = s->row->used[s->area];
3057 struct glyph *glyphs = s->row->glyphs[s->area];
3058 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3059
3060 k = -1;
3061 x = 0;
3062 for (i = first; i < end; ++i)
3063 {
3064 int left, right;
3065 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3066 if (x - left < 0)
3067 k = i;
3068 x += glyphs[i].pixel_width;
3069 }
3070
3071 return k;
3072 }
3073
3074
3075 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3076
3077 static INLINE void
3078 x_clear_glyph_string_rect (s, x, y, w, h)
3079 struct glyph_string *s;
3080 int x, y, w, h;
3081 {
3082 XGCValues xgcv;
3083 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3084 XSetForeground (s->display, s->gc, xgcv.background);
3085 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3086 XSetForeground (s->display, s->gc, xgcv.foreground);
3087 }
3088
3089
3090 /* Draw the background of glyph_string S. If S->background_filled_p
3091 is non-zero don't draw it. FORCE_P non-zero means draw the
3092 background even if it wouldn't be drawn normally. This is used
3093 when a string preceding S draws into the background of S, or S
3094 contains the first component of a composition. */
3095
3096 static void
3097 x_draw_glyph_string_background (s, force_p)
3098 struct glyph_string *s;
3099 int force_p;
3100 {
3101 /* Nothing to do if background has already been drawn or if it
3102 shouldn't be drawn in the first place. */
3103 if (!s->background_filled_p)
3104 {
3105 int box_line_width = max (s->face->box_line_width, 0);
3106
3107 if (s->stippled_p)
3108 {
3109 /* Fill background with a stipple pattern. */
3110 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3111 XFillRectangle (s->display, s->window, s->gc, s->x,
3112 s->y + box_line_width,
3113 s->background_width,
3114 s->height - 2 * box_line_width);
3115 XSetFillStyle (s->display, s->gc, FillSolid);
3116 s->background_filled_p = 1;
3117 }
3118 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
3119 || s->font_not_found_p
3120 || s->extends_to_end_of_line_p
3121 || force_p)
3122 {
3123 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
3124 s->background_width,
3125 s->height - 2 * box_line_width);
3126 s->background_filled_p = 1;
3127 }
3128 }
3129 }
3130
3131
3132 /* Draw the foreground of glyph string S. */
3133
3134 static void
3135 x_draw_glyph_string_foreground (s)
3136 struct glyph_string *s;
3137 {
3138 int i, x;
3139
3140 /* If first glyph of S has a left box line, start drawing the text
3141 of S to the right of that box line. */
3142 if (s->face->box != FACE_NO_BOX
3143 && s->first_glyph->left_box_line_p)
3144 x = s->x + abs (s->face->box_line_width);
3145 else
3146 x = s->x;
3147
3148 /* Draw characters of S as rectangles if S's font could not be
3149 loaded. */
3150 if (s->font_not_found_p)
3151 {
3152 for (i = 0; i < s->nchars; ++i)
3153 {
3154 struct glyph *g = s->first_glyph + i;
3155 XDrawRectangle (s->display, s->window,
3156 s->gc, x, s->y, g->pixel_width - 1,
3157 s->height - 1);
3158 x += g->pixel_width;
3159 }
3160 }
3161 else
3162 {
3163 char *char1b = (char *) s->char2b;
3164 int boff = s->font_info->baseline_offset;
3165
3166 if (s->font_info->vertical_centering)
3167 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3168
3169 /* If we can use 8-bit functions, condense S->char2b. */
3170 if (!s->two_byte_p)
3171 for (i = 0; i < s->nchars; ++i)
3172 char1b[i] = s->char2b[i].byte2;
3173
3174 /* Draw text with XDrawString if background has already been
3175 filled. Otherwise, use XDrawImageString. (Note that
3176 XDrawImageString is usually faster than XDrawString.) Always
3177 use XDrawImageString when drawing the cursor so that there is
3178 no chance that characters under a box cursor are invisible. */
3179 if (s->for_overlaps_p
3180 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3181 {
3182 /* Draw characters with 16-bit or 8-bit functions. */
3183 if (s->two_byte_p)
3184 XDrawString16 (s->display, s->window, s->gc, x,
3185 s->ybase - boff, s->char2b, s->nchars);
3186 else
3187 XDrawString (s->display, s->window, s->gc, x,
3188 s->ybase - boff, char1b, s->nchars);
3189 }
3190 else
3191 {
3192 if (s->two_byte_p)
3193 XDrawImageString16 (s->display, s->window, s->gc, x,
3194 s->ybase - boff, s->char2b, s->nchars);
3195 else
3196 XDrawImageString (s->display, s->window, s->gc, x,
3197 s->ybase - boff, char1b, s->nchars);
3198 }
3199 }
3200 }
3201
3202 /* Draw the foreground of composite glyph string S. */
3203
3204 static void
3205 x_draw_composite_glyph_string_foreground (s)
3206 struct glyph_string *s;
3207 {
3208 int i, x;
3209
3210 /* If first glyph of S has a left box line, start drawing the text
3211 of S to the right of that box line. */
3212 if (s->face->box != FACE_NO_BOX
3213 && s->first_glyph->left_box_line_p)
3214 x = s->x + abs (s->face->box_line_width);
3215 else
3216 x = s->x;
3217
3218 /* S is a glyph string for a composition. S->gidx is the index of
3219 the first character drawn for glyphs of this composition.
3220 S->gidx == 0 means we are drawing the very first character of
3221 this composition. */
3222
3223 /* Draw a rectangle for the composition if the font for the very
3224 first character of the composition could not be loaded. */
3225 if (s->font_not_found_p)
3226 {
3227 if (s->gidx == 0)
3228 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3229 s->width - 1, s->height - 1);
3230 }
3231 else
3232 {
3233 for (i = 0; i < s->nchars; i++, ++s->gidx)
3234 XDrawString16 (s->display, s->window, s->gc,
3235 x + s->cmp->offsets[s->gidx * 2],
3236 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3237 s->char2b + i, 1);
3238 }
3239 }
3240
3241
3242 #ifdef USE_X_TOOLKIT
3243
3244 static struct frame *x_frame_of_widget P_ ((Widget));
3245 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
3246 XrmValue *, XrmValue *, XtPointer *));
3247 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
3248 XrmValue *, Cardinal *));
3249
3250
3251 /* Return the frame on which widget WIDGET is used.. Abort if frame
3252 cannot be determined. */
3253
3254 static struct frame *
3255 x_frame_of_widget (widget)
3256 Widget widget;
3257 {
3258 struct x_display_info *dpyinfo;
3259 Lisp_Object tail;
3260 struct frame *f;
3261
3262 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3263
3264 /* Find the top-level shell of the widget. Note that this function
3265 can be called when the widget is not yet realized, so XtWindow
3266 (widget) == 0. That's the reason we can't simply use
3267 x_any_window_to_frame. */
3268 while (!XtIsTopLevelShell (widget))
3269 widget = XtParent (widget);
3270
3271 /* Look for a frame with that top-level widget. Allocate the color
3272 on that frame to get the right gamma correction value. */
3273 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3274 if (GC_FRAMEP (XCAR (tail))
3275 && (f = XFRAME (XCAR (tail)),
3276 (f->output_data.nothing != 1
3277 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3278 && f->output_data.x->widget == widget)
3279 return f;
3280
3281 abort ();
3282 }
3283
3284
3285 /* Allocate the color COLOR->pixel on the screen and display of
3286 widget WIDGET in colormap CMAP. If an exact match cannot be
3287 allocated, try the nearest color available. Value is non-zero
3288 if successful. This is called from lwlib. */
3289
3290 int
3291 x_alloc_nearest_color_for_widget (widget, cmap, color)
3292 Widget widget;
3293 Colormap cmap;
3294 XColor *color;
3295 {
3296 struct frame *f = x_frame_of_widget (widget);
3297 return x_alloc_nearest_color (f, cmap, color);
3298 }
3299
3300
3301 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3302 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3303 If this produces the same color as PIXEL, try a color where all RGB
3304 values have DELTA added. Return the allocated color in *PIXEL.
3305 DISPLAY is the X display, CMAP is the colormap to operate on.
3306 Value is non-zero if successful. */
3307
3308 int
3309 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3310 Widget widget;
3311 Display *display;
3312 Colormap cmap;
3313 unsigned long *pixel;
3314 double factor;
3315 int delta;
3316 {
3317 struct frame *f = x_frame_of_widget (widget);
3318 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3319 }
3320
3321
3322 /* Structure specifying which arguments should be passed by Xt to
3323 cvt_string_to_pixel. We want the widget's screen and colormap. */
3324
3325 static XtConvertArgRec cvt_string_to_pixel_args[] =
3326 {
3327 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
3328 sizeof (Screen *)},
3329 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
3330 sizeof (Colormap)}
3331 };
3332
3333
3334 /* The address of this variable is returned by
3335 cvt_string_to_pixel. */
3336
3337 static Pixel cvt_string_to_pixel_value;
3338
3339
3340 /* Convert a color name to a pixel color.
3341
3342 DPY is the display we are working on.
3343
3344 ARGS is an array of *NARGS XrmValue structures holding additional
3345 information about the widget for which the conversion takes place.
3346 The contents of this array are determined by the specification
3347 in cvt_string_to_pixel_args.
3348
3349 FROM is a pointer to an XrmValue which points to the color name to
3350 convert. TO is an XrmValue in which to return the pixel color.
3351
3352 CLOSURE_RET is a pointer to user-data, in which we record if
3353 we allocated the color or not.
3354
3355 Value is True if successful, False otherwise. */
3356
3357 static Boolean
3358 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
3359 Display *dpy;
3360 XrmValue *args;
3361 Cardinal *nargs;
3362 XrmValue *from, *to;
3363 XtPointer *closure_ret;
3364 {
3365 Screen *screen;
3366 Colormap cmap;
3367 Pixel pixel;
3368 String color_name;
3369 XColor color;
3370
3371 if (*nargs != 2)
3372 {
3373 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3374 "wrongParameters", "cvt_string_to_pixel",
3375 "XtToolkitError",
3376 "Screen and colormap args required", NULL, NULL);
3377 return False;
3378 }
3379
3380 screen = *(Screen **) args[0].addr;
3381 cmap = *(Colormap *) args[1].addr;
3382 color_name = (String) from->addr;
3383
3384 if (strcmp (color_name, XtDefaultBackground) == 0)
3385 {
3386 *closure_ret = (XtPointer) False;
3387 pixel = WhitePixelOfScreen (screen);
3388 }
3389 else if (strcmp (color_name, XtDefaultForeground) == 0)
3390 {
3391 *closure_ret = (XtPointer) False;
3392 pixel = BlackPixelOfScreen (screen);
3393 }
3394 else if (XParseColor (dpy, cmap, color_name, &color)
3395 && x_alloc_nearest_color_1 (dpy, cmap, &color))
3396 {
3397 pixel = color.pixel;
3398 *closure_ret = (XtPointer) True;
3399 }
3400 else
3401 {
3402 String params[1];
3403 Cardinal nparams = 1;
3404
3405 params[0] = color_name;
3406 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3407 "badValue", "cvt_string_to_pixel",
3408 "XtToolkitError", "Invalid color `%s'",
3409 params, &nparams);
3410 return False;
3411 }
3412
3413 if (to->addr != NULL)
3414 {
3415 if (to->size < sizeof (Pixel))
3416 {
3417 to->size = sizeof (Pixel);
3418 return False;
3419 }
3420
3421 *(Pixel *) to->addr = pixel;
3422 }
3423 else
3424 {
3425 cvt_string_to_pixel_value = pixel;
3426 to->addr = (XtPointer) &cvt_string_to_pixel_value;
3427 }
3428
3429 to->size = sizeof (Pixel);
3430 return True;
3431 }
3432
3433
3434 /* Free a pixel color which was previously allocated via
3435 cvt_string_to_pixel. This is registered as the destructor
3436 for this type of resource via XtSetTypeConverter.
3437
3438 APP is the application context in which we work.
3439
3440 TO is a pointer to an XrmValue holding the color to free.
3441 CLOSURE is the value we stored in CLOSURE_RET for this color
3442 in cvt_string_to_pixel.
3443
3444 ARGS and NARGS are like for cvt_string_to_pixel. */
3445
3446 static void
3447 cvt_pixel_dtor (app, to, closure, args, nargs)
3448 XtAppContext app;
3449 XrmValuePtr to;
3450 XtPointer closure;
3451 XrmValuePtr args;
3452 Cardinal *nargs;
3453 {
3454 if (*nargs != 2)
3455 {
3456 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
3457 "XtToolkitError",
3458 "Screen and colormap arguments required",
3459 NULL, NULL);
3460 }
3461 else if (closure != NULL)
3462 {
3463 /* We did allocate the pixel, so free it. */
3464 Screen *screen = *(Screen **) args[0].addr;
3465 Colormap cmap = *(Colormap *) args[1].addr;
3466 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
3467 (Pixel *) to->addr, 1);
3468 }
3469 }
3470
3471
3472 #endif /* USE_X_TOOLKIT */
3473
3474
3475 /* Value is an array of XColor structures for the contents of the
3476 color map of display DPY. Set *NCELLS to the size of the array.
3477 Note that this probably shouldn't be called for large color maps,
3478 say a 24-bit TrueColor map. */
3479
3480 static const XColor *
3481 x_color_cells (dpy, ncells)
3482 Display *dpy;
3483 int *ncells;
3484 {
3485 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3486
3487 if (dpyinfo->color_cells == NULL)
3488 {
3489 Screen *screen = dpyinfo->screen;
3490 int i;
3491
3492 dpyinfo->ncolor_cells
3493 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
3494 dpyinfo->color_cells
3495 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3496 * sizeof *dpyinfo->color_cells);
3497
3498 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3499 dpyinfo->color_cells[i].pixel = i;
3500
3501 XQueryColors (dpy, dpyinfo->cmap,
3502 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3503 }
3504
3505 *ncells = dpyinfo->ncolor_cells;
3506 return dpyinfo->color_cells;
3507 }
3508
3509
3510 /* On frame F, translate pixel colors to RGB values for the NCOLORS
3511 colors in COLORS. Use cached information, if available. */
3512
3513 void
3514 x_query_colors (f, colors, ncolors)
3515 struct frame *f;
3516 XColor *colors;
3517 int ncolors;
3518 {
3519 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3520
3521 if (dpyinfo->color_cells)
3522 {
3523 int i;
3524 for (i = 0; i < ncolors; ++i)
3525 {
3526 unsigned long pixel = colors[i].pixel;
3527 xassert (pixel < dpyinfo->ncolor_cells);
3528 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3529 colors[i] = dpyinfo->color_cells[pixel];
3530 }
3531 }
3532 else
3533 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3534 }
3535
3536
3537 /* On frame F, translate pixel color to RGB values for the color in
3538 COLOR. Use cached information, if available. */
3539
3540 void
3541 x_query_color (f, color)
3542 struct frame *f;
3543 XColor *color;
3544 {
3545 x_query_colors (f, color, 1);
3546 }
3547
3548
3549 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
3550 exact match can't be allocated, try the nearest color available.
3551 Value is non-zero if successful. Set *COLOR to the color
3552 allocated. */
3553
3554 static int
3555 x_alloc_nearest_color_1 (dpy, cmap, color)
3556 Display *dpy;
3557 Colormap cmap;
3558 XColor *color;
3559 {
3560 int rc;
3561
3562 rc = XAllocColor (dpy, cmap, color);
3563 if (rc == 0)
3564 {
3565 /* If we got to this point, the colormap is full, so we're going
3566 to try to get the next closest color. The algorithm used is
3567 a least-squares matching, which is what X uses for closest
3568 color matching with StaticColor visuals. */
3569 int nearest, i;
3570 unsigned long nearest_delta = ~0;
3571 int ncells;
3572 const XColor *cells = x_color_cells (dpy, &ncells);
3573
3574 for (nearest = i = 0; i < ncells; ++i)
3575 {
3576 long dred = (color->red >> 8) - (cells[i].red >> 8);
3577 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3578 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3579 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3580
3581 if (delta < nearest_delta)
3582 {
3583 nearest = i;
3584 nearest_delta = delta;
3585 }
3586 }
3587
3588 color->red = cells[nearest].red;
3589 color->green = cells[nearest].green;
3590 color->blue = cells[nearest].blue;
3591 rc = XAllocColor (dpy, cmap, color);
3592 }
3593 else
3594 {
3595 /* If allocation succeeded, and the allocated pixel color is not
3596 equal to a cached pixel color recorded earlier, there was a
3597 change in the colormap, so clear the color cache. */
3598 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3599 XColor *cached_color;
3600
3601 if (dpyinfo->color_cells
3602 && (cached_color = &dpyinfo->color_cells[color->pixel],
3603 (cached_color->red != color->red
3604 || cached_color->blue != color->blue
3605 || cached_color->green != color->green)))
3606 {
3607 xfree (dpyinfo->color_cells);
3608 dpyinfo->color_cells = NULL;
3609 dpyinfo->ncolor_cells = 0;
3610 }
3611 }
3612
3613 #ifdef DEBUG_X_COLORS
3614 if (rc)
3615 register_color (color->pixel);
3616 #endif /* DEBUG_X_COLORS */
3617
3618 return rc;
3619 }
3620
3621
3622 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
3623 exact match can't be allocated, try the nearest color available.
3624 Value is non-zero if successful. Set *COLOR to the color
3625 allocated. */
3626
3627 int
3628 x_alloc_nearest_color (f, cmap, color)
3629 struct frame *f;
3630 Colormap cmap;
3631 XColor *color;
3632 {
3633 gamma_correct (f, color);
3634 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
3635 }
3636
3637
3638 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3639 It's necessary to do this instead of just using PIXEL directly to
3640 get color reference counts right. */
3641
3642 unsigned long
3643 x_copy_color (f, pixel)
3644 struct frame *f;
3645 unsigned long pixel;
3646 {
3647 XColor color;
3648
3649 color.pixel = pixel;
3650 BLOCK_INPUT;
3651 x_query_color (f, &color);
3652 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3653 UNBLOCK_INPUT;
3654 #ifdef DEBUG_X_COLORS
3655 register_color (pixel);
3656 #endif
3657 return color.pixel;
3658 }
3659
3660
3661 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3662 It's necessary to do this instead of just using PIXEL directly to
3663 get color reference counts right. */
3664
3665 unsigned long
3666 x_copy_dpy_color (dpy, cmap, pixel)
3667 Display *dpy;
3668 Colormap cmap;
3669 unsigned long pixel;
3670 {
3671 XColor color;
3672
3673 color.pixel = pixel;
3674 BLOCK_INPUT;
3675 XQueryColor (dpy, cmap, &color);
3676 XAllocColor (dpy, cmap, &color);
3677 UNBLOCK_INPUT;
3678 #ifdef DEBUG_X_COLORS
3679 register_color (pixel);
3680 #endif
3681 return color.pixel;
3682 }
3683
3684
3685 /* Brightness beyond which a color won't have its highlight brightness
3686 boosted.
3687
3688 Nominally, highlight colors for `3d' faces are calculated by
3689 brightening an object's color by a constant scale factor, but this
3690 doesn't yield good results for dark colors, so for colors who's
3691 brightness is less than this value (on a scale of 0-65535) have an
3692 use an additional additive factor.
3693
3694 The value here is set so that the default menu-bar/mode-line color
3695 (grey75) will not have its highlights changed at all. */
3696 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3697
3698
3699 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3700 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3701 If this produces the same color as PIXEL, try a color where all RGB
3702 values have DELTA added. Return the allocated color in *PIXEL.
3703 DISPLAY is the X display, CMAP is the colormap to operate on.
3704 Value is non-zero if successful. */
3705
3706 static int
3707 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3708 struct frame *f;
3709 Display *display;
3710 Colormap cmap;
3711 unsigned long *pixel;
3712 double factor;
3713 int delta;
3714 {
3715 XColor color, new;
3716 long bright;
3717 int success_p;
3718
3719 /* Get RGB color values. */
3720 color.pixel = *pixel;
3721 x_query_color (f, &color);
3722
3723 /* Change RGB values by specified FACTOR. Avoid overflow! */
3724 xassert (factor >= 0);
3725 new.red = min (0xffff, factor * color.red);
3726 new.green = min (0xffff, factor * color.green);
3727 new.blue = min (0xffff, factor * color.blue);
3728
3729 /* Calculate brightness of COLOR. */
3730 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
3731
3732 /* We only boost colors that are darker than
3733 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3734 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3735 /* Make an additive adjustment to NEW, because it's dark enough so
3736 that scaling by FACTOR alone isn't enough. */
3737 {
3738 /* How far below the limit this color is (0 - 1, 1 being darker). */
3739 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3740 /* The additive adjustment. */
3741 int min_delta = delta * dimness * factor / 2;
3742
3743 if (factor < 1)
3744 {
3745 new.red = max (0, new.red - min_delta);
3746 new.green = max (0, new.green - min_delta);
3747 new.blue = max (0, new.blue - min_delta);
3748 }
3749 else
3750 {
3751 new.red = min (0xffff, min_delta + new.red);
3752 new.green = min (0xffff, min_delta + new.green);
3753 new.blue = min (0xffff, min_delta + new.blue);
3754 }
3755 }
3756
3757 /* Try to allocate the color. */
3758 success_p = x_alloc_nearest_color (f, cmap, &new);
3759 if (success_p)
3760 {
3761 if (new.pixel == *pixel)
3762 {
3763 /* If we end up with the same color as before, try adding
3764 delta to the RGB values. */
3765 x_free_colors (f, &new.pixel, 1);
3766
3767 new.red = min (0xffff, delta + color.red);
3768 new.green = min (0xffff, delta + color.green);
3769 new.blue = min (0xffff, delta + color.blue);
3770 success_p = x_alloc_nearest_color (f, cmap, &new);
3771 }
3772 else
3773 success_p = 1;
3774 *pixel = new.pixel;
3775 }
3776
3777 return success_p;
3778 }
3779
3780
3781 /* Set up the foreground color for drawing relief lines of glyph
3782 string S. RELIEF is a pointer to a struct relief containing the GC
3783 with which lines will be drawn. Use a color that is FACTOR or
3784 DELTA lighter or darker than the relief's background which is found
3785 in S->f->output_data.x->relief_background. If such a color cannot
3786 be allocated, use DEFAULT_PIXEL, instead. */
3787
3788 static void
3789 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3790 struct frame *f;
3791 struct relief *relief;
3792 double factor;
3793 int delta;
3794 unsigned long default_pixel;
3795 {
3796 XGCValues xgcv;
3797 struct x_output *di = f->output_data.x;
3798 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3799 unsigned long pixel;
3800 unsigned long background = di->relief_background;
3801 Colormap cmap = FRAME_X_COLORMAP (f);
3802 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3803 Display *dpy = FRAME_X_DISPLAY (f);
3804
3805 xgcv.graphics_exposures = False;
3806 xgcv.line_width = 1;
3807
3808 /* Free previously allocated color. The color cell will be reused
3809 when it has been freed as many times as it was allocated, so this
3810 doesn't affect faces using the same colors. */
3811 if (relief->gc
3812 && relief->allocated_p)
3813 {
3814 x_free_colors (f, &relief->pixel, 1);
3815 relief->allocated_p = 0;
3816 }
3817
3818 /* Allocate new color. */
3819 xgcv.foreground = default_pixel;
3820 pixel = background;
3821 if (dpyinfo->n_planes != 1
3822 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3823 {
3824 relief->allocated_p = 1;
3825 xgcv.foreground = relief->pixel = pixel;
3826 }
3827
3828 if (relief->gc == 0)
3829 {
3830 xgcv.stipple = dpyinfo->gray;
3831 mask |= GCStipple;
3832 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3833 }
3834 else
3835 XChangeGC (dpy, relief->gc, mask, &xgcv);
3836 }
3837
3838
3839 /* Set up colors for the relief lines around glyph string S. */
3840
3841 static void
3842 x_setup_relief_colors (s)
3843 struct glyph_string *s;
3844 {
3845 struct x_output *di = s->f->output_data.x;
3846 unsigned long color;
3847
3848 if (s->face->use_box_color_for_shadows_p)
3849 color = s->face->box_color;
3850 else
3851 {
3852 XGCValues xgcv;
3853
3854 /* Get the background color of the face. */
3855 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3856 color = xgcv.background;
3857 }
3858
3859 if (di->white_relief.gc == 0
3860 || color != di->relief_background)
3861 {
3862 di->relief_background = color;
3863 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3864 WHITE_PIX_DEFAULT (s->f));
3865 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3866 BLACK_PIX_DEFAULT (s->f));
3867 }
3868 }
3869
3870
3871 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3872 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3873 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3874 relief. LEFT_P non-zero means draw a relief on the left side of
3875 the rectangle. RIGHT_P non-zero means draw a relief on the right
3876 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3877 when drawing. */
3878
3879 static void
3880 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3881 raised_p, left_p, right_p, clip_rect)
3882 struct frame *f;
3883 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3884 XRectangle *clip_rect;
3885 {
3886 Display *dpy = FRAME_X_DISPLAY (f);
3887 Window window = FRAME_X_WINDOW (f);
3888 int i;
3889 GC gc;
3890
3891 if (raised_p)
3892 gc = f->output_data.x->white_relief.gc;
3893 else
3894 gc = f->output_data.x->black_relief.gc;
3895 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
3896
3897 /* Top. */
3898 for (i = 0; i < width; ++i)
3899 XDrawLine (dpy, window, gc,
3900 left_x + i * left_p, top_y + i,
3901 right_x + 1 - i * right_p, top_y + i);
3902
3903 /* Left. */
3904 if (left_p)
3905 for (i = 0; i < width; ++i)
3906 XDrawLine (dpy, window, gc,
3907 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
3908
3909 XSetClipMask (dpy, gc, None);
3910 if (raised_p)
3911 gc = f->output_data.x->black_relief.gc;
3912 else
3913 gc = f->output_data.x->white_relief.gc;
3914 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
3915
3916 /* Bottom. */
3917 for (i = 0; i < width; ++i)
3918 XDrawLine (dpy, window, gc,
3919 left_x + i * left_p, bottom_y - i,
3920 right_x + 2 - i * right_p, bottom_y - i);
3921
3922 /* Right. */
3923 if (right_p)
3924 for (i = 0; i < width; ++i)
3925 XDrawLine (dpy, window, gc,
3926 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3927
3928 XSetClipMask (dpy, gc, None);
3929 }
3930
3931
3932 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3933 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3934 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3935 left side of the rectangle. RIGHT_P non-zero means draw a line
3936 on the right side of the rectangle. CLIP_RECT is the clipping
3937 rectangle to use when drawing. */
3938
3939 static void
3940 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3941 left_p, right_p, clip_rect)
3942 struct glyph_string *s;
3943 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3944 XRectangle *clip_rect;
3945 {
3946 XGCValues xgcv;
3947
3948 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3949 XSetForeground (s->display, s->gc, s->face->box_color);
3950 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3951
3952 /* Top. */
3953 XFillRectangle (s->display, s->window, s->gc,
3954 left_x, top_y, right_x - left_x + 1, width);
3955
3956 /* Left. */
3957 if (left_p)
3958 XFillRectangle (s->display, s->window, s->gc,
3959 left_x, top_y, width, bottom_y - top_y + 1);
3960
3961 /* Bottom. */
3962 XFillRectangle (s->display, s->window, s->gc,
3963 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3964
3965 /* Right. */
3966 if (right_p)
3967 XFillRectangle (s->display, s->window, s->gc,
3968 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3969
3970 XSetForeground (s->display, s->gc, xgcv.foreground);
3971 XSetClipMask (s->display, s->gc, None);
3972 }
3973
3974
3975 /* Draw a box around glyph string S. */
3976
3977 static void
3978 x_draw_glyph_string_box (s)
3979 struct glyph_string *s;
3980 {
3981 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3982 int left_p, right_p;
3983 struct glyph *last_glyph;
3984 XRectangle clip_rect;
3985
3986 last_x = window_box_right (s->w, s->area);
3987 if (s->row->full_width_p
3988 && !s->w->pseudo_window_p)
3989 {
3990 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3991 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3992 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3993 }
3994
3995 /* The glyph that may have a right box line. */
3996 last_glyph = (s->cmp || s->img
3997 ? s->first_glyph
3998 : s->first_glyph + s->nchars - 1);
3999
4000 width = abs (s->face->box_line_width);
4001 raised_p = s->face->box == FACE_RAISED_BOX;
4002 left_x = s->x;
4003 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
4004 ? last_x - 1
4005 : min (last_x, s->x + s->background_width) - 1);
4006 top_y = s->y;
4007 bottom_y = top_y + s->height - 1;
4008
4009 left_p = (s->first_glyph->left_box_line_p
4010 || (s->hl == DRAW_MOUSE_FACE
4011 && (s->prev == NULL
4012 || s->prev->hl != s->hl)));
4013 right_p = (last_glyph->right_box_line_p
4014 || (s->hl == DRAW_MOUSE_FACE
4015 && (s->next == NULL
4016 || s->next->hl != s->hl)));
4017
4018 x_get_glyph_string_clip_rect (s, &clip_rect);
4019
4020 if (s->face->box == FACE_SIMPLE_BOX)
4021 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
4022 left_p, right_p, &clip_rect);
4023 else
4024 {
4025 x_setup_relief_colors (s);
4026 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
4027 width, raised_p, left_p, right_p, &clip_rect);
4028 }
4029 }
4030
4031
4032 /* Draw foreground of image glyph string S. */
4033
4034 static void
4035 x_draw_image_foreground (s)
4036 struct glyph_string *s;
4037 {
4038 int x;
4039 int y = s->ybase - image_ascent (s->img, s->face);
4040
4041 /* If first glyph of S has a left box line, start drawing it to the
4042 right of that line. */
4043 if (s->face->box != FACE_NO_BOX
4044 && s->first_glyph->left_box_line_p)
4045 x = s->x + abs (s->face->box_line_width);
4046 else
4047 x = s->x;
4048
4049 /* If there is a margin around the image, adjust x- and y-position
4050 by that margin. */
4051 x += s->img->hmargin;
4052 y += s->img->vmargin;
4053
4054 if (s->img->pixmap)
4055 {
4056 if (s->img->mask)
4057 {
4058 /* We can't set both a clip mask and use XSetClipRectangles
4059 because the latter also sets a clip mask. We also can't
4060 trust on the shape extension to be available
4061 (XShapeCombineRegion). So, compute the rectangle to draw
4062 manually. */
4063 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4064 | GCFunction);
4065 XGCValues xgcv;
4066 XRectangle clip_rect, image_rect, r;
4067
4068 xgcv.clip_mask = s->img->mask;
4069 xgcv.clip_x_origin = x;
4070 xgcv.clip_y_origin = y;
4071 xgcv.function = GXcopy;
4072 XChangeGC (s->display, s->gc, mask, &xgcv);
4073
4074 x_get_glyph_string_clip_rect (s, &clip_rect);
4075 image_rect.x = x;
4076 image_rect.y = y;
4077 image_rect.width = s->img->width;
4078 image_rect.height = s->img->height;
4079 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4080 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4081 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4082 }
4083 else
4084 {
4085 unsigned long mask = GCClipXOrigin | GCClipYOrigin | GCFunction;
4086 XGCValues xgcv;
4087 XRectangle clip_rect, image_rect, r;
4088
4089 x_get_glyph_string_clip_rect (s, &clip_rect);
4090 image_rect.x = x;
4091 image_rect.y = y;
4092 image_rect.width = s->img->width;
4093 image_rect.height = s->img->height;
4094 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4095 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4096 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4097
4098 /* When the image has a mask, we can expect that at
4099 least part of a mouse highlight or a block cursor will
4100 be visible. If the image doesn't have a mask, make
4101 a block cursor visible by drawing a rectangle around
4102 the image. I believe it's looking better if we do
4103 nothing here for mouse-face. */
4104 if (s->hl == DRAW_CURSOR)
4105 XDrawRectangle (s->display, s->window, s->gc, x, y,
4106 s->img->width - 1, s->img->height - 1);
4107 }
4108 }
4109 else
4110 /* Draw a rectangle if image could not be loaded. */
4111 XDrawRectangle (s->display, s->window, s->gc, x, y,
4112 s->img->width - 1, s->img->height - 1);
4113 }
4114
4115
4116 /* Draw a relief around the image glyph string S. */
4117
4118 static void
4119 x_draw_image_relief (s)
4120 struct glyph_string *s;
4121 {
4122 int x0, y0, x1, y1, thick, raised_p;
4123 XRectangle r;
4124 int x;
4125 int y = s->ybase - image_ascent (s->img, s->face);
4126
4127 /* If first glyph of S has a left box line, start drawing it to the
4128 right of that line. */
4129 if (s->face->box != FACE_NO_BOX
4130 && s->first_glyph->left_box_line_p)
4131 x = s->x + abs (s->face->box_line_width);
4132 else
4133 x = s->x;
4134
4135 /* If there is a margin around the image, adjust x- and y-position
4136 by that margin. */
4137 x += s->img->hmargin;
4138 y += s->img->vmargin;
4139
4140 if (s->hl == DRAW_IMAGE_SUNKEN
4141 || s->hl == DRAW_IMAGE_RAISED)
4142 {
4143 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
4144 raised_p = s->hl == DRAW_IMAGE_RAISED;
4145 }
4146 else
4147 {
4148 thick = abs (s->img->relief);
4149 raised_p = s->img->relief > 0;
4150 }
4151
4152 x0 = x - thick;
4153 y0 = y - thick;
4154 x1 = x + s->img->width + thick - 1;
4155 y1 = y + s->img->height + thick - 1;
4156
4157 x_setup_relief_colors (s);
4158 x_get_glyph_string_clip_rect (s, &r);
4159 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
4160 }
4161
4162
4163 /* Draw the foreground of image glyph string S to PIXMAP. */
4164
4165 static void
4166 x_draw_image_foreground_1 (s, pixmap)
4167 struct glyph_string *s;
4168 Pixmap pixmap;
4169 {
4170 int x;
4171 int y = s->ybase - s->y - image_ascent (s->img, s->face);
4172
4173 /* If first glyph of S has a left box line, start drawing it to the
4174 right of that line. */
4175 if (s->face->box != FACE_NO_BOX
4176 && s->first_glyph->left_box_line_p)
4177 x = abs (s->face->box_line_width);
4178 else
4179 x = 0;
4180
4181 /* If there is a margin around the image, adjust x- and y-position
4182 by that margin. */
4183 x += s->img->hmargin;
4184 y += s->img->vmargin;
4185
4186 if (s->img->pixmap)
4187 {
4188 if (s->img->mask)
4189 {
4190 /* We can't set both a clip mask and use XSetClipRectangles
4191 because the latter also sets a clip mask. We also can't
4192 trust on the shape extension to be available
4193 (XShapeCombineRegion). So, compute the rectangle to draw
4194 manually. */
4195 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4196 | GCFunction);
4197 XGCValues xgcv;
4198
4199 xgcv.clip_mask = s->img->mask;
4200 xgcv.clip_x_origin = x;
4201 xgcv.clip_y_origin = y;
4202 xgcv.function = GXcopy;
4203 XChangeGC (s->display, s->gc, mask, &xgcv);
4204
4205 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4206 0, 0, s->img->width, s->img->height, x, y);
4207 XSetClipMask (s->display, s->gc, None);
4208 }
4209 else
4210 {
4211 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4212 0, 0, s->img->width, s->img->height, x, y);
4213
4214 /* When the image has a mask, we can expect that at
4215 least part of a mouse highlight or a block cursor will
4216 be visible. If the image doesn't have a mask, make
4217 a block cursor visible by drawing a rectangle around
4218 the image. I believe it's looking better if we do
4219 nothing here for mouse-face. */
4220 if (s->hl == DRAW_CURSOR)
4221 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4222 s->img->width - 1, s->img->height - 1);
4223 }
4224 }
4225 else
4226 /* Draw a rectangle if image could not be loaded. */
4227 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4228 s->img->width - 1, s->img->height - 1);
4229 }
4230
4231
4232 /* Draw part of the background of glyph string S. X, Y, W, and H
4233 give the rectangle to draw. */
4234
4235 static void
4236 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4237 struct glyph_string *s;
4238 int x, y, w, h;
4239 {
4240 if (s->stippled_p)
4241 {
4242 /* Fill background with a stipple pattern. */
4243 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4244 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4245 XSetFillStyle (s->display, s->gc, FillSolid);
4246 }
4247 else
4248 x_clear_glyph_string_rect (s, x, y, w, h);
4249 }
4250
4251
4252 /* Draw image glyph string S.
4253
4254 s->y
4255 s->x +-------------------------
4256 | s->face->box
4257 |
4258 | +-------------------------
4259 | | s->img->margin
4260 | |
4261 | | +-------------------
4262 | | | the image
4263
4264 */
4265
4266 static void
4267 x_draw_image_glyph_string (s)
4268 struct glyph_string *s;
4269 {
4270 int x, y;
4271 int box_line_hwidth = abs (s->face->box_line_width);
4272 int box_line_vwidth = max (s->face->box_line_width, 0);
4273 int height;
4274 Pixmap pixmap = None;
4275
4276 height = s->height - 2 * box_line_vwidth;
4277
4278 /* Fill background with face under the image. Do it only if row is
4279 taller than image or if image has a clip mask to reduce
4280 flickering. */
4281 s->stippled_p = s->face->stipple != 0;
4282 if (height > s->img->height
4283 || s->img->hmargin
4284 || s->img->vmargin
4285 || s->img->mask
4286 || s->img->pixmap == 0
4287 || s->width != s->background_width)
4288 {
4289 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4290 x = s->x + box_line_hwidth;
4291 else
4292 x = s->x;
4293
4294 y = s->y + box_line_vwidth;
4295
4296 if (s->img->mask)
4297 {
4298 /* Create a pixmap as large as the glyph string. Fill it
4299 with the background color. Copy the image to it, using
4300 its mask. Copy the temporary pixmap to the display. */
4301 Screen *screen = FRAME_X_SCREEN (s->f);
4302 int depth = DefaultDepthOfScreen (screen);
4303
4304 /* Create a pixmap as large as the glyph string. */
4305 pixmap = XCreatePixmap (s->display, s->window,
4306 s->background_width,
4307 s->height, depth);
4308
4309 /* Don't clip in the following because we're working on the
4310 pixmap. */
4311 XSetClipMask (s->display, s->gc, None);
4312
4313 /* Fill the pixmap with the background color/stipple. */
4314 if (s->stippled_p)
4315 {
4316 /* Fill background with a stipple pattern. */
4317 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4318 XFillRectangle (s->display, pixmap, s->gc,
4319 0, 0, s->background_width, s->height);
4320 XSetFillStyle (s->display, s->gc, FillSolid);
4321 }
4322 else
4323 {
4324 XGCValues xgcv;
4325 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4326 &xgcv);
4327 XSetForeground (s->display, s->gc, xgcv.background);
4328 XFillRectangle (s->display, pixmap, s->gc,
4329 0, 0, s->background_width, s->height);
4330 XSetForeground (s->display, s->gc, xgcv.foreground);
4331 }
4332 }
4333 else
4334 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4335
4336 s->background_filled_p = 1;
4337 }
4338
4339 /* Draw the foreground. */
4340 if (pixmap != None)
4341 {
4342 x_draw_image_foreground_1 (s, pixmap);
4343 x_set_glyph_string_clipping (s);
4344 XCopyArea (s->display, pixmap, s->window, s->gc,
4345 0, 0, s->background_width, s->height, s->x, s->y);
4346 XFreePixmap (s->display, pixmap);
4347 }
4348 else
4349 x_draw_image_foreground (s);
4350
4351 /* If we must draw a relief around the image, do it. */
4352 if (s->img->relief
4353 || s->hl == DRAW_IMAGE_RAISED
4354 || s->hl == DRAW_IMAGE_SUNKEN)
4355 x_draw_image_relief (s);
4356 }
4357
4358
4359 /* Draw stretch glyph string S. */
4360
4361 static void
4362 x_draw_stretch_glyph_string (s)
4363 struct glyph_string *s;
4364 {
4365 xassert (s->first_glyph->type == STRETCH_GLYPH);
4366 s->stippled_p = s->face->stipple != 0;
4367
4368 if (s->hl == DRAW_CURSOR
4369 && !x_stretch_cursor_p)
4370 {
4371 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4372 as wide as the stretch glyph. */
4373 int width = min (CANON_X_UNIT (s->f), s->background_width);
4374
4375 /* Draw cursor. */
4376 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4377
4378 /* Clear rest using the GC of the original non-cursor face. */
4379 if (width < s->background_width)
4380 {
4381 int x = s->x + width, y = s->y;
4382 int w = s->background_width - width, h = s->height;
4383 XRectangle r;
4384 GC gc;
4385
4386 if (s->row->mouse_face_p
4387 && cursor_in_mouse_face_p (s->w))
4388 {
4389 x_set_mouse_face_gc (s);
4390 gc = s->gc;
4391 }
4392 else
4393 gc = s->face->gc;
4394
4395 x_get_glyph_string_clip_rect (s, &r);
4396 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4397
4398 if (s->face->stipple)
4399 {
4400 /* Fill background with a stipple pattern. */
4401 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4402 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4403 XSetFillStyle (s->display, gc, FillSolid);
4404 }
4405 else
4406 {
4407 XGCValues xgcv;
4408 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4409 XSetForeground (s->display, gc, xgcv.background);
4410 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4411 XSetForeground (s->display, gc, xgcv.foreground);
4412 }
4413 }
4414 }
4415 else if (!s->background_filled_p)
4416 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4417 s->height);
4418
4419 s->background_filled_p = 1;
4420 }
4421
4422
4423 /* Draw glyph string S. */
4424
4425 static void
4426 x_draw_glyph_string (s)
4427 struct glyph_string *s;
4428 {
4429 int relief_drawn_p = 0;
4430
4431 /* If S draws into the background of its successor, draw the
4432 background of the successor first so that S can draw into it.
4433 This makes S->next use XDrawString instead of XDrawImageString. */
4434 if (s->next && s->right_overhang && !s->for_overlaps_p)
4435 {
4436 xassert (s->next->img == NULL);
4437 x_set_glyph_string_gc (s->next);
4438 x_set_glyph_string_clipping (s->next);
4439 x_draw_glyph_string_background (s->next, 1);
4440 }
4441
4442 /* Set up S->gc, set clipping and draw S. */
4443 x_set_glyph_string_gc (s);
4444 x_set_glyph_string_clipping (s);
4445
4446 /* Draw relief (if any) in advance for char/composition so that the
4447 glyph string can be drawn over it. */
4448 if (!s->for_overlaps_p
4449 && s->face->box != FACE_NO_BOX
4450 && (s->first_glyph->type == CHAR_GLYPH
4451 || s->first_glyph->type == COMPOSITE_GLYPH))
4452
4453 {
4454 x_draw_glyph_string_background (s, 1);
4455 x_draw_glyph_string_box (s);
4456 relief_drawn_p = 1;
4457 }
4458
4459 switch (s->first_glyph->type)
4460 {
4461 case IMAGE_GLYPH:
4462 x_draw_image_glyph_string (s);
4463 break;
4464
4465 case STRETCH_GLYPH:
4466 x_draw_stretch_glyph_string (s);
4467 break;
4468
4469 case CHAR_GLYPH:
4470 if (s->for_overlaps_p)
4471 s->background_filled_p = 1;
4472 else
4473 x_draw_glyph_string_background (s, 0);
4474 x_draw_glyph_string_foreground (s);
4475 break;
4476
4477 case COMPOSITE_GLYPH:
4478 if (s->for_overlaps_p || s->gidx > 0)
4479 s->background_filled_p = 1;
4480 else
4481 x_draw_glyph_string_background (s, 1);
4482 x_draw_composite_glyph_string_foreground (s);
4483 break;
4484
4485 default:
4486 abort ();
4487 }
4488
4489 if (!s->for_overlaps_p)
4490 {
4491 /* Draw underline. */
4492 if (s->face->underline_p)
4493 {
4494 unsigned long tem, h;
4495 int y;
4496
4497 /* Get the underline thickness. Default is 1 pixel. */
4498 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4499 h = 1;
4500
4501 /* Get the underline position. This is the recommended
4502 vertical offset in pixels from the baseline to the top of
4503 the underline. This is a signed value according to the
4504 specs, and its default is
4505
4506 ROUND ((maximum descent) / 2), with
4507 ROUND(x) = floor (x + 0.5) */
4508
4509 if (x_use_underline_position_properties
4510 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4511 y = s->ybase + (long) tem;
4512 else if (s->face->font)
4513 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4514 else
4515 y = s->y + s->height - h;
4516
4517 if (s->face->underline_defaulted_p)
4518 XFillRectangle (s->display, s->window, s->gc,
4519 s->x, y, s->width, h);
4520 else
4521 {
4522 XGCValues xgcv;
4523 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4524 XSetForeground (s->display, s->gc, s->face->underline_color);
4525 XFillRectangle (s->display, s->window, s->gc,
4526 s->x, y, s->width, h);
4527 XSetForeground (s->display, s->gc, xgcv.foreground);
4528 }
4529 }
4530
4531 /* Draw overline. */
4532 if (s->face->overline_p)
4533 {
4534 unsigned long dy = 0, h = 1;
4535
4536 if (s->face->overline_color_defaulted_p)
4537 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4538 s->width, h);
4539 else
4540 {
4541 XGCValues xgcv;
4542 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4543 XSetForeground (s->display, s->gc, s->face->overline_color);
4544 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4545 s->width, h);
4546 XSetForeground (s->display, s->gc, xgcv.foreground);
4547 }
4548 }
4549
4550 /* Draw strike-through. */
4551 if (s->face->strike_through_p)
4552 {
4553 unsigned long h = 1;
4554 unsigned long dy = (s->height - h) / 2;
4555
4556 if (s->face->strike_through_color_defaulted_p)
4557 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4558 s->width, h);
4559 else
4560 {
4561 XGCValues xgcv;
4562 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4563 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4564 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4565 s->width, h);
4566 XSetForeground (s->display, s->gc, xgcv.foreground);
4567 }
4568 }
4569
4570 /* Draw relief if not yet drawn. */
4571 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
4572 x_draw_glyph_string_box (s);
4573 }
4574
4575 /* Reset clipping. */
4576 XSetClipMask (s->display, s->gc, None);
4577 }
4578
4579
4580 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4581 struct face **, int));
4582
4583
4584 /* Fill glyph string S with composition components specified by S->cmp.
4585
4586 FACES is an array of faces for all components of this composition.
4587 S->gidx is the index of the first component for S.
4588 OVERLAPS_P non-zero means S should draw the foreground only, and
4589 use its physical height for clipping.
4590
4591 Value is the index of a component not in S. */
4592
4593 static int
4594 x_fill_composite_glyph_string (s, faces, overlaps_p)
4595 struct glyph_string *s;
4596 struct face **faces;
4597 int overlaps_p;
4598 {
4599 int i;
4600
4601 xassert (s);
4602
4603 s->for_overlaps_p = overlaps_p;
4604
4605 s->face = faces[s->gidx];
4606 s->font = s->face->font;
4607 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4608
4609 /* For all glyphs of this composition, starting at the offset
4610 S->gidx, until we reach the end of the definition or encounter a
4611 glyph that requires the different face, add it to S. */
4612 ++s->nchars;
4613 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4614 ++s->nchars;
4615
4616 /* All glyph strings for the same composition has the same width,
4617 i.e. the width set for the first component of the composition. */
4618
4619 s->width = s->first_glyph->pixel_width;
4620
4621 /* If the specified font could not be loaded, use the frame's
4622 default font, but record the fact that we couldn't load it in
4623 the glyph string so that we can draw rectangles for the
4624 characters of the glyph string. */
4625 if (s->font == NULL)
4626 {
4627 s->font_not_found_p = 1;
4628 s->font = FRAME_FONT (s->f);
4629 }
4630
4631 /* Adjust base line for subscript/superscript text. */
4632 s->ybase += s->first_glyph->voffset;
4633
4634 xassert (s->face && s->face->gc);
4635
4636 /* This glyph string must always be drawn with 16-bit functions. */
4637 s->two_byte_p = 1;
4638
4639 return s->gidx + s->nchars;
4640 }
4641
4642
4643 /* Fill glyph string S from a sequence of character glyphs.
4644
4645 FACE_ID is the face id of the string. START is the index of the
4646 first glyph to consider, END is the index of the last + 1.
4647 OVERLAPS_P non-zero means S should draw the foreground only, and
4648 use its physical height for clipping.
4649
4650 Value is the index of the first glyph not in S. */
4651
4652 static int
4653 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4654 struct glyph_string *s;
4655 int face_id;
4656 int start, end, overlaps_p;
4657 {
4658 struct glyph *glyph, *last;
4659 int voffset;
4660 int glyph_not_available_p;
4661
4662 xassert (s->f == XFRAME (s->w->frame));
4663 xassert (s->nchars == 0);
4664 xassert (start >= 0 && end > start);
4665
4666 s->for_overlaps_p = overlaps_p,
4667 glyph = s->row->glyphs[s->area] + start;
4668 last = s->row->glyphs[s->area] + end;
4669 voffset = glyph->voffset;
4670
4671 glyph_not_available_p = glyph->glyph_not_available_p;
4672
4673 while (glyph < last
4674 && glyph->type == CHAR_GLYPH
4675 && glyph->voffset == voffset
4676 /* Same face id implies same font, nowadays. */
4677 && glyph->face_id == face_id
4678 && glyph->glyph_not_available_p == glyph_not_available_p)
4679 {
4680 int two_byte_p;
4681
4682 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4683 s->char2b + s->nchars,
4684 &two_byte_p);
4685 s->two_byte_p = two_byte_p;
4686 ++s->nchars;
4687 xassert (s->nchars <= end - start);
4688 s->width += glyph->pixel_width;
4689 ++glyph;
4690 }
4691
4692 s->font = s->face->font;
4693 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4694
4695 /* If the specified font could not be loaded, use the frame's font,
4696 but record the fact that we couldn't load it in
4697 S->font_not_found_p so that we can draw rectangles for the
4698 characters of the glyph string. */
4699 if (s->font == NULL || glyph_not_available_p)
4700 {
4701 s->font_not_found_p = 1;
4702 s->font = FRAME_FONT (s->f);
4703 }
4704
4705 /* Adjust base line for subscript/superscript text. */
4706 s->ybase += voffset;
4707
4708 xassert (s->face && s->face->gc);
4709 return glyph - s->row->glyphs[s->area];
4710 }
4711
4712
4713 /* Fill glyph string S from image glyph S->first_glyph. */
4714
4715 static void
4716 x_fill_image_glyph_string (s)
4717 struct glyph_string *s;
4718 {
4719 xassert (s->first_glyph->type == IMAGE_GLYPH);
4720 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4721 xassert (s->img);
4722 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4723 s->font = s->face->font;
4724 s->width = s->first_glyph->pixel_width;
4725
4726 /* Adjust base line for subscript/superscript text. */
4727 s->ybase += s->first_glyph->voffset;
4728 }
4729
4730
4731 /* Fill glyph string S from a sequence of stretch glyphs.
4732
4733 ROW is the glyph row in which the glyphs are found, AREA is the
4734 area within the row. START is the index of the first glyph to
4735 consider, END is the index of the last + 1.
4736
4737 Value is the index of the first glyph not in S. */
4738
4739 static int
4740 x_fill_stretch_glyph_string (s, row, area, start, end)
4741 struct glyph_string *s;
4742 struct glyph_row *row;
4743 enum glyph_row_area area;
4744 int start, end;
4745 {
4746 struct glyph *glyph, *last;
4747 int voffset, face_id;
4748
4749 xassert (s->first_glyph->type == STRETCH_GLYPH);
4750
4751 glyph = s->row->glyphs[s->area] + start;
4752 last = s->row->glyphs[s->area] + end;
4753 face_id = glyph->face_id;
4754 s->face = FACE_FROM_ID (s->f, face_id);
4755 s->font = s->face->font;
4756 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4757 s->width = glyph->pixel_width;
4758 voffset = glyph->voffset;
4759
4760 for (++glyph;
4761 (glyph < last
4762 && glyph->type == STRETCH_GLYPH
4763 && glyph->voffset == voffset
4764 && glyph->face_id == face_id);
4765 ++glyph)
4766 s->width += glyph->pixel_width;
4767
4768 /* Adjust base line for subscript/superscript text. */
4769 s->ybase += voffset;
4770
4771 /* The case that face->gc == 0 is handled when drawing the glyph
4772 string by calling PREPARE_FACE_FOR_DISPLAY. */
4773 xassert (s->face);
4774 return glyph - s->row->glyphs[s->area];
4775 }
4776
4777
4778 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4779 of XChar2b structures for S; it can't be allocated in
4780 x_init_glyph_string because it must be allocated via `alloca'. W
4781 is the window on which S is drawn. ROW and AREA are the glyph row
4782 and area within the row from which S is constructed. START is the
4783 index of the first glyph structure covered by S. HL is a
4784 face-override for drawing S. */
4785
4786 static void
4787 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4788 struct glyph_string *s;
4789 XChar2b *char2b;
4790 struct window *w;
4791 struct glyph_row *row;
4792 enum glyph_row_area area;
4793 int start;
4794 enum draw_glyphs_face hl;
4795 {
4796 bzero (s, sizeof *s);
4797 s->w = w;
4798 s->f = XFRAME (w->frame);
4799 s->display = FRAME_X_DISPLAY (s->f);
4800 s->window = FRAME_X_WINDOW (s->f);
4801 s->char2b = char2b;
4802 s->hl = hl;
4803 s->row = row;
4804 s->area = area;
4805 s->first_glyph = row->glyphs[area] + start;
4806 s->height = row->height;
4807 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4808
4809 /* Display the internal border below the tool-bar window. */
4810 if (s->w == XWINDOW (s->f->tool_bar_window))
4811 s->y -= s->f->output_data.x->internal_border_width;
4812
4813 s->ybase = s->y + row->ascent;
4814 }
4815
4816
4817 /* Set background width of glyph string S. START is the index of the
4818 first glyph following S. LAST_X is the right-most x-position + 1
4819 in the drawing area. */
4820
4821 static INLINE void
4822 x_set_glyph_string_background_width (s, start, last_x)
4823 struct glyph_string *s;
4824 int start;
4825 int last_x;
4826 {
4827 /* If the face of this glyph string has to be drawn to the end of
4828 the drawing area, set S->extends_to_end_of_line_p. */
4829 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4830
4831 if (start == s->row->used[s->area]
4832 && s->area == TEXT_AREA
4833 && ((s->hl == DRAW_NORMAL_TEXT
4834 && (s->row->fill_line_p
4835 || s->face->background != default_face->background
4836 || s->face->stipple != default_face->stipple
4837 || s->row->mouse_face_p))
4838 || s->hl == DRAW_MOUSE_FACE))
4839 s->extends_to_end_of_line_p = 1;
4840
4841 /* If S extends its face to the end of the line, set its
4842 background_width to the distance to the right edge of the drawing
4843 area. */
4844 if (s->extends_to_end_of_line_p)
4845 s->background_width = last_x - s->x + 1;
4846 else
4847 s->background_width = s->width;
4848 }
4849
4850
4851 /* Add a glyph string for a stretch glyph to the list of strings
4852 between HEAD and TAIL. START is the index of the stretch glyph in
4853 row area AREA of glyph row ROW. END is the index of the last glyph
4854 in that glyph row area. X is the current output position assigned
4855 to the new glyph string constructed. HL overrides that face of the
4856 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4857 is the right-most x-position of the drawing area. */
4858
4859 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4860 and below -- keep them on one line. */
4861 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4862 do \
4863 { \
4864 s = (struct glyph_string *) alloca (sizeof *s); \
4865 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4866 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4867 x_append_glyph_string (&HEAD, &TAIL, s); \
4868 s->x = (X); \
4869 } \
4870 while (0)
4871
4872
4873 /* Add a glyph string for an image glyph to the list of strings
4874 between HEAD and TAIL. START is the index of the image glyph in
4875 row area AREA of glyph row ROW. END is the index of the last glyph
4876 in that glyph row area. X is the current output position assigned
4877 to the new glyph string constructed. HL overrides that face of the
4878 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4879 is the right-most x-position of the drawing area. */
4880
4881 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4882 do \
4883 { \
4884 s = (struct glyph_string *) alloca (sizeof *s); \
4885 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4886 x_fill_image_glyph_string (s); \
4887 x_append_glyph_string (&HEAD, &TAIL, s); \
4888 ++START; \
4889 s->x = (X); \
4890 } \
4891 while (0)
4892
4893
4894 /* Add a glyph string for a sequence of character glyphs to the list
4895 of strings between HEAD and TAIL. START is the index of the first
4896 glyph in row area AREA of glyph row ROW that is part of the new
4897 glyph string. END is the index of the last glyph in that glyph row
4898 area. X is the current output position assigned to the new glyph
4899 string constructed. HL overrides that face of the glyph; e.g. it
4900 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4901 right-most x-position of the drawing area. */
4902
4903 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4904 do \
4905 { \
4906 int c, face_id; \
4907 XChar2b *char2b; \
4908 \
4909 c = (ROW)->glyphs[AREA][START].u.ch; \
4910 face_id = (ROW)->glyphs[AREA][START].face_id; \
4911 \
4912 s = (struct glyph_string *) alloca (sizeof *s); \
4913 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4914 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4915 x_append_glyph_string (&HEAD, &TAIL, s); \
4916 s->x = (X); \
4917 START = x_fill_glyph_string (s, face_id, START, END, \
4918 OVERLAPS_P); \
4919 } \
4920 while (0)
4921
4922
4923 /* Add a glyph string for a composite sequence to the list of strings
4924 between HEAD and TAIL. START is the index of the first glyph in
4925 row area AREA of glyph row ROW that is part of the new glyph
4926 string. END is the index of the last glyph in that glyph row area.
4927 X is the current output position assigned to the new glyph string
4928 constructed. HL overrides that face of the glyph; e.g. it is
4929 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4930 x-position of the drawing area. */
4931
4932 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4933 do { \
4934 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4935 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4936 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4937 struct composition *cmp = composition_table[cmp_id]; \
4938 int glyph_len = cmp->glyph_len; \
4939 XChar2b *char2b; \
4940 struct face **faces; \
4941 struct glyph_string *first_s = NULL; \
4942 int n; \
4943 \
4944 base_face = base_face->ascii_face; \
4945 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4946 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4947 /* At first, fill in `char2b' and `faces'. */ \
4948 for (n = 0; n < glyph_len; n++) \
4949 { \
4950 int c = COMPOSITION_GLYPH (cmp, n); \
4951 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4952 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4953 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4954 this_face_id, char2b + n, 1); \
4955 } \
4956 \
4957 /* Make glyph_strings for each glyph sequence that is drawable by \
4958 the same face, and append them to HEAD/TAIL. */ \
4959 for (n = 0; n < cmp->glyph_len;) \
4960 { \
4961 s = (struct glyph_string *) alloca (sizeof *s); \
4962 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4963 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4964 s->cmp = cmp; \
4965 s->gidx = n; \
4966 s->x = (X); \
4967 \
4968 if (n == 0) \
4969 first_s = s; \
4970 \
4971 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4972 } \
4973 \
4974 ++START; \
4975 s = first_s; \
4976 } while (0)
4977
4978
4979 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4980 of AREA of glyph row ROW on window W between indices START and END.
4981 HL overrides the face for drawing glyph strings, e.g. it is
4982 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4983 x-positions of the drawing area.
4984
4985 This is an ugly monster macro construct because we must use alloca
4986 to allocate glyph strings (because x_draw_glyphs can be called
4987 asynchronously). */
4988
4989 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4990 do \
4991 { \
4992 HEAD = TAIL = NULL; \
4993 while (START < END) \
4994 { \
4995 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4996 switch (first_glyph->type) \
4997 { \
4998 case CHAR_GLYPH: \
4999 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
5000 TAIL, HL, X, LAST_X, \
5001 OVERLAPS_P); \
5002 break; \
5003 \
5004 case COMPOSITE_GLYPH: \
5005 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
5006 HEAD, TAIL, HL, X, LAST_X,\
5007 OVERLAPS_P); \
5008 break; \
5009 \
5010 case STRETCH_GLYPH: \
5011 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
5012 HEAD, TAIL, HL, X, LAST_X); \
5013 break; \
5014 \
5015 case IMAGE_GLYPH: \
5016 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
5017 TAIL, HL, X, LAST_X); \
5018 break; \
5019 \
5020 default: \
5021 abort (); \
5022 } \
5023 \
5024 x_set_glyph_string_background_width (s, START, LAST_X); \
5025 (X) += s->width; \
5026 } \
5027 } \
5028 while (0)
5029
5030
5031 /* Draw glyphs between START and END in AREA of ROW on window W,
5032 starting at x-position X. X is relative to AREA in W. HL is a
5033 face-override with the following meaning:
5034
5035 DRAW_NORMAL_TEXT draw normally
5036 DRAW_CURSOR draw in cursor face
5037 DRAW_MOUSE_FACE draw in mouse face.
5038 DRAW_INVERSE_VIDEO draw in mode line face
5039 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
5040 DRAW_IMAGE_RAISED draw an image with a raised relief around it
5041
5042 If REAL_START is non-null, return in *REAL_START the real starting
5043 position for display. This can be different from START in case
5044 overlapping glyphs must be displayed. If REAL_END is non-null,
5045 return in *REAL_END the real end position for display. This can be
5046 different from END in case overlapping glyphs must be displayed.
5047
5048 If OVERLAPS_P is non-zero, draw only the foreground of characters
5049 and clip to the physical height of ROW.
5050
5051 Value is the x-position reached, relative to AREA of W. */
5052
5053 static int
5054 x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
5055 overlaps_p)
5056 struct window *w;
5057 int x;
5058 struct glyph_row *row;
5059 enum glyph_row_area area;
5060 int start, end;
5061 enum draw_glyphs_face hl;
5062 int *real_start, *real_end;
5063 int overlaps_p;
5064 {
5065 struct glyph_string *head, *tail;
5066 struct glyph_string *s;
5067 int last_x, area_width;
5068 int x_reached;
5069 int i, j;
5070
5071 /* Let's rather be paranoid than getting a SEGV. */
5072 end = min (end, row->used[area]);
5073 start = max (0, start);
5074 start = min (end, start);
5075 if (real_start)
5076 *real_start = start;
5077 if (real_end)
5078 *real_end = end;
5079
5080 /* Translate X to frame coordinates. Set last_x to the right
5081 end of the drawing area. */
5082 if (row->full_width_p)
5083 {
5084 /* X is relative to the left edge of W, without scroll bars
5085 or flag areas. */
5086 struct frame *f = XFRAME (w->frame);
5087 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
5088 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
5089
5090 x += window_left_x;
5091 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
5092 last_x = window_left_x + area_width;
5093
5094 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5095 {
5096 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5097 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
5098 last_x += width;
5099 else
5100 x -= width;
5101 }
5102
5103 x += FRAME_INTERNAL_BORDER_WIDTH (f);
5104 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
5105 }
5106 else
5107 {
5108 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
5109 area_width = window_box_width (w, area);
5110 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
5111 }
5112
5113 /* Build a doubly-linked list of glyph_string structures between
5114 head and tail from what we have to draw. Note that the macro
5115 BUILD_GLYPH_STRINGS will modify its start parameter. That's
5116 the reason we use a separate variable `i'. */
5117 i = start;
5118 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
5119 overlaps_p);
5120 if (tail)
5121 x_reached = tail->x + tail->background_width;
5122 else
5123 x_reached = x;
5124
5125 /* If there are any glyphs with lbearing < 0 or rbearing > width in
5126 the row, redraw some glyphs in front or following the glyph
5127 strings built above. */
5128 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
5129 {
5130 int dummy_x = 0;
5131 struct glyph_string *h, *t;
5132
5133 /* Compute overhangs for all glyph strings. */
5134 for (s = head; s; s = s->next)
5135 x_compute_glyph_string_overhangs (s);
5136
5137 /* Prepend glyph strings for glyphs in front of the first glyph
5138 string that are overwritten because of the first glyph
5139 string's left overhang. The background of all strings
5140 prepended must be drawn because the first glyph string
5141 draws over it. */
5142 i = x_left_overwritten (head);
5143 if (i >= 0)
5144 {
5145 j = i;
5146 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
5147 DRAW_NORMAL_TEXT, dummy_x, last_x,
5148 overlaps_p);
5149 start = i;
5150 if (real_start)
5151 *real_start = start;
5152 x_compute_overhangs_and_x (t, head->x, 1);
5153 x_prepend_glyph_string_lists (&head, &tail, h, t);
5154 }
5155
5156 /* Prepend glyph strings for glyphs in front of the first glyph
5157 string that overwrite that glyph string because of their
5158 right overhang. For these strings, only the foreground must
5159 be drawn, because it draws over the glyph string at `head'.
5160 The background must not be drawn because this would overwrite
5161 right overhangs of preceding glyphs for which no glyph
5162 strings exist. */
5163 i = x_left_overwriting (head);
5164 if (i >= 0)
5165 {
5166 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
5167 DRAW_NORMAL_TEXT, dummy_x, last_x,
5168 overlaps_p);
5169 for (s = h; s; s = s->next)
5170 s->background_filled_p = 1;
5171 if (real_start)
5172 *real_start = i;
5173 x_compute_overhangs_and_x (t, head->x, 1);
5174 x_prepend_glyph_string_lists (&head, &tail, h, t);
5175 }
5176
5177 /* Append glyphs strings for glyphs following the last glyph
5178 string tail that are overwritten by tail. The background of
5179 these strings has to be drawn because tail's foreground draws
5180 over it. */
5181 i = x_right_overwritten (tail);
5182 if (i >= 0)
5183 {
5184 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5185 DRAW_NORMAL_TEXT, x, last_x,
5186 overlaps_p);
5187 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5188 x_append_glyph_string_lists (&head, &tail, h, t);
5189 if (real_end)
5190 *real_end = i;
5191 }
5192
5193 /* Append glyph strings for glyphs following the last glyph
5194 string tail that overwrite tail. The foreground of such
5195 glyphs has to be drawn because it writes into the background
5196 of tail. The background must not be drawn because it could
5197 paint over the foreground of following glyphs. */
5198 i = x_right_overwriting (tail);
5199 if (i >= 0)
5200 {
5201 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5202 DRAW_NORMAL_TEXT, x, last_x,
5203 overlaps_p);
5204 for (s = h; s; s = s->next)
5205 s->background_filled_p = 1;
5206 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5207 x_append_glyph_string_lists (&head, &tail, h, t);
5208 if (real_end)
5209 *real_end = i;
5210 }
5211 }
5212
5213 /* Draw all strings. */
5214 for (s = head; s; s = s->next)
5215 x_draw_glyph_string (s);
5216
5217 /* Value is the x-position up to which drawn, relative to AREA of W.
5218 This doesn't include parts drawn because of overhangs. */
5219 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5220 if (!row->full_width_p)
5221 {
5222 if (area > LEFT_MARGIN_AREA)
5223 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5224 if (area > TEXT_AREA)
5225 x_reached -= window_box_width (w, TEXT_AREA);
5226 }
5227
5228 return x_reached;
5229 }
5230
5231
5232 /* Fix the display of area AREA of overlapping row ROW in window W. */
5233
5234 static void
5235 x_fix_overlapping_area (w, row, area)
5236 struct window *w;
5237 struct glyph_row *row;
5238 enum glyph_row_area area;
5239 {
5240 int i, x;
5241
5242 BLOCK_INPUT;
5243
5244 if (area == LEFT_MARGIN_AREA)
5245 x = 0;
5246 else if (area == TEXT_AREA)
5247 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5248 else
5249 x = (window_box_width (w, LEFT_MARGIN_AREA)
5250 + window_box_width (w, TEXT_AREA));
5251
5252 for (i = 0; i < row->used[area];)
5253 {
5254 if (row->glyphs[area][i].overlaps_vertically_p)
5255 {
5256 int start = i, start_x = x;
5257
5258 do
5259 {
5260 x += row->glyphs[area][i].pixel_width;
5261 ++i;
5262 }
5263 while (i < row->used[area]
5264 && row->glyphs[area][i].overlaps_vertically_p);
5265
5266 x_draw_glyphs (w, start_x, row, area, start, i,
5267 (row->inverse_p
5268 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5269 NULL, NULL, 1);
5270 }
5271 else
5272 {
5273 x += row->glyphs[area][i].pixel_width;
5274 ++i;
5275 }
5276 }
5277
5278 UNBLOCK_INPUT;
5279 }
5280
5281
5282 /* Output LEN glyphs starting at START at the nominal cursor position.
5283 Advance the nominal cursor over the text. The global variable
5284 updated_window contains the window being updated, updated_row is
5285 the glyph row being updated, and updated_area is the area of that
5286 row being updated. */
5287
5288 static void
5289 x_write_glyphs (start, len)
5290 struct glyph *start;
5291 int len;
5292 {
5293 int x, hpos, real_start, real_end;
5294
5295 xassert (updated_window && updated_row);
5296 BLOCK_INPUT;
5297
5298 /* Write glyphs. */
5299
5300 hpos = start - updated_row->glyphs[updated_area];
5301 x = x_draw_glyphs (updated_window, output_cursor.x,
5302 updated_row, updated_area,
5303 hpos, hpos + len,
5304 (updated_row->inverse_p
5305 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5306 &real_start, &real_end, 0);
5307
5308 /* If we drew over the cursor, note that it is not visible any more. */
5309 notice_overwritten_cursor (updated_window, real_start,
5310 real_end - real_start);
5311
5312 UNBLOCK_INPUT;
5313
5314 /* Advance the output cursor. */
5315 output_cursor.hpos += len;
5316 output_cursor.x = x;
5317 }
5318
5319
5320 /* Insert LEN glyphs from START at the nominal cursor position. */
5321
5322 static void
5323 x_insert_glyphs (start, len)
5324 struct glyph *start;
5325 register int len;
5326 {
5327 struct frame *f;
5328 struct window *w;
5329 int line_height, shift_by_width, shifted_region_width;
5330 struct glyph_row *row;
5331 struct glyph *glyph;
5332 int frame_x, frame_y, hpos, real_start, real_end;
5333
5334 xassert (updated_window && updated_row);
5335 BLOCK_INPUT;
5336 w = updated_window;
5337 f = XFRAME (WINDOW_FRAME (w));
5338
5339 /* Get the height of the line we are in. */
5340 row = updated_row;
5341 line_height = row->height;
5342
5343 /* Get the width of the glyphs to insert. */
5344 shift_by_width = 0;
5345 for (glyph = start; glyph < start + len; ++glyph)
5346 shift_by_width += glyph->pixel_width;
5347
5348 /* Get the width of the region to shift right. */
5349 shifted_region_width = (window_box_width (w, updated_area)
5350 - output_cursor.x
5351 - shift_by_width);
5352
5353 /* Shift right. */
5354 frame_x = window_box_left (w, updated_area) + output_cursor.x;
5355 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5356 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5357 f->output_data.x->normal_gc,
5358 frame_x, frame_y,
5359 shifted_region_width, line_height,
5360 frame_x + shift_by_width, frame_y);
5361
5362 /* Write the glyphs. */
5363 hpos = start - row->glyphs[updated_area];
5364 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5365 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
5366 notice_overwritten_cursor (w, real_start, real_end - real_start);
5367
5368 /* Advance the output cursor. */
5369 output_cursor.hpos += len;
5370 output_cursor.x += shift_by_width;
5371 UNBLOCK_INPUT;
5372 }
5373
5374
5375 /* Delete N glyphs at the nominal cursor position. Not implemented
5376 for X frames. */
5377
5378 static void
5379 x_delete_glyphs (n)
5380 register int n;
5381 {
5382 abort ();
5383 }
5384
5385
5386 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5387 If they are <= 0, this is probably an error. */
5388
5389 void
5390 x_clear_area (dpy, window, x, y, width, height, exposures)
5391 Display *dpy;
5392 Window window;
5393 int x, y;
5394 int width, height;
5395 int exposures;
5396 {
5397 xassert (width > 0 && height > 0);
5398 XClearArea (dpy, window, x, y, width, height, exposures);
5399 }
5400
5401
5402 /* Erase the current text line from the nominal cursor position
5403 (inclusive) to pixel column TO_X (exclusive). The idea is that
5404 everything from TO_X onward is already erased.
5405
5406 TO_X is a pixel position relative to updated_area of
5407 updated_window. TO_X == -1 means clear to the end of this area. */
5408
5409 static void
5410 x_clear_end_of_line (to_x)
5411 int to_x;
5412 {
5413 struct frame *f;
5414 struct window *w = updated_window;
5415 int max_x, min_y, max_y;
5416 int from_x, from_y, to_y;
5417
5418 xassert (updated_window && updated_row);
5419 f = XFRAME (w->frame);
5420
5421 if (updated_row->full_width_p)
5422 {
5423 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5424 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5425 && !w->pseudo_window_p)
5426 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5427 }
5428 else
5429 max_x = window_box_width (w, updated_area);
5430 max_y = window_text_bottom_y (w);
5431
5432 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5433 of window. For TO_X > 0, truncate to end of drawing area. */
5434 if (to_x == 0)
5435 return;
5436 else if (to_x < 0)
5437 to_x = max_x;
5438 else
5439 to_x = min (to_x, max_x);
5440
5441 to_y = min (max_y, output_cursor.y + updated_row->height);
5442
5443 /* Notice if the cursor will be cleared by this operation. */
5444 if (!updated_row->full_width_p)
5445 notice_overwritten_cursor (w, output_cursor.hpos, -1);
5446
5447 from_x = output_cursor.x;
5448
5449 /* Translate to frame coordinates. */
5450 if (updated_row->full_width_p)
5451 {
5452 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5453 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5454 }
5455 else
5456 {
5457 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5458 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5459 }
5460
5461 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5462 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5463 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5464
5465 /* Prevent inadvertently clearing to end of the X window. */
5466 if (to_x > from_x && to_y > from_y)
5467 {
5468 BLOCK_INPUT;
5469 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5470 from_x, from_y, to_x - from_x, to_y - from_y,
5471 False);
5472 UNBLOCK_INPUT;
5473 }
5474 }
5475
5476
5477 /* Clear entire frame. If updating_frame is non-null, clear that
5478 frame. Otherwise clear the selected frame. */
5479
5480 static void
5481 x_clear_frame ()
5482 {
5483 struct frame *f;
5484
5485 if (updating_frame)
5486 f = updating_frame;
5487 else
5488 f = SELECTED_FRAME ();
5489
5490 /* Clearing the frame will erase any cursor, so mark them all as no
5491 longer visible. */
5492 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5493 output_cursor.hpos = output_cursor.vpos = 0;
5494 output_cursor.x = -1;
5495
5496 /* We don't set the output cursor here because there will always
5497 follow an explicit cursor_to. */
5498 BLOCK_INPUT;
5499 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5500
5501 /* We have to clear the scroll bars, too. If we have changed
5502 colors or something like that, then they should be notified. */
5503 x_scroll_bar_clear (f);
5504
5505 XFlush (FRAME_X_DISPLAY (f));
5506 UNBLOCK_INPUT;
5507 }
5508
5509
5510 \f
5511 /* Invert the middle quarter of the frame for .15 sec. */
5512
5513 /* We use the select system call to do the waiting, so we have to make
5514 sure it's available. If it isn't, we just won't do visual bells. */
5515
5516 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5517
5518
5519 /* Subtract the `struct timeval' values X and Y, storing the result in
5520 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5521
5522 static int
5523 timeval_subtract (result, x, y)
5524 struct timeval *result, x, y;
5525 {
5526 /* Perform the carry for the later subtraction by updating y. This
5527 is safer because on some systems the tv_sec member is unsigned. */
5528 if (x.tv_usec < y.tv_usec)
5529 {
5530 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5531 y.tv_usec -= 1000000 * nsec;
5532 y.tv_sec += nsec;
5533 }
5534
5535 if (x.tv_usec - y.tv_usec > 1000000)
5536 {
5537 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5538 y.tv_usec += 1000000 * nsec;
5539 y.tv_sec -= nsec;
5540 }
5541
5542 /* Compute the time remaining to wait. tv_usec is certainly
5543 positive. */
5544 result->tv_sec = x.tv_sec - y.tv_sec;
5545 result->tv_usec = x.tv_usec - y.tv_usec;
5546
5547 /* Return indication of whether the result should be considered
5548 negative. */
5549 return x.tv_sec < y.tv_sec;
5550 }
5551
5552 void
5553 XTflash (f)
5554 struct frame *f;
5555 {
5556 BLOCK_INPUT;
5557
5558 {
5559 GC gc;
5560
5561 /* Create a GC that will use the GXxor function to flip foreground
5562 pixels into background pixels. */
5563 {
5564 XGCValues values;
5565
5566 values.function = GXxor;
5567 values.foreground = (f->output_data.x->foreground_pixel
5568 ^ f->output_data.x->background_pixel);
5569
5570 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5571 GCFunction | GCForeground, &values);
5572 }
5573
5574 {
5575 /* Get the height not including a menu bar widget. */
5576 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5577 /* Height of each line to flash. */
5578 int flash_height = FRAME_LINE_HEIGHT (f);
5579 /* These will be the left and right margins of the rectangles. */
5580 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5581 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5582
5583 int width;
5584
5585 /* Don't flash the area between a scroll bar and the frame
5586 edge it is next to. */
5587 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5588 {
5589 case vertical_scroll_bar_left:
5590 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5591 break;
5592
5593 case vertical_scroll_bar_right:
5594 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5595 break;
5596
5597 default:
5598 break;
5599 }
5600
5601 width = flash_right - flash_left;
5602
5603 /* If window is tall, flash top and bottom line. */
5604 if (height > 3 * FRAME_LINE_HEIGHT (f))
5605 {
5606 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5607 flash_left,
5608 (FRAME_INTERNAL_BORDER_WIDTH (f)
5609 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5610 width, flash_height);
5611 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5612 flash_left,
5613 (height - flash_height
5614 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5615 width, flash_height);
5616 }
5617 else
5618 /* If it is short, flash it all. */
5619 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5620 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5621 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5622
5623 x_flush (f);
5624
5625 {
5626 struct timeval wakeup;
5627
5628 EMACS_GET_TIME (wakeup);
5629
5630 /* Compute time to wait until, propagating carry from usecs. */
5631 wakeup.tv_usec += 150000;
5632 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5633 wakeup.tv_usec %= 1000000;
5634
5635 /* Keep waiting until past the time wakeup or any input gets
5636 available. */
5637 while (! detect_input_pending ())
5638 {
5639 struct timeval current;
5640 struct timeval timeout;
5641
5642 EMACS_GET_TIME (current);
5643
5644 /* Break if result would be negative. */
5645 if (timeval_subtract (&current, wakeup, current))
5646 break;
5647
5648 /* How long `select' should wait. */
5649 timeout.tv_sec = 0;
5650 timeout.tv_usec = 10000;
5651
5652 /* Try to wait that long--but we might wake up sooner. */
5653 select (0, NULL, NULL, NULL, &timeout);
5654 }
5655 }
5656
5657 /* If window is tall, flash top and bottom line. */
5658 if (height > 3 * FRAME_LINE_HEIGHT (f))
5659 {
5660 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5661 flash_left,
5662 (FRAME_INTERNAL_BORDER_WIDTH (f)
5663 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5664 width, flash_height);
5665 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5666 flash_left,
5667 (height - flash_height
5668 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5669 width, flash_height);
5670 }
5671 else
5672 /* If it is short, flash it all. */
5673 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5674 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5675 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5676
5677 XFreeGC (FRAME_X_DISPLAY (f), gc);
5678 x_flush (f);
5679 }
5680 }
5681
5682 UNBLOCK_INPUT;
5683 }
5684
5685 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5686
5687
5688 /* Make audible bell. */
5689
5690 void
5691 XTring_bell ()
5692 {
5693 struct frame *f = SELECTED_FRAME ();
5694
5695 if (FRAME_X_DISPLAY (f))
5696 {
5697 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5698 if (visible_bell)
5699 XTflash (f);
5700 else
5701 #endif
5702 {
5703 BLOCK_INPUT;
5704 XBell (FRAME_X_DISPLAY (f), 0);
5705 XFlush (FRAME_X_DISPLAY (f));
5706 UNBLOCK_INPUT;
5707 }
5708 }
5709 }
5710
5711 \f
5712 /* Specify how many text lines, from the top of the window,
5713 should be affected by insert-lines and delete-lines operations.
5714 This, and those operations, are used only within an update
5715 that is bounded by calls to x_update_begin and x_update_end. */
5716
5717 static void
5718 XTset_terminal_window (n)
5719 register int n;
5720 {
5721 /* This function intentionally left blank. */
5722 }
5723
5724
5725 \f
5726 /***********************************************************************
5727 Line Dance
5728 ***********************************************************************/
5729
5730 /* Perform an insert-lines or delete-lines operation, inserting N
5731 lines or deleting -N lines at vertical position VPOS. */
5732
5733 static void
5734 x_ins_del_lines (vpos, n)
5735 int vpos, n;
5736 {
5737 abort ();
5738 }
5739
5740
5741 /* Scroll part of the display as described by RUN. */
5742
5743 static void
5744 x_scroll_run (w, run)
5745 struct window *w;
5746 struct run *run;
5747 {
5748 struct frame *f = XFRAME (w->frame);
5749 int x, y, width, height, from_y, to_y, bottom_y;
5750
5751 /* Get frame-relative bounding box of the text display area of W,
5752 without mode lines. Include in this box the flags areas to the
5753 left and right of W. */
5754 window_box (w, -1, &x, &y, &width, &height);
5755 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5756 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
5757
5758 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5759 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5760 bottom_y = y + height;
5761
5762 if (to_y < from_y)
5763 {
5764 /* Scrolling up. Make sure we don't copy part of the mode
5765 line at the bottom. */
5766 if (from_y + run->height > bottom_y)
5767 height = bottom_y - from_y;
5768 else
5769 height = run->height;
5770 }
5771 else
5772 {
5773 /* Scolling down. Make sure we don't copy over the mode line.
5774 at the bottom. */
5775 if (to_y + run->height > bottom_y)
5776 height = bottom_y - to_y;
5777 else
5778 height = run->height;
5779 }
5780
5781 BLOCK_INPUT;
5782
5783 /* Cursor off. Will be switched on again in x_update_window_end. */
5784 updated_window = w;
5785 x_clear_cursor (w);
5786
5787 XCopyArea (FRAME_X_DISPLAY (f),
5788 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5789 f->output_data.x->normal_gc,
5790 x, from_y,
5791 width, height,
5792 x, to_y);
5793
5794 UNBLOCK_INPUT;
5795 }
5796
5797
5798 \f
5799 /***********************************************************************
5800 Exposure Events
5801 ***********************************************************************/
5802
5803 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5804 corner of the exposed rectangle. W and H are width and height of
5805 the exposed area. All are pixel values. W or H zero means redraw
5806 the entire frame. */
5807
5808 static void
5809 expose_frame (f, x, y, w, h)
5810 struct frame *f;
5811 int x, y, w, h;
5812 {
5813 XRectangle r;
5814 int mouse_face_overwritten_p = 0;
5815
5816 TRACE ((stderr, "expose_frame "));
5817
5818 /* No need to redraw if frame will be redrawn soon. */
5819 if (FRAME_GARBAGED_P (f))
5820 {
5821 TRACE ((stderr, " garbaged\n"));
5822 return;
5823 }
5824
5825 /* If basic faces haven't been realized yet, there is no point in
5826 trying to redraw anything. This can happen when we get an expose
5827 event while Emacs is starting, e.g. by moving another window. */
5828 if (FRAME_FACE_CACHE (f) == NULL
5829 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5830 {
5831 TRACE ((stderr, " no faces\n"));
5832 return;
5833 }
5834
5835 if (w == 0 || h == 0)
5836 {
5837 r.x = r.y = 0;
5838 r.width = CANON_X_UNIT (f) * f->width;
5839 r.height = CANON_Y_UNIT (f) * f->height;
5840 }
5841 else
5842 {
5843 r.x = x;
5844 r.y = y;
5845 r.width = w;
5846 r.height = h;
5847 }
5848
5849 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5850 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
5851
5852 if (WINDOWP (f->tool_bar_window))
5853 mouse_face_overwritten_p
5854 |= expose_window (XWINDOW (f->tool_bar_window), &r);
5855
5856 #ifndef USE_X_TOOLKIT
5857 if (WINDOWP (f->menu_bar_window))
5858 mouse_face_overwritten_p
5859 |= expose_window (XWINDOW (f->menu_bar_window), &r);
5860 #endif /* not USE_X_TOOLKIT */
5861
5862 /* Some window managers support a focus-follows-mouse style with
5863 delayed raising of frames. Imagine a partially obscured frame,
5864 and moving the mouse into partially obscured mouse-face on that
5865 frame. The visible part of the mouse-face will be highlighted,
5866 then the WM raises the obscured frame. With at least one WM, KDE
5867 2.1, Emacs is not getting any event for the raising of the frame
5868 (even tried with SubstructureRedirectMask), only Expose events.
5869 These expose events will draw text normally, i.e. not
5870 highlighted. Which means we must redo the highlight here.
5871 Subsume it under ``we love X''. --gerd 2001-08-15 */
5872 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
5873 {
5874 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5875 if (f == dpyinfo->mouse_face_mouse_frame)
5876 {
5877 int x = dpyinfo->mouse_face_mouse_x;
5878 int y = dpyinfo->mouse_face_mouse_y;
5879 clear_mouse_face (dpyinfo);
5880 note_mouse_highlight (f, x, y);
5881 }
5882 }
5883 }
5884
5885
5886 /* Redraw (parts) of all windows in the window tree rooted at W that
5887 intersect R. R contains frame pixel coordinates. Value is
5888 non-zero if the exposure overwrites mouse-face. */
5889
5890 static int
5891 expose_window_tree (w, r)
5892 struct window *w;
5893 XRectangle *r;
5894 {
5895 struct frame *f = XFRAME (w->frame);
5896 int mouse_face_overwritten_p = 0;
5897
5898 while (w && !FRAME_GARBAGED_P (f))
5899 {
5900 if (!NILP (w->hchild))
5901 mouse_face_overwritten_p
5902 |= expose_window_tree (XWINDOW (w->hchild), r);
5903 else if (!NILP (w->vchild))
5904 mouse_face_overwritten_p
5905 |= expose_window_tree (XWINDOW (w->vchild), r);
5906 else
5907 mouse_face_overwritten_p |= expose_window (w, r);
5908
5909 w = NILP (w->next) ? NULL : XWINDOW (w->next);
5910 }
5911
5912 return mouse_face_overwritten_p;
5913 }
5914
5915
5916 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5917 which intersects rectangle R. R is in window-relative coordinates. */
5918
5919 static void
5920 expose_area (w, row, r, area)
5921 struct window *w;
5922 struct glyph_row *row;
5923 XRectangle *r;
5924 enum glyph_row_area area;
5925 {
5926 struct glyph *first = row->glyphs[area];
5927 struct glyph *end = row->glyphs[area] + row->used[area];
5928 struct glyph *last;
5929 int first_x, start_x, x;
5930
5931 if (area == TEXT_AREA && row->fill_line_p)
5932 /* If row extends face to end of line write the whole line. */
5933 x_draw_glyphs (w, 0, row, area,
5934 0, row->used[area],
5935 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5936 NULL, NULL, 0);
5937 else
5938 {
5939 /* Set START_X to the window-relative start position for drawing glyphs of
5940 AREA. The first glyph of the text area can be partially visible.
5941 The first glyphs of other areas cannot. */
5942 if (area == LEFT_MARGIN_AREA)
5943 start_x = 0;
5944 else if (area == TEXT_AREA)
5945 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5946 else
5947 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5948 + window_box_width (w, TEXT_AREA));
5949 x = start_x;
5950
5951 /* Find the first glyph that must be redrawn. */
5952 while (first < end
5953 && x + first->pixel_width < r->x)
5954 {
5955 x += first->pixel_width;
5956 ++first;
5957 }
5958
5959 /* Find the last one. */
5960 last = first;
5961 first_x = x;
5962 while (last < end
5963 && x < r->x + r->width)
5964 {
5965 x += last->pixel_width;
5966 ++last;
5967 }
5968
5969 /* Repaint. */
5970 if (last > first)
5971 x_draw_glyphs (w, first_x - start_x, row, area,
5972 first - row->glyphs[area],
5973 last - row->glyphs[area],
5974 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5975 NULL, NULL, 0);
5976 }
5977 }
5978
5979
5980 /* Redraw the parts of the glyph row ROW on window W intersecting
5981 rectangle R. R is in window-relative coordinates. Value is
5982 non-zero if mouse-face was overwritten. */
5983
5984 static int
5985 expose_line (w, row, r)
5986 struct window *w;
5987 struct glyph_row *row;
5988 XRectangle *r;
5989 {
5990 xassert (row->enabled_p);
5991
5992 if (row->mode_line_p || w->pseudo_window_p)
5993 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5994 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5995 NULL, NULL, 0);
5996 else
5997 {
5998 if (row->used[LEFT_MARGIN_AREA])
5999 expose_area (w, row, r, LEFT_MARGIN_AREA);
6000 if (row->used[TEXT_AREA])
6001 expose_area (w, row, r, TEXT_AREA);
6002 if (row->used[RIGHT_MARGIN_AREA])
6003 expose_area (w, row, r, RIGHT_MARGIN_AREA);
6004 x_draw_row_bitmaps (w, row);
6005 }
6006
6007 return row->mouse_face_p;
6008 }
6009
6010
6011 /* Return non-zero if W's cursor intersects rectangle R. */
6012
6013 static int
6014 x_phys_cursor_in_rect_p (w, r)
6015 struct window *w;
6016 XRectangle *r;
6017 {
6018 XRectangle cr, result;
6019 struct glyph *cursor_glyph;
6020
6021 cursor_glyph = get_phys_cursor_glyph (w);
6022 if (cursor_glyph)
6023 {
6024 cr.x = w->phys_cursor.x;
6025 cr.y = w->phys_cursor.y;
6026 cr.width = cursor_glyph->pixel_width;
6027 cr.height = w->phys_cursor_height;
6028 return x_intersect_rectangles (&cr, r, &result);
6029 }
6030 else
6031 return 0;
6032 }
6033
6034
6035 /* Redraw the part of window W intersection rectangle FR. Pixel
6036 coordinates in FR are frame-relative. Call this function with
6037 input blocked. Value is non-zero if the exposure overwrites
6038 mouse-face. */
6039
6040 static int
6041 expose_window (w, fr)
6042 struct window *w;
6043 XRectangle *fr;
6044 {
6045 struct frame *f = XFRAME (w->frame);
6046 XRectangle wr, r;
6047 int mouse_face_overwritten_p = 0;
6048
6049 /* If window is not yet fully initialized, do nothing. This can
6050 happen when toolkit scroll bars are used and a window is split.
6051 Reconfiguring the scroll bar will generate an expose for a newly
6052 created window. */
6053 if (w->current_matrix == NULL)
6054 return 0;
6055
6056 /* When we're currently updating the window, display and current
6057 matrix usually don't agree. Arrange for a thorough display
6058 later. */
6059 if (w == updated_window)
6060 {
6061 SET_FRAME_GARBAGED (f);
6062 return 0;
6063 }
6064
6065 /* Frame-relative pixel rectangle of W. */
6066 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
6067 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
6068 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
6069 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
6070
6071 if (x_intersect_rectangles (fr, &wr, &r))
6072 {
6073 int yb = window_text_bottom_y (w);
6074 struct glyph_row *row;
6075 int cursor_cleared_p;
6076
6077 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
6078 r.x, r.y, r.width, r.height));
6079
6080 /* Convert to window coordinates. */
6081 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
6082 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
6083
6084 /* Turn off the cursor. */
6085 if (!w->pseudo_window_p
6086 && x_phys_cursor_in_rect_p (w, &r))
6087 {
6088 x_clear_cursor (w);
6089 cursor_cleared_p = 1;
6090 }
6091 else
6092 cursor_cleared_p = 0;
6093
6094 /* Find the first row intersecting the rectangle R. */
6095 for (row = w->current_matrix->rows;
6096 row->enabled_p;
6097 ++row)
6098 {
6099 int y0 = row->y;
6100 int y1 = MATRIX_ROW_BOTTOM_Y (row);
6101
6102 if ((y0 >= r.y && y0 < r.y + r.height)
6103 || (y1 > r.y && y1 < r.y + r.height)
6104 || (r.y >= y0 && r.y < y1)
6105 || (r.y + r.height > y0 && r.y + r.height < y1))
6106 {
6107 if (expose_line (w, row, &r))
6108 mouse_face_overwritten_p = 1;
6109 }
6110
6111 if (y1 >= yb)
6112 break;
6113 }
6114
6115 /* Display the mode line if there is one. */
6116 if (WINDOW_WANTS_MODELINE_P (w)
6117 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
6118 row->enabled_p)
6119 && row->y < r.y + r.height)
6120 {
6121 if (expose_line (w, row, &r))
6122 mouse_face_overwritten_p = 1;
6123 }
6124
6125 if (!w->pseudo_window_p)
6126 {
6127 /* Draw border between windows. */
6128 x_draw_vertical_border (w);
6129
6130 /* Turn the cursor on again. */
6131 if (cursor_cleared_p)
6132 x_update_window_cursor (w, 1);
6133 }
6134 }
6135
6136 return mouse_face_overwritten_p;
6137 }
6138
6139
6140 /* Determine the intersection of two rectangles R1 and R2. Return
6141 the intersection in *RESULT. Value is non-zero if RESULT is not
6142 empty. */
6143
6144 static int
6145 x_intersect_rectangles (r1, r2, result)
6146 XRectangle *r1, *r2, *result;
6147 {
6148 XRectangle *left, *right;
6149 XRectangle *upper, *lower;
6150 int intersection_p = 0;
6151
6152 /* Rearrange so that R1 is the left-most rectangle. */
6153 if (r1->x < r2->x)
6154 left = r1, right = r2;
6155 else
6156 left = r2, right = r1;
6157
6158 /* X0 of the intersection is right.x0, if this is inside R1,
6159 otherwise there is no intersection. */
6160 if (right->x <= left->x + left->width)
6161 {
6162 result->x = right->x;
6163
6164 /* The right end of the intersection is the minimum of the
6165 the right ends of left and right. */
6166 result->width = (min (left->x + left->width, right->x + right->width)
6167 - result->x);
6168
6169 /* Same game for Y. */
6170 if (r1->y < r2->y)
6171 upper = r1, lower = r2;
6172 else
6173 upper = r2, lower = r1;
6174
6175 /* The upper end of the intersection is lower.y0, if this is inside
6176 of upper. Otherwise, there is no intersection. */
6177 if (lower->y <= upper->y + upper->height)
6178 {
6179 result->y = lower->y;
6180
6181 /* The lower end of the intersection is the minimum of the lower
6182 ends of upper and lower. */
6183 result->height = (min (lower->y + lower->height,
6184 upper->y + upper->height)
6185 - result->y);
6186 intersection_p = 1;
6187 }
6188 }
6189
6190 return intersection_p;
6191 }
6192
6193
6194
6195
6196 \f
6197 static void
6198 frame_highlight (f)
6199 struct frame *f;
6200 {
6201 /* We used to only do this if Vx_no_window_manager was non-nil, but
6202 the ICCCM (section 4.1.6) says that the window's border pixmap
6203 and border pixel are window attributes which are "private to the
6204 client", so we can always change it to whatever we want. */
6205 BLOCK_INPUT;
6206 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6207 f->output_data.x->border_pixel);
6208 UNBLOCK_INPUT;
6209 x_update_cursor (f, 1);
6210 }
6211
6212 static void
6213 frame_unhighlight (f)
6214 struct frame *f;
6215 {
6216 /* We used to only do this if Vx_no_window_manager was non-nil, but
6217 the ICCCM (section 4.1.6) says that the window's border pixmap
6218 and border pixel are window attributes which are "private to the
6219 client", so we can always change it to whatever we want. */
6220 BLOCK_INPUT;
6221 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6222 f->output_data.x->border_tile);
6223 UNBLOCK_INPUT;
6224 x_update_cursor (f, 1);
6225 }
6226
6227 /* The focus has changed. Update the frames as necessary to reflect
6228 the new situation. Note that we can't change the selected frame
6229 here, because the Lisp code we are interrupting might become confused.
6230 Each event gets marked with the frame in which it occurred, so the
6231 Lisp code can tell when the switch took place by examining the events. */
6232
6233 static void
6234 x_new_focus_frame (dpyinfo, frame)
6235 struct x_display_info *dpyinfo;
6236 struct frame *frame;
6237 {
6238 struct frame *old_focus = dpyinfo->x_focus_frame;
6239
6240 if (frame != dpyinfo->x_focus_frame)
6241 {
6242 /* Set this before calling other routines, so that they see
6243 the correct value of x_focus_frame. */
6244 dpyinfo->x_focus_frame = frame;
6245
6246 if (old_focus && old_focus->auto_lower)
6247 x_lower_frame (old_focus);
6248
6249 #if 0
6250 selected_frame = frame;
6251 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6252 selected_frame);
6253 Fselect_window (selected_frame->selected_window);
6254 choose_minibuf_frame ();
6255 #endif /* ! 0 */
6256
6257 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6258 pending_autoraise_frame = dpyinfo->x_focus_frame;
6259 else
6260 pending_autoraise_frame = 0;
6261 }
6262
6263 x_frame_rehighlight (dpyinfo);
6264 }
6265
6266 /* Handle an event saying the mouse has moved out of an Emacs frame. */
6267
6268 void
6269 x_mouse_leave (dpyinfo)
6270 struct x_display_info *dpyinfo;
6271 {
6272 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
6273 }
6274
6275 /* The focus has changed, or we have redirected a frame's focus to
6276 another frame (this happens when a frame uses a surrogate
6277 mini-buffer frame). Shift the highlight as appropriate.
6278
6279 The FRAME argument doesn't necessarily have anything to do with which
6280 frame is being highlighted or un-highlighted; we only use it to find
6281 the appropriate X display info. */
6282
6283 static void
6284 XTframe_rehighlight (frame)
6285 struct frame *frame;
6286 {
6287 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6288 }
6289
6290 static void
6291 x_frame_rehighlight (dpyinfo)
6292 struct x_display_info *dpyinfo;
6293 {
6294 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6295
6296 if (dpyinfo->x_focus_frame)
6297 {
6298 dpyinfo->x_highlight_frame
6299 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6300 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6301 : dpyinfo->x_focus_frame);
6302 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
6303 {
6304 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6305 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
6306 }
6307 }
6308 else
6309 dpyinfo->x_highlight_frame = 0;
6310
6311 if (dpyinfo->x_highlight_frame != old_highlight)
6312 {
6313 if (old_highlight)
6314 frame_unhighlight (old_highlight);
6315 if (dpyinfo->x_highlight_frame)
6316 frame_highlight (dpyinfo->x_highlight_frame);
6317 }
6318 }
6319
6320
6321 \f
6322 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
6323
6324 /* Initialize mode_switch_bit and modifier_meaning. */
6325 static void
6326 x_find_modifier_meanings (dpyinfo)
6327 struct x_display_info *dpyinfo;
6328 {
6329 int min_code, max_code;
6330 KeySym *syms;
6331 int syms_per_code;
6332 XModifierKeymap *mods;
6333
6334 dpyinfo->meta_mod_mask = 0;
6335 dpyinfo->shift_lock_mask = 0;
6336 dpyinfo->alt_mod_mask = 0;
6337 dpyinfo->super_mod_mask = 0;
6338 dpyinfo->hyper_mod_mask = 0;
6339
6340 #ifdef HAVE_X11R4
6341 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
6342 #else
6343 min_code = dpyinfo->display->min_keycode;
6344 max_code = dpyinfo->display->max_keycode;
6345 #endif
6346
6347 syms = XGetKeyboardMapping (dpyinfo->display,
6348 min_code, max_code - min_code + 1,
6349 &syms_per_code);
6350 mods = XGetModifierMapping (dpyinfo->display);
6351
6352 /* Scan the modifier table to see which modifier bits the Meta and
6353 Alt keysyms are on. */
6354 {
6355 int row, col; /* The row and column in the modifier table. */
6356
6357 for (row = 3; row < 8; row++)
6358 for (col = 0; col < mods->max_keypermod; col++)
6359 {
6360 KeyCode code
6361 = mods->modifiermap[(row * mods->max_keypermod) + col];
6362
6363 /* Zeroes are used for filler. Skip them. */
6364 if (code == 0)
6365 continue;
6366
6367 /* Are any of this keycode's keysyms a meta key? */
6368 {
6369 int code_col;
6370
6371 for (code_col = 0; code_col < syms_per_code; code_col++)
6372 {
6373 int sym = syms[((code - min_code) * syms_per_code) + code_col];
6374
6375 switch (sym)
6376 {
6377 case XK_Meta_L:
6378 case XK_Meta_R:
6379 dpyinfo->meta_mod_mask |= (1 << row);
6380 break;
6381
6382 case XK_Alt_L:
6383 case XK_Alt_R:
6384 dpyinfo->alt_mod_mask |= (1 << row);
6385 break;
6386
6387 case XK_Hyper_L:
6388 case XK_Hyper_R:
6389 dpyinfo->hyper_mod_mask |= (1 << row);
6390 break;
6391
6392 case XK_Super_L:
6393 case XK_Super_R:
6394 dpyinfo->super_mod_mask |= (1 << row);
6395 break;
6396
6397 case XK_Shift_Lock:
6398 /* Ignore this if it's not on the lock modifier. */
6399 if ((1 << row) == LockMask)
6400 dpyinfo->shift_lock_mask = LockMask;
6401 break;
6402 }
6403 }
6404 }
6405 }
6406 }
6407
6408 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
6409 if (! dpyinfo->meta_mod_mask)
6410 {
6411 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6412 dpyinfo->alt_mod_mask = 0;
6413 }
6414
6415 /* If some keys are both alt and meta,
6416 make them just meta, not alt. */
6417 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6418 {
6419 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6420 }
6421
6422 XFree ((char *) syms);
6423 XFreeModifiermap (mods);
6424 }
6425
6426 /* Convert between the modifier bits X uses and the modifier bits
6427 Emacs uses. */
6428
6429 static unsigned int
6430 x_x_to_emacs_modifiers (dpyinfo, state)
6431 struct x_display_info *dpyinfo;
6432 unsigned int state;
6433 {
6434 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6435 | ((state & ControlMask) ? ctrl_modifier : 0)
6436 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6437 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6438 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6439 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
6440 }
6441
6442 static unsigned int
6443 x_emacs_to_x_modifiers (dpyinfo, state)
6444 struct x_display_info *dpyinfo;
6445 unsigned int state;
6446 {
6447 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6448 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6449 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6450 | ((state & shift_modifier) ? ShiftMask : 0)
6451 | ((state & ctrl_modifier) ? ControlMask : 0)
6452 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
6453 }
6454
6455 /* Convert a keysym to its name. */
6456
6457 char *
6458 x_get_keysym_name (keysym)
6459 KeySym keysym;
6460 {
6461 char *value;
6462
6463 BLOCK_INPUT;
6464 value = XKeysymToString (keysym);
6465 UNBLOCK_INPUT;
6466
6467 return value;
6468 }
6469
6470
6471 \f
6472 /* Mouse clicks and mouse movement. Rah. */
6473
6474 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6475 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6476 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6477 not force the value into range. */
6478
6479 void
6480 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6481 FRAME_PTR f;
6482 register int pix_x, pix_y;
6483 register int *x, *y;
6484 XRectangle *bounds;
6485 int noclip;
6486 {
6487 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6488 even for negative values. */
6489 if (pix_x < 0)
6490 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6491 if (pix_y < 0)
6492 pix_y -= (f)->output_data.x->line_height - 1;
6493
6494 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6495 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6496
6497 if (bounds)
6498 {
6499 bounds->width = FONT_WIDTH (f->output_data.x->font);
6500 bounds->height = f->output_data.x->line_height;
6501 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6502 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6503 }
6504
6505 if (!noclip)
6506 {
6507 if (pix_x < 0)
6508 pix_x = 0;
6509 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6510 pix_x = FRAME_WINDOW_WIDTH (f);
6511
6512 if (pix_y < 0)
6513 pix_y = 0;
6514 else if (pix_y > f->height)
6515 pix_y = f->height;
6516 }
6517
6518 *x = pix_x;
6519 *y = pix_y;
6520 }
6521
6522
6523 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6524 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6525 can't tell the positions because W's display is not up to date,
6526 return 0. */
6527
6528 int
6529 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6530 struct window *w;
6531 int hpos, vpos;
6532 int *frame_x, *frame_y;
6533 {
6534 int success_p;
6535
6536 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6537 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6538
6539 if (display_completed)
6540 {
6541 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6542 struct glyph *glyph = row->glyphs[TEXT_AREA];
6543 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6544
6545 *frame_y = row->y;
6546 *frame_x = row->x;
6547 while (glyph < end)
6548 {
6549 *frame_x += glyph->pixel_width;
6550 ++glyph;
6551 }
6552
6553 success_p = 1;
6554 }
6555 else
6556 {
6557 *frame_y = *frame_x = 0;
6558 success_p = 0;
6559 }
6560
6561 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6562 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6563 return success_p;
6564 }
6565
6566
6567 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6568
6569 If the event is a button press, then note that we have grabbed
6570 the mouse. */
6571
6572 static Lisp_Object
6573 construct_mouse_click (result, event, f)
6574 struct input_event *result;
6575 XButtonEvent *event;
6576 struct frame *f;
6577 {
6578 /* Make the event type no_event; we'll change that when we decide
6579 otherwise. */
6580 result->kind = mouse_click;
6581 result->code = event->button - Button1;
6582 result->timestamp = event->time;
6583 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6584 event->state)
6585 | (event->type == ButtonRelease
6586 ? up_modifier
6587 : down_modifier));
6588
6589 XSETINT (result->x, event->x);
6590 XSETINT (result->y, event->y);
6591 XSETFRAME (result->frame_or_window, f);
6592 result->arg = Qnil;
6593 return Qnil;
6594 }
6595
6596 \f
6597 /* Function to report a mouse movement to the mainstream Emacs code.
6598 The input handler calls this.
6599
6600 We have received a mouse movement event, which is given in *event.
6601 If the mouse is over a different glyph than it was last time, tell
6602 the mainstream emacs code by setting mouse_moved. If not, ask for
6603 another motion event, so we can check again the next time it moves. */
6604
6605 static XMotionEvent last_mouse_motion_event;
6606 static Lisp_Object last_mouse_motion_frame;
6607
6608 static void
6609 note_mouse_movement (frame, event)
6610 FRAME_PTR frame;
6611 XMotionEvent *event;
6612 {
6613 last_mouse_movement_time = event->time;
6614 last_mouse_motion_event = *event;
6615 XSETFRAME (last_mouse_motion_frame, frame);
6616
6617 if (event->window != FRAME_X_WINDOW (frame))
6618 {
6619 frame->mouse_moved = 1;
6620 last_mouse_scroll_bar = Qnil;
6621 note_mouse_highlight (frame, -1, -1);
6622 }
6623
6624 /* Has the mouse moved off the glyph it was on at the last sighting? */
6625 else if (event->x < last_mouse_glyph.x
6626 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6627 || event->y < last_mouse_glyph.y
6628 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6629 {
6630 frame->mouse_moved = 1;
6631 last_mouse_scroll_bar = Qnil;
6632 note_mouse_highlight (frame, event->x, event->y);
6633 }
6634 }
6635
6636 /* This is used for debugging, to turn off note_mouse_highlight. */
6637
6638 int disable_mouse_highlight;
6639
6640
6641 \f
6642 /************************************************************************
6643 Mouse Face
6644 ************************************************************************/
6645
6646 /* Find the glyph under window-relative coordinates X/Y in window W.
6647 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6648 strings. Return in *HPOS and *VPOS the row and column number of
6649 the glyph found. Return in *AREA the glyph area containing X.
6650 Value is a pointer to the glyph found or null if X/Y is not on
6651 text, or we can't tell because W's current matrix is not up to
6652 date. */
6653
6654 static struct glyph *
6655 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
6656 struct window *w;
6657 int x, y;
6658 int *hpos, *vpos, *area;
6659 int buffer_only_p;
6660 {
6661 struct glyph *glyph, *end;
6662 struct glyph_row *row = NULL;
6663 int x0, i, left_area_width;
6664
6665 /* Find row containing Y. Give up if some row is not enabled. */
6666 for (i = 0; i < w->current_matrix->nrows; ++i)
6667 {
6668 row = MATRIX_ROW (w->current_matrix, i);
6669 if (!row->enabled_p)
6670 return NULL;
6671 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6672 break;
6673 }
6674
6675 *vpos = i;
6676 *hpos = 0;
6677
6678 /* Give up if Y is not in the window. */
6679 if (i == w->current_matrix->nrows)
6680 return NULL;
6681
6682 /* Get the glyph area containing X. */
6683 if (w->pseudo_window_p)
6684 {
6685 *area = TEXT_AREA;
6686 x0 = 0;
6687 }
6688 else
6689 {
6690 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6691 if (x < left_area_width)
6692 {
6693 *area = LEFT_MARGIN_AREA;
6694 x0 = 0;
6695 }
6696 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6697 {
6698 *area = TEXT_AREA;
6699 x0 = row->x + left_area_width;
6700 }
6701 else
6702 {
6703 *area = RIGHT_MARGIN_AREA;
6704 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6705 }
6706 }
6707
6708 /* Find glyph containing X. */
6709 glyph = row->glyphs[*area];
6710 end = glyph + row->used[*area];
6711 while (glyph < end)
6712 {
6713 if (x < x0 + glyph->pixel_width)
6714 {
6715 if (w->pseudo_window_p)
6716 break;
6717 else if (!buffer_only_p || BUFFERP (glyph->object))
6718 break;
6719 }
6720
6721 x0 += glyph->pixel_width;
6722 ++glyph;
6723 }
6724
6725 if (glyph == end)
6726 return NULL;
6727
6728 *hpos = glyph - row->glyphs[*area];
6729 return glyph;
6730 }
6731
6732
6733 /* Convert frame-relative x/y to coordinates relative to window W.
6734 Takes pseudo-windows into account. */
6735
6736 static void
6737 frame_to_window_pixel_xy (w, x, y)
6738 struct window *w;
6739 int *x, *y;
6740 {
6741 if (w->pseudo_window_p)
6742 {
6743 /* A pseudo-window is always full-width, and starts at the
6744 left edge of the frame, plus a frame border. */
6745 struct frame *f = XFRAME (w->frame);
6746 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6747 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6748 }
6749 else
6750 {
6751 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6752 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6753 }
6754 }
6755
6756
6757 /* Take proper action when mouse has moved to the mode or header line of
6758 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6759 mode line. X is relative to the start of the text display area of
6760 W, so the width of bitmap areas and scroll bars must be subtracted
6761 to get a position relative to the start of the mode line. */
6762
6763 static void
6764 note_mode_line_highlight (w, x, mode_line_p)
6765 struct window *w;
6766 int x, mode_line_p;
6767 {
6768 struct frame *f = XFRAME (w->frame);
6769 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6770 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6771 struct glyph_row *row;
6772
6773 if (mode_line_p)
6774 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6775 else
6776 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6777
6778 if (row->enabled_p)
6779 {
6780 struct glyph *glyph, *end;
6781 Lisp_Object help, map;
6782 int x0;
6783
6784 /* Find the glyph under X. */
6785 glyph = row->glyphs[TEXT_AREA];
6786 end = glyph + row->used[TEXT_AREA];
6787 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6788 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
6789
6790 while (glyph < end
6791 && x >= x0 + glyph->pixel_width)
6792 {
6793 x0 += glyph->pixel_width;
6794 ++glyph;
6795 }
6796
6797 if (glyph < end
6798 && STRINGP (glyph->object)
6799 && XSTRING (glyph->object)->intervals
6800 && glyph->charpos >= 0
6801 && glyph->charpos < XSTRING (glyph->object)->size)
6802 {
6803 /* If we're on a string with `help-echo' text property,
6804 arrange for the help to be displayed. This is done by
6805 setting the global variable help_echo to the help string. */
6806 help = Fget_text_property (make_number (glyph->charpos),
6807 Qhelp_echo, glyph->object);
6808 if (!NILP (help))
6809 {
6810 help_echo = help;
6811 XSETWINDOW (help_echo_window, w);
6812 help_echo_object = glyph->object;
6813 help_echo_pos = glyph->charpos;
6814 }
6815
6816 /* Change the mouse pointer according to what is under X/Y. */
6817 map = Fget_text_property (make_number (glyph->charpos),
6818 Qlocal_map, glyph->object);
6819 if (KEYMAPP (map))
6820 cursor = f->output_data.x->nontext_cursor;
6821 else
6822 {
6823 map = Fget_text_property (make_number (glyph->charpos),
6824 Qkeymap, glyph->object);
6825 if (KEYMAPP (map))
6826 cursor = f->output_data.x->nontext_cursor;
6827 }
6828 }
6829 }
6830
6831 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6832 }
6833
6834
6835 /* Take proper action when the mouse has moved to position X, Y on
6836 frame F as regards highlighting characters that have mouse-face
6837 properties. Also de-highlighting chars where the mouse was before.
6838 X and Y can be negative or out of range. */
6839
6840 static void
6841 note_mouse_highlight (f, x, y)
6842 struct frame *f;
6843 int x, y;
6844 {
6845 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6846 int portion;
6847 Lisp_Object window;
6848 struct window *w;
6849 Cursor cursor = None;
6850 struct buffer *b;
6851
6852 /* When a menu is active, don't highlight because this looks odd. */
6853 #ifdef USE_X_TOOLKIT
6854 if (popup_activated ())
6855 return;
6856 #endif
6857
6858 if (disable_mouse_highlight
6859 || !f->glyphs_initialized_p)
6860 return;
6861
6862 dpyinfo->mouse_face_mouse_x = x;
6863 dpyinfo->mouse_face_mouse_y = y;
6864 dpyinfo->mouse_face_mouse_frame = f;
6865
6866 if (dpyinfo->mouse_face_defer)
6867 return;
6868
6869 if (gc_in_progress)
6870 {
6871 dpyinfo->mouse_face_deferred_gc = 1;
6872 return;
6873 }
6874
6875 /* Which window is that in? */
6876 window = window_from_coordinates (f, x, y, &portion, 1);
6877
6878 /* If we were displaying active text in another window, clear that. */
6879 if (! EQ (window, dpyinfo->mouse_face_window))
6880 clear_mouse_face (dpyinfo);
6881
6882 /* Not on a window -> return. */
6883 if (!WINDOWP (window))
6884 return;
6885
6886 /* Convert to window-relative pixel coordinates. */
6887 w = XWINDOW (window);
6888 frame_to_window_pixel_xy (w, &x, &y);
6889
6890 /* Handle tool-bar window differently since it doesn't display a
6891 buffer. */
6892 if (EQ (window, f->tool_bar_window))
6893 {
6894 note_tool_bar_highlight (f, x, y);
6895 return;
6896 }
6897
6898 /* Mouse is on the mode or header line? */
6899 if (portion == 1 || portion == 3)
6900 {
6901 note_mode_line_highlight (w, x, portion == 1);
6902 return;
6903 }
6904
6905 if (portion == 2)
6906 cursor = f->output_data.x->horizontal_drag_cursor;
6907 else
6908 cursor = f->output_data.x->text_cursor;
6909
6910 /* Are we in a window whose display is up to date?
6911 And verify the buffer's text has not changed. */
6912 b = XBUFFER (w->buffer);
6913 if (/* Within text portion of the window. */
6914 portion == 0
6915 && EQ (w->window_end_valid, w->buffer)
6916 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
6917 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
6918 {
6919 int hpos, vpos, pos, i, area;
6920 struct glyph *glyph;
6921 Lisp_Object object;
6922 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
6923 Lisp_Object *overlay_vec = NULL;
6924 int len, noverlays;
6925 struct buffer *obuf;
6926 int obegv, ozv, same_region;
6927
6928 /* Find the glyph under X/Y. */
6929 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
6930
6931 /* Clear mouse face if X/Y not over text. */
6932 if (glyph == NULL
6933 || area != TEXT_AREA
6934 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6935 {
6936 if (clear_mouse_face (dpyinfo))
6937 cursor = None;
6938 goto set_cursor;
6939 }
6940
6941 pos = glyph->charpos;
6942 object = glyph->object;
6943 if (!STRINGP (object) && !BUFFERP (object))
6944 goto set_cursor;
6945
6946 /* If we get an out-of-range value, return now; avoid an error. */
6947 if (BUFFERP (object) && pos > BUF_Z (b))
6948 goto set_cursor;
6949
6950 /* Make the window's buffer temporarily current for
6951 overlays_at and compute_char_face. */
6952 obuf = current_buffer;
6953 current_buffer = b;
6954 obegv = BEGV;
6955 ozv = ZV;
6956 BEGV = BEG;
6957 ZV = Z;
6958
6959 /* Is this char mouse-active or does it have help-echo? */
6960 position = make_number (pos);
6961
6962 if (BUFFERP (object))
6963 {
6964 /* Put all the overlays we want in a vector in overlay_vec.
6965 Store the length in len. If there are more than 10, make
6966 enough space for all, and try again. */
6967 len = 10;
6968 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6969 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6970 if (noverlays > len)
6971 {
6972 len = noverlays;
6973 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6974 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6975 }
6976
6977 /* Sort overlays into increasing priority order. */
6978 noverlays = sort_overlays (overlay_vec, noverlays, w);
6979 }
6980 else
6981 noverlays = 0;
6982
6983 same_region = (EQ (window, dpyinfo->mouse_face_window)
6984 && vpos >= dpyinfo->mouse_face_beg_row
6985 && vpos <= dpyinfo->mouse_face_end_row
6986 && (vpos > dpyinfo->mouse_face_beg_row
6987 || hpos >= dpyinfo->mouse_face_beg_col)
6988 && (vpos < dpyinfo->mouse_face_end_row
6989 || hpos < dpyinfo->mouse_face_end_col
6990 || dpyinfo->mouse_face_past_end));
6991
6992 if (same_region)
6993 cursor = None;
6994
6995 /* Check mouse-face highlighting. */
6996 if (! same_region
6997 /* If there exists an overlay with mouse-face overlapping
6998 the one we are currently highlighting, we have to
6999 check if we enter the overlapping overlay, and then
7000 highlight only that. */
7001 || (OVERLAYP (dpyinfo->mouse_face_overlay)
7002 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
7003 {
7004 /* Find the highest priority overlay that has a mouse-face
7005 property. */
7006 overlay = Qnil;
7007 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
7008 {
7009 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
7010 if (!NILP (mouse_face))
7011 overlay = overlay_vec[i];
7012 }
7013
7014 /* If we're actually highlighting the same overlay as
7015 before, there's no need to do that again. */
7016 if (!NILP (overlay)
7017 && EQ (overlay, dpyinfo->mouse_face_overlay))
7018 goto check_help_echo;
7019
7020 dpyinfo->mouse_face_overlay = overlay;
7021
7022 /* Clear the display of the old active region, if any. */
7023 if (clear_mouse_face (dpyinfo))
7024 cursor = None;
7025
7026 /* If no overlay applies, get a text property. */
7027 if (NILP (overlay))
7028 mouse_face = Fget_text_property (position, Qmouse_face, object);
7029
7030 /* Handle the overlay case. */
7031 if (!NILP (overlay))
7032 {
7033 /* Find the range of text around this char that
7034 should be active. */
7035 Lisp_Object before, after;
7036 int ignore;
7037
7038 before = Foverlay_start (overlay);
7039 after = Foverlay_end (overlay);
7040 /* Record this as the current active region. */
7041 fast_find_position (w, XFASTINT (before),
7042 &dpyinfo->mouse_face_beg_col,
7043 &dpyinfo->mouse_face_beg_row,
7044 &dpyinfo->mouse_face_beg_x,
7045 &dpyinfo->mouse_face_beg_y, Qnil);
7046
7047 dpyinfo->mouse_face_past_end
7048 = !fast_find_position (w, XFASTINT (after),
7049 &dpyinfo->mouse_face_end_col,
7050 &dpyinfo->mouse_face_end_row,
7051 &dpyinfo->mouse_face_end_x,
7052 &dpyinfo->mouse_face_end_y, Qnil);
7053 dpyinfo->mouse_face_window = window;
7054 dpyinfo->mouse_face_face_id
7055 = face_at_buffer_position (w, pos, 0, 0,
7056 &ignore, pos + 1, 1);
7057
7058 /* Display it as active. */
7059 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7060 cursor = None;
7061 }
7062 /* Handle the text property case. */
7063 else if (!NILP (mouse_face) && BUFFERP (object))
7064 {
7065 /* Find the range of text around this char that
7066 should be active. */
7067 Lisp_Object before, after, beginning, end;
7068 int ignore;
7069
7070 beginning = Fmarker_position (w->start);
7071 end = make_number (BUF_Z (XBUFFER (object))
7072 - XFASTINT (w->window_end_pos));
7073 before
7074 = Fprevious_single_property_change (make_number (pos + 1),
7075 Qmouse_face,
7076 object, beginning);
7077 after
7078 = Fnext_single_property_change (position, Qmouse_face,
7079 object, end);
7080
7081 /* Record this as the current active region. */
7082 fast_find_position (w, XFASTINT (before),
7083 &dpyinfo->mouse_face_beg_col,
7084 &dpyinfo->mouse_face_beg_row,
7085 &dpyinfo->mouse_face_beg_x,
7086 &dpyinfo->mouse_face_beg_y, Qnil);
7087 dpyinfo->mouse_face_past_end
7088 = !fast_find_position (w, XFASTINT (after),
7089 &dpyinfo->mouse_face_end_col,
7090 &dpyinfo->mouse_face_end_row,
7091 &dpyinfo->mouse_face_end_x,
7092 &dpyinfo->mouse_face_end_y, Qnil);
7093 dpyinfo->mouse_face_window = window;
7094
7095 if (BUFFERP (object))
7096 dpyinfo->mouse_face_face_id
7097 = face_at_buffer_position (w, pos, 0, 0,
7098 &ignore, pos + 1, 1);
7099
7100 /* Display it as active. */
7101 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7102 cursor = None;
7103 }
7104 else if (!NILP (mouse_face) && STRINGP (object))
7105 {
7106 Lisp_Object b, e;
7107 int ignore;
7108
7109 b = Fprevious_single_property_change (make_number (pos + 1),
7110 Qmouse_face,
7111 object, Qnil);
7112 e = Fnext_single_property_change (position, Qmouse_face,
7113 object, Qnil);
7114 if (NILP (b))
7115 b = make_number (0);
7116 if (NILP (e))
7117 e = make_number (XSTRING (object)->size - 1);
7118 fast_find_string_pos (w, XINT (b), object,
7119 &dpyinfo->mouse_face_beg_col,
7120 &dpyinfo->mouse_face_beg_row,
7121 &dpyinfo->mouse_face_beg_x,
7122 &dpyinfo->mouse_face_beg_y, 0);
7123 fast_find_string_pos (w, XINT (e), object,
7124 &dpyinfo->mouse_face_end_col,
7125 &dpyinfo->mouse_face_end_row,
7126 &dpyinfo->mouse_face_end_x,
7127 &dpyinfo->mouse_face_end_y, 1);
7128 dpyinfo->mouse_face_past_end = 0;
7129 dpyinfo->mouse_face_window = window;
7130 dpyinfo->mouse_face_face_id
7131 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
7132 glyph->face_id, 1);
7133 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7134 cursor = None;
7135 }
7136 else if (STRINGP (object) && NILP (mouse_face))
7137 {
7138 /* A string which doesn't have mouse-face, but
7139 the text ``under'' it might have. */
7140 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
7141 int start = MATRIX_ROW_START_CHARPOS (r);
7142
7143 pos = string_buffer_position (w, object, start);
7144 if (pos > 0)
7145 mouse_face = get_char_property_and_overlay (make_number (pos),
7146 Qmouse_face,
7147 w->buffer,
7148 &overlay);
7149 if (!NILP (mouse_face) && !NILP (overlay))
7150 {
7151 Lisp_Object before = Foverlay_start (overlay);
7152 Lisp_Object after = Foverlay_end (overlay);
7153 Lisp_Object ignore;
7154
7155 /* Note that we might not be able to find position
7156 BEFORE in the glyph matrix if the overlay is
7157 entirely covered by a `display' property. In
7158 this case, we overshoot. So let's stop in
7159 the glyph matrix before glyphs for OBJECT. */
7160 fast_find_position (w, XFASTINT (before),
7161 &dpyinfo->mouse_face_beg_col,
7162 &dpyinfo->mouse_face_beg_row,
7163 &dpyinfo->mouse_face_beg_x,
7164 &dpyinfo->mouse_face_beg_y,
7165 object);
7166
7167 dpyinfo->mouse_face_past_end
7168 = !fast_find_position (w, XFASTINT (after),
7169 &dpyinfo->mouse_face_end_col,
7170 &dpyinfo->mouse_face_end_row,
7171 &dpyinfo->mouse_face_end_x,
7172 &dpyinfo->mouse_face_end_y,
7173 Qnil);
7174 dpyinfo->mouse_face_window = window;
7175 dpyinfo->mouse_face_face_id
7176 = face_at_buffer_position (w, pos, 0, 0,
7177 &ignore, pos + 1, 1);
7178
7179 /* Display it as active. */
7180 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7181 cursor = None;
7182 }
7183 }
7184 }
7185
7186 check_help_echo:
7187
7188 /* Look for a `help-echo' property. */
7189 {
7190 Lisp_Object help, overlay;
7191
7192 /* Check overlays first. */
7193 help = overlay = Qnil;
7194 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
7195 {
7196 overlay = overlay_vec[i];
7197 help = Foverlay_get (overlay, Qhelp_echo);
7198 }
7199
7200 if (!NILP (help))
7201 {
7202 help_echo = help;
7203 help_echo_window = window;
7204 help_echo_object = overlay;
7205 help_echo_pos = pos;
7206 }
7207 else
7208 {
7209 Lisp_Object object = glyph->object;
7210 int charpos = glyph->charpos;
7211
7212 /* Try text properties. */
7213 if (STRINGP (object)
7214 && charpos >= 0
7215 && charpos < XSTRING (object)->size)
7216 {
7217 help = Fget_text_property (make_number (charpos),
7218 Qhelp_echo, object);
7219 if (NILP (help))
7220 {
7221 /* If the string itself doesn't specify a help-echo,
7222 see if the buffer text ``under'' it does. */
7223 struct glyph_row *r
7224 = MATRIX_ROW (w->current_matrix, vpos);
7225 int start = MATRIX_ROW_START_CHARPOS (r);
7226 int pos = string_buffer_position (w, object, start);
7227 if (pos > 0)
7228 {
7229 help = Fget_char_property (make_number (pos),
7230 Qhelp_echo, w->buffer);
7231 if (!NILP (help))
7232 {
7233 charpos = pos;
7234 object = w->buffer;
7235 }
7236 }
7237 }
7238 }
7239 else if (BUFFERP (object)
7240 && charpos >= BEGV
7241 && charpos < ZV)
7242 help = Fget_text_property (make_number (charpos), Qhelp_echo,
7243 object);
7244
7245 if (!NILP (help))
7246 {
7247 help_echo = help;
7248 help_echo_window = window;
7249 help_echo_object = object;
7250 help_echo_pos = charpos;
7251 }
7252 }
7253 }
7254
7255 BEGV = obegv;
7256 ZV = ozv;
7257 current_buffer = obuf;
7258 }
7259
7260 set_cursor:
7261
7262 if (cursor != None)
7263 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7264 }
7265
7266 static void
7267 redo_mouse_highlight ()
7268 {
7269 if (!NILP (last_mouse_motion_frame)
7270 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
7271 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
7272 last_mouse_motion_event.x,
7273 last_mouse_motion_event.y);
7274 }
7275
7276
7277 \f
7278 /***********************************************************************
7279 Tool-bars
7280 ***********************************************************************/
7281
7282 static int x_tool_bar_item P_ ((struct frame *, int, int,
7283 struct glyph **, int *, int *, int *));
7284
7285 /* Tool-bar item index of the item on which a mouse button was pressed
7286 or -1. */
7287
7288 static int last_tool_bar_item;
7289
7290
7291 /* Get information about the tool-bar item at position X/Y on frame F.
7292 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7293 the current matrix of the tool-bar window of F, or NULL if not
7294 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
7295 item in F->tool_bar_items. Value is
7296
7297 -1 if X/Y is not on a tool-bar item
7298 0 if X/Y is on the same item that was highlighted before.
7299 1 otherwise. */
7300
7301 static int
7302 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
7303 struct frame *f;
7304 int x, y;
7305 struct glyph **glyph;
7306 int *hpos, *vpos, *prop_idx;
7307 {
7308 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7309 struct window *w = XWINDOW (f->tool_bar_window);
7310 int area;
7311
7312 /* Find the glyph under X/Y. */
7313 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
7314 if (*glyph == NULL)
7315 return -1;
7316
7317 /* Get the start of this tool-bar item's properties in
7318 f->tool_bar_items. */
7319 if (!tool_bar_item_info (f, *glyph, prop_idx))
7320 return -1;
7321
7322 /* Is mouse on the highlighted item? */
7323 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
7324 && *vpos >= dpyinfo->mouse_face_beg_row
7325 && *vpos <= dpyinfo->mouse_face_end_row
7326 && (*vpos > dpyinfo->mouse_face_beg_row
7327 || *hpos >= dpyinfo->mouse_face_beg_col)
7328 && (*vpos < dpyinfo->mouse_face_end_row
7329 || *hpos < dpyinfo->mouse_face_end_col
7330 || dpyinfo->mouse_face_past_end))
7331 return 0;
7332
7333 return 1;
7334 }
7335
7336
7337 /* Handle mouse button event on the tool-bar of frame F, at
7338 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
7339 or ButtonRelase. */
7340
7341 static void
7342 x_handle_tool_bar_click (f, button_event)
7343 struct frame *f;
7344 XButtonEvent *button_event;
7345 {
7346 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7347 struct window *w = XWINDOW (f->tool_bar_window);
7348 int hpos, vpos, prop_idx;
7349 struct glyph *glyph;
7350 Lisp_Object enabled_p;
7351 int x = button_event->x;
7352 int y = button_event->y;
7353
7354 /* If not on the highlighted tool-bar item, return. */
7355 frame_to_window_pixel_xy (w, &x, &y);
7356 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
7357 return;
7358
7359 /* If item is disabled, do nothing. */
7360 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7361 if (NILP (enabled_p))
7362 return;
7363
7364 if (button_event->type == ButtonPress)
7365 {
7366 /* Show item in pressed state. */
7367 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7368 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
7369 last_tool_bar_item = prop_idx;
7370 }
7371 else
7372 {
7373 Lisp_Object key, frame;
7374 struct input_event event;
7375
7376 /* Show item in released state. */
7377 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7378 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7379
7380 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
7381
7382 XSETFRAME (frame, f);
7383 event.kind = TOOL_BAR_EVENT;
7384 event.frame_or_window = frame;
7385 event.arg = frame;
7386 kbd_buffer_store_event (&event);
7387
7388 event.kind = TOOL_BAR_EVENT;
7389 event.frame_or_window = frame;
7390 event.arg = key;
7391 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7392 button_event->state);
7393 kbd_buffer_store_event (&event);
7394 last_tool_bar_item = -1;
7395 }
7396 }
7397
7398
7399 /* Possibly highlight a tool-bar item on frame F when mouse moves to
7400 tool-bar window-relative coordinates X/Y. Called from
7401 note_mouse_highlight. */
7402
7403 static void
7404 note_tool_bar_highlight (f, x, y)
7405 struct frame *f;
7406 int x, y;
7407 {
7408 Lisp_Object window = f->tool_bar_window;
7409 struct window *w = XWINDOW (window);
7410 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7411 int hpos, vpos;
7412 struct glyph *glyph;
7413 struct glyph_row *row;
7414 int i;
7415 Lisp_Object enabled_p;
7416 int prop_idx;
7417 enum draw_glyphs_face draw;
7418 int mouse_down_p, rc;
7419
7420 /* Function note_mouse_highlight is called with negative x(y
7421 values when mouse moves outside of the frame. */
7422 if (x <= 0 || y <= 0)
7423 {
7424 clear_mouse_face (dpyinfo);
7425 return;
7426 }
7427
7428 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7429 if (rc < 0)
7430 {
7431 /* Not on tool-bar item. */
7432 clear_mouse_face (dpyinfo);
7433 return;
7434 }
7435 else if (rc == 0)
7436 goto set_help_echo;
7437
7438 clear_mouse_face (dpyinfo);
7439
7440 /* Mouse is down, but on different tool-bar item? */
7441 mouse_down_p = (dpyinfo->grabbed
7442 && f == last_mouse_frame
7443 && FRAME_LIVE_P (f));
7444 if (mouse_down_p
7445 && last_tool_bar_item != prop_idx)
7446 return;
7447
7448 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7449 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7450
7451 /* If tool-bar item is not enabled, don't highlight it. */
7452 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7453 if (!NILP (enabled_p))
7454 {
7455 /* Compute the x-position of the glyph. In front and past the
7456 image is a space. We include this is the highlighted area. */
7457 row = MATRIX_ROW (w->current_matrix, vpos);
7458 for (i = x = 0; i < hpos; ++i)
7459 x += row->glyphs[TEXT_AREA][i].pixel_width;
7460
7461 /* Record this as the current active region. */
7462 dpyinfo->mouse_face_beg_col = hpos;
7463 dpyinfo->mouse_face_beg_row = vpos;
7464 dpyinfo->mouse_face_beg_x = x;
7465 dpyinfo->mouse_face_beg_y = row->y;
7466 dpyinfo->mouse_face_past_end = 0;
7467
7468 dpyinfo->mouse_face_end_col = hpos + 1;
7469 dpyinfo->mouse_face_end_row = vpos;
7470 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7471 dpyinfo->mouse_face_end_y = row->y;
7472 dpyinfo->mouse_face_window = window;
7473 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7474
7475 /* Display it as active. */
7476 show_mouse_face (dpyinfo, draw);
7477 dpyinfo->mouse_face_image_state = draw;
7478 }
7479
7480 set_help_echo:
7481
7482 /* Set help_echo to a help string.to display for this tool-bar item.
7483 XTread_socket does the rest. */
7484 help_echo_object = help_echo_window = Qnil;
7485 help_echo_pos = -1;
7486 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
7487 if (NILP (help_echo))
7488 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
7489 }
7490
7491
7492 \f
7493 /* Find the glyph matrix position of buffer position CHARPOS in window
7494 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7495 current glyphs must be up to date. If CHARPOS is above window
7496 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
7497 of last line in W. In the row containing CHARPOS, stop before glyphs
7498 having STOP as object. */
7499
7500 #if 0 /* This is a version of fast_find_position that's more correct
7501 in the presence of hscrolling, for example. I didn't install
7502 it right away because the problem fixed is minor, it failed
7503 in 20.x as well, and I think it's too risky to install
7504 so near the release of 21.1. 2001-09-25 gerd. */
7505
7506 static int
7507 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
7508 struct window *w;
7509 int charpos;
7510 int *hpos, *vpos, *x, *y;
7511 Lisp_Object stop;
7512 {
7513 struct glyph_row *row, *first;
7514 struct glyph *glyph, *end;
7515 int i, past_end = 0;
7516
7517 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7518 row = row_containing_pos (w, charpos, first, NULL);
7519 if (row == NULL)
7520 {
7521 if (charpos < MATRIX_ROW_START_CHARPOS (first))
7522 {
7523 *x = *y = *hpos = *vpos = 0;
7524 return 0;
7525 }
7526 else
7527 {
7528 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
7529 past_end = 1;
7530 }
7531 }
7532
7533 *x = row->x;
7534 *y = row->y;
7535 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7536
7537 glyph = row->glyphs[TEXT_AREA];
7538 end = glyph + row->used[TEXT_AREA];
7539
7540 /* Skip over glyphs not having an object at the start of the row.
7541 These are special glyphs like truncation marks on terminal
7542 frames. */
7543 if (row->displays_text_p)
7544 while (glyph < end
7545 && INTEGERP (glyph->object)
7546 && !EQ (stop, glyph->object)
7547 && glyph->charpos < 0)
7548 {
7549 *x += glyph->pixel_width;
7550 ++glyph;
7551 }
7552
7553 while (glyph < end
7554 && !INTEGERP (glyph->object)
7555 && !EQ (stop, glyph->object)
7556 && (!BUFFERP (glyph->object)
7557 || glyph->charpos < charpos))
7558 {
7559 *x += glyph->pixel_width;
7560 ++glyph;
7561 }
7562
7563 *hpos = glyph - row->glyphs[TEXT_AREA];
7564 return past_end;
7565 }
7566
7567 #else /* not 0 */
7568
7569 static int
7570 fast_find_position (w, pos, hpos, vpos, x, y, stop)
7571 struct window *w;
7572 int pos;
7573 int *hpos, *vpos, *x, *y;
7574 Lisp_Object stop;
7575 {
7576 int i;
7577 int lastcol;
7578 int maybe_next_line_p = 0;
7579 int line_start_position;
7580 int yb = window_text_bottom_y (w);
7581 struct glyph_row *row, *best_row;
7582 int row_vpos, best_row_vpos;
7583 int current_x;
7584
7585 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7586 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7587
7588 while (row->y < yb)
7589 {
7590 if (row->used[TEXT_AREA])
7591 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7592 else
7593 line_start_position = 0;
7594
7595 if (line_start_position > pos)
7596 break;
7597 /* If the position sought is the end of the buffer,
7598 don't include the blank lines at the bottom of the window. */
7599 else if (line_start_position == pos
7600 && pos == BUF_ZV (XBUFFER (w->buffer)))
7601 {
7602 maybe_next_line_p = 1;
7603 break;
7604 }
7605 else if (line_start_position > 0)
7606 {
7607 best_row = row;
7608 best_row_vpos = row_vpos;
7609 }
7610
7611 if (row->y + row->height >= yb)
7612 break;
7613
7614 ++row;
7615 ++row_vpos;
7616 }
7617
7618 /* Find the right column within BEST_ROW. */
7619 lastcol = 0;
7620 current_x = best_row->x;
7621 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
7622 {
7623 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7624 int charpos = glyph->charpos;
7625
7626 if (BUFFERP (glyph->object))
7627 {
7628 if (charpos == pos)
7629 {
7630 *hpos = i;
7631 *vpos = best_row_vpos;
7632 *x = current_x;
7633 *y = best_row->y;
7634 return 1;
7635 }
7636 else if (charpos > pos)
7637 break;
7638 }
7639 else if (EQ (glyph->object, stop))
7640 break;
7641
7642 if (charpos > 0)
7643 lastcol = i;
7644 current_x += glyph->pixel_width;
7645 }
7646
7647 /* If we're looking for the end of the buffer,
7648 and we didn't find it in the line we scanned,
7649 use the start of the following line. */
7650 if (maybe_next_line_p)
7651 {
7652 ++best_row;
7653 ++best_row_vpos;
7654 lastcol = 0;
7655 current_x = best_row->x;
7656 }
7657
7658 *vpos = best_row_vpos;
7659 *hpos = lastcol + 1;
7660 *x = current_x;
7661 *y = best_row->y;
7662 return 0;
7663 }
7664
7665 #endif /* not 0 */
7666
7667
7668 /* Find the position of the the glyph for position POS in OBJECT in
7669 window W's current matrix, and return in *X/*Y the pixel
7670 coordinates, and return in *HPOS/*VPOS the column/row of the glyph.
7671
7672 RIGHT_P non-zero means return the position of the right edge of the
7673 glyph, RIGHT_P zero means return the left edge position.
7674
7675 If no glyph for POS exists in the matrix, return the position of
7676 the glyph with the next smaller position that is in the matrix, if
7677 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7678 exists in the matrix, return the position of the glyph with the
7679 next larger position in OBJECT.
7680
7681 Value is non-zero if a glyph was found. */
7682
7683 static int
7684 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7685 struct window *w;
7686 int pos;
7687 Lisp_Object object;
7688 int *hpos, *vpos, *x, *y;
7689 int right_p;
7690 {
7691 int yb = window_text_bottom_y (w);
7692 struct glyph_row *r;
7693 struct glyph *best_glyph = NULL;
7694 struct glyph_row *best_row = NULL;
7695 int best_x = 0;
7696
7697 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7698 r->enabled_p && r->y < yb;
7699 ++r)
7700 {
7701 struct glyph *g = r->glyphs[TEXT_AREA];
7702 struct glyph *e = g + r->used[TEXT_AREA];
7703 int gx;
7704
7705 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7706 if (EQ (g->object, object))
7707 {
7708 if (g->charpos == pos)
7709 {
7710 best_glyph = g;
7711 best_x = gx;
7712 best_row = r;
7713 goto found;
7714 }
7715 else if (best_glyph == NULL
7716 || ((abs (g->charpos - pos)
7717 < abs (best_glyph->charpos - pos))
7718 && (right_p
7719 ? g->charpos < pos
7720 : g->charpos > pos)))
7721 {
7722 best_glyph = g;
7723 best_x = gx;
7724 best_row = r;
7725 }
7726 }
7727 }
7728
7729 found:
7730
7731 if (best_glyph)
7732 {
7733 *x = best_x;
7734 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7735
7736 if (right_p)
7737 {
7738 *x += best_glyph->pixel_width;
7739 ++*hpos;
7740 }
7741
7742 *y = best_row->y;
7743 *vpos = best_row - w->current_matrix->rows;
7744 }
7745
7746 return best_glyph != NULL;
7747 }
7748
7749
7750 /* Display the active region described by mouse_face_*
7751 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7752
7753 static void
7754 show_mouse_face (dpyinfo, draw)
7755 struct x_display_info *dpyinfo;
7756 enum draw_glyphs_face draw;
7757 {
7758 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7759 struct frame *f = XFRAME (WINDOW_FRAME (w));
7760 int i;
7761 int cursor_off_p = 0;
7762 struct cursor_pos saved_cursor;
7763
7764 saved_cursor = output_cursor;
7765
7766 /* If window is in the process of being destroyed, don't bother
7767 to do anything. */
7768 if (w->current_matrix == NULL)
7769 goto set_x_cursor;
7770
7771 /* Recognize when we are called to operate on rows that don't exist
7772 anymore. This can happen when a window is split. */
7773 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7774 goto set_x_cursor;
7775
7776 set_output_cursor (&w->phys_cursor);
7777
7778 /* Note that mouse_face_beg_row etc. are window relative. */
7779 for (i = dpyinfo->mouse_face_beg_row;
7780 i <= dpyinfo->mouse_face_end_row;
7781 i++)
7782 {
7783 int start_hpos, end_hpos, start_x;
7784 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7785
7786 /* Don't do anything if row doesn't have valid contents. */
7787 if (!row->enabled_p)
7788 continue;
7789
7790 /* For all but the first row, the highlight starts at column 0. */
7791 if (i == dpyinfo->mouse_face_beg_row)
7792 {
7793 start_hpos = dpyinfo->mouse_face_beg_col;
7794 start_x = dpyinfo->mouse_face_beg_x;
7795 }
7796 else
7797 {
7798 start_hpos = 0;
7799 start_x = 0;
7800 }
7801
7802 if (i == dpyinfo->mouse_face_end_row)
7803 end_hpos = dpyinfo->mouse_face_end_col;
7804 else
7805 end_hpos = row->used[TEXT_AREA];
7806
7807 /* If the cursor's in the text we are about to rewrite, turn the
7808 cursor off. */
7809 if (!w->pseudo_window_p
7810 && i == output_cursor.vpos
7811 && output_cursor.hpos >= start_hpos - 1
7812 && output_cursor.hpos <= end_hpos)
7813 {
7814 x_update_window_cursor (w, 0);
7815 cursor_off_p = 1;
7816 }
7817
7818 if (end_hpos > start_hpos)
7819 {
7820 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7821 start_hpos, end_hpos, draw, NULL, NULL, 0);
7822 row->mouse_face_p = draw == DRAW_MOUSE_FACE || DRAW_IMAGE_RAISED;
7823 }
7824 }
7825
7826 /* If we turned the cursor off, turn it back on. */
7827 if (cursor_off_p)
7828 x_display_cursor (w, 1,
7829 output_cursor.hpos, output_cursor.vpos,
7830 output_cursor.x, output_cursor.y);
7831
7832 output_cursor = saved_cursor;
7833
7834 set_x_cursor:
7835
7836 /* Change the mouse cursor. */
7837 if (draw == DRAW_NORMAL_TEXT)
7838 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7839 f->output_data.x->text_cursor);
7840 else if (draw == DRAW_MOUSE_FACE)
7841 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7842 f->output_data.x->cross_cursor);
7843 else
7844 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7845 f->output_data.x->nontext_cursor);
7846 }
7847
7848 /* Clear out the mouse-highlighted active region.
7849 Redraw it un-highlighted first. Value is non-zero if mouse
7850 face was actually drawn unhighlighted. */
7851
7852 static int
7853 clear_mouse_face (dpyinfo)
7854 struct x_display_info *dpyinfo;
7855 {
7856 int cleared = 0;
7857
7858 if (!NILP (dpyinfo->mouse_face_window))
7859 {
7860 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7861 cleared = 1;
7862 }
7863
7864 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7865 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7866 dpyinfo->mouse_face_window = Qnil;
7867 dpyinfo->mouse_face_overlay = Qnil;
7868 return cleared;
7869 }
7870
7871
7872 /* Clear any mouse-face on window W. This function is part of the
7873 redisplay interface, and is called from try_window_id and similar
7874 functions to ensure the mouse-highlight is off. */
7875
7876 static void
7877 x_clear_mouse_face (w)
7878 struct window *w;
7879 {
7880 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7881 Lisp_Object window;
7882
7883 BLOCK_INPUT;
7884 XSETWINDOW (window, w);
7885 if (EQ (window, dpyinfo->mouse_face_window))
7886 clear_mouse_face (dpyinfo);
7887 UNBLOCK_INPUT;
7888 }
7889
7890
7891 /* Just discard the mouse face information for frame F, if any.
7892 This is used when the size of F is changed. */
7893
7894 void
7895 cancel_mouse_face (f)
7896 FRAME_PTR f;
7897 {
7898 Lisp_Object window;
7899 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7900
7901 window = dpyinfo->mouse_face_window;
7902 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7903 {
7904 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7905 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7906 dpyinfo->mouse_face_window = Qnil;
7907 }
7908 }
7909
7910 \f
7911 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
7912
7913
7914 /* Try to determine frame pixel position and size of the glyph under
7915 frame pixel coordinates X/Y on frame F . Return the position and
7916 size in *RECT. Value is non-zero if we could compute these
7917 values. */
7918
7919 static int
7920 glyph_rect (f, x, y, rect)
7921 struct frame *f;
7922 int x, y;
7923 XRectangle *rect;
7924 {
7925 Lisp_Object window;
7926 int part, found = 0;
7927
7928 window = window_from_coordinates (f, x, y, &part, 0);
7929 if (!NILP (window))
7930 {
7931 struct window *w = XWINDOW (window);
7932 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7933 struct glyph_row *end = r + w->current_matrix->nrows - 1;
7934 int area;
7935
7936 frame_to_window_pixel_xy (w, &x, &y);
7937
7938 for (; !found && r < end && r->enabled_p; ++r)
7939 if (r->y >= y)
7940 {
7941 struct glyph *g = r->glyphs[TEXT_AREA];
7942 struct glyph *end = g + r->used[TEXT_AREA];
7943 int gx;
7944
7945 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
7946 if (gx >= x)
7947 {
7948 rect->width = g->pixel_width;
7949 rect->height = r->height;
7950 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
7951 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
7952 found = 1;
7953 }
7954 }
7955 }
7956
7957 return found;
7958 }
7959
7960
7961 /* Return the current position of the mouse.
7962 *FP should be a frame which indicates which display to ask about.
7963
7964 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
7965 and *PART to the frame, window, and scroll bar part that the mouse
7966 is over. Set *X and *Y to the portion and whole of the mouse's
7967 position on the scroll bar.
7968
7969 If the mouse movement started elsewhere, set *FP to the frame the
7970 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
7971 the mouse is over.
7972
7973 Set *TIME to the server time-stamp for the time at which the mouse
7974 was at this position.
7975
7976 Don't store anything if we don't have a valid set of values to report.
7977
7978 This clears the mouse_moved flag, so we can wait for the next mouse
7979 movement. */
7980
7981 static void
7982 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7983 FRAME_PTR *fp;
7984 int insist;
7985 Lisp_Object *bar_window;
7986 enum scroll_bar_part *part;
7987 Lisp_Object *x, *y;
7988 unsigned long *time;
7989 {
7990 FRAME_PTR f1;
7991
7992 BLOCK_INPUT;
7993
7994 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7995 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7996 else
7997 {
7998 Window root;
7999 int root_x, root_y;
8000
8001 Window dummy_window;
8002 int dummy;
8003
8004 Lisp_Object frame, tail;
8005
8006 /* Clear the mouse-moved flag for every frame on this display. */
8007 FOR_EACH_FRAME (tail, frame)
8008 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
8009 XFRAME (frame)->mouse_moved = 0;
8010
8011 last_mouse_scroll_bar = Qnil;
8012
8013 /* Figure out which root window we're on. */
8014 XQueryPointer (FRAME_X_DISPLAY (*fp),
8015 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
8016
8017 /* The root window which contains the pointer. */
8018 &root,
8019
8020 /* Trash which we can't trust if the pointer is on
8021 a different screen. */
8022 &dummy_window,
8023
8024 /* The position on that root window. */
8025 &root_x, &root_y,
8026
8027 /* More trash we can't trust. */
8028 &dummy, &dummy,
8029
8030 /* Modifier keys and pointer buttons, about which
8031 we don't care. */
8032 (unsigned int *) &dummy);
8033
8034 /* Now we have a position on the root; find the innermost window
8035 containing the pointer. */
8036 {
8037 Window win, child;
8038 int win_x, win_y;
8039 int parent_x = 0, parent_y = 0;
8040 int count;
8041
8042 win = root;
8043
8044 /* XTranslateCoordinates can get errors if the window
8045 structure is changing at the same time this function
8046 is running. So at least we must not crash from them. */
8047
8048 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
8049
8050 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
8051 && FRAME_LIVE_P (last_mouse_frame))
8052 {
8053 /* If mouse was grabbed on a frame, give coords for that frame
8054 even if the mouse is now outside it. */
8055 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
8056
8057 /* From-window, to-window. */
8058 root, FRAME_X_WINDOW (last_mouse_frame),
8059
8060 /* From-position, to-position. */
8061 root_x, root_y, &win_x, &win_y,
8062
8063 /* Child of win. */
8064 &child);
8065 f1 = last_mouse_frame;
8066 }
8067 else
8068 {
8069 while (1)
8070 {
8071 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
8072
8073 /* From-window, to-window. */
8074 root, win,
8075
8076 /* From-position, to-position. */
8077 root_x, root_y, &win_x, &win_y,
8078
8079 /* Child of win. */
8080 &child);
8081
8082 if (child == None || child == win)
8083 break;
8084
8085 win = child;
8086 parent_x = win_x;
8087 parent_y = win_y;
8088 }
8089
8090 /* Now we know that:
8091 win is the innermost window containing the pointer
8092 (XTC says it has no child containing the pointer),
8093 win_x and win_y are the pointer's position in it
8094 (XTC did this the last time through), and
8095 parent_x and parent_y are the pointer's position in win's parent.
8096 (They are what win_x and win_y were when win was child.
8097 If win is the root window, it has no parent, and
8098 parent_{x,y} are invalid, but that's okay, because we'll
8099 never use them in that case.) */
8100
8101 /* Is win one of our frames? */
8102 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
8103
8104 #ifdef USE_X_TOOLKIT
8105 /* If we end up with the menu bar window, say it's not
8106 on the frame. */
8107 if (f1 != NULL
8108 && f1->output_data.x->menubar_widget
8109 && win == XtWindow (f1->output_data.x->menubar_widget))
8110 f1 = NULL;
8111 #endif /* USE_X_TOOLKIT */
8112 }
8113
8114 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
8115 f1 = 0;
8116
8117 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
8118
8119 /* If not, is it one of our scroll bars? */
8120 if (! f1)
8121 {
8122 struct scroll_bar *bar = x_window_to_scroll_bar (win);
8123
8124 if (bar)
8125 {
8126 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8127 win_x = parent_x;
8128 win_y = parent_y;
8129 }
8130 }
8131
8132 if (f1 == 0 && insist > 0)
8133 f1 = SELECTED_FRAME ();
8134
8135 if (f1)
8136 {
8137 /* Ok, we found a frame. Store all the values.
8138 last_mouse_glyph is a rectangle used to reduce the
8139 generation of mouse events. To not miss any motion
8140 events, we must divide the frame into rectangles of the
8141 size of the smallest character that could be displayed
8142 on it, i.e. into the same rectangles that matrices on
8143 the frame are divided into. */
8144
8145 int width, height, gx, gy;
8146 XRectangle rect;
8147
8148 if (glyph_rect (f1, win_x, win_y, &rect))
8149 last_mouse_glyph = rect;
8150 else
8151 {
8152 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
8153 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
8154 gx = win_x;
8155 gy = win_y;
8156
8157 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
8158 round down even for negative values. */
8159 if (gx < 0)
8160 gx -= width - 1;
8161 if (gy < 0)
8162 gy -= height - 1;
8163 gx = (gx + width - 1) / width * width;
8164 gy = (gy + height - 1) / height * height;
8165
8166 last_mouse_glyph.width = width;
8167 last_mouse_glyph.height = height;
8168 last_mouse_glyph.x = gx;
8169 last_mouse_glyph.y = gy;
8170 }
8171
8172 *bar_window = Qnil;
8173 *part = 0;
8174 *fp = f1;
8175 XSETINT (*x, win_x);
8176 XSETINT (*y, win_y);
8177 *time = last_mouse_movement_time;
8178 }
8179 }
8180 }
8181
8182 UNBLOCK_INPUT;
8183 }
8184
8185
8186 #ifdef USE_X_TOOLKIT
8187
8188 /* Atimer callback function for TIMER. Called every 0.1s to process
8189 Xt timeouts, if needed. We must avoid calling XtAppPending as
8190 much as possible because that function does an implicit XFlush
8191 that slows us down. */
8192
8193 static void
8194 x_process_timeouts (timer)
8195 struct atimer *timer;
8196 {
8197 if (toolkit_scroll_bar_interaction || popup_activated_flag)
8198 {
8199 BLOCK_INPUT;
8200 while (XtAppPending (Xt_app_con) & XtIMTimer)
8201 XtAppProcessEvent (Xt_app_con, XtIMTimer);
8202 UNBLOCK_INPUT;
8203 }
8204 }
8205
8206 #endif /* USE_X_TOOLKIT */
8207
8208 \f
8209 /* Scroll bar support. */
8210
8211 /* Given an X window ID, find the struct scroll_bar which manages it.
8212 This can be called in GC, so we have to make sure to strip off mark
8213 bits. */
8214
8215 static struct scroll_bar *
8216 x_window_to_scroll_bar (window_id)
8217 Window window_id;
8218 {
8219 Lisp_Object tail;
8220
8221 for (tail = Vframe_list;
8222 XGCTYPE (tail) == Lisp_Cons;
8223 tail = XCDR (tail))
8224 {
8225 Lisp_Object frame, bar, condemned;
8226
8227 frame = XCAR (tail);
8228 /* All elements of Vframe_list should be frames. */
8229 if (! GC_FRAMEP (frame))
8230 abort ();
8231
8232 /* Scan this frame's scroll bar list for a scroll bar with the
8233 right window ID. */
8234 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
8235 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
8236 /* This trick allows us to search both the ordinary and
8237 condemned scroll bar lists with one loop. */
8238 ! GC_NILP (bar) || (bar = condemned,
8239 condemned = Qnil,
8240 ! GC_NILP (bar));
8241 bar = XSCROLL_BAR (bar)->next)
8242 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
8243 return XSCROLL_BAR (bar);
8244 }
8245
8246 return 0;
8247 }
8248
8249
8250 #if defined USE_LUCID
8251
8252 /* Return the Lucid menu bar WINDOW is part of. Return null
8253 if WINDOW is not part of a menu bar. */
8254
8255 static Widget
8256 x_window_to_menu_bar (window)
8257 Window window;
8258 {
8259 Lisp_Object tail;
8260
8261 for (tail = Vframe_list;
8262 XGCTYPE (tail) == Lisp_Cons;
8263 tail = XCDR (tail))
8264 {
8265 Lisp_Object frame = XCAR (tail);
8266 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
8267
8268 if (menu_bar && xlwmenu_window_p (menu_bar, window))
8269 return menu_bar;
8270 }
8271
8272 return NULL;
8273 }
8274
8275 #endif /* USE_LUCID */
8276
8277 \f
8278 /************************************************************************
8279 Toolkit scroll bars
8280 ************************************************************************/
8281
8282 #ifdef USE_TOOLKIT_SCROLL_BARS
8283
8284 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
8285 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
8286 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
8287 struct scroll_bar *));
8288 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
8289 int, int, int));
8290
8291
8292 /* Id of action hook installed for scroll bars. */
8293
8294 static XtActionHookId action_hook_id;
8295
8296 /* Lisp window being scrolled. Set when starting to interact with
8297 a toolkit scroll bar, reset to nil when ending the interaction. */
8298
8299 static Lisp_Object window_being_scrolled;
8300
8301 /* Last scroll bar part sent in xm_scroll_callback. */
8302
8303 static int last_scroll_bar_part;
8304
8305 /* Whether this is an Xaw with arrow-scrollbars. This should imply
8306 that movements of 1/20 of the screen size are mapped to up/down. */
8307
8308 static Boolean xaw3d_arrow_scroll;
8309
8310 /* Whether the drag scrolling maintains the mouse at the top of the
8311 thumb. If not, resizing the thumb needs to be done more carefully
8312 to avoid jerkyness. */
8313
8314 static Boolean xaw3d_pick_top;
8315
8316
8317 /* Action hook installed via XtAppAddActionHook when toolkit scroll
8318 bars are used.. The hook is responsible for detecting when
8319 the user ends an interaction with the scroll bar, and generates
8320 a `end-scroll' scroll_bar_click' event if so. */
8321
8322 static void
8323 xt_action_hook (widget, client_data, action_name, event, params,
8324 num_params)
8325 Widget widget;
8326 XtPointer client_data;
8327 String action_name;
8328 XEvent *event;
8329 String *params;
8330 Cardinal *num_params;
8331 {
8332 int scroll_bar_p;
8333 char *end_action;
8334
8335 #ifdef USE_MOTIF
8336 scroll_bar_p = XmIsScrollBar (widget);
8337 end_action = "Release";
8338 #else /* !USE_MOTIF i.e. use Xaw */
8339 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
8340 end_action = "EndScroll";
8341 #endif /* USE_MOTIF */
8342
8343 if (scroll_bar_p
8344 && strcmp (action_name, end_action) == 0
8345 && WINDOWP (window_being_scrolled))
8346 {
8347 struct window *w;
8348
8349 x_send_scroll_bar_event (window_being_scrolled,
8350 scroll_bar_end_scroll, 0, 0);
8351 w = XWINDOW (window_being_scrolled);
8352 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
8353 window_being_scrolled = Qnil;
8354 last_scroll_bar_part = -1;
8355
8356 /* Xt timeouts no longer needed. */
8357 toolkit_scroll_bar_interaction = 0;
8358 }
8359 }
8360
8361 /* A vector of windows used for communication between
8362 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
8363
8364 static struct window **scroll_bar_windows;
8365 static int scroll_bar_windows_size;
8366
8367
8368 /* Send a client message with message type Xatom_Scrollbar for a
8369 scroll action to the frame of WINDOW. PART is a value identifying
8370 the part of the scroll bar that was clicked on. PORTION is the
8371 amount to scroll of a whole of WHOLE. */
8372
8373 static void
8374 x_send_scroll_bar_event (window, part, portion, whole)
8375 Lisp_Object window;
8376 int part, portion, whole;
8377 {
8378 XEvent event;
8379 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
8380 struct window *w = XWINDOW (window);
8381 struct frame *f = XFRAME (w->frame);
8382 int i;
8383
8384 BLOCK_INPUT;
8385
8386 /* Construct a ClientMessage event to send to the frame. */
8387 ev->type = ClientMessage;
8388 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
8389 ev->display = FRAME_X_DISPLAY (f);
8390 ev->window = FRAME_X_WINDOW (f);
8391 ev->format = 32;
8392
8393 /* We can only transfer 32 bits in the XClientMessageEvent, which is
8394 not enough to store a pointer or Lisp_Object on a 64 bit system.
8395 So, store the window in scroll_bar_windows and pass the index
8396 into that array in the event. */
8397 for (i = 0; i < scroll_bar_windows_size; ++i)
8398 if (scroll_bar_windows[i] == NULL)
8399 break;
8400
8401 if (i == scroll_bar_windows_size)
8402 {
8403 int new_size = max (10, 2 * scroll_bar_windows_size);
8404 size_t nbytes = new_size * sizeof *scroll_bar_windows;
8405 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
8406
8407 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
8408 nbytes);
8409 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
8410 scroll_bar_windows_size = new_size;
8411 }
8412
8413 scroll_bar_windows[i] = w;
8414 ev->data.l[0] = (long) i;
8415 ev->data.l[1] = (long) part;
8416 ev->data.l[2] = (long) 0;
8417 ev->data.l[3] = (long) portion;
8418 ev->data.l[4] = (long) whole;
8419
8420 /* Make Xt timeouts work while the scroll bar is active. */
8421 toolkit_scroll_bar_interaction = 1;
8422
8423 /* Setting the event mask to zero means that the message will
8424 be sent to the client that created the window, and if that
8425 window no longer exists, no event will be sent. */
8426 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
8427 UNBLOCK_INPUT;
8428 }
8429
8430
8431 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
8432 in *IEVENT. */
8433
8434 static void
8435 x_scroll_bar_to_input_event (event, ievent)
8436 XEvent *event;
8437 struct input_event *ievent;
8438 {
8439 XClientMessageEvent *ev = (XClientMessageEvent *) event;
8440 Lisp_Object window;
8441 struct frame *f;
8442 struct window *w;
8443
8444 w = scroll_bar_windows[ev->data.l[0]];
8445 scroll_bar_windows[ev->data.l[0]] = NULL;
8446
8447 XSETWINDOW (window, w);
8448 f = XFRAME (w->frame);
8449
8450 ievent->kind = scroll_bar_click;
8451 ievent->frame_or_window = window;
8452 ievent->arg = Qnil;
8453 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
8454 ievent->part = ev->data.l[1];
8455 ievent->code = ev->data.l[2];
8456 ievent->x = make_number ((int) ev->data.l[3]);
8457 ievent->y = make_number ((int) ev->data.l[4]);
8458 ievent->modifiers = 0;
8459 }
8460
8461
8462 #ifdef USE_MOTIF
8463
8464 /* Minimum and maximum values used for Motif scroll bars. */
8465
8466 #define XM_SB_MIN 1
8467 #define XM_SB_MAX 10000000
8468 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
8469
8470
8471 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
8472 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
8473 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
8474
8475 static void
8476 xm_scroll_callback (widget, client_data, call_data)
8477 Widget widget;
8478 XtPointer client_data, call_data;
8479 {
8480 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8481 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
8482 int part = -1, whole = 0, portion = 0;
8483
8484 switch (cs->reason)
8485 {
8486 case XmCR_DECREMENT:
8487 bar->dragging = Qnil;
8488 part = scroll_bar_up_arrow;
8489 break;
8490
8491 case XmCR_INCREMENT:
8492 bar->dragging = Qnil;
8493 part = scroll_bar_down_arrow;
8494 break;
8495
8496 case XmCR_PAGE_DECREMENT:
8497 bar->dragging = Qnil;
8498 part = scroll_bar_above_handle;
8499 break;
8500
8501 case XmCR_PAGE_INCREMENT:
8502 bar->dragging = Qnil;
8503 part = scroll_bar_below_handle;
8504 break;
8505
8506 case XmCR_TO_TOP:
8507 bar->dragging = Qnil;
8508 part = scroll_bar_to_top;
8509 break;
8510
8511 case XmCR_TO_BOTTOM:
8512 bar->dragging = Qnil;
8513 part = scroll_bar_to_bottom;
8514 break;
8515
8516 case XmCR_DRAG:
8517 {
8518 int slider_size;
8519 int dragging_down_p = (INTEGERP (bar->dragging)
8520 && XINT (bar->dragging) <= cs->value);
8521
8522 /* Get the slider size. */
8523 BLOCK_INPUT;
8524 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
8525 UNBLOCK_INPUT;
8526
8527 /* At the max position of the scroll bar, do a line-wise
8528 movement. Without doing anything, we would be called with
8529 the same cs->value again and again. If we want to make
8530 sure that we can reach the end of the buffer, we have to do
8531 something.
8532
8533 Implementation note: setting bar->dragging always to
8534 cs->value gives a smoother movement at the max position.
8535 Setting it to nil when doing line-wise movement gives
8536 a better slider behavior. */
8537
8538 if (cs->value + slider_size == XM_SB_MAX
8539 || (dragging_down_p
8540 && last_scroll_bar_part == scroll_bar_down_arrow))
8541 {
8542 part = scroll_bar_down_arrow;
8543 bar->dragging = Qnil;
8544 }
8545 else
8546 {
8547 whole = XM_SB_RANGE;
8548 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
8549 part = scroll_bar_handle;
8550 bar->dragging = make_number (cs->value);
8551 }
8552 }
8553 break;
8554
8555 case XmCR_VALUE_CHANGED:
8556 break;
8557 };
8558
8559 if (part >= 0)
8560 {
8561 window_being_scrolled = bar->window;
8562 last_scroll_bar_part = part;
8563 x_send_scroll_bar_event (bar->window, part, portion, whole);
8564 }
8565 }
8566
8567
8568 #else /* !USE_MOTIF, i.e. Xaw. */
8569
8570
8571 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
8572 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8573 scroll bar struct. CALL_DATA is a pointer to a float saying where
8574 the thumb is. */
8575
8576 static void
8577 xaw_jump_callback (widget, client_data, call_data)
8578 Widget widget;
8579 XtPointer client_data, call_data;
8580 {
8581 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8582 float top = *(float *) call_data;
8583 float shown;
8584 int whole, portion, height;
8585 int part;
8586
8587 /* Get the size of the thumb, a value between 0 and 1. */
8588 BLOCK_INPUT;
8589 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
8590 UNBLOCK_INPUT;
8591
8592 whole = 10000000;
8593 portion = shown < 1 ? top * whole : 0;
8594
8595 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8596 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8597 the bottom, so we force the scrolling whenever we see that we're
8598 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8599 we try to ensure that we always stay two pixels away from the
8600 bottom). */
8601 part = scroll_bar_down_arrow;
8602 else
8603 part = scroll_bar_handle;
8604
8605 window_being_scrolled = bar->window;
8606 bar->dragging = make_number (portion);
8607 last_scroll_bar_part = part;
8608 x_send_scroll_bar_event (bar->window, part, portion, whole);
8609 }
8610
8611
8612 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
8613 i.e. line or page up or down. WIDGET is the Xaw scroll bar
8614 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8615 the scroll bar. CALL_DATA is an integer specifying the action that
8616 has taken place. It's magnitude is in the range 0..height of the
8617 scroll bar. Negative values mean scroll towards buffer start.
8618 Values < height of scroll bar mean line-wise movement. */
8619
8620 static void
8621 xaw_scroll_callback (widget, client_data, call_data)
8622 Widget widget;
8623 XtPointer client_data, call_data;
8624 {
8625 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8626 int position = (int) call_data;
8627 Dimension height;
8628 int part;
8629
8630 /* Get the height of the scroll bar. */
8631 BLOCK_INPUT;
8632 XtVaGetValues (widget, XtNheight, &height, NULL);
8633 UNBLOCK_INPUT;
8634
8635 if (abs (position) >= height)
8636 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8637
8638 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8639 it maps line-movement to call_data = max(5, height/20). */
8640 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8641 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
8642 else
8643 part = scroll_bar_move_ratio;
8644
8645 window_being_scrolled = bar->window;
8646 bar->dragging = Qnil;
8647 last_scroll_bar_part = part;
8648 x_send_scroll_bar_event (bar->window, part, position, height);
8649 }
8650
8651
8652 #endif /* not USE_MOTIF */
8653
8654
8655 /* Create the widget for scroll bar BAR on frame F. Record the widget
8656 and X window of the scroll bar in BAR. */
8657
8658 static void
8659 x_create_toolkit_scroll_bar (f, bar)
8660 struct frame *f;
8661 struct scroll_bar *bar;
8662 {
8663 Window xwindow;
8664 Widget widget;
8665 Arg av[20];
8666 int ac = 0;
8667 char *scroll_bar_name = "verticalScrollBar";
8668 unsigned long pixel;
8669
8670 BLOCK_INPUT;
8671
8672 #ifdef USE_MOTIF
8673 /* Set resources. Create the widget. */
8674 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8675 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8676 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8677 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8678 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8679 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8680 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8681
8682 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8683 if (pixel != -1)
8684 {
8685 XtSetArg (av[ac], XmNforeground, pixel);
8686 ++ac;
8687 }
8688
8689 pixel = f->output_data.x->scroll_bar_background_pixel;
8690 if (pixel != -1)
8691 {
8692 XtSetArg (av[ac], XmNbackground, pixel);
8693 ++ac;
8694 }
8695
8696 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8697 scroll_bar_name, av, ac);
8698
8699 /* Add one callback for everything that can happen. */
8700 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8701 (XtPointer) bar);
8702 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8703 (XtPointer) bar);
8704 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8705 (XtPointer) bar);
8706 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8707 (XtPointer) bar);
8708 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8709 (XtPointer) bar);
8710 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8711 (XtPointer) bar);
8712 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8713 (XtPointer) bar);
8714
8715 /* Realize the widget. Only after that is the X window created. */
8716 XtRealizeWidget (widget);
8717
8718 /* Set the cursor to an arrow. I didn't find a resource to do that.
8719 And I'm wondering why it hasn't an arrow cursor by default. */
8720 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8721 f->output_data.x->nontext_cursor);
8722
8723 #else /* !USE_MOTIF i.e. use Xaw */
8724
8725 /* Set resources. Create the widget. The background of the
8726 Xaw3d scroll bar widget is a little bit light for my taste.
8727 We don't alter it here to let users change it according
8728 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8729 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8730 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
8731 /* For smoother scrolling with Xaw3d -sm */
8732 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8733
8734 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8735 if (pixel != -1)
8736 {
8737 XtSetArg (av[ac], XtNforeground, pixel);
8738 ++ac;
8739 }
8740
8741 pixel = f->output_data.x->scroll_bar_background_pixel;
8742 if (pixel != -1)
8743 {
8744 XtSetArg (av[ac], XtNbackground, pixel);
8745 ++ac;
8746 }
8747
8748 /* Top/bottom shadow colors. */
8749
8750 /* Allocate them, if necessary. */
8751 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
8752 {
8753 pixel = f->output_data.x->scroll_bar_background_pixel;
8754 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8755 &pixel, 1.2, 0x8000))
8756 pixel = -1;
8757 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
8758 }
8759 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8760 {
8761 pixel = f->output_data.x->scroll_bar_background_pixel;
8762 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8763 &pixel, 0.6, 0x4000))
8764 pixel = -1;
8765 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
8766 }
8767
8768 /* Tell the toolkit about them. */
8769 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
8770 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8771 /* We tried to allocate a color for the top/bottom shadow, and
8772 failed, so tell Xaw3d to use dithering instead. */
8773 {
8774 XtSetArg (av[ac], XtNbeNiceToColormap, True);
8775 ++ac;
8776 }
8777 else
8778 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
8779 be more consistent with other emacs 3d colors, and since Xaw3d is
8780 not good at dealing with allocation failure. */
8781 {
8782 /* This tells Xaw3d to use real colors instead of dithering for
8783 the shadows. */
8784 XtSetArg (av[ac], XtNbeNiceToColormap, False);
8785 ++ac;
8786
8787 /* Specify the colors. */
8788 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
8789 if (pixel != -1)
8790 {
8791 XtSetArg (av[ac], "topShadowPixel", pixel);
8792 ++ac;
8793 }
8794 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
8795 if (pixel != -1)
8796 {
8797 XtSetArg (av[ac], "bottomShadowPixel", pixel);
8798 ++ac;
8799 }
8800 }
8801
8802 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8803 f->output_data.x->edit_widget, av, ac);
8804
8805 {
8806 char *initial = "";
8807 char *val = initial;
8808 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8809 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8810 if (val == initial)
8811 { /* ARROW_SCROLL */
8812 xaw3d_arrow_scroll = True;
8813 /* Isn't that just a personal preference ? -sm */
8814 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8815 }
8816 }
8817
8818 /* Define callbacks. */
8819 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8820 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
8821 (XtPointer) bar);
8822
8823 /* Realize the widget. Only after that is the X window created. */
8824 XtRealizeWidget (widget);
8825
8826 #endif /* !USE_MOTIF */
8827
8828 /* Install an action hook that let's us detect when the user
8829 finishes interacting with a scroll bar. */
8830 if (action_hook_id == 0)
8831 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8832
8833 /* Remember X window and widget in the scroll bar vector. */
8834 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8835 xwindow = XtWindow (widget);
8836 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8837
8838 UNBLOCK_INPUT;
8839 }
8840
8841
8842 /* Set the thumb size and position of scroll bar BAR. We are currently
8843 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8844
8845 static void
8846 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8847 struct scroll_bar *bar;
8848 int portion, position, whole;
8849 {
8850 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8851 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8852 float top, shown;
8853
8854 if (whole == 0)
8855 top = 0, shown = 1;
8856 else
8857 {
8858 top = (float) position / whole;
8859 shown = (float) portion / whole;
8860 }
8861
8862 BLOCK_INPUT;
8863
8864 #ifdef USE_MOTIF
8865 {
8866 int size, value;
8867
8868 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
8869 is the scroll bar's maximum and MIN is the scroll bar's minimum
8870 value. */
8871 size = shown * XM_SB_RANGE;
8872 size = min (size, XM_SB_RANGE);
8873 size = max (size, 1);
8874
8875 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8876 value = top * XM_SB_RANGE;
8877 value = min (value, XM_SB_MAX - size);
8878 value = max (value, XM_SB_MIN);
8879
8880 if (NILP (bar->dragging))
8881 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8882 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8883 /* This has the negative side effect that the slider value is
8884 not what it would be if we scrolled here using line-wise or
8885 page-wise movement. */
8886 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8887 else
8888 {
8889 /* If currently dragging, only update the slider size.
8890 This reduces flicker effects. */
8891 int old_value, old_size, increment, page_increment;
8892
8893 XmScrollBarGetValues (widget, &old_value, &old_size,
8894 &increment, &page_increment);
8895 XmScrollBarSetValues (widget, old_value,
8896 min (size, XM_SB_RANGE - old_value),
8897 0, 0, False);
8898 }
8899 }
8900 #else /* !USE_MOTIF i.e. use Xaw */
8901 {
8902 float old_top, old_shown;
8903 Dimension height;
8904 XtVaGetValues (widget,
8905 XtNtopOfThumb, &old_top,
8906 XtNshown, &old_shown,
8907 XtNheight, &height,
8908 NULL);
8909
8910 /* Massage the top+shown values. */
8911 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8912 top = max (0, min (1, top));
8913 else
8914 top = old_top;
8915 /* Keep two pixels available for moving the thumb down. */
8916 shown = max (0, min (1 - top - (2.0 / height), shown));
8917
8918 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8919 check that your system's configuration file contains a define
8920 for `NARROWPROTO'. See s/freebsd.h for an example. */
8921 if (top != old_top || shown != old_shown)
8922 {
8923 if (NILP (bar->dragging))
8924 XawScrollbarSetThumb (widget, top, shown);
8925 else
8926 {
8927 #ifdef HAVE_XAW3D
8928 ScrollbarWidget sb = (ScrollbarWidget) widget;
8929 int scroll_mode = 0;
8930
8931 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8932 if (xaw3d_arrow_scroll)
8933 {
8934 /* Xaw3d stupidly ignores resize requests while dragging
8935 so we have to make it believe it's not in dragging mode. */
8936 scroll_mode = sb->scrollbar.scroll_mode;
8937 if (scroll_mode == 2)
8938 sb->scrollbar.scroll_mode = 0;
8939 }
8940 #endif
8941 /* Try to make the scrolling a tad smoother. */
8942 if (!xaw3d_pick_top)
8943 shown = min (shown, old_shown);
8944
8945 XawScrollbarSetThumb (widget, top, shown);
8946
8947 #ifdef HAVE_XAW3D
8948 if (xaw3d_arrow_scroll && scroll_mode == 2)
8949 sb->scrollbar.scroll_mode = scroll_mode;
8950 #endif
8951 }
8952 }
8953 }
8954 #endif /* !USE_MOTIF */
8955
8956 UNBLOCK_INPUT;
8957 }
8958
8959 #endif /* USE_TOOLKIT_SCROLL_BARS */
8960
8961
8962 \f
8963 /************************************************************************
8964 Scroll bars, general
8965 ************************************************************************/
8966
8967 /* Create a scroll bar and return the scroll bar vector for it. W is
8968 the Emacs window on which to create the scroll bar. TOP, LEFT,
8969 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8970 scroll bar. */
8971
8972 static struct scroll_bar *
8973 x_scroll_bar_create (w, top, left, width, height)
8974 struct window *w;
8975 int top, left, width, height;
8976 {
8977 struct frame *f = XFRAME (w->frame);
8978 struct scroll_bar *bar
8979 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8980
8981 BLOCK_INPUT;
8982
8983 #ifdef USE_TOOLKIT_SCROLL_BARS
8984 x_create_toolkit_scroll_bar (f, bar);
8985 #else /* not USE_TOOLKIT_SCROLL_BARS */
8986 {
8987 XSetWindowAttributes a;
8988 unsigned long mask;
8989 Window window;
8990
8991 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8992 if (a.background_pixel == -1)
8993 a.background_pixel = f->output_data.x->background_pixel;
8994
8995 a.event_mask = (ButtonPressMask | ButtonReleaseMask
8996 | ButtonMotionMask | PointerMotionHintMask
8997 | ExposureMask);
8998 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
8999
9000 mask = (CWBackPixel | CWEventMask | CWCursor);
9001
9002 /* Clear the area of W that will serve as a scroll bar. This is
9003 for the case that a window has been split horizontally. In
9004 this case, no clear_frame is generated to reduce flickering. */
9005 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9006 left, top, width,
9007 window_box_height (w), False);
9008
9009 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9010 /* Position and size of scroll bar. */
9011 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9012 top,
9013 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9014 height,
9015 /* Border width, depth, class, and visual. */
9016 0,
9017 CopyFromParent,
9018 CopyFromParent,
9019 CopyFromParent,
9020 /* Attributes. */
9021 mask, &a);
9022 SET_SCROLL_BAR_X_WINDOW (bar, window);
9023 }
9024 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9025
9026 XSETWINDOW (bar->window, w);
9027 XSETINT (bar->top, top);
9028 XSETINT (bar->left, left);
9029 XSETINT (bar->width, width);
9030 XSETINT (bar->height, height);
9031 XSETINT (bar->start, 0);
9032 XSETINT (bar->end, 0);
9033 bar->dragging = Qnil;
9034
9035 /* Add bar to its frame's list of scroll bars. */
9036 bar->next = FRAME_SCROLL_BARS (f);
9037 bar->prev = Qnil;
9038 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9039 if (!NILP (bar->next))
9040 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9041
9042 /* Map the window/widget. */
9043 #ifdef USE_TOOLKIT_SCROLL_BARS
9044 {
9045 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
9046 XtConfigureWidget (scroll_bar,
9047 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9048 top,
9049 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9050 max (height, 1), 0);
9051 XtMapWidget (scroll_bar);
9052 }
9053 #else /* not USE_TOOLKIT_SCROLL_BARS */
9054 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9055 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9056
9057 UNBLOCK_INPUT;
9058 return bar;
9059 }
9060
9061
9062 /* Draw BAR's handle in the proper position.
9063
9064 If the handle is already drawn from START to END, don't bother
9065 redrawing it, unless REBUILD is non-zero; in that case, always
9066 redraw it. (REBUILD is handy for drawing the handle after expose
9067 events.)
9068
9069 Normally, we want to constrain the start and end of the handle to
9070 fit inside its rectangle, but if the user is dragging the scroll
9071 bar handle, we want to let them drag it down all the way, so that
9072 the bar's top is as far down as it goes; otherwise, there's no way
9073 to move to the very end of the buffer. */
9074
9075 #ifndef USE_TOOLKIT_SCROLL_BARS
9076
9077 static void
9078 x_scroll_bar_set_handle (bar, start, end, rebuild)
9079 struct scroll_bar *bar;
9080 int start, end;
9081 int rebuild;
9082 {
9083 int dragging = ! NILP (bar->dragging);
9084 Window w = SCROLL_BAR_X_WINDOW (bar);
9085 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9086 GC gc = f->output_data.x->normal_gc;
9087
9088 /* If the display is already accurate, do nothing. */
9089 if (! rebuild
9090 && start == XINT (bar->start)
9091 && end == XINT (bar->end))
9092 return;
9093
9094 BLOCK_INPUT;
9095
9096 {
9097 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
9098 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9099 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9100
9101 /* Make sure the values are reasonable, and try to preserve
9102 the distance between start and end. */
9103 {
9104 int length = end - start;
9105
9106 if (start < 0)
9107 start = 0;
9108 else if (start > top_range)
9109 start = top_range;
9110 end = start + length;
9111
9112 if (end < start)
9113 end = start;
9114 else if (end > top_range && ! dragging)
9115 end = top_range;
9116 }
9117
9118 /* Store the adjusted setting in the scroll bar. */
9119 XSETINT (bar->start, start);
9120 XSETINT (bar->end, end);
9121
9122 /* Clip the end position, just for display. */
9123 if (end > top_range)
9124 end = top_range;
9125
9126 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
9127 below top positions, to make sure the handle is always at least
9128 that many pixels tall. */
9129 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
9130
9131 /* Draw the empty space above the handle. Note that we can't clear
9132 zero-height areas; that means "clear to end of window." */
9133 if (0 < start)
9134 x_clear_area (FRAME_X_DISPLAY (f), w,
9135 /* x, y, width, height, and exposures. */
9136 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9137 VERTICAL_SCROLL_BAR_TOP_BORDER,
9138 inside_width, start,
9139 False);
9140
9141 /* Change to proper foreground color if one is specified. */
9142 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9143 XSetForeground (FRAME_X_DISPLAY (f), gc,
9144 f->output_data.x->scroll_bar_foreground_pixel);
9145
9146 /* Draw the handle itself. */
9147 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
9148 /* x, y, width, height */
9149 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9150 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
9151 inside_width, end - start);
9152
9153 /* Restore the foreground color of the GC if we changed it above. */
9154 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9155 XSetForeground (FRAME_X_DISPLAY (f), gc,
9156 f->output_data.x->foreground_pixel);
9157
9158 /* Draw the empty space below the handle. Note that we can't
9159 clear zero-height areas; that means "clear to end of window." */
9160 if (end < inside_height)
9161 x_clear_area (FRAME_X_DISPLAY (f), w,
9162 /* x, y, width, height, and exposures. */
9163 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9164 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
9165 inside_width, inside_height - end,
9166 False);
9167
9168 }
9169
9170 UNBLOCK_INPUT;
9171 }
9172
9173 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9174
9175 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
9176 nil. */
9177
9178 static void
9179 x_scroll_bar_remove (bar)
9180 struct scroll_bar *bar;
9181 {
9182 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9183 BLOCK_INPUT;
9184
9185 #ifdef USE_TOOLKIT_SCROLL_BARS
9186 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
9187 #else
9188 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9189 #endif
9190
9191 /* Disassociate this scroll bar from its window. */
9192 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
9193
9194 UNBLOCK_INPUT;
9195 }
9196
9197
9198 /* Set the handle of the vertical scroll bar for WINDOW to indicate
9199 that we are displaying PORTION characters out of a total of WHOLE
9200 characters, starting at POSITION. If WINDOW has no scroll bar,
9201 create one. */
9202
9203 static void
9204 XTset_vertical_scroll_bar (w, portion, whole, position)
9205 struct window *w;
9206 int portion, whole, position;
9207 {
9208 struct frame *f = XFRAME (w->frame);
9209 struct scroll_bar *bar;
9210 int top, height, left, sb_left, width, sb_width;
9211 int window_x, window_y, window_width, window_height;
9212
9213 /* Get window dimensions. */
9214 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
9215 top = window_y;
9216 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9217 height = window_height;
9218
9219 /* Compute the left edge of the scroll bar area. */
9220 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9221 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
9222 else
9223 left = XFASTINT (w->left);
9224 left *= CANON_X_UNIT (f);
9225 left += FRAME_INTERNAL_BORDER_WIDTH (f);
9226
9227 /* Compute the width of the scroll bar which might be less than
9228 the width of the area reserved for the scroll bar. */
9229 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
9230 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
9231 else
9232 sb_width = width;
9233
9234 /* Compute the left edge of the scroll bar. */
9235 #ifdef USE_TOOLKIT_SCROLL_BARS
9236 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9237 sb_left = left + width - sb_width - (width - sb_width) / 2;
9238 else
9239 sb_left = left + (width - sb_width) / 2;
9240 #else
9241 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9242 sb_left = left + width - sb_width;
9243 else
9244 sb_left = left;
9245 #endif
9246
9247 /* Does the scroll bar exist yet? */
9248 if (NILP (w->vertical_scroll_bar))
9249 {
9250 BLOCK_INPUT;
9251 if (width && height)
9252 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9253 left, top, width, height, False);
9254 UNBLOCK_INPUT;
9255 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
9256 }
9257 else
9258 {
9259 /* It may just need to be moved and resized. */
9260 unsigned int mask = 0;
9261
9262 bar = XSCROLL_BAR (w->vertical_scroll_bar);
9263
9264 BLOCK_INPUT;
9265
9266 if (sb_left != XINT (bar->left))
9267 mask |= CWX;
9268 if (top != XINT (bar->top))
9269 mask |= CWY;
9270 if (sb_width != XINT (bar->width))
9271 mask |= CWWidth;
9272 if (height != XINT (bar->height))
9273 mask |= CWHeight;
9274
9275 #ifdef USE_TOOLKIT_SCROLL_BARS
9276
9277 /* Since toolkit scroll bars are smaller than the space reserved
9278 for them on the frame, we have to clear "under" them. */
9279 if (width && height)
9280 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9281 left, top, width, height, False);
9282
9283 /* Move/size the scroll bar widget. */
9284 if (mask)
9285 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
9286 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9287 top,
9288 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9289 max (height, 1), 0);
9290
9291 #else /* not USE_TOOLKIT_SCROLL_BARS */
9292
9293 /* Clear areas not covered by the scroll bar because of
9294 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
9295 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
9296 {
9297 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9298 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9299 height, False);
9300 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9301 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9302 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9303 height, False);
9304 }
9305
9306 /* Clear areas not covered by the scroll bar because it's not as
9307 wide as the area reserved for it . This makes sure a
9308 previous mode line display is cleared after C-x 2 C-x 1, for
9309 example. */
9310 {
9311 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9312 int rest = area_width - sb_width;
9313 if (rest > 0)
9314 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9315 left + area_width - rest, 0,
9316 rest, max (height, 1), False);
9317 }
9318
9319 /* Move/size the scroll bar window. */
9320 if (mask)
9321 {
9322 XWindowChanges wc;
9323
9324 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9325 wc.y = top;
9326 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
9327 wc.height = height;
9328 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
9329 mask, &wc);
9330 }
9331
9332 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9333
9334 /* Remember new settings. */
9335 XSETINT (bar->left, sb_left);
9336 XSETINT (bar->top, top);
9337 XSETINT (bar->width, sb_width);
9338 XSETINT (bar->height, height);
9339
9340 UNBLOCK_INPUT;
9341 }
9342
9343 #ifdef USE_TOOLKIT_SCROLL_BARS
9344 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
9345 #else /* not USE_TOOLKIT_SCROLL_BARS */
9346 /* Set the scroll bar's current state, unless we're currently being
9347 dragged. */
9348 if (NILP (bar->dragging))
9349 {
9350 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
9351
9352 if (whole == 0)
9353 x_scroll_bar_set_handle (bar, 0, top_range, 0);
9354 else
9355 {
9356 int start = ((double) position * top_range) / whole;
9357 int end = ((double) (position + portion) * top_range) / whole;
9358 x_scroll_bar_set_handle (bar, start, end, 0);
9359 }
9360 }
9361 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9362
9363 XSETVECTOR (w->vertical_scroll_bar, bar);
9364 }
9365
9366
9367 /* The following three hooks are used when we're doing a thorough
9368 redisplay of the frame. We don't explicitly know which scroll bars
9369 are going to be deleted, because keeping track of when windows go
9370 away is a real pain - "Can you say set-window-configuration, boys
9371 and girls?" Instead, we just assert at the beginning of redisplay
9372 that *all* scroll bars are to be removed, and then save a scroll bar
9373 from the fiery pit when we actually redisplay its window. */
9374
9375 /* Arrange for all scroll bars on FRAME to be removed at the next call
9376 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
9377 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
9378
9379 static void
9380 XTcondemn_scroll_bars (frame)
9381 FRAME_PTR frame;
9382 {
9383 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
9384 while (! NILP (FRAME_SCROLL_BARS (frame)))
9385 {
9386 Lisp_Object bar;
9387 bar = FRAME_SCROLL_BARS (frame);
9388 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
9389 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
9390 XSCROLL_BAR (bar)->prev = Qnil;
9391 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
9392 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
9393 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
9394 }
9395 }
9396
9397
9398 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
9399 Note that WINDOW isn't necessarily condemned at all. */
9400
9401 static void
9402 XTredeem_scroll_bar (window)
9403 struct window *window;
9404 {
9405 struct scroll_bar *bar;
9406 struct frame *f;
9407
9408 /* We can't redeem this window's scroll bar if it doesn't have one. */
9409 if (NILP (window->vertical_scroll_bar))
9410 abort ();
9411
9412 bar = XSCROLL_BAR (window->vertical_scroll_bar);
9413
9414 /* Unlink it from the condemned list. */
9415 f = XFRAME (WINDOW_FRAME (window));
9416 if (NILP (bar->prev))
9417 {
9418 /* If the prev pointer is nil, it must be the first in one of
9419 the lists. */
9420 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
9421 /* It's not condemned. Everything's fine. */
9422 return;
9423 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
9424 window->vertical_scroll_bar))
9425 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
9426 else
9427 /* If its prev pointer is nil, it must be at the front of
9428 one or the other! */
9429 abort ();
9430 }
9431 else
9432 XSCROLL_BAR (bar->prev)->next = bar->next;
9433
9434 if (! NILP (bar->next))
9435 XSCROLL_BAR (bar->next)->prev = bar->prev;
9436
9437 bar->next = FRAME_SCROLL_BARS (f);
9438 bar->prev = Qnil;
9439 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9440 if (! NILP (bar->next))
9441 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9442 }
9443
9444 /* Remove all scroll bars on FRAME that haven't been saved since the
9445 last call to `*condemn_scroll_bars_hook'. */
9446
9447 static void
9448 XTjudge_scroll_bars (f)
9449 FRAME_PTR f;
9450 {
9451 Lisp_Object bar, next;
9452
9453 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
9454
9455 /* Clear out the condemned list now so we won't try to process any
9456 more events on the hapless scroll bars. */
9457 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
9458
9459 for (; ! NILP (bar); bar = next)
9460 {
9461 struct scroll_bar *b = XSCROLL_BAR (bar);
9462
9463 x_scroll_bar_remove (b);
9464
9465 next = b->next;
9466 b->next = b->prev = Qnil;
9467 }
9468
9469 /* Now there should be no references to the condemned scroll bars,
9470 and they should get garbage-collected. */
9471 }
9472
9473
9474 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
9475 is a no-op when using toolkit scroll bars.
9476
9477 This may be called from a signal handler, so we have to ignore GC
9478 mark bits. */
9479
9480 static void
9481 x_scroll_bar_expose (bar, event)
9482 struct scroll_bar *bar;
9483 XEvent *event;
9484 {
9485 #ifndef USE_TOOLKIT_SCROLL_BARS
9486
9487 Window w = SCROLL_BAR_X_WINDOW (bar);
9488 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9489 GC gc = f->output_data.x->normal_gc;
9490 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9491
9492 BLOCK_INPUT;
9493
9494 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
9495
9496 /* Draw a one-pixel border just inside the edges of the scroll bar. */
9497 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
9498
9499 /* x, y, width, height */
9500 0, 0,
9501 XINT (bar->width) - 1 - width_trim - width_trim,
9502 XINT (bar->height) - 1);
9503
9504 UNBLOCK_INPUT;
9505
9506 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9507 }
9508
9509 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
9510 is set to something other than no_event, it is enqueued.
9511
9512 This may be called from a signal handler, so we have to ignore GC
9513 mark bits. */
9514
9515 #ifndef USE_TOOLKIT_SCROLL_BARS
9516
9517 static void
9518 x_scroll_bar_handle_click (bar, event, emacs_event)
9519 struct scroll_bar *bar;
9520 XEvent *event;
9521 struct input_event *emacs_event;
9522 {
9523 if (! GC_WINDOWP (bar->window))
9524 abort ();
9525
9526 emacs_event->kind = scroll_bar_click;
9527 emacs_event->code = event->xbutton.button - Button1;
9528 emacs_event->modifiers
9529 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
9530 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
9531 event->xbutton.state)
9532 | (event->type == ButtonRelease
9533 ? up_modifier
9534 : down_modifier));
9535 emacs_event->frame_or_window = bar->window;
9536 emacs_event->arg = Qnil;
9537 emacs_event->timestamp = event->xbutton.time;
9538 {
9539 #if 0
9540 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9541 int internal_height
9542 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9543 #endif
9544 int top_range
9545 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9546 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
9547
9548 if (y < 0) y = 0;
9549 if (y > top_range) y = top_range;
9550
9551 if (y < XINT (bar->start))
9552 emacs_event->part = scroll_bar_above_handle;
9553 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9554 emacs_event->part = scroll_bar_handle;
9555 else
9556 emacs_event->part = scroll_bar_below_handle;
9557
9558 /* Just because the user has clicked on the handle doesn't mean
9559 they want to drag it. Lisp code needs to be able to decide
9560 whether or not we're dragging. */
9561 #if 0
9562 /* If the user has just clicked on the handle, record where they're
9563 holding it. */
9564 if (event->type == ButtonPress
9565 && emacs_event->part == scroll_bar_handle)
9566 XSETINT (bar->dragging, y - XINT (bar->start));
9567 #endif
9568
9569 /* If the user has released the handle, set it to its final position. */
9570 if (event->type == ButtonRelease
9571 && ! NILP (bar->dragging))
9572 {
9573 int new_start = y - XINT (bar->dragging);
9574 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9575
9576 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9577 bar->dragging = Qnil;
9578 }
9579
9580 /* Same deal here as the other #if 0. */
9581 #if 0
9582 /* Clicks on the handle are always reported as occurring at the top of
9583 the handle. */
9584 if (emacs_event->part == scroll_bar_handle)
9585 emacs_event->x = bar->start;
9586 else
9587 XSETINT (emacs_event->x, y);
9588 #else
9589 XSETINT (emacs_event->x, y);
9590 #endif
9591
9592 XSETINT (emacs_event->y, top_range);
9593 }
9594 }
9595
9596 /* Handle some mouse motion while someone is dragging the scroll bar.
9597
9598 This may be called from a signal handler, so we have to ignore GC
9599 mark bits. */
9600
9601 static void
9602 x_scroll_bar_note_movement (bar, event)
9603 struct scroll_bar *bar;
9604 XEvent *event;
9605 {
9606 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9607
9608 last_mouse_movement_time = event->xmotion.time;
9609
9610 f->mouse_moved = 1;
9611 XSETVECTOR (last_mouse_scroll_bar, bar);
9612
9613 /* If we're dragging the bar, display it. */
9614 if (! GC_NILP (bar->dragging))
9615 {
9616 /* Where should the handle be now? */
9617 int new_start = event->xmotion.y - XINT (bar->dragging);
9618
9619 if (new_start != XINT (bar->start))
9620 {
9621 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9622
9623 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9624 }
9625 }
9626 }
9627
9628 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9629
9630 /* Return information to the user about the current position of the mouse
9631 on the scroll bar. */
9632
9633 static void
9634 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9635 FRAME_PTR *fp;
9636 Lisp_Object *bar_window;
9637 enum scroll_bar_part *part;
9638 Lisp_Object *x, *y;
9639 unsigned long *time;
9640 {
9641 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
9642 Window w = SCROLL_BAR_X_WINDOW (bar);
9643 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9644 int win_x, win_y;
9645 Window dummy_window;
9646 int dummy_coord;
9647 unsigned int dummy_mask;
9648
9649 BLOCK_INPUT;
9650
9651 /* Get the mouse's position relative to the scroll bar window, and
9652 report that. */
9653 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
9654
9655 /* Root, child, root x and root y. */
9656 &dummy_window, &dummy_window,
9657 &dummy_coord, &dummy_coord,
9658
9659 /* Position relative to scroll bar. */
9660 &win_x, &win_y,
9661
9662 /* Mouse buttons and modifier keys. */
9663 &dummy_mask))
9664 ;
9665 else
9666 {
9667 #if 0
9668 int inside_height
9669 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9670 #endif
9671 int top_range
9672 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9673
9674 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9675
9676 if (! NILP (bar->dragging))
9677 win_y -= XINT (bar->dragging);
9678
9679 if (win_y < 0)
9680 win_y = 0;
9681 if (win_y > top_range)
9682 win_y = top_range;
9683
9684 *fp = f;
9685 *bar_window = bar->window;
9686
9687 if (! NILP (bar->dragging))
9688 *part = scroll_bar_handle;
9689 else if (win_y < XINT (bar->start))
9690 *part = scroll_bar_above_handle;
9691 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9692 *part = scroll_bar_handle;
9693 else
9694 *part = scroll_bar_below_handle;
9695
9696 XSETINT (*x, win_y);
9697 XSETINT (*y, top_range);
9698
9699 f->mouse_moved = 0;
9700 last_mouse_scroll_bar = Qnil;
9701 }
9702
9703 *time = last_mouse_movement_time;
9704
9705 UNBLOCK_INPUT;
9706 }
9707
9708
9709 /* The screen has been cleared so we may have changed foreground or
9710 background colors, and the scroll bars may need to be redrawn.
9711 Clear out the scroll bars, and ask for expose events, so we can
9712 redraw them. */
9713
9714 void
9715 x_scroll_bar_clear (f)
9716 FRAME_PTR f;
9717 {
9718 #ifndef USE_TOOLKIT_SCROLL_BARS
9719 Lisp_Object bar;
9720
9721 /* We can have scroll bars even if this is 0,
9722 if we just turned off scroll bar mode.
9723 But in that case we should not clear them. */
9724 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9725 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9726 bar = XSCROLL_BAR (bar)->next)
9727 XClearArea (FRAME_X_DISPLAY (f),
9728 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9729 0, 0, 0, 0, True);
9730 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9731 }
9732
9733 /* This processes Expose events from the menu-bar specific X event
9734 loop in xmenu.c. This allows to redisplay the frame if necessary
9735 when handling menu-bar or pop-up items. */
9736
9737 int
9738 process_expose_from_menu (event)
9739 XEvent event;
9740 {
9741 FRAME_PTR f;
9742 struct x_display_info *dpyinfo;
9743 int frame_exposed_p = 0;
9744
9745 BLOCK_INPUT;
9746
9747 dpyinfo = x_display_info_for_display (event.xexpose.display);
9748 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9749 if (f)
9750 {
9751 if (f->async_visible == 0)
9752 {
9753 f->async_visible = 1;
9754 f->async_iconified = 0;
9755 f->output_data.x->has_been_visible = 1;
9756 SET_FRAME_GARBAGED (f);
9757 }
9758 else
9759 {
9760 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9761 event.xexpose.x, event.xexpose.y,
9762 event.xexpose.width, event.xexpose.height);
9763 frame_exposed_p = 1;
9764 }
9765 }
9766 else
9767 {
9768 struct scroll_bar *bar
9769 = x_window_to_scroll_bar (event.xexpose.window);
9770
9771 if (bar)
9772 x_scroll_bar_expose (bar, &event);
9773 }
9774
9775 UNBLOCK_INPUT;
9776 return frame_exposed_p;
9777 }
9778 \f
9779 /* Define a queue to save up SelectionRequest events for later handling. */
9780
9781 struct selection_event_queue
9782 {
9783 XEvent event;
9784 struct selection_event_queue *next;
9785 };
9786
9787 static struct selection_event_queue *queue;
9788
9789 /* Nonzero means queue up certain events--don't process them yet. */
9790
9791 static int x_queue_selection_requests;
9792
9793 /* Queue up an X event *EVENT, to be processed later. */
9794
9795 static void
9796 x_queue_event (f, event)
9797 FRAME_PTR f;
9798 XEvent *event;
9799 {
9800 struct selection_event_queue *queue_tmp
9801 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
9802
9803 if (queue_tmp != NULL)
9804 {
9805 queue_tmp->event = *event;
9806 queue_tmp->next = queue;
9807 queue = queue_tmp;
9808 }
9809 }
9810
9811 /* Take all the queued events and put them back
9812 so that they get processed afresh. */
9813
9814 static void
9815 x_unqueue_events (display)
9816 Display *display;
9817 {
9818 while (queue != NULL)
9819 {
9820 struct selection_event_queue *queue_tmp = queue;
9821 XPutBackEvent (display, &queue_tmp->event);
9822 queue = queue_tmp->next;
9823 xfree ((char *)queue_tmp);
9824 }
9825 }
9826
9827 /* Start queuing SelectionRequest events. */
9828
9829 void
9830 x_start_queuing_selection_requests (display)
9831 Display *display;
9832 {
9833 x_queue_selection_requests++;
9834 }
9835
9836 /* Stop queuing SelectionRequest events. */
9837
9838 void
9839 x_stop_queuing_selection_requests (display)
9840 Display *display;
9841 {
9842 x_queue_selection_requests--;
9843 x_unqueue_events (display);
9844 }
9845 \f
9846 /* The main X event-reading loop - XTread_socket. */
9847
9848 /* Time stamp of enter window event. This is only used by XTread_socket,
9849 but we have to put it out here, since static variables within functions
9850 sometimes don't work. */
9851
9852 static Time enter_timestamp;
9853
9854 /* This holds the state XLookupString needs to implement dead keys
9855 and other tricks known as "compose processing". _X Window System_
9856 says that a portable program can't use this, but Stephen Gildea assures
9857 me that letting the compiler initialize it to zeros will work okay.
9858
9859 This must be defined outside of XTread_socket, for the same reasons
9860 given for enter_time stamp, above. */
9861
9862 static XComposeStatus compose_status;
9863
9864 /* Record the last 100 characters stored
9865 to help debug the loss-of-chars-during-GC problem. */
9866
9867 static int temp_index;
9868 static short temp_buffer[100];
9869
9870 /* Set this to nonzero to fake an "X I/O error"
9871 on a particular display. */
9872
9873 struct x_display_info *XTread_socket_fake_io_error;
9874
9875 /* When we find no input here, we occasionally do a no-op command
9876 to verify that the X server is still running and we can still talk with it.
9877 We try all the open displays, one by one.
9878 This variable is used for cycling thru the displays. */
9879
9880 static struct x_display_info *next_noop_dpyinfo;
9881
9882 #define SET_SAVED_MENU_EVENT(size) \
9883 do \
9884 { \
9885 if (f->output_data.x->saved_menu_event == 0) \
9886 f->output_data.x->saved_menu_event \
9887 = (XEvent *) xmalloc (sizeof (XEvent)); \
9888 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9889 if (numchars >= 1) \
9890 { \
9891 bufp->kind = menu_bar_activate_event; \
9892 XSETFRAME (bufp->frame_or_window, f); \
9893 bufp->arg = Qnil; \
9894 bufp++; \
9895 count++; \
9896 numchars--; \
9897 } \
9898 } \
9899 while (0)
9900
9901 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9902 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9903
9904 /* Read events coming from the X server.
9905 This routine is called by the SIGIO handler.
9906 We return as soon as there are no more events to be read.
9907
9908 Events representing keys are stored in buffer BUFP,
9909 which can hold up to NUMCHARS characters.
9910 We return the number of characters stored into the buffer,
9911 thus pretending to be `read'.
9912
9913 EXPECTED is nonzero if the caller knows input is available. */
9914
9915 int
9916 XTread_socket (sd, bufp, numchars, expected)
9917 register int sd;
9918 /* register */ struct input_event *bufp;
9919 /* register */ int numchars;
9920 int expected;
9921 {
9922 int count = 0;
9923 int nbytes = 0;
9924 XEvent event;
9925 struct frame *f;
9926 int event_found = 0;
9927 struct x_display_info *dpyinfo;
9928 struct coding_system coding;
9929
9930 if (interrupt_input_blocked)
9931 {
9932 interrupt_input_pending = 1;
9933 return -1;
9934 }
9935
9936 interrupt_input_pending = 0;
9937 BLOCK_INPUT;
9938
9939 /* So people can tell when we have read the available input. */
9940 input_signal_count++;
9941
9942 if (numchars <= 0)
9943 abort (); /* Don't think this happens. */
9944
9945 ++handling_signal;
9946
9947 /* The input should be decoded if it is from XIM. Currently the
9948 locale of XIM is the same as that of the system. So, we can use
9949 Vlocale_coding_system which is initialized properly at Emacs
9950 startup time. */
9951 setup_coding_system (Vlocale_coding_system, &coding);
9952 coding.src_multibyte = 0;
9953 coding.dst_multibyte = 1;
9954 /* The input is converted to events, thus we can't handle
9955 composition. Anyway, there's no XIM that gives us composition
9956 information. */
9957 coding.composing = COMPOSITION_DISABLED;
9958
9959 /* Find the display we are supposed to read input for.
9960 It's the one communicating on descriptor SD. */
9961 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9962 {
9963 #if 0 /* This ought to be unnecessary; let's verify it. */
9964 #ifdef FIOSNBIO
9965 /* If available, Xlib uses FIOSNBIO to make the socket
9966 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
9967 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
9968 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
9969 fcntl (dpyinfo->connection, F_SETFL, 0);
9970 #endif /* ! defined (FIOSNBIO) */
9971 #endif
9972
9973 #if 0 /* This code can't be made to work, with multiple displays,
9974 and appears not to be used on any system any more.
9975 Also keyboard.c doesn't turn O_NDELAY on and off
9976 for X connections. */
9977 #ifndef SIGIO
9978 #ifndef HAVE_SELECT
9979 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9980 {
9981 extern int read_alarm_should_throw;
9982 read_alarm_should_throw = 1;
9983 XPeekEvent (dpyinfo->display, &event);
9984 read_alarm_should_throw = 0;
9985 }
9986 #endif /* HAVE_SELECT */
9987 #endif /* SIGIO */
9988 #endif
9989
9990 /* For debugging, this gives a way to fake an I/O error. */
9991 if (dpyinfo == XTread_socket_fake_io_error)
9992 {
9993 XTread_socket_fake_io_error = 0;
9994 x_io_error_quitter (dpyinfo->display);
9995 }
9996
9997 while (XPending (dpyinfo->display))
9998 {
9999 XNextEvent (dpyinfo->display, &event);
10000
10001 #ifdef HAVE_X_I18N
10002 {
10003 /* Filter events for the current X input method.
10004 XFilterEvent returns non-zero if the input method has
10005 consumed the event. We pass the frame's X window to
10006 XFilterEvent because that's the one for which the IC
10007 was created. */
10008 struct frame *f1 = x_any_window_to_frame (dpyinfo,
10009 event.xclient.window);
10010 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
10011 break;
10012 }
10013 #endif
10014 event_found = 1;
10015
10016 switch (event.type)
10017 {
10018 case ClientMessage:
10019 {
10020 if (event.xclient.message_type
10021 == dpyinfo->Xatom_wm_protocols
10022 && event.xclient.format == 32)
10023 {
10024 if (event.xclient.data.l[0]
10025 == dpyinfo->Xatom_wm_take_focus)
10026 {
10027 /* Use x_any_window_to_frame because this
10028 could be the shell widget window
10029 if the frame has no title bar. */
10030 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
10031 #ifdef HAVE_X_I18N
10032 /* Not quite sure this is needed -pd */
10033 if (f && FRAME_XIC (f))
10034 XSetICFocus (FRAME_XIC (f));
10035 #endif
10036 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
10037 instructs the WM to set the input focus automatically for
10038 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
10039 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
10040 it has set the focus. So, XSetInputFocus below is not
10041 needed.
10042
10043 The call to XSetInputFocus below has also caused trouble. In
10044 cases where the XSetInputFocus done by the WM and the one
10045 below are temporally close (on a fast machine), the call
10046 below can generate additional FocusIn events which confuse
10047 Emacs. */
10048
10049 /* Since we set WM_TAKE_FOCUS, we must call
10050 XSetInputFocus explicitly. But not if f is null,
10051 since that might be an event for a deleted frame. */
10052 if (f)
10053 {
10054 Display *d = event.xclient.display;
10055 /* Catch and ignore errors, in case window has been
10056 iconified by a window manager such as GWM. */
10057 int count = x_catch_errors (d);
10058 XSetInputFocus (d, event.xclient.window,
10059 /* The ICCCM says this is
10060 the only valid choice. */
10061 RevertToParent,
10062 event.xclient.data.l[1]);
10063 /* This is needed to detect the error
10064 if there is an error. */
10065 XSync (d, False);
10066 x_uncatch_errors (d, count);
10067 }
10068 /* Not certain about handling scroll bars here */
10069 #endif /* 0 */
10070 }
10071 else if (event.xclient.data.l[0]
10072 == dpyinfo->Xatom_wm_save_yourself)
10073 {
10074 /* Save state modify the WM_COMMAND property to
10075 something which can reinstate us. This notifies
10076 the session manager, who's looking for such a
10077 PropertyNotify. Can restart processing when
10078 a keyboard or mouse event arrives. */
10079 if (numchars > 0)
10080 {
10081 f = x_top_window_to_frame (dpyinfo,
10082 event.xclient.window);
10083
10084 /* This is just so we only give real data once
10085 for a single Emacs process. */
10086 if (f == SELECTED_FRAME ())
10087 XSetCommand (FRAME_X_DISPLAY (f),
10088 event.xclient.window,
10089 initial_argv, initial_argc);
10090 else if (f)
10091 XSetCommand (FRAME_X_DISPLAY (f),
10092 event.xclient.window,
10093 0, 0);
10094 }
10095 }
10096 else if (event.xclient.data.l[0]
10097 == dpyinfo->Xatom_wm_delete_window)
10098 {
10099 struct frame *f
10100 = x_any_window_to_frame (dpyinfo,
10101 event.xclient.window);
10102
10103 if (f)
10104 {
10105 if (numchars == 0)
10106 abort ();
10107
10108 bufp->kind = delete_window_event;
10109 XSETFRAME (bufp->frame_or_window, f);
10110 bufp->arg = Qnil;
10111 bufp++;
10112
10113 count += 1;
10114 numchars -= 1;
10115 }
10116 }
10117 }
10118 else if (event.xclient.message_type
10119 == dpyinfo->Xatom_wm_configure_denied)
10120 {
10121 }
10122 else if (event.xclient.message_type
10123 == dpyinfo->Xatom_wm_window_moved)
10124 {
10125 int new_x, new_y;
10126 struct frame *f
10127 = x_window_to_frame (dpyinfo, event.xclient.window);
10128
10129 new_x = event.xclient.data.s[0];
10130 new_y = event.xclient.data.s[1];
10131
10132 if (f)
10133 {
10134 f->output_data.x->left_pos = new_x;
10135 f->output_data.x->top_pos = new_y;
10136 }
10137 }
10138 #ifdef HACK_EDITRES
10139 else if (event.xclient.message_type
10140 == dpyinfo->Xatom_editres)
10141 {
10142 struct frame *f
10143 = x_any_window_to_frame (dpyinfo, event.xclient.window);
10144 _XEditResCheckMessages (f->output_data.x->widget, NULL,
10145 &event, NULL);
10146 }
10147 #endif /* HACK_EDITRES */
10148 else if ((event.xclient.message_type
10149 == dpyinfo->Xatom_DONE)
10150 || (event.xclient.message_type
10151 == dpyinfo->Xatom_PAGE))
10152 {
10153 /* Ghostview job completed. Kill it. We could
10154 reply with "Next" if we received "Page", but we
10155 currently never do because we are interested in
10156 images, only, which should have 1 page. */
10157 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
10158 struct frame *f
10159 = x_window_to_frame (dpyinfo, event.xclient.window);
10160 x_kill_gs_process (pixmap, f);
10161 expose_frame (f, 0, 0, 0, 0);
10162 }
10163 #ifdef USE_TOOLKIT_SCROLL_BARS
10164 /* Scroll bar callbacks send a ClientMessage from which
10165 we construct an input_event. */
10166 else if (event.xclient.message_type
10167 == dpyinfo->Xatom_Scrollbar)
10168 {
10169 x_scroll_bar_to_input_event (&event, bufp);
10170 ++bufp, ++count, --numchars;
10171 goto out;
10172 }
10173 #endif /* USE_TOOLKIT_SCROLL_BARS */
10174 else
10175 goto OTHER;
10176 }
10177 break;
10178
10179 case SelectionNotify:
10180 #ifdef USE_X_TOOLKIT
10181 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
10182 goto OTHER;
10183 #endif /* not USE_X_TOOLKIT */
10184 x_handle_selection_notify (&event.xselection);
10185 break;
10186
10187 case SelectionClear: /* Someone has grabbed ownership. */
10188 #ifdef USE_X_TOOLKIT
10189 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
10190 goto OTHER;
10191 #endif /* USE_X_TOOLKIT */
10192 {
10193 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
10194
10195 if (numchars == 0)
10196 abort ();
10197
10198 bufp->kind = selection_clear_event;
10199 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10200 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10201 SELECTION_EVENT_TIME (bufp) = eventp->time;
10202 bufp->frame_or_window = Qnil;
10203 bufp->arg = Qnil;
10204 bufp++;
10205
10206 count += 1;
10207 numchars -= 1;
10208 }
10209 break;
10210
10211 case SelectionRequest: /* Someone wants our selection. */
10212 #ifdef USE_X_TOOLKIT
10213 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
10214 goto OTHER;
10215 #endif /* USE_X_TOOLKIT */
10216 if (x_queue_selection_requests)
10217 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
10218 &event);
10219 else
10220 {
10221 XSelectionRequestEvent *eventp
10222 = (XSelectionRequestEvent *) &event;
10223
10224 if (numchars == 0)
10225 abort ();
10226
10227 bufp->kind = selection_request_event;
10228 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10229 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
10230 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10231 SELECTION_EVENT_TARGET (bufp) = eventp->target;
10232 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
10233 SELECTION_EVENT_TIME (bufp) = eventp->time;
10234 bufp->frame_or_window = Qnil;
10235 bufp->arg = Qnil;
10236 bufp++;
10237
10238 count += 1;
10239 numchars -= 1;
10240 }
10241 break;
10242
10243 case PropertyNotify:
10244 #if 0 /* This is plain wrong. In the case that we are waiting for a
10245 PropertyNotify used as an ACK in incremental selection
10246 transfer, the property will be on the receiver's window. */
10247 #if defined USE_X_TOOLKIT
10248 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
10249 goto OTHER;
10250 #endif
10251 #endif
10252 x_handle_property_notify (&event.xproperty);
10253 goto OTHER;
10254
10255 case ReparentNotify:
10256 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
10257 if (f)
10258 {
10259 int x, y;
10260 f->output_data.x->parent_desc = event.xreparent.parent;
10261 x_real_positions (f, &x, &y);
10262 f->output_data.x->left_pos = x;
10263 f->output_data.x->top_pos = y;
10264 }
10265 break;
10266
10267 case Expose:
10268 f = x_window_to_frame (dpyinfo, event.xexpose.window);
10269 if (f)
10270 {
10271 if (f->async_visible == 0)
10272 {
10273 f->async_visible = 1;
10274 f->async_iconified = 0;
10275 f->output_data.x->has_been_visible = 1;
10276 SET_FRAME_GARBAGED (f);
10277 }
10278 else
10279 expose_frame (x_window_to_frame (dpyinfo,
10280 event.xexpose.window),
10281 event.xexpose.x, event.xexpose.y,
10282 event.xexpose.width, event.xexpose.height);
10283 }
10284 else
10285 {
10286 #ifndef USE_TOOLKIT_SCROLL_BARS
10287 struct scroll_bar *bar;
10288 #endif
10289 #if defined USE_LUCID
10290 /* Submenus of the Lucid menu bar aren't widgets
10291 themselves, so there's no way to dispatch events
10292 to them. Recognize this case separately. */
10293 {
10294 Widget widget
10295 = x_window_to_menu_bar (event.xexpose.window);
10296 if (widget)
10297 xlwmenu_redisplay (widget);
10298 }
10299 #endif /* USE_LUCID */
10300
10301 #ifdef USE_TOOLKIT_SCROLL_BARS
10302 /* Dispatch event to the widget. */
10303 goto OTHER;
10304 #else /* not USE_TOOLKIT_SCROLL_BARS */
10305 bar = x_window_to_scroll_bar (event.xexpose.window);
10306
10307 if (bar)
10308 x_scroll_bar_expose (bar, &event);
10309 #ifdef USE_X_TOOLKIT
10310 else
10311 goto OTHER;
10312 #endif /* USE_X_TOOLKIT */
10313 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10314 }
10315 break;
10316
10317 case GraphicsExpose: /* This occurs when an XCopyArea's
10318 source area was obscured or not
10319 available.*/
10320 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
10321 if (f)
10322 {
10323 expose_frame (f,
10324 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
10325 event.xgraphicsexpose.width,
10326 event.xgraphicsexpose.height);
10327 }
10328 #ifdef USE_X_TOOLKIT
10329 else
10330 goto OTHER;
10331 #endif /* USE_X_TOOLKIT */
10332 break;
10333
10334 case NoExpose: /* This occurs when an XCopyArea's
10335 source area was completely
10336 available */
10337 break;
10338
10339 case UnmapNotify:
10340 /* Redo the mouse-highlight after the tooltip has gone. */
10341 if (event.xmap.window == tip_window)
10342 {
10343 tip_window = 0;
10344 redo_mouse_highlight ();
10345 }
10346
10347 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
10348 if (f) /* F may no longer exist if
10349 the frame was deleted. */
10350 {
10351 /* While a frame is unmapped, display generation is
10352 disabled; you don't want to spend time updating a
10353 display that won't ever be seen. */
10354 f->async_visible = 0;
10355 /* We can't distinguish, from the event, whether the window
10356 has become iconified or invisible. So assume, if it
10357 was previously visible, than now it is iconified.
10358 But x_make_frame_invisible clears both
10359 the visible flag and the iconified flag;
10360 and that way, we know the window is not iconified now. */
10361 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
10362 {
10363 f->async_iconified = 1;
10364
10365 bufp->kind = iconify_event;
10366 XSETFRAME (bufp->frame_or_window, f);
10367 bufp->arg = Qnil;
10368 bufp++;
10369 count++;
10370 numchars--;
10371 }
10372 }
10373 goto OTHER;
10374
10375 case MapNotify:
10376 if (event.xmap.window == tip_window)
10377 /* The tooltip has been drawn already. Avoid
10378 the SET_FRAME_GARBAGED below. */
10379 goto OTHER;
10380
10381 /* We use x_top_window_to_frame because map events can
10382 come for sub-windows and they don't mean that the
10383 frame is visible. */
10384 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
10385 if (f)
10386 {
10387 f->async_visible = 1;
10388 f->async_iconified = 0;
10389 f->output_data.x->has_been_visible = 1;
10390
10391 /* wait_reading_process_input will notice this and update
10392 the frame's display structures. */
10393 SET_FRAME_GARBAGED (f);
10394
10395 if (f->iconified)
10396 {
10397 bufp->kind = deiconify_event;
10398 XSETFRAME (bufp->frame_or_window, f);
10399 bufp->arg = Qnil;
10400 bufp++;
10401 count++;
10402 numchars--;
10403 }
10404 else if (! NILP (Vframe_list)
10405 && ! NILP (XCDR (Vframe_list)))
10406 /* Force a redisplay sooner or later
10407 to update the frame titles
10408 in case this is the second frame. */
10409 record_asynch_buffer_change ();
10410 }
10411 goto OTHER;
10412
10413 case KeyPress:
10414 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
10415
10416 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
10417 if (f == 0)
10418 {
10419 /* Scroll bars consume key events, but we want
10420 the keys to go to the scroll bar's frame. */
10421 Widget widget = XtWindowToWidget (dpyinfo->display,
10422 event.xkey.window);
10423 if (widget && XmIsScrollBar (widget))
10424 {
10425 widget = XtParent (widget);
10426 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
10427 }
10428 }
10429 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
10430
10431 if (f != 0)
10432 {
10433 KeySym keysym, orig_keysym;
10434 /* al%imercury@uunet.uu.net says that making this 81
10435 instead of 80 fixed a bug whereby meta chars made
10436 his Emacs hang.
10437
10438 It seems that some version of XmbLookupString has
10439 a bug of not returning XBufferOverflow in
10440 status_return even if the input is too long to
10441 fit in 81 bytes. So, we must prepare sufficient
10442 bytes for copy_buffer. 513 bytes (256 chars for
10443 two-byte character set) seems to be a faily good
10444 approximation. -- 2000.8.10 handa@etl.go.jp */
10445 unsigned char copy_buffer[513];
10446 unsigned char *copy_bufptr = copy_buffer;
10447 int copy_bufsiz = sizeof (copy_buffer);
10448 int modifiers;
10449
10450 event.xkey.state
10451 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
10452 extra_keyboard_modifiers);
10453 modifiers = event.xkey.state;
10454
10455 /* This will have to go some day... */
10456
10457 /* make_lispy_event turns chars into control chars.
10458 Don't do it here because XLookupString is too eager. */
10459 event.xkey.state &= ~ControlMask;
10460 event.xkey.state &= ~(dpyinfo->meta_mod_mask
10461 | dpyinfo->super_mod_mask
10462 | dpyinfo->hyper_mod_mask
10463 | dpyinfo->alt_mod_mask);
10464
10465 /* In case Meta is ComposeCharacter,
10466 clear its status. According to Markus Ehrnsperger
10467 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
10468 this enables ComposeCharacter to work whether or
10469 not it is combined with Meta. */
10470 if (modifiers & dpyinfo->meta_mod_mask)
10471 bzero (&compose_status, sizeof (compose_status));
10472
10473 #ifdef HAVE_X_I18N
10474 if (FRAME_XIC (f))
10475 {
10476 Status status_return;
10477
10478 nbytes = XmbLookupString (FRAME_XIC (f),
10479 &event.xkey, copy_bufptr,
10480 copy_bufsiz, &keysym,
10481 &status_return);
10482 if (status_return == XBufferOverflow)
10483 {
10484 copy_bufsiz = nbytes + 1;
10485 copy_bufptr = (char *) alloca (copy_bufsiz);
10486 nbytes = XmbLookupString (FRAME_XIC (f),
10487 &event.xkey, copy_bufptr,
10488 copy_bufsiz, &keysym,
10489 &status_return);
10490 }
10491
10492 if (status_return == XLookupNone)
10493 break;
10494 else if (status_return == XLookupChars)
10495 {
10496 keysym = NoSymbol;
10497 modifiers = 0;
10498 }
10499 else if (status_return != XLookupKeySym
10500 && status_return != XLookupBoth)
10501 abort ();
10502 }
10503 else
10504 nbytes = XLookupString (&event.xkey, copy_bufptr,
10505 copy_bufsiz, &keysym,
10506 &compose_status);
10507 #else
10508 nbytes = XLookupString (&event.xkey, copy_bufptr,
10509 copy_bufsiz, &keysym,
10510 &compose_status);
10511 #endif
10512
10513 orig_keysym = keysym;
10514
10515 if (numchars > 1)
10516 {
10517 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
10518 || keysym == XK_Delete
10519 #ifdef XK_ISO_Left_Tab
10520 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
10521 #endif
10522 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
10523 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
10524 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
10525 #ifdef HPUX
10526 /* This recognizes the "extended function keys".
10527 It seems there's no cleaner way.
10528 Test IsModifierKey to avoid handling mode_switch
10529 incorrectly. */
10530 || ((unsigned) (keysym) >= XK_Select
10531 && (unsigned)(keysym) < XK_KP_Space)
10532 #endif
10533 #ifdef XK_dead_circumflex
10534 || orig_keysym == XK_dead_circumflex
10535 #endif
10536 #ifdef XK_dead_grave
10537 || orig_keysym == XK_dead_grave
10538 #endif
10539 #ifdef XK_dead_tilde
10540 || orig_keysym == XK_dead_tilde
10541 #endif
10542 #ifdef XK_dead_diaeresis
10543 || orig_keysym == XK_dead_diaeresis
10544 #endif
10545 #ifdef XK_dead_macron
10546 || orig_keysym == XK_dead_macron
10547 #endif
10548 #ifdef XK_dead_degree
10549 || orig_keysym == XK_dead_degree
10550 #endif
10551 #ifdef XK_dead_acute
10552 || orig_keysym == XK_dead_acute
10553 #endif
10554 #ifdef XK_dead_cedilla
10555 || orig_keysym == XK_dead_cedilla
10556 #endif
10557 #ifdef XK_dead_breve
10558 || orig_keysym == XK_dead_breve
10559 #endif
10560 #ifdef XK_dead_ogonek
10561 || orig_keysym == XK_dead_ogonek
10562 #endif
10563 #ifdef XK_dead_caron
10564 || orig_keysym == XK_dead_caron
10565 #endif
10566 #ifdef XK_dead_doubleacute
10567 || orig_keysym == XK_dead_doubleacute
10568 #endif
10569 #ifdef XK_dead_abovedot
10570 || orig_keysym == XK_dead_abovedot
10571 #endif
10572 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10573 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10574 /* Any "vendor-specific" key is ok. */
10575 || (orig_keysym & (1 << 28))
10576 || (keysym != NoSymbol && nbytes == 0))
10577 && ! (IsModifierKey (orig_keysym)
10578 #ifndef HAVE_X11R5
10579 #ifdef XK_Mode_switch
10580 || ((unsigned)(orig_keysym) == XK_Mode_switch)
10581 #endif
10582 #ifdef XK_Num_Lock
10583 || ((unsigned)(orig_keysym) == XK_Num_Lock)
10584 #endif
10585 #endif /* not HAVE_X11R5 */
10586 ))
10587 {
10588 if (temp_index == sizeof temp_buffer / sizeof (short))
10589 temp_index = 0;
10590 temp_buffer[temp_index++] = keysym;
10591 bufp->kind = non_ascii_keystroke;
10592 bufp->code = keysym;
10593 XSETFRAME (bufp->frame_or_window, f);
10594 bufp->arg = Qnil;
10595 bufp->modifiers
10596 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10597 modifiers);
10598 bufp->timestamp = event.xkey.time;
10599 bufp++;
10600 count++;
10601 numchars--;
10602 }
10603 else if (numchars > nbytes)
10604 {
10605 register int i;
10606 register int c;
10607 int nchars, len;
10608
10609 for (i = 0; i < nbytes; i++)
10610 {
10611 if (temp_index == (sizeof temp_buffer
10612 / sizeof (short)))
10613 temp_index = 0;
10614 temp_buffer[temp_index++] = copy_bufptr[i];
10615 }
10616
10617 if (/* If the event is not from XIM, */
10618 event.xkey.keycode != 0
10619 /* or the current locale doesn't request
10620 decoding of the intup data, ... */
10621 || coding.type == coding_type_raw_text
10622 || coding.type == coding_type_no_conversion)
10623 {
10624 /* ... we can use the input data as is. */
10625 nchars = nbytes;
10626 }
10627 else
10628 {
10629 /* We have to decode the input data. */
10630 int require;
10631 unsigned char *p;
10632
10633 require = decoding_buffer_size (&coding, nbytes);
10634 p = (unsigned char *) alloca (require);
10635 coding.mode |= CODING_MODE_LAST_BLOCK;
10636 decode_coding (&coding, copy_bufptr, p,
10637 nbytes, require);
10638 nbytes = coding.produced;
10639 nchars = coding.produced_char;
10640 copy_bufptr = p;
10641 }
10642
10643 /* Convert the input data to a sequence of
10644 character events. */
10645 for (i = 0; i < nbytes; i += len)
10646 {
10647 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10648 nbytes - i, len);
10649 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
10650 ? ascii_keystroke
10651 : multibyte_char_keystroke);
10652 bufp->code = c;
10653 XSETFRAME (bufp->frame_or_window, f);
10654 bufp->arg = Qnil;
10655 bufp->modifiers
10656 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10657 modifiers);
10658 bufp->timestamp = event.xkey.time;
10659 bufp++;
10660 }
10661
10662 count += nchars;
10663 numchars -= nchars;
10664
10665 if (keysym == NoSymbol)
10666 break;
10667 }
10668 else
10669 abort ();
10670 }
10671 else
10672 abort ();
10673 }
10674 #ifdef HAVE_X_I18N
10675 /* Don't dispatch this event since XtDispatchEvent calls
10676 XFilterEvent, and two calls in a row may freeze the
10677 client. */
10678 break;
10679 #else
10680 goto OTHER;
10681 #endif
10682
10683 case KeyRelease:
10684 #ifdef HAVE_X_I18N
10685 /* Don't dispatch this event since XtDispatchEvent calls
10686 XFilterEvent, and two calls in a row may freeze the
10687 client. */
10688 break;
10689 #else
10690 goto OTHER;
10691 #endif
10692
10693 /* Here's a possible interpretation of the whole
10694 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
10695 you get a FocusIn event, you have to get a FocusOut
10696 event before you relinquish the focus. If you
10697 haven't received a FocusIn event, then a mere
10698 LeaveNotify is enough to free you. */
10699
10700 case EnterNotify:
10701 {
10702 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10703
10704 if (event.xcrossing.focus)
10705 {
10706 /* Avoid nasty pop/raise loops. */
10707 if (f && (!(f->auto_raise)
10708 || !(f->auto_lower)
10709 || (event.xcrossing.time - enter_timestamp) > 500))
10710 {
10711 x_new_focus_frame (dpyinfo, f);
10712 enter_timestamp = event.xcrossing.time;
10713 }
10714 }
10715 else if (f == dpyinfo->x_focus_frame)
10716 x_new_focus_frame (dpyinfo, 0);
10717
10718 /* EnterNotify counts as mouse movement,
10719 so update things that depend on mouse position. */
10720 if (f && !f->output_data.x->hourglass_p)
10721 note_mouse_movement (f, &event.xmotion);
10722 goto OTHER;
10723 }
10724
10725 case FocusIn:
10726 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10727 if (event.xfocus.detail != NotifyPointer)
10728 dpyinfo->x_focus_event_frame = f;
10729 if (f)
10730 {
10731 x_new_focus_frame (dpyinfo, f);
10732
10733 /* Don't stop displaying the initial startup message
10734 for a switch-frame event we don't need. */
10735 if (GC_NILP (Vterminal_frame)
10736 && GC_CONSP (Vframe_list)
10737 && !GC_NILP (XCDR (Vframe_list)))
10738 {
10739 bufp->kind = FOCUS_IN_EVENT;
10740 XSETFRAME (bufp->frame_or_window, f);
10741 bufp->arg = Qnil;
10742 ++bufp, ++count, --numchars;
10743 }
10744 }
10745
10746 #ifdef HAVE_X_I18N
10747 if (f && FRAME_XIC (f))
10748 XSetICFocus (FRAME_XIC (f));
10749 #endif
10750
10751 goto OTHER;
10752
10753 case LeaveNotify:
10754 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
10755 if (f)
10756 {
10757 if (f == dpyinfo->mouse_face_mouse_frame)
10758 {
10759 /* If we move outside the frame, then we're
10760 certainly no longer on any text in the frame. */
10761 clear_mouse_face (dpyinfo);
10762 dpyinfo->mouse_face_mouse_frame = 0;
10763 }
10764
10765 /* Generate a nil HELP_EVENT to cancel a help-echo.
10766 Do it only if there's something to cancel.
10767 Otherwise, the startup message is cleared when
10768 the mouse leaves the frame. */
10769 if (any_help_event_p)
10770 {
10771 Lisp_Object frame;
10772 int n;
10773
10774 XSETFRAME (frame, f);
10775 help_echo = Qnil;
10776 n = gen_help_event (bufp, numchars,
10777 Qnil, frame, Qnil, Qnil, 0);
10778 bufp += n, count += n, numchars -= n;
10779 }
10780
10781 if (event.xcrossing.focus)
10782 x_mouse_leave (dpyinfo);
10783 else
10784 {
10785 if (f == dpyinfo->x_focus_event_frame)
10786 dpyinfo->x_focus_event_frame = 0;
10787 if (f == dpyinfo->x_focus_frame)
10788 x_new_focus_frame (dpyinfo, 0);
10789 }
10790 }
10791 goto OTHER;
10792
10793 case FocusOut:
10794 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10795 if (event.xfocus.detail != NotifyPointer
10796 && f == dpyinfo->x_focus_event_frame)
10797 dpyinfo->x_focus_event_frame = 0;
10798 if (f && f == dpyinfo->x_focus_frame)
10799 x_new_focus_frame (dpyinfo, 0);
10800
10801 #ifdef HAVE_X_I18N
10802 if (f && FRAME_XIC (f))
10803 XUnsetICFocus (FRAME_XIC (f));
10804 #endif
10805
10806 goto OTHER;
10807
10808 case MotionNotify:
10809 {
10810 previous_help_echo = help_echo;
10811 help_echo = help_echo_object = help_echo_window = Qnil;
10812 help_echo_pos = -1;
10813
10814 if (dpyinfo->grabbed && last_mouse_frame
10815 && FRAME_LIVE_P (last_mouse_frame))
10816 f = last_mouse_frame;
10817 else
10818 f = x_window_to_frame (dpyinfo, event.xmotion.window);
10819
10820 if (f)
10821 note_mouse_movement (f, &event.xmotion);
10822 else
10823 {
10824 #ifndef USE_TOOLKIT_SCROLL_BARS
10825 struct scroll_bar *bar
10826 = x_window_to_scroll_bar (event.xmotion.window);
10827
10828 if (bar)
10829 x_scroll_bar_note_movement (bar, &event);
10830 #endif /* USE_TOOLKIT_SCROLL_BARS */
10831
10832 /* If we move outside the frame, then we're
10833 certainly no longer on any text in the frame. */
10834 clear_mouse_face (dpyinfo);
10835 }
10836
10837 /* If the contents of the global variable help_echo
10838 has changed, generate a HELP_EVENT. */
10839 if (!NILP (help_echo)
10840 || !NILP (previous_help_echo))
10841 {
10842 Lisp_Object frame;
10843 int n;
10844
10845 if (f)
10846 XSETFRAME (frame, f);
10847 else
10848 frame = Qnil;
10849
10850 any_help_event_p = 1;
10851 n = gen_help_event (bufp, numchars, help_echo, frame,
10852 help_echo_window, help_echo_object,
10853 help_echo_pos);
10854 bufp += n, count += n, numchars -= n;
10855 }
10856
10857 goto OTHER;
10858 }
10859
10860 case ConfigureNotify:
10861 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10862 if (f)
10863 {
10864 #ifndef USE_X_TOOLKIT
10865 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10866 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
10867
10868 /* In the toolkit version, change_frame_size
10869 is called by the code that handles resizing
10870 of the EmacsFrame widget. */
10871
10872 /* Even if the number of character rows and columns has
10873 not changed, the font size may have changed, so we need
10874 to check the pixel dimensions as well. */
10875 if (columns != f->width
10876 || rows != f->height
10877 || event.xconfigure.width != f->output_data.x->pixel_width
10878 || event.xconfigure.height != f->output_data.x->pixel_height)
10879 {
10880 change_frame_size (f, rows, columns, 0, 1, 0);
10881 SET_FRAME_GARBAGED (f);
10882 cancel_mouse_face (f);
10883 }
10884 #endif
10885
10886 f->output_data.x->pixel_width = event.xconfigure.width;
10887 f->output_data.x->pixel_height = event.xconfigure.height;
10888
10889 /* What we have now is the position of Emacs's own window.
10890 Convert that to the position of the window manager window. */
10891 x_real_positions (f, &f->output_data.x->left_pos,
10892 &f->output_data.x->top_pos);
10893
10894 #ifdef HAVE_X_I18N
10895 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10896 xic_set_statusarea (f);
10897 #endif
10898
10899 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10900 {
10901 /* Since the WM decorations come below top_pos now,
10902 we must put them below top_pos in the future. */
10903 f->output_data.x->win_gravity = NorthWestGravity;
10904 x_wm_set_size_hint (f, (long) 0, 0);
10905 }
10906 #ifdef USE_MOTIF
10907 /* Some window managers pass (0,0) as the location of
10908 the window, and the Motif event handler stores it
10909 in the emacs widget, which messes up Motif menus. */
10910 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10911 {
10912 event.xconfigure.x = f->output_data.x->widget->core.x;
10913 event.xconfigure.y = f->output_data.x->widget->core.y;
10914 }
10915 #endif /* USE_MOTIF */
10916 }
10917 goto OTHER;
10918
10919 case ButtonPress:
10920 case ButtonRelease:
10921 {
10922 /* If we decide we want to generate an event to be seen
10923 by the rest of Emacs, we put it here. */
10924 struct input_event emacs_event;
10925 int tool_bar_p = 0;
10926
10927 emacs_event.kind = no_event;
10928 bzero (&compose_status, sizeof (compose_status));
10929
10930 if (dpyinfo->grabbed
10931 && last_mouse_frame
10932 && FRAME_LIVE_P (last_mouse_frame))
10933 f = last_mouse_frame;
10934 else
10935 f = x_window_to_frame (dpyinfo, event.xbutton.window);
10936
10937 if (f)
10938 {
10939 /* Is this in the tool-bar? */
10940 if (WINDOWP (f->tool_bar_window)
10941 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
10942 {
10943 Lisp_Object window;
10944 int p, x, y;
10945
10946 x = event.xbutton.x;
10947 y = event.xbutton.y;
10948
10949 /* Set x and y. */
10950 window = window_from_coordinates (f, x, y, &p, 1);
10951 if (EQ (window, f->tool_bar_window))
10952 {
10953 x_handle_tool_bar_click (f, &event.xbutton);
10954 tool_bar_p = 1;
10955 }
10956 }
10957
10958 if (!tool_bar_p)
10959 if (!dpyinfo->x_focus_frame
10960 || f == dpyinfo->x_focus_frame)
10961 construct_mouse_click (&emacs_event, &event, f);
10962 }
10963 else
10964 {
10965 #ifndef USE_TOOLKIT_SCROLL_BARS
10966 struct scroll_bar *bar
10967 = x_window_to_scroll_bar (event.xbutton.window);
10968
10969 if (bar)
10970 x_scroll_bar_handle_click (bar, &event, &emacs_event);
10971 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10972 }
10973
10974 if (event.type == ButtonPress)
10975 {
10976 dpyinfo->grabbed |= (1 << event.xbutton.button);
10977 last_mouse_frame = f;
10978 /* Ignore any mouse motion that happened
10979 before this event; any subsequent mouse-movement
10980 Emacs events should reflect only motion after
10981 the ButtonPress. */
10982 if (f != 0)
10983 f->mouse_moved = 0;
10984
10985 if (!tool_bar_p)
10986 last_tool_bar_item = -1;
10987 }
10988 else
10989 {
10990 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
10991 }
10992
10993 if (numchars >= 1 && emacs_event.kind != no_event)
10994 {
10995 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10996 bufp++;
10997 count++;
10998 numchars--;
10999 }
11000
11001 #ifdef USE_X_TOOLKIT
11002 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
11003 /* For a down-event in the menu bar,
11004 don't pass it to Xt right now.
11005 Instead, save it away
11006 and we will pass it to Xt from kbd_buffer_get_event.
11007 That way, we can run some Lisp code first. */
11008 if (f && event.type == ButtonPress
11009 /* Verify the event is really within the menu bar
11010 and not just sent to it due to grabbing. */
11011 && event.xbutton.x >= 0
11012 && event.xbutton.x < f->output_data.x->pixel_width
11013 && event.xbutton.y >= 0
11014 && event.xbutton.y < f->output_data.x->menubar_height
11015 && event.xbutton.same_screen)
11016 {
11017 SET_SAVED_BUTTON_EVENT;
11018 XSETFRAME (last_mouse_press_frame, f);
11019 }
11020 else if (event.type == ButtonPress)
11021 {
11022 last_mouse_press_frame = Qnil;
11023 goto OTHER;
11024 }
11025
11026 #ifdef USE_MOTIF /* This should do not harm for Lucid,
11027 but I am trying to be cautious. */
11028 else if (event.type == ButtonRelease)
11029 {
11030 if (!NILP (last_mouse_press_frame))
11031 {
11032 f = XFRAME (last_mouse_press_frame);
11033 if (f->output_data.x)
11034 SET_SAVED_BUTTON_EVENT;
11035 }
11036 else
11037 goto OTHER;
11038 }
11039 #endif /* USE_MOTIF */
11040 else
11041 goto OTHER;
11042 #endif /* USE_X_TOOLKIT */
11043 }
11044 break;
11045
11046 case CirculateNotify:
11047 goto OTHER;
11048
11049 case CirculateRequest:
11050 goto OTHER;
11051
11052 case VisibilityNotify:
11053 goto OTHER;
11054
11055 case MappingNotify:
11056 /* Someone has changed the keyboard mapping - update the
11057 local cache. */
11058 switch (event.xmapping.request)
11059 {
11060 case MappingModifier:
11061 x_find_modifier_meanings (dpyinfo);
11062 /* This is meant to fall through. */
11063 case MappingKeyboard:
11064 XRefreshKeyboardMapping (&event.xmapping);
11065 }
11066 goto OTHER;
11067
11068 default:
11069 OTHER:
11070 #ifdef USE_X_TOOLKIT
11071 BLOCK_INPUT;
11072 XtDispatchEvent (&event);
11073 UNBLOCK_INPUT;
11074 #endif /* USE_X_TOOLKIT */
11075 break;
11076 }
11077 }
11078 }
11079
11080 out:;
11081
11082 /* On some systems, an X bug causes Emacs to get no more events
11083 when the window is destroyed. Detect that. (1994.) */
11084 if (! event_found)
11085 {
11086 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
11087 One XNOOP in 100 loops will make Emacs terminate.
11088 B. Bretthauer, 1994 */
11089 x_noop_count++;
11090 if (x_noop_count >= 100)
11091 {
11092 x_noop_count=0;
11093
11094 if (next_noop_dpyinfo == 0)
11095 next_noop_dpyinfo = x_display_list;
11096
11097 XNoOp (next_noop_dpyinfo->display);
11098
11099 /* Each time we get here, cycle through the displays now open. */
11100 next_noop_dpyinfo = next_noop_dpyinfo->next;
11101 }
11102 }
11103
11104 /* If the focus was just given to an auto-raising frame,
11105 raise it now. */
11106 /* ??? This ought to be able to handle more than one such frame. */
11107 if (pending_autoraise_frame)
11108 {
11109 x_raise_frame (pending_autoraise_frame);
11110 pending_autoraise_frame = 0;
11111 }
11112
11113 UNBLOCK_INPUT;
11114 --handling_signal;
11115 return count;
11116 }
11117
11118
11119
11120 \f
11121 /***********************************************************************
11122 Text Cursor
11123 ***********************************************************************/
11124
11125 /* Notice if the text cursor of window W has been overwritten by a
11126 drawing operation that outputs N glyphs starting at HPOS in the
11127 line given by output_cursor.vpos.
11128
11129 N < 0 means all the rest of the line after HPOS has been
11130 written. */
11131
11132 static void
11133 notice_overwritten_cursor (w, hpos, n)
11134 struct window *w;
11135 int hpos, n;
11136 {
11137 if (updated_area == TEXT_AREA
11138 && output_cursor.vpos == w->phys_cursor.vpos
11139 && output_cursor.x <= w->phys_cursor.x
11140 && w->phys_cursor_on_p)
11141 {
11142 if (n < 0)
11143 w->phys_cursor_on_p = 0;
11144 else
11145 {
11146 /* It depends on the width of the N glyphs written at HPOS
11147 if the cursor has been overwritten or not. */
11148 struct glyph *glyph = &updated_row->glyphs[TEXT_AREA][hpos];
11149 struct glyph *end = glyph + n;
11150 int width = 0;
11151
11152 for (; glyph < end; ++glyph)
11153 width += glyph->pixel_width;
11154
11155 if (output_cursor.x + width > w->phys_cursor.x)
11156 w->phys_cursor_on_p = 0;
11157 }
11158 }
11159 }
11160
11161
11162 /* Set clipping for output in glyph row ROW. W is the window in which
11163 we operate. GC is the graphics context to set clipping in.
11164 WHOLE_LINE_P non-zero means include the areas used for truncation
11165 mark display and alike in the clipping rectangle.
11166
11167 ROW may be a text row or, e.g., a mode line. Text rows must be
11168 clipped to the interior of the window dedicated to text display,
11169 mode lines must be clipped to the whole window. */
11170
11171 static void
11172 x_clip_to_row (w, row, gc, whole_line_p)
11173 struct window *w;
11174 struct glyph_row *row;
11175 GC gc;
11176 int whole_line_p;
11177 {
11178 struct frame *f = XFRAME (WINDOW_FRAME (w));
11179 XRectangle clip_rect;
11180 int window_x, window_y, window_width, window_height;
11181
11182 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
11183
11184 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
11185 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
11186 clip_rect.y = max (clip_rect.y, window_y);
11187 clip_rect.width = window_width;
11188 clip_rect.height = row->visible_height;
11189
11190 /* If clipping to the whole line, including trunc marks, extend
11191 the rectangle to the left and increase its width. */
11192 if (whole_line_p)
11193 {
11194 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
11195 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
11196 }
11197
11198 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
11199 }
11200
11201
11202 /* Draw a hollow box cursor on window W in glyph row ROW. */
11203
11204 static void
11205 x_draw_hollow_cursor (w, row)
11206 struct window *w;
11207 struct glyph_row *row;
11208 {
11209 struct frame *f = XFRAME (WINDOW_FRAME (w));
11210 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11211 Display *dpy = FRAME_X_DISPLAY (f);
11212 int x, y, wd, h;
11213 XGCValues xgcv;
11214 struct glyph *cursor_glyph;
11215 GC gc;
11216
11217 /* Compute frame-relative coordinates from window-relative
11218 coordinates. */
11219 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11220 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
11221 + row->ascent - w->phys_cursor_ascent);
11222 h = row->height - 1;
11223
11224 /* Get the glyph the cursor is on. If we can't tell because
11225 the current matrix is invalid or such, give up. */
11226 cursor_glyph = get_phys_cursor_glyph (w);
11227 if (cursor_glyph == NULL)
11228 return;
11229
11230 /* Compute the width of the rectangle to draw. If on a stretch
11231 glyph, and `x-stretch-block-cursor' is nil, don't draw a
11232 rectangle as wide as the glyph, but use a canonical character
11233 width instead. */
11234 wd = cursor_glyph->pixel_width - 1;
11235 if (cursor_glyph->type == STRETCH_GLYPH
11236 && !x_stretch_cursor_p)
11237 wd = min (CANON_X_UNIT (f), wd);
11238
11239 /* The foreground of cursor_gc is typically the same as the normal
11240 background color, which can cause the cursor box to be invisible. */
11241 xgcv.foreground = f->output_data.x->cursor_pixel;
11242 if (dpyinfo->scratch_cursor_gc)
11243 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
11244 else
11245 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
11246 GCForeground, &xgcv);
11247 gc = dpyinfo->scratch_cursor_gc;
11248
11249 /* Set clipping, draw the rectangle, and reset clipping again. */
11250 x_clip_to_row (w, row, gc, 0);
11251 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
11252 XSetClipMask (dpy, gc, None);
11253 }
11254
11255
11256 /* Draw a bar cursor on window W in glyph row ROW.
11257
11258 Implementation note: One would like to draw a bar cursor with an
11259 angle equal to the one given by the font property XA_ITALIC_ANGLE.
11260 Unfortunately, I didn't find a font yet that has this property set.
11261 --gerd. */
11262
11263 static void
11264 x_draw_bar_cursor (w, row, width)
11265 struct window *w;
11266 struct glyph_row *row;
11267 int width;
11268 {
11269 struct frame *f = XFRAME (w->frame);
11270 struct glyph *cursor_glyph;
11271 GC gc;
11272 int x;
11273 unsigned long mask;
11274 XGCValues xgcv;
11275 Display *dpy;
11276 Window window;
11277
11278 /* If cursor is out of bounds, don't draw garbage. This can happen
11279 in mini-buffer windows when switching between echo area glyphs
11280 and mini-buffer. */
11281 cursor_glyph = get_phys_cursor_glyph (w);
11282 if (cursor_glyph == NULL)
11283 return;
11284
11285 /* If on an image, draw like a normal cursor. That's usually better
11286 visible than drawing a bar, esp. if the image is large so that
11287 the bar might not be in the window. */
11288 if (cursor_glyph->type == IMAGE_GLYPH)
11289 {
11290 struct glyph_row *row;
11291 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
11292 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
11293 }
11294 else
11295 {
11296 xgcv.background = f->output_data.x->cursor_pixel;
11297 xgcv.foreground = f->output_data.x->cursor_pixel;
11298 xgcv.graphics_exposures = 0;
11299 mask = GCForeground | GCBackground | GCGraphicsExposures;
11300 dpy = FRAME_X_DISPLAY (f);
11301 window = FRAME_X_WINDOW (f);
11302 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
11303
11304 if (gc)
11305 XChangeGC (dpy, gc, mask, &xgcv);
11306 else
11307 {
11308 gc = XCreateGC (dpy, window, mask, &xgcv);
11309 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
11310 }
11311
11312 if (width < 0)
11313 width = f->output_data.x->cursor_width;
11314
11315 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11316 x_clip_to_row (w, row, gc, 0);
11317 XFillRectangle (dpy, window, gc,
11318 x,
11319 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
11320 min (cursor_glyph->pixel_width, width),
11321 row->height);
11322 XSetClipMask (dpy, gc, None);
11323 }
11324 }
11325
11326
11327 /* Clear the cursor of window W to background color, and mark the
11328 cursor as not shown. This is used when the text where the cursor
11329 is is about to be rewritten. */
11330
11331 static void
11332 x_clear_cursor (w)
11333 struct window *w;
11334 {
11335 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
11336 x_update_window_cursor (w, 0);
11337 }
11338
11339
11340 /* Draw the cursor glyph of window W in glyph row ROW. See the
11341 comment of x_draw_glyphs for the meaning of HL. */
11342
11343 static void
11344 x_draw_phys_cursor_glyph (w, row, hl)
11345 struct window *w;
11346 struct glyph_row *row;
11347 enum draw_glyphs_face hl;
11348 {
11349 /* If cursor hpos is out of bounds, don't draw garbage. This can
11350 happen in mini-buffer windows when switching between echo area
11351 glyphs and mini-buffer. */
11352 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
11353 {
11354 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
11355 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
11356 hl, 0, 0, 0);
11357
11358 /* When we erase the cursor, and ROW is overlapped by other
11359 rows, make sure that these overlapping parts of other rows
11360 are redrawn. */
11361 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
11362 {
11363 if (row > w->current_matrix->rows
11364 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
11365 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
11366
11367 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
11368 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
11369 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
11370 }
11371 }
11372 }
11373
11374
11375 /* Erase the image of a cursor of window W from the screen. */
11376
11377 static void
11378 x_erase_phys_cursor (w)
11379 struct window *w;
11380 {
11381 struct frame *f = XFRAME (w->frame);
11382 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11383 int hpos = w->phys_cursor.hpos;
11384 int vpos = w->phys_cursor.vpos;
11385 int mouse_face_here_p = 0;
11386 struct glyph_matrix *active_glyphs = w->current_matrix;
11387 struct glyph_row *cursor_row;
11388 struct glyph *cursor_glyph;
11389 enum draw_glyphs_face hl;
11390
11391 /* No cursor displayed or row invalidated => nothing to do on the
11392 screen. */
11393 if (w->phys_cursor_type == NO_CURSOR)
11394 goto mark_cursor_off;
11395
11396 /* VPOS >= active_glyphs->nrows means that window has been resized.
11397 Don't bother to erase the cursor. */
11398 if (vpos >= active_glyphs->nrows)
11399 goto mark_cursor_off;
11400
11401 /* If row containing cursor is marked invalid, there is nothing we
11402 can do. */
11403 cursor_row = MATRIX_ROW (active_glyphs, vpos);
11404 if (!cursor_row->enabled_p)
11405 goto mark_cursor_off;
11406
11407 /* This can happen when the new row is shorter than the old one.
11408 In this case, either x_draw_glyphs or clear_end_of_line
11409 should have cleared the cursor. Note that we wouldn't be
11410 able to erase the cursor in this case because we don't have a
11411 cursor glyph at hand. */
11412 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
11413 goto mark_cursor_off;
11414
11415 /* If the cursor is in the mouse face area, redisplay that when
11416 we clear the cursor. */
11417 if (! NILP (dpyinfo->mouse_face_window)
11418 && w == XWINDOW (dpyinfo->mouse_face_window)
11419 && (vpos > dpyinfo->mouse_face_beg_row
11420 || (vpos == dpyinfo->mouse_face_beg_row
11421 && hpos >= dpyinfo->mouse_face_beg_col))
11422 && (vpos < dpyinfo->mouse_face_end_row
11423 || (vpos == dpyinfo->mouse_face_end_row
11424 && hpos < dpyinfo->mouse_face_end_col))
11425 /* Don't redraw the cursor's spot in mouse face if it is at the
11426 end of a line (on a newline). The cursor appears there, but
11427 mouse highlighting does not. */
11428 && cursor_row->used[TEXT_AREA] > hpos)
11429 mouse_face_here_p = 1;
11430
11431 /* Maybe clear the display under the cursor. */
11432 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
11433 {
11434 int x;
11435 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11436
11437 cursor_glyph = get_phys_cursor_glyph (w);
11438 if (cursor_glyph == NULL)
11439 goto mark_cursor_off;
11440
11441 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11442
11443 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11444 x,
11445 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
11446 cursor_row->y)),
11447 cursor_glyph->pixel_width,
11448 cursor_row->visible_height,
11449 False);
11450 }
11451
11452 /* Erase the cursor by redrawing the character underneath it. */
11453 if (mouse_face_here_p)
11454 hl = DRAW_MOUSE_FACE;
11455 else if (cursor_row->inverse_p)
11456 hl = DRAW_INVERSE_VIDEO;
11457 else
11458 hl = DRAW_NORMAL_TEXT;
11459 x_draw_phys_cursor_glyph (w, cursor_row, hl);
11460
11461 mark_cursor_off:
11462 w->phys_cursor_on_p = 0;
11463 w->phys_cursor_type = NO_CURSOR;
11464 }
11465
11466
11467 /* Non-zero if physical cursor of window W is within mouse face. */
11468
11469 static int
11470 cursor_in_mouse_face_p (w)
11471 struct window *w;
11472 {
11473 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
11474 int in_mouse_face = 0;
11475
11476 if (WINDOWP (dpyinfo->mouse_face_window)
11477 && XWINDOW (dpyinfo->mouse_face_window) == w)
11478 {
11479 int hpos = w->phys_cursor.hpos;
11480 int vpos = w->phys_cursor.vpos;
11481
11482 if (vpos >= dpyinfo->mouse_face_beg_row
11483 && vpos <= dpyinfo->mouse_face_end_row
11484 && (vpos > dpyinfo->mouse_face_beg_row
11485 || hpos >= dpyinfo->mouse_face_beg_col)
11486 && (vpos < dpyinfo->mouse_face_end_row
11487 || hpos < dpyinfo->mouse_face_end_col
11488 || dpyinfo->mouse_face_past_end))
11489 in_mouse_face = 1;
11490 }
11491
11492 return in_mouse_face;
11493 }
11494
11495
11496 /* Display or clear cursor of window W. If ON is zero, clear the
11497 cursor. If it is non-zero, display the cursor. If ON is nonzero,
11498 where to put the cursor is specified by HPOS, VPOS, X and Y. */
11499
11500 void
11501 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
11502 struct window *w;
11503 int on, hpos, vpos, x, y;
11504 {
11505 struct frame *f = XFRAME (w->frame);
11506 int new_cursor_type;
11507 int new_cursor_width;
11508 struct glyph_matrix *current_glyphs;
11509 struct glyph_row *glyph_row;
11510 struct glyph *glyph;
11511
11512 /* This is pointless on invisible frames, and dangerous on garbaged
11513 windows and frames; in the latter case, the frame or window may
11514 be in the midst of changing its size, and x and y may be off the
11515 window. */
11516 if (! FRAME_VISIBLE_P (f)
11517 || FRAME_GARBAGED_P (f)
11518 || vpos >= w->current_matrix->nrows
11519 || hpos >= w->current_matrix->matrix_w)
11520 return;
11521
11522 /* If cursor is off and we want it off, return quickly. */
11523 if (!on && !w->phys_cursor_on_p)
11524 return;
11525
11526 current_glyphs = w->current_matrix;
11527 glyph_row = MATRIX_ROW (current_glyphs, vpos);
11528 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
11529
11530 /* If cursor row is not enabled, we don't really know where to
11531 display the cursor. */
11532 if (!glyph_row->enabled_p)
11533 {
11534 w->phys_cursor_on_p = 0;
11535 return;
11536 }
11537
11538 xassert (interrupt_input_blocked);
11539
11540 /* Set new_cursor_type to the cursor we want to be displayed. In a
11541 mini-buffer window, we want the cursor only to appear if we are
11542 reading input from this window. For the selected window, we want
11543 the cursor type given by the frame parameter. If explicitly
11544 marked off, draw no cursor. In all other cases, we want a hollow
11545 box cursor. */
11546 new_cursor_width = -1;
11547 if (cursor_in_echo_area
11548 && FRAME_HAS_MINIBUF_P (f)
11549 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
11550 {
11551 if (w == XWINDOW (echo_area_window))
11552 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11553 else
11554 new_cursor_type = HOLLOW_BOX_CURSOR;
11555 }
11556 else
11557 {
11558 if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
11559 || w != XWINDOW (f->selected_window))
11560 {
11561 extern int cursor_in_non_selected_windows;
11562
11563 if (MINI_WINDOW_P (w)
11564 || !cursor_in_non_selected_windows
11565 || NILP (XBUFFER (w->buffer)->cursor_type))
11566 new_cursor_type = NO_CURSOR;
11567 else
11568 new_cursor_type = HOLLOW_BOX_CURSOR;
11569 }
11570 else if (w->cursor_off_p)
11571 new_cursor_type = NO_CURSOR;
11572 else
11573 {
11574 struct buffer *b = XBUFFER (w->buffer);
11575
11576 if (EQ (b->cursor_type, Qt))
11577 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11578 else
11579 new_cursor_type = x_specified_cursor_type (b->cursor_type,
11580 &new_cursor_width);
11581 }
11582 }
11583
11584 /* If cursor is currently being shown and we don't want it to be or
11585 it is in the wrong place, or the cursor type is not what we want,
11586 erase it. */
11587 if (w->phys_cursor_on_p
11588 && (!on
11589 || w->phys_cursor.x != x
11590 || w->phys_cursor.y != y
11591 || new_cursor_type != w->phys_cursor_type))
11592 x_erase_phys_cursor (w);
11593
11594 /* If the cursor is now invisible and we want it to be visible,
11595 display it. */
11596 if (on && !w->phys_cursor_on_p)
11597 {
11598 w->phys_cursor_ascent = glyph_row->ascent;
11599 w->phys_cursor_height = glyph_row->height;
11600
11601 /* Set phys_cursor_.* before x_draw_.* is called because some
11602 of them may need the information. */
11603 w->phys_cursor.x = x;
11604 w->phys_cursor.y = glyph_row->y;
11605 w->phys_cursor.hpos = hpos;
11606 w->phys_cursor.vpos = vpos;
11607 w->phys_cursor_type = new_cursor_type;
11608 w->phys_cursor_on_p = 1;
11609
11610 switch (new_cursor_type)
11611 {
11612 case HOLLOW_BOX_CURSOR:
11613 x_draw_hollow_cursor (w, glyph_row);
11614 break;
11615
11616 case FILLED_BOX_CURSOR:
11617 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
11618 break;
11619
11620 case BAR_CURSOR:
11621 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
11622 break;
11623
11624 case NO_CURSOR:
11625 break;
11626
11627 default:
11628 abort ();
11629 }
11630
11631 #ifdef HAVE_X_I18N
11632 if (w == XWINDOW (f->selected_window))
11633 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
11634 xic_set_preeditarea (w, x, y);
11635 #endif
11636 }
11637
11638 #ifndef XFlush
11639 if (updating_frame != f)
11640 XFlush (FRAME_X_DISPLAY (f));
11641 #endif
11642 }
11643
11644
11645 /* Display the cursor on window W, or clear it. X and Y are window
11646 relative pixel coordinates. HPOS and VPOS are glyph matrix
11647 positions. If W is not the selected window, display a hollow
11648 cursor. ON non-zero means display the cursor at X, Y which
11649 correspond to HPOS, VPOS, otherwise it is cleared. */
11650
11651 void
11652 x_display_cursor (w, on, hpos, vpos, x, y)
11653 struct window *w;
11654 int on, hpos, vpos, x, y;
11655 {
11656 BLOCK_INPUT;
11657 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
11658 UNBLOCK_INPUT;
11659 }
11660
11661
11662 /* Display the cursor on window W, or clear it, according to ON_P.
11663 Don't change the cursor's position. */
11664
11665 void
11666 x_update_cursor (f, on_p)
11667 struct frame *f;
11668 {
11669 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11670 }
11671
11672
11673 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
11674 in the window tree rooted at W. */
11675
11676 static void
11677 x_update_cursor_in_window_tree (w, on_p)
11678 struct window *w;
11679 int on_p;
11680 {
11681 while (w)
11682 {
11683 if (!NILP (w->hchild))
11684 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11685 else if (!NILP (w->vchild))
11686 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11687 else
11688 x_update_window_cursor (w, on_p);
11689
11690 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11691 }
11692 }
11693
11694
11695 /* Switch the display of W's cursor on or off, according to the value
11696 of ON. */
11697
11698 static void
11699 x_update_window_cursor (w, on)
11700 struct window *w;
11701 int on;
11702 {
11703 /* Don't update cursor in windows whose frame is in the process
11704 of being deleted. */
11705 if (w->current_matrix)
11706 {
11707 BLOCK_INPUT;
11708 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11709 w->phys_cursor.x, w->phys_cursor.y);
11710 UNBLOCK_INPUT;
11711 }
11712 }
11713
11714
11715
11716 \f
11717 /* Icons. */
11718
11719 /* Make the x-window of frame F use the gnu icon bitmap. */
11720
11721 int
11722 x_bitmap_icon (f, file)
11723 struct frame *f;
11724 Lisp_Object file;
11725 {
11726 int bitmap_id;
11727
11728 if (FRAME_X_WINDOW (f) == 0)
11729 return 1;
11730
11731 /* Free up our existing icon bitmap if any. */
11732 if (f->output_data.x->icon_bitmap > 0)
11733 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11734 f->output_data.x->icon_bitmap = 0;
11735
11736 if (STRINGP (file))
11737 bitmap_id = x_create_bitmap_from_file (f, file);
11738 else
11739 {
11740 /* Create the GNU bitmap if necessary. */
11741 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
11742 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11743 = x_create_bitmap_from_data (f, gnu_bits,
11744 gnu_width, gnu_height);
11745
11746 /* The first time we create the GNU bitmap,
11747 this increments the ref-count one extra time.
11748 As a result, the GNU bitmap is never freed.
11749 That way, we don't have to worry about allocating it again. */
11750 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
11751
11752 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
11753 }
11754
11755 x_wm_set_icon_pixmap (f, bitmap_id);
11756 f->output_data.x->icon_bitmap = bitmap_id;
11757
11758 return 0;
11759 }
11760
11761
11762 /* Make the x-window of frame F use a rectangle with text.
11763 Use ICON_NAME as the text. */
11764
11765 int
11766 x_text_icon (f, icon_name)
11767 struct frame *f;
11768 char *icon_name;
11769 {
11770 if (FRAME_X_WINDOW (f) == 0)
11771 return 1;
11772
11773 #ifdef HAVE_X11R4
11774 {
11775 XTextProperty text;
11776 text.value = (unsigned char *) icon_name;
11777 text.encoding = XA_STRING;
11778 text.format = 8;
11779 text.nitems = strlen (icon_name);
11780 #ifdef USE_X_TOOLKIT
11781 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11782 &text);
11783 #else /* not USE_X_TOOLKIT */
11784 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11785 #endif /* not USE_X_TOOLKIT */
11786 }
11787 #else /* not HAVE_X11R4 */
11788 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11789 #endif /* not HAVE_X11R4 */
11790
11791 if (f->output_data.x->icon_bitmap > 0)
11792 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11793 f->output_data.x->icon_bitmap = 0;
11794 x_wm_set_icon_pixmap (f, 0);
11795
11796 return 0;
11797 }
11798 \f
11799 #define X_ERROR_MESSAGE_SIZE 200
11800
11801 /* If non-nil, this should be a string.
11802 It means catch X errors and store the error message in this string. */
11803
11804 static Lisp_Object x_error_message_string;
11805
11806 /* An X error handler which stores the error message in
11807 x_error_message_string. This is called from x_error_handler if
11808 x_catch_errors is in effect. */
11809
11810 static void
11811 x_error_catcher (display, error)
11812 Display *display;
11813 XErrorEvent *error;
11814 {
11815 XGetErrorText (display, error->error_code,
11816 XSTRING (x_error_message_string)->data,
11817 X_ERROR_MESSAGE_SIZE);
11818 }
11819
11820 /* Begin trapping X errors for display DPY. Actually we trap X errors
11821 for all displays, but DPY should be the display you are actually
11822 operating on.
11823
11824 After calling this function, X protocol errors no longer cause
11825 Emacs to exit; instead, they are recorded in the string
11826 stored in x_error_message_string.
11827
11828 Calling x_check_errors signals an Emacs error if an X error has
11829 occurred since the last call to x_catch_errors or x_check_errors.
11830
11831 Calling x_uncatch_errors resumes the normal error handling. */
11832
11833 void x_check_errors ();
11834 static Lisp_Object x_catch_errors_unwind ();
11835
11836 int
11837 x_catch_errors (dpy)
11838 Display *dpy;
11839 {
11840 int count = specpdl_ptr - specpdl;
11841
11842 /* Make sure any errors from previous requests have been dealt with. */
11843 XSync (dpy, False);
11844
11845 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11846
11847 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11848 XSTRING (x_error_message_string)->data[0] = 0;
11849
11850 return count;
11851 }
11852
11853 /* Unbind the binding that we made to check for X errors. */
11854
11855 static Lisp_Object
11856 x_catch_errors_unwind (old_val)
11857 Lisp_Object old_val;
11858 {
11859 x_error_message_string = old_val;
11860 return Qnil;
11861 }
11862
11863 /* If any X protocol errors have arrived since the last call to
11864 x_catch_errors or x_check_errors, signal an Emacs error using
11865 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11866
11867 void
11868 x_check_errors (dpy, format)
11869 Display *dpy;
11870 char *format;
11871 {
11872 /* Make sure to catch any errors incurred so far. */
11873 XSync (dpy, False);
11874
11875 if (XSTRING (x_error_message_string)->data[0])
11876 error (format, XSTRING (x_error_message_string)->data);
11877 }
11878
11879 /* Nonzero if we had any X protocol errors
11880 since we did x_catch_errors on DPY. */
11881
11882 int
11883 x_had_errors_p (dpy)
11884 Display *dpy;
11885 {
11886 /* Make sure to catch any errors incurred so far. */
11887 XSync (dpy, False);
11888
11889 return XSTRING (x_error_message_string)->data[0] != 0;
11890 }
11891
11892 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11893
11894 void
11895 x_clear_errors (dpy)
11896 Display *dpy;
11897 {
11898 XSTRING (x_error_message_string)->data[0] = 0;
11899 }
11900
11901 /* Stop catching X protocol errors and let them make Emacs die.
11902 DPY should be the display that was passed to x_catch_errors.
11903 COUNT should be the value that was returned by
11904 the corresponding call to x_catch_errors. */
11905
11906 void
11907 x_uncatch_errors (dpy, count)
11908 Display *dpy;
11909 int count;
11910 {
11911 unbind_to (count, Qnil);
11912 }
11913
11914 #if 0
11915 static unsigned int x_wire_count;
11916 x_trace_wire ()
11917 {
11918 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11919 }
11920 #endif /* ! 0 */
11921
11922 \f
11923 /* Handle SIGPIPE, which can happen when the connection to a server
11924 simply goes away. SIGPIPE is handled by x_connection_signal.
11925 Don't need to do anything, because the write which caused the
11926 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
11927 which will do the appropriate cleanup for us. */
11928
11929 static SIGTYPE
11930 x_connection_signal (signalnum) /* If we don't have an argument, */
11931 int signalnum; /* some compilers complain in signal calls. */
11932 {
11933 #ifdef USG
11934 /* USG systems forget handlers when they are used;
11935 must reestablish each time */
11936 signal (signalnum, x_connection_signal);
11937 #endif /* USG */
11938 }
11939
11940 \f
11941 /************************************************************************
11942 Handling X errors
11943 ************************************************************************/
11944
11945 /* Error message passed to x_connection_closed. */
11946
11947 static char *error_msg;
11948
11949 /* Function installed as fatal_error_signal_hook.in
11950 x_connection_closed. Print the X error message, and exit normally,
11951 instead of dumping core when XtCloseDisplay fails. */
11952
11953 static void
11954 x_fatal_error_signal ()
11955 {
11956 fprintf (stderr, "%s\n", error_msg);
11957 exit (70);
11958 }
11959
11960 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
11961 the text of an error message that lead to the connection loss. */
11962
11963 static SIGTYPE
11964 x_connection_closed (dpy, error_message)
11965 Display *dpy;
11966 char *error_message;
11967 {
11968 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
11969 Lisp_Object frame, tail;
11970 int count;
11971
11972 error_msg = (char *) alloca (strlen (error_message) + 1);
11973 strcpy (error_msg, error_message);
11974 handling_signal = 0;
11975
11976 /* Prevent being called recursively because of an error condition
11977 below. Otherwise, we might end up with printing ``can't find per
11978 display information'' in the recursive call instead of printing
11979 the original message here. */
11980 count = x_catch_errors (dpy);
11981
11982 /* We have to close the display to inform Xt that it doesn't
11983 exist anymore. If we don't, Xt will continue to wait for
11984 events from the display. As a consequence, a sequence of
11985
11986 M-x make-frame-on-display RET :1 RET
11987 ...kill the new frame, so that we get an IO error...
11988 M-x make-frame-on-display RET :1 RET
11989
11990 will indefinitely wait in Xt for events for display `:1', opened
11991 in the first class to make-frame-on-display.
11992
11993 Closing the display is reported to lead to a bus error on
11994 OpenWindows in certain situations. I suspect that is a bug
11995 in OpenWindows. I don't know how to cicumvent it here. */
11996
11997 #ifdef USE_X_TOOLKIT
11998 /* If DPYINFO is null, this means we didn't open the display
11999 in the first place, so don't try to close it. */
12000 if (dpyinfo)
12001 {
12002 extern void (*fatal_error_signal_hook) P_ ((void));
12003 fatal_error_signal_hook = x_fatal_error_signal;
12004 XtCloseDisplay (dpy);
12005 fatal_error_signal_hook = NULL;
12006 }
12007 #endif
12008
12009 /* Indicate that this display is dead. */
12010 if (dpyinfo)
12011 dpyinfo->display = 0;
12012
12013 /* First delete frames whose mini-buffers are on frames
12014 that are on the dead display. */
12015 FOR_EACH_FRAME (tail, frame)
12016 {
12017 Lisp_Object minibuf_frame;
12018 minibuf_frame
12019 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
12020 if (FRAME_X_P (XFRAME (frame))
12021 && FRAME_X_P (XFRAME (minibuf_frame))
12022 && ! EQ (frame, minibuf_frame)
12023 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
12024 Fdelete_frame (frame, Qt);
12025 }
12026
12027 /* Now delete all remaining frames on the dead display.
12028 We are now sure none of these is used as the mini-buffer
12029 for another frame that we need to delete. */
12030 FOR_EACH_FRAME (tail, frame)
12031 if (FRAME_X_P (XFRAME (frame))
12032 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
12033 {
12034 /* Set this to t so that Fdelete_frame won't get confused
12035 trying to find a replacement. */
12036 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
12037 Fdelete_frame (frame, Qt);
12038 }
12039
12040 if (dpyinfo)
12041 x_delete_display (dpyinfo);
12042
12043 x_uncatch_errors (dpy, count);
12044
12045 if (x_display_list == 0)
12046 {
12047 fprintf (stderr, "%s\n", error_msg);
12048 shut_down_emacs (0, 0, Qnil);
12049 exit (70);
12050 }
12051
12052 /* Ordinary stack unwind doesn't deal with these. */
12053 #ifdef SIGIO
12054 sigunblock (sigmask (SIGIO));
12055 #endif
12056 sigunblock (sigmask (SIGALRM));
12057 TOTALLY_UNBLOCK_INPUT;
12058
12059 clear_waiting_for_input ();
12060 error ("%s", error_msg);
12061 }
12062
12063
12064 /* This is the usual handler for X protocol errors.
12065 It kills all frames on the display that we got the error for.
12066 If that was the only one, it prints an error message and kills Emacs. */
12067
12068 static void
12069 x_error_quitter (display, error)
12070 Display *display;
12071 XErrorEvent *error;
12072 {
12073 char buf[256], buf1[356];
12074
12075 /* Note that there is no real way portable across R3/R4 to get the
12076 original error handler. */
12077
12078 XGetErrorText (display, error->error_code, buf, sizeof (buf));
12079 sprintf (buf1, "X protocol error: %s on protocol request %d",
12080 buf, error->request_code);
12081 x_connection_closed (display, buf1);
12082 }
12083
12084
12085 /* This is the first-level handler for X protocol errors.
12086 It calls x_error_quitter or x_error_catcher. */
12087
12088 static int
12089 x_error_handler (display, error)
12090 Display *display;
12091 XErrorEvent *error;
12092 {
12093 if (! NILP (x_error_message_string))
12094 x_error_catcher (display, error);
12095 else
12096 x_error_quitter (display, error);
12097 return 0;
12098 }
12099
12100 /* This is the handler for X IO errors, always.
12101 It kills all frames on the display that we lost touch with.
12102 If that was the only one, it prints an error message and kills Emacs. */
12103
12104 static int
12105 x_io_error_quitter (display)
12106 Display *display;
12107 {
12108 char buf[256];
12109
12110 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
12111 x_connection_closed (display, buf);
12112 return 0;
12113 }
12114 \f
12115 /* Changing the font of the frame. */
12116
12117 /* Give frame F the font named FONTNAME as its default font, and
12118 return the full name of that font. FONTNAME may be a wildcard
12119 pattern; in that case, we choose some font that fits the pattern.
12120 The return value shows which font we chose. */
12121
12122 Lisp_Object
12123 x_new_font (f, fontname)
12124 struct frame *f;
12125 register char *fontname;
12126 {
12127 struct font_info *fontp
12128 = FS_LOAD_FONT (f, 0, fontname, -1);
12129
12130 if (!fontp)
12131 return Qnil;
12132
12133 f->output_data.x->font = (XFontStruct *) (fontp->font);
12134 f->output_data.x->baseline_offset = fontp->baseline_offset;
12135 f->output_data.x->fontset = -1;
12136
12137 /* Compute the scroll bar width in character columns. */
12138 if (f->scroll_bar_pixel_width > 0)
12139 {
12140 int wid = FONT_WIDTH (f->output_data.x->font);
12141 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
12142 }
12143 else
12144 {
12145 int wid = FONT_WIDTH (f->output_data.x->font);
12146 f->scroll_bar_cols = (14 + wid - 1) / wid;
12147 }
12148
12149 /* Now make the frame display the given font. */
12150 if (FRAME_X_WINDOW (f) != 0)
12151 {
12152 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
12153 f->output_data.x->font->fid);
12154 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
12155 f->output_data.x->font->fid);
12156 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
12157 f->output_data.x->font->fid);
12158
12159 frame_update_line_height (f);
12160
12161 /* Don't change the size of a tip frame; there's no point in
12162 doing it because it's done in Fx_show_tip, and it leads to
12163 problems because the tip frame has no widget. */
12164 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
12165 x_set_window_size (f, 0, f->width, f->height);
12166 }
12167 else
12168 /* If we are setting a new frame's font for the first time,
12169 there are no faces yet, so this font's height is the line height. */
12170 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
12171
12172 return build_string (fontp->full_name);
12173 }
12174
12175 /* Give frame F the fontset named FONTSETNAME as its default font, and
12176 return the full name of that fontset. FONTSETNAME may be a wildcard
12177 pattern; in that case, we choose some fontset that fits the pattern.
12178 The return value shows which fontset we chose. */
12179
12180 Lisp_Object
12181 x_new_fontset (f, fontsetname)
12182 struct frame *f;
12183 char *fontsetname;
12184 {
12185 int fontset = fs_query_fontset (build_string (fontsetname), 0);
12186 Lisp_Object result;
12187
12188 if (fontset < 0)
12189 return Qnil;
12190
12191 if (f->output_data.x->fontset == fontset)
12192 /* This fontset is already set in frame F. There's nothing more
12193 to do. */
12194 return fontset_name (fontset);
12195
12196 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
12197
12198 if (!STRINGP (result))
12199 /* Can't load ASCII font. */
12200 return Qnil;
12201
12202 /* Since x_new_font doesn't update any fontset information, do it now. */
12203 f->output_data.x->fontset = fontset;
12204
12205 #ifdef HAVE_X_I18N
12206 if (FRAME_XIC (f)
12207 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
12208 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
12209 #endif
12210
12211 return build_string (fontsetname);
12212 }
12213
12214 \f
12215 /***********************************************************************
12216 X Input Methods
12217 ***********************************************************************/
12218
12219 #ifdef HAVE_X_I18N
12220
12221 #ifdef HAVE_X11R6
12222
12223 /* XIM destroy callback function, which is called whenever the
12224 connection to input method XIM dies. CLIENT_DATA contains a
12225 pointer to the x_display_info structure corresponding to XIM. */
12226
12227 static void
12228 xim_destroy_callback (xim, client_data, call_data)
12229 XIM xim;
12230 XPointer client_data;
12231 XPointer call_data;
12232 {
12233 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
12234 Lisp_Object frame, tail;
12235
12236 BLOCK_INPUT;
12237
12238 /* No need to call XDestroyIC.. */
12239 FOR_EACH_FRAME (tail, frame)
12240 {
12241 struct frame *f = XFRAME (frame);
12242 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
12243 {
12244 FRAME_XIC (f) = NULL;
12245 if (FRAME_XIC_FONTSET (f))
12246 {
12247 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
12248 FRAME_XIC_FONTSET (f) = NULL;
12249 }
12250 }
12251 }
12252
12253 /* No need to call XCloseIM. */
12254 dpyinfo->xim = NULL;
12255 XFree (dpyinfo->xim_styles);
12256 UNBLOCK_INPUT;
12257 }
12258
12259 #endif /* HAVE_X11R6 */
12260
12261 /* Open the connection to the XIM server on display DPYINFO.
12262 RESOURCE_NAME is the resource name Emacs uses. */
12263
12264 static void
12265 xim_open_dpy (dpyinfo, resource_name)
12266 struct x_display_info *dpyinfo;
12267 char *resource_name;
12268 {
12269 #ifdef USE_XIM
12270 XIM xim;
12271
12272 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
12273 dpyinfo->xim = xim;
12274
12275 if (xim)
12276 {
12277 #ifdef HAVE_X11R6
12278 XIMCallback destroy;
12279 #endif
12280
12281 /* Get supported styles and XIM values. */
12282 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
12283
12284 #ifdef HAVE_X11R6
12285 destroy.callback = xim_destroy_callback;
12286 destroy.client_data = (XPointer)dpyinfo;
12287 /* This isn't prototyped in OSF 5.0. */
12288 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
12289 #endif
12290 }
12291
12292 #else /* not USE_XIM */
12293 dpyinfo->xim = NULL;
12294 #endif /* not USE_XIM */
12295 }
12296
12297
12298 #ifdef HAVE_X11R6_XIM
12299
12300 struct xim_inst_t
12301 {
12302 struct x_display_info *dpyinfo;
12303 char *resource_name;
12304 };
12305
12306 /* XIM instantiate callback function, which is called whenever an XIM
12307 server is available. DISPLAY is teh display of the XIM.
12308 CLIENT_DATA contains a pointer to an xim_inst_t structure created
12309 when the callback was registered. */
12310
12311 static void
12312 xim_instantiate_callback (display, client_data, call_data)
12313 Display *display;
12314 XPointer client_data;
12315 XPointer call_data;
12316 {
12317 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
12318 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
12319
12320 /* We don't support multiple XIM connections. */
12321 if (dpyinfo->xim)
12322 return;
12323
12324 xim_open_dpy (dpyinfo, xim_inst->resource_name);
12325
12326 /* Create XIC for the existing frames on the same display, as long
12327 as they have no XIC. */
12328 if (dpyinfo->xim && dpyinfo->reference_count > 0)
12329 {
12330 Lisp_Object tail, frame;
12331
12332 BLOCK_INPUT;
12333 FOR_EACH_FRAME (tail, frame)
12334 {
12335 struct frame *f = XFRAME (frame);
12336
12337 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
12338 if (FRAME_XIC (f) == NULL)
12339 {
12340 create_frame_xic (f);
12341 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
12342 xic_set_statusarea (f);
12343 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
12344 {
12345 struct window *w = XWINDOW (f->selected_window);
12346 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
12347 }
12348 }
12349 }
12350
12351 UNBLOCK_INPUT;
12352 }
12353 }
12354
12355 #endif /* HAVE_X11R6_XIM */
12356
12357
12358 /* Open a connection to the XIM server on display DPYINFO.
12359 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
12360 connection only at the first time. On X11R6, open the connection
12361 in the XIM instantiate callback function. */
12362
12363 static void
12364 xim_initialize (dpyinfo, resource_name)
12365 struct x_display_info *dpyinfo;
12366 char *resource_name;
12367 {
12368 #ifdef USE_XIM
12369 #ifdef HAVE_X11R6_XIM
12370 struct xim_inst_t *xim_inst;
12371 int len;
12372
12373 dpyinfo->xim = NULL;
12374 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
12375 xim_inst->dpyinfo = dpyinfo;
12376 len = strlen (resource_name);
12377 xim_inst->resource_name = (char *) xmalloc (len + 1);
12378 bcopy (resource_name, xim_inst->resource_name, len + 1);
12379 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12380 resource_name, EMACS_CLASS,
12381 xim_instantiate_callback,
12382 /* Fixme: This is XPointer in
12383 XFree86 but (XPointer *) on
12384 Tru64, at least. */
12385 (XPointer) xim_inst);
12386 #else /* not HAVE_X11R6_XIM */
12387 dpyinfo->xim = NULL;
12388 xim_open_dpy (dpyinfo, resource_name);
12389 #endif /* not HAVE_X11R6_XIM */
12390
12391 #else /* not USE_XIM */
12392 dpyinfo->xim = NULL;
12393 #endif /* not USE_XIM */
12394 }
12395
12396
12397 /* Close the connection to the XIM server on display DPYINFO. */
12398
12399 static void
12400 xim_close_dpy (dpyinfo)
12401 struct x_display_info *dpyinfo;
12402 {
12403 #ifdef USE_XIM
12404 #ifdef HAVE_X11R6_XIM
12405 if (dpyinfo->display)
12406 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12407 NULL, EMACS_CLASS,
12408 xim_instantiate_callback, NULL);
12409 #endif /* not HAVE_X11R6_XIM */
12410 if (dpyinfo->display)
12411 XCloseIM (dpyinfo->xim);
12412 dpyinfo->xim = NULL;
12413 XFree (dpyinfo->xim_styles);
12414 #endif /* USE_XIM */
12415 }
12416
12417 #endif /* not HAVE_X11R6_XIM */
12418
12419
12420 \f
12421 /* Calculate the absolute position in frame F
12422 from its current recorded position values and gravity. */
12423
12424 void
12425 x_calc_absolute_position (f)
12426 struct frame *f;
12427 {
12428 Window child;
12429 int win_x = 0, win_y = 0;
12430 int flags = f->output_data.x->size_hint_flags;
12431 int this_window;
12432
12433 /* We have nothing to do if the current position
12434 is already for the top-left corner. */
12435 if (! ((flags & XNegative) || (flags & YNegative)))
12436 return;
12437
12438 #ifdef USE_X_TOOLKIT
12439 this_window = XtWindow (f->output_data.x->widget);
12440 #else
12441 this_window = FRAME_X_WINDOW (f);
12442 #endif
12443
12444 /* Find the position of the outside upper-left corner of
12445 the inner window, with respect to the outer window.
12446 But do this only if we will need the results. */
12447 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
12448 {
12449 int count;
12450
12451 BLOCK_INPUT;
12452 count = x_catch_errors (FRAME_X_DISPLAY (f));
12453 while (1)
12454 {
12455 x_clear_errors (FRAME_X_DISPLAY (f));
12456 XTranslateCoordinates (FRAME_X_DISPLAY (f),
12457
12458 /* From-window, to-window. */
12459 this_window,
12460 f->output_data.x->parent_desc,
12461
12462 /* From-position, to-position. */
12463 0, 0, &win_x, &win_y,
12464
12465 /* Child of win. */
12466 &child);
12467 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12468 {
12469 Window newroot, newparent = 0xdeadbeef;
12470 Window *newchildren;
12471 unsigned int nchildren;
12472
12473 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
12474 &newparent, &newchildren, &nchildren))
12475 break;
12476
12477 XFree ((char *) newchildren);
12478
12479 f->output_data.x->parent_desc = newparent;
12480 }
12481 else
12482 break;
12483 }
12484
12485 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12486 UNBLOCK_INPUT;
12487 }
12488
12489 /* Treat negative positions as relative to the leftmost bottommost
12490 position that fits on the screen. */
12491 if (flags & XNegative)
12492 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
12493 - 2 * f->output_data.x->border_width - win_x
12494 - PIXEL_WIDTH (f)
12495 + f->output_data.x->left_pos);
12496
12497 {
12498 int height = PIXEL_HEIGHT (f);
12499
12500 #if defined USE_X_TOOLKIT && defined USE_MOTIF
12501 /* Something is fishy here. When using Motif, starting Emacs with
12502 `-g -0-0', the frame appears too low by a few pixels.
12503
12504 This seems to be so because initially, while Emacs is starting,
12505 the column widget's height and the frame's pixel height are
12506 different. The column widget's height is the right one. In
12507 later invocations, when Emacs is up, the frame's pixel height
12508 is right, though.
12509
12510 It's not obvious where the initial small difference comes from.
12511 2000-12-01, gerd. */
12512
12513 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
12514 #endif
12515
12516 if (flags & YNegative)
12517 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
12518 - 2 * f->output_data.x->border_width
12519 - win_y
12520 - height
12521 + f->output_data.x->top_pos);
12522 }
12523
12524 /* The left_pos and top_pos
12525 are now relative to the top and left screen edges,
12526 so the flags should correspond. */
12527 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12528 }
12529
12530 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
12531 to really change the position, and 0 when calling from
12532 x_make_frame_visible (in that case, XOFF and YOFF are the current
12533 position values). It is -1 when calling from x_set_frame_parameters,
12534 which means, do adjust for borders but don't change the gravity. */
12535
12536 void
12537 x_set_offset (f, xoff, yoff, change_gravity)
12538 struct frame *f;
12539 register int xoff, yoff;
12540 int change_gravity;
12541 {
12542 int modified_top, modified_left;
12543
12544 if (change_gravity > 0)
12545 {
12546 f->output_data.x->top_pos = yoff;
12547 f->output_data.x->left_pos = xoff;
12548 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12549 if (xoff < 0)
12550 f->output_data.x->size_hint_flags |= XNegative;
12551 if (yoff < 0)
12552 f->output_data.x->size_hint_flags |= YNegative;
12553 f->output_data.x->win_gravity = NorthWestGravity;
12554 }
12555 x_calc_absolute_position (f);
12556
12557 BLOCK_INPUT;
12558 x_wm_set_size_hint (f, (long) 0, 0);
12559
12560 modified_left = f->output_data.x->left_pos;
12561 modified_top = f->output_data.x->top_pos;
12562 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
12563 this seems to be unnecessary and incorrect. rms, 4/17/97. */
12564 /* It is a mystery why we need to add the border_width here
12565 when the frame is already visible, but experiment says we do. */
12566 if (change_gravity != 0)
12567 {
12568 modified_left += f->output_data.x->border_width;
12569 modified_top += f->output_data.x->border_width;
12570 }
12571 #endif
12572
12573 #ifdef USE_X_TOOLKIT
12574 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
12575 modified_left, modified_top);
12576 #else /* not USE_X_TOOLKIT */
12577 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12578 modified_left, modified_top);
12579 #endif /* not USE_X_TOOLKIT */
12580 UNBLOCK_INPUT;
12581 }
12582
12583
12584 /* Change the size of frame F's X window to COLS/ROWS in the case F
12585 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
12586 top-left-corner window gravity for this size change and subsequent
12587 size changes. Otherwise we leave the window gravity unchanged. */
12588
12589 static void
12590 x_set_window_size_1 (f, change_gravity, cols, rows)
12591 struct frame *f;
12592 int change_gravity;
12593 int cols, rows;
12594 {
12595 int pixelwidth, pixelheight;
12596
12597 check_frame_size (f, &rows, &cols);
12598 f->output_data.x->vertical_scroll_bar_extra
12599 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
12600 ? 0
12601 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
12602 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
12603 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
12604 f->output_data.x->flags_areas_extra
12605 = FRAME_FLAGS_AREA_WIDTH (f);
12606 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
12607 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
12608
12609 f->output_data.x->win_gravity = NorthWestGravity;
12610 x_wm_set_size_hint (f, (long) 0, 0);
12611
12612 XSync (FRAME_X_DISPLAY (f), False);
12613 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12614 pixelwidth, pixelheight);
12615
12616 /* Now, strictly speaking, we can't be sure that this is accurate,
12617 but the window manager will get around to dealing with the size
12618 change request eventually, and we'll hear how it went when the
12619 ConfigureNotify event gets here.
12620
12621 We could just not bother storing any of this information here,
12622 and let the ConfigureNotify event set everything up, but that
12623 might be kind of confusing to the Lisp code, since size changes
12624 wouldn't be reported in the frame parameters until some random
12625 point in the future when the ConfigureNotify event arrives.
12626
12627 We pass 1 for DELAY since we can't run Lisp code inside of
12628 a BLOCK_INPUT. */
12629 change_frame_size (f, rows, cols, 0, 1, 0);
12630 PIXEL_WIDTH (f) = pixelwidth;
12631 PIXEL_HEIGHT (f) = pixelheight;
12632
12633 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
12634 receive in the ConfigureNotify event; if we get what we asked
12635 for, then the event won't cause the screen to become garbaged, so
12636 we have to make sure to do it here. */
12637 SET_FRAME_GARBAGED (f);
12638
12639 XFlush (FRAME_X_DISPLAY (f));
12640 }
12641
12642
12643 /* Call this to change the size of frame F's x-window.
12644 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
12645 for this size change and subsequent size changes.
12646 Otherwise we leave the window gravity unchanged. */
12647
12648 void
12649 x_set_window_size (f, change_gravity, cols, rows)
12650 struct frame *f;
12651 int change_gravity;
12652 int cols, rows;
12653 {
12654 BLOCK_INPUT;
12655
12656 #ifdef USE_X_TOOLKIT
12657
12658 if (f->output_data.x->widget != NULL)
12659 {
12660 /* The x and y position of the widget is clobbered by the
12661 call to XtSetValues within EmacsFrameSetCharSize.
12662 This is a real kludge, but I don't understand Xt so I can't
12663 figure out a correct fix. Can anyone else tell me? -- rms. */
12664 int xpos = f->output_data.x->widget->core.x;
12665 int ypos = f->output_data.x->widget->core.y;
12666 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
12667 f->output_data.x->widget->core.x = xpos;
12668 f->output_data.x->widget->core.y = ypos;
12669 }
12670 else
12671 x_set_window_size_1 (f, change_gravity, cols, rows);
12672
12673 #else /* not USE_X_TOOLKIT */
12674
12675 x_set_window_size_1 (f, change_gravity, cols, rows);
12676
12677 #endif /* not USE_X_TOOLKIT */
12678
12679 /* If cursor was outside the new size, mark it as off. */
12680 mark_window_cursors_off (XWINDOW (f->root_window));
12681
12682 /* Clear out any recollection of where the mouse highlighting was,
12683 since it might be in a place that's outside the new frame size.
12684 Actually checking whether it is outside is a pain in the neck,
12685 so don't try--just let the highlighting be done afresh with new size. */
12686 cancel_mouse_face (f);
12687
12688 UNBLOCK_INPUT;
12689 }
12690 \f
12691 /* Mouse warping. */
12692
12693 void
12694 x_set_mouse_position (f, x, y)
12695 struct frame *f;
12696 int x, y;
12697 {
12698 int pix_x, pix_y;
12699
12700 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
12701 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
12702
12703 if (pix_x < 0) pix_x = 0;
12704 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
12705
12706 if (pix_y < 0) pix_y = 0;
12707 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
12708
12709 BLOCK_INPUT;
12710
12711 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12712 0, 0, 0, 0, pix_x, pix_y);
12713 UNBLOCK_INPUT;
12714 }
12715
12716 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
12717
12718 void
12719 x_set_mouse_pixel_position (f, pix_x, pix_y)
12720 struct frame *f;
12721 int pix_x, pix_y;
12722 {
12723 BLOCK_INPUT;
12724
12725 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12726 0, 0, 0, 0, pix_x, pix_y);
12727 UNBLOCK_INPUT;
12728 }
12729 \f
12730 /* focus shifting, raising and lowering. */
12731
12732 void
12733 x_focus_on_frame (f)
12734 struct frame *f;
12735 {
12736 #if 0 /* This proves to be unpleasant. */
12737 x_raise_frame (f);
12738 #endif
12739 #if 0
12740 /* I don't think that the ICCCM allows programs to do things like this
12741 without the interaction of the window manager. Whatever you end up
12742 doing with this code, do it to x_unfocus_frame too. */
12743 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12744 RevertToPointerRoot, CurrentTime);
12745 #endif /* ! 0 */
12746 }
12747
12748 void
12749 x_unfocus_frame (f)
12750 struct frame *f;
12751 {
12752 #if 0
12753 /* Look at the remarks in x_focus_on_frame. */
12754 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
12755 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
12756 RevertToPointerRoot, CurrentTime);
12757 #endif /* ! 0 */
12758 }
12759
12760 /* Raise frame F. */
12761
12762 void
12763 x_raise_frame (f)
12764 struct frame *f;
12765 {
12766 if (f->async_visible)
12767 {
12768 BLOCK_INPUT;
12769 #ifdef USE_X_TOOLKIT
12770 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12771 #else /* not USE_X_TOOLKIT */
12772 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12773 #endif /* not USE_X_TOOLKIT */
12774 XFlush (FRAME_X_DISPLAY (f));
12775 UNBLOCK_INPUT;
12776 }
12777 }
12778
12779 /* Lower frame F. */
12780
12781 void
12782 x_lower_frame (f)
12783 struct frame *f;
12784 {
12785 if (f->async_visible)
12786 {
12787 BLOCK_INPUT;
12788 #ifdef USE_X_TOOLKIT
12789 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12790 #else /* not USE_X_TOOLKIT */
12791 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12792 #endif /* not USE_X_TOOLKIT */
12793 XFlush (FRAME_X_DISPLAY (f));
12794 UNBLOCK_INPUT;
12795 }
12796 }
12797
12798 static void
12799 XTframe_raise_lower (f, raise_flag)
12800 FRAME_PTR f;
12801 int raise_flag;
12802 {
12803 if (raise_flag)
12804 x_raise_frame (f);
12805 else
12806 x_lower_frame (f);
12807 }
12808 \f
12809 /* Change of visibility. */
12810
12811 /* This tries to wait until the frame is really visible.
12812 However, if the window manager asks the user where to position
12813 the frame, this will return before the user finishes doing that.
12814 The frame will not actually be visible at that time,
12815 but it will become visible later when the window manager
12816 finishes with it. */
12817
12818 void
12819 x_make_frame_visible (f)
12820 struct frame *f;
12821 {
12822 Lisp_Object type;
12823 int original_top, original_left;
12824 int retry_count = 2;
12825
12826 retry:
12827
12828 BLOCK_INPUT;
12829
12830 type = x_icon_type (f);
12831 if (!NILP (type))
12832 x_bitmap_icon (f, type);
12833
12834 if (! FRAME_VISIBLE_P (f))
12835 {
12836 /* We test FRAME_GARBAGED_P here to make sure we don't
12837 call x_set_offset a second time
12838 if we get to x_make_frame_visible a second time
12839 before the window gets really visible. */
12840 if (! FRAME_ICONIFIED_P (f)
12841 && ! f->output_data.x->asked_for_visible)
12842 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12843
12844 f->output_data.x->asked_for_visible = 1;
12845
12846 if (! EQ (Vx_no_window_manager, Qt))
12847 x_wm_set_window_state (f, NormalState);
12848 #ifdef USE_X_TOOLKIT
12849 /* This was XtPopup, but that did nothing for an iconified frame. */
12850 XtMapWidget (f->output_data.x->widget);
12851 #else /* not USE_X_TOOLKIT */
12852 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12853 #endif /* not USE_X_TOOLKIT */
12854 #if 0 /* This seems to bring back scroll bars in the wrong places
12855 if the window configuration has changed. They seem
12856 to come back ok without this. */
12857 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
12858 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12859 #endif
12860 }
12861
12862 XFlush (FRAME_X_DISPLAY (f));
12863
12864 /* Synchronize to ensure Emacs knows the frame is visible
12865 before we do anything else. We do this loop with input not blocked
12866 so that incoming events are handled. */
12867 {
12868 Lisp_Object frame;
12869 int count;
12870 /* This must be before UNBLOCK_INPUT
12871 since events that arrive in response to the actions above
12872 will set it when they are handled. */
12873 int previously_visible = f->output_data.x->has_been_visible;
12874
12875 original_left = f->output_data.x->left_pos;
12876 original_top = f->output_data.x->top_pos;
12877
12878 /* This must come after we set COUNT. */
12879 UNBLOCK_INPUT;
12880
12881 /* We unblock here so that arriving X events are processed. */
12882
12883 /* Now move the window back to where it was "supposed to be".
12884 But don't do it if the gravity is negative.
12885 When the gravity is negative, this uses a position
12886 that is 3 pixels too low. Perhaps that's really the border width.
12887
12888 Don't do this if the window has never been visible before,
12889 because the window manager may choose the position
12890 and we don't want to override it. */
12891
12892 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
12893 && f->output_data.x->win_gravity == NorthWestGravity
12894 && previously_visible)
12895 {
12896 Drawable rootw;
12897 int x, y;
12898 unsigned int width, height, border, depth;
12899
12900 BLOCK_INPUT;
12901
12902 /* On some window managers (such as FVWM) moving an existing
12903 window, even to the same place, causes the window manager
12904 to introduce an offset. This can cause the window to move
12905 to an unexpected location. Check the geometry (a little
12906 slow here) and then verify that the window is in the right
12907 place. If the window is not in the right place, move it
12908 there, and take the potential window manager hit. */
12909 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12910 &rootw, &x, &y, &width, &height, &border, &depth);
12911
12912 if (original_left != x || original_top != y)
12913 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12914 original_left, original_top);
12915
12916 UNBLOCK_INPUT;
12917 }
12918
12919 XSETFRAME (frame, f);
12920
12921 /* Wait until the frame is visible. Process X events until a
12922 MapNotify event has been seen, or until we think we won't get a
12923 MapNotify at all.. */
12924 for (count = input_signal_count + 10;
12925 input_signal_count < count && !FRAME_VISIBLE_P (f);)
12926 {
12927 /* Force processing of queued events. */
12928 x_sync (f);
12929
12930 /* Machines that do polling rather than SIGIO have been
12931 observed to go into a busy-wait here. So we'll fake an
12932 alarm signal to let the handler know that there's something
12933 to be read. We used to raise a real alarm, but it seems
12934 that the handler isn't always enabled here. This is
12935 probably a bug. */
12936 if (input_polling_used ())
12937 {
12938 /* It could be confusing if a real alarm arrives while
12939 processing the fake one. Turn it off and let the
12940 handler reset it. */
12941 extern void poll_for_input_1 P_ ((void));
12942 int old_poll_suppress_count = poll_suppress_count;
12943 poll_suppress_count = 1;
12944 poll_for_input_1 ();
12945 poll_suppress_count = old_poll_suppress_count;
12946 }
12947
12948 /* See if a MapNotify event has been processed. */
12949 FRAME_SAMPLE_VISIBILITY (f);
12950 }
12951
12952 /* 2000-09-28: In
12953
12954 (let ((f (selected-frame)))
12955 (iconify-frame f)
12956 (raise-frame f))
12957
12958 the frame is not raised with various window managers on
12959 FreeBSD, Linux and Solaris. It turns out that, for some
12960 unknown reason, the call to XtMapWidget is completely ignored.
12961 Mapping the widget a second time works. */
12962
12963 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
12964 goto retry;
12965 }
12966 }
12967
12968 /* Change from mapped state to withdrawn state. */
12969
12970 /* Make the frame visible (mapped and not iconified). */
12971
12972 void
12973 x_make_frame_invisible (f)
12974 struct frame *f;
12975 {
12976 Window window;
12977
12978 #ifdef USE_X_TOOLKIT
12979 /* Use the frame's outermost window, not the one we normally draw on. */
12980 window = XtWindow (f->output_data.x->widget);
12981 #else /* not USE_X_TOOLKIT */
12982 window = FRAME_X_WINDOW (f);
12983 #endif /* not USE_X_TOOLKIT */
12984
12985 /* Don't keep the highlight on an invisible frame. */
12986 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12987 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12988
12989 #if 0/* This might add unreliability; I don't trust it -- rms. */
12990 if (! f->async_visible && ! f->async_iconified)
12991 return;
12992 #endif
12993
12994 BLOCK_INPUT;
12995
12996 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
12997 that the current position of the window is user-specified, rather than
12998 program-specified, so that when the window is mapped again, it will be
12999 placed at the same location, without forcing the user to position it
13000 by hand again (they have already done that once for this window.) */
13001 x_wm_set_size_hint (f, (long) 0, 1);
13002
13003 #ifdef HAVE_X11R4
13004
13005 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
13006 DefaultScreen (FRAME_X_DISPLAY (f))))
13007 {
13008 UNBLOCK_INPUT_RESIGNAL;
13009 error ("Can't notify window manager of window withdrawal");
13010 }
13011 #else /* ! defined (HAVE_X11R4) */
13012
13013 /* Tell the window manager what we're going to do. */
13014 if (! EQ (Vx_no_window_manager, Qt))
13015 {
13016 XEvent unmap;
13017
13018 unmap.xunmap.type = UnmapNotify;
13019 unmap.xunmap.window = window;
13020 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
13021 unmap.xunmap.from_configure = False;
13022 if (! XSendEvent (FRAME_X_DISPLAY (f),
13023 DefaultRootWindow (FRAME_X_DISPLAY (f)),
13024 False,
13025 SubstructureRedirectMaskSubstructureNotifyMask,
13026 &unmap))
13027 {
13028 UNBLOCK_INPUT_RESIGNAL;
13029 error ("Can't notify window manager of withdrawal");
13030 }
13031 }
13032
13033 /* Unmap the window ourselves. Cheeky! */
13034 XUnmapWindow (FRAME_X_DISPLAY (f), window);
13035 #endif /* ! defined (HAVE_X11R4) */
13036
13037 /* We can't distinguish this from iconification
13038 just by the event that we get from the server.
13039 So we can't win using the usual strategy of letting
13040 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
13041 and synchronize with the server to make sure we agree. */
13042 f->visible = 0;
13043 FRAME_ICONIFIED_P (f) = 0;
13044 f->async_visible = 0;
13045 f->async_iconified = 0;
13046
13047 x_sync (f);
13048
13049 UNBLOCK_INPUT;
13050 }
13051
13052 /* Change window state from mapped to iconified. */
13053
13054 void
13055 x_iconify_frame (f)
13056 struct frame *f;
13057 {
13058 int result;
13059 Lisp_Object type;
13060
13061 /* Don't keep the highlight on an invisible frame. */
13062 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13063 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
13064
13065 if (f->async_iconified)
13066 return;
13067
13068 BLOCK_INPUT;
13069
13070 FRAME_SAMPLE_VISIBILITY (f);
13071
13072 type = x_icon_type (f);
13073 if (!NILP (type))
13074 x_bitmap_icon (f, type);
13075
13076 #ifdef USE_X_TOOLKIT
13077
13078 if (! FRAME_VISIBLE_P (f))
13079 {
13080 if (! EQ (Vx_no_window_manager, Qt))
13081 x_wm_set_window_state (f, IconicState);
13082 /* This was XtPopup, but that did nothing for an iconified frame. */
13083 XtMapWidget (f->output_data.x->widget);
13084 /* The server won't give us any event to indicate
13085 that an invisible frame was changed to an icon,
13086 so we have to record it here. */
13087 f->iconified = 1;
13088 f->visible = 1;
13089 f->async_iconified = 1;
13090 f->async_visible = 0;
13091 UNBLOCK_INPUT;
13092 return;
13093 }
13094
13095 result = XIconifyWindow (FRAME_X_DISPLAY (f),
13096 XtWindow (f->output_data.x->widget),
13097 DefaultScreen (FRAME_X_DISPLAY (f)));
13098 UNBLOCK_INPUT;
13099
13100 if (!result)
13101 error ("Can't notify window manager of iconification");
13102
13103 f->async_iconified = 1;
13104 f->async_visible = 0;
13105
13106
13107 BLOCK_INPUT;
13108 XFlush (FRAME_X_DISPLAY (f));
13109 UNBLOCK_INPUT;
13110 #else /* not USE_X_TOOLKIT */
13111
13112 /* Make sure the X server knows where the window should be positioned,
13113 in case the user deiconifies with the window manager. */
13114 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
13115 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13116
13117 /* Since we don't know which revision of X we're running, we'll use both
13118 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
13119
13120 /* X11R4: send a ClientMessage to the window manager using the
13121 WM_CHANGE_STATE type. */
13122 {
13123 XEvent message;
13124
13125 message.xclient.window = FRAME_X_WINDOW (f);
13126 message.xclient.type = ClientMessage;
13127 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
13128 message.xclient.format = 32;
13129 message.xclient.data.l[0] = IconicState;
13130
13131 if (! XSendEvent (FRAME_X_DISPLAY (f),
13132 DefaultRootWindow (FRAME_X_DISPLAY (f)),
13133 False,
13134 SubstructureRedirectMask | SubstructureNotifyMask,
13135 &message))
13136 {
13137 UNBLOCK_INPUT_RESIGNAL;
13138 error ("Can't notify window manager of iconification");
13139 }
13140 }
13141
13142 /* X11R3: set the initial_state field of the window manager hints to
13143 IconicState. */
13144 x_wm_set_window_state (f, IconicState);
13145
13146 if (!FRAME_VISIBLE_P (f))
13147 {
13148 /* If the frame was withdrawn, before, we must map it. */
13149 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13150 }
13151
13152 f->async_iconified = 1;
13153 f->async_visible = 0;
13154
13155 XFlush (FRAME_X_DISPLAY (f));
13156 UNBLOCK_INPUT;
13157 #endif /* not USE_X_TOOLKIT */
13158 }
13159
13160 \f
13161 /* Free X resources of frame F. */
13162
13163 void
13164 x_free_frame_resources (f)
13165 struct frame *f;
13166 {
13167 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13168
13169 BLOCK_INPUT;
13170
13171 /* If a display connection is dead, don't try sending more
13172 commands to the X server. */
13173 if (dpyinfo->display)
13174 {
13175 if (f->output_data.x->icon_desc)
13176 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
13177
13178 #ifdef HAVE_X_I18N
13179 if (FRAME_XIC (f))
13180 free_frame_xic (f);
13181 #endif
13182
13183 if (FRAME_X_WINDOW (f))
13184 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13185
13186 #ifdef USE_X_TOOLKIT
13187 if (f->output_data.x->widget)
13188 {
13189 XtDestroyWidget (f->output_data.x->widget);
13190 f->output_data.x->widget = NULL;
13191 }
13192 free_frame_menubar (f);
13193 #endif /* USE_X_TOOLKIT */
13194
13195 unload_color (f, f->output_data.x->foreground_pixel);
13196 unload_color (f, f->output_data.x->background_pixel);
13197 unload_color (f, f->output_data.x->cursor_pixel);
13198 unload_color (f, f->output_data.x->cursor_foreground_pixel);
13199 unload_color (f, f->output_data.x->border_pixel);
13200 unload_color (f, f->output_data.x->mouse_pixel);
13201
13202 if (f->output_data.x->scroll_bar_background_pixel != -1)
13203 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
13204 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
13205 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
13206 #ifdef USE_TOOLKIT_SCROLL_BARS
13207 /* Scrollbar shadow colors. */
13208 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
13209 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
13210 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
13211 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
13212 #endif /* USE_TOOLKIT_SCROLL_BARS */
13213 if (f->output_data.x->white_relief.allocated_p)
13214 unload_color (f, f->output_data.x->white_relief.pixel);
13215 if (f->output_data.x->black_relief.allocated_p)
13216 unload_color (f, f->output_data.x->black_relief.pixel);
13217
13218 if (FRAME_FACE_CACHE (f))
13219 free_frame_faces (f);
13220
13221 x_free_gcs (f);
13222 XFlush (FRAME_X_DISPLAY (f));
13223 }
13224
13225 if (f->output_data.x->saved_menu_event)
13226 xfree (f->output_data.x->saved_menu_event);
13227
13228 xfree (f->output_data.x);
13229 f->output_data.x = NULL;
13230
13231 if (f == dpyinfo->x_focus_frame)
13232 dpyinfo->x_focus_frame = 0;
13233 if (f == dpyinfo->x_focus_event_frame)
13234 dpyinfo->x_focus_event_frame = 0;
13235 if (f == dpyinfo->x_highlight_frame)
13236 dpyinfo->x_highlight_frame = 0;
13237
13238 if (f == dpyinfo->mouse_face_mouse_frame)
13239 {
13240 dpyinfo->mouse_face_beg_row
13241 = dpyinfo->mouse_face_beg_col = -1;
13242 dpyinfo->mouse_face_end_row
13243 = dpyinfo->mouse_face_end_col = -1;
13244 dpyinfo->mouse_face_window = Qnil;
13245 dpyinfo->mouse_face_deferred_gc = 0;
13246 dpyinfo->mouse_face_mouse_frame = 0;
13247 }
13248
13249 UNBLOCK_INPUT;
13250 }
13251
13252
13253 /* Destroy the X window of frame F. */
13254
13255 void
13256 x_destroy_window (f)
13257 struct frame *f;
13258 {
13259 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13260
13261 /* If a display connection is dead, don't try sending more
13262 commands to the X server. */
13263 if (dpyinfo->display != 0)
13264 x_free_frame_resources (f);
13265
13266 dpyinfo->reference_count--;
13267 }
13268
13269 \f
13270 /* Setting window manager hints. */
13271
13272 /* Set the normal size hints for the window manager, for frame F.
13273 FLAGS is the flags word to use--or 0 meaning preserve the flags
13274 that the window now has.
13275 If USER_POSITION is nonzero, we set the USPosition
13276 flag (this is useful when FLAGS is 0). */
13277
13278 void
13279 x_wm_set_size_hint (f, flags, user_position)
13280 struct frame *f;
13281 long flags;
13282 int user_position;
13283 {
13284 XSizeHints size_hints;
13285
13286 #ifdef USE_X_TOOLKIT
13287 Arg al[2];
13288 int ac = 0;
13289 Dimension widget_width, widget_height;
13290 Window window = XtWindow (f->output_data.x->widget);
13291 #else /* not USE_X_TOOLKIT */
13292 Window window = FRAME_X_WINDOW (f);
13293 #endif /* not USE_X_TOOLKIT */
13294
13295 /* Setting PMaxSize caused various problems. */
13296 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
13297
13298 size_hints.x = f->output_data.x->left_pos;
13299 size_hints.y = f->output_data.x->top_pos;
13300
13301 #ifdef USE_X_TOOLKIT
13302 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
13303 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
13304 XtGetValues (f->output_data.x->widget, al, ac);
13305 size_hints.height = widget_height;
13306 size_hints.width = widget_width;
13307 #else /* not USE_X_TOOLKIT */
13308 size_hints.height = PIXEL_HEIGHT (f);
13309 size_hints.width = PIXEL_WIDTH (f);
13310 #endif /* not USE_X_TOOLKIT */
13311
13312 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
13313 size_hints.height_inc = f->output_data.x->line_height;
13314 size_hints.max_width
13315 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
13316 size_hints.max_height
13317 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
13318
13319 /* Calculate the base and minimum sizes.
13320
13321 (When we use the X toolkit, we don't do it here.
13322 Instead we copy the values that the widgets are using, below.) */
13323 #ifndef USE_X_TOOLKIT
13324 {
13325 int base_width, base_height;
13326 int min_rows = 0, min_cols = 0;
13327
13328 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
13329 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
13330
13331 check_frame_size (f, &min_rows, &min_cols);
13332
13333 /* The window manager uses the base width hints to calculate the
13334 current number of rows and columns in the frame while
13335 resizing; min_width and min_height aren't useful for this
13336 purpose, since they might not give the dimensions for a
13337 zero-row, zero-column frame.
13338
13339 We use the base_width and base_height members if we have
13340 them; otherwise, we set the min_width and min_height members
13341 to the size for a zero x zero frame. */
13342
13343 #ifdef HAVE_X11R4
13344 size_hints.flags |= PBaseSize;
13345 size_hints.base_width = base_width;
13346 size_hints.base_height = base_height;
13347 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
13348 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
13349 #else
13350 size_hints.min_width = base_width;
13351 size_hints.min_height = base_height;
13352 #endif
13353 }
13354
13355 /* If we don't need the old flags, we don't need the old hint at all. */
13356 if (flags)
13357 {
13358 size_hints.flags |= flags;
13359 goto no_read;
13360 }
13361 #endif /* not USE_X_TOOLKIT */
13362
13363 {
13364 XSizeHints hints; /* Sometimes I hate X Windows... */
13365 long supplied_return;
13366 int value;
13367
13368 #ifdef HAVE_X11R4
13369 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
13370 &supplied_return);
13371 #else
13372 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
13373 #endif
13374
13375 #ifdef USE_X_TOOLKIT
13376 size_hints.base_height = hints.base_height;
13377 size_hints.base_width = hints.base_width;
13378 size_hints.min_height = hints.min_height;
13379 size_hints.min_width = hints.min_width;
13380 #endif
13381
13382 if (flags)
13383 size_hints.flags |= flags;
13384 else
13385 {
13386 if (value == 0)
13387 hints.flags = 0;
13388 if (hints.flags & PSize)
13389 size_hints.flags |= PSize;
13390 if (hints.flags & PPosition)
13391 size_hints.flags |= PPosition;
13392 if (hints.flags & USPosition)
13393 size_hints.flags |= USPosition;
13394 if (hints.flags & USSize)
13395 size_hints.flags |= USSize;
13396 }
13397 }
13398
13399 #ifndef USE_X_TOOLKIT
13400 no_read:
13401 #endif
13402
13403 #ifdef PWinGravity
13404 size_hints.win_gravity = f->output_data.x->win_gravity;
13405 size_hints.flags |= PWinGravity;
13406
13407 if (user_position)
13408 {
13409 size_hints.flags &= ~ PPosition;
13410 size_hints.flags |= USPosition;
13411 }
13412 #endif /* PWinGravity */
13413
13414 #ifdef HAVE_X11R4
13415 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13416 #else
13417 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13418 #endif
13419 }
13420
13421 /* Used for IconicState or NormalState */
13422
13423 void
13424 x_wm_set_window_state (f, state)
13425 struct frame *f;
13426 int state;
13427 {
13428 #ifdef USE_X_TOOLKIT
13429 Arg al[1];
13430
13431 XtSetArg (al[0], XtNinitialState, state);
13432 XtSetValues (f->output_data.x->widget, al, 1);
13433 #else /* not USE_X_TOOLKIT */
13434 Window window = FRAME_X_WINDOW (f);
13435
13436 f->output_data.x->wm_hints.flags |= StateHint;
13437 f->output_data.x->wm_hints.initial_state = state;
13438
13439 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13440 #endif /* not USE_X_TOOLKIT */
13441 }
13442
13443 void
13444 x_wm_set_icon_pixmap (f, pixmap_id)
13445 struct frame *f;
13446 int pixmap_id;
13447 {
13448 Pixmap icon_pixmap;
13449
13450 #ifndef USE_X_TOOLKIT
13451 Window window = FRAME_X_WINDOW (f);
13452 #endif
13453
13454 if (pixmap_id > 0)
13455 {
13456 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
13457 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
13458 }
13459 else
13460 {
13461 /* It seems there is no way to turn off use of an icon pixmap.
13462 The following line does it, only if no icon has yet been created,
13463 for some window managers. But with mwm it crashes.
13464 Some people say it should clear the IconPixmapHint bit in this case,
13465 but that doesn't work, and the X consortium said it isn't the
13466 right thing at all. Since there is no way to win,
13467 best to explicitly give up. */
13468 #if 0
13469 f->output_data.x->wm_hints.icon_pixmap = None;
13470 #else
13471 return;
13472 #endif
13473 }
13474
13475 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
13476
13477 {
13478 Arg al[1];
13479 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
13480 XtSetValues (f->output_data.x->widget, al, 1);
13481 }
13482
13483 #else /* not USE_X_TOOLKIT */
13484
13485 f->output_data.x->wm_hints.flags |= IconPixmapHint;
13486 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13487
13488 #endif /* not USE_X_TOOLKIT */
13489 }
13490
13491 void
13492 x_wm_set_icon_position (f, icon_x, icon_y)
13493 struct frame *f;
13494 int icon_x, icon_y;
13495 {
13496 #ifdef USE_X_TOOLKIT
13497 Window window = XtWindow (f->output_data.x->widget);
13498 #else
13499 Window window = FRAME_X_WINDOW (f);
13500 #endif
13501
13502 f->output_data.x->wm_hints.flags |= IconPositionHint;
13503 f->output_data.x->wm_hints.icon_x = icon_x;
13504 f->output_data.x->wm_hints.icon_y = icon_y;
13505
13506 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13507 }
13508
13509 \f
13510 /***********************************************************************
13511 Fonts
13512 ***********************************************************************/
13513
13514 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
13515
13516 struct font_info *
13517 x_get_font_info (f, font_idx)
13518 FRAME_PTR f;
13519 int font_idx;
13520 {
13521 return (FRAME_X_FONT_TABLE (f) + font_idx);
13522 }
13523
13524
13525 /* Return a list of names of available fonts matching PATTERN on frame F.
13526
13527 If SIZE is > 0, it is the size (maximum bounds width) of fonts
13528 to be listed.
13529
13530 SIZE < 0 means include scalable fonts.
13531
13532 Frame F null means we have not yet created any frame on X, and
13533 consult the first display in x_display_list. MAXNAMES sets a limit
13534 on how many fonts to match. */
13535
13536 Lisp_Object
13537 x_list_fonts (f, pattern, size, maxnames)
13538 struct frame *f;
13539 Lisp_Object pattern;
13540 int size;
13541 int maxnames;
13542 {
13543 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
13544 Lisp_Object tem, second_best;
13545 struct x_display_info *dpyinfo
13546 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
13547 Display *dpy = dpyinfo->display;
13548 int try_XLoadQueryFont = 0;
13549 int count;
13550 int allow_scalable_fonts_p = 0;
13551
13552 if (size < 0)
13553 {
13554 allow_scalable_fonts_p = 1;
13555 size = 0;
13556 }
13557
13558 patterns = Fassoc (pattern, Valternate_fontname_alist);
13559 if (NILP (patterns))
13560 patterns = Fcons (pattern, Qnil);
13561
13562 if (maxnames == 1 && !size)
13563 /* We can return any single font matching PATTERN. */
13564 try_XLoadQueryFont = 1;
13565
13566 for (; CONSP (patterns); patterns = XCDR (patterns))
13567 {
13568 int num_fonts;
13569 char **names = NULL;
13570
13571 pattern = XCAR (patterns);
13572 /* See if we cached the result for this particular query.
13573 The cache is an alist of the form:
13574 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
13575 tem = XCDR (dpyinfo->name_list_element);
13576 key = Fcons (Fcons (pattern, make_number (maxnames)),
13577 allow_scalable_fonts_p ? Qt : Qnil);
13578 list = Fassoc (key, tem);
13579 if (!NILP (list))
13580 {
13581 list = Fcdr_safe (list);
13582 /* We have a cashed list. Don't have to get the list again. */
13583 goto label_cached;
13584 }
13585
13586 /* At first, put PATTERN in the cache. */
13587
13588 BLOCK_INPUT;
13589 count = x_catch_errors (dpy);
13590
13591 if (try_XLoadQueryFont)
13592 {
13593 XFontStruct *font;
13594 unsigned long value;
13595
13596 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
13597 if (x_had_errors_p (dpy))
13598 {
13599 /* This error is perhaps due to insufficient memory on X
13600 server. Let's just ignore it. */
13601 font = NULL;
13602 x_clear_errors (dpy);
13603 }
13604
13605 if (font
13606 && XGetFontProperty (font, XA_FONT, &value))
13607 {
13608 char *name = (char *) XGetAtomName (dpy, (Atom) value);
13609 int len = strlen (name);
13610 char *tmp;
13611
13612 /* If DXPC (a Differential X Protocol Compressor)
13613 Ver.3.7 is running, XGetAtomName will return null
13614 string. We must avoid such a name. */
13615 if (len == 0)
13616 try_XLoadQueryFont = 0;
13617 else
13618 {
13619 num_fonts = 1;
13620 names = (char **) alloca (sizeof (char *));
13621 /* Some systems only allow alloca assigned to a
13622 simple var. */
13623 tmp = (char *) alloca (len + 1); names[0] = tmp;
13624 bcopy (name, names[0], len + 1);
13625 XFree (name);
13626 }
13627 }
13628 else
13629 try_XLoadQueryFont = 0;
13630
13631 if (font)
13632 XFreeFont (dpy, font);
13633 }
13634
13635 if (!try_XLoadQueryFont)
13636 {
13637 /* We try at least 10 fonts because XListFonts will return
13638 auto-scaled fonts at the head. */
13639 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
13640 &num_fonts);
13641 if (x_had_errors_p (dpy))
13642 {
13643 /* This error is perhaps due to insufficient memory on X
13644 server. Let's just ignore it. */
13645 names = NULL;
13646 x_clear_errors (dpy);
13647 }
13648 }
13649
13650 x_uncatch_errors (dpy, count);
13651 UNBLOCK_INPUT;
13652
13653 if (names)
13654 {
13655 int i;
13656
13657 /* Make a list of all the fonts we got back.
13658 Store that in the font cache for the display. */
13659 for (i = 0; i < num_fonts; i++)
13660 {
13661 int width = 0;
13662 char *p = names[i];
13663 int average_width = -1, dashes = 0;
13664
13665 /* Count the number of dashes in NAMES[I]. If there are
13666 14 dashes, and the field value following 12th dash
13667 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
13668 is usually too ugly to be used for editing. Let's
13669 ignore it. */
13670 while (*p)
13671 if (*p++ == '-')
13672 {
13673 dashes++;
13674 if (dashes == 7) /* PIXEL_SIZE field */
13675 width = atoi (p);
13676 else if (dashes == 12) /* AVERAGE_WIDTH field */
13677 average_width = atoi (p);
13678 }
13679
13680 if (allow_scalable_fonts_p
13681 || dashes < 14 || average_width != 0)
13682 {
13683 tem = build_string (names[i]);
13684 if (NILP (Fassoc (tem, list)))
13685 {
13686 if (STRINGP (Vx_pixel_size_width_font_regexp)
13687 && ((fast_c_string_match_ignore_case
13688 (Vx_pixel_size_width_font_regexp, names[i]))
13689 >= 0))
13690 /* We can set the value of PIXEL_SIZE to the
13691 width of this font. */
13692 list = Fcons (Fcons (tem, make_number (width)), list);
13693 else
13694 /* For the moment, width is not known. */
13695 list = Fcons (Fcons (tem, Qnil), list);
13696 }
13697 }
13698 }
13699
13700 if (!try_XLoadQueryFont)
13701 {
13702 BLOCK_INPUT;
13703 XFreeFontNames (names);
13704 UNBLOCK_INPUT;
13705 }
13706 }
13707
13708 /* Now store the result in the cache. */
13709 XCDR (dpyinfo->name_list_element)
13710 = Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element));
13711
13712 label_cached:
13713 if (NILP (list)) continue; /* Try the remaining alternatives. */
13714
13715 newlist = second_best = Qnil;
13716 /* Make a list of the fonts that have the right width. */
13717 for (; CONSP (list); list = XCDR (list))
13718 {
13719 int found_size;
13720
13721 tem = XCAR (list);
13722
13723 if (!CONSP (tem) || NILP (XCAR (tem)))
13724 continue;
13725 if (!size)
13726 {
13727 newlist = Fcons (XCAR (tem), newlist);
13728 continue;
13729 }
13730
13731 if (!INTEGERP (XCDR (tem)))
13732 {
13733 /* Since we have not yet known the size of this font, we
13734 must try slow function call XLoadQueryFont. */
13735 XFontStruct *thisinfo;
13736
13737 BLOCK_INPUT;
13738 count = x_catch_errors (dpy);
13739 thisinfo = XLoadQueryFont (dpy,
13740 XSTRING (XCAR (tem))->data);
13741 if (x_had_errors_p (dpy))
13742 {
13743 /* This error is perhaps due to insufficient memory on X
13744 server. Let's just ignore it. */
13745 thisinfo = NULL;
13746 x_clear_errors (dpy);
13747 }
13748 x_uncatch_errors (dpy, count);
13749 UNBLOCK_INPUT;
13750
13751 if (thisinfo)
13752 {
13753 XCDR (tem)
13754 = (thisinfo->min_bounds.width == 0
13755 ? make_number (0)
13756 : make_number (thisinfo->max_bounds.width));
13757 BLOCK_INPUT;
13758 XFreeFont (dpy, thisinfo);
13759 UNBLOCK_INPUT;
13760 }
13761 else
13762 /* For unknown reason, the previous call of XListFont had
13763 returned a font which can't be opened. Record the size
13764 as 0 not to try to open it again. */
13765 XCDR (tem) = make_number (0);
13766 }
13767
13768 found_size = XINT (XCDR (tem));
13769 if (found_size == size)
13770 newlist = Fcons (XCAR (tem), newlist);
13771 else if (found_size > 0)
13772 {
13773 if (NILP (second_best))
13774 second_best = tem;
13775 else if (found_size < size)
13776 {
13777 if (XINT (XCDR (second_best)) > size
13778 || XINT (XCDR (second_best)) < found_size)
13779 second_best = tem;
13780 }
13781 else
13782 {
13783 if (XINT (XCDR (second_best)) > size
13784 && XINT (XCDR (second_best)) > found_size)
13785 second_best = tem;
13786 }
13787 }
13788 }
13789 if (!NILP (newlist))
13790 break;
13791 else if (!NILP (second_best))
13792 {
13793 newlist = Fcons (XCAR (second_best), Qnil);
13794 break;
13795 }
13796 }
13797
13798 return newlist;
13799 }
13800
13801
13802 #if GLYPH_DEBUG
13803
13804 /* Check that FONT is valid on frame F. It is if it can be found in F's
13805 font table. */
13806
13807 static void
13808 x_check_font (f, font)
13809 struct frame *f;
13810 XFontStruct *font;
13811 {
13812 int i;
13813 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13814
13815 xassert (font != NULL);
13816
13817 for (i = 0; i < dpyinfo->n_fonts; i++)
13818 if (dpyinfo->font_table[i].name
13819 && font == dpyinfo->font_table[i].font)
13820 break;
13821
13822 xassert (i < dpyinfo->n_fonts);
13823 }
13824
13825 #endif /* GLYPH_DEBUG != 0 */
13826
13827 /* Set *W to the minimum width, *H to the minimum font height of FONT.
13828 Note: There are (broken) X fonts out there with invalid XFontStruct
13829 min_bounds contents. For example, handa@etl.go.jp reports that
13830 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
13831 have font->min_bounds.width == 0. */
13832
13833 static INLINE void
13834 x_font_min_bounds (font, w, h)
13835 XFontStruct *font;
13836 int *w, *h;
13837 {
13838 *h = FONT_HEIGHT (font);
13839 *w = font->min_bounds.width;
13840
13841 /* Try to handle the case where FONT->min_bounds has invalid
13842 contents. Since the only font known to have invalid min_bounds
13843 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
13844 if (*w <= 0)
13845 *w = font->max_bounds.width;
13846 }
13847
13848
13849 /* Compute the smallest character width and smallest font height over
13850 all fonts available on frame F. Set the members smallest_char_width
13851 and smallest_font_height in F's x_display_info structure to
13852 the values computed. Value is non-zero if smallest_font_height or
13853 smallest_char_width become smaller than they were before. */
13854
13855 static int
13856 x_compute_min_glyph_bounds (f)
13857 struct frame *f;
13858 {
13859 int i;
13860 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13861 XFontStruct *font;
13862 int old_width = dpyinfo->smallest_char_width;
13863 int old_height = dpyinfo->smallest_font_height;
13864
13865 dpyinfo->smallest_font_height = 100000;
13866 dpyinfo->smallest_char_width = 100000;
13867
13868 for (i = 0; i < dpyinfo->n_fonts; ++i)
13869 if (dpyinfo->font_table[i].name)
13870 {
13871 struct font_info *fontp = dpyinfo->font_table + i;
13872 int w, h;
13873
13874 font = (XFontStruct *) fontp->font;
13875 xassert (font != (XFontStruct *) ~0);
13876 x_font_min_bounds (font, &w, &h);
13877
13878 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
13879 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
13880 }
13881
13882 xassert (dpyinfo->smallest_char_width > 0
13883 && dpyinfo->smallest_font_height > 0);
13884
13885 return (dpyinfo->n_fonts == 1
13886 || dpyinfo->smallest_char_width < old_width
13887 || dpyinfo->smallest_font_height < old_height);
13888 }
13889
13890
13891 /* Load font named FONTNAME of the size SIZE for frame F, and return a
13892 pointer to the structure font_info while allocating it dynamically.
13893 If SIZE is 0, load any size of font.
13894 If loading is failed, return NULL. */
13895
13896 struct font_info *
13897 x_load_font (f, fontname, size)
13898 struct frame *f;
13899 register char *fontname;
13900 int size;
13901 {
13902 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13903 Lisp_Object font_names;
13904 int count;
13905
13906 /* Get a list of all the fonts that match this name. Once we
13907 have a list of matching fonts, we compare them against the fonts
13908 we already have by comparing names. */
13909 font_names = x_list_fonts (f, build_string (fontname), size, 1);
13910
13911 if (!NILP (font_names))
13912 {
13913 Lisp_Object tail;
13914 int i;
13915
13916 for (i = 0; i < dpyinfo->n_fonts; i++)
13917 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
13918 if (dpyinfo->font_table[i].name
13919 && (!strcmp (dpyinfo->font_table[i].name,
13920 XSTRING (XCAR (tail))->data)
13921 || !strcmp (dpyinfo->font_table[i].full_name,
13922 XSTRING (XCAR (tail))->data)))
13923 return (dpyinfo->font_table + i);
13924 }
13925
13926 /* Load the font and add it to the table. */
13927 {
13928 char *full_name;
13929 XFontStruct *font;
13930 struct font_info *fontp;
13931 unsigned long value;
13932 int i;
13933
13934 /* If we have found fonts by x_list_font, load one of them. If
13935 not, we still try to load a font by the name given as FONTNAME
13936 because XListFonts (called in x_list_font) of some X server has
13937 a bug of not finding a font even if the font surely exists and
13938 is loadable by XLoadQueryFont. */
13939 if (size > 0 && !NILP (font_names))
13940 fontname = (char *) XSTRING (XCAR (font_names))->data;
13941
13942 BLOCK_INPUT;
13943 count = x_catch_errors (FRAME_X_DISPLAY (f));
13944 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
13945 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
13946 {
13947 /* This error is perhaps due to insufficient memory on X
13948 server. Let's just ignore it. */
13949 font = NULL;
13950 x_clear_errors (FRAME_X_DISPLAY (f));
13951 }
13952 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
13953 UNBLOCK_INPUT;
13954 if (!font)
13955 return NULL;
13956
13957 /* Find a free slot in the font table. */
13958 for (i = 0; i < dpyinfo->n_fonts; ++i)
13959 if (dpyinfo->font_table[i].name == NULL)
13960 break;
13961
13962 /* If no free slot found, maybe enlarge the font table. */
13963 if (i == dpyinfo->n_fonts
13964 && dpyinfo->n_fonts == dpyinfo->font_table_size)
13965 {
13966 int sz;
13967 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
13968 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
13969 dpyinfo->font_table
13970 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
13971 }
13972
13973 fontp = dpyinfo->font_table + i;
13974 if (i == dpyinfo->n_fonts)
13975 ++dpyinfo->n_fonts;
13976
13977 /* Now fill in the slots of *FONTP. */
13978 BLOCK_INPUT;
13979 fontp->font = font;
13980 fontp->font_idx = i;
13981 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
13982 bcopy (fontname, fontp->name, strlen (fontname) + 1);
13983
13984 /* Try to get the full name of FONT. Put it in FULL_NAME. */
13985 full_name = 0;
13986 if (XGetFontProperty (font, XA_FONT, &value))
13987 {
13988 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
13989 char *p = name;
13990 int dashes = 0;
13991
13992 /* Count the number of dashes in the "full name".
13993 If it is too few, this isn't really the font's full name,
13994 so don't use it.
13995 In X11R4, the fonts did not come with their canonical names
13996 stored in them. */
13997 while (*p)
13998 {
13999 if (*p == '-')
14000 dashes++;
14001 p++;
14002 }
14003
14004 if (dashes >= 13)
14005 {
14006 full_name = (char *) xmalloc (p - name + 1);
14007 bcopy (name, full_name, p - name + 1);
14008 }
14009
14010 XFree (name);
14011 }
14012
14013 if (full_name != 0)
14014 fontp->full_name = full_name;
14015 else
14016 fontp->full_name = fontp->name;
14017
14018 fontp->size = font->max_bounds.width;
14019 fontp->height = FONT_HEIGHT (font);
14020
14021 if (NILP (font_names))
14022 {
14023 /* We come here because of a bug of XListFonts mentioned at
14024 the head of this block. Let's store this information in
14025 the cache for x_list_fonts. */
14026 Lisp_Object lispy_name = build_string (fontname);
14027 Lisp_Object lispy_full_name = build_string (fontp->full_name);
14028 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
14029 Qnil);
14030
14031 XCDR (dpyinfo->name_list_element)
14032 = Fcons (Fcons (key,
14033 Fcons (Fcons (lispy_full_name,
14034 make_number (fontp->size)),
14035 Qnil)),
14036 XCDR (dpyinfo->name_list_element));
14037 if (full_name)
14038 {
14039 key = Fcons (Fcons (lispy_full_name, make_number (256)),
14040 Qnil);
14041 XCDR (dpyinfo->name_list_element)
14042 = Fcons (Fcons (key,
14043 Fcons (Fcons (lispy_full_name,
14044 make_number (fontp->size)),
14045 Qnil)),
14046 XCDR (dpyinfo->name_list_element));
14047 }
14048 }
14049
14050 /* The slot `encoding' specifies how to map a character
14051 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
14052 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
14053 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
14054 2:0xA020..0xFF7F). For the moment, we don't know which charset
14055 uses this font. So, we set information in fontp->encoding[1]
14056 which is never used by any charset. If mapping can't be
14057 decided, set FONT_ENCODING_NOT_DECIDED. */
14058 fontp->encoding[1]
14059 = (font->max_byte1 == 0
14060 /* 1-byte font */
14061 ? (font->min_char_or_byte2 < 0x80
14062 ? (font->max_char_or_byte2 < 0x80
14063 ? 0 /* 0x20..0x7F */
14064 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
14065 : 1) /* 0xA0..0xFF */
14066 /* 2-byte font */
14067 : (font->min_byte1 < 0x80
14068 ? (font->max_byte1 < 0x80
14069 ? (font->min_char_or_byte2 < 0x80
14070 ? (font->max_char_or_byte2 < 0x80
14071 ? 0 /* 0x2020..0x7F7F */
14072 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
14073 : 3) /* 0x20A0..0x7FFF */
14074 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
14075 : (font->min_char_or_byte2 < 0x80
14076 ? (font->max_char_or_byte2 < 0x80
14077 ? 2 /* 0xA020..0xFF7F */
14078 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
14079 : 1))); /* 0xA0A0..0xFFFF */
14080
14081 fontp->baseline_offset
14082 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
14083 ? (long) value : 0);
14084 fontp->relative_compose
14085 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
14086 ? (long) value : 0);
14087 fontp->default_ascent
14088 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
14089 ? (long) value : 0);
14090
14091 /* Set global flag fonts_changed_p to non-zero if the font loaded
14092 has a character with a smaller width than any other character
14093 before, or if the font loaded has a smalle>r height than any
14094 other font loaded before. If this happens, it will make a
14095 glyph matrix reallocation necessary. */
14096 fonts_changed_p = x_compute_min_glyph_bounds (f);
14097 UNBLOCK_INPUT;
14098 return fontp;
14099 }
14100 }
14101
14102
14103 /* Return a pointer to struct font_info of a font named FONTNAME for
14104 frame F. If no such font is loaded, return NULL. */
14105
14106 struct font_info *
14107 x_query_font (f, fontname)
14108 struct frame *f;
14109 register char *fontname;
14110 {
14111 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14112 int i;
14113
14114 for (i = 0; i < dpyinfo->n_fonts; i++)
14115 if (dpyinfo->font_table[i].name
14116 && (!strcmp (dpyinfo->font_table[i].name, fontname)
14117 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
14118 return (dpyinfo->font_table + i);
14119 return NULL;
14120 }
14121
14122
14123 /* Find a CCL program for a font specified by FONTP, and set the member
14124 `encoder' of the structure. */
14125
14126 void
14127 x_find_ccl_program (fontp)
14128 struct font_info *fontp;
14129 {
14130 Lisp_Object list, elt;
14131
14132 elt = Qnil;
14133 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
14134 {
14135 elt = XCAR (list);
14136 if (CONSP (elt)
14137 && STRINGP (XCAR (elt))
14138 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
14139 >= 0)
14140 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
14141 >= 0)))
14142 break;
14143 }
14144
14145 if (! NILP (list))
14146 {
14147 struct ccl_program *ccl
14148 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
14149
14150 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
14151 xfree (ccl);
14152 else
14153 fontp->font_encoder = ccl;
14154 }
14155 }
14156
14157
14158 \f
14159 /***********************************************************************
14160 Initialization
14161 ***********************************************************************/
14162
14163 #ifdef USE_X_TOOLKIT
14164 static XrmOptionDescRec emacs_options[] = {
14165 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
14166 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
14167
14168 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
14169 XrmoptionSepArg, NULL},
14170 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
14171
14172 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14173 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14174 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14175 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14176 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14177 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
14178 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
14179 };
14180 #endif /* USE_X_TOOLKIT */
14181
14182 static int x_initialized;
14183
14184 #ifdef MULTI_KBOARD
14185 /* Test whether two display-name strings agree up to the dot that separates
14186 the screen number from the server number. */
14187 static int
14188 same_x_server (name1, name2)
14189 char *name1, *name2;
14190 {
14191 int seen_colon = 0;
14192 unsigned char *system_name = XSTRING (Vsystem_name)->data;
14193 int system_name_length = strlen (system_name);
14194 int length_until_period = 0;
14195
14196 while (system_name[length_until_period] != 0
14197 && system_name[length_until_period] != '.')
14198 length_until_period++;
14199
14200 /* Treat `unix' like an empty host name. */
14201 if (! strncmp (name1, "unix:", 5))
14202 name1 += 4;
14203 if (! strncmp (name2, "unix:", 5))
14204 name2 += 4;
14205 /* Treat this host's name like an empty host name. */
14206 if (! strncmp (name1, system_name, system_name_length)
14207 && name1[system_name_length] == ':')
14208 name1 += system_name_length;
14209 if (! strncmp (name2, system_name, system_name_length)
14210 && name2[system_name_length] == ':')
14211 name2 += system_name_length;
14212 /* Treat this host's domainless name like an empty host name. */
14213 if (! strncmp (name1, system_name, length_until_period)
14214 && name1[length_until_period] == ':')
14215 name1 += length_until_period;
14216 if (! strncmp (name2, system_name, length_until_period)
14217 && name2[length_until_period] == ':')
14218 name2 += length_until_period;
14219
14220 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
14221 {
14222 if (*name1 == ':')
14223 seen_colon++;
14224 if (seen_colon && *name1 == '.')
14225 return 1;
14226 }
14227 return (seen_colon
14228 && (*name1 == '.' || *name1 == '\0')
14229 && (*name2 == '.' || *name2 == '\0'));
14230 }
14231 #endif
14232
14233 struct x_display_info *
14234 x_term_init (display_name, xrm_option, resource_name)
14235 Lisp_Object display_name;
14236 char *xrm_option;
14237 char *resource_name;
14238 {
14239 int connection;
14240 Display *dpy;
14241 struct x_display_info *dpyinfo;
14242 XrmDatabase xrdb;
14243
14244 BLOCK_INPUT;
14245
14246 if (!x_initialized)
14247 {
14248 x_initialize ();
14249 x_initialized = 1;
14250 }
14251
14252 #ifdef USE_X_TOOLKIT
14253 /* weiner@footloose.sps.mot.com reports that this causes
14254 errors with X11R5:
14255 X protocol error: BadAtom (invalid Atom parameter)
14256 on protocol request 18skiloaf.
14257 So let's not use it until R6. */
14258 #ifdef HAVE_X11XTR6
14259 XtSetLanguageProc (NULL, NULL, NULL);
14260 #endif
14261
14262 {
14263 int argc = 0;
14264 char *argv[3];
14265
14266 argv[0] = "";
14267 argc = 1;
14268 if (xrm_option)
14269 {
14270 argv[argc++] = "-xrm";
14271 argv[argc++] = xrm_option;
14272 }
14273 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
14274 resource_name, EMACS_CLASS,
14275 emacs_options, XtNumber (emacs_options),
14276 &argc, argv);
14277
14278 #ifdef HAVE_X11XTR6
14279 /* I think this is to compensate for XtSetLanguageProc. */
14280 fixup_locale ();
14281 #endif
14282 }
14283
14284 #else /* not USE_X_TOOLKIT */
14285 #ifdef HAVE_X11R5
14286 XSetLocaleModifiers ("");
14287 #endif
14288 dpy = XOpenDisplay (XSTRING (display_name)->data);
14289 #endif /* not USE_X_TOOLKIT */
14290
14291 /* Detect failure. */
14292 if (dpy == 0)
14293 {
14294 UNBLOCK_INPUT;
14295 return 0;
14296 }
14297
14298 /* We have definitely succeeded. Record the new connection. */
14299
14300 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
14301 bzero (dpyinfo, sizeof *dpyinfo);
14302
14303 #ifdef MULTI_KBOARD
14304 {
14305 struct x_display_info *share;
14306 Lisp_Object tail;
14307
14308 for (share = x_display_list, tail = x_display_name_list; share;
14309 share = share->next, tail = XCDR (tail))
14310 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
14311 XSTRING (display_name)->data))
14312 break;
14313 if (share)
14314 dpyinfo->kboard = share->kboard;
14315 else
14316 {
14317 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
14318 init_kboard (dpyinfo->kboard);
14319 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
14320 {
14321 char *vendor = ServerVendor (dpy);
14322 UNBLOCK_INPUT;
14323 dpyinfo->kboard->Vsystem_key_alist
14324 = call1 (Qvendor_specific_keysyms,
14325 build_string (vendor ? vendor : ""));
14326 BLOCK_INPUT;
14327 }
14328
14329 dpyinfo->kboard->next_kboard = all_kboards;
14330 all_kboards = dpyinfo->kboard;
14331 /* Don't let the initial kboard remain current longer than necessary.
14332 That would cause problems if a file loaded on startup tries to
14333 prompt in the mini-buffer. */
14334 if (current_kboard == initial_kboard)
14335 current_kboard = dpyinfo->kboard;
14336 }
14337 dpyinfo->kboard->reference_count++;
14338 }
14339 #endif
14340
14341 /* Put this display on the chain. */
14342 dpyinfo->next = x_display_list;
14343 x_display_list = dpyinfo;
14344
14345 /* Put it on x_display_name_list as well, to keep them parallel. */
14346 x_display_name_list = Fcons (Fcons (display_name, Qnil),
14347 x_display_name_list);
14348 dpyinfo->name_list_element = XCAR (x_display_name_list);
14349
14350 dpyinfo->display = dpy;
14351
14352 #if 0
14353 XSetAfterFunction (x_current_display, x_trace_wire);
14354 #endif /* ! 0 */
14355
14356 dpyinfo->x_id_name
14357 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
14358 + STRING_BYTES (XSTRING (Vsystem_name))
14359 + 2);
14360 sprintf (dpyinfo->x_id_name, "%s@%s",
14361 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
14362
14363 /* Figure out which modifier bits mean what. */
14364 x_find_modifier_meanings (dpyinfo);
14365
14366 /* Get the scroll bar cursor. */
14367 dpyinfo->vertical_scroll_bar_cursor
14368 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
14369
14370 xrdb = x_load_resources (dpyinfo->display, xrm_option,
14371 resource_name, EMACS_CLASS);
14372 #ifdef HAVE_XRMSETDATABASE
14373 XrmSetDatabase (dpyinfo->display, xrdb);
14374 #else
14375 dpyinfo->display->db = xrdb;
14376 #endif
14377 /* Put the rdb where we can find it in a way that works on
14378 all versions. */
14379 dpyinfo->xrdb = xrdb;
14380
14381 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
14382 DefaultScreen (dpyinfo->display));
14383 select_visual (dpyinfo);
14384 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
14385 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
14386 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
14387 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
14388 dpyinfo->grabbed = 0;
14389 dpyinfo->reference_count = 0;
14390 dpyinfo->icon_bitmap_id = -1;
14391 dpyinfo->font_table = NULL;
14392 dpyinfo->n_fonts = 0;
14393 dpyinfo->font_table_size = 0;
14394 dpyinfo->bitmaps = 0;
14395 dpyinfo->bitmaps_size = 0;
14396 dpyinfo->bitmaps_last = 0;
14397 dpyinfo->scratch_cursor_gc = 0;
14398 dpyinfo->mouse_face_mouse_frame = 0;
14399 dpyinfo->mouse_face_deferred_gc = 0;
14400 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
14401 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
14402 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
14403 dpyinfo->mouse_face_window = Qnil;
14404 dpyinfo->mouse_face_overlay = Qnil;
14405 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
14406 dpyinfo->mouse_face_defer = 0;
14407 dpyinfo->x_focus_frame = 0;
14408 dpyinfo->x_focus_event_frame = 0;
14409 dpyinfo->x_highlight_frame = 0;
14410 dpyinfo->image_cache = make_image_cache ();
14411
14412 /* See if a private colormap is requested. */
14413 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
14414 {
14415 if (dpyinfo->visual->class == PseudoColor)
14416 {
14417 Lisp_Object value;
14418 value = display_x_get_resource (dpyinfo,
14419 build_string ("privateColormap"),
14420 build_string ("PrivateColormap"),
14421 Qnil, Qnil);
14422 if (STRINGP (value)
14423 && (!strcmp (XSTRING (value)->data, "true")
14424 || !strcmp (XSTRING (value)->data, "on")))
14425 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
14426 }
14427 }
14428 else
14429 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
14430 dpyinfo->visual, AllocNone);
14431
14432 {
14433 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
14434 double pixels = DisplayHeight (dpyinfo->display, screen_number);
14435 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
14436 dpyinfo->resy = pixels * 25.4 / mm;
14437 pixels = DisplayWidth (dpyinfo->display, screen_number);
14438 mm = DisplayWidthMM (dpyinfo->display, screen_number);
14439 dpyinfo->resx = pixels * 25.4 / mm;
14440 }
14441
14442 dpyinfo->Xatom_wm_protocols
14443 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
14444 dpyinfo->Xatom_wm_take_focus
14445 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
14446 dpyinfo->Xatom_wm_save_yourself
14447 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
14448 dpyinfo->Xatom_wm_delete_window
14449 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
14450 dpyinfo->Xatom_wm_change_state
14451 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
14452 dpyinfo->Xatom_wm_configure_denied
14453 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
14454 dpyinfo->Xatom_wm_window_moved
14455 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
14456 dpyinfo->Xatom_editres
14457 = XInternAtom (dpyinfo->display, "Editres", False);
14458 dpyinfo->Xatom_CLIPBOARD
14459 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
14460 dpyinfo->Xatom_TIMESTAMP
14461 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
14462 dpyinfo->Xatom_TEXT
14463 = XInternAtom (dpyinfo->display, "TEXT", False);
14464 dpyinfo->Xatom_COMPOUND_TEXT
14465 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
14466 dpyinfo->Xatom_DELETE
14467 = XInternAtom (dpyinfo->display, "DELETE", False);
14468 dpyinfo->Xatom_MULTIPLE
14469 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
14470 dpyinfo->Xatom_INCR
14471 = XInternAtom (dpyinfo->display, "INCR", False);
14472 dpyinfo->Xatom_EMACS_TMP
14473 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
14474 dpyinfo->Xatom_TARGETS
14475 = XInternAtom (dpyinfo->display, "TARGETS", False);
14476 dpyinfo->Xatom_NULL
14477 = XInternAtom (dpyinfo->display, "NULL", False);
14478 dpyinfo->Xatom_ATOM_PAIR
14479 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
14480 /* For properties of font. */
14481 dpyinfo->Xatom_PIXEL_SIZE
14482 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
14483 dpyinfo->Xatom_MULE_BASELINE_OFFSET
14484 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
14485 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
14486 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
14487 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
14488 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
14489
14490 /* Ghostscript support. */
14491 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
14492 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
14493
14494 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
14495 False);
14496
14497 dpyinfo->cut_buffers_initialized = 0;
14498
14499 connection = ConnectionNumber (dpyinfo->display);
14500 dpyinfo->connection = connection;
14501
14502 {
14503 char null_bits[1];
14504
14505 null_bits[0] = 0x00;
14506
14507 dpyinfo->null_pixel
14508 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14509 null_bits, 1, 1, (long) 0, (long) 0,
14510 1);
14511 }
14512
14513 {
14514 extern int gray_bitmap_width, gray_bitmap_height;
14515 extern char *gray_bitmap_bits;
14516 dpyinfo->gray
14517 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14518 gray_bitmap_bits,
14519 gray_bitmap_width, gray_bitmap_height,
14520 (unsigned long) 1, (unsigned long) 0, 1);
14521 }
14522
14523 #ifdef HAVE_X_I18N
14524 xim_initialize (dpyinfo, resource_name);
14525 #endif
14526
14527 #ifdef subprocesses
14528 /* This is only needed for distinguishing keyboard and process input. */
14529 if (connection != 0)
14530 add_keyboard_wait_descriptor (connection);
14531 #endif
14532
14533 #ifndef F_SETOWN_BUG
14534 #ifdef F_SETOWN
14535 #ifdef F_SETOWN_SOCK_NEG
14536 /* stdin is a socket here */
14537 fcntl (connection, F_SETOWN, -getpid ());
14538 #else /* ! defined (F_SETOWN_SOCK_NEG) */
14539 fcntl (connection, F_SETOWN, getpid ());
14540 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
14541 #endif /* ! defined (F_SETOWN) */
14542 #endif /* F_SETOWN_BUG */
14543
14544 #ifdef SIGIO
14545 if (interrupt_input)
14546 init_sigio (connection);
14547 #endif /* ! defined (SIGIO) */
14548
14549 #ifdef USE_LUCID
14550 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
14551 /* Make sure that we have a valid font for dialog boxes
14552 so that Xt does not crash. */
14553 {
14554 Display *dpy = dpyinfo->display;
14555 XrmValue d, fr, to;
14556 Font font;
14557 int count;
14558
14559 d.addr = (XPointer)&dpy;
14560 d.size = sizeof (Display *);
14561 fr.addr = XtDefaultFont;
14562 fr.size = sizeof (XtDefaultFont);
14563 to.size = sizeof (Font *);
14564 to.addr = (XPointer)&font;
14565 count = x_catch_errors (dpy);
14566 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
14567 abort ();
14568 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
14569 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
14570 x_uncatch_errors (dpy, count);
14571 }
14572 #endif
14573 #endif
14574
14575 /* See if we should run in synchronous mode. This is useful
14576 for debugging X code. */
14577 {
14578 Lisp_Object value;
14579 value = display_x_get_resource (dpyinfo,
14580 build_string ("synchronous"),
14581 build_string ("Synchronous"),
14582 Qnil, Qnil);
14583 if (STRINGP (value)
14584 && (!strcmp (XSTRING (value)->data, "true")
14585 || !strcmp (XSTRING (value)->data, "on")))
14586 XSynchronize (dpyinfo->display, True);
14587 }
14588
14589 UNBLOCK_INPUT;
14590
14591 return dpyinfo;
14592 }
14593 \f
14594 /* Get rid of display DPYINFO, assuming all frames are already gone,
14595 and without sending any more commands to the X server. */
14596
14597 void
14598 x_delete_display (dpyinfo)
14599 struct x_display_info *dpyinfo;
14600 {
14601 delete_keyboard_wait_descriptor (dpyinfo->connection);
14602
14603 /* Discard this display from x_display_name_list and x_display_list.
14604 We can't use Fdelq because that can quit. */
14605 if (! NILP (x_display_name_list)
14606 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
14607 x_display_name_list = XCDR (x_display_name_list);
14608 else
14609 {
14610 Lisp_Object tail;
14611
14612 tail = x_display_name_list;
14613 while (CONSP (tail) && CONSP (XCDR (tail)))
14614 {
14615 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
14616 {
14617 XCDR (tail) = XCDR (XCDR (tail));
14618 break;
14619 }
14620 tail = XCDR (tail);
14621 }
14622 }
14623
14624 if (next_noop_dpyinfo == dpyinfo)
14625 next_noop_dpyinfo = dpyinfo->next;
14626
14627 if (x_display_list == dpyinfo)
14628 x_display_list = dpyinfo->next;
14629 else
14630 {
14631 struct x_display_info *tail;
14632
14633 for (tail = x_display_list; tail; tail = tail->next)
14634 if (tail->next == dpyinfo)
14635 tail->next = tail->next->next;
14636 }
14637
14638 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
14639 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
14640 XrmDestroyDatabase (dpyinfo->xrdb);
14641 #endif
14642 #endif
14643 #ifdef MULTI_KBOARD
14644 if (--dpyinfo->kboard->reference_count == 0)
14645 delete_kboard (dpyinfo->kboard);
14646 #endif
14647 #ifdef HAVE_X_I18N
14648 if (dpyinfo->xim)
14649 xim_close_dpy (dpyinfo);
14650 #endif
14651
14652 xfree (dpyinfo->font_table);
14653 xfree (dpyinfo->x_id_name);
14654 xfree (dpyinfo->color_cells);
14655 xfree (dpyinfo);
14656 }
14657
14658 \f
14659 /* Set up use of X before we make the first connection. */
14660
14661 static struct redisplay_interface x_redisplay_interface =
14662 {
14663 x_produce_glyphs,
14664 x_write_glyphs,
14665 x_insert_glyphs,
14666 x_clear_end_of_line,
14667 x_scroll_run,
14668 x_after_update_window_line,
14669 x_update_window_begin,
14670 x_update_window_end,
14671 XTcursor_to,
14672 x_flush,
14673 x_clear_mouse_face,
14674 x_get_glyph_overhangs,
14675 x_fix_overlapping_area
14676 };
14677
14678 void
14679 x_initialize ()
14680 {
14681 rif = &x_redisplay_interface;
14682
14683 clear_frame_hook = x_clear_frame;
14684 ins_del_lines_hook = x_ins_del_lines;
14685 change_line_highlight_hook = x_change_line_highlight;
14686 delete_glyphs_hook = x_delete_glyphs;
14687 ring_bell_hook = XTring_bell;
14688 reset_terminal_modes_hook = XTreset_terminal_modes;
14689 set_terminal_modes_hook = XTset_terminal_modes;
14690 update_begin_hook = x_update_begin;
14691 update_end_hook = x_update_end;
14692 set_terminal_window_hook = XTset_terminal_window;
14693 read_socket_hook = XTread_socket;
14694 frame_up_to_date_hook = XTframe_up_to_date;
14695 reassert_line_highlight_hook = XTreassert_line_highlight;
14696 mouse_position_hook = XTmouse_position;
14697 frame_rehighlight_hook = XTframe_rehighlight;
14698 frame_raise_lower_hook = XTframe_raise_lower;
14699 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
14700 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
14701 redeem_scroll_bar_hook = XTredeem_scroll_bar;
14702 judge_scroll_bars_hook = XTjudge_scroll_bars;
14703 estimate_mode_line_height_hook = x_estimate_mode_line_height;
14704
14705 scroll_region_ok = 1; /* we'll scroll partial frames */
14706 char_ins_del_ok = 1;
14707 line_ins_del_ok = 1; /* we'll just blt 'em */
14708 fast_clear_end_of_line = 1; /* X does this well */
14709 memory_below_frame = 0; /* we don't remember what scrolls
14710 off the bottom */
14711 baud_rate = 19200;
14712
14713 x_noop_count = 0;
14714 last_tool_bar_item = -1;
14715 any_help_event_p = 0;
14716
14717 /* Try to use interrupt input; if we can't, then start polling. */
14718 Fset_input_mode (Qt, Qnil, Qt, Qnil);
14719
14720 #ifdef USE_X_TOOLKIT
14721 XtToolkitInitialize ();
14722
14723 Xt_app_con = XtCreateApplicationContext ();
14724
14725 /* Register a converter from strings to pixels, which uses
14726 Emacs' color allocation infrastructure. */
14727 XtAppSetTypeConverter (Xt_app_con,
14728 XtRString, XtRPixel, cvt_string_to_pixel,
14729 cvt_string_to_pixel_args,
14730 XtNumber (cvt_string_to_pixel_args),
14731 XtCacheByDisplay, cvt_pixel_dtor);
14732
14733 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
14734
14735 /* Install an asynchronous timer that processes Xt timeout events
14736 every 0.1s. This is necessary because some widget sets use
14737 timeouts internally, for example the LessTif menu bar, or the
14738 Xaw3d scroll bar. When Xt timouts aren't processed, these
14739 widgets don't behave normally. */
14740 {
14741 EMACS_TIME interval;
14742 EMACS_SET_SECS_USECS (interval, 0, 100000);
14743 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
14744 }
14745 #endif
14746
14747 #ifdef USE_TOOLKIT_SCROLL_BARS
14748 xaw3d_arrow_scroll = False;
14749 xaw3d_pick_top = True;
14750 #endif
14751
14752 /* Note that there is no real way portable across R3/R4 to get the
14753 original error handler. */
14754 XSetErrorHandler (x_error_handler);
14755 XSetIOErrorHandler (x_io_error_quitter);
14756
14757 /* Disable Window Change signals; they are handled by X events. */
14758 #ifdef SIGWINCH
14759 signal (SIGWINCH, SIG_DFL);
14760 #endif /* ! defined (SIGWINCH) */
14761
14762 signal (SIGPIPE, x_connection_signal);
14763 }
14764
14765
14766 void
14767 syms_of_xterm ()
14768 {
14769 staticpro (&x_error_message_string);
14770 x_error_message_string = Qnil;
14771
14772 staticpro (&x_display_name_list);
14773 x_display_name_list = Qnil;
14774
14775 staticpro (&last_mouse_scroll_bar);
14776 last_mouse_scroll_bar = Qnil;
14777
14778 staticpro (&Qvendor_specific_keysyms);
14779 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
14780
14781 staticpro (&last_mouse_press_frame);
14782 last_mouse_press_frame = Qnil;
14783
14784 help_echo = Qnil;
14785 staticpro (&help_echo);
14786 help_echo_object = Qnil;
14787 staticpro (&help_echo_object);
14788 help_echo_window = Qnil;
14789 staticpro (&help_echo_window);
14790 previous_help_echo = Qnil;
14791 staticpro (&previous_help_echo);
14792 help_echo_pos = -1;
14793
14794 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p
14795 /* *Non-nil means draw block cursor as wide as the glyph under it.
14796 For example, if a block cursor is over a tab, it will be drawn as
14797 wide as that tab on the display. */);
14798 x_stretch_cursor_p = 0;
14799
14800 DEFVAR_BOOL ("x-use-underline-position-properties",
14801 &x_use_underline_position_properties
14802 /* *Non-nil means make use of UNDERLINE_POSITION font properties.
14803 Nil means ignore them. If you encounter fonts with bogus
14804 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
14805 to 4.1, set this to nil. */);
14806 x_use_underline_position_properties = 1;
14807
14808 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars
14809 /* What X toolkit scroll bars Emacs uses.
14810 A value of nil means Emacs doesn't use X toolkit scroll bars.
14811 Otherwise, value is a symbol describing the X toolkit. */);
14812 #ifdef USE_TOOLKIT_SCROLL_BARS
14813 #ifdef USE_MOTIF
14814 Vx_toolkit_scroll_bars = intern ("motif");
14815 #elif defined HAVE_XAW3D
14816 Vx_toolkit_scroll_bars = intern ("xaw3d");
14817 #else
14818 Vx_toolkit_scroll_bars = intern ("xaw");
14819 #endif
14820 #else
14821 Vx_toolkit_scroll_bars = Qnil;
14822 #endif
14823
14824 staticpro (&last_mouse_motion_frame);
14825 last_mouse_motion_frame = Qnil;
14826 }
14827
14828 #endif /* HAVE_X_WINDOWS */