]> code.delx.au - gnu-emacs/blob - src/xterm.c
4e82a8092d432a41d175c76de0c342d5b12b7730
[gnu-emacs] / src / xterm.c
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
23 /* Xt features made by Fred Pierresteguy. */
24
25 #include <config.h>
26
27 /* On 4.3 these lose if they come after xterm.h. */
28 /* Putting these at the beginning seems to be standard for other .c files. */
29 #include <signal.h>
30
31 #include <stdio.h>
32
33 #ifdef HAVE_X_WINDOWS
34
35 #include "lisp.h"
36 #include "blockinput.h"
37
38 /* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40 #include "syssignal.h"
41
42 /* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44 #include "xterm.h"
45 #include <X11/cursorfont.h>
46
47 /* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49 #ifndef makedev
50 #include <sys/types.h>
51 #endif /* makedev */
52
53 #ifdef BSD_SYSTEM
54 #include <sys/ioctl.h>
55 #endif /* ! defined (BSD_SYSTEM) */
56
57 #include "systty.h"
58 #include "systime.h"
59
60 #ifndef INCLUDED_FCNTL
61 #include <fcntl.h>
62 #endif
63 #include <ctype.h>
64 #include <errno.h>
65 #include <setjmp.h>
66 #include <sys/stat.h>
67 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68 /* #include <sys/param.h> */
69
70 #include "charset.h"
71 #include "coding.h"
72 #include "ccl.h"
73 #include "frame.h"
74 #include "dispextern.h"
75 #include "fontset.h"
76 #include "termhooks.h"
77 #include "termopts.h"
78 #include "termchar.h"
79 #include "gnu.h"
80 #include "disptab.h"
81 #include "buffer.h"
82 #include "window.h"
83 #include "keyboard.h"
84 #include "intervals.h"
85 #include "process.h"
86 #include "atimer.h"
87 #include "keymap.h"
88
89 #ifdef USE_X_TOOLKIT
90 #include <X11/Shell.h>
91 #endif
92
93 #ifdef HAVE_SYS_TIME_H
94 #include <sys/time.h>
95 #endif
96 #ifdef HAVE_UNISTD_H
97 #include <unistd.h>
98 #endif
99
100 #ifdef USE_LUCID
101 extern int xlwmenu_window_p (Widget w, Window window);
102 extern void xlwmenu_redisplay P_ ((Widget));
103 #endif
104
105 #ifdef USE_X_TOOLKIT
106
107 extern void free_frame_menubar P_ ((struct frame *));
108 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
109 int));
110
111 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
112 #define HACK_EDITRES
113 extern void _XEditResCheckMessages ();
114 #endif /* not NO_EDITRES */
115
116 /* Include toolkit specific headers for the scroll bar widget. */
117
118 #ifdef USE_TOOLKIT_SCROLL_BARS
119 #if defined USE_MOTIF
120 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
121 #include <Xm/ScrollBar.h>
122 #else /* !USE_MOTIF i.e. use Xaw */
123
124 #ifdef HAVE_XAW3D
125 #include <X11/Xaw3d/Simple.h>
126 #include <X11/Xaw3d/Scrollbar.h>
127 #define ARROW_SCROLLBAR
128 #include <X11/Xaw3d/ScrollbarP.h>
129 #else /* !HAVE_XAW3D */
130 #include <X11/Xaw/Simple.h>
131 #include <X11/Xaw/Scrollbar.h>
132 #endif /* !HAVE_XAW3D */
133 #ifndef XtNpickTop
134 #define XtNpickTop "pickTop"
135 #endif /* !XtNpickTop */
136 #endif /* !USE_MOTIF */
137 #endif /* USE_TOOLKIT_SCROLL_BARS */
138
139 #endif /* USE_X_TOOLKIT */
140
141 #ifndef USE_X_TOOLKIT
142 #define x_any_window_to_frame x_window_to_frame
143 #define x_top_window_to_frame x_window_to_frame
144 #endif
145
146 #ifdef USE_X_TOOLKIT
147 #include "widget.h"
148 #ifndef XtNinitialState
149 #define XtNinitialState "initialState"
150 #endif
151 #endif
152
153 #define abs(x) ((x) < 0 ? -(x) : (x))
154
155 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
156
157 \f
158 /* Fringe bitmaps. */
159
160 enum fringe_bitmap_type
161 {
162 NO_FRINGE_BITMAP,
163 LEFT_TRUNCATION_BITMAP,
164 RIGHT_TRUNCATION_BITMAP,
165 OVERLAY_ARROW_BITMAP,
166 CONTINUED_LINE_BITMAP,
167 CONTINUATION_LINE_BITMAP,
168 ZV_LINE_BITMAP
169 };
170
171 /* Bitmap drawn to indicate lines not displaying text if
172 `indicate-empty-lines' is non-nil. */
173
174 #define zv_width 8
175 #define zv_height 8
176 static unsigned char zv_bits[] = {
177 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
178
179 /* An arrow like this: `<-'. */
180
181 #define left_width 8
182 #define left_height 8
183 static unsigned char left_bits[] = {
184 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
185
186 /* Right truncation arrow bitmap `->'. */
187
188 #define right_width 8
189 #define right_height 8
190 static unsigned char right_bits[] = {
191 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
192
193 /* Marker for continued lines. */
194
195 #define continued_width 8
196 #define continued_height 8
197 static unsigned char continued_bits[] = {
198 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
199
200 /* Marker for continuation lines. */
201
202 #define continuation_width 8
203 #define continuation_height 8
204 static unsigned char continuation_bits[] = {
205 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
206
207 /* Overlay arrow bitmap. */
208
209 #if 0
210 /* A bomb. */
211 #define ov_width 8
212 #define ov_height 8
213 static unsigned char ov_bits[] = {
214 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
215 #else
216 /* A triangular arrow. */
217 #define ov_width 8
218 #define ov_height 8
219 static unsigned char ov_bits[] = {
220 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
221
222 #endif
223
224 extern Lisp_Object Qhelp_echo;
225
226 \f
227 /* Non-nil means Emacs uses toolkit scroll bars. */
228
229 Lisp_Object Vx_toolkit_scroll_bars;
230
231 /* If a string, XTread_socket generates an event to display that string.
232 (The display is done in read_char.) */
233
234 static Lisp_Object help_echo;
235 static Lisp_Object help_echo_window;
236 static Lisp_Object help_echo_object;
237 static int help_echo_pos;
238
239 /* Temporary variable for XTread_socket. */
240
241 static Lisp_Object previous_help_echo;
242
243 /* Non-zero means that a HELP_EVENT has been generated since Emacs
244 start. */
245
246 static int any_help_event_p;
247
248 /* Non-zero means draw block and hollow cursor as wide as the glyph
249 under it. For example, if a block cursor is over a tab, it will be
250 drawn as wide as that tab on the display. */
251
252 int x_stretch_cursor_p;
253
254 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
255
256 int x_use_underline_position_properties;
257
258 /* This is a chain of structures for all the X displays currently in
259 use. */
260
261 struct x_display_info *x_display_list;
262
263 /* This is a list of cons cells, each of the form (NAME
264 . FONT-LIST-CACHE), one for each element of x_display_list and in
265 the same order. NAME is the name of the frame. FONT-LIST-CACHE
266 records previous values returned by x-list-fonts. */
267
268 Lisp_Object x_display_name_list;
269
270 /* Frame being updated by update_frame. This is declared in term.c.
271 This is set by update_begin and looked at by all the XT functions.
272 It is zero while not inside an update. In that case, the XT
273 functions assume that `selected_frame' is the frame to apply to. */
274
275 extern struct frame *updating_frame;
276
277 /* This is a frame waiting to be auto-raised, within XTread_socket. */
278
279 struct frame *pending_autoraise_frame;
280
281 #ifdef USE_X_TOOLKIT
282 /* The application context for Xt use. */
283 XtAppContext Xt_app_con;
284 static String Xt_default_resources[] = {0};
285 #endif /* USE_X_TOOLKIT */
286
287 /* Nominal cursor position -- where to draw output.
288 HPOS and VPOS are window relative glyph matrix coordinates.
289 X and Y are window relative pixel coordinates. */
290
291 struct cursor_pos output_cursor;
292
293 /* Non-zero means user is interacting with a toolkit scroll bar. */
294
295 static int toolkit_scroll_bar_interaction;
296
297 /* Mouse movement.
298
299 Formerly, we used PointerMotionHintMask (in standard_event_mask)
300 so that we would have to call XQueryPointer after each MotionNotify
301 event to ask for another such event. However, this made mouse tracking
302 slow, and there was a bug that made it eventually stop.
303
304 Simply asking for MotionNotify all the time seems to work better.
305
306 In order to avoid asking for motion events and then throwing most
307 of them away or busy-polling the server for mouse positions, we ask
308 the server for pointer motion hints. This means that we get only
309 one event per group of mouse movements. "Groups" are delimited by
310 other kinds of events (focus changes and button clicks, for
311 example), or by XQueryPointer calls; when one of these happens, we
312 get another MotionNotify event the next time the mouse moves. This
313 is at least as efficient as getting motion events when mouse
314 tracking is on, and I suspect only negligibly worse when tracking
315 is off. */
316
317 /* Where the mouse was last time we reported a mouse event. */
318
319 FRAME_PTR last_mouse_frame;
320 static XRectangle last_mouse_glyph;
321 static Lisp_Object last_mouse_press_frame;
322
323 /* The scroll bar in which the last X motion event occurred.
324
325 If the last X motion event occurred in a scroll bar, we set this so
326 XTmouse_position can know whether to report a scroll bar motion or
327 an ordinary motion.
328
329 If the last X motion event didn't occur in a scroll bar, we set
330 this to Qnil, to tell XTmouse_position to return an ordinary motion
331 event. */
332
333 static Lisp_Object last_mouse_scroll_bar;
334
335 /* This is a hack. We would really prefer that XTmouse_position would
336 return the time associated with the position it returns, but there
337 doesn't seem to be any way to wrest the time-stamp from the server
338 along with the position query. So, we just keep track of the time
339 of the last movement we received, and return that in hopes that
340 it's somewhat accurate. */
341
342 static Time last_mouse_movement_time;
343
344 /* Incremented by XTread_socket whenever it really tries to read
345 events. */
346
347 #ifdef __STDC__
348 static int volatile input_signal_count;
349 #else
350 static int input_signal_count;
351 #endif
352
353 /* Used locally within XTread_socket. */
354
355 static int x_noop_count;
356
357 /* Initial values of argv and argc. */
358
359 extern char **initial_argv;
360 extern int initial_argc;
361
362 extern Lisp_Object Vcommand_line_args, Vsystem_name;
363
364 /* Tells if a window manager is present or not. */
365
366 extern Lisp_Object Vx_no_window_manager;
367
368 extern Lisp_Object Qface, Qmouse_face;
369
370 extern int errno;
371
372 /* A mask of extra modifier bits to put into every keyboard char. */
373
374 extern int extra_keyboard_modifiers;
375
376 static Lisp_Object Qvendor_specific_keysyms;
377
378 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
379 extern Lisp_Object x_icon_type P_ ((struct frame *));
380
381
382 /* Enumeration for overriding/changing the face to use for drawing
383 glyphs in x_draw_glyphs. */
384
385 enum draw_glyphs_face
386 {
387 DRAW_NORMAL_TEXT,
388 DRAW_INVERSE_VIDEO,
389 DRAW_CURSOR,
390 DRAW_MOUSE_FACE,
391 DRAW_IMAGE_RAISED,
392 DRAW_IMAGE_SUNKEN
393 };
394
395 static int cursor_in_mouse_face_p P_ ((struct window *));
396 static int clear_mouse_face P_ ((struct x_display_info *));
397 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
398 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
399 static const XColor *x_color_cells P_ ((Display *, int *));
400 static void x_update_window_end P_ ((struct window *, int, int));
401 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
402 void x_delete_display P_ ((struct x_display_info *));
403 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
404 unsigned));
405 static int fast_find_position P_ ((struct window *, int, int *, int *,
406 int *, int *, Lisp_Object));
407 static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
408 int *, int *, int *, int *, int));
409 static void set_output_cursor P_ ((struct cursor_pos *));
410 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
411 int *, int *, int *, int));
412 static void note_mode_line_highlight P_ ((struct window *, int, int));
413 static void note_mouse_highlight P_ ((struct frame *, int, int));
414 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
415 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
416 static void show_mouse_face P_ ((struct x_display_info *,
417 enum draw_glyphs_face));
418 static int x_io_error_quitter P_ ((Display *));
419 int x_catch_errors P_ ((Display *));
420 void x_uncatch_errors P_ ((Display *, int));
421 void x_lower_frame P_ ((struct frame *));
422 void x_scroll_bar_clear P_ ((struct frame *));
423 int x_had_errors_p P_ ((Display *));
424 void x_wm_set_size_hint P_ ((struct frame *, long, int));
425 void x_raise_frame P_ ((struct frame *));
426 void x_set_window_size P_ ((struct frame *, int, int, int));
427 void x_wm_set_window_state P_ ((struct frame *, int));
428 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
429 void x_initialize P_ ((void));
430 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
431 static int x_compute_min_glyph_bounds P_ ((struct frame *));
432 static void x_draw_phys_cursor_glyph P_ ((struct window *,
433 struct glyph_row *,
434 enum draw_glyphs_face));
435 static void x_update_end P_ ((struct frame *));
436 static void XTframe_up_to_date P_ ((struct frame *));
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_fringe_bitmap P_ ((struct window *, struct glyph_row *,
465 enum fringe_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_fringe_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_FRINGE_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 fringes if no bitmaps are required
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 struct frame *f;
771 int width, height;
772
773 xassert (w);
774
775 if (!desired_row->mode_line_p && !w->pseudo_window_p)
776 {
777 BLOCK_INPUT;
778 x_draw_row_fringe_bitmaps (w, desired_row);
779 UNBLOCK_INPUT;
780 }
781
782 /* When a window has disappeared, make sure that no rest of
783 full-width rows stays visible in the internal border. Could
784 check here if updated_window is the leftmost/rightmost window,
785 but I guess it's not worth doing since vertically split windows
786 are almost never used, internal border is rarely set, and the
787 overhead is very small. */
788 if (windows_or_buffers_changed
789 && desired_row->full_width_p
790 && (f = XFRAME (w->frame),
791 width = FRAME_INTERNAL_BORDER_WIDTH (f),
792 width != 0)
793 && (height = desired_row->visible_height,
794 height > 0))
795 {
796 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
797
798 /* Internal border is drawn below the tool bar. */
799 if (WINDOWP (f->tool_bar_window)
800 && w == XWINDOW (f->tool_bar_window))
801 y -= width;
802
803 BLOCK_INPUT;
804 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
805 0, y, width, height, False);
806 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
807 f->output_data.x->pixel_width - width,
808 y, width, height, False);
809 UNBLOCK_INPUT;
810 }
811 }
812
813
814 /* Draw the bitmap WHICH in one of the left or right fringes of
815 window W. ROW is the glyph row for which to display the bitmap; it
816 determines the vertical position at which the bitmap has to be
817 drawn. */
818
819 static void
820 x_draw_fringe_bitmap (w, row, which)
821 struct window *w;
822 struct glyph_row *row;
823 enum fringe_bitmap_type which;
824 {
825 struct frame *f = XFRAME (WINDOW_FRAME (w));
826 Display *display = FRAME_X_DISPLAY (f);
827 Window window = FRAME_X_WINDOW (f);
828 int x, y, wd, h, dy;
829 unsigned char *bits;
830 Pixmap pixmap;
831 GC gc = f->output_data.x->normal_gc;
832 struct face *face;
833 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
834
835 /* Must clip because of partially visible lines. */
836 x_clip_to_row (w, row, gc, 1);
837
838 switch (which)
839 {
840 case LEFT_TRUNCATION_BITMAP:
841 wd = left_width;
842 h = left_height;
843 bits = left_bits;
844 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
845 - wd
846 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - wd) / 2);
847 break;
848
849 case OVERLAY_ARROW_BITMAP:
850 wd = left_width;
851 h = left_height;
852 bits = ov_bits;
853 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
854 - wd
855 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - wd) / 2);
856 break;
857
858 case RIGHT_TRUNCATION_BITMAP:
859 wd = right_width;
860 h = right_height;
861 bits = right_bits;
862 x = window_box_right (w, -1);
863 x += (FRAME_X_RIGHT_FRINGE_WIDTH (f) - wd) / 2;
864 break;
865
866 case CONTINUED_LINE_BITMAP:
867 wd = right_width;
868 h = right_height;
869 bits = continued_bits;
870 x = window_box_right (w, -1);
871 x += (FRAME_X_RIGHT_FRINGE_WIDTH (f) - wd) / 2;
872 break;
873
874 case CONTINUATION_LINE_BITMAP:
875 wd = continuation_width;
876 h = continuation_height;
877 bits = continuation_bits;
878 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
879 - wd
880 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - wd) / 2);
881 break;
882
883 case ZV_LINE_BITMAP:
884 wd = zv_width;
885 h = zv_height;
886 bits = zv_bits;
887 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
888 - wd
889 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - wd) / 2);
890 break;
891
892 default:
893 abort ();
894 }
895
896 /* Convert to frame coordinates. Set dy to the offset in the row to
897 start drawing the bitmap. */
898 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
899 dy = (row->height - h) / 2;
900
901 /* Draw the bitmap. I believe these small pixmaps can be cached
902 by the server. */
903 face = FACE_FROM_ID (f, FRINGE_FACE_ID);
904 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
905 face->foreground,
906 face->background, depth);
907 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
908 XFreePixmap (display, pixmap);
909 XSetClipMask (display, gc, None);
910 }
911
912
913 /* Draw fringe bitmaps for glyph row ROW on window W. Call this
914 function with input blocked. */
915
916 static void
917 x_draw_row_fringe_bitmaps (w, row)
918 struct window *w;
919 struct glyph_row *row;
920 {
921 struct frame *f = XFRAME (w->frame);
922 enum fringe_bitmap_type bitmap;
923 struct face *face;
924 int header_line_height = -1;
925
926 xassert (interrupt_input_blocked);
927
928 /* If row is completely invisible, because of vscrolling, we
929 don't have to draw anything. */
930 if (row->visible_height <= 0)
931 return;
932
933 face = FACE_FROM_ID (f, FRINGE_FACE_ID);
934 PREPARE_FACE_FOR_DISPLAY (f, face);
935
936 /* Decide which bitmap to draw in the left fringe. */
937 if (row->overlay_arrow_p)
938 bitmap = OVERLAY_ARROW_BITMAP;
939 else if (row->truncated_on_left_p)
940 bitmap = LEFT_TRUNCATION_BITMAP;
941 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
942 bitmap = CONTINUATION_LINE_BITMAP;
943 else if (row->indicate_empty_line_p)
944 bitmap = ZV_LINE_BITMAP;
945 else
946 bitmap = NO_FRINGE_BITMAP;
947
948 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
949 the fringe. */
950 if (bitmap == NO_FRINGE_BITMAP
951 || FRAME_FRINGE_BITMAP_WIDTH (f) < FRAME_X_LEFT_FRINGE_WIDTH (f)
952 || row->height > FRAME_FRINGE_BITMAP_HEIGHT (f))
953 {
954 /* If W has a vertical border to its left, don't draw over it. */
955 int border = ((XFASTINT (w->left) > 0
956 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
957 ? 1 : 0);
958 int left = window_box_left (w, -1);
959
960 if (header_line_height < 0)
961 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
962
963 /* In case the same realized face is used for fringes and
964 for something displayed in the text (e.g. face `region' on
965 mono-displays, the fill style may have been changed to
966 FillSolid in x_draw_glyph_string_background. */
967 if (face->stipple)
968 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
969 else
970 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
971
972 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
973 face->gc,
974 (left
975 - FRAME_X_LEFT_FRINGE_WIDTH (f)
976 + border),
977 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
978 row->y)),
979 FRAME_X_LEFT_FRINGE_WIDTH (f) - border,
980 row->visible_height);
981 if (!face->stipple)
982 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
983 }
984
985 /* Draw the left bitmap. */
986 if (bitmap != NO_FRINGE_BITMAP)
987 x_draw_fringe_bitmap (w, row, bitmap);
988
989 /* Decide which bitmap to draw in the right fringe. */
990 if (row->truncated_on_right_p)
991 bitmap = RIGHT_TRUNCATION_BITMAP;
992 else if (row->continued_p)
993 bitmap = CONTINUED_LINE_BITMAP;
994 else
995 bitmap = NO_FRINGE_BITMAP;
996
997 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
998 the fringe. */
999 if (bitmap == NO_FRINGE_BITMAP
1000 || FRAME_FRINGE_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FRINGE_WIDTH (f)
1001 || row->height > FRAME_FRINGE_BITMAP_HEIGHT (f))
1002 {
1003 int right = window_box_right (w, -1);
1004
1005 if (header_line_height < 0)
1006 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1007
1008 /* In case the same realized face is used for fringes and
1009 for something displayed in the text (e.g. face `region' on
1010 mono-displays, the fill style may have been changed to
1011 FillSolid in x_draw_glyph_string_background. */
1012 if (face->stipple)
1013 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
1014 else
1015 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
1016 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1017 face->gc,
1018 right,
1019 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
1020 row->y)),
1021 FRAME_X_RIGHT_FRINGE_WIDTH (f),
1022 row->visible_height);
1023 if (!face->stipple)
1024 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
1025 }
1026
1027 /* Draw the right bitmap. */
1028 if (bitmap != NO_FRINGE_BITMAP)
1029 x_draw_fringe_bitmap (w, row, bitmap);
1030 }
1031
1032 \f
1033
1034 /* This is called when starting Emacs and when restarting after
1035 suspend. When starting Emacs, no X window is mapped. And nothing
1036 must be done to Emacs's own window if it is suspended (though that
1037 rarely happens). */
1038
1039 static void
1040 XTset_terminal_modes ()
1041 {
1042 }
1043
1044 /* This is called when exiting or suspending Emacs. Exiting will make
1045 the X-windows go away, and suspending requires no action. */
1046
1047 static void
1048 XTreset_terminal_modes ()
1049 {
1050 }
1051
1052
1053 \f
1054 /***********************************************************************
1055 Output Cursor
1056 ***********************************************************************/
1057
1058 /* Set the global variable output_cursor to CURSOR. All cursor
1059 positions are relative to updated_window. */
1060
1061 static void
1062 set_output_cursor (cursor)
1063 struct cursor_pos *cursor;
1064 {
1065 output_cursor.hpos = cursor->hpos;
1066 output_cursor.vpos = cursor->vpos;
1067 output_cursor.x = cursor->x;
1068 output_cursor.y = cursor->y;
1069 }
1070
1071
1072 /* Set a nominal cursor position.
1073
1074 HPOS and VPOS are column/row positions in a window glyph matrix. X
1075 and Y are window text area relative pixel positions.
1076
1077 If this is done during an update, updated_window will contain the
1078 window that is being updated and the position is the future output
1079 cursor position for that window. If updated_window is null, use
1080 selected_window and display the cursor at the given position. */
1081
1082 static void
1083 XTcursor_to (vpos, hpos, y, x)
1084 int vpos, hpos, y, x;
1085 {
1086 struct window *w;
1087
1088 /* If updated_window is not set, work on selected_window. */
1089 if (updated_window)
1090 w = updated_window;
1091 else
1092 w = XWINDOW (selected_window);
1093
1094 /* Set the output cursor. */
1095 output_cursor.hpos = hpos;
1096 output_cursor.vpos = vpos;
1097 output_cursor.x = x;
1098 output_cursor.y = y;
1099
1100 /* If not called as part of an update, really display the cursor.
1101 This will also set the cursor position of W. */
1102 if (updated_window == NULL)
1103 {
1104 BLOCK_INPUT;
1105 x_display_cursor (w, 1, hpos, vpos, x, y);
1106 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1107 UNBLOCK_INPUT;
1108 }
1109 }
1110
1111
1112 \f
1113 /***********************************************************************
1114 Display Iterator
1115 ***********************************************************************/
1116
1117 /* Function prototypes of this page. */
1118
1119 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1120 struct glyph *,
1121 XChar2b *,
1122 int *));
1123 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1124 int, XChar2b *, int));
1125 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1126 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1127 static void x_append_glyph P_ ((struct it *));
1128 static void x_append_composite_glyph P_ ((struct it *));
1129 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1130 int, int, double));
1131 static void x_produce_glyphs P_ ((struct it *));
1132 static void x_produce_image_glyph P_ ((struct it *it));
1133
1134
1135 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1136 is not contained in the font. */
1137
1138 static INLINE XCharStruct *
1139 x_per_char_metric (font, char2b)
1140 XFontStruct *font;
1141 XChar2b *char2b;
1142 {
1143 /* The result metric information. */
1144 XCharStruct *pcm = NULL;
1145
1146 xassert (font && char2b);
1147
1148 if (font->per_char != NULL)
1149 {
1150 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1151 {
1152 /* min_char_or_byte2 specifies the linear character index
1153 corresponding to the first element of the per_char array,
1154 max_char_or_byte2 is the index of the last character. A
1155 character with non-zero CHAR2B->byte1 is not in the font.
1156 A character with byte2 less than min_char_or_byte2 or
1157 greater max_char_or_byte2 is not in the font. */
1158 if (char2b->byte1 == 0
1159 && char2b->byte2 >= font->min_char_or_byte2
1160 && char2b->byte2 <= font->max_char_or_byte2)
1161 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1162 }
1163 else
1164 {
1165 /* If either min_byte1 or max_byte1 are nonzero, both
1166 min_char_or_byte2 and max_char_or_byte2 are less than
1167 256, and the 2-byte character index values corresponding
1168 to the per_char array element N (counting from 0) are:
1169
1170 byte1 = N/D + min_byte1
1171 byte2 = N\D + min_char_or_byte2
1172
1173 where:
1174
1175 D = max_char_or_byte2 - min_char_or_byte2 + 1
1176 / = integer division
1177 \ = integer modulus */
1178 if (char2b->byte1 >= font->min_byte1
1179 && char2b->byte1 <= font->max_byte1
1180 && char2b->byte2 >= font->min_char_or_byte2
1181 && char2b->byte2 <= font->max_char_or_byte2)
1182 {
1183 pcm = (font->per_char
1184 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1185 * (char2b->byte1 - font->min_byte1))
1186 + (char2b->byte2 - font->min_char_or_byte2));
1187 }
1188 }
1189 }
1190 else
1191 {
1192 /* If the per_char pointer is null, all glyphs between the first
1193 and last character indexes inclusive have the same
1194 information, as given by both min_bounds and max_bounds. */
1195 if (char2b->byte2 >= font->min_char_or_byte2
1196 && char2b->byte2 <= font->max_char_or_byte2)
1197 pcm = &font->max_bounds;
1198 }
1199
1200 return ((pcm == NULL
1201 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1202 ? NULL : pcm);
1203 }
1204
1205
1206 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1207 the two-byte form of C. Encoding is returned in *CHAR2B. */
1208
1209 static INLINE void
1210 x_encode_char (c, char2b, font_info)
1211 int c;
1212 XChar2b *char2b;
1213 struct font_info *font_info;
1214 {
1215 int charset = CHAR_CHARSET (c);
1216 XFontStruct *font = font_info->font;
1217
1218 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1219 This may be either a program in a special encoder language or a
1220 fixed encoding. */
1221 if (font_info->font_encoder)
1222 {
1223 /* It's a program. */
1224 struct ccl_program *ccl = font_info->font_encoder;
1225
1226 if (CHARSET_DIMENSION (charset) == 1)
1227 {
1228 ccl->reg[0] = charset;
1229 ccl->reg[1] = char2b->byte2;
1230 }
1231 else
1232 {
1233 ccl->reg[0] = charset;
1234 ccl->reg[1] = char2b->byte1;
1235 ccl->reg[2] = char2b->byte2;
1236 }
1237
1238 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1239
1240 /* We assume that MSBs are appropriately set/reset by CCL
1241 program. */
1242 if (font->max_byte1 == 0) /* 1-byte font */
1243 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1244 else
1245 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1246 }
1247 else if (font_info->encoding[charset])
1248 {
1249 /* Fixed encoding scheme. See fontset.h for the meaning of the
1250 encoding numbers. */
1251 int enc = font_info->encoding[charset];
1252
1253 if ((enc == 1 || enc == 2)
1254 && CHARSET_DIMENSION (charset) == 2)
1255 char2b->byte1 |= 0x80;
1256
1257 if (enc == 1 || enc == 3)
1258 char2b->byte2 |= 0x80;
1259 }
1260 }
1261
1262
1263 /* Get face and two-byte form of character C in face FACE_ID on frame
1264 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1265 means we want to display multibyte text. Value is a pointer to a
1266 realized face that is ready for display. */
1267
1268 static INLINE struct face *
1269 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1270 struct frame *f;
1271 int c, face_id;
1272 XChar2b *char2b;
1273 int multibyte_p;
1274 {
1275 struct face *face = FACE_FROM_ID (f, face_id);
1276
1277 if (!multibyte_p)
1278 {
1279 /* Unibyte case. We don't have to encode, but we have to make
1280 sure to use a face suitable for unibyte. */
1281 char2b->byte1 = 0;
1282 char2b->byte2 = c;
1283 face_id = FACE_FOR_CHAR (f, face, c);
1284 face = FACE_FROM_ID (f, face_id);
1285 }
1286 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1287 {
1288 /* Case of ASCII in a face known to fit ASCII. */
1289 char2b->byte1 = 0;
1290 char2b->byte2 = c;
1291 }
1292 else
1293 {
1294 int c1, c2, charset;
1295
1296 /* Split characters into bytes. If c2 is -1 afterwards, C is
1297 really a one-byte character so that byte1 is zero. */
1298 SPLIT_CHAR (c, charset, c1, c2);
1299 if (c2 > 0)
1300 char2b->byte1 = c1, char2b->byte2 = c2;
1301 else
1302 char2b->byte1 = 0, char2b->byte2 = c1;
1303
1304 /* Maybe encode the character in *CHAR2B. */
1305 if (face->font != NULL)
1306 {
1307 struct font_info *font_info
1308 = FONT_INFO_FROM_ID (f, face->font_info_id);
1309 if (font_info)
1310 x_encode_char (c, char2b, font_info);
1311 }
1312 }
1313
1314 /* Make sure X resources of the face are allocated. */
1315 xassert (face != NULL);
1316 PREPARE_FACE_FOR_DISPLAY (f, face);
1317
1318 return face;
1319 }
1320
1321
1322 /* Get face and two-byte form of character glyph GLYPH on frame F.
1323 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1324 a pointer to a realized face that is ready for display. */
1325
1326 static INLINE struct face *
1327 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1328 struct frame *f;
1329 struct glyph *glyph;
1330 XChar2b *char2b;
1331 int *two_byte_p;
1332 {
1333 struct face *face;
1334
1335 xassert (glyph->type == CHAR_GLYPH);
1336 face = FACE_FROM_ID (f, glyph->face_id);
1337
1338 if (two_byte_p)
1339 *two_byte_p = 0;
1340
1341 if (!glyph->multibyte_p)
1342 {
1343 /* Unibyte case. We don't have to encode, but we have to make
1344 sure to use a face suitable for unibyte. */
1345 char2b->byte1 = 0;
1346 char2b->byte2 = glyph->u.ch;
1347 }
1348 else if (glyph->u.ch < 128
1349 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1350 {
1351 /* Case of ASCII in a face known to fit ASCII. */
1352 char2b->byte1 = 0;
1353 char2b->byte2 = glyph->u.ch;
1354 }
1355 else
1356 {
1357 int c1, c2, charset;
1358
1359 /* Split characters into bytes. If c2 is -1 afterwards, C is
1360 really a one-byte character so that byte1 is zero. */
1361 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1362 if (c2 > 0)
1363 char2b->byte1 = c1, char2b->byte2 = c2;
1364 else
1365 char2b->byte1 = 0, char2b->byte2 = c1;
1366
1367 /* Maybe encode the character in *CHAR2B. */
1368 if (charset != CHARSET_ASCII)
1369 {
1370 struct font_info *font_info
1371 = FONT_INFO_FROM_ID (f, face->font_info_id);
1372 if (font_info)
1373 {
1374 x_encode_char (glyph->u.ch, char2b, font_info);
1375 if (two_byte_p)
1376 *two_byte_p
1377 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1378 }
1379 }
1380 }
1381
1382 /* Make sure X resources of the face are allocated. */
1383 xassert (face != NULL);
1384 PREPARE_FACE_FOR_DISPLAY (f, face);
1385 return face;
1386 }
1387
1388
1389 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1390 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1391
1392 static INLINE void
1393 x_append_glyph (it)
1394 struct it *it;
1395 {
1396 struct glyph *glyph;
1397 enum glyph_row_area area = it->area;
1398
1399 xassert (it->glyph_row);
1400 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1401
1402 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1403 if (glyph < it->glyph_row->glyphs[area + 1])
1404 {
1405 glyph->charpos = CHARPOS (it->position);
1406 glyph->object = it->object;
1407 glyph->pixel_width = it->pixel_width;
1408 glyph->voffset = it->voffset;
1409 glyph->type = CHAR_GLYPH;
1410 glyph->multibyte_p = it->multibyte_p;
1411 glyph->left_box_line_p = it->start_of_box_run_p;
1412 glyph->right_box_line_p = it->end_of_box_run_p;
1413 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1414 || it->phys_descent > it->descent);
1415 glyph->padding_p = 0;
1416 glyph->glyph_not_available_p = it->glyph_not_available_p;
1417 glyph->face_id = it->face_id;
1418 glyph->u.ch = it->char_to_display;
1419 ++it->glyph_row->used[area];
1420 }
1421 }
1422
1423 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1424 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1425
1426 static INLINE void
1427 x_append_composite_glyph (it)
1428 struct it *it;
1429 {
1430 struct glyph *glyph;
1431 enum glyph_row_area area = it->area;
1432
1433 xassert (it->glyph_row);
1434
1435 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1436 if (glyph < it->glyph_row->glyphs[area + 1])
1437 {
1438 glyph->charpos = CHARPOS (it->position);
1439 glyph->object = it->object;
1440 glyph->pixel_width = it->pixel_width;
1441 glyph->voffset = it->voffset;
1442 glyph->type = COMPOSITE_GLYPH;
1443 glyph->multibyte_p = it->multibyte_p;
1444 glyph->left_box_line_p = it->start_of_box_run_p;
1445 glyph->right_box_line_p = it->end_of_box_run_p;
1446 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1447 || it->phys_descent > it->descent);
1448 glyph->padding_p = 0;
1449 glyph->glyph_not_available_p = 0;
1450 glyph->face_id = it->face_id;
1451 glyph->u.cmp_id = it->cmp_id;
1452 ++it->glyph_row->used[area];
1453 }
1454 }
1455
1456
1457 /* Change IT->ascent and IT->height according to the setting of
1458 IT->voffset. */
1459
1460 static INLINE void
1461 take_vertical_position_into_account (it)
1462 struct it *it;
1463 {
1464 if (it->voffset)
1465 {
1466 if (it->voffset < 0)
1467 /* Increase the ascent so that we can display the text higher
1468 in the line. */
1469 it->ascent += abs (it->voffset);
1470 else
1471 /* Increase the descent so that we can display the text lower
1472 in the line. */
1473 it->descent += it->voffset;
1474 }
1475 }
1476
1477
1478 /* Produce glyphs/get display metrics for the image IT is loaded with.
1479 See the description of struct display_iterator in dispextern.h for
1480 an overview of struct display_iterator. */
1481
1482 static void
1483 x_produce_image_glyph (it)
1484 struct it *it;
1485 {
1486 struct image *img;
1487 struct face *face;
1488
1489 xassert (it->what == IT_IMAGE);
1490
1491 face = FACE_FROM_ID (it->f, it->face_id);
1492 img = IMAGE_FROM_ID (it->f, it->image_id);
1493 xassert (img);
1494
1495 /* Make sure X resources of the face and image are loaded. */
1496 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1497 prepare_image_for_display (it->f, img);
1498
1499 it->ascent = it->phys_ascent = image_ascent (img, face);
1500 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1501 it->pixel_width = img->width + 2 * img->hmargin;
1502
1503 it->nglyphs = 1;
1504
1505 if (face->box != FACE_NO_BOX)
1506 {
1507 if (face->box_line_width > 0)
1508 {
1509 it->ascent += face->box_line_width;
1510 it->descent += face->box_line_width;
1511 }
1512
1513 if (it->start_of_box_run_p)
1514 it->pixel_width += abs (face->box_line_width);
1515 if (it->end_of_box_run_p)
1516 it->pixel_width += abs (face->box_line_width);
1517 }
1518
1519 take_vertical_position_into_account (it);
1520
1521 if (it->glyph_row)
1522 {
1523 struct glyph *glyph;
1524 enum glyph_row_area area = it->area;
1525
1526 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1527 if (glyph < it->glyph_row->glyphs[area + 1])
1528 {
1529 glyph->charpos = CHARPOS (it->position);
1530 glyph->object = it->object;
1531 glyph->pixel_width = it->pixel_width;
1532 glyph->voffset = it->voffset;
1533 glyph->type = IMAGE_GLYPH;
1534 glyph->multibyte_p = it->multibyte_p;
1535 glyph->left_box_line_p = it->start_of_box_run_p;
1536 glyph->right_box_line_p = it->end_of_box_run_p;
1537 glyph->overlaps_vertically_p = 0;
1538 glyph->padding_p = 0;
1539 glyph->glyph_not_available_p = 0;
1540 glyph->face_id = it->face_id;
1541 glyph->u.img_id = img->id;
1542 ++it->glyph_row->used[area];
1543 }
1544 }
1545 }
1546
1547
1548 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1549 of the glyph, WIDTH and HEIGHT are the width and height of the
1550 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1551 ascent of the glyph (0 <= ASCENT <= 1). */
1552
1553 static void
1554 x_append_stretch_glyph (it, object, width, height, ascent)
1555 struct it *it;
1556 Lisp_Object object;
1557 int width, height;
1558 double ascent;
1559 {
1560 struct glyph *glyph;
1561 enum glyph_row_area area = it->area;
1562
1563 xassert (ascent >= 0 && ascent <= 1);
1564
1565 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1566 if (glyph < it->glyph_row->glyphs[area + 1])
1567 {
1568 glyph->charpos = CHARPOS (it->position);
1569 glyph->object = object;
1570 glyph->pixel_width = width;
1571 glyph->voffset = it->voffset;
1572 glyph->type = STRETCH_GLYPH;
1573 glyph->multibyte_p = it->multibyte_p;
1574 glyph->left_box_line_p = it->start_of_box_run_p;
1575 glyph->right_box_line_p = it->end_of_box_run_p;
1576 glyph->overlaps_vertically_p = 0;
1577 glyph->padding_p = 0;
1578 glyph->glyph_not_available_p = 0;
1579 glyph->face_id = it->face_id;
1580 glyph->u.stretch.ascent = height * ascent;
1581 glyph->u.stretch.height = height;
1582 ++it->glyph_row->used[area];
1583 }
1584 }
1585
1586
1587 /* Produce a stretch glyph for iterator IT. IT->object is the value
1588 of the glyph property displayed. The value must be a list
1589 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1590 being recognized:
1591
1592 1. `:width WIDTH' specifies that the space should be WIDTH *
1593 canonical char width wide. WIDTH may be an integer or floating
1594 point number.
1595
1596 2. `:relative-width FACTOR' specifies that the width of the stretch
1597 should be computed from the width of the first character having the
1598 `glyph' property, and should be FACTOR times that width.
1599
1600 3. `:align-to HPOS' specifies that the space should be wide enough
1601 to reach HPOS, a value in canonical character units.
1602
1603 Exactly one of the above pairs must be present.
1604
1605 4. `:height HEIGHT' specifies that the height of the stretch produced
1606 should be HEIGHT, measured in canonical character units.
1607
1608 5. `:relative-height FACTOR' specifies that the height of the the
1609 stretch should be FACTOR times the height of the characters having
1610 the glyph property.
1611
1612 Either none or exactly one of 4 or 5 must be present.
1613
1614 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1615 of the stretch should be used for the ascent of the stretch.
1616 ASCENT must be in the range 0 <= ASCENT <= 100. */
1617
1618 #define NUMVAL(X) \
1619 ((INTEGERP (X) || FLOATP (X)) \
1620 ? XFLOATINT (X) \
1621 : - 1)
1622
1623
1624 static void
1625 x_produce_stretch_glyph (it)
1626 struct it *it;
1627 {
1628 /* (space :width WIDTH :height HEIGHT. */
1629 #if GLYPH_DEBUG
1630 extern Lisp_Object Qspace;
1631 #endif
1632 extern Lisp_Object QCwidth, QCheight, QCascent;
1633 extern Lisp_Object QCrelative_width, QCrelative_height;
1634 extern Lisp_Object QCalign_to;
1635 Lisp_Object prop, plist;
1636 double width = 0, height = 0, ascent = 0;
1637 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1638 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1639
1640 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1641
1642 /* List should start with `space'. */
1643 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1644 plist = XCDR (it->object);
1645
1646 /* Compute the width of the stretch. */
1647 if (prop = Fplist_get (plist, QCwidth),
1648 NUMVAL (prop) > 0)
1649 /* Absolute width `:width WIDTH' specified and valid. */
1650 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1651 else if (prop = Fplist_get (plist, QCrelative_width),
1652 NUMVAL (prop) > 0)
1653 {
1654 /* Relative width `:relative-width FACTOR' specified and valid.
1655 Compute the width of the characters having the `glyph'
1656 property. */
1657 struct it it2;
1658 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1659
1660 it2 = *it;
1661 if (it->multibyte_p)
1662 {
1663 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1664 - IT_BYTEPOS (*it));
1665 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1666 }
1667 else
1668 it2.c = *p, it2.len = 1;
1669
1670 it2.glyph_row = NULL;
1671 it2.what = IT_CHARACTER;
1672 x_produce_glyphs (&it2);
1673 width = NUMVAL (prop) * it2.pixel_width;
1674 }
1675 else if (prop = Fplist_get (plist, QCalign_to),
1676 NUMVAL (prop) > 0)
1677 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1678 else
1679 /* Nothing specified -> width defaults to canonical char width. */
1680 width = CANON_X_UNIT (it->f);
1681
1682 /* Compute height. */
1683 if (prop = Fplist_get (plist, QCheight),
1684 NUMVAL (prop) > 0)
1685 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1686 else if (prop = Fplist_get (plist, QCrelative_height),
1687 NUMVAL (prop) > 0)
1688 height = FONT_HEIGHT (font) * NUMVAL (prop);
1689 else
1690 height = FONT_HEIGHT (font);
1691
1692 /* Compute percentage of height used for ascent. If
1693 `:ascent ASCENT' is present and valid, use that. Otherwise,
1694 derive the ascent from the font in use. */
1695 if (prop = Fplist_get (plist, QCascent),
1696 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1697 ascent = NUMVAL (prop) / 100.0;
1698 else
1699 ascent = (double) font->ascent / FONT_HEIGHT (font);
1700
1701 if (width <= 0)
1702 width = 1;
1703 if (height <= 0)
1704 height = 1;
1705
1706 if (it->glyph_row)
1707 {
1708 Lisp_Object object = it->stack[it->sp - 1].string;
1709 if (!STRINGP (object))
1710 object = it->w->buffer;
1711 x_append_stretch_glyph (it, object, width, height, ascent);
1712 }
1713
1714 it->pixel_width = width;
1715 it->ascent = it->phys_ascent = height * ascent;
1716 it->descent = it->phys_descent = height - it->ascent;
1717 it->nglyphs = 1;
1718
1719 if (face->box != FACE_NO_BOX)
1720 {
1721 if (face->box_line_width > 0)
1722 {
1723 it->ascent += face->box_line_width;
1724 it->descent += face->box_line_width;
1725 }
1726
1727 if (it->start_of_box_run_p)
1728 it->pixel_width += abs (face->box_line_width);
1729 if (it->end_of_box_run_p)
1730 it->pixel_width += abs (face->box_line_width);
1731 }
1732
1733 take_vertical_position_into_account (it);
1734 }
1735
1736 /* Return proper value to be used as baseline offset of font that has
1737 ASCENT and DESCENT to draw characters by the font at the vertical
1738 center of the line of frame F.
1739
1740 Here, out task is to find the value of BOFF in the following figure;
1741
1742 -------------------------+-----------+-
1743 -+-+---------+-+ | |
1744 | | | | | |
1745 | | | | F_ASCENT F_HEIGHT
1746 | | | ASCENT | |
1747 HEIGHT | | | | |
1748 | | |-|-+------+-----------|------- baseline
1749 | | | | BOFF | |
1750 | |---------|-+-+ | |
1751 | | | DESCENT | |
1752 -+-+---------+-+ F_DESCENT |
1753 -------------------------+-----------+-
1754
1755 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1756 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1757 DESCENT = FONT->descent
1758 HEIGHT = FONT_HEIGHT (FONT)
1759 F_DESCENT = (F->output_data.x->font->descent
1760 - F->output_data.x->baseline_offset)
1761 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1762 */
1763
1764 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1765 ((FONT)->descent \
1766 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1767 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1768 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1769
1770 /* Produce glyphs/get display metrics for the display element IT is
1771 loaded with. See the description of struct display_iterator in
1772 dispextern.h for an overview of struct display_iterator. */
1773
1774 static void
1775 x_produce_glyphs (it)
1776 struct it *it;
1777 {
1778 it->glyph_not_available_p = 0;
1779
1780 if (it->what == IT_CHARACTER)
1781 {
1782 XChar2b char2b;
1783 XFontStruct *font;
1784 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1785 XCharStruct *pcm;
1786 int font_not_found_p;
1787 struct font_info *font_info;
1788 int boff; /* baseline offset */
1789 /* We may change it->multibyte_p upon unibyte<->multibyte
1790 conversion. So, save the current value now and restore it
1791 later.
1792
1793 Note: It seems that we don't have to record multibyte_p in
1794 struct glyph because the character code itself tells if or
1795 not the character is multibyte. Thus, in the future, we must
1796 consider eliminating the field `multibyte_p' in the struct
1797 glyph. */
1798 int saved_multibyte_p = it->multibyte_p;
1799
1800 /* Maybe translate single-byte characters to multibyte, or the
1801 other way. */
1802 it->char_to_display = it->c;
1803 if (!ASCII_BYTE_P (it->c))
1804 {
1805 if (unibyte_display_via_language_environment
1806 && SINGLE_BYTE_CHAR_P (it->c)
1807 && (it->c >= 0240
1808 || !NILP (Vnonascii_translation_table)))
1809 {
1810 it->char_to_display = unibyte_char_to_multibyte (it->c);
1811 it->multibyte_p = 1;
1812 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1813 face = FACE_FROM_ID (it->f, it->face_id);
1814 }
1815 else if (!SINGLE_BYTE_CHAR_P (it->c)
1816 && !it->multibyte_p)
1817 {
1818 it->multibyte_p = 1;
1819 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1820 face = FACE_FROM_ID (it->f, it->face_id);
1821 }
1822 }
1823
1824 /* Get font to use. Encode IT->char_to_display. */
1825 x_get_char_face_and_encoding (it->f, it->char_to_display,
1826 it->face_id, &char2b,
1827 it->multibyte_p);
1828 font = face->font;
1829
1830 /* When no suitable font found, use the default font. */
1831 font_not_found_p = font == NULL;
1832 if (font_not_found_p)
1833 {
1834 font = FRAME_FONT (it->f);
1835 boff = it->f->output_data.x->baseline_offset;
1836 font_info = NULL;
1837 }
1838 else
1839 {
1840 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1841 boff = font_info->baseline_offset;
1842 if (font_info->vertical_centering)
1843 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1844 }
1845
1846 if (it->char_to_display >= ' '
1847 && (!it->multibyte_p || it->char_to_display < 128))
1848 {
1849 /* Either unibyte or ASCII. */
1850 int stretched_p;
1851
1852 it->nglyphs = 1;
1853
1854 pcm = x_per_char_metric (font, &char2b);
1855 it->ascent = font->ascent + boff;
1856 it->descent = font->descent - boff;
1857
1858 if (pcm)
1859 {
1860 it->phys_ascent = pcm->ascent + boff;
1861 it->phys_descent = pcm->descent - boff;
1862 it->pixel_width = pcm->width;
1863 }
1864 else
1865 {
1866 it->glyph_not_available_p = 1;
1867 it->phys_ascent = font->ascent + boff;
1868 it->phys_descent = font->descent - boff;
1869 it->pixel_width = FONT_WIDTH (font);
1870 }
1871
1872 /* If this is a space inside a region of text with
1873 `space-width' property, change its width. */
1874 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1875 if (stretched_p)
1876 it->pixel_width *= XFLOATINT (it->space_width);
1877
1878 /* If face has a box, add the box thickness to the character
1879 height. If character has a box line to the left and/or
1880 right, add the box line width to the character's width. */
1881 if (face->box != FACE_NO_BOX)
1882 {
1883 int thick = face->box_line_width;
1884
1885 if (thick > 0)
1886 {
1887 it->ascent += thick;
1888 it->descent += thick;
1889 }
1890 else
1891 thick = -thick;
1892
1893 if (it->start_of_box_run_p)
1894 it->pixel_width += thick;
1895 if (it->end_of_box_run_p)
1896 it->pixel_width += thick;
1897 }
1898
1899 /* If face has an overline, add the height of the overline
1900 (1 pixel) and a 1 pixel margin to the character height. */
1901 if (face->overline_p)
1902 it->ascent += 2;
1903
1904 take_vertical_position_into_account (it);
1905
1906 /* If we have to actually produce glyphs, do it. */
1907 if (it->glyph_row)
1908 {
1909 if (stretched_p)
1910 {
1911 /* Translate a space with a `space-width' property
1912 into a stretch glyph. */
1913 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1914 x_append_stretch_glyph (it, it->object, it->pixel_width,
1915 it->ascent + it->descent, ascent);
1916 }
1917 else
1918 x_append_glyph (it);
1919
1920 /* If characters with lbearing or rbearing are displayed
1921 in this line, record that fact in a flag of the
1922 glyph row. This is used to optimize X output code. */
1923 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1924 it->glyph_row->contains_overlapping_glyphs_p = 1;
1925 }
1926 }
1927 else if (it->char_to_display == '\n')
1928 {
1929 /* A newline has no width but we need the height of the line. */
1930 it->pixel_width = 0;
1931 it->nglyphs = 0;
1932 it->ascent = it->phys_ascent = font->ascent + boff;
1933 it->descent = it->phys_descent = font->descent - boff;
1934
1935 if (face->box != FACE_NO_BOX
1936 && face->box_line_width > 0)
1937 {
1938 it->ascent += face->box_line_width;
1939 it->descent += face->box_line_width;
1940 }
1941 }
1942 else if (it->char_to_display == '\t')
1943 {
1944 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1945 int x = it->current_x + it->continuation_lines_width;
1946 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1947
1948 /* If the distance from the current position to the next tab
1949 stop is less than a canonical character width, use the
1950 tab stop after that. */
1951 if (next_tab_x - x < CANON_X_UNIT (it->f))
1952 next_tab_x += tab_width;
1953
1954 it->pixel_width = next_tab_x - x;
1955 it->nglyphs = 1;
1956 it->ascent = it->phys_ascent = font->ascent + boff;
1957 it->descent = it->phys_descent = font->descent - boff;
1958
1959 if (it->glyph_row)
1960 {
1961 double ascent = (double) it->ascent / (it->ascent + it->descent);
1962 x_append_stretch_glyph (it, it->object, it->pixel_width,
1963 it->ascent + it->descent, ascent);
1964 }
1965 }
1966 else
1967 {
1968 /* A multi-byte character. Assume that the display width of the
1969 character is the width of the character multiplied by the
1970 width of the font. */
1971
1972 /* If we found a font, this font should give us the right
1973 metrics. If we didn't find a font, use the frame's
1974 default font and calculate the width of the character
1975 from the charset width; this is what old redisplay code
1976 did. */
1977 pcm = x_per_char_metric (font, &char2b);
1978 if (font_not_found_p || !pcm)
1979 {
1980 int charset = CHAR_CHARSET (it->char_to_display);
1981
1982 it->glyph_not_available_p = 1;
1983 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1984 * CHARSET_WIDTH (charset));
1985 it->phys_ascent = font->ascent + boff;
1986 it->phys_descent = font->descent - boff;
1987 }
1988 else
1989 {
1990 it->pixel_width = pcm->width;
1991 it->phys_ascent = pcm->ascent + boff;
1992 it->phys_descent = pcm->descent - boff;
1993 if (it->glyph_row
1994 && (pcm->lbearing < 0
1995 || pcm->rbearing > pcm->width))
1996 it->glyph_row->contains_overlapping_glyphs_p = 1;
1997 }
1998 it->nglyphs = 1;
1999 it->ascent = font->ascent + boff;
2000 it->descent = font->descent - boff;
2001 if (face->box != FACE_NO_BOX)
2002 {
2003 int thick = face->box_line_width;
2004
2005 if (thick > 0)
2006 {
2007 it->ascent += thick;
2008 it->descent += thick;
2009 }
2010 else
2011 thick = - thick;
2012
2013 if (it->start_of_box_run_p)
2014 it->pixel_width += thick;
2015 if (it->end_of_box_run_p)
2016 it->pixel_width += thick;
2017 }
2018
2019 /* If face has an overline, add the height of the overline
2020 (1 pixel) and a 1 pixel margin to the character height. */
2021 if (face->overline_p)
2022 it->ascent += 2;
2023
2024 take_vertical_position_into_account (it);
2025
2026 if (it->glyph_row)
2027 x_append_glyph (it);
2028 }
2029 it->multibyte_p = saved_multibyte_p;
2030 }
2031 else if (it->what == IT_COMPOSITION)
2032 {
2033 /* Note: A composition is represented as one glyph in the
2034 glyph matrix. There are no padding glyphs. */
2035 XChar2b char2b;
2036 XFontStruct *font;
2037 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2038 XCharStruct *pcm;
2039 int font_not_found_p;
2040 struct font_info *font_info;
2041 int boff; /* baseline offset */
2042 struct composition *cmp = composition_table[it->cmp_id];
2043
2044 /* Maybe translate single-byte characters to multibyte. */
2045 it->char_to_display = it->c;
2046 if (unibyte_display_via_language_environment
2047 && SINGLE_BYTE_CHAR_P (it->c)
2048 && (it->c >= 0240
2049 || (it->c >= 0200
2050 && !NILP (Vnonascii_translation_table))))
2051 {
2052 it->char_to_display = unibyte_char_to_multibyte (it->c);
2053 }
2054
2055 /* Get face and font to use. Encode IT->char_to_display. */
2056 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2057 face = FACE_FROM_ID (it->f, it->face_id);
2058 x_get_char_face_and_encoding (it->f, it->char_to_display,
2059 it->face_id, &char2b, it->multibyte_p);
2060 font = face->font;
2061
2062 /* When no suitable font found, use the default font. */
2063 font_not_found_p = font == NULL;
2064 if (font_not_found_p)
2065 {
2066 font = FRAME_FONT (it->f);
2067 boff = it->f->output_data.x->baseline_offset;
2068 font_info = NULL;
2069 }
2070 else
2071 {
2072 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2073 boff = font_info->baseline_offset;
2074 if (font_info->vertical_centering)
2075 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2076 }
2077
2078 /* There are no padding glyphs, so there is only one glyph to
2079 produce for the composition. Important is that pixel_width,
2080 ascent and descent are the values of what is drawn by
2081 draw_glyphs (i.e. the values of the overall glyphs composed). */
2082 it->nglyphs = 1;
2083
2084 /* If we have not yet calculated pixel size data of glyphs of
2085 the composition for the current face font, calculate them
2086 now. Theoretically, we have to check all fonts for the
2087 glyphs, but that requires much time and memory space. So,
2088 here we check only the font of the first glyph. This leads
2089 to incorrect display very rarely, and C-l (recenter) can
2090 correct the display anyway. */
2091 if (cmp->font != (void *) font)
2092 {
2093 /* Ascent and descent of the font of the first character of
2094 this composition (adjusted by baseline offset). Ascent
2095 and descent of overall glyphs should not be less than
2096 them respectively. */
2097 int font_ascent = font->ascent + boff;
2098 int font_descent = font->descent - boff;
2099 /* Bounding box of the overall glyphs. */
2100 int leftmost, rightmost, lowest, highest;
2101 int i, width, ascent, descent;
2102
2103 cmp->font = (void *) font;
2104
2105 /* Initialize the bounding box. */
2106 if (font_info
2107 && (pcm = x_per_char_metric (font, &char2b)))
2108 {
2109 width = pcm->width;
2110 ascent = pcm->ascent;
2111 descent = pcm->descent;
2112 }
2113 else
2114 {
2115 width = FONT_WIDTH (font);
2116 ascent = font->ascent;
2117 descent = font->descent;
2118 }
2119
2120 rightmost = width;
2121 lowest = - descent + boff;
2122 highest = ascent + boff;
2123 leftmost = 0;
2124
2125 if (font_info
2126 && font_info->default_ascent
2127 && CHAR_TABLE_P (Vuse_default_ascent)
2128 && !NILP (Faref (Vuse_default_ascent,
2129 make_number (it->char_to_display))))
2130 highest = font_info->default_ascent + boff;
2131
2132 /* Draw the first glyph at the normal position. It may be
2133 shifted to right later if some other glyphs are drawn at
2134 the left. */
2135 cmp->offsets[0] = 0;
2136 cmp->offsets[1] = boff;
2137
2138 /* Set cmp->offsets for the remaining glyphs. */
2139 for (i = 1; i < cmp->glyph_len; i++)
2140 {
2141 int left, right, btm, top;
2142 int ch = COMPOSITION_GLYPH (cmp, i);
2143 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2144
2145 face = FACE_FROM_ID (it->f, face_id);
2146 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2147 it->multibyte_p);
2148 font = face->font;
2149 if (font == NULL)
2150 {
2151 font = FRAME_FONT (it->f);
2152 boff = it->f->output_data.x->baseline_offset;
2153 font_info = NULL;
2154 }
2155 else
2156 {
2157 font_info
2158 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2159 boff = font_info->baseline_offset;
2160 if (font_info->vertical_centering)
2161 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2162 }
2163
2164 if (font_info
2165 && (pcm = x_per_char_metric (font, &char2b)))
2166 {
2167 width = pcm->width;
2168 ascent = pcm->ascent;
2169 descent = pcm->descent;
2170 }
2171 else
2172 {
2173 width = FONT_WIDTH (font);
2174 ascent = 1;
2175 descent = 0;
2176 }
2177
2178 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2179 {
2180 /* Relative composition with or without
2181 alternate chars. */
2182 left = (leftmost + rightmost - width) / 2;
2183 btm = - descent + boff;
2184 if (font_info && font_info->relative_compose
2185 && (! CHAR_TABLE_P (Vignore_relative_composition)
2186 || NILP (Faref (Vignore_relative_composition,
2187 make_number (ch)))))
2188 {
2189
2190 if (- descent >= font_info->relative_compose)
2191 /* One extra pixel between two glyphs. */
2192 btm = highest + 1;
2193 else if (ascent <= 0)
2194 /* One extra pixel between two glyphs. */
2195 btm = lowest - 1 - ascent - descent;
2196 }
2197 }
2198 else
2199 {
2200 /* A composition rule is specified by an integer
2201 value that encodes global and new reference
2202 points (GREF and NREF). GREF and NREF are
2203 specified by numbers as below:
2204
2205 0---1---2 -- ascent
2206 | |
2207 | |
2208 | |
2209 9--10--11 -- center
2210 | |
2211 ---3---4---5--- baseline
2212 | |
2213 6---7---8 -- descent
2214 */
2215 int rule = COMPOSITION_RULE (cmp, i);
2216 int gref, nref, grefx, grefy, nrefx, nrefy;
2217
2218 COMPOSITION_DECODE_RULE (rule, gref, nref);
2219 grefx = gref % 3, nrefx = nref % 3;
2220 grefy = gref / 3, nrefy = nref / 3;
2221
2222 left = (leftmost
2223 + grefx * (rightmost - leftmost) / 2
2224 - nrefx * width / 2);
2225 btm = ((grefy == 0 ? highest
2226 : grefy == 1 ? 0
2227 : grefy == 2 ? lowest
2228 : (highest + lowest) / 2)
2229 - (nrefy == 0 ? ascent + descent
2230 : nrefy == 1 ? descent - boff
2231 : nrefy == 2 ? 0
2232 : (ascent + descent) / 2));
2233 }
2234
2235 cmp->offsets[i * 2] = left;
2236 cmp->offsets[i * 2 + 1] = btm + descent;
2237
2238 /* Update the bounding box of the overall glyphs. */
2239 right = left + width;
2240 top = btm + descent + ascent;
2241 if (left < leftmost)
2242 leftmost = left;
2243 if (right > rightmost)
2244 rightmost = right;
2245 if (top > highest)
2246 highest = top;
2247 if (btm < lowest)
2248 lowest = btm;
2249 }
2250
2251 /* If there are glyphs whose x-offsets are negative,
2252 shift all glyphs to the right and make all x-offsets
2253 non-negative. */
2254 if (leftmost < 0)
2255 {
2256 for (i = 0; i < cmp->glyph_len; i++)
2257 cmp->offsets[i * 2] -= leftmost;
2258 rightmost -= leftmost;
2259 }
2260
2261 cmp->pixel_width = rightmost;
2262 cmp->ascent = highest;
2263 cmp->descent = - lowest;
2264 if (cmp->ascent < font_ascent)
2265 cmp->ascent = font_ascent;
2266 if (cmp->descent < font_descent)
2267 cmp->descent = font_descent;
2268 }
2269
2270 it->pixel_width = cmp->pixel_width;
2271 it->ascent = it->phys_ascent = cmp->ascent;
2272 it->descent = it->phys_descent = cmp->descent;
2273
2274 if (face->box != FACE_NO_BOX)
2275 {
2276 int thick = face->box_line_width;
2277
2278 if (thick > 0)
2279 {
2280 it->ascent += thick;
2281 it->descent += thick;
2282 }
2283 else
2284 thick = - thick;
2285
2286 if (it->start_of_box_run_p)
2287 it->pixel_width += thick;
2288 if (it->end_of_box_run_p)
2289 it->pixel_width += thick;
2290 }
2291
2292 /* If face has an overline, add the height of the overline
2293 (1 pixel) and a 1 pixel margin to the character height. */
2294 if (face->overline_p)
2295 it->ascent += 2;
2296
2297 take_vertical_position_into_account (it);
2298
2299 if (it->glyph_row)
2300 x_append_composite_glyph (it);
2301 }
2302 else if (it->what == IT_IMAGE)
2303 x_produce_image_glyph (it);
2304 else if (it->what == IT_STRETCH)
2305 x_produce_stretch_glyph (it);
2306
2307 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2308 because this isn't true for images with `:ascent 100'. */
2309 xassert (it->ascent >= 0 && it->descent >= 0);
2310 if (it->area == TEXT_AREA)
2311 it->current_x += it->pixel_width;
2312
2313 it->descent += it->extra_line_spacing;
2314
2315 it->max_ascent = max (it->max_ascent, it->ascent);
2316 it->max_descent = max (it->max_descent, it->descent);
2317 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2318 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2319 }
2320
2321
2322 /* Estimate the pixel height of the mode or top line on frame F.
2323 FACE_ID specifies what line's height to estimate. */
2324
2325 int
2326 x_estimate_mode_line_height (f, face_id)
2327 struct frame *f;
2328 enum face_id face_id;
2329 {
2330 int height = FONT_HEIGHT (FRAME_FONT (f));
2331
2332 /* This function is called so early when Emacs starts that the face
2333 cache and mode line face are not yet initialized. */
2334 if (FRAME_FACE_CACHE (f))
2335 {
2336 struct face *face = FACE_FROM_ID (f, face_id);
2337 if (face)
2338 {
2339 if (face->font)
2340 height = FONT_HEIGHT (face->font);
2341 if (face->box_line_width > 0)
2342 height += 2 * face->box_line_width;
2343 }
2344 }
2345
2346 return height;
2347 }
2348
2349 \f
2350 /***********************************************************************
2351 Glyph display
2352 ***********************************************************************/
2353
2354 /* A sequence of glyphs to be drawn in the same face.
2355
2356 This data structure is not really completely X specific, so it
2357 could possibly, at least partially, be useful for other systems. It
2358 is currently not part of the external redisplay interface because
2359 it's not clear what other systems will need. */
2360
2361 struct glyph_string
2362 {
2363 /* X-origin of the string. */
2364 int x;
2365
2366 /* Y-origin and y-position of the base line of this string. */
2367 int y, ybase;
2368
2369 /* The width of the string, not including a face extension. */
2370 int width;
2371
2372 /* The width of the string, including a face extension. */
2373 int background_width;
2374
2375 /* The height of this string. This is the height of the line this
2376 string is drawn in, and can be different from the height of the
2377 font the string is drawn in. */
2378 int height;
2379
2380 /* Number of pixels this string overwrites in front of its x-origin.
2381 This number is zero if the string has an lbearing >= 0; it is
2382 -lbearing, if the string has an lbearing < 0. */
2383 int left_overhang;
2384
2385 /* Number of pixels this string overwrites past its right-most
2386 nominal x-position, i.e. x + width. Zero if the string's
2387 rbearing is <= its nominal width, rbearing - width otherwise. */
2388 int right_overhang;
2389
2390 /* The frame on which the glyph string is drawn. */
2391 struct frame *f;
2392
2393 /* The window on which the glyph string is drawn. */
2394 struct window *w;
2395
2396 /* X display and window for convenience. */
2397 Display *display;
2398 Window window;
2399
2400 /* The glyph row for which this string was built. It determines the
2401 y-origin and height of the string. */
2402 struct glyph_row *row;
2403
2404 /* The area within row. */
2405 enum glyph_row_area area;
2406
2407 /* Characters to be drawn, and number of characters. */
2408 XChar2b *char2b;
2409 int nchars;
2410
2411 /* A face-override for drawing cursors, mouse face and similar. */
2412 enum draw_glyphs_face hl;
2413
2414 /* Face in which this string is to be drawn. */
2415 struct face *face;
2416
2417 /* Font in which this string is to be drawn. */
2418 XFontStruct *font;
2419
2420 /* Font info for this string. */
2421 struct font_info *font_info;
2422
2423 /* Non-null means this string describes (part of) a composition.
2424 All characters from char2b are drawn composed. */
2425 struct composition *cmp;
2426
2427 /* Index of this glyph string's first character in the glyph
2428 definition of CMP. If this is zero, this glyph string describes
2429 the first character of a composition. */
2430 int gidx;
2431
2432 /* 1 means this glyph strings face has to be drawn to the right end
2433 of the window's drawing area. */
2434 unsigned extends_to_end_of_line_p : 1;
2435
2436 /* 1 means the background of this string has been drawn. */
2437 unsigned background_filled_p : 1;
2438
2439 /* 1 means glyph string must be drawn with 16-bit functions. */
2440 unsigned two_byte_p : 1;
2441
2442 /* 1 means that the original font determined for drawing this glyph
2443 string could not be loaded. The member `font' has been set to
2444 the frame's default font in this case. */
2445 unsigned font_not_found_p : 1;
2446
2447 /* 1 means that the face in which this glyph string is drawn has a
2448 stipple pattern. */
2449 unsigned stippled_p : 1;
2450
2451 /* 1 means only the foreground of this glyph string must be drawn,
2452 and we should use the physical height of the line this glyph
2453 string appears in as clip rect. */
2454 unsigned for_overlaps_p : 1;
2455
2456 /* The GC to use for drawing this glyph string. */
2457 GC gc;
2458
2459 /* A pointer to the first glyph in the string. This glyph
2460 corresponds to char2b[0]. Needed to draw rectangles if
2461 font_not_found_p is 1. */
2462 struct glyph *first_glyph;
2463
2464 /* Image, if any. */
2465 struct image *img;
2466
2467 struct glyph_string *next, *prev;
2468 };
2469
2470
2471 #if GLYPH_DEBUG
2472
2473 static void
2474 x_dump_glyph_string (s)
2475 struct glyph_string *s;
2476 {
2477 fprintf (stderr, "glyph string\n");
2478 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2479 s->x, s->y, s->width, s->height);
2480 fprintf (stderr, " ybase = %d\n", s->ybase);
2481 fprintf (stderr, " hl = %d\n", s->hl);
2482 fprintf (stderr, " left overhang = %d, right = %d\n",
2483 s->left_overhang, s->right_overhang);
2484 fprintf (stderr, " nchars = %d\n", s->nchars);
2485 fprintf (stderr, " extends to end of line = %d\n",
2486 s->extends_to_end_of_line_p);
2487 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2488 fprintf (stderr, " bg width = %d\n", s->background_width);
2489 }
2490
2491 #endif /* GLYPH_DEBUG */
2492
2493
2494
2495 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2496 struct glyph_string **,
2497 struct glyph_string *,
2498 struct glyph_string *));
2499 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2500 struct glyph_string **,
2501 struct glyph_string *,
2502 struct glyph_string *));
2503 static void x_append_glyph_string P_ ((struct glyph_string **,
2504 struct glyph_string **,
2505 struct glyph_string *));
2506 static int x_left_overwritten P_ ((struct glyph_string *));
2507 static int x_left_overwriting P_ ((struct glyph_string *));
2508 static int x_right_overwritten P_ ((struct glyph_string *));
2509 static int x_right_overwriting P_ ((struct glyph_string *));
2510 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2511 int));
2512 static void x_init_glyph_string P_ ((struct glyph_string *,
2513 XChar2b *, struct window *,
2514 struct glyph_row *,
2515 enum glyph_row_area, int,
2516 enum draw_glyphs_face));
2517 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2518 enum glyph_row_area, int, int,
2519 enum draw_glyphs_face, int));
2520 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2521 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2522 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2523 int));
2524 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2525 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2526 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2527 static void x_draw_glyph_string P_ ((struct glyph_string *));
2528 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2529 static void x_set_cursor_gc P_ ((struct glyph_string *));
2530 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2531 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2532 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2533 int *, int *));
2534 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2535 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2536 unsigned long *, double, int));
2537 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2538 double, int, unsigned long));
2539 static void x_setup_relief_colors P_ ((struct glyph_string *));
2540 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2541 static void x_draw_image_relief P_ ((struct glyph_string *));
2542 static void x_draw_image_foreground P_ ((struct glyph_string *));
2543 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2544 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2545 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2546 int, int, int));
2547 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2548 int, int, int, int, XRectangle *));
2549 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2550 int, int, int, XRectangle *));
2551 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2552 enum glyph_row_area));
2553 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2554 struct glyph_row *,
2555 enum glyph_row_area, int, int));
2556
2557 #if GLYPH_DEBUG
2558 static void x_check_font P_ ((struct frame *, XFontStruct *));
2559 #endif
2560
2561
2562 /* Append the list of glyph strings with head H and tail T to the list
2563 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2564
2565 static INLINE void
2566 x_append_glyph_string_lists (head, tail, h, t)
2567 struct glyph_string **head, **tail;
2568 struct glyph_string *h, *t;
2569 {
2570 if (h)
2571 {
2572 if (*head)
2573 (*tail)->next = h;
2574 else
2575 *head = h;
2576 h->prev = *tail;
2577 *tail = t;
2578 }
2579 }
2580
2581
2582 /* Prepend the list of glyph strings with head H and tail T to the
2583 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2584 result. */
2585
2586 static INLINE void
2587 x_prepend_glyph_string_lists (head, tail, h, t)
2588 struct glyph_string **head, **tail;
2589 struct glyph_string *h, *t;
2590 {
2591 if (h)
2592 {
2593 if (*head)
2594 (*head)->prev = t;
2595 else
2596 *tail = t;
2597 t->next = *head;
2598 *head = h;
2599 }
2600 }
2601
2602
2603 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2604 Set *HEAD and *TAIL to the resulting list. */
2605
2606 static INLINE void
2607 x_append_glyph_string (head, tail, s)
2608 struct glyph_string **head, **tail;
2609 struct glyph_string *s;
2610 {
2611 s->next = s->prev = NULL;
2612 x_append_glyph_string_lists (head, tail, s, s);
2613 }
2614
2615
2616 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2617 face. */
2618
2619 static void
2620 x_set_cursor_gc (s)
2621 struct glyph_string *s;
2622 {
2623 if (s->font == FRAME_FONT (s->f)
2624 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2625 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2626 && !s->cmp)
2627 s->gc = s->f->output_data.x->cursor_gc;
2628 else
2629 {
2630 /* Cursor on non-default face: must merge. */
2631 XGCValues xgcv;
2632 unsigned long mask;
2633
2634 xgcv.background = s->f->output_data.x->cursor_pixel;
2635 xgcv.foreground = s->face->background;
2636
2637 /* If the glyph would be invisible, try a different foreground. */
2638 if (xgcv.foreground == xgcv.background)
2639 xgcv.foreground = s->face->foreground;
2640 if (xgcv.foreground == xgcv.background)
2641 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2642 if (xgcv.foreground == xgcv.background)
2643 xgcv.foreground = s->face->foreground;
2644
2645 /* Make sure the cursor is distinct from text in this face. */
2646 if (xgcv.background == s->face->background
2647 && xgcv.foreground == s->face->foreground)
2648 {
2649 xgcv.background = s->face->foreground;
2650 xgcv.foreground = s->face->background;
2651 }
2652
2653 IF_DEBUG (x_check_font (s->f, s->font));
2654 xgcv.font = s->font->fid;
2655 xgcv.graphics_exposures = False;
2656 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2657
2658 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2659 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2660 mask, &xgcv);
2661 else
2662 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2663 = XCreateGC (s->display, s->window, mask, &xgcv);
2664
2665 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2666 }
2667 }
2668
2669
2670 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2671
2672 static void
2673 x_set_mouse_face_gc (s)
2674 struct glyph_string *s;
2675 {
2676 int face_id;
2677 struct face *face;
2678
2679 /* What face has to be used last for the mouse face? */
2680 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2681 face = FACE_FROM_ID (s->f, face_id);
2682 if (face == NULL)
2683 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2684
2685 if (s->first_glyph->type == CHAR_GLYPH)
2686 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2687 else
2688 face_id = FACE_FOR_CHAR (s->f, face, 0);
2689 s->face = FACE_FROM_ID (s->f, face_id);
2690 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2691
2692 /* If font in this face is same as S->font, use it. */
2693 if (s->font == s->face->font)
2694 s->gc = s->face->gc;
2695 else
2696 {
2697 /* Otherwise construct scratch_cursor_gc with values from FACE
2698 but font FONT. */
2699 XGCValues xgcv;
2700 unsigned long mask;
2701
2702 xgcv.background = s->face->background;
2703 xgcv.foreground = s->face->foreground;
2704 IF_DEBUG (x_check_font (s->f, s->font));
2705 xgcv.font = s->font->fid;
2706 xgcv.graphics_exposures = False;
2707 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2708
2709 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2710 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2711 mask, &xgcv);
2712 else
2713 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2714 = XCreateGC (s->display, s->window, mask, &xgcv);
2715
2716 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2717 }
2718
2719 xassert (s->gc != 0);
2720 }
2721
2722
2723 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2724 Faces to use in the mode line have already been computed when the
2725 matrix was built, so there isn't much to do, here. */
2726
2727 static INLINE void
2728 x_set_mode_line_face_gc (s)
2729 struct glyph_string *s;
2730 {
2731 s->gc = s->face->gc;
2732 }
2733
2734
2735 /* Set S->gc of glyph string S for drawing that glyph string. Set
2736 S->stippled_p to a non-zero value if the face of S has a stipple
2737 pattern. */
2738
2739 static INLINE void
2740 x_set_glyph_string_gc (s)
2741 struct glyph_string *s;
2742 {
2743 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2744
2745 if (s->hl == DRAW_NORMAL_TEXT)
2746 {
2747 s->gc = s->face->gc;
2748 s->stippled_p = s->face->stipple != 0;
2749 }
2750 else if (s->hl == DRAW_INVERSE_VIDEO)
2751 {
2752 x_set_mode_line_face_gc (s);
2753 s->stippled_p = s->face->stipple != 0;
2754 }
2755 else if (s->hl == DRAW_CURSOR)
2756 {
2757 x_set_cursor_gc (s);
2758 s->stippled_p = 0;
2759 }
2760 else if (s->hl == DRAW_MOUSE_FACE)
2761 {
2762 x_set_mouse_face_gc (s);
2763 s->stippled_p = s->face->stipple != 0;
2764 }
2765 else if (s->hl == DRAW_IMAGE_RAISED
2766 || s->hl == DRAW_IMAGE_SUNKEN)
2767 {
2768 s->gc = s->face->gc;
2769 s->stippled_p = s->face->stipple != 0;
2770 }
2771 else
2772 {
2773 s->gc = s->face->gc;
2774 s->stippled_p = s->face->stipple != 0;
2775 }
2776
2777 /* GC must have been set. */
2778 xassert (s->gc != 0);
2779 }
2780
2781
2782 /* Return in *R the clipping rectangle for glyph string S. */
2783
2784 static void
2785 x_get_glyph_string_clip_rect (s, r)
2786 struct glyph_string *s;
2787 XRectangle *r;
2788 {
2789 if (s->row->full_width_p)
2790 {
2791 /* Draw full-width. X coordinates are relative to S->w->left. */
2792 int canon_x = CANON_X_UNIT (s->f);
2793
2794 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2795 r->width = XFASTINT (s->w->width) * canon_x;
2796
2797 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2798 {
2799 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2800 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2801 r->x -= width;
2802 }
2803
2804 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2805
2806 /* Unless displaying a mode or menu bar line, which are always
2807 fully visible, clip to the visible part of the row. */
2808 if (s->w->pseudo_window_p)
2809 r->height = s->row->visible_height;
2810 else
2811 r->height = s->height;
2812 }
2813 else
2814 {
2815 /* This is a text line that may be partially visible. */
2816 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2817 r->width = window_box_width (s->w, s->area);
2818 r->height = s->row->visible_height;
2819 }
2820
2821 /* If S draws overlapping rows, it's sufficient to use the top and
2822 bottom of the window for clipping because this glyph string
2823 intentionally draws over other lines. */
2824 if (s->for_overlaps_p)
2825 {
2826 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2827 r->height = window_text_bottom_y (s->w) - r->y;
2828 }
2829 else
2830 {
2831 /* Don't use S->y for clipping because it doesn't take partially
2832 visible lines into account. For example, it can be negative for
2833 partially visible lines at the top of a window. */
2834 if (!s->row->full_width_p
2835 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2836 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2837 else
2838 r->y = max (0, s->row->y);
2839
2840 /* If drawing a tool-bar window, draw it over the internal border
2841 at the top of the window. */
2842 if (s->w == XWINDOW (s->f->tool_bar_window))
2843 r->y -= s->f->output_data.x->internal_border_width;
2844 }
2845
2846 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2847 }
2848
2849
2850 /* Set clipping for output of glyph string S. S may be part of a mode
2851 line or menu if we don't have X toolkit support. */
2852
2853 static INLINE void
2854 x_set_glyph_string_clipping (s)
2855 struct glyph_string *s;
2856 {
2857 XRectangle r;
2858 x_get_glyph_string_clip_rect (s, &r);
2859 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2860 }
2861
2862
2863 /* Compute left and right overhang of glyph string S. If S is a glyph
2864 string for a composition, assume overhangs don't exist. */
2865
2866 static INLINE void
2867 x_compute_glyph_string_overhangs (s)
2868 struct glyph_string *s;
2869 {
2870 if (s->cmp == NULL
2871 && s->first_glyph->type == CHAR_GLYPH)
2872 {
2873 XCharStruct cs;
2874 int direction, font_ascent, font_descent;
2875 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2876 &font_ascent, &font_descent, &cs);
2877 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2878 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2879 }
2880 }
2881
2882
2883 /* Compute overhangs and x-positions for glyph string S and its
2884 predecessors, or successors. X is the starting x-position for S.
2885 BACKWARD_P non-zero means process predecessors. */
2886
2887 static void
2888 x_compute_overhangs_and_x (s, x, backward_p)
2889 struct glyph_string *s;
2890 int x;
2891 int backward_p;
2892 {
2893 if (backward_p)
2894 {
2895 while (s)
2896 {
2897 x_compute_glyph_string_overhangs (s);
2898 x -= s->width;
2899 s->x = x;
2900 s = s->prev;
2901 }
2902 }
2903 else
2904 {
2905 while (s)
2906 {
2907 x_compute_glyph_string_overhangs (s);
2908 s->x = x;
2909 x += s->width;
2910 s = s->next;
2911 }
2912 }
2913 }
2914
2915
2916 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2917 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2918 assumed to be zero. */
2919
2920 static void
2921 x_get_glyph_overhangs (glyph, f, left, right)
2922 struct glyph *glyph;
2923 struct frame *f;
2924 int *left, *right;
2925 {
2926 *left = *right = 0;
2927
2928 if (glyph->type == CHAR_GLYPH)
2929 {
2930 XFontStruct *font;
2931 struct face *face;
2932 struct font_info *font_info;
2933 XChar2b char2b;
2934 XCharStruct *pcm;
2935
2936 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2937 font = face->font;
2938 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2939 if (font
2940 && (pcm = x_per_char_metric (font, &char2b)))
2941 {
2942 if (pcm->rbearing > pcm->width)
2943 *right = pcm->rbearing - pcm->width;
2944 if (pcm->lbearing < 0)
2945 *left = -pcm->lbearing;
2946 }
2947 }
2948 }
2949
2950
2951 /* Return the index of the first glyph preceding glyph string S that
2952 is overwritten by S because of S's left overhang. Value is -1
2953 if no glyphs are overwritten. */
2954
2955 static int
2956 x_left_overwritten (s)
2957 struct glyph_string *s;
2958 {
2959 int k;
2960
2961 if (s->left_overhang)
2962 {
2963 int x = 0, i;
2964 struct glyph *glyphs = s->row->glyphs[s->area];
2965 int first = s->first_glyph - glyphs;
2966
2967 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2968 x -= glyphs[i].pixel_width;
2969
2970 k = i + 1;
2971 }
2972 else
2973 k = -1;
2974
2975 return k;
2976 }
2977
2978
2979 /* Return the index of the first glyph preceding glyph string S that
2980 is overwriting S because of its right overhang. Value is -1 if no
2981 glyph in front of S overwrites S. */
2982
2983 static int
2984 x_left_overwriting (s)
2985 struct glyph_string *s;
2986 {
2987 int i, k, x;
2988 struct glyph *glyphs = s->row->glyphs[s->area];
2989 int first = s->first_glyph - glyphs;
2990
2991 k = -1;
2992 x = 0;
2993 for (i = first - 1; i >= 0; --i)
2994 {
2995 int left, right;
2996 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2997 if (x + right > 0)
2998 k = i;
2999 x -= glyphs[i].pixel_width;
3000 }
3001
3002 return k;
3003 }
3004
3005
3006 /* Return the index of the last glyph following glyph string S that is
3007 not overwritten by S because of S's right overhang. Value is -1 if
3008 no such glyph is found. */
3009
3010 static int
3011 x_right_overwritten (s)
3012 struct glyph_string *s;
3013 {
3014 int k = -1;
3015
3016 if (s->right_overhang)
3017 {
3018 int x = 0, i;
3019 struct glyph *glyphs = s->row->glyphs[s->area];
3020 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3021 int end = s->row->used[s->area];
3022
3023 for (i = first; i < end && s->right_overhang > x; ++i)
3024 x += glyphs[i].pixel_width;
3025
3026 k = i;
3027 }
3028
3029 return k;
3030 }
3031
3032
3033 /* Return the index of the last glyph following glyph string S that
3034 overwrites S because of its left overhang. Value is negative
3035 if no such glyph is found. */
3036
3037 static int
3038 x_right_overwriting (s)
3039 struct glyph_string *s;
3040 {
3041 int i, k, x;
3042 int end = s->row->used[s->area];
3043 struct glyph *glyphs = s->row->glyphs[s->area];
3044 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3045
3046 k = -1;
3047 x = 0;
3048 for (i = first; i < end; ++i)
3049 {
3050 int left, right;
3051 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3052 if (x - left < 0)
3053 k = i;
3054 x += glyphs[i].pixel_width;
3055 }
3056
3057 return k;
3058 }
3059
3060
3061 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3062
3063 static INLINE void
3064 x_clear_glyph_string_rect (s, x, y, w, h)
3065 struct glyph_string *s;
3066 int x, y, w, h;
3067 {
3068 XGCValues xgcv;
3069 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3070 XSetForeground (s->display, s->gc, xgcv.background);
3071 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3072 XSetForeground (s->display, s->gc, xgcv.foreground);
3073 }
3074
3075
3076 /* Draw the background of glyph_string S. If S->background_filled_p
3077 is non-zero don't draw it. FORCE_P non-zero means draw the
3078 background even if it wouldn't be drawn normally. This is used
3079 when a string preceding S draws into the background of S, or S
3080 contains the first component of a composition. */
3081
3082 static void
3083 x_draw_glyph_string_background (s, force_p)
3084 struct glyph_string *s;
3085 int force_p;
3086 {
3087 /* Nothing to do if background has already been drawn or if it
3088 shouldn't be drawn in the first place. */
3089 if (!s->background_filled_p)
3090 {
3091 int box_line_width = max (s->face->box_line_width, 0);
3092
3093 if (s->stippled_p)
3094 {
3095 /* Fill background with a stipple pattern. */
3096 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3097 XFillRectangle (s->display, s->window, s->gc, s->x,
3098 s->y + box_line_width,
3099 s->background_width,
3100 s->height - 2 * box_line_width);
3101 XSetFillStyle (s->display, s->gc, FillSolid);
3102 s->background_filled_p = 1;
3103 }
3104 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
3105 || s->font_not_found_p
3106 || s->extends_to_end_of_line_p
3107 || force_p)
3108 {
3109 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
3110 s->background_width,
3111 s->height - 2 * box_line_width);
3112 s->background_filled_p = 1;
3113 }
3114 }
3115 }
3116
3117
3118 /* Draw the foreground of glyph string S. */
3119
3120 static void
3121 x_draw_glyph_string_foreground (s)
3122 struct glyph_string *s;
3123 {
3124 int i, x;
3125
3126 /* If first glyph of S has a left box line, start drawing the text
3127 of S to the right of that box line. */
3128 if (s->face->box != FACE_NO_BOX
3129 && s->first_glyph->left_box_line_p)
3130 x = s->x + abs (s->face->box_line_width);
3131 else
3132 x = s->x;
3133
3134 /* Draw characters of S as rectangles if S's font could not be
3135 loaded. */
3136 if (s->font_not_found_p)
3137 {
3138 for (i = 0; i < s->nchars; ++i)
3139 {
3140 struct glyph *g = s->first_glyph + i;
3141 XDrawRectangle (s->display, s->window,
3142 s->gc, x, s->y, g->pixel_width - 1,
3143 s->height - 1);
3144 x += g->pixel_width;
3145 }
3146 }
3147 else
3148 {
3149 char *char1b = (char *) s->char2b;
3150 int boff = s->font_info->baseline_offset;
3151
3152 if (s->font_info->vertical_centering)
3153 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3154
3155 /* If we can use 8-bit functions, condense S->char2b. */
3156 if (!s->two_byte_p)
3157 for (i = 0; i < s->nchars; ++i)
3158 char1b[i] = s->char2b[i].byte2;
3159
3160 /* Draw text with XDrawString if background has already been
3161 filled. Otherwise, use XDrawImageString. (Note that
3162 XDrawImageString is usually faster than XDrawString.) Always
3163 use XDrawImageString when drawing the cursor so that there is
3164 no chance that characters under a box cursor are invisible. */
3165 if (s->for_overlaps_p
3166 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3167 {
3168 /* Draw characters with 16-bit or 8-bit functions. */
3169 if (s->two_byte_p)
3170 XDrawString16 (s->display, s->window, s->gc, x,
3171 s->ybase - boff, s->char2b, s->nchars);
3172 else
3173 XDrawString (s->display, s->window, s->gc, x,
3174 s->ybase - boff, char1b, s->nchars);
3175 }
3176 else
3177 {
3178 if (s->two_byte_p)
3179 XDrawImageString16 (s->display, s->window, s->gc, x,
3180 s->ybase - boff, s->char2b, s->nchars);
3181 else
3182 XDrawImageString (s->display, s->window, s->gc, x,
3183 s->ybase - boff, char1b, s->nchars);
3184 }
3185 }
3186 }
3187
3188 /* Draw the foreground of composite glyph string S. */
3189
3190 static void
3191 x_draw_composite_glyph_string_foreground (s)
3192 struct glyph_string *s;
3193 {
3194 int i, x;
3195
3196 /* If first glyph of S has a left box line, start drawing the text
3197 of S to the right of that box line. */
3198 if (s->face->box != FACE_NO_BOX
3199 && s->first_glyph->left_box_line_p)
3200 x = s->x + abs (s->face->box_line_width);
3201 else
3202 x = s->x;
3203
3204 /* S is a glyph string for a composition. S->gidx is the index of
3205 the first character drawn for glyphs of this composition.
3206 S->gidx == 0 means we are drawing the very first character of
3207 this composition. */
3208
3209 /* Draw a rectangle for the composition if the font for the very
3210 first character of the composition could not be loaded. */
3211 if (s->font_not_found_p)
3212 {
3213 if (s->gidx == 0)
3214 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3215 s->width - 1, s->height - 1);
3216 }
3217 else
3218 {
3219 for (i = 0; i < s->nchars; i++, ++s->gidx)
3220 XDrawString16 (s->display, s->window, s->gc,
3221 x + s->cmp->offsets[s->gidx * 2],
3222 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3223 s->char2b + i, 1);
3224 }
3225 }
3226
3227
3228 #ifdef USE_X_TOOLKIT
3229
3230 static struct frame *x_frame_of_widget P_ ((Widget));
3231 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
3232 XrmValue *, XrmValue *, XtPointer *));
3233 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
3234 XrmValue *, Cardinal *));
3235
3236
3237 /* Return the frame on which widget WIDGET is used.. Abort if frame
3238 cannot be determined. */
3239
3240 static struct frame *
3241 x_frame_of_widget (widget)
3242 Widget widget;
3243 {
3244 struct x_display_info *dpyinfo;
3245 Lisp_Object tail;
3246 struct frame *f;
3247
3248 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3249
3250 /* Find the top-level shell of the widget. Note that this function
3251 can be called when the widget is not yet realized, so XtWindow
3252 (widget) == 0. That's the reason we can't simply use
3253 x_any_window_to_frame. */
3254 while (!XtIsTopLevelShell (widget))
3255 widget = XtParent (widget);
3256
3257 /* Look for a frame with that top-level widget. Allocate the color
3258 on that frame to get the right gamma correction value. */
3259 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3260 if (GC_FRAMEP (XCAR (tail))
3261 && (f = XFRAME (XCAR (tail)),
3262 (f->output_data.nothing != 1
3263 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3264 && f->output_data.x->widget == widget)
3265 return f;
3266
3267 abort ();
3268 }
3269
3270
3271 /* Allocate the color COLOR->pixel on the screen and display of
3272 widget WIDGET in colormap CMAP. If an exact match cannot be
3273 allocated, try the nearest color available. Value is non-zero
3274 if successful. This is called from lwlib. */
3275
3276 int
3277 x_alloc_nearest_color_for_widget (widget, cmap, color)
3278 Widget widget;
3279 Colormap cmap;
3280 XColor *color;
3281 {
3282 struct frame *f = x_frame_of_widget (widget);
3283 return x_alloc_nearest_color (f, cmap, color);
3284 }
3285
3286
3287 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3288 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3289 If this produces the same color as PIXEL, try a color where all RGB
3290 values have DELTA added. Return the allocated color in *PIXEL.
3291 DISPLAY is the X display, CMAP is the colormap to operate on.
3292 Value is non-zero if successful. */
3293
3294 int
3295 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3296 Widget widget;
3297 Display *display;
3298 Colormap cmap;
3299 unsigned long *pixel;
3300 double factor;
3301 int delta;
3302 {
3303 struct frame *f = x_frame_of_widget (widget);
3304 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3305 }
3306
3307
3308 /* Structure specifying which arguments should be passed by Xt to
3309 cvt_string_to_pixel. We want the widget's screen and colormap. */
3310
3311 static XtConvertArgRec cvt_string_to_pixel_args[] =
3312 {
3313 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
3314 sizeof (Screen *)},
3315 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
3316 sizeof (Colormap)}
3317 };
3318
3319
3320 /* The address of this variable is returned by
3321 cvt_string_to_pixel. */
3322
3323 static Pixel cvt_string_to_pixel_value;
3324
3325
3326 /* Convert a color name to a pixel color.
3327
3328 DPY is the display we are working on.
3329
3330 ARGS is an array of *NARGS XrmValue structures holding additional
3331 information about the widget for which the conversion takes place.
3332 The contents of this array are determined by the specification
3333 in cvt_string_to_pixel_args.
3334
3335 FROM is a pointer to an XrmValue which points to the color name to
3336 convert. TO is an XrmValue in which to return the pixel color.
3337
3338 CLOSURE_RET is a pointer to user-data, in which we record if
3339 we allocated the color or not.
3340
3341 Value is True if successful, False otherwise. */
3342
3343 static Boolean
3344 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
3345 Display *dpy;
3346 XrmValue *args;
3347 Cardinal *nargs;
3348 XrmValue *from, *to;
3349 XtPointer *closure_ret;
3350 {
3351 Screen *screen;
3352 Colormap cmap;
3353 Pixel pixel;
3354 String color_name;
3355 XColor color;
3356
3357 if (*nargs != 2)
3358 {
3359 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3360 "wrongParameters", "cvt_string_to_pixel",
3361 "XtToolkitError",
3362 "Screen and colormap args required", NULL, NULL);
3363 return False;
3364 }
3365
3366 screen = *(Screen **) args[0].addr;
3367 cmap = *(Colormap *) args[1].addr;
3368 color_name = (String) from->addr;
3369
3370 if (strcmp (color_name, XtDefaultBackground) == 0)
3371 {
3372 *closure_ret = (XtPointer) False;
3373 pixel = WhitePixelOfScreen (screen);
3374 }
3375 else if (strcmp (color_name, XtDefaultForeground) == 0)
3376 {
3377 *closure_ret = (XtPointer) False;
3378 pixel = BlackPixelOfScreen (screen);
3379 }
3380 else if (XParseColor (dpy, cmap, color_name, &color)
3381 && x_alloc_nearest_color_1 (dpy, cmap, &color))
3382 {
3383 pixel = color.pixel;
3384 *closure_ret = (XtPointer) True;
3385 }
3386 else
3387 {
3388 String params[1];
3389 Cardinal nparams = 1;
3390
3391 params[0] = color_name;
3392 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3393 "badValue", "cvt_string_to_pixel",
3394 "XtToolkitError", "Invalid color `%s'",
3395 params, &nparams);
3396 return False;
3397 }
3398
3399 if (to->addr != NULL)
3400 {
3401 if (to->size < sizeof (Pixel))
3402 {
3403 to->size = sizeof (Pixel);
3404 return False;
3405 }
3406
3407 *(Pixel *) to->addr = pixel;
3408 }
3409 else
3410 {
3411 cvt_string_to_pixel_value = pixel;
3412 to->addr = (XtPointer) &cvt_string_to_pixel_value;
3413 }
3414
3415 to->size = sizeof (Pixel);
3416 return True;
3417 }
3418
3419
3420 /* Free a pixel color which was previously allocated via
3421 cvt_string_to_pixel. This is registered as the destructor
3422 for this type of resource via XtSetTypeConverter.
3423
3424 APP is the application context in which we work.
3425
3426 TO is a pointer to an XrmValue holding the color to free.
3427 CLOSURE is the value we stored in CLOSURE_RET for this color
3428 in cvt_string_to_pixel.
3429
3430 ARGS and NARGS are like for cvt_string_to_pixel. */
3431
3432 static void
3433 cvt_pixel_dtor (app, to, closure, args, nargs)
3434 XtAppContext app;
3435 XrmValuePtr to;
3436 XtPointer closure;
3437 XrmValuePtr args;
3438 Cardinal *nargs;
3439 {
3440 if (*nargs != 2)
3441 {
3442 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
3443 "XtToolkitError",
3444 "Screen and colormap arguments required",
3445 NULL, NULL);
3446 }
3447 else if (closure != NULL)
3448 {
3449 /* We did allocate the pixel, so free it. */
3450 Screen *screen = *(Screen **) args[0].addr;
3451 Colormap cmap = *(Colormap *) args[1].addr;
3452 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
3453 (Pixel *) to->addr, 1);
3454 }
3455 }
3456
3457
3458 #endif /* USE_X_TOOLKIT */
3459
3460
3461 /* Value is an array of XColor structures for the contents of the
3462 color map of display DPY. Set *NCELLS to the size of the array.
3463 Note that this probably shouldn't be called for large color maps,
3464 say a 24-bit TrueColor map. */
3465
3466 static const XColor *
3467 x_color_cells (dpy, ncells)
3468 Display *dpy;
3469 int *ncells;
3470 {
3471 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3472
3473 if (dpyinfo->color_cells == NULL)
3474 {
3475 Screen *screen = dpyinfo->screen;
3476 int i;
3477
3478 dpyinfo->ncolor_cells
3479 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
3480 dpyinfo->color_cells
3481 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3482 * sizeof *dpyinfo->color_cells);
3483
3484 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3485 dpyinfo->color_cells[i].pixel = i;
3486
3487 XQueryColors (dpy, dpyinfo->cmap,
3488 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3489 }
3490
3491 *ncells = dpyinfo->ncolor_cells;
3492 return dpyinfo->color_cells;
3493 }
3494
3495
3496 /* On frame F, translate pixel colors to RGB values for the NCOLORS
3497 colors in COLORS. Use cached information, if available. */
3498
3499 void
3500 x_query_colors (f, colors, ncolors)
3501 struct frame *f;
3502 XColor *colors;
3503 int ncolors;
3504 {
3505 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3506
3507 if (dpyinfo->color_cells)
3508 {
3509 int i;
3510 for (i = 0; i < ncolors; ++i)
3511 {
3512 unsigned long pixel = colors[i].pixel;
3513 xassert (pixel < dpyinfo->ncolor_cells);
3514 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3515 colors[i] = dpyinfo->color_cells[pixel];
3516 }
3517 }
3518 else
3519 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3520 }
3521
3522
3523 /* On frame F, translate pixel color to RGB values for the color in
3524 COLOR. Use cached information, if available. */
3525
3526 void
3527 x_query_color (f, color)
3528 struct frame *f;
3529 XColor *color;
3530 {
3531 x_query_colors (f, color, 1);
3532 }
3533
3534
3535 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
3536 exact match can't be allocated, try the nearest color available.
3537 Value is non-zero if successful. Set *COLOR to the color
3538 allocated. */
3539
3540 static int
3541 x_alloc_nearest_color_1 (dpy, cmap, color)
3542 Display *dpy;
3543 Colormap cmap;
3544 XColor *color;
3545 {
3546 int rc;
3547
3548 rc = XAllocColor (dpy, cmap, color);
3549 if (rc == 0)
3550 {
3551 /* If we got to this point, the colormap is full, so we're going
3552 to try to get the next closest color. The algorithm used is
3553 a least-squares matching, which is what X uses for closest
3554 color matching with StaticColor visuals. */
3555 int nearest, i;
3556 unsigned long nearest_delta = ~0;
3557 int ncells;
3558 const XColor *cells = x_color_cells (dpy, &ncells);
3559
3560 for (nearest = i = 0; i < ncells; ++i)
3561 {
3562 long dred = (color->red >> 8) - (cells[i].red >> 8);
3563 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3564 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3565 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3566
3567 if (delta < nearest_delta)
3568 {
3569 nearest = i;
3570 nearest_delta = delta;
3571 }
3572 }
3573
3574 color->red = cells[nearest].red;
3575 color->green = cells[nearest].green;
3576 color->blue = cells[nearest].blue;
3577 rc = XAllocColor (dpy, cmap, color);
3578 }
3579 else
3580 {
3581 /* If allocation succeeded, and the allocated pixel color is not
3582 equal to a cached pixel color recorded earlier, there was a
3583 change in the colormap, so clear the color cache. */
3584 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3585 XColor *cached_color;
3586
3587 if (dpyinfo->color_cells
3588 && (cached_color = &dpyinfo->color_cells[color->pixel],
3589 (cached_color->red != color->red
3590 || cached_color->blue != color->blue
3591 || cached_color->green != color->green)))
3592 {
3593 xfree (dpyinfo->color_cells);
3594 dpyinfo->color_cells = NULL;
3595 dpyinfo->ncolor_cells = 0;
3596 }
3597 }
3598
3599 #ifdef DEBUG_X_COLORS
3600 if (rc)
3601 register_color (color->pixel);
3602 #endif /* DEBUG_X_COLORS */
3603
3604 return rc;
3605 }
3606
3607
3608 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
3609 exact match can't be allocated, try the nearest color available.
3610 Value is non-zero if successful. Set *COLOR to the color
3611 allocated. */
3612
3613 int
3614 x_alloc_nearest_color (f, cmap, color)
3615 struct frame *f;
3616 Colormap cmap;
3617 XColor *color;
3618 {
3619 gamma_correct (f, color);
3620 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
3621 }
3622
3623
3624 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3625 It's necessary to do this instead of just using PIXEL directly to
3626 get color reference counts right. */
3627
3628 unsigned long
3629 x_copy_color (f, pixel)
3630 struct frame *f;
3631 unsigned long pixel;
3632 {
3633 XColor color;
3634
3635 color.pixel = pixel;
3636 BLOCK_INPUT;
3637 x_query_color (f, &color);
3638 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3639 UNBLOCK_INPUT;
3640 #ifdef DEBUG_X_COLORS
3641 register_color (pixel);
3642 #endif
3643 return color.pixel;
3644 }
3645
3646
3647 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3648 It's necessary to do this instead of just using PIXEL directly to
3649 get color reference counts right. */
3650
3651 unsigned long
3652 x_copy_dpy_color (dpy, cmap, pixel)
3653 Display *dpy;
3654 Colormap cmap;
3655 unsigned long pixel;
3656 {
3657 XColor color;
3658
3659 color.pixel = pixel;
3660 BLOCK_INPUT;
3661 XQueryColor (dpy, cmap, &color);
3662 XAllocColor (dpy, cmap, &color);
3663 UNBLOCK_INPUT;
3664 #ifdef DEBUG_X_COLORS
3665 register_color (pixel);
3666 #endif
3667 return color.pixel;
3668 }
3669
3670
3671 /* Brightness beyond which a color won't have its highlight brightness
3672 boosted.
3673
3674 Nominally, highlight colors for `3d' faces are calculated by
3675 brightening an object's color by a constant scale factor, but this
3676 doesn't yield good results for dark colors, so for colors who's
3677 brightness is less than this value (on a scale of 0-65535) have an
3678 use an additional additive factor.
3679
3680 The value here is set so that the default menu-bar/mode-line color
3681 (grey75) will not have its highlights changed at all. */
3682 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3683
3684
3685 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3686 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3687 If this produces the same color as PIXEL, try a color where all RGB
3688 values have DELTA added. Return the allocated color in *PIXEL.
3689 DISPLAY is the X display, CMAP is the colormap to operate on.
3690 Value is non-zero if successful. */
3691
3692 static int
3693 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3694 struct frame *f;
3695 Display *display;
3696 Colormap cmap;
3697 unsigned long *pixel;
3698 double factor;
3699 int delta;
3700 {
3701 XColor color, new;
3702 long bright;
3703 int success_p;
3704
3705 /* Get RGB color values. */
3706 color.pixel = *pixel;
3707 x_query_color (f, &color);
3708
3709 /* Change RGB values by specified FACTOR. Avoid overflow! */
3710 xassert (factor >= 0);
3711 new.red = min (0xffff, factor * color.red);
3712 new.green = min (0xffff, factor * color.green);
3713 new.blue = min (0xffff, factor * color.blue);
3714
3715 /* Calculate brightness of COLOR. */
3716 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
3717
3718 /* We only boost colors that are darker than
3719 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3720 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3721 /* Make an additive adjustment to NEW, because it's dark enough so
3722 that scaling by FACTOR alone isn't enough. */
3723 {
3724 /* How far below the limit this color is (0 - 1, 1 being darker). */
3725 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3726 /* The additive adjustment. */
3727 int min_delta = delta * dimness * factor / 2;
3728
3729 if (factor < 1)
3730 {
3731 new.red = max (0, new.red - min_delta);
3732 new.green = max (0, new.green - min_delta);
3733 new.blue = max (0, new.blue - min_delta);
3734 }
3735 else
3736 {
3737 new.red = min (0xffff, min_delta + new.red);
3738 new.green = min (0xffff, min_delta + new.green);
3739 new.blue = min (0xffff, min_delta + new.blue);
3740 }
3741 }
3742
3743 /* Try to allocate the color. */
3744 success_p = x_alloc_nearest_color (f, cmap, &new);
3745 if (success_p)
3746 {
3747 if (new.pixel == *pixel)
3748 {
3749 /* If we end up with the same color as before, try adding
3750 delta to the RGB values. */
3751 x_free_colors (f, &new.pixel, 1);
3752
3753 new.red = min (0xffff, delta + color.red);
3754 new.green = min (0xffff, delta + color.green);
3755 new.blue = min (0xffff, delta + color.blue);
3756 success_p = x_alloc_nearest_color (f, cmap, &new);
3757 }
3758 else
3759 success_p = 1;
3760 *pixel = new.pixel;
3761 }
3762
3763 return success_p;
3764 }
3765
3766
3767 /* Set up the foreground color for drawing relief lines of glyph
3768 string S. RELIEF is a pointer to a struct relief containing the GC
3769 with which lines will be drawn. Use a color that is FACTOR or
3770 DELTA lighter or darker than the relief's background which is found
3771 in S->f->output_data.x->relief_background. If such a color cannot
3772 be allocated, use DEFAULT_PIXEL, instead. */
3773
3774 static void
3775 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3776 struct frame *f;
3777 struct relief *relief;
3778 double factor;
3779 int delta;
3780 unsigned long default_pixel;
3781 {
3782 XGCValues xgcv;
3783 struct x_output *di = f->output_data.x;
3784 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3785 unsigned long pixel;
3786 unsigned long background = di->relief_background;
3787 Colormap cmap = FRAME_X_COLORMAP (f);
3788 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3789 Display *dpy = FRAME_X_DISPLAY (f);
3790
3791 xgcv.graphics_exposures = False;
3792 xgcv.line_width = 1;
3793
3794 /* Free previously allocated color. The color cell will be reused
3795 when it has been freed as many times as it was allocated, so this
3796 doesn't affect faces using the same colors. */
3797 if (relief->gc
3798 && relief->allocated_p)
3799 {
3800 x_free_colors (f, &relief->pixel, 1);
3801 relief->allocated_p = 0;
3802 }
3803
3804 /* Allocate new color. */
3805 xgcv.foreground = default_pixel;
3806 pixel = background;
3807 if (dpyinfo->n_planes != 1
3808 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3809 {
3810 relief->allocated_p = 1;
3811 xgcv.foreground = relief->pixel = pixel;
3812 }
3813
3814 if (relief->gc == 0)
3815 {
3816 xgcv.stipple = dpyinfo->gray;
3817 mask |= GCStipple;
3818 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3819 }
3820 else
3821 XChangeGC (dpy, relief->gc, mask, &xgcv);
3822 }
3823
3824
3825 /* Set up colors for the relief lines around glyph string S. */
3826
3827 static void
3828 x_setup_relief_colors (s)
3829 struct glyph_string *s;
3830 {
3831 struct x_output *di = s->f->output_data.x;
3832 unsigned long color;
3833
3834 if (s->face->use_box_color_for_shadows_p)
3835 color = s->face->box_color;
3836 else if (s->first_glyph->type == IMAGE_GLYPH
3837 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
3838 color = IMAGE_BACKGROUND (s->img, s->f, 0);
3839 else
3840 {
3841 XGCValues xgcv;
3842
3843 /* Get the background color of the face. */
3844 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3845 color = xgcv.background;
3846 }
3847
3848 if (di->white_relief.gc == 0
3849 || color != di->relief_background)
3850 {
3851 di->relief_background = color;
3852 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3853 WHITE_PIX_DEFAULT (s->f));
3854 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3855 BLACK_PIX_DEFAULT (s->f));
3856 }
3857 }
3858
3859
3860 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3861 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3862 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3863 relief. LEFT_P non-zero means draw a relief on the left side of
3864 the rectangle. RIGHT_P non-zero means draw a relief on the right
3865 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3866 when drawing. */
3867
3868 static void
3869 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3870 raised_p, left_p, right_p, clip_rect)
3871 struct frame *f;
3872 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3873 XRectangle *clip_rect;
3874 {
3875 Display *dpy = FRAME_X_DISPLAY (f);
3876 Window window = FRAME_X_WINDOW (f);
3877 int i;
3878 GC gc;
3879
3880 if (raised_p)
3881 gc = f->output_data.x->white_relief.gc;
3882 else
3883 gc = f->output_data.x->black_relief.gc;
3884 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
3885
3886 /* Top. */
3887 for (i = 0; i < width; ++i)
3888 XDrawLine (dpy, window, gc,
3889 left_x + i * left_p, top_y + i,
3890 right_x + 1 - i * right_p, top_y + i);
3891
3892 /* Left. */
3893 if (left_p)
3894 for (i = 0; i < width; ++i)
3895 XDrawLine (dpy, window, gc,
3896 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
3897
3898 XSetClipMask (dpy, gc, None);
3899 if (raised_p)
3900 gc = f->output_data.x->black_relief.gc;
3901 else
3902 gc = f->output_data.x->white_relief.gc;
3903 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
3904
3905 /* Bottom. */
3906 for (i = 0; i < width; ++i)
3907 XDrawLine (dpy, window, gc,
3908 left_x + i * left_p, bottom_y - i,
3909 right_x + 1 - i * right_p, bottom_y - i);
3910
3911 /* Right. */
3912 if (right_p)
3913 for (i = 0; i < width; ++i)
3914 XDrawLine (dpy, window, gc,
3915 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3916
3917 XSetClipMask (dpy, gc, None);
3918 }
3919
3920
3921 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3922 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3923 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3924 left side of the rectangle. RIGHT_P non-zero means draw a line
3925 on the right side of the rectangle. CLIP_RECT is the clipping
3926 rectangle to use when drawing. */
3927
3928 static void
3929 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3930 left_p, right_p, clip_rect)
3931 struct glyph_string *s;
3932 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3933 XRectangle *clip_rect;
3934 {
3935 XGCValues xgcv;
3936
3937 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3938 XSetForeground (s->display, s->gc, s->face->box_color);
3939 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3940
3941 /* Top. */
3942 XFillRectangle (s->display, s->window, s->gc,
3943 left_x, top_y, right_x - left_x + 1, width);
3944
3945 /* Left. */
3946 if (left_p)
3947 XFillRectangle (s->display, s->window, s->gc,
3948 left_x, top_y, width, bottom_y - top_y + 1);
3949
3950 /* Bottom. */
3951 XFillRectangle (s->display, s->window, s->gc,
3952 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3953
3954 /* Right. */
3955 if (right_p)
3956 XFillRectangle (s->display, s->window, s->gc,
3957 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3958
3959 XSetForeground (s->display, s->gc, xgcv.foreground);
3960 XSetClipMask (s->display, s->gc, None);
3961 }
3962
3963
3964 /* Draw a box around glyph string S. */
3965
3966 static void
3967 x_draw_glyph_string_box (s)
3968 struct glyph_string *s;
3969 {
3970 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3971 int left_p, right_p;
3972 struct glyph *last_glyph;
3973 XRectangle clip_rect;
3974
3975 last_x = window_box_right (s->w, s->area);
3976 if (s->row->full_width_p
3977 && !s->w->pseudo_window_p)
3978 {
3979 last_x += FRAME_X_RIGHT_FRINGE_WIDTH (s->f);
3980 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3981 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3982 }
3983
3984 /* The glyph that may have a right box line. */
3985 last_glyph = (s->cmp || s->img
3986 ? s->first_glyph
3987 : s->first_glyph + s->nchars - 1);
3988
3989 width = abs (s->face->box_line_width);
3990 raised_p = s->face->box == FACE_RAISED_BOX;
3991 left_x = s->x;
3992 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
3993 ? last_x - 1
3994 : min (last_x, s->x + s->background_width) - 1);
3995 top_y = s->y;
3996 bottom_y = top_y + s->height - 1;
3997
3998 left_p = (s->first_glyph->left_box_line_p
3999 || (s->hl == DRAW_MOUSE_FACE
4000 && (s->prev == NULL
4001 || s->prev->hl != s->hl)));
4002 right_p = (last_glyph->right_box_line_p
4003 || (s->hl == DRAW_MOUSE_FACE
4004 && (s->next == NULL
4005 || s->next->hl != s->hl)));
4006
4007 x_get_glyph_string_clip_rect (s, &clip_rect);
4008
4009 if (s->face->box == FACE_SIMPLE_BOX)
4010 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
4011 left_p, right_p, &clip_rect);
4012 else
4013 {
4014 x_setup_relief_colors (s);
4015 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
4016 width, raised_p, left_p, right_p, &clip_rect);
4017 }
4018 }
4019
4020
4021 /* Draw foreground of image glyph string S. */
4022
4023 static void
4024 x_draw_image_foreground (s)
4025 struct glyph_string *s;
4026 {
4027 int x;
4028 int y = s->ybase - image_ascent (s->img, s->face);
4029
4030 /* If first glyph of S has a left box line, start drawing it to the
4031 right of that line. */
4032 if (s->face->box != FACE_NO_BOX
4033 && s->first_glyph->left_box_line_p)
4034 x = s->x + abs (s->face->box_line_width);
4035 else
4036 x = s->x;
4037
4038 /* If there is a margin around the image, adjust x- and y-position
4039 by that margin. */
4040 x += s->img->hmargin;
4041 y += s->img->vmargin;
4042
4043 if (s->img->pixmap)
4044 {
4045 if (s->img->mask)
4046 {
4047 /* We can't set both a clip mask and use XSetClipRectangles
4048 because the latter also sets a clip mask. We also can't
4049 trust on the shape extension to be available
4050 (XShapeCombineRegion). So, compute the rectangle to draw
4051 manually. */
4052 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4053 | GCFunction);
4054 XGCValues xgcv;
4055 XRectangle clip_rect, image_rect, r;
4056
4057 xgcv.clip_mask = s->img->mask;
4058 xgcv.clip_x_origin = x;
4059 xgcv.clip_y_origin = y;
4060 xgcv.function = GXcopy;
4061 XChangeGC (s->display, s->gc, mask, &xgcv);
4062
4063 x_get_glyph_string_clip_rect (s, &clip_rect);
4064 image_rect.x = x;
4065 image_rect.y = y;
4066 image_rect.width = s->img->width;
4067 image_rect.height = s->img->height;
4068 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4069 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4070 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4071 }
4072 else
4073 {
4074 XRectangle clip_rect, image_rect, r;
4075
4076 x_get_glyph_string_clip_rect (s, &clip_rect);
4077 image_rect.x = x;
4078 image_rect.y = y;
4079 image_rect.width = s->img->width;
4080 image_rect.height = s->img->height;
4081 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4082 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4083 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4084
4085 /* When the image has a mask, we can expect that at
4086 least part of a mouse highlight or a block cursor will
4087 be visible. If the image doesn't have a mask, make
4088 a block cursor visible by drawing a rectangle around
4089 the image. I believe it's looking better if we do
4090 nothing here for mouse-face. */
4091 if (s->hl == DRAW_CURSOR)
4092 XDrawRectangle (s->display, s->window, s->gc, x, y,
4093 s->img->width - 1, s->img->height - 1);
4094 }
4095 }
4096 else
4097 /* Draw a rectangle if image could not be loaded. */
4098 XDrawRectangle (s->display, s->window, s->gc, x, y,
4099 s->img->width - 1, s->img->height - 1);
4100 }
4101
4102
4103 /* Draw a relief around the image glyph string S. */
4104
4105 static void
4106 x_draw_image_relief (s)
4107 struct glyph_string *s;
4108 {
4109 int x0, y0, x1, y1, thick, raised_p;
4110 XRectangle r;
4111 int x;
4112 int y = s->ybase - image_ascent (s->img, s->face);
4113
4114 /* If first glyph of S has a left box line, start drawing it to the
4115 right of that line. */
4116 if (s->face->box != FACE_NO_BOX
4117 && s->first_glyph->left_box_line_p)
4118 x = s->x + abs (s->face->box_line_width);
4119 else
4120 x = s->x;
4121
4122 /* If there is a margin around the image, adjust x- and y-position
4123 by that margin. */
4124 x += s->img->hmargin;
4125 y += s->img->vmargin;
4126
4127 if (s->hl == DRAW_IMAGE_SUNKEN
4128 || s->hl == DRAW_IMAGE_RAISED)
4129 {
4130 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : 3;
4131 raised_p = s->hl == DRAW_IMAGE_RAISED;
4132 }
4133 else
4134 {
4135 thick = abs (s->img->relief);
4136 raised_p = s->img->relief > 0;
4137 }
4138
4139 x0 = x - thick;
4140 y0 = y - thick;
4141 x1 = x + s->img->width + thick - 1;
4142 y1 = y + s->img->height + thick - 1;
4143
4144 x_setup_relief_colors (s);
4145 x_get_glyph_string_clip_rect (s, &r);
4146 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
4147 }
4148
4149
4150 /* Draw the foreground of image glyph string S to PIXMAP. */
4151
4152 static void
4153 x_draw_image_foreground_1 (s, pixmap)
4154 struct glyph_string *s;
4155 Pixmap pixmap;
4156 {
4157 int x;
4158 int y = s->ybase - s->y - image_ascent (s->img, s->face);
4159
4160 /* If first glyph of S has a left box line, start drawing it to the
4161 right of that line. */
4162 if (s->face->box != FACE_NO_BOX
4163 && s->first_glyph->left_box_line_p)
4164 x = abs (s->face->box_line_width);
4165 else
4166 x = 0;
4167
4168 /* If there is a margin around the image, adjust x- and y-position
4169 by that margin. */
4170 x += s->img->hmargin;
4171 y += s->img->vmargin;
4172
4173 if (s->img->pixmap)
4174 {
4175 if (s->img->mask)
4176 {
4177 /* We can't set both a clip mask and use XSetClipRectangles
4178 because the latter also sets a clip mask. We also can't
4179 trust on the shape extension to be available
4180 (XShapeCombineRegion). So, compute the rectangle to draw
4181 manually. */
4182 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4183 | GCFunction);
4184 XGCValues xgcv;
4185
4186 xgcv.clip_mask = s->img->mask;
4187 xgcv.clip_x_origin = x;
4188 xgcv.clip_y_origin = y;
4189 xgcv.function = GXcopy;
4190 XChangeGC (s->display, s->gc, mask, &xgcv);
4191
4192 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4193 0, 0, s->img->width, s->img->height, x, y);
4194 XSetClipMask (s->display, s->gc, None);
4195 }
4196 else
4197 {
4198 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4199 0, 0, s->img->width, s->img->height, x, y);
4200
4201 /* When the image has a mask, we can expect that at
4202 least part of a mouse highlight or a block cursor will
4203 be visible. If the image doesn't have a mask, make
4204 a block cursor visible by drawing a rectangle around
4205 the image. I believe it's looking better if we do
4206 nothing here for mouse-face. */
4207 if (s->hl == DRAW_CURSOR)
4208 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4209 s->img->width - 1, s->img->height - 1);
4210 }
4211 }
4212 else
4213 /* Draw a rectangle if image could not be loaded. */
4214 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4215 s->img->width - 1, s->img->height - 1);
4216 }
4217
4218
4219 /* Draw part of the background of glyph string S. X, Y, W, and H
4220 give the rectangle to draw. */
4221
4222 static void
4223 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4224 struct glyph_string *s;
4225 int x, y, w, h;
4226 {
4227 if (s->stippled_p)
4228 {
4229 /* Fill background with a stipple pattern. */
4230 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4231 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4232 XSetFillStyle (s->display, s->gc, FillSolid);
4233 }
4234 else
4235 x_clear_glyph_string_rect (s, x, y, w, h);
4236 }
4237
4238
4239 /* Draw image glyph string S.
4240
4241 s->y
4242 s->x +-------------------------
4243 | s->face->box
4244 |
4245 | +-------------------------
4246 | | s->img->margin
4247 | |
4248 | | +-------------------
4249 | | | the image
4250
4251 */
4252
4253 static void
4254 x_draw_image_glyph_string (s)
4255 struct glyph_string *s;
4256 {
4257 int x, y;
4258 int box_line_hwidth = abs (s->face->box_line_width);
4259 int box_line_vwidth = max (s->face->box_line_width, 0);
4260 int height;
4261 Pixmap pixmap = None;
4262
4263 height = s->height - 2 * box_line_vwidth;
4264
4265 /* Fill background with face under the image. Do it only if row is
4266 taller than image or if image has a clip mask to reduce
4267 flickering. */
4268 s->stippled_p = s->face->stipple != 0;
4269 if (height > s->img->height
4270 || s->img->hmargin
4271 || s->img->vmargin
4272 || s->img->mask
4273 || s->img->pixmap == 0
4274 || s->width != s->background_width)
4275 {
4276 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4277 x = s->x + box_line_hwidth;
4278 else
4279 x = s->x;
4280
4281 y = s->y + box_line_vwidth;
4282
4283 if (s->img->mask)
4284 {
4285 /* Create a pixmap as large as the glyph string. Fill it
4286 with the background color. Copy the image to it, using
4287 its mask. Copy the temporary pixmap to the display. */
4288 Screen *screen = FRAME_X_SCREEN (s->f);
4289 int depth = DefaultDepthOfScreen (screen);
4290
4291 /* Create a pixmap as large as the glyph string. */
4292 pixmap = XCreatePixmap (s->display, s->window,
4293 s->background_width,
4294 s->height, depth);
4295
4296 /* Don't clip in the following because we're working on the
4297 pixmap. */
4298 XSetClipMask (s->display, s->gc, None);
4299
4300 /* Fill the pixmap with the background color/stipple. */
4301 if (s->stippled_p)
4302 {
4303 /* Fill background with a stipple pattern. */
4304 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4305 XFillRectangle (s->display, pixmap, s->gc,
4306 0, 0, s->background_width, s->height);
4307 XSetFillStyle (s->display, s->gc, FillSolid);
4308 }
4309 else
4310 {
4311 XGCValues xgcv;
4312 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4313 &xgcv);
4314 XSetForeground (s->display, s->gc, xgcv.background);
4315 XFillRectangle (s->display, pixmap, s->gc,
4316 0, 0, s->background_width, s->height);
4317 XSetForeground (s->display, s->gc, xgcv.foreground);
4318 }
4319 }
4320 else
4321 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4322
4323 s->background_filled_p = 1;
4324 }
4325
4326 /* Draw the foreground. */
4327 if (pixmap != None)
4328 {
4329 x_draw_image_foreground_1 (s, pixmap);
4330 x_set_glyph_string_clipping (s);
4331 XCopyArea (s->display, pixmap, s->window, s->gc,
4332 0, 0, s->background_width, s->height, s->x, s->y);
4333 XFreePixmap (s->display, pixmap);
4334 }
4335 else
4336 x_draw_image_foreground (s);
4337
4338 /* If we must draw a relief around the image, do it. */
4339 if (s->img->relief
4340 || s->hl == DRAW_IMAGE_RAISED
4341 || s->hl == DRAW_IMAGE_SUNKEN)
4342 x_draw_image_relief (s);
4343 }
4344
4345
4346 /* Draw stretch glyph string S. */
4347
4348 static void
4349 x_draw_stretch_glyph_string (s)
4350 struct glyph_string *s;
4351 {
4352 xassert (s->first_glyph->type == STRETCH_GLYPH);
4353 s->stippled_p = s->face->stipple != 0;
4354
4355 if (s->hl == DRAW_CURSOR
4356 && !x_stretch_cursor_p)
4357 {
4358 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4359 as wide as the stretch glyph. */
4360 int width = min (CANON_X_UNIT (s->f), s->background_width);
4361
4362 /* Draw cursor. */
4363 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4364
4365 /* Clear rest using the GC of the original non-cursor face. */
4366 if (width < s->background_width)
4367 {
4368 int x = s->x + width, y = s->y;
4369 int w = s->background_width - width, h = s->height;
4370 XRectangle r;
4371 GC gc;
4372
4373 if (s->row->mouse_face_p
4374 && cursor_in_mouse_face_p (s->w))
4375 {
4376 x_set_mouse_face_gc (s);
4377 gc = s->gc;
4378 }
4379 else
4380 gc = s->face->gc;
4381
4382 x_get_glyph_string_clip_rect (s, &r);
4383 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4384
4385 if (s->face->stipple)
4386 {
4387 /* Fill background with a stipple pattern. */
4388 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4389 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4390 XSetFillStyle (s->display, gc, FillSolid);
4391 }
4392 else
4393 {
4394 XGCValues xgcv;
4395 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4396 XSetForeground (s->display, gc, xgcv.background);
4397 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4398 XSetForeground (s->display, gc, xgcv.foreground);
4399 }
4400 }
4401 }
4402 else if (!s->background_filled_p)
4403 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4404 s->height);
4405
4406 s->background_filled_p = 1;
4407 }
4408
4409
4410 /* Draw glyph string S. */
4411
4412 static void
4413 x_draw_glyph_string (s)
4414 struct glyph_string *s;
4415 {
4416 int relief_drawn_p = 0;
4417
4418 /* If S draws into the background of its successor, draw the
4419 background of the successor first so that S can draw into it.
4420 This makes S->next use XDrawString instead of XDrawImageString. */
4421 if (s->next && s->right_overhang && !s->for_overlaps_p)
4422 {
4423 xassert (s->next->img == NULL);
4424 x_set_glyph_string_gc (s->next);
4425 x_set_glyph_string_clipping (s->next);
4426 x_draw_glyph_string_background (s->next, 1);
4427 }
4428
4429 /* Set up S->gc, set clipping and draw S. */
4430 x_set_glyph_string_gc (s);
4431
4432 /* Draw relief (if any) in advance for char/composition so that the
4433 glyph string can be drawn over it. */
4434 if (!s->for_overlaps_p
4435 && s->face->box != FACE_NO_BOX
4436 && (s->first_glyph->type == CHAR_GLYPH
4437 || s->first_glyph->type == COMPOSITE_GLYPH))
4438
4439 {
4440 x_set_glyph_string_clipping (s);
4441 x_draw_glyph_string_background (s, 1);
4442 x_draw_glyph_string_box (s);
4443 x_set_glyph_string_clipping (s);
4444 relief_drawn_p = 1;
4445 }
4446 else
4447 x_set_glyph_string_clipping (s);
4448
4449 switch (s->first_glyph->type)
4450 {
4451 case IMAGE_GLYPH:
4452 x_draw_image_glyph_string (s);
4453 break;
4454
4455 case STRETCH_GLYPH:
4456 x_draw_stretch_glyph_string (s);
4457 break;
4458
4459 case CHAR_GLYPH:
4460 if (s->for_overlaps_p)
4461 s->background_filled_p = 1;
4462 else
4463 x_draw_glyph_string_background (s, 0);
4464 x_draw_glyph_string_foreground (s);
4465 break;
4466
4467 case COMPOSITE_GLYPH:
4468 if (s->for_overlaps_p || s->gidx > 0)
4469 s->background_filled_p = 1;
4470 else
4471 x_draw_glyph_string_background (s, 1);
4472 x_draw_composite_glyph_string_foreground (s);
4473 break;
4474
4475 default:
4476 abort ();
4477 }
4478
4479 if (!s->for_overlaps_p)
4480 {
4481 /* Draw underline. */
4482 if (s->face->underline_p)
4483 {
4484 unsigned long tem, h;
4485 int y;
4486
4487 /* Get the underline thickness. Default is 1 pixel. */
4488 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4489 h = 1;
4490
4491 /* Get the underline position. This is the recommended
4492 vertical offset in pixels from the baseline to the top of
4493 the underline. This is a signed value according to the
4494 specs, and its default is
4495
4496 ROUND ((maximum descent) / 2), with
4497 ROUND(x) = floor (x + 0.5) */
4498
4499 if (x_use_underline_position_properties
4500 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4501 y = s->ybase + (long) tem;
4502 else if (s->face->font)
4503 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4504 else
4505 y = s->y + s->height - h;
4506
4507 if (s->face->underline_defaulted_p)
4508 XFillRectangle (s->display, s->window, s->gc,
4509 s->x, y, s->width, h);
4510 else
4511 {
4512 XGCValues xgcv;
4513 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4514 XSetForeground (s->display, s->gc, s->face->underline_color);
4515 XFillRectangle (s->display, s->window, s->gc,
4516 s->x, y, s->width, h);
4517 XSetForeground (s->display, s->gc, xgcv.foreground);
4518 }
4519 }
4520
4521 /* Draw overline. */
4522 if (s->face->overline_p)
4523 {
4524 unsigned long dy = 0, h = 1;
4525
4526 if (s->face->overline_color_defaulted_p)
4527 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4528 s->width, h);
4529 else
4530 {
4531 XGCValues xgcv;
4532 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4533 XSetForeground (s->display, s->gc, s->face->overline_color);
4534 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4535 s->width, h);
4536 XSetForeground (s->display, s->gc, xgcv.foreground);
4537 }
4538 }
4539
4540 /* Draw strike-through. */
4541 if (s->face->strike_through_p)
4542 {
4543 unsigned long h = 1;
4544 unsigned long dy = (s->height - h) / 2;
4545
4546 if (s->face->strike_through_color_defaulted_p)
4547 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4548 s->width, h);
4549 else
4550 {
4551 XGCValues xgcv;
4552 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4553 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4554 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4555 s->width, h);
4556 XSetForeground (s->display, s->gc, xgcv.foreground);
4557 }
4558 }
4559
4560 /* Draw relief if not yet drawn. */
4561 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
4562 x_draw_glyph_string_box (s);
4563 }
4564
4565 /* Reset clipping. */
4566 XSetClipMask (s->display, s->gc, None);
4567 }
4568
4569
4570 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4571 struct face **, int));
4572
4573
4574 /* Fill glyph string S with composition components specified by S->cmp.
4575
4576 FACES is an array of faces for all components of this composition.
4577 S->gidx is the index of the first component for S.
4578 OVERLAPS_P non-zero means S should draw the foreground only, and
4579 use its physical height for clipping.
4580
4581 Value is the index of a component not in S. */
4582
4583 static int
4584 x_fill_composite_glyph_string (s, faces, overlaps_p)
4585 struct glyph_string *s;
4586 struct face **faces;
4587 int overlaps_p;
4588 {
4589 int i;
4590
4591 xassert (s);
4592
4593 s->for_overlaps_p = overlaps_p;
4594
4595 s->face = faces[s->gidx];
4596 s->font = s->face->font;
4597 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4598
4599 /* For all glyphs of this composition, starting at the offset
4600 S->gidx, until we reach the end of the definition or encounter a
4601 glyph that requires the different face, add it to S. */
4602 ++s->nchars;
4603 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4604 ++s->nchars;
4605
4606 /* All glyph strings for the same composition has the same width,
4607 i.e. the width set for the first component of the composition. */
4608
4609 s->width = s->first_glyph->pixel_width;
4610
4611 /* If the specified font could not be loaded, use the frame's
4612 default font, but record the fact that we couldn't load it in
4613 the glyph string so that we can draw rectangles for the
4614 characters of the glyph string. */
4615 if (s->font == NULL)
4616 {
4617 s->font_not_found_p = 1;
4618 s->font = FRAME_FONT (s->f);
4619 }
4620
4621 /* Adjust base line for subscript/superscript text. */
4622 s->ybase += s->first_glyph->voffset;
4623
4624 xassert (s->face && s->face->gc);
4625
4626 /* This glyph string must always be drawn with 16-bit functions. */
4627 s->two_byte_p = 1;
4628
4629 return s->gidx + s->nchars;
4630 }
4631
4632
4633 /* Fill glyph string S from a sequence of character glyphs.
4634
4635 FACE_ID is the face id of the string. START is the index of the
4636 first glyph to consider, END is the index of the last + 1.
4637 OVERLAPS_P non-zero means S should draw the foreground only, and
4638 use its physical height for clipping.
4639
4640 Value is the index of the first glyph not in S. */
4641
4642 static int
4643 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4644 struct glyph_string *s;
4645 int face_id;
4646 int start, end, overlaps_p;
4647 {
4648 struct glyph *glyph, *last;
4649 int voffset;
4650 int glyph_not_available_p;
4651
4652 xassert (s->f == XFRAME (s->w->frame));
4653 xassert (s->nchars == 0);
4654 xassert (start >= 0 && end > start);
4655
4656 s->for_overlaps_p = overlaps_p,
4657 glyph = s->row->glyphs[s->area] + start;
4658 last = s->row->glyphs[s->area] + end;
4659 voffset = glyph->voffset;
4660
4661 glyph_not_available_p = glyph->glyph_not_available_p;
4662
4663 while (glyph < last
4664 && glyph->type == CHAR_GLYPH
4665 && glyph->voffset == voffset
4666 /* Same face id implies same font, nowadays. */
4667 && glyph->face_id == face_id
4668 && glyph->glyph_not_available_p == glyph_not_available_p)
4669 {
4670 int two_byte_p;
4671
4672 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4673 s->char2b + s->nchars,
4674 &two_byte_p);
4675 s->two_byte_p = two_byte_p;
4676 ++s->nchars;
4677 xassert (s->nchars <= end - start);
4678 s->width += glyph->pixel_width;
4679 ++glyph;
4680 }
4681
4682 s->font = s->face->font;
4683 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4684
4685 /* If the specified font could not be loaded, use the frame's font,
4686 but record the fact that we couldn't load it in
4687 S->font_not_found_p so that we can draw rectangles for the
4688 characters of the glyph string. */
4689 if (s->font == NULL || glyph_not_available_p)
4690 {
4691 s->font_not_found_p = 1;
4692 s->font = FRAME_FONT (s->f);
4693 }
4694
4695 /* Adjust base line for subscript/superscript text. */
4696 s->ybase += voffset;
4697
4698 xassert (s->face && s->face->gc);
4699 return glyph - s->row->glyphs[s->area];
4700 }
4701
4702
4703 /* Fill glyph string S from image glyph S->first_glyph. */
4704
4705 static void
4706 x_fill_image_glyph_string (s)
4707 struct glyph_string *s;
4708 {
4709 xassert (s->first_glyph->type == IMAGE_GLYPH);
4710 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4711 xassert (s->img);
4712 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4713 s->font = s->face->font;
4714 s->width = s->first_glyph->pixel_width;
4715
4716 /* Adjust base line for subscript/superscript text. */
4717 s->ybase += s->first_glyph->voffset;
4718 }
4719
4720
4721 /* Fill glyph string S from a sequence of stretch glyphs.
4722
4723 ROW is the glyph row in which the glyphs are found, AREA is the
4724 area within the row. START is the index of the first glyph to
4725 consider, END is the index of the last + 1.
4726
4727 Value is the index of the first glyph not in S. */
4728
4729 static int
4730 x_fill_stretch_glyph_string (s, row, area, start, end)
4731 struct glyph_string *s;
4732 struct glyph_row *row;
4733 enum glyph_row_area area;
4734 int start, end;
4735 {
4736 struct glyph *glyph, *last;
4737 int voffset, face_id;
4738
4739 xassert (s->first_glyph->type == STRETCH_GLYPH);
4740
4741 glyph = s->row->glyphs[s->area] + start;
4742 last = s->row->glyphs[s->area] + end;
4743 face_id = glyph->face_id;
4744 s->face = FACE_FROM_ID (s->f, face_id);
4745 s->font = s->face->font;
4746 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4747 s->width = glyph->pixel_width;
4748 voffset = glyph->voffset;
4749
4750 for (++glyph;
4751 (glyph < last
4752 && glyph->type == STRETCH_GLYPH
4753 && glyph->voffset == voffset
4754 && glyph->face_id == face_id);
4755 ++glyph)
4756 s->width += glyph->pixel_width;
4757
4758 /* Adjust base line for subscript/superscript text. */
4759 s->ybase += voffset;
4760
4761 /* The case that face->gc == 0 is handled when drawing the glyph
4762 string by calling PREPARE_FACE_FOR_DISPLAY. */
4763 xassert (s->face);
4764 return glyph - s->row->glyphs[s->area];
4765 }
4766
4767
4768 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4769 of XChar2b structures for S; it can't be allocated in
4770 x_init_glyph_string because it must be allocated via `alloca'. W
4771 is the window on which S is drawn. ROW and AREA are the glyph row
4772 and area within the row from which S is constructed. START is the
4773 index of the first glyph structure covered by S. HL is a
4774 face-override for drawing S. */
4775
4776 static void
4777 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4778 struct glyph_string *s;
4779 XChar2b *char2b;
4780 struct window *w;
4781 struct glyph_row *row;
4782 enum glyph_row_area area;
4783 int start;
4784 enum draw_glyphs_face hl;
4785 {
4786 bzero (s, sizeof *s);
4787 s->w = w;
4788 s->f = XFRAME (w->frame);
4789 s->display = FRAME_X_DISPLAY (s->f);
4790 s->window = FRAME_X_WINDOW (s->f);
4791 s->char2b = char2b;
4792 s->hl = hl;
4793 s->row = row;
4794 s->area = area;
4795 s->first_glyph = row->glyphs[area] + start;
4796 s->height = row->height;
4797 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4798
4799 /* Display the internal border below the tool-bar window. */
4800 if (s->w == XWINDOW (s->f->tool_bar_window))
4801 s->y -= s->f->output_data.x->internal_border_width;
4802
4803 s->ybase = s->y + row->ascent;
4804 }
4805
4806
4807 /* Set background width of glyph string S. START is the index of the
4808 first glyph following S. LAST_X is the right-most x-position + 1
4809 in the drawing area. */
4810
4811 static INLINE void
4812 x_set_glyph_string_background_width (s, start, last_x)
4813 struct glyph_string *s;
4814 int start;
4815 int last_x;
4816 {
4817 /* If the face of this glyph string has to be drawn to the end of
4818 the drawing area, set S->extends_to_end_of_line_p. */
4819 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4820
4821 if (start == s->row->used[s->area]
4822 && s->area == TEXT_AREA
4823 && ((s->hl == DRAW_NORMAL_TEXT
4824 && (s->row->fill_line_p
4825 || s->face->background != default_face->background
4826 || s->face->stipple != default_face->stipple
4827 || s->row->mouse_face_p))
4828 || s->hl == DRAW_MOUSE_FACE
4829 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
4830 && s->row->fill_line_p)))
4831 s->extends_to_end_of_line_p = 1;
4832
4833 /* If S extends its face to the end of the line, set its
4834 background_width to the distance to the right edge of the drawing
4835 area. */
4836 if (s->extends_to_end_of_line_p)
4837 s->background_width = last_x - s->x + 1;
4838 else
4839 s->background_width = s->width;
4840 }
4841
4842
4843 /* Add a glyph string for a stretch glyph to the list of strings
4844 between HEAD and TAIL. START is the index of the stretch glyph in
4845 row area AREA of glyph row ROW. END is the index of the last glyph
4846 in that glyph row area. X is the current output position assigned
4847 to the new glyph string constructed. HL overrides that face of the
4848 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4849 is the right-most x-position of the drawing area. */
4850
4851 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4852 and below -- keep them on one line. */
4853 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4854 do \
4855 { \
4856 s = (struct glyph_string *) alloca (sizeof *s); \
4857 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4858 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4859 x_append_glyph_string (&HEAD, &TAIL, s); \
4860 s->x = (X); \
4861 } \
4862 while (0)
4863
4864
4865 /* Add a glyph string for an image glyph to the list of strings
4866 between HEAD and TAIL. START is the index of the image glyph in
4867 row area AREA of glyph row ROW. END is the index of the last glyph
4868 in that glyph row area. X is the current output position assigned
4869 to the new glyph string constructed. HL overrides that face of the
4870 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4871 is the right-most x-position of the drawing area. */
4872
4873 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4874 do \
4875 { \
4876 s = (struct glyph_string *) alloca (sizeof *s); \
4877 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4878 x_fill_image_glyph_string (s); \
4879 x_append_glyph_string (&HEAD, &TAIL, s); \
4880 ++START; \
4881 s->x = (X); \
4882 } \
4883 while (0)
4884
4885
4886 /* Add a glyph string for a sequence of character glyphs to the list
4887 of strings between HEAD and TAIL. START is the index of the first
4888 glyph in row area AREA of glyph row ROW that is part of the new
4889 glyph string. END is the index of the last glyph in that glyph row
4890 area. X is the current output position assigned to the new glyph
4891 string constructed. HL overrides that face of the glyph; e.g. it
4892 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4893 right-most x-position of the drawing area. */
4894
4895 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4896 do \
4897 { \
4898 int c, face_id; \
4899 XChar2b *char2b; \
4900 \
4901 c = (ROW)->glyphs[AREA][START].u.ch; \
4902 face_id = (ROW)->glyphs[AREA][START].face_id; \
4903 \
4904 s = (struct glyph_string *) alloca (sizeof *s); \
4905 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4906 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4907 x_append_glyph_string (&HEAD, &TAIL, s); \
4908 s->x = (X); \
4909 START = x_fill_glyph_string (s, face_id, START, END, \
4910 OVERLAPS_P); \
4911 } \
4912 while (0)
4913
4914
4915 /* Add a glyph string for a composite sequence to the list of strings
4916 between HEAD and TAIL. START is the index of the first glyph in
4917 row area AREA of glyph row ROW that is part of the new glyph
4918 string. END is the index of the last glyph in that glyph row area.
4919 X is the current output position assigned to the new glyph string
4920 constructed. HL overrides that face of the glyph; e.g. it is
4921 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4922 x-position of the drawing area. */
4923
4924 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4925 do { \
4926 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4927 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4928 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4929 struct composition *cmp = composition_table[cmp_id]; \
4930 int glyph_len = cmp->glyph_len; \
4931 XChar2b *char2b; \
4932 struct face **faces; \
4933 struct glyph_string *first_s = NULL; \
4934 int n; \
4935 \
4936 base_face = base_face->ascii_face; \
4937 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4938 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4939 /* At first, fill in `char2b' and `faces'. */ \
4940 for (n = 0; n < glyph_len; n++) \
4941 { \
4942 int c = COMPOSITION_GLYPH (cmp, n); \
4943 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4944 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4945 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4946 this_face_id, char2b + n, 1); \
4947 } \
4948 \
4949 /* Make glyph_strings for each glyph sequence that is drawable by \
4950 the same face, and append them to HEAD/TAIL. */ \
4951 for (n = 0; n < cmp->glyph_len;) \
4952 { \
4953 s = (struct glyph_string *) alloca (sizeof *s); \
4954 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4955 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4956 s->cmp = cmp; \
4957 s->gidx = n; \
4958 s->x = (X); \
4959 \
4960 if (n == 0) \
4961 first_s = s; \
4962 \
4963 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4964 } \
4965 \
4966 ++START; \
4967 s = first_s; \
4968 } while (0)
4969
4970
4971 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4972 of AREA of glyph row ROW on window W between indices START and END.
4973 HL overrides the face for drawing glyph strings, e.g. it is
4974 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4975 x-positions of the drawing area.
4976
4977 This is an ugly monster macro construct because we must use alloca
4978 to allocate glyph strings (because x_draw_glyphs can be called
4979 asynchronously). */
4980
4981 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4982 do \
4983 { \
4984 HEAD = TAIL = NULL; \
4985 while (START < END) \
4986 { \
4987 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4988 switch (first_glyph->type) \
4989 { \
4990 case CHAR_GLYPH: \
4991 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
4992 TAIL, HL, X, LAST_X, \
4993 OVERLAPS_P); \
4994 break; \
4995 \
4996 case COMPOSITE_GLYPH: \
4997 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4998 HEAD, TAIL, HL, X, LAST_X,\
4999 OVERLAPS_P); \
5000 break; \
5001 \
5002 case STRETCH_GLYPH: \
5003 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
5004 HEAD, TAIL, HL, X, LAST_X); \
5005 break; \
5006 \
5007 case IMAGE_GLYPH: \
5008 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
5009 TAIL, HL, X, LAST_X); \
5010 break; \
5011 \
5012 default: \
5013 abort (); \
5014 } \
5015 \
5016 x_set_glyph_string_background_width (s, START, LAST_X); \
5017 (X) += s->width; \
5018 } \
5019 } \
5020 while (0)
5021
5022
5023 /* Draw glyphs between START and END in AREA of ROW on window W,
5024 starting at x-position X. X is relative to AREA in W. HL is a
5025 face-override with the following meaning:
5026
5027 DRAW_NORMAL_TEXT draw normally
5028 DRAW_CURSOR draw in cursor face
5029 DRAW_MOUSE_FACE draw in mouse face.
5030 DRAW_INVERSE_VIDEO draw in mode line face
5031 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
5032 DRAW_IMAGE_RAISED draw an image with a raised relief around it
5033
5034 If OVERLAPS_P is non-zero, draw only the foreground of characters
5035 and clip to the physical height of ROW.
5036
5037 Value is the x-position reached, relative to AREA of W. */
5038
5039 static int
5040 x_draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
5041 struct window *w;
5042 int x;
5043 struct glyph_row *row;
5044 enum glyph_row_area area;
5045 int start, end;
5046 enum draw_glyphs_face hl;
5047 int overlaps_p;
5048 {
5049 struct glyph_string *head, *tail;
5050 struct glyph_string *s;
5051 int last_x, area_width;
5052 int x_reached;
5053 int i, j;
5054
5055 /* Let's rather be paranoid than getting a SEGV. */
5056 end = min (end, row->used[area]);
5057 start = max (0, start);
5058 start = min (end, start);
5059
5060 /* Translate X to frame coordinates. Set last_x to the right
5061 end of the drawing area. */
5062 if (row->full_width_p)
5063 {
5064 /* X is relative to the left edge of W, without scroll bars
5065 or fringes. */
5066 struct frame *f = XFRAME (w->frame);
5067 /* int width = FRAME_FRINGE_WIDTH (f); */
5068 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
5069
5070 x += window_left_x;
5071 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
5072 last_x = window_left_x + area_width;
5073
5074 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5075 {
5076 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5077 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
5078 last_x += width;
5079 else
5080 x -= width;
5081 }
5082
5083 x += FRAME_INTERNAL_BORDER_WIDTH (f);
5084 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
5085 }
5086 else
5087 {
5088 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
5089 area_width = window_box_width (w, area);
5090 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
5091 }
5092
5093 /* Build a doubly-linked list of glyph_string structures between
5094 head and tail from what we have to draw. Note that the macro
5095 BUILD_GLYPH_STRINGS will modify its start parameter. That's
5096 the reason we use a separate variable `i'. */
5097 i = start;
5098 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
5099 overlaps_p);
5100 if (tail)
5101 x_reached = tail->x + tail->background_width;
5102 else
5103 x_reached = x;
5104
5105 /* If there are any glyphs with lbearing < 0 or rbearing > width in
5106 the row, redraw some glyphs in front or following the glyph
5107 strings built above. */
5108 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
5109 {
5110 int dummy_x = 0;
5111 struct glyph_string *h, *t;
5112
5113 /* Compute overhangs for all glyph strings. */
5114 for (s = head; s; s = s->next)
5115 x_compute_glyph_string_overhangs (s);
5116
5117 /* Prepend glyph strings for glyphs in front of the first glyph
5118 string that are overwritten because of the first glyph
5119 string's left overhang. The background of all strings
5120 prepended must be drawn because the first glyph string
5121 draws over it. */
5122 i = x_left_overwritten (head);
5123 if (i >= 0)
5124 {
5125 j = i;
5126 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
5127 DRAW_NORMAL_TEXT, dummy_x, last_x,
5128 overlaps_p);
5129 start = i;
5130 x_compute_overhangs_and_x (t, head->x, 1);
5131 x_prepend_glyph_string_lists (&head, &tail, h, t);
5132 }
5133
5134 /* Prepend glyph strings for glyphs in front of the first glyph
5135 string that overwrite that glyph string because of their
5136 right overhang. For these strings, only the foreground must
5137 be drawn, because it draws over the glyph string at `head'.
5138 The background must not be drawn because this would overwrite
5139 right overhangs of preceding glyphs for which no glyph
5140 strings exist. */
5141 i = x_left_overwriting (head);
5142 if (i >= 0)
5143 {
5144 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
5145 DRAW_NORMAL_TEXT, dummy_x, last_x,
5146 overlaps_p);
5147 for (s = h; s; s = s->next)
5148 s->background_filled_p = 1;
5149 x_compute_overhangs_and_x (t, head->x, 1);
5150 x_prepend_glyph_string_lists (&head, &tail, h, t);
5151 }
5152
5153 /* Append glyphs strings for glyphs following the last glyph
5154 string tail that are overwritten by tail. The background of
5155 these strings has to be drawn because tail's foreground draws
5156 over it. */
5157 i = x_right_overwritten (tail);
5158 if (i >= 0)
5159 {
5160 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5161 DRAW_NORMAL_TEXT, x, last_x,
5162 overlaps_p);
5163 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5164 x_append_glyph_string_lists (&head, &tail, h, t);
5165 }
5166
5167 /* Append glyph strings for glyphs following the last glyph
5168 string tail that overwrite tail. The foreground of such
5169 glyphs has to be drawn because it writes into the background
5170 of tail. The background must not be drawn because it could
5171 paint over the foreground of following glyphs. */
5172 i = x_right_overwriting (tail);
5173 if (i >= 0)
5174 {
5175 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5176 DRAW_NORMAL_TEXT, x, last_x,
5177 overlaps_p);
5178 for (s = h; s; s = s->next)
5179 s->background_filled_p = 1;
5180 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5181 x_append_glyph_string_lists (&head, &tail, h, t);
5182 }
5183 }
5184
5185 /* Draw all strings. */
5186 for (s = head; s; s = s->next)
5187 x_draw_glyph_string (s);
5188
5189 if (area == TEXT_AREA && !row->full_width_p)
5190 {
5191 int x0 = head ? head->x : x;
5192 int x1 = tail ? tail->x + tail->background_width : x;
5193
5194 x0 = FRAME_TO_WINDOW_PIXEL_X (w, x0);
5195 x1 = FRAME_TO_WINDOW_PIXEL_X (w, x1);
5196
5197 if (!row->full_width_p && XFASTINT (w->left_margin_width) != 0)
5198 {
5199 int left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
5200 x0 -= left_area_width;
5201 x1 -= left_area_width;
5202 }
5203
5204 notice_overwritten_cursor (w, x0, x1);
5205 }
5206
5207 /* Value is the x-position up to which drawn, relative to AREA of W.
5208 This doesn't include parts drawn because of overhangs. */
5209 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5210 if (!row->full_width_p)
5211 {
5212 if (area > LEFT_MARGIN_AREA && XFASTINT (w->left_margin_width) != 0)
5213 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5214 if (area > TEXT_AREA)
5215 x_reached -= window_box_width (w, TEXT_AREA);
5216 }
5217
5218 return x_reached;
5219 }
5220
5221
5222 /* Fix the display of area AREA of overlapping row ROW in window W. */
5223
5224 static void
5225 x_fix_overlapping_area (w, row, area)
5226 struct window *w;
5227 struct glyph_row *row;
5228 enum glyph_row_area area;
5229 {
5230 int i, x;
5231
5232 BLOCK_INPUT;
5233
5234 if (area == LEFT_MARGIN_AREA)
5235 x = 0;
5236 else if (area == TEXT_AREA)
5237 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5238 else
5239 x = (window_box_width (w, LEFT_MARGIN_AREA)
5240 + window_box_width (w, TEXT_AREA));
5241
5242 for (i = 0; i < row->used[area];)
5243 {
5244 if (row->glyphs[area][i].overlaps_vertically_p)
5245 {
5246 int start = i, start_x = x;
5247
5248 do
5249 {
5250 x += row->glyphs[area][i].pixel_width;
5251 ++i;
5252 }
5253 while (i < row->used[area]
5254 && row->glyphs[area][i].overlaps_vertically_p);
5255
5256 x_draw_glyphs (w, start_x, row, area, start, i,
5257 DRAW_NORMAL_TEXT, 1);
5258 }
5259 else
5260 {
5261 x += row->glyphs[area][i].pixel_width;
5262 ++i;
5263 }
5264 }
5265
5266 UNBLOCK_INPUT;
5267 }
5268
5269
5270 /* Output LEN glyphs starting at START at the nominal cursor position.
5271 Advance the nominal cursor over the text. The global variable
5272 updated_window contains the window being updated, updated_row is
5273 the glyph row being updated, and updated_area is the area of that
5274 row being updated. */
5275
5276 static void
5277 x_write_glyphs (start, len)
5278 struct glyph *start;
5279 int len;
5280 {
5281 int x, hpos;
5282
5283 xassert (updated_window && updated_row);
5284 BLOCK_INPUT;
5285
5286 /* Write glyphs. */
5287
5288 hpos = start - updated_row->glyphs[updated_area];
5289 x = x_draw_glyphs (updated_window, output_cursor.x,
5290 updated_row, updated_area,
5291 hpos, hpos + len,
5292 DRAW_NORMAL_TEXT, 0);
5293
5294 UNBLOCK_INPUT;
5295
5296 /* Advance the output cursor. */
5297 output_cursor.hpos += len;
5298 output_cursor.x = x;
5299 }
5300
5301
5302 /* Insert LEN glyphs from START at the nominal cursor position. */
5303
5304 static void
5305 x_insert_glyphs (start, len)
5306 struct glyph *start;
5307 register int len;
5308 {
5309 struct frame *f;
5310 struct window *w;
5311 int line_height, shift_by_width, shifted_region_width;
5312 struct glyph_row *row;
5313 struct glyph *glyph;
5314 int frame_x, frame_y, hpos;
5315
5316 xassert (updated_window && updated_row);
5317 BLOCK_INPUT;
5318 w = updated_window;
5319 f = XFRAME (WINDOW_FRAME (w));
5320
5321 /* Get the height of the line we are in. */
5322 row = updated_row;
5323 line_height = row->height;
5324
5325 /* Get the width of the glyphs to insert. */
5326 shift_by_width = 0;
5327 for (glyph = start; glyph < start + len; ++glyph)
5328 shift_by_width += glyph->pixel_width;
5329
5330 /* Get the width of the region to shift right. */
5331 shifted_region_width = (window_box_width (w, updated_area)
5332 - output_cursor.x
5333 - shift_by_width);
5334
5335 /* Shift right. */
5336 frame_x = window_box_left (w, updated_area) + output_cursor.x;
5337 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5338 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5339 f->output_data.x->normal_gc,
5340 frame_x, frame_y,
5341 shifted_region_width, line_height,
5342 frame_x + shift_by_width, frame_y);
5343
5344 /* Write the glyphs. */
5345 hpos = start - row->glyphs[updated_area];
5346 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5347 DRAW_NORMAL_TEXT, 0);
5348
5349 /* Advance the output cursor. */
5350 output_cursor.hpos += len;
5351 output_cursor.x += shift_by_width;
5352 UNBLOCK_INPUT;
5353 }
5354
5355
5356 /* Delete N glyphs at the nominal cursor position. Not implemented
5357 for X frames. */
5358
5359 static void
5360 x_delete_glyphs (n)
5361 register int n;
5362 {
5363 abort ();
5364 }
5365
5366
5367 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5368 If they are <= 0, this is probably an error. */
5369
5370 void
5371 x_clear_area (dpy, window, x, y, width, height, exposures)
5372 Display *dpy;
5373 Window window;
5374 int x, y;
5375 int width, height;
5376 int exposures;
5377 {
5378 xassert (width > 0 && height > 0);
5379 XClearArea (dpy, window, x, y, width, height, exposures);
5380 }
5381
5382
5383 /* Erase the current text line from the nominal cursor position
5384 (inclusive) to pixel column TO_X (exclusive). The idea is that
5385 everything from TO_X onward is already erased.
5386
5387 TO_X is a pixel position relative to updated_area of
5388 updated_window. TO_X == -1 means clear to the end of this area. */
5389
5390 static void
5391 x_clear_end_of_line (to_x)
5392 int to_x;
5393 {
5394 struct frame *f;
5395 struct window *w = updated_window;
5396 int max_x, min_y, max_y;
5397 int from_x, from_y, to_y;
5398
5399 xassert (updated_window && updated_row);
5400 f = XFRAME (w->frame);
5401
5402 if (updated_row->full_width_p)
5403 {
5404 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5405 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5406 && !w->pseudo_window_p)
5407 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5408 }
5409 else
5410 max_x = window_box_width (w, updated_area);
5411 max_y = window_text_bottom_y (w);
5412
5413 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5414 of window. For TO_X > 0, truncate to end of drawing area. */
5415 if (to_x == 0)
5416 return;
5417 else if (to_x < 0)
5418 to_x = max_x;
5419 else
5420 to_x = min (to_x, max_x);
5421
5422 to_y = min (max_y, output_cursor.y + updated_row->height);
5423
5424 /* Notice if the cursor will be cleared by this operation. */
5425 if (!updated_row->full_width_p)
5426 notice_overwritten_cursor (w, output_cursor.x, -1);
5427
5428 from_x = output_cursor.x;
5429
5430 /* Translate to frame coordinates. */
5431 if (updated_row->full_width_p)
5432 {
5433 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5434 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5435 }
5436 else
5437 {
5438 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5439 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5440 }
5441
5442 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5443 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5444 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5445
5446 /* Prevent inadvertently clearing to end of the X window. */
5447 if (to_x > from_x && to_y > from_y)
5448 {
5449 BLOCK_INPUT;
5450 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5451 from_x, from_y, to_x - from_x, to_y - from_y,
5452 False);
5453 UNBLOCK_INPUT;
5454 }
5455 }
5456
5457
5458 /* Clear entire frame. If updating_frame is non-null, clear that
5459 frame. Otherwise clear the selected frame. */
5460
5461 static void
5462 x_clear_frame ()
5463 {
5464 struct frame *f;
5465
5466 if (updating_frame)
5467 f = updating_frame;
5468 else
5469 f = SELECTED_FRAME ();
5470
5471 /* Clearing the frame will erase any cursor, so mark them all as no
5472 longer visible. */
5473 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5474 output_cursor.hpos = output_cursor.vpos = 0;
5475 output_cursor.x = -1;
5476
5477 /* We don't set the output cursor here because there will always
5478 follow an explicit cursor_to. */
5479 BLOCK_INPUT;
5480 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5481
5482 /* We have to clear the scroll bars, too. If we have changed
5483 colors or something like that, then they should be notified. */
5484 x_scroll_bar_clear (f);
5485
5486 XFlush (FRAME_X_DISPLAY (f));
5487 UNBLOCK_INPUT;
5488 }
5489
5490
5491 \f
5492 /* Invert the middle quarter of the frame for .15 sec. */
5493
5494 /* We use the select system call to do the waiting, so we have to make
5495 sure it's available. If it isn't, we just won't do visual bells. */
5496
5497 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5498
5499
5500 /* Subtract the `struct timeval' values X and Y, storing the result in
5501 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5502
5503 static int
5504 timeval_subtract (result, x, y)
5505 struct timeval *result, x, y;
5506 {
5507 /* Perform the carry for the later subtraction by updating y. This
5508 is safer because on some systems the tv_sec member is unsigned. */
5509 if (x.tv_usec < y.tv_usec)
5510 {
5511 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5512 y.tv_usec -= 1000000 * nsec;
5513 y.tv_sec += nsec;
5514 }
5515
5516 if (x.tv_usec - y.tv_usec > 1000000)
5517 {
5518 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5519 y.tv_usec += 1000000 * nsec;
5520 y.tv_sec -= nsec;
5521 }
5522
5523 /* Compute the time remaining to wait. tv_usec is certainly
5524 positive. */
5525 result->tv_sec = x.tv_sec - y.tv_sec;
5526 result->tv_usec = x.tv_usec - y.tv_usec;
5527
5528 /* Return indication of whether the result should be considered
5529 negative. */
5530 return x.tv_sec < y.tv_sec;
5531 }
5532
5533 void
5534 XTflash (f)
5535 struct frame *f;
5536 {
5537 BLOCK_INPUT;
5538
5539 {
5540 GC gc;
5541
5542 /* Create a GC that will use the GXxor function to flip foreground
5543 pixels into background pixels. */
5544 {
5545 XGCValues values;
5546
5547 values.function = GXxor;
5548 values.foreground = (f->output_data.x->foreground_pixel
5549 ^ f->output_data.x->background_pixel);
5550
5551 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5552 GCFunction | GCForeground, &values);
5553 }
5554
5555 {
5556 /* Get the height not including a menu bar widget. */
5557 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5558 /* Height of each line to flash. */
5559 int flash_height = FRAME_LINE_HEIGHT (f);
5560 /* These will be the left and right margins of the rectangles. */
5561 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5562 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5563
5564 int width;
5565
5566 /* Don't flash the area between a scroll bar and the frame
5567 edge it is next to. */
5568 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5569 {
5570 case vertical_scroll_bar_left:
5571 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5572 break;
5573
5574 case vertical_scroll_bar_right:
5575 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5576 break;
5577
5578 default:
5579 break;
5580 }
5581
5582 width = flash_right - flash_left;
5583
5584 /* If window is tall, flash top and bottom line. */
5585 if (height > 3 * FRAME_LINE_HEIGHT (f))
5586 {
5587 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5588 flash_left,
5589 (FRAME_INTERNAL_BORDER_WIDTH (f)
5590 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5591 width, flash_height);
5592 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5593 flash_left,
5594 (height - flash_height
5595 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5596 width, flash_height);
5597 }
5598 else
5599 /* If it is short, flash it all. */
5600 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5601 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5602 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5603
5604 x_flush (f);
5605
5606 {
5607 struct timeval wakeup;
5608
5609 EMACS_GET_TIME (wakeup);
5610
5611 /* Compute time to wait until, propagating carry from usecs. */
5612 wakeup.tv_usec += 150000;
5613 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5614 wakeup.tv_usec %= 1000000;
5615
5616 /* Keep waiting until past the time wakeup or any input gets
5617 available. */
5618 while (! detect_input_pending ())
5619 {
5620 struct timeval current;
5621 struct timeval timeout;
5622
5623 EMACS_GET_TIME (current);
5624
5625 /* Break if result would be negative. */
5626 if (timeval_subtract (&current, wakeup, current))
5627 break;
5628
5629 /* How long `select' should wait. */
5630 timeout.tv_sec = 0;
5631 timeout.tv_usec = 10000;
5632
5633 /* Try to wait that long--but we might wake up sooner. */
5634 select (0, NULL, NULL, NULL, &timeout);
5635 }
5636 }
5637
5638 /* If window is tall, flash top and bottom line. */
5639 if (height > 3 * FRAME_LINE_HEIGHT (f))
5640 {
5641 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5642 flash_left,
5643 (FRAME_INTERNAL_BORDER_WIDTH (f)
5644 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5645 width, flash_height);
5646 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5647 flash_left,
5648 (height - flash_height
5649 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5650 width, flash_height);
5651 }
5652 else
5653 /* If it is short, flash it all. */
5654 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5655 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5656 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5657
5658 XFreeGC (FRAME_X_DISPLAY (f), gc);
5659 x_flush (f);
5660 }
5661 }
5662
5663 UNBLOCK_INPUT;
5664 }
5665
5666 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5667
5668
5669 /* Make audible bell. */
5670
5671 void
5672 XTring_bell ()
5673 {
5674 struct frame *f = SELECTED_FRAME ();
5675
5676 if (FRAME_X_DISPLAY (f))
5677 {
5678 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5679 if (visible_bell)
5680 XTflash (f);
5681 else
5682 #endif
5683 {
5684 BLOCK_INPUT;
5685 XBell (FRAME_X_DISPLAY (f), 0);
5686 XFlush (FRAME_X_DISPLAY (f));
5687 UNBLOCK_INPUT;
5688 }
5689 }
5690 }
5691
5692 \f
5693 /* Specify how many text lines, from the top of the window,
5694 should be affected by insert-lines and delete-lines operations.
5695 This, and those operations, are used only within an update
5696 that is bounded by calls to x_update_begin and x_update_end. */
5697
5698 static void
5699 XTset_terminal_window (n)
5700 register int n;
5701 {
5702 /* This function intentionally left blank. */
5703 }
5704
5705
5706 \f
5707 /***********************************************************************
5708 Line Dance
5709 ***********************************************************************/
5710
5711 /* Perform an insert-lines or delete-lines operation, inserting N
5712 lines or deleting -N lines at vertical position VPOS. */
5713
5714 static void
5715 x_ins_del_lines (vpos, n)
5716 int vpos, n;
5717 {
5718 abort ();
5719 }
5720
5721
5722 /* Scroll part of the display as described by RUN. */
5723
5724 static void
5725 x_scroll_run (w, run)
5726 struct window *w;
5727 struct run *run;
5728 {
5729 struct frame *f = XFRAME (w->frame);
5730 int x, y, width, height, from_y, to_y, bottom_y;
5731
5732 /* Get frame-relative bounding box of the text display area of W,
5733 without mode lines. Include in this box the left and right
5734 fringe of W. */
5735 window_box (w, -1, &x, &y, &width, &height);
5736 width += FRAME_X_FRINGE_WIDTH (f);
5737 x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
5738
5739 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5740 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5741 bottom_y = y + height;
5742
5743 if (to_y < from_y)
5744 {
5745 /* Scrolling up. Make sure we don't copy part of the mode
5746 line at the bottom. */
5747 if (from_y + run->height > bottom_y)
5748 height = bottom_y - from_y;
5749 else
5750 height = run->height;
5751 }
5752 else
5753 {
5754 /* Scolling down. Make sure we don't copy over the mode line.
5755 at the bottom. */
5756 if (to_y + run->height > bottom_y)
5757 height = bottom_y - to_y;
5758 else
5759 height = run->height;
5760 }
5761
5762 BLOCK_INPUT;
5763
5764 /* Cursor off. Will be switched on again in x_update_window_end. */
5765 updated_window = w;
5766 x_clear_cursor (w);
5767
5768 XCopyArea (FRAME_X_DISPLAY (f),
5769 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5770 f->output_data.x->normal_gc,
5771 x, from_y,
5772 width, height,
5773 x, to_y);
5774
5775 UNBLOCK_INPUT;
5776 }
5777
5778
5779 \f
5780 /***********************************************************************
5781 Exposure Events
5782 ***********************************************************************/
5783
5784 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5785 corner of the exposed rectangle. W and H are width and height of
5786 the exposed area. All are pixel values. W or H zero means redraw
5787 the entire frame. */
5788
5789 static void
5790 expose_frame (f, x, y, w, h)
5791 struct frame *f;
5792 int x, y, w, h;
5793 {
5794 XRectangle r;
5795 int mouse_face_overwritten_p = 0;
5796
5797 TRACE ((stderr, "expose_frame "));
5798
5799 /* No need to redraw if frame will be redrawn soon. */
5800 if (FRAME_GARBAGED_P (f))
5801 {
5802 TRACE ((stderr, " garbaged\n"));
5803 return;
5804 }
5805
5806 /* If basic faces haven't been realized yet, there is no point in
5807 trying to redraw anything. This can happen when we get an expose
5808 event while Emacs is starting, e.g. by moving another window. */
5809 if (FRAME_FACE_CACHE (f) == NULL
5810 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5811 {
5812 TRACE ((stderr, " no faces\n"));
5813 return;
5814 }
5815
5816 if (w == 0 || h == 0)
5817 {
5818 r.x = r.y = 0;
5819 r.width = CANON_X_UNIT (f) * f->width;
5820 r.height = CANON_Y_UNIT (f) * f->height;
5821 }
5822 else
5823 {
5824 r.x = x;
5825 r.y = y;
5826 r.width = w;
5827 r.height = h;
5828 }
5829
5830 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5831 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
5832
5833 if (WINDOWP (f->tool_bar_window))
5834 mouse_face_overwritten_p
5835 |= expose_window (XWINDOW (f->tool_bar_window), &r);
5836
5837 #ifndef USE_X_TOOLKIT
5838 if (WINDOWP (f->menu_bar_window))
5839 mouse_face_overwritten_p
5840 |= expose_window (XWINDOW (f->menu_bar_window), &r);
5841 #endif /* not USE_X_TOOLKIT */
5842
5843 /* Some window managers support a focus-follows-mouse style with
5844 delayed raising of frames. Imagine a partially obscured frame,
5845 and moving the mouse into partially obscured mouse-face on that
5846 frame. The visible part of the mouse-face will be highlighted,
5847 then the WM raises the obscured frame. With at least one WM, KDE
5848 2.1, Emacs is not getting any event for the raising of the frame
5849 (even tried with SubstructureRedirectMask), only Expose events.
5850 These expose events will draw text normally, i.e. not
5851 highlighted. Which means we must redo the highlight here.
5852 Subsume it under ``we love X''. --gerd 2001-08-15 */
5853 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
5854 {
5855 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5856 if (f == dpyinfo->mouse_face_mouse_frame)
5857 {
5858 int x = dpyinfo->mouse_face_mouse_x;
5859 int y = dpyinfo->mouse_face_mouse_y;
5860 clear_mouse_face (dpyinfo);
5861 note_mouse_highlight (f, x, y);
5862 }
5863 }
5864 }
5865
5866
5867 /* Redraw (parts) of all windows in the window tree rooted at W that
5868 intersect R. R contains frame pixel coordinates. Value is
5869 non-zero if the exposure overwrites mouse-face. */
5870
5871 static int
5872 expose_window_tree (w, r)
5873 struct window *w;
5874 XRectangle *r;
5875 {
5876 struct frame *f = XFRAME (w->frame);
5877 int mouse_face_overwritten_p = 0;
5878
5879 while (w && !FRAME_GARBAGED_P (f))
5880 {
5881 if (!NILP (w->hchild))
5882 mouse_face_overwritten_p
5883 |= expose_window_tree (XWINDOW (w->hchild), r);
5884 else if (!NILP (w->vchild))
5885 mouse_face_overwritten_p
5886 |= expose_window_tree (XWINDOW (w->vchild), r);
5887 else
5888 mouse_face_overwritten_p |= expose_window (w, r);
5889
5890 w = NILP (w->next) ? NULL : XWINDOW (w->next);
5891 }
5892
5893 return mouse_face_overwritten_p;
5894 }
5895
5896
5897 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5898 which intersects rectangle R. R is in window-relative coordinates. */
5899
5900 static void
5901 expose_area (w, row, r, area)
5902 struct window *w;
5903 struct glyph_row *row;
5904 XRectangle *r;
5905 enum glyph_row_area area;
5906 {
5907 struct glyph *first = row->glyphs[area];
5908 struct glyph *end = row->glyphs[area] + row->used[area];
5909 struct glyph *last;
5910 int first_x, start_x, x;
5911
5912 if (area == TEXT_AREA && row->fill_line_p)
5913 /* If row extends face to end of line write the whole line. */
5914 x_draw_glyphs (w, 0, row, area, 0, row->used[area],
5915 DRAW_NORMAL_TEXT, 0);
5916 else
5917 {
5918 /* Set START_X to the window-relative start position for drawing glyphs of
5919 AREA. The first glyph of the text area can be partially visible.
5920 The first glyphs of other areas cannot. */
5921 if (area == LEFT_MARGIN_AREA)
5922 start_x = 0;
5923 else if (area == TEXT_AREA)
5924 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5925 else
5926 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5927 + window_box_width (w, TEXT_AREA));
5928 x = start_x;
5929
5930 /* Find the first glyph that must be redrawn. */
5931 while (first < end
5932 && x + first->pixel_width < r->x)
5933 {
5934 x += first->pixel_width;
5935 ++first;
5936 }
5937
5938 /* Find the last one. */
5939 last = first;
5940 first_x = x;
5941 while (last < end
5942 && x < r->x + r->width)
5943 {
5944 x += last->pixel_width;
5945 ++last;
5946 }
5947
5948 /* Repaint. */
5949 if (last > first)
5950 x_draw_glyphs (w, first_x - start_x, row, area,
5951 first - row->glyphs[area],
5952 last - row->glyphs[area],
5953 DRAW_NORMAL_TEXT, 0);
5954 }
5955 }
5956
5957
5958 /* Redraw the parts of the glyph row ROW on window W intersecting
5959 rectangle R. R is in window-relative coordinates. Value is
5960 non-zero if mouse-face was overwritten. */
5961
5962 static int
5963 expose_line (w, row, r)
5964 struct window *w;
5965 struct glyph_row *row;
5966 XRectangle *r;
5967 {
5968 xassert (row->enabled_p);
5969
5970 if (row->mode_line_p || w->pseudo_window_p)
5971 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5972 DRAW_NORMAL_TEXT, 0);
5973 else
5974 {
5975 if (row->used[LEFT_MARGIN_AREA])
5976 expose_area (w, row, r, LEFT_MARGIN_AREA);
5977 if (row->used[TEXT_AREA])
5978 expose_area (w, row, r, TEXT_AREA);
5979 if (row->used[RIGHT_MARGIN_AREA])
5980 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5981 x_draw_row_fringe_bitmaps (w, row);
5982 }
5983
5984 return row->mouse_face_p;
5985 }
5986
5987
5988 /* Return non-zero if W's cursor intersects rectangle R. */
5989
5990 static int
5991 x_phys_cursor_in_rect_p (w, r)
5992 struct window *w;
5993 XRectangle *r;
5994 {
5995 XRectangle cr, result;
5996 struct glyph *cursor_glyph;
5997
5998 cursor_glyph = get_phys_cursor_glyph (w);
5999 if (cursor_glyph)
6000 {
6001 cr.x = w->phys_cursor.x;
6002 cr.y = w->phys_cursor.y;
6003 cr.width = cursor_glyph->pixel_width;
6004 cr.height = w->phys_cursor_height;
6005 return x_intersect_rectangles (&cr, r, &result);
6006 }
6007 else
6008 return 0;
6009 }
6010
6011
6012 /* Redraw the part of window W intersection rectangle FR. Pixel
6013 coordinates in FR are frame-relative. Call this function with
6014 input blocked. Value is non-zero if the exposure overwrites
6015 mouse-face. */
6016
6017 static int
6018 expose_window (w, fr)
6019 struct window *w;
6020 XRectangle *fr;
6021 {
6022 struct frame *f = XFRAME (w->frame);
6023 XRectangle wr, r;
6024 int mouse_face_overwritten_p = 0;
6025
6026 /* If window is not yet fully initialized, do nothing. This can
6027 happen when toolkit scroll bars are used and a window is split.
6028 Reconfiguring the scroll bar will generate an expose for a newly
6029 created window. */
6030 if (w->current_matrix == NULL)
6031 return 0;
6032
6033 /* When we're currently updating the window, display and current
6034 matrix usually don't agree. Arrange for a thorough display
6035 later. */
6036 if (w == updated_window)
6037 {
6038 SET_FRAME_GARBAGED (f);
6039 return 0;
6040 }
6041
6042 /* Frame-relative pixel rectangle of W. */
6043 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
6044 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
6045 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
6046 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
6047
6048 if (x_intersect_rectangles (fr, &wr, &r))
6049 {
6050 int yb = window_text_bottom_y (w);
6051 struct glyph_row *row;
6052 int cursor_cleared_p;
6053
6054 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
6055 r.x, r.y, r.width, r.height));
6056
6057 /* Convert to window coordinates. */
6058 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
6059 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
6060
6061 /* Turn off the cursor. */
6062 if (!w->pseudo_window_p
6063 && x_phys_cursor_in_rect_p (w, &r))
6064 {
6065 x_clear_cursor (w);
6066 cursor_cleared_p = 1;
6067 }
6068 else
6069 cursor_cleared_p = 0;
6070
6071 /* Find the first row intersecting the rectangle R. */
6072 for (row = w->current_matrix->rows;
6073 row->enabled_p;
6074 ++row)
6075 {
6076 int y0 = row->y;
6077 int y1 = MATRIX_ROW_BOTTOM_Y (row);
6078
6079 if ((y0 >= r.y && y0 < r.y + r.height)
6080 || (y1 > r.y && y1 < r.y + r.height)
6081 || (r.y >= y0 && r.y < y1)
6082 || (r.y + r.height > y0 && r.y + r.height < y1))
6083 {
6084 if (expose_line (w, row, &r))
6085 mouse_face_overwritten_p = 1;
6086 }
6087
6088 if (y1 >= yb)
6089 break;
6090 }
6091
6092 /* Display the mode line if there is one. */
6093 if (WINDOW_WANTS_MODELINE_P (w)
6094 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
6095 row->enabled_p)
6096 && row->y < r.y + r.height)
6097 {
6098 if (expose_line (w, row, &r))
6099 mouse_face_overwritten_p = 1;
6100 }
6101
6102 if (!w->pseudo_window_p)
6103 {
6104 /* Draw border between windows. */
6105 x_draw_vertical_border (w);
6106
6107 /* Turn the cursor on again. */
6108 if (cursor_cleared_p)
6109 x_update_window_cursor (w, 1);
6110 }
6111 }
6112
6113 return mouse_face_overwritten_p;
6114 }
6115
6116
6117 /* Determine the intersection of two rectangles R1 and R2. Return
6118 the intersection in *RESULT. Value is non-zero if RESULT is not
6119 empty. */
6120
6121 static int
6122 x_intersect_rectangles (r1, r2, result)
6123 XRectangle *r1, *r2, *result;
6124 {
6125 XRectangle *left, *right;
6126 XRectangle *upper, *lower;
6127 int intersection_p = 0;
6128
6129 /* Rearrange so that R1 is the left-most rectangle. */
6130 if (r1->x < r2->x)
6131 left = r1, right = r2;
6132 else
6133 left = r2, right = r1;
6134
6135 /* X0 of the intersection is right.x0, if this is inside R1,
6136 otherwise there is no intersection. */
6137 if (right->x <= left->x + left->width)
6138 {
6139 result->x = right->x;
6140
6141 /* The right end of the intersection is the minimum of the
6142 the right ends of left and right. */
6143 result->width = (min (left->x + left->width, right->x + right->width)
6144 - result->x);
6145
6146 /* Same game for Y. */
6147 if (r1->y < r2->y)
6148 upper = r1, lower = r2;
6149 else
6150 upper = r2, lower = r1;
6151
6152 /* The upper end of the intersection is lower.y0, if this is inside
6153 of upper. Otherwise, there is no intersection. */
6154 if (lower->y <= upper->y + upper->height)
6155 {
6156 result->y = lower->y;
6157
6158 /* The lower end of the intersection is the minimum of the lower
6159 ends of upper and lower. */
6160 result->height = (min (lower->y + lower->height,
6161 upper->y + upper->height)
6162 - result->y);
6163 intersection_p = 1;
6164 }
6165 }
6166
6167 return intersection_p;
6168 }
6169
6170
6171
6172
6173 \f
6174 static void
6175 frame_highlight (f)
6176 struct frame *f;
6177 {
6178 /* We used to only do this if Vx_no_window_manager was non-nil, but
6179 the ICCCM (section 4.1.6) says that the window's border pixmap
6180 and border pixel are window attributes which are "private to the
6181 client", so we can always change it to whatever we want. */
6182 BLOCK_INPUT;
6183 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6184 f->output_data.x->border_pixel);
6185 UNBLOCK_INPUT;
6186 x_update_cursor (f, 1);
6187 }
6188
6189 static void
6190 frame_unhighlight (f)
6191 struct frame *f;
6192 {
6193 /* We used to only do this if Vx_no_window_manager was non-nil, but
6194 the ICCCM (section 4.1.6) says that the window's border pixmap
6195 and border pixel are window attributes which are "private to the
6196 client", so we can always change it to whatever we want. */
6197 BLOCK_INPUT;
6198 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6199 f->output_data.x->border_tile);
6200 UNBLOCK_INPUT;
6201 x_update_cursor (f, 1);
6202 }
6203
6204 /* The focus has changed. Update the frames as necessary to reflect
6205 the new situation. Note that we can't change the selected frame
6206 here, because the Lisp code we are interrupting might become confused.
6207 Each event gets marked with the frame in which it occurred, so the
6208 Lisp code can tell when the switch took place by examining the events. */
6209
6210 static void
6211 x_new_focus_frame (dpyinfo, frame)
6212 struct x_display_info *dpyinfo;
6213 struct frame *frame;
6214 {
6215 struct frame *old_focus = dpyinfo->x_focus_frame;
6216
6217 if (frame != dpyinfo->x_focus_frame)
6218 {
6219 /* Set this before calling other routines, so that they see
6220 the correct value of x_focus_frame. */
6221 dpyinfo->x_focus_frame = frame;
6222
6223 if (old_focus && old_focus->auto_lower)
6224 x_lower_frame (old_focus);
6225
6226 #if 0
6227 selected_frame = frame;
6228 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6229 selected_frame);
6230 Fselect_window (selected_frame->selected_window);
6231 choose_minibuf_frame ();
6232 #endif /* ! 0 */
6233
6234 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6235 pending_autoraise_frame = dpyinfo->x_focus_frame;
6236 else
6237 pending_autoraise_frame = 0;
6238 }
6239
6240 x_frame_rehighlight (dpyinfo);
6241 }
6242
6243 /* Handle an event saying the mouse has moved out of an Emacs frame. */
6244
6245 void
6246 x_mouse_leave (dpyinfo)
6247 struct x_display_info *dpyinfo;
6248 {
6249 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
6250 }
6251
6252 /* The focus has changed, or we have redirected a frame's focus to
6253 another frame (this happens when a frame uses a surrogate
6254 mini-buffer frame). Shift the highlight as appropriate.
6255
6256 The FRAME argument doesn't necessarily have anything to do with which
6257 frame is being highlighted or un-highlighted; we only use it to find
6258 the appropriate X display info. */
6259
6260 static void
6261 XTframe_rehighlight (frame)
6262 struct frame *frame;
6263 {
6264 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6265 }
6266
6267 static void
6268 x_frame_rehighlight (dpyinfo)
6269 struct x_display_info *dpyinfo;
6270 {
6271 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6272
6273 if (dpyinfo->x_focus_frame)
6274 {
6275 dpyinfo->x_highlight_frame
6276 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6277 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6278 : dpyinfo->x_focus_frame);
6279 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
6280 {
6281 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6282 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
6283 }
6284 }
6285 else
6286 dpyinfo->x_highlight_frame = 0;
6287
6288 if (dpyinfo->x_highlight_frame != old_highlight)
6289 {
6290 if (old_highlight)
6291 frame_unhighlight (old_highlight);
6292 if (dpyinfo->x_highlight_frame)
6293 frame_highlight (dpyinfo->x_highlight_frame);
6294 }
6295 }
6296
6297
6298 \f
6299 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
6300
6301 /* Initialize mode_switch_bit and modifier_meaning. */
6302 static void
6303 x_find_modifier_meanings (dpyinfo)
6304 struct x_display_info *dpyinfo;
6305 {
6306 int min_code, max_code;
6307 KeySym *syms;
6308 int syms_per_code;
6309 XModifierKeymap *mods;
6310
6311 dpyinfo->meta_mod_mask = 0;
6312 dpyinfo->shift_lock_mask = 0;
6313 dpyinfo->alt_mod_mask = 0;
6314 dpyinfo->super_mod_mask = 0;
6315 dpyinfo->hyper_mod_mask = 0;
6316
6317 #ifdef HAVE_X11R4
6318 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
6319 #else
6320 min_code = dpyinfo->display->min_keycode;
6321 max_code = dpyinfo->display->max_keycode;
6322 #endif
6323
6324 syms = XGetKeyboardMapping (dpyinfo->display,
6325 min_code, max_code - min_code + 1,
6326 &syms_per_code);
6327 mods = XGetModifierMapping (dpyinfo->display);
6328
6329 /* Scan the modifier table to see which modifier bits the Meta and
6330 Alt keysyms are on. */
6331 {
6332 int row, col; /* The row and column in the modifier table. */
6333
6334 for (row = 3; row < 8; row++)
6335 for (col = 0; col < mods->max_keypermod; col++)
6336 {
6337 KeyCode code
6338 = mods->modifiermap[(row * mods->max_keypermod) + col];
6339
6340 /* Zeroes are used for filler. Skip them. */
6341 if (code == 0)
6342 continue;
6343
6344 /* Are any of this keycode's keysyms a meta key? */
6345 {
6346 int code_col;
6347
6348 for (code_col = 0; code_col < syms_per_code; code_col++)
6349 {
6350 int sym = syms[((code - min_code) * syms_per_code) + code_col];
6351
6352 switch (sym)
6353 {
6354 case XK_Meta_L:
6355 case XK_Meta_R:
6356 dpyinfo->meta_mod_mask |= (1 << row);
6357 break;
6358
6359 case XK_Alt_L:
6360 case XK_Alt_R:
6361 dpyinfo->alt_mod_mask |= (1 << row);
6362 break;
6363
6364 case XK_Hyper_L:
6365 case XK_Hyper_R:
6366 dpyinfo->hyper_mod_mask |= (1 << row);
6367 break;
6368
6369 case XK_Super_L:
6370 case XK_Super_R:
6371 dpyinfo->super_mod_mask |= (1 << row);
6372 break;
6373
6374 case XK_Shift_Lock:
6375 /* Ignore this if it's not on the lock modifier. */
6376 if ((1 << row) == LockMask)
6377 dpyinfo->shift_lock_mask = LockMask;
6378 break;
6379 }
6380 }
6381 }
6382 }
6383 }
6384
6385 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
6386 if (! dpyinfo->meta_mod_mask)
6387 {
6388 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6389 dpyinfo->alt_mod_mask = 0;
6390 }
6391
6392 /* If some keys are both alt and meta,
6393 make them just meta, not alt. */
6394 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6395 {
6396 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6397 }
6398
6399 XFree ((char *) syms);
6400 XFreeModifiermap (mods);
6401 }
6402
6403 /* Convert between the modifier bits X uses and the modifier bits
6404 Emacs uses. */
6405
6406 static unsigned int
6407 x_x_to_emacs_modifiers (dpyinfo, state)
6408 struct x_display_info *dpyinfo;
6409 unsigned int state;
6410 {
6411 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6412 | ((state & ControlMask) ? ctrl_modifier : 0)
6413 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6414 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6415 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6416 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
6417 }
6418
6419 static unsigned int
6420 x_emacs_to_x_modifiers (dpyinfo, state)
6421 struct x_display_info *dpyinfo;
6422 unsigned int state;
6423 {
6424 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6425 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6426 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6427 | ((state & shift_modifier) ? ShiftMask : 0)
6428 | ((state & ctrl_modifier) ? ControlMask : 0)
6429 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
6430 }
6431
6432 /* Convert a keysym to its name. */
6433
6434 char *
6435 x_get_keysym_name (keysym)
6436 KeySym keysym;
6437 {
6438 char *value;
6439
6440 BLOCK_INPUT;
6441 value = XKeysymToString (keysym);
6442 UNBLOCK_INPUT;
6443
6444 return value;
6445 }
6446
6447
6448 \f
6449 /* Mouse clicks and mouse movement. Rah. */
6450
6451 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6452 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6453 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6454 not force the value into range. */
6455
6456 void
6457 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6458 FRAME_PTR f;
6459 register int pix_x, pix_y;
6460 register int *x, *y;
6461 XRectangle *bounds;
6462 int noclip;
6463 {
6464 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6465 even for negative values. */
6466 if (pix_x < 0)
6467 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6468 if (pix_y < 0)
6469 pix_y -= (f)->output_data.x->line_height - 1;
6470
6471 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6472 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6473
6474 if (bounds)
6475 {
6476 bounds->width = FONT_WIDTH (f->output_data.x->font);
6477 bounds->height = f->output_data.x->line_height;
6478 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6479 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6480 }
6481
6482 if (!noclip)
6483 {
6484 if (pix_x < 0)
6485 pix_x = 0;
6486 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6487 pix_x = FRAME_WINDOW_WIDTH (f);
6488
6489 if (pix_y < 0)
6490 pix_y = 0;
6491 else if (pix_y > f->height)
6492 pix_y = f->height;
6493 }
6494
6495 *x = pix_x;
6496 *y = pix_y;
6497 }
6498
6499
6500 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6501 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6502 can't tell the positions because W's display is not up to date,
6503 return 0. */
6504
6505 int
6506 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6507 struct window *w;
6508 int hpos, vpos;
6509 int *frame_x, *frame_y;
6510 {
6511 int success_p;
6512
6513 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6514 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6515
6516 if (display_completed)
6517 {
6518 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6519 struct glyph *glyph = row->glyphs[TEXT_AREA];
6520 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6521
6522 *frame_y = row->y;
6523 *frame_x = row->x;
6524 while (glyph < end)
6525 {
6526 *frame_x += glyph->pixel_width;
6527 ++glyph;
6528 }
6529
6530 success_p = 1;
6531 }
6532 else
6533 {
6534 *frame_y = *frame_x = 0;
6535 success_p = 0;
6536 }
6537
6538 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6539 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6540 return success_p;
6541 }
6542
6543
6544 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6545
6546 If the event is a button press, then note that we have grabbed
6547 the mouse. */
6548
6549 static Lisp_Object
6550 construct_mouse_click (result, event, f)
6551 struct input_event *result;
6552 XButtonEvent *event;
6553 struct frame *f;
6554 {
6555 /* Make the event type no_event; we'll change that when we decide
6556 otherwise. */
6557 result->kind = mouse_click;
6558 result->code = event->button - Button1;
6559 result->timestamp = event->time;
6560 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6561 event->state)
6562 | (event->type == ButtonRelease
6563 ? up_modifier
6564 : down_modifier));
6565
6566 XSETINT (result->x, event->x);
6567 XSETINT (result->y, event->y);
6568 XSETFRAME (result->frame_or_window, f);
6569 result->arg = Qnil;
6570 return Qnil;
6571 }
6572
6573 \f
6574 /* Function to report a mouse movement to the mainstream Emacs code.
6575 The input handler calls this.
6576
6577 We have received a mouse movement event, which is given in *event.
6578 If the mouse is over a different glyph than it was last time, tell
6579 the mainstream emacs code by setting mouse_moved. If not, ask for
6580 another motion event, so we can check again the next time it moves. */
6581
6582 static XMotionEvent last_mouse_motion_event;
6583 static Lisp_Object last_mouse_motion_frame;
6584
6585 static void
6586 note_mouse_movement (frame, event)
6587 FRAME_PTR frame;
6588 XMotionEvent *event;
6589 {
6590 last_mouse_movement_time = event->time;
6591 last_mouse_motion_event = *event;
6592 XSETFRAME (last_mouse_motion_frame, frame);
6593
6594 if (event->window != FRAME_X_WINDOW (frame))
6595 {
6596 frame->mouse_moved = 1;
6597 last_mouse_scroll_bar = Qnil;
6598 note_mouse_highlight (frame, -1, -1);
6599 }
6600
6601 /* Has the mouse moved off the glyph it was on at the last sighting? */
6602 else if (event->x < last_mouse_glyph.x
6603 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6604 || event->y < last_mouse_glyph.y
6605 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6606 {
6607 frame->mouse_moved = 1;
6608 last_mouse_scroll_bar = Qnil;
6609 note_mouse_highlight (frame, event->x, event->y);
6610 }
6611 }
6612
6613 /* This is used for debugging, to turn off note_mouse_highlight. */
6614
6615 int disable_mouse_highlight;
6616
6617
6618 \f
6619 /************************************************************************
6620 Mouse Face
6621 ************************************************************************/
6622
6623 /* Find the glyph under window-relative coordinates X/Y in window W.
6624 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6625 strings. Return in *HPOS and *VPOS the row and column number of
6626 the glyph found. Return in *AREA the glyph area containing X.
6627 Value is a pointer to the glyph found or null if X/Y is not on
6628 text, or we can't tell because W's current matrix is not up to
6629 date. */
6630
6631 static struct glyph *
6632 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
6633 struct window *w;
6634 int x, y;
6635 int *hpos, *vpos, *area;
6636 int buffer_only_p;
6637 {
6638 struct glyph *glyph, *end;
6639 struct glyph_row *row = NULL;
6640 int x0, i, left_area_width;
6641
6642 /* Find row containing Y. Give up if some row is not enabled. */
6643 for (i = 0; i < w->current_matrix->nrows; ++i)
6644 {
6645 row = MATRIX_ROW (w->current_matrix, i);
6646 if (!row->enabled_p)
6647 return NULL;
6648 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6649 break;
6650 }
6651
6652 *vpos = i;
6653 *hpos = 0;
6654
6655 /* Give up if Y is not in the window. */
6656 if (i == w->current_matrix->nrows)
6657 return NULL;
6658
6659 /* Get the glyph area containing X. */
6660 if (w->pseudo_window_p)
6661 {
6662 *area = TEXT_AREA;
6663 x0 = 0;
6664 }
6665 else
6666 {
6667 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6668 if (x < left_area_width)
6669 {
6670 *area = LEFT_MARGIN_AREA;
6671 x0 = 0;
6672 }
6673 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6674 {
6675 *area = TEXT_AREA;
6676 x0 = row->x + left_area_width;
6677 }
6678 else
6679 {
6680 *area = RIGHT_MARGIN_AREA;
6681 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6682 }
6683 }
6684
6685 /* Find glyph containing X. */
6686 glyph = row->glyphs[*area];
6687 end = glyph + row->used[*area];
6688 while (glyph < end)
6689 {
6690 if (x < x0 + glyph->pixel_width)
6691 {
6692 if (w->pseudo_window_p)
6693 break;
6694 else if (!buffer_only_p || BUFFERP (glyph->object))
6695 break;
6696 }
6697
6698 x0 += glyph->pixel_width;
6699 ++glyph;
6700 }
6701
6702 if (glyph == end)
6703 return NULL;
6704
6705 *hpos = glyph - row->glyphs[*area];
6706 return glyph;
6707 }
6708
6709
6710 /* Convert frame-relative x/y to coordinates relative to window W.
6711 Takes pseudo-windows into account. */
6712
6713 static void
6714 frame_to_window_pixel_xy (w, x, y)
6715 struct window *w;
6716 int *x, *y;
6717 {
6718 if (w->pseudo_window_p)
6719 {
6720 /* A pseudo-window is always full-width, and starts at the
6721 left edge of the frame, plus a frame border. */
6722 struct frame *f = XFRAME (w->frame);
6723 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6724 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6725 }
6726 else
6727 {
6728 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6729 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6730 }
6731 }
6732
6733
6734 /* Take proper action when mouse has moved to the mode or header line of
6735 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6736 mode line. X is relative to the start of the text display area of
6737 W, so the width of fringes and scroll bars must be subtracted
6738 to get a position relative to the start of the mode line. */
6739
6740 static void
6741 note_mode_line_highlight (w, x, mode_line_p)
6742 struct window *w;
6743 int x, mode_line_p;
6744 {
6745 struct frame *f = XFRAME (w->frame);
6746 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6747 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6748 struct glyph_row *row;
6749
6750 if (mode_line_p)
6751 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6752 else
6753 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6754
6755 if (row->enabled_p)
6756 {
6757 struct glyph *glyph, *end;
6758 Lisp_Object help, map;
6759 int x0;
6760
6761 /* Find the glyph under X. */
6762 glyph = row->glyphs[TEXT_AREA];
6763 end = glyph + row->used[TEXT_AREA];
6764 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6765 + FRAME_X_LEFT_FRINGE_WIDTH (f));
6766
6767 while (glyph < end
6768 && x >= x0 + glyph->pixel_width)
6769 {
6770 x0 += glyph->pixel_width;
6771 ++glyph;
6772 }
6773
6774 if (glyph < end
6775 && STRINGP (glyph->object)
6776 && XSTRING (glyph->object)->intervals
6777 && glyph->charpos >= 0
6778 && glyph->charpos < XSTRING (glyph->object)->size)
6779 {
6780 /* If we're on a string with `help-echo' text property,
6781 arrange for the help to be displayed. This is done by
6782 setting the global variable help_echo to the help string. */
6783 help = Fget_text_property (make_number (glyph->charpos),
6784 Qhelp_echo, glyph->object);
6785 if (!NILP (help))
6786 {
6787 help_echo = help;
6788 XSETWINDOW (help_echo_window, w);
6789 help_echo_object = glyph->object;
6790 help_echo_pos = glyph->charpos;
6791 }
6792
6793 /* Change the mouse pointer according to what is under X/Y. */
6794 map = Fget_text_property (make_number (glyph->charpos),
6795 Qlocal_map, glyph->object);
6796 if (KEYMAPP (map))
6797 cursor = f->output_data.x->nontext_cursor;
6798 else
6799 {
6800 map = Fget_text_property (make_number (glyph->charpos),
6801 Qkeymap, glyph->object);
6802 if (KEYMAPP (map))
6803 cursor = f->output_data.x->nontext_cursor;
6804 }
6805 }
6806 }
6807
6808 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6809 }
6810
6811
6812 /* Take proper action when the mouse has moved to position X, Y on
6813 frame F as regards highlighting characters that have mouse-face
6814 properties. Also de-highlighting chars where the mouse was before.
6815 X and Y can be negative or out of range. */
6816
6817 static void
6818 note_mouse_highlight (f, x, y)
6819 struct frame *f;
6820 int x, y;
6821 {
6822 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6823 int portion;
6824 Lisp_Object window;
6825 struct window *w;
6826 Cursor cursor = None;
6827 struct buffer *b;
6828
6829 /* When a menu is active, don't highlight because this looks odd. */
6830 #ifdef USE_X_TOOLKIT
6831 if (popup_activated ())
6832 return;
6833 #endif
6834
6835 if (disable_mouse_highlight
6836 || !f->glyphs_initialized_p)
6837 return;
6838
6839 dpyinfo->mouse_face_mouse_x = x;
6840 dpyinfo->mouse_face_mouse_y = y;
6841 dpyinfo->mouse_face_mouse_frame = f;
6842
6843 if (dpyinfo->mouse_face_defer)
6844 return;
6845
6846 if (gc_in_progress)
6847 {
6848 dpyinfo->mouse_face_deferred_gc = 1;
6849 return;
6850 }
6851
6852 /* Which window is that in? */
6853 window = window_from_coordinates (f, x, y, &portion, 1);
6854
6855 /* If we were displaying active text in another window, clear that. */
6856 if (! EQ (window, dpyinfo->mouse_face_window))
6857 clear_mouse_face (dpyinfo);
6858
6859 /* Not on a window -> return. */
6860 if (!WINDOWP (window))
6861 return;
6862
6863 /* Convert to window-relative pixel coordinates. */
6864 w = XWINDOW (window);
6865 frame_to_window_pixel_xy (w, &x, &y);
6866
6867 /* Handle tool-bar window differently since it doesn't display a
6868 buffer. */
6869 if (EQ (window, f->tool_bar_window))
6870 {
6871 note_tool_bar_highlight (f, x, y);
6872 return;
6873 }
6874
6875 /* Mouse is on the mode or header line? */
6876 if (portion == 1 || portion == 3)
6877 {
6878 note_mode_line_highlight (w, x, portion == 1);
6879 return;
6880 }
6881
6882 if (portion == 2)
6883 cursor = f->output_data.x->horizontal_drag_cursor;
6884 else
6885 cursor = f->output_data.x->text_cursor;
6886
6887 /* Are we in a window whose display is up to date?
6888 And verify the buffer's text has not changed. */
6889 b = XBUFFER (w->buffer);
6890 if (/* Within text portion of the window. */
6891 portion == 0
6892 && EQ (w->window_end_valid, w->buffer)
6893 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
6894 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
6895 {
6896 int hpos, vpos, pos, i, area;
6897 struct glyph *glyph;
6898 Lisp_Object object;
6899 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
6900 Lisp_Object *overlay_vec = NULL;
6901 int len, noverlays;
6902 struct buffer *obuf;
6903 int obegv, ozv, same_region;
6904
6905 /* Find the glyph under X/Y. */
6906 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
6907
6908 /* Clear mouse face if X/Y not over text. */
6909 if (glyph == NULL
6910 || area != TEXT_AREA
6911 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6912 {
6913 if (clear_mouse_face (dpyinfo))
6914 cursor = None;
6915 goto set_cursor;
6916 }
6917
6918 pos = glyph->charpos;
6919 object = glyph->object;
6920 if (!STRINGP (object) && !BUFFERP (object))
6921 goto set_cursor;
6922
6923 /* If we get an out-of-range value, return now; avoid an error. */
6924 if (BUFFERP (object) && pos > BUF_Z (b))
6925 goto set_cursor;
6926
6927 /* Make the window's buffer temporarily current for
6928 overlays_at and compute_char_face. */
6929 obuf = current_buffer;
6930 current_buffer = b;
6931 obegv = BEGV;
6932 ozv = ZV;
6933 BEGV = BEG;
6934 ZV = Z;
6935
6936 /* Is this char mouse-active or does it have help-echo? */
6937 position = make_number (pos);
6938
6939 if (BUFFERP (object))
6940 {
6941 /* Put all the overlays we want in a vector in overlay_vec.
6942 Store the length in len. If there are more than 10, make
6943 enough space for all, and try again. */
6944 len = 10;
6945 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6946 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6947 if (noverlays > len)
6948 {
6949 len = noverlays;
6950 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6951 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6952 }
6953
6954 /* Sort overlays into increasing priority order. */
6955 noverlays = sort_overlays (overlay_vec, noverlays, w);
6956 }
6957 else
6958 noverlays = 0;
6959
6960 same_region = (EQ (window, dpyinfo->mouse_face_window)
6961 && vpos >= dpyinfo->mouse_face_beg_row
6962 && vpos <= dpyinfo->mouse_face_end_row
6963 && (vpos > dpyinfo->mouse_face_beg_row
6964 || hpos >= dpyinfo->mouse_face_beg_col)
6965 && (vpos < dpyinfo->mouse_face_end_row
6966 || hpos < dpyinfo->mouse_face_end_col
6967 || dpyinfo->mouse_face_past_end));
6968
6969 if (same_region)
6970 cursor = None;
6971
6972 /* Check mouse-face highlighting. */
6973 if (! same_region
6974 /* If there exists an overlay with mouse-face overlapping
6975 the one we are currently highlighting, we have to
6976 check if we enter the overlapping overlay, and then
6977 highlight only that. */
6978 || (OVERLAYP (dpyinfo->mouse_face_overlay)
6979 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
6980 {
6981 /* Find the highest priority overlay that has a mouse-face
6982 property. */
6983 overlay = Qnil;
6984 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
6985 {
6986 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6987 if (!NILP (mouse_face))
6988 overlay = overlay_vec[i];
6989 }
6990
6991 /* If we're actually highlighting the same overlay as
6992 before, there's no need to do that again. */
6993 if (!NILP (overlay)
6994 && EQ (overlay, dpyinfo->mouse_face_overlay))
6995 goto check_help_echo;
6996
6997 dpyinfo->mouse_face_overlay = overlay;
6998
6999 /* Clear the display of the old active region, if any. */
7000 if (clear_mouse_face (dpyinfo))
7001 cursor = None;
7002
7003 /* If no overlay applies, get a text property. */
7004 if (NILP (overlay))
7005 mouse_face = Fget_text_property (position, Qmouse_face, object);
7006
7007 /* Handle the overlay case. */
7008 if (!NILP (overlay))
7009 {
7010 /* Find the range of text around this char that
7011 should be active. */
7012 Lisp_Object before, after;
7013 int ignore;
7014
7015 before = Foverlay_start (overlay);
7016 after = Foverlay_end (overlay);
7017 /* Record this as the current active region. */
7018 fast_find_position (w, XFASTINT (before),
7019 &dpyinfo->mouse_face_beg_col,
7020 &dpyinfo->mouse_face_beg_row,
7021 &dpyinfo->mouse_face_beg_x,
7022 &dpyinfo->mouse_face_beg_y, Qnil);
7023
7024 dpyinfo->mouse_face_past_end
7025 = !fast_find_position (w, XFASTINT (after),
7026 &dpyinfo->mouse_face_end_col,
7027 &dpyinfo->mouse_face_end_row,
7028 &dpyinfo->mouse_face_end_x,
7029 &dpyinfo->mouse_face_end_y, Qnil);
7030 dpyinfo->mouse_face_window = window;
7031 dpyinfo->mouse_face_face_id
7032 = face_at_buffer_position (w, pos, 0, 0,
7033 &ignore, pos + 1, 1);
7034
7035 /* Display it as active. */
7036 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7037 cursor = None;
7038 }
7039 /* Handle the text property case. */
7040 else if (!NILP (mouse_face) && BUFFERP (object))
7041 {
7042 /* Find the range of text around this char that
7043 should be active. */
7044 Lisp_Object before, after, beginning, end;
7045 int ignore;
7046
7047 beginning = Fmarker_position (w->start);
7048 end = make_number (BUF_Z (XBUFFER (object))
7049 - XFASTINT (w->window_end_pos));
7050 before
7051 = Fprevious_single_property_change (make_number (pos + 1),
7052 Qmouse_face,
7053 object, beginning);
7054 after
7055 = Fnext_single_property_change (position, Qmouse_face,
7056 object, end);
7057
7058 /* Record this as the current active region. */
7059 fast_find_position (w, XFASTINT (before),
7060 &dpyinfo->mouse_face_beg_col,
7061 &dpyinfo->mouse_face_beg_row,
7062 &dpyinfo->mouse_face_beg_x,
7063 &dpyinfo->mouse_face_beg_y, Qnil);
7064 dpyinfo->mouse_face_past_end
7065 = !fast_find_position (w, XFASTINT (after),
7066 &dpyinfo->mouse_face_end_col,
7067 &dpyinfo->mouse_face_end_row,
7068 &dpyinfo->mouse_face_end_x,
7069 &dpyinfo->mouse_face_end_y, Qnil);
7070 dpyinfo->mouse_face_window = window;
7071
7072 if (BUFFERP (object))
7073 dpyinfo->mouse_face_face_id
7074 = face_at_buffer_position (w, pos, 0, 0,
7075 &ignore, pos + 1, 1);
7076
7077 /* Display it as active. */
7078 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7079 cursor = None;
7080 }
7081 else if (!NILP (mouse_face) && STRINGP (object))
7082 {
7083 Lisp_Object b, e;
7084 int ignore;
7085
7086 b = Fprevious_single_property_change (make_number (pos + 1),
7087 Qmouse_face,
7088 object, Qnil);
7089 e = Fnext_single_property_change (position, Qmouse_face,
7090 object, Qnil);
7091 if (NILP (b))
7092 b = make_number (0);
7093 if (NILP (e))
7094 e = make_number (XSTRING (object)->size - 1);
7095 fast_find_string_pos (w, XINT (b), object,
7096 &dpyinfo->mouse_face_beg_col,
7097 &dpyinfo->mouse_face_beg_row,
7098 &dpyinfo->mouse_face_beg_x,
7099 &dpyinfo->mouse_face_beg_y, 0);
7100 fast_find_string_pos (w, XINT (e), object,
7101 &dpyinfo->mouse_face_end_col,
7102 &dpyinfo->mouse_face_end_row,
7103 &dpyinfo->mouse_face_end_x,
7104 &dpyinfo->mouse_face_end_y, 1);
7105 dpyinfo->mouse_face_past_end = 0;
7106 dpyinfo->mouse_face_window = window;
7107 dpyinfo->mouse_face_face_id
7108 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
7109 glyph->face_id, 1);
7110 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7111 cursor = None;
7112 }
7113 else if (STRINGP (object) && NILP (mouse_face))
7114 {
7115 /* A string which doesn't have mouse-face, but
7116 the text ``under'' it might have. */
7117 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
7118 int start = MATRIX_ROW_START_CHARPOS (r);
7119
7120 pos = string_buffer_position (w, object, start);
7121 if (pos > 0)
7122 mouse_face = get_char_property_and_overlay (make_number (pos),
7123 Qmouse_face,
7124 w->buffer,
7125 &overlay);
7126 if (!NILP (mouse_face) && !NILP (overlay))
7127 {
7128 Lisp_Object before = Foverlay_start (overlay);
7129 Lisp_Object after = Foverlay_end (overlay);
7130 Lisp_Object ignore;
7131
7132 /* Note that we might not be able to find position
7133 BEFORE in the glyph matrix if the overlay is
7134 entirely covered by a `display' property. In
7135 this case, we overshoot. So let's stop in
7136 the glyph matrix before glyphs for OBJECT. */
7137 fast_find_position (w, XFASTINT (before),
7138 &dpyinfo->mouse_face_beg_col,
7139 &dpyinfo->mouse_face_beg_row,
7140 &dpyinfo->mouse_face_beg_x,
7141 &dpyinfo->mouse_face_beg_y,
7142 object);
7143
7144 dpyinfo->mouse_face_past_end
7145 = !fast_find_position (w, XFASTINT (after),
7146 &dpyinfo->mouse_face_end_col,
7147 &dpyinfo->mouse_face_end_row,
7148 &dpyinfo->mouse_face_end_x,
7149 &dpyinfo->mouse_face_end_y,
7150 Qnil);
7151 dpyinfo->mouse_face_window = window;
7152 dpyinfo->mouse_face_face_id
7153 = face_at_buffer_position (w, pos, 0, 0,
7154 &ignore, pos + 1, 1);
7155
7156 /* Display it as active. */
7157 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7158 cursor = None;
7159 }
7160 }
7161 }
7162
7163 check_help_echo:
7164
7165 /* Look for a `help-echo' property. */
7166 {
7167 Lisp_Object help, overlay;
7168
7169 /* Check overlays first. */
7170 help = overlay = Qnil;
7171 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
7172 {
7173 overlay = overlay_vec[i];
7174 help = Foverlay_get (overlay, Qhelp_echo);
7175 }
7176
7177 if (!NILP (help))
7178 {
7179 help_echo = help;
7180 help_echo_window = window;
7181 help_echo_object = overlay;
7182 help_echo_pos = pos;
7183 }
7184 else
7185 {
7186 Lisp_Object object = glyph->object;
7187 int charpos = glyph->charpos;
7188
7189 /* Try text properties. */
7190 if (STRINGP (object)
7191 && charpos >= 0
7192 && charpos < XSTRING (object)->size)
7193 {
7194 help = Fget_text_property (make_number (charpos),
7195 Qhelp_echo, object);
7196 if (NILP (help))
7197 {
7198 /* If the string itself doesn't specify a help-echo,
7199 see if the buffer text ``under'' it does. */
7200 struct glyph_row *r
7201 = MATRIX_ROW (w->current_matrix, vpos);
7202 int start = MATRIX_ROW_START_CHARPOS (r);
7203 int pos = string_buffer_position (w, object, start);
7204 if (pos > 0)
7205 {
7206 help = Fget_char_property (make_number (pos),
7207 Qhelp_echo, w->buffer);
7208 if (!NILP (help))
7209 {
7210 charpos = pos;
7211 object = w->buffer;
7212 }
7213 }
7214 }
7215 }
7216 else if (BUFFERP (object)
7217 && charpos >= BEGV
7218 && charpos < ZV)
7219 help = Fget_text_property (make_number (charpos), Qhelp_echo,
7220 object);
7221
7222 if (!NILP (help))
7223 {
7224 help_echo = help;
7225 help_echo_window = window;
7226 help_echo_object = object;
7227 help_echo_pos = charpos;
7228 }
7229 }
7230 }
7231
7232 BEGV = obegv;
7233 ZV = ozv;
7234 current_buffer = obuf;
7235 }
7236
7237 set_cursor:
7238
7239 if (cursor != None)
7240 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7241 }
7242
7243 static void
7244 redo_mouse_highlight ()
7245 {
7246 if (!NILP (last_mouse_motion_frame)
7247 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
7248 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
7249 last_mouse_motion_event.x,
7250 last_mouse_motion_event.y);
7251 }
7252
7253
7254 \f
7255 /***********************************************************************
7256 Tool-bars
7257 ***********************************************************************/
7258
7259 static int x_tool_bar_item P_ ((struct frame *, int, int,
7260 struct glyph **, int *, int *, int *));
7261
7262 /* Tool-bar item index of the item on which a mouse button was pressed
7263 or -1. */
7264
7265 static int last_tool_bar_item;
7266
7267
7268 /* Get information about the tool-bar item at position X/Y on frame F.
7269 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7270 the current matrix of the tool-bar window of F, or NULL if not
7271 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
7272 item in F->tool_bar_items. Value is
7273
7274 -1 if X/Y is not on a tool-bar item
7275 0 if X/Y is on the same item that was highlighted before.
7276 1 otherwise. */
7277
7278 static int
7279 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
7280 struct frame *f;
7281 int x, y;
7282 struct glyph **glyph;
7283 int *hpos, *vpos, *prop_idx;
7284 {
7285 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7286 struct window *w = XWINDOW (f->tool_bar_window);
7287 int area;
7288
7289 /* Find the glyph under X/Y. */
7290 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
7291 if (*glyph == NULL)
7292 return -1;
7293
7294 /* Get the start of this tool-bar item's properties in
7295 f->tool_bar_items. */
7296 if (!tool_bar_item_info (f, *glyph, prop_idx))
7297 return -1;
7298
7299 /* Is mouse on the highlighted item? */
7300 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
7301 && *vpos >= dpyinfo->mouse_face_beg_row
7302 && *vpos <= dpyinfo->mouse_face_end_row
7303 && (*vpos > dpyinfo->mouse_face_beg_row
7304 || *hpos >= dpyinfo->mouse_face_beg_col)
7305 && (*vpos < dpyinfo->mouse_face_end_row
7306 || *hpos < dpyinfo->mouse_face_end_col
7307 || dpyinfo->mouse_face_past_end))
7308 return 0;
7309
7310 return 1;
7311 }
7312
7313
7314 /* Handle mouse button event on the tool-bar of frame F, at
7315 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
7316 or ButtonRelase. */
7317
7318 static void
7319 x_handle_tool_bar_click (f, button_event)
7320 struct frame *f;
7321 XButtonEvent *button_event;
7322 {
7323 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7324 struct window *w = XWINDOW (f->tool_bar_window);
7325 int hpos, vpos, prop_idx;
7326 struct glyph *glyph;
7327 Lisp_Object enabled_p;
7328 int x = button_event->x;
7329 int y = button_event->y;
7330
7331 /* If not on the highlighted tool-bar item, return. */
7332 frame_to_window_pixel_xy (w, &x, &y);
7333 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
7334 return;
7335
7336 /* If item is disabled, do nothing. */
7337 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7338 if (NILP (enabled_p))
7339 return;
7340
7341 if (button_event->type == ButtonPress)
7342 {
7343 /* Show item in pressed state. */
7344 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7345 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
7346 last_tool_bar_item = prop_idx;
7347 }
7348 else
7349 {
7350 Lisp_Object key, frame;
7351 struct input_event event;
7352
7353 /* Show item in released state. */
7354 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7355 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7356
7357 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
7358
7359 XSETFRAME (frame, f);
7360 event.kind = TOOL_BAR_EVENT;
7361 event.frame_or_window = frame;
7362 event.arg = frame;
7363 kbd_buffer_store_event (&event);
7364
7365 event.kind = TOOL_BAR_EVENT;
7366 event.frame_or_window = frame;
7367 event.arg = key;
7368 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7369 button_event->state);
7370 kbd_buffer_store_event (&event);
7371 last_tool_bar_item = -1;
7372 }
7373 }
7374
7375
7376 /* Possibly highlight a tool-bar item on frame F when mouse moves to
7377 tool-bar window-relative coordinates X/Y. Called from
7378 note_mouse_highlight. */
7379
7380 static void
7381 note_tool_bar_highlight (f, x, y)
7382 struct frame *f;
7383 int x, y;
7384 {
7385 Lisp_Object window = f->tool_bar_window;
7386 struct window *w = XWINDOW (window);
7387 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7388 int hpos, vpos;
7389 struct glyph *glyph;
7390 struct glyph_row *row;
7391 int i;
7392 Lisp_Object enabled_p;
7393 int prop_idx;
7394 enum draw_glyphs_face draw;
7395 int mouse_down_p, rc;
7396
7397 /* Function note_mouse_highlight is called with negative x(y
7398 values when mouse moves outside of the frame. */
7399 if (x <= 0 || y <= 0)
7400 {
7401 clear_mouse_face (dpyinfo);
7402 return;
7403 }
7404
7405 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7406 if (rc < 0)
7407 {
7408 /* Not on tool-bar item. */
7409 clear_mouse_face (dpyinfo);
7410 return;
7411 }
7412 else if (rc == 0)
7413 goto set_help_echo;
7414
7415 clear_mouse_face (dpyinfo);
7416
7417 /* Mouse is down, but on different tool-bar item? */
7418 mouse_down_p = (dpyinfo->grabbed
7419 && f == last_mouse_frame
7420 && FRAME_LIVE_P (f));
7421 if (mouse_down_p
7422 && last_tool_bar_item != prop_idx)
7423 return;
7424
7425 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7426 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7427
7428 /* If tool-bar item is not enabled, don't highlight it. */
7429 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7430 if (!NILP (enabled_p))
7431 {
7432 /* Compute the x-position of the glyph. In front and past the
7433 image is a space. We include this is the highlighted area. */
7434 row = MATRIX_ROW (w->current_matrix, vpos);
7435 for (i = x = 0; i < hpos; ++i)
7436 x += row->glyphs[TEXT_AREA][i].pixel_width;
7437
7438 /* Record this as the current active region. */
7439 dpyinfo->mouse_face_beg_col = hpos;
7440 dpyinfo->mouse_face_beg_row = vpos;
7441 dpyinfo->mouse_face_beg_x = x;
7442 dpyinfo->mouse_face_beg_y = row->y;
7443 dpyinfo->mouse_face_past_end = 0;
7444
7445 dpyinfo->mouse_face_end_col = hpos + 1;
7446 dpyinfo->mouse_face_end_row = vpos;
7447 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7448 dpyinfo->mouse_face_end_y = row->y;
7449 dpyinfo->mouse_face_window = window;
7450 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7451
7452 /* Display it as active. */
7453 show_mouse_face (dpyinfo, draw);
7454 dpyinfo->mouse_face_image_state = draw;
7455 }
7456
7457 set_help_echo:
7458
7459 /* Set help_echo to a help string.to display for this tool-bar item.
7460 XTread_socket does the rest. */
7461 help_echo_object = help_echo_window = Qnil;
7462 help_echo_pos = -1;
7463 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
7464 if (NILP (help_echo))
7465 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
7466 }
7467
7468
7469 \f
7470 /* Find the glyph matrix position of buffer position CHARPOS in window
7471 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7472 current glyphs must be up to date. If CHARPOS is above window
7473 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
7474 of last line in W. In the row containing CHARPOS, stop before glyphs
7475 having STOP as object. */
7476
7477 #if 0 /* This is a version of fast_find_position that's more correct
7478 in the presence of hscrolling, for example. I didn't install
7479 it right away because the problem fixed is minor, it failed
7480 in 20.x as well, and I think it's too risky to install
7481 so near the release of 21.1. 2001-09-25 gerd. */
7482
7483 static int
7484 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
7485 struct window *w;
7486 int charpos;
7487 int *hpos, *vpos, *x, *y;
7488 Lisp_Object stop;
7489 {
7490 struct glyph_row *row, *first;
7491 struct glyph *glyph, *end;
7492 int i, past_end = 0;
7493
7494 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7495 row = row_containing_pos (w, charpos, first, NULL, 0);
7496 if (row == NULL)
7497 {
7498 if (charpos < MATRIX_ROW_START_CHARPOS (first))
7499 {
7500 *x = *y = *hpos = *vpos = 0;
7501 return 0;
7502 }
7503 else
7504 {
7505 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
7506 past_end = 1;
7507 }
7508 }
7509
7510 *x = row->x;
7511 *y = row->y;
7512 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7513
7514 glyph = row->glyphs[TEXT_AREA];
7515 end = glyph + row->used[TEXT_AREA];
7516
7517 /* Skip over glyphs not having an object at the start of the row.
7518 These are special glyphs like truncation marks on terminal
7519 frames. */
7520 if (row->displays_text_p)
7521 while (glyph < end
7522 && INTEGERP (glyph->object)
7523 && !EQ (stop, glyph->object)
7524 && glyph->charpos < 0)
7525 {
7526 *x += glyph->pixel_width;
7527 ++glyph;
7528 }
7529
7530 while (glyph < end
7531 && !INTEGERP (glyph->object)
7532 && !EQ (stop, glyph->object)
7533 && (!BUFFERP (glyph->object)
7534 || glyph->charpos < charpos))
7535 {
7536 *x += glyph->pixel_width;
7537 ++glyph;
7538 }
7539
7540 *hpos = glyph - row->glyphs[TEXT_AREA];
7541 return past_end;
7542 }
7543
7544 #else /* not 0 */
7545
7546 static int
7547 fast_find_position (w, pos, hpos, vpos, x, y, stop)
7548 struct window *w;
7549 int pos;
7550 int *hpos, *vpos, *x, *y;
7551 Lisp_Object stop;
7552 {
7553 int i;
7554 int lastcol;
7555 int maybe_next_line_p = 0;
7556 int line_start_position;
7557 int yb = window_text_bottom_y (w);
7558 struct glyph_row *row, *best_row;
7559 int row_vpos, best_row_vpos;
7560 int current_x;
7561
7562 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7563 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7564
7565 while (row->y < yb)
7566 {
7567 if (row->used[TEXT_AREA])
7568 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7569 else
7570 line_start_position = 0;
7571
7572 if (line_start_position > pos)
7573 break;
7574 /* If the position sought is the end of the buffer,
7575 don't include the blank lines at the bottom of the window. */
7576 else if (line_start_position == pos
7577 && pos == BUF_ZV (XBUFFER (w->buffer)))
7578 {
7579 maybe_next_line_p = 1;
7580 break;
7581 }
7582 else if (line_start_position > 0)
7583 {
7584 best_row = row;
7585 best_row_vpos = row_vpos;
7586 }
7587
7588 if (row->y + row->height >= yb)
7589 break;
7590
7591 ++row;
7592 ++row_vpos;
7593 }
7594
7595 /* Find the right column within BEST_ROW. */
7596 lastcol = 0;
7597 current_x = best_row->x;
7598 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
7599 {
7600 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7601 int charpos = glyph->charpos;
7602
7603 if (BUFFERP (glyph->object))
7604 {
7605 if (charpos == pos)
7606 {
7607 *hpos = i;
7608 *vpos = best_row_vpos;
7609 *x = current_x;
7610 *y = best_row->y;
7611 return 1;
7612 }
7613 else if (charpos > pos)
7614 break;
7615 }
7616 else if (EQ (glyph->object, stop))
7617 break;
7618
7619 if (charpos > 0)
7620 lastcol = i;
7621 current_x += glyph->pixel_width;
7622 }
7623
7624 /* If we're looking for the end of the buffer,
7625 and we didn't find it in the line we scanned,
7626 use the start of the following line. */
7627 if (maybe_next_line_p)
7628 {
7629 ++best_row;
7630 ++best_row_vpos;
7631 lastcol = 0;
7632 current_x = best_row->x;
7633 }
7634
7635 *vpos = best_row_vpos;
7636 *hpos = lastcol + 1;
7637 *x = current_x;
7638 *y = best_row->y;
7639 return 0;
7640 }
7641
7642 #endif /* not 0 */
7643
7644
7645 /* Find the position of the the glyph for position POS in OBJECT in
7646 window W's current matrix, and return in *X/*Y the pixel
7647 coordinates, and return in *HPOS/*VPOS the column/row of the glyph.
7648
7649 RIGHT_P non-zero means return the position of the right edge of the
7650 glyph, RIGHT_P zero means return the left edge position.
7651
7652 If no glyph for POS exists in the matrix, return the position of
7653 the glyph with the next smaller position that is in the matrix, if
7654 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7655 exists in the matrix, return the position of the glyph with the
7656 next larger position in OBJECT.
7657
7658 Value is non-zero if a glyph was found. */
7659
7660 static int
7661 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7662 struct window *w;
7663 int pos;
7664 Lisp_Object object;
7665 int *hpos, *vpos, *x, *y;
7666 int right_p;
7667 {
7668 int yb = window_text_bottom_y (w);
7669 struct glyph_row *r;
7670 struct glyph *best_glyph = NULL;
7671 struct glyph_row *best_row = NULL;
7672 int best_x = 0;
7673
7674 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7675 r->enabled_p && r->y < yb;
7676 ++r)
7677 {
7678 struct glyph *g = r->glyphs[TEXT_AREA];
7679 struct glyph *e = g + r->used[TEXT_AREA];
7680 int gx;
7681
7682 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7683 if (EQ (g->object, object))
7684 {
7685 if (g->charpos == pos)
7686 {
7687 best_glyph = g;
7688 best_x = gx;
7689 best_row = r;
7690 goto found;
7691 }
7692 else if (best_glyph == NULL
7693 || ((abs (g->charpos - pos)
7694 < abs (best_glyph->charpos - pos))
7695 && (right_p
7696 ? g->charpos < pos
7697 : g->charpos > pos)))
7698 {
7699 best_glyph = g;
7700 best_x = gx;
7701 best_row = r;
7702 }
7703 }
7704 }
7705
7706 found:
7707
7708 if (best_glyph)
7709 {
7710 *x = best_x;
7711 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7712
7713 if (right_p)
7714 {
7715 *x += best_glyph->pixel_width;
7716 ++*hpos;
7717 }
7718
7719 *y = best_row->y;
7720 *vpos = best_row - w->current_matrix->rows;
7721 }
7722
7723 return best_glyph != NULL;
7724 }
7725
7726
7727 /* Display the active region described by mouse_face_*
7728 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7729
7730 static void
7731 show_mouse_face (dpyinfo, draw)
7732 struct x_display_info *dpyinfo;
7733 enum draw_glyphs_face draw;
7734 {
7735 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7736 struct frame *f = XFRAME (WINDOW_FRAME (w));
7737
7738 if (/* If window is in the process of being destroyed, don't bother
7739 to do anything. */
7740 w->current_matrix != NULL
7741 /* Recognize when we are called to operate on rows that don't exist
7742 anymore. This can happen when a window is split. */
7743 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
7744 {
7745 int phys_cursor_on_p = w->phys_cursor_on_p;
7746 struct glyph_row *row, *first, *last;
7747
7748 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
7749 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
7750
7751 for (row = first; row <= last && row->enabled_p; ++row)
7752 {
7753 int start_hpos, end_hpos, start_x;
7754
7755 /* For all but the first row, the highlight starts at column 0. */
7756 if (row == first)
7757 {
7758 start_hpos = dpyinfo->mouse_face_beg_col;
7759 start_x = dpyinfo->mouse_face_beg_x;
7760 }
7761 else
7762 {
7763 start_hpos = 0;
7764 start_x = 0;
7765 }
7766
7767 if (row == last)
7768 end_hpos = dpyinfo->mouse_face_end_col;
7769 else
7770 end_hpos = row->used[TEXT_AREA];
7771
7772 if (end_hpos > start_hpos)
7773 {
7774 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7775 start_hpos, end_hpos, draw, 0);
7776
7777 row->mouse_face_p = draw == DRAW_MOUSE_FACE || DRAW_IMAGE_RAISED;
7778 }
7779 }
7780
7781 /* When we've written over the cursor, arrange for it to
7782 be displayed again. */
7783 if (phys_cursor_on_p && !w->phys_cursor_on_p)
7784 x_display_cursor (w, 1,
7785 w->phys_cursor.hpos, w->phys_cursor.vpos,
7786 w->phys_cursor.x, w->phys_cursor.y);
7787 }
7788
7789 /* Change the mouse cursor. */
7790 if (draw == DRAW_NORMAL_TEXT)
7791 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7792 f->output_data.x->text_cursor);
7793 else if (draw == DRAW_MOUSE_FACE)
7794 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7795 f->output_data.x->cross_cursor);
7796 else
7797 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7798 f->output_data.x->nontext_cursor);
7799 }
7800
7801 /* Clear out the mouse-highlighted active region.
7802 Redraw it un-highlighted first. Value is non-zero if mouse
7803 face was actually drawn unhighlighted. */
7804
7805 static int
7806 clear_mouse_face (dpyinfo)
7807 struct x_display_info *dpyinfo;
7808 {
7809 int cleared = 0;
7810
7811 if (!NILP (dpyinfo->mouse_face_window))
7812 {
7813 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7814 cleared = 1;
7815 }
7816
7817 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7818 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7819 dpyinfo->mouse_face_window = Qnil;
7820 dpyinfo->mouse_face_overlay = Qnil;
7821 return cleared;
7822 }
7823
7824
7825 /* Clear any mouse-face on window W. This function is part of the
7826 redisplay interface, and is called from try_window_id and similar
7827 functions to ensure the mouse-highlight is off. */
7828
7829 static void
7830 x_clear_mouse_face (w)
7831 struct window *w;
7832 {
7833 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7834 Lisp_Object window;
7835
7836 BLOCK_INPUT;
7837 XSETWINDOW (window, w);
7838 if (EQ (window, dpyinfo->mouse_face_window))
7839 clear_mouse_face (dpyinfo);
7840 UNBLOCK_INPUT;
7841 }
7842
7843
7844 /* Just discard the mouse face information for frame F, if any.
7845 This is used when the size of F is changed. */
7846
7847 void
7848 cancel_mouse_face (f)
7849 FRAME_PTR f;
7850 {
7851 Lisp_Object window;
7852 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7853
7854 window = dpyinfo->mouse_face_window;
7855 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7856 {
7857 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7858 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7859 dpyinfo->mouse_face_window = Qnil;
7860 }
7861 }
7862
7863 \f
7864 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
7865
7866
7867 /* Try to determine frame pixel position and size of the glyph under
7868 frame pixel coordinates X/Y on frame F . Return the position and
7869 size in *RECT. Value is non-zero if we could compute these
7870 values. */
7871
7872 static int
7873 glyph_rect (f, x, y, rect)
7874 struct frame *f;
7875 int x, y;
7876 XRectangle *rect;
7877 {
7878 Lisp_Object window;
7879 int part, found = 0;
7880
7881 window = window_from_coordinates (f, x, y, &part, 0);
7882 if (!NILP (window))
7883 {
7884 struct window *w = XWINDOW (window);
7885 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7886 struct glyph_row *end = r + w->current_matrix->nrows - 1;
7887
7888 frame_to_window_pixel_xy (w, &x, &y);
7889
7890 for (; !found && r < end && r->enabled_p; ++r)
7891 if (r->y >= y)
7892 {
7893 struct glyph *g = r->glyphs[TEXT_AREA];
7894 struct glyph *end = g + r->used[TEXT_AREA];
7895 int gx;
7896
7897 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
7898 if (gx >= x)
7899 {
7900 rect->width = g->pixel_width;
7901 rect->height = r->height;
7902 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
7903 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
7904 found = 1;
7905 }
7906 }
7907 }
7908
7909 return found;
7910 }
7911
7912
7913 /* Return the current position of the mouse.
7914 *FP should be a frame which indicates which display to ask about.
7915
7916 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
7917 and *PART to the frame, window, and scroll bar part that the mouse
7918 is over. Set *X and *Y to the portion and whole of the mouse's
7919 position on the scroll bar.
7920
7921 If the mouse movement started elsewhere, set *FP to the frame the
7922 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
7923 the mouse is over.
7924
7925 Set *TIME to the server time-stamp for the time at which the mouse
7926 was at this position.
7927
7928 Don't store anything if we don't have a valid set of values to report.
7929
7930 This clears the mouse_moved flag, so we can wait for the next mouse
7931 movement. */
7932
7933 static void
7934 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7935 FRAME_PTR *fp;
7936 int insist;
7937 Lisp_Object *bar_window;
7938 enum scroll_bar_part *part;
7939 Lisp_Object *x, *y;
7940 unsigned long *time;
7941 {
7942 FRAME_PTR f1;
7943
7944 BLOCK_INPUT;
7945
7946 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7947 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7948 else
7949 {
7950 Window root;
7951 int root_x, root_y;
7952
7953 Window dummy_window;
7954 int dummy;
7955
7956 Lisp_Object frame, tail;
7957
7958 /* Clear the mouse-moved flag for every frame on this display. */
7959 FOR_EACH_FRAME (tail, frame)
7960 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7961 XFRAME (frame)->mouse_moved = 0;
7962
7963 last_mouse_scroll_bar = Qnil;
7964
7965 /* Figure out which root window we're on. */
7966 XQueryPointer (FRAME_X_DISPLAY (*fp),
7967 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
7968
7969 /* The root window which contains the pointer. */
7970 &root,
7971
7972 /* Trash which we can't trust if the pointer is on
7973 a different screen. */
7974 &dummy_window,
7975
7976 /* The position on that root window. */
7977 &root_x, &root_y,
7978
7979 /* More trash we can't trust. */
7980 &dummy, &dummy,
7981
7982 /* Modifier keys and pointer buttons, about which
7983 we don't care. */
7984 (unsigned int *) &dummy);
7985
7986 /* Now we have a position on the root; find the innermost window
7987 containing the pointer. */
7988 {
7989 Window win, child;
7990 int win_x, win_y;
7991 int parent_x = 0, parent_y = 0;
7992 int count;
7993
7994 win = root;
7995
7996 /* XTranslateCoordinates can get errors if the window
7997 structure is changing at the same time this function
7998 is running. So at least we must not crash from them. */
7999
8000 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
8001
8002 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
8003 && FRAME_LIVE_P (last_mouse_frame))
8004 {
8005 /* If mouse was grabbed on a frame, give coords for that frame
8006 even if the mouse is now outside it. */
8007 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
8008
8009 /* From-window, to-window. */
8010 root, FRAME_X_WINDOW (last_mouse_frame),
8011
8012 /* From-position, to-position. */
8013 root_x, root_y, &win_x, &win_y,
8014
8015 /* Child of win. */
8016 &child);
8017 f1 = last_mouse_frame;
8018 }
8019 else
8020 {
8021 while (1)
8022 {
8023 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
8024
8025 /* From-window, to-window. */
8026 root, win,
8027
8028 /* From-position, to-position. */
8029 root_x, root_y, &win_x, &win_y,
8030
8031 /* Child of win. */
8032 &child);
8033
8034 if (child == None || child == win)
8035 break;
8036
8037 win = child;
8038 parent_x = win_x;
8039 parent_y = win_y;
8040 }
8041
8042 /* Now we know that:
8043 win is the innermost window containing the pointer
8044 (XTC says it has no child containing the pointer),
8045 win_x and win_y are the pointer's position in it
8046 (XTC did this the last time through), and
8047 parent_x and parent_y are the pointer's position in win's parent.
8048 (They are what win_x and win_y were when win was child.
8049 If win is the root window, it has no parent, and
8050 parent_{x,y} are invalid, but that's okay, because we'll
8051 never use them in that case.) */
8052
8053 /* Is win one of our frames? */
8054 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
8055
8056 #ifdef USE_X_TOOLKIT
8057 /* If we end up with the menu bar window, say it's not
8058 on the frame. */
8059 if (f1 != NULL
8060 && f1->output_data.x->menubar_widget
8061 && win == XtWindow (f1->output_data.x->menubar_widget))
8062 f1 = NULL;
8063 #endif /* USE_X_TOOLKIT */
8064 }
8065
8066 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
8067 f1 = 0;
8068
8069 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
8070
8071 /* If not, is it one of our scroll bars? */
8072 if (! f1)
8073 {
8074 struct scroll_bar *bar = x_window_to_scroll_bar (win);
8075
8076 if (bar)
8077 {
8078 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8079 win_x = parent_x;
8080 win_y = parent_y;
8081 }
8082 }
8083
8084 if (f1 == 0 && insist > 0)
8085 f1 = SELECTED_FRAME ();
8086
8087 if (f1)
8088 {
8089 /* Ok, we found a frame. Store all the values.
8090 last_mouse_glyph is a rectangle used to reduce the
8091 generation of mouse events. To not miss any motion
8092 events, we must divide the frame into rectangles of the
8093 size of the smallest character that could be displayed
8094 on it, i.e. into the same rectangles that matrices on
8095 the frame are divided into. */
8096
8097 int width, height, gx, gy;
8098 XRectangle rect;
8099
8100 if (glyph_rect (f1, win_x, win_y, &rect))
8101 last_mouse_glyph = rect;
8102 else
8103 {
8104 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
8105 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
8106 gx = win_x;
8107 gy = win_y;
8108
8109 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
8110 round down even for negative values. */
8111 if (gx < 0)
8112 gx -= width - 1;
8113 if (gy < 0)
8114 gy -= height - 1;
8115 gx = (gx + width - 1) / width * width;
8116 gy = (gy + height - 1) / height * height;
8117
8118 last_mouse_glyph.width = width;
8119 last_mouse_glyph.height = height;
8120 last_mouse_glyph.x = gx;
8121 last_mouse_glyph.y = gy;
8122 }
8123
8124 *bar_window = Qnil;
8125 *part = 0;
8126 *fp = f1;
8127 XSETINT (*x, win_x);
8128 XSETINT (*y, win_y);
8129 *time = last_mouse_movement_time;
8130 }
8131 }
8132 }
8133
8134 UNBLOCK_INPUT;
8135 }
8136
8137
8138 #ifdef USE_X_TOOLKIT
8139
8140 /* Atimer callback function for TIMER. Called every 0.1s to process
8141 Xt timeouts, if needed. We must avoid calling XtAppPending as
8142 much as possible because that function does an implicit XFlush
8143 that slows us down. */
8144
8145 static void
8146 x_process_timeouts (timer)
8147 struct atimer *timer;
8148 {
8149 if (toolkit_scroll_bar_interaction || popup_activated_flag)
8150 {
8151 BLOCK_INPUT;
8152 while (XtAppPending (Xt_app_con) & XtIMTimer)
8153 XtAppProcessEvent (Xt_app_con, XtIMTimer);
8154 UNBLOCK_INPUT;
8155 }
8156 }
8157
8158 #endif /* USE_X_TOOLKIT */
8159
8160 \f
8161 /* Scroll bar support. */
8162
8163 /* Given an X window ID, find the struct scroll_bar which manages it.
8164 This can be called in GC, so we have to make sure to strip off mark
8165 bits. */
8166
8167 static struct scroll_bar *
8168 x_window_to_scroll_bar (window_id)
8169 Window window_id;
8170 {
8171 Lisp_Object tail;
8172
8173 for (tail = Vframe_list;
8174 XGCTYPE (tail) == Lisp_Cons;
8175 tail = XCDR (tail))
8176 {
8177 Lisp_Object frame, bar, condemned;
8178
8179 frame = XCAR (tail);
8180 /* All elements of Vframe_list should be frames. */
8181 if (! GC_FRAMEP (frame))
8182 abort ();
8183
8184 /* Scan this frame's scroll bar list for a scroll bar with the
8185 right window ID. */
8186 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
8187 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
8188 /* This trick allows us to search both the ordinary and
8189 condemned scroll bar lists with one loop. */
8190 ! GC_NILP (bar) || (bar = condemned,
8191 condemned = Qnil,
8192 ! GC_NILP (bar));
8193 bar = XSCROLL_BAR (bar)->next)
8194 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
8195 return XSCROLL_BAR (bar);
8196 }
8197
8198 return 0;
8199 }
8200
8201
8202 #if defined USE_LUCID
8203
8204 /* Return the Lucid menu bar WINDOW is part of. Return null
8205 if WINDOW is not part of a menu bar. */
8206
8207 static Widget
8208 x_window_to_menu_bar (window)
8209 Window window;
8210 {
8211 Lisp_Object tail;
8212
8213 for (tail = Vframe_list;
8214 XGCTYPE (tail) == Lisp_Cons;
8215 tail = XCDR (tail))
8216 {
8217 Lisp_Object frame = XCAR (tail);
8218 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
8219
8220 if (menu_bar && xlwmenu_window_p (menu_bar, window))
8221 return menu_bar;
8222 }
8223
8224 return NULL;
8225 }
8226
8227 #endif /* USE_LUCID */
8228
8229 \f
8230 /************************************************************************
8231 Toolkit scroll bars
8232 ************************************************************************/
8233
8234 #ifdef USE_TOOLKIT_SCROLL_BARS
8235
8236 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
8237 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
8238 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
8239 struct scroll_bar *));
8240 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
8241 int, int, int));
8242
8243
8244 /* Id of action hook installed for scroll bars. */
8245
8246 static XtActionHookId action_hook_id;
8247
8248 /* Lisp window being scrolled. Set when starting to interact with
8249 a toolkit scroll bar, reset to nil when ending the interaction. */
8250
8251 static Lisp_Object window_being_scrolled;
8252
8253 /* Last scroll bar part sent in xm_scroll_callback. */
8254
8255 static int last_scroll_bar_part;
8256
8257 /* Whether this is an Xaw with arrow-scrollbars. This should imply
8258 that movements of 1/20 of the screen size are mapped to up/down. */
8259
8260 static Boolean xaw3d_arrow_scroll;
8261
8262 /* Whether the drag scrolling maintains the mouse at the top of the
8263 thumb. If not, resizing the thumb needs to be done more carefully
8264 to avoid jerkyness. */
8265
8266 static Boolean xaw3d_pick_top;
8267
8268
8269 /* Action hook installed via XtAppAddActionHook when toolkit scroll
8270 bars are used.. The hook is responsible for detecting when
8271 the user ends an interaction with the scroll bar, and generates
8272 a `end-scroll' scroll_bar_click' event if so. */
8273
8274 static void
8275 xt_action_hook (widget, client_data, action_name, event, params,
8276 num_params)
8277 Widget widget;
8278 XtPointer client_data;
8279 String action_name;
8280 XEvent *event;
8281 String *params;
8282 Cardinal *num_params;
8283 {
8284 int scroll_bar_p;
8285 char *end_action;
8286
8287 #ifdef USE_MOTIF
8288 scroll_bar_p = XmIsScrollBar (widget);
8289 end_action = "Release";
8290 #else /* !USE_MOTIF i.e. use Xaw */
8291 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
8292 end_action = "EndScroll";
8293 #endif /* USE_MOTIF */
8294
8295 if (scroll_bar_p
8296 && strcmp (action_name, end_action) == 0
8297 && WINDOWP (window_being_scrolled))
8298 {
8299 struct window *w;
8300
8301 x_send_scroll_bar_event (window_being_scrolled,
8302 scroll_bar_end_scroll, 0, 0);
8303 w = XWINDOW (window_being_scrolled);
8304 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
8305 window_being_scrolled = Qnil;
8306 last_scroll_bar_part = -1;
8307
8308 /* Xt timeouts no longer needed. */
8309 toolkit_scroll_bar_interaction = 0;
8310 }
8311 }
8312
8313 /* A vector of windows used for communication between
8314 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
8315
8316 static struct window **scroll_bar_windows;
8317 static int scroll_bar_windows_size;
8318
8319
8320 /* Send a client message with message type Xatom_Scrollbar for a
8321 scroll action to the frame of WINDOW. PART is a value identifying
8322 the part of the scroll bar that was clicked on. PORTION is the
8323 amount to scroll of a whole of WHOLE. */
8324
8325 static void
8326 x_send_scroll_bar_event (window, part, portion, whole)
8327 Lisp_Object window;
8328 int part, portion, whole;
8329 {
8330 XEvent event;
8331 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
8332 struct window *w = XWINDOW (window);
8333 struct frame *f = XFRAME (w->frame);
8334 int i;
8335
8336 BLOCK_INPUT;
8337
8338 /* Construct a ClientMessage event to send to the frame. */
8339 ev->type = ClientMessage;
8340 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
8341 ev->display = FRAME_X_DISPLAY (f);
8342 ev->window = FRAME_X_WINDOW (f);
8343 ev->format = 32;
8344
8345 /* We can only transfer 32 bits in the XClientMessageEvent, which is
8346 not enough to store a pointer or Lisp_Object on a 64 bit system.
8347 So, store the window in scroll_bar_windows and pass the index
8348 into that array in the event. */
8349 for (i = 0; i < scroll_bar_windows_size; ++i)
8350 if (scroll_bar_windows[i] == NULL)
8351 break;
8352
8353 if (i == scroll_bar_windows_size)
8354 {
8355 int new_size = max (10, 2 * scroll_bar_windows_size);
8356 size_t nbytes = new_size * sizeof *scroll_bar_windows;
8357 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
8358
8359 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
8360 nbytes);
8361 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
8362 scroll_bar_windows_size = new_size;
8363 }
8364
8365 scroll_bar_windows[i] = w;
8366 ev->data.l[0] = (long) i;
8367 ev->data.l[1] = (long) part;
8368 ev->data.l[2] = (long) 0;
8369 ev->data.l[3] = (long) portion;
8370 ev->data.l[4] = (long) whole;
8371
8372 /* Make Xt timeouts work while the scroll bar is active. */
8373 toolkit_scroll_bar_interaction = 1;
8374
8375 /* Setting the event mask to zero means that the message will
8376 be sent to the client that created the window, and if that
8377 window no longer exists, no event will be sent. */
8378 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
8379 UNBLOCK_INPUT;
8380 }
8381
8382
8383 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
8384 in *IEVENT. */
8385
8386 static void
8387 x_scroll_bar_to_input_event (event, ievent)
8388 XEvent *event;
8389 struct input_event *ievent;
8390 {
8391 XClientMessageEvent *ev = (XClientMessageEvent *) event;
8392 Lisp_Object window;
8393 struct frame *f;
8394 struct window *w;
8395
8396 w = scroll_bar_windows[ev->data.l[0]];
8397 scroll_bar_windows[ev->data.l[0]] = NULL;
8398
8399 XSETWINDOW (window, w);
8400 f = XFRAME (w->frame);
8401
8402 ievent->kind = scroll_bar_click;
8403 ievent->frame_or_window = window;
8404 ievent->arg = Qnil;
8405 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
8406 ievent->part = ev->data.l[1];
8407 ievent->code = ev->data.l[2];
8408 ievent->x = make_number ((int) ev->data.l[3]);
8409 ievent->y = make_number ((int) ev->data.l[4]);
8410 ievent->modifiers = 0;
8411 }
8412
8413
8414 #ifdef USE_MOTIF
8415
8416 /* Minimum and maximum values used for Motif scroll bars. */
8417
8418 #define XM_SB_MIN 1
8419 #define XM_SB_MAX 10000000
8420 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
8421
8422
8423 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
8424 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
8425 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
8426
8427 static void
8428 xm_scroll_callback (widget, client_data, call_data)
8429 Widget widget;
8430 XtPointer client_data, call_data;
8431 {
8432 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8433 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
8434 int part = -1, whole = 0, portion = 0;
8435
8436 switch (cs->reason)
8437 {
8438 case XmCR_DECREMENT:
8439 bar->dragging = Qnil;
8440 part = scroll_bar_up_arrow;
8441 break;
8442
8443 case XmCR_INCREMENT:
8444 bar->dragging = Qnil;
8445 part = scroll_bar_down_arrow;
8446 break;
8447
8448 case XmCR_PAGE_DECREMENT:
8449 bar->dragging = Qnil;
8450 part = scroll_bar_above_handle;
8451 break;
8452
8453 case XmCR_PAGE_INCREMENT:
8454 bar->dragging = Qnil;
8455 part = scroll_bar_below_handle;
8456 break;
8457
8458 case XmCR_TO_TOP:
8459 bar->dragging = Qnil;
8460 part = scroll_bar_to_top;
8461 break;
8462
8463 case XmCR_TO_BOTTOM:
8464 bar->dragging = Qnil;
8465 part = scroll_bar_to_bottom;
8466 break;
8467
8468 case XmCR_DRAG:
8469 {
8470 int slider_size;
8471 int dragging_down_p = (INTEGERP (bar->dragging)
8472 && XINT (bar->dragging) <= cs->value);
8473
8474 /* Get the slider size. */
8475 BLOCK_INPUT;
8476 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
8477 UNBLOCK_INPUT;
8478
8479 /* At the max position of the scroll bar, do a line-wise
8480 movement. Without doing anything, we would be called with
8481 the same cs->value again and again. If we want to make
8482 sure that we can reach the end of the buffer, we have to do
8483 something.
8484
8485 Implementation note: setting bar->dragging always to
8486 cs->value gives a smoother movement at the max position.
8487 Setting it to nil when doing line-wise movement gives
8488 a better slider behavior. */
8489
8490 if (cs->value + slider_size == XM_SB_MAX
8491 || (dragging_down_p
8492 && last_scroll_bar_part == scroll_bar_down_arrow))
8493 {
8494 part = scroll_bar_down_arrow;
8495 bar->dragging = Qnil;
8496 }
8497 else
8498 {
8499 whole = XM_SB_RANGE;
8500 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
8501 part = scroll_bar_handle;
8502 bar->dragging = make_number (cs->value);
8503 }
8504 }
8505 break;
8506
8507 case XmCR_VALUE_CHANGED:
8508 break;
8509 };
8510
8511 if (part >= 0)
8512 {
8513 window_being_scrolled = bar->window;
8514 last_scroll_bar_part = part;
8515 x_send_scroll_bar_event (bar->window, part, portion, whole);
8516 }
8517 }
8518
8519
8520 #else /* !USE_MOTIF, i.e. Xaw. */
8521
8522
8523 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
8524 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8525 scroll bar struct. CALL_DATA is a pointer to a float saying where
8526 the thumb is. */
8527
8528 static void
8529 xaw_jump_callback (widget, client_data, call_data)
8530 Widget widget;
8531 XtPointer client_data, call_data;
8532 {
8533 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8534 float top = *(float *) call_data;
8535 float shown;
8536 int whole, portion, height;
8537 int part;
8538
8539 /* Get the size of the thumb, a value between 0 and 1. */
8540 BLOCK_INPUT;
8541 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
8542 UNBLOCK_INPUT;
8543
8544 whole = 10000000;
8545 portion = shown < 1 ? top * whole : 0;
8546
8547 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8548 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8549 the bottom, so we force the scrolling whenever we see that we're
8550 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8551 we try to ensure that we always stay two pixels away from the
8552 bottom). */
8553 part = scroll_bar_down_arrow;
8554 else
8555 part = scroll_bar_handle;
8556
8557 window_being_scrolled = bar->window;
8558 bar->dragging = make_number (portion);
8559 last_scroll_bar_part = part;
8560 x_send_scroll_bar_event (bar->window, part, portion, whole);
8561 }
8562
8563
8564 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
8565 i.e. line or page up or down. WIDGET is the Xaw scroll bar
8566 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8567 the scroll bar. CALL_DATA is an integer specifying the action that
8568 has taken place. It's magnitude is in the range 0..height of the
8569 scroll bar. Negative values mean scroll towards buffer start.
8570 Values < height of scroll bar mean line-wise movement. */
8571
8572 static void
8573 xaw_scroll_callback (widget, client_data, call_data)
8574 Widget widget;
8575 XtPointer client_data, call_data;
8576 {
8577 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8578 int position = (int) call_data;
8579 Dimension height;
8580 int part;
8581
8582 /* Get the height of the scroll bar. */
8583 BLOCK_INPUT;
8584 XtVaGetValues (widget, XtNheight, &height, NULL);
8585 UNBLOCK_INPUT;
8586
8587 if (abs (position) >= height)
8588 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8589
8590 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8591 it maps line-movement to call_data = max(5, height/20). */
8592 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8593 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
8594 else
8595 part = scroll_bar_move_ratio;
8596
8597 window_being_scrolled = bar->window;
8598 bar->dragging = Qnil;
8599 last_scroll_bar_part = part;
8600 x_send_scroll_bar_event (bar->window, part, position, height);
8601 }
8602
8603
8604 #endif /* not USE_MOTIF */
8605
8606
8607 /* Create the widget for scroll bar BAR on frame F. Record the widget
8608 and X window of the scroll bar in BAR. */
8609
8610 static void
8611 x_create_toolkit_scroll_bar (f, bar)
8612 struct frame *f;
8613 struct scroll_bar *bar;
8614 {
8615 Window xwindow;
8616 Widget widget;
8617 Arg av[20];
8618 int ac = 0;
8619 char *scroll_bar_name = "verticalScrollBar";
8620 unsigned long pixel;
8621
8622 BLOCK_INPUT;
8623
8624 #ifdef USE_MOTIF
8625 /* Set resources. Create the widget. */
8626 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8627 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8628 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8629 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8630 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8631 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8632 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8633
8634 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8635 if (pixel != -1)
8636 {
8637 XtSetArg (av[ac], XmNforeground, pixel);
8638 ++ac;
8639 }
8640
8641 pixel = f->output_data.x->scroll_bar_background_pixel;
8642 if (pixel != -1)
8643 {
8644 XtSetArg (av[ac], XmNbackground, pixel);
8645 ++ac;
8646 }
8647
8648 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8649 scroll_bar_name, av, ac);
8650
8651 /* Add one callback for everything that can happen. */
8652 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8653 (XtPointer) bar);
8654 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8655 (XtPointer) bar);
8656 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8657 (XtPointer) bar);
8658 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8659 (XtPointer) bar);
8660 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8661 (XtPointer) bar);
8662 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8663 (XtPointer) bar);
8664 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8665 (XtPointer) bar);
8666
8667 /* Realize the widget. Only after that is the X window created. */
8668 XtRealizeWidget (widget);
8669
8670 /* Set the cursor to an arrow. I didn't find a resource to do that.
8671 And I'm wondering why it hasn't an arrow cursor by default. */
8672 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8673 f->output_data.x->nontext_cursor);
8674
8675 #else /* !USE_MOTIF i.e. use Xaw */
8676
8677 /* Set resources. Create the widget. The background of the
8678 Xaw3d scroll bar widget is a little bit light for my taste.
8679 We don't alter it here to let users change it according
8680 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8681 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8682 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
8683 /* For smoother scrolling with Xaw3d -sm */
8684 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8685
8686 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8687 if (pixel != -1)
8688 {
8689 XtSetArg (av[ac], XtNforeground, pixel);
8690 ++ac;
8691 }
8692
8693 pixel = f->output_data.x->scroll_bar_background_pixel;
8694 if (pixel != -1)
8695 {
8696 XtSetArg (av[ac], XtNbackground, pixel);
8697 ++ac;
8698 }
8699
8700 /* Top/bottom shadow colors. */
8701
8702 /* Allocate them, if necessary. */
8703 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
8704 {
8705 pixel = f->output_data.x->scroll_bar_background_pixel;
8706 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8707 &pixel, 1.2, 0x8000))
8708 pixel = -1;
8709 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
8710 }
8711 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8712 {
8713 pixel = f->output_data.x->scroll_bar_background_pixel;
8714 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8715 &pixel, 0.6, 0x4000))
8716 pixel = -1;
8717 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
8718 }
8719
8720 /* Tell the toolkit about them. */
8721 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
8722 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8723 /* We tried to allocate a color for the top/bottom shadow, and
8724 failed, so tell Xaw3d to use dithering instead. */
8725 {
8726 XtSetArg (av[ac], XtNbeNiceToColormap, True);
8727 ++ac;
8728 }
8729 else
8730 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
8731 be more consistent with other emacs 3d colors, and since Xaw3d is
8732 not good at dealing with allocation failure. */
8733 {
8734 /* This tells Xaw3d to use real colors instead of dithering for
8735 the shadows. */
8736 XtSetArg (av[ac], XtNbeNiceToColormap, False);
8737 ++ac;
8738
8739 /* Specify the colors. */
8740 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
8741 if (pixel != -1)
8742 {
8743 XtSetArg (av[ac], "topShadowPixel", pixel);
8744 ++ac;
8745 }
8746 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
8747 if (pixel != -1)
8748 {
8749 XtSetArg (av[ac], "bottomShadowPixel", pixel);
8750 ++ac;
8751 }
8752 }
8753
8754 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8755 f->output_data.x->edit_widget, av, ac);
8756
8757 {
8758 char *initial = "";
8759 char *val = initial;
8760 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8761 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8762 if (val == initial)
8763 { /* ARROW_SCROLL */
8764 xaw3d_arrow_scroll = True;
8765 /* Isn't that just a personal preference ? -sm */
8766 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8767 }
8768 }
8769
8770 /* Define callbacks. */
8771 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8772 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
8773 (XtPointer) bar);
8774
8775 /* Realize the widget. Only after that is the X window created. */
8776 XtRealizeWidget (widget);
8777
8778 #endif /* !USE_MOTIF */
8779
8780 /* Install an action hook that let's us detect when the user
8781 finishes interacting with a scroll bar. */
8782 if (action_hook_id == 0)
8783 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8784
8785 /* Remember X window and widget in the scroll bar vector. */
8786 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8787 xwindow = XtWindow (widget);
8788 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8789
8790 UNBLOCK_INPUT;
8791 }
8792
8793
8794 /* Set the thumb size and position of scroll bar BAR. We are currently
8795 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8796
8797 static void
8798 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8799 struct scroll_bar *bar;
8800 int portion, position, whole;
8801 {
8802 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8803 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8804 float top, shown;
8805
8806 if (whole == 0)
8807 top = 0, shown = 1;
8808 else
8809 {
8810 top = (float) position / whole;
8811 shown = (float) portion / whole;
8812 }
8813
8814 BLOCK_INPUT;
8815
8816 #ifdef USE_MOTIF
8817 {
8818 int size, value;
8819
8820 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
8821 is the scroll bar's maximum and MIN is the scroll bar's minimum
8822 value. */
8823 size = shown * XM_SB_RANGE;
8824 size = min (size, XM_SB_RANGE);
8825 size = max (size, 1);
8826
8827 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8828 value = top * XM_SB_RANGE;
8829 value = min (value, XM_SB_MAX - size);
8830 value = max (value, XM_SB_MIN);
8831
8832 if (NILP (bar->dragging))
8833 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8834 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8835 /* This has the negative side effect that the slider value is
8836 not what it would be if we scrolled here using line-wise or
8837 page-wise movement. */
8838 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8839 else
8840 {
8841 /* If currently dragging, only update the slider size.
8842 This reduces flicker effects. */
8843 int old_value, old_size, increment, page_increment;
8844
8845 XmScrollBarGetValues (widget, &old_value, &old_size,
8846 &increment, &page_increment);
8847 XmScrollBarSetValues (widget, old_value,
8848 min (size, XM_SB_RANGE - old_value),
8849 0, 0, False);
8850 }
8851 }
8852 #else /* !USE_MOTIF i.e. use Xaw */
8853 {
8854 float old_top, old_shown;
8855 Dimension height;
8856 XtVaGetValues (widget,
8857 XtNtopOfThumb, &old_top,
8858 XtNshown, &old_shown,
8859 XtNheight, &height,
8860 NULL);
8861
8862 /* Massage the top+shown values. */
8863 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8864 top = max (0, min (1, top));
8865 else
8866 top = old_top;
8867 /* Keep two pixels available for moving the thumb down. */
8868 shown = max (0, min (1 - top - (2.0 / height), shown));
8869
8870 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8871 check that your system's configuration file contains a define
8872 for `NARROWPROTO'. See s/freebsd.h for an example. */
8873 if (top != old_top || shown != old_shown)
8874 {
8875 if (NILP (bar->dragging))
8876 XawScrollbarSetThumb (widget, top, shown);
8877 else
8878 {
8879 #ifdef HAVE_XAW3D
8880 ScrollbarWidget sb = (ScrollbarWidget) widget;
8881 int scroll_mode = 0;
8882
8883 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8884 if (xaw3d_arrow_scroll)
8885 {
8886 /* Xaw3d stupidly ignores resize requests while dragging
8887 so we have to make it believe it's not in dragging mode. */
8888 scroll_mode = sb->scrollbar.scroll_mode;
8889 if (scroll_mode == 2)
8890 sb->scrollbar.scroll_mode = 0;
8891 }
8892 #endif
8893 /* Try to make the scrolling a tad smoother. */
8894 if (!xaw3d_pick_top)
8895 shown = min (shown, old_shown);
8896
8897 XawScrollbarSetThumb (widget, top, shown);
8898
8899 #ifdef HAVE_XAW3D
8900 if (xaw3d_arrow_scroll && scroll_mode == 2)
8901 sb->scrollbar.scroll_mode = scroll_mode;
8902 #endif
8903 }
8904 }
8905 }
8906 #endif /* !USE_MOTIF */
8907
8908 UNBLOCK_INPUT;
8909 }
8910
8911 #endif /* USE_TOOLKIT_SCROLL_BARS */
8912
8913
8914 \f
8915 /************************************************************************
8916 Scroll bars, general
8917 ************************************************************************/
8918
8919 /* Create a scroll bar and return the scroll bar vector for it. W is
8920 the Emacs window on which to create the scroll bar. TOP, LEFT,
8921 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8922 scroll bar. */
8923
8924 static struct scroll_bar *
8925 x_scroll_bar_create (w, top, left, width, height)
8926 struct window *w;
8927 int top, left, width, height;
8928 {
8929 struct frame *f = XFRAME (w->frame);
8930 struct scroll_bar *bar
8931 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8932
8933 BLOCK_INPUT;
8934
8935 #ifdef USE_TOOLKIT_SCROLL_BARS
8936 x_create_toolkit_scroll_bar (f, bar);
8937 #else /* not USE_TOOLKIT_SCROLL_BARS */
8938 {
8939 XSetWindowAttributes a;
8940 unsigned long mask;
8941 Window window;
8942
8943 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8944 if (a.background_pixel == -1)
8945 a.background_pixel = f->output_data.x->background_pixel;
8946
8947 a.event_mask = (ButtonPressMask | ButtonReleaseMask
8948 | ButtonMotionMask | PointerMotionHintMask
8949 | ExposureMask);
8950 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
8951
8952 mask = (CWBackPixel | CWEventMask | CWCursor);
8953
8954 /* Clear the area of W that will serve as a scroll bar. This is
8955 for the case that a window has been split horizontally. In
8956 this case, no clear_frame is generated to reduce flickering. */
8957 if (width > 0 && height > 0)
8958 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8959 left, top, width,
8960 window_box_height (w), False);
8961
8962 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8963 /* Position and size of scroll bar. */
8964 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8965 top,
8966 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8967 height,
8968 /* Border width, depth, class, and visual. */
8969 0,
8970 CopyFromParent,
8971 CopyFromParent,
8972 CopyFromParent,
8973 /* Attributes. */
8974 mask, &a);
8975 SET_SCROLL_BAR_X_WINDOW (bar, window);
8976 }
8977 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8978
8979 XSETWINDOW (bar->window, w);
8980 XSETINT (bar->top, top);
8981 XSETINT (bar->left, left);
8982 XSETINT (bar->width, width);
8983 XSETINT (bar->height, height);
8984 XSETINT (bar->start, 0);
8985 XSETINT (bar->end, 0);
8986 bar->dragging = Qnil;
8987
8988 /* Add bar to its frame's list of scroll bars. */
8989 bar->next = FRAME_SCROLL_BARS (f);
8990 bar->prev = Qnil;
8991 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8992 if (!NILP (bar->next))
8993 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8994
8995 /* Map the window/widget. */
8996 #ifdef USE_TOOLKIT_SCROLL_BARS
8997 {
8998 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8999 XtConfigureWidget (scroll_bar,
9000 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9001 top,
9002 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9003 max (height, 1), 0);
9004 XtMapWidget (scroll_bar);
9005 }
9006 #else /* not USE_TOOLKIT_SCROLL_BARS */
9007 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9008 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9009
9010 UNBLOCK_INPUT;
9011 return bar;
9012 }
9013
9014
9015 /* Draw BAR's handle in the proper position.
9016
9017 If the handle is already drawn from START to END, don't bother
9018 redrawing it, unless REBUILD is non-zero; in that case, always
9019 redraw it. (REBUILD is handy for drawing the handle after expose
9020 events.)
9021
9022 Normally, we want to constrain the start and end of the handle to
9023 fit inside its rectangle, but if the user is dragging the scroll
9024 bar handle, we want to let them drag it down all the way, so that
9025 the bar's top is as far down as it goes; otherwise, there's no way
9026 to move to the very end of the buffer. */
9027
9028 #ifndef USE_TOOLKIT_SCROLL_BARS
9029
9030 static void
9031 x_scroll_bar_set_handle (bar, start, end, rebuild)
9032 struct scroll_bar *bar;
9033 int start, end;
9034 int rebuild;
9035 {
9036 int dragging = ! NILP (bar->dragging);
9037 Window w = SCROLL_BAR_X_WINDOW (bar);
9038 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9039 GC gc = f->output_data.x->normal_gc;
9040
9041 /* If the display is already accurate, do nothing. */
9042 if (! rebuild
9043 && start == XINT (bar->start)
9044 && end == XINT (bar->end))
9045 return;
9046
9047 BLOCK_INPUT;
9048
9049 {
9050 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
9051 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9052 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9053
9054 /* Make sure the values are reasonable, and try to preserve
9055 the distance between start and end. */
9056 {
9057 int length = end - start;
9058
9059 if (start < 0)
9060 start = 0;
9061 else if (start > top_range)
9062 start = top_range;
9063 end = start + length;
9064
9065 if (end < start)
9066 end = start;
9067 else if (end > top_range && ! dragging)
9068 end = top_range;
9069 }
9070
9071 /* Store the adjusted setting in the scroll bar. */
9072 XSETINT (bar->start, start);
9073 XSETINT (bar->end, end);
9074
9075 /* Clip the end position, just for display. */
9076 if (end > top_range)
9077 end = top_range;
9078
9079 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
9080 below top positions, to make sure the handle is always at least
9081 that many pixels tall. */
9082 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
9083
9084 /* Draw the empty space above the handle. Note that we can't clear
9085 zero-height areas; that means "clear to end of window." */
9086 if (0 < start)
9087 x_clear_area (FRAME_X_DISPLAY (f), w,
9088 /* x, y, width, height, and exposures. */
9089 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9090 VERTICAL_SCROLL_BAR_TOP_BORDER,
9091 inside_width, start,
9092 False);
9093
9094 /* Change to proper foreground color if one is specified. */
9095 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9096 XSetForeground (FRAME_X_DISPLAY (f), gc,
9097 f->output_data.x->scroll_bar_foreground_pixel);
9098
9099 /* Draw the handle itself. */
9100 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
9101 /* x, y, width, height */
9102 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9103 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
9104 inside_width, end - start);
9105
9106 /* Restore the foreground color of the GC if we changed it above. */
9107 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9108 XSetForeground (FRAME_X_DISPLAY (f), gc,
9109 f->output_data.x->foreground_pixel);
9110
9111 /* Draw the empty space below the handle. Note that we can't
9112 clear zero-height areas; that means "clear to end of window." */
9113 if (end < inside_height)
9114 x_clear_area (FRAME_X_DISPLAY (f), w,
9115 /* x, y, width, height, and exposures. */
9116 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9117 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
9118 inside_width, inside_height - end,
9119 False);
9120
9121 }
9122
9123 UNBLOCK_INPUT;
9124 }
9125
9126 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9127
9128 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
9129 nil. */
9130
9131 static void
9132 x_scroll_bar_remove (bar)
9133 struct scroll_bar *bar;
9134 {
9135 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9136 BLOCK_INPUT;
9137
9138 #ifdef USE_TOOLKIT_SCROLL_BARS
9139 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
9140 #else
9141 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9142 #endif
9143
9144 /* Disassociate this scroll bar from its window. */
9145 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
9146
9147 UNBLOCK_INPUT;
9148 }
9149
9150
9151 /* Set the handle of the vertical scroll bar for WINDOW to indicate
9152 that we are displaying PORTION characters out of a total of WHOLE
9153 characters, starting at POSITION. If WINDOW has no scroll bar,
9154 create one. */
9155
9156 static void
9157 XTset_vertical_scroll_bar (w, portion, whole, position)
9158 struct window *w;
9159 int portion, whole, position;
9160 {
9161 struct frame *f = XFRAME (w->frame);
9162 struct scroll_bar *bar;
9163 int top, height, left, sb_left, width, sb_width;
9164 int window_x, window_y, window_width, window_height;
9165
9166 /* Get window dimensions. */
9167 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
9168 top = window_y;
9169 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9170 height = window_height;
9171
9172 /* Compute the left edge of the scroll bar area. */
9173 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9174 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
9175 else
9176 left = XFASTINT (w->left);
9177 left *= CANON_X_UNIT (f);
9178 left += FRAME_INTERNAL_BORDER_WIDTH (f);
9179
9180 /* Compute the width of the scroll bar which might be less than
9181 the width of the area reserved for the scroll bar. */
9182 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
9183 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
9184 else
9185 sb_width = width;
9186
9187 /* Compute the left edge of the scroll bar. */
9188 #ifdef USE_TOOLKIT_SCROLL_BARS
9189 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9190 sb_left = left + width - sb_width - (width - sb_width) / 2;
9191 else
9192 sb_left = left + (width - sb_width) / 2;
9193 #else
9194 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9195 sb_left = left + width - sb_width;
9196 else
9197 sb_left = left;
9198 #endif
9199
9200 /* Does the scroll bar exist yet? */
9201 if (NILP (w->vertical_scroll_bar))
9202 {
9203 if (width > 0 && height > 0)
9204 {
9205 BLOCK_INPUT;
9206 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9207 left, top, width, height, False);
9208 UNBLOCK_INPUT;
9209 }
9210
9211 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
9212 }
9213 else
9214 {
9215 /* It may just need to be moved and resized. */
9216 unsigned int mask = 0;
9217
9218 bar = XSCROLL_BAR (w->vertical_scroll_bar);
9219
9220 BLOCK_INPUT;
9221
9222 if (sb_left != XINT (bar->left))
9223 mask |= CWX;
9224 if (top != XINT (bar->top))
9225 mask |= CWY;
9226 if (sb_width != XINT (bar->width))
9227 mask |= CWWidth;
9228 if (height != XINT (bar->height))
9229 mask |= CWHeight;
9230
9231 #ifdef USE_TOOLKIT_SCROLL_BARS
9232
9233 /* Since toolkit scroll bars are smaller than the space reserved
9234 for them on the frame, we have to clear "under" them. */
9235 if (width > 0 && height > 0)
9236 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9237 left, top, width, height, False);
9238
9239 /* Move/size the scroll bar widget. */
9240 if (mask)
9241 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
9242 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9243 top,
9244 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9245 max (height, 1), 0);
9246
9247 #else /* not USE_TOOLKIT_SCROLL_BARS */
9248
9249 /* Clear areas not covered by the scroll bar because of
9250 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
9251 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
9252 {
9253 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9254 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9255 height, False);
9256 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9257 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9258 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9259 height, False);
9260 }
9261
9262 /* Clear areas not covered by the scroll bar because it's not as
9263 wide as the area reserved for it . This makes sure a
9264 previous mode line display is cleared after C-x 2 C-x 1, for
9265 example. */
9266 {
9267 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9268 int rest = area_width - sb_width;
9269 if (rest > 0 && height > 0)
9270 {
9271 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
9272 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9273 left + area_width - rest, top,
9274 rest, height, False);
9275 else
9276 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9277 left, top, rest, height, False);
9278 }
9279 }
9280
9281 /* Move/size the scroll bar window. */
9282 if (mask)
9283 {
9284 XWindowChanges wc;
9285
9286 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9287 wc.y = top;
9288 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
9289 wc.height = height;
9290 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
9291 mask, &wc);
9292 }
9293
9294 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9295
9296 /* Remember new settings. */
9297 XSETINT (bar->left, sb_left);
9298 XSETINT (bar->top, top);
9299 XSETINT (bar->width, sb_width);
9300 XSETINT (bar->height, height);
9301
9302 UNBLOCK_INPUT;
9303 }
9304
9305 #ifdef USE_TOOLKIT_SCROLL_BARS
9306 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
9307 #else /* not USE_TOOLKIT_SCROLL_BARS */
9308 /* Set the scroll bar's current state, unless we're currently being
9309 dragged. */
9310 if (NILP (bar->dragging))
9311 {
9312 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
9313
9314 if (whole == 0)
9315 x_scroll_bar_set_handle (bar, 0, top_range, 0);
9316 else
9317 {
9318 int start = ((double) position * top_range) / whole;
9319 int end = ((double) (position + portion) * top_range) / whole;
9320 x_scroll_bar_set_handle (bar, start, end, 0);
9321 }
9322 }
9323 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9324
9325 XSETVECTOR (w->vertical_scroll_bar, bar);
9326 }
9327
9328
9329 /* The following three hooks are used when we're doing a thorough
9330 redisplay of the frame. We don't explicitly know which scroll bars
9331 are going to be deleted, because keeping track of when windows go
9332 away is a real pain - "Can you say set-window-configuration, boys
9333 and girls?" Instead, we just assert at the beginning of redisplay
9334 that *all* scroll bars are to be removed, and then save a scroll bar
9335 from the fiery pit when we actually redisplay its window. */
9336
9337 /* Arrange for all scroll bars on FRAME to be removed at the next call
9338 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
9339 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
9340
9341 static void
9342 XTcondemn_scroll_bars (frame)
9343 FRAME_PTR frame;
9344 {
9345 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
9346 while (! NILP (FRAME_SCROLL_BARS (frame)))
9347 {
9348 Lisp_Object bar;
9349 bar = FRAME_SCROLL_BARS (frame);
9350 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
9351 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
9352 XSCROLL_BAR (bar)->prev = Qnil;
9353 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
9354 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
9355 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
9356 }
9357 }
9358
9359
9360 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
9361 Note that WINDOW isn't necessarily condemned at all. */
9362
9363 static void
9364 XTredeem_scroll_bar (window)
9365 struct window *window;
9366 {
9367 struct scroll_bar *bar;
9368 struct frame *f;
9369
9370 /* We can't redeem this window's scroll bar if it doesn't have one. */
9371 if (NILP (window->vertical_scroll_bar))
9372 abort ();
9373
9374 bar = XSCROLL_BAR (window->vertical_scroll_bar);
9375
9376 /* Unlink it from the condemned list. */
9377 f = XFRAME (WINDOW_FRAME (window));
9378 if (NILP (bar->prev))
9379 {
9380 /* If the prev pointer is nil, it must be the first in one of
9381 the lists. */
9382 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
9383 /* It's not condemned. Everything's fine. */
9384 return;
9385 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
9386 window->vertical_scroll_bar))
9387 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
9388 else
9389 /* If its prev pointer is nil, it must be at the front of
9390 one or the other! */
9391 abort ();
9392 }
9393 else
9394 XSCROLL_BAR (bar->prev)->next = bar->next;
9395
9396 if (! NILP (bar->next))
9397 XSCROLL_BAR (bar->next)->prev = bar->prev;
9398
9399 bar->next = FRAME_SCROLL_BARS (f);
9400 bar->prev = Qnil;
9401 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9402 if (! NILP (bar->next))
9403 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9404 }
9405
9406 /* Remove all scroll bars on FRAME that haven't been saved since the
9407 last call to `*condemn_scroll_bars_hook'. */
9408
9409 static void
9410 XTjudge_scroll_bars (f)
9411 FRAME_PTR f;
9412 {
9413 Lisp_Object bar, next;
9414
9415 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
9416
9417 /* Clear out the condemned list now so we won't try to process any
9418 more events on the hapless scroll bars. */
9419 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
9420
9421 for (; ! NILP (bar); bar = next)
9422 {
9423 struct scroll_bar *b = XSCROLL_BAR (bar);
9424
9425 x_scroll_bar_remove (b);
9426
9427 next = b->next;
9428 b->next = b->prev = Qnil;
9429 }
9430
9431 /* Now there should be no references to the condemned scroll bars,
9432 and they should get garbage-collected. */
9433 }
9434
9435
9436 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
9437 is a no-op when using toolkit scroll bars.
9438
9439 This may be called from a signal handler, so we have to ignore GC
9440 mark bits. */
9441
9442 static void
9443 x_scroll_bar_expose (bar, event)
9444 struct scroll_bar *bar;
9445 XEvent *event;
9446 {
9447 #ifndef USE_TOOLKIT_SCROLL_BARS
9448
9449 Window w = SCROLL_BAR_X_WINDOW (bar);
9450 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9451 GC gc = f->output_data.x->normal_gc;
9452 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9453
9454 BLOCK_INPUT;
9455
9456 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
9457
9458 /* Draw a one-pixel border just inside the edges of the scroll bar. */
9459 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
9460
9461 /* x, y, width, height */
9462 0, 0,
9463 XINT (bar->width) - 1 - width_trim - width_trim,
9464 XINT (bar->height) - 1);
9465
9466 UNBLOCK_INPUT;
9467
9468 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9469 }
9470
9471 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
9472 is set to something other than no_event, it is enqueued.
9473
9474 This may be called from a signal handler, so we have to ignore GC
9475 mark bits. */
9476
9477 #ifndef USE_TOOLKIT_SCROLL_BARS
9478
9479 static void
9480 x_scroll_bar_handle_click (bar, event, emacs_event)
9481 struct scroll_bar *bar;
9482 XEvent *event;
9483 struct input_event *emacs_event;
9484 {
9485 if (! GC_WINDOWP (bar->window))
9486 abort ();
9487
9488 emacs_event->kind = scroll_bar_click;
9489 emacs_event->code = event->xbutton.button - Button1;
9490 emacs_event->modifiers
9491 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
9492 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
9493 event->xbutton.state)
9494 | (event->type == ButtonRelease
9495 ? up_modifier
9496 : down_modifier));
9497 emacs_event->frame_or_window = bar->window;
9498 emacs_event->arg = Qnil;
9499 emacs_event->timestamp = event->xbutton.time;
9500 {
9501 #if 0
9502 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9503 int internal_height
9504 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9505 #endif
9506 int top_range
9507 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9508 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
9509
9510 if (y < 0) y = 0;
9511 if (y > top_range) y = top_range;
9512
9513 if (y < XINT (bar->start))
9514 emacs_event->part = scroll_bar_above_handle;
9515 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9516 emacs_event->part = scroll_bar_handle;
9517 else
9518 emacs_event->part = scroll_bar_below_handle;
9519
9520 /* Just because the user has clicked on the handle doesn't mean
9521 they want to drag it. Lisp code needs to be able to decide
9522 whether or not we're dragging. */
9523 #if 0
9524 /* If the user has just clicked on the handle, record where they're
9525 holding it. */
9526 if (event->type == ButtonPress
9527 && emacs_event->part == scroll_bar_handle)
9528 XSETINT (bar->dragging, y - XINT (bar->start));
9529 #endif
9530
9531 /* If the user has released the handle, set it to its final position. */
9532 if (event->type == ButtonRelease
9533 && ! NILP (bar->dragging))
9534 {
9535 int new_start = y - XINT (bar->dragging);
9536 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9537
9538 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9539 bar->dragging = Qnil;
9540 }
9541
9542 /* Same deal here as the other #if 0. */
9543 #if 0
9544 /* Clicks on the handle are always reported as occurring at the top of
9545 the handle. */
9546 if (emacs_event->part == scroll_bar_handle)
9547 emacs_event->x = bar->start;
9548 else
9549 XSETINT (emacs_event->x, y);
9550 #else
9551 XSETINT (emacs_event->x, y);
9552 #endif
9553
9554 XSETINT (emacs_event->y, top_range);
9555 }
9556 }
9557
9558 /* Handle some mouse motion while someone is dragging the scroll bar.
9559
9560 This may be called from a signal handler, so we have to ignore GC
9561 mark bits. */
9562
9563 static void
9564 x_scroll_bar_note_movement (bar, event)
9565 struct scroll_bar *bar;
9566 XEvent *event;
9567 {
9568 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9569
9570 last_mouse_movement_time = event->xmotion.time;
9571
9572 f->mouse_moved = 1;
9573 XSETVECTOR (last_mouse_scroll_bar, bar);
9574
9575 /* If we're dragging the bar, display it. */
9576 if (! GC_NILP (bar->dragging))
9577 {
9578 /* Where should the handle be now? */
9579 int new_start = event->xmotion.y - XINT (bar->dragging);
9580
9581 if (new_start != XINT (bar->start))
9582 {
9583 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9584
9585 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9586 }
9587 }
9588 }
9589
9590 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9591
9592 /* Return information to the user about the current position of the mouse
9593 on the scroll bar. */
9594
9595 static void
9596 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9597 FRAME_PTR *fp;
9598 Lisp_Object *bar_window;
9599 enum scroll_bar_part *part;
9600 Lisp_Object *x, *y;
9601 unsigned long *time;
9602 {
9603 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
9604 Window w = SCROLL_BAR_X_WINDOW (bar);
9605 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9606 int win_x, win_y;
9607 Window dummy_window;
9608 int dummy_coord;
9609 unsigned int dummy_mask;
9610
9611 BLOCK_INPUT;
9612
9613 /* Get the mouse's position relative to the scroll bar window, and
9614 report that. */
9615 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
9616
9617 /* Root, child, root x and root y. */
9618 &dummy_window, &dummy_window,
9619 &dummy_coord, &dummy_coord,
9620
9621 /* Position relative to scroll bar. */
9622 &win_x, &win_y,
9623
9624 /* Mouse buttons and modifier keys. */
9625 &dummy_mask))
9626 ;
9627 else
9628 {
9629 #if 0
9630 int inside_height
9631 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9632 #endif
9633 int top_range
9634 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9635
9636 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9637
9638 if (! NILP (bar->dragging))
9639 win_y -= XINT (bar->dragging);
9640
9641 if (win_y < 0)
9642 win_y = 0;
9643 if (win_y > top_range)
9644 win_y = top_range;
9645
9646 *fp = f;
9647 *bar_window = bar->window;
9648
9649 if (! NILP (bar->dragging))
9650 *part = scroll_bar_handle;
9651 else if (win_y < XINT (bar->start))
9652 *part = scroll_bar_above_handle;
9653 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9654 *part = scroll_bar_handle;
9655 else
9656 *part = scroll_bar_below_handle;
9657
9658 XSETINT (*x, win_y);
9659 XSETINT (*y, top_range);
9660
9661 f->mouse_moved = 0;
9662 last_mouse_scroll_bar = Qnil;
9663 }
9664
9665 *time = last_mouse_movement_time;
9666
9667 UNBLOCK_INPUT;
9668 }
9669
9670
9671 /* The screen has been cleared so we may have changed foreground or
9672 background colors, and the scroll bars may need to be redrawn.
9673 Clear out the scroll bars, and ask for expose events, so we can
9674 redraw them. */
9675
9676 void
9677 x_scroll_bar_clear (f)
9678 FRAME_PTR f;
9679 {
9680 #ifndef USE_TOOLKIT_SCROLL_BARS
9681 Lisp_Object bar;
9682
9683 /* We can have scroll bars even if this is 0,
9684 if we just turned off scroll bar mode.
9685 But in that case we should not clear them. */
9686 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9687 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9688 bar = XSCROLL_BAR (bar)->next)
9689 XClearArea (FRAME_X_DISPLAY (f),
9690 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9691 0, 0, 0, 0, True);
9692 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9693 }
9694
9695 /* This processes Expose events from the menu-bar specific X event
9696 loop in xmenu.c. This allows to redisplay the frame if necessary
9697 when handling menu-bar or pop-up items. */
9698
9699 int
9700 process_expose_from_menu (event)
9701 XEvent event;
9702 {
9703 FRAME_PTR f;
9704 struct x_display_info *dpyinfo;
9705 int frame_exposed_p = 0;
9706
9707 BLOCK_INPUT;
9708
9709 dpyinfo = x_display_info_for_display (event.xexpose.display);
9710 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9711 if (f)
9712 {
9713 if (f->async_visible == 0)
9714 {
9715 f->async_visible = 1;
9716 f->async_iconified = 0;
9717 f->output_data.x->has_been_visible = 1;
9718 SET_FRAME_GARBAGED (f);
9719 }
9720 else
9721 {
9722 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9723 event.xexpose.x, event.xexpose.y,
9724 event.xexpose.width, event.xexpose.height);
9725 frame_exposed_p = 1;
9726 }
9727 }
9728 else
9729 {
9730 struct scroll_bar *bar
9731 = x_window_to_scroll_bar (event.xexpose.window);
9732
9733 if (bar)
9734 x_scroll_bar_expose (bar, &event);
9735 }
9736
9737 UNBLOCK_INPUT;
9738 return frame_exposed_p;
9739 }
9740 \f
9741 /* Define a queue to save up SelectionRequest events for later handling. */
9742
9743 struct selection_event_queue
9744 {
9745 XEvent event;
9746 struct selection_event_queue *next;
9747 };
9748
9749 static struct selection_event_queue *queue;
9750
9751 /* Nonzero means queue up certain events--don't process them yet. */
9752
9753 static int x_queue_selection_requests;
9754
9755 /* Queue up an X event *EVENT, to be processed later. */
9756
9757 static void
9758 x_queue_event (f, event)
9759 FRAME_PTR f;
9760 XEvent *event;
9761 {
9762 struct selection_event_queue *queue_tmp
9763 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
9764
9765 if (queue_tmp != NULL)
9766 {
9767 queue_tmp->event = *event;
9768 queue_tmp->next = queue;
9769 queue = queue_tmp;
9770 }
9771 }
9772
9773 /* Take all the queued events and put them back
9774 so that they get processed afresh. */
9775
9776 static void
9777 x_unqueue_events (display)
9778 Display *display;
9779 {
9780 while (queue != NULL)
9781 {
9782 struct selection_event_queue *queue_tmp = queue;
9783 XPutBackEvent (display, &queue_tmp->event);
9784 queue = queue_tmp->next;
9785 xfree ((char *)queue_tmp);
9786 }
9787 }
9788
9789 /* Start queuing SelectionRequest events. */
9790
9791 void
9792 x_start_queuing_selection_requests (display)
9793 Display *display;
9794 {
9795 x_queue_selection_requests++;
9796 }
9797
9798 /* Stop queuing SelectionRequest events. */
9799
9800 void
9801 x_stop_queuing_selection_requests (display)
9802 Display *display;
9803 {
9804 x_queue_selection_requests--;
9805 x_unqueue_events (display);
9806 }
9807 \f
9808 /* The main X event-reading loop - XTread_socket. */
9809
9810 /* Time stamp of enter window event. This is only used by XTread_socket,
9811 but we have to put it out here, since static variables within functions
9812 sometimes don't work. */
9813
9814 static Time enter_timestamp;
9815
9816 /* This holds the state XLookupString needs to implement dead keys
9817 and other tricks known as "compose processing". _X Window System_
9818 says that a portable program can't use this, but Stephen Gildea assures
9819 me that letting the compiler initialize it to zeros will work okay.
9820
9821 This must be defined outside of XTread_socket, for the same reasons
9822 given for enter_time stamp, above. */
9823
9824 static XComposeStatus compose_status;
9825
9826 /* Record the last 100 characters stored
9827 to help debug the loss-of-chars-during-GC problem. */
9828
9829 static int temp_index;
9830 static short temp_buffer[100];
9831
9832 /* Set this to nonzero to fake an "X I/O error"
9833 on a particular display. */
9834
9835 struct x_display_info *XTread_socket_fake_io_error;
9836
9837 /* When we find no input here, we occasionally do a no-op command
9838 to verify that the X server is still running and we can still talk with it.
9839 We try all the open displays, one by one.
9840 This variable is used for cycling thru the displays. */
9841
9842 static struct x_display_info *next_noop_dpyinfo;
9843
9844 #define SET_SAVED_MENU_EVENT(size) \
9845 do \
9846 { \
9847 if (f->output_data.x->saved_menu_event == 0) \
9848 f->output_data.x->saved_menu_event \
9849 = (XEvent *) xmalloc (sizeof (XEvent)); \
9850 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9851 if (numchars >= 1) \
9852 { \
9853 bufp->kind = menu_bar_activate_event; \
9854 XSETFRAME (bufp->frame_or_window, f); \
9855 bufp->arg = Qnil; \
9856 bufp++; \
9857 count++; \
9858 numchars--; \
9859 } \
9860 } \
9861 while (0)
9862
9863 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9864 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9865
9866 /* Read events coming from the X server.
9867 This routine is called by the SIGIO handler.
9868 We return as soon as there are no more events to be read.
9869
9870 Events representing keys are stored in buffer BUFP,
9871 which can hold up to NUMCHARS characters.
9872 We return the number of characters stored into the buffer,
9873 thus pretending to be `read'.
9874
9875 EXPECTED is nonzero if the caller knows input is available. */
9876
9877 int
9878 XTread_socket (sd, bufp, numchars, expected)
9879 register int sd;
9880 /* register */ struct input_event *bufp;
9881 /* register */ int numchars;
9882 int expected;
9883 {
9884 int count = 0;
9885 int nbytes = 0;
9886 XEvent event;
9887 struct frame *f;
9888 int event_found = 0;
9889 struct x_display_info *dpyinfo;
9890 struct coding_system coding;
9891
9892 if (interrupt_input_blocked)
9893 {
9894 interrupt_input_pending = 1;
9895 return -1;
9896 }
9897
9898 interrupt_input_pending = 0;
9899 BLOCK_INPUT;
9900
9901 /* So people can tell when we have read the available input. */
9902 input_signal_count++;
9903
9904 if (numchars <= 0)
9905 abort (); /* Don't think this happens. */
9906
9907 ++handling_signal;
9908
9909 /* The input should be decoded if it is from XIM. Currently the
9910 locale of XIM is the same as that of the system. So, we can use
9911 Vlocale_coding_system which is initialized properly at Emacs
9912 startup time. */
9913 setup_coding_system (Vlocale_coding_system, &coding);
9914 coding.src_multibyte = 0;
9915 coding.dst_multibyte = 1;
9916 /* The input is converted to events, thus we can't handle
9917 composition. Anyway, there's no XIM that gives us composition
9918 information. */
9919 coding.composing = COMPOSITION_DISABLED;
9920
9921 /* Find the display we are supposed to read input for.
9922 It's the one communicating on descriptor SD. */
9923 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9924 {
9925 #if 0 /* This ought to be unnecessary; let's verify it. */
9926 #ifdef FIOSNBIO
9927 /* If available, Xlib uses FIOSNBIO to make the socket
9928 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
9929 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
9930 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
9931 fcntl (dpyinfo->connection, F_SETFL, 0);
9932 #endif /* ! defined (FIOSNBIO) */
9933 #endif
9934
9935 #if 0 /* This code can't be made to work, with multiple displays,
9936 and appears not to be used on any system any more.
9937 Also keyboard.c doesn't turn O_NDELAY on and off
9938 for X connections. */
9939 #ifndef SIGIO
9940 #ifndef HAVE_SELECT
9941 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9942 {
9943 extern int read_alarm_should_throw;
9944 read_alarm_should_throw = 1;
9945 XPeekEvent (dpyinfo->display, &event);
9946 read_alarm_should_throw = 0;
9947 }
9948 #endif /* HAVE_SELECT */
9949 #endif /* SIGIO */
9950 #endif
9951
9952 /* For debugging, this gives a way to fake an I/O error. */
9953 if (dpyinfo == XTread_socket_fake_io_error)
9954 {
9955 XTread_socket_fake_io_error = 0;
9956 x_io_error_quitter (dpyinfo->display);
9957 }
9958
9959 while (XPending (dpyinfo->display))
9960 {
9961 XNextEvent (dpyinfo->display, &event);
9962
9963 #ifdef HAVE_X_I18N
9964 {
9965 /* Filter events for the current X input method.
9966 XFilterEvent returns non-zero if the input method has
9967 consumed the event. We pass the frame's X window to
9968 XFilterEvent because that's the one for which the IC
9969 was created. */
9970 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9971 event.xclient.window);
9972 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
9973 break;
9974 }
9975 #endif
9976 event_found = 1;
9977
9978 switch (event.type)
9979 {
9980 case ClientMessage:
9981 {
9982 if (event.xclient.message_type
9983 == dpyinfo->Xatom_wm_protocols
9984 && event.xclient.format == 32)
9985 {
9986 if (event.xclient.data.l[0]
9987 == dpyinfo->Xatom_wm_take_focus)
9988 {
9989 /* Use x_any_window_to_frame because this
9990 could be the shell widget window
9991 if the frame has no title bar. */
9992 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
9993 #ifdef HAVE_X_I18N
9994 /* Not quite sure this is needed -pd */
9995 if (f && FRAME_XIC (f))
9996 XSetICFocus (FRAME_XIC (f));
9997 #endif
9998 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9999 instructs the WM to set the input focus automatically for
10000 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
10001 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
10002 it has set the focus. So, XSetInputFocus below is not
10003 needed.
10004
10005 The call to XSetInputFocus below has also caused trouble. In
10006 cases where the XSetInputFocus done by the WM and the one
10007 below are temporally close (on a fast machine), the call
10008 below can generate additional FocusIn events which confuse
10009 Emacs. */
10010
10011 /* Since we set WM_TAKE_FOCUS, we must call
10012 XSetInputFocus explicitly. But not if f is null,
10013 since that might be an event for a deleted frame. */
10014 if (f)
10015 {
10016 Display *d = event.xclient.display;
10017 /* Catch and ignore errors, in case window has been
10018 iconified by a window manager such as GWM. */
10019 int count = x_catch_errors (d);
10020 XSetInputFocus (d, event.xclient.window,
10021 /* The ICCCM says this is
10022 the only valid choice. */
10023 RevertToParent,
10024 event.xclient.data.l[1]);
10025 /* This is needed to detect the error
10026 if there is an error. */
10027 XSync (d, False);
10028 x_uncatch_errors (d, count);
10029 }
10030 /* Not certain about handling scroll bars here */
10031 #endif /* 0 */
10032 }
10033 else if (event.xclient.data.l[0]
10034 == dpyinfo->Xatom_wm_save_yourself)
10035 {
10036 /* Save state modify the WM_COMMAND property to
10037 something which can reinstate us. This notifies
10038 the session manager, who's looking for such a
10039 PropertyNotify. Can restart processing when
10040 a keyboard or mouse event arrives. */
10041 if (numchars > 0)
10042 {
10043 f = x_top_window_to_frame (dpyinfo,
10044 event.xclient.window);
10045
10046 /* This is just so we only give real data once
10047 for a single Emacs process. */
10048 if (f == SELECTED_FRAME ())
10049 XSetCommand (FRAME_X_DISPLAY (f),
10050 event.xclient.window,
10051 initial_argv, initial_argc);
10052 else if (f)
10053 XSetCommand (FRAME_X_DISPLAY (f),
10054 event.xclient.window,
10055 0, 0);
10056 }
10057 }
10058 else if (event.xclient.data.l[0]
10059 == dpyinfo->Xatom_wm_delete_window)
10060 {
10061 struct frame *f
10062 = x_any_window_to_frame (dpyinfo,
10063 event.xclient.window);
10064
10065 if (f)
10066 {
10067 if (numchars == 0)
10068 abort ();
10069
10070 bufp->kind = delete_window_event;
10071 XSETFRAME (bufp->frame_or_window, f);
10072 bufp->arg = Qnil;
10073 bufp++;
10074
10075 count += 1;
10076 numchars -= 1;
10077 }
10078 }
10079 }
10080 else if (event.xclient.message_type
10081 == dpyinfo->Xatom_wm_configure_denied)
10082 {
10083 }
10084 else if (event.xclient.message_type
10085 == dpyinfo->Xatom_wm_window_moved)
10086 {
10087 int new_x, new_y;
10088 struct frame *f
10089 = x_window_to_frame (dpyinfo, event.xclient.window);
10090
10091 new_x = event.xclient.data.s[0];
10092 new_y = event.xclient.data.s[1];
10093
10094 if (f)
10095 {
10096 f->output_data.x->left_pos = new_x;
10097 f->output_data.x->top_pos = new_y;
10098 }
10099 }
10100 #ifdef HACK_EDITRES
10101 else if (event.xclient.message_type
10102 == dpyinfo->Xatom_editres)
10103 {
10104 struct frame *f
10105 = x_any_window_to_frame (dpyinfo, event.xclient.window);
10106 _XEditResCheckMessages (f->output_data.x->widget, NULL,
10107 &event, NULL);
10108 }
10109 #endif /* HACK_EDITRES */
10110 else if ((event.xclient.message_type
10111 == dpyinfo->Xatom_DONE)
10112 || (event.xclient.message_type
10113 == dpyinfo->Xatom_PAGE))
10114 {
10115 /* Ghostview job completed. Kill it. We could
10116 reply with "Next" if we received "Page", but we
10117 currently never do because we are interested in
10118 images, only, which should have 1 page. */
10119 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
10120 struct frame *f
10121 = x_window_to_frame (dpyinfo, event.xclient.window);
10122 x_kill_gs_process (pixmap, f);
10123 expose_frame (f, 0, 0, 0, 0);
10124 }
10125 #ifdef USE_TOOLKIT_SCROLL_BARS
10126 /* Scroll bar callbacks send a ClientMessage from which
10127 we construct an input_event. */
10128 else if (event.xclient.message_type
10129 == dpyinfo->Xatom_Scrollbar)
10130 {
10131 x_scroll_bar_to_input_event (&event, bufp);
10132 ++bufp, ++count, --numchars;
10133 goto out;
10134 }
10135 #endif /* USE_TOOLKIT_SCROLL_BARS */
10136 else
10137 goto OTHER;
10138 }
10139 break;
10140
10141 case SelectionNotify:
10142 #ifdef USE_X_TOOLKIT
10143 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
10144 goto OTHER;
10145 #endif /* not USE_X_TOOLKIT */
10146 x_handle_selection_notify (&event.xselection);
10147 break;
10148
10149 case SelectionClear: /* Someone has grabbed ownership. */
10150 #ifdef USE_X_TOOLKIT
10151 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
10152 goto OTHER;
10153 #endif /* USE_X_TOOLKIT */
10154 {
10155 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
10156
10157 if (numchars == 0)
10158 abort ();
10159
10160 bufp->kind = selection_clear_event;
10161 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10162 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10163 SELECTION_EVENT_TIME (bufp) = eventp->time;
10164 bufp->frame_or_window = Qnil;
10165 bufp->arg = Qnil;
10166 bufp++;
10167
10168 count += 1;
10169 numchars -= 1;
10170 }
10171 break;
10172
10173 case SelectionRequest: /* Someone wants our selection. */
10174 #ifdef USE_X_TOOLKIT
10175 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
10176 goto OTHER;
10177 #endif /* USE_X_TOOLKIT */
10178 if (x_queue_selection_requests)
10179 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
10180 &event);
10181 else
10182 {
10183 XSelectionRequestEvent *eventp
10184 = (XSelectionRequestEvent *) &event;
10185
10186 if (numchars == 0)
10187 abort ();
10188
10189 bufp->kind = selection_request_event;
10190 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10191 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
10192 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10193 SELECTION_EVENT_TARGET (bufp) = eventp->target;
10194 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
10195 SELECTION_EVENT_TIME (bufp) = eventp->time;
10196 bufp->frame_or_window = Qnil;
10197 bufp->arg = Qnil;
10198 bufp++;
10199
10200 count += 1;
10201 numchars -= 1;
10202 }
10203 break;
10204
10205 case PropertyNotify:
10206 #if 0 /* This is plain wrong. In the case that we are waiting for a
10207 PropertyNotify used as an ACK in incremental selection
10208 transfer, the property will be on the receiver's window. */
10209 #if defined USE_X_TOOLKIT
10210 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
10211 goto OTHER;
10212 #endif
10213 #endif
10214 x_handle_property_notify (&event.xproperty);
10215 goto OTHER;
10216
10217 case ReparentNotify:
10218 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
10219 if (f)
10220 {
10221 int x, y;
10222 f->output_data.x->parent_desc = event.xreparent.parent;
10223 x_real_positions (f, &x, &y);
10224 f->output_data.x->left_pos = x;
10225 f->output_data.x->top_pos = y;
10226 }
10227 break;
10228
10229 case Expose:
10230 f = x_window_to_frame (dpyinfo, event.xexpose.window);
10231 if (f)
10232 {
10233 if (f->async_visible == 0)
10234 {
10235 f->async_visible = 1;
10236 f->async_iconified = 0;
10237 f->output_data.x->has_been_visible = 1;
10238 SET_FRAME_GARBAGED (f);
10239 }
10240 else
10241 expose_frame (x_window_to_frame (dpyinfo,
10242 event.xexpose.window),
10243 event.xexpose.x, event.xexpose.y,
10244 event.xexpose.width, event.xexpose.height);
10245 }
10246 else
10247 {
10248 #ifndef USE_TOOLKIT_SCROLL_BARS
10249 struct scroll_bar *bar;
10250 #endif
10251 #if defined USE_LUCID
10252 /* Submenus of the Lucid menu bar aren't widgets
10253 themselves, so there's no way to dispatch events
10254 to them. Recognize this case separately. */
10255 {
10256 Widget widget
10257 = x_window_to_menu_bar (event.xexpose.window);
10258 if (widget)
10259 xlwmenu_redisplay (widget);
10260 }
10261 #endif /* USE_LUCID */
10262
10263 #ifdef USE_TOOLKIT_SCROLL_BARS
10264 /* Dispatch event to the widget. */
10265 goto OTHER;
10266 #else /* not USE_TOOLKIT_SCROLL_BARS */
10267 bar = x_window_to_scroll_bar (event.xexpose.window);
10268
10269 if (bar)
10270 x_scroll_bar_expose (bar, &event);
10271 #ifdef USE_X_TOOLKIT
10272 else
10273 goto OTHER;
10274 #endif /* USE_X_TOOLKIT */
10275 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10276 }
10277 break;
10278
10279 case GraphicsExpose: /* This occurs when an XCopyArea's
10280 source area was obscured or not
10281 available. */
10282 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
10283 if (f)
10284 {
10285 expose_frame (f,
10286 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
10287 event.xgraphicsexpose.width,
10288 event.xgraphicsexpose.height);
10289 }
10290 #ifdef USE_X_TOOLKIT
10291 else
10292 goto OTHER;
10293 #endif /* USE_X_TOOLKIT */
10294 break;
10295
10296 case NoExpose: /* This occurs when an XCopyArea's
10297 source area was completely
10298 available. */
10299 break;
10300
10301 case UnmapNotify:
10302 /* Redo the mouse-highlight after the tooltip has gone. */
10303 if (event.xmap.window == tip_window)
10304 {
10305 tip_window = 0;
10306 redo_mouse_highlight ();
10307 }
10308
10309 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
10310 if (f) /* F may no longer exist if
10311 the frame was deleted. */
10312 {
10313 /* While a frame is unmapped, display generation is
10314 disabled; you don't want to spend time updating a
10315 display that won't ever be seen. */
10316 f->async_visible = 0;
10317 /* We can't distinguish, from the event, whether the window
10318 has become iconified or invisible. So assume, if it
10319 was previously visible, than now it is iconified.
10320 But x_make_frame_invisible clears both
10321 the visible flag and the iconified flag;
10322 and that way, we know the window is not iconified now. */
10323 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
10324 {
10325 f->async_iconified = 1;
10326
10327 bufp->kind = iconify_event;
10328 XSETFRAME (bufp->frame_or_window, f);
10329 bufp->arg = Qnil;
10330 bufp++;
10331 count++;
10332 numchars--;
10333 }
10334 }
10335 goto OTHER;
10336
10337 case MapNotify:
10338 if (event.xmap.window == tip_window)
10339 /* The tooltip has been drawn already. Avoid
10340 the SET_FRAME_GARBAGED below. */
10341 goto OTHER;
10342
10343 /* We use x_top_window_to_frame because map events can
10344 come for sub-windows and they don't mean that the
10345 frame is visible. */
10346 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
10347 if (f)
10348 {
10349 f->async_visible = 1;
10350 f->async_iconified = 0;
10351 f->output_data.x->has_been_visible = 1;
10352
10353 /* wait_reading_process_input will notice this and update
10354 the frame's display structures. */
10355 SET_FRAME_GARBAGED (f);
10356
10357 if (f->iconified)
10358 {
10359 bufp->kind = deiconify_event;
10360 XSETFRAME (bufp->frame_or_window, f);
10361 bufp->arg = Qnil;
10362 bufp++;
10363 count++;
10364 numchars--;
10365 }
10366 else if (! NILP (Vframe_list)
10367 && ! NILP (XCDR (Vframe_list)))
10368 /* Force a redisplay sooner or later
10369 to update the frame titles
10370 in case this is the second frame. */
10371 record_asynch_buffer_change ();
10372 }
10373 goto OTHER;
10374
10375 case KeyPress:
10376 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
10377
10378 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
10379 if (f == 0)
10380 {
10381 /* Scroll bars consume key events, but we want
10382 the keys to go to the scroll bar's frame. */
10383 Widget widget = XtWindowToWidget (dpyinfo->display,
10384 event.xkey.window);
10385 if (widget && XmIsScrollBar (widget))
10386 {
10387 widget = XtParent (widget);
10388 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
10389 }
10390 }
10391 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
10392
10393 if (f != 0)
10394 {
10395 KeySym keysym, orig_keysym;
10396 /* al%imercury@uunet.uu.net says that making this 81
10397 instead of 80 fixed a bug whereby meta chars made
10398 his Emacs hang.
10399
10400 It seems that some version of XmbLookupString has
10401 a bug of not returning XBufferOverflow in
10402 status_return even if the input is too long to
10403 fit in 81 bytes. So, we must prepare sufficient
10404 bytes for copy_buffer. 513 bytes (256 chars for
10405 two-byte character set) seems to be a faily good
10406 approximation. -- 2000.8.10 handa@etl.go.jp */
10407 unsigned char copy_buffer[513];
10408 unsigned char *copy_bufptr = copy_buffer;
10409 int copy_bufsiz = sizeof (copy_buffer);
10410 int modifiers;
10411
10412 event.xkey.state
10413 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
10414 extra_keyboard_modifiers);
10415 modifiers = event.xkey.state;
10416
10417 /* This will have to go some day... */
10418
10419 /* make_lispy_event turns chars into control chars.
10420 Don't do it here because XLookupString is too eager. */
10421 event.xkey.state &= ~ControlMask;
10422 event.xkey.state &= ~(dpyinfo->meta_mod_mask
10423 | dpyinfo->super_mod_mask
10424 | dpyinfo->hyper_mod_mask
10425 | dpyinfo->alt_mod_mask);
10426
10427 /* In case Meta is ComposeCharacter,
10428 clear its status. According to Markus Ehrnsperger
10429 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
10430 this enables ComposeCharacter to work whether or
10431 not it is combined with Meta. */
10432 if (modifiers & dpyinfo->meta_mod_mask)
10433 bzero (&compose_status, sizeof (compose_status));
10434
10435 #ifdef HAVE_X_I18N
10436 if (FRAME_XIC (f))
10437 {
10438 Status status_return;
10439
10440 nbytes = XmbLookupString (FRAME_XIC (f),
10441 &event.xkey, copy_bufptr,
10442 copy_bufsiz, &keysym,
10443 &status_return);
10444 if (status_return == XBufferOverflow)
10445 {
10446 copy_bufsiz = nbytes + 1;
10447 copy_bufptr = (char *) alloca (copy_bufsiz);
10448 nbytes = XmbLookupString (FRAME_XIC (f),
10449 &event.xkey, copy_bufptr,
10450 copy_bufsiz, &keysym,
10451 &status_return);
10452 }
10453
10454 if (status_return == XLookupNone)
10455 break;
10456 else if (status_return == XLookupChars)
10457 {
10458 keysym = NoSymbol;
10459 modifiers = 0;
10460 }
10461 else if (status_return != XLookupKeySym
10462 && status_return != XLookupBoth)
10463 abort ();
10464 }
10465 else
10466 nbytes = XLookupString (&event.xkey, copy_bufptr,
10467 copy_bufsiz, &keysym,
10468 &compose_status);
10469 #else
10470 nbytes = XLookupString (&event.xkey, copy_bufptr,
10471 copy_bufsiz, &keysym,
10472 &compose_status);
10473 #endif
10474
10475 orig_keysym = keysym;
10476
10477 if (numchars > 1)
10478 {
10479 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
10480 || keysym == XK_Delete
10481 #ifdef XK_ISO_Left_Tab
10482 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
10483 #endif
10484 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
10485 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
10486 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
10487 #ifdef HPUX
10488 /* This recognizes the "extended function keys".
10489 It seems there's no cleaner way.
10490 Test IsModifierKey to avoid handling mode_switch
10491 incorrectly. */
10492 || ((unsigned) (keysym) >= XK_Select
10493 && (unsigned)(keysym) < XK_KP_Space)
10494 #endif
10495 #ifdef XK_dead_circumflex
10496 || orig_keysym == XK_dead_circumflex
10497 #endif
10498 #ifdef XK_dead_grave
10499 || orig_keysym == XK_dead_grave
10500 #endif
10501 #ifdef XK_dead_tilde
10502 || orig_keysym == XK_dead_tilde
10503 #endif
10504 #ifdef XK_dead_diaeresis
10505 || orig_keysym == XK_dead_diaeresis
10506 #endif
10507 #ifdef XK_dead_macron
10508 || orig_keysym == XK_dead_macron
10509 #endif
10510 #ifdef XK_dead_degree
10511 || orig_keysym == XK_dead_degree
10512 #endif
10513 #ifdef XK_dead_acute
10514 || orig_keysym == XK_dead_acute
10515 #endif
10516 #ifdef XK_dead_cedilla
10517 || orig_keysym == XK_dead_cedilla
10518 #endif
10519 #ifdef XK_dead_breve
10520 || orig_keysym == XK_dead_breve
10521 #endif
10522 #ifdef XK_dead_ogonek
10523 || orig_keysym == XK_dead_ogonek
10524 #endif
10525 #ifdef XK_dead_caron
10526 || orig_keysym == XK_dead_caron
10527 #endif
10528 #ifdef XK_dead_doubleacute
10529 || orig_keysym == XK_dead_doubleacute
10530 #endif
10531 #ifdef XK_dead_abovedot
10532 || orig_keysym == XK_dead_abovedot
10533 #endif
10534 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10535 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10536 /* Any "vendor-specific" key is ok. */
10537 || (orig_keysym & (1 << 28))
10538 || (keysym != NoSymbol && nbytes == 0))
10539 && ! (IsModifierKey (orig_keysym)
10540 #ifndef HAVE_X11R5
10541 #ifdef XK_Mode_switch
10542 || ((unsigned)(orig_keysym) == XK_Mode_switch)
10543 #endif
10544 #ifdef XK_Num_Lock
10545 || ((unsigned)(orig_keysym) == XK_Num_Lock)
10546 #endif
10547 #endif /* not HAVE_X11R5 */
10548 ))
10549 {
10550 if (temp_index == sizeof temp_buffer / sizeof (short))
10551 temp_index = 0;
10552 temp_buffer[temp_index++] = keysym;
10553 bufp->kind = non_ascii_keystroke;
10554 bufp->code = keysym;
10555 XSETFRAME (bufp->frame_or_window, f);
10556 bufp->arg = Qnil;
10557 bufp->modifiers
10558 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10559 modifiers);
10560 bufp->timestamp = event.xkey.time;
10561 bufp++;
10562 count++;
10563 numchars--;
10564 }
10565 else if (numchars > nbytes)
10566 {
10567 register int i;
10568 register int c;
10569 int nchars, len;
10570
10571 for (i = 0; i < nbytes; i++)
10572 {
10573 if (temp_index == (sizeof temp_buffer
10574 / sizeof (short)))
10575 temp_index = 0;
10576 temp_buffer[temp_index++] = copy_bufptr[i];
10577 }
10578
10579 if (/* If the event is not from XIM, */
10580 event.xkey.keycode != 0
10581 /* or the current locale doesn't request
10582 decoding of the intup data, ... */
10583 || coding.type == coding_type_raw_text
10584 || coding.type == coding_type_no_conversion)
10585 {
10586 /* ... we can use the input data as is. */
10587 nchars = nbytes;
10588 }
10589 else
10590 {
10591 /* We have to decode the input data. */
10592 int require;
10593 unsigned char *p;
10594
10595 require = decoding_buffer_size (&coding, nbytes);
10596 p = (unsigned char *) alloca (require);
10597 coding.mode |= CODING_MODE_LAST_BLOCK;
10598 decode_coding (&coding, copy_bufptr, p,
10599 nbytes, require);
10600 nbytes = coding.produced;
10601 nchars = coding.produced_char;
10602 copy_bufptr = p;
10603 }
10604
10605 /* Convert the input data to a sequence of
10606 character events. */
10607 for (i = 0; i < nbytes; i += len)
10608 {
10609 if (nchars == nbytes)
10610 c = copy_bufptr[i], len = 1;
10611 else
10612 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10613 nbytes - i, len);
10614
10615 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
10616 ? ascii_keystroke
10617 : multibyte_char_keystroke);
10618 bufp->code = c;
10619 XSETFRAME (bufp->frame_or_window, f);
10620 bufp->arg = Qnil;
10621 bufp->modifiers
10622 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10623 modifiers);
10624 bufp->timestamp = event.xkey.time;
10625 bufp++;
10626 }
10627
10628 count += nchars;
10629 numchars -= nchars;
10630
10631 if (keysym == NoSymbol)
10632 break;
10633 }
10634 else
10635 abort ();
10636 }
10637 else
10638 abort ();
10639 }
10640 #ifdef HAVE_X_I18N
10641 /* Don't dispatch this event since XtDispatchEvent calls
10642 XFilterEvent, and two calls in a row may freeze the
10643 client. */
10644 break;
10645 #else
10646 goto OTHER;
10647 #endif
10648
10649 case KeyRelease:
10650 #ifdef HAVE_X_I18N
10651 /* Don't dispatch this event since XtDispatchEvent calls
10652 XFilterEvent, and two calls in a row may freeze the
10653 client. */
10654 break;
10655 #else
10656 goto OTHER;
10657 #endif
10658
10659 /* Here's a possible interpretation of the whole
10660 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
10661 you get a FocusIn event, you have to get a FocusOut
10662 event before you relinquish the focus. If you
10663 haven't received a FocusIn event, then a mere
10664 LeaveNotify is enough to free you. */
10665
10666 case EnterNotify:
10667 {
10668 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10669
10670 #if 0
10671 if (event.xcrossing.focus)
10672 {
10673 /* Avoid nasty pop/raise loops. */
10674 if (f && (!(f->auto_raise)
10675 || !(f->auto_lower)
10676 || (event.xcrossing.time - enter_timestamp) > 500))
10677 {
10678 x_new_focus_frame (dpyinfo, f);
10679 enter_timestamp = event.xcrossing.time;
10680 }
10681 }
10682 else if (f == dpyinfo->x_focus_frame)
10683 x_new_focus_frame (dpyinfo, 0);
10684 #endif
10685
10686 /* EnterNotify counts as mouse movement,
10687 so update things that depend on mouse position. */
10688 if (f && !f->output_data.x->hourglass_p)
10689 note_mouse_movement (f, &event.xmotion);
10690 goto OTHER;
10691 }
10692
10693 case FocusIn:
10694 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10695 if (event.xfocus.detail != NotifyPointer)
10696 dpyinfo->x_focus_event_frame = f;
10697 if (f)
10698 {
10699 x_new_focus_frame (dpyinfo, f);
10700
10701 /* Don't stop displaying the initial startup message
10702 for a switch-frame event we don't need. */
10703 if (GC_NILP (Vterminal_frame)
10704 && GC_CONSP (Vframe_list)
10705 && !GC_NILP (XCDR (Vframe_list)))
10706 {
10707 bufp->kind = FOCUS_IN_EVENT;
10708 XSETFRAME (bufp->frame_or_window, f);
10709 bufp->arg = Qnil;
10710 ++bufp, ++count, --numchars;
10711 }
10712 }
10713
10714 #ifdef HAVE_X_I18N
10715 if (f && FRAME_XIC (f))
10716 XSetICFocus (FRAME_XIC (f));
10717 #endif
10718
10719 goto OTHER;
10720
10721 case LeaveNotify:
10722 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
10723 if (f)
10724 {
10725 if (f == dpyinfo->mouse_face_mouse_frame)
10726 {
10727 /* If we move outside the frame, then we're
10728 certainly no longer on any text in the frame. */
10729 clear_mouse_face (dpyinfo);
10730 dpyinfo->mouse_face_mouse_frame = 0;
10731 }
10732
10733 /* Generate a nil HELP_EVENT to cancel a help-echo.
10734 Do it only if there's something to cancel.
10735 Otherwise, the startup message is cleared when
10736 the mouse leaves the frame. */
10737 if (any_help_event_p)
10738 {
10739 Lisp_Object frame;
10740 int n;
10741
10742 XSETFRAME (frame, f);
10743 help_echo = Qnil;
10744 n = gen_help_event (bufp, numchars,
10745 Qnil, frame, Qnil, Qnil, 0);
10746 bufp += n, count += n, numchars -= n;
10747 }
10748
10749 #if 0
10750 if (event.xcrossing.focus)
10751 x_mouse_leave (dpyinfo);
10752 else
10753 {
10754 if (f == dpyinfo->x_focus_event_frame)
10755 dpyinfo->x_focus_event_frame = 0;
10756 if (f == dpyinfo->x_focus_frame)
10757 x_new_focus_frame (dpyinfo, 0);
10758 }
10759 #endif
10760 }
10761 goto OTHER;
10762
10763 case FocusOut:
10764 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10765 if (event.xfocus.detail != NotifyPointer
10766 && f == dpyinfo->x_focus_event_frame)
10767 dpyinfo->x_focus_event_frame = 0;
10768 if (f && f == dpyinfo->x_focus_frame)
10769 x_new_focus_frame (dpyinfo, 0);
10770
10771 #ifdef HAVE_X_I18N
10772 if (f && FRAME_XIC (f))
10773 XUnsetICFocus (FRAME_XIC (f));
10774 #endif
10775
10776 goto OTHER;
10777
10778 case MotionNotify:
10779 {
10780 previous_help_echo = help_echo;
10781 help_echo = help_echo_object = help_echo_window = Qnil;
10782 help_echo_pos = -1;
10783
10784 if (dpyinfo->grabbed && last_mouse_frame
10785 && FRAME_LIVE_P (last_mouse_frame))
10786 f = last_mouse_frame;
10787 else
10788 f = x_window_to_frame (dpyinfo, event.xmotion.window);
10789
10790 if (f)
10791 note_mouse_movement (f, &event.xmotion);
10792 else
10793 {
10794 #ifndef USE_TOOLKIT_SCROLL_BARS
10795 struct scroll_bar *bar
10796 = x_window_to_scroll_bar (event.xmotion.window);
10797
10798 if (bar)
10799 x_scroll_bar_note_movement (bar, &event);
10800 #endif /* USE_TOOLKIT_SCROLL_BARS */
10801
10802 /* If we move outside the frame, then we're
10803 certainly no longer on any text in the frame. */
10804 clear_mouse_face (dpyinfo);
10805 }
10806
10807 /* If the contents of the global variable help_echo
10808 has changed, generate a HELP_EVENT. */
10809 if (!NILP (help_echo)
10810 || !NILP (previous_help_echo))
10811 {
10812 Lisp_Object frame;
10813 int n;
10814
10815 if (f)
10816 XSETFRAME (frame, f);
10817 else
10818 frame = Qnil;
10819
10820 any_help_event_p = 1;
10821 n = gen_help_event (bufp, numchars, help_echo, frame,
10822 help_echo_window, help_echo_object,
10823 help_echo_pos);
10824 bufp += n, count += n, numchars -= n;
10825 }
10826
10827 goto OTHER;
10828 }
10829
10830 case ConfigureNotify:
10831 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10832 if (f)
10833 {
10834 #ifndef USE_X_TOOLKIT
10835 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10836 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
10837
10838 /* In the toolkit version, change_frame_size
10839 is called by the code that handles resizing
10840 of the EmacsFrame widget. */
10841
10842 /* Even if the number of character rows and columns has
10843 not changed, the font size may have changed, so we need
10844 to check the pixel dimensions as well. */
10845 if (columns != f->width
10846 || rows != f->height
10847 || event.xconfigure.width != f->output_data.x->pixel_width
10848 || event.xconfigure.height != f->output_data.x->pixel_height)
10849 {
10850 change_frame_size (f, rows, columns, 0, 1, 0);
10851 SET_FRAME_GARBAGED (f);
10852 cancel_mouse_face (f);
10853 }
10854 #endif
10855
10856 f->output_data.x->pixel_width = event.xconfigure.width;
10857 f->output_data.x->pixel_height = event.xconfigure.height;
10858
10859 /* What we have now is the position of Emacs's own window.
10860 Convert that to the position of the window manager window. */
10861 x_real_positions (f, &f->output_data.x->left_pos,
10862 &f->output_data.x->top_pos);
10863
10864 #ifdef HAVE_X_I18N
10865 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10866 xic_set_statusarea (f);
10867 #endif
10868
10869 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10870 {
10871 /* Since the WM decorations come below top_pos now,
10872 we must put them below top_pos in the future. */
10873 f->output_data.x->win_gravity = NorthWestGravity;
10874 x_wm_set_size_hint (f, (long) 0, 0);
10875 }
10876 #ifdef USE_MOTIF
10877 /* Some window managers pass (0,0) as the location of
10878 the window, and the Motif event handler stores it
10879 in the emacs widget, which messes up Motif menus. */
10880 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10881 {
10882 event.xconfigure.x = f->output_data.x->widget->core.x;
10883 event.xconfigure.y = f->output_data.x->widget->core.y;
10884 }
10885 #endif /* USE_MOTIF */
10886 }
10887 goto OTHER;
10888
10889 case ButtonPress:
10890 case ButtonRelease:
10891 {
10892 /* If we decide we want to generate an event to be seen
10893 by the rest of Emacs, we put it here. */
10894 struct input_event emacs_event;
10895 int tool_bar_p = 0;
10896
10897 emacs_event.kind = no_event;
10898 bzero (&compose_status, sizeof (compose_status));
10899
10900 if (dpyinfo->grabbed
10901 && last_mouse_frame
10902 && FRAME_LIVE_P (last_mouse_frame))
10903 f = last_mouse_frame;
10904 else
10905 f = x_window_to_frame (dpyinfo, event.xbutton.window);
10906
10907 if (f)
10908 {
10909 /* Is this in the tool-bar? */
10910 if (WINDOWP (f->tool_bar_window)
10911 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
10912 {
10913 Lisp_Object window;
10914 int p, x, y;
10915
10916 x = event.xbutton.x;
10917 y = event.xbutton.y;
10918
10919 /* Set x and y. */
10920 window = window_from_coordinates (f, x, y, &p, 1);
10921 if (EQ (window, f->tool_bar_window))
10922 {
10923 x_handle_tool_bar_click (f, &event.xbutton);
10924 tool_bar_p = 1;
10925 }
10926 }
10927
10928 if (!tool_bar_p)
10929 if (!dpyinfo->x_focus_frame
10930 || f == dpyinfo->x_focus_frame)
10931 construct_mouse_click (&emacs_event, &event, f);
10932 }
10933 else
10934 {
10935 #ifndef USE_TOOLKIT_SCROLL_BARS
10936 struct scroll_bar *bar
10937 = x_window_to_scroll_bar (event.xbutton.window);
10938
10939 if (bar)
10940 x_scroll_bar_handle_click (bar, &event, &emacs_event);
10941 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10942 }
10943
10944 if (event.type == ButtonPress)
10945 {
10946 dpyinfo->grabbed |= (1 << event.xbutton.button);
10947 last_mouse_frame = f;
10948 /* Ignore any mouse motion that happened
10949 before this event; any subsequent mouse-movement
10950 Emacs events should reflect only motion after
10951 the ButtonPress. */
10952 if (f != 0)
10953 f->mouse_moved = 0;
10954
10955 if (!tool_bar_p)
10956 last_tool_bar_item = -1;
10957 }
10958 else
10959 {
10960 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
10961 }
10962
10963 if (numchars >= 1 && emacs_event.kind != no_event)
10964 {
10965 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10966 bufp++;
10967 count++;
10968 numchars--;
10969 }
10970
10971 #ifdef USE_X_TOOLKIT
10972 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10973 /* For a down-event in the menu bar,
10974 don't pass it to Xt right now.
10975 Instead, save it away
10976 and we will pass it to Xt from kbd_buffer_get_event.
10977 That way, we can run some Lisp code first. */
10978 if (f && event.type == ButtonPress
10979 /* Verify the event is really within the menu bar
10980 and not just sent to it due to grabbing. */
10981 && event.xbutton.x >= 0
10982 && event.xbutton.x < f->output_data.x->pixel_width
10983 && event.xbutton.y >= 0
10984 && event.xbutton.y < f->output_data.x->menubar_height
10985 && event.xbutton.same_screen)
10986 {
10987 SET_SAVED_BUTTON_EVENT;
10988 XSETFRAME (last_mouse_press_frame, f);
10989 }
10990 else if (event.type == ButtonPress)
10991 {
10992 last_mouse_press_frame = Qnil;
10993 goto OTHER;
10994 }
10995
10996 #ifdef USE_MOTIF /* This should do not harm for Lucid,
10997 but I am trying to be cautious. */
10998 else if (event.type == ButtonRelease)
10999 {
11000 if (!NILP (last_mouse_press_frame))
11001 {
11002 f = XFRAME (last_mouse_press_frame);
11003 if (f->output_data.x)
11004 SET_SAVED_BUTTON_EVENT;
11005 }
11006 else
11007 goto OTHER;
11008 }
11009 #endif /* USE_MOTIF */
11010 else
11011 goto OTHER;
11012 #endif /* USE_X_TOOLKIT */
11013 }
11014 break;
11015
11016 case CirculateNotify:
11017 goto OTHER;
11018
11019 case CirculateRequest:
11020 goto OTHER;
11021
11022 case VisibilityNotify:
11023 goto OTHER;
11024
11025 case MappingNotify:
11026 /* Someone has changed the keyboard mapping - update the
11027 local cache. */
11028 switch (event.xmapping.request)
11029 {
11030 case MappingModifier:
11031 x_find_modifier_meanings (dpyinfo);
11032 /* This is meant to fall through. */
11033 case MappingKeyboard:
11034 XRefreshKeyboardMapping (&event.xmapping);
11035 }
11036 goto OTHER;
11037
11038 default:
11039 OTHER:
11040 #ifdef USE_X_TOOLKIT
11041 BLOCK_INPUT;
11042 XtDispatchEvent (&event);
11043 UNBLOCK_INPUT;
11044 #endif /* USE_X_TOOLKIT */
11045 break;
11046 }
11047 }
11048 }
11049
11050 out:;
11051
11052 /* On some systems, an X bug causes Emacs to get no more events
11053 when the window is destroyed. Detect that. (1994.) */
11054 if (! event_found)
11055 {
11056 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
11057 One XNOOP in 100 loops will make Emacs terminate.
11058 B. Bretthauer, 1994 */
11059 x_noop_count++;
11060 if (x_noop_count >= 100)
11061 {
11062 x_noop_count=0;
11063
11064 if (next_noop_dpyinfo == 0)
11065 next_noop_dpyinfo = x_display_list;
11066
11067 XNoOp (next_noop_dpyinfo->display);
11068
11069 /* Each time we get here, cycle through the displays now open. */
11070 next_noop_dpyinfo = next_noop_dpyinfo->next;
11071 }
11072 }
11073
11074 /* If the focus was just given to an auto-raising frame,
11075 raise it now. */
11076 /* ??? This ought to be able to handle more than one such frame. */
11077 if (pending_autoraise_frame)
11078 {
11079 x_raise_frame (pending_autoraise_frame);
11080 pending_autoraise_frame = 0;
11081 }
11082
11083 UNBLOCK_INPUT;
11084 --handling_signal;
11085 return count;
11086 }
11087
11088
11089
11090 \f
11091 /***********************************************************************
11092 Text Cursor
11093 ***********************************************************************/
11094
11095 /* Notice if the text cursor of window W has been overwritten by a
11096 drawing operation that outputs N glyphs starting at START_X and
11097 ending at END_X in the line given by output_cursor.vpos.
11098 Coordinates are area-relative. END_X < 0 means all the rest
11099 of the line after START_X has been written. */
11100
11101 static void
11102 notice_overwritten_cursor (w, start_x, end_x)
11103 struct window *w;
11104 int start_x, end_x;
11105 {
11106 if (updated_area == TEXT_AREA
11107 && w->phys_cursor_on_p
11108 && output_cursor.vpos == w->phys_cursor.vpos
11109 && start_x <= w->phys_cursor.x
11110 && (end_x < 0 || end_x > w->phys_cursor.x))
11111 w->phys_cursor_on_p = 0;
11112 }
11113
11114
11115 /* Set clipping for output in glyph row ROW. W is the window in which
11116 we operate. GC is the graphics context to set clipping in.
11117 WHOLE_LINE_P non-zero means include the areas used for truncation
11118 mark display and alike in the clipping rectangle.
11119
11120 ROW may be a text row or, e.g., a mode line. Text rows must be
11121 clipped to the interior of the window dedicated to text display,
11122 mode lines must be clipped to the whole window. */
11123
11124 static void
11125 x_clip_to_row (w, row, gc, whole_line_p)
11126 struct window *w;
11127 struct glyph_row *row;
11128 GC gc;
11129 int whole_line_p;
11130 {
11131 struct frame *f = XFRAME (WINDOW_FRAME (w));
11132 XRectangle clip_rect;
11133 int window_x, window_y, window_width, window_height;
11134
11135 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
11136
11137 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
11138 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
11139 clip_rect.y = max (clip_rect.y, window_y);
11140 clip_rect.width = window_width;
11141 clip_rect.height = row->visible_height;
11142
11143 /* If clipping to the whole line, including trunc marks, extend
11144 the rectangle to the left and increase its width. */
11145 if (whole_line_p)
11146 {
11147 clip_rect.x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
11148 clip_rect.width += FRAME_X_FRINGE_WIDTH (f);
11149 }
11150
11151 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
11152 }
11153
11154
11155 /* Draw a hollow box cursor on window W in glyph row ROW. */
11156
11157 static void
11158 x_draw_hollow_cursor (w, row)
11159 struct window *w;
11160 struct glyph_row *row;
11161 {
11162 struct frame *f = XFRAME (WINDOW_FRAME (w));
11163 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11164 Display *dpy = FRAME_X_DISPLAY (f);
11165 int x, y, wd, h;
11166 XGCValues xgcv;
11167 struct glyph *cursor_glyph;
11168 GC gc;
11169
11170 /* Compute frame-relative coordinates from window-relative
11171 coordinates. */
11172 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11173 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
11174 + row->ascent - w->phys_cursor_ascent);
11175 h = row->height - 1;
11176
11177 /* Get the glyph the cursor is on. If we can't tell because
11178 the current matrix is invalid or such, give up. */
11179 cursor_glyph = get_phys_cursor_glyph (w);
11180 if (cursor_glyph == NULL)
11181 return;
11182
11183 /* Compute the width of the rectangle to draw. If on a stretch
11184 glyph, and `x-stretch-block-cursor' is nil, don't draw a
11185 rectangle as wide as the glyph, but use a canonical character
11186 width instead. */
11187 wd = cursor_glyph->pixel_width - 1;
11188 if (cursor_glyph->type == STRETCH_GLYPH
11189 && !x_stretch_cursor_p)
11190 wd = min (CANON_X_UNIT (f), wd);
11191
11192 /* The foreground of cursor_gc is typically the same as the normal
11193 background color, which can cause the cursor box to be invisible. */
11194 xgcv.foreground = f->output_data.x->cursor_pixel;
11195 if (dpyinfo->scratch_cursor_gc)
11196 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
11197 else
11198 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
11199 GCForeground, &xgcv);
11200 gc = dpyinfo->scratch_cursor_gc;
11201
11202 /* Set clipping, draw the rectangle, and reset clipping again. */
11203 x_clip_to_row (w, row, gc, 0);
11204 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
11205 XSetClipMask (dpy, gc, None);
11206 }
11207
11208
11209 /* Draw a bar cursor on window W in glyph row ROW.
11210
11211 Implementation note: One would like to draw a bar cursor with an
11212 angle equal to the one given by the font property XA_ITALIC_ANGLE.
11213 Unfortunately, I didn't find a font yet that has this property set.
11214 --gerd. */
11215
11216 static void
11217 x_draw_bar_cursor (w, row, width)
11218 struct window *w;
11219 struct glyph_row *row;
11220 int width;
11221 {
11222 struct frame *f = XFRAME (w->frame);
11223 struct glyph *cursor_glyph;
11224
11225 /* If cursor is out of bounds, don't draw garbage. This can happen
11226 in mini-buffer windows when switching between echo area glyphs
11227 and mini-buffer. */
11228 cursor_glyph = get_phys_cursor_glyph (w);
11229 if (cursor_glyph == NULL)
11230 return;
11231
11232 /* If on an image, draw like a normal cursor. That's usually better
11233 visible than drawing a bar, esp. if the image is large so that
11234 the bar might not be in the window. */
11235 if (cursor_glyph->type == IMAGE_GLYPH)
11236 {
11237 struct glyph_row *row;
11238 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
11239 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
11240 }
11241 else
11242 {
11243 Display *dpy = FRAME_X_DISPLAY (f);
11244 Window window = FRAME_X_WINDOW (f);
11245 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
11246 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
11247 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
11248 XGCValues xgcv;
11249
11250 /* If the glyph's background equals the color we normally draw
11251 the bar cursor in, the bar cursor in its normal color is
11252 invisible. Use the glyph's foreground color instead in this
11253 case, on the assumption that the glyph's colors are chosen so
11254 that the glyph is legible. */
11255 if (face->background == f->output_data.x->cursor_pixel)
11256 xgcv.background = xgcv.foreground = face->foreground;
11257 else
11258 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
11259 xgcv.graphics_exposures = 0;
11260
11261 if (gc)
11262 XChangeGC (dpy, gc, mask, &xgcv);
11263 else
11264 {
11265 gc = XCreateGC (dpy, window, mask, &xgcv);
11266 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
11267 }
11268
11269 if (width < 0)
11270 width = f->output_data.x->cursor_width;
11271 width = min (cursor_glyph->pixel_width, width);
11272
11273 x_clip_to_row (w, row, gc, 0);
11274 XFillRectangle (dpy, window, gc,
11275 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11276 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
11277 width, row->height);
11278 XSetClipMask (dpy, gc, None);
11279 }
11280 }
11281
11282
11283 /* Clear the cursor of window W to background color, and mark the
11284 cursor as not shown. This is used when the text where the cursor
11285 is is about to be rewritten. */
11286
11287 static void
11288 x_clear_cursor (w)
11289 struct window *w;
11290 {
11291 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
11292 x_update_window_cursor (w, 0);
11293 }
11294
11295
11296 /* Draw the cursor glyph of window W in glyph row ROW. See the
11297 comment of x_draw_glyphs for the meaning of HL. */
11298
11299 static void
11300 x_draw_phys_cursor_glyph (w, row, hl)
11301 struct window *w;
11302 struct glyph_row *row;
11303 enum draw_glyphs_face hl;
11304 {
11305 /* If cursor hpos is out of bounds, don't draw garbage. This can
11306 happen in mini-buffer windows when switching between echo area
11307 glyphs and mini-buffer. */
11308 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
11309 {
11310 int on_p = w->phys_cursor_on_p;
11311
11312 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
11313 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
11314 hl, 0);
11315 w->phys_cursor_on_p = on_p;
11316
11317 /* When we erase the cursor, and ROW is overlapped by other
11318 rows, make sure that these overlapping parts of other rows
11319 are redrawn. */
11320 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
11321 {
11322 if (row > w->current_matrix->rows
11323 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
11324 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
11325
11326 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
11327 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
11328 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
11329 }
11330 }
11331 }
11332
11333
11334 /* Erase the image of a cursor of window W from the screen. */
11335
11336 static void
11337 x_erase_phys_cursor (w)
11338 struct window *w;
11339 {
11340 struct frame *f = XFRAME (w->frame);
11341 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11342 int hpos = w->phys_cursor.hpos;
11343 int vpos = w->phys_cursor.vpos;
11344 int mouse_face_here_p = 0;
11345 struct glyph_matrix *active_glyphs = w->current_matrix;
11346 struct glyph_row *cursor_row;
11347 struct glyph *cursor_glyph;
11348 enum draw_glyphs_face hl;
11349
11350 /* No cursor displayed or row invalidated => nothing to do on the
11351 screen. */
11352 if (w->phys_cursor_type == NO_CURSOR)
11353 goto mark_cursor_off;
11354
11355 /* VPOS >= active_glyphs->nrows means that window has been resized.
11356 Don't bother to erase the cursor. */
11357 if (vpos >= active_glyphs->nrows)
11358 goto mark_cursor_off;
11359
11360 /* If row containing cursor is marked invalid, there is nothing we
11361 can do. */
11362 cursor_row = MATRIX_ROW (active_glyphs, vpos);
11363 if (!cursor_row->enabled_p)
11364 goto mark_cursor_off;
11365
11366 /* This can happen when the new row is shorter than the old one.
11367 In this case, either x_draw_glyphs or clear_end_of_line
11368 should have cleared the cursor. Note that we wouldn't be
11369 able to erase the cursor in this case because we don't have a
11370 cursor glyph at hand. */
11371 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
11372 goto mark_cursor_off;
11373
11374 /* If the cursor is in the mouse face area, redisplay that when
11375 we clear the cursor. */
11376 if (! NILP (dpyinfo->mouse_face_window)
11377 && w == XWINDOW (dpyinfo->mouse_face_window)
11378 && (vpos > dpyinfo->mouse_face_beg_row
11379 || (vpos == dpyinfo->mouse_face_beg_row
11380 && hpos >= dpyinfo->mouse_face_beg_col))
11381 && (vpos < dpyinfo->mouse_face_end_row
11382 || (vpos == dpyinfo->mouse_face_end_row
11383 && hpos < dpyinfo->mouse_face_end_col))
11384 /* Don't redraw the cursor's spot in mouse face if it is at the
11385 end of a line (on a newline). The cursor appears there, but
11386 mouse highlighting does not. */
11387 && cursor_row->used[TEXT_AREA] > hpos)
11388 mouse_face_here_p = 1;
11389
11390 /* Maybe clear the display under the cursor. */
11391 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
11392 {
11393 int x;
11394 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11395
11396 cursor_glyph = get_phys_cursor_glyph (w);
11397 if (cursor_glyph == NULL)
11398 goto mark_cursor_off;
11399
11400 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11401
11402 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11403 x,
11404 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
11405 cursor_row->y)),
11406 cursor_glyph->pixel_width,
11407 cursor_row->visible_height,
11408 False);
11409 }
11410
11411 /* Erase the cursor by redrawing the character underneath it. */
11412 if (mouse_face_here_p)
11413 hl = DRAW_MOUSE_FACE;
11414 else
11415 hl = DRAW_NORMAL_TEXT;
11416 x_draw_phys_cursor_glyph (w, cursor_row, hl);
11417
11418 mark_cursor_off:
11419 w->phys_cursor_on_p = 0;
11420 w->phys_cursor_type = NO_CURSOR;
11421 }
11422
11423
11424 /* Non-zero if physical cursor of window W is within mouse face. */
11425
11426 static int
11427 cursor_in_mouse_face_p (w)
11428 struct window *w;
11429 {
11430 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
11431 int in_mouse_face = 0;
11432
11433 if (WINDOWP (dpyinfo->mouse_face_window)
11434 && XWINDOW (dpyinfo->mouse_face_window) == w)
11435 {
11436 int hpos = w->phys_cursor.hpos;
11437 int vpos = w->phys_cursor.vpos;
11438
11439 if (vpos >= dpyinfo->mouse_face_beg_row
11440 && vpos <= dpyinfo->mouse_face_end_row
11441 && (vpos > dpyinfo->mouse_face_beg_row
11442 || hpos >= dpyinfo->mouse_face_beg_col)
11443 && (vpos < dpyinfo->mouse_face_end_row
11444 || hpos < dpyinfo->mouse_face_end_col
11445 || dpyinfo->mouse_face_past_end))
11446 in_mouse_face = 1;
11447 }
11448
11449 return in_mouse_face;
11450 }
11451
11452
11453 /* Display or clear cursor of window W. If ON is zero, clear the
11454 cursor. If it is non-zero, display the cursor. If ON is nonzero,
11455 where to put the cursor is specified by HPOS, VPOS, X and Y. */
11456
11457 void
11458 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
11459 struct window *w;
11460 int on, hpos, vpos, x, y;
11461 {
11462 struct frame *f = XFRAME (w->frame);
11463 int new_cursor_type;
11464 int new_cursor_width;
11465 struct glyph_matrix *current_glyphs;
11466 struct glyph_row *glyph_row;
11467 struct glyph *glyph;
11468 int cursor_non_selected;
11469
11470 /* This is pointless on invisible frames, and dangerous on garbaged
11471 windows and frames; in the latter case, the frame or window may
11472 be in the midst of changing its size, and x and y may be off the
11473 window. */
11474 if (! FRAME_VISIBLE_P (f)
11475 || FRAME_GARBAGED_P (f)
11476 || vpos >= w->current_matrix->nrows
11477 || hpos >= w->current_matrix->matrix_w)
11478 return;
11479
11480 /* If cursor is off and we want it off, return quickly. */
11481 if (!on && !w->phys_cursor_on_p)
11482 return;
11483
11484 current_glyphs = w->current_matrix;
11485 glyph_row = MATRIX_ROW (current_glyphs, vpos);
11486 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
11487
11488 /* If cursor row is not enabled, we don't really know where to
11489 display the cursor. */
11490 if (!glyph_row->enabled_p)
11491 {
11492 w->phys_cursor_on_p = 0;
11493 return;
11494 }
11495
11496 xassert (interrupt_input_blocked);
11497
11498 /* Set new_cursor_type to the cursor we want to be displayed. In a
11499 mini-buffer window, we want the cursor only to appear if we are
11500 reading input from this window. For the selected window, we want
11501 the cursor type given by the frame parameter. If explicitly
11502 marked off, draw no cursor. In all other cases, we want a hollow
11503 box cursor. */
11504 cursor_non_selected
11505 = !NILP (Fbuffer_local_value (Qcursor_in_non_selected_windows,
11506 w->buffer));
11507 new_cursor_width = -1;
11508 if (cursor_in_echo_area
11509 && FRAME_HAS_MINIBUF_P (f)
11510 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
11511 {
11512 if (w == XWINDOW (echo_area_window))
11513 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11514 else if (cursor_non_selected)
11515 new_cursor_type = HOLLOW_BOX_CURSOR;
11516 else
11517 new_cursor_type = NO_CURSOR;
11518 }
11519 else
11520 {
11521 if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
11522 || w != XWINDOW (f->selected_window))
11523 {
11524 if ((MINI_WINDOW_P (w) && minibuf_level == 0)
11525 || !cursor_non_selected
11526 || NILP (XBUFFER (w->buffer)->cursor_type))
11527 new_cursor_type = NO_CURSOR;
11528 else
11529 new_cursor_type = HOLLOW_BOX_CURSOR;
11530 }
11531 else if (w->cursor_off_p)
11532 new_cursor_type = NO_CURSOR;
11533 else
11534 {
11535 struct buffer *b = XBUFFER (w->buffer);
11536
11537 if (EQ (b->cursor_type, Qt))
11538 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11539 else
11540 new_cursor_type = x_specified_cursor_type (b->cursor_type,
11541 &new_cursor_width);
11542 }
11543 }
11544
11545 /* If cursor is currently being shown and we don't want it to be or
11546 it is in the wrong place, or the cursor type is not what we want,
11547 erase it. */
11548 if (w->phys_cursor_on_p
11549 && (!on
11550 || w->phys_cursor.x != x
11551 || w->phys_cursor.y != y
11552 || new_cursor_type != w->phys_cursor_type))
11553 x_erase_phys_cursor (w);
11554
11555 /* If the cursor is now invisible and we want it to be visible,
11556 display it. */
11557 if (on && !w->phys_cursor_on_p)
11558 {
11559 w->phys_cursor_ascent = glyph_row->ascent;
11560 w->phys_cursor_height = glyph_row->height;
11561
11562 /* Set phys_cursor_.* before x_draw_.* is called because some
11563 of them may need the information. */
11564 w->phys_cursor.x = x;
11565 w->phys_cursor.y = glyph_row->y;
11566 w->phys_cursor.hpos = hpos;
11567 w->phys_cursor.vpos = vpos;
11568 w->phys_cursor_type = new_cursor_type;
11569 w->phys_cursor_on_p = 1;
11570
11571 switch (new_cursor_type)
11572 {
11573 case HOLLOW_BOX_CURSOR:
11574 x_draw_hollow_cursor (w, glyph_row);
11575 break;
11576
11577 case FILLED_BOX_CURSOR:
11578 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
11579 break;
11580
11581 case BAR_CURSOR:
11582 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
11583 break;
11584
11585 case NO_CURSOR:
11586 break;
11587
11588 default:
11589 abort ();
11590 }
11591
11592 #ifdef HAVE_X_I18N
11593 if (w == XWINDOW (f->selected_window))
11594 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
11595 xic_set_preeditarea (w, x, y);
11596 #endif
11597 }
11598
11599 #ifndef XFlush
11600 if (updating_frame != f)
11601 XFlush (FRAME_X_DISPLAY (f));
11602 #endif
11603 }
11604
11605
11606 /* Display the cursor on window W, or clear it. X and Y are window
11607 relative pixel coordinates. HPOS and VPOS are glyph matrix
11608 positions. If W is not the selected window, display a hollow
11609 cursor. ON non-zero means display the cursor at X, Y which
11610 correspond to HPOS, VPOS, otherwise it is cleared. */
11611
11612 void
11613 x_display_cursor (w, on, hpos, vpos, x, y)
11614 struct window *w;
11615 int on, hpos, vpos, x, y;
11616 {
11617 BLOCK_INPUT;
11618 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
11619 UNBLOCK_INPUT;
11620 }
11621
11622
11623 /* Display the cursor on window W, or clear it, according to ON_P.
11624 Don't change the cursor's position. */
11625
11626 void
11627 x_update_cursor (f, on_p)
11628 struct frame *f;
11629 {
11630 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11631 }
11632
11633
11634 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
11635 in the window tree rooted at W. */
11636
11637 static void
11638 x_update_cursor_in_window_tree (w, on_p)
11639 struct window *w;
11640 int on_p;
11641 {
11642 while (w)
11643 {
11644 if (!NILP (w->hchild))
11645 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11646 else if (!NILP (w->vchild))
11647 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11648 else
11649 x_update_window_cursor (w, on_p);
11650
11651 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11652 }
11653 }
11654
11655
11656 /* Switch the display of W's cursor on or off, according to the value
11657 of ON. */
11658
11659 static void
11660 x_update_window_cursor (w, on)
11661 struct window *w;
11662 int on;
11663 {
11664 /* Don't update cursor in windows whose frame is in the process
11665 of being deleted. */
11666 if (w->current_matrix)
11667 {
11668 BLOCK_INPUT;
11669 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11670 w->phys_cursor.x, w->phys_cursor.y);
11671 UNBLOCK_INPUT;
11672 }
11673 }
11674
11675
11676
11677 \f
11678 /* Icons. */
11679
11680 /* Make the x-window of frame F use the gnu icon bitmap. */
11681
11682 int
11683 x_bitmap_icon (f, file)
11684 struct frame *f;
11685 Lisp_Object file;
11686 {
11687 int bitmap_id;
11688
11689 if (FRAME_X_WINDOW (f) == 0)
11690 return 1;
11691
11692 /* Free up our existing icon bitmap if any. */
11693 if (f->output_data.x->icon_bitmap > 0)
11694 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11695 f->output_data.x->icon_bitmap = 0;
11696
11697 if (STRINGP (file))
11698 bitmap_id = x_create_bitmap_from_file (f, file);
11699 else
11700 {
11701 /* Create the GNU bitmap if necessary. */
11702 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
11703 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11704 = x_create_bitmap_from_data (f, gnu_bits,
11705 gnu_width, gnu_height);
11706
11707 /* The first time we create the GNU bitmap,
11708 this increments the ref-count one extra time.
11709 As a result, the GNU bitmap is never freed.
11710 That way, we don't have to worry about allocating it again. */
11711 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
11712
11713 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
11714 }
11715
11716 x_wm_set_icon_pixmap (f, bitmap_id);
11717 f->output_data.x->icon_bitmap = bitmap_id;
11718
11719 return 0;
11720 }
11721
11722
11723 /* Make the x-window of frame F use a rectangle with text.
11724 Use ICON_NAME as the text. */
11725
11726 int
11727 x_text_icon (f, icon_name)
11728 struct frame *f;
11729 char *icon_name;
11730 {
11731 if (FRAME_X_WINDOW (f) == 0)
11732 return 1;
11733
11734 #ifdef HAVE_X11R4
11735 {
11736 XTextProperty text;
11737 text.value = (unsigned char *) icon_name;
11738 text.encoding = XA_STRING;
11739 text.format = 8;
11740 text.nitems = strlen (icon_name);
11741 #ifdef USE_X_TOOLKIT
11742 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11743 &text);
11744 #else /* not USE_X_TOOLKIT */
11745 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11746 #endif /* not USE_X_TOOLKIT */
11747 }
11748 #else /* not HAVE_X11R4 */
11749 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11750 #endif /* not HAVE_X11R4 */
11751
11752 if (f->output_data.x->icon_bitmap > 0)
11753 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11754 f->output_data.x->icon_bitmap = 0;
11755 x_wm_set_icon_pixmap (f, 0);
11756
11757 return 0;
11758 }
11759 \f
11760 #define X_ERROR_MESSAGE_SIZE 200
11761
11762 /* If non-nil, this should be a string.
11763 It means catch X errors and store the error message in this string. */
11764
11765 static Lisp_Object x_error_message_string;
11766
11767 /* An X error handler which stores the error message in
11768 x_error_message_string. This is called from x_error_handler if
11769 x_catch_errors is in effect. */
11770
11771 static void
11772 x_error_catcher (display, error)
11773 Display *display;
11774 XErrorEvent *error;
11775 {
11776 XGetErrorText (display, error->error_code,
11777 XSTRING (x_error_message_string)->data,
11778 X_ERROR_MESSAGE_SIZE);
11779 }
11780
11781 /* Begin trapping X errors for display DPY. Actually we trap X errors
11782 for all displays, but DPY should be the display you are actually
11783 operating on.
11784
11785 After calling this function, X protocol errors no longer cause
11786 Emacs to exit; instead, they are recorded in the string
11787 stored in x_error_message_string.
11788
11789 Calling x_check_errors signals an Emacs error if an X error has
11790 occurred since the last call to x_catch_errors or x_check_errors.
11791
11792 Calling x_uncatch_errors resumes the normal error handling. */
11793
11794 void x_check_errors ();
11795 static Lisp_Object x_catch_errors_unwind ();
11796
11797 int
11798 x_catch_errors (dpy)
11799 Display *dpy;
11800 {
11801 int count = specpdl_ptr - specpdl;
11802
11803 /* Make sure any errors from previous requests have been dealt with. */
11804 XSync (dpy, False);
11805
11806 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11807
11808 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11809 XSTRING (x_error_message_string)->data[0] = 0;
11810
11811 return count;
11812 }
11813
11814 /* Unbind the binding that we made to check for X errors. */
11815
11816 static Lisp_Object
11817 x_catch_errors_unwind (old_val)
11818 Lisp_Object old_val;
11819 {
11820 x_error_message_string = old_val;
11821 return Qnil;
11822 }
11823
11824 /* If any X protocol errors have arrived since the last call to
11825 x_catch_errors or x_check_errors, signal an Emacs error using
11826 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11827
11828 void
11829 x_check_errors (dpy, format)
11830 Display *dpy;
11831 char *format;
11832 {
11833 /* Make sure to catch any errors incurred so far. */
11834 XSync (dpy, False);
11835
11836 if (XSTRING (x_error_message_string)->data[0])
11837 error (format, XSTRING (x_error_message_string)->data);
11838 }
11839
11840 /* Nonzero if we had any X protocol errors
11841 since we did x_catch_errors on DPY. */
11842
11843 int
11844 x_had_errors_p (dpy)
11845 Display *dpy;
11846 {
11847 /* Make sure to catch any errors incurred so far. */
11848 XSync (dpy, False);
11849
11850 return XSTRING (x_error_message_string)->data[0] != 0;
11851 }
11852
11853 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11854
11855 void
11856 x_clear_errors (dpy)
11857 Display *dpy;
11858 {
11859 XSTRING (x_error_message_string)->data[0] = 0;
11860 }
11861
11862 /* Stop catching X protocol errors and let them make Emacs die.
11863 DPY should be the display that was passed to x_catch_errors.
11864 COUNT should be the value that was returned by
11865 the corresponding call to x_catch_errors. */
11866
11867 void
11868 x_uncatch_errors (dpy, count)
11869 Display *dpy;
11870 int count;
11871 {
11872 unbind_to (count, Qnil);
11873 }
11874
11875 #if 0
11876 static unsigned int x_wire_count;
11877 x_trace_wire ()
11878 {
11879 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11880 }
11881 #endif /* ! 0 */
11882
11883 \f
11884 /* Handle SIGPIPE, which can happen when the connection to a server
11885 simply goes away. SIGPIPE is handled by x_connection_signal.
11886 Don't need to do anything, because the write which caused the
11887 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
11888 which will do the appropriate cleanup for us. */
11889
11890 static SIGTYPE
11891 x_connection_signal (signalnum) /* If we don't have an argument, */
11892 int signalnum; /* some compilers complain in signal calls. */
11893 {
11894 #ifdef USG
11895 /* USG systems forget handlers when they are used;
11896 must reestablish each time */
11897 signal (signalnum, x_connection_signal);
11898 #endif /* USG */
11899 }
11900
11901 \f
11902 /************************************************************************
11903 Handling X errors
11904 ************************************************************************/
11905
11906 /* Error message passed to x_connection_closed. */
11907
11908 static char *error_msg;
11909
11910 /* Function installed as fatal_error_signal_hook in
11911 x_connection_closed. Print the X error message, and exit normally,
11912 instead of dumping core when XtCloseDisplay fails. */
11913
11914 static void
11915 x_fatal_error_signal ()
11916 {
11917 fprintf (stderr, "%s\n", error_msg);
11918 exit (70);
11919 }
11920
11921 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
11922 the text of an error message that lead to the connection loss. */
11923
11924 static SIGTYPE
11925 x_connection_closed (dpy, error_message)
11926 Display *dpy;
11927 char *error_message;
11928 {
11929 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
11930 Lisp_Object frame, tail;
11931 int count;
11932
11933 error_msg = (char *) alloca (strlen (error_message) + 1);
11934 strcpy (error_msg, error_message);
11935 handling_signal = 0;
11936
11937 /* Prevent being called recursively because of an error condition
11938 below. Otherwise, we might end up with printing ``can't find per
11939 display information'' in the recursive call instead of printing
11940 the original message here. */
11941 count = x_catch_errors (dpy);
11942
11943 /* We have to close the display to inform Xt that it doesn't
11944 exist anymore. If we don't, Xt will continue to wait for
11945 events from the display. As a consequence, a sequence of
11946
11947 M-x make-frame-on-display RET :1 RET
11948 ...kill the new frame, so that we get an IO error...
11949 M-x make-frame-on-display RET :1 RET
11950
11951 will indefinitely wait in Xt for events for display `:1', opened
11952 in the first class to make-frame-on-display.
11953
11954 Closing the display is reported to lead to a bus error on
11955 OpenWindows in certain situations. I suspect that is a bug
11956 in OpenWindows. I don't know how to cicumvent it here. */
11957
11958 #ifdef USE_X_TOOLKIT
11959 /* If DPYINFO is null, this means we didn't open the display
11960 in the first place, so don't try to close it. */
11961 if (dpyinfo)
11962 {
11963 extern void (*fatal_error_signal_hook) P_ ((void));
11964 fatal_error_signal_hook = x_fatal_error_signal;
11965 XtCloseDisplay (dpy);
11966 fatal_error_signal_hook = NULL;
11967 }
11968 #endif
11969
11970 /* Indicate that this display is dead. */
11971 if (dpyinfo)
11972 dpyinfo->display = 0;
11973
11974 /* First delete frames whose mini-buffers are on frames
11975 that are on the dead display. */
11976 FOR_EACH_FRAME (tail, frame)
11977 {
11978 Lisp_Object minibuf_frame;
11979 minibuf_frame
11980 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
11981 if (FRAME_X_P (XFRAME (frame))
11982 && FRAME_X_P (XFRAME (minibuf_frame))
11983 && ! EQ (frame, minibuf_frame)
11984 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
11985 Fdelete_frame (frame, Qt);
11986 }
11987
11988 /* Now delete all remaining frames on the dead display.
11989 We are now sure none of these is used as the mini-buffer
11990 for another frame that we need to delete. */
11991 FOR_EACH_FRAME (tail, frame)
11992 if (FRAME_X_P (XFRAME (frame))
11993 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
11994 {
11995 /* Set this to t so that Fdelete_frame won't get confused
11996 trying to find a replacement. */
11997 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11998 Fdelete_frame (frame, Qt);
11999 }
12000
12001 if (dpyinfo)
12002 x_delete_display (dpyinfo);
12003
12004 x_uncatch_errors (dpy, count);
12005
12006 if (x_display_list == 0)
12007 {
12008 fprintf (stderr, "%s\n", error_msg);
12009 shut_down_emacs (0, 0, Qnil);
12010 exit (70);
12011 }
12012
12013 /* Ordinary stack unwind doesn't deal with these. */
12014 #ifdef SIGIO
12015 sigunblock (sigmask (SIGIO));
12016 #endif
12017 sigunblock (sigmask (SIGALRM));
12018 TOTALLY_UNBLOCK_INPUT;
12019
12020 clear_waiting_for_input ();
12021 error ("%s", error_msg);
12022 }
12023
12024
12025 /* This is the usual handler for X protocol errors.
12026 It kills all frames on the display that we got the error for.
12027 If that was the only one, it prints an error message and kills Emacs. */
12028
12029 static void
12030 x_error_quitter (display, error)
12031 Display *display;
12032 XErrorEvent *error;
12033 {
12034 char buf[256], buf1[356];
12035
12036 /* Note that there is no real way portable across R3/R4 to get the
12037 original error handler. */
12038
12039 XGetErrorText (display, error->error_code, buf, sizeof (buf));
12040 sprintf (buf1, "X protocol error: %s on protocol request %d",
12041 buf, error->request_code);
12042 x_connection_closed (display, buf1);
12043 }
12044
12045
12046 /* This is the first-level handler for X protocol errors.
12047 It calls x_error_quitter or x_error_catcher. */
12048
12049 static int
12050 x_error_handler (display, error)
12051 Display *display;
12052 XErrorEvent *error;
12053 {
12054 if (! NILP (x_error_message_string))
12055 x_error_catcher (display, error);
12056 else
12057 x_error_quitter (display, error);
12058 return 0;
12059 }
12060
12061 /* This is the handler for X IO errors, always.
12062 It kills all frames on the display that we lost touch with.
12063 If that was the only one, it prints an error message and kills Emacs. */
12064
12065 static int
12066 x_io_error_quitter (display)
12067 Display *display;
12068 {
12069 char buf[256];
12070
12071 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
12072 x_connection_closed (display, buf);
12073 return 0;
12074 }
12075 \f
12076 /* Changing the font of the frame. */
12077
12078 /* Give frame F the font named FONTNAME as its default font, and
12079 return the full name of that font. FONTNAME may be a wildcard
12080 pattern; in that case, we choose some font that fits the pattern.
12081 The return value shows which font we chose. */
12082
12083 Lisp_Object
12084 x_new_font (f, fontname)
12085 struct frame *f;
12086 register char *fontname;
12087 {
12088 struct font_info *fontp
12089 = FS_LOAD_FONT (f, 0, fontname, -1);
12090
12091 if (!fontp)
12092 return Qnil;
12093
12094 f->output_data.x->font = (XFontStruct *) (fontp->font);
12095 f->output_data.x->baseline_offset = fontp->baseline_offset;
12096 f->output_data.x->fontset = -1;
12097
12098 /* Compute the scroll bar width in character columns. */
12099 if (f->scroll_bar_pixel_width > 0)
12100 {
12101 int wid = FONT_WIDTH (f->output_data.x->font);
12102 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
12103 }
12104 else
12105 {
12106 int wid = FONT_WIDTH (f->output_data.x->font);
12107 f->scroll_bar_cols = (14 + wid - 1) / wid;
12108 }
12109
12110 /* Now make the frame display the given font. */
12111 if (FRAME_X_WINDOW (f) != 0)
12112 {
12113 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
12114 f->output_data.x->font->fid);
12115 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
12116 f->output_data.x->font->fid);
12117 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
12118 f->output_data.x->font->fid);
12119
12120 frame_update_line_height (f);
12121
12122 /* Don't change the size of a tip frame; there's no point in
12123 doing it because it's done in Fx_show_tip, and it leads to
12124 problems because the tip frame has no widget. */
12125 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
12126 x_set_window_size (f, 0, f->width, f->height);
12127 }
12128 else
12129 /* If we are setting a new frame's font for the first time,
12130 there are no faces yet, so this font's height is the line height. */
12131 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
12132
12133 return build_string (fontp->full_name);
12134 }
12135
12136 /* Give frame F the fontset named FONTSETNAME as its default font, and
12137 return the full name of that fontset. FONTSETNAME may be a wildcard
12138 pattern; in that case, we choose some fontset that fits the pattern.
12139 The return value shows which fontset we chose. */
12140
12141 Lisp_Object
12142 x_new_fontset (f, fontsetname)
12143 struct frame *f;
12144 char *fontsetname;
12145 {
12146 int fontset = fs_query_fontset (build_string (fontsetname), 0);
12147 Lisp_Object result;
12148
12149 if (fontset < 0)
12150 return Qnil;
12151
12152 if (f->output_data.x->fontset == fontset)
12153 /* This fontset is already set in frame F. There's nothing more
12154 to do. */
12155 return fontset_name (fontset);
12156
12157 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
12158
12159 if (!STRINGP (result))
12160 /* Can't load ASCII font. */
12161 return Qnil;
12162
12163 /* Since x_new_font doesn't update any fontset information, do it now. */
12164 f->output_data.x->fontset = fontset;
12165
12166 #ifdef HAVE_X_I18N
12167 if (FRAME_XIC (f)
12168 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
12169 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
12170 #endif
12171
12172 return build_string (fontsetname);
12173 }
12174
12175 \f
12176 /***********************************************************************
12177 X Input Methods
12178 ***********************************************************************/
12179
12180 #ifdef HAVE_X_I18N
12181
12182 #ifdef HAVE_X11R6
12183
12184 /* XIM destroy callback function, which is called whenever the
12185 connection to input method XIM dies. CLIENT_DATA contains a
12186 pointer to the x_display_info structure corresponding to XIM. */
12187
12188 static void
12189 xim_destroy_callback (xim, client_data, call_data)
12190 XIM xim;
12191 XPointer client_data;
12192 XPointer call_data;
12193 {
12194 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
12195 Lisp_Object frame, tail;
12196
12197 BLOCK_INPUT;
12198
12199 /* No need to call XDestroyIC.. */
12200 FOR_EACH_FRAME (tail, frame)
12201 {
12202 struct frame *f = XFRAME (frame);
12203 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
12204 {
12205 FRAME_XIC (f) = NULL;
12206 if (FRAME_XIC_FONTSET (f))
12207 {
12208 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
12209 FRAME_XIC_FONTSET (f) = NULL;
12210 }
12211 }
12212 }
12213
12214 /* No need to call XCloseIM. */
12215 dpyinfo->xim = NULL;
12216 XFree (dpyinfo->xim_styles);
12217 UNBLOCK_INPUT;
12218 }
12219
12220 #endif /* HAVE_X11R6 */
12221
12222 /* Open the connection to the XIM server on display DPYINFO.
12223 RESOURCE_NAME is the resource name Emacs uses. */
12224
12225 static void
12226 xim_open_dpy (dpyinfo, resource_name)
12227 struct x_display_info *dpyinfo;
12228 char *resource_name;
12229 {
12230 #ifdef USE_XIM
12231 XIM xim;
12232
12233 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
12234 dpyinfo->xim = xim;
12235
12236 if (xim)
12237 {
12238 #ifdef HAVE_X11R6
12239 XIMCallback destroy;
12240 #endif
12241
12242 /* Get supported styles and XIM values. */
12243 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
12244
12245 #ifdef HAVE_X11R6
12246 destroy.callback = xim_destroy_callback;
12247 destroy.client_data = (XPointer)dpyinfo;
12248 /* This isn't prototyped in OSF 5.0. */
12249 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
12250 #endif
12251 }
12252
12253 #else /* not USE_XIM */
12254 dpyinfo->xim = NULL;
12255 #endif /* not USE_XIM */
12256 }
12257
12258
12259 #ifdef HAVE_X11R6_XIM
12260
12261 struct xim_inst_t
12262 {
12263 struct x_display_info *dpyinfo;
12264 char *resource_name;
12265 };
12266
12267 /* XIM instantiate callback function, which is called whenever an XIM
12268 server is available. DISPLAY is teh display of the XIM.
12269 CLIENT_DATA contains a pointer to an xim_inst_t structure created
12270 when the callback was registered. */
12271
12272 static void
12273 xim_instantiate_callback (display, client_data, call_data)
12274 Display *display;
12275 XPointer client_data;
12276 XPointer call_data;
12277 {
12278 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
12279 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
12280
12281 /* We don't support multiple XIM connections. */
12282 if (dpyinfo->xim)
12283 return;
12284
12285 xim_open_dpy (dpyinfo, xim_inst->resource_name);
12286
12287 /* Create XIC for the existing frames on the same display, as long
12288 as they have no XIC. */
12289 if (dpyinfo->xim && dpyinfo->reference_count > 0)
12290 {
12291 Lisp_Object tail, frame;
12292
12293 BLOCK_INPUT;
12294 FOR_EACH_FRAME (tail, frame)
12295 {
12296 struct frame *f = XFRAME (frame);
12297
12298 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
12299 if (FRAME_XIC (f) == NULL)
12300 {
12301 create_frame_xic (f);
12302 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
12303 xic_set_statusarea (f);
12304 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
12305 {
12306 struct window *w = XWINDOW (f->selected_window);
12307 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
12308 }
12309 }
12310 }
12311
12312 UNBLOCK_INPUT;
12313 }
12314 }
12315
12316 #endif /* HAVE_X11R6_XIM */
12317
12318
12319 /* Open a connection to the XIM server on display DPYINFO.
12320 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
12321 connection only at the first time. On X11R6, open the connection
12322 in the XIM instantiate callback function. */
12323
12324 static void
12325 xim_initialize (dpyinfo, resource_name)
12326 struct x_display_info *dpyinfo;
12327 char *resource_name;
12328 {
12329 #ifdef USE_XIM
12330 #ifdef HAVE_X11R6_XIM
12331 struct xim_inst_t *xim_inst;
12332 int len;
12333
12334 dpyinfo->xim = NULL;
12335 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
12336 xim_inst->dpyinfo = dpyinfo;
12337 len = strlen (resource_name);
12338 xim_inst->resource_name = (char *) xmalloc (len + 1);
12339 bcopy (resource_name, xim_inst->resource_name, len + 1);
12340 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12341 resource_name, EMACS_CLASS,
12342 xim_instantiate_callback,
12343 /* Fixme: This is XPointer in
12344 XFree86 but (XPointer *) on
12345 Tru64, at least. */
12346 (XPointer) xim_inst);
12347 #else /* not HAVE_X11R6_XIM */
12348 dpyinfo->xim = NULL;
12349 xim_open_dpy (dpyinfo, resource_name);
12350 #endif /* not HAVE_X11R6_XIM */
12351
12352 #else /* not USE_XIM */
12353 dpyinfo->xim = NULL;
12354 #endif /* not USE_XIM */
12355 }
12356
12357
12358 /* Close the connection to the XIM server on display DPYINFO. */
12359
12360 static void
12361 xim_close_dpy (dpyinfo)
12362 struct x_display_info *dpyinfo;
12363 {
12364 #ifdef USE_XIM
12365 #ifdef HAVE_X11R6_XIM
12366 if (dpyinfo->display)
12367 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12368 NULL, EMACS_CLASS,
12369 xim_instantiate_callback, NULL);
12370 #endif /* not HAVE_X11R6_XIM */
12371 if (dpyinfo->display)
12372 XCloseIM (dpyinfo->xim);
12373 dpyinfo->xim = NULL;
12374 XFree (dpyinfo->xim_styles);
12375 #endif /* USE_XIM */
12376 }
12377
12378 #endif /* not HAVE_X11R6_XIM */
12379
12380
12381 \f
12382 /* Calculate the absolute position in frame F
12383 from its current recorded position values and gravity. */
12384
12385 void
12386 x_calc_absolute_position (f)
12387 struct frame *f;
12388 {
12389 Window child;
12390 int win_x = 0, win_y = 0;
12391 int flags = f->output_data.x->size_hint_flags;
12392 int this_window;
12393
12394 /* We have nothing to do if the current position
12395 is already for the top-left corner. */
12396 if (! ((flags & XNegative) || (flags & YNegative)))
12397 return;
12398
12399 #ifdef USE_X_TOOLKIT
12400 this_window = XtWindow (f->output_data.x->widget);
12401 #else
12402 this_window = FRAME_X_WINDOW (f);
12403 #endif
12404
12405 /* Find the position of the outside upper-left corner of
12406 the inner window, with respect to the outer window.
12407 But do this only if we will need the results. */
12408 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
12409 {
12410 int count;
12411
12412 BLOCK_INPUT;
12413 count = x_catch_errors (FRAME_X_DISPLAY (f));
12414 while (1)
12415 {
12416 x_clear_errors (FRAME_X_DISPLAY (f));
12417 XTranslateCoordinates (FRAME_X_DISPLAY (f),
12418
12419 /* From-window, to-window. */
12420 this_window,
12421 f->output_data.x->parent_desc,
12422
12423 /* From-position, to-position. */
12424 0, 0, &win_x, &win_y,
12425
12426 /* Child of win. */
12427 &child);
12428 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12429 {
12430 Window newroot, newparent = 0xdeadbeef;
12431 Window *newchildren;
12432 unsigned int nchildren;
12433
12434 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
12435 &newparent, &newchildren, &nchildren))
12436 break;
12437
12438 XFree ((char *) newchildren);
12439
12440 f->output_data.x->parent_desc = newparent;
12441 }
12442 else
12443 break;
12444 }
12445
12446 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12447 UNBLOCK_INPUT;
12448 }
12449
12450 /* Treat negative positions as relative to the leftmost bottommost
12451 position that fits on the screen. */
12452 if (flags & XNegative)
12453 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
12454 - 2 * f->output_data.x->border_width - win_x
12455 - PIXEL_WIDTH (f)
12456 + f->output_data.x->left_pos);
12457
12458 {
12459 int height = PIXEL_HEIGHT (f);
12460
12461 #if defined USE_X_TOOLKIT && defined USE_MOTIF
12462 /* Something is fishy here. When using Motif, starting Emacs with
12463 `-g -0-0', the frame appears too low by a few pixels.
12464
12465 This seems to be so because initially, while Emacs is starting,
12466 the column widget's height and the frame's pixel height are
12467 different. The column widget's height is the right one. In
12468 later invocations, when Emacs is up, the frame's pixel height
12469 is right, though.
12470
12471 It's not obvious where the initial small difference comes from.
12472 2000-12-01, gerd. */
12473
12474 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
12475 #endif
12476
12477 if (flags & YNegative)
12478 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
12479 - 2 * f->output_data.x->border_width
12480 - win_y
12481 - height
12482 + f->output_data.x->top_pos);
12483 }
12484
12485 /* The left_pos and top_pos
12486 are now relative to the top and left screen edges,
12487 so the flags should correspond. */
12488 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12489 }
12490
12491 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
12492 to really change the position, and 0 when calling from
12493 x_make_frame_visible (in that case, XOFF and YOFF are the current
12494 position values). It is -1 when calling from x_set_frame_parameters,
12495 which means, do adjust for borders but don't change the gravity. */
12496
12497 void
12498 x_set_offset (f, xoff, yoff, change_gravity)
12499 struct frame *f;
12500 register int xoff, yoff;
12501 int change_gravity;
12502 {
12503 int modified_top, modified_left;
12504
12505 if (change_gravity > 0)
12506 {
12507 f->output_data.x->top_pos = yoff;
12508 f->output_data.x->left_pos = xoff;
12509 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12510 if (xoff < 0)
12511 f->output_data.x->size_hint_flags |= XNegative;
12512 if (yoff < 0)
12513 f->output_data.x->size_hint_flags |= YNegative;
12514 f->output_data.x->win_gravity = NorthWestGravity;
12515 }
12516 x_calc_absolute_position (f);
12517
12518 BLOCK_INPUT;
12519 x_wm_set_size_hint (f, (long) 0, 0);
12520
12521 modified_left = f->output_data.x->left_pos;
12522 modified_top = f->output_data.x->top_pos;
12523 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
12524 this seems to be unnecessary and incorrect. rms, 4/17/97. */
12525 /* It is a mystery why we need to add the border_width here
12526 when the frame is already visible, but experiment says we do. */
12527 if (change_gravity != 0)
12528 {
12529 modified_left += f->output_data.x->border_width;
12530 modified_top += f->output_data.x->border_width;
12531 }
12532 #endif
12533
12534 #ifdef USE_X_TOOLKIT
12535 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
12536 modified_left, modified_top);
12537 #else /* not USE_X_TOOLKIT */
12538 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12539 modified_left, modified_top);
12540 #endif /* not USE_X_TOOLKIT */
12541 UNBLOCK_INPUT;
12542 }
12543
12544
12545 /* Change the size of frame F's X window to COLS/ROWS in the case F
12546 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
12547 top-left-corner window gravity for this size change and subsequent
12548 size changes. Otherwise we leave the window gravity unchanged. */
12549
12550 static void
12551 x_set_window_size_1 (f, change_gravity, cols, rows)
12552 struct frame *f;
12553 int change_gravity;
12554 int cols, rows;
12555 {
12556 int pixelwidth, pixelheight;
12557
12558 check_frame_size (f, &rows, &cols);
12559 f->output_data.x->vertical_scroll_bar_extra
12560 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
12561 ? 0
12562 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
12563 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
12564 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
12565 f->output_data.x->fringes_extra
12566 = FRAME_FRINGE_WIDTH (f);
12567 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
12568 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
12569
12570 f->output_data.x->win_gravity = NorthWestGravity;
12571 x_wm_set_size_hint (f, (long) 0, 0);
12572
12573 XSync (FRAME_X_DISPLAY (f), False);
12574 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12575 pixelwidth, pixelheight);
12576
12577 /* Now, strictly speaking, we can't be sure that this is accurate,
12578 but the window manager will get around to dealing with the size
12579 change request eventually, and we'll hear how it went when the
12580 ConfigureNotify event gets here.
12581
12582 We could just not bother storing any of this information here,
12583 and let the ConfigureNotify event set everything up, but that
12584 might be kind of confusing to the Lisp code, since size changes
12585 wouldn't be reported in the frame parameters until some random
12586 point in the future when the ConfigureNotify event arrives.
12587
12588 We pass 1 for DELAY since we can't run Lisp code inside of
12589 a BLOCK_INPUT. */
12590 change_frame_size (f, rows, cols, 0, 1, 0);
12591 PIXEL_WIDTH (f) = pixelwidth;
12592 PIXEL_HEIGHT (f) = pixelheight;
12593
12594 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
12595 receive in the ConfigureNotify event; if we get what we asked
12596 for, then the event won't cause the screen to become garbaged, so
12597 we have to make sure to do it here. */
12598 SET_FRAME_GARBAGED (f);
12599
12600 XFlush (FRAME_X_DISPLAY (f));
12601 }
12602
12603
12604 /* Call this to change the size of frame F's x-window.
12605 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
12606 for this size change and subsequent size changes.
12607 Otherwise we leave the window gravity unchanged. */
12608
12609 void
12610 x_set_window_size (f, change_gravity, cols, rows)
12611 struct frame *f;
12612 int change_gravity;
12613 int cols, rows;
12614 {
12615 BLOCK_INPUT;
12616
12617 #ifdef USE_X_TOOLKIT
12618
12619 if (f->output_data.x->widget != NULL)
12620 {
12621 /* The x and y position of the widget is clobbered by the
12622 call to XtSetValues within EmacsFrameSetCharSize.
12623 This is a real kludge, but I don't understand Xt so I can't
12624 figure out a correct fix. Can anyone else tell me? -- rms. */
12625 int xpos = f->output_data.x->widget->core.x;
12626 int ypos = f->output_data.x->widget->core.y;
12627 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
12628 f->output_data.x->widget->core.x = xpos;
12629 f->output_data.x->widget->core.y = ypos;
12630 }
12631 else
12632 x_set_window_size_1 (f, change_gravity, cols, rows);
12633
12634 #else /* not USE_X_TOOLKIT */
12635
12636 x_set_window_size_1 (f, change_gravity, cols, rows);
12637
12638 #endif /* not USE_X_TOOLKIT */
12639
12640 /* If cursor was outside the new size, mark it as off. */
12641 mark_window_cursors_off (XWINDOW (f->root_window));
12642
12643 /* Clear out any recollection of where the mouse highlighting was,
12644 since it might be in a place that's outside the new frame size.
12645 Actually checking whether it is outside is a pain in the neck,
12646 so don't try--just let the highlighting be done afresh with new size. */
12647 cancel_mouse_face (f);
12648
12649 UNBLOCK_INPUT;
12650 }
12651 \f
12652 /* Mouse warping. */
12653
12654 void
12655 x_set_mouse_position (f, x, y)
12656 struct frame *f;
12657 int x, y;
12658 {
12659 int pix_x, pix_y;
12660
12661 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
12662 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
12663
12664 if (pix_x < 0) pix_x = 0;
12665 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
12666
12667 if (pix_y < 0) pix_y = 0;
12668 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
12669
12670 BLOCK_INPUT;
12671
12672 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12673 0, 0, 0, 0, pix_x, pix_y);
12674 UNBLOCK_INPUT;
12675 }
12676
12677 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
12678
12679 void
12680 x_set_mouse_pixel_position (f, pix_x, pix_y)
12681 struct frame *f;
12682 int pix_x, pix_y;
12683 {
12684 BLOCK_INPUT;
12685
12686 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12687 0, 0, 0, 0, pix_x, pix_y);
12688 UNBLOCK_INPUT;
12689 }
12690 \f
12691 /* focus shifting, raising and lowering. */
12692
12693 void
12694 x_focus_on_frame (f)
12695 struct frame *f;
12696 {
12697 #if 0 /* This proves to be unpleasant. */
12698 x_raise_frame (f);
12699 #endif
12700 #if 0
12701 /* I don't think that the ICCCM allows programs to do things like this
12702 without the interaction of the window manager. Whatever you end up
12703 doing with this code, do it to x_unfocus_frame too. */
12704 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12705 RevertToPointerRoot, CurrentTime);
12706 #endif /* ! 0 */
12707 }
12708
12709 void
12710 x_unfocus_frame (f)
12711 struct frame *f;
12712 {
12713 #if 0
12714 /* Look at the remarks in x_focus_on_frame. */
12715 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
12716 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
12717 RevertToPointerRoot, CurrentTime);
12718 #endif /* ! 0 */
12719 }
12720
12721 /* Raise frame F. */
12722
12723 void
12724 x_raise_frame (f)
12725 struct frame *f;
12726 {
12727 if (f->async_visible)
12728 {
12729 BLOCK_INPUT;
12730 #ifdef USE_X_TOOLKIT
12731 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12732 #else /* not USE_X_TOOLKIT */
12733 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12734 #endif /* not USE_X_TOOLKIT */
12735 XFlush (FRAME_X_DISPLAY (f));
12736 UNBLOCK_INPUT;
12737 }
12738 }
12739
12740 /* Lower frame F. */
12741
12742 void
12743 x_lower_frame (f)
12744 struct frame *f;
12745 {
12746 if (f->async_visible)
12747 {
12748 BLOCK_INPUT;
12749 #ifdef USE_X_TOOLKIT
12750 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12751 #else /* not USE_X_TOOLKIT */
12752 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12753 #endif /* not USE_X_TOOLKIT */
12754 XFlush (FRAME_X_DISPLAY (f));
12755 UNBLOCK_INPUT;
12756 }
12757 }
12758
12759 static void
12760 XTframe_raise_lower (f, raise_flag)
12761 FRAME_PTR f;
12762 int raise_flag;
12763 {
12764 if (raise_flag)
12765 x_raise_frame (f);
12766 else
12767 x_lower_frame (f);
12768 }
12769 \f
12770 /* Change of visibility. */
12771
12772 /* This tries to wait until the frame is really visible.
12773 However, if the window manager asks the user where to position
12774 the frame, this will return before the user finishes doing that.
12775 The frame will not actually be visible at that time,
12776 but it will become visible later when the window manager
12777 finishes with it. */
12778
12779 void
12780 x_make_frame_visible (f)
12781 struct frame *f;
12782 {
12783 Lisp_Object type;
12784 int original_top, original_left;
12785 int retry_count = 2;
12786
12787 retry:
12788
12789 BLOCK_INPUT;
12790
12791 type = x_icon_type (f);
12792 if (!NILP (type))
12793 x_bitmap_icon (f, type);
12794
12795 if (! FRAME_VISIBLE_P (f))
12796 {
12797 /* We test FRAME_GARBAGED_P here to make sure we don't
12798 call x_set_offset a second time
12799 if we get to x_make_frame_visible a second time
12800 before the window gets really visible. */
12801 if (! FRAME_ICONIFIED_P (f)
12802 && ! f->output_data.x->asked_for_visible)
12803 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12804
12805 f->output_data.x->asked_for_visible = 1;
12806
12807 if (! EQ (Vx_no_window_manager, Qt))
12808 x_wm_set_window_state (f, NormalState);
12809 #ifdef USE_X_TOOLKIT
12810 /* This was XtPopup, but that did nothing for an iconified frame. */
12811 XtMapWidget (f->output_data.x->widget);
12812 #else /* not USE_X_TOOLKIT */
12813 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12814 #endif /* not USE_X_TOOLKIT */
12815 #if 0 /* This seems to bring back scroll bars in the wrong places
12816 if the window configuration has changed. They seem
12817 to come back ok without this. */
12818 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
12819 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12820 #endif
12821 }
12822
12823 XFlush (FRAME_X_DISPLAY (f));
12824
12825 /* Synchronize to ensure Emacs knows the frame is visible
12826 before we do anything else. We do this loop with input not blocked
12827 so that incoming events are handled. */
12828 {
12829 Lisp_Object frame;
12830 int count;
12831 /* This must be before UNBLOCK_INPUT
12832 since events that arrive in response to the actions above
12833 will set it when they are handled. */
12834 int previously_visible = f->output_data.x->has_been_visible;
12835
12836 original_left = f->output_data.x->left_pos;
12837 original_top = f->output_data.x->top_pos;
12838
12839 /* This must come after we set COUNT. */
12840 UNBLOCK_INPUT;
12841
12842 /* We unblock here so that arriving X events are processed. */
12843
12844 /* Now move the window back to where it was "supposed to be".
12845 But don't do it if the gravity is negative.
12846 When the gravity is negative, this uses a position
12847 that is 3 pixels too low. Perhaps that's really the border width.
12848
12849 Don't do this if the window has never been visible before,
12850 because the window manager may choose the position
12851 and we don't want to override it. */
12852
12853 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
12854 && f->output_data.x->win_gravity == NorthWestGravity
12855 && previously_visible)
12856 {
12857 Drawable rootw;
12858 int x, y;
12859 unsigned int width, height, border, depth;
12860
12861 BLOCK_INPUT;
12862
12863 /* On some window managers (such as FVWM) moving an existing
12864 window, even to the same place, causes the window manager
12865 to introduce an offset. This can cause the window to move
12866 to an unexpected location. Check the geometry (a little
12867 slow here) and then verify that the window is in the right
12868 place. If the window is not in the right place, move it
12869 there, and take the potential window manager hit. */
12870 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12871 &rootw, &x, &y, &width, &height, &border, &depth);
12872
12873 if (original_left != x || original_top != y)
12874 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12875 original_left, original_top);
12876
12877 UNBLOCK_INPUT;
12878 }
12879
12880 XSETFRAME (frame, f);
12881
12882 /* Wait until the frame is visible. Process X events until a
12883 MapNotify event has been seen, or until we think we won't get a
12884 MapNotify at all.. */
12885 for (count = input_signal_count + 10;
12886 input_signal_count < count && !FRAME_VISIBLE_P (f);)
12887 {
12888 /* Force processing of queued events. */
12889 x_sync (f);
12890
12891 /* Machines that do polling rather than SIGIO have been
12892 observed to go into a busy-wait here. So we'll fake an
12893 alarm signal to let the handler know that there's something
12894 to be read. We used to raise a real alarm, but it seems
12895 that the handler isn't always enabled here. This is
12896 probably a bug. */
12897 if (input_polling_used ())
12898 {
12899 /* It could be confusing if a real alarm arrives while
12900 processing the fake one. Turn it off and let the
12901 handler reset it. */
12902 extern void poll_for_input_1 P_ ((void));
12903 int old_poll_suppress_count = poll_suppress_count;
12904 poll_suppress_count = 1;
12905 poll_for_input_1 ();
12906 poll_suppress_count = old_poll_suppress_count;
12907 }
12908
12909 /* See if a MapNotify event has been processed. */
12910 FRAME_SAMPLE_VISIBILITY (f);
12911 }
12912
12913 /* 2000-09-28: In
12914
12915 (let ((f (selected-frame)))
12916 (iconify-frame f)
12917 (raise-frame f))
12918
12919 the frame is not raised with various window managers on
12920 FreeBSD, Linux and Solaris. It turns out that, for some
12921 unknown reason, the call to XtMapWidget is completely ignored.
12922 Mapping the widget a second time works. */
12923
12924 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
12925 goto retry;
12926 }
12927 }
12928
12929 /* Change from mapped state to withdrawn state. */
12930
12931 /* Make the frame visible (mapped and not iconified). */
12932
12933 void
12934 x_make_frame_invisible (f)
12935 struct frame *f;
12936 {
12937 Window window;
12938
12939 #ifdef USE_X_TOOLKIT
12940 /* Use the frame's outermost window, not the one we normally draw on. */
12941 window = XtWindow (f->output_data.x->widget);
12942 #else /* not USE_X_TOOLKIT */
12943 window = FRAME_X_WINDOW (f);
12944 #endif /* not USE_X_TOOLKIT */
12945
12946 /* Don't keep the highlight on an invisible frame. */
12947 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12948 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12949
12950 #if 0/* This might add unreliability; I don't trust it -- rms. */
12951 if (! f->async_visible && ! f->async_iconified)
12952 return;
12953 #endif
12954
12955 BLOCK_INPUT;
12956
12957 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
12958 that the current position of the window is user-specified, rather than
12959 program-specified, so that when the window is mapped again, it will be
12960 placed at the same location, without forcing the user to position it
12961 by hand again (they have already done that once for this window.) */
12962 x_wm_set_size_hint (f, (long) 0, 1);
12963
12964 #ifdef HAVE_X11R4
12965
12966 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
12967 DefaultScreen (FRAME_X_DISPLAY (f))))
12968 {
12969 UNBLOCK_INPUT_RESIGNAL;
12970 error ("Can't notify window manager of window withdrawal");
12971 }
12972 #else /* ! defined (HAVE_X11R4) */
12973
12974 /* Tell the window manager what we're going to do. */
12975 if (! EQ (Vx_no_window_manager, Qt))
12976 {
12977 XEvent unmap;
12978
12979 unmap.xunmap.type = UnmapNotify;
12980 unmap.xunmap.window = window;
12981 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
12982 unmap.xunmap.from_configure = False;
12983 if (! XSendEvent (FRAME_X_DISPLAY (f),
12984 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12985 False,
12986 SubstructureRedirectMaskSubstructureNotifyMask,
12987 &unmap))
12988 {
12989 UNBLOCK_INPUT_RESIGNAL;
12990 error ("Can't notify window manager of withdrawal");
12991 }
12992 }
12993
12994 /* Unmap the window ourselves. Cheeky! */
12995 XUnmapWindow (FRAME_X_DISPLAY (f), window);
12996 #endif /* ! defined (HAVE_X11R4) */
12997
12998 /* We can't distinguish this from iconification
12999 just by the event that we get from the server.
13000 So we can't win using the usual strategy of letting
13001 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
13002 and synchronize with the server to make sure we agree. */
13003 f->visible = 0;
13004 FRAME_ICONIFIED_P (f) = 0;
13005 f->async_visible = 0;
13006 f->async_iconified = 0;
13007
13008 x_sync (f);
13009
13010 UNBLOCK_INPUT;
13011 }
13012
13013 /* Change window state from mapped to iconified. */
13014
13015 void
13016 x_iconify_frame (f)
13017 struct frame *f;
13018 {
13019 int result;
13020 Lisp_Object type;
13021
13022 /* Don't keep the highlight on an invisible frame. */
13023 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13024 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
13025
13026 if (f->async_iconified)
13027 return;
13028
13029 BLOCK_INPUT;
13030
13031 FRAME_SAMPLE_VISIBILITY (f);
13032
13033 type = x_icon_type (f);
13034 if (!NILP (type))
13035 x_bitmap_icon (f, type);
13036
13037 #ifdef USE_X_TOOLKIT
13038
13039 if (! FRAME_VISIBLE_P (f))
13040 {
13041 if (! EQ (Vx_no_window_manager, Qt))
13042 x_wm_set_window_state (f, IconicState);
13043 /* This was XtPopup, but that did nothing for an iconified frame. */
13044 XtMapWidget (f->output_data.x->widget);
13045 /* The server won't give us any event to indicate
13046 that an invisible frame was changed to an icon,
13047 so we have to record it here. */
13048 f->iconified = 1;
13049 f->visible = 1;
13050 f->async_iconified = 1;
13051 f->async_visible = 0;
13052 UNBLOCK_INPUT;
13053 return;
13054 }
13055
13056 result = XIconifyWindow (FRAME_X_DISPLAY (f),
13057 XtWindow (f->output_data.x->widget),
13058 DefaultScreen (FRAME_X_DISPLAY (f)));
13059 UNBLOCK_INPUT;
13060
13061 if (!result)
13062 error ("Can't notify window manager of iconification");
13063
13064 f->async_iconified = 1;
13065 f->async_visible = 0;
13066
13067
13068 BLOCK_INPUT;
13069 XFlush (FRAME_X_DISPLAY (f));
13070 UNBLOCK_INPUT;
13071 #else /* not USE_X_TOOLKIT */
13072
13073 /* Make sure the X server knows where the window should be positioned,
13074 in case the user deiconifies with the window manager. */
13075 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
13076 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13077
13078 /* Since we don't know which revision of X we're running, we'll use both
13079 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
13080
13081 /* X11R4: send a ClientMessage to the window manager using the
13082 WM_CHANGE_STATE type. */
13083 {
13084 XEvent message;
13085
13086 message.xclient.window = FRAME_X_WINDOW (f);
13087 message.xclient.type = ClientMessage;
13088 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
13089 message.xclient.format = 32;
13090 message.xclient.data.l[0] = IconicState;
13091
13092 if (! XSendEvent (FRAME_X_DISPLAY (f),
13093 DefaultRootWindow (FRAME_X_DISPLAY (f)),
13094 False,
13095 SubstructureRedirectMask | SubstructureNotifyMask,
13096 &message))
13097 {
13098 UNBLOCK_INPUT_RESIGNAL;
13099 error ("Can't notify window manager of iconification");
13100 }
13101 }
13102
13103 /* X11R3: set the initial_state field of the window manager hints to
13104 IconicState. */
13105 x_wm_set_window_state (f, IconicState);
13106
13107 if (!FRAME_VISIBLE_P (f))
13108 {
13109 /* If the frame was withdrawn, before, we must map it. */
13110 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13111 }
13112
13113 f->async_iconified = 1;
13114 f->async_visible = 0;
13115
13116 XFlush (FRAME_X_DISPLAY (f));
13117 UNBLOCK_INPUT;
13118 #endif /* not USE_X_TOOLKIT */
13119 }
13120
13121 \f
13122 /* Free X resources of frame F. */
13123
13124 void
13125 x_free_frame_resources (f)
13126 struct frame *f;
13127 {
13128 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13129
13130 BLOCK_INPUT;
13131
13132 /* If a display connection is dead, don't try sending more
13133 commands to the X server. */
13134 if (dpyinfo->display)
13135 {
13136 if (f->output_data.x->icon_desc)
13137 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
13138
13139 #ifdef HAVE_X_I18N
13140 if (FRAME_XIC (f))
13141 free_frame_xic (f);
13142 #endif
13143
13144 if (FRAME_X_WINDOW (f))
13145 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13146
13147 #ifdef USE_X_TOOLKIT
13148 if (f->output_data.x->widget)
13149 {
13150 XtDestroyWidget (f->output_data.x->widget);
13151 f->output_data.x->widget = NULL;
13152 }
13153 free_frame_menubar (f);
13154 #endif /* USE_X_TOOLKIT */
13155
13156 unload_color (f, f->output_data.x->foreground_pixel);
13157 unload_color (f, f->output_data.x->background_pixel);
13158 unload_color (f, f->output_data.x->cursor_pixel);
13159 unload_color (f, f->output_data.x->cursor_foreground_pixel);
13160 unload_color (f, f->output_data.x->border_pixel);
13161 unload_color (f, f->output_data.x->mouse_pixel);
13162
13163 if (f->output_data.x->scroll_bar_background_pixel != -1)
13164 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
13165 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
13166 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
13167 #ifdef USE_TOOLKIT_SCROLL_BARS
13168 /* Scrollbar shadow colors. */
13169 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
13170 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
13171 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
13172 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
13173 #endif /* USE_TOOLKIT_SCROLL_BARS */
13174 if (f->output_data.x->white_relief.allocated_p)
13175 unload_color (f, f->output_data.x->white_relief.pixel);
13176 if (f->output_data.x->black_relief.allocated_p)
13177 unload_color (f, f->output_data.x->black_relief.pixel);
13178
13179 if (FRAME_FACE_CACHE (f))
13180 free_frame_faces (f);
13181
13182 x_free_gcs (f);
13183 XFlush (FRAME_X_DISPLAY (f));
13184 }
13185
13186 if (f->output_data.x->saved_menu_event)
13187 xfree (f->output_data.x->saved_menu_event);
13188
13189 xfree (f->output_data.x);
13190 f->output_data.x = NULL;
13191
13192 if (f == dpyinfo->x_focus_frame)
13193 dpyinfo->x_focus_frame = 0;
13194 if (f == dpyinfo->x_focus_event_frame)
13195 dpyinfo->x_focus_event_frame = 0;
13196 if (f == dpyinfo->x_highlight_frame)
13197 dpyinfo->x_highlight_frame = 0;
13198
13199 if (f == dpyinfo->mouse_face_mouse_frame)
13200 {
13201 dpyinfo->mouse_face_beg_row
13202 = dpyinfo->mouse_face_beg_col = -1;
13203 dpyinfo->mouse_face_end_row
13204 = dpyinfo->mouse_face_end_col = -1;
13205 dpyinfo->mouse_face_window = Qnil;
13206 dpyinfo->mouse_face_deferred_gc = 0;
13207 dpyinfo->mouse_face_mouse_frame = 0;
13208 }
13209
13210 UNBLOCK_INPUT;
13211 }
13212
13213
13214 /* Destroy the X window of frame F. */
13215
13216 void
13217 x_destroy_window (f)
13218 struct frame *f;
13219 {
13220 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13221
13222 /* If a display connection is dead, don't try sending more
13223 commands to the X server. */
13224 if (dpyinfo->display != 0)
13225 x_free_frame_resources (f);
13226
13227 dpyinfo->reference_count--;
13228 }
13229
13230 \f
13231 /* Setting window manager hints. */
13232
13233 /* Set the normal size hints for the window manager, for frame F.
13234 FLAGS is the flags word to use--or 0 meaning preserve the flags
13235 that the window now has.
13236 If USER_POSITION is nonzero, we set the USPosition
13237 flag (this is useful when FLAGS is 0). */
13238
13239 void
13240 x_wm_set_size_hint (f, flags, user_position)
13241 struct frame *f;
13242 long flags;
13243 int user_position;
13244 {
13245 XSizeHints size_hints;
13246
13247 #ifdef USE_X_TOOLKIT
13248 Arg al[2];
13249 int ac = 0;
13250 Dimension widget_width, widget_height;
13251 Window window = XtWindow (f->output_data.x->widget);
13252 #else /* not USE_X_TOOLKIT */
13253 Window window = FRAME_X_WINDOW (f);
13254 #endif /* not USE_X_TOOLKIT */
13255
13256 /* Setting PMaxSize caused various problems. */
13257 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
13258
13259 size_hints.x = f->output_data.x->left_pos;
13260 size_hints.y = f->output_data.x->top_pos;
13261
13262 #ifdef USE_X_TOOLKIT
13263 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
13264 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
13265 XtGetValues (f->output_data.x->widget, al, ac);
13266 size_hints.height = widget_height;
13267 size_hints.width = widget_width;
13268 #else /* not USE_X_TOOLKIT */
13269 size_hints.height = PIXEL_HEIGHT (f);
13270 size_hints.width = PIXEL_WIDTH (f);
13271 #endif /* not USE_X_TOOLKIT */
13272
13273 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
13274 size_hints.height_inc = f->output_data.x->line_height;
13275 size_hints.max_width
13276 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
13277 size_hints.max_height
13278 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
13279
13280 /* Calculate the base and minimum sizes.
13281
13282 (When we use the X toolkit, we don't do it here.
13283 Instead we copy the values that the widgets are using, below.) */
13284 #ifndef USE_X_TOOLKIT
13285 {
13286 int base_width, base_height;
13287 int min_rows = 0, min_cols = 0;
13288
13289 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
13290 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
13291
13292 check_frame_size (f, &min_rows, &min_cols);
13293
13294 /* The window manager uses the base width hints to calculate the
13295 current number of rows and columns in the frame while
13296 resizing; min_width and min_height aren't useful for this
13297 purpose, since they might not give the dimensions for a
13298 zero-row, zero-column frame.
13299
13300 We use the base_width and base_height members if we have
13301 them; otherwise, we set the min_width and min_height members
13302 to the size for a zero x zero frame. */
13303
13304 #ifdef HAVE_X11R4
13305 size_hints.flags |= PBaseSize;
13306 size_hints.base_width = base_width;
13307 size_hints.base_height = base_height;
13308 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
13309 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
13310 #else
13311 size_hints.min_width = base_width;
13312 size_hints.min_height = base_height;
13313 #endif
13314 }
13315
13316 /* If we don't need the old flags, we don't need the old hint at all. */
13317 if (flags)
13318 {
13319 size_hints.flags |= flags;
13320 goto no_read;
13321 }
13322 #endif /* not USE_X_TOOLKIT */
13323
13324 {
13325 XSizeHints hints; /* Sometimes I hate X Windows... */
13326 long supplied_return;
13327 int value;
13328
13329 #ifdef HAVE_X11R4
13330 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
13331 &supplied_return);
13332 #else
13333 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
13334 #endif
13335
13336 #ifdef USE_X_TOOLKIT
13337 size_hints.base_height = hints.base_height;
13338 size_hints.base_width = hints.base_width;
13339 size_hints.min_height = hints.min_height;
13340 size_hints.min_width = hints.min_width;
13341 #endif
13342
13343 if (flags)
13344 size_hints.flags |= flags;
13345 else
13346 {
13347 if (value == 0)
13348 hints.flags = 0;
13349 if (hints.flags & PSize)
13350 size_hints.flags |= PSize;
13351 if (hints.flags & PPosition)
13352 size_hints.flags |= PPosition;
13353 if (hints.flags & USPosition)
13354 size_hints.flags |= USPosition;
13355 if (hints.flags & USSize)
13356 size_hints.flags |= USSize;
13357 }
13358 }
13359
13360 #ifndef USE_X_TOOLKIT
13361 no_read:
13362 #endif
13363
13364 #ifdef PWinGravity
13365 size_hints.win_gravity = f->output_data.x->win_gravity;
13366 size_hints.flags |= PWinGravity;
13367
13368 if (user_position)
13369 {
13370 size_hints.flags &= ~ PPosition;
13371 size_hints.flags |= USPosition;
13372 }
13373 #endif /* PWinGravity */
13374
13375 #ifdef HAVE_X11R4
13376 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13377 #else
13378 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13379 #endif
13380 }
13381
13382 /* Used for IconicState or NormalState */
13383
13384 void
13385 x_wm_set_window_state (f, state)
13386 struct frame *f;
13387 int state;
13388 {
13389 #ifdef USE_X_TOOLKIT
13390 Arg al[1];
13391
13392 XtSetArg (al[0], XtNinitialState, state);
13393 XtSetValues (f->output_data.x->widget, al, 1);
13394 #else /* not USE_X_TOOLKIT */
13395 Window window = FRAME_X_WINDOW (f);
13396
13397 f->output_data.x->wm_hints.flags |= StateHint;
13398 f->output_data.x->wm_hints.initial_state = state;
13399
13400 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13401 #endif /* not USE_X_TOOLKIT */
13402 }
13403
13404 void
13405 x_wm_set_icon_pixmap (f, pixmap_id)
13406 struct frame *f;
13407 int pixmap_id;
13408 {
13409 Pixmap icon_pixmap;
13410
13411 #ifndef USE_X_TOOLKIT
13412 Window window = FRAME_X_WINDOW (f);
13413 #endif
13414
13415 if (pixmap_id > 0)
13416 {
13417 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
13418 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
13419 }
13420 else
13421 {
13422 /* It seems there is no way to turn off use of an icon pixmap.
13423 The following line does it, only if no icon has yet been created,
13424 for some window managers. But with mwm it crashes.
13425 Some people say it should clear the IconPixmapHint bit in this case,
13426 but that doesn't work, and the X consortium said it isn't the
13427 right thing at all. Since there is no way to win,
13428 best to explicitly give up. */
13429 #if 0
13430 f->output_data.x->wm_hints.icon_pixmap = None;
13431 #else
13432 return;
13433 #endif
13434 }
13435
13436 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
13437
13438 {
13439 Arg al[1];
13440 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
13441 XtSetValues (f->output_data.x->widget, al, 1);
13442 }
13443
13444 #else /* not USE_X_TOOLKIT */
13445
13446 f->output_data.x->wm_hints.flags |= IconPixmapHint;
13447 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13448
13449 #endif /* not USE_X_TOOLKIT */
13450 }
13451
13452 void
13453 x_wm_set_icon_position (f, icon_x, icon_y)
13454 struct frame *f;
13455 int icon_x, icon_y;
13456 {
13457 #ifdef USE_X_TOOLKIT
13458 Window window = XtWindow (f->output_data.x->widget);
13459 #else
13460 Window window = FRAME_X_WINDOW (f);
13461 #endif
13462
13463 f->output_data.x->wm_hints.flags |= IconPositionHint;
13464 f->output_data.x->wm_hints.icon_x = icon_x;
13465 f->output_data.x->wm_hints.icon_y = icon_y;
13466
13467 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13468 }
13469
13470 \f
13471 /***********************************************************************
13472 Fonts
13473 ***********************************************************************/
13474
13475 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
13476
13477 struct font_info *
13478 x_get_font_info (f, font_idx)
13479 FRAME_PTR f;
13480 int font_idx;
13481 {
13482 return (FRAME_X_FONT_TABLE (f) + font_idx);
13483 }
13484
13485
13486 /* Return a list of names of available fonts matching PATTERN on frame F.
13487
13488 If SIZE is > 0, it is the size (maximum bounds width) of fonts
13489 to be listed.
13490
13491 SIZE < 0 means include scalable fonts.
13492
13493 Frame F null means we have not yet created any frame on X, and
13494 consult the first display in x_display_list. MAXNAMES sets a limit
13495 on how many fonts to match. */
13496
13497 Lisp_Object
13498 x_list_fonts (f, pattern, size, maxnames)
13499 struct frame *f;
13500 Lisp_Object pattern;
13501 int size;
13502 int maxnames;
13503 {
13504 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
13505 Lisp_Object tem, second_best;
13506 struct x_display_info *dpyinfo
13507 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
13508 Display *dpy = dpyinfo->display;
13509 int try_XLoadQueryFont = 0;
13510 int count;
13511 int allow_scalable_fonts_p = 0;
13512
13513 if (size < 0)
13514 {
13515 allow_scalable_fonts_p = 1;
13516 size = 0;
13517 }
13518
13519 patterns = Fassoc (pattern, Valternate_fontname_alist);
13520 if (NILP (patterns))
13521 patterns = Fcons (pattern, Qnil);
13522
13523 if (maxnames == 1 && !size)
13524 /* We can return any single font matching PATTERN. */
13525 try_XLoadQueryFont = 1;
13526
13527 for (; CONSP (patterns); patterns = XCDR (patterns))
13528 {
13529 int num_fonts;
13530 char **names = NULL;
13531
13532 pattern = XCAR (patterns);
13533 /* See if we cached the result for this particular query.
13534 The cache is an alist of the form:
13535 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
13536 tem = XCDR (dpyinfo->name_list_element);
13537 key = Fcons (Fcons (pattern, make_number (maxnames)),
13538 allow_scalable_fonts_p ? Qt : Qnil);
13539 list = Fassoc (key, tem);
13540 if (!NILP (list))
13541 {
13542 list = Fcdr_safe (list);
13543 /* We have a cashed list. Don't have to get the list again. */
13544 goto label_cached;
13545 }
13546
13547 /* At first, put PATTERN in the cache. */
13548
13549 BLOCK_INPUT;
13550 count = x_catch_errors (dpy);
13551
13552 if (try_XLoadQueryFont)
13553 {
13554 XFontStruct *font;
13555 unsigned long value;
13556
13557 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
13558 if (x_had_errors_p (dpy))
13559 {
13560 /* This error is perhaps due to insufficient memory on X
13561 server. Let's just ignore it. */
13562 font = NULL;
13563 x_clear_errors (dpy);
13564 }
13565
13566 if (font
13567 && XGetFontProperty (font, XA_FONT, &value))
13568 {
13569 char *name = (char *) XGetAtomName (dpy, (Atom) value);
13570 int len = strlen (name);
13571 char *tmp;
13572
13573 /* If DXPC (a Differential X Protocol Compressor)
13574 Ver.3.7 is running, XGetAtomName will return null
13575 string. We must avoid such a name. */
13576 if (len == 0)
13577 try_XLoadQueryFont = 0;
13578 else
13579 {
13580 num_fonts = 1;
13581 names = (char **) alloca (sizeof (char *));
13582 /* Some systems only allow alloca assigned to a
13583 simple var. */
13584 tmp = (char *) alloca (len + 1); names[0] = tmp;
13585 bcopy (name, names[0], len + 1);
13586 XFree (name);
13587 }
13588 }
13589 else
13590 try_XLoadQueryFont = 0;
13591
13592 if (font)
13593 XFreeFont (dpy, font);
13594 }
13595
13596 if (!try_XLoadQueryFont)
13597 {
13598 /* We try at least 10 fonts because XListFonts will return
13599 auto-scaled fonts at the head. */
13600 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
13601 &num_fonts);
13602 if (x_had_errors_p (dpy))
13603 {
13604 /* This error is perhaps due to insufficient memory on X
13605 server. Let's just ignore it. */
13606 names = NULL;
13607 x_clear_errors (dpy);
13608 }
13609 }
13610
13611 x_uncatch_errors (dpy, count);
13612 UNBLOCK_INPUT;
13613
13614 if (names)
13615 {
13616 int i;
13617
13618 /* Make a list of all the fonts we got back.
13619 Store that in the font cache for the display. */
13620 for (i = 0; i < num_fonts; i++)
13621 {
13622 int width = 0;
13623 char *p = names[i];
13624 int average_width = -1, dashes = 0;
13625
13626 /* Count the number of dashes in NAMES[I]. If there are
13627 14 dashes, and the field value following 12th dash
13628 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
13629 is usually too ugly to be used for editing. Let's
13630 ignore it. */
13631 while (*p)
13632 if (*p++ == '-')
13633 {
13634 dashes++;
13635 if (dashes == 7) /* PIXEL_SIZE field */
13636 width = atoi (p);
13637 else if (dashes == 12) /* AVERAGE_WIDTH field */
13638 average_width = atoi (p);
13639 }
13640
13641 if (allow_scalable_fonts_p
13642 || dashes < 14 || average_width != 0)
13643 {
13644 tem = build_string (names[i]);
13645 if (NILP (Fassoc (tem, list)))
13646 {
13647 if (STRINGP (Vx_pixel_size_width_font_regexp)
13648 && ((fast_c_string_match_ignore_case
13649 (Vx_pixel_size_width_font_regexp, names[i]))
13650 >= 0))
13651 /* We can set the value of PIXEL_SIZE to the
13652 width of this font. */
13653 list = Fcons (Fcons (tem, make_number (width)), list);
13654 else
13655 /* For the moment, width is not known. */
13656 list = Fcons (Fcons (tem, Qnil), list);
13657 }
13658 }
13659 }
13660
13661 if (!try_XLoadQueryFont)
13662 {
13663 BLOCK_INPUT;
13664 XFreeFontNames (names);
13665 UNBLOCK_INPUT;
13666 }
13667 }
13668
13669 /* Now store the result in the cache. */
13670 XSETCDR (dpyinfo->name_list_element,
13671 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
13672
13673 label_cached:
13674 if (NILP (list)) continue; /* Try the remaining alternatives. */
13675
13676 newlist = second_best = Qnil;
13677 /* Make a list of the fonts that have the right width. */
13678 for (; CONSP (list); list = XCDR (list))
13679 {
13680 int found_size;
13681
13682 tem = XCAR (list);
13683
13684 if (!CONSP (tem) || NILP (XCAR (tem)))
13685 continue;
13686 if (!size)
13687 {
13688 newlist = Fcons (XCAR (tem), newlist);
13689 continue;
13690 }
13691
13692 if (!INTEGERP (XCDR (tem)))
13693 {
13694 /* Since we have not yet known the size of this font, we
13695 must try slow function call XLoadQueryFont. */
13696 XFontStruct *thisinfo;
13697
13698 BLOCK_INPUT;
13699 count = x_catch_errors (dpy);
13700 thisinfo = XLoadQueryFont (dpy,
13701 XSTRING (XCAR (tem))->data);
13702 if (x_had_errors_p (dpy))
13703 {
13704 /* This error is perhaps due to insufficient memory on X
13705 server. Let's just ignore it. */
13706 thisinfo = NULL;
13707 x_clear_errors (dpy);
13708 }
13709 x_uncatch_errors (dpy, count);
13710 UNBLOCK_INPUT;
13711
13712 if (thisinfo)
13713 {
13714 XSETCDR (tem,
13715 (thisinfo->min_bounds.width == 0
13716 ? make_number (0)
13717 : make_number (thisinfo->max_bounds.width)));
13718 BLOCK_INPUT;
13719 XFreeFont (dpy, thisinfo);
13720 UNBLOCK_INPUT;
13721 }
13722 else
13723 /* For unknown reason, the previous call of XListFont had
13724 returned a font which can't be opened. Record the size
13725 as 0 not to try to open it again. */
13726 XSETCDR (tem, make_number (0));
13727 }
13728
13729 found_size = XINT (XCDR (tem));
13730 if (found_size == size)
13731 newlist = Fcons (XCAR (tem), newlist);
13732 else if (found_size > 0)
13733 {
13734 if (NILP (second_best))
13735 second_best = tem;
13736 else if (found_size < size)
13737 {
13738 if (XINT (XCDR (second_best)) > size
13739 || XINT (XCDR (second_best)) < found_size)
13740 second_best = tem;
13741 }
13742 else
13743 {
13744 if (XINT (XCDR (second_best)) > size
13745 && XINT (XCDR (second_best)) > found_size)
13746 second_best = tem;
13747 }
13748 }
13749 }
13750 if (!NILP (newlist))
13751 break;
13752 else if (!NILP (second_best))
13753 {
13754 newlist = Fcons (XCAR (second_best), Qnil);
13755 break;
13756 }
13757 }
13758
13759 return newlist;
13760 }
13761
13762
13763 #if GLYPH_DEBUG
13764
13765 /* Check that FONT is valid on frame F. It is if it can be found in F's
13766 font table. */
13767
13768 static void
13769 x_check_font (f, font)
13770 struct frame *f;
13771 XFontStruct *font;
13772 {
13773 int i;
13774 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13775
13776 xassert (font != NULL);
13777
13778 for (i = 0; i < dpyinfo->n_fonts; i++)
13779 if (dpyinfo->font_table[i].name
13780 && font == dpyinfo->font_table[i].font)
13781 break;
13782
13783 xassert (i < dpyinfo->n_fonts);
13784 }
13785
13786 #endif /* GLYPH_DEBUG != 0 */
13787
13788 /* Set *W to the minimum width, *H to the minimum font height of FONT.
13789 Note: There are (broken) X fonts out there with invalid XFontStruct
13790 min_bounds contents. For example, handa@etl.go.jp reports that
13791 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
13792 have font->min_bounds.width == 0. */
13793
13794 static INLINE void
13795 x_font_min_bounds (font, w, h)
13796 XFontStruct *font;
13797 int *w, *h;
13798 {
13799 *h = FONT_HEIGHT (font);
13800 *w = font->min_bounds.width;
13801
13802 /* Try to handle the case where FONT->min_bounds has invalid
13803 contents. Since the only font known to have invalid min_bounds
13804 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
13805 if (*w <= 0)
13806 *w = font->max_bounds.width;
13807 }
13808
13809
13810 /* Compute the smallest character width and smallest font height over
13811 all fonts available on frame F. Set the members smallest_char_width
13812 and smallest_font_height in F's x_display_info structure to
13813 the values computed. Value is non-zero if smallest_font_height or
13814 smallest_char_width become smaller than they were before. */
13815
13816 static int
13817 x_compute_min_glyph_bounds (f)
13818 struct frame *f;
13819 {
13820 int i;
13821 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13822 XFontStruct *font;
13823 int old_width = dpyinfo->smallest_char_width;
13824 int old_height = dpyinfo->smallest_font_height;
13825
13826 dpyinfo->smallest_font_height = 100000;
13827 dpyinfo->smallest_char_width = 100000;
13828
13829 for (i = 0; i < dpyinfo->n_fonts; ++i)
13830 if (dpyinfo->font_table[i].name)
13831 {
13832 struct font_info *fontp = dpyinfo->font_table + i;
13833 int w, h;
13834
13835 font = (XFontStruct *) fontp->font;
13836 xassert (font != (XFontStruct *) ~0);
13837 x_font_min_bounds (font, &w, &h);
13838
13839 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
13840 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
13841 }
13842
13843 xassert (dpyinfo->smallest_char_width > 0
13844 && dpyinfo->smallest_font_height > 0);
13845
13846 return (dpyinfo->n_fonts == 1
13847 || dpyinfo->smallest_char_width < old_width
13848 || dpyinfo->smallest_font_height < old_height);
13849 }
13850
13851
13852 /* Load font named FONTNAME of the size SIZE for frame F, and return a
13853 pointer to the structure font_info while allocating it dynamically.
13854 If SIZE is 0, load any size of font.
13855 If loading is failed, return NULL. */
13856
13857 struct font_info *
13858 x_load_font (f, fontname, size)
13859 struct frame *f;
13860 register char *fontname;
13861 int size;
13862 {
13863 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13864 Lisp_Object font_names;
13865 int count;
13866
13867 /* Get a list of all the fonts that match this name. Once we
13868 have a list of matching fonts, we compare them against the fonts
13869 we already have by comparing names. */
13870 font_names = x_list_fonts (f, build_string (fontname), size, 1);
13871
13872 if (!NILP (font_names))
13873 {
13874 Lisp_Object tail;
13875 int i;
13876
13877 for (i = 0; i < dpyinfo->n_fonts; i++)
13878 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
13879 if (dpyinfo->font_table[i].name
13880 && (!strcmp (dpyinfo->font_table[i].name,
13881 XSTRING (XCAR (tail))->data)
13882 || !strcmp (dpyinfo->font_table[i].full_name,
13883 XSTRING (XCAR (tail))->data)))
13884 return (dpyinfo->font_table + i);
13885 }
13886
13887 /* Load the font and add it to the table. */
13888 {
13889 char *full_name;
13890 XFontStruct *font;
13891 struct font_info *fontp;
13892 unsigned long value;
13893 int i;
13894
13895 /* If we have found fonts by x_list_font, load one of them. If
13896 not, we still try to load a font by the name given as FONTNAME
13897 because XListFonts (called in x_list_font) of some X server has
13898 a bug of not finding a font even if the font surely exists and
13899 is loadable by XLoadQueryFont. */
13900 if (size > 0 && !NILP (font_names))
13901 fontname = (char *) XSTRING (XCAR (font_names))->data;
13902
13903 BLOCK_INPUT;
13904 count = x_catch_errors (FRAME_X_DISPLAY (f));
13905 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
13906 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
13907 {
13908 /* This error is perhaps due to insufficient memory on X
13909 server. Let's just ignore it. */
13910 font = NULL;
13911 x_clear_errors (FRAME_X_DISPLAY (f));
13912 }
13913 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
13914 UNBLOCK_INPUT;
13915 if (!font)
13916 return NULL;
13917
13918 /* Find a free slot in the font table. */
13919 for (i = 0; i < dpyinfo->n_fonts; ++i)
13920 if (dpyinfo->font_table[i].name == NULL)
13921 break;
13922
13923 /* If no free slot found, maybe enlarge the font table. */
13924 if (i == dpyinfo->n_fonts
13925 && dpyinfo->n_fonts == dpyinfo->font_table_size)
13926 {
13927 int sz;
13928 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
13929 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
13930 dpyinfo->font_table
13931 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
13932 }
13933
13934 fontp = dpyinfo->font_table + i;
13935 if (i == dpyinfo->n_fonts)
13936 ++dpyinfo->n_fonts;
13937
13938 /* Now fill in the slots of *FONTP. */
13939 BLOCK_INPUT;
13940 fontp->font = font;
13941 fontp->font_idx = i;
13942 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
13943 bcopy (fontname, fontp->name, strlen (fontname) + 1);
13944
13945 /* Try to get the full name of FONT. Put it in FULL_NAME. */
13946 full_name = 0;
13947 if (XGetFontProperty (font, XA_FONT, &value))
13948 {
13949 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
13950 char *p = name;
13951 int dashes = 0;
13952
13953 /* Count the number of dashes in the "full name".
13954 If it is too few, this isn't really the font's full name,
13955 so don't use it.
13956 In X11R4, the fonts did not come with their canonical names
13957 stored in them. */
13958 while (*p)
13959 {
13960 if (*p == '-')
13961 dashes++;
13962 p++;
13963 }
13964
13965 if (dashes >= 13)
13966 {
13967 full_name = (char *) xmalloc (p - name + 1);
13968 bcopy (name, full_name, p - name + 1);
13969 }
13970
13971 XFree (name);
13972 }
13973
13974 if (full_name != 0)
13975 fontp->full_name = full_name;
13976 else
13977 fontp->full_name = fontp->name;
13978
13979 fontp->size = font->max_bounds.width;
13980 fontp->height = FONT_HEIGHT (font);
13981
13982 if (NILP (font_names))
13983 {
13984 /* We come here because of a bug of XListFonts mentioned at
13985 the head of this block. Let's store this information in
13986 the cache for x_list_fonts. */
13987 Lisp_Object lispy_name = build_string (fontname);
13988 Lisp_Object lispy_full_name = build_string (fontp->full_name);
13989 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
13990 Qnil);
13991
13992 XSETCDR (dpyinfo->name_list_element,
13993 Fcons (Fcons (key,
13994 Fcons (Fcons (lispy_full_name,
13995 make_number (fontp->size)),
13996 Qnil)),
13997 XCDR (dpyinfo->name_list_element)));
13998 if (full_name)
13999 {
14000 key = Fcons (Fcons (lispy_full_name, make_number (256)),
14001 Qnil);
14002 XSETCDR (dpyinfo->name_list_element,
14003 Fcons (Fcons (key,
14004 Fcons (Fcons (lispy_full_name,
14005 make_number (fontp->size)),
14006 Qnil)),
14007 XCDR (dpyinfo->name_list_element)));
14008 }
14009 }
14010
14011 /* The slot `encoding' specifies how to map a character
14012 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
14013 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
14014 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
14015 2:0xA020..0xFF7F). For the moment, we don't know which charset
14016 uses this font. So, we set information in fontp->encoding[1]
14017 which is never used by any charset. If mapping can't be
14018 decided, set FONT_ENCODING_NOT_DECIDED. */
14019 fontp->encoding[1]
14020 = (font->max_byte1 == 0
14021 /* 1-byte font */
14022 ? (font->min_char_or_byte2 < 0x80
14023 ? (font->max_char_or_byte2 < 0x80
14024 ? 0 /* 0x20..0x7F */
14025 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
14026 : 1) /* 0xA0..0xFF */
14027 /* 2-byte font */
14028 : (font->min_byte1 < 0x80
14029 ? (font->max_byte1 < 0x80
14030 ? (font->min_char_or_byte2 < 0x80
14031 ? (font->max_char_or_byte2 < 0x80
14032 ? 0 /* 0x2020..0x7F7F */
14033 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
14034 : 3) /* 0x20A0..0x7FFF */
14035 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
14036 : (font->min_char_or_byte2 < 0x80
14037 ? (font->max_char_or_byte2 < 0x80
14038 ? 2 /* 0xA020..0xFF7F */
14039 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
14040 : 1))); /* 0xA0A0..0xFFFF */
14041
14042 fontp->baseline_offset
14043 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
14044 ? (long) value : 0);
14045 fontp->relative_compose
14046 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
14047 ? (long) value : 0);
14048 fontp->default_ascent
14049 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
14050 ? (long) value : 0);
14051
14052 /* Set global flag fonts_changed_p to non-zero if the font loaded
14053 has a character with a smaller width than any other character
14054 before, or if the font loaded has a smalle>r height than any
14055 other font loaded before. If this happens, it will make a
14056 glyph matrix reallocation necessary. */
14057 fonts_changed_p = x_compute_min_glyph_bounds (f);
14058 UNBLOCK_INPUT;
14059 return fontp;
14060 }
14061 }
14062
14063
14064 /* Return a pointer to struct font_info of a font named FONTNAME for
14065 frame F. If no such font is loaded, return NULL. */
14066
14067 struct font_info *
14068 x_query_font (f, fontname)
14069 struct frame *f;
14070 register char *fontname;
14071 {
14072 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14073 int i;
14074
14075 for (i = 0; i < dpyinfo->n_fonts; i++)
14076 if (dpyinfo->font_table[i].name
14077 && (!strcmp (dpyinfo->font_table[i].name, fontname)
14078 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
14079 return (dpyinfo->font_table + i);
14080 return NULL;
14081 }
14082
14083
14084 /* Find a CCL program for a font specified by FONTP, and set the member
14085 `encoder' of the structure. */
14086
14087 void
14088 x_find_ccl_program (fontp)
14089 struct font_info *fontp;
14090 {
14091 Lisp_Object list, elt;
14092
14093 elt = Qnil;
14094 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
14095 {
14096 elt = XCAR (list);
14097 if (CONSP (elt)
14098 && STRINGP (XCAR (elt))
14099 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
14100 >= 0)
14101 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
14102 >= 0)))
14103 break;
14104 }
14105
14106 if (! NILP (list))
14107 {
14108 struct ccl_program *ccl
14109 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
14110
14111 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
14112 xfree (ccl);
14113 else
14114 fontp->font_encoder = ccl;
14115 }
14116 }
14117
14118
14119 \f
14120 /***********************************************************************
14121 Initialization
14122 ***********************************************************************/
14123
14124 #ifdef USE_X_TOOLKIT
14125 static XrmOptionDescRec emacs_options[] = {
14126 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
14127 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
14128
14129 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
14130 XrmoptionSepArg, NULL},
14131 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
14132
14133 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14134 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14135 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14136 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14137 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14138 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
14139 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
14140 };
14141 #endif /* USE_X_TOOLKIT */
14142
14143 static int x_initialized;
14144
14145 #ifdef MULTI_KBOARD
14146 /* Test whether two display-name strings agree up to the dot that separates
14147 the screen number from the server number. */
14148 static int
14149 same_x_server (name1, name2)
14150 char *name1, *name2;
14151 {
14152 int seen_colon = 0;
14153 unsigned char *system_name = XSTRING (Vsystem_name)->data;
14154 int system_name_length = strlen (system_name);
14155 int length_until_period = 0;
14156
14157 while (system_name[length_until_period] != 0
14158 && system_name[length_until_period] != '.')
14159 length_until_period++;
14160
14161 /* Treat `unix' like an empty host name. */
14162 if (! strncmp (name1, "unix:", 5))
14163 name1 += 4;
14164 if (! strncmp (name2, "unix:", 5))
14165 name2 += 4;
14166 /* Treat this host's name like an empty host name. */
14167 if (! strncmp (name1, system_name, system_name_length)
14168 && name1[system_name_length] == ':')
14169 name1 += system_name_length;
14170 if (! strncmp (name2, system_name, system_name_length)
14171 && name2[system_name_length] == ':')
14172 name2 += system_name_length;
14173 /* Treat this host's domainless name like an empty host name. */
14174 if (! strncmp (name1, system_name, length_until_period)
14175 && name1[length_until_period] == ':')
14176 name1 += length_until_period;
14177 if (! strncmp (name2, system_name, length_until_period)
14178 && name2[length_until_period] == ':')
14179 name2 += length_until_period;
14180
14181 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
14182 {
14183 if (*name1 == ':')
14184 seen_colon++;
14185 if (seen_colon && *name1 == '.')
14186 return 1;
14187 }
14188 return (seen_colon
14189 && (*name1 == '.' || *name1 == '\0')
14190 && (*name2 == '.' || *name2 == '\0'));
14191 }
14192 #endif
14193
14194 struct x_display_info *
14195 x_term_init (display_name, xrm_option, resource_name)
14196 Lisp_Object display_name;
14197 char *xrm_option;
14198 char *resource_name;
14199 {
14200 int connection;
14201 Display *dpy;
14202 struct x_display_info *dpyinfo;
14203 XrmDatabase xrdb;
14204
14205 BLOCK_INPUT;
14206
14207 if (!x_initialized)
14208 {
14209 x_initialize ();
14210 x_initialized = 1;
14211 }
14212
14213 #ifdef USE_X_TOOLKIT
14214 /* weiner@footloose.sps.mot.com reports that this causes
14215 errors with X11R5:
14216 X protocol error: BadAtom (invalid Atom parameter)
14217 on protocol request 18skiloaf.
14218 So let's not use it until R6. */
14219 #ifdef HAVE_X11XTR6
14220 XtSetLanguageProc (NULL, NULL, NULL);
14221 #endif
14222
14223 {
14224 int argc = 0;
14225 char *argv[3];
14226
14227 argv[0] = "";
14228 argc = 1;
14229 if (xrm_option)
14230 {
14231 argv[argc++] = "-xrm";
14232 argv[argc++] = xrm_option;
14233 }
14234 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
14235 resource_name, EMACS_CLASS,
14236 emacs_options, XtNumber (emacs_options),
14237 &argc, argv);
14238
14239 #ifdef HAVE_X11XTR6
14240 /* I think this is to compensate for XtSetLanguageProc. */
14241 fixup_locale ();
14242 #endif
14243 }
14244
14245 #else /* not USE_X_TOOLKIT */
14246 #ifdef HAVE_X11R5
14247 XSetLocaleModifiers ("");
14248 #endif
14249 dpy = XOpenDisplay (XSTRING (display_name)->data);
14250 #endif /* not USE_X_TOOLKIT */
14251
14252 /* Detect failure. */
14253 if (dpy == 0)
14254 {
14255 UNBLOCK_INPUT;
14256 return 0;
14257 }
14258
14259 /* We have definitely succeeded. Record the new connection. */
14260
14261 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
14262 bzero (dpyinfo, sizeof *dpyinfo);
14263
14264 #ifdef MULTI_KBOARD
14265 {
14266 struct x_display_info *share;
14267 Lisp_Object tail;
14268
14269 for (share = x_display_list, tail = x_display_name_list; share;
14270 share = share->next, tail = XCDR (tail))
14271 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
14272 XSTRING (display_name)->data))
14273 break;
14274 if (share)
14275 dpyinfo->kboard = share->kboard;
14276 else
14277 {
14278 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
14279 init_kboard (dpyinfo->kboard);
14280 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
14281 {
14282 char *vendor = ServerVendor (dpy);
14283 UNBLOCK_INPUT;
14284 dpyinfo->kboard->Vsystem_key_alist
14285 = call1 (Qvendor_specific_keysyms,
14286 build_string (vendor ? vendor : ""));
14287 BLOCK_INPUT;
14288 }
14289
14290 dpyinfo->kboard->next_kboard = all_kboards;
14291 all_kboards = dpyinfo->kboard;
14292 /* Don't let the initial kboard remain current longer than necessary.
14293 That would cause problems if a file loaded on startup tries to
14294 prompt in the mini-buffer. */
14295 if (current_kboard == initial_kboard)
14296 current_kboard = dpyinfo->kboard;
14297 }
14298 dpyinfo->kboard->reference_count++;
14299 }
14300 #endif
14301
14302 /* Put this display on the chain. */
14303 dpyinfo->next = x_display_list;
14304 x_display_list = dpyinfo;
14305
14306 /* Put it on x_display_name_list as well, to keep them parallel. */
14307 x_display_name_list = Fcons (Fcons (display_name, Qnil),
14308 x_display_name_list);
14309 dpyinfo->name_list_element = XCAR (x_display_name_list);
14310
14311 dpyinfo->display = dpy;
14312
14313 #if 0
14314 XSetAfterFunction (x_current_display, x_trace_wire);
14315 #endif /* ! 0 */
14316
14317 dpyinfo->x_id_name
14318 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
14319 + STRING_BYTES (XSTRING (Vsystem_name))
14320 + 2);
14321 sprintf (dpyinfo->x_id_name, "%s@%s",
14322 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
14323
14324 /* Figure out which modifier bits mean what. */
14325 x_find_modifier_meanings (dpyinfo);
14326
14327 /* Get the scroll bar cursor. */
14328 dpyinfo->vertical_scroll_bar_cursor
14329 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
14330
14331 xrdb = x_load_resources (dpyinfo->display, xrm_option,
14332 resource_name, EMACS_CLASS);
14333 #ifdef HAVE_XRMSETDATABASE
14334 XrmSetDatabase (dpyinfo->display, xrdb);
14335 #else
14336 dpyinfo->display->db = xrdb;
14337 #endif
14338 /* Put the rdb where we can find it in a way that works on
14339 all versions. */
14340 dpyinfo->xrdb = xrdb;
14341
14342 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
14343 DefaultScreen (dpyinfo->display));
14344 select_visual (dpyinfo);
14345 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
14346 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
14347 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
14348 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
14349 dpyinfo->grabbed = 0;
14350 dpyinfo->reference_count = 0;
14351 dpyinfo->icon_bitmap_id = -1;
14352 dpyinfo->font_table = NULL;
14353 dpyinfo->n_fonts = 0;
14354 dpyinfo->font_table_size = 0;
14355 dpyinfo->bitmaps = 0;
14356 dpyinfo->bitmaps_size = 0;
14357 dpyinfo->bitmaps_last = 0;
14358 dpyinfo->scratch_cursor_gc = 0;
14359 dpyinfo->mouse_face_mouse_frame = 0;
14360 dpyinfo->mouse_face_deferred_gc = 0;
14361 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
14362 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
14363 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
14364 dpyinfo->mouse_face_window = Qnil;
14365 dpyinfo->mouse_face_overlay = Qnil;
14366 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
14367 dpyinfo->mouse_face_defer = 0;
14368 dpyinfo->x_focus_frame = 0;
14369 dpyinfo->x_focus_event_frame = 0;
14370 dpyinfo->x_highlight_frame = 0;
14371 dpyinfo->image_cache = make_image_cache ();
14372
14373 /* See if a private colormap is requested. */
14374 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
14375 {
14376 if (dpyinfo->visual->class == PseudoColor)
14377 {
14378 Lisp_Object value;
14379 value = display_x_get_resource (dpyinfo,
14380 build_string ("privateColormap"),
14381 build_string ("PrivateColormap"),
14382 Qnil, Qnil);
14383 if (STRINGP (value)
14384 && (!strcmp (XSTRING (value)->data, "true")
14385 || !strcmp (XSTRING (value)->data, "on")))
14386 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
14387 }
14388 }
14389 else
14390 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
14391 dpyinfo->visual, AllocNone);
14392
14393 {
14394 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
14395 double pixels = DisplayHeight (dpyinfo->display, screen_number);
14396 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
14397 dpyinfo->resy = pixels * 25.4 / mm;
14398 pixels = DisplayWidth (dpyinfo->display, screen_number);
14399 mm = DisplayWidthMM (dpyinfo->display, screen_number);
14400 dpyinfo->resx = pixels * 25.4 / mm;
14401 }
14402
14403 dpyinfo->Xatom_wm_protocols
14404 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
14405 dpyinfo->Xatom_wm_take_focus
14406 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
14407 dpyinfo->Xatom_wm_save_yourself
14408 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
14409 dpyinfo->Xatom_wm_delete_window
14410 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
14411 dpyinfo->Xatom_wm_change_state
14412 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
14413 dpyinfo->Xatom_wm_configure_denied
14414 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
14415 dpyinfo->Xatom_wm_window_moved
14416 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
14417 dpyinfo->Xatom_editres
14418 = XInternAtom (dpyinfo->display, "Editres", False);
14419 dpyinfo->Xatom_CLIPBOARD
14420 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
14421 dpyinfo->Xatom_TIMESTAMP
14422 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
14423 dpyinfo->Xatom_TEXT
14424 = XInternAtom (dpyinfo->display, "TEXT", False);
14425 dpyinfo->Xatom_COMPOUND_TEXT
14426 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
14427 dpyinfo->Xatom_DELETE
14428 = XInternAtom (dpyinfo->display, "DELETE", False);
14429 dpyinfo->Xatom_MULTIPLE
14430 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
14431 dpyinfo->Xatom_INCR
14432 = XInternAtom (dpyinfo->display, "INCR", False);
14433 dpyinfo->Xatom_EMACS_TMP
14434 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
14435 dpyinfo->Xatom_TARGETS
14436 = XInternAtom (dpyinfo->display, "TARGETS", False);
14437 dpyinfo->Xatom_NULL
14438 = XInternAtom (dpyinfo->display, "NULL", False);
14439 dpyinfo->Xatom_ATOM_PAIR
14440 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
14441 /* For properties of font. */
14442 dpyinfo->Xatom_PIXEL_SIZE
14443 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
14444 dpyinfo->Xatom_MULE_BASELINE_OFFSET
14445 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
14446 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
14447 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
14448 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
14449 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
14450
14451 /* Ghostscript support. */
14452 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
14453 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
14454
14455 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
14456 False);
14457
14458 dpyinfo->cut_buffers_initialized = 0;
14459
14460 connection = ConnectionNumber (dpyinfo->display);
14461 dpyinfo->connection = connection;
14462
14463 {
14464 char null_bits[1];
14465
14466 null_bits[0] = 0x00;
14467
14468 dpyinfo->null_pixel
14469 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14470 null_bits, 1, 1, (long) 0, (long) 0,
14471 1);
14472 }
14473
14474 {
14475 extern int gray_bitmap_width, gray_bitmap_height;
14476 extern char *gray_bitmap_bits;
14477 dpyinfo->gray
14478 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14479 gray_bitmap_bits,
14480 gray_bitmap_width, gray_bitmap_height,
14481 (unsigned long) 1, (unsigned long) 0, 1);
14482 }
14483
14484 #ifdef HAVE_X_I18N
14485 xim_initialize (dpyinfo, resource_name);
14486 #endif
14487
14488 #ifdef subprocesses
14489 /* This is only needed for distinguishing keyboard and process input. */
14490 if (connection != 0)
14491 add_keyboard_wait_descriptor (connection);
14492 #endif
14493
14494 #ifndef F_SETOWN_BUG
14495 #ifdef F_SETOWN
14496 #ifdef F_SETOWN_SOCK_NEG
14497 /* stdin is a socket here */
14498 fcntl (connection, F_SETOWN, -getpid ());
14499 #else /* ! defined (F_SETOWN_SOCK_NEG) */
14500 fcntl (connection, F_SETOWN, getpid ());
14501 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
14502 #endif /* ! defined (F_SETOWN) */
14503 #endif /* F_SETOWN_BUG */
14504
14505 #ifdef SIGIO
14506 if (interrupt_input)
14507 init_sigio (connection);
14508 #endif /* ! defined (SIGIO) */
14509
14510 #ifdef USE_LUCID
14511 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
14512 /* Make sure that we have a valid font for dialog boxes
14513 so that Xt does not crash. */
14514 {
14515 Display *dpy = dpyinfo->display;
14516 XrmValue d, fr, to;
14517 Font font;
14518 int count;
14519
14520 d.addr = (XPointer)&dpy;
14521 d.size = sizeof (Display *);
14522 fr.addr = XtDefaultFont;
14523 fr.size = sizeof (XtDefaultFont);
14524 to.size = sizeof (Font *);
14525 to.addr = (XPointer)&font;
14526 count = x_catch_errors (dpy);
14527 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
14528 abort ();
14529 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
14530 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
14531 x_uncatch_errors (dpy, count);
14532 }
14533 #endif
14534 #endif
14535
14536 /* See if we should run in synchronous mode. This is useful
14537 for debugging X code. */
14538 {
14539 Lisp_Object value;
14540 value = display_x_get_resource (dpyinfo,
14541 build_string ("synchronous"),
14542 build_string ("Synchronous"),
14543 Qnil, Qnil);
14544 if (STRINGP (value)
14545 && (!strcmp (XSTRING (value)->data, "true")
14546 || !strcmp (XSTRING (value)->data, "on")))
14547 XSynchronize (dpyinfo->display, True);
14548 }
14549
14550 UNBLOCK_INPUT;
14551
14552 return dpyinfo;
14553 }
14554 \f
14555 /* Get rid of display DPYINFO, assuming all frames are already gone,
14556 and without sending any more commands to the X server. */
14557
14558 void
14559 x_delete_display (dpyinfo)
14560 struct x_display_info *dpyinfo;
14561 {
14562 delete_keyboard_wait_descriptor (dpyinfo->connection);
14563
14564 /* Discard this display from x_display_name_list and x_display_list.
14565 We can't use Fdelq because that can quit. */
14566 if (! NILP (x_display_name_list)
14567 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
14568 x_display_name_list = XCDR (x_display_name_list);
14569 else
14570 {
14571 Lisp_Object tail;
14572
14573 tail = x_display_name_list;
14574 while (CONSP (tail) && CONSP (XCDR (tail)))
14575 {
14576 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
14577 {
14578 XSETCDR (tail, XCDR (XCDR (tail)));
14579 break;
14580 }
14581 tail = XCDR (tail);
14582 }
14583 }
14584
14585 if (next_noop_dpyinfo == dpyinfo)
14586 next_noop_dpyinfo = dpyinfo->next;
14587
14588 if (x_display_list == dpyinfo)
14589 x_display_list = dpyinfo->next;
14590 else
14591 {
14592 struct x_display_info *tail;
14593
14594 for (tail = x_display_list; tail; tail = tail->next)
14595 if (tail->next == dpyinfo)
14596 tail->next = tail->next->next;
14597 }
14598
14599 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
14600 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
14601 XrmDestroyDatabase (dpyinfo->xrdb);
14602 #endif
14603 #endif
14604 #ifdef MULTI_KBOARD
14605 if (--dpyinfo->kboard->reference_count == 0)
14606 delete_kboard (dpyinfo->kboard);
14607 #endif
14608 #ifdef HAVE_X_I18N
14609 if (dpyinfo->xim)
14610 xim_close_dpy (dpyinfo);
14611 #endif
14612
14613 xfree (dpyinfo->font_table);
14614 xfree (dpyinfo->x_id_name);
14615 xfree (dpyinfo->color_cells);
14616 xfree (dpyinfo);
14617 }
14618
14619 \f
14620 /* Set up use of X before we make the first connection. */
14621
14622 static struct redisplay_interface x_redisplay_interface =
14623 {
14624 x_produce_glyphs,
14625 x_write_glyphs,
14626 x_insert_glyphs,
14627 x_clear_end_of_line,
14628 x_scroll_run,
14629 x_after_update_window_line,
14630 x_update_window_begin,
14631 x_update_window_end,
14632 XTcursor_to,
14633 x_flush,
14634 x_clear_mouse_face,
14635 x_get_glyph_overhangs,
14636 x_fix_overlapping_area
14637 };
14638
14639 void
14640 x_initialize ()
14641 {
14642 rif = &x_redisplay_interface;
14643
14644 clear_frame_hook = x_clear_frame;
14645 ins_del_lines_hook = x_ins_del_lines;
14646 delete_glyphs_hook = x_delete_glyphs;
14647 ring_bell_hook = XTring_bell;
14648 reset_terminal_modes_hook = XTreset_terminal_modes;
14649 set_terminal_modes_hook = XTset_terminal_modes;
14650 update_begin_hook = x_update_begin;
14651 update_end_hook = x_update_end;
14652 set_terminal_window_hook = XTset_terminal_window;
14653 read_socket_hook = XTread_socket;
14654 frame_up_to_date_hook = XTframe_up_to_date;
14655 mouse_position_hook = XTmouse_position;
14656 frame_rehighlight_hook = XTframe_rehighlight;
14657 frame_raise_lower_hook = XTframe_raise_lower;
14658 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
14659 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
14660 redeem_scroll_bar_hook = XTredeem_scroll_bar;
14661 judge_scroll_bars_hook = XTjudge_scroll_bars;
14662 estimate_mode_line_height_hook = x_estimate_mode_line_height;
14663
14664 scroll_region_ok = 1; /* we'll scroll partial frames */
14665 char_ins_del_ok = 1;
14666 line_ins_del_ok = 1; /* we'll just blt 'em */
14667 fast_clear_end_of_line = 1; /* X does this well */
14668 memory_below_frame = 0; /* we don't remember what scrolls
14669 off the bottom */
14670 baud_rate = 19200;
14671
14672 x_noop_count = 0;
14673 last_tool_bar_item = -1;
14674 any_help_event_p = 0;
14675
14676 /* Try to use interrupt input; if we can't, then start polling. */
14677 Fset_input_mode (Qt, Qnil, Qt, Qnil);
14678
14679 #ifdef USE_X_TOOLKIT
14680 XtToolkitInitialize ();
14681
14682 Xt_app_con = XtCreateApplicationContext ();
14683
14684 /* Register a converter from strings to pixels, which uses
14685 Emacs' color allocation infrastructure. */
14686 XtAppSetTypeConverter (Xt_app_con,
14687 XtRString, XtRPixel, cvt_string_to_pixel,
14688 cvt_string_to_pixel_args,
14689 XtNumber (cvt_string_to_pixel_args),
14690 XtCacheByDisplay, cvt_pixel_dtor);
14691
14692 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
14693
14694 /* Install an asynchronous timer that processes Xt timeout events
14695 every 0.1s. This is necessary because some widget sets use
14696 timeouts internally, for example the LessTif menu bar, or the
14697 Xaw3d scroll bar. When Xt timouts aren't processed, these
14698 widgets don't behave normally. */
14699 {
14700 EMACS_TIME interval;
14701 EMACS_SET_SECS_USECS (interval, 0, 100000);
14702 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
14703 }
14704 #endif
14705
14706 #ifdef USE_TOOLKIT_SCROLL_BARS
14707 xaw3d_arrow_scroll = False;
14708 xaw3d_pick_top = True;
14709 #endif
14710
14711 /* Note that there is no real way portable across R3/R4 to get the
14712 original error handler. */
14713 XSetErrorHandler (x_error_handler);
14714 XSetIOErrorHandler (x_io_error_quitter);
14715
14716 /* Disable Window Change signals; they are handled by X events. */
14717 #ifdef SIGWINCH
14718 signal (SIGWINCH, SIG_DFL);
14719 #endif /* ! defined (SIGWINCH) */
14720
14721 signal (SIGPIPE, x_connection_signal);
14722 }
14723
14724
14725 void
14726 syms_of_xterm ()
14727 {
14728 staticpro (&x_error_message_string);
14729 x_error_message_string = Qnil;
14730
14731 staticpro (&x_display_name_list);
14732 x_display_name_list = Qnil;
14733
14734 staticpro (&last_mouse_scroll_bar);
14735 last_mouse_scroll_bar = Qnil;
14736
14737 staticpro (&Qvendor_specific_keysyms);
14738 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
14739
14740 staticpro (&last_mouse_press_frame);
14741 last_mouse_press_frame = Qnil;
14742
14743 help_echo = Qnil;
14744 staticpro (&help_echo);
14745 help_echo_object = Qnil;
14746 staticpro (&help_echo_object);
14747 help_echo_window = Qnil;
14748 staticpro (&help_echo_window);
14749 previous_help_echo = Qnil;
14750 staticpro (&previous_help_echo);
14751 help_echo_pos = -1;
14752
14753 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
14754 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
14755 For example, if a block cursor is over a tab, it will be drawn as
14756 wide as that tab on the display. */);
14757 x_stretch_cursor_p = 0;
14758
14759 DEFVAR_BOOL ("x-use-underline-position-properties",
14760 &x_use_underline_position_properties,
14761 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
14762 Nil means ignore them. If you encounter fonts with bogus
14763 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
14764 to 4.1, set this to nil. */);
14765 x_use_underline_position_properties = 1;
14766
14767 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
14768 doc: /* What X toolkit scroll bars Emacs uses.
14769 A value of nil means Emacs doesn't use X toolkit scroll bars.
14770 Otherwise, value is a symbol describing the X toolkit. */);
14771 #ifdef USE_TOOLKIT_SCROLL_BARS
14772 #ifdef USE_MOTIF
14773 Vx_toolkit_scroll_bars = intern ("motif");
14774 #elif defined HAVE_XAW3D
14775 Vx_toolkit_scroll_bars = intern ("xaw3d");
14776 #else
14777 Vx_toolkit_scroll_bars = intern ("xaw");
14778 #endif
14779 #else
14780 Vx_toolkit_scroll_bars = Qnil;
14781 #endif
14782
14783 staticpro (&last_mouse_motion_frame);
14784 last_mouse_motion_frame = Qnil;
14785 }
14786
14787 #endif /* HAVE_X_WINDOWS */