]> code.delx.au - gnu-emacs/blob - src/xterm.c
(x_dump_glyph_string): Put in #if GLYPH_DEBUG.
[gnu-emacs] / src / xterm.c
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
23 /* Xt features made by Fred Pierresteguy. */
24
25 #include <config.h>
26
27 /* On 4.3 these lose if they come after xterm.h. */
28 /* Putting these at the beginning seems to be standard for other .c files. */
29 #include <signal.h>
30
31 #include <stdio.h>
32
33 #ifdef HAVE_X_WINDOWS
34
35 #include "lisp.h"
36 #include "blockinput.h"
37
38 /* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40 #include "syssignal.h"
41
42 /* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44 #include "xterm.h"
45 #include <X11/cursorfont.h>
46
47 /* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49 #ifndef makedev
50 #include <sys/types.h>
51 #endif /* makedev */
52
53 #ifdef BSD_SYSTEM
54 #include <sys/ioctl.h>
55 #endif /* ! defined (BSD_SYSTEM) */
56
57 #include "systty.h"
58 #include "systime.h"
59
60 #ifndef INCLUDED_FCNTL
61 #include <fcntl.h>
62 #endif
63 #include <ctype.h>
64 #include <errno.h>
65 #include <setjmp.h>
66 #include <sys/stat.h>
67 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68 /* #include <sys/param.h> */
69
70 #include "charset.h"
71 #include "coding.h"
72 #include "ccl.h"
73 #include "frame.h"
74 #include "dispextern.h"
75 #include "fontset.h"
76 #include "termhooks.h"
77 #include "termopts.h"
78 #include "termchar.h"
79 #if 0
80 #include "sink.h"
81 #include "sinkmask.h"
82 #endif /* ! 0 */
83 #include "gnu.h"
84 #include "disptab.h"
85 #include "buffer.h"
86 #include "window.h"
87 #include "keyboard.h"
88 #include "intervals.h"
89 #include "process.h"
90 #include "atimer.h"
91
92 #ifdef USE_X_TOOLKIT
93 #include <X11/Shell.h>
94 #endif
95
96 #ifdef HAVE_SYS_TIME_H
97 #include <sys/time.h>
98 #endif
99 #ifdef HAVE_UNISTD_H
100 #include <unistd.h>
101 #endif
102
103 #ifdef USE_X_TOOLKIT
104
105 extern void free_frame_menubar P_ ((struct frame *));
106 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
107 int));
108
109 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
110 #define HACK_EDITRES
111 extern void _XEditResCheckMessages ();
112 #endif /* not NO_EDITRES */
113
114 /* Include toolkit specific headers for the scroll bar widget. */
115
116 #ifdef USE_TOOLKIT_SCROLL_BARS
117 #if defined USE_MOTIF
118 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
119 #include <Xm/ScrollBar.h>
120 #else /* !USE_MOTIF i.e. use Xaw */
121
122 #ifdef HAVE_XAW3D
123 #include <X11/Xaw3d/Simple.h>
124 #include <X11/Xaw3d/Scrollbar.h>
125 #define ARROW_SCROLLBAR
126 #include <X11/Xaw3d/ScrollbarP.h>
127 #else /* !HAVE_XAW3D */
128 #include <X11/Xaw/Simple.h>
129 #include <X11/Xaw/Scrollbar.h>
130 #endif /* !HAVE_XAW3D */
131 #ifndef XtNpickTop
132 #define XtNpickTop "pickTop"
133 #endif /* !XtNpickTop */
134 #endif /* !USE_MOTIF */
135 #endif /* USE_TOOLKIT_SCROLL_BARS */
136
137 #endif /* USE_X_TOOLKIT */
138
139 #ifndef USE_X_TOOLKIT
140 #define x_any_window_to_frame x_window_to_frame
141 #define x_top_window_to_frame x_window_to_frame
142 #endif
143
144 #ifdef USE_X_TOOLKIT
145 #include "widget.h"
146 #ifndef XtNinitialState
147 #define XtNinitialState "initialState"
148 #endif
149 #endif
150
151 #ifndef min
152 #define min(a,b) ((a) < (b) ? (a) : (b))
153 #endif
154 #ifndef max
155 #define max(a,b) ((a) > (b) ? (a) : (b))
156 #endif
157
158 #define abs(x) ((x) < 0 ? -(x) : (x))
159
160 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
161
162 \f
163 /* Bitmaps for truncated lines. */
164
165 enum bitmap_type
166 {
167 NO_BITMAP,
168 LEFT_TRUNCATION_BITMAP,
169 RIGHT_TRUNCATION_BITMAP,
170 OVERLAY_ARROW_BITMAP,
171 CONTINUED_LINE_BITMAP,
172 CONTINUATION_LINE_BITMAP,
173 ZV_LINE_BITMAP
174 };
175
176 /* Bitmap drawn to indicate lines not displaying text if
177 `indicate-empty-lines' is non-nil. */
178
179 #define zv_width 8
180 #define zv_height 8
181 static unsigned char zv_bits[] = {
182 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
183
184 /* An arrow like this: `<-'. */
185
186 #define left_width 8
187 #define left_height 8
188 static unsigned char left_bits[] = {
189 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
190
191 /* Right truncation arrow bitmap `->'. */
192
193 #define right_width 8
194 #define right_height 8
195 static unsigned char right_bits[] = {
196 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
197
198 /* Marker for continued lines. */
199
200 #define continued_width 8
201 #define continued_height 8
202 static unsigned char continued_bits[] = {
203 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
204
205 /* Marker for continuation lines. */
206
207 #define continuation_width 8
208 #define continuation_height 8
209 static unsigned char continuation_bits[] = {
210 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
211
212 /* Overlay arrow bitmap. */
213
214 #if 0
215 /* A bomb. */
216 #define ov_width 8
217 #define ov_height 8
218 static unsigned char ov_bits[] = {
219 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
220 #else
221 /* A triangular arrow. */
222 #define ov_width 8
223 #define ov_height 8
224 static unsigned char ov_bits[] = {
225 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
226
227 #endif
228
229 extern Lisp_Object Qhelp_echo;
230
231 \f
232 /* Non-nil means Emacs uses toolkit scroll bars. */
233
234 Lisp_Object Vx_toolkit_scroll_bars;
235
236 /* If a string, XTread_socket generates an event to display that string.
237 (The display is done in read_char.) */
238
239 static Lisp_Object help_echo;
240 static Lisp_Object help_echo_window;
241 static Lisp_Object help_echo_object;
242 static int help_echo_pos;
243
244 /* Temporary variable for XTread_socket. */
245
246 static Lisp_Object previous_help_echo;
247
248 /* Non-zero means that a HELP_EVENT has been generated since Emacs
249 start. */
250
251 static int any_help_event_p;
252
253 /* Non-zero means draw block and hollow cursor as wide as the glyph
254 under it. For example, if a block cursor is over a tab, it will be
255 drawn as wide as that tab on the display. */
256
257 int x_stretch_cursor_p;
258
259 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
260
261 int x_use_underline_position_properties;
262
263 /* This is a chain of structures for all the X displays currently in
264 use. */
265
266 struct x_display_info *x_display_list;
267
268 /* This is a list of cons cells, each of the form (NAME
269 . FONT-LIST-CACHE), one for each element of x_display_list and in
270 the same order. NAME is the name of the frame. FONT-LIST-CACHE
271 records previous values returned by x-list-fonts. */
272
273 Lisp_Object x_display_name_list;
274
275 /* Frame being updated by update_frame. This is declared in term.c.
276 This is set by update_begin and looked at by all the XT functions.
277 It is zero while not inside an update. In that case, the XT
278 functions assume that `selected_frame' is the frame to apply to. */
279
280 extern struct frame *updating_frame;
281
282 extern int waiting_for_input;
283
284 /* This is a frame waiting to be auto-raised, within XTread_socket. */
285
286 struct frame *pending_autoraise_frame;
287
288 #ifdef USE_X_TOOLKIT
289 /* The application context for Xt use. */
290 XtAppContext Xt_app_con;
291 static String Xt_default_resources[] = {0};
292 #endif /* USE_X_TOOLKIT */
293
294 /* Nominal cursor position -- where to draw output.
295 HPOS and VPOS are window relative glyph matrix coordinates.
296 X and Y are window relative pixel coordinates. */
297
298 struct cursor_pos output_cursor;
299
300 /* Non-zero means user is interacting with a toolkit scroll bar. */
301
302 static int toolkit_scroll_bar_interaction;
303
304 /* Mouse movement.
305
306 Formerly, we used PointerMotionHintMask (in standard_event_mask)
307 so that we would have to call XQueryPointer after each MotionNotify
308 event to ask for another such event. However, this made mouse tracking
309 slow, and there was a bug that made it eventually stop.
310
311 Simply asking for MotionNotify all the time seems to work better.
312
313 In order to avoid asking for motion events and then throwing most
314 of them away or busy-polling the server for mouse positions, we ask
315 the server for pointer motion hints. This means that we get only
316 one event per group of mouse movements. "Groups" are delimited by
317 other kinds of events (focus changes and button clicks, for
318 example), or by XQueryPointer calls; when one of these happens, we
319 get another MotionNotify event the next time the mouse moves. This
320 is at least as efficient as getting motion events when mouse
321 tracking is on, and I suspect only negligibly worse when tracking
322 is off. */
323
324 /* Where the mouse was last time we reported a mouse event. */
325
326 FRAME_PTR last_mouse_frame;
327 static XRectangle last_mouse_glyph;
328 static Lisp_Object last_mouse_press_frame;
329
330 /* The scroll bar in which the last X motion event occurred.
331
332 If the last X motion event occurred in a scroll bar, we set this so
333 XTmouse_position can know whether to report a scroll bar motion or
334 an ordinary motion.
335
336 If the last X motion event didn't occur in a scroll bar, we set
337 this to Qnil, to tell XTmouse_position to return an ordinary motion
338 event. */
339
340 static Lisp_Object last_mouse_scroll_bar;
341
342 /* This is a hack. We would really prefer that XTmouse_position would
343 return the time associated with the position it returns, but there
344 doesn't seem to be any way to wrest the time-stamp from the server
345 along with the position query. So, we just keep track of the time
346 of the last movement we received, and return that in hopes that
347 it's somewhat accurate. */
348
349 static Time last_mouse_movement_time;
350
351 /* Incremented by XTread_socket whenever it really tries to read
352 events. */
353
354 #ifdef __STDC__
355 static int volatile input_signal_count;
356 #else
357 static int input_signal_count;
358 #endif
359
360 /* Used locally within XTread_socket. */
361
362 static int x_noop_count;
363
364 /* Initial values of argv and argc. */
365
366 extern char **initial_argv;
367 extern int initial_argc;
368
369 extern Lisp_Object Vcommand_line_args, Vsystem_name;
370
371 /* Tells if a window manager is present or not. */
372
373 extern Lisp_Object Vx_no_window_manager;
374
375 extern Lisp_Object Qface, Qmouse_face;
376
377 extern int errno;
378
379 /* A mask of extra modifier bits to put into every keyboard char. */
380
381 extern int extra_keyboard_modifiers;
382
383 static Lisp_Object Qvendor_specific_keysyms;
384
385 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
386 extern Lisp_Object x_icon_type P_ ((struct frame *));
387
388
389 /* Enumeration for overriding/changing the face to use for drawing
390 glyphs in x_draw_glyphs. */
391
392 enum draw_glyphs_face
393 {
394 DRAW_NORMAL_TEXT,
395 DRAW_INVERSE_VIDEO,
396 DRAW_CURSOR,
397 DRAW_MOUSE_FACE,
398 DRAW_IMAGE_RAISED,
399 DRAW_IMAGE_SUNKEN
400 };
401
402 static int cursor_in_mouse_face_p P_ ((struct window *));
403 static int clear_mouse_face P_ ((struct x_display_info *));
404 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
405 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
406 static const XColor *x_color_cells P_ ((Display *, int *));
407 static void x_update_window_end P_ ((struct window *, int, int));
408 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
409 void x_delete_display P_ ((struct x_display_info *));
410 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
411 unsigned));
412 static int fast_find_position P_ ((struct window *, int, int *, int *,
413 int *, int *));
414 static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
415 int *, int *, int *, int *, int));
416 static void set_output_cursor P_ ((struct cursor_pos *));
417 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
418 int *, int *, int *, int));
419 static void note_mode_line_highlight P_ ((struct window *, int, int));
420 static void note_mouse_highlight P_ ((struct frame *, int, int));
421 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
422 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
423 static void show_mouse_face P_ ((struct x_display_info *,
424 enum draw_glyphs_face));
425 static int x_io_error_quitter P_ ((Display *));
426 int x_catch_errors P_ ((Display *));
427 void x_uncatch_errors P_ ((Display *, int));
428 void x_lower_frame P_ ((struct frame *));
429 void x_scroll_bar_clear P_ ((struct frame *));
430 int x_had_errors_p P_ ((Display *));
431 void x_wm_set_size_hint P_ ((struct frame *, long, int));
432 void x_raise_frame P_ ((struct frame *));
433 void x_set_window_size P_ ((struct frame *, int, int, int));
434 void x_wm_set_window_state P_ ((struct frame *, int));
435 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
436 void x_initialize P_ ((void));
437 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
438 static int x_compute_min_glyph_bounds P_ ((struct frame *));
439 static void x_draw_phys_cursor_glyph P_ ((struct window *,
440 struct glyph_row *,
441 enum draw_glyphs_face));
442 static void x_update_end P_ ((struct frame *));
443 static void XTframe_up_to_date P_ ((struct frame *));
444 static void XTreassert_line_highlight P_ ((int, int));
445 static void x_change_line_highlight P_ ((int, int, int, int));
446 static void XTset_terminal_modes P_ ((void));
447 static void XTreset_terminal_modes P_ ((void));
448 static void XTcursor_to P_ ((int, int, int, int));
449 static void x_write_glyphs P_ ((struct glyph *, int));
450 static void x_clear_end_of_line P_ ((int));
451 static void x_clear_frame P_ ((void));
452 static void x_clear_cursor P_ ((struct window *));
453 static void frame_highlight P_ ((struct frame *));
454 static void frame_unhighlight P_ ((struct frame *));
455 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
456 static void XTframe_rehighlight P_ ((struct frame *));
457 static void x_frame_rehighlight P_ ((struct x_display_info *));
458 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
459 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
460 static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
461 XRectangle *));
462 static void expose_frame P_ ((struct frame *, int, int, int, int));
463 static void expose_window_tree P_ ((struct window *, XRectangle *));
464 static int expose_window P_ ((struct window *, XRectangle *));
465 static void expose_area P_ ((struct window *, struct glyph_row *,
466 XRectangle *, enum glyph_row_area));
467 static void expose_line P_ ((struct window *, struct glyph_row *,
468 XRectangle *));
469 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
470 static void x_update_window_cursor P_ ((struct window *, int));
471 static void x_erase_phys_cursor P_ ((struct window *));
472 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
473 static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
474 enum bitmap_type));
475
476 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
477 GC, int));
478 static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
479 static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
480 static void note_overwritten_text_cursor P_ ((struct window *, int, int));
481 static void x_flush P_ ((struct frame *f));
482 static void x_update_begin P_ ((struct frame *));
483 static void x_update_window_begin P_ ((struct window *));
484 static void x_draw_vertical_border P_ ((struct window *));
485 static void x_after_update_window_line P_ ((struct glyph_row *));
486 static INLINE void take_vertical_position_into_account P_ ((struct it *));
487 static void x_produce_stretch_glyph P_ ((struct it *));
488 static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
489 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
490 enum scroll_bar_part *,
491 Lisp_Object *, Lisp_Object *,
492 unsigned long *));
493
494 /* Flush display of frame F, or of all frames if F is null. */
495
496 static void
497 x_flush (f)
498 struct frame *f;
499 {
500 BLOCK_INPUT;
501 if (f == NULL)
502 {
503 Lisp_Object rest, frame;
504 FOR_EACH_FRAME (rest, frame)
505 x_flush (XFRAME (frame));
506 }
507 else if (FRAME_X_P (f))
508 XFlush (FRAME_X_DISPLAY (f));
509 UNBLOCK_INPUT;
510 }
511
512
513 /* Remove calls to XFlush by defining XFlush to an empty replacement.
514 Calls to XFlush should be unnecessary because the X output buffer
515 is flushed automatically as needed by calls to XPending,
516 XNextEvent, or XWindowEvent according to the XFlush man page.
517 XTread_socket calls XPending. Removing XFlush improves
518 performance. */
519
520 #define XFlush(DISPLAY) (void) 0
521
522 \f
523 /***********************************************************************
524 Debugging
525 ***********************************************************************/
526
527 #if 0
528
529 /* This is a function useful for recording debugging information about
530 the sequence of occurrences in this file. */
531
532 struct record
533 {
534 char *locus;
535 int type;
536 };
537
538 struct record event_record[100];
539
540 int event_record_index;
541
542 record_event (locus, type)
543 char *locus;
544 int type;
545 {
546 if (event_record_index == sizeof (event_record) / sizeof (struct record))
547 event_record_index = 0;
548
549 event_record[event_record_index].locus = locus;
550 event_record[event_record_index].type = type;
551 event_record_index++;
552 }
553
554 #endif /* 0 */
555
556
557 \f
558 /* Return the struct x_display_info corresponding to DPY. */
559
560 struct x_display_info *
561 x_display_info_for_display (dpy)
562 Display *dpy;
563 {
564 struct x_display_info *dpyinfo;
565
566 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
567 if (dpyinfo->display == dpy)
568 return dpyinfo;
569
570 return 0;
571 }
572
573
574 \f
575 /***********************************************************************
576 Starting and ending an update
577 ***********************************************************************/
578
579 /* Start an update of frame F. This function is installed as a hook
580 for update_begin, i.e. it is called when update_begin is called.
581 This function is called prior to calls to x_update_window_begin for
582 each window being updated. Currently, there is nothing to do here
583 because all interesting stuff is done on a window basis. */
584
585 static void
586 x_update_begin (f)
587 struct frame *f;
588 {
589 /* Nothing to do. */
590 }
591
592
593 /* Start update of window W. Set the global variable updated_window
594 to the window being updated and set output_cursor to the cursor
595 position of W. */
596
597 static void
598 x_update_window_begin (w)
599 struct window *w;
600 {
601 struct frame *f = XFRAME (WINDOW_FRAME (w));
602 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
603
604 updated_window = w;
605 set_output_cursor (&w->cursor);
606
607 BLOCK_INPUT;
608
609 if (f == display_info->mouse_face_mouse_frame)
610 {
611 /* Don't do highlighting for mouse motion during the update. */
612 display_info->mouse_face_defer = 1;
613
614 /* If F needs to be redrawn, simply forget about any prior mouse
615 highlighting. */
616 if (FRAME_GARBAGED_P (f))
617 display_info->mouse_face_window = Qnil;
618
619 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
620 their mouse_face_p flag set, which means that they are always
621 unequal to rows in a desired matrix which never have that
622 flag set. So, rows containing mouse-face glyphs are never
623 scrolled, and we don't have to switch the mouse highlight off
624 here to prevent it from being scrolled. */
625
626 /* Can we tell that this update does not affect the window
627 where the mouse highlight is? If so, no need to turn off.
628 Likewise, don't do anything if the frame is garbaged;
629 in that case, the frame's current matrix that we would use
630 is all wrong, and we will redisplay that line anyway. */
631 if (!NILP (display_info->mouse_face_window)
632 && w == XWINDOW (display_info->mouse_face_window))
633 {
634 int i;
635
636 for (i = 0; i < w->desired_matrix->nrows; ++i)
637 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
638 break;
639
640 if (i < w->desired_matrix->nrows)
641 clear_mouse_face (display_info);
642 }
643 #endif /* 0 */
644 }
645
646 UNBLOCK_INPUT;
647 }
648
649
650 /* Draw a vertical window border to the right of window W if W doesn't
651 have vertical scroll bars. */
652
653 static void
654 x_draw_vertical_border (w)
655 struct window *w;
656 {
657 struct frame *f = XFRAME (WINDOW_FRAME (w));
658
659 /* Redraw borders between horizontally adjacent windows. Don't
660 do it for frames with vertical scroll bars because either the
661 right scroll bar of a window, or the left scroll bar of its
662 neighbor will suffice as a border. */
663 if (!WINDOW_RIGHTMOST_P (w)
664 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
665 {
666 int x0, x1, y0, y1;
667
668 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
669 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
670 y1 -= 1;
671
672 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
673 f->output_data.x->normal_gc, x1, y0, x1, y1);
674 }
675 }
676
677
678 /* End update of window W (which is equal to updated_window).
679
680 Draw vertical borders between horizontally adjacent windows, and
681 display W's cursor if CURSOR_ON_P is non-zero.
682
683 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
684 glyphs in mouse-face were overwritten. In that case we have to
685 make sure that the mouse-highlight is properly redrawn.
686
687 W may be a menu bar pseudo-window in case we don't have X toolkit
688 support. Such windows don't have a cursor, so don't display it
689 here. */
690
691 static void
692 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
693 struct window *w;
694 int cursor_on_p, mouse_face_overwritten_p;
695 {
696 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
697
698 if (!w->pseudo_window_p)
699 {
700 BLOCK_INPUT;
701
702 if (cursor_on_p)
703 x_display_and_set_cursor (w, 1, output_cursor.hpos,
704 output_cursor.vpos,
705 output_cursor.x, output_cursor.y);
706
707 x_draw_vertical_border (w);
708 UNBLOCK_INPUT;
709 }
710
711 /* If a row with mouse-face was overwritten, arrange for
712 XTframe_up_to_date to redisplay the mouse highlight. */
713 if (mouse_face_overwritten_p)
714 {
715 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
716 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
717 dpyinfo->mouse_face_window = Qnil;
718 }
719
720 updated_window = NULL;
721 }
722
723
724 /* End update of frame F. This function is installed as a hook in
725 update_end. */
726
727 static void
728 x_update_end (f)
729 struct frame *f;
730 {
731 /* Mouse highlight may be displayed again. */
732 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
733
734 BLOCK_INPUT;
735 XFlush (FRAME_X_DISPLAY (f));
736 UNBLOCK_INPUT;
737 }
738
739
740 /* This function is called from various places in xdisp.c whenever a
741 complete update has been performed. The global variable
742 updated_window is not available here. */
743
744 static void
745 XTframe_up_to_date (f)
746 struct frame *f;
747 {
748 if (FRAME_X_P (f))
749 {
750 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
751
752 if (dpyinfo->mouse_face_deferred_gc
753 || f == dpyinfo->mouse_face_mouse_frame)
754 {
755 BLOCK_INPUT;
756 if (dpyinfo->mouse_face_mouse_frame)
757 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
758 dpyinfo->mouse_face_mouse_x,
759 dpyinfo->mouse_face_mouse_y);
760 dpyinfo->mouse_face_deferred_gc = 0;
761 UNBLOCK_INPUT;
762 }
763 }
764 }
765
766
767 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
768 arrow bitmaps, or clear the areas where they would be displayed
769 before DESIRED_ROW is made current. The window being updated is
770 found in updated_window. This function It is called from
771 update_window_line only if it is known that there are differences
772 between bitmaps to be drawn between current row and DESIRED_ROW. */
773
774 static void
775 x_after_update_window_line (desired_row)
776 struct glyph_row *desired_row;
777 {
778 struct window *w = updated_window;
779
780 xassert (w);
781
782 if (!desired_row->mode_line_p && !w->pseudo_window_p)
783 {
784 struct frame *f;
785 int width;
786
787 BLOCK_INPUT;
788 x_draw_row_bitmaps (w, desired_row);
789
790 /* When a window has disappeared, make sure that no rest of
791 full-width rows stays visible in the internal border. */
792 if (windows_or_buffers_changed
793 && (f = XFRAME (w->frame),
794 width = FRAME_INTERNAL_BORDER_WIDTH (f),
795 width != 0))
796 {
797 int height = desired_row->visible_height;
798 int x = (window_box_right (w, -1)
799 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
800 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
801
802 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
803 x, y, width, height, False);
804 }
805
806 UNBLOCK_INPUT;
807 }
808 }
809
810
811 /* Draw the bitmap WHICH in one of the areas to the left or right of
812 window W. ROW is the glyph row for which to display the bitmap; it
813 determines the vertical position at which the bitmap has to be
814 drawn. */
815
816 static void
817 x_draw_bitmap (w, row, which)
818 struct window *w;
819 struct glyph_row *row;
820 enum bitmap_type which;
821 {
822 struct frame *f = XFRAME (WINDOW_FRAME (w));
823 Display *display = FRAME_X_DISPLAY (f);
824 Window window = FRAME_X_WINDOW (f);
825 int x, y, wd, h, dy;
826 unsigned char *bits;
827 Pixmap pixmap;
828 GC gc = f->output_data.x->normal_gc;
829 struct face *face;
830 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
831
832 /* Must clip because of partially visible lines. */
833 x_clip_to_row (w, row, gc, 1);
834
835 switch (which)
836 {
837 case LEFT_TRUNCATION_BITMAP:
838 wd = left_width;
839 h = left_height;
840 bits = left_bits;
841 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
842 - wd
843 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
844 break;
845
846 case OVERLAY_ARROW_BITMAP:
847 wd = left_width;
848 h = left_height;
849 bits = ov_bits;
850 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
851 - wd
852 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
853 break;
854
855 case RIGHT_TRUNCATION_BITMAP:
856 wd = right_width;
857 h = right_height;
858 bits = right_bits;
859 x = window_box_right (w, -1);
860 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
861 break;
862
863 case CONTINUED_LINE_BITMAP:
864 wd = right_width;
865 h = right_height;
866 bits = continued_bits;
867 x = window_box_right (w, -1);
868 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
869 break;
870
871 case CONTINUATION_LINE_BITMAP:
872 wd = continuation_width;
873 h = continuation_height;
874 bits = continuation_bits;
875 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
876 - wd
877 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
878 break;
879
880 case ZV_LINE_BITMAP:
881 wd = zv_width;
882 h = zv_height;
883 bits = zv_bits;
884 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
885 - wd
886 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
887 break;
888
889 default:
890 abort ();
891 }
892
893 /* Convert to frame coordinates. Set dy to the offset in the row to
894 start drawing the bitmap. */
895 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
896 dy = (row->height - h) / 2;
897
898 /* Draw the bitmap. I believe these small pixmaps can be cached
899 by the server. */
900 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
901 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
902 face->foreground,
903 face->background, depth);
904 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
905 XFreePixmap (display, pixmap);
906 XSetClipMask (display, gc, None);
907 }
908
909
910 /* Draw flags bitmaps for glyph row ROW on window W. Call this
911 function with input blocked. */
912
913 static void
914 x_draw_row_bitmaps (w, row)
915 struct window *w;
916 struct glyph_row *row;
917 {
918 struct frame *f = XFRAME (w->frame);
919 enum bitmap_type bitmap;
920 struct face *face;
921 int header_line_height = -1;
922
923 xassert (interrupt_input_blocked);
924
925 /* If row is completely invisible, because of vscrolling, we
926 don't have to draw anything. */
927 if (row->visible_height <= 0)
928 return;
929
930 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
931 PREPARE_FACE_FOR_DISPLAY (f, face);
932
933 /* Decide which bitmap to draw at the left side. */
934 if (row->overlay_arrow_p)
935 bitmap = OVERLAY_ARROW_BITMAP;
936 else if (row->truncated_on_left_p)
937 bitmap = LEFT_TRUNCATION_BITMAP;
938 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
939 bitmap = CONTINUATION_LINE_BITMAP;
940 else if (row->indicate_empty_line_p)
941 bitmap = ZV_LINE_BITMAP;
942 else
943 bitmap = NO_BITMAP;
944
945 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
946 the flags area. */
947 if (bitmap == NO_BITMAP
948 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
949 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
950 {
951 /* If W has a vertical border to its left, don't draw over it. */
952 int border = ((XFASTINT (w->left) > 0
953 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
954 ? 1 : 0);
955 int left = window_box_left (w, -1);
956
957 if (header_line_height < 0)
958 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
959
960 /* In case the same realized face is used for bitmap areas and
961 for something displayed in the text (e.g. face `region' on
962 mono-displays, the fill style may have been changed to
963 FillSolid in x_draw_glyph_string_background. */
964 if (face->stipple)
965 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
966 else
967 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
968
969 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
970 face->gc,
971 (left
972 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
973 + border),
974 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
975 row->y)),
976 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
977 row->visible_height);
978 if (!face->stipple)
979 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
980 }
981
982 /* Draw the left bitmap. */
983 if (bitmap != NO_BITMAP)
984 x_draw_bitmap (w, row, bitmap);
985
986 /* Decide which bitmap to draw at the right side. */
987 if (row->truncated_on_right_p)
988 bitmap = RIGHT_TRUNCATION_BITMAP;
989 else if (row->continued_p)
990 bitmap = CONTINUED_LINE_BITMAP;
991 else
992 bitmap = NO_BITMAP;
993
994 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
995 the flags area. */
996 if (bitmap == NO_BITMAP
997 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
998 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
999 {
1000 int right = window_box_right (w, -1);
1001
1002 if (header_line_height < 0)
1003 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1004
1005 /* In case the same realized face is used for bitmap areas and
1006 for something displayed in the text (e.g. face `region' on
1007 mono-displays, the fill style may have been changed to
1008 FillSolid in x_draw_glyph_string_background. */
1009 if (face->stipple)
1010 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
1011 else
1012 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
1013 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1014 face->gc,
1015 right,
1016 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
1017 row->y)),
1018 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
1019 row->visible_height);
1020 if (!face->stipple)
1021 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
1022 }
1023
1024 /* Draw the right bitmap. */
1025 if (bitmap != NO_BITMAP)
1026 x_draw_bitmap (w, row, bitmap);
1027 }
1028
1029 \f
1030 /***********************************************************************
1031 Line Highlighting
1032 ***********************************************************************/
1033
1034 /* External interface to control of standout mode. Not used for X
1035 frames. Aborts when called. */
1036
1037 static void
1038 XTreassert_line_highlight (new, vpos)
1039 int new, vpos;
1040 {
1041 abort ();
1042 }
1043
1044
1045 /* Call this when about to modify line at position VPOS and change
1046 whether it is highlighted. Not used for X frames. Aborts when
1047 called. */
1048
1049 static void
1050 x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1051 int new_highlight, vpos, y, first_unused_hpos;
1052 {
1053 abort ();
1054 }
1055
1056
1057 /* This is called when starting Emacs and when restarting after
1058 suspend. When starting Emacs, no X window is mapped. And nothing
1059 must be done to Emacs's own window if it is suspended (though that
1060 rarely happens). */
1061
1062 static void
1063 XTset_terminal_modes ()
1064 {
1065 }
1066
1067 /* This is called when exiting or suspending Emacs. Exiting will make
1068 the X-windows go away, and suspending requires no action. */
1069
1070 static void
1071 XTreset_terminal_modes ()
1072 {
1073 }
1074
1075
1076 \f
1077 /***********************************************************************
1078 Output Cursor
1079 ***********************************************************************/
1080
1081 /* Set the global variable output_cursor to CURSOR. All cursor
1082 positions are relative to updated_window. */
1083
1084 static void
1085 set_output_cursor (cursor)
1086 struct cursor_pos *cursor;
1087 {
1088 output_cursor.hpos = cursor->hpos;
1089 output_cursor.vpos = cursor->vpos;
1090 output_cursor.x = cursor->x;
1091 output_cursor.y = cursor->y;
1092 }
1093
1094
1095 /* Set a nominal cursor position.
1096
1097 HPOS and VPOS are column/row positions in a window glyph matrix. X
1098 and Y are window text area relative pixel positions.
1099
1100 If this is done during an update, updated_window will contain the
1101 window that is being updated and the position is the future output
1102 cursor position for that window. If updated_window is null, use
1103 selected_window and display the cursor at the given position. */
1104
1105 static void
1106 XTcursor_to (vpos, hpos, y, x)
1107 int vpos, hpos, y, x;
1108 {
1109 struct window *w;
1110
1111 /* If updated_window is not set, work on selected_window. */
1112 if (updated_window)
1113 w = updated_window;
1114 else
1115 w = XWINDOW (selected_window);
1116
1117 /* Set the output cursor. */
1118 output_cursor.hpos = hpos;
1119 output_cursor.vpos = vpos;
1120 output_cursor.x = x;
1121 output_cursor.y = y;
1122
1123 /* If not called as part of an update, really display the cursor.
1124 This will also set the cursor position of W. */
1125 if (updated_window == NULL)
1126 {
1127 BLOCK_INPUT;
1128 x_display_cursor (w, 1, hpos, vpos, x, y);
1129 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1130 UNBLOCK_INPUT;
1131 }
1132 }
1133
1134
1135 \f
1136 /***********************************************************************
1137 Display Iterator
1138 ***********************************************************************/
1139
1140 /* Function prototypes of this page. */
1141
1142 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1143 struct glyph *,
1144 XChar2b *,
1145 int *));
1146 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1147 int, XChar2b *, int));
1148 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1149 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1150 static void x_append_glyph P_ ((struct it *));
1151 static void x_append_composite_glyph P_ ((struct it *));
1152 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1153 int, int, double));
1154 static void x_produce_glyphs P_ ((struct it *));
1155 static void x_produce_image_glyph P_ ((struct it *it));
1156
1157
1158 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1159 is not contained in the font. */
1160
1161 static INLINE XCharStruct *
1162 x_per_char_metric (font, char2b)
1163 XFontStruct *font;
1164 XChar2b *char2b;
1165 {
1166 /* The result metric information. */
1167 XCharStruct *pcm = NULL;
1168
1169 xassert (font && char2b);
1170
1171 if (font->per_char != NULL)
1172 {
1173 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1174 {
1175 /* min_char_or_byte2 specifies the linear character index
1176 corresponding to the first element of the per_char array,
1177 max_char_or_byte2 is the index of the last character. A
1178 character with non-zero CHAR2B->byte1 is not in the font.
1179 A character with byte2 less than min_char_or_byte2 or
1180 greater max_char_or_byte2 is not in the font. */
1181 if (char2b->byte1 == 0
1182 && char2b->byte2 >= font->min_char_or_byte2
1183 && char2b->byte2 <= font->max_char_or_byte2)
1184 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1185 }
1186 else
1187 {
1188 /* If either min_byte1 or max_byte1 are nonzero, both
1189 min_char_or_byte2 and max_char_or_byte2 are less than
1190 256, and the 2-byte character index values corresponding
1191 to the per_char array element N (counting from 0) are:
1192
1193 byte1 = N/D + min_byte1
1194 byte2 = N\D + min_char_or_byte2
1195
1196 where:
1197
1198 D = max_char_or_byte2 - min_char_or_byte2 + 1
1199 / = integer division
1200 \ = integer modulus */
1201 if (char2b->byte1 >= font->min_byte1
1202 && char2b->byte1 <= font->max_byte1
1203 && char2b->byte2 >= font->min_char_or_byte2
1204 && char2b->byte2 <= font->max_char_or_byte2)
1205 {
1206 pcm = (font->per_char
1207 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1208 * (char2b->byte1 - font->min_byte1))
1209 + (char2b->byte2 - font->min_char_or_byte2));
1210 }
1211 }
1212 }
1213 else
1214 {
1215 /* If the per_char pointer is null, all glyphs between the first
1216 and last character indexes inclusive have the same
1217 information, as given by both min_bounds and max_bounds. */
1218 if (char2b->byte2 >= font->min_char_or_byte2
1219 && char2b->byte2 <= font->max_char_or_byte2)
1220 pcm = &font->max_bounds;
1221 }
1222
1223 return ((pcm == NULL
1224 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1225 ? NULL : pcm);
1226 }
1227
1228
1229 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1230 the two-byte form of C. Encoding is returned in *CHAR2B. */
1231
1232 static INLINE void
1233 x_encode_char (c, char2b, font_info)
1234 int c;
1235 XChar2b *char2b;
1236 struct font_info *font_info;
1237 {
1238 int charset = CHAR_CHARSET (c);
1239 XFontStruct *font = font_info->font;
1240
1241 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1242 This may be either a program in a special encoder language or a
1243 fixed encoding. */
1244 if (font_info->font_encoder)
1245 {
1246 /* It's a program. */
1247 struct ccl_program *ccl = font_info->font_encoder;
1248
1249 if (CHARSET_DIMENSION (charset) == 1)
1250 {
1251 ccl->reg[0] = charset;
1252 ccl->reg[1] = char2b->byte2;
1253 }
1254 else
1255 {
1256 ccl->reg[0] = charset;
1257 ccl->reg[1] = char2b->byte1;
1258 ccl->reg[2] = char2b->byte2;
1259 }
1260
1261 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1262
1263 /* We assume that MSBs are appropriately set/reset by CCL
1264 program. */
1265 if (font->max_byte1 == 0) /* 1-byte font */
1266 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1267 else
1268 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1269 }
1270 else if (font_info->encoding[charset])
1271 {
1272 /* Fixed encoding scheme. See fontset.h for the meaning of the
1273 encoding numbers. */
1274 int enc = font_info->encoding[charset];
1275
1276 if ((enc == 1 || enc == 2)
1277 && CHARSET_DIMENSION (charset) == 2)
1278 char2b->byte1 |= 0x80;
1279
1280 if (enc == 1 || enc == 3)
1281 char2b->byte2 |= 0x80;
1282 }
1283 }
1284
1285
1286 /* Get face and two-byte form of character C in face FACE_ID on frame
1287 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1288 means we want to display multibyte text. Value is a pointer to a
1289 realized face that is ready for display. */
1290
1291 static INLINE struct face *
1292 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1293 struct frame *f;
1294 int c, face_id;
1295 XChar2b *char2b;
1296 int multibyte_p;
1297 {
1298 struct face *face = FACE_FROM_ID (f, face_id);
1299
1300 if (!multibyte_p)
1301 {
1302 /* Unibyte case. We don't have to encode, but we have to make
1303 sure to use a face suitable for unibyte. */
1304 char2b->byte1 = 0;
1305 char2b->byte2 = c;
1306 face_id = FACE_FOR_CHAR (f, face, c);
1307 face = FACE_FROM_ID (f, face_id);
1308 }
1309 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1310 {
1311 /* Case of ASCII in a face known to fit ASCII. */
1312 char2b->byte1 = 0;
1313 char2b->byte2 = c;
1314 }
1315 else
1316 {
1317 int c1, c2, charset;
1318
1319 /* Split characters into bytes. If c2 is -1 afterwards, C is
1320 really a one-byte character so that byte1 is zero. */
1321 SPLIT_CHAR (c, charset, c1, c2);
1322 if (c2 > 0)
1323 char2b->byte1 = c1, char2b->byte2 = c2;
1324 else
1325 char2b->byte1 = 0, char2b->byte2 = c1;
1326
1327 /* Maybe encode the character in *CHAR2B. */
1328 if (face->font != NULL)
1329 {
1330 struct font_info *font_info
1331 = FONT_INFO_FROM_ID (f, face->font_info_id);
1332 if (font_info)
1333 x_encode_char (c, char2b, font_info);
1334 }
1335 }
1336
1337 /* Make sure X resources of the face are allocated. */
1338 xassert (face != NULL);
1339 PREPARE_FACE_FOR_DISPLAY (f, face);
1340
1341 return face;
1342 }
1343
1344
1345 /* Get face and two-byte form of character glyph GLYPH on frame F.
1346 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1347 a pointer to a realized face that is ready for display. */
1348
1349 static INLINE struct face *
1350 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1351 struct frame *f;
1352 struct glyph *glyph;
1353 XChar2b *char2b;
1354 int *two_byte_p;
1355 {
1356 struct face *face;
1357
1358 xassert (glyph->type == CHAR_GLYPH);
1359 face = FACE_FROM_ID (f, glyph->face_id);
1360
1361 if (two_byte_p)
1362 *two_byte_p = 0;
1363
1364 if (!glyph->multibyte_p)
1365 {
1366 /* Unibyte case. We don't have to encode, but we have to make
1367 sure to use a face suitable for unibyte. */
1368 char2b->byte1 = 0;
1369 char2b->byte2 = glyph->u.ch;
1370 }
1371 else if (glyph->u.ch < 128
1372 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1373 {
1374 /* Case of ASCII in a face known to fit ASCII. */
1375 char2b->byte1 = 0;
1376 char2b->byte2 = glyph->u.ch;
1377 }
1378 else
1379 {
1380 int c1, c2, charset;
1381
1382 /* Split characters into bytes. If c2 is -1 afterwards, C is
1383 really a one-byte character so that byte1 is zero. */
1384 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1385 if (c2 > 0)
1386 char2b->byte1 = c1, char2b->byte2 = c2;
1387 else
1388 char2b->byte1 = 0, char2b->byte2 = c1;
1389
1390 /* Maybe encode the character in *CHAR2B. */
1391 if (charset != CHARSET_ASCII)
1392 {
1393 struct font_info *font_info
1394 = FONT_INFO_FROM_ID (f, face->font_info_id);
1395 if (font_info)
1396 {
1397 x_encode_char (glyph->u.ch, char2b, font_info);
1398 if (two_byte_p)
1399 *two_byte_p
1400 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1401 }
1402 }
1403 }
1404
1405 /* Make sure X resources of the face are allocated. */
1406 xassert (face != NULL);
1407 PREPARE_FACE_FOR_DISPLAY (f, face);
1408 return face;
1409 }
1410
1411
1412 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1413 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1414
1415 static INLINE void
1416 x_append_glyph (it)
1417 struct it *it;
1418 {
1419 struct glyph *glyph;
1420 enum glyph_row_area area = it->area;
1421
1422 xassert (it->glyph_row);
1423 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1424
1425 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1426 if (glyph < it->glyph_row->glyphs[area + 1])
1427 {
1428 glyph->charpos = CHARPOS (it->position);
1429 glyph->object = it->object;
1430 glyph->pixel_width = it->pixel_width;
1431 glyph->voffset = it->voffset;
1432 glyph->type = CHAR_GLYPH;
1433 glyph->multibyte_p = it->multibyte_p;
1434 glyph->left_box_line_p = it->start_of_box_run_p;
1435 glyph->right_box_line_p = it->end_of_box_run_p;
1436 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1437 || it->phys_descent > it->descent);
1438 glyph->padding_p = 0;
1439 glyph->glyph_not_available_p = it->glyph_not_available_p;
1440 glyph->face_id = it->face_id;
1441 glyph->u.ch = it->char_to_display;
1442 ++it->glyph_row->used[area];
1443 }
1444 }
1445
1446 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1447 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1448
1449 static INLINE void
1450 x_append_composite_glyph (it)
1451 struct it *it;
1452 {
1453 struct glyph *glyph;
1454 enum glyph_row_area area = it->area;
1455
1456 xassert (it->glyph_row);
1457
1458 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1459 if (glyph < it->glyph_row->glyphs[area + 1])
1460 {
1461 glyph->charpos = CHARPOS (it->position);
1462 glyph->object = it->object;
1463 glyph->pixel_width = it->pixel_width;
1464 glyph->voffset = it->voffset;
1465 glyph->type = COMPOSITE_GLYPH;
1466 glyph->multibyte_p = it->multibyte_p;
1467 glyph->left_box_line_p = it->start_of_box_run_p;
1468 glyph->right_box_line_p = it->end_of_box_run_p;
1469 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1470 || it->phys_descent > it->descent);
1471 glyph->padding_p = 0;
1472 glyph->glyph_not_available_p = 0;
1473 glyph->face_id = it->face_id;
1474 glyph->u.cmp_id = it->cmp_id;
1475 ++it->glyph_row->used[area];
1476 }
1477 }
1478
1479
1480 /* Change IT->ascent and IT->height according to the setting of
1481 IT->voffset. */
1482
1483 static INLINE void
1484 take_vertical_position_into_account (it)
1485 struct it *it;
1486 {
1487 if (it->voffset)
1488 {
1489 if (it->voffset < 0)
1490 /* Increase the ascent so that we can display the text higher
1491 in the line. */
1492 it->ascent += abs (it->voffset);
1493 else
1494 /* Increase the descent so that we can display the text lower
1495 in the line. */
1496 it->descent += it->voffset;
1497 }
1498 }
1499
1500
1501 /* Produce glyphs/get display metrics for the image IT is loaded with.
1502 See the description of struct display_iterator in dispextern.h for
1503 an overview of struct display_iterator. */
1504
1505 static void
1506 x_produce_image_glyph (it)
1507 struct it *it;
1508 {
1509 struct image *img;
1510 struct face *face;
1511
1512 xassert (it->what == IT_IMAGE);
1513
1514 face = FACE_FROM_ID (it->f, it->face_id);
1515 img = IMAGE_FROM_ID (it->f, it->image_id);
1516 xassert (img);
1517
1518 /* Make sure X resources of the face and image are loaded. */
1519 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1520 prepare_image_for_display (it->f, img);
1521
1522 it->ascent = it->phys_ascent = image_ascent (img, face);
1523 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1524 it->pixel_width = img->width + 2 * img->hmargin;
1525
1526 it->nglyphs = 1;
1527
1528 if (face->box != FACE_NO_BOX)
1529 {
1530 if (face->box_line_width > 0)
1531 {
1532 it->ascent += face->box_line_width;
1533 it->descent += face->box_line_width;
1534 }
1535
1536 if (it->start_of_box_run_p)
1537 it->pixel_width += abs (face->box_line_width);
1538 if (it->end_of_box_run_p)
1539 it->pixel_width += abs (face->box_line_width);
1540 }
1541
1542 take_vertical_position_into_account (it);
1543
1544 if (it->glyph_row)
1545 {
1546 struct glyph *glyph;
1547 enum glyph_row_area area = it->area;
1548
1549 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1550 if (glyph < it->glyph_row->glyphs[area + 1])
1551 {
1552 glyph->charpos = CHARPOS (it->position);
1553 glyph->object = it->object;
1554 glyph->pixel_width = it->pixel_width;
1555 glyph->voffset = it->voffset;
1556 glyph->type = IMAGE_GLYPH;
1557 glyph->multibyte_p = it->multibyte_p;
1558 glyph->left_box_line_p = it->start_of_box_run_p;
1559 glyph->right_box_line_p = it->end_of_box_run_p;
1560 glyph->overlaps_vertically_p = 0;
1561 glyph->padding_p = 0;
1562 glyph->glyph_not_available_p = 0;
1563 glyph->face_id = it->face_id;
1564 glyph->u.img_id = img->id;
1565 ++it->glyph_row->used[area];
1566 }
1567 }
1568 }
1569
1570
1571 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1572 of the glyph, WIDTH and HEIGHT are the width and height of the
1573 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1574 ascent of the glyph (0 <= ASCENT <= 1). */
1575
1576 static void
1577 x_append_stretch_glyph (it, object, width, height, ascent)
1578 struct it *it;
1579 Lisp_Object object;
1580 int width, height;
1581 double ascent;
1582 {
1583 struct glyph *glyph;
1584 enum glyph_row_area area = it->area;
1585
1586 xassert (ascent >= 0 && ascent <= 1);
1587
1588 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1589 if (glyph < it->glyph_row->glyphs[area + 1])
1590 {
1591 glyph->charpos = CHARPOS (it->position);
1592 glyph->object = object;
1593 glyph->pixel_width = width;
1594 glyph->voffset = it->voffset;
1595 glyph->type = STRETCH_GLYPH;
1596 glyph->multibyte_p = it->multibyte_p;
1597 glyph->left_box_line_p = it->start_of_box_run_p;
1598 glyph->right_box_line_p = it->end_of_box_run_p;
1599 glyph->overlaps_vertically_p = 0;
1600 glyph->padding_p = 0;
1601 glyph->glyph_not_available_p = 0;
1602 glyph->face_id = it->face_id;
1603 glyph->u.stretch.ascent = height * ascent;
1604 glyph->u.stretch.height = height;
1605 ++it->glyph_row->used[area];
1606 }
1607 }
1608
1609
1610 /* Produce a stretch glyph for iterator IT. IT->object is the value
1611 of the glyph property displayed. The value must be a list
1612 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1613 being recognized:
1614
1615 1. `:width WIDTH' specifies that the space should be WIDTH *
1616 canonical char width wide. WIDTH may be an integer or floating
1617 point number.
1618
1619 2. `:relative-width FACTOR' specifies that the width of the stretch
1620 should be computed from the width of the first character having the
1621 `glyph' property, and should be FACTOR times that width.
1622
1623 3. `:align-to HPOS' specifies that the space should be wide enough
1624 to reach HPOS, a value in canonical character units.
1625
1626 Exactly one of the above pairs must be present.
1627
1628 4. `:height HEIGHT' specifies that the height of the stretch produced
1629 should be HEIGHT, measured in canonical character units.
1630
1631 5. `:relative-height FACTOR' specifies that the height of the the
1632 stretch should be FACTOR times the height of the characters having
1633 the glyph property.
1634
1635 Either none or exactly one of 4 or 5 must be present.
1636
1637 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1638 of the stretch should be used for the ascent of the stretch.
1639 ASCENT must be in the range 0 <= ASCENT <= 100. */
1640
1641 #define NUMVAL(X) \
1642 ((INTEGERP (X) || FLOATP (X)) \
1643 ? XFLOATINT (X) \
1644 : - 1)
1645
1646
1647 static void
1648 x_produce_stretch_glyph (it)
1649 struct it *it;
1650 {
1651 /* (space :width WIDTH :height HEIGHT. */
1652 #if GLYPH_DEBUG
1653 extern Lisp_Object Qspace;
1654 #endif
1655 extern Lisp_Object QCwidth, QCheight, QCascent;
1656 extern Lisp_Object QCrelative_width, QCrelative_height;
1657 extern Lisp_Object QCalign_to;
1658 Lisp_Object prop, plist;
1659 double width = 0, height = 0, ascent = 0;
1660 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1661 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1662
1663 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1664
1665 /* List should start with `space'. */
1666 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1667 plist = XCDR (it->object);
1668
1669 /* Compute the width of the stretch. */
1670 if (prop = Fplist_get (plist, QCwidth),
1671 NUMVAL (prop) > 0)
1672 /* Absolute width `:width WIDTH' specified and valid. */
1673 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1674 else if (prop = Fplist_get (plist, QCrelative_width),
1675 NUMVAL (prop) > 0)
1676 {
1677 /* Relative width `:relative-width FACTOR' specified and valid.
1678 Compute the width of the characters having the `glyph'
1679 property. */
1680 struct it it2;
1681 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1682
1683 it2 = *it;
1684 if (it->multibyte_p)
1685 {
1686 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1687 - IT_BYTEPOS (*it));
1688 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1689 }
1690 else
1691 it2.c = *p, it2.len = 1;
1692
1693 it2.glyph_row = NULL;
1694 it2.what = IT_CHARACTER;
1695 x_produce_glyphs (&it2);
1696 width = NUMVAL (prop) * it2.pixel_width;
1697 }
1698 else if (prop = Fplist_get (plist, QCalign_to),
1699 NUMVAL (prop) > 0)
1700 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1701 else
1702 /* Nothing specified -> width defaults to canonical char width. */
1703 width = CANON_X_UNIT (it->f);
1704
1705 /* Compute height. */
1706 if (prop = Fplist_get (plist, QCheight),
1707 NUMVAL (prop) > 0)
1708 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1709 else if (prop = Fplist_get (plist, QCrelative_height),
1710 NUMVAL (prop) > 0)
1711 height = FONT_HEIGHT (font) * NUMVAL (prop);
1712 else
1713 height = FONT_HEIGHT (font);
1714
1715 /* Compute percentage of height used for ascent. If
1716 `:ascent ASCENT' is present and valid, use that. Otherwise,
1717 derive the ascent from the font in use. */
1718 if (prop = Fplist_get (plist, QCascent),
1719 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1720 ascent = NUMVAL (prop) / 100.0;
1721 else
1722 ascent = (double) font->ascent / FONT_HEIGHT (font);
1723
1724 if (width <= 0)
1725 width = 1;
1726 if (height <= 0)
1727 height = 1;
1728
1729 if (it->glyph_row)
1730 {
1731 Lisp_Object object = it->stack[it->sp - 1].string;
1732 if (!STRINGP (object))
1733 object = it->w->buffer;
1734 x_append_stretch_glyph (it, object, width, height, ascent);
1735 }
1736
1737 it->pixel_width = width;
1738 it->ascent = it->phys_ascent = height * ascent;
1739 it->descent = it->phys_descent = height - it->ascent;
1740 it->nglyphs = 1;
1741
1742 if (face->box != FACE_NO_BOX)
1743 {
1744 if (face->box_line_width > 0)
1745 {
1746 it->ascent += face->box_line_width;
1747 it->descent += face->box_line_width;
1748 }
1749
1750 if (it->start_of_box_run_p)
1751 it->pixel_width += abs (face->box_line_width);
1752 if (it->end_of_box_run_p)
1753 it->pixel_width += abs (face->box_line_width);
1754 }
1755
1756 take_vertical_position_into_account (it);
1757 }
1758
1759 /* Return proper value to be used as baseline offset of font that has
1760 ASCENT and DESCENT to draw characters by the font at the vertical
1761 center of the line of frame F.
1762
1763 Here, out task is to find the value of BOFF in the following figure;
1764
1765 -------------------------+-----------+-
1766 -+-+---------+-+ | |
1767 | | | | | |
1768 | | | | F_ASCENT F_HEIGHT
1769 | | | ASCENT | |
1770 HEIGHT | | | | |
1771 | | |-|-+------+-----------|------- baseline
1772 | | | | BOFF | |
1773 | |---------|-+-+ | |
1774 | | | DESCENT | |
1775 -+-+---------+-+ F_DESCENT |
1776 -------------------------+-----------+-
1777
1778 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1779 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1780 DESCENT = FONT->descent
1781 HEIGHT = FONT_HEIGHT (FONT)
1782 F_DESCENT = (F->output_data.x->font->descent
1783 - F->output_data.x->baseline_offset)
1784 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1785 */
1786
1787 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1788 ((FONT)->descent \
1789 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1790 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1791 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1792
1793 /* Produce glyphs/get display metrics for the display element IT is
1794 loaded with. See the description of struct display_iterator in
1795 dispextern.h for an overview of struct display_iterator. */
1796
1797 static void
1798 x_produce_glyphs (it)
1799 struct it *it;
1800 {
1801 it->glyph_not_available_p = 0;
1802
1803 if (it->what == IT_CHARACTER)
1804 {
1805 XChar2b char2b;
1806 XFontStruct *font;
1807 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1808 XCharStruct *pcm;
1809 int font_not_found_p;
1810 struct font_info *font_info;
1811 int boff; /* baseline offset */
1812 /* We may change it->multibyte_p upon unibyte<->multibyte
1813 conversion. So, save the current value now and restore it
1814 later.
1815
1816 Note: It seems that we don't have to record multibyte_p in
1817 struct glyph because the character code itself tells if or
1818 not the character is multibyte. Thus, in the future, we must
1819 consider eliminating the field `multibyte_p' in the struct
1820 glyph. */
1821 int saved_multibyte_p = it->multibyte_p;
1822
1823 /* Maybe translate single-byte characters to multibyte, or the
1824 other way. */
1825 it->char_to_display = it->c;
1826 if (!ASCII_BYTE_P (it->c))
1827 {
1828 if (unibyte_display_via_language_environment
1829 && SINGLE_BYTE_CHAR_P (it->c)
1830 && (it->c >= 0240
1831 || !NILP (Vnonascii_translation_table)))
1832 {
1833 it->char_to_display = unibyte_char_to_multibyte (it->c);
1834 it->multibyte_p = 1;
1835 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1836 face = FACE_FROM_ID (it->f, it->face_id);
1837 }
1838 else if (!SINGLE_BYTE_CHAR_P (it->c)
1839 && !it->multibyte_p)
1840 {
1841 it->multibyte_p = 1;
1842 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1843 face = FACE_FROM_ID (it->f, it->face_id);
1844 }
1845 }
1846
1847 /* Get font to use. Encode IT->char_to_display. */
1848 x_get_char_face_and_encoding (it->f, it->char_to_display,
1849 it->face_id, &char2b,
1850 it->multibyte_p);
1851 font = face->font;
1852
1853 /* When no suitable font found, use the default font. */
1854 font_not_found_p = font == NULL;
1855 if (font_not_found_p)
1856 {
1857 font = FRAME_FONT (it->f);
1858 boff = it->f->output_data.x->baseline_offset;
1859 font_info = NULL;
1860 }
1861 else
1862 {
1863 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1864 boff = font_info->baseline_offset;
1865 if (font_info->vertical_centering)
1866 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1867 }
1868
1869 if (it->char_to_display >= ' '
1870 && (!it->multibyte_p || it->char_to_display < 128))
1871 {
1872 /* Either unibyte or ASCII. */
1873 int stretched_p;
1874
1875 it->nglyphs = 1;
1876
1877 pcm = x_per_char_metric (font, &char2b);
1878 it->ascent = font->ascent + boff;
1879 it->descent = font->descent - boff;
1880
1881 if (pcm)
1882 {
1883 it->phys_ascent = pcm->ascent + boff;
1884 it->phys_descent = pcm->descent - boff;
1885 it->pixel_width = pcm->width;
1886 }
1887 else
1888 {
1889 it->glyph_not_available_p = 1;
1890 it->phys_ascent = font->ascent + boff;
1891 it->phys_descent = font->descent - boff;
1892 it->pixel_width = FONT_WIDTH (font);
1893 }
1894
1895 /* If this is a space inside a region of text with
1896 `space-width' property, change its width. */
1897 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1898 if (stretched_p)
1899 it->pixel_width *= XFLOATINT (it->space_width);
1900
1901 /* If face has a box, add the box thickness to the character
1902 height. If character has a box line to the left and/or
1903 right, add the box line width to the character's width. */
1904 if (face->box != FACE_NO_BOX)
1905 {
1906 int thick = face->box_line_width;
1907
1908 if (thick > 0)
1909 {
1910 it->ascent += thick;
1911 it->descent += thick;
1912 }
1913 else
1914 thick = -thick;
1915
1916 if (it->start_of_box_run_p)
1917 it->pixel_width += thick;
1918 if (it->end_of_box_run_p)
1919 it->pixel_width += thick;
1920 }
1921
1922 /* If face has an overline, add the height of the overline
1923 (1 pixel) and a 1 pixel margin to the character height. */
1924 if (face->overline_p)
1925 it->ascent += 2;
1926
1927 take_vertical_position_into_account (it);
1928
1929 /* If we have to actually produce glyphs, do it. */
1930 if (it->glyph_row)
1931 {
1932 if (stretched_p)
1933 {
1934 /* Translate a space with a `space-width' property
1935 into a stretch glyph. */
1936 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1937 x_append_stretch_glyph (it, it->object, it->pixel_width,
1938 it->ascent + it->descent, ascent);
1939 }
1940 else
1941 x_append_glyph (it);
1942
1943 /* If characters with lbearing or rbearing are displayed
1944 in this line, record that fact in a flag of the
1945 glyph row. This is used to optimize X output code. */
1946 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1947 it->glyph_row->contains_overlapping_glyphs_p = 1;
1948 }
1949 }
1950 else if (it->char_to_display == '\n')
1951 {
1952 /* A newline has no width but we need the height of the line. */
1953 it->pixel_width = 0;
1954 it->nglyphs = 0;
1955 it->ascent = it->phys_ascent = font->ascent + boff;
1956 it->descent = it->phys_descent = font->descent - boff;
1957
1958 if (face->box != FACE_NO_BOX
1959 && face->box_line_width > 0)
1960 {
1961 it->ascent += face->box_line_width;
1962 it->descent += face->box_line_width;
1963 }
1964 }
1965 else if (it->char_to_display == '\t')
1966 {
1967 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1968 int x = it->current_x + it->continuation_lines_width;
1969 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1970
1971 /* If the distance from the current position to the next tab
1972 stop is less than a canonical character width, use the
1973 tab stop after that. */
1974 if (next_tab_x - x < CANON_X_UNIT (it->f))
1975 next_tab_x += tab_width;
1976
1977 it->pixel_width = next_tab_x - x;
1978 it->nglyphs = 1;
1979 it->ascent = it->phys_ascent = font->ascent + boff;
1980 it->descent = it->phys_descent = font->descent - boff;
1981
1982 if (it->glyph_row)
1983 {
1984 double ascent = (double) it->ascent / (it->ascent + it->descent);
1985 x_append_stretch_glyph (it, it->object, it->pixel_width,
1986 it->ascent + it->descent, ascent);
1987 }
1988 }
1989 else
1990 {
1991 /* A multi-byte character. Assume that the display width of the
1992 character is the width of the character multiplied by the
1993 width of the font. */
1994
1995 /* If we found a font, this font should give us the right
1996 metrics. If we didn't find a font, use the frame's
1997 default font and calculate the width of the character
1998 from the charset width; this is what old redisplay code
1999 did. */
2000 pcm = x_per_char_metric (font, &char2b);
2001 if (font_not_found_p || !pcm)
2002 {
2003 int charset = CHAR_CHARSET (it->char_to_display);
2004
2005 it->glyph_not_available_p = 1;
2006 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
2007 * CHARSET_WIDTH (charset));
2008 it->phys_ascent = font->ascent + boff;
2009 it->phys_descent = font->descent - boff;
2010 }
2011 else
2012 {
2013 it->pixel_width = pcm->width;
2014 it->phys_ascent = pcm->ascent + boff;
2015 it->phys_descent = pcm->descent - boff;
2016 if (it->glyph_row
2017 && (pcm->lbearing < 0
2018 || pcm->rbearing > pcm->width))
2019 it->glyph_row->contains_overlapping_glyphs_p = 1;
2020 }
2021 it->nglyphs = 1;
2022 it->ascent = font->ascent + boff;
2023 it->descent = font->descent - boff;
2024 if (face->box != FACE_NO_BOX)
2025 {
2026 int thick = face->box_line_width;
2027
2028 if (thick > 0)
2029 {
2030 it->ascent += thick;
2031 it->descent += thick;
2032 }
2033 else
2034 thick = - thick;
2035
2036 if (it->start_of_box_run_p)
2037 it->pixel_width += thick;
2038 if (it->end_of_box_run_p)
2039 it->pixel_width += thick;
2040 }
2041
2042 /* If face has an overline, add the height of the overline
2043 (1 pixel) and a 1 pixel margin to the character height. */
2044 if (face->overline_p)
2045 it->ascent += 2;
2046
2047 take_vertical_position_into_account (it);
2048
2049 if (it->glyph_row)
2050 x_append_glyph (it);
2051 }
2052 it->multibyte_p = saved_multibyte_p;
2053 }
2054 else if (it->what == IT_COMPOSITION)
2055 {
2056 /* Note: A composition is represented as one glyph in the
2057 glyph matrix. There are no padding glyphs. */
2058 XChar2b char2b;
2059 XFontStruct *font;
2060 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2061 XCharStruct *pcm;
2062 int font_not_found_p;
2063 struct font_info *font_info;
2064 int boff; /* baseline offset */
2065 struct composition *cmp = composition_table[it->cmp_id];
2066
2067 /* Maybe translate single-byte characters to multibyte. */
2068 it->char_to_display = it->c;
2069 if (unibyte_display_via_language_environment
2070 && SINGLE_BYTE_CHAR_P (it->c)
2071 && (it->c >= 0240
2072 || (it->c >= 0200
2073 && !NILP (Vnonascii_translation_table))))
2074 {
2075 it->char_to_display = unibyte_char_to_multibyte (it->c);
2076 }
2077
2078 /* Get face and font to use. Encode IT->char_to_display. */
2079 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2080 face = FACE_FROM_ID (it->f, it->face_id);
2081 x_get_char_face_and_encoding (it->f, it->char_to_display,
2082 it->face_id, &char2b, it->multibyte_p);
2083 font = face->font;
2084
2085 /* When no suitable font found, use the default font. */
2086 font_not_found_p = font == NULL;
2087 if (font_not_found_p)
2088 {
2089 font = FRAME_FONT (it->f);
2090 boff = it->f->output_data.x->baseline_offset;
2091 font_info = NULL;
2092 }
2093 else
2094 {
2095 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2096 boff = font_info->baseline_offset;
2097 if (font_info->vertical_centering)
2098 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2099 }
2100
2101 /* There are no padding glyphs, so there is only one glyph to
2102 produce for the composition. Important is that pixel_width,
2103 ascent and descent are the values of what is drawn by
2104 draw_glyphs (i.e. the values of the overall glyphs composed). */
2105 it->nglyphs = 1;
2106
2107 /* If we have not yet calculated pixel size data of glyphs of
2108 the composition for the current face font, calculate them
2109 now. Theoretically, we have to check all fonts for the
2110 glyphs, but that requires much time and memory space. So,
2111 here we check only the font of the first glyph. This leads
2112 to incorrect display very rarely, and C-l (recenter) can
2113 correct the display anyway. */
2114 if (cmp->font != (void *) font)
2115 {
2116 /* Ascent and descent of the font of the first character of
2117 this composition (adjusted by baseline offset). Ascent
2118 and descent of overall glyphs should not be less than
2119 them respectively. */
2120 int font_ascent = font->ascent + boff;
2121 int font_descent = font->descent - boff;
2122 /* Bounding box of the overall glyphs. */
2123 int leftmost, rightmost, lowest, highest;
2124 int i, width, ascent, descent;
2125
2126 cmp->font = (void *) font;
2127
2128 /* Initialize the bounding box. */
2129 if (font_info
2130 && (pcm = x_per_char_metric (font, &char2b)))
2131 {
2132 width = pcm->width;
2133 ascent = pcm->ascent;
2134 descent = pcm->descent;
2135 }
2136 else
2137 {
2138 width = FONT_WIDTH (font);
2139 ascent = font->ascent;
2140 descent = font->descent;
2141 }
2142
2143 rightmost = width;
2144 lowest = - descent + boff;
2145 highest = ascent + boff;
2146 leftmost = 0;
2147
2148 if (font_info
2149 && font_info->default_ascent
2150 && CHAR_TABLE_P (Vuse_default_ascent)
2151 && !NILP (Faref (Vuse_default_ascent,
2152 make_number (it->char_to_display))))
2153 highest = font_info->default_ascent + boff;
2154
2155 /* Draw the first glyph at the normal position. It may be
2156 shifted to right later if some other glyphs are drawn at
2157 the left. */
2158 cmp->offsets[0] = 0;
2159 cmp->offsets[1] = boff;
2160
2161 /* Set cmp->offsets for the remaining glyphs. */
2162 for (i = 1; i < cmp->glyph_len; i++)
2163 {
2164 int left, right, btm, top;
2165 int ch = COMPOSITION_GLYPH (cmp, i);
2166 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2167
2168 face = FACE_FROM_ID (it->f, face_id);
2169 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2170 it->multibyte_p);
2171 font = face->font;
2172 if (font == NULL)
2173 {
2174 font = FRAME_FONT (it->f);
2175 boff = it->f->output_data.x->baseline_offset;
2176 font_info = NULL;
2177 }
2178 else
2179 {
2180 font_info
2181 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2182 boff = font_info->baseline_offset;
2183 if (font_info->vertical_centering)
2184 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2185 }
2186
2187 if (font_info
2188 && (pcm = x_per_char_metric (font, &char2b)))
2189 {
2190 width = pcm->width;
2191 ascent = pcm->ascent;
2192 descent = pcm->descent;
2193 }
2194 else
2195 {
2196 width = FONT_WIDTH (font);
2197 ascent = 1;
2198 descent = 0;
2199 }
2200
2201 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2202 {
2203 /* Relative composition with or without
2204 alternate chars. */
2205 left = (leftmost + rightmost - width) / 2;
2206 btm = - descent + boff;
2207 if (font_info && font_info->relative_compose
2208 && (! CHAR_TABLE_P (Vignore_relative_composition)
2209 || NILP (Faref (Vignore_relative_composition,
2210 make_number (ch)))))
2211 {
2212
2213 if (- descent >= font_info->relative_compose)
2214 /* One extra pixel between two glyphs. */
2215 btm = highest + 1;
2216 else if (ascent <= 0)
2217 /* One extra pixel between two glyphs. */
2218 btm = lowest - 1 - ascent - descent;
2219 }
2220 }
2221 else
2222 {
2223 /* A composition rule is specified by an integer
2224 value that encodes global and new reference
2225 points (GREF and NREF). GREF and NREF are
2226 specified by numbers as below:
2227
2228 0---1---2 -- ascent
2229 | |
2230 | |
2231 | |
2232 9--10--11 -- center
2233 | |
2234 ---3---4---5--- baseline
2235 | |
2236 6---7---8 -- descent
2237 */
2238 int rule = COMPOSITION_RULE (cmp, i);
2239 int gref, nref, grefx, grefy, nrefx, nrefy;
2240
2241 COMPOSITION_DECODE_RULE (rule, gref, nref);
2242 grefx = gref % 3, nrefx = nref % 3;
2243 grefy = gref / 3, nrefy = nref / 3;
2244
2245 left = (leftmost
2246 + grefx * (rightmost - leftmost) / 2
2247 - nrefx * width / 2);
2248 btm = ((grefy == 0 ? highest
2249 : grefy == 1 ? 0
2250 : grefy == 2 ? lowest
2251 : (highest + lowest) / 2)
2252 - (nrefy == 0 ? ascent + descent
2253 : nrefy == 1 ? descent - boff
2254 : nrefy == 2 ? 0
2255 : (ascent + descent) / 2));
2256 }
2257
2258 cmp->offsets[i * 2] = left;
2259 cmp->offsets[i * 2 + 1] = btm + descent;
2260
2261 /* Update the bounding box of the overall glyphs. */
2262 right = left + width;
2263 top = btm + descent + ascent;
2264 if (left < leftmost)
2265 leftmost = left;
2266 if (right > rightmost)
2267 rightmost = right;
2268 if (top > highest)
2269 highest = top;
2270 if (btm < lowest)
2271 lowest = btm;
2272 }
2273
2274 /* If there are glyphs whose x-offsets are negative,
2275 shift all glyphs to the right and make all x-offsets
2276 non-negative. */
2277 if (leftmost < 0)
2278 {
2279 for (i = 0; i < cmp->glyph_len; i++)
2280 cmp->offsets[i * 2] -= leftmost;
2281 rightmost -= leftmost;
2282 }
2283
2284 cmp->pixel_width = rightmost;
2285 cmp->ascent = highest;
2286 cmp->descent = - lowest;
2287 if (cmp->ascent < font_ascent)
2288 cmp->ascent = font_ascent;
2289 if (cmp->descent < font_descent)
2290 cmp->descent = font_descent;
2291 }
2292
2293 it->pixel_width = cmp->pixel_width;
2294 it->ascent = it->phys_ascent = cmp->ascent;
2295 it->descent = it->phys_descent = cmp->descent;
2296
2297 if (face->box != FACE_NO_BOX)
2298 {
2299 int thick = face->box_line_width;
2300
2301 if (thick > 0)
2302 {
2303 it->ascent += thick;
2304 it->descent += thick;
2305 }
2306 else
2307 thick = - thick;
2308
2309 if (it->start_of_box_run_p)
2310 it->pixel_width += thick;
2311 if (it->end_of_box_run_p)
2312 it->pixel_width += thick;
2313 }
2314
2315 /* If face has an overline, add the height of the overline
2316 (1 pixel) and a 1 pixel margin to the character height. */
2317 if (face->overline_p)
2318 it->ascent += 2;
2319
2320 take_vertical_position_into_account (it);
2321
2322 if (it->glyph_row)
2323 x_append_composite_glyph (it);
2324 }
2325 else if (it->what == IT_IMAGE)
2326 x_produce_image_glyph (it);
2327 else if (it->what == IT_STRETCH)
2328 x_produce_stretch_glyph (it);
2329
2330 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2331 because this isn't true for images with `:ascent 100'. */
2332 xassert (it->ascent >= 0 && it->descent >= 0);
2333 if (it->area == TEXT_AREA)
2334 it->current_x += it->pixel_width;
2335
2336 it->descent += it->extra_line_spacing;
2337
2338 it->max_ascent = max (it->max_ascent, it->ascent);
2339 it->max_descent = max (it->max_descent, it->descent);
2340 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2341 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2342 }
2343
2344
2345 /* Estimate the pixel height of the mode or top line on frame F.
2346 FACE_ID specifies what line's height to estimate. */
2347
2348 int
2349 x_estimate_mode_line_height (f, face_id)
2350 struct frame *f;
2351 enum face_id face_id;
2352 {
2353 int height = FONT_HEIGHT (FRAME_FONT (f));
2354
2355 /* This function is called so early when Emacs starts that the face
2356 cache and mode line face are not yet initialized. */
2357 if (FRAME_FACE_CACHE (f))
2358 {
2359 struct face *face = FACE_FROM_ID (f, face_id);
2360 if (face)
2361 {
2362 if (face->font)
2363 height = FONT_HEIGHT (face->font);
2364 if (face->box_line_width > 0)
2365 height += 2 * face->box_line_width;
2366 }
2367 }
2368
2369 return height;
2370 }
2371
2372 \f
2373 /***********************************************************************
2374 Glyph display
2375 ***********************************************************************/
2376
2377 /* A sequence of glyphs to be drawn in the same face.
2378
2379 This data structure is not really completely X specific, so it
2380 could possibly, at least partially, be useful for other systems. It
2381 is currently not part of the external redisplay interface because
2382 it's not clear what other systems will need. */
2383
2384 struct glyph_string
2385 {
2386 /* X-origin of the string. */
2387 int x;
2388
2389 /* Y-origin and y-position of the base line of this string. */
2390 int y, ybase;
2391
2392 /* The width of the string, not including a face extension. */
2393 int width;
2394
2395 /* The width of the string, including a face extension. */
2396 int background_width;
2397
2398 /* The height of this string. This is the height of the line this
2399 string is drawn in, and can be different from the height of the
2400 font the string is drawn in. */
2401 int height;
2402
2403 /* Number of pixels this string overwrites in front of its x-origin.
2404 This number is zero if the string has an lbearing >= 0; it is
2405 -lbearing, if the string has an lbearing < 0. */
2406 int left_overhang;
2407
2408 /* Number of pixels this string overwrites past its right-most
2409 nominal x-position, i.e. x + width. Zero if the string's
2410 rbearing is <= its nominal width, rbearing - width otherwise. */
2411 int right_overhang;
2412
2413 /* The frame on which the glyph string is drawn. */
2414 struct frame *f;
2415
2416 /* The window on which the glyph string is drawn. */
2417 struct window *w;
2418
2419 /* X display and window for convenience. */
2420 Display *display;
2421 Window window;
2422
2423 /* The glyph row for which this string was built. It determines the
2424 y-origin and height of the string. */
2425 struct glyph_row *row;
2426
2427 /* The area within row. */
2428 enum glyph_row_area area;
2429
2430 /* Characters to be drawn, and number of characters. */
2431 XChar2b *char2b;
2432 int nchars;
2433
2434 /* A face-override for drawing cursors, mouse face and similar. */
2435 enum draw_glyphs_face hl;
2436
2437 /* Face in which this string is to be drawn. */
2438 struct face *face;
2439
2440 /* Font in which this string is to be drawn. */
2441 XFontStruct *font;
2442
2443 /* Font info for this string. */
2444 struct font_info *font_info;
2445
2446 /* Non-null means this string describes (part of) a composition.
2447 All characters from char2b are drawn composed. */
2448 struct composition *cmp;
2449
2450 /* Index of this glyph string's first character in the glyph
2451 definition of CMP. If this is zero, this glyph string describes
2452 the first character of a composition. */
2453 int gidx;
2454
2455 /* 1 means this glyph strings face has to be drawn to the right end
2456 of the window's drawing area. */
2457 unsigned extends_to_end_of_line_p : 1;
2458
2459 /* 1 means the background of this string has been drawn. */
2460 unsigned background_filled_p : 1;
2461
2462 /* 1 means glyph string must be drawn with 16-bit functions. */
2463 unsigned two_byte_p : 1;
2464
2465 /* 1 means that the original font determined for drawing this glyph
2466 string could not be loaded. The member `font' has been set to
2467 the frame's default font in this case. */
2468 unsigned font_not_found_p : 1;
2469
2470 /* 1 means that the face in which this glyph string is drawn has a
2471 stipple pattern. */
2472 unsigned stippled_p : 1;
2473
2474 /* 1 means only the foreground of this glyph string must be drawn,
2475 and we should use the physical height of the line this glyph
2476 string appears in as clip rect. */
2477 unsigned for_overlaps_p : 1;
2478
2479 /* The GC to use for drawing this glyph string. */
2480 GC gc;
2481
2482 /* A pointer to the first glyph in the string. This glyph
2483 corresponds to char2b[0]. Needed to draw rectangles if
2484 font_not_found_p is 1. */
2485 struct glyph *first_glyph;
2486
2487 /* Image, if any. */
2488 struct image *img;
2489
2490 struct glyph_string *next, *prev;
2491 };
2492
2493
2494 #if GLYPH_DEBUG
2495
2496 static void
2497 x_dump_glyph_string (s)
2498 struct glyph_string *s;
2499 {
2500 fprintf (stderr, "glyph string\n");
2501 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2502 s->x, s->y, s->width, s->height);
2503 fprintf (stderr, " ybase = %d\n", s->ybase);
2504 fprintf (stderr, " hl = %d\n", s->hl);
2505 fprintf (stderr, " left overhang = %d, right = %d\n",
2506 s->left_overhang, s->right_overhang);
2507 fprintf (stderr, " nchars = %d\n", s->nchars);
2508 fprintf (stderr, " extends to end of line = %d\n",
2509 s->extends_to_end_of_line_p);
2510 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2511 fprintf (stderr, " bg width = %d\n", s->background_width);
2512 }
2513
2514 #endif /* GLYPH_DEBUG */
2515
2516
2517
2518 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2519 struct glyph_string **,
2520 struct glyph_string *,
2521 struct glyph_string *));
2522 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2523 struct glyph_string **,
2524 struct glyph_string *,
2525 struct glyph_string *));
2526 static void x_append_glyph_string P_ ((struct glyph_string **,
2527 struct glyph_string **,
2528 struct glyph_string *));
2529 static int x_left_overwritten P_ ((struct glyph_string *));
2530 static int x_left_overwriting P_ ((struct glyph_string *));
2531 static int x_right_overwritten P_ ((struct glyph_string *));
2532 static int x_right_overwriting P_ ((struct glyph_string *));
2533 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2534 int));
2535 static void x_init_glyph_string P_ ((struct glyph_string *,
2536 XChar2b *, struct window *,
2537 struct glyph_row *,
2538 enum glyph_row_area, int,
2539 enum draw_glyphs_face));
2540 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2541 enum glyph_row_area, int, int,
2542 enum draw_glyphs_face, int *, int *, int));
2543 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2544 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2545 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2546 int));
2547 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2548 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2549 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2550 static void x_draw_glyph_string P_ ((struct glyph_string *));
2551 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2552 static void x_set_cursor_gc P_ ((struct glyph_string *));
2553 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2554 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2555 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2556 int *, int *));
2557 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2558 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2559 unsigned long *, double, int));
2560 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2561 double, int, unsigned long));
2562 static void x_setup_relief_colors P_ ((struct glyph_string *));
2563 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2564 static void x_draw_image_relief P_ ((struct glyph_string *));
2565 static void x_draw_image_foreground P_ ((struct glyph_string *));
2566 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2567 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2568 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2569 int, int, int));
2570 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2571 int, int, int, int, XRectangle *));
2572 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2573 int, int, int, XRectangle *));
2574 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2575 enum glyph_row_area));
2576 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2577 struct glyph_row *,
2578 enum glyph_row_area, int, int));
2579
2580 #if GLYPH_DEBUG
2581 static void x_check_font P_ ((struct frame *, XFontStruct *));
2582 #endif
2583
2584
2585 /* Append the list of glyph strings with head H and tail T to the list
2586 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2587
2588 static INLINE void
2589 x_append_glyph_string_lists (head, tail, h, t)
2590 struct glyph_string **head, **tail;
2591 struct glyph_string *h, *t;
2592 {
2593 if (h)
2594 {
2595 if (*head)
2596 (*tail)->next = h;
2597 else
2598 *head = h;
2599 h->prev = *tail;
2600 *tail = t;
2601 }
2602 }
2603
2604
2605 /* Prepend the list of glyph strings with head H and tail T to the
2606 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2607 result. */
2608
2609 static INLINE void
2610 x_prepend_glyph_string_lists (head, tail, h, t)
2611 struct glyph_string **head, **tail;
2612 struct glyph_string *h, *t;
2613 {
2614 if (h)
2615 {
2616 if (*head)
2617 (*head)->prev = t;
2618 else
2619 *tail = t;
2620 t->next = *head;
2621 *head = h;
2622 }
2623 }
2624
2625
2626 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2627 Set *HEAD and *TAIL to the resulting list. */
2628
2629 static INLINE void
2630 x_append_glyph_string (head, tail, s)
2631 struct glyph_string **head, **tail;
2632 struct glyph_string *s;
2633 {
2634 s->next = s->prev = NULL;
2635 x_append_glyph_string_lists (head, tail, s, s);
2636 }
2637
2638
2639 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2640 face. */
2641
2642 static void
2643 x_set_cursor_gc (s)
2644 struct glyph_string *s;
2645 {
2646 if (s->font == FRAME_FONT (s->f)
2647 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2648 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2649 && !s->cmp)
2650 s->gc = s->f->output_data.x->cursor_gc;
2651 else
2652 {
2653 /* Cursor on non-default face: must merge. */
2654 XGCValues xgcv;
2655 unsigned long mask;
2656
2657 xgcv.background = s->f->output_data.x->cursor_pixel;
2658 xgcv.foreground = s->face->background;
2659
2660 /* If the glyph would be invisible, try a different foreground. */
2661 if (xgcv.foreground == xgcv.background)
2662 xgcv.foreground = s->face->foreground;
2663 if (xgcv.foreground == xgcv.background)
2664 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2665 if (xgcv.foreground == xgcv.background)
2666 xgcv.foreground = s->face->foreground;
2667
2668 /* Make sure the cursor is distinct from text in this face. */
2669 if (xgcv.background == s->face->background
2670 && xgcv.foreground == s->face->foreground)
2671 {
2672 xgcv.background = s->face->foreground;
2673 xgcv.foreground = s->face->background;
2674 }
2675
2676 IF_DEBUG (x_check_font (s->f, s->font));
2677 xgcv.font = s->font->fid;
2678 xgcv.graphics_exposures = False;
2679 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2680
2681 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2682 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2683 mask, &xgcv);
2684 else
2685 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2686 = XCreateGC (s->display, s->window, mask, &xgcv);
2687
2688 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2689 }
2690 }
2691
2692
2693 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2694
2695 static void
2696 x_set_mouse_face_gc (s)
2697 struct glyph_string *s;
2698 {
2699 int face_id;
2700 struct face *face;
2701
2702 /* What face has to be used last for the mouse face? */
2703 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2704 face = FACE_FROM_ID (s->f, face_id);
2705 if (face == NULL)
2706 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2707
2708 if (s->first_glyph->type == CHAR_GLYPH)
2709 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2710 else
2711 face_id = FACE_FOR_CHAR (s->f, face, 0);
2712 s->face = FACE_FROM_ID (s->f, face_id);
2713 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2714
2715 /* If font in this face is same as S->font, use it. */
2716 if (s->font == s->face->font)
2717 s->gc = s->face->gc;
2718 else
2719 {
2720 /* Otherwise construct scratch_cursor_gc with values from FACE
2721 but font FONT. */
2722 XGCValues xgcv;
2723 unsigned long mask;
2724
2725 xgcv.background = s->face->background;
2726 xgcv.foreground = s->face->foreground;
2727 IF_DEBUG (x_check_font (s->f, s->font));
2728 xgcv.font = s->font->fid;
2729 xgcv.graphics_exposures = False;
2730 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2731
2732 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2733 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2734 mask, &xgcv);
2735 else
2736 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2737 = XCreateGC (s->display, s->window, mask, &xgcv);
2738
2739 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2740 }
2741
2742 xassert (s->gc != 0);
2743 }
2744
2745
2746 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2747 Faces to use in the mode line have already been computed when the
2748 matrix was built, so there isn't much to do, here. */
2749
2750 static INLINE void
2751 x_set_mode_line_face_gc (s)
2752 struct glyph_string *s;
2753 {
2754 s->gc = s->face->gc;
2755 }
2756
2757
2758 /* Set S->gc of glyph string S for drawing that glyph string. Set
2759 S->stippled_p to a non-zero value if the face of S has a stipple
2760 pattern. */
2761
2762 static INLINE void
2763 x_set_glyph_string_gc (s)
2764 struct glyph_string *s;
2765 {
2766 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2767
2768 if (s->hl == DRAW_NORMAL_TEXT)
2769 {
2770 s->gc = s->face->gc;
2771 s->stippled_p = s->face->stipple != 0;
2772 }
2773 else if (s->hl == DRAW_INVERSE_VIDEO)
2774 {
2775 x_set_mode_line_face_gc (s);
2776 s->stippled_p = s->face->stipple != 0;
2777 }
2778 else if (s->hl == DRAW_CURSOR)
2779 {
2780 x_set_cursor_gc (s);
2781 s->stippled_p = 0;
2782 }
2783 else if (s->hl == DRAW_MOUSE_FACE)
2784 {
2785 x_set_mouse_face_gc (s);
2786 s->stippled_p = s->face->stipple != 0;
2787 }
2788 else if (s->hl == DRAW_IMAGE_RAISED
2789 || s->hl == DRAW_IMAGE_SUNKEN)
2790 {
2791 s->gc = s->face->gc;
2792 s->stippled_p = s->face->stipple != 0;
2793 }
2794 else
2795 {
2796 s->gc = s->face->gc;
2797 s->stippled_p = s->face->stipple != 0;
2798 }
2799
2800 /* GC must have been set. */
2801 xassert (s->gc != 0);
2802 }
2803
2804
2805 /* Return in *R the clipping rectangle for glyph string S. */
2806
2807 static void
2808 x_get_glyph_string_clip_rect (s, r)
2809 struct glyph_string *s;
2810 XRectangle *r;
2811 {
2812 if (s->row->full_width_p)
2813 {
2814 /* Draw full-width. X coordinates are relative to S->w->left. */
2815 int canon_x = CANON_X_UNIT (s->f);
2816
2817 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2818 r->width = XFASTINT (s->w->width) * canon_x;
2819
2820 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2821 {
2822 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2823 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2824 r->x -= width;
2825 }
2826
2827 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2828
2829 /* Unless displaying a mode or menu bar line, which are always
2830 fully visible, clip to the visible part of the row. */
2831 if (s->w->pseudo_window_p)
2832 r->height = s->row->visible_height;
2833 else
2834 r->height = s->height;
2835 }
2836 else
2837 {
2838 /* This is a text line that may be partially visible. */
2839 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2840 r->width = window_box_width (s->w, s->area);
2841 r->height = s->row->visible_height;
2842 }
2843
2844 /* If S draws overlapping rows, it's sufficient to use the top and
2845 bottom of the window for clipping because this glyph string
2846 intentionally draws over other lines. */
2847 if (s->for_overlaps_p)
2848 {
2849 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2850 r->height = window_text_bottom_y (s->w) - r->y;
2851 }
2852 else
2853 {
2854 /* Don't use S->y for clipping because it doesn't take partially
2855 visible lines into account. For example, it can be negative for
2856 partially visible lines at the top of a window. */
2857 if (!s->row->full_width_p
2858 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2859 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2860 else
2861 r->y = max (0, s->row->y);
2862
2863 /* If drawing a tool-bar window, draw it over the internal border
2864 at the top of the window. */
2865 if (s->w == XWINDOW (s->f->tool_bar_window))
2866 r->y -= s->f->output_data.x->internal_border_width;
2867 }
2868
2869 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2870 }
2871
2872
2873 /* Set clipping for output of glyph string S. S may be part of a mode
2874 line or menu if we don't have X toolkit support. */
2875
2876 static INLINE void
2877 x_set_glyph_string_clipping (s)
2878 struct glyph_string *s;
2879 {
2880 XRectangle r;
2881 x_get_glyph_string_clip_rect (s, &r);
2882 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2883 }
2884
2885
2886 /* Compute left and right overhang of glyph string S. If S is a glyph
2887 string for a composition, assume overhangs don't exist. */
2888
2889 static INLINE void
2890 x_compute_glyph_string_overhangs (s)
2891 struct glyph_string *s;
2892 {
2893 if (s->cmp == NULL
2894 && s->first_glyph->type == CHAR_GLYPH)
2895 {
2896 XCharStruct cs;
2897 int direction, font_ascent, font_descent;
2898 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2899 &font_ascent, &font_descent, &cs);
2900 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2901 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2902 }
2903 }
2904
2905
2906 /* Compute overhangs and x-positions for glyph string S and its
2907 predecessors, or successors. X is the starting x-position for S.
2908 BACKWARD_P non-zero means process predecessors. */
2909
2910 static void
2911 x_compute_overhangs_and_x (s, x, backward_p)
2912 struct glyph_string *s;
2913 int x;
2914 int backward_p;
2915 {
2916 if (backward_p)
2917 {
2918 while (s)
2919 {
2920 x_compute_glyph_string_overhangs (s);
2921 x -= s->width;
2922 s->x = x;
2923 s = s->prev;
2924 }
2925 }
2926 else
2927 {
2928 while (s)
2929 {
2930 x_compute_glyph_string_overhangs (s);
2931 s->x = x;
2932 x += s->width;
2933 s = s->next;
2934 }
2935 }
2936 }
2937
2938
2939 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2940 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2941 assumed to be zero. */
2942
2943 static void
2944 x_get_glyph_overhangs (glyph, f, left, right)
2945 struct glyph *glyph;
2946 struct frame *f;
2947 int *left, *right;
2948 {
2949 *left = *right = 0;
2950
2951 if (glyph->type == CHAR_GLYPH)
2952 {
2953 XFontStruct *font;
2954 struct face *face;
2955 struct font_info *font_info;
2956 XChar2b char2b;
2957 XCharStruct *pcm;
2958
2959 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2960 font = face->font;
2961 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2962 if (font
2963 && (pcm = x_per_char_metric (font, &char2b)))
2964 {
2965 if (pcm->rbearing > pcm->width)
2966 *right = pcm->rbearing - pcm->width;
2967 if (pcm->lbearing < 0)
2968 *left = -pcm->lbearing;
2969 }
2970 }
2971 }
2972
2973
2974 /* Return the index of the first glyph preceding glyph string S that
2975 is overwritten by S because of S's left overhang. Value is -1
2976 if no glyphs are overwritten. */
2977
2978 static int
2979 x_left_overwritten (s)
2980 struct glyph_string *s;
2981 {
2982 int k;
2983
2984 if (s->left_overhang)
2985 {
2986 int x = 0, i;
2987 struct glyph *glyphs = s->row->glyphs[s->area];
2988 int first = s->first_glyph - glyphs;
2989
2990 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2991 x -= glyphs[i].pixel_width;
2992
2993 k = i + 1;
2994 }
2995 else
2996 k = -1;
2997
2998 return k;
2999 }
3000
3001
3002 /* Return the index of the first glyph preceding glyph string S that
3003 is overwriting S because of its right overhang. Value is -1 if no
3004 glyph in front of S overwrites S. */
3005
3006 static int
3007 x_left_overwriting (s)
3008 struct glyph_string *s;
3009 {
3010 int i, k, x;
3011 struct glyph *glyphs = s->row->glyphs[s->area];
3012 int first = s->first_glyph - glyphs;
3013
3014 k = -1;
3015 x = 0;
3016 for (i = first - 1; i >= 0; --i)
3017 {
3018 int left, right;
3019 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3020 if (x + right > 0)
3021 k = i;
3022 x -= glyphs[i].pixel_width;
3023 }
3024
3025 return k;
3026 }
3027
3028
3029 /* Return the index of the last glyph following glyph string S that is
3030 not overwritten by S because of S's right overhang. Value is -1 if
3031 no such glyph is found. */
3032
3033 static int
3034 x_right_overwritten (s)
3035 struct glyph_string *s;
3036 {
3037 int k = -1;
3038
3039 if (s->right_overhang)
3040 {
3041 int x = 0, i;
3042 struct glyph *glyphs = s->row->glyphs[s->area];
3043 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3044 int end = s->row->used[s->area];
3045
3046 for (i = first; i < end && s->right_overhang > x; ++i)
3047 x += glyphs[i].pixel_width;
3048
3049 k = i;
3050 }
3051
3052 return k;
3053 }
3054
3055
3056 /* Return the index of the last glyph following glyph string S that
3057 overwrites S because of its left overhang. Value is negative
3058 if no such glyph is found. */
3059
3060 static int
3061 x_right_overwriting (s)
3062 struct glyph_string *s;
3063 {
3064 int i, k, x;
3065 int end = s->row->used[s->area];
3066 struct glyph *glyphs = s->row->glyphs[s->area];
3067 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3068
3069 k = -1;
3070 x = 0;
3071 for (i = first; i < end; ++i)
3072 {
3073 int left, right;
3074 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3075 if (x - left < 0)
3076 k = i;
3077 x += glyphs[i].pixel_width;
3078 }
3079
3080 return k;
3081 }
3082
3083
3084 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3085
3086 static INLINE void
3087 x_clear_glyph_string_rect (s, x, y, w, h)
3088 struct glyph_string *s;
3089 int x, y, w, h;
3090 {
3091 XGCValues xgcv;
3092 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3093 XSetForeground (s->display, s->gc, xgcv.background);
3094 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3095 XSetForeground (s->display, s->gc, xgcv.foreground);
3096 }
3097
3098
3099 /* Draw the background of glyph_string S. If S->background_filled_p
3100 is non-zero don't draw it. FORCE_P non-zero means draw the
3101 background even if it wouldn't be drawn normally. This is used
3102 when a string preceding S draws into the background of S, or S
3103 contains the first component of a composition. */
3104
3105 static void
3106 x_draw_glyph_string_background (s, force_p)
3107 struct glyph_string *s;
3108 int force_p;
3109 {
3110 /* Nothing to do if background has already been drawn or if it
3111 shouldn't be drawn in the first place. */
3112 if (!s->background_filled_p)
3113 {
3114 int box_line_width = max (s->face->box_line_width, 0);
3115
3116 if (s->stippled_p)
3117 {
3118 /* Fill background with a stipple pattern. */
3119 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3120 XFillRectangle (s->display, s->window, s->gc, s->x,
3121 s->y + box_line_width,
3122 s->background_width,
3123 s->height - 2 * box_line_width);
3124 XSetFillStyle (s->display, s->gc, FillSolid);
3125 s->background_filled_p = 1;
3126 }
3127 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
3128 || s->font_not_found_p
3129 || s->extends_to_end_of_line_p
3130 || force_p)
3131 {
3132 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
3133 s->background_width,
3134 s->height - 2 * box_line_width);
3135 s->background_filled_p = 1;
3136 }
3137 }
3138 }
3139
3140
3141 /* Draw the foreground of glyph string S. */
3142
3143 static void
3144 x_draw_glyph_string_foreground (s)
3145 struct glyph_string *s;
3146 {
3147 int i, x;
3148
3149 /* If first glyph of S has a left box line, start drawing the text
3150 of S to the right of that box line. */
3151 if (s->face->box != FACE_NO_BOX
3152 && s->first_glyph->left_box_line_p)
3153 x = s->x + abs (s->face->box_line_width);
3154 else
3155 x = s->x;
3156
3157 /* Draw characters of S as rectangles if S's font could not be
3158 loaded. */
3159 if (s->font_not_found_p)
3160 {
3161 for (i = 0; i < s->nchars; ++i)
3162 {
3163 struct glyph *g = s->first_glyph + i;
3164 XDrawRectangle (s->display, s->window,
3165 s->gc, x, s->y, g->pixel_width - 1,
3166 s->height - 1);
3167 x += g->pixel_width;
3168 }
3169 }
3170 else
3171 {
3172 char *char1b = (char *) s->char2b;
3173 int boff = s->font_info->baseline_offset;
3174
3175 if (s->font_info->vertical_centering)
3176 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3177
3178 /* If we can use 8-bit functions, condense S->char2b. */
3179 if (!s->two_byte_p)
3180 for (i = 0; i < s->nchars; ++i)
3181 char1b[i] = s->char2b[i].byte2;
3182
3183 /* Draw text with XDrawString if background has already been
3184 filled. Otherwise, use XDrawImageString. (Note that
3185 XDrawImageString is usually faster than XDrawString.) Always
3186 use XDrawImageString when drawing the cursor so that there is
3187 no chance that characters under a box cursor are invisible. */
3188 if (s->for_overlaps_p
3189 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3190 {
3191 /* Draw characters with 16-bit or 8-bit functions. */
3192 if (s->two_byte_p)
3193 XDrawString16 (s->display, s->window, s->gc, x,
3194 s->ybase - boff, s->char2b, s->nchars);
3195 else
3196 XDrawString (s->display, s->window, s->gc, x,
3197 s->ybase - boff, char1b, s->nchars);
3198 }
3199 else
3200 {
3201 if (s->two_byte_p)
3202 XDrawImageString16 (s->display, s->window, s->gc, x,
3203 s->ybase - boff, s->char2b, s->nchars);
3204 else
3205 XDrawImageString (s->display, s->window, s->gc, x,
3206 s->ybase - boff, char1b, s->nchars);
3207 }
3208 }
3209 }
3210
3211 /* Draw the foreground of composite glyph string S. */
3212
3213 static void
3214 x_draw_composite_glyph_string_foreground (s)
3215 struct glyph_string *s;
3216 {
3217 int i, x;
3218
3219 /* If first glyph of S has a left box line, start drawing the text
3220 of S to the right of that box line. */
3221 if (s->face->box != FACE_NO_BOX
3222 && s->first_glyph->left_box_line_p)
3223 x = s->x + abs (s->face->box_line_width);
3224 else
3225 x = s->x;
3226
3227 /* S is a glyph string for a composition. S->gidx is the index of
3228 the first character drawn for glyphs of this composition.
3229 S->gidx == 0 means we are drawing the very first character of
3230 this composition. */
3231
3232 /* Draw a rectangle for the composition if the font for the very
3233 first character of the composition could not be loaded. */
3234 if (s->font_not_found_p)
3235 {
3236 if (s->gidx == 0)
3237 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3238 s->width - 1, s->height - 1);
3239 }
3240 else
3241 {
3242 for (i = 0; i < s->nchars; i++, ++s->gidx)
3243 XDrawString16 (s->display, s->window, s->gc,
3244 x + s->cmp->offsets[s->gidx * 2],
3245 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3246 s->char2b + i, 1);
3247 }
3248 }
3249
3250
3251 #ifdef USE_X_TOOLKIT
3252
3253 static struct frame *x_frame_of_widget P_ ((Widget));
3254 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
3255 XrmValue *, XrmValue *, XtPointer *));
3256 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
3257 XrmValue *, Cardinal *));
3258
3259
3260 /* Return the frame on which widget WIDGET is used.. Abort if frame
3261 cannot be determined. */
3262
3263 static struct frame *
3264 x_frame_of_widget (widget)
3265 Widget widget;
3266 {
3267 struct x_display_info *dpyinfo;
3268 Lisp_Object tail;
3269 struct frame *f;
3270
3271 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3272
3273 /* Find the top-level shell of the widget. Note that this function
3274 can be called when the widget is not yet realized, so XtWindow
3275 (widget) == 0. That's the reason we can't simply use
3276 x_any_window_to_frame. */
3277 while (!XtIsTopLevelShell (widget))
3278 widget = XtParent (widget);
3279
3280 /* Look for a frame with that top-level widget. Allocate the color
3281 on that frame to get the right gamma correction value. */
3282 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3283 if (GC_FRAMEP (XCAR (tail))
3284 && (f = XFRAME (XCAR (tail)),
3285 (f->output_data.nothing != 1
3286 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3287 && f->output_data.x->widget == widget)
3288 return f;
3289
3290 abort ();
3291 }
3292
3293
3294 /* Allocate the color COLOR->pixel on the screen and display of
3295 widget WIDGET in colormap CMAP. If an exact match cannot be
3296 allocated, try the nearest color available. Value is non-zero
3297 if successful. This is called from lwlib. */
3298
3299 int
3300 x_alloc_nearest_color_for_widget (widget, cmap, color)
3301 Widget widget;
3302 Colormap cmap;
3303 XColor *color;
3304 {
3305 struct frame *f = x_frame_of_widget (widget);
3306 return x_alloc_nearest_color (f, cmap, color);
3307 }
3308
3309
3310 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3311 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3312 If this produces the same color as PIXEL, try a color where all RGB
3313 values have DELTA added. Return the allocated color in *PIXEL.
3314 DISPLAY is the X display, CMAP is the colormap to operate on.
3315 Value is non-zero if successful. */
3316
3317 int
3318 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3319 Widget widget;
3320 Display *display;
3321 Colormap cmap;
3322 unsigned long *pixel;
3323 double factor;
3324 int delta;
3325 {
3326 struct frame *f = x_frame_of_widget (widget);
3327 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3328 }
3329
3330
3331 /* Structure specifying which arguments should be passed by Xt to
3332 cvt_string_to_pixel. We want the widget's screen and colormap. */
3333
3334 static XtConvertArgRec cvt_string_to_pixel_args[] =
3335 {
3336 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
3337 sizeof (Screen *)},
3338 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
3339 sizeof (Colormap)}
3340 };
3341
3342
3343 /* The address of this variable is returned by
3344 cvt_string_to_pixel. */
3345
3346 static Pixel cvt_string_to_pixel_value;
3347
3348
3349 /* Convert a color name to a pixel color.
3350
3351 DPY is the display we are working on.
3352
3353 ARGS is an array of *NARGS XrmValue structures holding additional
3354 information about the widget for which the conversion takes place.
3355 The contents of this array are determined by the specification
3356 in cvt_string_to_pixel_args.
3357
3358 FROM is a pointer to an XrmValue which points to the color name to
3359 convert. TO is an XrmValue in which to return the pixel color.
3360
3361 CLOSURE_RET is a pointer to user-data, in which we record if
3362 we allocated the color or not.
3363
3364 Value is True if successful, False otherwise. */
3365
3366 static Boolean
3367 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
3368 Display *dpy;
3369 XrmValue *args;
3370 Cardinal *nargs;
3371 XrmValue *from, *to;
3372 XtPointer *closure_ret;
3373 {
3374 Screen *screen;
3375 Colormap cmap;
3376 Pixel pixel;
3377 String color_name;
3378 XColor color;
3379
3380 if (*nargs != 2)
3381 {
3382 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3383 "wrongParameters", "cvt_string_to_pixel",
3384 "XtToolkitError",
3385 "Screen and colormap args required", NULL, NULL);
3386 return False;
3387 }
3388
3389 screen = *(Screen **) args[0].addr;
3390 cmap = *(Colormap *) args[1].addr;
3391 color_name = (String) from->addr;
3392
3393 if (strcmp (color_name, XtDefaultBackground) == 0)
3394 {
3395 *closure_ret = (XtPointer) False;
3396 pixel = WhitePixelOfScreen (screen);
3397 }
3398 else if (strcmp (color_name, XtDefaultForeground) == 0)
3399 {
3400 *closure_ret = (XtPointer) False;
3401 pixel = BlackPixelOfScreen (screen);
3402 }
3403 else if (XParseColor (dpy, cmap, color_name, &color)
3404 && x_alloc_nearest_color_1 (dpy, cmap, &color))
3405 {
3406 pixel = color.pixel;
3407 *closure_ret = (XtPointer) True;
3408 }
3409 else
3410 {
3411 String params[1];
3412 Cardinal nparams = 1;
3413
3414 params[0] = color_name;
3415 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3416 "badValue", "cvt_string_to_pixel",
3417 "XtToolkitError", "Invalid color `%s'",
3418 params, &nparams);
3419 return False;
3420 }
3421
3422 if (to->addr != NULL)
3423 {
3424 if (to->size < sizeof (Pixel))
3425 {
3426 to->size = sizeof (Pixel);
3427 return False;
3428 }
3429
3430 *(Pixel *) to->addr = pixel;
3431 }
3432 else
3433 {
3434 cvt_string_to_pixel_value = pixel;
3435 to->addr = (XtPointer) &cvt_string_to_pixel_value;
3436 }
3437
3438 to->size = sizeof (Pixel);
3439 return True;
3440 }
3441
3442
3443 /* Free a pixel color which was previously allocated via
3444 cvt_string_to_pixel. This is registered as the destructor
3445 for this type of resource via XtSetTypeConverter.
3446
3447 APP is the application context in which we work.
3448
3449 TO is a pointer to an XrmValue holding the color to free.
3450 CLOSURE is the value we stored in CLOSURE_RET for this color
3451 in cvt_string_to_pixel.
3452
3453 ARGS and NARGS are like for cvt_string_to_pixel. */
3454
3455 static void
3456 cvt_pixel_dtor (app, to, closure, args, nargs)
3457 XtAppContext app;
3458 XrmValuePtr to;
3459 XtPointer closure;
3460 XrmValuePtr args;
3461 Cardinal *nargs;
3462 {
3463 if (*nargs != 2)
3464 {
3465 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
3466 "XtToolkitError",
3467 "Screen and colormap arguments required",
3468 NULL, NULL);
3469 }
3470 else if (closure != NULL)
3471 {
3472 /* We did allocate the pixel, so free it. */
3473 Screen *screen = *(Screen **) args[0].addr;
3474 Colormap cmap = *(Colormap *) args[1].addr;
3475 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
3476 (Pixel *) to->addr, 1);
3477 }
3478 }
3479
3480
3481 #endif /* USE_X_TOOLKIT */
3482
3483
3484 /* Value is an array of XColor structures for the contents of the
3485 color map of display DPY. Set *NCELLS to the size of the array.
3486 Note that this probably shouldn't be called for large color maps,
3487 say a 24-bit TrueColor map. */
3488
3489 static const XColor *
3490 x_color_cells (dpy, ncells)
3491 Display *dpy;
3492 int *ncells;
3493 {
3494 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3495
3496 if (dpyinfo->color_cells == NULL)
3497 {
3498 Screen *screen = dpyinfo->screen;
3499 int i;
3500
3501 dpyinfo->ncolor_cells
3502 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
3503 dpyinfo->color_cells
3504 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3505 * sizeof *dpyinfo->color_cells);
3506
3507 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3508 dpyinfo->color_cells[i].pixel = i;
3509
3510 XQueryColors (dpy, dpyinfo->cmap,
3511 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3512 }
3513
3514 *ncells = dpyinfo->ncolor_cells;
3515 return dpyinfo->color_cells;
3516 }
3517
3518
3519 /* On frame F, translate pixel colors to RGB values for the NCOLORS
3520 colors in COLORS. Use cached information, if available. */
3521
3522 void
3523 x_query_colors (f, colors, ncolors)
3524 struct frame *f;
3525 XColor *colors;
3526 int ncolors;
3527 {
3528 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3529
3530 if (dpyinfo->color_cells)
3531 {
3532 int i;
3533 for (i = 0; i < ncolors; ++i)
3534 {
3535 unsigned long pixel = colors[i].pixel;
3536 xassert (pixel < dpyinfo->ncolor_cells);
3537 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3538 colors[i] = dpyinfo->color_cells[pixel];
3539 }
3540 }
3541 else
3542 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3543 }
3544
3545
3546 /* On frame F, translate pixel color to RGB values for the color in
3547 COLOR. Use cached information, if available. */
3548
3549 void
3550 x_query_color (f, color)
3551 struct frame *f;
3552 XColor *color;
3553 {
3554 x_query_colors (f, color, 1);
3555 }
3556
3557
3558 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
3559 exact match can't be allocated, try the nearest color available.
3560 Value is non-zero if successful. Set *COLOR to the color
3561 allocated. */
3562
3563 static int
3564 x_alloc_nearest_color_1 (dpy, cmap, color)
3565 Display *dpy;
3566 Colormap cmap;
3567 XColor *color;
3568 {
3569 int rc;
3570
3571 rc = XAllocColor (dpy, cmap, color);
3572 if (rc == 0)
3573 {
3574 /* If we got to this point, the colormap is full, so we're going
3575 to try to get the next closest color. The algorithm used is
3576 a least-squares matching, which is what X uses for closest
3577 color matching with StaticColor visuals. */
3578 int nearest, i;
3579 unsigned long nearest_delta = ~0;
3580 int ncells;
3581 const XColor *cells = x_color_cells (dpy, &ncells);
3582
3583 for (nearest = i = 0; i < ncells; ++i)
3584 {
3585 long dred = (color->red >> 8) - (cells[i].red >> 8);
3586 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3587 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3588 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3589
3590 if (delta < nearest_delta)
3591 {
3592 nearest = i;
3593 nearest_delta = delta;
3594 }
3595 }
3596
3597 color->red = cells[nearest].red;
3598 color->green = cells[nearest].green;
3599 color->blue = cells[nearest].blue;
3600 rc = XAllocColor (dpy, cmap, color);
3601 }
3602 else
3603 {
3604 /* If allocation succeeded, and the allocated pixel color is not
3605 equal to a cached pixel color recorded earlier, there was a
3606 change in the colormap, so clear the color cache. */
3607 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3608 XColor *cached_color;
3609
3610 if (dpyinfo->color_cells
3611 && (cached_color = &dpyinfo->color_cells[color->pixel],
3612 (cached_color->red != color->red
3613 || cached_color->blue != color->blue
3614 || cached_color->green != color->green)))
3615 {
3616 xfree (dpyinfo->color_cells);
3617 dpyinfo->color_cells = NULL;
3618 dpyinfo->ncolor_cells = 0;
3619 }
3620 }
3621
3622 #ifdef DEBUG_X_COLORS
3623 if (rc)
3624 register_color (color->pixel);
3625 #endif /* DEBUG_X_COLORS */
3626
3627 return rc;
3628 }
3629
3630
3631 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
3632 exact match can't be allocated, try the nearest color available.
3633 Value is non-zero if successful. Set *COLOR to the color
3634 allocated. */
3635
3636 int
3637 x_alloc_nearest_color (f, cmap, color)
3638 struct frame *f;
3639 Colormap cmap;
3640 XColor *color;
3641 {
3642 gamma_correct (f, color);
3643 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
3644 }
3645
3646
3647 /* Allocate color PIXEL on frame F. 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_color (f, pixel)
3653 struct frame *f;
3654 unsigned long pixel;
3655 {
3656 XColor color;
3657
3658 color.pixel = pixel;
3659 BLOCK_INPUT;
3660 x_query_color (f, &color);
3661 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3662 UNBLOCK_INPUT;
3663 #ifdef DEBUG_X_COLORS
3664 register_color (pixel);
3665 #endif
3666 return color.pixel;
3667 }
3668
3669
3670 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3671 It's necessary to do this instead of just using PIXEL directly to
3672 get color reference counts right. */
3673
3674 unsigned long
3675 x_copy_dpy_color (dpy, cmap, pixel)
3676 Display *dpy;
3677 Colormap cmap;
3678 unsigned long pixel;
3679 {
3680 XColor color;
3681
3682 color.pixel = pixel;
3683 BLOCK_INPUT;
3684 XQueryColor (dpy, cmap, &color);
3685 XAllocColor (dpy, cmap, &color);
3686 UNBLOCK_INPUT;
3687 #ifdef DEBUG_X_COLORS
3688 register_color (pixel);
3689 #endif
3690 return color.pixel;
3691 }
3692
3693
3694 /* Brightness beyond which a color won't have its highlight brightness
3695 boosted.
3696
3697 Nominally, highlight colors for `3d' faces are calculated by
3698 brightening an object's color by a constant scale factor, but this
3699 doesn't yield good results for dark colors, so for colors who's
3700 brightness is less than this value (on a scale of 0-65535) have an
3701 use an additional additive factor.
3702
3703 The value here is set so that the default menu-bar/mode-line color
3704 (grey75) will not have its highlights changed at all. */
3705 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3706
3707
3708 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3709 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3710 If this produces the same color as PIXEL, try a color where all RGB
3711 values have DELTA added. Return the allocated color in *PIXEL.
3712 DISPLAY is the X display, CMAP is the colormap to operate on.
3713 Value is non-zero if successful. */
3714
3715 static int
3716 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3717 struct frame *f;
3718 Display *display;
3719 Colormap cmap;
3720 unsigned long *pixel;
3721 double factor;
3722 int delta;
3723 {
3724 XColor color, new;
3725 long bright;
3726 int success_p;
3727
3728 /* Get RGB color values. */
3729 color.pixel = *pixel;
3730 x_query_color (f, &color);
3731
3732 /* Change RGB values by specified FACTOR. Avoid overflow! */
3733 xassert (factor >= 0);
3734 new.red = min (0xffff, factor * color.red);
3735 new.green = min (0xffff, factor * color.green);
3736 new.blue = min (0xffff, factor * color.blue);
3737
3738 /* Calculate brightness of COLOR. */
3739 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
3740
3741 /* We only boost colors that are darker than
3742 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3743 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3744 /* Make an additive adjustment to NEW, because it's dark enough so
3745 that scaling by FACTOR alone isn't enough. */
3746 {
3747 /* How far below the limit this color is (0 - 1, 1 being darker). */
3748 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3749 /* The additive adjustment. */
3750 int min_delta = delta * dimness * factor / 2;
3751
3752 if (factor < 1)
3753 {
3754 new.red = max (0, new.red - min_delta);
3755 new.green = max (0, new.green - min_delta);
3756 new.blue = max (0, new.blue - min_delta);
3757 }
3758 else
3759 {
3760 new.red = min (0xffff, min_delta + new.red);
3761 new.green = min (0xffff, min_delta + new.green);
3762 new.blue = min (0xffff, min_delta + new.blue);
3763 }
3764 }
3765
3766 /* Try to allocate the color. */
3767 success_p = x_alloc_nearest_color (f, cmap, &new);
3768 if (success_p)
3769 {
3770 if (new.pixel == *pixel)
3771 {
3772 /* If we end up with the same color as before, try adding
3773 delta to the RGB values. */
3774 x_free_colors (f, &new.pixel, 1);
3775
3776 new.red = min (0xffff, delta + color.red);
3777 new.green = min (0xffff, delta + color.green);
3778 new.blue = min (0xffff, delta + color.blue);
3779 success_p = x_alloc_nearest_color (f, cmap, &new);
3780 }
3781 else
3782 success_p = 1;
3783 *pixel = new.pixel;
3784 }
3785
3786 return success_p;
3787 }
3788
3789
3790 /* Set up the foreground color for drawing relief lines of glyph
3791 string S. RELIEF is a pointer to a struct relief containing the GC
3792 with which lines will be drawn. Use a color that is FACTOR or
3793 DELTA lighter or darker than the relief's background which is found
3794 in S->f->output_data.x->relief_background. If such a color cannot
3795 be allocated, use DEFAULT_PIXEL, instead. */
3796
3797 static void
3798 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3799 struct frame *f;
3800 struct relief *relief;
3801 double factor;
3802 int delta;
3803 unsigned long default_pixel;
3804 {
3805 XGCValues xgcv;
3806 struct x_output *di = f->output_data.x;
3807 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3808 unsigned long pixel;
3809 unsigned long background = di->relief_background;
3810 Colormap cmap = FRAME_X_COLORMAP (f);
3811 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3812 Display *dpy = FRAME_X_DISPLAY (f);
3813
3814 xgcv.graphics_exposures = False;
3815 xgcv.line_width = 1;
3816
3817 /* Free previously allocated color. The color cell will be reused
3818 when it has been freed as many times as it was allocated, so this
3819 doesn't affect faces using the same colors. */
3820 if (relief->gc
3821 && relief->allocated_p)
3822 {
3823 x_free_colors (f, &relief->pixel, 1);
3824 relief->allocated_p = 0;
3825 }
3826
3827 /* Allocate new color. */
3828 xgcv.foreground = default_pixel;
3829 pixel = background;
3830 if (dpyinfo->n_planes != 1
3831 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3832 {
3833 relief->allocated_p = 1;
3834 xgcv.foreground = relief->pixel = pixel;
3835 }
3836
3837 if (relief->gc == 0)
3838 {
3839 xgcv.stipple = dpyinfo->gray;
3840 mask |= GCStipple;
3841 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3842 }
3843 else
3844 XChangeGC (dpy, relief->gc, mask, &xgcv);
3845 }
3846
3847
3848 /* Set up colors for the relief lines around glyph string S. */
3849
3850 static void
3851 x_setup_relief_colors (s)
3852 struct glyph_string *s;
3853 {
3854 struct x_output *di = s->f->output_data.x;
3855 unsigned long color;
3856
3857 if (s->face->use_box_color_for_shadows_p)
3858 color = s->face->box_color;
3859 else
3860 {
3861 XGCValues xgcv;
3862
3863 /* Get the background color of the face. */
3864 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3865 color = xgcv.background;
3866 }
3867
3868 if (di->white_relief.gc == 0
3869 || color != di->relief_background)
3870 {
3871 di->relief_background = color;
3872 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3873 WHITE_PIX_DEFAULT (s->f));
3874 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3875 BLACK_PIX_DEFAULT (s->f));
3876 }
3877 }
3878
3879
3880 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3881 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3882 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3883 relief. LEFT_P non-zero means draw a relief on the left side of
3884 the rectangle. RIGHT_P non-zero means draw a relief on the right
3885 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3886 when drawing. */
3887
3888 static void
3889 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3890 raised_p, left_p, right_p, clip_rect)
3891 struct frame *f;
3892 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3893 XRectangle *clip_rect;
3894 {
3895 int i;
3896 GC gc;
3897
3898 if (raised_p)
3899 gc = f->output_data.x->white_relief.gc;
3900 else
3901 gc = f->output_data.x->black_relief.gc;
3902 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3903
3904 /* Top. */
3905 for (i = 0; i < width; ++i)
3906 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3907 left_x + i * left_p, top_y + i,
3908 right_x + 1 - i * right_p, top_y + i);
3909
3910 /* Left. */
3911 if (left_p)
3912 for (i = 0; i < width; ++i)
3913 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3914 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
3915
3916 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3917 if (raised_p)
3918 gc = f->output_data.x->black_relief.gc;
3919 else
3920 gc = f->output_data.x->white_relief.gc;
3921 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3922
3923 /* Bottom. */
3924 for (i = 0; i < width; ++i)
3925 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3926 left_x + i * left_p + 1, bottom_y - i,
3927 right_x + 1 - i * right_p, bottom_y - i);
3928
3929 /* Right. */
3930 if (right_p)
3931 for (i = 0; i < width; ++i)
3932 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3933 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3934
3935 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3936 }
3937
3938
3939 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3940 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3941 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3942 left side of the rectangle. RIGHT_P non-zero means draw a line
3943 on the right side of the rectangle. CLIP_RECT is the clipping
3944 rectangle to use when drawing. */
3945
3946 static void
3947 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3948 left_p, right_p, clip_rect)
3949 struct glyph_string *s;
3950 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3951 XRectangle *clip_rect;
3952 {
3953 XGCValues xgcv;
3954
3955 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3956 XSetForeground (s->display, s->gc, s->face->box_color);
3957 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3958
3959 /* Top. */
3960 XFillRectangle (s->display, s->window, s->gc,
3961 left_x, top_y, right_x - left_x + 1, width);
3962
3963 /* Left. */
3964 if (left_p)
3965 XFillRectangle (s->display, s->window, s->gc,
3966 left_x, top_y, width, bottom_y - top_y + 1);
3967
3968 /* Bottom. */
3969 XFillRectangle (s->display, s->window, s->gc,
3970 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3971
3972 /* Right. */
3973 if (right_p)
3974 XFillRectangle (s->display, s->window, s->gc,
3975 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3976
3977 XSetForeground (s->display, s->gc, xgcv.foreground);
3978 XSetClipMask (s->display, s->gc, None);
3979 }
3980
3981
3982 /* Draw a box around glyph string S. */
3983
3984 static void
3985 x_draw_glyph_string_box (s)
3986 struct glyph_string *s;
3987 {
3988 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3989 int left_p, right_p;
3990 struct glyph *last_glyph;
3991 XRectangle clip_rect;
3992
3993 last_x = window_box_right (s->w, s->area);
3994 if (s->row->full_width_p
3995 && !s->w->pseudo_window_p)
3996 {
3997 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3998 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3999 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
4000 }
4001
4002 /* The glyph that may have a right box line. */
4003 last_glyph = (s->cmp || s->img
4004 ? s->first_glyph
4005 : s->first_glyph + s->nchars - 1);
4006
4007 width = abs (s->face->box_line_width);
4008 raised_p = s->face->box == FACE_RAISED_BOX;
4009 left_x = s->x;
4010 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
4011 ? last_x - 1
4012 : min (last_x, s->x + s->background_width) - 1);
4013 top_y = s->y;
4014 bottom_y = top_y + s->height - 1;
4015
4016 left_p = (s->first_glyph->left_box_line_p
4017 || (s->hl == DRAW_MOUSE_FACE
4018 && (s->prev == NULL
4019 || s->prev->hl != s->hl)));
4020 right_p = (last_glyph->right_box_line_p
4021 || (s->hl == DRAW_MOUSE_FACE
4022 && (s->next == NULL
4023 || s->next->hl != s->hl)));
4024
4025 x_get_glyph_string_clip_rect (s, &clip_rect);
4026
4027 if (s->face->box == FACE_SIMPLE_BOX)
4028 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
4029 left_p, right_p, &clip_rect);
4030 else
4031 {
4032 x_setup_relief_colors (s);
4033 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
4034 width, raised_p, left_p, right_p, &clip_rect);
4035 }
4036 }
4037
4038
4039 /* Draw foreground of image glyph string S. */
4040
4041 static void
4042 x_draw_image_foreground (s)
4043 struct glyph_string *s;
4044 {
4045 int x;
4046 int y = s->ybase - image_ascent (s->img, s->face);
4047
4048 /* If first glyph of S has a left box line, start drawing it to the
4049 right of that line. */
4050 if (s->face->box != FACE_NO_BOX
4051 && s->first_glyph->left_box_line_p)
4052 x = s->x + abs (s->face->box_line_width);
4053 else
4054 x = s->x;
4055
4056 /* If there is a margin around the image, adjust x- and y-position
4057 by that margin. */
4058 x += s->img->hmargin;
4059 y += s->img->vmargin;
4060
4061 if (s->img->pixmap)
4062 {
4063 if (s->img->mask)
4064 {
4065 /* We can't set both a clip mask and use XSetClipRectangles
4066 because the latter also sets a clip mask. We also can't
4067 trust on the shape extension to be available
4068 (XShapeCombineRegion). So, compute the rectangle to draw
4069 manually. */
4070 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4071 | GCFunction);
4072 XGCValues xgcv;
4073 XRectangle clip_rect, image_rect, r;
4074
4075 xgcv.clip_mask = s->img->mask;
4076 xgcv.clip_x_origin = x;
4077 xgcv.clip_y_origin = y;
4078 xgcv.function = GXcopy;
4079 XChangeGC (s->display, s->gc, mask, &xgcv);
4080
4081 x_get_glyph_string_clip_rect (s, &clip_rect);
4082 image_rect.x = x;
4083 image_rect.y = y;
4084 image_rect.width = s->img->width;
4085 image_rect.height = s->img->height;
4086 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4087 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4088 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4089 }
4090 else
4091 {
4092 unsigned long mask = GCClipXOrigin | GCClipYOrigin | GCFunction;
4093 XGCValues xgcv;
4094 XRectangle clip_rect, image_rect, r;
4095
4096 x_get_glyph_string_clip_rect (s, &clip_rect);
4097 image_rect.x = x;
4098 image_rect.y = y;
4099 image_rect.width = s->img->width;
4100 image_rect.height = s->img->height;
4101 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4102 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4103 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4104
4105 /* When the image has a mask, we can expect that at
4106 least part of a mouse highlight or a block cursor will
4107 be visible. If the image doesn't have a mask, make
4108 a block cursor visible by drawing a rectangle around
4109 the image. I believe it's looking better if we do
4110 nothing here for mouse-face. */
4111 if (s->hl == DRAW_CURSOR)
4112 XDrawRectangle (s->display, s->window, s->gc, x, y,
4113 s->img->width - 1, s->img->height - 1);
4114 }
4115 }
4116 else
4117 /* Draw a rectangle if image could not be loaded. */
4118 XDrawRectangle (s->display, s->window, s->gc, x, y,
4119 s->img->width - 1, s->img->height - 1);
4120 }
4121
4122
4123 /* Draw a relief around the image glyph string S. */
4124
4125 static void
4126 x_draw_image_relief (s)
4127 struct glyph_string *s;
4128 {
4129 int x0, y0, x1, y1, thick, raised_p;
4130 XRectangle r;
4131 int x;
4132 int y = s->ybase - image_ascent (s->img, s->face);
4133
4134 /* If first glyph of S has a left box line, start drawing it to the
4135 right of that line. */
4136 if (s->face->box != FACE_NO_BOX
4137 && s->first_glyph->left_box_line_p)
4138 x = s->x + abs (s->face->box_line_width);
4139 else
4140 x = s->x;
4141
4142 /* If there is a margin around the image, adjust x- and y-position
4143 by that margin. */
4144 x += s->img->hmargin;
4145 y += s->img->vmargin;
4146
4147 if (s->hl == DRAW_IMAGE_SUNKEN
4148 || s->hl == DRAW_IMAGE_RAISED)
4149 {
4150 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
4151 raised_p = s->hl == DRAW_IMAGE_RAISED;
4152 }
4153 else
4154 {
4155 thick = abs (s->img->relief);
4156 raised_p = s->img->relief > 0;
4157 }
4158
4159 x0 = x - thick;
4160 y0 = y - thick;
4161 x1 = x + s->img->width + thick - 1;
4162 y1 = y + s->img->height + thick - 1;
4163
4164 x_setup_relief_colors (s);
4165 x_get_glyph_string_clip_rect (s, &r);
4166 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
4167 }
4168
4169
4170 /* Draw the foreground of image glyph string S to PIXMAP. */
4171
4172 static void
4173 x_draw_image_foreground_1 (s, pixmap)
4174 struct glyph_string *s;
4175 Pixmap pixmap;
4176 {
4177 int x;
4178 int y = s->ybase - s->y - image_ascent (s->img, s->face);
4179
4180 /* If first glyph of S has a left box line, start drawing it to the
4181 right of that line. */
4182 if (s->face->box != FACE_NO_BOX
4183 && s->first_glyph->left_box_line_p)
4184 x = abs (s->face->box_line_width);
4185 else
4186 x = 0;
4187
4188 /* If there is a margin around the image, adjust x- and y-position
4189 by that margin. */
4190 x += s->img->hmargin;
4191 y += s->img->vmargin;
4192
4193 if (s->img->pixmap)
4194 {
4195 if (s->img->mask)
4196 {
4197 /* We can't set both a clip mask and use XSetClipRectangles
4198 because the latter also sets a clip mask. We also can't
4199 trust on the shape extension to be available
4200 (XShapeCombineRegion). So, compute the rectangle to draw
4201 manually. */
4202 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4203 | GCFunction);
4204 XGCValues xgcv;
4205
4206 xgcv.clip_mask = s->img->mask;
4207 xgcv.clip_x_origin = x;
4208 xgcv.clip_y_origin = y;
4209 xgcv.function = GXcopy;
4210 XChangeGC (s->display, s->gc, mask, &xgcv);
4211
4212 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4213 0, 0, s->img->width, s->img->height, x, y);
4214 XSetClipMask (s->display, s->gc, None);
4215 }
4216 else
4217 {
4218 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4219 0, 0, s->img->width, s->img->height, x, y);
4220
4221 /* When the image has a mask, we can expect that at
4222 least part of a mouse highlight or a block cursor will
4223 be visible. If the image doesn't have a mask, make
4224 a block cursor visible by drawing a rectangle around
4225 the image. I believe it's looking better if we do
4226 nothing here for mouse-face. */
4227 if (s->hl == DRAW_CURSOR)
4228 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4229 s->img->width - 1, s->img->height - 1);
4230 }
4231 }
4232 else
4233 /* Draw a rectangle if image could not be loaded. */
4234 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4235 s->img->width - 1, s->img->height - 1);
4236 }
4237
4238
4239 /* Draw part of the background of glyph string S. X, Y, W, and H
4240 give the rectangle to draw. */
4241
4242 static void
4243 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4244 struct glyph_string *s;
4245 int x, y, w, h;
4246 {
4247 if (s->stippled_p)
4248 {
4249 /* Fill background with a stipple pattern. */
4250 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4251 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4252 XSetFillStyle (s->display, s->gc, FillSolid);
4253 }
4254 else
4255 x_clear_glyph_string_rect (s, x, y, w, h);
4256 }
4257
4258
4259 /* Draw image glyph string S.
4260
4261 s->y
4262 s->x +-------------------------
4263 | s->face->box
4264 |
4265 | +-------------------------
4266 | | s->img->margin
4267 | |
4268 | | +-------------------
4269 | | | the image
4270
4271 */
4272
4273 static void
4274 x_draw_image_glyph_string (s)
4275 struct glyph_string *s;
4276 {
4277 int x, y;
4278 int box_line_hwidth = abs (s->face->box_line_width);
4279 int box_line_vwidth = max (s->face->box_line_width, 0);
4280 int height;
4281 Pixmap pixmap = None;
4282
4283 height = s->height - 2 * box_line_vwidth;
4284
4285 /* Fill background with face under the image. Do it only if row is
4286 taller than image or if image has a clip mask to reduce
4287 flickering. */
4288 s->stippled_p = s->face->stipple != 0;
4289 if (height > s->img->height
4290 || s->img->hmargin
4291 || s->img->vmargin
4292 || s->img->mask
4293 || s->img->pixmap == 0
4294 || s->width != s->background_width)
4295 {
4296 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4297 x = s->x + box_line_hwidth;
4298 else
4299 x = s->x;
4300
4301 y = s->y + box_line_vwidth;
4302
4303 if (s->img->mask)
4304 {
4305 /* Create a pixmap as large as the glyph string. Fill it
4306 with the background color. Copy the image to it, using
4307 its mask. Copy the temporary pixmap to the display. */
4308 Screen *screen = FRAME_X_SCREEN (s->f);
4309 int depth = DefaultDepthOfScreen (screen);
4310
4311 /* Create a pixmap as large as the glyph string. */
4312 pixmap = XCreatePixmap (s->display, s->window,
4313 s->background_width,
4314 s->height, depth);
4315
4316 /* Don't clip in the following because we're working on the
4317 pixmap. */
4318 XSetClipMask (s->display, s->gc, None);
4319
4320 /* Fill the pixmap with the background color/stipple. */
4321 if (s->stippled_p)
4322 {
4323 /* Fill background with a stipple pattern. */
4324 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4325 XFillRectangle (s->display, pixmap, s->gc,
4326 0, 0, s->background_width, s->height);
4327 XSetFillStyle (s->display, s->gc, FillSolid);
4328 }
4329 else
4330 {
4331 XGCValues xgcv;
4332 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4333 &xgcv);
4334 XSetForeground (s->display, s->gc, xgcv.background);
4335 XFillRectangle (s->display, pixmap, s->gc,
4336 0, 0, s->background_width, s->height);
4337 XSetForeground (s->display, s->gc, xgcv.foreground);
4338 }
4339 }
4340 else
4341 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4342
4343 s->background_filled_p = 1;
4344 }
4345
4346 /* Draw the foreground. */
4347 if (pixmap != None)
4348 {
4349 x_draw_image_foreground_1 (s, pixmap);
4350 x_set_glyph_string_clipping (s);
4351 XCopyArea (s->display, pixmap, s->window, s->gc,
4352 0, 0, s->background_width, s->height, s->x, s->y);
4353 XFreePixmap (s->display, pixmap);
4354 }
4355 else
4356 x_draw_image_foreground (s);
4357
4358 /* If we must draw a relief around the image, do it. */
4359 if (s->img->relief
4360 || s->hl == DRAW_IMAGE_RAISED
4361 || s->hl == DRAW_IMAGE_SUNKEN)
4362 x_draw_image_relief (s);
4363 }
4364
4365
4366 /* Draw stretch glyph string S. */
4367
4368 static void
4369 x_draw_stretch_glyph_string (s)
4370 struct glyph_string *s;
4371 {
4372 xassert (s->first_glyph->type == STRETCH_GLYPH);
4373 s->stippled_p = s->face->stipple != 0;
4374
4375 if (s->hl == DRAW_CURSOR
4376 && !x_stretch_cursor_p)
4377 {
4378 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4379 as wide as the stretch glyph. */
4380 int width = min (CANON_X_UNIT (s->f), s->background_width);
4381
4382 /* Draw cursor. */
4383 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4384
4385 /* Clear rest using the GC of the original non-cursor face. */
4386 if (width < s->background_width)
4387 {
4388 int x = s->x + width, y = s->y;
4389 int w = s->background_width - width, h = s->height;
4390 XRectangle r;
4391 GC gc;
4392
4393 if (s->row->mouse_face_p
4394 && cursor_in_mouse_face_p (s->w))
4395 {
4396 x_set_mouse_face_gc (s);
4397 gc = s->gc;
4398 }
4399 else
4400 gc = s->face->gc;
4401
4402 x_get_glyph_string_clip_rect (s, &r);
4403 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4404
4405 if (s->face->stipple)
4406 {
4407 /* Fill background with a stipple pattern. */
4408 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4409 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4410 XSetFillStyle (s->display, gc, FillSolid);
4411 }
4412 else
4413 {
4414 XGCValues xgcv;
4415 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4416 XSetForeground (s->display, gc, xgcv.background);
4417 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4418 XSetForeground (s->display, gc, xgcv.foreground);
4419 }
4420 }
4421 }
4422 else if (!s->background_filled_p)
4423 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4424 s->height);
4425
4426 s->background_filled_p = 1;
4427 }
4428
4429
4430 /* Draw glyph string S. */
4431
4432 static void
4433 x_draw_glyph_string (s)
4434 struct glyph_string *s;
4435 {
4436 int relief_drawn_p = 0;
4437
4438 /* If S draws into the background of its successor, draw the
4439 background of the successor first so that S can draw into it.
4440 This makes S->next use XDrawString instead of XDrawImageString. */
4441 if (s->next && s->right_overhang && !s->for_overlaps_p)
4442 {
4443 xassert (s->next->img == NULL);
4444 x_set_glyph_string_gc (s->next);
4445 x_set_glyph_string_clipping (s->next);
4446 x_draw_glyph_string_background (s->next, 1);
4447 }
4448
4449 /* Set up S->gc, set clipping and draw S. */
4450 x_set_glyph_string_gc (s);
4451 x_set_glyph_string_clipping (s);
4452
4453 /* Draw relief (if any) in advance for char/composition so that the
4454 glyph string can be drawn over it. */
4455 if (!s->for_overlaps_p
4456 && s->face->box != FACE_NO_BOX
4457 && (s->first_glyph->type == CHAR_GLYPH
4458 || s->first_glyph->type == COMPOSITE_GLYPH))
4459
4460 {
4461 x_draw_glyph_string_background (s, 1);
4462 x_draw_glyph_string_box (s);
4463 relief_drawn_p = 1;
4464 }
4465
4466 switch (s->first_glyph->type)
4467 {
4468 case IMAGE_GLYPH:
4469 x_draw_image_glyph_string (s);
4470 break;
4471
4472 case STRETCH_GLYPH:
4473 x_draw_stretch_glyph_string (s);
4474 break;
4475
4476 case CHAR_GLYPH:
4477 if (s->for_overlaps_p)
4478 s->background_filled_p = 1;
4479 else
4480 x_draw_glyph_string_background (s, 0);
4481 x_draw_glyph_string_foreground (s);
4482 break;
4483
4484 case COMPOSITE_GLYPH:
4485 if (s->for_overlaps_p || s->gidx > 0)
4486 s->background_filled_p = 1;
4487 else
4488 x_draw_glyph_string_background (s, 1);
4489 x_draw_composite_glyph_string_foreground (s);
4490 break;
4491
4492 default:
4493 abort ();
4494 }
4495
4496 if (!s->for_overlaps_p)
4497 {
4498 /* Draw underline. */
4499 if (s->face->underline_p)
4500 {
4501 unsigned long tem, h;
4502 int y;
4503
4504 /* Get the underline thickness. Default is 1 pixel. */
4505 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4506 h = 1;
4507
4508 /* Get the underline position. This is the recommended
4509 vertical offset in pixels from the baseline to the top of
4510 the underline. This is a signed value according to the
4511 specs, and its default is
4512
4513 ROUND ((maximum descent) / 2), with
4514 ROUND(x) = floor (x + 0.5) */
4515
4516 if (x_use_underline_position_properties
4517 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4518 y = s->ybase + (long) tem;
4519 else if (s->face->font)
4520 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4521 else
4522 y = s->y + s->height - h;
4523
4524 if (s->face->underline_defaulted_p)
4525 XFillRectangle (s->display, s->window, s->gc,
4526 s->x, y, s->width, h);
4527 else
4528 {
4529 XGCValues xgcv;
4530 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4531 XSetForeground (s->display, s->gc, s->face->underline_color);
4532 XFillRectangle (s->display, s->window, s->gc,
4533 s->x, y, s->width, h);
4534 XSetForeground (s->display, s->gc, xgcv.foreground);
4535 }
4536 }
4537
4538 /* Draw overline. */
4539 if (s->face->overline_p)
4540 {
4541 unsigned long dy = 0, h = 1;
4542
4543 if (s->face->overline_color_defaulted_p)
4544 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4545 s->width, h);
4546 else
4547 {
4548 XGCValues xgcv;
4549 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4550 XSetForeground (s->display, s->gc, s->face->overline_color);
4551 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4552 s->width, h);
4553 XSetForeground (s->display, s->gc, xgcv.foreground);
4554 }
4555 }
4556
4557 /* Draw strike-through. */
4558 if (s->face->strike_through_p)
4559 {
4560 unsigned long h = 1;
4561 unsigned long dy = (s->height - h) / 2;
4562
4563 if (s->face->strike_through_color_defaulted_p)
4564 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4565 s->width, h);
4566 else
4567 {
4568 XGCValues xgcv;
4569 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4570 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4571 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4572 s->width, h);
4573 XSetForeground (s->display, s->gc, xgcv.foreground);
4574 }
4575 }
4576
4577 /* Draw relief if not yet drawn. */
4578 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
4579 x_draw_glyph_string_box (s);
4580 }
4581
4582 /* Reset clipping. */
4583 XSetClipMask (s->display, s->gc, None);
4584 }
4585
4586
4587 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4588 struct face **, int));
4589
4590
4591 /* Fill glyph string S with composition components specified by S->cmp.
4592
4593 FACES is an array of faces for all components of this composition.
4594 S->gidx is the index of the first component for S.
4595 OVERLAPS_P non-zero means S should draw the foreground only, and
4596 use its physical height for clipping.
4597
4598 Value is the index of a component not in S. */
4599
4600 static int
4601 x_fill_composite_glyph_string (s, faces, overlaps_p)
4602 struct glyph_string *s;
4603 struct face **faces;
4604 int overlaps_p;
4605 {
4606 int i;
4607
4608 xassert (s);
4609
4610 s->for_overlaps_p = overlaps_p;
4611
4612 s->face = faces[s->gidx];
4613 s->font = s->face->font;
4614 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4615
4616 /* For all glyphs of this composition, starting at the offset
4617 S->gidx, until we reach the end of the definition or encounter a
4618 glyph that requires the different face, add it to S. */
4619 ++s->nchars;
4620 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4621 ++s->nchars;
4622
4623 /* All glyph strings for the same composition has the same width,
4624 i.e. the width set for the first component of the composition. */
4625
4626 s->width = s->first_glyph->pixel_width;
4627
4628 /* If the specified font could not be loaded, use the frame's
4629 default font, but record the fact that we couldn't load it in
4630 the glyph string so that we can draw rectangles for the
4631 characters of the glyph string. */
4632 if (s->font == NULL)
4633 {
4634 s->font_not_found_p = 1;
4635 s->font = FRAME_FONT (s->f);
4636 }
4637
4638 /* Adjust base line for subscript/superscript text. */
4639 s->ybase += s->first_glyph->voffset;
4640
4641 xassert (s->face && s->face->gc);
4642
4643 /* This glyph string must always be drawn with 16-bit functions. */
4644 s->two_byte_p = 1;
4645
4646 return s->gidx + s->nchars;
4647 }
4648
4649
4650 /* Fill glyph string S from a sequence of character glyphs.
4651
4652 FACE_ID is the face id of the string. START is the index of the
4653 first glyph to consider, END is the index of the last + 1.
4654 OVERLAPS_P non-zero means S should draw the foreground only, and
4655 use its physical height for clipping.
4656
4657 Value is the index of the first glyph not in S. */
4658
4659 static int
4660 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4661 struct glyph_string *s;
4662 int face_id;
4663 int start, end, overlaps_p;
4664 {
4665 struct glyph *glyph, *last;
4666 int voffset;
4667 int glyph_not_available_p;
4668
4669 xassert (s->f == XFRAME (s->w->frame));
4670 xassert (s->nchars == 0);
4671 xassert (start >= 0 && end > start);
4672
4673 s->for_overlaps_p = overlaps_p,
4674 glyph = s->row->glyphs[s->area] + start;
4675 last = s->row->glyphs[s->area] + end;
4676 voffset = glyph->voffset;
4677
4678 glyph_not_available_p = glyph->glyph_not_available_p;
4679
4680 while (glyph < last
4681 && glyph->type == CHAR_GLYPH
4682 && glyph->voffset == voffset
4683 /* Same face id implies same font, nowadays. */
4684 && glyph->face_id == face_id
4685 && glyph->glyph_not_available_p == glyph_not_available_p)
4686 {
4687 int two_byte_p;
4688
4689 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4690 s->char2b + s->nchars,
4691 &two_byte_p);
4692 s->two_byte_p = two_byte_p;
4693 ++s->nchars;
4694 xassert (s->nchars <= end - start);
4695 s->width += glyph->pixel_width;
4696 ++glyph;
4697 }
4698
4699 s->font = s->face->font;
4700 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4701
4702 /* If the specified font could not be loaded, use the frame's font,
4703 but record the fact that we couldn't load it in
4704 S->font_not_found_p so that we can draw rectangles for the
4705 characters of the glyph string. */
4706 if (s->font == NULL || glyph_not_available_p)
4707 {
4708 s->font_not_found_p = 1;
4709 s->font = FRAME_FONT (s->f);
4710 }
4711
4712 /* Adjust base line for subscript/superscript text. */
4713 s->ybase += voffset;
4714
4715 xassert (s->face && s->face->gc);
4716 return glyph - s->row->glyphs[s->area];
4717 }
4718
4719
4720 /* Fill glyph string S from image glyph S->first_glyph. */
4721
4722 static void
4723 x_fill_image_glyph_string (s)
4724 struct glyph_string *s;
4725 {
4726 xassert (s->first_glyph->type == IMAGE_GLYPH);
4727 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4728 xassert (s->img);
4729 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4730 s->font = s->face->font;
4731 s->width = s->first_glyph->pixel_width;
4732
4733 /* Adjust base line for subscript/superscript text. */
4734 s->ybase += s->first_glyph->voffset;
4735 }
4736
4737
4738 /* Fill glyph string S from a sequence of stretch glyphs.
4739
4740 ROW is the glyph row in which the glyphs are found, AREA is the
4741 area within the row. START is the index of the first glyph to
4742 consider, END is the index of the last + 1.
4743
4744 Value is the index of the first glyph not in S. */
4745
4746 static int
4747 x_fill_stretch_glyph_string (s, row, area, start, end)
4748 struct glyph_string *s;
4749 struct glyph_row *row;
4750 enum glyph_row_area area;
4751 int start, end;
4752 {
4753 struct glyph *glyph, *last;
4754 int voffset, face_id;
4755
4756 xassert (s->first_glyph->type == STRETCH_GLYPH);
4757
4758 glyph = s->row->glyphs[s->area] + start;
4759 last = s->row->glyphs[s->area] + end;
4760 face_id = glyph->face_id;
4761 s->face = FACE_FROM_ID (s->f, face_id);
4762 s->font = s->face->font;
4763 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4764 s->width = glyph->pixel_width;
4765 voffset = glyph->voffset;
4766
4767 for (++glyph;
4768 (glyph < last
4769 && glyph->type == STRETCH_GLYPH
4770 && glyph->voffset == voffset
4771 && glyph->face_id == face_id);
4772 ++glyph)
4773 s->width += glyph->pixel_width;
4774
4775 /* Adjust base line for subscript/superscript text. */
4776 s->ybase += voffset;
4777
4778 /* The case that face->gc == 0 is handled when drawing the glyph
4779 string by calling PREPARE_FACE_FOR_DISPLAY. */
4780 xassert (s->face);
4781 return glyph - s->row->glyphs[s->area];
4782 }
4783
4784
4785 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4786 of XChar2b structures for S; it can't be allocated in
4787 x_init_glyph_string because it must be allocated via `alloca'. W
4788 is the window on which S is drawn. ROW and AREA are the glyph row
4789 and area within the row from which S is constructed. START is the
4790 index of the first glyph structure covered by S. HL is a
4791 face-override for drawing S. */
4792
4793 static void
4794 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4795 struct glyph_string *s;
4796 XChar2b *char2b;
4797 struct window *w;
4798 struct glyph_row *row;
4799 enum glyph_row_area area;
4800 int start;
4801 enum draw_glyphs_face hl;
4802 {
4803 bzero (s, sizeof *s);
4804 s->w = w;
4805 s->f = XFRAME (w->frame);
4806 s->display = FRAME_X_DISPLAY (s->f);
4807 s->window = FRAME_X_WINDOW (s->f);
4808 s->char2b = char2b;
4809 s->hl = hl;
4810 s->row = row;
4811 s->area = area;
4812 s->first_glyph = row->glyphs[area] + start;
4813 s->height = row->height;
4814 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4815
4816 /* Display the internal border below the tool-bar window. */
4817 if (s->w == XWINDOW (s->f->tool_bar_window))
4818 s->y -= s->f->output_data.x->internal_border_width;
4819
4820 s->ybase = s->y + row->ascent;
4821 }
4822
4823
4824 /* Set background width of glyph string S. START is the index of the
4825 first glyph following S. LAST_X is the right-most x-position + 1
4826 in the drawing area. */
4827
4828 static INLINE void
4829 x_set_glyph_string_background_width (s, start, last_x)
4830 struct glyph_string *s;
4831 int start;
4832 int last_x;
4833 {
4834 /* If the face of this glyph string has to be drawn to the end of
4835 the drawing area, set S->extends_to_end_of_line_p. */
4836 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4837
4838 if (start == s->row->used[s->area]
4839 && s->hl == DRAW_NORMAL_TEXT
4840 && s->area == TEXT_AREA
4841 && (s->row->fill_line_p
4842 || s->face->background != default_face->background
4843 || s->face->stipple != default_face->stipple))
4844 s->extends_to_end_of_line_p = 1;
4845
4846 /* If S extends its face to the end of the line, set its
4847 background_width to the distance to the right edge of the drawing
4848 area. */
4849 if (s->extends_to_end_of_line_p)
4850 s->background_width = last_x - s->x + 1;
4851 else
4852 s->background_width = s->width;
4853 }
4854
4855
4856 /* Add a glyph string for a stretch glyph to the list of strings
4857 between HEAD and TAIL. START is the index of the stretch glyph in
4858 row area AREA of glyph row ROW. END is the index of the last glyph
4859 in that glyph row area. X is the current output position assigned
4860 to the new glyph string constructed. HL overrides that face of the
4861 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4862 is the right-most x-position of the drawing area. */
4863
4864 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4865 and below -- keep them on one line. */
4866 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4867 do \
4868 { \
4869 s = (struct glyph_string *) alloca (sizeof *s); \
4870 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4871 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4872 x_append_glyph_string (&HEAD, &TAIL, s); \
4873 s->x = (X); \
4874 } \
4875 while (0)
4876
4877
4878 /* Add a glyph string for an image glyph to the list of strings
4879 between HEAD and TAIL. START is the index of the image glyph in
4880 row area AREA of glyph row ROW. END is the index of the last glyph
4881 in that glyph row area. X is the current output position assigned
4882 to the new glyph string constructed. HL overrides that face of the
4883 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4884 is the right-most x-position of the drawing area. */
4885
4886 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4887 do \
4888 { \
4889 s = (struct glyph_string *) alloca (sizeof *s); \
4890 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4891 x_fill_image_glyph_string (s); \
4892 x_append_glyph_string (&HEAD, &TAIL, s); \
4893 ++START; \
4894 s->x = (X); \
4895 } \
4896 while (0)
4897
4898
4899 /* Add a glyph string for a sequence of character glyphs to the list
4900 of strings between HEAD and TAIL. START is the index of the first
4901 glyph in row area AREA of glyph row ROW that is part of the new
4902 glyph string. END is the index of the last glyph in that glyph row
4903 area. X is the current output position assigned to the new glyph
4904 string constructed. HL overrides that face of the glyph; e.g. it
4905 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4906 right-most x-position of the drawing area. */
4907
4908 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4909 do \
4910 { \
4911 int c, face_id; \
4912 XChar2b *char2b; \
4913 \
4914 c = (ROW)->glyphs[AREA][START].u.ch; \
4915 face_id = (ROW)->glyphs[AREA][START].face_id; \
4916 \
4917 s = (struct glyph_string *) alloca (sizeof *s); \
4918 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4919 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4920 x_append_glyph_string (&HEAD, &TAIL, s); \
4921 s->x = (X); \
4922 START = x_fill_glyph_string (s, face_id, START, END, \
4923 OVERLAPS_P); \
4924 } \
4925 while (0)
4926
4927
4928 /* Add a glyph string for a composite sequence to the list of strings
4929 between HEAD and TAIL. START is the index of the first glyph in
4930 row area AREA of glyph row ROW that is part of the new glyph
4931 string. END is the index of the last glyph in that glyph row area.
4932 X is the current output position assigned to the new glyph string
4933 constructed. HL overrides that face of the glyph; e.g. it is
4934 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4935 x-position of the drawing area. */
4936
4937 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4938 do { \
4939 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4940 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4941 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4942 struct composition *cmp = composition_table[cmp_id]; \
4943 int glyph_len = cmp->glyph_len; \
4944 XChar2b *char2b; \
4945 struct face **faces; \
4946 struct glyph_string *first_s = NULL; \
4947 int n; \
4948 \
4949 base_face = base_face->ascii_face; \
4950 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4951 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4952 /* At first, fill in `char2b' and `faces'. */ \
4953 for (n = 0; n < glyph_len; n++) \
4954 { \
4955 int c = COMPOSITION_GLYPH (cmp, n); \
4956 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4957 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4958 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4959 this_face_id, char2b + n, 1); \
4960 } \
4961 \
4962 /* Make glyph_strings for each glyph sequence that is drawable by \
4963 the same face, and append them to HEAD/TAIL. */ \
4964 for (n = 0; n < cmp->glyph_len;) \
4965 { \
4966 s = (struct glyph_string *) alloca (sizeof *s); \
4967 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4968 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4969 s->cmp = cmp; \
4970 s->gidx = n; \
4971 s->x = (X); \
4972 \
4973 if (n == 0) \
4974 first_s = s; \
4975 \
4976 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4977 } \
4978 \
4979 ++START; \
4980 s = first_s; \
4981 } while (0)
4982
4983
4984 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4985 of AREA of glyph row ROW on window W between indices START and END.
4986 HL overrides the face for drawing glyph strings, e.g. it is
4987 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4988 x-positions of the drawing area.
4989
4990 This is an ugly monster macro construct because we must use alloca
4991 to allocate glyph strings (because x_draw_glyphs can be called
4992 asynchronously). */
4993
4994 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4995 do \
4996 { \
4997 HEAD = TAIL = NULL; \
4998 while (START < END) \
4999 { \
5000 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
5001 switch (first_glyph->type) \
5002 { \
5003 case CHAR_GLYPH: \
5004 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
5005 TAIL, HL, X, LAST_X, \
5006 OVERLAPS_P); \
5007 break; \
5008 \
5009 case COMPOSITE_GLYPH: \
5010 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
5011 HEAD, TAIL, HL, X, LAST_X,\
5012 OVERLAPS_P); \
5013 break; \
5014 \
5015 case STRETCH_GLYPH: \
5016 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
5017 HEAD, TAIL, HL, X, LAST_X); \
5018 break; \
5019 \
5020 case IMAGE_GLYPH: \
5021 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
5022 TAIL, HL, X, LAST_X); \
5023 break; \
5024 \
5025 default: \
5026 abort (); \
5027 } \
5028 \
5029 x_set_glyph_string_background_width (s, START, LAST_X); \
5030 (X) += s->width; \
5031 } \
5032 } \
5033 while (0)
5034
5035
5036 /* Draw glyphs between START and END in AREA of ROW on window W,
5037 starting at x-position X. X is relative to AREA in W. HL is a
5038 face-override with the following meaning:
5039
5040 DRAW_NORMAL_TEXT draw normally
5041 DRAW_CURSOR draw in cursor face
5042 DRAW_MOUSE_FACE draw in mouse face.
5043 DRAW_INVERSE_VIDEO draw in mode line face
5044 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
5045 DRAW_IMAGE_RAISED draw an image with a raised relief around it
5046
5047 If REAL_START is non-null, return in *REAL_START the real starting
5048 position for display. This can be different from START in case
5049 overlapping glyphs must be displayed. If REAL_END is non-null,
5050 return in *REAL_END the real end position for display. This can be
5051 different from END in case overlapping glyphs must be displayed.
5052
5053 If OVERLAPS_P is non-zero, draw only the foreground of characters
5054 and clip to the physical height of ROW.
5055
5056 Value is the x-position reached, relative to AREA of W. */
5057
5058 static int
5059 x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
5060 overlaps_p)
5061 struct window *w;
5062 int x;
5063 struct glyph_row *row;
5064 enum glyph_row_area area;
5065 int start, end;
5066 enum draw_glyphs_face hl;
5067 int *real_start, *real_end;
5068 int overlaps_p;
5069 {
5070 struct glyph_string *head, *tail;
5071 struct glyph_string *s;
5072 int last_x, area_width;
5073 int x_reached;
5074 int i, j;
5075
5076 /* Let's rather be paranoid than getting a SEGV. */
5077 end = min (end, row->used[area]);
5078 start = max (0, start);
5079 start = min (end, start);
5080 if (real_start)
5081 *real_start = start;
5082 if (real_end)
5083 *real_end = end;
5084
5085 /* Translate X to frame coordinates. Set last_x to the right
5086 end of the drawing area. */
5087 if (row->full_width_p)
5088 {
5089 /* X is relative to the left edge of W, without scroll bars
5090 or flag areas. */
5091 struct frame *f = XFRAME (w->frame);
5092 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
5093 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
5094
5095 x += window_left_x;
5096 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
5097 last_x = window_left_x + area_width;
5098
5099 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5100 {
5101 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5102 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
5103 last_x += width;
5104 else
5105 x -= width;
5106 }
5107
5108 x += FRAME_INTERNAL_BORDER_WIDTH (f);
5109 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
5110 }
5111 else
5112 {
5113 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
5114 area_width = window_box_width (w, area);
5115 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
5116 }
5117
5118 /* Build a doubly-linked list of glyph_string structures between
5119 head and tail from what we have to draw. Note that the macro
5120 BUILD_GLYPH_STRINGS will modify its start parameter. That's
5121 the reason we use a separate variable `i'. */
5122 i = start;
5123 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
5124 overlaps_p);
5125 if (tail)
5126 x_reached = tail->x + tail->background_width;
5127 else
5128 x_reached = x;
5129
5130 /* If there are any glyphs with lbearing < 0 or rbearing > width in
5131 the row, redraw some glyphs in front or following the glyph
5132 strings built above. */
5133 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
5134 {
5135 int dummy_x = 0;
5136 struct glyph_string *h, *t;
5137
5138 /* Compute overhangs for all glyph strings. */
5139 for (s = head; s; s = s->next)
5140 x_compute_glyph_string_overhangs (s);
5141
5142 /* Prepend glyph strings for glyphs in front of the first glyph
5143 string that are overwritten because of the first glyph
5144 string's left overhang. The background of all strings
5145 prepended must be drawn because the first glyph string
5146 draws over it. */
5147 i = x_left_overwritten (head);
5148 if (i >= 0)
5149 {
5150 j = i;
5151 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
5152 DRAW_NORMAL_TEXT, dummy_x, last_x,
5153 overlaps_p);
5154 start = i;
5155 if (real_start)
5156 *real_start = start;
5157 x_compute_overhangs_and_x (t, head->x, 1);
5158 x_prepend_glyph_string_lists (&head, &tail, h, t);
5159 }
5160
5161 /* Prepend glyph strings for glyphs in front of the first glyph
5162 string that overwrite that glyph string because of their
5163 right overhang. For these strings, only the foreground must
5164 be drawn, because it draws over the glyph string at `head'.
5165 The background must not be drawn because this would overwrite
5166 right overhangs of preceding glyphs for which no glyph
5167 strings exist. */
5168 i = x_left_overwriting (head);
5169 if (i >= 0)
5170 {
5171 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
5172 DRAW_NORMAL_TEXT, dummy_x, last_x,
5173 overlaps_p);
5174 for (s = h; s; s = s->next)
5175 s->background_filled_p = 1;
5176 if (real_start)
5177 *real_start = i;
5178 x_compute_overhangs_and_x (t, head->x, 1);
5179 x_prepend_glyph_string_lists (&head, &tail, h, t);
5180 }
5181
5182 /* Append glyphs strings for glyphs following the last glyph
5183 string tail that are overwritten by tail. The background of
5184 these strings has to be drawn because tail's foreground draws
5185 over it. */
5186 i = x_right_overwritten (tail);
5187 if (i >= 0)
5188 {
5189 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5190 DRAW_NORMAL_TEXT, x, last_x,
5191 overlaps_p);
5192 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5193 x_append_glyph_string_lists (&head, &tail, h, t);
5194 if (real_end)
5195 *real_end = i;
5196 }
5197
5198 /* Append glyph strings for glyphs following the last glyph
5199 string tail that overwrite tail. The foreground of such
5200 glyphs has to be drawn because it writes into the background
5201 of tail. The background must not be drawn because it could
5202 paint over the foreground of following glyphs. */
5203 i = x_right_overwriting (tail);
5204 if (i >= 0)
5205 {
5206 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5207 DRAW_NORMAL_TEXT, x, last_x,
5208 overlaps_p);
5209 for (s = h; s; s = s->next)
5210 s->background_filled_p = 1;
5211 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5212 x_append_glyph_string_lists (&head, &tail, h, t);
5213 if (real_end)
5214 *real_end = i;
5215 }
5216 }
5217
5218 /* Draw all strings. */
5219 for (s = head; s; s = s->next)
5220 x_draw_glyph_string (s);
5221
5222 /* Value is the x-position up to which drawn, relative to AREA of W.
5223 This doesn't include parts drawn because of overhangs. */
5224 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5225 if (!row->full_width_p)
5226 {
5227 if (area > LEFT_MARGIN_AREA)
5228 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5229 if (area > TEXT_AREA)
5230 x_reached -= window_box_width (w, TEXT_AREA);
5231 }
5232
5233 return x_reached;
5234 }
5235
5236
5237 /* Fix the display of area AREA of overlapping row ROW in window W. */
5238
5239 static void
5240 x_fix_overlapping_area (w, row, area)
5241 struct window *w;
5242 struct glyph_row *row;
5243 enum glyph_row_area area;
5244 {
5245 int i, x;
5246
5247 BLOCK_INPUT;
5248
5249 if (area == LEFT_MARGIN_AREA)
5250 x = 0;
5251 else if (area == TEXT_AREA)
5252 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5253 else
5254 x = (window_box_width (w, LEFT_MARGIN_AREA)
5255 + window_box_width (w, TEXT_AREA));
5256
5257 for (i = 0; i < row->used[area];)
5258 {
5259 if (row->glyphs[area][i].overlaps_vertically_p)
5260 {
5261 int start = i, start_x = x;
5262
5263 do
5264 {
5265 x += row->glyphs[area][i].pixel_width;
5266 ++i;
5267 }
5268 while (i < row->used[area]
5269 && row->glyphs[area][i].overlaps_vertically_p);
5270
5271 x_draw_glyphs (w, start_x, row, area, start, i,
5272 (row->inverse_p
5273 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5274 NULL, NULL, 1);
5275 }
5276 else
5277 {
5278 x += row->glyphs[area][i].pixel_width;
5279 ++i;
5280 }
5281 }
5282
5283 UNBLOCK_INPUT;
5284 }
5285
5286
5287 /* Output LEN glyphs starting at START at the nominal cursor position.
5288 Advance the nominal cursor over the text. The global variable
5289 updated_window contains the window being updated, updated_row is
5290 the glyph row being updated, and updated_area is the area of that
5291 row being updated. */
5292
5293 static void
5294 x_write_glyphs (start, len)
5295 struct glyph *start;
5296 int len;
5297 {
5298 int x, hpos, real_start, real_end;
5299
5300 xassert (updated_window && updated_row);
5301 BLOCK_INPUT;
5302
5303 /* Write glyphs. */
5304
5305 hpos = start - updated_row->glyphs[updated_area];
5306 x = x_draw_glyphs (updated_window, output_cursor.x,
5307 updated_row, updated_area,
5308 hpos, hpos + len,
5309 (updated_row->inverse_p
5310 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5311 &real_start, &real_end, 0);
5312
5313 /* If we drew over the cursor, note that it is not visible any more. */
5314 note_overwritten_text_cursor (updated_window, real_start,
5315 real_end - real_start);
5316
5317 UNBLOCK_INPUT;
5318
5319 /* Advance the output cursor. */
5320 output_cursor.hpos += len;
5321 output_cursor.x = x;
5322 }
5323
5324
5325 /* Insert LEN glyphs from START at the nominal cursor position. */
5326
5327 static void
5328 x_insert_glyphs (start, len)
5329 struct glyph *start;
5330 register int len;
5331 {
5332 struct frame *f;
5333 struct window *w;
5334 int line_height, shift_by_width, shifted_region_width;
5335 struct glyph_row *row;
5336 struct glyph *glyph;
5337 int frame_x, frame_y, hpos, real_start, real_end;
5338
5339 xassert (updated_window && updated_row);
5340 BLOCK_INPUT;
5341 w = updated_window;
5342 f = XFRAME (WINDOW_FRAME (w));
5343
5344 /* Get the height of the line we are in. */
5345 row = updated_row;
5346 line_height = row->height;
5347
5348 /* Get the width of the glyphs to insert. */
5349 shift_by_width = 0;
5350 for (glyph = start; glyph < start + len; ++glyph)
5351 shift_by_width += glyph->pixel_width;
5352
5353 /* Get the width of the region to shift right. */
5354 shifted_region_width = (window_box_width (w, updated_area)
5355 - output_cursor.x
5356 - shift_by_width);
5357
5358 /* Shift right. */
5359 frame_x = window_box_left (w, updated_area) + output_cursor.x;
5360 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5361 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5362 f->output_data.x->normal_gc,
5363 frame_x, frame_y,
5364 shifted_region_width, line_height,
5365 frame_x + shift_by_width, frame_y);
5366
5367 /* Write the glyphs. */
5368 hpos = start - row->glyphs[updated_area];
5369 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5370 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
5371 note_overwritten_text_cursor (w, real_start, real_end - real_start);
5372
5373 /* Advance the output cursor. */
5374 output_cursor.hpos += len;
5375 output_cursor.x += shift_by_width;
5376 UNBLOCK_INPUT;
5377 }
5378
5379
5380 /* Delete N glyphs at the nominal cursor position. Not implemented
5381 for X frames. */
5382
5383 static void
5384 x_delete_glyphs (n)
5385 register int n;
5386 {
5387 abort ();
5388 }
5389
5390
5391 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5392 If they are <= 0, this is probably an error. */
5393
5394 void
5395 x_clear_area (dpy, window, x, y, width, height, exposures)
5396 Display *dpy;
5397 Window window;
5398 int x, y;
5399 int width, height;
5400 int exposures;
5401 {
5402 xassert (width > 0 && height > 0);
5403 XClearArea (dpy, window, x, y, width, height, exposures);
5404 }
5405
5406
5407 /* Erase the current text line from the nominal cursor position
5408 (inclusive) to pixel column TO_X (exclusive). The idea is that
5409 everything from TO_X onward is already erased.
5410
5411 TO_X is a pixel position relative to updated_area of
5412 updated_window. TO_X == -1 means clear to the end of this area. */
5413
5414 static void
5415 x_clear_end_of_line (to_x)
5416 int to_x;
5417 {
5418 struct frame *f;
5419 struct window *w = updated_window;
5420 int max_x, min_y, max_y;
5421 int from_x, from_y, to_y;
5422
5423 xassert (updated_window && updated_row);
5424 f = XFRAME (w->frame);
5425
5426 if (updated_row->full_width_p)
5427 {
5428 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5429 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5430 && !w->pseudo_window_p)
5431 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5432 }
5433 else
5434 max_x = window_box_width (w, updated_area);
5435 max_y = window_text_bottom_y (w);
5436
5437 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5438 of window. For TO_X > 0, truncate to end of drawing area. */
5439 if (to_x == 0)
5440 return;
5441 else if (to_x < 0)
5442 to_x = max_x;
5443 else
5444 to_x = min (to_x, max_x);
5445
5446 to_y = min (max_y, output_cursor.y + updated_row->height);
5447
5448 /* Notice if the cursor will be cleared by this operation. */
5449 if (!updated_row->full_width_p)
5450 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
5451
5452 from_x = output_cursor.x;
5453
5454 /* Translate to frame coordinates. */
5455 if (updated_row->full_width_p)
5456 {
5457 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5458 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5459 }
5460 else
5461 {
5462 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5463 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5464 }
5465
5466 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5467 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5468 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5469
5470 /* Prevent inadvertently clearing to end of the X window. */
5471 if (to_x > from_x && to_y > from_y)
5472 {
5473 BLOCK_INPUT;
5474 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5475 from_x, from_y, to_x - from_x, to_y - from_y,
5476 False);
5477 UNBLOCK_INPUT;
5478 }
5479 }
5480
5481
5482 /* Clear entire frame. If updating_frame is non-null, clear that
5483 frame. Otherwise clear the selected frame. */
5484
5485 static void
5486 x_clear_frame ()
5487 {
5488 struct frame *f;
5489
5490 if (updating_frame)
5491 f = updating_frame;
5492 else
5493 f = SELECTED_FRAME ();
5494
5495 /* Clearing the frame will erase any cursor, so mark them all as no
5496 longer visible. */
5497 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5498 output_cursor.hpos = output_cursor.vpos = 0;
5499 output_cursor.x = -1;
5500
5501 /* We don't set the output cursor here because there will always
5502 follow an explicit cursor_to. */
5503 BLOCK_INPUT;
5504 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5505
5506 /* We have to clear the scroll bars, too. If we have changed
5507 colors or something like that, then they should be notified. */
5508 x_scroll_bar_clear (f);
5509
5510 XFlush (FRAME_X_DISPLAY (f));
5511 UNBLOCK_INPUT;
5512 }
5513
5514
5515 \f
5516 /* Invert the middle quarter of the frame for .15 sec. */
5517
5518 /* We use the select system call to do the waiting, so we have to make
5519 sure it's available. If it isn't, we just won't do visual bells. */
5520
5521 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5522
5523
5524 /* Subtract the `struct timeval' values X and Y, storing the result in
5525 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5526
5527 static int
5528 timeval_subtract (result, x, y)
5529 struct timeval *result, x, y;
5530 {
5531 /* Perform the carry for the later subtraction by updating y. This
5532 is safer because on some systems the tv_sec member is unsigned. */
5533 if (x.tv_usec < y.tv_usec)
5534 {
5535 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5536 y.tv_usec -= 1000000 * nsec;
5537 y.tv_sec += nsec;
5538 }
5539
5540 if (x.tv_usec - y.tv_usec > 1000000)
5541 {
5542 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5543 y.tv_usec += 1000000 * nsec;
5544 y.tv_sec -= nsec;
5545 }
5546
5547 /* Compute the time remaining to wait. tv_usec is certainly
5548 positive. */
5549 result->tv_sec = x.tv_sec - y.tv_sec;
5550 result->tv_usec = x.tv_usec - y.tv_usec;
5551
5552 /* Return indication of whether the result should be considered
5553 negative. */
5554 return x.tv_sec < y.tv_sec;
5555 }
5556
5557 void
5558 XTflash (f)
5559 struct frame *f;
5560 {
5561 BLOCK_INPUT;
5562
5563 {
5564 GC gc;
5565
5566 /* Create a GC that will use the GXxor function to flip foreground
5567 pixels into background pixels. */
5568 {
5569 XGCValues values;
5570
5571 values.function = GXxor;
5572 values.foreground = (f->output_data.x->foreground_pixel
5573 ^ f->output_data.x->background_pixel);
5574
5575 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5576 GCFunction | GCForeground, &values);
5577 }
5578
5579 {
5580 /* Get the height not including a menu bar widget. */
5581 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5582 /* Height of each line to flash. */
5583 int flash_height = FRAME_LINE_HEIGHT (f);
5584 /* These will be the left and right margins of the rectangles. */
5585 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5586 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5587
5588 int width;
5589
5590 /* Don't flash the area between a scroll bar and the frame
5591 edge it is next to. */
5592 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5593 {
5594 case vertical_scroll_bar_left:
5595 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5596 break;
5597
5598 case vertical_scroll_bar_right:
5599 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5600 break;
5601
5602 default:
5603 break;
5604 }
5605
5606 width = flash_right - flash_left;
5607
5608 /* If window is tall, flash top and bottom line. */
5609 if (height > 3 * FRAME_LINE_HEIGHT (f))
5610 {
5611 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5612 flash_left,
5613 (FRAME_INTERNAL_BORDER_WIDTH (f)
5614 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5615 width, flash_height);
5616 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5617 flash_left,
5618 (height - flash_height
5619 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5620 width, flash_height);
5621 }
5622 else
5623 /* If it is short, flash it all. */
5624 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5625 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5626 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5627
5628 x_flush (f);
5629
5630 {
5631 struct timeval wakeup;
5632
5633 EMACS_GET_TIME (wakeup);
5634
5635 /* Compute time to wait until, propagating carry from usecs. */
5636 wakeup.tv_usec += 150000;
5637 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5638 wakeup.tv_usec %= 1000000;
5639
5640 /* Keep waiting until past the time wakeup or any input gets
5641 available. */
5642 while (! detect_input_pending ())
5643 {
5644 struct timeval current;
5645 struct timeval timeout;
5646
5647 EMACS_GET_TIME (current);
5648
5649 /* Break if result would be negative. */
5650 if (timeval_subtract (&current, wakeup, current))
5651 break;
5652
5653 /* How long `select' should wait. */
5654 timeout.tv_sec = 0;
5655 timeout.tv_usec = 10000;
5656
5657 /* Try to wait that long--but we might wake up sooner. */
5658 select (0, NULL, NULL, NULL, &timeout);
5659 }
5660 }
5661
5662 /* If window is tall, flash top and bottom line. */
5663 if (height > 3 * FRAME_LINE_HEIGHT (f))
5664 {
5665 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5666 flash_left,
5667 (FRAME_INTERNAL_BORDER_WIDTH (f)
5668 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5669 width, flash_height);
5670 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5671 flash_left,
5672 (height - flash_height
5673 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5674 width, flash_height);
5675 }
5676 else
5677 /* If it is short, flash it all. */
5678 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5679 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5680 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5681
5682 XFreeGC (FRAME_X_DISPLAY (f), gc);
5683 x_flush (f);
5684 }
5685 }
5686
5687 UNBLOCK_INPUT;
5688 }
5689
5690 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5691
5692
5693 /* Make audible bell. */
5694
5695 void
5696 XTring_bell ()
5697 {
5698 struct frame *f = SELECTED_FRAME ();
5699
5700 if (FRAME_X_DISPLAY (f))
5701 {
5702 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5703 if (visible_bell)
5704 XTflash (f);
5705 else
5706 #endif
5707 {
5708 BLOCK_INPUT;
5709 XBell (FRAME_X_DISPLAY (f), 0);
5710 XFlush (FRAME_X_DISPLAY (f));
5711 UNBLOCK_INPUT;
5712 }
5713 }
5714 }
5715
5716 \f
5717 /* Specify how many text lines, from the top of the window,
5718 should be affected by insert-lines and delete-lines operations.
5719 This, and those operations, are used only within an update
5720 that is bounded by calls to x_update_begin and x_update_end. */
5721
5722 static void
5723 XTset_terminal_window (n)
5724 register int n;
5725 {
5726 /* This function intentionally left blank. */
5727 }
5728
5729
5730 \f
5731 /***********************************************************************
5732 Line Dance
5733 ***********************************************************************/
5734
5735 /* Perform an insert-lines or delete-lines operation, inserting N
5736 lines or deleting -N lines at vertical position VPOS. */
5737
5738 static void
5739 x_ins_del_lines (vpos, n)
5740 int vpos, n;
5741 {
5742 abort ();
5743 }
5744
5745
5746 /* Scroll part of the display as described by RUN. */
5747
5748 static void
5749 x_scroll_run (w, run)
5750 struct window *w;
5751 struct run *run;
5752 {
5753 struct frame *f = XFRAME (w->frame);
5754 int x, y, width, height, from_y, to_y, bottom_y;
5755
5756 /* Get frame-relative bounding box of the text display area of W,
5757 without mode lines. Include in this box the flags areas to the
5758 left and right of W. */
5759 window_box (w, -1, &x, &y, &width, &height);
5760 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5761 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
5762
5763 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5764 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5765 bottom_y = y + height;
5766
5767 if (to_y < from_y)
5768 {
5769 /* Scrolling up. Make sure we don't copy part of the mode
5770 line at the bottom. */
5771 if (from_y + run->height > bottom_y)
5772 height = bottom_y - from_y;
5773 else
5774 height = run->height;
5775 }
5776 else
5777 {
5778 /* Scolling down. Make sure we don't copy over the mode line.
5779 at the bottom. */
5780 if (to_y + run->height > bottom_y)
5781 height = bottom_y - to_y;
5782 else
5783 height = run->height;
5784 }
5785
5786 BLOCK_INPUT;
5787
5788 /* Cursor off. Will be switched on again in x_update_window_end. */
5789 updated_window = w;
5790 x_clear_cursor (w);
5791
5792 XCopyArea (FRAME_X_DISPLAY (f),
5793 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5794 f->output_data.x->normal_gc,
5795 x, from_y,
5796 width, height,
5797 x, to_y);
5798
5799 UNBLOCK_INPUT;
5800 }
5801
5802
5803 \f
5804 /***********************************************************************
5805 Exposure Events
5806 ***********************************************************************/
5807
5808 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5809 corner of the exposed rectangle. W and H are width and height of
5810 the exposed area. All are pixel values. W or H zero means redraw
5811 the entire frame. */
5812
5813 static void
5814 expose_frame (f, x, y, w, h)
5815 struct frame *f;
5816 int x, y, w, h;
5817 {
5818 XRectangle r;
5819
5820 TRACE ((stderr, "expose_frame "));
5821
5822 /* No need to redraw if frame will be redrawn soon. */
5823 if (FRAME_GARBAGED_P (f))
5824 {
5825 TRACE ((stderr, " garbaged\n"));
5826 return;
5827 }
5828
5829 /* If basic faces haven't been realized yet, there is no point in
5830 trying to redraw anything. This can happen when we get an expose
5831 event while Emacs is starting, e.g. by moving another window. */
5832 if (FRAME_FACE_CACHE (f) == NULL
5833 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5834 {
5835 TRACE ((stderr, " no faces\n"));
5836 return;
5837 }
5838
5839 if (w == 0 || h == 0)
5840 {
5841 r.x = r.y = 0;
5842 r.width = CANON_X_UNIT (f) * f->width;
5843 r.height = CANON_Y_UNIT (f) * f->height;
5844 }
5845 else
5846 {
5847 r.x = x;
5848 r.y = y;
5849 r.width = w;
5850 r.height = h;
5851 }
5852
5853 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5854 expose_window_tree (XWINDOW (f->root_window), &r);
5855
5856 if (WINDOWP (f->tool_bar_window))
5857 expose_window (XWINDOW (f->tool_bar_window), &r);
5858
5859 #ifndef USE_X_TOOLKIT
5860 if (WINDOWP (f->menu_bar_window))
5861 expose_window (XWINDOW (f->menu_bar_window), &r);
5862 #endif /* not USE_X_TOOLKIT */
5863 }
5864
5865
5866 /* Redraw (parts) of all windows in the window tree rooted at W that
5867 intersect R. R contains frame pixel coordinates. */
5868
5869 static void
5870 expose_window_tree (w, r)
5871 struct window *w;
5872 XRectangle *r;
5873 {
5874 while (w)
5875 {
5876 if (!NILP (w->hchild))
5877 expose_window_tree (XWINDOW (w->hchild), r);
5878 else if (!NILP (w->vchild))
5879 expose_window_tree (XWINDOW (w->vchild), r);
5880 else if (expose_window (w, r) == 0)
5881 break;
5882 w = NILP (w->next) ? NULL : XWINDOW (w->next);
5883 }
5884 }
5885
5886
5887 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5888 which intersects rectangle R. R is in window-relative coordinates. */
5889
5890 static void
5891 expose_area (w, row, r, area)
5892 struct window *w;
5893 struct glyph_row *row;
5894 XRectangle *r;
5895 enum glyph_row_area area;
5896 {
5897 struct glyph *first = row->glyphs[area];
5898 struct glyph *end = row->glyphs[area] + row->used[area];
5899 struct glyph *last;
5900 int first_x, start_x, x;
5901
5902 if (area == TEXT_AREA && row->fill_line_p)
5903 /* If row extends face to end of line write the whole line. */
5904 x_draw_glyphs (w, 0, row, area,
5905 0, row->used[area],
5906 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5907 NULL, NULL, 0);
5908 else
5909 {
5910 /* Set START_X to the window-relative start position for drawing glyphs of
5911 AREA. The first glyph of the text area can be partially visible.
5912 The first glyphs of other areas cannot. */
5913 if (area == LEFT_MARGIN_AREA)
5914 start_x = 0;
5915 else if (area == TEXT_AREA)
5916 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5917 else
5918 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5919 + window_box_width (w, TEXT_AREA));
5920 x = start_x;
5921
5922 /* Find the first glyph that must be redrawn. */
5923 while (first < end
5924 && x + first->pixel_width < r->x)
5925 {
5926 x += first->pixel_width;
5927 ++first;
5928 }
5929
5930 /* Find the last one. */
5931 last = first;
5932 first_x = x;
5933 while (last < end
5934 && x < r->x + r->width)
5935 {
5936 x += last->pixel_width;
5937 ++last;
5938 }
5939
5940 /* Repaint. */
5941 if (last > first)
5942 x_draw_glyphs (w, first_x - start_x, row, area,
5943 first - row->glyphs[area],
5944 last - row->glyphs[area],
5945 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5946 NULL, NULL, 0);
5947 }
5948 }
5949
5950
5951 /* Redraw the parts of the glyph row ROW on window W intersecting
5952 rectangle R. R is in window-relative coordinates. */
5953
5954 static void
5955 expose_line (w, row, r)
5956 struct window *w;
5957 struct glyph_row *row;
5958 XRectangle *r;
5959 {
5960 xassert (row->enabled_p);
5961
5962 if (row->mode_line_p || w->pseudo_window_p)
5963 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5964 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5965 NULL, NULL, 0);
5966 else
5967 {
5968 if (row->used[LEFT_MARGIN_AREA])
5969 expose_area (w, row, r, LEFT_MARGIN_AREA);
5970 if (row->used[TEXT_AREA])
5971 expose_area (w, row, r, TEXT_AREA);
5972 if (row->used[RIGHT_MARGIN_AREA])
5973 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5974 x_draw_row_bitmaps (w, row);
5975 }
5976 }
5977
5978
5979 /* Return non-zero if W's cursor intersects rectangle R. */
5980
5981 static int
5982 x_phys_cursor_in_rect_p (w, r)
5983 struct window *w;
5984 XRectangle *r;
5985 {
5986 XRectangle cr, result;
5987 struct glyph *cursor_glyph;
5988
5989 cursor_glyph = get_phys_cursor_glyph (w);
5990 if (cursor_glyph)
5991 {
5992 cr.x = w->phys_cursor.x;
5993 cr.y = w->phys_cursor.y;
5994 cr.width = cursor_glyph->pixel_width;
5995 cr.height = w->phys_cursor_height;
5996 return x_intersect_rectangles (&cr, r, &result);
5997 }
5998 else
5999 return 0;
6000 }
6001
6002
6003 /* Redraw the part of window W intersection rectangle FR. Pixel
6004 coordinates in FR are frame-relative. Call this function with
6005 input blocked. */
6006
6007 static int
6008 expose_window (w, fr)
6009 struct window *w;
6010 XRectangle *fr;
6011 {
6012 struct frame *f = XFRAME (w->frame);
6013 XRectangle wr, r;
6014
6015 /* If window is not yet fully initialized, do nothing. This can
6016 happen when toolkit scroll bars are used and a window is split.
6017 Reconfiguring the scroll bar will generate an expose for a newly
6018 created window. */
6019 if (w->current_matrix == NULL)
6020 return 1;
6021
6022 /* When we're currently updating the window, display and current
6023 matrix usually don't agree. Arrange for a thorough display
6024 later. */
6025 if (w == updated_window)
6026 {
6027 SET_FRAME_GARBAGED (f);
6028 return 0;
6029 }
6030
6031 /* Frame-relative pixel rectangle of W. */
6032 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
6033 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
6034 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
6035 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
6036
6037 if (x_intersect_rectangles (fr, &wr, &r))
6038 {
6039 int yb = window_text_bottom_y (w);
6040 struct glyph_row *row;
6041 int cursor_cleared_p;
6042
6043 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
6044 r.x, r.y, r.width, r.height));
6045
6046 /* Convert to window coordinates. */
6047 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
6048 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
6049
6050 /* Turn off the cursor. */
6051 if (!w->pseudo_window_p
6052 && x_phys_cursor_in_rect_p (w, &r))
6053 {
6054 x_clear_cursor (w);
6055 cursor_cleared_p = 1;
6056 }
6057 else
6058 cursor_cleared_p = 0;
6059
6060 /* Find the first row intersecting the rectangle R. */
6061 for (row = w->current_matrix->rows;
6062 row->enabled_p;
6063 ++row)
6064 {
6065 int y0 = row->y;
6066 int y1 = MATRIX_ROW_BOTTOM_Y (row);
6067
6068 if ((y0 >= r.y && y0 < r.y + r.height)
6069 || (y1 > r.y && y1 < r.y + r.height)
6070 || (r.y >= y0 && r.y < y1)
6071 || (r.y + r.height > y0 && r.y + r.height < y1))
6072 expose_line (w, row, &r);
6073
6074 if (y1 >= yb)
6075 break;
6076 }
6077
6078 /* Display the mode line if there is one. */
6079 if (WINDOW_WANTS_MODELINE_P (w)
6080 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
6081 row->enabled_p)
6082 && row->y < r.y + r.height)
6083 expose_line (w, row, &r);
6084
6085 if (!w->pseudo_window_p)
6086 {
6087 /* Draw border between windows. */
6088 x_draw_vertical_border (w);
6089
6090 /* Turn the cursor on again. */
6091 if (cursor_cleared_p)
6092 x_update_window_cursor (w, 1);
6093 }
6094 }
6095
6096 return 1;
6097 }
6098
6099
6100 /* Determine the intersection of two rectangles R1 and R2. Return
6101 the intersection in *RESULT. Value is non-zero if RESULT is not
6102 empty. */
6103
6104 static int
6105 x_intersect_rectangles (r1, r2, result)
6106 XRectangle *r1, *r2, *result;
6107 {
6108 XRectangle *left, *right;
6109 XRectangle *upper, *lower;
6110 int intersection_p = 0;
6111
6112 /* Rearrange so that R1 is the left-most rectangle. */
6113 if (r1->x < r2->x)
6114 left = r1, right = r2;
6115 else
6116 left = r2, right = r1;
6117
6118 /* X0 of the intersection is right.x0, if this is inside R1,
6119 otherwise there is no intersection. */
6120 if (right->x <= left->x + left->width)
6121 {
6122 result->x = right->x;
6123
6124 /* The right end of the intersection is the minimum of the
6125 the right ends of left and right. */
6126 result->width = (min (left->x + left->width, right->x + right->width)
6127 - result->x);
6128
6129 /* Same game for Y. */
6130 if (r1->y < r2->y)
6131 upper = r1, lower = r2;
6132 else
6133 upper = r2, lower = r1;
6134
6135 /* The upper end of the intersection is lower.y0, if this is inside
6136 of upper. Otherwise, there is no intersection. */
6137 if (lower->y <= upper->y + upper->height)
6138 {
6139 result->y = lower->y;
6140
6141 /* The lower end of the intersection is the minimum of the lower
6142 ends of upper and lower. */
6143 result->height = (min (lower->y + lower->height,
6144 upper->y + upper->height)
6145 - result->y);
6146 intersection_p = 1;
6147 }
6148 }
6149
6150 return intersection_p;
6151 }
6152
6153
6154
6155
6156 \f
6157 static void
6158 frame_highlight (f)
6159 struct frame *f;
6160 {
6161 /* We used to only do this if Vx_no_window_manager was non-nil, but
6162 the ICCCM (section 4.1.6) says that the window's border pixmap
6163 and border pixel are window attributes which are "private to the
6164 client", so we can always change it to whatever we want. */
6165 BLOCK_INPUT;
6166 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6167 f->output_data.x->border_pixel);
6168 UNBLOCK_INPUT;
6169 x_update_cursor (f, 1);
6170 }
6171
6172 static void
6173 frame_unhighlight (f)
6174 struct frame *f;
6175 {
6176 /* We used to only do this if Vx_no_window_manager was non-nil, but
6177 the ICCCM (section 4.1.6) says that the window's border pixmap
6178 and border pixel are window attributes which are "private to the
6179 client", so we can always change it to whatever we want. */
6180 BLOCK_INPUT;
6181 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6182 f->output_data.x->border_tile);
6183 UNBLOCK_INPUT;
6184 x_update_cursor (f, 1);
6185 }
6186
6187 /* The focus has changed. Update the frames as necessary to reflect
6188 the new situation. Note that we can't change the selected frame
6189 here, because the Lisp code we are interrupting might become confused.
6190 Each event gets marked with the frame in which it occurred, so the
6191 Lisp code can tell when the switch took place by examining the events. */
6192
6193 static void
6194 x_new_focus_frame (dpyinfo, frame)
6195 struct x_display_info *dpyinfo;
6196 struct frame *frame;
6197 {
6198 struct frame *old_focus = dpyinfo->x_focus_frame;
6199
6200 if (frame != dpyinfo->x_focus_frame)
6201 {
6202 /* Set this before calling other routines, so that they see
6203 the correct value of x_focus_frame. */
6204 dpyinfo->x_focus_frame = frame;
6205
6206 if (old_focus && old_focus->auto_lower)
6207 x_lower_frame (old_focus);
6208
6209 #if 0
6210 selected_frame = frame;
6211 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6212 selected_frame);
6213 Fselect_window (selected_frame->selected_window);
6214 choose_minibuf_frame ();
6215 #endif /* ! 0 */
6216
6217 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6218 pending_autoraise_frame = dpyinfo->x_focus_frame;
6219 else
6220 pending_autoraise_frame = 0;
6221 }
6222
6223 x_frame_rehighlight (dpyinfo);
6224 }
6225
6226 /* Handle an event saying the mouse has moved out of an Emacs frame. */
6227
6228 void
6229 x_mouse_leave (dpyinfo)
6230 struct x_display_info *dpyinfo;
6231 {
6232 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
6233 }
6234
6235 /* The focus has changed, or we have redirected a frame's focus to
6236 another frame (this happens when a frame uses a surrogate
6237 mini-buffer frame). Shift the highlight as appropriate.
6238
6239 The FRAME argument doesn't necessarily have anything to do with which
6240 frame is being highlighted or un-highlighted; we only use it to find
6241 the appropriate X display info. */
6242
6243 static void
6244 XTframe_rehighlight (frame)
6245 struct frame *frame;
6246 {
6247 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6248 }
6249
6250 static void
6251 x_frame_rehighlight (dpyinfo)
6252 struct x_display_info *dpyinfo;
6253 {
6254 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6255
6256 if (dpyinfo->x_focus_frame)
6257 {
6258 dpyinfo->x_highlight_frame
6259 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6260 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6261 : dpyinfo->x_focus_frame);
6262 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
6263 {
6264 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6265 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
6266 }
6267 }
6268 else
6269 dpyinfo->x_highlight_frame = 0;
6270
6271 if (dpyinfo->x_highlight_frame != old_highlight)
6272 {
6273 if (old_highlight)
6274 frame_unhighlight (old_highlight);
6275 if (dpyinfo->x_highlight_frame)
6276 frame_highlight (dpyinfo->x_highlight_frame);
6277 }
6278 }
6279
6280
6281 \f
6282 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
6283
6284 /* Initialize mode_switch_bit and modifier_meaning. */
6285 static void
6286 x_find_modifier_meanings (dpyinfo)
6287 struct x_display_info *dpyinfo;
6288 {
6289 int min_code, max_code;
6290 KeySym *syms;
6291 int syms_per_code;
6292 XModifierKeymap *mods;
6293
6294 dpyinfo->meta_mod_mask = 0;
6295 dpyinfo->shift_lock_mask = 0;
6296 dpyinfo->alt_mod_mask = 0;
6297 dpyinfo->super_mod_mask = 0;
6298 dpyinfo->hyper_mod_mask = 0;
6299
6300 #ifdef HAVE_X11R4
6301 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
6302 #else
6303 min_code = dpyinfo->display->min_keycode;
6304 max_code = dpyinfo->display->max_keycode;
6305 #endif
6306
6307 syms = XGetKeyboardMapping (dpyinfo->display,
6308 min_code, max_code - min_code + 1,
6309 &syms_per_code);
6310 mods = XGetModifierMapping (dpyinfo->display);
6311
6312 /* Scan the modifier table to see which modifier bits the Meta and
6313 Alt keysyms are on. */
6314 {
6315 int row, col; /* The row and column in the modifier table. */
6316
6317 for (row = 3; row < 8; row++)
6318 for (col = 0; col < mods->max_keypermod; col++)
6319 {
6320 KeyCode code
6321 = mods->modifiermap[(row * mods->max_keypermod) + col];
6322
6323 /* Zeroes are used for filler. Skip them. */
6324 if (code == 0)
6325 continue;
6326
6327 /* Are any of this keycode's keysyms a meta key? */
6328 {
6329 int code_col;
6330
6331 for (code_col = 0; code_col < syms_per_code; code_col++)
6332 {
6333 int sym = syms[((code - min_code) * syms_per_code) + code_col];
6334
6335 switch (sym)
6336 {
6337 case XK_Meta_L:
6338 case XK_Meta_R:
6339 dpyinfo->meta_mod_mask |= (1 << row);
6340 break;
6341
6342 case XK_Alt_L:
6343 case XK_Alt_R:
6344 dpyinfo->alt_mod_mask |= (1 << row);
6345 break;
6346
6347 case XK_Hyper_L:
6348 case XK_Hyper_R:
6349 dpyinfo->hyper_mod_mask |= (1 << row);
6350 break;
6351
6352 case XK_Super_L:
6353 case XK_Super_R:
6354 dpyinfo->super_mod_mask |= (1 << row);
6355 break;
6356
6357 case XK_Shift_Lock:
6358 /* Ignore this if it's not on the lock modifier. */
6359 if ((1 << row) == LockMask)
6360 dpyinfo->shift_lock_mask = LockMask;
6361 break;
6362 }
6363 }
6364 }
6365 }
6366 }
6367
6368 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
6369 if (! dpyinfo->meta_mod_mask)
6370 {
6371 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6372 dpyinfo->alt_mod_mask = 0;
6373 }
6374
6375 /* If some keys are both alt and meta,
6376 make them just meta, not alt. */
6377 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6378 {
6379 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6380 }
6381
6382 XFree ((char *) syms);
6383 XFreeModifiermap (mods);
6384 }
6385
6386 /* Convert between the modifier bits X uses and the modifier bits
6387 Emacs uses. */
6388
6389 static unsigned int
6390 x_x_to_emacs_modifiers (dpyinfo, state)
6391 struct x_display_info *dpyinfo;
6392 unsigned int state;
6393 {
6394 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6395 | ((state & ControlMask) ? ctrl_modifier : 0)
6396 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6397 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6398 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6399 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
6400 }
6401
6402 static unsigned int
6403 x_emacs_to_x_modifiers (dpyinfo, state)
6404 struct x_display_info *dpyinfo;
6405 unsigned int state;
6406 {
6407 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6408 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6409 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6410 | ((state & shift_modifier) ? ShiftMask : 0)
6411 | ((state & ctrl_modifier) ? ControlMask : 0)
6412 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
6413 }
6414
6415 /* Convert a keysym to its name. */
6416
6417 char *
6418 x_get_keysym_name (keysym)
6419 KeySym keysym;
6420 {
6421 char *value;
6422
6423 BLOCK_INPUT;
6424 value = XKeysymToString (keysym);
6425 UNBLOCK_INPUT;
6426
6427 return value;
6428 }
6429
6430
6431 \f
6432 /* Mouse clicks and mouse movement. Rah. */
6433
6434 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6435 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6436 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6437 not force the value into range. */
6438
6439 void
6440 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6441 FRAME_PTR f;
6442 register int pix_x, pix_y;
6443 register int *x, *y;
6444 XRectangle *bounds;
6445 int noclip;
6446 {
6447 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6448 even for negative values. */
6449 if (pix_x < 0)
6450 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6451 if (pix_y < 0)
6452 pix_y -= (f)->output_data.x->line_height - 1;
6453
6454 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6455 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6456
6457 if (bounds)
6458 {
6459 bounds->width = FONT_WIDTH (f->output_data.x->font);
6460 bounds->height = f->output_data.x->line_height;
6461 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6462 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6463 }
6464
6465 if (!noclip)
6466 {
6467 if (pix_x < 0)
6468 pix_x = 0;
6469 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6470 pix_x = FRAME_WINDOW_WIDTH (f);
6471
6472 if (pix_y < 0)
6473 pix_y = 0;
6474 else if (pix_y > f->height)
6475 pix_y = f->height;
6476 }
6477
6478 *x = pix_x;
6479 *y = pix_y;
6480 }
6481
6482
6483 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6484 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6485 can't tell the positions because W's display is not up to date,
6486 return 0. */
6487
6488 int
6489 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6490 struct window *w;
6491 int hpos, vpos;
6492 int *frame_x, *frame_y;
6493 {
6494 int success_p;
6495
6496 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6497 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6498
6499 if (display_completed)
6500 {
6501 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6502 struct glyph *glyph = row->glyphs[TEXT_AREA];
6503 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6504
6505 *frame_y = row->y;
6506 *frame_x = row->x;
6507 while (glyph < end)
6508 {
6509 *frame_x += glyph->pixel_width;
6510 ++glyph;
6511 }
6512
6513 success_p = 1;
6514 }
6515 else
6516 {
6517 *frame_y = *frame_x = 0;
6518 success_p = 0;
6519 }
6520
6521 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6522 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6523 return success_p;
6524 }
6525
6526
6527 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6528
6529 If the event is a button press, then note that we have grabbed
6530 the mouse. */
6531
6532 static Lisp_Object
6533 construct_mouse_click (result, event, f)
6534 struct input_event *result;
6535 XButtonEvent *event;
6536 struct frame *f;
6537 {
6538 /* Make the event type no_event; we'll change that when we decide
6539 otherwise. */
6540 result->kind = mouse_click;
6541 result->code = event->button - Button1;
6542 result->timestamp = event->time;
6543 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6544 event->state)
6545 | (event->type == ButtonRelease
6546 ? up_modifier
6547 : down_modifier));
6548
6549 XSETINT (result->x, event->x);
6550 XSETINT (result->y, event->y);
6551 XSETFRAME (result->frame_or_window, f);
6552 result->arg = Qnil;
6553 return Qnil;
6554 }
6555
6556 \f
6557 /* Function to report a mouse movement to the mainstream Emacs code.
6558 The input handler calls this.
6559
6560 We have received a mouse movement event, which is given in *event.
6561 If the mouse is over a different glyph than it was last time, tell
6562 the mainstream emacs code by setting mouse_moved. If not, ask for
6563 another motion event, so we can check again the next time it moves. */
6564
6565 static XMotionEvent last_mouse_motion_event;
6566 static Lisp_Object last_mouse_motion_frame;
6567
6568 static void
6569 note_mouse_movement (frame, event)
6570 FRAME_PTR frame;
6571 XMotionEvent *event;
6572 {
6573 last_mouse_movement_time = event->time;
6574 last_mouse_motion_event = *event;
6575 XSETFRAME (last_mouse_motion_frame, frame);
6576
6577 if (event->window != FRAME_X_WINDOW (frame))
6578 {
6579 frame->mouse_moved = 1;
6580 last_mouse_scroll_bar = Qnil;
6581 note_mouse_highlight (frame, -1, -1);
6582 }
6583
6584 /* Has the mouse moved off the glyph it was on at the last sighting? */
6585 else if (event->x < last_mouse_glyph.x
6586 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6587 || event->y < last_mouse_glyph.y
6588 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6589 {
6590 frame->mouse_moved = 1;
6591 last_mouse_scroll_bar = Qnil;
6592 note_mouse_highlight (frame, event->x, event->y);
6593 }
6594 }
6595
6596 /* This is used for debugging, to turn off note_mouse_highlight. */
6597
6598 int disable_mouse_highlight;
6599
6600
6601 \f
6602 /************************************************************************
6603 Mouse Face
6604 ************************************************************************/
6605
6606 /* Find the glyph under window-relative coordinates X/Y in window W.
6607 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6608 strings. Return in *HPOS and *VPOS the row and column number of
6609 the glyph found. Return in *AREA the glyph area containing X.
6610 Value is a pointer to the glyph found or null if X/Y is not on
6611 text, or we can't tell because W's current matrix is not up to
6612 date. */
6613
6614 static struct glyph *
6615 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
6616 struct window *w;
6617 int x, y;
6618 int *hpos, *vpos, *area;
6619 int buffer_only_p;
6620 {
6621 struct glyph *glyph, *end;
6622 struct glyph_row *row = NULL;
6623 int x0, i, left_area_width;
6624
6625 /* Find row containing Y. Give up if some row is not enabled. */
6626 for (i = 0; i < w->current_matrix->nrows; ++i)
6627 {
6628 row = MATRIX_ROW (w->current_matrix, i);
6629 if (!row->enabled_p)
6630 return NULL;
6631 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6632 break;
6633 }
6634
6635 *vpos = i;
6636 *hpos = 0;
6637
6638 /* Give up if Y is not in the window. */
6639 if (i == w->current_matrix->nrows)
6640 return NULL;
6641
6642 /* Get the glyph area containing X. */
6643 if (w->pseudo_window_p)
6644 {
6645 *area = TEXT_AREA;
6646 x0 = 0;
6647 }
6648 else
6649 {
6650 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6651 if (x < left_area_width)
6652 {
6653 *area = LEFT_MARGIN_AREA;
6654 x0 = 0;
6655 }
6656 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6657 {
6658 *area = TEXT_AREA;
6659 x0 = row->x + left_area_width;
6660 }
6661 else
6662 {
6663 *area = RIGHT_MARGIN_AREA;
6664 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6665 }
6666 }
6667
6668 /* Find glyph containing X. */
6669 glyph = row->glyphs[*area];
6670 end = glyph + row->used[*area];
6671 while (glyph < end)
6672 {
6673 if (x < x0 + glyph->pixel_width)
6674 {
6675 if (w->pseudo_window_p)
6676 break;
6677 else if (!buffer_only_p || BUFFERP (glyph->object))
6678 break;
6679 }
6680
6681 x0 += glyph->pixel_width;
6682 ++glyph;
6683 }
6684
6685 if (glyph == end)
6686 return NULL;
6687
6688 *hpos = glyph - row->glyphs[*area];
6689 return glyph;
6690 }
6691
6692
6693 /* Convert frame-relative x/y to coordinates relative to window W.
6694 Takes pseudo-windows into account. */
6695
6696 static void
6697 frame_to_window_pixel_xy (w, x, y)
6698 struct window *w;
6699 int *x, *y;
6700 {
6701 if (w->pseudo_window_p)
6702 {
6703 /* A pseudo-window is always full-width, and starts at the
6704 left edge of the frame, plus a frame border. */
6705 struct frame *f = XFRAME (w->frame);
6706 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6707 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6708 }
6709 else
6710 {
6711 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6712 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6713 }
6714 }
6715
6716
6717 /* Take proper action when mouse has moved to the mode or header line of
6718 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6719 mode line. X is relative to the start of the text display area of
6720 W, so the width of bitmap areas and scroll bars must be subtracted
6721 to get a position relative to the start of the mode line. */
6722
6723 static void
6724 note_mode_line_highlight (w, x, mode_line_p)
6725 struct window *w;
6726 int x, mode_line_p;
6727 {
6728 struct frame *f = XFRAME (w->frame);
6729 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6730 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6731 struct glyph_row *row;
6732
6733 if (mode_line_p)
6734 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6735 else
6736 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6737
6738 if (row->enabled_p)
6739 {
6740 struct glyph *glyph, *end;
6741 Lisp_Object help, map;
6742 int x0;
6743
6744 /* Find the glyph under X. */
6745 glyph = row->glyphs[TEXT_AREA];
6746 end = glyph + row->used[TEXT_AREA];
6747 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6748 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
6749
6750 while (glyph < end
6751 && x >= x0 + glyph->pixel_width)
6752 {
6753 x0 += glyph->pixel_width;
6754 ++glyph;
6755 }
6756
6757 if (glyph < end
6758 && STRINGP (glyph->object)
6759 && XSTRING (glyph->object)->intervals
6760 && glyph->charpos >= 0
6761 && glyph->charpos < XSTRING (glyph->object)->size)
6762 {
6763 /* If we're on a string with `help-echo' text property,
6764 arrange for the help to be displayed. This is done by
6765 setting the global variable help_echo to the help string. */
6766 help = Fget_text_property (make_number (glyph->charpos),
6767 Qhelp_echo, glyph->object);
6768 if (!NILP (help))
6769 {
6770 help_echo = help;
6771 XSETWINDOW (help_echo_window, w);
6772 help_echo_object = glyph->object;
6773 help_echo_pos = glyph->charpos;
6774 }
6775
6776 /* Change the mouse pointer according to what is under X/Y. */
6777 map = Fget_text_property (make_number (glyph->charpos),
6778 Qlocal_map, glyph->object);
6779 if (KEYMAPP (map))
6780 cursor = f->output_data.x->nontext_cursor;
6781 else
6782 {
6783 map = Fget_text_property (make_number (glyph->charpos),
6784 Qkeymap, glyph->object);
6785 if (KEYMAPP (map))
6786 cursor = f->output_data.x->nontext_cursor;
6787 }
6788 }
6789 }
6790
6791 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6792 }
6793
6794
6795 /* Take proper action when the mouse has moved to position X, Y on
6796 frame F as regards highlighting characters that have mouse-face
6797 properties. Also de-highlighting chars where the mouse was before.
6798 X and Y can be negative or out of range. */
6799
6800 static void
6801 note_mouse_highlight (f, x, y)
6802 struct frame *f;
6803 int x, y;
6804 {
6805 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6806 int portion;
6807 Lisp_Object window;
6808 struct window *w;
6809 Cursor cursor = None;
6810 struct buffer *b;
6811
6812 /* When a menu is active, don't highlight because this looks odd. */
6813 #ifdef USE_X_TOOLKIT
6814 if (popup_activated ())
6815 return;
6816 #endif
6817
6818 if (disable_mouse_highlight
6819 || !f->glyphs_initialized_p)
6820 return;
6821
6822 dpyinfo->mouse_face_mouse_x = x;
6823 dpyinfo->mouse_face_mouse_y = y;
6824 dpyinfo->mouse_face_mouse_frame = f;
6825
6826 if (dpyinfo->mouse_face_defer)
6827 return;
6828
6829 if (gc_in_progress)
6830 {
6831 dpyinfo->mouse_face_deferred_gc = 1;
6832 return;
6833 }
6834
6835 /* Which window is that in? */
6836 window = window_from_coordinates (f, x, y, &portion, 1);
6837
6838 /* If we were displaying active text in another window, clear that. */
6839 if (! EQ (window, dpyinfo->mouse_face_window))
6840 clear_mouse_face (dpyinfo);
6841
6842 /* Not on a window -> return. */
6843 if (!WINDOWP (window))
6844 return;
6845
6846 /* Convert to window-relative pixel coordinates. */
6847 w = XWINDOW (window);
6848 frame_to_window_pixel_xy (w, &x, &y);
6849
6850 /* Handle tool-bar window differently since it doesn't display a
6851 buffer. */
6852 if (EQ (window, f->tool_bar_window))
6853 {
6854 note_tool_bar_highlight (f, x, y);
6855 return;
6856 }
6857
6858 /* Mouse is on the mode or header line? */
6859 if (portion == 1 || portion == 3)
6860 {
6861 note_mode_line_highlight (w, x, portion == 1);
6862 return;
6863 }
6864
6865 if (portion == 2)
6866 cursor = f->output_data.x->horizontal_drag_cursor;
6867 else
6868 cursor = f->output_data.x->text_cursor;
6869
6870 /* Are we in a window whose display is up to date?
6871 And verify the buffer's text has not changed. */
6872 b = XBUFFER (w->buffer);
6873 if (/* Within text portion of the window. */
6874 portion == 0
6875 && EQ (w->window_end_valid, w->buffer)
6876 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
6877 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
6878 {
6879 int hpos, vpos, pos, i, area;
6880 struct glyph *glyph;
6881 Lisp_Object object;
6882 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
6883 Lisp_Object *overlay_vec = NULL;
6884 int len, noverlays;
6885 struct buffer *obuf;
6886 int obegv, ozv, same_region;
6887
6888 /* Find the glyph under X/Y. */
6889 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
6890
6891 /* Clear mouse face if X/Y not over text. */
6892 if (glyph == NULL
6893 || area != TEXT_AREA
6894 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6895 {
6896 if (clear_mouse_face (dpyinfo))
6897 cursor = None;
6898 goto set_cursor;
6899 }
6900
6901 pos = glyph->charpos;
6902 object = glyph->object;
6903 if (!STRINGP (object) && !BUFFERP (object))
6904 goto set_cursor;
6905
6906 /* If we get an out-of-range value, return now; avoid an error. */
6907 if (BUFFERP (object) && pos > BUF_Z (b))
6908 goto set_cursor;
6909
6910 /* Make the window's buffer temporarily current for
6911 overlays_at and compute_char_face. */
6912 obuf = current_buffer;
6913 current_buffer = b;
6914 obegv = BEGV;
6915 ozv = ZV;
6916 BEGV = BEG;
6917 ZV = Z;
6918
6919 /* Is this char mouse-active or does it have help-echo? */
6920 position = make_number (pos);
6921
6922 if (BUFFERP (object))
6923 {
6924 /* Put all the overlays we want in a vector in overlay_vec.
6925 Store the length in len. If there are more than 10, make
6926 enough space for all, and try again. */
6927 len = 10;
6928 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6929 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6930 if (noverlays > len)
6931 {
6932 len = noverlays;
6933 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6934 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6935 }
6936
6937 /* Sort overlays into increasing priority order. */
6938 noverlays = sort_overlays (overlay_vec, noverlays, w);
6939 }
6940 else
6941 noverlays = 0;
6942
6943 same_region = (EQ (window, dpyinfo->mouse_face_window)
6944 && vpos >= dpyinfo->mouse_face_beg_row
6945 && vpos <= dpyinfo->mouse_face_end_row
6946 && (vpos > dpyinfo->mouse_face_beg_row
6947 || hpos >= dpyinfo->mouse_face_beg_col)
6948 && (vpos < dpyinfo->mouse_face_end_row
6949 || hpos < dpyinfo->mouse_face_end_col
6950 || dpyinfo->mouse_face_past_end));
6951
6952 if (same_region)
6953 cursor = None;
6954
6955 /* Check mouse-face highlighting. */
6956 if (! same_region
6957 /* If there exists an overlay with mouse-face overlapping
6958 the one we are currently highlighting, we have to
6959 check if we enter the overlapping overlay, and then
6960 highlight only that. */
6961 || (OVERLAYP (dpyinfo->mouse_face_overlay)
6962 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
6963 {
6964 /* Find the highest priority overlay that has a mouse-face
6965 property. */
6966 overlay = Qnil;
6967 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
6968 {
6969 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6970 if (!NILP (mouse_face))
6971 overlay = overlay_vec[i];
6972 }
6973
6974 /* If we're actually highlighting the same overlay as
6975 before, there's no need to do that again. */
6976 if (!NILP (overlay)
6977 && EQ (overlay, dpyinfo->mouse_face_overlay))
6978 goto check_help_echo;
6979
6980 dpyinfo->mouse_face_overlay = overlay;
6981
6982 /* Clear the display of the old active region, if any. */
6983 if (clear_mouse_face (dpyinfo))
6984 cursor = None;
6985
6986 /* If no overlay applies, get a text property. */
6987 if (NILP (overlay))
6988 mouse_face = Fget_text_property (position, Qmouse_face, object);
6989
6990 /* Handle the overlay case. */
6991 if (!NILP (overlay))
6992 {
6993 /* Find the range of text around this char that
6994 should be active. */
6995 Lisp_Object before, after;
6996 int ignore;
6997
6998 before = Foverlay_start (overlay);
6999 after = Foverlay_end (overlay);
7000 /* Record this as the current active region. */
7001 fast_find_position (w, XFASTINT (before),
7002 &dpyinfo->mouse_face_beg_col,
7003 &dpyinfo->mouse_face_beg_row,
7004 &dpyinfo->mouse_face_beg_x,
7005 &dpyinfo->mouse_face_beg_y);
7006 dpyinfo->mouse_face_past_end
7007 = !fast_find_position (w, XFASTINT (after),
7008 &dpyinfo->mouse_face_end_col,
7009 &dpyinfo->mouse_face_end_row,
7010 &dpyinfo->mouse_face_end_x,
7011 &dpyinfo->mouse_face_end_y);
7012 dpyinfo->mouse_face_window = window;
7013 dpyinfo->mouse_face_face_id
7014 = face_at_buffer_position (w, pos, 0, 0,
7015 &ignore, pos + 1, 1);
7016
7017 /* Display it as active. */
7018 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7019 cursor = None;
7020 }
7021 /* Handle the text property case. */
7022 else if (!NILP (mouse_face) && BUFFERP (object))
7023 {
7024 /* Find the range of text around this char that
7025 should be active. */
7026 Lisp_Object before, after, beginning, end;
7027 int ignore;
7028
7029 beginning = Fmarker_position (w->start);
7030 end = make_number (BUF_Z (XBUFFER (object))
7031 - XFASTINT (w->window_end_pos));
7032 before
7033 = Fprevious_single_property_change (make_number (pos + 1),
7034 Qmouse_face,
7035 object, beginning);
7036 after
7037 = Fnext_single_property_change (position, Qmouse_face,
7038 object, end);
7039
7040 /* Record this as the current active region. */
7041 fast_find_position (w, XFASTINT (before),
7042 &dpyinfo->mouse_face_beg_col,
7043 &dpyinfo->mouse_face_beg_row,
7044 &dpyinfo->mouse_face_beg_x,
7045 &dpyinfo->mouse_face_beg_y);
7046 dpyinfo->mouse_face_past_end
7047 = !fast_find_position (w, XFASTINT (after),
7048 &dpyinfo->mouse_face_end_col,
7049 &dpyinfo->mouse_face_end_row,
7050 &dpyinfo->mouse_face_end_x,
7051 &dpyinfo->mouse_face_end_y);
7052 dpyinfo->mouse_face_window = window;
7053
7054 if (BUFFERP (object))
7055 dpyinfo->mouse_face_face_id
7056 = face_at_buffer_position (w, pos, 0, 0,
7057 &ignore, pos + 1, 1);
7058
7059 /* Display it as active. */
7060 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7061 cursor = None;
7062 }
7063 else if (!NILP (mouse_face) && STRINGP (object))
7064 {
7065 Lisp_Object b, e;
7066 int ignore;
7067
7068 b = Fprevious_single_property_change (make_number (pos + 1),
7069 Qmouse_face,
7070 object, Qnil);
7071 e = Fnext_single_property_change (position, Qmouse_face,
7072 object, Qnil);
7073 if (NILP (b))
7074 b = make_number (0);
7075 if (NILP (e))
7076 e = make_number (XSTRING (object)->size - 1);
7077 fast_find_string_pos (w, XINT (b), object,
7078 &dpyinfo->mouse_face_beg_col,
7079 &dpyinfo->mouse_face_beg_row,
7080 &dpyinfo->mouse_face_beg_x,
7081 &dpyinfo->mouse_face_beg_y, 0);
7082 fast_find_string_pos (w, XINT (e), object,
7083 &dpyinfo->mouse_face_end_col,
7084 &dpyinfo->mouse_face_end_row,
7085 &dpyinfo->mouse_face_end_x,
7086 &dpyinfo->mouse_face_end_y, 1);
7087 dpyinfo->mouse_face_past_end = 0;
7088 dpyinfo->mouse_face_window = window;
7089 dpyinfo->mouse_face_face_id
7090 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
7091 glyph->face_id, 1);
7092 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7093 cursor = None;
7094 }
7095 }
7096
7097 check_help_echo:
7098
7099 /* Look for a `help-echo' property. */
7100 {
7101 Lisp_Object help, overlay;
7102
7103 /* Check overlays first. */
7104 help = overlay = Qnil;
7105 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
7106 {
7107 overlay = overlay_vec[i];
7108 help = Foverlay_get (overlay, Qhelp_echo);
7109 }
7110
7111 if (!NILP (help))
7112 {
7113 help_echo = help;
7114 help_echo_window = window;
7115 help_echo_object = overlay;
7116 help_echo_pos = pos;
7117 }
7118 else
7119 {
7120 Lisp_Object object = glyph->object;
7121 int charpos = glyph->charpos;
7122
7123 /* Try text properties. */
7124 if (STRINGP (object)
7125 && charpos >= 0
7126 && charpos < XSTRING (object)->size)
7127 {
7128 help = Fget_text_property (make_number (charpos),
7129 Qhelp_echo, object);
7130 if (NILP (help))
7131 {
7132 /* If the string itself doesn't specify a help-echo,
7133 see if the buffer text ``under'' it does. */
7134 struct glyph_row *r
7135 = MATRIX_ROW (w->current_matrix, vpos);
7136 int start = MATRIX_ROW_START_CHARPOS (r);
7137 int pos = string_buffer_position (w, object, start);
7138 if (pos > 0)
7139 {
7140 help = Fget_text_property (make_number (pos),
7141 Qhelp_echo, w->buffer);
7142 if (!NILP (help))
7143 {
7144 charpos = pos;
7145 object = w->buffer;
7146 }
7147 }
7148 }
7149 }
7150 else if (BUFFERP (object)
7151 && charpos >= BEGV
7152 && charpos < ZV)
7153 help = Fget_text_property (make_number (charpos), Qhelp_echo,
7154 object);
7155
7156 if (!NILP (help))
7157 {
7158 help_echo = help;
7159 help_echo_window = window;
7160 help_echo_object = object;
7161 help_echo_pos = charpos;
7162 }
7163 }
7164 }
7165
7166 BEGV = obegv;
7167 ZV = ozv;
7168 current_buffer = obuf;
7169 }
7170
7171 set_cursor:
7172
7173 if (cursor != None)
7174 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7175 }
7176
7177 static void
7178 redo_mouse_highlight ()
7179 {
7180 if (!NILP (last_mouse_motion_frame)
7181 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
7182 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
7183 last_mouse_motion_event.x,
7184 last_mouse_motion_event.y);
7185 }
7186
7187
7188 \f
7189 /***********************************************************************
7190 Tool-bars
7191 ***********************************************************************/
7192
7193 static int x_tool_bar_item P_ ((struct frame *, int, int,
7194 struct glyph **, int *, int *, int *));
7195
7196 /* Tool-bar item index of the item on which a mouse button was pressed
7197 or -1. */
7198
7199 static int last_tool_bar_item;
7200
7201
7202 /* Get information about the tool-bar item at position X/Y on frame F.
7203 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7204 the current matrix of the tool-bar window of F, or NULL if not
7205 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
7206 item in F->tool_bar_items. Value is
7207
7208 -1 if X/Y is not on a tool-bar item
7209 0 if X/Y is on the same item that was highlighted before.
7210 1 otherwise. */
7211
7212 static int
7213 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
7214 struct frame *f;
7215 int x, y;
7216 struct glyph **glyph;
7217 int *hpos, *vpos, *prop_idx;
7218 {
7219 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7220 struct window *w = XWINDOW (f->tool_bar_window);
7221 int area;
7222
7223 /* Find the glyph under X/Y. */
7224 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
7225 if (*glyph == NULL)
7226 return -1;
7227
7228 /* Get the start of this tool-bar item's properties in
7229 f->tool_bar_items. */
7230 if (!tool_bar_item_info (f, *glyph, prop_idx))
7231 return -1;
7232
7233 /* Is mouse on the highlighted item? */
7234 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
7235 && *vpos >= dpyinfo->mouse_face_beg_row
7236 && *vpos <= dpyinfo->mouse_face_end_row
7237 && (*vpos > dpyinfo->mouse_face_beg_row
7238 || *hpos >= dpyinfo->mouse_face_beg_col)
7239 && (*vpos < dpyinfo->mouse_face_end_row
7240 || *hpos < dpyinfo->mouse_face_end_col
7241 || dpyinfo->mouse_face_past_end))
7242 return 0;
7243
7244 return 1;
7245 }
7246
7247
7248 /* Handle mouse button event on the tool-bar of frame F, at
7249 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
7250 or ButtonRelase. */
7251
7252 static void
7253 x_handle_tool_bar_click (f, button_event)
7254 struct frame *f;
7255 XButtonEvent *button_event;
7256 {
7257 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7258 struct window *w = XWINDOW (f->tool_bar_window);
7259 int hpos, vpos, prop_idx;
7260 struct glyph *glyph;
7261 Lisp_Object enabled_p;
7262 int x = button_event->x;
7263 int y = button_event->y;
7264
7265 /* If not on the highlighted tool-bar item, return. */
7266 frame_to_window_pixel_xy (w, &x, &y);
7267 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
7268 return;
7269
7270 /* If item is disabled, do nothing. */
7271 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7272 if (NILP (enabled_p))
7273 return;
7274
7275 if (button_event->type == ButtonPress)
7276 {
7277 /* Show item in pressed state. */
7278 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7279 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
7280 last_tool_bar_item = prop_idx;
7281 }
7282 else
7283 {
7284 Lisp_Object key, frame;
7285 struct input_event event;
7286
7287 /* Show item in released state. */
7288 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7289 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7290
7291 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
7292
7293 XSETFRAME (frame, f);
7294 event.kind = TOOL_BAR_EVENT;
7295 event.frame_or_window = frame;
7296 event.arg = frame;
7297 kbd_buffer_store_event (&event);
7298
7299 event.kind = TOOL_BAR_EVENT;
7300 event.frame_or_window = frame;
7301 event.arg = key;
7302 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7303 button_event->state);
7304 kbd_buffer_store_event (&event);
7305 last_tool_bar_item = -1;
7306 }
7307 }
7308
7309
7310 /* Possibly highlight a tool-bar item on frame F when mouse moves to
7311 tool-bar window-relative coordinates X/Y. Called from
7312 note_mouse_highlight. */
7313
7314 static void
7315 note_tool_bar_highlight (f, x, y)
7316 struct frame *f;
7317 int x, y;
7318 {
7319 Lisp_Object window = f->tool_bar_window;
7320 struct window *w = XWINDOW (window);
7321 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7322 int hpos, vpos;
7323 struct glyph *glyph;
7324 struct glyph_row *row;
7325 int i;
7326 Lisp_Object enabled_p;
7327 int prop_idx;
7328 enum draw_glyphs_face draw;
7329 int mouse_down_p, rc;
7330
7331 /* Function note_mouse_highlight is called with negative x(y
7332 values when mouse moves outside of the frame. */
7333 if (x <= 0 || y <= 0)
7334 {
7335 clear_mouse_face (dpyinfo);
7336 return;
7337 }
7338
7339 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7340 if (rc < 0)
7341 {
7342 /* Not on tool-bar item. */
7343 clear_mouse_face (dpyinfo);
7344 return;
7345 }
7346 else if (rc == 0)
7347 goto set_help_echo;
7348
7349 clear_mouse_face (dpyinfo);
7350
7351 /* Mouse is down, but on different tool-bar item? */
7352 mouse_down_p = (dpyinfo->grabbed
7353 && f == last_mouse_frame
7354 && FRAME_LIVE_P (f));
7355 if (mouse_down_p
7356 && last_tool_bar_item != prop_idx)
7357 return;
7358
7359 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7360 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7361
7362 /* If tool-bar item is not enabled, don't highlight it. */
7363 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7364 if (!NILP (enabled_p))
7365 {
7366 /* Compute the x-position of the glyph. In front and past the
7367 image is a space. We include this is the highlighted area. */
7368 row = MATRIX_ROW (w->current_matrix, vpos);
7369 for (i = x = 0; i < hpos; ++i)
7370 x += row->glyphs[TEXT_AREA][i].pixel_width;
7371
7372 /* Record this as the current active region. */
7373 dpyinfo->mouse_face_beg_col = hpos;
7374 dpyinfo->mouse_face_beg_row = vpos;
7375 dpyinfo->mouse_face_beg_x = x;
7376 dpyinfo->mouse_face_beg_y = row->y;
7377 dpyinfo->mouse_face_past_end = 0;
7378
7379 dpyinfo->mouse_face_end_col = hpos + 1;
7380 dpyinfo->mouse_face_end_row = vpos;
7381 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7382 dpyinfo->mouse_face_end_y = row->y;
7383 dpyinfo->mouse_face_window = window;
7384 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7385
7386 /* Display it as active. */
7387 show_mouse_face (dpyinfo, draw);
7388 dpyinfo->mouse_face_image_state = draw;
7389 }
7390
7391 set_help_echo:
7392
7393 /* Set help_echo to a help string.to display for this tool-bar item.
7394 XTread_socket does the rest. */
7395 help_echo_object = help_echo_window = Qnil;
7396 help_echo_pos = -1;
7397 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
7398 if (NILP (help_echo))
7399 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
7400 }
7401
7402
7403 \f
7404 /* Find the glyph matrix position of buffer position POS in window W.
7405 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7406 current glyphs must be up to date. If POS is above window start
7407 return (0, 0, 0, 0). If POS is after end of W, return end of
7408 last line in W. */
7409
7410 static int
7411 fast_find_position (w, pos, hpos, vpos, x, y)
7412 struct window *w;
7413 int pos;
7414 int *hpos, *vpos, *x, *y;
7415 {
7416 int i;
7417 int lastcol;
7418 int maybe_next_line_p = 0;
7419 int line_start_position;
7420 int yb = window_text_bottom_y (w);
7421 struct glyph_row *row, *best_row;
7422 int row_vpos, best_row_vpos;
7423 int current_x;
7424
7425 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7426 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7427
7428 while (row->y < yb)
7429 {
7430 if (row->used[TEXT_AREA])
7431 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7432 else
7433 line_start_position = 0;
7434
7435 if (line_start_position > pos)
7436 break;
7437 /* If the position sought is the end of the buffer,
7438 don't include the blank lines at the bottom of the window. */
7439 else if (line_start_position == pos
7440 && pos == BUF_ZV (XBUFFER (w->buffer)))
7441 {
7442 maybe_next_line_p = 1;
7443 break;
7444 }
7445 else if (line_start_position > 0)
7446 {
7447 best_row = row;
7448 best_row_vpos = row_vpos;
7449 }
7450
7451 if (row->y + row->height >= yb)
7452 break;
7453
7454 ++row;
7455 ++row_vpos;
7456 }
7457
7458 /* Find the right column within BEST_ROW. */
7459 lastcol = 0;
7460 current_x = best_row->x;
7461 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
7462 {
7463 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7464 int charpos;
7465
7466 charpos = glyph->charpos;
7467 if (charpos == pos)
7468 {
7469 *hpos = i;
7470 *vpos = best_row_vpos;
7471 *x = current_x;
7472 *y = best_row->y;
7473 return 1;
7474 }
7475 else if (charpos > pos)
7476 break;
7477 else if (charpos > 0)
7478 lastcol = i;
7479
7480 current_x += glyph->pixel_width;
7481 }
7482
7483 /* If we're looking for the end of the buffer,
7484 and we didn't find it in the line we scanned,
7485 use the start of the following line. */
7486 if (maybe_next_line_p)
7487 {
7488 ++best_row;
7489 ++best_row_vpos;
7490 lastcol = 0;
7491 current_x = best_row->x;
7492 }
7493
7494 *vpos = best_row_vpos;
7495 *hpos = lastcol + 1;
7496 *x = current_x;
7497 *y = best_row->y;
7498 return 0;
7499 }
7500
7501
7502 /* Find the position of the the glyph for position POS in OBJECT in
7503 window W's current matrix, and return in *X/*Y the pixel
7504 coordinates, and return in *HPOS/*VPOS the column/row of the glyph.
7505
7506 RIGHT_P non-zero means return the position of the right edge of the
7507 glyph, RIGHT_P zero means return the left edge position.
7508
7509 If no glyph for POS exists in the matrix, return the position of
7510 the glyph with the next smaller position that is in the matrix, if
7511 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7512 exists in the matrix, return the position of the glyph with the
7513 next larger position in OBJECT.
7514
7515 Value is non-zero if a glyph was found. */
7516
7517 static int
7518 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7519 struct window *w;
7520 int pos;
7521 Lisp_Object object;
7522 int *hpos, *vpos, *x, *y;
7523 int right_p;
7524 {
7525 int yb = window_text_bottom_y (w);
7526 struct glyph_row *r;
7527 struct glyph *best_glyph = NULL;
7528 struct glyph_row *best_row = NULL;
7529 int best_x = 0;
7530
7531 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7532 r->enabled_p && r->y < yb;
7533 ++r)
7534 {
7535 struct glyph *g = r->glyphs[TEXT_AREA];
7536 struct glyph *e = g + r->used[TEXT_AREA];
7537 int gx;
7538
7539 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7540 if (EQ (g->object, object))
7541 {
7542 if (g->charpos == pos)
7543 {
7544 best_glyph = g;
7545 best_x = gx;
7546 best_row = r;
7547 goto found;
7548 }
7549 else if (best_glyph == NULL
7550 || ((abs (g->charpos - pos)
7551 < abs (best_glyph->charpos - pos))
7552 && (right_p
7553 ? g->charpos < pos
7554 : g->charpos > pos)))
7555 {
7556 best_glyph = g;
7557 best_x = gx;
7558 best_row = r;
7559 }
7560 }
7561 }
7562
7563 found:
7564
7565 if (best_glyph)
7566 {
7567 *x = best_x;
7568 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7569
7570 if (right_p)
7571 {
7572 *x += best_glyph->pixel_width;
7573 ++*hpos;
7574 }
7575
7576 *y = best_row->y;
7577 *vpos = best_row - w->current_matrix->rows;
7578 }
7579
7580 return best_glyph != NULL;
7581 }
7582
7583
7584 /* Display the active region described by mouse_face_*
7585 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7586
7587 static void
7588 show_mouse_face (dpyinfo, draw)
7589 struct x_display_info *dpyinfo;
7590 enum draw_glyphs_face draw;
7591 {
7592 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7593 struct frame *f = XFRAME (WINDOW_FRAME (w));
7594 int i;
7595 int cursor_off_p = 0;
7596 struct cursor_pos saved_cursor;
7597
7598 saved_cursor = output_cursor;
7599
7600 /* If window is in the process of being destroyed, don't bother
7601 to do anything. */
7602 if (w->current_matrix == NULL)
7603 goto set_x_cursor;
7604
7605 /* Recognize when we are called to operate on rows that don't exist
7606 anymore. This can happen when a window is split. */
7607 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7608 goto set_x_cursor;
7609
7610 set_output_cursor (&w->phys_cursor);
7611
7612 /* Note that mouse_face_beg_row etc. are window relative. */
7613 for (i = dpyinfo->mouse_face_beg_row;
7614 i <= dpyinfo->mouse_face_end_row;
7615 i++)
7616 {
7617 int start_hpos, end_hpos, start_x;
7618 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7619
7620 /* Don't do anything if row doesn't have valid contents. */
7621 if (!row->enabled_p)
7622 continue;
7623
7624 /* For all but the first row, the highlight starts at column 0. */
7625 if (i == dpyinfo->mouse_face_beg_row)
7626 {
7627 start_hpos = dpyinfo->mouse_face_beg_col;
7628 start_x = dpyinfo->mouse_face_beg_x;
7629 }
7630 else
7631 {
7632 start_hpos = 0;
7633 start_x = 0;
7634 }
7635
7636 if (i == dpyinfo->mouse_face_end_row)
7637 end_hpos = dpyinfo->mouse_face_end_col;
7638 else
7639 end_hpos = row->used[TEXT_AREA];
7640
7641 /* If the cursor's in the text we are about to rewrite, turn the
7642 cursor off. */
7643 if (!w->pseudo_window_p
7644 && i == output_cursor.vpos
7645 && output_cursor.hpos >= start_hpos - 1
7646 && output_cursor.hpos <= end_hpos)
7647 {
7648 x_update_window_cursor (w, 0);
7649 cursor_off_p = 1;
7650 }
7651
7652 if (end_hpos > start_hpos)
7653 {
7654 row->mouse_face_p = draw == DRAW_MOUSE_FACE || DRAW_IMAGE_RAISED;
7655 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7656 start_hpos, end_hpos, draw, NULL, NULL, 0);
7657 }
7658 }
7659
7660 /* If we turned the cursor off, turn it back on. */
7661 if (cursor_off_p)
7662 x_display_cursor (w, 1,
7663 output_cursor.hpos, output_cursor.vpos,
7664 output_cursor.x, output_cursor.y);
7665
7666 output_cursor = saved_cursor;
7667
7668 set_x_cursor:
7669
7670 /* Change the mouse cursor. */
7671 if (draw == DRAW_NORMAL_TEXT)
7672 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7673 f->output_data.x->text_cursor);
7674 else if (draw == DRAW_MOUSE_FACE)
7675 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7676 f->output_data.x->cross_cursor);
7677 else
7678 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7679 f->output_data.x->nontext_cursor);
7680 }
7681
7682 /* Clear out the mouse-highlighted active region.
7683 Redraw it un-highlighted first. Value is non-zero if mouse
7684 face was actually drawn unhighlighted. */
7685
7686 static int
7687 clear_mouse_face (dpyinfo)
7688 struct x_display_info *dpyinfo;
7689 {
7690 int cleared = 0;
7691
7692 if (!NILP (dpyinfo->mouse_face_window))
7693 {
7694 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7695 cleared = 1;
7696 }
7697
7698 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7699 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7700 dpyinfo->mouse_face_window = Qnil;
7701 dpyinfo->mouse_face_overlay = Qnil;
7702 return cleared;
7703 }
7704
7705
7706 /* Clear any mouse-face on window W. This function is part of the
7707 redisplay interface, and is called from try_window_id and similar
7708 functions to ensure the mouse-highlight is off. */
7709
7710 static void
7711 x_clear_mouse_face (w)
7712 struct window *w;
7713 {
7714 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7715 Lisp_Object window;
7716
7717 BLOCK_INPUT;
7718 XSETWINDOW (window, w);
7719 if (EQ (window, dpyinfo->mouse_face_window))
7720 clear_mouse_face (dpyinfo);
7721 UNBLOCK_INPUT;
7722 }
7723
7724
7725 /* Just discard the mouse face information for frame F, if any.
7726 This is used when the size of F is changed. */
7727
7728 void
7729 cancel_mouse_face (f)
7730 FRAME_PTR f;
7731 {
7732 Lisp_Object window;
7733 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7734
7735 window = dpyinfo->mouse_face_window;
7736 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7737 {
7738 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7739 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7740 dpyinfo->mouse_face_window = Qnil;
7741 }
7742 }
7743
7744 \f
7745 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
7746
7747
7748 /* Try to determine frame pixel position and size of the glyph under
7749 frame pixel coordinates X/Y on frame F . Return the position and
7750 size in *RECT. Value is non-zero if we could compute these
7751 values. */
7752
7753 static int
7754 glyph_rect (f, x, y, rect)
7755 struct frame *f;
7756 int x, y;
7757 XRectangle *rect;
7758 {
7759 Lisp_Object window;
7760 int part, found = 0;
7761
7762 window = window_from_coordinates (f, x, y, &part, 0);
7763 if (!NILP (window))
7764 {
7765 struct window *w = XWINDOW (window);
7766 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7767 struct glyph_row *end = r + w->current_matrix->nrows - 1;
7768 int area;
7769
7770 frame_to_window_pixel_xy (w, &x, &y);
7771
7772 for (; !found && r < end && r->enabled_p; ++r)
7773 if (r->y >= y)
7774 {
7775 struct glyph *g = r->glyphs[TEXT_AREA];
7776 struct glyph *end = g + r->used[TEXT_AREA];
7777 int gx;
7778
7779 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
7780 if (gx >= x)
7781 {
7782 rect->width = g->pixel_width;
7783 rect->height = r->height;
7784 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
7785 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
7786 found = 1;
7787 }
7788 }
7789 }
7790
7791 return found;
7792 }
7793
7794
7795 /* Return the current position of the mouse.
7796 *FP should be a frame which indicates which display to ask about.
7797
7798 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
7799 and *PART to the frame, window, and scroll bar part that the mouse
7800 is over. Set *X and *Y to the portion and whole of the mouse's
7801 position on the scroll bar.
7802
7803 If the mouse movement started elsewhere, set *FP to the frame the
7804 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
7805 the mouse is over.
7806
7807 Set *TIME to the server time-stamp for the time at which the mouse
7808 was at this position.
7809
7810 Don't store anything if we don't have a valid set of values to report.
7811
7812 This clears the mouse_moved flag, so we can wait for the next mouse
7813 movement. */
7814
7815 static void
7816 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7817 FRAME_PTR *fp;
7818 int insist;
7819 Lisp_Object *bar_window;
7820 enum scroll_bar_part *part;
7821 Lisp_Object *x, *y;
7822 unsigned long *time;
7823 {
7824 FRAME_PTR f1;
7825
7826 BLOCK_INPUT;
7827
7828 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7829 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7830 else
7831 {
7832 Window root;
7833 int root_x, root_y;
7834
7835 Window dummy_window;
7836 int dummy;
7837
7838 Lisp_Object frame, tail;
7839
7840 /* Clear the mouse-moved flag for every frame on this display. */
7841 FOR_EACH_FRAME (tail, frame)
7842 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7843 XFRAME (frame)->mouse_moved = 0;
7844
7845 last_mouse_scroll_bar = Qnil;
7846
7847 /* Figure out which root window we're on. */
7848 XQueryPointer (FRAME_X_DISPLAY (*fp),
7849 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
7850
7851 /* The root window which contains the pointer. */
7852 &root,
7853
7854 /* Trash which we can't trust if the pointer is on
7855 a different screen. */
7856 &dummy_window,
7857
7858 /* The position on that root window. */
7859 &root_x, &root_y,
7860
7861 /* More trash we can't trust. */
7862 &dummy, &dummy,
7863
7864 /* Modifier keys and pointer buttons, about which
7865 we don't care. */
7866 (unsigned int *) &dummy);
7867
7868 /* Now we have a position on the root; find the innermost window
7869 containing the pointer. */
7870 {
7871 Window win, child;
7872 int win_x, win_y;
7873 int parent_x = 0, parent_y = 0;
7874 int count;
7875
7876 win = root;
7877
7878 /* XTranslateCoordinates can get errors if the window
7879 structure is changing at the same time this function
7880 is running. So at least we must not crash from them. */
7881
7882 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
7883
7884 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
7885 && FRAME_LIVE_P (last_mouse_frame))
7886 {
7887 /* If mouse was grabbed on a frame, give coords for that frame
7888 even if the mouse is now outside it. */
7889 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7890
7891 /* From-window, to-window. */
7892 root, FRAME_X_WINDOW (last_mouse_frame),
7893
7894 /* From-position, to-position. */
7895 root_x, root_y, &win_x, &win_y,
7896
7897 /* Child of win. */
7898 &child);
7899 f1 = last_mouse_frame;
7900 }
7901 else
7902 {
7903 while (1)
7904 {
7905 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7906
7907 /* From-window, to-window. */
7908 root, win,
7909
7910 /* From-position, to-position. */
7911 root_x, root_y, &win_x, &win_y,
7912
7913 /* Child of win. */
7914 &child);
7915
7916 if (child == None || child == win)
7917 break;
7918
7919 win = child;
7920 parent_x = win_x;
7921 parent_y = win_y;
7922 }
7923
7924 /* Now we know that:
7925 win is the innermost window containing the pointer
7926 (XTC says it has no child containing the pointer),
7927 win_x and win_y are the pointer's position in it
7928 (XTC did this the last time through), and
7929 parent_x and parent_y are the pointer's position in win's parent.
7930 (They are what win_x and win_y were when win was child.
7931 If win is the root window, it has no parent, and
7932 parent_{x,y} are invalid, but that's okay, because we'll
7933 never use them in that case.) */
7934
7935 /* Is win one of our frames? */
7936 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
7937
7938 #ifdef USE_X_TOOLKIT
7939 /* If we end up with the menu bar window, say it's not
7940 on the frame. */
7941 if (f1 != NULL
7942 && f1->output_data.x->menubar_widget
7943 && win == XtWindow (f1->output_data.x->menubar_widget))
7944 f1 = NULL;
7945 #endif /* USE_X_TOOLKIT */
7946 }
7947
7948 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7949 f1 = 0;
7950
7951 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
7952
7953 /* If not, is it one of our scroll bars? */
7954 if (! f1)
7955 {
7956 struct scroll_bar *bar = x_window_to_scroll_bar (win);
7957
7958 if (bar)
7959 {
7960 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7961 win_x = parent_x;
7962 win_y = parent_y;
7963 }
7964 }
7965
7966 if (f1 == 0 && insist > 0)
7967 f1 = SELECTED_FRAME ();
7968
7969 if (f1)
7970 {
7971 /* Ok, we found a frame. Store all the values.
7972 last_mouse_glyph is a rectangle used to reduce the
7973 generation of mouse events. To not miss any motion
7974 events, we must divide the frame into rectangles of the
7975 size of the smallest character that could be displayed
7976 on it, i.e. into the same rectangles that matrices on
7977 the frame are divided into. */
7978
7979 int width, height, gx, gy;
7980 XRectangle rect;
7981
7982 if (glyph_rect (f1, win_x, win_y, &rect))
7983 last_mouse_glyph = rect;
7984 else
7985 {
7986 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7987 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7988 gx = win_x;
7989 gy = win_y;
7990
7991 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7992 round down even for negative values. */
7993 if (gx < 0)
7994 gx -= width - 1;
7995 if (gy < 0)
7996 gy -= height - 1;
7997 gx = (gx + width - 1) / width * width;
7998 gy = (gy + height - 1) / height * height;
7999
8000 last_mouse_glyph.width = width;
8001 last_mouse_glyph.height = height;
8002 last_mouse_glyph.x = gx;
8003 last_mouse_glyph.y = gy;
8004 }
8005
8006 *bar_window = Qnil;
8007 *part = 0;
8008 *fp = f1;
8009 XSETINT (*x, win_x);
8010 XSETINT (*y, win_y);
8011 *time = last_mouse_movement_time;
8012 }
8013 }
8014 }
8015
8016 UNBLOCK_INPUT;
8017 }
8018
8019
8020 #ifdef USE_X_TOOLKIT
8021
8022 /* Atimer callback function for TIMER. Called every 0.1s to process
8023 Xt timeouts, if needed. We must avoid calling XtAppPending as
8024 much as possible because that function does an implicit XFlush
8025 that slows us down. */
8026
8027 static void
8028 x_process_timeouts (timer)
8029 struct atimer *timer;
8030 {
8031 if (toolkit_scroll_bar_interaction || popup_activated_flag)
8032 {
8033 BLOCK_INPUT;
8034 while (XtAppPending (Xt_app_con) & XtIMTimer)
8035 XtAppProcessEvent (Xt_app_con, XtIMTimer);
8036 UNBLOCK_INPUT;
8037 }
8038 }
8039
8040 #endif /* USE_X_TOOLKIT */
8041
8042 \f
8043 /* Scroll bar support. */
8044
8045 /* Given an X window ID, find the struct scroll_bar which manages it.
8046 This can be called in GC, so we have to make sure to strip off mark
8047 bits. */
8048
8049 static struct scroll_bar *
8050 x_window_to_scroll_bar (window_id)
8051 Window window_id;
8052 {
8053 Lisp_Object tail;
8054
8055 for (tail = Vframe_list;
8056 XGCTYPE (tail) == Lisp_Cons;
8057 tail = XCDR (tail))
8058 {
8059 Lisp_Object frame, bar, condemned;
8060
8061 frame = XCAR (tail);
8062 /* All elements of Vframe_list should be frames. */
8063 if (! GC_FRAMEP (frame))
8064 abort ();
8065
8066 /* Scan this frame's scroll bar list for a scroll bar with the
8067 right window ID. */
8068 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
8069 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
8070 /* This trick allows us to search both the ordinary and
8071 condemned scroll bar lists with one loop. */
8072 ! GC_NILP (bar) || (bar = condemned,
8073 condemned = Qnil,
8074 ! GC_NILP (bar));
8075 bar = XSCROLL_BAR (bar)->next)
8076 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
8077 return XSCROLL_BAR (bar);
8078 }
8079
8080 return 0;
8081 }
8082
8083
8084 #if defined USE_X_TOOLKIT && defined USE_LUCID
8085
8086 /* Return the Lucid menu bar WINDOW is part of. Return null
8087 if WINDOW is not part of a menu bar. */
8088
8089 static Widget
8090 x_window_to_menu_bar (window)
8091 Window window;
8092 {
8093 Lisp_Object tail;
8094
8095 for (tail = Vframe_list;
8096 XGCTYPE (tail) == Lisp_Cons;
8097 tail = XCDR (tail))
8098 {
8099 Lisp_Object frame = XCAR (tail);
8100 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
8101
8102 if (menu_bar && xlwmenu_window_p (menu_bar, window))
8103 return menu_bar;
8104 }
8105
8106 return NULL;
8107 }
8108
8109 #endif /* USE_X_TOOLKIT && USE_LUCID */
8110
8111 \f
8112 /************************************************************************
8113 Toolkit scroll bars
8114 ************************************************************************/
8115
8116 #ifdef USE_TOOLKIT_SCROLL_BARS
8117
8118 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
8119 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
8120 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
8121 struct scroll_bar *));
8122 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
8123 int, int, int));
8124
8125
8126 /* Id of action hook installed for scroll bars. */
8127
8128 static XtActionHookId action_hook_id;
8129
8130 /* Lisp window being scrolled. Set when starting to interact with
8131 a toolkit scroll bar, reset to nil when ending the interaction. */
8132
8133 static Lisp_Object window_being_scrolled;
8134
8135 /* Last scroll bar part sent in xm_scroll_callback. */
8136
8137 static int last_scroll_bar_part;
8138
8139 /* Whether this is an Xaw with arrow-scrollbars. This should imply
8140 that movements of 1/20 of the screen size are mapped to up/down. */
8141
8142 static Boolean xaw3d_arrow_scroll;
8143
8144 /* Whether the drag scrolling maintains the mouse at the top of the
8145 thumb. If not, resizing the thumb needs to be done more carefully
8146 to avoid jerkyness. */
8147
8148 static Boolean xaw3d_pick_top;
8149
8150
8151 /* Action hook installed via XtAppAddActionHook when toolkit scroll
8152 bars are used.. The hook is responsible for detecting when
8153 the user ends an interaction with the scroll bar, and generates
8154 a `end-scroll' scroll_bar_click' event if so. */
8155
8156 static void
8157 xt_action_hook (widget, client_data, action_name, event, params,
8158 num_params)
8159 Widget widget;
8160 XtPointer client_data;
8161 String action_name;
8162 XEvent *event;
8163 String *params;
8164 Cardinal *num_params;
8165 {
8166 int scroll_bar_p;
8167 char *end_action;
8168
8169 #ifdef USE_MOTIF
8170 scroll_bar_p = XmIsScrollBar (widget);
8171 end_action = "Release";
8172 #else /* !USE_MOTIF i.e. use Xaw */
8173 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
8174 end_action = "EndScroll";
8175 #endif /* USE_MOTIF */
8176
8177 if (scroll_bar_p
8178 && strcmp (action_name, end_action) == 0
8179 && WINDOWP (window_being_scrolled))
8180 {
8181 struct window *w;
8182
8183 x_send_scroll_bar_event (window_being_scrolled,
8184 scroll_bar_end_scroll, 0, 0);
8185 w = XWINDOW (window_being_scrolled);
8186 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
8187 window_being_scrolled = Qnil;
8188 last_scroll_bar_part = -1;
8189
8190 /* Xt timeouts no longer needed. */
8191 toolkit_scroll_bar_interaction = 0;
8192 }
8193 }
8194
8195 /* A vector of windows used for communication between
8196 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
8197
8198 static struct window **scroll_bar_windows;
8199 static int scroll_bar_windows_size;
8200
8201
8202 /* Send a client message with message type Xatom_Scrollbar for a
8203 scroll action to the frame of WINDOW. PART is a value identifying
8204 the part of the scroll bar that was clicked on. PORTION is the
8205 amount to scroll of a whole of WHOLE. */
8206
8207 static void
8208 x_send_scroll_bar_event (window, part, portion, whole)
8209 Lisp_Object window;
8210 int part, portion, whole;
8211 {
8212 XEvent event;
8213 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
8214 struct window *w = XWINDOW (window);
8215 struct frame *f = XFRAME (w->frame);
8216 int i;
8217
8218 BLOCK_INPUT;
8219
8220 /* Construct a ClientMessage event to send to the frame. */
8221 ev->type = ClientMessage;
8222 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
8223 ev->display = FRAME_X_DISPLAY (f);
8224 ev->window = FRAME_X_WINDOW (f);
8225 ev->format = 32;
8226
8227 /* We can only transfer 32 bits in the XClientMessageEvent, which is
8228 not enough to store a pointer or Lisp_Object on a 64 bit system.
8229 So, store the window in scroll_bar_windows and pass the index
8230 into that array in the event. */
8231 for (i = 0; i < scroll_bar_windows_size; ++i)
8232 if (scroll_bar_windows[i] == NULL)
8233 break;
8234
8235 if (i == scroll_bar_windows_size)
8236 {
8237 int new_size = max (10, 2 * scroll_bar_windows_size);
8238 size_t nbytes = new_size * sizeof *scroll_bar_windows;
8239 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
8240
8241 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
8242 nbytes);
8243 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
8244 scroll_bar_windows_size = new_size;
8245 }
8246
8247 scroll_bar_windows[i] = w;
8248 ev->data.l[0] = (long) i;
8249 ev->data.l[1] = (long) part;
8250 ev->data.l[2] = (long) 0;
8251 ev->data.l[3] = (long) portion;
8252 ev->data.l[4] = (long) whole;
8253
8254 /* Make Xt timeouts work while the scroll bar is active. */
8255 toolkit_scroll_bar_interaction = 1;
8256
8257 /* Setting the event mask to zero means that the message will
8258 be sent to the client that created the window, and if that
8259 window no longer exists, no event will be sent. */
8260 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
8261 UNBLOCK_INPUT;
8262 }
8263
8264
8265 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
8266 in *IEVENT. */
8267
8268 static void
8269 x_scroll_bar_to_input_event (event, ievent)
8270 XEvent *event;
8271 struct input_event *ievent;
8272 {
8273 XClientMessageEvent *ev = (XClientMessageEvent *) event;
8274 Lisp_Object window;
8275 struct frame *f;
8276 struct window *w;
8277
8278 w = scroll_bar_windows[ev->data.l[0]];
8279 scroll_bar_windows[ev->data.l[0]] = NULL;
8280
8281 XSETWINDOW (window, w);
8282 f = XFRAME (w->frame);
8283
8284 ievent->kind = scroll_bar_click;
8285 ievent->frame_or_window = window;
8286 ievent->arg = Qnil;
8287 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
8288 ievent->part = ev->data.l[1];
8289 ievent->code = ev->data.l[2];
8290 ievent->x = make_number ((int) ev->data.l[3]);
8291 ievent->y = make_number ((int) ev->data.l[4]);
8292 ievent->modifiers = 0;
8293 }
8294
8295
8296 #ifdef USE_MOTIF
8297
8298 /* Minimum and maximum values used for Motif scroll bars. */
8299
8300 #define XM_SB_MIN 1
8301 #define XM_SB_MAX 10000000
8302 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
8303
8304
8305 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
8306 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
8307 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
8308
8309 static void
8310 xm_scroll_callback (widget, client_data, call_data)
8311 Widget widget;
8312 XtPointer client_data, call_data;
8313 {
8314 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8315 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
8316 double percent;
8317 int part = -1, whole = 0, portion = 0;
8318
8319 switch (cs->reason)
8320 {
8321 case XmCR_DECREMENT:
8322 bar->dragging = Qnil;
8323 part = scroll_bar_up_arrow;
8324 break;
8325
8326 case XmCR_INCREMENT:
8327 bar->dragging = Qnil;
8328 part = scroll_bar_down_arrow;
8329 break;
8330
8331 case XmCR_PAGE_DECREMENT:
8332 bar->dragging = Qnil;
8333 part = scroll_bar_above_handle;
8334 break;
8335
8336 case XmCR_PAGE_INCREMENT:
8337 bar->dragging = Qnil;
8338 part = scroll_bar_below_handle;
8339 break;
8340
8341 case XmCR_TO_TOP:
8342 bar->dragging = Qnil;
8343 part = scroll_bar_to_top;
8344 break;
8345
8346 case XmCR_TO_BOTTOM:
8347 bar->dragging = Qnil;
8348 part = scroll_bar_to_bottom;
8349 break;
8350
8351 case XmCR_DRAG:
8352 {
8353 int slider_size;
8354 int dragging_down_p = (INTEGERP (bar->dragging)
8355 && XINT (bar->dragging) <= cs->value);
8356
8357 /* Get the slider size. */
8358 BLOCK_INPUT;
8359 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
8360 UNBLOCK_INPUT;
8361
8362 /* At the max position of the scroll bar, do a line-wise
8363 movement. Without doing anything, we would be called with
8364 the same cs->value again and again. If we want to make
8365 sure that we can reach the end of the buffer, we have to do
8366 something.
8367
8368 Implementation note: setting bar->dragging always to
8369 cs->value gives a smoother movement at the max position.
8370 Setting it to nil when doing line-wise movement gives
8371 a better slider behavior. */
8372
8373 if (cs->value + slider_size == XM_SB_MAX
8374 || (dragging_down_p
8375 && last_scroll_bar_part == scroll_bar_down_arrow))
8376 {
8377 part = scroll_bar_down_arrow;
8378 bar->dragging = Qnil;
8379 }
8380 else
8381 {
8382 whole = XM_SB_RANGE;
8383 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
8384 part = scroll_bar_handle;
8385 bar->dragging = make_number (cs->value);
8386 }
8387 }
8388 break;
8389
8390 case XmCR_VALUE_CHANGED:
8391 break;
8392 };
8393
8394 if (part >= 0)
8395 {
8396 window_being_scrolled = bar->window;
8397 last_scroll_bar_part = part;
8398 x_send_scroll_bar_event (bar->window, part, portion, whole);
8399 }
8400 }
8401
8402
8403 #else /* !USE_MOTIF, i.e. Xaw. */
8404
8405
8406 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
8407 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8408 scroll bar struct. CALL_DATA is a pointer to a float saying where
8409 the thumb is. */
8410
8411 static void
8412 xaw_jump_callback (widget, client_data, call_data)
8413 Widget widget;
8414 XtPointer client_data, call_data;
8415 {
8416 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8417 float top = *(float *) call_data;
8418 float shown;
8419 int whole, portion, height;
8420 int part;
8421
8422 /* Get the size of the thumb, a value between 0 and 1. */
8423 BLOCK_INPUT;
8424 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
8425 UNBLOCK_INPUT;
8426
8427 whole = 10000000;
8428 portion = shown < 1 ? top * whole : 0;
8429
8430 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8431 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8432 the bottom, so we force the scrolling whenever we see that we're
8433 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8434 we try to ensure that we always stay two pixels away from the
8435 bottom). */
8436 part = scroll_bar_down_arrow;
8437 else
8438 part = scroll_bar_handle;
8439
8440 window_being_scrolled = bar->window;
8441 bar->dragging = make_number (portion);
8442 last_scroll_bar_part = part;
8443 x_send_scroll_bar_event (bar->window, part, portion, whole);
8444 }
8445
8446
8447 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
8448 i.e. line or page up or down. WIDGET is the Xaw scroll bar
8449 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8450 the scroll bar. CALL_DATA is an integer specifying the action that
8451 has taken place. It's magnitude is in the range 0..height of the
8452 scroll bar. Negative values mean scroll towards buffer start.
8453 Values < height of scroll bar mean line-wise movement. */
8454
8455 static void
8456 xaw_scroll_callback (widget, client_data, call_data)
8457 Widget widget;
8458 XtPointer client_data, call_data;
8459 {
8460 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8461 int position = (int) call_data;
8462 Dimension height;
8463 int part;
8464
8465 /* Get the height of the scroll bar. */
8466 BLOCK_INPUT;
8467 XtVaGetValues (widget, XtNheight, &height, NULL);
8468 UNBLOCK_INPUT;
8469
8470 if (abs (position) >= height)
8471 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8472
8473 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8474 it maps line-movement to call_data = max(5, height/20). */
8475 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8476 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
8477 else
8478 part = scroll_bar_move_ratio;
8479
8480 window_being_scrolled = bar->window;
8481 bar->dragging = Qnil;
8482 last_scroll_bar_part = part;
8483 x_send_scroll_bar_event (bar->window, part, position, height);
8484 }
8485
8486
8487 #endif /* not USE_MOTIF */
8488
8489
8490 /* Create the widget for scroll bar BAR on frame F. Record the widget
8491 and X window of the scroll bar in BAR. */
8492
8493 static void
8494 x_create_toolkit_scroll_bar (f, bar)
8495 struct frame *f;
8496 struct scroll_bar *bar;
8497 {
8498 Window xwindow;
8499 Widget widget;
8500 Arg av[20];
8501 int ac = 0;
8502 char *scroll_bar_name = "verticalScrollBar";
8503 unsigned long pixel;
8504
8505 BLOCK_INPUT;
8506
8507 #ifdef USE_MOTIF
8508 /* Set resources. Create the widget. */
8509 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8510 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8511 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8512 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8513 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8514 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8515 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8516
8517 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8518 if (pixel != -1)
8519 {
8520 XtSetArg (av[ac], XmNforeground, pixel);
8521 ++ac;
8522 }
8523
8524 pixel = f->output_data.x->scroll_bar_background_pixel;
8525 if (pixel != -1)
8526 {
8527 XtSetArg (av[ac], XmNbackground, pixel);
8528 ++ac;
8529 }
8530
8531 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8532 scroll_bar_name, av, ac);
8533
8534 /* Add one callback for everything that can happen. */
8535 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8536 (XtPointer) bar);
8537 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8538 (XtPointer) bar);
8539 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8540 (XtPointer) bar);
8541 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8542 (XtPointer) bar);
8543 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8544 (XtPointer) bar);
8545 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8546 (XtPointer) bar);
8547 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8548 (XtPointer) bar);
8549
8550 /* Realize the widget. Only after that is the X window created. */
8551 XtRealizeWidget (widget);
8552
8553 /* Set the cursor to an arrow. I didn't find a resource to do that.
8554 And I'm wondering why it hasn't an arrow cursor by default. */
8555 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8556 f->output_data.x->nontext_cursor);
8557
8558 #else /* !USE_MOTIF i.e. use Xaw */
8559
8560 /* Set resources. Create the widget. The background of the
8561 Xaw3d scroll bar widget is a little bit light for my taste.
8562 We don't alter it here to let users change it according
8563 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8564 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8565 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
8566 /* For smoother scrolling with Xaw3d -sm */
8567 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8568 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
8569
8570 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8571 if (pixel != -1)
8572 {
8573 XtSetArg (av[ac], XtNforeground, pixel);
8574 ++ac;
8575 }
8576
8577 pixel = f->output_data.x->scroll_bar_background_pixel;
8578 if (pixel != -1)
8579 {
8580 XtSetArg (av[ac], XtNbackground, pixel);
8581 ++ac;
8582 }
8583
8584 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8585 f->output_data.x->edit_widget, av, ac);
8586
8587 {
8588 char *initial = "";
8589 char *val = initial;
8590 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8591 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8592 if (val == initial)
8593 { /* ARROW_SCROLL */
8594 xaw3d_arrow_scroll = True;
8595 /* Isn't that just a personal preference ? -sm */
8596 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8597 }
8598 }
8599
8600 /* Define callbacks. */
8601 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8602 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
8603 (XtPointer) bar);
8604
8605 /* Realize the widget. Only after that is the X window created. */
8606 XtRealizeWidget (widget);
8607
8608 #endif /* !USE_MOTIF */
8609
8610 /* Install an action hook that let's us detect when the user
8611 finishes interacting with a scroll bar. */
8612 if (action_hook_id == 0)
8613 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8614
8615 /* Remember X window and widget in the scroll bar vector. */
8616 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8617 xwindow = XtWindow (widget);
8618 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8619
8620 UNBLOCK_INPUT;
8621 }
8622
8623
8624 /* Set the thumb size and position of scroll bar BAR. We are currently
8625 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8626
8627 static void
8628 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8629 struct scroll_bar *bar;
8630 int portion, position, whole;
8631 {
8632 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8633 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8634 float top, shown;
8635
8636 if (whole == 0)
8637 top = 0, shown = 1;
8638 else
8639 {
8640 top = (float) position / whole;
8641 shown = (float) portion / whole;
8642 }
8643
8644 BLOCK_INPUT;
8645
8646 #ifdef USE_MOTIF
8647 {
8648 int size, value;
8649 XmScrollBarWidget sb;
8650
8651 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
8652 is the scroll bar's maximum and MIN is the scroll bar's minimum
8653 value. */
8654 size = shown * XM_SB_RANGE;
8655 size = min (size, XM_SB_RANGE);
8656 size = max (size, 1);
8657
8658 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8659 value = top * XM_SB_RANGE;
8660 value = min (value, XM_SB_MAX - size);
8661 value = max (value, XM_SB_MIN);
8662
8663 if (NILP (bar->dragging))
8664 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8665 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8666 /* This has the negative side effect that the slider value is
8667 not what it would be if we scrolled here using line-wise or
8668 page-wise movement. */
8669 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8670 else
8671 {
8672 /* If currently dragging, only update the slider size.
8673 This reduces flicker effects. */
8674 int old_value, old_size, increment, page_increment;
8675
8676 XmScrollBarGetValues (widget, &old_value, &old_size,
8677 &increment, &page_increment);
8678 XmScrollBarSetValues (widget, old_value,
8679 min (size, XM_SB_RANGE - old_value),
8680 0, 0, False);
8681 }
8682 }
8683 #else /* !USE_MOTIF i.e. use Xaw */
8684 {
8685 float old_top, old_shown;
8686 Dimension height;
8687 XtVaGetValues (widget,
8688 XtNtopOfThumb, &old_top,
8689 XtNshown, &old_shown,
8690 XtNheight, &height,
8691 NULL);
8692
8693 /* Massage the top+shown values. */
8694 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8695 top = max (0, min (1, top));
8696 else
8697 top = old_top;
8698 /* Keep two pixels available for moving the thumb down. */
8699 shown = max (0, min (1 - top - (2.0 / height), shown));
8700
8701 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8702 check that your system's configuration file contains a define
8703 for `NARROWPROTO'. See s/freebsd.h for an example. */
8704 if (top != old_top || shown != old_shown)
8705 {
8706 if (NILP (bar->dragging))
8707 XawScrollbarSetThumb (widget, top, shown);
8708 else
8709 {
8710 #ifdef HAVE_XAW3D
8711 ScrollbarWidget sb = (ScrollbarWidget) widget;
8712 int scroll_mode = 0;
8713
8714 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8715 if (xaw3d_arrow_scroll)
8716 {
8717 /* Xaw3d stupidly ignores resize requests while dragging
8718 so we have to make it believe it's not in dragging mode. */
8719 scroll_mode = sb->scrollbar.scroll_mode;
8720 if (scroll_mode == 2)
8721 sb->scrollbar.scroll_mode = 0;
8722 }
8723 #endif
8724 /* Try to make the scrolling a tad smoother. */
8725 if (!xaw3d_pick_top)
8726 shown = min (shown, old_shown);
8727
8728 XawScrollbarSetThumb (widget, top, shown);
8729
8730 #ifdef HAVE_XAW3D
8731 if (xaw3d_arrow_scroll && scroll_mode == 2)
8732 sb->scrollbar.scroll_mode = scroll_mode;
8733 #endif
8734 }
8735 }
8736 }
8737 #endif /* !USE_MOTIF */
8738
8739 UNBLOCK_INPUT;
8740 }
8741
8742 #endif /* USE_TOOLKIT_SCROLL_BARS */
8743
8744
8745 \f
8746 /************************************************************************
8747 Scroll bars, general
8748 ************************************************************************/
8749
8750 /* Create a scroll bar and return the scroll bar vector for it. W is
8751 the Emacs window on which to create the scroll bar. TOP, LEFT,
8752 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8753 scroll bar. */
8754
8755 static struct scroll_bar *
8756 x_scroll_bar_create (w, top, left, width, height)
8757 struct window *w;
8758 int top, left, width, height;
8759 {
8760 struct frame *f = XFRAME (w->frame);
8761 struct scroll_bar *bar
8762 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8763
8764 BLOCK_INPUT;
8765
8766 #ifdef USE_TOOLKIT_SCROLL_BARS
8767 x_create_toolkit_scroll_bar (f, bar);
8768 #else /* not USE_TOOLKIT_SCROLL_BARS */
8769 {
8770 XSetWindowAttributes a;
8771 unsigned long mask;
8772 Window window;
8773
8774 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8775 if (a.background_pixel == -1)
8776 a.background_pixel = f->output_data.x->background_pixel;
8777
8778 a.event_mask = (ButtonPressMask | ButtonReleaseMask
8779 | ButtonMotionMask | PointerMotionHintMask
8780 | ExposureMask);
8781 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
8782
8783 mask = (CWBackPixel | CWEventMask | CWCursor);
8784
8785 /* Clear the area of W that will serve as a scroll bar. This is
8786 for the case that a window has been split horizontally. In
8787 this case, no clear_frame is generated to reduce flickering. */
8788 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8789 left, top, width,
8790 window_box_height (w), False);
8791
8792 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8793 /* Position and size of scroll bar. */
8794 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8795 top,
8796 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8797 height,
8798 /* Border width, depth, class, and visual. */
8799 0,
8800 CopyFromParent,
8801 CopyFromParent,
8802 CopyFromParent,
8803 /* Attributes. */
8804 mask, &a);
8805 SET_SCROLL_BAR_X_WINDOW (bar, window);
8806 }
8807 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8808
8809 XSETWINDOW (bar->window, w);
8810 XSETINT (bar->top, top);
8811 XSETINT (bar->left, left);
8812 XSETINT (bar->width, width);
8813 XSETINT (bar->height, height);
8814 XSETINT (bar->start, 0);
8815 XSETINT (bar->end, 0);
8816 bar->dragging = Qnil;
8817
8818 /* Add bar to its frame's list of scroll bars. */
8819 bar->next = FRAME_SCROLL_BARS (f);
8820 bar->prev = Qnil;
8821 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8822 if (!NILP (bar->next))
8823 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8824
8825 /* Map the window/widget. */
8826 #ifdef USE_TOOLKIT_SCROLL_BARS
8827 {
8828 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8829 XtConfigureWidget (scroll_bar,
8830 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8831 top,
8832 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8833 max (height, 1), 0);
8834 XtMapWidget (scroll_bar);
8835 }
8836 #else /* not USE_TOOLKIT_SCROLL_BARS */
8837 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8838 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8839
8840 UNBLOCK_INPUT;
8841 return bar;
8842 }
8843
8844
8845 /* Draw BAR's handle in the proper position.
8846
8847 If the handle is already drawn from START to END, don't bother
8848 redrawing it, unless REBUILD is non-zero; in that case, always
8849 redraw it. (REBUILD is handy for drawing the handle after expose
8850 events.)
8851
8852 Normally, we want to constrain the start and end of the handle to
8853 fit inside its rectangle, but if the user is dragging the scroll
8854 bar handle, we want to let them drag it down all the way, so that
8855 the bar's top is as far down as it goes; otherwise, there's no way
8856 to move to the very end of the buffer. */
8857
8858 #ifndef USE_TOOLKIT_SCROLL_BARS
8859
8860 static void
8861 x_scroll_bar_set_handle (bar, start, end, rebuild)
8862 struct scroll_bar *bar;
8863 int start, end;
8864 int rebuild;
8865 {
8866 int dragging = ! NILP (bar->dragging);
8867 Window w = SCROLL_BAR_X_WINDOW (bar);
8868 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8869 GC gc = f->output_data.x->normal_gc;
8870
8871 /* If the display is already accurate, do nothing. */
8872 if (! rebuild
8873 && start == XINT (bar->start)
8874 && end == XINT (bar->end))
8875 return;
8876
8877 BLOCK_INPUT;
8878
8879 {
8880 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8881 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8882 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8883
8884 /* Make sure the values are reasonable, and try to preserve
8885 the distance between start and end. */
8886 {
8887 int length = end - start;
8888
8889 if (start < 0)
8890 start = 0;
8891 else if (start > top_range)
8892 start = top_range;
8893 end = start + length;
8894
8895 if (end < start)
8896 end = start;
8897 else if (end > top_range && ! dragging)
8898 end = top_range;
8899 }
8900
8901 /* Store the adjusted setting in the scroll bar. */
8902 XSETINT (bar->start, start);
8903 XSETINT (bar->end, end);
8904
8905 /* Clip the end position, just for display. */
8906 if (end > top_range)
8907 end = top_range;
8908
8909 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
8910 below top positions, to make sure the handle is always at least
8911 that many pixels tall. */
8912 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
8913
8914 /* Draw the empty space above the handle. Note that we can't clear
8915 zero-height areas; that means "clear to end of window." */
8916 if (0 < start)
8917 x_clear_area (FRAME_X_DISPLAY (f), w,
8918 /* x, y, width, height, and exposures. */
8919 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8920 VERTICAL_SCROLL_BAR_TOP_BORDER,
8921 inside_width, start,
8922 False);
8923
8924 /* Change to proper foreground color if one is specified. */
8925 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8926 XSetForeground (FRAME_X_DISPLAY (f), gc,
8927 f->output_data.x->scroll_bar_foreground_pixel);
8928
8929 /* Draw the handle itself. */
8930 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
8931 /* x, y, width, height */
8932 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8933 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
8934 inside_width, end - start);
8935
8936 /* Restore the foreground color of the GC if we changed it above. */
8937 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8938 XSetForeground (FRAME_X_DISPLAY (f), gc,
8939 f->output_data.x->foreground_pixel);
8940
8941 /* Draw the empty space below the handle. Note that we can't
8942 clear zero-height areas; that means "clear to end of window." */
8943 if (end < inside_height)
8944 x_clear_area (FRAME_X_DISPLAY (f), w,
8945 /* x, y, width, height, and exposures. */
8946 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8947 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
8948 inside_width, inside_height - end,
8949 False);
8950
8951 }
8952
8953 UNBLOCK_INPUT;
8954 }
8955
8956 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8957
8958 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8959 nil. */
8960
8961 static void
8962 x_scroll_bar_remove (bar)
8963 struct scroll_bar *bar;
8964 {
8965 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8966 BLOCK_INPUT;
8967
8968 #ifdef USE_TOOLKIT_SCROLL_BARS
8969 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
8970 #else
8971 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8972 #endif
8973
8974 /* Disassociate this scroll bar from its window. */
8975 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
8976
8977 UNBLOCK_INPUT;
8978 }
8979
8980
8981 /* Set the handle of the vertical scroll bar for WINDOW to indicate
8982 that we are displaying PORTION characters out of a total of WHOLE
8983 characters, starting at POSITION. If WINDOW has no scroll bar,
8984 create one. */
8985
8986 static void
8987 XTset_vertical_scroll_bar (w, portion, whole, position)
8988 struct window *w;
8989 int portion, whole, position;
8990 {
8991 struct frame *f = XFRAME (w->frame);
8992 struct scroll_bar *bar;
8993 int top, height, left, sb_left, width, sb_width;
8994 int window_x, window_y, window_width, window_height;
8995
8996 /* Get window dimensions. */
8997 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8998 top = window_y;
8999 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9000 height = window_height;
9001
9002 /* Compute the left edge of the scroll bar area. */
9003 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9004 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
9005 else
9006 left = XFASTINT (w->left);
9007 left *= CANON_X_UNIT (f);
9008 left += FRAME_INTERNAL_BORDER_WIDTH (f);
9009
9010 /* Compute the width of the scroll bar which might be less than
9011 the width of the area reserved for the scroll bar. */
9012 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
9013 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
9014 else
9015 sb_width = width;
9016
9017 /* Compute the left edge of the scroll bar. */
9018 #ifdef USE_TOOLKIT_SCROLL_BARS
9019 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9020 sb_left = left + width - sb_width - (width - sb_width) / 2;
9021 else
9022 sb_left = left + (width - sb_width) / 2;
9023 #else
9024 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9025 sb_left = left + width - sb_width;
9026 else
9027 sb_left = left;
9028 #endif
9029
9030 /* Does the scroll bar exist yet? */
9031 if (NILP (w->vertical_scroll_bar))
9032 {
9033 BLOCK_INPUT;
9034 if (width && height)
9035 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9036 left, top, width, height, False);
9037 UNBLOCK_INPUT;
9038 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
9039 }
9040 else
9041 {
9042 /* It may just need to be moved and resized. */
9043 unsigned int mask = 0;
9044
9045 bar = XSCROLL_BAR (w->vertical_scroll_bar);
9046
9047 BLOCK_INPUT;
9048
9049 if (sb_left != XINT (bar->left))
9050 mask |= CWX;
9051 if (top != XINT (bar->top))
9052 mask |= CWY;
9053 if (sb_width != XINT (bar->width))
9054 mask |= CWWidth;
9055 if (height != XINT (bar->height))
9056 mask |= CWHeight;
9057
9058 #ifdef USE_TOOLKIT_SCROLL_BARS
9059
9060 /* Since toolkit scroll bars are smaller than the space reserved
9061 for them on the frame, we have to clear "under" them. */
9062 if (width && height)
9063 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9064 left, top, width, height, False);
9065
9066 /* Move/size the scroll bar widget. */
9067 if (mask)
9068 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
9069 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9070 top,
9071 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9072 max (height, 1), 0);
9073
9074 #else /* not USE_TOOLKIT_SCROLL_BARS */
9075
9076 /* Clear areas not covered by the scroll bar because of
9077 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
9078 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
9079 {
9080 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9081 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9082 height, False);
9083 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9084 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9085 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9086 height, False);
9087 }
9088
9089 /* Clear areas not covered by the scroll bar because it's not as
9090 wide as the area reserved for it . This makes sure a
9091 previous mode line display is cleared after C-x 2 C-x 1, for
9092 example. */
9093 {
9094 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9095 int rest = area_width - sb_width;
9096 if (rest > 0)
9097 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9098 left + area_width - rest, 0,
9099 rest, max (height, 1), False);
9100 }
9101
9102 /* Move/size the scroll bar window. */
9103 if (mask)
9104 {
9105 XWindowChanges wc;
9106
9107 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9108 wc.y = top;
9109 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
9110 wc.height = height;
9111 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
9112 mask, &wc);
9113 }
9114
9115 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9116
9117 /* Remember new settings. */
9118 XSETINT (bar->left, sb_left);
9119 XSETINT (bar->top, top);
9120 XSETINT (bar->width, sb_width);
9121 XSETINT (bar->height, height);
9122
9123 UNBLOCK_INPUT;
9124 }
9125
9126 #ifdef USE_TOOLKIT_SCROLL_BARS
9127 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
9128 #else /* not USE_TOOLKIT_SCROLL_BARS */
9129 /* Set the scroll bar's current state, unless we're currently being
9130 dragged. */
9131 if (NILP (bar->dragging))
9132 {
9133 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
9134
9135 if (whole == 0)
9136 x_scroll_bar_set_handle (bar, 0, top_range, 0);
9137 else
9138 {
9139 int start = ((double) position * top_range) / whole;
9140 int end = ((double) (position + portion) * top_range) / whole;
9141 x_scroll_bar_set_handle (bar, start, end, 0);
9142 }
9143 }
9144 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9145
9146 XSETVECTOR (w->vertical_scroll_bar, bar);
9147 }
9148
9149
9150 /* The following three hooks are used when we're doing a thorough
9151 redisplay of the frame. We don't explicitly know which scroll bars
9152 are going to be deleted, because keeping track of when windows go
9153 away is a real pain - "Can you say set-window-configuration, boys
9154 and girls?" Instead, we just assert at the beginning of redisplay
9155 that *all* scroll bars are to be removed, and then save a scroll bar
9156 from the fiery pit when we actually redisplay its window. */
9157
9158 /* Arrange for all scroll bars on FRAME to be removed at the next call
9159 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
9160 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
9161
9162 static void
9163 XTcondemn_scroll_bars (frame)
9164 FRAME_PTR frame;
9165 {
9166 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
9167 while (! NILP (FRAME_SCROLL_BARS (frame)))
9168 {
9169 Lisp_Object bar;
9170 bar = FRAME_SCROLL_BARS (frame);
9171 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
9172 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
9173 XSCROLL_BAR (bar)->prev = Qnil;
9174 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
9175 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
9176 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
9177 }
9178 }
9179
9180
9181 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
9182 Note that WINDOW isn't necessarily condemned at all. */
9183
9184 static void
9185 XTredeem_scroll_bar (window)
9186 struct window *window;
9187 {
9188 struct scroll_bar *bar;
9189 struct frame *f;
9190
9191 /* We can't redeem this window's scroll bar if it doesn't have one. */
9192 if (NILP (window->vertical_scroll_bar))
9193 abort ();
9194
9195 bar = XSCROLL_BAR (window->vertical_scroll_bar);
9196
9197 /* Unlink it from the condemned list. */
9198 f = XFRAME (WINDOW_FRAME (window));
9199 if (NILP (bar->prev))
9200 {
9201 /* If the prev pointer is nil, it must be the first in one of
9202 the lists. */
9203 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
9204 /* It's not condemned. Everything's fine. */
9205 return;
9206 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
9207 window->vertical_scroll_bar))
9208 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
9209 else
9210 /* If its prev pointer is nil, it must be at the front of
9211 one or the other! */
9212 abort ();
9213 }
9214 else
9215 XSCROLL_BAR (bar->prev)->next = bar->next;
9216
9217 if (! NILP (bar->next))
9218 XSCROLL_BAR (bar->next)->prev = bar->prev;
9219
9220 bar->next = FRAME_SCROLL_BARS (f);
9221 bar->prev = Qnil;
9222 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9223 if (! NILP (bar->next))
9224 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9225 }
9226
9227 /* Remove all scroll bars on FRAME that haven't been saved since the
9228 last call to `*condemn_scroll_bars_hook'. */
9229
9230 static void
9231 XTjudge_scroll_bars (f)
9232 FRAME_PTR f;
9233 {
9234 Lisp_Object bar, next;
9235
9236 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
9237
9238 /* Clear out the condemned list now so we won't try to process any
9239 more events on the hapless scroll bars. */
9240 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
9241
9242 for (; ! NILP (bar); bar = next)
9243 {
9244 struct scroll_bar *b = XSCROLL_BAR (bar);
9245
9246 x_scroll_bar_remove (b);
9247
9248 next = b->next;
9249 b->next = b->prev = Qnil;
9250 }
9251
9252 /* Now there should be no references to the condemned scroll bars,
9253 and they should get garbage-collected. */
9254 }
9255
9256
9257 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
9258 is a no-op when using toolkit scroll bars.
9259
9260 This may be called from a signal handler, so we have to ignore GC
9261 mark bits. */
9262
9263 static void
9264 x_scroll_bar_expose (bar, event)
9265 struct scroll_bar *bar;
9266 XEvent *event;
9267 {
9268 #ifndef USE_TOOLKIT_SCROLL_BARS
9269
9270 Window w = SCROLL_BAR_X_WINDOW (bar);
9271 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9272 GC gc = f->output_data.x->normal_gc;
9273 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9274
9275 BLOCK_INPUT;
9276
9277 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
9278
9279 /* Draw a one-pixel border just inside the edges of the scroll bar. */
9280 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
9281
9282 /* x, y, width, height */
9283 0, 0,
9284 XINT (bar->width) - 1 - width_trim - width_trim,
9285 XINT (bar->height) - 1);
9286
9287 UNBLOCK_INPUT;
9288
9289 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9290 }
9291
9292 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
9293 is set to something other than no_event, it is enqueued.
9294
9295 This may be called from a signal handler, so we have to ignore GC
9296 mark bits. */
9297
9298 #ifndef USE_TOOLKIT_SCROLL_BARS
9299
9300 static void
9301 x_scroll_bar_handle_click (bar, event, emacs_event)
9302 struct scroll_bar *bar;
9303 XEvent *event;
9304 struct input_event *emacs_event;
9305 {
9306 if (! GC_WINDOWP (bar->window))
9307 abort ();
9308
9309 emacs_event->kind = scroll_bar_click;
9310 emacs_event->code = event->xbutton.button - Button1;
9311 emacs_event->modifiers
9312 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
9313 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
9314 event->xbutton.state)
9315 | (event->type == ButtonRelease
9316 ? up_modifier
9317 : down_modifier));
9318 emacs_event->frame_or_window = bar->window;
9319 emacs_event->arg = Qnil;
9320 emacs_event->timestamp = event->xbutton.time;
9321 {
9322 #if 0
9323 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9324 int internal_height
9325 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9326 #endif
9327 int top_range
9328 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9329 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
9330
9331 if (y < 0) y = 0;
9332 if (y > top_range) y = top_range;
9333
9334 if (y < XINT (bar->start))
9335 emacs_event->part = scroll_bar_above_handle;
9336 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9337 emacs_event->part = scroll_bar_handle;
9338 else
9339 emacs_event->part = scroll_bar_below_handle;
9340
9341 /* Just because the user has clicked on the handle doesn't mean
9342 they want to drag it. Lisp code needs to be able to decide
9343 whether or not we're dragging. */
9344 #if 0
9345 /* If the user has just clicked on the handle, record where they're
9346 holding it. */
9347 if (event->type == ButtonPress
9348 && emacs_event->part == scroll_bar_handle)
9349 XSETINT (bar->dragging, y - XINT (bar->start));
9350 #endif
9351
9352 /* If the user has released the handle, set it to its final position. */
9353 if (event->type == ButtonRelease
9354 && ! NILP (bar->dragging))
9355 {
9356 int new_start = y - XINT (bar->dragging);
9357 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9358
9359 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9360 bar->dragging = Qnil;
9361 }
9362
9363 /* Same deal here as the other #if 0. */
9364 #if 0
9365 /* Clicks on the handle are always reported as occurring at the top of
9366 the handle. */
9367 if (emacs_event->part == scroll_bar_handle)
9368 emacs_event->x = bar->start;
9369 else
9370 XSETINT (emacs_event->x, y);
9371 #else
9372 XSETINT (emacs_event->x, y);
9373 #endif
9374
9375 XSETINT (emacs_event->y, top_range);
9376 }
9377 }
9378
9379 /* Handle some mouse motion while someone is dragging the scroll bar.
9380
9381 This may be called from a signal handler, so we have to ignore GC
9382 mark bits. */
9383
9384 static void
9385 x_scroll_bar_note_movement (bar, event)
9386 struct scroll_bar *bar;
9387 XEvent *event;
9388 {
9389 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9390
9391 last_mouse_movement_time = event->xmotion.time;
9392
9393 f->mouse_moved = 1;
9394 XSETVECTOR (last_mouse_scroll_bar, bar);
9395
9396 /* If we're dragging the bar, display it. */
9397 if (! GC_NILP (bar->dragging))
9398 {
9399 /* Where should the handle be now? */
9400 int new_start = event->xmotion.y - XINT (bar->dragging);
9401
9402 if (new_start != XINT (bar->start))
9403 {
9404 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9405
9406 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9407 }
9408 }
9409 }
9410
9411 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9412
9413 /* Return information to the user about the current position of the mouse
9414 on the scroll bar. */
9415
9416 static void
9417 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9418 FRAME_PTR *fp;
9419 Lisp_Object *bar_window;
9420 enum scroll_bar_part *part;
9421 Lisp_Object *x, *y;
9422 unsigned long *time;
9423 {
9424 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
9425 Window w = SCROLL_BAR_X_WINDOW (bar);
9426 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9427 int win_x, win_y;
9428 Window dummy_window;
9429 int dummy_coord;
9430 unsigned int dummy_mask;
9431
9432 BLOCK_INPUT;
9433
9434 /* Get the mouse's position relative to the scroll bar window, and
9435 report that. */
9436 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
9437
9438 /* Root, child, root x and root y. */
9439 &dummy_window, &dummy_window,
9440 &dummy_coord, &dummy_coord,
9441
9442 /* Position relative to scroll bar. */
9443 &win_x, &win_y,
9444
9445 /* Mouse buttons and modifier keys. */
9446 &dummy_mask))
9447 ;
9448 else
9449 {
9450 #if 0
9451 int inside_height
9452 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9453 #endif
9454 int top_range
9455 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9456
9457 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9458
9459 if (! NILP (bar->dragging))
9460 win_y -= XINT (bar->dragging);
9461
9462 if (win_y < 0)
9463 win_y = 0;
9464 if (win_y > top_range)
9465 win_y = top_range;
9466
9467 *fp = f;
9468 *bar_window = bar->window;
9469
9470 if (! NILP (bar->dragging))
9471 *part = scroll_bar_handle;
9472 else if (win_y < XINT (bar->start))
9473 *part = scroll_bar_above_handle;
9474 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9475 *part = scroll_bar_handle;
9476 else
9477 *part = scroll_bar_below_handle;
9478
9479 XSETINT (*x, win_y);
9480 XSETINT (*y, top_range);
9481
9482 f->mouse_moved = 0;
9483 last_mouse_scroll_bar = Qnil;
9484 }
9485
9486 *time = last_mouse_movement_time;
9487
9488 UNBLOCK_INPUT;
9489 }
9490
9491
9492 /* The screen has been cleared so we may have changed foreground or
9493 background colors, and the scroll bars may need to be redrawn.
9494 Clear out the scroll bars, and ask for expose events, so we can
9495 redraw them. */
9496
9497 void
9498 x_scroll_bar_clear (f)
9499 FRAME_PTR f;
9500 {
9501 #ifndef USE_TOOLKIT_SCROLL_BARS
9502 Lisp_Object bar;
9503
9504 /* We can have scroll bars even if this is 0,
9505 if we just turned off scroll bar mode.
9506 But in that case we should not clear them. */
9507 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9508 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9509 bar = XSCROLL_BAR (bar)->next)
9510 XClearArea (FRAME_X_DISPLAY (f),
9511 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9512 0, 0, 0, 0, True);
9513 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9514 }
9515
9516 /* This processes Expose events from the menu-bar specific X event
9517 loop in xmenu.c. This allows to redisplay the frame if necessary
9518 when handling menu-bar or pop-up items. */
9519
9520 int
9521 process_expose_from_menu (event)
9522 XEvent event;
9523 {
9524 FRAME_PTR f;
9525 struct x_display_info *dpyinfo;
9526 int frame_exposed_p = 0;
9527
9528 BLOCK_INPUT;
9529
9530 dpyinfo = x_display_info_for_display (event.xexpose.display);
9531 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9532 if (f)
9533 {
9534 if (f->async_visible == 0)
9535 {
9536 f->async_visible = 1;
9537 f->async_iconified = 0;
9538 f->output_data.x->has_been_visible = 1;
9539 SET_FRAME_GARBAGED (f);
9540 }
9541 else
9542 {
9543 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9544 event.xexpose.x, event.xexpose.y,
9545 event.xexpose.width, event.xexpose.height);
9546 frame_exposed_p = 1;
9547 }
9548 }
9549 else
9550 {
9551 struct scroll_bar *bar
9552 = x_window_to_scroll_bar (event.xexpose.window);
9553
9554 if (bar)
9555 x_scroll_bar_expose (bar, &event);
9556 }
9557
9558 UNBLOCK_INPUT;
9559 return frame_exposed_p;
9560 }
9561 \f
9562 /* Define a queue to save up SelectionRequest events for later handling. */
9563
9564 struct selection_event_queue
9565 {
9566 XEvent event;
9567 struct selection_event_queue *next;
9568 };
9569
9570 static struct selection_event_queue *queue;
9571
9572 /* Nonzero means queue up certain events--don't process them yet. */
9573
9574 static int x_queue_selection_requests;
9575
9576 /* Queue up an X event *EVENT, to be processed later. */
9577
9578 static void
9579 x_queue_event (f, event)
9580 FRAME_PTR f;
9581 XEvent *event;
9582 {
9583 struct selection_event_queue *queue_tmp
9584 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
9585
9586 if (queue_tmp != NULL)
9587 {
9588 queue_tmp->event = *event;
9589 queue_tmp->next = queue;
9590 queue = queue_tmp;
9591 }
9592 }
9593
9594 /* Take all the queued events and put them back
9595 so that they get processed afresh. */
9596
9597 static void
9598 x_unqueue_events (display)
9599 Display *display;
9600 {
9601 while (queue != NULL)
9602 {
9603 struct selection_event_queue *queue_tmp = queue;
9604 XPutBackEvent (display, &queue_tmp->event);
9605 queue = queue_tmp->next;
9606 xfree ((char *)queue_tmp);
9607 }
9608 }
9609
9610 /* Start queuing SelectionRequest events. */
9611
9612 void
9613 x_start_queuing_selection_requests (display)
9614 Display *display;
9615 {
9616 x_queue_selection_requests++;
9617 }
9618
9619 /* Stop queuing SelectionRequest events. */
9620
9621 void
9622 x_stop_queuing_selection_requests (display)
9623 Display *display;
9624 {
9625 x_queue_selection_requests--;
9626 x_unqueue_events (display);
9627 }
9628 \f
9629 /* The main X event-reading loop - XTread_socket. */
9630
9631 /* Time stamp of enter window event. This is only used by XTread_socket,
9632 but we have to put it out here, since static variables within functions
9633 sometimes don't work. */
9634
9635 static Time enter_timestamp;
9636
9637 /* This holds the state XLookupString needs to implement dead keys
9638 and other tricks known as "compose processing". _X Window System_
9639 says that a portable program can't use this, but Stephen Gildea assures
9640 me that letting the compiler initialize it to zeros will work okay.
9641
9642 This must be defined outside of XTread_socket, for the same reasons
9643 given for enter_time stamp, above. */
9644
9645 static XComposeStatus compose_status;
9646
9647 /* Record the last 100 characters stored
9648 to help debug the loss-of-chars-during-GC problem. */
9649
9650 static int temp_index;
9651 static short temp_buffer[100];
9652
9653 /* Set this to nonzero to fake an "X I/O error"
9654 on a particular display. */
9655
9656 struct x_display_info *XTread_socket_fake_io_error;
9657
9658 /* When we find no input here, we occasionally do a no-op command
9659 to verify that the X server is still running and we can still talk with it.
9660 We try all the open displays, one by one.
9661 This variable is used for cycling thru the displays. */
9662
9663 static struct x_display_info *next_noop_dpyinfo;
9664
9665 #define SET_SAVED_MENU_EVENT(size) \
9666 do \
9667 { \
9668 if (f->output_data.x->saved_menu_event == 0) \
9669 f->output_data.x->saved_menu_event \
9670 = (XEvent *) xmalloc (sizeof (XEvent)); \
9671 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9672 if (numchars >= 1) \
9673 { \
9674 bufp->kind = menu_bar_activate_event; \
9675 XSETFRAME (bufp->frame_or_window, f); \
9676 bufp->arg = Qnil; \
9677 bufp++; \
9678 count++; \
9679 numchars--; \
9680 } \
9681 } \
9682 while (0)
9683
9684 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9685 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9686
9687 /* Read events coming from the X server.
9688 This routine is called by the SIGIO handler.
9689 We return as soon as there are no more events to be read.
9690
9691 Events representing keys are stored in buffer BUFP,
9692 which can hold up to NUMCHARS characters.
9693 We return the number of characters stored into the buffer,
9694 thus pretending to be `read'.
9695
9696 EXPECTED is nonzero if the caller knows input is available. */
9697
9698 int
9699 XTread_socket (sd, bufp, numchars, expected)
9700 register int sd;
9701 /* register */ struct input_event *bufp;
9702 /* register */ int numchars;
9703 int expected;
9704 {
9705 int count = 0;
9706 int nbytes = 0;
9707 XEvent event;
9708 struct frame *f;
9709 int event_found = 0;
9710 struct x_display_info *dpyinfo;
9711 struct coding_system coding;
9712
9713 if (interrupt_input_blocked)
9714 {
9715 interrupt_input_pending = 1;
9716 return -1;
9717 }
9718
9719 interrupt_input_pending = 0;
9720 BLOCK_INPUT;
9721
9722 /* So people can tell when we have read the available input. */
9723 input_signal_count++;
9724
9725 if (numchars <= 0)
9726 abort (); /* Don't think this happens. */
9727
9728 ++handling_signal;
9729
9730 /* The input should be decoded if it is from XIM. Currently the
9731 locale of XIM is the same as that of the system. So, we can use
9732 Vlocale_coding_system which is initialized properly at Emacs
9733 startup time. */
9734 setup_coding_system (Vlocale_coding_system, &coding);
9735 coding.src_multibyte = 0;
9736 coding.dst_multibyte = 1;
9737 /* The input is converted to events, thus we can't handle
9738 composition. Anyway, there's no XIM that gives us composition
9739 information. */
9740 coding.composing = COMPOSITION_DISABLED;
9741
9742 /* Find the display we are supposed to read input for.
9743 It's the one communicating on descriptor SD. */
9744 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9745 {
9746 #if 0 /* This ought to be unnecessary; let's verify it. */
9747 #ifdef FIOSNBIO
9748 /* If available, Xlib uses FIOSNBIO to make the socket
9749 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
9750 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
9751 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
9752 fcntl (dpyinfo->connection, F_SETFL, 0);
9753 #endif /* ! defined (FIOSNBIO) */
9754 #endif
9755
9756 #if 0 /* This code can't be made to work, with multiple displays,
9757 and appears not to be used on any system any more.
9758 Also keyboard.c doesn't turn O_NDELAY on and off
9759 for X connections. */
9760 #ifndef SIGIO
9761 #ifndef HAVE_SELECT
9762 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9763 {
9764 extern int read_alarm_should_throw;
9765 read_alarm_should_throw = 1;
9766 XPeekEvent (dpyinfo->display, &event);
9767 read_alarm_should_throw = 0;
9768 }
9769 #endif /* HAVE_SELECT */
9770 #endif /* SIGIO */
9771 #endif
9772
9773 /* For debugging, this gives a way to fake an I/O error. */
9774 if (dpyinfo == XTread_socket_fake_io_error)
9775 {
9776 XTread_socket_fake_io_error = 0;
9777 x_io_error_quitter (dpyinfo->display);
9778 }
9779
9780 while (XPending (dpyinfo->display))
9781 {
9782 XNextEvent (dpyinfo->display, &event);
9783
9784 #ifdef HAVE_X_I18N
9785 {
9786 /* Filter events for the current X input method.
9787 XFilterEvent returns non-zero if the input method has
9788 consumed the event. We pass the frame's X window to
9789 XFilterEvent because that's the one for which the IC
9790 was created. */
9791 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9792 event.xclient.window);
9793 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
9794 break;
9795 }
9796 #endif
9797 event_found = 1;
9798
9799 switch (event.type)
9800 {
9801 case ClientMessage:
9802 {
9803 if (event.xclient.message_type
9804 == dpyinfo->Xatom_wm_protocols
9805 && event.xclient.format == 32)
9806 {
9807 if (event.xclient.data.l[0]
9808 == dpyinfo->Xatom_wm_take_focus)
9809 {
9810 /* Use x_any_window_to_frame because this
9811 could be the shell widget window
9812 if the frame has no title bar. */
9813 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
9814 #ifdef HAVE_X_I18N
9815 /* Not quite sure this is needed -pd */
9816 if (f && FRAME_XIC (f))
9817 XSetICFocus (FRAME_XIC (f));
9818 #endif
9819 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9820 instructs the WM to set the input focus automatically for
9821 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9822 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9823 it has set the focus. So, XSetInputFocus below is not
9824 needed.
9825
9826 The call to XSetInputFocus below has also caused trouble. In
9827 cases where the XSetInputFocus done by the WM and the one
9828 below are temporally close (on a fast machine), the call
9829 below can generate additional FocusIn events which confuse
9830 Emacs. */
9831
9832 /* Since we set WM_TAKE_FOCUS, we must call
9833 XSetInputFocus explicitly. But not if f is null,
9834 since that might be an event for a deleted frame. */
9835 if (f)
9836 {
9837 Display *d = event.xclient.display;
9838 /* Catch and ignore errors, in case window has been
9839 iconified by a window manager such as GWM. */
9840 int count = x_catch_errors (d);
9841 XSetInputFocus (d, event.xclient.window,
9842 /* The ICCCM says this is
9843 the only valid choice. */
9844 RevertToParent,
9845 event.xclient.data.l[1]);
9846 /* This is needed to detect the error
9847 if there is an error. */
9848 XSync (d, False);
9849 x_uncatch_errors (d, count);
9850 }
9851 /* Not certain about handling scroll bars here */
9852 #endif /* 0 */
9853 }
9854 else if (event.xclient.data.l[0]
9855 == dpyinfo->Xatom_wm_save_yourself)
9856 {
9857 /* Save state modify the WM_COMMAND property to
9858 something which can reinstate us. This notifies
9859 the session manager, who's looking for such a
9860 PropertyNotify. Can restart processing when
9861 a keyboard or mouse event arrives. */
9862 if (numchars > 0)
9863 {
9864 f = x_top_window_to_frame (dpyinfo,
9865 event.xclient.window);
9866
9867 /* This is just so we only give real data once
9868 for a single Emacs process. */
9869 if (f == SELECTED_FRAME ())
9870 XSetCommand (FRAME_X_DISPLAY (f),
9871 event.xclient.window,
9872 initial_argv, initial_argc);
9873 else if (f)
9874 XSetCommand (FRAME_X_DISPLAY (f),
9875 event.xclient.window,
9876 0, 0);
9877 }
9878 }
9879 else if (event.xclient.data.l[0]
9880 == dpyinfo->Xatom_wm_delete_window)
9881 {
9882 struct frame *f
9883 = x_any_window_to_frame (dpyinfo,
9884 event.xclient.window);
9885
9886 if (f)
9887 {
9888 if (numchars == 0)
9889 abort ();
9890
9891 bufp->kind = delete_window_event;
9892 XSETFRAME (bufp->frame_or_window, f);
9893 bufp->arg = Qnil;
9894 bufp++;
9895
9896 count += 1;
9897 numchars -= 1;
9898 }
9899 }
9900 }
9901 else if (event.xclient.message_type
9902 == dpyinfo->Xatom_wm_configure_denied)
9903 {
9904 }
9905 else if (event.xclient.message_type
9906 == dpyinfo->Xatom_wm_window_moved)
9907 {
9908 int new_x, new_y;
9909 struct frame *f
9910 = x_window_to_frame (dpyinfo, event.xclient.window);
9911
9912 new_x = event.xclient.data.s[0];
9913 new_y = event.xclient.data.s[1];
9914
9915 if (f)
9916 {
9917 f->output_data.x->left_pos = new_x;
9918 f->output_data.x->top_pos = new_y;
9919 }
9920 }
9921 #ifdef HACK_EDITRES
9922 else if (event.xclient.message_type
9923 == dpyinfo->Xatom_editres)
9924 {
9925 struct frame *f
9926 = x_any_window_to_frame (dpyinfo, event.xclient.window);
9927 _XEditResCheckMessages (f->output_data.x->widget, NULL,
9928 &event, NULL);
9929 }
9930 #endif /* HACK_EDITRES */
9931 else if ((event.xclient.message_type
9932 == dpyinfo->Xatom_DONE)
9933 || (event.xclient.message_type
9934 == dpyinfo->Xatom_PAGE))
9935 {
9936 /* Ghostview job completed. Kill it. We could
9937 reply with "Next" if we received "Page", but we
9938 currently never do because we are interested in
9939 images, only, which should have 1 page. */
9940 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9941 struct frame *f
9942 = x_window_to_frame (dpyinfo, event.xclient.window);
9943 x_kill_gs_process (pixmap, f);
9944 expose_frame (f, 0, 0, 0, 0);
9945 }
9946 #ifdef USE_TOOLKIT_SCROLL_BARS
9947 /* Scroll bar callbacks send a ClientMessage from which
9948 we construct an input_event. */
9949 else if (event.xclient.message_type
9950 == dpyinfo->Xatom_Scrollbar)
9951 {
9952 x_scroll_bar_to_input_event (&event, bufp);
9953 ++bufp, ++count, --numchars;
9954 goto out;
9955 }
9956 #endif /* USE_TOOLKIT_SCROLL_BARS */
9957 else
9958 goto OTHER;
9959 }
9960 break;
9961
9962 case SelectionNotify:
9963 #ifdef USE_X_TOOLKIT
9964 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
9965 goto OTHER;
9966 #endif /* not USE_X_TOOLKIT */
9967 x_handle_selection_notify (&event.xselection);
9968 break;
9969
9970 case SelectionClear: /* Someone has grabbed ownership. */
9971 #ifdef USE_X_TOOLKIT
9972 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
9973 goto OTHER;
9974 #endif /* USE_X_TOOLKIT */
9975 {
9976 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
9977
9978 if (numchars == 0)
9979 abort ();
9980
9981 bufp->kind = selection_clear_event;
9982 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9983 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9984 SELECTION_EVENT_TIME (bufp) = eventp->time;
9985 bufp->frame_or_window = Qnil;
9986 bufp->arg = Qnil;
9987 bufp++;
9988
9989 count += 1;
9990 numchars -= 1;
9991 }
9992 break;
9993
9994 case SelectionRequest: /* Someone wants our selection. */
9995 #ifdef USE_X_TOOLKIT
9996 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
9997 goto OTHER;
9998 #endif /* USE_X_TOOLKIT */
9999 if (x_queue_selection_requests)
10000 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
10001 &event);
10002 else
10003 {
10004 XSelectionRequestEvent *eventp
10005 = (XSelectionRequestEvent *) &event;
10006
10007 if (numchars == 0)
10008 abort ();
10009
10010 bufp->kind = selection_request_event;
10011 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10012 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
10013 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10014 SELECTION_EVENT_TARGET (bufp) = eventp->target;
10015 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
10016 SELECTION_EVENT_TIME (bufp) = eventp->time;
10017 bufp->frame_or_window = Qnil;
10018 bufp->arg = Qnil;
10019 bufp++;
10020
10021 count += 1;
10022 numchars -= 1;
10023 }
10024 break;
10025
10026 case PropertyNotify:
10027 #if 0 /* This is plain wrong. In the case that we are waiting for a
10028 PropertyNotify used as an ACK in incremental selection
10029 transfer, the property will be on the receiver's window. */
10030 #if defined USE_X_TOOLKIT
10031 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
10032 goto OTHER;
10033 #endif
10034 #endif
10035 x_handle_property_notify (&event.xproperty);
10036 goto OTHER;
10037
10038 case ReparentNotify:
10039 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
10040 if (f)
10041 {
10042 int x, y;
10043 f->output_data.x->parent_desc = event.xreparent.parent;
10044 x_real_positions (f, &x, &y);
10045 f->output_data.x->left_pos = x;
10046 f->output_data.x->top_pos = y;
10047 }
10048 break;
10049
10050 case Expose:
10051 f = x_window_to_frame (dpyinfo, event.xexpose.window);
10052 if (f)
10053 {
10054 if (f->async_visible == 0)
10055 {
10056 f->async_visible = 1;
10057 f->async_iconified = 0;
10058 f->output_data.x->has_been_visible = 1;
10059 SET_FRAME_GARBAGED (f);
10060 }
10061 else
10062 expose_frame (x_window_to_frame (dpyinfo,
10063 event.xexpose.window),
10064 event.xexpose.x, event.xexpose.y,
10065 event.xexpose.width, event.xexpose.height);
10066 }
10067 else
10068 {
10069 #ifndef USE_TOOLKIT_SCROLL_BARS
10070 struct scroll_bar *bar;
10071 #endif
10072 #if defined USE_X_TOOLKIT && defined USE_LUCID
10073 /* Submenus of the Lucid menu bar aren't widgets
10074 themselves, so there's no way to dispatch events
10075 to them. Recognize this case separately. */
10076 {
10077 Widget widget
10078 = x_window_to_menu_bar (event.xexpose.window);
10079 if (widget)
10080 xlwmenu_redisplay (widget);
10081 }
10082 #endif /* USE_X_TOOLKIT && USE_LUCID */
10083
10084 #ifdef USE_TOOLKIT_SCROLL_BARS
10085 /* Dispatch event to the widget. */
10086 goto OTHER;
10087 #else /* not USE_TOOLKIT_SCROLL_BARS */
10088 bar = x_window_to_scroll_bar (event.xexpose.window);
10089
10090 if (bar)
10091 x_scroll_bar_expose (bar, &event);
10092 #ifdef USE_X_TOOLKIT
10093 else
10094 goto OTHER;
10095 #endif /* USE_X_TOOLKIT */
10096 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10097 }
10098 break;
10099
10100 case GraphicsExpose: /* This occurs when an XCopyArea's
10101 source area was obscured or not
10102 available.*/
10103 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
10104 if (f)
10105 {
10106 expose_frame (f,
10107 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
10108 event.xgraphicsexpose.width,
10109 event.xgraphicsexpose.height);
10110 }
10111 #ifdef USE_X_TOOLKIT
10112 else
10113 goto OTHER;
10114 #endif /* USE_X_TOOLKIT */
10115 break;
10116
10117 case NoExpose: /* This occurs when an XCopyArea's
10118 source area was completely
10119 available */
10120 break;
10121
10122 case UnmapNotify:
10123 /* Redo the mouse-highlight after the tooltip has gone. */
10124 if (event.xmap.window == tip_window)
10125 {
10126 tip_window = 0;
10127 redo_mouse_highlight ();
10128 }
10129
10130 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
10131 if (f) /* F may no longer exist if
10132 the frame was deleted. */
10133 {
10134 /* While a frame is unmapped, display generation is
10135 disabled; you don't want to spend time updating a
10136 display that won't ever be seen. */
10137 f->async_visible = 0;
10138 /* We can't distinguish, from the event, whether the window
10139 has become iconified or invisible. So assume, if it
10140 was previously visible, than now it is iconified.
10141 But x_make_frame_invisible clears both
10142 the visible flag and the iconified flag;
10143 and that way, we know the window is not iconified now. */
10144 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
10145 {
10146 f->async_iconified = 1;
10147
10148 bufp->kind = iconify_event;
10149 XSETFRAME (bufp->frame_or_window, f);
10150 bufp->arg = Qnil;
10151 bufp++;
10152 count++;
10153 numchars--;
10154 }
10155 }
10156 goto OTHER;
10157
10158 case MapNotify:
10159 if (event.xmap.window == tip_window)
10160 /* The tooltip has been drawn already. Avoid
10161 the SET_FRAME_GARBAGED below. */
10162 goto OTHER;
10163
10164 /* We use x_top_window_to_frame because map events can
10165 come for sub-windows and they don't mean that the
10166 frame is visible. */
10167 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
10168 if (f)
10169 {
10170 f->async_visible = 1;
10171 f->async_iconified = 0;
10172 f->output_data.x->has_been_visible = 1;
10173
10174 /* wait_reading_process_input will notice this and update
10175 the frame's display structures. */
10176 SET_FRAME_GARBAGED (f);
10177
10178 if (f->iconified)
10179 {
10180 bufp->kind = deiconify_event;
10181 XSETFRAME (bufp->frame_or_window, f);
10182 bufp->arg = Qnil;
10183 bufp++;
10184 count++;
10185 numchars--;
10186 }
10187 else if (! NILP (Vframe_list)
10188 && ! NILP (XCDR (Vframe_list)))
10189 /* Force a redisplay sooner or later
10190 to update the frame titles
10191 in case this is the second frame. */
10192 record_asynch_buffer_change ();
10193 }
10194 goto OTHER;
10195
10196 case KeyPress:
10197 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
10198
10199 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
10200 if (f == 0)
10201 {
10202 /* Scroll bars consume key events, but we want
10203 the keys to go to the scroll bar's frame. */
10204 Widget widget = XtWindowToWidget (dpyinfo->display,
10205 event.xkey.window);
10206 if (widget && XmIsScrollBar (widget))
10207 {
10208 widget = XtParent (widget);
10209 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
10210 }
10211 }
10212 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
10213
10214 if (f != 0)
10215 {
10216 KeySym keysym, orig_keysym;
10217 /* al%imercury@uunet.uu.net says that making this 81
10218 instead of 80 fixed a bug whereby meta chars made
10219 his Emacs hang.
10220
10221 It seems that some version of XmbLookupString has
10222 a bug of not returning XBufferOverflow in
10223 status_return even if the input is too long to
10224 fit in 81 bytes. So, we must prepare sufficient
10225 bytes for copy_buffer. 513 bytes (256 chars for
10226 two-byte character set) seems to be a faily good
10227 approximation. -- 2000.8.10 handa@etl.go.jp */
10228 unsigned char copy_buffer[513];
10229 unsigned char *copy_bufptr = copy_buffer;
10230 int copy_bufsiz = sizeof (copy_buffer);
10231 int modifiers;
10232
10233 event.xkey.state
10234 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
10235 extra_keyboard_modifiers);
10236 modifiers = event.xkey.state;
10237
10238 /* This will have to go some day... */
10239
10240 /* make_lispy_event turns chars into control chars.
10241 Don't do it here because XLookupString is too eager. */
10242 event.xkey.state &= ~ControlMask;
10243 event.xkey.state &= ~(dpyinfo->meta_mod_mask
10244 | dpyinfo->super_mod_mask
10245 | dpyinfo->hyper_mod_mask
10246 | dpyinfo->alt_mod_mask);
10247
10248 /* In case Meta is ComposeCharacter,
10249 clear its status. According to Markus Ehrnsperger
10250 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
10251 this enables ComposeCharacter to work whether or
10252 not it is combined with Meta. */
10253 if (modifiers & dpyinfo->meta_mod_mask)
10254 bzero (&compose_status, sizeof (compose_status));
10255
10256 #ifdef HAVE_X_I18N
10257 if (FRAME_XIC (f))
10258 {
10259 Status status_return;
10260
10261 nbytes = XmbLookupString (FRAME_XIC (f),
10262 &event.xkey, copy_bufptr,
10263 copy_bufsiz, &keysym,
10264 &status_return);
10265 if (status_return == XBufferOverflow)
10266 {
10267 copy_bufsiz = nbytes + 1;
10268 copy_bufptr = (char *) alloca (copy_bufsiz);
10269 nbytes = XmbLookupString (FRAME_XIC (f),
10270 &event.xkey, copy_bufptr,
10271 copy_bufsiz, &keysym,
10272 &status_return);
10273 }
10274
10275 if (status_return == XLookupNone)
10276 break;
10277 else if (status_return == XLookupChars)
10278 {
10279 keysym = NoSymbol;
10280 modifiers = 0;
10281 }
10282 else if (status_return != XLookupKeySym
10283 && status_return != XLookupBoth)
10284 abort ();
10285 }
10286 else
10287 nbytes = XLookupString (&event.xkey, copy_bufptr,
10288 copy_bufsiz, &keysym,
10289 &compose_status);
10290 #else
10291 nbytes = XLookupString (&event.xkey, copy_bufptr,
10292 copy_bufsiz, &keysym,
10293 &compose_status);
10294 #endif
10295
10296 orig_keysym = keysym;
10297
10298 if (numchars > 1)
10299 {
10300 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
10301 || keysym == XK_Delete
10302 #ifdef XK_ISO_Left_Tab
10303 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
10304 #endif
10305 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
10306 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
10307 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
10308 #ifdef HPUX
10309 /* This recognizes the "extended function keys".
10310 It seems there's no cleaner way.
10311 Test IsModifierKey to avoid handling mode_switch
10312 incorrectly. */
10313 || ((unsigned) (keysym) >= XK_Select
10314 && (unsigned)(keysym) < XK_KP_Space)
10315 #endif
10316 #ifdef XK_dead_circumflex
10317 || orig_keysym == XK_dead_circumflex
10318 #endif
10319 #ifdef XK_dead_grave
10320 || orig_keysym == XK_dead_grave
10321 #endif
10322 #ifdef XK_dead_tilde
10323 || orig_keysym == XK_dead_tilde
10324 #endif
10325 #ifdef XK_dead_diaeresis
10326 || orig_keysym == XK_dead_diaeresis
10327 #endif
10328 #ifdef XK_dead_macron
10329 || orig_keysym == XK_dead_macron
10330 #endif
10331 #ifdef XK_dead_degree
10332 || orig_keysym == XK_dead_degree
10333 #endif
10334 #ifdef XK_dead_acute
10335 || orig_keysym == XK_dead_acute
10336 #endif
10337 #ifdef XK_dead_cedilla
10338 || orig_keysym == XK_dead_cedilla
10339 #endif
10340 #ifdef XK_dead_breve
10341 || orig_keysym == XK_dead_breve
10342 #endif
10343 #ifdef XK_dead_ogonek
10344 || orig_keysym == XK_dead_ogonek
10345 #endif
10346 #ifdef XK_dead_caron
10347 || orig_keysym == XK_dead_caron
10348 #endif
10349 #ifdef XK_dead_doubleacute
10350 || orig_keysym == XK_dead_doubleacute
10351 #endif
10352 #ifdef XK_dead_abovedot
10353 || orig_keysym == XK_dead_abovedot
10354 #endif
10355 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10356 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10357 /* Any "vendor-specific" key is ok. */
10358 || (orig_keysym & (1 << 28)))
10359 && ! (IsModifierKey (orig_keysym)
10360 #ifndef HAVE_X11R5
10361 #ifdef XK_Mode_switch
10362 || ((unsigned)(orig_keysym) == XK_Mode_switch)
10363 #endif
10364 #ifdef XK_Num_Lock
10365 || ((unsigned)(orig_keysym) == XK_Num_Lock)
10366 #endif
10367 #endif /* not HAVE_X11R5 */
10368 ))
10369 {
10370 if (temp_index == sizeof temp_buffer / sizeof (short))
10371 temp_index = 0;
10372 temp_buffer[temp_index++] = keysym;
10373 bufp->kind = non_ascii_keystroke;
10374 bufp->code = keysym;
10375 XSETFRAME (bufp->frame_or_window, f);
10376 bufp->arg = Qnil;
10377 bufp->modifiers
10378 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10379 modifiers);
10380 bufp->timestamp = event.xkey.time;
10381 bufp++;
10382 count++;
10383 numchars--;
10384 }
10385 else if (numchars > nbytes)
10386 {
10387 register int i;
10388 register int c;
10389 int nchars, len;
10390
10391 for (i = 0; i < nbytes; i++)
10392 {
10393 if (temp_index == (sizeof temp_buffer
10394 / sizeof (short)))
10395 temp_index = 0;
10396 temp_buffer[temp_index++] = copy_bufptr[i];
10397 }
10398
10399 if (/* If the event is not from XIM, */
10400 event.xkey.keycode != 0
10401 /* or the current locale doesn't request
10402 decoding of the intup data, ... */
10403 || coding.type == coding_type_raw_text
10404 || coding.type == coding_type_no_conversion)
10405 {
10406 /* ... we can use the input data as is. */
10407 nchars = nbytes;
10408 }
10409 else
10410 {
10411 /* We have to decode the input data. */
10412 int require;
10413 unsigned char *p;
10414
10415 require = decoding_buffer_size (&coding, nbytes);
10416 p = (unsigned char *) alloca (require);
10417 coding.mode |= CODING_MODE_LAST_BLOCK;
10418 decode_coding (&coding, copy_bufptr, p,
10419 nbytes, require);
10420 nbytes = coding.produced;
10421 nchars = coding.produced_char;
10422 copy_bufptr = p;
10423 }
10424
10425 /* Convert the input data to a sequence of
10426 character events. */
10427 for (i = 0; i < nbytes; i += len)
10428 {
10429 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10430 nbytes - i, len);
10431 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
10432 ? ascii_keystroke
10433 : multibyte_char_keystroke);
10434 bufp->code = c;
10435 XSETFRAME (bufp->frame_or_window, f);
10436 bufp->arg = Qnil;
10437 bufp->modifiers
10438 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10439 modifiers);
10440 bufp->timestamp = event.xkey.time;
10441 bufp++;
10442 }
10443
10444 count += nchars;
10445 numchars -= nchars;
10446
10447 if (keysym == NoSymbol)
10448 break;
10449 }
10450 else
10451 abort ();
10452 }
10453 else
10454 abort ();
10455 }
10456 #ifdef HAVE_X_I18N
10457 /* Don't dispatch this event since XtDispatchEvent calls
10458 XFilterEvent, and two calls in a row may freeze the
10459 client. */
10460 break;
10461 #else
10462 goto OTHER;
10463 #endif
10464
10465 case KeyRelease:
10466 #ifdef HAVE_X_I18N
10467 /* Don't dispatch this event since XtDispatchEvent calls
10468 XFilterEvent, and two calls in a row may freeze the
10469 client. */
10470 break;
10471 #else
10472 goto OTHER;
10473 #endif
10474
10475 /* Here's a possible interpretation of the whole
10476 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
10477 you get a FocusIn event, you have to get a FocusOut
10478 event before you relinquish the focus. If you
10479 haven't received a FocusIn event, then a mere
10480 LeaveNotify is enough to free you. */
10481
10482 case EnterNotify:
10483 {
10484 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10485
10486 if (event.xcrossing.focus)
10487 {
10488 /* Avoid nasty pop/raise loops. */
10489 if (f && (!(f->auto_raise)
10490 || !(f->auto_lower)
10491 || (event.xcrossing.time - enter_timestamp) > 500))
10492 {
10493 x_new_focus_frame (dpyinfo, f);
10494 enter_timestamp = event.xcrossing.time;
10495 }
10496 }
10497 else if (f == dpyinfo->x_focus_frame)
10498 x_new_focus_frame (dpyinfo, 0);
10499
10500 /* EnterNotify counts as mouse movement,
10501 so update things that depend on mouse position. */
10502 if (f && !f->output_data.x->hourglass_p)
10503 note_mouse_movement (f, &event.xmotion);
10504 goto OTHER;
10505 }
10506
10507 case FocusIn:
10508 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10509 if (event.xfocus.detail != NotifyPointer)
10510 dpyinfo->x_focus_event_frame = f;
10511 if (f)
10512 {
10513 x_new_focus_frame (dpyinfo, f);
10514
10515 /* Don't stop displaying the initial startup message
10516 for a switch-frame event we don't need. */
10517 if (GC_NILP (Vterminal_frame)
10518 && GC_CONSP (Vframe_list)
10519 && !GC_NILP (XCDR (Vframe_list)))
10520 {
10521 bufp->kind = FOCUS_IN_EVENT;
10522 XSETFRAME (bufp->frame_or_window, f);
10523 bufp->arg = Qnil;
10524 ++bufp, ++count, --numchars;
10525 }
10526 }
10527
10528 #ifdef HAVE_X_I18N
10529 if (f && FRAME_XIC (f))
10530 XSetICFocus (FRAME_XIC (f));
10531 #endif
10532
10533 goto OTHER;
10534
10535 case LeaveNotify:
10536 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
10537 if (f)
10538 {
10539 if (f == dpyinfo->mouse_face_mouse_frame)
10540 {
10541 /* If we move outside the frame, then we're
10542 certainly no longer on any text in the frame. */
10543 clear_mouse_face (dpyinfo);
10544 dpyinfo->mouse_face_mouse_frame = 0;
10545 }
10546
10547 /* Generate a nil HELP_EVENT to cancel a help-echo.
10548 Do it only if there's something to cancel.
10549 Otherwise, the startup message is cleared when
10550 the mouse leaves the frame. */
10551 if (any_help_event_p)
10552 {
10553 Lisp_Object frame;
10554 int n;
10555
10556 XSETFRAME (frame, f);
10557 help_echo = Qnil;
10558 n = gen_help_event (bufp, numchars,
10559 Qnil, frame, Qnil, Qnil, 0);
10560 bufp += n, count += n, numchars -= n;
10561 }
10562
10563 if (event.xcrossing.focus)
10564 x_mouse_leave (dpyinfo);
10565 else
10566 {
10567 if (f == dpyinfo->x_focus_event_frame)
10568 dpyinfo->x_focus_event_frame = 0;
10569 if (f == dpyinfo->x_focus_frame)
10570 x_new_focus_frame (dpyinfo, 0);
10571 }
10572 }
10573 goto OTHER;
10574
10575 case FocusOut:
10576 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10577 if (event.xfocus.detail != NotifyPointer
10578 && f == dpyinfo->x_focus_event_frame)
10579 dpyinfo->x_focus_event_frame = 0;
10580 if (f && f == dpyinfo->x_focus_frame)
10581 x_new_focus_frame (dpyinfo, 0);
10582
10583 #ifdef HAVE_X_I18N
10584 if (f && FRAME_XIC (f))
10585 XUnsetICFocus (FRAME_XIC (f));
10586 #endif
10587
10588 goto OTHER;
10589
10590 case MotionNotify:
10591 {
10592 previous_help_echo = help_echo;
10593 help_echo = help_echo_object = help_echo_window = Qnil;
10594 help_echo_pos = -1;
10595
10596 if (dpyinfo->grabbed && last_mouse_frame
10597 && FRAME_LIVE_P (last_mouse_frame))
10598 f = last_mouse_frame;
10599 else
10600 f = x_window_to_frame (dpyinfo, event.xmotion.window);
10601
10602 if (f)
10603 note_mouse_movement (f, &event.xmotion);
10604 else
10605 {
10606 #ifndef USE_TOOLKIT_SCROLL_BARS
10607 struct scroll_bar *bar
10608 = x_window_to_scroll_bar (event.xmotion.window);
10609
10610 if (bar)
10611 x_scroll_bar_note_movement (bar, &event);
10612 #endif /* USE_TOOLKIT_SCROLL_BARS */
10613
10614 /* If we move outside the frame, then we're
10615 certainly no longer on any text in the frame. */
10616 clear_mouse_face (dpyinfo);
10617 }
10618
10619 /* If the contents of the global variable help_echo
10620 has changed, generate a HELP_EVENT. */
10621 if (!NILP (help_echo)
10622 || !NILP (previous_help_echo))
10623 {
10624 Lisp_Object frame;
10625 int n;
10626
10627 if (f)
10628 XSETFRAME (frame, f);
10629 else
10630 frame = Qnil;
10631
10632 any_help_event_p = 1;
10633 n = gen_help_event (bufp, numchars, help_echo, frame,
10634 help_echo_window, help_echo_object,
10635 help_echo_pos);
10636 bufp += n, count += n, numchars -= n;
10637 }
10638
10639 goto OTHER;
10640 }
10641
10642 case ConfigureNotify:
10643 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10644 if (f)
10645 {
10646 #ifndef USE_X_TOOLKIT
10647 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10648 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
10649
10650 /* In the toolkit version, change_frame_size
10651 is called by the code that handles resizing
10652 of the EmacsFrame widget. */
10653
10654 /* Even if the number of character rows and columns has
10655 not changed, the font size may have changed, so we need
10656 to check the pixel dimensions as well. */
10657 if (columns != f->width
10658 || rows != f->height
10659 || event.xconfigure.width != f->output_data.x->pixel_width
10660 || event.xconfigure.height != f->output_data.x->pixel_height)
10661 {
10662 change_frame_size (f, rows, columns, 0, 1, 0);
10663 SET_FRAME_GARBAGED (f);
10664 cancel_mouse_face (f);
10665 }
10666 #endif
10667
10668 f->output_data.x->pixel_width = event.xconfigure.width;
10669 f->output_data.x->pixel_height = event.xconfigure.height;
10670
10671 /* What we have now is the position of Emacs's own window.
10672 Convert that to the position of the window manager window. */
10673 x_real_positions (f, &f->output_data.x->left_pos,
10674 &f->output_data.x->top_pos);
10675
10676 #ifdef HAVE_X_I18N
10677 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10678 xic_set_statusarea (f);
10679 #endif
10680
10681 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10682 {
10683 /* Since the WM decorations come below top_pos now,
10684 we must put them below top_pos in the future. */
10685 f->output_data.x->win_gravity = NorthWestGravity;
10686 x_wm_set_size_hint (f, (long) 0, 0);
10687 }
10688 #ifdef USE_MOTIF
10689 /* Some window managers pass (0,0) as the location of
10690 the window, and the Motif event handler stores it
10691 in the emacs widget, which messes up Motif menus. */
10692 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10693 {
10694 event.xconfigure.x = f->output_data.x->widget->core.x;
10695 event.xconfigure.y = f->output_data.x->widget->core.y;
10696 }
10697 #endif /* USE_MOTIF */
10698 }
10699 goto OTHER;
10700
10701 case ButtonPress:
10702 case ButtonRelease:
10703 {
10704 /* If we decide we want to generate an event to be seen
10705 by the rest of Emacs, we put it here. */
10706 struct input_event emacs_event;
10707 int tool_bar_p = 0;
10708
10709 emacs_event.kind = no_event;
10710 bzero (&compose_status, sizeof (compose_status));
10711
10712 if (dpyinfo->grabbed
10713 && last_mouse_frame
10714 && FRAME_LIVE_P (last_mouse_frame))
10715 f = last_mouse_frame;
10716 else
10717 f = x_window_to_frame (dpyinfo, event.xbutton.window);
10718
10719 if (f)
10720 {
10721 /* Is this in the tool-bar? */
10722 if (WINDOWP (f->tool_bar_window)
10723 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
10724 {
10725 Lisp_Object window;
10726 int p, x, y;
10727
10728 x = event.xbutton.x;
10729 y = event.xbutton.y;
10730
10731 /* Set x and y. */
10732 window = window_from_coordinates (f, x, y, &p, 1);
10733 if (EQ (window, f->tool_bar_window))
10734 {
10735 x_handle_tool_bar_click (f, &event.xbutton);
10736 tool_bar_p = 1;
10737 }
10738 }
10739
10740 if (!tool_bar_p)
10741 if (!dpyinfo->x_focus_frame
10742 || f == dpyinfo->x_focus_frame)
10743 construct_mouse_click (&emacs_event, &event, f);
10744 }
10745 else
10746 {
10747 #ifndef USE_TOOLKIT_SCROLL_BARS
10748 struct scroll_bar *bar
10749 = x_window_to_scroll_bar (event.xbutton.window);
10750
10751 if (bar)
10752 x_scroll_bar_handle_click (bar, &event, &emacs_event);
10753 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10754 }
10755
10756 if (event.type == ButtonPress)
10757 {
10758 dpyinfo->grabbed |= (1 << event.xbutton.button);
10759 last_mouse_frame = f;
10760 /* Ignore any mouse motion that happened
10761 before this event; any subsequent mouse-movement
10762 Emacs events should reflect only motion after
10763 the ButtonPress. */
10764 if (f != 0)
10765 f->mouse_moved = 0;
10766
10767 if (!tool_bar_p)
10768 last_tool_bar_item = -1;
10769 }
10770 else
10771 {
10772 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
10773 }
10774
10775 if (numchars >= 1 && emacs_event.kind != no_event)
10776 {
10777 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10778 bufp++;
10779 count++;
10780 numchars--;
10781 }
10782
10783 #ifdef USE_X_TOOLKIT
10784 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10785 /* For a down-event in the menu bar,
10786 don't pass it to Xt right now.
10787 Instead, save it away
10788 and we will pass it to Xt from kbd_buffer_get_event.
10789 That way, we can run some Lisp code first. */
10790 if (f && event.type == ButtonPress
10791 /* Verify the event is really within the menu bar
10792 and not just sent to it due to grabbing. */
10793 && event.xbutton.x >= 0
10794 && event.xbutton.x < f->output_data.x->pixel_width
10795 && event.xbutton.y >= 0
10796 && event.xbutton.y < f->output_data.x->menubar_height
10797 && event.xbutton.same_screen)
10798 {
10799 SET_SAVED_BUTTON_EVENT;
10800 XSETFRAME (last_mouse_press_frame, f);
10801 }
10802 else if (event.type == ButtonPress)
10803 {
10804 last_mouse_press_frame = Qnil;
10805 goto OTHER;
10806 }
10807
10808 #ifdef USE_MOTIF /* This should do not harm for Lucid,
10809 but I am trying to be cautious. */
10810 else if (event.type == ButtonRelease)
10811 {
10812 if (!NILP (last_mouse_press_frame))
10813 {
10814 f = XFRAME (last_mouse_press_frame);
10815 if (f->output_data.x)
10816 SET_SAVED_BUTTON_EVENT;
10817 }
10818 else
10819 goto OTHER;
10820 }
10821 #endif /* USE_MOTIF */
10822 else
10823 goto OTHER;
10824 #endif /* USE_X_TOOLKIT */
10825 }
10826 break;
10827
10828 case CirculateNotify:
10829 goto OTHER;
10830
10831 case CirculateRequest:
10832 goto OTHER;
10833
10834 case VisibilityNotify:
10835 goto OTHER;
10836
10837 case MappingNotify:
10838 /* Someone has changed the keyboard mapping - update the
10839 local cache. */
10840 switch (event.xmapping.request)
10841 {
10842 case MappingModifier:
10843 x_find_modifier_meanings (dpyinfo);
10844 /* This is meant to fall through. */
10845 case MappingKeyboard:
10846 XRefreshKeyboardMapping (&event.xmapping);
10847 }
10848 goto OTHER;
10849
10850 default:
10851 OTHER:
10852 #ifdef USE_X_TOOLKIT
10853 BLOCK_INPUT;
10854 XtDispatchEvent (&event);
10855 UNBLOCK_INPUT;
10856 #endif /* USE_X_TOOLKIT */
10857 break;
10858 }
10859 }
10860 }
10861
10862 out:;
10863
10864 /* On some systems, an X bug causes Emacs to get no more events
10865 when the window is destroyed. Detect that. (1994.) */
10866 if (! event_found)
10867 {
10868 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10869 One XNOOP in 100 loops will make Emacs terminate.
10870 B. Bretthauer, 1994 */
10871 x_noop_count++;
10872 if (x_noop_count >= 100)
10873 {
10874 x_noop_count=0;
10875
10876 if (next_noop_dpyinfo == 0)
10877 next_noop_dpyinfo = x_display_list;
10878
10879 XNoOp (next_noop_dpyinfo->display);
10880
10881 /* Each time we get here, cycle through the displays now open. */
10882 next_noop_dpyinfo = next_noop_dpyinfo->next;
10883 }
10884 }
10885
10886 /* If the focus was just given to an auto-raising frame,
10887 raise it now. */
10888 /* ??? This ought to be able to handle more than one such frame. */
10889 if (pending_autoraise_frame)
10890 {
10891 x_raise_frame (pending_autoraise_frame);
10892 pending_autoraise_frame = 0;
10893 }
10894
10895 UNBLOCK_INPUT;
10896 --handling_signal;
10897 return count;
10898 }
10899
10900
10901
10902 \f
10903 /***********************************************************************
10904 Text Cursor
10905 ***********************************************************************/
10906
10907 /* Note if the text cursor of window W has been overwritten by a
10908 drawing operation that outputs N glyphs starting at HPOS in the
10909 line given by output_cursor.vpos. N < 0 means all the rest of the
10910 line after HPOS has been written. */
10911
10912 static void
10913 note_overwritten_text_cursor (w, hpos, n)
10914 struct window *w;
10915 int hpos, n;
10916 {
10917 if (updated_area == TEXT_AREA
10918 && output_cursor.vpos == w->phys_cursor.vpos
10919 && hpos <= w->phys_cursor.hpos
10920 && (n < 0
10921 || hpos + n > w->phys_cursor.hpos))
10922 w->phys_cursor_on_p = 0;
10923 }
10924
10925
10926 /* Set clipping for output in glyph row ROW. W is the window in which
10927 we operate. GC is the graphics context to set clipping in.
10928 WHOLE_LINE_P non-zero means include the areas used for truncation
10929 mark display and alike in the clipping rectangle.
10930
10931 ROW may be a text row or, e.g., a mode line. Text rows must be
10932 clipped to the interior of the window dedicated to text display,
10933 mode lines must be clipped to the whole window. */
10934
10935 static void
10936 x_clip_to_row (w, row, gc, whole_line_p)
10937 struct window *w;
10938 struct glyph_row *row;
10939 GC gc;
10940 int whole_line_p;
10941 {
10942 struct frame *f = XFRAME (WINDOW_FRAME (w));
10943 XRectangle clip_rect;
10944 int window_x, window_y, window_width, window_height;
10945
10946 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
10947
10948 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10949 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10950 clip_rect.y = max (clip_rect.y, window_y);
10951 clip_rect.width = window_width;
10952 clip_rect.height = row->visible_height;
10953
10954 /* If clipping to the whole line, including trunc marks, extend
10955 the rectangle to the left and increase its width. */
10956 if (whole_line_p)
10957 {
10958 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10959 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
10960 }
10961
10962 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
10963 }
10964
10965
10966 /* Draw a hollow box cursor on window W in glyph row ROW. */
10967
10968 static void
10969 x_draw_hollow_cursor (w, row)
10970 struct window *w;
10971 struct glyph_row *row;
10972 {
10973 struct frame *f = XFRAME (WINDOW_FRAME (w));
10974 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10975 Display *dpy = FRAME_X_DISPLAY (f);
10976 int x, y, wd, h;
10977 XGCValues xgcv;
10978 struct glyph *cursor_glyph;
10979 GC gc;
10980
10981 /* Compute frame-relative coordinates from window-relative
10982 coordinates. */
10983 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10984 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10985 + row->ascent - w->phys_cursor_ascent);
10986 h = row->height - 1;
10987
10988 /* Get the glyph the cursor is on. If we can't tell because
10989 the current matrix is invalid or such, give up. */
10990 cursor_glyph = get_phys_cursor_glyph (w);
10991 if (cursor_glyph == NULL)
10992 return;
10993
10994 /* Compute the width of the rectangle to draw. If on a stretch
10995 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10996 rectangle as wide as the glyph, but use a canonical character
10997 width instead. */
10998 wd = cursor_glyph->pixel_width - 1;
10999 if (cursor_glyph->type == STRETCH_GLYPH
11000 && !x_stretch_cursor_p)
11001 wd = min (CANON_X_UNIT (f), wd);
11002
11003 /* The foreground of cursor_gc is typically the same as the normal
11004 background color, which can cause the cursor box to be invisible. */
11005 xgcv.foreground = f->output_data.x->cursor_pixel;
11006 if (dpyinfo->scratch_cursor_gc)
11007 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
11008 else
11009 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
11010 GCForeground, &xgcv);
11011 gc = dpyinfo->scratch_cursor_gc;
11012
11013 /* Set clipping, draw the rectangle, and reset clipping again. */
11014 x_clip_to_row (w, row, gc, 0);
11015 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
11016 XSetClipMask (dpy, gc, None);
11017 }
11018
11019
11020 /* Draw a bar cursor on window W in glyph row ROW.
11021
11022 Implementation note: One would like to draw a bar cursor with an
11023 angle equal to the one given by the font property XA_ITALIC_ANGLE.
11024 Unfortunately, I didn't find a font yet that has this property set.
11025 --gerd. */
11026
11027 static void
11028 x_draw_bar_cursor (w, row, width)
11029 struct window *w;
11030 struct glyph_row *row;
11031 int width;
11032 {
11033 struct frame *f = XFRAME (w->frame);
11034 struct glyph *cursor_glyph;
11035 GC gc;
11036 int x;
11037 unsigned long mask;
11038 XGCValues xgcv;
11039 Display *dpy;
11040 Window window;
11041
11042 /* If cursor is out of bounds, don't draw garbage. This can happen
11043 in mini-buffer windows when switching between echo area glyphs
11044 and mini-buffer. */
11045 cursor_glyph = get_phys_cursor_glyph (w);
11046 if (cursor_glyph == NULL)
11047 return;
11048
11049 /* If on an image, draw like a normal cursor. That's usually better
11050 visible than drawing a bar, esp. if the image is large so that
11051 the bar might not be in the window. */
11052 if (cursor_glyph->type == IMAGE_GLYPH)
11053 {
11054 struct glyph_row *row;
11055 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
11056 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
11057 }
11058 else
11059 {
11060 xgcv.background = f->output_data.x->cursor_pixel;
11061 xgcv.foreground = f->output_data.x->cursor_pixel;
11062 xgcv.graphics_exposures = 0;
11063 mask = GCForeground | GCBackground | GCGraphicsExposures;
11064 dpy = FRAME_X_DISPLAY (f);
11065 window = FRAME_X_WINDOW (f);
11066 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
11067
11068 if (gc)
11069 XChangeGC (dpy, gc, mask, &xgcv);
11070 else
11071 {
11072 gc = XCreateGC (dpy, window, mask, &xgcv);
11073 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
11074 }
11075
11076 if (width < 0)
11077 width = f->output_data.x->cursor_width;
11078
11079 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11080 x_clip_to_row (w, row, gc, 0);
11081 XFillRectangle (dpy, window, gc,
11082 x,
11083 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
11084 min (cursor_glyph->pixel_width, width),
11085 row->height);
11086 XSetClipMask (dpy, gc, None);
11087 }
11088 }
11089
11090
11091 /* Clear the cursor of window W to background color, and mark the
11092 cursor as not shown. This is used when the text where the cursor
11093 is is about to be rewritten. */
11094
11095 static void
11096 x_clear_cursor (w)
11097 struct window *w;
11098 {
11099 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
11100 x_update_window_cursor (w, 0);
11101 }
11102
11103
11104 /* Draw the cursor glyph of window W in glyph row ROW. See the
11105 comment of x_draw_glyphs for the meaning of HL. */
11106
11107 static void
11108 x_draw_phys_cursor_glyph (w, row, hl)
11109 struct window *w;
11110 struct glyph_row *row;
11111 enum draw_glyphs_face hl;
11112 {
11113 /* If cursor hpos is out of bounds, don't draw garbage. This can
11114 happen in mini-buffer windows when switching between echo area
11115 glyphs and mini-buffer. */
11116 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
11117 {
11118 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
11119 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
11120 hl, 0, 0, 0);
11121
11122 /* When we erase the cursor, and ROW is overlapped by other
11123 rows, make sure that these overlapping parts of other rows
11124 are redrawn. */
11125 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
11126 {
11127 if (row > w->current_matrix->rows
11128 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
11129 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
11130
11131 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
11132 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
11133 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
11134 }
11135 }
11136 }
11137
11138
11139 /* Erase the image of a cursor of window W from the screen. */
11140
11141 static void
11142 x_erase_phys_cursor (w)
11143 struct window *w;
11144 {
11145 struct frame *f = XFRAME (w->frame);
11146 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11147 int hpos = w->phys_cursor.hpos;
11148 int vpos = w->phys_cursor.vpos;
11149 int mouse_face_here_p = 0;
11150 struct glyph_matrix *active_glyphs = w->current_matrix;
11151 struct glyph_row *cursor_row;
11152 struct glyph *cursor_glyph;
11153 enum draw_glyphs_face hl;
11154
11155 /* No cursor displayed or row invalidated => nothing to do on the
11156 screen. */
11157 if (w->phys_cursor_type == NO_CURSOR)
11158 goto mark_cursor_off;
11159
11160 /* VPOS >= active_glyphs->nrows means that window has been resized.
11161 Don't bother to erase the cursor. */
11162 if (vpos >= active_glyphs->nrows)
11163 goto mark_cursor_off;
11164
11165 /* If row containing cursor is marked invalid, there is nothing we
11166 can do. */
11167 cursor_row = MATRIX_ROW (active_glyphs, vpos);
11168 if (!cursor_row->enabled_p)
11169 goto mark_cursor_off;
11170
11171 /* This can happen when the new row is shorter than the old one.
11172 In this case, either x_draw_glyphs or clear_end_of_line
11173 should have cleared the cursor. Note that we wouldn't be
11174 able to erase the cursor in this case because we don't have a
11175 cursor glyph at hand. */
11176 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
11177 goto mark_cursor_off;
11178
11179 /* If the cursor is in the mouse face area, redisplay that when
11180 we clear the cursor. */
11181 if (! NILP (dpyinfo->mouse_face_window)
11182 && w == XWINDOW (dpyinfo->mouse_face_window)
11183 && (vpos > dpyinfo->mouse_face_beg_row
11184 || (vpos == dpyinfo->mouse_face_beg_row
11185 && hpos >= dpyinfo->mouse_face_beg_col))
11186 && (vpos < dpyinfo->mouse_face_end_row
11187 || (vpos == dpyinfo->mouse_face_end_row
11188 && hpos < dpyinfo->mouse_face_end_col))
11189 /* Don't redraw the cursor's spot in mouse face if it is at the
11190 end of a line (on a newline). The cursor appears there, but
11191 mouse highlighting does not. */
11192 && cursor_row->used[TEXT_AREA] > hpos)
11193 mouse_face_here_p = 1;
11194
11195 /* Maybe clear the display under the cursor. */
11196 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
11197 {
11198 int x;
11199 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11200
11201 cursor_glyph = get_phys_cursor_glyph (w);
11202 if (cursor_glyph == NULL)
11203 goto mark_cursor_off;
11204
11205 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11206
11207 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11208 x,
11209 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
11210 cursor_row->y)),
11211 cursor_glyph->pixel_width,
11212 cursor_row->visible_height,
11213 False);
11214 }
11215
11216 /* Erase the cursor by redrawing the character underneath it. */
11217 if (mouse_face_here_p)
11218 hl = DRAW_MOUSE_FACE;
11219 else if (cursor_row->inverse_p)
11220 hl = DRAW_INVERSE_VIDEO;
11221 else
11222 hl = DRAW_NORMAL_TEXT;
11223 x_draw_phys_cursor_glyph (w, cursor_row, hl);
11224
11225 mark_cursor_off:
11226 w->phys_cursor_on_p = 0;
11227 w->phys_cursor_type = NO_CURSOR;
11228 }
11229
11230
11231 /* Non-zero if physical cursor of window W is within mouse face. */
11232
11233 static int
11234 cursor_in_mouse_face_p (w)
11235 struct window *w;
11236 {
11237 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
11238 int in_mouse_face = 0;
11239
11240 if (WINDOWP (dpyinfo->mouse_face_window)
11241 && XWINDOW (dpyinfo->mouse_face_window) == w)
11242 {
11243 int hpos = w->phys_cursor.hpos;
11244 int vpos = w->phys_cursor.vpos;
11245
11246 if (vpos >= dpyinfo->mouse_face_beg_row
11247 && vpos <= dpyinfo->mouse_face_end_row
11248 && (vpos > dpyinfo->mouse_face_beg_row
11249 || hpos >= dpyinfo->mouse_face_beg_col)
11250 && (vpos < dpyinfo->mouse_face_end_row
11251 || hpos < dpyinfo->mouse_face_end_col
11252 || dpyinfo->mouse_face_past_end))
11253 in_mouse_face = 1;
11254 }
11255
11256 return in_mouse_face;
11257 }
11258
11259
11260 /* Display or clear cursor of window W. If ON is zero, clear the
11261 cursor. If it is non-zero, display the cursor. If ON is nonzero,
11262 where to put the cursor is specified by HPOS, VPOS, X and Y. */
11263
11264 void
11265 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
11266 struct window *w;
11267 int on, hpos, vpos, x, y;
11268 {
11269 struct frame *f = XFRAME (w->frame);
11270 int new_cursor_type;
11271 int new_cursor_width;
11272 struct glyph_matrix *current_glyphs;
11273 struct glyph_row *glyph_row;
11274 struct glyph *glyph;
11275
11276 /* This is pointless on invisible frames, and dangerous on garbaged
11277 windows and frames; in the latter case, the frame or window may
11278 be in the midst of changing its size, and x and y may be off the
11279 window. */
11280 if (! FRAME_VISIBLE_P (f)
11281 || FRAME_GARBAGED_P (f)
11282 || vpos >= w->current_matrix->nrows
11283 || hpos >= w->current_matrix->matrix_w)
11284 return;
11285
11286 /* If cursor is off and we want it off, return quickly. */
11287 if (!on && !w->phys_cursor_on_p)
11288 return;
11289
11290 current_glyphs = w->current_matrix;
11291 glyph_row = MATRIX_ROW (current_glyphs, vpos);
11292 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
11293
11294 /* If cursor row is not enabled, we don't really know where to
11295 display the cursor. */
11296 if (!glyph_row->enabled_p)
11297 {
11298 w->phys_cursor_on_p = 0;
11299 return;
11300 }
11301
11302 xassert (interrupt_input_blocked);
11303
11304 /* Set new_cursor_type to the cursor we want to be displayed. In a
11305 mini-buffer window, we want the cursor only to appear if we are
11306 reading input from this window. For the selected window, we want
11307 the cursor type given by the frame parameter. If explicitly
11308 marked off, draw no cursor. In all other cases, we want a hollow
11309 box cursor. */
11310 new_cursor_width = -1;
11311 if (cursor_in_echo_area
11312 && FRAME_HAS_MINIBUF_P (f)
11313 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
11314 {
11315 if (w == XWINDOW (echo_area_window))
11316 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11317 else
11318 new_cursor_type = HOLLOW_BOX_CURSOR;
11319 }
11320 else
11321 {
11322 if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
11323 || w != XWINDOW (f->selected_window))
11324 {
11325 extern int cursor_in_non_selected_windows;
11326
11327 if (MINI_WINDOW_P (w)
11328 || !cursor_in_non_selected_windows
11329 || NILP (XBUFFER (w->buffer)->cursor_type))
11330 new_cursor_type = NO_CURSOR;
11331 else
11332 new_cursor_type = HOLLOW_BOX_CURSOR;
11333 }
11334 else if (w->cursor_off_p)
11335 new_cursor_type = NO_CURSOR;
11336 else
11337 {
11338 struct buffer *b = XBUFFER (w->buffer);
11339
11340 if (EQ (b->cursor_type, Qt))
11341 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11342 else
11343 new_cursor_type = x_specified_cursor_type (b->cursor_type,
11344 &new_cursor_width);
11345 }
11346 }
11347
11348 /* If cursor is currently being shown and we don't want it to be or
11349 it is in the wrong place, or the cursor type is not what we want,
11350 erase it. */
11351 if (w->phys_cursor_on_p
11352 && (!on
11353 || w->phys_cursor.x != x
11354 || w->phys_cursor.y != y
11355 || new_cursor_type != w->phys_cursor_type))
11356 x_erase_phys_cursor (w);
11357
11358 /* If the cursor is now invisible and we want it to be visible,
11359 display it. */
11360 if (on && !w->phys_cursor_on_p)
11361 {
11362 w->phys_cursor_ascent = glyph_row->ascent;
11363 w->phys_cursor_height = glyph_row->height;
11364
11365 /* Set phys_cursor_.* before x_draw_.* is called because some
11366 of them may need the information. */
11367 w->phys_cursor.x = x;
11368 w->phys_cursor.y = glyph_row->y;
11369 w->phys_cursor.hpos = hpos;
11370 w->phys_cursor.vpos = vpos;
11371 w->phys_cursor_type = new_cursor_type;
11372 w->phys_cursor_on_p = 1;
11373
11374 switch (new_cursor_type)
11375 {
11376 case HOLLOW_BOX_CURSOR:
11377 x_draw_hollow_cursor (w, glyph_row);
11378 break;
11379
11380 case FILLED_BOX_CURSOR:
11381 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
11382 break;
11383
11384 case BAR_CURSOR:
11385 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
11386 break;
11387
11388 case NO_CURSOR:
11389 break;
11390
11391 default:
11392 abort ();
11393 }
11394
11395 #ifdef HAVE_X_I18N
11396 if (w == XWINDOW (f->selected_window))
11397 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
11398 xic_set_preeditarea (w, x, y);
11399 #endif
11400 }
11401
11402 #ifndef XFlush
11403 if (updating_frame != f)
11404 XFlush (FRAME_X_DISPLAY (f));
11405 #endif
11406 }
11407
11408
11409 /* Display the cursor on window W, or clear it. X and Y are window
11410 relative pixel coordinates. HPOS and VPOS are glyph matrix
11411 positions. If W is not the selected window, display a hollow
11412 cursor. ON non-zero means display the cursor at X, Y which
11413 correspond to HPOS, VPOS, otherwise it is cleared. */
11414
11415 void
11416 x_display_cursor (w, on, hpos, vpos, x, y)
11417 struct window *w;
11418 int on, hpos, vpos, x, y;
11419 {
11420 BLOCK_INPUT;
11421 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
11422 UNBLOCK_INPUT;
11423 }
11424
11425
11426 /* Display the cursor on window W, or clear it, according to ON_P.
11427 Don't change the cursor's position. */
11428
11429 void
11430 x_update_cursor (f, on_p)
11431 struct frame *f;
11432 {
11433 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11434 }
11435
11436
11437 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
11438 in the window tree rooted at W. */
11439
11440 static void
11441 x_update_cursor_in_window_tree (w, on_p)
11442 struct window *w;
11443 int on_p;
11444 {
11445 while (w)
11446 {
11447 if (!NILP (w->hchild))
11448 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11449 else if (!NILP (w->vchild))
11450 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11451 else
11452 x_update_window_cursor (w, on_p);
11453
11454 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11455 }
11456 }
11457
11458
11459 /* Switch the display of W's cursor on or off, according to the value
11460 of ON. */
11461
11462 static void
11463 x_update_window_cursor (w, on)
11464 struct window *w;
11465 int on;
11466 {
11467 /* Don't update cursor in windows whose frame is in the process
11468 of being deleted. */
11469 if (w->current_matrix)
11470 {
11471 BLOCK_INPUT;
11472 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11473 w->phys_cursor.x, w->phys_cursor.y);
11474 UNBLOCK_INPUT;
11475 }
11476 }
11477
11478
11479
11480 \f
11481 /* Icons. */
11482
11483 /* Refresh bitmap kitchen sink icon for frame F
11484 when we get an expose event for it. */
11485
11486 void
11487 refreshicon (f)
11488 struct frame *f;
11489 {
11490 /* Normally, the window manager handles this function. */
11491 }
11492
11493 /* Make the x-window of frame F use the gnu icon bitmap. */
11494
11495 int
11496 x_bitmap_icon (f, file)
11497 struct frame *f;
11498 Lisp_Object file;
11499 {
11500 int bitmap_id;
11501
11502 if (FRAME_X_WINDOW (f) == 0)
11503 return 1;
11504
11505 /* Free up our existing icon bitmap if any. */
11506 if (f->output_data.x->icon_bitmap > 0)
11507 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11508 f->output_data.x->icon_bitmap = 0;
11509
11510 if (STRINGP (file))
11511 bitmap_id = x_create_bitmap_from_file (f, file);
11512 else
11513 {
11514 /* Create the GNU bitmap if necessary. */
11515 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
11516 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11517 = x_create_bitmap_from_data (f, gnu_bits,
11518 gnu_width, gnu_height);
11519
11520 /* The first time we create the GNU bitmap,
11521 this increments the ref-count one extra time.
11522 As a result, the GNU bitmap is never freed.
11523 That way, we don't have to worry about allocating it again. */
11524 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
11525
11526 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
11527 }
11528
11529 x_wm_set_icon_pixmap (f, bitmap_id);
11530 f->output_data.x->icon_bitmap = bitmap_id;
11531
11532 return 0;
11533 }
11534
11535
11536 /* Make the x-window of frame F use a rectangle with text.
11537 Use ICON_NAME as the text. */
11538
11539 int
11540 x_text_icon (f, icon_name)
11541 struct frame *f;
11542 char *icon_name;
11543 {
11544 if (FRAME_X_WINDOW (f) == 0)
11545 return 1;
11546
11547 #ifdef HAVE_X11R4
11548 {
11549 XTextProperty text;
11550 text.value = (unsigned char *) icon_name;
11551 text.encoding = XA_STRING;
11552 text.format = 8;
11553 text.nitems = strlen (icon_name);
11554 #ifdef USE_X_TOOLKIT
11555 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11556 &text);
11557 #else /* not USE_X_TOOLKIT */
11558 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11559 #endif /* not USE_X_TOOLKIT */
11560 }
11561 #else /* not HAVE_X11R4 */
11562 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11563 #endif /* not HAVE_X11R4 */
11564
11565 if (f->output_data.x->icon_bitmap > 0)
11566 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11567 f->output_data.x->icon_bitmap = 0;
11568 x_wm_set_icon_pixmap (f, 0);
11569
11570 return 0;
11571 }
11572 \f
11573 #define X_ERROR_MESSAGE_SIZE 200
11574
11575 /* If non-nil, this should be a string.
11576 It means catch X errors and store the error message in this string. */
11577
11578 static Lisp_Object x_error_message_string;
11579
11580 /* An X error handler which stores the error message in
11581 x_error_message_string. This is called from x_error_handler if
11582 x_catch_errors is in effect. */
11583
11584 static void
11585 x_error_catcher (display, error)
11586 Display *display;
11587 XErrorEvent *error;
11588 {
11589 XGetErrorText (display, error->error_code,
11590 XSTRING (x_error_message_string)->data,
11591 X_ERROR_MESSAGE_SIZE);
11592 }
11593
11594 /* Begin trapping X errors for display DPY. Actually we trap X errors
11595 for all displays, but DPY should be the display you are actually
11596 operating on.
11597
11598 After calling this function, X protocol errors no longer cause
11599 Emacs to exit; instead, they are recorded in the string
11600 stored in x_error_message_string.
11601
11602 Calling x_check_errors signals an Emacs error if an X error has
11603 occurred since the last call to x_catch_errors or x_check_errors.
11604
11605 Calling x_uncatch_errors resumes the normal error handling. */
11606
11607 void x_check_errors ();
11608 static Lisp_Object x_catch_errors_unwind ();
11609
11610 int
11611 x_catch_errors (dpy)
11612 Display *dpy;
11613 {
11614 int count = specpdl_ptr - specpdl;
11615
11616 /* Make sure any errors from previous requests have been dealt with. */
11617 XSync (dpy, False);
11618
11619 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11620
11621 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11622 XSTRING (x_error_message_string)->data[0] = 0;
11623
11624 return count;
11625 }
11626
11627 /* Unbind the binding that we made to check for X errors. */
11628
11629 static Lisp_Object
11630 x_catch_errors_unwind (old_val)
11631 Lisp_Object old_val;
11632 {
11633 x_error_message_string = old_val;
11634 return Qnil;
11635 }
11636
11637 /* If any X protocol errors have arrived since the last call to
11638 x_catch_errors or x_check_errors, signal an Emacs error using
11639 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11640
11641 void
11642 x_check_errors (dpy, format)
11643 Display *dpy;
11644 char *format;
11645 {
11646 /* Make sure to catch any errors incurred so far. */
11647 XSync (dpy, False);
11648
11649 if (XSTRING (x_error_message_string)->data[0])
11650 error (format, XSTRING (x_error_message_string)->data);
11651 }
11652
11653 /* Nonzero if we had any X protocol errors
11654 since we did x_catch_errors on DPY. */
11655
11656 int
11657 x_had_errors_p (dpy)
11658 Display *dpy;
11659 {
11660 /* Make sure to catch any errors incurred so far. */
11661 XSync (dpy, False);
11662
11663 return XSTRING (x_error_message_string)->data[0] != 0;
11664 }
11665
11666 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11667
11668 void
11669 x_clear_errors (dpy)
11670 Display *dpy;
11671 {
11672 XSTRING (x_error_message_string)->data[0] = 0;
11673 }
11674
11675 /* Stop catching X protocol errors and let them make Emacs die.
11676 DPY should be the display that was passed to x_catch_errors.
11677 COUNT should be the value that was returned by
11678 the corresponding call to x_catch_errors. */
11679
11680 void
11681 x_uncatch_errors (dpy, count)
11682 Display *dpy;
11683 int count;
11684 {
11685 unbind_to (count, Qnil);
11686 }
11687
11688 #if 0
11689 static unsigned int x_wire_count;
11690 x_trace_wire ()
11691 {
11692 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11693 }
11694 #endif /* ! 0 */
11695
11696 \f
11697 /* Handle SIGPIPE, which can happen when the connection to a server
11698 simply goes away. SIGPIPE is handled by x_connection_signal.
11699 Don't need to do anything, because the write which caused the
11700 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
11701 which will do the appropriate cleanup for us. */
11702
11703 static SIGTYPE
11704 x_connection_signal (signalnum) /* If we don't have an argument, */
11705 int signalnum; /* some compilers complain in signal calls. */
11706 {
11707 #ifdef USG
11708 /* USG systems forget handlers when they are used;
11709 must reestablish each time */
11710 signal (signalnum, x_connection_signal);
11711 #endif /* USG */
11712 }
11713
11714 \f
11715 /************************************************************************
11716 Handling X errors
11717 ************************************************************************/
11718
11719 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
11720 the text of an error message that lead to the connection loss. */
11721
11722 static SIGTYPE
11723 x_connection_closed (dpy, error_message)
11724 Display *dpy;
11725 char *error_message;
11726 {
11727 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
11728 Lisp_Object frame, tail;
11729 int count;
11730 char *msg;
11731
11732 msg = (char *) alloca (strlen (error_message) + 1);
11733 strcpy (msg, error_message);
11734 handling_signal = 0;
11735
11736 /* Prevent being called recursively because of an error condition
11737 below. Otherwise, we might end up with printing ``can't find per
11738 display information'' in the recursive call instead of printing
11739 the original message here. */
11740 count = x_catch_errors (dpy);
11741
11742 /* We have to close the display to inform Xt that it doesn't
11743 exist anymore. If we don't, Xt will continue to wait for
11744 events from the display. As a consequence, a sequence of
11745
11746 M-x make-frame-on-display RET :1 RET
11747 ...kill the new frame, so that we get an IO error...
11748 M-x make-frame-on-display RET :1 RET
11749
11750 will indefinitely wait in Xt for events for display `:1', opened
11751 in the first class to make-frame-on-display.
11752
11753 Closing the display is reported to lead to a bus error on
11754 OpenWindows in certain situations. I suspect that is a bug
11755 in OpenWindows. I don't know how to cicumvent it here. */
11756
11757 #ifdef USE_X_TOOLKIT
11758 /* If DPYINFO is null, this means we didn't open the display
11759 in the first place, so don't try to close it. */
11760 if (dpyinfo)
11761 XtCloseDisplay (dpy);
11762 #endif
11763
11764 /* Indicate that this display is dead. */
11765 if (dpyinfo)
11766 dpyinfo->display = 0;
11767
11768 /* First delete frames whose mini-buffers are on frames
11769 that are on the dead display. */
11770 FOR_EACH_FRAME (tail, frame)
11771 {
11772 Lisp_Object minibuf_frame;
11773 minibuf_frame
11774 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
11775 if (FRAME_X_P (XFRAME (frame))
11776 && FRAME_X_P (XFRAME (minibuf_frame))
11777 && ! EQ (frame, minibuf_frame)
11778 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
11779 Fdelete_frame (frame, Qt);
11780 }
11781
11782 /* Now delete all remaining frames on the dead display.
11783 We are now sure none of these is used as the mini-buffer
11784 for another frame that we need to delete. */
11785 FOR_EACH_FRAME (tail, frame)
11786 if (FRAME_X_P (XFRAME (frame))
11787 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
11788 {
11789 /* Set this to t so that Fdelete_frame won't get confused
11790 trying to find a replacement. */
11791 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11792 Fdelete_frame (frame, Qt);
11793 }
11794
11795 if (dpyinfo)
11796 x_delete_display (dpyinfo);
11797
11798 x_uncatch_errors (dpy, count);
11799
11800 if (x_display_list == 0)
11801 {
11802 fprintf (stderr, "%s\n", msg);
11803 shut_down_emacs (0, 0, Qnil);
11804 exit (70);
11805 }
11806
11807 /* Ordinary stack unwind doesn't deal with these. */
11808 #ifdef SIGIO
11809 sigunblock (sigmask (SIGIO));
11810 #endif
11811 sigunblock (sigmask (SIGALRM));
11812 TOTALLY_UNBLOCK_INPUT;
11813
11814 clear_waiting_for_input ();
11815 error ("%s", msg);
11816 }
11817
11818
11819 /* This is the usual handler for X protocol errors.
11820 It kills all frames on the display that we got the error for.
11821 If that was the only one, it prints an error message and kills Emacs. */
11822
11823 static void
11824 x_error_quitter (display, error)
11825 Display *display;
11826 XErrorEvent *error;
11827 {
11828 char buf[256], buf1[356];
11829
11830 /* Note that there is no real way portable across R3/R4 to get the
11831 original error handler. */
11832
11833 XGetErrorText (display, error->error_code, buf, sizeof (buf));
11834 sprintf (buf1, "X protocol error: %s on protocol request %d",
11835 buf, error->request_code);
11836 x_connection_closed (display, buf1);
11837 }
11838
11839
11840 /* This is the first-level handler for X protocol errors.
11841 It calls x_error_quitter or x_error_catcher. */
11842
11843 static int
11844 x_error_handler (display, error)
11845 Display *display;
11846 XErrorEvent *error;
11847 {
11848 if (! NILP (x_error_message_string))
11849 x_error_catcher (display, error);
11850 else
11851 x_error_quitter (display, error);
11852 return 0;
11853 }
11854
11855 /* This is the handler for X IO errors, always.
11856 It kills all frames on the display that we lost touch with.
11857 If that was the only one, it prints an error message and kills Emacs. */
11858
11859 static int
11860 x_io_error_quitter (display)
11861 Display *display;
11862 {
11863 char buf[256];
11864
11865 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11866 x_connection_closed (display, buf);
11867 return 0;
11868 }
11869 \f
11870 /* Changing the font of the frame. */
11871
11872 /* Give frame F the font named FONTNAME as its default font, and
11873 return the full name of that font. FONTNAME may be a wildcard
11874 pattern; in that case, we choose some font that fits the pattern.
11875 The return value shows which font we chose. */
11876
11877 Lisp_Object
11878 x_new_font (f, fontname)
11879 struct frame *f;
11880 register char *fontname;
11881 {
11882 struct font_info *fontp
11883 = FS_LOAD_FONT (f, 0, fontname, -1);
11884
11885 if (!fontp)
11886 return Qnil;
11887
11888 f->output_data.x->font = (XFontStruct *) (fontp->font);
11889 f->output_data.x->baseline_offset = fontp->baseline_offset;
11890 f->output_data.x->fontset = -1;
11891
11892 /* Compute the scroll bar width in character columns. */
11893 if (f->scroll_bar_pixel_width > 0)
11894 {
11895 int wid = FONT_WIDTH (f->output_data.x->font);
11896 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11897 }
11898 else
11899 {
11900 int wid = FONT_WIDTH (f->output_data.x->font);
11901 f->scroll_bar_cols = (14 + wid - 1) / wid;
11902 }
11903
11904 /* Now make the frame display the given font. */
11905 if (FRAME_X_WINDOW (f) != 0)
11906 {
11907 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11908 f->output_data.x->font->fid);
11909 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11910 f->output_data.x->font->fid);
11911 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11912 f->output_data.x->font->fid);
11913
11914 frame_update_line_height (f);
11915
11916 /* Don't change the size of a tip frame; there's no point in
11917 doing it because it's done in Fx_show_tip, and it leads to
11918 problems because the tip frame has no widget. */
11919 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
11920 x_set_window_size (f, 0, f->width, f->height);
11921 }
11922 else
11923 /* If we are setting a new frame's font for the first time,
11924 there are no faces yet, so this font's height is the line height. */
11925 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
11926
11927 return build_string (fontp->full_name);
11928 }
11929
11930 /* Give frame F the fontset named FONTSETNAME as its default font, and
11931 return the full name of that fontset. FONTSETNAME may be a wildcard
11932 pattern; in that case, we choose some fontset that fits the pattern.
11933 The return value shows which fontset we chose. */
11934
11935 Lisp_Object
11936 x_new_fontset (f, fontsetname)
11937 struct frame *f;
11938 char *fontsetname;
11939 {
11940 int fontset = fs_query_fontset (build_string (fontsetname), 0);
11941 Lisp_Object result;
11942
11943 if (fontset < 0)
11944 return Qnil;
11945
11946 if (f->output_data.x->fontset == fontset)
11947 /* This fontset is already set in frame F. There's nothing more
11948 to do. */
11949 return fontset_name (fontset);
11950
11951 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
11952
11953 if (!STRINGP (result))
11954 /* Can't load ASCII font. */
11955 return Qnil;
11956
11957 /* Since x_new_font doesn't update any fontset information, do it now. */
11958 f->output_data.x->fontset = fontset;
11959
11960 #ifdef HAVE_X_I18N
11961 if (FRAME_XIC (f)
11962 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
11963 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
11964 #endif
11965
11966 return build_string (fontsetname);
11967 }
11968
11969 \f
11970 /***********************************************************************
11971 X Input Methods
11972 ***********************************************************************/
11973
11974 #ifdef HAVE_X_I18N
11975
11976 #ifdef HAVE_X11R6
11977
11978 /* XIM destroy callback function, which is called whenever the
11979 connection to input method XIM dies. CLIENT_DATA contains a
11980 pointer to the x_display_info structure corresponding to XIM. */
11981
11982 static void
11983 xim_destroy_callback (xim, client_data, call_data)
11984 XIM xim;
11985 XPointer client_data;
11986 XPointer call_data;
11987 {
11988 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11989 Lisp_Object frame, tail;
11990
11991 BLOCK_INPUT;
11992
11993 /* No need to call XDestroyIC.. */
11994 FOR_EACH_FRAME (tail, frame)
11995 {
11996 struct frame *f = XFRAME (frame);
11997 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11998 {
11999 FRAME_XIC (f) = NULL;
12000 if (FRAME_XIC_FONTSET (f))
12001 {
12002 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
12003 FRAME_XIC_FONTSET (f) = NULL;
12004 }
12005 }
12006 }
12007
12008 /* No need to call XCloseIM. */
12009 dpyinfo->xim = NULL;
12010 XFree (dpyinfo->xim_styles);
12011 UNBLOCK_INPUT;
12012 }
12013
12014 #endif /* HAVE_X11R6 */
12015
12016 /* Open the connection to the XIM server on display DPYINFO.
12017 RESOURCE_NAME is the resource name Emacs uses. */
12018
12019 static void
12020 xim_open_dpy (dpyinfo, resource_name)
12021 struct x_display_info *dpyinfo;
12022 char *resource_name;
12023 {
12024 #ifdef USE_XIM
12025 XIM xim;
12026
12027 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
12028 dpyinfo->xim = xim;
12029
12030 if (xim)
12031 {
12032 #ifdef HAVE_X11R6
12033 XIMCallback destroy;
12034 #endif
12035
12036 /* Get supported styles and XIM values. */
12037 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
12038
12039 #ifdef HAVE_X11R6
12040 destroy.callback = xim_destroy_callback;
12041 destroy.client_data = (XPointer)dpyinfo;
12042 /* This isn't prototyped in OSF 5.0. */
12043 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
12044 #endif
12045 }
12046
12047 #else /* not USE_XIM */
12048 dpyinfo->xim = NULL;
12049 #endif /* not USE_XIM */
12050 }
12051
12052
12053 #ifdef HAVE_X11R6_XIM
12054
12055 struct xim_inst_t
12056 {
12057 struct x_display_info *dpyinfo;
12058 char *resource_name;
12059 };
12060
12061 /* XIM instantiate callback function, which is called whenever an XIM
12062 server is available. DISPLAY is teh display of the XIM.
12063 CLIENT_DATA contains a pointer to an xim_inst_t structure created
12064 when the callback was registered. */
12065
12066 static void
12067 xim_instantiate_callback (display, client_data, call_data)
12068 Display *display;
12069 XPointer client_data;
12070 XPointer call_data;
12071 {
12072 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
12073 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
12074
12075 /* We don't support multiple XIM connections. */
12076 if (dpyinfo->xim)
12077 return;
12078
12079 xim_open_dpy (dpyinfo, xim_inst->resource_name);
12080
12081 /* Create XIC for the existing frames on the same display, as long
12082 as they have no XIC. */
12083 if (dpyinfo->xim && dpyinfo->reference_count > 0)
12084 {
12085 Lisp_Object tail, frame;
12086
12087 BLOCK_INPUT;
12088 FOR_EACH_FRAME (tail, frame)
12089 {
12090 struct frame *f = XFRAME (frame);
12091
12092 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
12093 if (FRAME_XIC (f) == NULL)
12094 {
12095 create_frame_xic (f);
12096 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
12097 xic_set_statusarea (f);
12098 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
12099 {
12100 struct window *w = XWINDOW (f->selected_window);
12101 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
12102 }
12103 }
12104 }
12105
12106 UNBLOCK_INPUT;
12107 }
12108 }
12109
12110 #endif /* HAVE_X11R6_XIM */
12111
12112
12113 /* Open a connection to the XIM server on display DPYINFO.
12114 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
12115 connection only at the first time. On X11R6, open the connection
12116 in the XIM instantiate callback function. */
12117
12118 static void
12119 xim_initialize (dpyinfo, resource_name)
12120 struct x_display_info *dpyinfo;
12121 char *resource_name;
12122 {
12123 #ifdef USE_XIM
12124 #ifdef HAVE_X11R6_XIM
12125 struct xim_inst_t *xim_inst;
12126 int len;
12127
12128 dpyinfo->xim = NULL;
12129 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
12130 xim_inst->dpyinfo = dpyinfo;
12131 len = strlen (resource_name);
12132 xim_inst->resource_name = (char *) xmalloc (len + 1);
12133 bcopy (resource_name, xim_inst->resource_name, len + 1);
12134 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12135 resource_name, EMACS_CLASS,
12136 xim_instantiate_callback,
12137 /* Fixme: This is XPointer in
12138 XFree86 but (XPointer *) on
12139 Tru64, at least. */
12140 (XPointer) xim_inst);
12141 #else /* not HAVE_X11R6_XIM */
12142 dpyinfo->xim = NULL;
12143 xim_open_dpy (dpyinfo, resource_name);
12144 #endif /* not HAVE_X11R6_XIM */
12145
12146 #else /* not USE_XIM */
12147 dpyinfo->xim = NULL;
12148 #endif /* not USE_XIM */
12149 }
12150
12151
12152 /* Close the connection to the XIM server on display DPYINFO. */
12153
12154 static void
12155 xim_close_dpy (dpyinfo)
12156 struct x_display_info *dpyinfo;
12157 {
12158 #ifdef USE_XIM
12159 #ifdef HAVE_X11R6_XIM
12160 if (dpyinfo->display)
12161 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12162 NULL, EMACS_CLASS,
12163 xim_instantiate_callback, NULL);
12164 #endif /* not HAVE_X11R6_XIM */
12165 if (dpyinfo->display)
12166 XCloseIM (dpyinfo->xim);
12167 dpyinfo->xim = NULL;
12168 XFree (dpyinfo->xim_styles);
12169 #endif /* USE_XIM */
12170 }
12171
12172 #endif /* not HAVE_X11R6_XIM */
12173
12174
12175 \f
12176 /* Calculate the absolute position in frame F
12177 from its current recorded position values and gravity. */
12178
12179 void
12180 x_calc_absolute_position (f)
12181 struct frame *f;
12182 {
12183 Window child;
12184 int win_x = 0, win_y = 0;
12185 int flags = f->output_data.x->size_hint_flags;
12186 int this_window;
12187
12188 /* We have nothing to do if the current position
12189 is already for the top-left corner. */
12190 if (! ((flags & XNegative) || (flags & YNegative)))
12191 return;
12192
12193 #ifdef USE_X_TOOLKIT
12194 this_window = XtWindow (f->output_data.x->widget);
12195 #else
12196 this_window = FRAME_X_WINDOW (f);
12197 #endif
12198
12199 /* Find the position of the outside upper-left corner of
12200 the inner window, with respect to the outer window.
12201 But do this only if we will need the results. */
12202 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
12203 {
12204 int count;
12205
12206 BLOCK_INPUT;
12207 count = x_catch_errors (FRAME_X_DISPLAY (f));
12208 while (1)
12209 {
12210 x_clear_errors (FRAME_X_DISPLAY (f));
12211 XTranslateCoordinates (FRAME_X_DISPLAY (f),
12212
12213 /* From-window, to-window. */
12214 this_window,
12215 f->output_data.x->parent_desc,
12216
12217 /* From-position, to-position. */
12218 0, 0, &win_x, &win_y,
12219
12220 /* Child of win. */
12221 &child);
12222 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12223 {
12224 Window newroot, newparent = 0xdeadbeef;
12225 Window *newchildren;
12226 unsigned int nchildren;
12227
12228 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
12229 &newparent, &newchildren, &nchildren))
12230 break;
12231
12232 XFree ((char *) newchildren);
12233
12234 f->output_data.x->parent_desc = newparent;
12235 }
12236 else
12237 break;
12238 }
12239
12240 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12241 UNBLOCK_INPUT;
12242 }
12243
12244 /* Treat negative positions as relative to the leftmost bottommost
12245 position that fits on the screen. */
12246 if (flags & XNegative)
12247 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
12248 - 2 * f->output_data.x->border_width - win_x
12249 - PIXEL_WIDTH (f)
12250 + f->output_data.x->left_pos);
12251
12252 {
12253 int height = PIXEL_HEIGHT (f);
12254
12255 #if defined USE_X_TOOLKIT && defined USE_MOTIF
12256 /* Something is fishy here. When using Motif, starting Emacs with
12257 `-g -0-0', the frame appears too low by a few pixels.
12258
12259 This seems to be so because initially, while Emacs is starting,
12260 the column widget's height and the frame's pixel height are
12261 different. The column widget's height is the right one. In
12262 later invocations, when Emacs is up, the frame's pixel height
12263 is right, though.
12264
12265 It's not obvious where the initial small difference comes from.
12266 2000-12-01, gerd. */
12267
12268 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
12269 #endif
12270
12271 if (flags & YNegative)
12272 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
12273 - 2 * f->output_data.x->border_width
12274 - win_y
12275 - height
12276 + f->output_data.x->top_pos);
12277 }
12278
12279 /* The left_pos and top_pos
12280 are now relative to the top and left screen edges,
12281 so the flags should correspond. */
12282 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12283 }
12284
12285 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
12286 to really change the position, and 0 when calling from
12287 x_make_frame_visible (in that case, XOFF and YOFF are the current
12288 position values). It is -1 when calling from x_set_frame_parameters,
12289 which means, do adjust for borders but don't change the gravity. */
12290
12291 void
12292 x_set_offset (f, xoff, yoff, change_gravity)
12293 struct frame *f;
12294 register int xoff, yoff;
12295 int change_gravity;
12296 {
12297 int modified_top, modified_left;
12298
12299 if (change_gravity > 0)
12300 {
12301 f->output_data.x->top_pos = yoff;
12302 f->output_data.x->left_pos = xoff;
12303 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12304 if (xoff < 0)
12305 f->output_data.x->size_hint_flags |= XNegative;
12306 if (yoff < 0)
12307 f->output_data.x->size_hint_flags |= YNegative;
12308 f->output_data.x->win_gravity = NorthWestGravity;
12309 }
12310 x_calc_absolute_position (f);
12311
12312 BLOCK_INPUT;
12313 x_wm_set_size_hint (f, (long) 0, 0);
12314
12315 modified_left = f->output_data.x->left_pos;
12316 modified_top = f->output_data.x->top_pos;
12317 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
12318 this seems to be unnecessary and incorrect. rms, 4/17/97. */
12319 /* It is a mystery why we need to add the border_width here
12320 when the frame is already visible, but experiment says we do. */
12321 if (change_gravity != 0)
12322 {
12323 modified_left += f->output_data.x->border_width;
12324 modified_top += f->output_data.x->border_width;
12325 }
12326 #endif
12327
12328 #ifdef USE_X_TOOLKIT
12329 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
12330 modified_left, modified_top);
12331 #else /* not USE_X_TOOLKIT */
12332 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12333 modified_left, modified_top);
12334 #endif /* not USE_X_TOOLKIT */
12335 UNBLOCK_INPUT;
12336 }
12337
12338
12339 /* Change the size of frame F's X window to COLS/ROWS in the case F
12340 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
12341 top-left-corner window gravity for this size change and subsequent
12342 size changes. Otherwise we leave the window gravity unchanged. */
12343
12344 static void
12345 x_set_window_size_1 (f, change_gravity, cols, rows)
12346 struct frame *f;
12347 int change_gravity;
12348 int cols, rows;
12349 {
12350 int pixelwidth, pixelheight;
12351
12352 check_frame_size (f, &rows, &cols);
12353 f->output_data.x->vertical_scroll_bar_extra
12354 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
12355 ? 0
12356 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
12357 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
12358 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
12359 f->output_data.x->flags_areas_extra
12360 = FRAME_FLAGS_AREA_WIDTH (f);
12361 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
12362 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
12363
12364 f->output_data.x->win_gravity = NorthWestGravity;
12365 x_wm_set_size_hint (f, (long) 0, 0);
12366
12367 XSync (FRAME_X_DISPLAY (f), False);
12368 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12369 pixelwidth, pixelheight);
12370
12371 /* Now, strictly speaking, we can't be sure that this is accurate,
12372 but the window manager will get around to dealing with the size
12373 change request eventually, and we'll hear how it went when the
12374 ConfigureNotify event gets here.
12375
12376 We could just not bother storing any of this information here,
12377 and let the ConfigureNotify event set everything up, but that
12378 might be kind of confusing to the Lisp code, since size changes
12379 wouldn't be reported in the frame parameters until some random
12380 point in the future when the ConfigureNotify event arrives.
12381
12382 We pass 1 for DELAY since we can't run Lisp code inside of
12383 a BLOCK_INPUT. */
12384 change_frame_size (f, rows, cols, 0, 1, 0);
12385 PIXEL_WIDTH (f) = pixelwidth;
12386 PIXEL_HEIGHT (f) = pixelheight;
12387
12388 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
12389 receive in the ConfigureNotify event; if we get what we asked
12390 for, then the event won't cause the screen to become garbaged, so
12391 we have to make sure to do it here. */
12392 SET_FRAME_GARBAGED (f);
12393
12394 XFlush (FRAME_X_DISPLAY (f));
12395 }
12396
12397
12398 /* Call this to change the size of frame F's x-window.
12399 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
12400 for this size change and subsequent size changes.
12401 Otherwise we leave the window gravity unchanged. */
12402
12403 void
12404 x_set_window_size (f, change_gravity, cols, rows)
12405 struct frame *f;
12406 int change_gravity;
12407 int cols, rows;
12408 {
12409 BLOCK_INPUT;
12410
12411 #ifdef USE_X_TOOLKIT
12412
12413 if (f->output_data.x->widget != NULL)
12414 {
12415 /* The x and y position of the widget is clobbered by the
12416 call to XtSetValues within EmacsFrameSetCharSize.
12417 This is a real kludge, but I don't understand Xt so I can't
12418 figure out a correct fix. Can anyone else tell me? -- rms. */
12419 int xpos = f->output_data.x->widget->core.x;
12420 int ypos = f->output_data.x->widget->core.y;
12421 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
12422 f->output_data.x->widget->core.x = xpos;
12423 f->output_data.x->widget->core.y = ypos;
12424 }
12425 else
12426 x_set_window_size_1 (f, change_gravity, cols, rows);
12427
12428 #else /* not USE_X_TOOLKIT */
12429
12430 x_set_window_size_1 (f, change_gravity, cols, rows);
12431
12432 #endif /* not USE_X_TOOLKIT */
12433
12434 /* If cursor was outside the new size, mark it as off. */
12435 mark_window_cursors_off (XWINDOW (f->root_window));
12436
12437 /* Clear out any recollection of where the mouse highlighting was,
12438 since it might be in a place that's outside the new frame size.
12439 Actually checking whether it is outside is a pain in the neck,
12440 so don't try--just let the highlighting be done afresh with new size. */
12441 cancel_mouse_face (f);
12442
12443 UNBLOCK_INPUT;
12444 }
12445 \f
12446 /* Mouse warping. */
12447
12448 void
12449 x_set_mouse_position (f, x, y)
12450 struct frame *f;
12451 int x, y;
12452 {
12453 int pix_x, pix_y;
12454
12455 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
12456 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
12457
12458 if (pix_x < 0) pix_x = 0;
12459 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
12460
12461 if (pix_y < 0) pix_y = 0;
12462 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
12463
12464 BLOCK_INPUT;
12465
12466 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12467 0, 0, 0, 0, pix_x, pix_y);
12468 UNBLOCK_INPUT;
12469 }
12470
12471 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
12472
12473 void
12474 x_set_mouse_pixel_position (f, pix_x, pix_y)
12475 struct frame *f;
12476 int pix_x, pix_y;
12477 {
12478 BLOCK_INPUT;
12479
12480 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12481 0, 0, 0, 0, pix_x, pix_y);
12482 UNBLOCK_INPUT;
12483 }
12484 \f
12485 /* focus shifting, raising and lowering. */
12486
12487 void
12488 x_focus_on_frame (f)
12489 struct frame *f;
12490 {
12491 #if 0 /* This proves to be unpleasant. */
12492 x_raise_frame (f);
12493 #endif
12494 #if 0
12495 /* I don't think that the ICCCM allows programs to do things like this
12496 without the interaction of the window manager. Whatever you end up
12497 doing with this code, do it to x_unfocus_frame too. */
12498 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12499 RevertToPointerRoot, CurrentTime);
12500 #endif /* ! 0 */
12501 }
12502
12503 void
12504 x_unfocus_frame (f)
12505 struct frame *f;
12506 {
12507 #if 0
12508 /* Look at the remarks in x_focus_on_frame. */
12509 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
12510 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
12511 RevertToPointerRoot, CurrentTime);
12512 #endif /* ! 0 */
12513 }
12514
12515 /* Raise frame F. */
12516
12517 void
12518 x_raise_frame (f)
12519 struct frame *f;
12520 {
12521 if (f->async_visible)
12522 {
12523 BLOCK_INPUT;
12524 #ifdef USE_X_TOOLKIT
12525 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12526 #else /* not USE_X_TOOLKIT */
12527 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12528 #endif /* not USE_X_TOOLKIT */
12529 XFlush (FRAME_X_DISPLAY (f));
12530 UNBLOCK_INPUT;
12531 }
12532 }
12533
12534 /* Lower frame F. */
12535
12536 void
12537 x_lower_frame (f)
12538 struct frame *f;
12539 {
12540 if (f->async_visible)
12541 {
12542 BLOCK_INPUT;
12543 #ifdef USE_X_TOOLKIT
12544 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12545 #else /* not USE_X_TOOLKIT */
12546 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12547 #endif /* not USE_X_TOOLKIT */
12548 XFlush (FRAME_X_DISPLAY (f));
12549 UNBLOCK_INPUT;
12550 }
12551 }
12552
12553 static void
12554 XTframe_raise_lower (f, raise_flag)
12555 FRAME_PTR f;
12556 int raise_flag;
12557 {
12558 if (raise_flag)
12559 x_raise_frame (f);
12560 else
12561 x_lower_frame (f);
12562 }
12563 \f
12564 /* Change of visibility. */
12565
12566 /* This tries to wait until the frame is really visible.
12567 However, if the window manager asks the user where to position
12568 the frame, this will return before the user finishes doing that.
12569 The frame will not actually be visible at that time,
12570 but it will become visible later when the window manager
12571 finishes with it. */
12572
12573 void
12574 x_make_frame_visible (f)
12575 struct frame *f;
12576 {
12577 Lisp_Object type;
12578 int original_top, original_left;
12579 int retry_count = 2;
12580
12581 retry:
12582
12583 BLOCK_INPUT;
12584
12585 type = x_icon_type (f);
12586 if (!NILP (type))
12587 x_bitmap_icon (f, type);
12588
12589 if (! FRAME_VISIBLE_P (f))
12590 {
12591 /* We test FRAME_GARBAGED_P here to make sure we don't
12592 call x_set_offset a second time
12593 if we get to x_make_frame_visible a second time
12594 before the window gets really visible. */
12595 if (! FRAME_ICONIFIED_P (f)
12596 && ! f->output_data.x->asked_for_visible)
12597 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12598
12599 f->output_data.x->asked_for_visible = 1;
12600
12601 if (! EQ (Vx_no_window_manager, Qt))
12602 x_wm_set_window_state (f, NormalState);
12603 #ifdef USE_X_TOOLKIT
12604 /* This was XtPopup, but that did nothing for an iconified frame. */
12605 XtMapWidget (f->output_data.x->widget);
12606 #else /* not USE_X_TOOLKIT */
12607 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12608 #endif /* not USE_X_TOOLKIT */
12609 #if 0 /* This seems to bring back scroll bars in the wrong places
12610 if the window configuration has changed. They seem
12611 to come back ok without this. */
12612 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
12613 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12614 #endif
12615 }
12616
12617 XFlush (FRAME_X_DISPLAY (f));
12618
12619 /* Synchronize to ensure Emacs knows the frame is visible
12620 before we do anything else. We do this loop with input not blocked
12621 so that incoming events are handled. */
12622 {
12623 Lisp_Object frame;
12624 int count;
12625 /* This must be before UNBLOCK_INPUT
12626 since events that arrive in response to the actions above
12627 will set it when they are handled. */
12628 int previously_visible = f->output_data.x->has_been_visible;
12629
12630 original_left = f->output_data.x->left_pos;
12631 original_top = f->output_data.x->top_pos;
12632
12633 /* This must come after we set COUNT. */
12634 UNBLOCK_INPUT;
12635
12636 /* We unblock here so that arriving X events are processed. */
12637
12638 /* Now move the window back to where it was "supposed to be".
12639 But don't do it if the gravity is negative.
12640 When the gravity is negative, this uses a position
12641 that is 3 pixels too low. Perhaps that's really the border width.
12642
12643 Don't do this if the window has never been visible before,
12644 because the window manager may choose the position
12645 and we don't want to override it. */
12646
12647 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
12648 && f->output_data.x->win_gravity == NorthWestGravity
12649 && previously_visible)
12650 {
12651 Drawable rootw;
12652 int x, y;
12653 unsigned int width, height, border, depth;
12654
12655 BLOCK_INPUT;
12656
12657 /* On some window managers (such as FVWM) moving an existing
12658 window, even to the same place, causes the window manager
12659 to introduce an offset. This can cause the window to move
12660 to an unexpected location. Check the geometry (a little
12661 slow here) and then verify that the window is in the right
12662 place. If the window is not in the right place, move it
12663 there, and take the potential window manager hit. */
12664 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12665 &rootw, &x, &y, &width, &height, &border, &depth);
12666
12667 if (original_left != x || original_top != y)
12668 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12669 original_left, original_top);
12670
12671 UNBLOCK_INPUT;
12672 }
12673
12674 XSETFRAME (frame, f);
12675
12676 /* Wait until the frame is visible. Process X events until a
12677 MapNotify event has been seen, or until we think we won't get a
12678 MapNotify at all.. */
12679 for (count = input_signal_count + 10;
12680 input_signal_count < count && !FRAME_VISIBLE_P (f);)
12681 {
12682 /* Force processing of queued events. */
12683 x_sync (f);
12684
12685 /* Machines that do polling rather than SIGIO have been
12686 observed to go into a busy-wait here. So we'll fake an
12687 alarm signal to let the handler know that there's something
12688 to be read. We used to raise a real alarm, but it seems
12689 that the handler isn't always enabled here. This is
12690 probably a bug. */
12691 if (input_polling_used ())
12692 {
12693 /* It could be confusing if a real alarm arrives while
12694 processing the fake one. Turn it off and let the
12695 handler reset it. */
12696 extern void poll_for_input_1 P_ ((void));
12697 int old_poll_suppress_count = poll_suppress_count;
12698 poll_suppress_count = 1;
12699 poll_for_input_1 ();
12700 poll_suppress_count = old_poll_suppress_count;
12701 }
12702
12703 /* See if a MapNotify event has been processed. */
12704 FRAME_SAMPLE_VISIBILITY (f);
12705 }
12706
12707 /* 2000-09-28: In
12708
12709 (let ((f (selected-frame)))
12710 (iconify-frame f)
12711 (raise-frame f))
12712
12713 the frame is not raised with various window managers on
12714 FreeBSD, Linux and Solaris. It turns out that, for some
12715 unknown reason, the call to XtMapWidget is completely ignored.
12716 Mapping the widget a second time works. */
12717
12718 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
12719 goto retry;
12720 }
12721 }
12722
12723 /* Change from mapped state to withdrawn state. */
12724
12725 /* Make the frame visible (mapped and not iconified). */
12726
12727 void
12728 x_make_frame_invisible (f)
12729 struct frame *f;
12730 {
12731 Window window;
12732
12733 #ifdef USE_X_TOOLKIT
12734 /* Use the frame's outermost window, not the one we normally draw on. */
12735 window = XtWindow (f->output_data.x->widget);
12736 #else /* not USE_X_TOOLKIT */
12737 window = FRAME_X_WINDOW (f);
12738 #endif /* not USE_X_TOOLKIT */
12739
12740 /* Don't keep the highlight on an invisible frame. */
12741 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12742 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12743
12744 #if 0/* This might add unreliability; I don't trust it -- rms. */
12745 if (! f->async_visible && ! f->async_iconified)
12746 return;
12747 #endif
12748
12749 BLOCK_INPUT;
12750
12751 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
12752 that the current position of the window is user-specified, rather than
12753 program-specified, so that when the window is mapped again, it will be
12754 placed at the same location, without forcing the user to position it
12755 by hand again (they have already done that once for this window.) */
12756 x_wm_set_size_hint (f, (long) 0, 1);
12757
12758 #ifdef HAVE_X11R4
12759
12760 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
12761 DefaultScreen (FRAME_X_DISPLAY (f))))
12762 {
12763 UNBLOCK_INPUT_RESIGNAL;
12764 error ("Can't notify window manager of window withdrawal");
12765 }
12766 #else /* ! defined (HAVE_X11R4) */
12767
12768 /* Tell the window manager what we're going to do. */
12769 if (! EQ (Vx_no_window_manager, Qt))
12770 {
12771 XEvent unmap;
12772
12773 unmap.xunmap.type = UnmapNotify;
12774 unmap.xunmap.window = window;
12775 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
12776 unmap.xunmap.from_configure = False;
12777 if (! XSendEvent (FRAME_X_DISPLAY (f),
12778 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12779 False,
12780 SubstructureRedirectMaskSubstructureNotifyMask,
12781 &unmap))
12782 {
12783 UNBLOCK_INPUT_RESIGNAL;
12784 error ("Can't notify window manager of withdrawal");
12785 }
12786 }
12787
12788 /* Unmap the window ourselves. Cheeky! */
12789 XUnmapWindow (FRAME_X_DISPLAY (f), window);
12790 #endif /* ! defined (HAVE_X11R4) */
12791
12792 /* We can't distinguish this from iconification
12793 just by the event that we get from the server.
12794 So we can't win using the usual strategy of letting
12795 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
12796 and synchronize with the server to make sure we agree. */
12797 f->visible = 0;
12798 FRAME_ICONIFIED_P (f) = 0;
12799 f->async_visible = 0;
12800 f->async_iconified = 0;
12801
12802 x_sync (f);
12803
12804 UNBLOCK_INPUT;
12805 }
12806
12807 /* Change window state from mapped to iconified. */
12808
12809 void
12810 x_iconify_frame (f)
12811 struct frame *f;
12812 {
12813 int result;
12814 Lisp_Object type;
12815
12816 /* Don't keep the highlight on an invisible frame. */
12817 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12818 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12819
12820 if (f->async_iconified)
12821 return;
12822
12823 BLOCK_INPUT;
12824
12825 FRAME_SAMPLE_VISIBILITY (f);
12826
12827 type = x_icon_type (f);
12828 if (!NILP (type))
12829 x_bitmap_icon (f, type);
12830
12831 #ifdef USE_X_TOOLKIT
12832
12833 if (! FRAME_VISIBLE_P (f))
12834 {
12835 if (! EQ (Vx_no_window_manager, Qt))
12836 x_wm_set_window_state (f, IconicState);
12837 /* This was XtPopup, but that did nothing for an iconified frame. */
12838 XtMapWidget (f->output_data.x->widget);
12839 /* The server won't give us any event to indicate
12840 that an invisible frame was changed to an icon,
12841 so we have to record it here. */
12842 f->iconified = 1;
12843 f->visible = 1;
12844 f->async_iconified = 1;
12845 f->async_visible = 0;
12846 UNBLOCK_INPUT;
12847 return;
12848 }
12849
12850 result = XIconifyWindow (FRAME_X_DISPLAY (f),
12851 XtWindow (f->output_data.x->widget),
12852 DefaultScreen (FRAME_X_DISPLAY (f)));
12853 UNBLOCK_INPUT;
12854
12855 if (!result)
12856 error ("Can't notify window manager of iconification");
12857
12858 f->async_iconified = 1;
12859 f->async_visible = 0;
12860
12861
12862 BLOCK_INPUT;
12863 XFlush (FRAME_X_DISPLAY (f));
12864 UNBLOCK_INPUT;
12865 #else /* not USE_X_TOOLKIT */
12866
12867 /* Make sure the X server knows where the window should be positioned,
12868 in case the user deiconifies with the window manager. */
12869 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
12870 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12871
12872 /* Since we don't know which revision of X we're running, we'll use both
12873 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
12874
12875 /* X11R4: send a ClientMessage to the window manager using the
12876 WM_CHANGE_STATE type. */
12877 {
12878 XEvent message;
12879
12880 message.xclient.window = FRAME_X_WINDOW (f);
12881 message.xclient.type = ClientMessage;
12882 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
12883 message.xclient.format = 32;
12884 message.xclient.data.l[0] = IconicState;
12885
12886 if (! XSendEvent (FRAME_X_DISPLAY (f),
12887 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12888 False,
12889 SubstructureRedirectMask | SubstructureNotifyMask,
12890 &message))
12891 {
12892 UNBLOCK_INPUT_RESIGNAL;
12893 error ("Can't notify window manager of iconification");
12894 }
12895 }
12896
12897 /* X11R3: set the initial_state field of the window manager hints to
12898 IconicState. */
12899 x_wm_set_window_state (f, IconicState);
12900
12901 if (!FRAME_VISIBLE_P (f))
12902 {
12903 /* If the frame was withdrawn, before, we must map it. */
12904 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12905 }
12906
12907 f->async_iconified = 1;
12908 f->async_visible = 0;
12909
12910 XFlush (FRAME_X_DISPLAY (f));
12911 UNBLOCK_INPUT;
12912 #endif /* not USE_X_TOOLKIT */
12913 }
12914
12915 \f
12916 /* Free X resources of frame F. */
12917
12918 void
12919 x_free_frame_resources (f)
12920 struct frame *f;
12921 {
12922 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12923
12924 BLOCK_INPUT;
12925
12926 /* If a display connection is dead, don't try sending more
12927 commands to the X server. */
12928 if (dpyinfo->display)
12929 {
12930 if (f->output_data.x->icon_desc)
12931 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
12932
12933 #ifdef HAVE_X_I18N
12934 if (FRAME_XIC (f))
12935 free_frame_xic (f);
12936 #endif
12937
12938 if (FRAME_X_WINDOW (f))
12939 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12940
12941 #ifdef USE_X_TOOLKIT
12942 if (f->output_data.x->widget)
12943 {
12944 XtDestroyWidget (f->output_data.x->widget);
12945 f->output_data.x->widget = NULL;
12946 }
12947 free_frame_menubar (f);
12948 #endif /* USE_X_TOOLKIT */
12949
12950 unload_color (f, f->output_data.x->foreground_pixel);
12951 unload_color (f, f->output_data.x->background_pixel);
12952 unload_color (f, f->output_data.x->cursor_pixel);
12953 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12954 unload_color (f, f->output_data.x->border_pixel);
12955 unload_color (f, f->output_data.x->mouse_pixel);
12956
12957 if (f->output_data.x->scroll_bar_background_pixel != -1)
12958 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
12959 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
12960 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
12961 if (f->output_data.x->white_relief.allocated_p)
12962 unload_color (f, f->output_data.x->white_relief.pixel);
12963 if (f->output_data.x->black_relief.allocated_p)
12964 unload_color (f, f->output_data.x->black_relief.pixel);
12965
12966 if (FRAME_FACE_CACHE (f))
12967 free_frame_faces (f);
12968
12969 x_free_gcs (f);
12970 XFlush (FRAME_X_DISPLAY (f));
12971 }
12972
12973 if (f->output_data.x->saved_menu_event)
12974 xfree (f->output_data.x->saved_menu_event);
12975
12976 xfree (f->output_data.x);
12977 f->output_data.x = NULL;
12978
12979 if (f == dpyinfo->x_focus_frame)
12980 dpyinfo->x_focus_frame = 0;
12981 if (f == dpyinfo->x_focus_event_frame)
12982 dpyinfo->x_focus_event_frame = 0;
12983 if (f == dpyinfo->x_highlight_frame)
12984 dpyinfo->x_highlight_frame = 0;
12985
12986 if (f == dpyinfo->mouse_face_mouse_frame)
12987 {
12988 dpyinfo->mouse_face_beg_row
12989 = dpyinfo->mouse_face_beg_col = -1;
12990 dpyinfo->mouse_face_end_row
12991 = dpyinfo->mouse_face_end_col = -1;
12992 dpyinfo->mouse_face_window = Qnil;
12993 dpyinfo->mouse_face_deferred_gc = 0;
12994 dpyinfo->mouse_face_mouse_frame = 0;
12995 }
12996
12997 UNBLOCK_INPUT;
12998 }
12999
13000
13001 /* Destroy the X window of frame F. */
13002
13003 void
13004 x_destroy_window (f)
13005 struct frame *f;
13006 {
13007 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13008
13009 /* If a display connection is dead, don't try sending more
13010 commands to the X server. */
13011 if (dpyinfo->display != 0)
13012 x_free_frame_resources (f);
13013
13014 dpyinfo->reference_count--;
13015 }
13016
13017 \f
13018 /* Setting window manager hints. */
13019
13020 /* Set the normal size hints for the window manager, for frame F.
13021 FLAGS is the flags word to use--or 0 meaning preserve the flags
13022 that the window now has.
13023 If USER_POSITION is nonzero, we set the USPosition
13024 flag (this is useful when FLAGS is 0). */
13025
13026 void
13027 x_wm_set_size_hint (f, flags, user_position)
13028 struct frame *f;
13029 long flags;
13030 int user_position;
13031 {
13032 XSizeHints size_hints;
13033
13034 #ifdef USE_X_TOOLKIT
13035 Arg al[2];
13036 int ac = 0;
13037 Dimension widget_width, widget_height;
13038 Window window = XtWindow (f->output_data.x->widget);
13039 #else /* not USE_X_TOOLKIT */
13040 Window window = FRAME_X_WINDOW (f);
13041 #endif /* not USE_X_TOOLKIT */
13042
13043 /* Setting PMaxSize caused various problems. */
13044 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
13045
13046 size_hints.x = f->output_data.x->left_pos;
13047 size_hints.y = f->output_data.x->top_pos;
13048
13049 #ifdef USE_X_TOOLKIT
13050 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
13051 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
13052 XtGetValues (f->output_data.x->widget, al, ac);
13053 size_hints.height = widget_height;
13054 size_hints.width = widget_width;
13055 #else /* not USE_X_TOOLKIT */
13056 size_hints.height = PIXEL_HEIGHT (f);
13057 size_hints.width = PIXEL_WIDTH (f);
13058 #endif /* not USE_X_TOOLKIT */
13059
13060 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
13061 size_hints.height_inc = f->output_data.x->line_height;
13062 size_hints.max_width
13063 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
13064 size_hints.max_height
13065 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
13066
13067 /* Calculate the base and minimum sizes.
13068
13069 (When we use the X toolkit, we don't do it here.
13070 Instead we copy the values that the widgets are using, below.) */
13071 #ifndef USE_X_TOOLKIT
13072 {
13073 int base_width, base_height;
13074 int min_rows = 0, min_cols = 0;
13075
13076 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
13077 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
13078
13079 check_frame_size (f, &min_rows, &min_cols);
13080
13081 /* The window manager uses the base width hints to calculate the
13082 current number of rows and columns in the frame while
13083 resizing; min_width and min_height aren't useful for this
13084 purpose, since they might not give the dimensions for a
13085 zero-row, zero-column frame.
13086
13087 We use the base_width and base_height members if we have
13088 them; otherwise, we set the min_width and min_height members
13089 to the size for a zero x zero frame. */
13090
13091 #ifdef HAVE_X11R4
13092 size_hints.flags |= PBaseSize;
13093 size_hints.base_width = base_width;
13094 size_hints.base_height = base_height;
13095 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
13096 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
13097 #else
13098 size_hints.min_width = base_width;
13099 size_hints.min_height = base_height;
13100 #endif
13101 }
13102
13103 /* If we don't need the old flags, we don't need the old hint at all. */
13104 if (flags)
13105 {
13106 size_hints.flags |= flags;
13107 goto no_read;
13108 }
13109 #endif /* not USE_X_TOOLKIT */
13110
13111 {
13112 XSizeHints hints; /* Sometimes I hate X Windows... */
13113 long supplied_return;
13114 int value;
13115
13116 #ifdef HAVE_X11R4
13117 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
13118 &supplied_return);
13119 #else
13120 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
13121 #endif
13122
13123 #ifdef USE_X_TOOLKIT
13124 size_hints.base_height = hints.base_height;
13125 size_hints.base_width = hints.base_width;
13126 size_hints.min_height = hints.min_height;
13127 size_hints.min_width = hints.min_width;
13128 #endif
13129
13130 if (flags)
13131 size_hints.flags |= flags;
13132 else
13133 {
13134 if (value == 0)
13135 hints.flags = 0;
13136 if (hints.flags & PSize)
13137 size_hints.flags |= PSize;
13138 if (hints.flags & PPosition)
13139 size_hints.flags |= PPosition;
13140 if (hints.flags & USPosition)
13141 size_hints.flags |= USPosition;
13142 if (hints.flags & USSize)
13143 size_hints.flags |= USSize;
13144 }
13145 }
13146
13147 #ifndef USE_X_TOOLKIT
13148 no_read:
13149 #endif
13150
13151 #ifdef PWinGravity
13152 size_hints.win_gravity = f->output_data.x->win_gravity;
13153 size_hints.flags |= PWinGravity;
13154
13155 if (user_position)
13156 {
13157 size_hints.flags &= ~ PPosition;
13158 size_hints.flags |= USPosition;
13159 }
13160 #endif /* PWinGravity */
13161
13162 #ifdef HAVE_X11R4
13163 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13164 #else
13165 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13166 #endif
13167 }
13168
13169 /* Used for IconicState or NormalState */
13170
13171 void
13172 x_wm_set_window_state (f, state)
13173 struct frame *f;
13174 int state;
13175 {
13176 #ifdef USE_X_TOOLKIT
13177 Arg al[1];
13178
13179 XtSetArg (al[0], XtNinitialState, state);
13180 XtSetValues (f->output_data.x->widget, al, 1);
13181 #else /* not USE_X_TOOLKIT */
13182 Window window = FRAME_X_WINDOW (f);
13183
13184 f->output_data.x->wm_hints.flags |= StateHint;
13185 f->output_data.x->wm_hints.initial_state = state;
13186
13187 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13188 #endif /* not USE_X_TOOLKIT */
13189 }
13190
13191 void
13192 x_wm_set_icon_pixmap (f, pixmap_id)
13193 struct frame *f;
13194 int pixmap_id;
13195 {
13196 Pixmap icon_pixmap;
13197
13198 #ifndef USE_X_TOOLKIT
13199 Window window = FRAME_X_WINDOW (f);
13200 #endif
13201
13202 if (pixmap_id > 0)
13203 {
13204 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
13205 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
13206 }
13207 else
13208 {
13209 /* It seems there is no way to turn off use of an icon pixmap.
13210 The following line does it, only if no icon has yet been created,
13211 for some window managers. But with mwm it crashes.
13212 Some people say it should clear the IconPixmapHint bit in this case,
13213 but that doesn't work, and the X consortium said it isn't the
13214 right thing at all. Since there is no way to win,
13215 best to explicitly give up. */
13216 #if 0
13217 f->output_data.x->wm_hints.icon_pixmap = None;
13218 #else
13219 return;
13220 #endif
13221 }
13222
13223 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
13224
13225 {
13226 Arg al[1];
13227 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
13228 XtSetValues (f->output_data.x->widget, al, 1);
13229 }
13230
13231 #else /* not USE_X_TOOLKIT */
13232
13233 f->output_data.x->wm_hints.flags |= IconPixmapHint;
13234 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13235
13236 #endif /* not USE_X_TOOLKIT */
13237 }
13238
13239 void
13240 x_wm_set_icon_position (f, icon_x, icon_y)
13241 struct frame *f;
13242 int icon_x, icon_y;
13243 {
13244 #ifdef USE_X_TOOLKIT
13245 Window window = XtWindow (f->output_data.x->widget);
13246 #else
13247 Window window = FRAME_X_WINDOW (f);
13248 #endif
13249
13250 f->output_data.x->wm_hints.flags |= IconPositionHint;
13251 f->output_data.x->wm_hints.icon_x = icon_x;
13252 f->output_data.x->wm_hints.icon_y = icon_y;
13253
13254 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13255 }
13256
13257 \f
13258 /***********************************************************************
13259 Fonts
13260 ***********************************************************************/
13261
13262 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
13263
13264 struct font_info *
13265 x_get_font_info (f, font_idx)
13266 FRAME_PTR f;
13267 int font_idx;
13268 {
13269 return (FRAME_X_FONT_TABLE (f) + font_idx);
13270 }
13271
13272
13273 /* Return a list of names of available fonts matching PATTERN on frame F.
13274
13275 If SIZE is > 0, it is the size (maximum bounds width) of fonts
13276 to be listed.
13277
13278 SIZE < 0 means include scalable fonts.
13279
13280 Frame F null means we have not yet created any frame on X, and
13281 consult the first display in x_display_list. MAXNAMES sets a limit
13282 on how many fonts to match. */
13283
13284 Lisp_Object
13285 x_list_fonts (f, pattern, size, maxnames)
13286 struct frame *f;
13287 Lisp_Object pattern;
13288 int size;
13289 int maxnames;
13290 {
13291 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
13292 Lisp_Object tem, second_best;
13293 struct x_display_info *dpyinfo
13294 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
13295 Display *dpy = dpyinfo->display;
13296 int try_XLoadQueryFont = 0;
13297 int count;
13298 int allow_scalable_fonts_p = 0;
13299
13300 if (size < 0)
13301 {
13302 allow_scalable_fonts_p = 1;
13303 size = 0;
13304 }
13305
13306 patterns = Fassoc (pattern, Valternate_fontname_alist);
13307 if (NILP (patterns))
13308 patterns = Fcons (pattern, Qnil);
13309
13310 if (maxnames == 1 && !size)
13311 /* We can return any single font matching PATTERN. */
13312 try_XLoadQueryFont = 1;
13313
13314 for (; CONSP (patterns); patterns = XCDR (patterns))
13315 {
13316 int num_fonts;
13317 char **names = NULL;
13318
13319 pattern = XCAR (patterns);
13320 /* See if we cached the result for this particular query.
13321 The cache is an alist of the form:
13322 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
13323 tem = XCDR (dpyinfo->name_list_element);
13324 key = Fcons (Fcons (pattern, make_number (maxnames)),
13325 allow_scalable_fonts_p ? Qt : Qnil);
13326 list = Fassoc (key, tem);
13327 if (!NILP (list))
13328 {
13329 list = Fcdr_safe (list);
13330 /* We have a cashed list. Don't have to get the list again. */
13331 goto label_cached;
13332 }
13333
13334 /* At first, put PATTERN in the cache. */
13335
13336 BLOCK_INPUT;
13337 count = x_catch_errors (dpy);
13338
13339 if (try_XLoadQueryFont)
13340 {
13341 XFontStruct *font;
13342 unsigned long value;
13343
13344 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
13345 if (x_had_errors_p (dpy))
13346 {
13347 /* This error is perhaps due to insufficient memory on X
13348 server. Let's just ignore it. */
13349 font = NULL;
13350 x_clear_errors (dpy);
13351 }
13352
13353 if (font
13354 && XGetFontProperty (font, XA_FONT, &value))
13355 {
13356 char *name = (char *) XGetAtomName (dpy, (Atom) value);
13357 int len = strlen (name);
13358 char *tmp;
13359
13360 /* If DXPC (a Differential X Protocol Compressor)
13361 Ver.3.7 is running, XGetAtomName will return null
13362 string. We must avoid such a name. */
13363 if (len == 0)
13364 try_XLoadQueryFont = 0;
13365 else
13366 {
13367 num_fonts = 1;
13368 names = (char **) alloca (sizeof (char *));
13369 /* Some systems only allow alloca assigned to a
13370 simple var. */
13371 tmp = (char *) alloca (len + 1); names[0] = tmp;
13372 bcopy (name, names[0], len + 1);
13373 XFree (name);
13374 }
13375 }
13376 else
13377 try_XLoadQueryFont = 0;
13378
13379 if (font)
13380 XFreeFont (dpy, font);
13381 }
13382
13383 if (!try_XLoadQueryFont)
13384 {
13385 /* We try at least 10 fonts because XListFonts will return
13386 auto-scaled fonts at the head. */
13387 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
13388 &num_fonts);
13389 if (x_had_errors_p (dpy))
13390 {
13391 /* This error is perhaps due to insufficient memory on X
13392 server. Let's just ignore it. */
13393 names = NULL;
13394 x_clear_errors (dpy);
13395 }
13396 }
13397
13398 x_uncatch_errors (dpy, count);
13399 UNBLOCK_INPUT;
13400
13401 if (names)
13402 {
13403 int i;
13404
13405 /* Make a list of all the fonts we got back.
13406 Store that in the font cache for the display. */
13407 for (i = 0; i < num_fonts; i++)
13408 {
13409 int width = 0;
13410 char *p = names[i];
13411 int average_width = -1, dashes = 0;
13412
13413 /* Count the number of dashes in NAMES[I]. If there are
13414 14 dashes, and the field value following 12th dash
13415 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
13416 is usually too ugly to be used for editing. Let's
13417 ignore it. */
13418 while (*p)
13419 if (*p++ == '-')
13420 {
13421 dashes++;
13422 if (dashes == 7) /* PIXEL_SIZE field */
13423 width = atoi (p);
13424 else if (dashes == 12) /* AVERAGE_WIDTH field */
13425 average_width = atoi (p);
13426 }
13427
13428 if (allow_scalable_fonts_p
13429 || dashes < 14 || average_width != 0)
13430 {
13431 tem = build_string (names[i]);
13432 if (NILP (Fassoc (tem, list)))
13433 {
13434 if (STRINGP (Vx_pixel_size_width_font_regexp)
13435 && ((fast_c_string_match_ignore_case
13436 (Vx_pixel_size_width_font_regexp, names[i]))
13437 >= 0))
13438 /* We can set the value of PIXEL_SIZE to the
13439 width of this font. */
13440 list = Fcons (Fcons (tem, make_number (width)), list);
13441 else
13442 /* For the moment, width is not known. */
13443 list = Fcons (Fcons (tem, Qnil), list);
13444 }
13445 }
13446 }
13447
13448 if (!try_XLoadQueryFont)
13449 {
13450 BLOCK_INPUT;
13451 XFreeFontNames (names);
13452 UNBLOCK_INPUT;
13453 }
13454 }
13455
13456 /* Now store the result in the cache. */
13457 XCDR (dpyinfo->name_list_element)
13458 = Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element));
13459
13460 label_cached:
13461 if (NILP (list)) continue; /* Try the remaining alternatives. */
13462
13463 newlist = second_best = Qnil;
13464 /* Make a list of the fonts that have the right width. */
13465 for (; CONSP (list); list = XCDR (list))
13466 {
13467 int found_size;
13468
13469 tem = XCAR (list);
13470
13471 if (!CONSP (tem) || NILP (XCAR (tem)))
13472 continue;
13473 if (!size)
13474 {
13475 newlist = Fcons (XCAR (tem), newlist);
13476 continue;
13477 }
13478
13479 if (!INTEGERP (XCDR (tem)))
13480 {
13481 /* Since we have not yet known the size of this font, we
13482 must try slow function call XLoadQueryFont. */
13483 XFontStruct *thisinfo;
13484
13485 BLOCK_INPUT;
13486 count = x_catch_errors (dpy);
13487 thisinfo = XLoadQueryFont (dpy,
13488 XSTRING (XCAR (tem))->data);
13489 if (x_had_errors_p (dpy))
13490 {
13491 /* This error is perhaps due to insufficient memory on X
13492 server. Let's just ignore it. */
13493 thisinfo = NULL;
13494 x_clear_errors (dpy);
13495 }
13496 x_uncatch_errors (dpy, count);
13497 UNBLOCK_INPUT;
13498
13499 if (thisinfo)
13500 {
13501 XCDR (tem)
13502 = (thisinfo->min_bounds.width == 0
13503 ? make_number (0)
13504 : make_number (thisinfo->max_bounds.width));
13505 BLOCK_INPUT;
13506 XFreeFont (dpy, thisinfo);
13507 UNBLOCK_INPUT;
13508 }
13509 else
13510 /* For unknown reason, the previous call of XListFont had
13511 returned a font which can't be opened. Record the size
13512 as 0 not to try to open it again. */
13513 XCDR (tem) = make_number (0);
13514 }
13515
13516 found_size = XINT (XCDR (tem));
13517 if (found_size == size)
13518 newlist = Fcons (XCAR (tem), newlist);
13519 else if (found_size > 0)
13520 {
13521 if (NILP (second_best))
13522 second_best = tem;
13523 else if (found_size < size)
13524 {
13525 if (XINT (XCDR (second_best)) > size
13526 || XINT (XCDR (second_best)) < found_size)
13527 second_best = tem;
13528 }
13529 else
13530 {
13531 if (XINT (XCDR (second_best)) > size
13532 && XINT (XCDR (second_best)) > found_size)
13533 second_best = tem;
13534 }
13535 }
13536 }
13537 if (!NILP (newlist))
13538 break;
13539 else if (!NILP (second_best))
13540 {
13541 newlist = Fcons (XCAR (second_best), Qnil);
13542 break;
13543 }
13544 }
13545
13546 return newlist;
13547 }
13548
13549
13550 #if GLYPH_DEBUG
13551
13552 /* Check that FONT is valid on frame F. It is if it can be found in F's
13553 font table. */
13554
13555 static void
13556 x_check_font (f, font)
13557 struct frame *f;
13558 XFontStruct *font;
13559 {
13560 int i;
13561 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13562
13563 xassert (font != NULL);
13564
13565 for (i = 0; i < dpyinfo->n_fonts; i++)
13566 if (dpyinfo->font_table[i].name
13567 && font == dpyinfo->font_table[i].font)
13568 break;
13569
13570 xassert (i < dpyinfo->n_fonts);
13571 }
13572
13573 #endif /* GLYPH_DEBUG != 0 */
13574
13575 /* Set *W to the minimum width, *H to the minimum font height of FONT.
13576 Note: There are (broken) X fonts out there with invalid XFontStruct
13577 min_bounds contents. For example, handa@etl.go.jp reports that
13578 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
13579 have font->min_bounds.width == 0. */
13580
13581 static INLINE void
13582 x_font_min_bounds (font, w, h)
13583 XFontStruct *font;
13584 int *w, *h;
13585 {
13586 *h = FONT_HEIGHT (font);
13587 *w = font->min_bounds.width;
13588
13589 /* Try to handle the case where FONT->min_bounds has invalid
13590 contents. Since the only font known to have invalid min_bounds
13591 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
13592 if (*w <= 0)
13593 *w = font->max_bounds.width;
13594 }
13595
13596
13597 /* Compute the smallest character width and smallest font height over
13598 all fonts available on frame F. Set the members smallest_char_width
13599 and smallest_font_height in F's x_display_info structure to
13600 the values computed. Value is non-zero if smallest_font_height or
13601 smallest_char_width become smaller than they were before. */
13602
13603 static int
13604 x_compute_min_glyph_bounds (f)
13605 struct frame *f;
13606 {
13607 int i;
13608 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13609 XFontStruct *font;
13610 int old_width = dpyinfo->smallest_char_width;
13611 int old_height = dpyinfo->smallest_font_height;
13612
13613 dpyinfo->smallest_font_height = 100000;
13614 dpyinfo->smallest_char_width = 100000;
13615
13616 for (i = 0; i < dpyinfo->n_fonts; ++i)
13617 if (dpyinfo->font_table[i].name)
13618 {
13619 struct font_info *fontp = dpyinfo->font_table + i;
13620 int w, h;
13621
13622 font = (XFontStruct *) fontp->font;
13623 xassert (font != (XFontStruct *) ~0);
13624 x_font_min_bounds (font, &w, &h);
13625
13626 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
13627 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
13628 }
13629
13630 xassert (dpyinfo->smallest_char_width > 0
13631 && dpyinfo->smallest_font_height > 0);
13632
13633 return (dpyinfo->n_fonts == 1
13634 || dpyinfo->smallest_char_width < old_width
13635 || dpyinfo->smallest_font_height < old_height);
13636 }
13637
13638
13639 /* Load font named FONTNAME of the size SIZE for frame F, and return a
13640 pointer to the structure font_info while allocating it dynamically.
13641 If SIZE is 0, load any size of font.
13642 If loading is failed, return NULL. */
13643
13644 struct font_info *
13645 x_load_font (f, fontname, size)
13646 struct frame *f;
13647 register char *fontname;
13648 int size;
13649 {
13650 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13651 Lisp_Object font_names;
13652 int count;
13653
13654 /* Get a list of all the fonts that match this name. Once we
13655 have a list of matching fonts, we compare them against the fonts
13656 we already have by comparing names. */
13657 font_names = x_list_fonts (f, build_string (fontname), size, 1);
13658
13659 if (!NILP (font_names))
13660 {
13661 Lisp_Object tail;
13662 int i;
13663
13664 for (i = 0; i < dpyinfo->n_fonts; i++)
13665 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
13666 if (dpyinfo->font_table[i].name
13667 && (!strcmp (dpyinfo->font_table[i].name,
13668 XSTRING (XCAR (tail))->data)
13669 || !strcmp (dpyinfo->font_table[i].full_name,
13670 XSTRING (XCAR (tail))->data)))
13671 return (dpyinfo->font_table + i);
13672 }
13673
13674 /* Load the font and add it to the table. */
13675 {
13676 char *full_name;
13677 XFontStruct *font;
13678 struct font_info *fontp;
13679 unsigned long value;
13680 int i;
13681
13682 /* If we have found fonts by x_list_font, load one of them. If
13683 not, we still try to load a font by the name given as FONTNAME
13684 because XListFonts (called in x_list_font) of some X server has
13685 a bug of not finding a font even if the font surely exists and
13686 is loadable by XLoadQueryFont. */
13687 if (size > 0 && !NILP (font_names))
13688 fontname = (char *) XSTRING (XCAR (font_names))->data;
13689
13690 BLOCK_INPUT;
13691 count = x_catch_errors (FRAME_X_DISPLAY (f));
13692 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
13693 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
13694 {
13695 /* This error is perhaps due to insufficient memory on X
13696 server. Let's just ignore it. */
13697 font = NULL;
13698 x_clear_errors (FRAME_X_DISPLAY (f));
13699 }
13700 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
13701 UNBLOCK_INPUT;
13702 if (!font)
13703 return NULL;
13704
13705 /* Find a free slot in the font table. */
13706 for (i = 0; i < dpyinfo->n_fonts; ++i)
13707 if (dpyinfo->font_table[i].name == NULL)
13708 break;
13709
13710 /* If no free slot found, maybe enlarge the font table. */
13711 if (i == dpyinfo->n_fonts
13712 && dpyinfo->n_fonts == dpyinfo->font_table_size)
13713 {
13714 int sz;
13715 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
13716 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
13717 dpyinfo->font_table
13718 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
13719 }
13720
13721 fontp = dpyinfo->font_table + i;
13722 if (i == dpyinfo->n_fonts)
13723 ++dpyinfo->n_fonts;
13724
13725 /* Now fill in the slots of *FONTP. */
13726 BLOCK_INPUT;
13727 fontp->font = font;
13728 fontp->font_idx = i;
13729 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
13730 bcopy (fontname, fontp->name, strlen (fontname) + 1);
13731
13732 /* Try to get the full name of FONT. Put it in FULL_NAME. */
13733 full_name = 0;
13734 if (XGetFontProperty (font, XA_FONT, &value))
13735 {
13736 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
13737 char *p = name;
13738 int dashes = 0;
13739
13740 /* Count the number of dashes in the "full name".
13741 If it is too few, this isn't really the font's full name,
13742 so don't use it.
13743 In X11R4, the fonts did not come with their canonical names
13744 stored in them. */
13745 while (*p)
13746 {
13747 if (*p == '-')
13748 dashes++;
13749 p++;
13750 }
13751
13752 if (dashes >= 13)
13753 {
13754 full_name = (char *) xmalloc (p - name + 1);
13755 bcopy (name, full_name, p - name + 1);
13756 }
13757
13758 XFree (name);
13759 }
13760
13761 if (full_name != 0)
13762 fontp->full_name = full_name;
13763 else
13764 fontp->full_name = fontp->name;
13765
13766 fontp->size = font->max_bounds.width;
13767 fontp->height = FONT_HEIGHT (font);
13768
13769 if (NILP (font_names))
13770 {
13771 /* We come here because of a bug of XListFonts mentioned at
13772 the head of this block. Let's store this information in
13773 the cache for x_list_fonts. */
13774 Lisp_Object lispy_name = build_string (fontname);
13775 Lisp_Object lispy_full_name = build_string (fontp->full_name);
13776 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
13777 Qnil);
13778
13779 XCDR (dpyinfo->name_list_element)
13780 = Fcons (Fcons (key,
13781 Fcons (Fcons (lispy_full_name,
13782 make_number (fontp->size)),
13783 Qnil)),
13784 XCDR (dpyinfo->name_list_element));
13785 if (full_name)
13786 {
13787 key = Fcons (Fcons (lispy_full_name, make_number (256)),
13788 Qnil);
13789 XCDR (dpyinfo->name_list_element)
13790 = Fcons (Fcons (key,
13791 Fcons (Fcons (lispy_full_name,
13792 make_number (fontp->size)),
13793 Qnil)),
13794 XCDR (dpyinfo->name_list_element));
13795 }
13796 }
13797
13798 /* The slot `encoding' specifies how to map a character
13799 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
13800 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
13801 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
13802 2:0xA020..0xFF7F). For the moment, we don't know which charset
13803 uses this font. So, we set information in fontp->encoding[1]
13804 which is never used by any charset. If mapping can't be
13805 decided, set FONT_ENCODING_NOT_DECIDED. */
13806 fontp->encoding[1]
13807 = (font->max_byte1 == 0
13808 /* 1-byte font */
13809 ? (font->min_char_or_byte2 < 0x80
13810 ? (font->max_char_or_byte2 < 0x80
13811 ? 0 /* 0x20..0x7F */
13812 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
13813 : 1) /* 0xA0..0xFF */
13814 /* 2-byte font */
13815 : (font->min_byte1 < 0x80
13816 ? (font->max_byte1 < 0x80
13817 ? (font->min_char_or_byte2 < 0x80
13818 ? (font->max_char_or_byte2 < 0x80
13819 ? 0 /* 0x2020..0x7F7F */
13820 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
13821 : 3) /* 0x20A0..0x7FFF */
13822 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
13823 : (font->min_char_or_byte2 < 0x80
13824 ? (font->max_char_or_byte2 < 0x80
13825 ? 2 /* 0xA020..0xFF7F */
13826 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
13827 : 1))); /* 0xA0A0..0xFFFF */
13828
13829 fontp->baseline_offset
13830 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
13831 ? (long) value : 0);
13832 fontp->relative_compose
13833 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
13834 ? (long) value : 0);
13835 fontp->default_ascent
13836 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
13837 ? (long) value : 0);
13838
13839 /* Set global flag fonts_changed_p to non-zero if the font loaded
13840 has a character with a smaller width than any other character
13841 before, or if the font loaded has a smalle>r height than any
13842 other font loaded before. If this happens, it will make a
13843 glyph matrix reallocation necessary. */
13844 fonts_changed_p = x_compute_min_glyph_bounds (f);
13845 UNBLOCK_INPUT;
13846 return fontp;
13847 }
13848 }
13849
13850
13851 /* Return a pointer to struct font_info of a font named FONTNAME for
13852 frame F. If no such font is loaded, return NULL. */
13853
13854 struct font_info *
13855 x_query_font (f, fontname)
13856 struct frame *f;
13857 register char *fontname;
13858 {
13859 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13860 int i;
13861
13862 for (i = 0; i < dpyinfo->n_fonts; i++)
13863 if (dpyinfo->font_table[i].name
13864 && (!strcmp (dpyinfo->font_table[i].name, fontname)
13865 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
13866 return (dpyinfo->font_table + i);
13867 return NULL;
13868 }
13869
13870
13871 /* Find a CCL program for a font specified by FONTP, and set the member
13872 `encoder' of the structure. */
13873
13874 void
13875 x_find_ccl_program (fontp)
13876 struct font_info *fontp;
13877 {
13878 Lisp_Object list, elt;
13879
13880 elt = Qnil;
13881 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
13882 {
13883 elt = XCAR (list);
13884 if (CONSP (elt)
13885 && STRINGP (XCAR (elt))
13886 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
13887 >= 0)
13888 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
13889 >= 0)))
13890 break;
13891 }
13892
13893 if (! NILP (list))
13894 {
13895 struct ccl_program *ccl
13896 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
13897
13898 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
13899 xfree (ccl);
13900 else
13901 fontp->font_encoder = ccl;
13902 }
13903 }
13904
13905
13906 \f
13907 /***********************************************************************
13908 Initialization
13909 ***********************************************************************/
13910
13911 #ifdef USE_X_TOOLKIT
13912 static XrmOptionDescRec emacs_options[] = {
13913 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13914 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13915
13916 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13917 XrmoptionSepArg, NULL},
13918 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13919
13920 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13921 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13922 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13923 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13924 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13925 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13926 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13927 };
13928 #endif /* USE_X_TOOLKIT */
13929
13930 static int x_initialized;
13931
13932 #ifdef MULTI_KBOARD
13933 /* Test whether two display-name strings agree up to the dot that separates
13934 the screen number from the server number. */
13935 static int
13936 same_x_server (name1, name2)
13937 char *name1, *name2;
13938 {
13939 int seen_colon = 0;
13940 unsigned char *system_name = XSTRING (Vsystem_name)->data;
13941 int system_name_length = strlen (system_name);
13942 int length_until_period = 0;
13943
13944 while (system_name[length_until_period] != 0
13945 && system_name[length_until_period] != '.')
13946 length_until_period++;
13947
13948 /* Treat `unix' like an empty host name. */
13949 if (! strncmp (name1, "unix:", 5))
13950 name1 += 4;
13951 if (! strncmp (name2, "unix:", 5))
13952 name2 += 4;
13953 /* Treat this host's name like an empty host name. */
13954 if (! strncmp (name1, system_name, system_name_length)
13955 && name1[system_name_length] == ':')
13956 name1 += system_name_length;
13957 if (! strncmp (name2, system_name, system_name_length)
13958 && name2[system_name_length] == ':')
13959 name2 += system_name_length;
13960 /* Treat this host's domainless name like an empty host name. */
13961 if (! strncmp (name1, system_name, length_until_period)
13962 && name1[length_until_period] == ':')
13963 name1 += length_until_period;
13964 if (! strncmp (name2, system_name, length_until_period)
13965 && name2[length_until_period] == ':')
13966 name2 += length_until_period;
13967
13968 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13969 {
13970 if (*name1 == ':')
13971 seen_colon++;
13972 if (seen_colon && *name1 == '.')
13973 return 1;
13974 }
13975 return (seen_colon
13976 && (*name1 == '.' || *name1 == '\0')
13977 && (*name2 == '.' || *name2 == '\0'));
13978 }
13979 #endif
13980
13981 struct x_display_info *
13982 x_term_init (display_name, xrm_option, resource_name)
13983 Lisp_Object display_name;
13984 char *xrm_option;
13985 char *resource_name;
13986 {
13987 int connection;
13988 Display *dpy;
13989 struct x_display_info *dpyinfo;
13990 XrmDatabase xrdb;
13991
13992 BLOCK_INPUT;
13993
13994 if (!x_initialized)
13995 {
13996 x_initialize ();
13997 x_initialized = 1;
13998 }
13999
14000 #ifdef USE_X_TOOLKIT
14001 /* weiner@footloose.sps.mot.com reports that this causes
14002 errors with X11R5:
14003 X protocol error: BadAtom (invalid Atom parameter)
14004 on protocol request 18skiloaf.
14005 So let's not use it until R6. */
14006 #ifdef HAVE_X11XTR6
14007 XtSetLanguageProc (NULL, NULL, NULL);
14008 #endif
14009
14010 {
14011 int argc = 0;
14012 char *argv[3];
14013
14014 argv[0] = "";
14015 argc = 1;
14016 if (xrm_option)
14017 {
14018 argv[argc++] = "-xrm";
14019 argv[argc++] = xrm_option;
14020 }
14021 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
14022 resource_name, EMACS_CLASS,
14023 emacs_options, XtNumber (emacs_options),
14024 &argc, argv);
14025
14026 #ifdef HAVE_X11XTR6
14027 /* I think this is to compensate for XtSetLanguageProc. */
14028 fixup_locale ();
14029 #endif
14030 }
14031
14032 #else /* not USE_X_TOOLKIT */
14033 #ifdef HAVE_X11R5
14034 XSetLocaleModifiers ("");
14035 #endif
14036 dpy = XOpenDisplay (XSTRING (display_name)->data);
14037 #endif /* not USE_X_TOOLKIT */
14038
14039 /* Detect failure. */
14040 if (dpy == 0)
14041 {
14042 UNBLOCK_INPUT;
14043 return 0;
14044 }
14045
14046 /* We have definitely succeeded. Record the new connection. */
14047
14048 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
14049 bzero (dpyinfo, sizeof *dpyinfo);
14050
14051 #ifdef MULTI_KBOARD
14052 {
14053 struct x_display_info *share;
14054 Lisp_Object tail;
14055
14056 for (share = x_display_list, tail = x_display_name_list; share;
14057 share = share->next, tail = XCDR (tail))
14058 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
14059 XSTRING (display_name)->data))
14060 break;
14061 if (share)
14062 dpyinfo->kboard = share->kboard;
14063 else
14064 {
14065 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
14066 init_kboard (dpyinfo->kboard);
14067 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
14068 {
14069 char *vendor = ServerVendor (dpy);
14070 UNBLOCK_INPUT;
14071 dpyinfo->kboard->Vsystem_key_alist
14072 = call1 (Qvendor_specific_keysyms,
14073 build_string (vendor ? vendor : ""));
14074 BLOCK_INPUT;
14075 }
14076
14077 dpyinfo->kboard->next_kboard = all_kboards;
14078 all_kboards = dpyinfo->kboard;
14079 /* Don't let the initial kboard remain current longer than necessary.
14080 That would cause problems if a file loaded on startup tries to
14081 prompt in the mini-buffer. */
14082 if (current_kboard == initial_kboard)
14083 current_kboard = dpyinfo->kboard;
14084 }
14085 dpyinfo->kboard->reference_count++;
14086 }
14087 #endif
14088
14089 /* Put this display on the chain. */
14090 dpyinfo->next = x_display_list;
14091 x_display_list = dpyinfo;
14092
14093 /* Put it on x_display_name_list as well, to keep them parallel. */
14094 x_display_name_list = Fcons (Fcons (display_name, Qnil),
14095 x_display_name_list);
14096 dpyinfo->name_list_element = XCAR (x_display_name_list);
14097
14098 dpyinfo->display = dpy;
14099
14100 #if 0
14101 XSetAfterFunction (x_current_display, x_trace_wire);
14102 #endif /* ! 0 */
14103
14104 dpyinfo->x_id_name
14105 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
14106 + STRING_BYTES (XSTRING (Vsystem_name))
14107 + 2);
14108 sprintf (dpyinfo->x_id_name, "%s@%s",
14109 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
14110
14111 /* Figure out which modifier bits mean what. */
14112 x_find_modifier_meanings (dpyinfo);
14113
14114 /* Get the scroll bar cursor. */
14115 dpyinfo->vertical_scroll_bar_cursor
14116 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
14117
14118 xrdb = x_load_resources (dpyinfo->display, xrm_option,
14119 resource_name, EMACS_CLASS);
14120 #ifdef HAVE_XRMSETDATABASE
14121 XrmSetDatabase (dpyinfo->display, xrdb);
14122 #else
14123 dpyinfo->display->db = xrdb;
14124 #endif
14125 /* Put the rdb where we can find it in a way that works on
14126 all versions. */
14127 dpyinfo->xrdb = xrdb;
14128
14129 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
14130 DefaultScreen (dpyinfo->display));
14131 select_visual (dpyinfo);
14132 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
14133 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
14134 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
14135 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
14136 dpyinfo->grabbed = 0;
14137 dpyinfo->reference_count = 0;
14138 dpyinfo->icon_bitmap_id = -1;
14139 dpyinfo->font_table = NULL;
14140 dpyinfo->n_fonts = 0;
14141 dpyinfo->font_table_size = 0;
14142 dpyinfo->bitmaps = 0;
14143 dpyinfo->bitmaps_size = 0;
14144 dpyinfo->bitmaps_last = 0;
14145 dpyinfo->scratch_cursor_gc = 0;
14146 dpyinfo->mouse_face_mouse_frame = 0;
14147 dpyinfo->mouse_face_deferred_gc = 0;
14148 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
14149 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
14150 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
14151 dpyinfo->mouse_face_window = Qnil;
14152 dpyinfo->mouse_face_overlay = Qnil;
14153 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
14154 dpyinfo->mouse_face_defer = 0;
14155 dpyinfo->x_focus_frame = 0;
14156 dpyinfo->x_focus_event_frame = 0;
14157 dpyinfo->x_highlight_frame = 0;
14158 dpyinfo->image_cache = make_image_cache ();
14159
14160 /* See if a private colormap is requested. */
14161 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
14162 {
14163 if (dpyinfo->visual->class == PseudoColor)
14164 {
14165 Lisp_Object value;
14166 value = display_x_get_resource (dpyinfo,
14167 build_string ("privateColormap"),
14168 build_string ("PrivateColormap"),
14169 Qnil, Qnil);
14170 if (STRINGP (value)
14171 && (!strcmp (XSTRING (value)->data, "true")
14172 || !strcmp (XSTRING (value)->data, "on")))
14173 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
14174 }
14175 }
14176 else
14177 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
14178 dpyinfo->visual, AllocNone);
14179
14180 {
14181 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
14182 double pixels = DisplayHeight (dpyinfo->display, screen_number);
14183 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
14184 dpyinfo->resy = pixels * 25.4 / mm;
14185 pixels = DisplayWidth (dpyinfo->display, screen_number);
14186 mm = DisplayWidthMM (dpyinfo->display, screen_number);
14187 dpyinfo->resx = pixels * 25.4 / mm;
14188 }
14189
14190 dpyinfo->Xatom_wm_protocols
14191 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
14192 dpyinfo->Xatom_wm_take_focus
14193 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
14194 dpyinfo->Xatom_wm_save_yourself
14195 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
14196 dpyinfo->Xatom_wm_delete_window
14197 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
14198 dpyinfo->Xatom_wm_change_state
14199 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
14200 dpyinfo->Xatom_wm_configure_denied
14201 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
14202 dpyinfo->Xatom_wm_window_moved
14203 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
14204 dpyinfo->Xatom_editres
14205 = XInternAtom (dpyinfo->display, "Editres", False);
14206 dpyinfo->Xatom_CLIPBOARD
14207 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
14208 dpyinfo->Xatom_TIMESTAMP
14209 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
14210 dpyinfo->Xatom_TEXT
14211 = XInternAtom (dpyinfo->display, "TEXT", False);
14212 dpyinfo->Xatom_COMPOUND_TEXT
14213 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
14214 dpyinfo->Xatom_DELETE
14215 = XInternAtom (dpyinfo->display, "DELETE", False);
14216 dpyinfo->Xatom_MULTIPLE
14217 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
14218 dpyinfo->Xatom_INCR
14219 = XInternAtom (dpyinfo->display, "INCR", False);
14220 dpyinfo->Xatom_EMACS_TMP
14221 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
14222 dpyinfo->Xatom_TARGETS
14223 = XInternAtom (dpyinfo->display, "TARGETS", False);
14224 dpyinfo->Xatom_NULL
14225 = XInternAtom (dpyinfo->display, "NULL", False);
14226 dpyinfo->Xatom_ATOM_PAIR
14227 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
14228 /* For properties of font. */
14229 dpyinfo->Xatom_PIXEL_SIZE
14230 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
14231 dpyinfo->Xatom_MULE_BASELINE_OFFSET
14232 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
14233 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
14234 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
14235 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
14236 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
14237
14238 /* Ghostscript support. */
14239 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
14240 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
14241
14242 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
14243 False);
14244
14245 dpyinfo->cut_buffers_initialized = 0;
14246
14247 connection = ConnectionNumber (dpyinfo->display);
14248 dpyinfo->connection = connection;
14249
14250 {
14251 char null_bits[1];
14252
14253 null_bits[0] = 0x00;
14254
14255 dpyinfo->null_pixel
14256 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14257 null_bits, 1, 1, (long) 0, (long) 0,
14258 1);
14259 }
14260
14261 {
14262 extern int gray_bitmap_width, gray_bitmap_height;
14263 extern char *gray_bitmap_bits;
14264 dpyinfo->gray
14265 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14266 gray_bitmap_bits,
14267 gray_bitmap_width, gray_bitmap_height,
14268 (unsigned long) 1, (unsigned long) 0, 1);
14269 }
14270
14271 #ifdef HAVE_X_I18N
14272 xim_initialize (dpyinfo, resource_name);
14273 #endif
14274
14275 #ifdef subprocesses
14276 /* This is only needed for distinguishing keyboard and process input. */
14277 if (connection != 0)
14278 add_keyboard_wait_descriptor (connection);
14279 #endif
14280
14281 #ifndef F_SETOWN_BUG
14282 #ifdef F_SETOWN
14283 #ifdef F_SETOWN_SOCK_NEG
14284 /* stdin is a socket here */
14285 fcntl (connection, F_SETOWN, -getpid ());
14286 #else /* ! defined (F_SETOWN_SOCK_NEG) */
14287 fcntl (connection, F_SETOWN, getpid ());
14288 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
14289 #endif /* ! defined (F_SETOWN) */
14290 #endif /* F_SETOWN_BUG */
14291
14292 #ifdef SIGIO
14293 if (interrupt_input)
14294 init_sigio (connection);
14295 #endif /* ! defined (SIGIO) */
14296
14297 #ifdef USE_LUCID
14298 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
14299 /* Make sure that we have a valid font for dialog boxes
14300 so that Xt does not crash. */
14301 {
14302 Display *dpy = dpyinfo->display;
14303 XrmValue d, fr, to;
14304 Font font;
14305 int count;
14306
14307 d.addr = (XPointer)&dpy;
14308 d.size = sizeof (Display *);
14309 fr.addr = XtDefaultFont;
14310 fr.size = sizeof (XtDefaultFont);
14311 to.size = sizeof (Font *);
14312 to.addr = (XPointer)&font;
14313 count = x_catch_errors (dpy);
14314 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
14315 abort ();
14316 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
14317 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
14318 x_uncatch_errors (dpy, count);
14319 }
14320 #endif
14321 #endif
14322
14323 /* See if we should run in synchronous mode. This is useful
14324 for debugging X code. */
14325 {
14326 Lisp_Object value;
14327 value = display_x_get_resource (dpyinfo,
14328 build_string ("synchronous"),
14329 build_string ("Synchronous"),
14330 Qnil, Qnil);
14331 if (STRINGP (value)
14332 && (!strcmp (XSTRING (value)->data, "true")
14333 || !strcmp (XSTRING (value)->data, "on")))
14334 XSynchronize (dpyinfo->display, True);
14335 }
14336
14337 UNBLOCK_INPUT;
14338
14339 return dpyinfo;
14340 }
14341 \f
14342 /* Get rid of display DPYINFO, assuming all frames are already gone,
14343 and without sending any more commands to the X server. */
14344
14345 void
14346 x_delete_display (dpyinfo)
14347 struct x_display_info *dpyinfo;
14348 {
14349 delete_keyboard_wait_descriptor (dpyinfo->connection);
14350
14351 /* Discard this display from x_display_name_list and x_display_list.
14352 We can't use Fdelq because that can quit. */
14353 if (! NILP (x_display_name_list)
14354 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
14355 x_display_name_list = XCDR (x_display_name_list);
14356 else
14357 {
14358 Lisp_Object tail;
14359
14360 tail = x_display_name_list;
14361 while (CONSP (tail) && CONSP (XCDR (tail)))
14362 {
14363 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
14364 {
14365 XCDR (tail) = XCDR (XCDR (tail));
14366 break;
14367 }
14368 tail = XCDR (tail);
14369 }
14370 }
14371
14372 if (next_noop_dpyinfo == dpyinfo)
14373 next_noop_dpyinfo = dpyinfo->next;
14374
14375 if (x_display_list == dpyinfo)
14376 x_display_list = dpyinfo->next;
14377 else
14378 {
14379 struct x_display_info *tail;
14380
14381 for (tail = x_display_list; tail; tail = tail->next)
14382 if (tail->next == dpyinfo)
14383 tail->next = tail->next->next;
14384 }
14385
14386 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
14387 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
14388 XrmDestroyDatabase (dpyinfo->xrdb);
14389 #endif
14390 #endif
14391 #ifdef MULTI_KBOARD
14392 if (--dpyinfo->kboard->reference_count == 0)
14393 delete_kboard (dpyinfo->kboard);
14394 #endif
14395 #ifdef HAVE_X_I18N
14396 if (dpyinfo->xim)
14397 xim_close_dpy (dpyinfo);
14398 #endif
14399
14400 xfree (dpyinfo->font_table);
14401 xfree (dpyinfo->x_id_name);
14402 xfree (dpyinfo->color_cells);
14403 xfree (dpyinfo);
14404 }
14405
14406 \f
14407 /* Set up use of X before we make the first connection. */
14408
14409 static struct redisplay_interface x_redisplay_interface =
14410 {
14411 x_produce_glyphs,
14412 x_write_glyphs,
14413 x_insert_glyphs,
14414 x_clear_end_of_line,
14415 x_scroll_run,
14416 x_after_update_window_line,
14417 x_update_window_begin,
14418 x_update_window_end,
14419 XTcursor_to,
14420 x_flush,
14421 x_clear_mouse_face,
14422 x_get_glyph_overhangs,
14423 x_fix_overlapping_area
14424 };
14425
14426 void
14427 x_initialize ()
14428 {
14429 rif = &x_redisplay_interface;
14430
14431 clear_frame_hook = x_clear_frame;
14432 ins_del_lines_hook = x_ins_del_lines;
14433 change_line_highlight_hook = x_change_line_highlight;
14434 delete_glyphs_hook = x_delete_glyphs;
14435 ring_bell_hook = XTring_bell;
14436 reset_terminal_modes_hook = XTreset_terminal_modes;
14437 set_terminal_modes_hook = XTset_terminal_modes;
14438 update_begin_hook = x_update_begin;
14439 update_end_hook = x_update_end;
14440 set_terminal_window_hook = XTset_terminal_window;
14441 read_socket_hook = XTread_socket;
14442 frame_up_to_date_hook = XTframe_up_to_date;
14443 reassert_line_highlight_hook = XTreassert_line_highlight;
14444 mouse_position_hook = XTmouse_position;
14445 frame_rehighlight_hook = XTframe_rehighlight;
14446 frame_raise_lower_hook = XTframe_raise_lower;
14447 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
14448 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
14449 redeem_scroll_bar_hook = XTredeem_scroll_bar;
14450 judge_scroll_bars_hook = XTjudge_scroll_bars;
14451 estimate_mode_line_height_hook = x_estimate_mode_line_height;
14452
14453 scroll_region_ok = 1; /* we'll scroll partial frames */
14454 char_ins_del_ok = 1;
14455 line_ins_del_ok = 1; /* we'll just blt 'em */
14456 fast_clear_end_of_line = 1; /* X does this well */
14457 memory_below_frame = 0; /* we don't remember what scrolls
14458 off the bottom */
14459 baud_rate = 19200;
14460
14461 x_noop_count = 0;
14462 last_tool_bar_item = -1;
14463 any_help_event_p = 0;
14464
14465 /* Try to use interrupt input; if we can't, then start polling. */
14466 Fset_input_mode (Qt, Qnil, Qt, Qnil);
14467
14468 #ifdef USE_X_TOOLKIT
14469 XtToolkitInitialize ();
14470
14471 Xt_app_con = XtCreateApplicationContext ();
14472
14473 /* Register a converter from strings to pixels, which uses
14474 Emacs' color allocation infrastructure. */
14475 XtAppSetTypeConverter (Xt_app_con,
14476 XtRString, XtRPixel, cvt_string_to_pixel,
14477 cvt_string_to_pixel_args,
14478 XtNumber (cvt_string_to_pixel_args),
14479 XtCacheByDisplay, cvt_pixel_dtor);
14480
14481 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
14482
14483 /* Install an asynchronous timer that processes Xt timeout events
14484 every 0.1s. This is necessary because some widget sets use
14485 timeouts internally, for example the LessTif menu bar, or the
14486 Xaw3d scroll bar. When Xt timouts aren't processed, these
14487 widgets don't behave normally. */
14488 {
14489 EMACS_TIME interval;
14490 EMACS_SET_SECS_USECS (interval, 0, 100000);
14491 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
14492 }
14493 #endif
14494
14495 #ifdef USE_TOOLKIT_SCROLL_BARS
14496 xaw3d_arrow_scroll = False;
14497 xaw3d_pick_top = True;
14498 #endif
14499
14500 /* Note that there is no real way portable across R3/R4 to get the
14501 original error handler. */
14502 XSetErrorHandler (x_error_handler);
14503 XSetIOErrorHandler (x_io_error_quitter);
14504
14505 /* Disable Window Change signals; they are handled by X events. */
14506 #ifdef SIGWINCH
14507 signal (SIGWINCH, SIG_DFL);
14508 #endif /* ! defined (SIGWINCH) */
14509
14510 signal (SIGPIPE, x_connection_signal);
14511 }
14512
14513
14514 void
14515 syms_of_xterm ()
14516 {
14517 staticpro (&x_error_message_string);
14518 x_error_message_string = Qnil;
14519
14520 staticpro (&x_display_name_list);
14521 x_display_name_list = Qnil;
14522
14523 staticpro (&last_mouse_scroll_bar);
14524 last_mouse_scroll_bar = Qnil;
14525
14526 staticpro (&Qvendor_specific_keysyms);
14527 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
14528
14529 staticpro (&last_mouse_press_frame);
14530 last_mouse_press_frame = Qnil;
14531
14532 help_echo = Qnil;
14533 staticpro (&help_echo);
14534 help_echo_object = Qnil;
14535 staticpro (&help_echo_object);
14536 help_echo_window = Qnil;
14537 staticpro (&help_echo_window);
14538 previous_help_echo = Qnil;
14539 staticpro (&previous_help_echo);
14540 help_echo_pos = -1;
14541
14542 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
14543 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
14544 For example, if a block cursor is over a tab, it will be drawn as\n\
14545 wide as that tab on the display.");
14546 x_stretch_cursor_p = 0;
14547
14548 DEFVAR_BOOL ("x-use-underline-position-properties",
14549 &x_use_underline_position_properties,
14550 "*Non-nil means make use of UNDERLINE_POSITION font properties.\n\
14551 Nil means ignore them. If you encounter fonts with bogus\n\
14552 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior\n\
14553 to 4.1, set this to nil.");
14554 x_use_underline_position_properties = 1;
14555
14556 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
14557 "What X toolkit scroll bars Emacs uses.\n\
14558 A value of nil means Emacs doesn't use X toolkit scroll bars.\n\
14559 Otherwise, value is a symbol describing the X toolkit.");
14560 #ifdef USE_TOOLKIT_SCROLL_BARS
14561 #ifdef USE_MOTIF
14562 Vx_toolkit_scroll_bars = intern ("motif");
14563 #elif defined HAVE_XAW3D
14564 Vx_toolkit_scroll_bars = intern ("xaw3d");
14565 #else
14566 Vx_toolkit_scroll_bars = intern ("xaw");
14567 #endif
14568 #else
14569 Vx_toolkit_scroll_bars = Qnil;
14570 #endif
14571
14572 staticpro (&last_mouse_motion_frame);
14573 last_mouse_motion_frame = Qnil;
14574 }
14575
14576 #endif /* not HAVE_X_WINDOWS */