]> code.delx.au - gnu-emacs/blob - src/xterm.c
*** empty log message ***
[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
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 #include <Xm/ScrollBarP.h>
121 #else /* !USE_MOTIF i.e. use Xaw */
122
123 #ifdef HAVE_XAW3D
124 #include <X11/Xaw3d/Simple.h>
125 #include <X11/Xaw3d/Scrollbar.h>
126 #define ARROW_SCROLLBAR
127 #include <X11/Xaw3d/ScrollbarP.h>
128 #else /* !HAVE_XAW3D */
129 #include <X11/Xaw/Simple.h>
130 #include <X11/Xaw/Scrollbar.h>
131 #endif /* !HAVE_XAW3D */
132 #ifndef XtNpickTop
133 #define XtNpickTop "pickTop"
134 #endif /* !XtNpickTop */
135 #endif /* !USE_MOTIF */
136 #endif /* USE_TOOLKIT_SCROLL_BARS */
137
138 #endif /* USE_X_TOOLKIT */
139
140 #ifndef USE_X_TOOLKIT
141 #define x_any_window_to_frame x_window_to_frame
142 #define x_top_window_to_frame x_window_to_frame
143 #endif
144
145 #ifdef USE_X_TOOLKIT
146 #include "widget.h"
147 #ifndef XtNinitialState
148 #define XtNinitialState "initialState"
149 #endif
150 #endif
151
152 #ifdef SOLARIS2
153 /* memmove will be defined as a macro in Xfuncs.h unless
154 <string.h> is included beforehand. The declaration for memmove in
155 <string.h> will cause a syntax error when Xfuncs.h later includes it. */
156 #include <string.h>
157 #endif
158
159 #ifndef min
160 #define min(a,b) ((a) < (b) ? (a) : (b))
161 #endif
162 #ifndef max
163 #define max(a,b) ((a) > (b) ? (a) : (b))
164 #endif
165
166 #define abs(x) ((x) < 0 ? -(x) : (x))
167
168 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
169
170 \f
171 /* Bitmaps for truncated lines. */
172
173 enum bitmap_type
174 {
175 NO_BITMAP,
176 LEFT_TRUNCATION_BITMAP,
177 RIGHT_TRUNCATION_BITMAP,
178 OVERLAY_ARROW_BITMAP,
179 CONTINUED_LINE_BITMAP,
180 CONTINUATION_LINE_BITMAP,
181 ZV_LINE_BITMAP
182 };
183
184 /* Bitmap drawn to indicate lines not displaying text if
185 `indicate-empty-lines' is non-nil. */
186
187 #define zv_width 8
188 #define zv_height 8
189 static unsigned char zv_bits[] = {
190 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
191
192 /* An arrow like this: `<-'. */
193
194 #define left_width 8
195 #define left_height 8
196 static unsigned char left_bits[] = {
197 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
198
199 /* Right truncation arrow bitmap `->'. */
200
201 #define right_width 8
202 #define right_height 8
203 static unsigned char right_bits[] = {
204 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
205
206 /* Marker for continued lines. */
207
208 #define continued_width 8
209 #define continued_height 8
210 static unsigned char continued_bits[] = {
211 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
212
213 /* Marker for continuation lines. */
214
215 #define continuation_width 8
216 #define continuation_height 8
217 static unsigned char continuation_bits[] = {
218 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
219
220 /* Overlay arrow bitmap. */
221
222 #if 0
223 /* A bomb. */
224 #define ov_width 8
225 #define ov_height 8
226 static unsigned char ov_bits[] = {
227 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
228 #else
229 /* A triangular arrow. */
230 #define ov_width 8
231 #define ov_height 8
232 static unsigned char ov_bits[] = {
233 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
234
235 #endif
236
237 extern Lisp_Object Qhelp_echo;
238
239 \f
240 /* Non-zero means Emacs uses toolkit scroll bars. */
241
242 int x_toolkit_scroll_bars_p;
243
244 /* If a string, XTread_socket generates an event to display that string.
245 (The display is done in read_char.) */
246
247 static Lisp_Object help_echo;
248 static Lisp_Object help_echo_window;
249 static Lisp_Object help_echo_object;
250 static int help_echo_pos;
251
252 /* Temporary variable for XTread_socket. */
253
254 static Lisp_Object previous_help_echo;
255
256 /* Non-zero means that a HELP_EVENT has been generated since Emacs
257 start. */
258
259 static int any_help_event_p;
260
261 /* Non-zero means draw block and hollow cursor as wide as the glyph
262 under it. For example, if a block cursor is over a tab, it will be
263 drawn as wide as that tab on the display. */
264
265 int x_stretch_cursor_p;
266
267 /* This is a chain of structures for all the X displays currently in
268 use. */
269
270 struct x_display_info *x_display_list;
271
272 /* This is a list of cons cells, each of the form (NAME
273 . FONT-LIST-CACHE), one for each element of x_display_list and in
274 the same order. NAME is the name of the frame. FONT-LIST-CACHE
275 records previous values returned by x-list-fonts. */
276
277 Lisp_Object x_display_name_list;
278
279 /* Frame being updated by update_frame. This is declared in term.c.
280 This is set by update_begin and looked at by all the XT functions.
281 It is zero while not inside an update. In that case, the XT
282 functions assume that `selected_frame' is the frame to apply to. */
283
284 extern struct frame *updating_frame;
285
286 extern int waiting_for_input;
287
288 /* This is a frame waiting to be auto-raised, within XTread_socket. */
289
290 struct frame *pending_autoraise_frame;
291
292 #ifdef USE_X_TOOLKIT
293 /* The application context for Xt use. */
294 XtAppContext Xt_app_con;
295 static String Xt_default_resources[] = {0};
296 #endif /* USE_X_TOOLKIT */
297
298 /* Nominal cursor position -- where to draw output.
299 HPOS and VPOS are window relative glyph matrix coordinates.
300 X and Y are window relative pixel coordinates. */
301
302 struct cursor_pos output_cursor;
303
304 /* Non-zero means user is interacting with a toolkit scroll bar. */
305
306 static int toolkit_scroll_bar_interaction;
307
308 /* Mouse movement.
309
310 Formerly, we used PointerMotionHintMask (in standard_event_mask)
311 so that we would have to call XQueryPointer after each MotionNotify
312 event to ask for another such event. However, this made mouse tracking
313 slow, and there was a bug that made it eventually stop.
314
315 Simply asking for MotionNotify all the time seems to work better.
316
317 In order to avoid asking for motion events and then throwing most
318 of them away or busy-polling the server for mouse positions, we ask
319 the server for pointer motion hints. This means that we get only
320 one event per group of mouse movements. "Groups" are delimited by
321 other kinds of events (focus changes and button clicks, for
322 example), or by XQueryPointer calls; when one of these happens, we
323 get another MotionNotify event the next time the mouse moves. This
324 is at least as efficient as getting motion events when mouse
325 tracking is on, and I suspect only negligibly worse when tracking
326 is off. */
327
328 /* Where the mouse was last time we reported a mouse event. */
329
330 FRAME_PTR last_mouse_frame;
331 static XRectangle last_mouse_glyph;
332 static Lisp_Object last_mouse_press_frame;
333
334 /* The scroll bar in which the last X motion event occurred.
335
336 If the last X motion event occurred in a scroll bar, we set this so
337 XTmouse_position can know whether to report a scroll bar motion or
338 an ordinary motion.
339
340 If the last X motion event didn't occur in a scroll bar, we set
341 this to Qnil, to tell XTmouse_position to return an ordinary motion
342 event. */
343
344 static Lisp_Object last_mouse_scroll_bar;
345
346 /* This is a hack. We would really prefer that XTmouse_position would
347 return the time associated with the position it returns, but there
348 doesn't seem to be any way to wrest the time-stamp from the server
349 along with the position query. So, we just keep track of the time
350 of the last movement we received, and return that in hopes that
351 it's somewhat accurate. */
352
353 static Time last_mouse_movement_time;
354
355 /* Incremented by XTread_socket whenever it really tries to read
356 events. */
357
358 #ifdef __STDC__
359 static int volatile input_signal_count;
360 #else
361 static int input_signal_count;
362 #endif
363
364 /* Used locally within XTread_socket. */
365
366 static int x_noop_count;
367
368 /* Initial values of argv and argc. */
369
370 extern char **initial_argv;
371 extern int initial_argc;
372
373 extern Lisp_Object Vcommand_line_args, Vsystem_name;
374
375 /* Tells if a window manager is present or not. */
376
377 extern Lisp_Object Vx_no_window_manager;
378
379 extern Lisp_Object Qface, Qmouse_face;
380
381 extern int errno;
382
383 /* A mask of extra modifier bits to put into every keyboard char. */
384
385 extern int extra_keyboard_modifiers;
386
387 static Lisp_Object Qvendor_specific_keysyms;
388
389 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
390 extern Lisp_Object x_icon_type P_ ((struct frame *));
391
392
393 /* Enumeration for overriding/changing the face to use for drawing
394 glyphs in x_draw_glyphs. */
395
396 enum draw_glyphs_face
397 {
398 DRAW_NORMAL_TEXT,
399 DRAW_INVERSE_VIDEO,
400 DRAW_CURSOR,
401 DRAW_MOUSE_FACE,
402 DRAW_IMAGE_RAISED,
403 DRAW_IMAGE_SUNKEN
404 };
405
406 static void x_update_window_end P_ ((struct window *, int, int));
407 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
408 void x_delete_display P_ ((struct x_display_info *));
409 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
410 unsigned));
411 static int fast_find_position P_ ((struct window *, int, int *, int *,
412 int *, int *));
413 static void set_output_cursor P_ ((struct cursor_pos *));
414 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
415 int *, int *, int *));
416 static void note_mode_line_highlight P_ ((struct window *, int, int));
417 static void note_mouse_highlight P_ ((struct frame *, int, int));
418 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
419 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
420 static void show_mouse_face P_ ((struct x_display_info *,
421 enum draw_glyphs_face));
422 static int x_io_error_quitter P_ ((Display *));
423 int x_catch_errors P_ ((Display *));
424 void x_uncatch_errors P_ ((Display *, int));
425 void x_lower_frame P_ ((struct frame *));
426 void x_scroll_bar_clear P_ ((struct frame *));
427 int x_had_errors_p P_ ((Display *));
428 void x_wm_set_size_hint P_ ((struct frame *, long, int));
429 void x_raise_frame P_ ((struct frame *));
430 void x_set_window_size P_ ((struct frame *, int, int, int));
431 void x_wm_set_window_state P_ ((struct frame *, int));
432 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
433 void x_initialize P_ ((void));
434 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
435 static int x_compute_min_glyph_bounds P_ ((struct frame *));
436 static void x_draw_phys_cursor_glyph P_ ((struct window *,
437 struct glyph_row *,
438 enum draw_glyphs_face));
439 static void x_update_end P_ ((struct frame *));
440 static void XTframe_up_to_date P_ ((struct frame *));
441 static void XTreassert_line_highlight P_ ((int, int));
442 static void x_change_line_highlight P_ ((int, int, int, int));
443 static void XTset_terminal_modes P_ ((void));
444 static void XTreset_terminal_modes P_ ((void));
445 static void XTcursor_to P_ ((int, int, int, int));
446 static void x_write_glyphs P_ ((struct glyph *, int));
447 static void x_clear_end_of_line P_ ((int));
448 static void x_clear_frame P_ ((void));
449 static void x_clear_cursor P_ ((struct window *));
450 static void frame_highlight P_ ((struct frame *));
451 static void frame_unhighlight P_ ((struct frame *));
452 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
453 static void XTframe_rehighlight P_ ((struct frame *));
454 static void x_frame_rehighlight P_ ((struct x_display_info *));
455 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
456 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
457 static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
458 XRectangle *));
459 static void expose_frame P_ ((struct frame *, int, int, int, int));
460 static void expose_window_tree P_ ((struct window *, XRectangle *));
461 static void expose_window P_ ((struct window *, XRectangle *));
462 static void expose_area P_ ((struct window *, struct glyph_row *,
463 XRectangle *, enum glyph_row_area));
464 static void expose_line P_ ((struct window *, struct glyph_row *,
465 XRectangle *));
466 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
467 static void x_update_window_cursor P_ ((struct window *, int));
468 static void x_erase_phys_cursor P_ ((struct window *));
469 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
470 static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
471 enum bitmap_type));
472
473 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
474 GC, int));
475 static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
476 static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
477 static void note_overwritten_text_cursor P_ ((struct window *, int, int));
478 static void x_flush P_ ((struct frame *f));
479 static void x_update_begin P_ ((struct frame *));
480 static void x_update_window_begin P_ ((struct window *));
481 static void x_draw_vertical_border P_ ((struct window *));
482 static void x_after_update_window_line P_ ((struct glyph_row *));
483 static INLINE void take_vertical_position_into_account P_ ((struct it *));
484 static void x_produce_stretch_glyph P_ ((struct it *));
485
486
487 /* Flush display of frame F, or of all frames if F is null. */
488
489 static void
490 x_flush (f)
491 struct frame *f;
492 {
493 BLOCK_INPUT;
494 if (f == NULL)
495 {
496 Lisp_Object rest, frame;
497 FOR_EACH_FRAME (rest, frame)
498 x_flush (XFRAME (frame));
499 }
500 else if (FRAME_X_P (f))
501 XFlush (FRAME_X_DISPLAY (f));
502 UNBLOCK_INPUT;
503 }
504
505
506 /* Remove calls to XFlush by defining XFlush to an empty replacement.
507 Calls to XFlush should be unnecessary because the X output buffer
508 is flushed automatically as needed by calls to XPending,
509 XNextEvent, or XWindowEvent according to the XFlush man page.
510 XTread_socket calls XPending. Removing XFlush improves
511 performance. */
512
513 #define XFlush(DISPLAY) (void) 0
514
515 \f
516 /***********************************************************************
517 Debugging
518 ***********************************************************************/
519
520 #if 0
521
522 /* This is a function useful for recording debugging information about
523 the sequence of occurrences in this file. */
524
525 struct record
526 {
527 char *locus;
528 int type;
529 };
530
531 struct record event_record[100];
532
533 int event_record_index;
534
535 record_event (locus, type)
536 char *locus;
537 int type;
538 {
539 if (event_record_index == sizeof (event_record) / sizeof (struct record))
540 event_record_index = 0;
541
542 event_record[event_record_index].locus = locus;
543 event_record[event_record_index].type = type;
544 event_record_index++;
545 }
546
547 #endif /* 0 */
548
549
550 \f
551 /* Return the struct x_display_info corresponding to DPY. */
552
553 struct x_display_info *
554 x_display_info_for_display (dpy)
555 Display *dpy;
556 {
557 struct x_display_info *dpyinfo;
558
559 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
560 if (dpyinfo->display == dpy)
561 return dpyinfo;
562
563 return 0;
564 }
565
566
567 \f
568 /***********************************************************************
569 Starting and ending an update
570 ***********************************************************************/
571
572 /* Start an update of frame F. This function is installed as a hook
573 for update_begin, i.e. it is called when update_begin is called.
574 This function is called prior to calls to x_update_window_begin for
575 each window being updated. Currently, there is nothing to do here
576 because all interesting stuff is done on a window basis. */
577
578 static void
579 x_update_begin (f)
580 struct frame *f;
581 {
582 /* Nothing to do. */
583 }
584
585
586 /* Start update of window W. Set the global variable updated_window
587 to the window being updated and set output_cursor to the cursor
588 position of W. */
589
590 static void
591 x_update_window_begin (w)
592 struct window *w;
593 {
594 struct frame *f = XFRAME (WINDOW_FRAME (w));
595 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
596
597 updated_window = w;
598 set_output_cursor (&w->cursor);
599
600 BLOCK_INPUT;
601
602 if (f == display_info->mouse_face_mouse_frame)
603 {
604 /* Don't do highlighting for mouse motion during the update. */
605 display_info->mouse_face_defer = 1;
606
607 /* If F needs to be redrawn, simply forget about any prior mouse
608 highlighting. */
609 if (FRAME_GARBAGED_P (f))
610 display_info->mouse_face_window = Qnil;
611
612 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
613 their mouse_face_p flag set, which means that they are always
614 unequal to rows in a desired matrix which never have that
615 flag set. So, rows containing mouse-face glyphs are never
616 scrolled, and we don't have to switch the mouse highlight off
617 here to prevent it from being scrolled. */
618
619 /* Can we tell that this update does not affect the window
620 where the mouse highlight is? If so, no need to turn off.
621 Likewise, don't do anything if the frame is garbaged;
622 in that case, the frame's current matrix that we would use
623 is all wrong, and we will redisplay that line anyway. */
624 if (!NILP (display_info->mouse_face_window)
625 && w == XWINDOW (display_info->mouse_face_window))
626 {
627 int i;
628
629 for (i = 0; i < w->desired_matrix->nrows; ++i)
630 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
631 break;
632
633 if (i < w->desired_matrix->nrows)
634 clear_mouse_face (display_info);
635 }
636 #endif /* 0 */
637 }
638
639 UNBLOCK_INPUT;
640 }
641
642
643 /* Draw a vertical window border to the right of window W if W doesn't
644 have vertical scroll bars. */
645
646 static void
647 x_draw_vertical_border (w)
648 struct window *w;
649 {
650 struct frame *f = XFRAME (WINDOW_FRAME (w));
651
652 /* Redraw borders between horizontally adjacent windows. Don't
653 do it for frames with vertical scroll bars because either the
654 right scroll bar of a window, or the left scroll bar of its
655 neighbor will suffice as a border. */
656 if (!WINDOW_RIGHTMOST_P (w)
657 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
658 {
659 int x0, x1, y0, y1;
660
661 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
662 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
663 y1 -= 1;
664
665 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
666 f->output_data.x->normal_gc, x1, y0, x1, y1);
667 }
668 }
669
670
671 /* End update of window W (which is equal to updated_window).
672
673 Draw vertical borders between horizontally adjacent windows, and
674 display W's cursor if CURSOR_ON_P is non-zero.
675
676 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
677 glyphs in mouse-face were overwritten. In that case we have to
678 make sure that the mouse-highlight is properly redrawn.
679
680 W may be a menu bar pseudo-window in case we don't have X toolkit
681 support. Such windows don't have a cursor, so don't display it
682 here. */
683
684 static void
685 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
686 struct window *w;
687 int cursor_on_p, mouse_face_overwritten_p;
688 {
689 if (!w->pseudo_window_p)
690 {
691 struct x_display_info *dpyinfo
692 = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
693
694 BLOCK_INPUT;
695
696 /* If a row with mouse-face was overwritten, arrange for
697 XTframe_up_to_date to redisplay the mouse highlight. */
698 if (mouse_face_overwritten_p)
699 {
700 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
701 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
702 dpyinfo->mouse_face_window = Qnil;
703 }
704
705 if (cursor_on_p)
706 x_display_and_set_cursor (w, 1, output_cursor.hpos,
707 output_cursor.vpos,
708 output_cursor.x, output_cursor.y);
709
710 x_draw_vertical_border (w);
711 UNBLOCK_INPUT;
712 }
713
714 updated_window = NULL;
715 }
716
717
718 /* End update of frame F. This function is installed as a hook in
719 update_end. */
720
721 static void
722 x_update_end (f)
723 struct frame *f;
724 {
725 /* Mouse highlight may be displayed again. */
726 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
727
728 BLOCK_INPUT;
729 XFlush (FRAME_X_DISPLAY (f));
730 UNBLOCK_INPUT;
731 }
732
733
734 /* This function is called from various places in xdisp.c whenever a
735 complete update has been performed. The global variable
736 updated_window is not available here. */
737
738 static void
739 XTframe_up_to_date (f)
740 struct frame *f;
741 {
742 if (FRAME_X_P (f))
743 {
744 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
745
746 if (dpyinfo->mouse_face_deferred_gc
747 || f == dpyinfo->mouse_face_mouse_frame)
748 {
749 BLOCK_INPUT;
750 if (dpyinfo->mouse_face_mouse_frame)
751 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
752 dpyinfo->mouse_face_mouse_x,
753 dpyinfo->mouse_face_mouse_y);
754 dpyinfo->mouse_face_deferred_gc = 0;
755 UNBLOCK_INPUT;
756 }
757 }
758 }
759
760
761 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
762 arrow bitmaps, or clear the areas where they would be displayed
763 before DESIRED_ROW is made current. The window being updated is
764 found in updated_window. This function It is called from
765 update_window_line only if it is known that there are differences
766 between bitmaps to be drawn between current row and DESIRED_ROW. */
767
768 static void
769 x_after_update_window_line (desired_row)
770 struct glyph_row *desired_row;
771 {
772 struct window *w = updated_window;
773
774 xassert (w);
775
776 if (!desired_row->mode_line_p && !w->pseudo_window_p)
777 {
778 BLOCK_INPUT;
779 x_draw_row_bitmaps (w, desired_row);
780
781 /* When a window has disappeared, make sure that no rest of
782 full-width rows stays visible in the internal border. */
783 if (windows_or_buffers_changed)
784 {
785 struct frame *f = XFRAME (w->frame);
786 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
787 int height = desired_row->visible_height;
788 int x = (window_box_right (w, -1)
789 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
790 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
791
792 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
793 x, y, width, height, False);
794 }
795
796 UNBLOCK_INPUT;
797 }
798 }
799
800
801 /* Draw the bitmap WHICH in one of the areas to the left or right of
802 window W. ROW is the glyph row for which to display the bitmap; it
803 determines the vertical position at which the bitmap has to be
804 drawn. */
805
806 static void
807 x_draw_bitmap (w, row, which)
808 struct window *w;
809 struct glyph_row *row;
810 enum bitmap_type which;
811 {
812 struct frame *f = XFRAME (WINDOW_FRAME (w));
813 Display *display = FRAME_X_DISPLAY (f);
814 Window window = FRAME_X_WINDOW (f);
815 int x, y, wd, h, dy;
816 unsigned char *bits;
817 Pixmap pixmap;
818 GC gc = f->output_data.x->normal_gc;
819 struct face *face;
820 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
821
822 /* Must clip because of partially visible lines. */
823 x_clip_to_row (w, row, gc, 1);
824
825 switch (which)
826 {
827 case LEFT_TRUNCATION_BITMAP:
828 wd = left_width;
829 h = left_height;
830 bits = left_bits;
831 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
832 - wd
833 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
834 break;
835
836 case OVERLAY_ARROW_BITMAP:
837 wd = left_width;
838 h = left_height;
839 bits = ov_bits;
840 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
841 - wd
842 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
843 break;
844
845 case RIGHT_TRUNCATION_BITMAP:
846 wd = right_width;
847 h = right_height;
848 bits = right_bits;
849 x = window_box_right (w, -1);
850 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
851 break;
852
853 case CONTINUED_LINE_BITMAP:
854 wd = right_width;
855 h = right_height;
856 bits = continued_bits;
857 x = window_box_right (w, -1);
858 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
859 break;
860
861 case CONTINUATION_LINE_BITMAP:
862 wd = continuation_width;
863 h = continuation_height;
864 bits = continuation_bits;
865 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
866 - wd
867 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
868 break;
869
870 case ZV_LINE_BITMAP:
871 wd = zv_width;
872 h = zv_height;
873 bits = zv_bits;
874 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
875 - wd
876 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
877 break;
878
879 default:
880 abort ();
881 }
882
883 /* Convert to frame coordinates. Set dy to the offset in the row to
884 start drawing the bitmap. */
885 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
886 dy = (row->height - h) / 2;
887
888 /* Draw the bitmap. I believe these small pixmaps can be cached
889 by the server. */
890 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
891 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
892 face->foreground,
893 face->background, depth);
894 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
895 XFreePixmap (display, pixmap);
896 XSetClipMask (display, gc, None);
897 }
898
899
900 /* Draw flags bitmaps for glyph row ROW on window W. Call this
901 function with input blocked. */
902
903 static void
904 x_draw_row_bitmaps (w, row)
905 struct window *w;
906 struct glyph_row *row;
907 {
908 struct frame *f = XFRAME (w->frame);
909 enum bitmap_type bitmap;
910 struct face *face;
911 int header_line_height = -1;
912
913 xassert (interrupt_input_blocked);
914
915 /* If row is completely invisible, because of vscrolling, we
916 don't have to draw anything. */
917 if (row->visible_height <= 0)
918 return;
919
920 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
921 PREPARE_FACE_FOR_DISPLAY (f, face);
922
923 /* Decide which bitmap to draw at the left side. */
924 if (row->overlay_arrow_p)
925 bitmap = OVERLAY_ARROW_BITMAP;
926 else if (row->truncated_on_left_p)
927 bitmap = LEFT_TRUNCATION_BITMAP;
928 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
929 bitmap = CONTINUATION_LINE_BITMAP;
930 else if (row->indicate_empty_line_p)
931 bitmap = ZV_LINE_BITMAP;
932 else
933 bitmap = NO_BITMAP;
934
935 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
936 the flags area. */
937 if (bitmap == NO_BITMAP
938 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
939 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
940 {
941 /* If W has a vertical border to its left, don't draw over it. */
942 int border = ((XFASTINT (w->left) > 0
943 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
944 ? 1 : 0);
945 int left = window_box_left (w, -1);
946
947 if (header_line_height < 0)
948 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
949
950 /* In case the same realized face is used for bitmap areas and
951 for something displayed in the text (e.g. face `region' on
952 mono-displays, the fill style may have been changed to
953 FillSolid in x_draw_glyph_string_background. */
954 if (face->stipple)
955 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
956 else
957 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
958
959 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
960 face->gc,
961 (left
962 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
963 + border),
964 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
965 row->y)),
966 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
967 row->visible_height);
968 if (!face->stipple)
969 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
970 }
971
972 /* Draw the left bitmap. */
973 if (bitmap != NO_BITMAP)
974 x_draw_bitmap (w, row, bitmap);
975
976 /* Decide which bitmap to draw at the right side. */
977 if (row->truncated_on_right_p)
978 bitmap = RIGHT_TRUNCATION_BITMAP;
979 else if (row->continued_p)
980 bitmap = CONTINUED_LINE_BITMAP;
981 else
982 bitmap = NO_BITMAP;
983
984 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
985 the flags area. */
986 if (bitmap == NO_BITMAP
987 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
988 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
989 {
990 int right = window_box_right (w, -1);
991
992 if (header_line_height < 0)
993 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
994
995 /* In case the same realized face is used for bitmap areas and
996 for something displayed in the text (e.g. face `region' on
997 mono-displays, the fill style may have been changed to
998 FillSolid in x_draw_glyph_string_background. */
999 if (face->stipple)
1000 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
1001 else
1002 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
1003 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1004 face->gc,
1005 right,
1006 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
1007 row->y)),
1008 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
1009 row->visible_height);
1010 if (!face->stipple)
1011 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
1012 }
1013
1014 /* Draw the right bitmap. */
1015 if (bitmap != NO_BITMAP)
1016 x_draw_bitmap (w, row, bitmap);
1017 }
1018
1019 \f
1020 /***********************************************************************
1021 Line Highlighting
1022 ***********************************************************************/
1023
1024 /* External interface to control of standout mode. Not used for X
1025 frames. Aborts when called. */
1026
1027 static void
1028 XTreassert_line_highlight (new, vpos)
1029 int new, vpos;
1030 {
1031 abort ();
1032 }
1033
1034
1035 /* Call this when about to modify line at position VPOS and change
1036 whether it is highlighted. Not used for X frames. Aborts when
1037 called. */
1038
1039 static void
1040 x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1041 int new_highlight, vpos, y, first_unused_hpos;
1042 {
1043 abort ();
1044 }
1045
1046
1047 /* This is called when starting Emacs and when restarting after
1048 suspend. When starting Emacs, no X window is mapped. And nothing
1049 must be done to Emacs's own window if it is suspended (though that
1050 rarely happens). */
1051
1052 static void
1053 XTset_terminal_modes ()
1054 {
1055 }
1056
1057 /* This is called when exiting or suspending Emacs. Exiting will make
1058 the X-windows go away, and suspending requires no action. */
1059
1060 static void
1061 XTreset_terminal_modes ()
1062 {
1063 }
1064
1065
1066 \f
1067 /***********************************************************************
1068 Output Cursor
1069 ***********************************************************************/
1070
1071 /* Set the global variable output_cursor to CURSOR. All cursor
1072 positions are relative to updated_window. */
1073
1074 static void
1075 set_output_cursor (cursor)
1076 struct cursor_pos *cursor;
1077 {
1078 output_cursor.hpos = cursor->hpos;
1079 output_cursor.vpos = cursor->vpos;
1080 output_cursor.x = cursor->x;
1081 output_cursor.y = cursor->y;
1082 }
1083
1084
1085 /* Set a nominal cursor position.
1086
1087 HPOS and VPOS are column/row positions in a window glyph matrix. X
1088 and Y are window text area relative pixel positions.
1089
1090 If this is done during an update, updated_window will contain the
1091 window that is being updated and the position is the future output
1092 cursor position for that window. If updated_window is null, use
1093 selected_window and display the cursor at the given position. */
1094
1095 static void
1096 XTcursor_to (vpos, hpos, y, x)
1097 int vpos, hpos, y, x;
1098 {
1099 struct window *w;
1100
1101 /* If updated_window is not set, work on selected_window. */
1102 if (updated_window)
1103 w = updated_window;
1104 else
1105 w = XWINDOW (selected_window);
1106
1107 /* Set the output cursor. */
1108 output_cursor.hpos = hpos;
1109 output_cursor.vpos = vpos;
1110 output_cursor.x = x;
1111 output_cursor.y = y;
1112
1113 /* If not called as part of an update, really display the cursor.
1114 This will also set the cursor position of W. */
1115 if (updated_window == NULL)
1116 {
1117 BLOCK_INPUT;
1118 x_display_cursor (w, 1, hpos, vpos, x, y);
1119 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1120 UNBLOCK_INPUT;
1121 }
1122 }
1123
1124
1125 \f
1126 /***********************************************************************
1127 Display Iterator
1128 ***********************************************************************/
1129
1130 /* Function prototypes of this page. */
1131
1132 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1133 struct glyph *,
1134 XChar2b *,
1135 int *));
1136 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1137 int, XChar2b *, int));
1138 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1139 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1140 static void x_append_glyph P_ ((struct it *));
1141 static void x_append_composite_glyph P_ ((struct it *));
1142 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1143 int, int, double));
1144 static void x_produce_glyphs P_ ((struct it *));
1145 static void x_produce_image_glyph P_ ((struct it *it));
1146
1147
1148 /* Return a pointer to per-char metric information in FONT of a
1149 character pointed by B which is a pointer to an XChar2b. */
1150
1151 #define PER_CHAR_METRIC(font, b) \
1152 ((font)->per_char \
1153 ? ((font)->per_char + (b)->byte2 - (font)->min_char_or_byte2 \
1154 + (((font)->min_byte1 || (font)->max_byte1) \
1155 ? (((b)->byte1 - (font)->min_byte1) \
1156 * ((font)->max_char_or_byte2 - (font)->min_char_or_byte2 + 1)) \
1157 : 0)) \
1158 : &((font)->max_bounds))
1159
1160
1161 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1162 is not contained in the font. */
1163
1164 static INLINE XCharStruct *
1165 x_per_char_metric (font, char2b)
1166 XFontStruct *font;
1167 XChar2b *char2b;
1168 {
1169 /* The result metric information. */
1170 XCharStruct *pcm = NULL;
1171
1172 xassert (font && char2b);
1173
1174 if (font->per_char != NULL)
1175 {
1176 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1177 {
1178 /* min_char_or_byte2 specifies the linear character index
1179 corresponding to the first element of the per_char array,
1180 max_char_or_byte2 is the index of the last character. A
1181 character with non-zero CHAR2B->byte1 is not in the font.
1182 A character with byte2 less than min_char_or_byte2 or
1183 greater max_char_or_byte2 is not in the font. */
1184 if (char2b->byte1 == 0
1185 && char2b->byte2 >= font->min_char_or_byte2
1186 && char2b->byte2 <= font->max_char_or_byte2)
1187 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1188 }
1189 else
1190 {
1191 /* If either min_byte1 or max_byte1 are nonzero, both
1192 min_char_or_byte2 and max_char_or_byte2 are less than
1193 256, and the 2-byte character index values corresponding
1194 to the per_char array element N (counting from 0) are:
1195
1196 byte1 = N/D + min_byte1
1197 byte2 = N\D + min_char_or_byte2
1198
1199 where:
1200
1201 D = max_char_or_byte2 - min_char_or_byte2 + 1
1202 / = integer division
1203 \ = integer modulus */
1204 if (char2b->byte1 >= font->min_byte1
1205 && char2b->byte1 <= font->max_byte1
1206 && char2b->byte2 >= font->min_char_or_byte2
1207 && char2b->byte2 <= font->max_char_or_byte2)
1208 {
1209 pcm = (font->per_char
1210 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1211 * (char2b->byte1 - font->min_byte1))
1212 + (char2b->byte2 - font->min_char_or_byte2));
1213 }
1214 }
1215 }
1216 else
1217 {
1218 /* If the per_char pointer is null, all glyphs between the first
1219 and last character indexes inclusive have the same
1220 information, as given by both min_bounds and max_bounds. */
1221 if (char2b->byte2 >= font->min_char_or_byte2
1222 && char2b->byte2 <= font->max_char_or_byte2)
1223 pcm = &font->max_bounds;
1224 }
1225
1226 return ((pcm == NULL
1227 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1228 ? NULL : pcm);
1229 }
1230
1231
1232 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1233 the two-byte form of C. Encoding is returned in *CHAR2B. */
1234
1235 static INLINE void
1236 x_encode_char (c, char2b, font_info)
1237 int c;
1238 XChar2b *char2b;
1239 struct font_info *font_info;
1240 {
1241 int charset = CHAR_CHARSET (c);
1242 XFontStruct *font = font_info->font;
1243
1244 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1245 This may be either a program in a special encoder language or a
1246 fixed encoding. */
1247 if (font_info->font_encoder)
1248 {
1249 /* It's a program. */
1250 struct ccl_program *ccl = font_info->font_encoder;
1251
1252 if (CHARSET_DIMENSION (charset) == 1)
1253 {
1254 ccl->reg[0] = charset;
1255 ccl->reg[1] = char2b->byte2;
1256 }
1257 else
1258 {
1259 ccl->reg[0] = charset;
1260 ccl->reg[1] = char2b->byte1;
1261 ccl->reg[2] = char2b->byte2;
1262 }
1263
1264 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1265
1266 /* We assume that MSBs are appropriately set/reset by CCL
1267 program. */
1268 if (font->max_byte1 == 0) /* 1-byte font */
1269 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1270 else
1271 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1272 }
1273 else if (font_info->encoding[charset])
1274 {
1275 /* Fixed encoding scheme. See fontset.h for the meaning of the
1276 encoding numbers. */
1277 int enc = font_info->encoding[charset];
1278
1279 if ((enc == 1 || enc == 2)
1280 && CHARSET_DIMENSION (charset) == 2)
1281 char2b->byte1 |= 0x80;
1282
1283 if (enc == 1 || enc == 3)
1284 char2b->byte2 |= 0x80;
1285 }
1286 }
1287
1288
1289 /* Get face and two-byte form of character C in face FACE_ID on frame
1290 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1291 means we want to display multibyte text. Value is a pointer to a
1292 realized face that is ready for display. */
1293
1294 static INLINE struct face *
1295 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1296 struct frame *f;
1297 int c, face_id;
1298 XChar2b *char2b;
1299 int multibyte_p;
1300 {
1301 struct face *face = FACE_FROM_ID (f, face_id);
1302
1303 if (!multibyte_p)
1304 {
1305 /* Unibyte case. We don't have to encode, but we have to make
1306 sure to use a face suitable for unibyte. */
1307 char2b->byte1 = 0;
1308 char2b->byte2 = c;
1309 face_id = FACE_FOR_CHAR (f, face, c);
1310 face = FACE_FROM_ID (f, face_id);
1311 }
1312 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1313 {
1314 /* Case of ASCII in a face known to fit ASCII. */
1315 char2b->byte1 = 0;
1316 char2b->byte2 = c;
1317 }
1318 else
1319 {
1320 int c1, c2, charset;
1321
1322 /* Split characters into bytes. If c2 is -1 afterwards, C is
1323 really a one-byte character so that byte1 is zero. */
1324 SPLIT_CHAR (c, charset, c1, c2);
1325 if (c2 > 0)
1326 char2b->byte1 = c1, char2b->byte2 = c2;
1327 else
1328 char2b->byte1 = 0, char2b->byte2 = c1;
1329
1330 /* Maybe encode the character in *CHAR2B. */
1331 if (face->font != NULL)
1332 {
1333 struct font_info *font_info
1334 = FONT_INFO_FROM_ID (f, face->font_info_id);
1335 if (font_info)
1336 x_encode_char (c, char2b, font_info);
1337 }
1338 }
1339
1340 /* Make sure X resources of the face are allocated. */
1341 xassert (face != NULL);
1342 PREPARE_FACE_FOR_DISPLAY (f, face);
1343
1344 return face;
1345 }
1346
1347
1348 /* Get face and two-byte form of character glyph GLYPH on frame F.
1349 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1350 a pointer to a realized face that is ready for display. */
1351
1352 static INLINE struct face *
1353 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1354 struct frame *f;
1355 struct glyph *glyph;
1356 XChar2b *char2b;
1357 int *two_byte_p;
1358 {
1359 struct face *face;
1360
1361 xassert (glyph->type == CHAR_GLYPH);
1362 face = FACE_FROM_ID (f, glyph->face_id);
1363
1364 if (two_byte_p)
1365 *two_byte_p = 0;
1366
1367 if (!glyph->multibyte_p)
1368 {
1369 /* Unibyte case. We don't have to encode, but we have to make
1370 sure to use a face suitable for unibyte. */
1371 char2b->byte1 = 0;
1372 char2b->byte2 = glyph->u.ch;
1373 }
1374 else if (glyph->u.ch < 128
1375 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1376 {
1377 /* Case of ASCII in a face known to fit ASCII. */
1378 char2b->byte1 = 0;
1379 char2b->byte2 = glyph->u.ch;
1380 }
1381 else
1382 {
1383 int c1, c2, charset;
1384
1385 /* Split characters into bytes. If c2 is -1 afterwards, C is
1386 really a one-byte character so that byte1 is zero. */
1387 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1388 if (c2 > 0)
1389 char2b->byte1 = c1, char2b->byte2 = c2;
1390 else
1391 char2b->byte1 = 0, char2b->byte2 = c1;
1392
1393 /* Maybe encode the character in *CHAR2B. */
1394 if (charset != CHARSET_ASCII)
1395 {
1396 struct font_info *font_info
1397 = FONT_INFO_FROM_ID (f, face->font_info_id);
1398 if (font_info)
1399 {
1400 x_encode_char (glyph->u.ch, char2b, font_info);
1401 if (two_byte_p)
1402 *two_byte_p
1403 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1404 }
1405 }
1406 }
1407
1408 /* Make sure X resources of the face are allocated. */
1409 xassert (face != NULL);
1410 PREPARE_FACE_FOR_DISPLAY (f, face);
1411 return face;
1412 }
1413
1414
1415 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1416 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1417
1418 static INLINE void
1419 x_append_glyph (it)
1420 struct it *it;
1421 {
1422 struct glyph *glyph;
1423 enum glyph_row_area area = it->area;
1424
1425 xassert (it->glyph_row);
1426 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1427
1428 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1429 if (glyph < it->glyph_row->glyphs[area + 1])
1430 {
1431 glyph->charpos = CHARPOS (it->position);
1432 glyph->object = it->object;
1433 glyph->pixel_width = it->pixel_width;
1434 glyph->voffset = it->voffset;
1435 glyph->type = CHAR_GLYPH;
1436 glyph->multibyte_p = it->multibyte_p;
1437 glyph->left_box_line_p = it->start_of_box_run_p;
1438 glyph->right_box_line_p = it->end_of_box_run_p;
1439 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1440 || it->phys_descent > it->descent);
1441 glyph->padding_p = 0;
1442 glyph->glyph_not_available_p = it->glyph_not_available_p;
1443 glyph->face_id = it->face_id;
1444 glyph->u.ch = it->char_to_display;
1445 ++it->glyph_row->used[area];
1446 }
1447 }
1448
1449 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1450 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1451
1452 static INLINE void
1453 x_append_composite_glyph (it)
1454 struct it *it;
1455 {
1456 struct glyph *glyph;
1457 enum glyph_row_area area = it->area;
1458
1459 xassert (it->glyph_row);
1460
1461 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1462 if (glyph < it->glyph_row->glyphs[area + 1])
1463 {
1464 glyph->charpos = CHARPOS (it->position);
1465 glyph->object = it->object;
1466 glyph->pixel_width = it->pixel_width;
1467 glyph->voffset = it->voffset;
1468 glyph->type = COMPOSITE_GLYPH;
1469 glyph->multibyte_p = it->multibyte_p;
1470 glyph->left_box_line_p = it->start_of_box_run_p;
1471 glyph->right_box_line_p = it->end_of_box_run_p;
1472 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1473 || it->phys_descent > it->descent);
1474 glyph->padding_p = 0;
1475 glyph->glyph_not_available_p = 0;
1476 glyph->face_id = it->face_id;
1477 glyph->u.cmp_id = it->cmp_id;
1478 ++it->glyph_row->used[area];
1479 }
1480 }
1481
1482
1483 /* Change IT->ascent and IT->height according to the setting of
1484 IT->voffset. */
1485
1486 static INLINE void
1487 take_vertical_position_into_account (it)
1488 struct it *it;
1489 {
1490 if (it->voffset)
1491 {
1492 if (it->voffset < 0)
1493 /* Increase the ascent so that we can display the text higher
1494 in the line. */
1495 it->ascent += abs (it->voffset);
1496 else
1497 /* Increase the descent so that we can display the text lower
1498 in the line. */
1499 it->descent += it->voffset;
1500 }
1501 }
1502
1503
1504 /* Produce glyphs/get display metrics for the image IT is loaded with.
1505 See the description of struct display_iterator in dispextern.h for
1506 an overview of struct display_iterator. */
1507
1508 static void
1509 x_produce_image_glyph (it)
1510 struct it *it;
1511 {
1512 struct image *img;
1513 struct face *face;
1514
1515 xassert (it->what == IT_IMAGE);
1516
1517 face = FACE_FROM_ID (it->f, it->face_id);
1518 img = IMAGE_FROM_ID (it->f, it->image_id);
1519 xassert (img);
1520
1521 /* Make sure X resources of the face and image are loaded. */
1522 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1523 prepare_image_for_display (it->f, img);
1524
1525 it->ascent = it->phys_ascent = image_ascent (img, face);
1526 it->descent = it->phys_descent = img->height + 2 * img->margin - it->ascent;
1527 it->pixel_width = img->width + 2 * img->margin;
1528
1529 it->nglyphs = 1;
1530
1531 if (face->box != FACE_NO_BOX)
1532 {
1533 it->ascent += face->box_line_width;
1534 it->descent += face->box_line_width;
1535
1536 if (it->start_of_box_run_p)
1537 it->pixel_width += face->box_line_width;
1538 if (it->end_of_box_run_p)
1539 it->pixel_width += 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 it->ascent += face->box_line_width;
1745 it->descent += face->box_line_width;
1746
1747 if (it->start_of_box_run_p)
1748 it->pixel_width += face->box_line_width;
1749 if (it->end_of_box_run_p)
1750 it->pixel_width += face->box_line_width;
1751 }
1752
1753 take_vertical_position_into_account (it);
1754 }
1755
1756 /* Return proper value to be used as baseline offset of font that has
1757 ASCENT and DESCENT to draw characters by the font at the vertical
1758 center of the line of frame F.
1759
1760 Here, out task is to find the value of BOFF in the following figure;
1761
1762 -------------------------+-----------+-
1763 -+-+---------+-+ | |
1764 | | | | | |
1765 | | | | F_ASCENT F_HEIGHT
1766 | | | ASCENT | |
1767 HEIGHT | | | | |
1768 | | |-|-+------+-----------|------- baseline
1769 | | | | BOFF | |
1770 | |---------|-+-+ | |
1771 | | | DESCENT | |
1772 -+-+---------+-+ F_DESCENT |
1773 -------------------------+-----------+-
1774
1775 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1776 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1777 DESCENT = FONT->descent
1778 HEIGHT = FONT_HEIGHT (FONT)
1779 F_DESCENT = (F->output_data.x->font->descent
1780 - F->output_data.x->baseline_offset)
1781 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1782 */
1783
1784 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1785 ((FONT)->descent \
1786 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT))) / 2 \
1787 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1788
1789 /* Produce glyphs/get display metrics for the display element IT is
1790 loaded with. See the description of struct display_iterator in
1791 dispextern.h for an overview of struct display_iterator. */
1792
1793 static void
1794 x_produce_glyphs (it)
1795 struct it *it;
1796 {
1797 it->glyph_not_available_p = 0;
1798
1799 if (it->what == IT_CHARACTER)
1800 {
1801 XChar2b char2b;
1802 XFontStruct *font;
1803 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1804 XCharStruct *pcm;
1805 int font_not_found_p;
1806 struct font_info *font_info;
1807 int boff; /* baseline offset */
1808
1809 /* Maybe translate single-byte characters to multibyte, or the
1810 other way. */
1811 it->char_to_display = it->c;
1812 if (!ASCII_BYTE_P (it->c))
1813 {
1814 if (unibyte_display_via_language_environment
1815 && SINGLE_BYTE_CHAR_P (it->c)
1816 && (it->c >= 0240
1817 || !NILP (Vnonascii_translation_table)))
1818 {
1819 it->char_to_display = unibyte_char_to_multibyte (it->c);
1820 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1821 face = FACE_FROM_ID (it->f, it->face_id);
1822 }
1823 else if (!SINGLE_BYTE_CHAR_P (it->c)
1824 && !it->multibyte_p)
1825 {
1826 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
1827 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1828 face = FACE_FROM_ID (it->f, it->face_id);
1829 }
1830 }
1831
1832 /* Get font to use. Encode IT->char_to_display. */
1833 x_get_char_face_and_encoding (it->f, it->char_to_display,
1834 it->face_id, &char2b,
1835 it->multibyte_p);
1836 font = face->font;
1837
1838 /* When no suitable font found, use the default font. */
1839 font_not_found_p = font == NULL;
1840 if (font_not_found_p)
1841 {
1842 font = FRAME_FONT (it->f);
1843 boff = it->f->output_data.x->baseline_offset;
1844 font_info = NULL;
1845 }
1846 else
1847 {
1848 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1849 boff = font_info->baseline_offset;
1850 if (font_info->vertical_centering)
1851 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1852 }
1853
1854 if (it->char_to_display >= ' '
1855 && (!it->multibyte_p || it->char_to_display < 128))
1856 {
1857 /* Either unibyte or ASCII. */
1858 int stretched_p;
1859
1860 it->nglyphs = 1;
1861
1862 pcm = x_per_char_metric (font, &char2b);
1863 it->ascent = font->ascent + boff;
1864 it->descent = font->descent - boff;
1865
1866 if (pcm)
1867 {
1868 it->phys_ascent = pcm->ascent + boff;
1869 it->phys_descent = pcm->descent - boff;
1870 it->pixel_width = pcm->width;
1871 }
1872 else
1873 {
1874 it->glyph_not_available_p = 1;
1875 it->phys_ascent = font->ascent + boff;
1876 it->phys_descent = font->descent - boff;
1877 it->pixel_width = FONT_WIDTH (font);
1878 }
1879
1880 /* If this is a space inside a region of text with
1881 `space-width' property, change its width. */
1882 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1883 if (stretched_p)
1884 it->pixel_width *= XFLOATINT (it->space_width);
1885
1886 /* If face has a box, add the box thickness to the character
1887 height. If character has a box line to the left and/or
1888 right, add the box line width to the character's width. */
1889 if (face->box != FACE_NO_BOX)
1890 {
1891 int thick = face->box_line_width;
1892
1893 it->ascent += thick;
1894 it->descent += thick;
1895
1896 if (it->start_of_box_run_p)
1897 it->pixel_width += thick;
1898 if (it->end_of_box_run_p)
1899 it->pixel_width += thick;
1900 }
1901
1902 /* If face has an overline, add the height of the overline
1903 (1 pixel) and a 1 pixel margin to the character height. */
1904 if (face->overline_p)
1905 it->ascent += 2;
1906
1907 take_vertical_position_into_account (it);
1908
1909 /* If we have to actually produce glyphs, do it. */
1910 if (it->glyph_row)
1911 {
1912 if (stretched_p)
1913 {
1914 /* Translate a space with a `space-width' property
1915 into a stretch glyph. */
1916 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1917 x_append_stretch_glyph (it, it->object, it->pixel_width,
1918 it->ascent + it->descent, ascent);
1919 }
1920 else
1921 x_append_glyph (it);
1922
1923 /* If characters with lbearing or rbearing are displayed
1924 in this line, record that fact in a flag of the
1925 glyph row. This is used to optimize X output code. */
1926 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1927 it->glyph_row->contains_overlapping_glyphs_p = 1;
1928 }
1929 }
1930 else if (it->char_to_display == '\n')
1931 {
1932 /* A newline has no width but we need the height of the line. */
1933 it->pixel_width = 0;
1934 it->nglyphs = 0;
1935 it->ascent = it->phys_ascent = font->ascent + boff;
1936 it->descent = it->phys_descent = font->descent - boff;
1937
1938 if (face->box != FACE_NO_BOX)
1939 {
1940 int thick = face->box_line_width;
1941 it->ascent += thick;
1942 it->descent += thick;
1943 }
1944 }
1945 else if (it->char_to_display == '\t')
1946 {
1947 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1948 int x = it->current_x + it->continuation_lines_width;
1949 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1950
1951 it->pixel_width = next_tab_x - x;
1952 it->nglyphs = 1;
1953 it->ascent = it->phys_ascent = font->ascent + boff;
1954 it->descent = it->phys_descent = font->descent - boff;
1955
1956 if (it->glyph_row)
1957 {
1958 double ascent = (double) it->ascent / (it->ascent + it->descent);
1959 x_append_stretch_glyph (it, it->object, it->pixel_width,
1960 it->ascent + it->descent, ascent);
1961 }
1962 }
1963 else
1964 {
1965 /* A multi-byte character. Assume that the display width of the
1966 character is the width of the character multiplied by the
1967 width of the font. */
1968
1969 /* If we found a font, this font should give us the right
1970 metrics. If we didn't find a font, use the frame's
1971 default font and calculate the width of the character
1972 from the charset width; this is what old redisplay code
1973 did. */
1974 pcm = x_per_char_metric (font, &char2b);
1975 if (font_not_found_p || !pcm)
1976 {
1977 int charset = CHAR_CHARSET (it->char_to_display);
1978
1979 it->glyph_not_available_p = 1;
1980 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1981 * CHARSET_WIDTH (charset));
1982 it->phys_ascent = font->ascent + boff;
1983 it->phys_descent = font->descent - boff;
1984 }
1985 else
1986 {
1987 it->pixel_width = pcm->width;
1988 it->phys_ascent = pcm->ascent + boff;
1989 it->phys_descent = pcm->descent - boff;
1990 if (it->glyph_row
1991 && (pcm->lbearing < 0
1992 || pcm->rbearing > pcm->width))
1993 it->glyph_row->contains_overlapping_glyphs_p = 1;
1994 }
1995 it->nglyphs = 1;
1996 it->ascent = font->ascent + boff;
1997 it->descent = font->descent - boff;
1998 if (face->box != FACE_NO_BOX)
1999 {
2000 int thick = face->box_line_width;
2001 it->ascent += thick;
2002 it->descent += thick;
2003
2004 if (it->start_of_box_run_p)
2005 it->pixel_width += thick;
2006 if (it->end_of_box_run_p)
2007 it->pixel_width += thick;
2008 }
2009
2010 /* If face has an overline, add the height of the overline
2011 (1 pixel) and a 1 pixel margin to the character height. */
2012 if (face->overline_p)
2013 it->ascent += 2;
2014
2015 take_vertical_position_into_account (it);
2016
2017 if (it->glyph_row)
2018 x_append_glyph (it);
2019 }
2020 }
2021 else if (it->what == IT_COMPOSITION)
2022 {
2023 /* Note: A composition is represented as one glyph in the
2024 glyph matrix. There are no padding glyphs. */
2025 XChar2b char2b;
2026 XFontStruct *font;
2027 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2028 XCharStruct *pcm;
2029 int font_not_found_p;
2030 struct font_info *font_info;
2031 int boff; /* baseline offset */
2032 struct composition *cmp = composition_table[it->cmp_id];
2033
2034 /* Maybe translate single-byte characters to multibyte. */
2035 it->char_to_display = it->c;
2036 if (unibyte_display_via_language_environment
2037 && SINGLE_BYTE_CHAR_P (it->c)
2038 && (it->c >= 0240
2039 || (it->c >= 0200
2040 && !NILP (Vnonascii_translation_table))))
2041 {
2042 it->char_to_display = unibyte_char_to_multibyte (it->c);
2043 }
2044
2045 /* Get face and font to use. Encode IT->char_to_display. */
2046 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2047 face = FACE_FROM_ID (it->f, it->face_id);
2048 x_get_char_face_and_encoding (it->f, it->char_to_display,
2049 it->face_id, &char2b, it->multibyte_p);
2050 font = face->font;
2051
2052 /* When no suitable font found, use the default font. */
2053 font_not_found_p = font == NULL;
2054 if (font_not_found_p)
2055 {
2056 font = FRAME_FONT (it->f);
2057 boff = it->f->output_data.x->baseline_offset;
2058 font_info = NULL;
2059 }
2060 else
2061 {
2062 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2063 boff = font_info->baseline_offset;
2064 if (font_info->vertical_centering)
2065 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2066 }
2067
2068 /* There are no padding glyphs, so there is only one glyph to
2069 produce for the composition. Important is that pixel_width,
2070 ascent and descent are the values of what is drawn by
2071 draw_glyphs (i.e. the values of the overall glyphs composed). */
2072 it->nglyphs = 1;
2073
2074 /* If we have not yet calculated pixel size data of glyphs of
2075 the composition for the current face font, calculate them
2076 now. Theoretically, we have to check all fonts for the
2077 glyphs, but that requires much time and memory space. So,
2078 here we check only the font of the first glyph. This leads
2079 to incorrect display very rarely, and C-l (recenter) can
2080 correct the display anyway. */
2081 if (cmp->font != (void *) font)
2082 {
2083 /* Ascent and descent of the font of the first character of
2084 this composition (adjusted by baseline offset). Ascent
2085 and descent of overall glyphs should not be less than
2086 them respectively. */
2087 int font_ascent = font->ascent + boff;
2088 int font_descent = font->descent - boff;
2089 /* Bounding box of the overall glyphs. */
2090 int leftmost, rightmost, lowest, highest;
2091 int i, width, ascent, descent;
2092
2093 cmp->font = (void *) font;
2094
2095 /* Initialize the bounding box. */
2096 pcm = x_per_char_metric (font, &char2b);
2097 if (pcm)
2098 {
2099 width = pcm->width;
2100 ascent = pcm->ascent;
2101 descent = pcm->descent;
2102 }
2103 else
2104 {
2105 width = FONT_WIDTH (font);
2106 ascent = font->ascent;
2107 descent = font->descent;
2108 }
2109
2110 rightmost = width;
2111 lowest = - descent + boff;
2112 highest = ascent + boff;
2113 leftmost = 0;
2114
2115 if (font_info
2116 && font_info->default_ascent
2117 && CHAR_TABLE_P (Vuse_default_ascent)
2118 && !NILP (Faref (Vuse_default_ascent,
2119 make_number (it->char_to_display))))
2120 highest = font_info->default_ascent + boff;
2121
2122 /* Draw the first glyph at the normal position. It may be
2123 shifted to right later if some other glyphs are drawn at
2124 the left. */
2125 cmp->offsets[0] = 0;
2126 cmp->offsets[1] = boff;
2127
2128 /* Set cmp->offsets for the remaining glyphs. */
2129 for (i = 1; i < cmp->glyph_len; i++)
2130 {
2131 int left, right, btm, top;
2132 int ch = COMPOSITION_GLYPH (cmp, i);
2133 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2134
2135 face = FACE_FROM_ID (it->f, face_id);
2136 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2137 it->multibyte_p);
2138 font = face->font;
2139 if (font == NULL)
2140 {
2141 font = FRAME_FONT (it->f);
2142 boff = it->f->output_data.x->baseline_offset;
2143 font_info = NULL;
2144 }
2145 else
2146 {
2147 font_info
2148 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2149 boff = font_info->baseline_offset;
2150 if (font_info->vertical_centering)
2151 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2152 }
2153
2154 pcm = x_per_char_metric (font, &char2b);
2155 if (pcm)
2156 {
2157 width = pcm->width;
2158 ascent = pcm->ascent;
2159 descent = pcm->descent;
2160 }
2161 else
2162 {
2163 width = FONT_WIDTH (font);
2164 ascent = font->ascent;
2165 descent = font->descent;
2166 }
2167
2168 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2169 {
2170 /* Relative composition with or without
2171 alternate chars. */
2172 left = (leftmost + rightmost - width) / 2;
2173 btm = - descent + boff;
2174 if (font_info && font_info->relative_compose
2175 && (! CHAR_TABLE_P (Vignore_relative_composition)
2176 || NILP (Faref (Vignore_relative_composition,
2177 make_number (ch)))))
2178 {
2179
2180 if (- descent >= font_info->relative_compose)
2181 /* One extra pixel between two glyphs. */
2182 btm = highest + 1;
2183 else if (ascent <= 0)
2184 /* One extra pixel between two glyphs. */
2185 btm = lowest - 1 - ascent - descent;
2186 }
2187 }
2188 else
2189 {
2190 /* A composition rule is specified by an integer
2191 value that encodes global and new reference
2192 points (GREF and NREF). GREF and NREF are
2193 specified by numbers as below:
2194
2195 0---1---2 -- ascent
2196 | |
2197 | |
2198 | |
2199 9--10--11 -- center
2200 | |
2201 ---3---4---5--- baseline
2202 | |
2203 6---7---8 -- descent
2204 */
2205 int rule = COMPOSITION_RULE (cmp, i);
2206 int gref, nref, grefx, grefy, nrefx, nrefy;
2207
2208 COMPOSITION_DECODE_RULE (rule, gref, nref);
2209 grefx = gref % 3, nrefx = nref % 3;
2210 grefy = gref / 3, nrefy = nref / 3;
2211
2212 left = (leftmost
2213 + grefx * (rightmost - leftmost) / 2
2214 - nrefx * width / 2);
2215 btm = ((grefy == 0 ? highest
2216 : grefy == 1 ? 0
2217 : grefy == 2 ? lowest
2218 : (highest + lowest) / 2)
2219 - (nrefy == 0 ? ascent + descent
2220 : nrefy == 1 ? descent - boff
2221 : nrefy == 2 ? 0
2222 : (ascent + descent) / 2));
2223 }
2224
2225 cmp->offsets[i * 2] = left;
2226 cmp->offsets[i * 2 + 1] = btm + descent;
2227
2228 /* Update the bounding box of the overall glyphs. */
2229 right = left + width;
2230 top = btm + descent + ascent;
2231 if (left < leftmost)
2232 leftmost = left;
2233 if (right > rightmost)
2234 rightmost = right;
2235 if (top > highest)
2236 highest = top;
2237 if (btm < lowest)
2238 lowest = btm;
2239 }
2240
2241 /* If there are glyphs whose x-offsets are negative,
2242 shift all glyphs to the right and make all x-offsets
2243 non-negative. */
2244 if (leftmost < 0)
2245 {
2246 for (i = 0; i < cmp->glyph_len; i++)
2247 cmp->offsets[i * 2] -= leftmost;
2248 rightmost -= leftmost;
2249 }
2250
2251 cmp->pixel_width = rightmost;
2252 cmp->ascent = highest;
2253 cmp->descent = - lowest;
2254 if (cmp->ascent < font_ascent)
2255 cmp->ascent = font_ascent;
2256 if (cmp->descent < font_descent)
2257 cmp->descent = font_descent;
2258 }
2259
2260 it->pixel_width = cmp->pixel_width;
2261 it->ascent = it->phys_ascent = cmp->ascent;
2262 it->descent = it->phys_descent = cmp->descent;
2263
2264 if (face->box != FACE_NO_BOX)
2265 {
2266 int thick = face->box_line_width;
2267 it->ascent += thick;
2268 it->descent += thick;
2269
2270 if (it->start_of_box_run_p)
2271 it->pixel_width += thick;
2272 if (it->end_of_box_run_p)
2273 it->pixel_width += thick;
2274 }
2275
2276 /* If face has an overline, add the height of the overline
2277 (1 pixel) and a 1 pixel margin to the character height. */
2278 if (face->overline_p)
2279 it->ascent += 2;
2280
2281 take_vertical_position_into_account (it);
2282
2283 if (it->glyph_row)
2284 x_append_composite_glyph (it);
2285 }
2286 else if (it->what == IT_IMAGE)
2287 x_produce_image_glyph (it);
2288 else if (it->what == IT_STRETCH)
2289 x_produce_stretch_glyph (it);
2290
2291 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2292 because this isn't true for images with `:ascent 100'. */
2293 xassert (it->ascent >= 0 && it->descent >= 0);
2294 if (it->area == TEXT_AREA)
2295 it->current_x += it->pixel_width;
2296
2297 it->descent += it->extra_line_spacing;
2298
2299 it->max_ascent = max (it->max_ascent, it->ascent);
2300 it->max_descent = max (it->max_descent, it->descent);
2301 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2302 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2303 }
2304
2305
2306 /* Estimate the pixel height of the mode or top line on frame F.
2307 FACE_ID specifies what line's height to estimate. */
2308
2309 int
2310 x_estimate_mode_line_height (f, face_id)
2311 struct frame *f;
2312 enum face_id face_id;
2313 {
2314 int height = 1;
2315
2316 /* This function is called so early when Emacs starts that the face
2317 cache and mode line face are not yet initialized. */
2318 if (FRAME_FACE_CACHE (f))
2319 {
2320 struct face *face = FACE_FROM_ID (f, face_id);
2321 if (face)
2322 height = FONT_HEIGHT (face->font) + 2 * face->box_line_width;
2323 }
2324
2325 return height;
2326 }
2327
2328 \f
2329 /***********************************************************************
2330 Glyph display
2331 ***********************************************************************/
2332
2333 /* A sequence of glyphs to be drawn in the same face.
2334
2335 This data structure is not really completely X specific, so it
2336 could possibly, at least partially, be useful for other systems. It
2337 is currently not part of the external redisplay interface because
2338 it's not clear what other systems will need. */
2339
2340 struct glyph_string
2341 {
2342 /* X-origin of the string. */
2343 int x;
2344
2345 /* Y-origin and y-position of the base line of this string. */
2346 int y, ybase;
2347
2348 /* The width of the string, not including a face extension. */
2349 int width;
2350
2351 /* The width of the string, including a face extension. */
2352 int background_width;
2353
2354 /* The height of this string. This is the height of the line this
2355 string is drawn in, and can be different from the height of the
2356 font the string is drawn in. */
2357 int height;
2358
2359 /* Number of pixels this string overwrites in front of its x-origin.
2360 This number is zero if the string has an lbearing >= 0; it is
2361 -lbearing, if the string has an lbearing < 0. */
2362 int left_overhang;
2363
2364 /* Number of pixels this string overwrites past its right-most
2365 nominal x-position, i.e. x + width. Zero if the string's
2366 rbearing is <= its nominal width, rbearing - width otherwise. */
2367 int right_overhang;
2368
2369 /* The frame on which the glyph string is drawn. */
2370 struct frame *f;
2371
2372 /* The window on which the glyph string is drawn. */
2373 struct window *w;
2374
2375 /* X display and window for convenience. */
2376 Display *display;
2377 Window window;
2378
2379 /* The glyph row for which this string was built. It determines the
2380 y-origin and height of the string. */
2381 struct glyph_row *row;
2382
2383 /* The area within row. */
2384 enum glyph_row_area area;
2385
2386 /* Characters to be drawn, and number of characters. */
2387 XChar2b *char2b;
2388 int nchars;
2389
2390 /* A face-override for drawing cursors, mouse face and similar. */
2391 enum draw_glyphs_face hl;
2392
2393 /* Face in which this string is to be drawn. */
2394 struct face *face;
2395
2396 /* Font in which this string is to be drawn. */
2397 XFontStruct *font;
2398
2399 /* Font info for this string. */
2400 struct font_info *font_info;
2401
2402 /* Non-null means this string describes (part of) a composition.
2403 All characters from char2b are drawn composed. */
2404 struct composition *cmp;
2405
2406 /* Index of this glyph string's first character in the glyph
2407 definition of CMP. If this is zero, this glyph string describes
2408 the first character of a composition. */
2409 int gidx;
2410
2411 /* 1 means this glyph strings face has to be drawn to the right end
2412 of the window's drawing area. */
2413 unsigned extends_to_end_of_line_p : 1;
2414
2415 /* 1 means the background of this string has been drawn. */
2416 unsigned background_filled_p : 1;
2417
2418 /* 1 means glyph string must be drawn with 16-bit functions. */
2419 unsigned two_byte_p : 1;
2420
2421 /* 1 means that the original font determined for drawing this glyph
2422 string could not be loaded. The member `font' has been set to
2423 the frame's default font in this case. */
2424 unsigned font_not_found_p : 1;
2425
2426 /* 1 means that the face in which this glyph string is drawn has a
2427 stipple pattern. */
2428 unsigned stippled_p : 1;
2429
2430 /* 1 means only the foreground of this glyph string must be drawn,
2431 and we should use the physical height of the line this glyph
2432 string appears in as clip rect. */
2433 unsigned for_overlaps_p : 1;
2434
2435 /* The GC to use for drawing this glyph string. */
2436 GC gc;
2437
2438 /* A pointer to the first glyph in the string. This glyph
2439 corresponds to char2b[0]. Needed to draw rectangles if
2440 font_not_found_p is 1. */
2441 struct glyph *first_glyph;
2442
2443 /* Image, if any. */
2444 struct image *img;
2445
2446 struct glyph_string *next, *prev;
2447 };
2448
2449
2450 #if 0
2451
2452 static void
2453 x_dump_glyph_string (s)
2454 struct glyph_string *s;
2455 {
2456 fprintf (stderr, "glyph string\n");
2457 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2458 s->x, s->y, s->width, s->height);
2459 fprintf (stderr, " ybase = %d\n", s->ybase);
2460 fprintf (stderr, " hl = %d\n", s->hl);
2461 fprintf (stderr, " left overhang = %d, right = %d\n",
2462 s->left_overhang, s->right_overhang);
2463 fprintf (stderr, " nchars = %d\n", s->nchars);
2464 fprintf (stderr, " extends to end of line = %d\n",
2465 s->extends_to_end_of_line_p);
2466 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2467 fprintf (stderr, " bg width = %d\n", s->background_width);
2468 }
2469
2470 #endif /* GLYPH_DEBUG */
2471
2472
2473
2474 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2475 struct glyph_string **,
2476 struct glyph_string *,
2477 struct glyph_string *));
2478 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2479 struct glyph_string **,
2480 struct glyph_string *,
2481 struct glyph_string *));
2482 static void x_append_glyph_string P_ ((struct glyph_string **,
2483 struct glyph_string **,
2484 struct glyph_string *));
2485 static int x_left_overwritten P_ ((struct glyph_string *));
2486 static int x_left_overwriting P_ ((struct glyph_string *));
2487 static int x_right_overwritten P_ ((struct glyph_string *));
2488 static int x_right_overwriting P_ ((struct glyph_string *));
2489 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2490 int));
2491 static void x_init_glyph_string P_ ((struct glyph_string *,
2492 XChar2b *, struct window *,
2493 struct glyph_row *,
2494 enum glyph_row_area, int,
2495 enum draw_glyphs_face));
2496 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2497 enum glyph_row_area, int, int,
2498 enum draw_glyphs_face, int *, int *, int));
2499 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2500 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2501 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2502 int));
2503 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2504 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2505 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2506 static void x_draw_glyph_string P_ ((struct glyph_string *));
2507 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2508 static void x_set_cursor_gc P_ ((struct glyph_string *));
2509 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2510 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2511 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2512 int *, int *));
2513 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2514 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2515 unsigned long *, double, int));
2516 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2517 double, int, unsigned long));
2518 static void x_setup_relief_colors P_ ((struct glyph_string *));
2519 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2520 static void x_draw_image_relief P_ ((struct glyph_string *));
2521 static void x_draw_image_foreground P_ ((struct glyph_string *));
2522 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2523 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2524 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2525 int, int, int));
2526 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2527 int, int, int, int, XRectangle *));
2528 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2529 int, int, int, XRectangle *));
2530 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2531 enum glyph_row_area));
2532 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2533 struct glyph_row *,
2534 enum glyph_row_area, int, int));
2535
2536 #if GLYPH_DEBUG
2537 static void x_check_font P_ ((struct frame *, XFontStruct *));
2538 #endif
2539
2540
2541 /* Append the list of glyph strings with head H and tail T to the list
2542 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2543
2544 static INLINE void
2545 x_append_glyph_string_lists (head, tail, h, t)
2546 struct glyph_string **head, **tail;
2547 struct glyph_string *h, *t;
2548 {
2549 if (h)
2550 {
2551 if (*head)
2552 (*tail)->next = h;
2553 else
2554 *head = h;
2555 h->prev = *tail;
2556 *tail = t;
2557 }
2558 }
2559
2560
2561 /* Prepend the list of glyph strings with head H and tail T to the
2562 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2563 result. */
2564
2565 static INLINE void
2566 x_prepend_glyph_string_lists (head, tail, h, t)
2567 struct glyph_string **head, **tail;
2568 struct glyph_string *h, *t;
2569 {
2570 if (h)
2571 {
2572 if (*head)
2573 (*head)->prev = t;
2574 else
2575 *tail = t;
2576 t->next = *head;
2577 *head = h;
2578 }
2579 }
2580
2581
2582 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2583 Set *HEAD and *TAIL to the resulting list. */
2584
2585 static INLINE void
2586 x_append_glyph_string (head, tail, s)
2587 struct glyph_string **head, **tail;
2588 struct glyph_string *s;
2589 {
2590 s->next = s->prev = NULL;
2591 x_append_glyph_string_lists (head, tail, s, s);
2592 }
2593
2594
2595 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2596 face. */
2597
2598 static void
2599 x_set_cursor_gc (s)
2600 struct glyph_string *s;
2601 {
2602 if (s->font == FRAME_FONT (s->f)
2603 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2604 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2605 && !s->cmp)
2606 s->gc = s->f->output_data.x->cursor_gc;
2607 else
2608 {
2609 /* Cursor on non-default face: must merge. */
2610 XGCValues xgcv;
2611 unsigned long mask;
2612
2613 xgcv.background = s->f->output_data.x->cursor_pixel;
2614 xgcv.foreground = s->face->background;
2615
2616 /* If the glyph would be invisible, try a different foreground. */
2617 if (xgcv.foreground == xgcv.background)
2618 xgcv.foreground = s->face->foreground;
2619 if (xgcv.foreground == xgcv.background)
2620 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2621 if (xgcv.foreground == xgcv.background)
2622 xgcv.foreground = s->face->foreground;
2623
2624 /* Make sure the cursor is distinct from text in this face. */
2625 if (xgcv.background == s->face->background
2626 && xgcv.foreground == s->face->foreground)
2627 {
2628 xgcv.background = s->face->foreground;
2629 xgcv.foreground = s->face->background;
2630 }
2631
2632 IF_DEBUG (x_check_font (s->f, s->font));
2633 xgcv.font = s->font->fid;
2634 xgcv.graphics_exposures = False;
2635 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2636
2637 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2638 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2639 mask, &xgcv);
2640 else
2641 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2642 = XCreateGC (s->display, s->window, mask, &xgcv);
2643
2644 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2645 }
2646 }
2647
2648
2649 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2650
2651 static void
2652 x_set_mouse_face_gc (s)
2653 struct glyph_string *s;
2654 {
2655 int face_id;
2656 struct face *face;
2657
2658 /* What face has to be used for the mouse face? */
2659 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2660 face = FACE_FROM_ID (s->f, face_id);
2661 if (s->first_glyph->type == CHAR_GLYPH)
2662 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2663 else
2664 face_id = FACE_FOR_CHAR (s->f, face, 0);
2665 s->face = FACE_FROM_ID (s->f, face_id);
2666 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2667
2668 /* If font in this face is same as S->font, use it. */
2669 if (s->font == s->face->font)
2670 s->gc = s->face->gc;
2671 else
2672 {
2673 /* Otherwise construct scratch_cursor_gc with values from FACE
2674 but font FONT. */
2675 XGCValues xgcv;
2676 unsigned long mask;
2677
2678 xgcv.background = s->face->background;
2679 xgcv.foreground = s->face->foreground;
2680 IF_DEBUG (x_check_font (s->f, s->font));
2681 xgcv.font = s->font->fid;
2682 xgcv.graphics_exposures = False;
2683 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2684
2685 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2686 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2687 mask, &xgcv);
2688 else
2689 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2690 = XCreateGC (s->display, s->window, mask, &xgcv);
2691
2692 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2693 }
2694
2695 xassert (s->gc != 0);
2696 }
2697
2698
2699 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2700 Faces to use in the mode line have already been computed when the
2701 matrix was built, so there isn't much to do, here. */
2702
2703 static INLINE void
2704 x_set_mode_line_face_gc (s)
2705 struct glyph_string *s;
2706 {
2707 s->gc = s->face->gc;
2708 }
2709
2710
2711 /* Set S->gc of glyph string S for drawing that glyph string. Set
2712 S->stippled_p to a non-zero value if the face of S has a stipple
2713 pattern. */
2714
2715 static INLINE void
2716 x_set_glyph_string_gc (s)
2717 struct glyph_string *s;
2718 {
2719 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2720
2721 if (s->hl == DRAW_NORMAL_TEXT)
2722 {
2723 s->gc = s->face->gc;
2724 s->stippled_p = s->face->stipple != 0;
2725 }
2726 else if (s->hl == DRAW_INVERSE_VIDEO)
2727 {
2728 x_set_mode_line_face_gc (s);
2729 s->stippled_p = s->face->stipple != 0;
2730 }
2731 else if (s->hl == DRAW_CURSOR)
2732 {
2733 x_set_cursor_gc (s);
2734 s->stippled_p = 0;
2735 }
2736 else if (s->hl == DRAW_MOUSE_FACE)
2737 {
2738 x_set_mouse_face_gc (s);
2739 s->stippled_p = s->face->stipple != 0;
2740 }
2741 else if (s->hl == DRAW_IMAGE_RAISED
2742 || s->hl == DRAW_IMAGE_SUNKEN)
2743 {
2744 s->gc = s->face->gc;
2745 s->stippled_p = s->face->stipple != 0;
2746 }
2747 else
2748 {
2749 s->gc = s->face->gc;
2750 s->stippled_p = s->face->stipple != 0;
2751 }
2752
2753 /* GC must have been set. */
2754 xassert (s->gc != 0);
2755 }
2756
2757
2758 /* Return in *R the clipping rectangle for glyph string S. */
2759
2760 static void
2761 x_get_glyph_string_clip_rect (s, r)
2762 struct glyph_string *s;
2763 XRectangle *r;
2764 {
2765 if (s->row->full_width_p)
2766 {
2767 /* Draw full-width. X coordinates are relative to S->w->left. */
2768 int canon_x = CANON_X_UNIT (s->f);
2769
2770 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2771 r->width = XFASTINT (s->w->width) * canon_x;
2772
2773 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2774 {
2775 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2776 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2777 r->x -= width;
2778 }
2779
2780 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2781
2782 /* Unless displaying a mode or menu bar line, which are always
2783 fully visible, clip to the visible part of the row. */
2784 if (s->w->pseudo_window_p)
2785 r->height = s->row->visible_height;
2786 else
2787 r->height = s->height;
2788 }
2789 else
2790 {
2791 /* This is a text line that may be partially visible. */
2792 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2793 r->width = window_box_width (s->w, s->area);
2794 r->height = s->row->visible_height;
2795 }
2796
2797 /* Don't use S->y for clipping because it doesn't take partially
2798 visible lines into account. For example, it can be negative for
2799 partially visible lines at the top of a window. */
2800 if (!s->row->full_width_p
2801 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2802 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2803 else
2804 r->y = max (0, s->row->y);
2805
2806 /* If drawing a tool-bar window, draw it over the internal border
2807 at the top of the window. */
2808 if (s->w == XWINDOW (s->f->tool_bar_window))
2809 r->y -= s->f->output_data.x->internal_border_width;
2810
2811 /* If S draws overlapping rows, it's sufficient to use the top and
2812 bottom of the window for clipping because this glyph string
2813 intentionally draws over other lines. */
2814 if (s->for_overlaps_p)
2815 {
2816 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2817 r->height = window_text_bottom_y (s->w) - r->y;
2818 }
2819
2820 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2821 }
2822
2823
2824 /* Set clipping for output of glyph string S. S may be part of a mode
2825 line or menu if we don't have X toolkit support. */
2826
2827 static INLINE void
2828 x_set_glyph_string_clipping (s)
2829 struct glyph_string *s;
2830 {
2831 XRectangle r;
2832 x_get_glyph_string_clip_rect (s, &r);
2833 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2834 }
2835
2836
2837 /* Compute left and right overhang of glyph string S. If S is a glyph
2838 string for a composition, assume overhangs don't exist. */
2839
2840 static INLINE void
2841 x_compute_glyph_string_overhangs (s)
2842 struct glyph_string *s;
2843 {
2844 if (s->cmp == NULL
2845 && s->first_glyph->type == CHAR_GLYPH)
2846 {
2847 XCharStruct cs;
2848 int direction, font_ascent, font_descent;
2849 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2850 &font_ascent, &font_descent, &cs);
2851 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2852 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2853 }
2854 }
2855
2856
2857 /* Compute overhangs and x-positions for glyph string S and its
2858 predecessors, or successors. X is the starting x-position for S.
2859 BACKWARD_P non-zero means process predecessors. */
2860
2861 static void
2862 x_compute_overhangs_and_x (s, x, backward_p)
2863 struct glyph_string *s;
2864 int x;
2865 int backward_p;
2866 {
2867 if (backward_p)
2868 {
2869 while (s)
2870 {
2871 x_compute_glyph_string_overhangs (s);
2872 x -= s->width;
2873 s->x = x;
2874 s = s->prev;
2875 }
2876 }
2877 else
2878 {
2879 while (s)
2880 {
2881 x_compute_glyph_string_overhangs (s);
2882 s->x = x;
2883 x += s->width;
2884 s = s->next;
2885 }
2886 }
2887 }
2888
2889
2890 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2891 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2892 assumed to be zero. */
2893
2894 static void
2895 x_get_glyph_overhangs (glyph, f, left, right)
2896 struct glyph *glyph;
2897 struct frame *f;
2898 int *left, *right;
2899 {
2900 *left = *right = 0;
2901
2902 if (glyph->type == CHAR_GLYPH)
2903 {
2904 XFontStruct *font;
2905 struct face *face;
2906 struct font_info *font_info;
2907 XChar2b char2b;
2908 XCharStruct *pcm;
2909
2910 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2911 font = face->font;
2912 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2913 if (font
2914 && (pcm = x_per_char_metric (font, &char2b)))
2915 {
2916 if (pcm->rbearing > pcm->width)
2917 *right = pcm->rbearing - pcm->width;
2918 if (pcm->lbearing < 0)
2919 *left = -pcm->lbearing;
2920 }
2921 }
2922 }
2923
2924
2925 /* Return the index of the first glyph preceding glyph string S that
2926 is overwritten by S because of S's left overhang. Value is -1
2927 if no glyphs are overwritten. */
2928
2929 static int
2930 x_left_overwritten (s)
2931 struct glyph_string *s;
2932 {
2933 int k;
2934
2935 if (s->left_overhang)
2936 {
2937 int x = 0, i;
2938 struct glyph *glyphs = s->row->glyphs[s->area];
2939 int first = s->first_glyph - glyphs;
2940
2941 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2942 x -= glyphs[i].pixel_width;
2943
2944 k = i + 1;
2945 }
2946 else
2947 k = -1;
2948
2949 return k;
2950 }
2951
2952
2953 /* Return the index of the first glyph preceding glyph string S that
2954 is overwriting S because of its right overhang. Value is -1 if no
2955 glyph in front of S overwrites S. */
2956
2957 static int
2958 x_left_overwriting (s)
2959 struct glyph_string *s;
2960 {
2961 int i, k, x;
2962 struct glyph *glyphs = s->row->glyphs[s->area];
2963 int first = s->first_glyph - glyphs;
2964
2965 k = -1;
2966 x = 0;
2967 for (i = first - 1; i >= 0; --i)
2968 {
2969 int left, right;
2970 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2971 if (x + right > 0)
2972 k = i;
2973 x -= glyphs[i].pixel_width;
2974 }
2975
2976 return k;
2977 }
2978
2979
2980 /* Return the index of the last glyph following glyph string S that is
2981 not overwritten by S because of S's right overhang. Value is -1 if
2982 no such glyph is found. */
2983
2984 static int
2985 x_right_overwritten (s)
2986 struct glyph_string *s;
2987 {
2988 int k = -1;
2989
2990 if (s->right_overhang)
2991 {
2992 int x = 0, i;
2993 struct glyph *glyphs = s->row->glyphs[s->area];
2994 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
2995 int end = s->row->used[s->area];
2996
2997 for (i = first; i < end && s->right_overhang > x; ++i)
2998 x += glyphs[i].pixel_width;
2999
3000 k = i;
3001 }
3002
3003 return k;
3004 }
3005
3006
3007 /* Return the index of the last glyph following glyph string S that
3008 overwrites S because of its left overhang. Value is negative
3009 if no such glyph is found. */
3010
3011 static int
3012 x_right_overwriting (s)
3013 struct glyph_string *s;
3014 {
3015 int i, k, x;
3016 int end = s->row->used[s->area];
3017 struct glyph *glyphs = s->row->glyphs[s->area];
3018 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3019
3020 k = -1;
3021 x = 0;
3022 for (i = first; i < end; ++i)
3023 {
3024 int left, right;
3025 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3026 if (x - left < 0)
3027 k = i;
3028 x += glyphs[i].pixel_width;
3029 }
3030
3031 return k;
3032 }
3033
3034
3035 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3036
3037 static INLINE void
3038 x_clear_glyph_string_rect (s, x, y, w, h)
3039 struct glyph_string *s;
3040 int x, y, w, h;
3041 {
3042 XGCValues xgcv;
3043 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3044 XSetForeground (s->display, s->gc, xgcv.background);
3045 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3046 XSetForeground (s->display, s->gc, xgcv.foreground);
3047 }
3048
3049
3050 /* Draw the background of glyph_string S. If S->background_filled_p
3051 is non-zero don't draw it. FORCE_P non-zero means draw the
3052 background even if it wouldn't be drawn normally. This is used
3053 when a string preceding S draws into the background of S, or S
3054 contains the first component of a composition. */
3055
3056 static void
3057 x_draw_glyph_string_background (s, force_p)
3058 struct glyph_string *s;
3059 int force_p;
3060 {
3061 /* Nothing to do if background has already been drawn or if it
3062 shouldn't be drawn in the first place. */
3063 if (!s->background_filled_p)
3064 {
3065 if (s->stippled_p)
3066 {
3067 /* Fill background with a stipple pattern. */
3068 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3069 XFillRectangle (s->display, s->window, s->gc, s->x,
3070 s->y + s->face->box_line_width,
3071 s->background_width,
3072 s->height - 2 * s->face->box_line_width);
3073 XSetFillStyle (s->display, s->gc, FillSolid);
3074 s->background_filled_p = 1;
3075 }
3076 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
3077 || s->font_not_found_p
3078 || s->extends_to_end_of_line_p
3079 || force_p)
3080 {
3081 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
3082 s->background_width,
3083 s->height - 2 * s->face->box_line_width);
3084 s->background_filled_p = 1;
3085 }
3086 }
3087 }
3088
3089
3090 /* Draw the foreground of glyph string S. */
3091
3092 static void
3093 x_draw_glyph_string_foreground (s)
3094 struct glyph_string *s;
3095 {
3096 int i, x;
3097
3098 /* If first glyph of S has a left box line, start drawing the text
3099 of S to the right of that box line. */
3100 if (s->face->box != FACE_NO_BOX
3101 && s->first_glyph->left_box_line_p)
3102 x = s->x + s->face->box_line_width;
3103 else
3104 x = s->x;
3105
3106 /* Draw characters of S as rectangles if S's font could not be
3107 loaded. */
3108 if (s->font_not_found_p)
3109 {
3110 for (i = 0; i < s->nchars; ++i)
3111 {
3112 struct glyph *g = s->first_glyph + i;
3113 XDrawRectangle (s->display, s->window,
3114 s->gc, x, s->y, g->pixel_width - 1,
3115 s->height - 1);
3116 x += g->pixel_width;
3117 }
3118 }
3119 else
3120 {
3121 char *char1b = (char *) s->char2b;
3122 int boff = s->font_info->baseline_offset;
3123
3124 if (s->font_info->vertical_centering)
3125 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3126
3127 /* If we can use 8-bit functions, condense S->char2b. */
3128 if (!s->two_byte_p)
3129 for (i = 0; i < s->nchars; ++i)
3130 char1b[i] = s->char2b[i].byte2;
3131
3132 /* Draw text with XDrawString if background has already been
3133 filled. Otherwise, use XDrawImageString. (Note that
3134 XDrawImageString is usually faster than XDrawString.) Always
3135 use XDrawImageString when drawing the cursor so that there is
3136 no chance that characters under a box cursor are invisible. */
3137 if (s->for_overlaps_p
3138 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3139 {
3140 /* Draw characters with 16-bit or 8-bit functions. */
3141 if (s->two_byte_p)
3142 XDrawString16 (s->display, s->window, s->gc, x,
3143 s->ybase - boff, s->char2b, s->nchars);
3144 else
3145 XDrawString (s->display, s->window, s->gc, x,
3146 s->ybase - boff, char1b, s->nchars);
3147 }
3148 else
3149 {
3150 if (s->two_byte_p)
3151 XDrawImageString16 (s->display, s->window, s->gc, x,
3152 s->ybase - boff, s->char2b, s->nchars);
3153 else
3154 XDrawImageString (s->display, s->window, s->gc, x,
3155 s->ybase - boff, char1b, s->nchars);
3156 }
3157 }
3158 }
3159
3160 /* Draw the foreground of composite glyph string S. */
3161
3162 static void
3163 x_draw_composite_glyph_string_foreground (s)
3164 struct glyph_string *s;
3165 {
3166 int i, x;
3167
3168 /* If first glyph of S has a left box line, start drawing the text
3169 of S to the right of that box line. */
3170 if (s->face->box != FACE_NO_BOX
3171 && s->first_glyph->left_box_line_p)
3172 x = s->x + s->face->box_line_width;
3173 else
3174 x = s->x;
3175
3176 /* S is a glyph string for a composition. S->gidx is the index of
3177 the first character drawn for glyphs of this composition.
3178 S->gidx == 0 means we are drawing the very first character of
3179 this composition. */
3180
3181 /* Draw a rectangle for the composition if the font for the very
3182 first character of the composition could not be loaded. */
3183 if (s->font_not_found_p)
3184 {
3185 if (s->gidx == 0)
3186 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3187 s->width - 1, s->height - 1);
3188 }
3189 else
3190 {
3191 for (i = 0; i < s->nchars; i++, ++s->gidx)
3192 XDrawString16 (s->display, s->window, s->gc,
3193 x + s->cmp->offsets[s->gidx * 2],
3194 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3195 s->char2b + i, 1);
3196 }
3197 }
3198
3199
3200 #ifdef USE_X_TOOLKIT
3201
3202 static struct frame *x_frame_of_widget P_ ((Widget));
3203
3204
3205 /* Return the frame on which widget WIDGET is used.. Abort if frame
3206 cannot be determined. */
3207
3208 static struct frame *
3209 x_frame_of_widget (widget)
3210 Widget widget;
3211 {
3212 struct x_display_info *dpyinfo;
3213 Lisp_Object tail;
3214 struct frame *f;
3215
3216 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3217
3218 /* Find the top-level shell of the widget. Note that this function
3219 can be called when the widget is not yet realized, so XtWindow
3220 (widget) == 0. That's the reason we can't simply use
3221 x_any_window_to_frame. */
3222 while (!XtIsTopLevelShell (widget))
3223 widget = XtParent (widget);
3224
3225 /* Look for a frame with that top-level widget. Allocate the color
3226 on that frame to get the right gamma correction value. */
3227 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3228 if (GC_FRAMEP (XCAR (tail))
3229 && (f = XFRAME (XCAR (tail)),
3230 (f->output_data.nothing != 1
3231 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3232 && f->output_data.x->widget == widget)
3233 return f;
3234
3235 abort ();
3236 }
3237
3238
3239 /* Allocate the color COLOR->pixel on the screen and display of
3240 widget WIDGET in colormap CMAP. If an exact match cannot be
3241 allocated, try the nearest color available. Value is non-zero
3242 if successful. This is called from lwlib. */
3243
3244 int
3245 x_alloc_nearest_color_for_widget (widget, cmap, color)
3246 Widget widget;
3247 Colormap cmap;
3248 XColor *color;
3249 {
3250 struct frame *f = x_frame_of_widget (widget);
3251 return x_alloc_nearest_color (f, cmap, color);
3252 }
3253
3254
3255 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3256 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3257 If this produces the same color as PIXEL, try a color where all RGB
3258 values have DELTA added. Return the allocated color in *PIXEL.
3259 DISPLAY is the X display, CMAP is the colormap to operate on.
3260 Value is non-zero if successful. */
3261
3262 int
3263 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3264 Widget widget;
3265 Display *display;
3266 Colormap cmap;
3267 unsigned long *pixel;
3268 double factor;
3269 int delta;
3270 {
3271 struct frame *f = x_frame_of_widget (widget);
3272 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3273 }
3274
3275
3276 #endif /* USE_X_TOOLKIT */
3277
3278
3279 /* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3280 CMAP. If an exact match can't be allocated, try the nearest color
3281 available. Value is non-zero if successful. Set *COLOR to the
3282 color allocated. */
3283
3284 int
3285 x_alloc_nearest_color (f, cmap, color)
3286 struct frame *f;
3287 Colormap cmap;
3288 XColor *color;
3289 {
3290 Display *display = FRAME_X_DISPLAY (f);
3291 Screen *screen = FRAME_X_SCREEN (f);
3292 int rc;
3293
3294 gamma_correct (f, color);
3295 rc = XAllocColor (display, cmap, color);
3296 if (rc == 0)
3297 {
3298 /* If we got to this point, the colormap is full, so we're going
3299 to try to get the next closest color. The algorithm used is
3300 a least-squares matching, which is what X uses for closest
3301 color matching with StaticColor visuals. */
3302 int nearest, i;
3303 unsigned long nearest_delta = ~0;
3304 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
3305 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
3306
3307 for (i = 0; i < ncells; ++i)
3308 cells[i].pixel = i;
3309 XQueryColors (display, cmap, cells, ncells);
3310
3311 for (nearest = i = 0; i < ncells; ++i)
3312 {
3313 long dred = (color->red >> 8) - (cells[i].red >> 8);
3314 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3315 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3316 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3317
3318 if (delta < nearest_delta)
3319 {
3320 nearest = i;
3321 nearest_delta = delta;
3322 }
3323 }
3324
3325 color->red = cells[nearest].red;
3326 color->green = cells[nearest].green;
3327 color->blue = cells[nearest].blue;
3328 rc = XAllocColor (display, cmap, color);
3329 }
3330
3331 #ifdef DEBUG_X_COLORS
3332 if (rc)
3333 register_color (color->pixel);
3334 #endif /* DEBUG_X_COLORS */
3335
3336 return rc;
3337 }
3338
3339
3340 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3341 It's necessary to do this instead of just using PIXEL directly to
3342 get color reference counts right. */
3343
3344 unsigned long
3345 x_copy_color (f, pixel)
3346 struct frame *f;
3347 unsigned long pixel;
3348 {
3349 XColor color;
3350
3351 color.pixel = pixel;
3352 BLOCK_INPUT;
3353 XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3354 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3355 UNBLOCK_INPUT;
3356 #ifdef DEBUG_X_COLORS
3357 register_color (pixel);
3358 #endif
3359 return color.pixel;
3360 }
3361
3362
3363 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3364 It's necessary to do this instead of just using PIXEL directly to
3365 get color reference counts right. */
3366
3367 unsigned long
3368 x_copy_dpy_color (dpy, cmap, pixel)
3369 Display *dpy;
3370 Colormap cmap;
3371 unsigned long pixel;
3372 {
3373 XColor color;
3374
3375 color.pixel = pixel;
3376 BLOCK_INPUT;
3377 XQueryColor (dpy, cmap, &color);
3378 XAllocColor (dpy, cmap, &color);
3379 UNBLOCK_INPUT;
3380 #ifdef DEBUG_X_COLORS
3381 register_color (pixel);
3382 #endif
3383 return color.pixel;
3384 }
3385
3386
3387 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3388 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3389 If this produces the same color as PIXEL, try a color where all RGB
3390 values have DELTA added. Return the allocated color in *PIXEL.
3391 DISPLAY is the X display, CMAP is the colormap to operate on.
3392 Value is non-zero if successful. */
3393
3394 static int
3395 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3396 struct frame *f;
3397 Display *display;
3398 Colormap cmap;
3399 unsigned long *pixel;
3400 double factor;
3401 int delta;
3402 {
3403 XColor color, new;
3404 int success_p;
3405
3406 /* Get RGB color values. */
3407 color.pixel = *pixel;
3408 XQueryColor (display, cmap, &color);
3409
3410 /* Change RGB values by specified FACTOR. Avoid overflow! */
3411 xassert (factor >= 0);
3412 new.red = min (0xffff, factor * color.red);
3413 new.green = min (0xffff, factor * color.green);
3414 new.blue = min (0xffff, factor * color.blue);
3415
3416 /* Try to allocate the color. */
3417 success_p = x_alloc_nearest_color (f, cmap, &new);
3418 if (success_p)
3419 {
3420 if (new.pixel == *pixel)
3421 {
3422 /* If we end up with the same color as before, try adding
3423 delta to the RGB values. */
3424 x_free_colors (f, &new.pixel, 1);
3425
3426 new.red = min (0xffff, delta + color.red);
3427 new.green = min (0xffff, delta + color.green);
3428 new.blue = min (0xffff, delta + color.blue);
3429 success_p = x_alloc_nearest_color (f, cmap, &new);
3430 }
3431 else
3432 success_p = 1;
3433 *pixel = new.pixel;
3434 }
3435
3436 return success_p;
3437 }
3438
3439
3440 /* Set up the foreground color for drawing relief lines of glyph
3441 string S. RELIEF is a pointer to a struct relief containing the GC
3442 with which lines will be drawn. Use a color that is FACTOR or
3443 DELTA lighter or darker than the relief's background which is found
3444 in S->f->output_data.x->relief_background. If such a color cannot
3445 be allocated, use DEFAULT_PIXEL, instead. */
3446
3447 static void
3448 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3449 struct frame *f;
3450 struct relief *relief;
3451 double factor;
3452 int delta;
3453 unsigned long default_pixel;
3454 {
3455 XGCValues xgcv;
3456 struct x_output *di = f->output_data.x;
3457 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3458 unsigned long pixel;
3459 unsigned long background = di->relief_background;
3460 Colormap cmap = FRAME_X_COLORMAP (f);
3461 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3462 Display *dpy = FRAME_X_DISPLAY (f);
3463
3464 xgcv.graphics_exposures = False;
3465 xgcv.line_width = 1;
3466
3467 /* Free previously allocated color. The color cell will be reused
3468 when it has been freed as many times as it was allocated, so this
3469 doesn't affect faces using the same colors. */
3470 if (relief->gc
3471 && relief->allocated_p)
3472 {
3473 x_free_colors (f, &relief->pixel, 1);
3474 relief->allocated_p = 0;
3475 }
3476
3477 /* Allocate new color. */
3478 xgcv.foreground = default_pixel;
3479 pixel = background;
3480 if (dpyinfo->n_planes != 1
3481 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3482 {
3483 relief->allocated_p = 1;
3484 xgcv.foreground = relief->pixel = pixel;
3485 }
3486
3487 if (relief->gc == 0)
3488 {
3489 xgcv.stipple = dpyinfo->gray;
3490 mask |= GCStipple;
3491 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3492 }
3493 else
3494 XChangeGC (dpy, relief->gc, mask, &xgcv);
3495 }
3496
3497
3498 /* Set up colors for the relief lines around glyph string S. */
3499
3500 static void
3501 x_setup_relief_colors (s)
3502 struct glyph_string *s;
3503 {
3504 struct x_output *di = s->f->output_data.x;
3505 unsigned long color;
3506
3507 if (s->face->use_box_color_for_shadows_p)
3508 color = s->face->box_color;
3509 else
3510 {
3511 XGCValues xgcv;
3512
3513 /* Get the background color of the face. */
3514 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3515 color = xgcv.background;
3516 }
3517
3518 if (di->white_relief.gc == 0
3519 || color != di->relief_background)
3520 {
3521 di->relief_background = color;
3522 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3523 WHITE_PIX_DEFAULT (s->f));
3524 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3525 BLACK_PIX_DEFAULT (s->f));
3526 }
3527 }
3528
3529
3530 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3531 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3532 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3533 relief. LEFT_P non-zero means draw a relief on the left side of
3534 the rectangle. RIGHT_P non-zero means draw a relief on the right
3535 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3536 when drawing. */
3537
3538 static void
3539 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3540 raised_p, left_p, right_p, clip_rect)
3541 struct frame *f;
3542 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3543 XRectangle *clip_rect;
3544 {
3545 int i;
3546 GC gc;
3547
3548 if (raised_p)
3549 gc = f->output_data.x->white_relief.gc;
3550 else
3551 gc = f->output_data.x->black_relief.gc;
3552 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3553
3554 /* Top. */
3555 for (i = 0; i < width; ++i)
3556 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3557 left_x + i * left_p, top_y + i,
3558 right_x + 1 - i * right_p, top_y + i);
3559
3560 /* Left. */
3561 if (left_p)
3562 for (i = 0; i < width; ++i)
3563 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3564 left_x + i, top_y + i, left_x + i, bottom_y - i);
3565
3566 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3567 if (raised_p)
3568 gc = f->output_data.x->black_relief.gc;
3569 else
3570 gc = f->output_data.x->white_relief.gc;
3571 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3572
3573 /* Bottom. */
3574 for (i = 0; i < width; ++i)
3575 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3576 left_x + i * left_p, bottom_y - i,
3577 right_x + 1 - i * right_p, bottom_y - i);
3578
3579 /* Right. */
3580 if (right_p)
3581 for (i = 0; i < width; ++i)
3582 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3583 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3584
3585 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3586 }
3587
3588
3589 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3590 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3591 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3592 left side of the rectangle. RIGHT_P non-zero means draw a line
3593 on the right side of the rectangle. CLIP_RECT is the clipping
3594 rectangle to use when drawing. */
3595
3596 static void
3597 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3598 left_p, right_p, clip_rect)
3599 struct glyph_string *s;
3600 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3601 XRectangle *clip_rect;
3602 {
3603 XGCValues xgcv;
3604
3605 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3606 XSetForeground (s->display, s->gc, s->face->box_color);
3607 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3608
3609 /* Top. */
3610 XFillRectangle (s->display, s->window, s->gc,
3611 left_x, top_y, right_x - left_x, width);
3612
3613 /* Left. */
3614 if (left_p)
3615 XFillRectangle (s->display, s->window, s->gc,
3616 left_x, top_y, width, bottom_y - top_y);
3617
3618 /* Bottom. */
3619 XFillRectangle (s->display, s->window, s->gc,
3620 left_x, bottom_y - width, right_x - left_x, width);
3621
3622 /* Right. */
3623 if (right_p)
3624 XFillRectangle (s->display, s->window, s->gc,
3625 right_x - width, top_y, width, bottom_y - top_y);
3626
3627 XSetForeground (s->display, s->gc, xgcv.foreground);
3628 XSetClipMask (s->display, s->gc, None);
3629 }
3630
3631
3632 /* Draw a box around glyph string S. */
3633
3634 static void
3635 x_draw_glyph_string_box (s)
3636 struct glyph_string *s;
3637 {
3638 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3639 int left_p, right_p;
3640 struct glyph *last_glyph;
3641 XRectangle clip_rect;
3642
3643 last_x = window_box_right (s->w, s->area);
3644 if (s->row->full_width_p
3645 && !s->w->pseudo_window_p)
3646 {
3647 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3648 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3649 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3650 }
3651
3652 /* The glyph that may have a right box line. */
3653 last_glyph = (s->cmp || s->img
3654 ? s->first_glyph
3655 : s->first_glyph + s->nchars - 1);
3656
3657 width = s->face->box_line_width;
3658 raised_p = s->face->box == FACE_RAISED_BOX;
3659 left_x = s->x;
3660 right_x = ((s->row->full_width_p
3661 ? last_x - 1
3662 : min (last_x, s->x + s->background_width) - 1));
3663 top_y = s->y;
3664 bottom_y = top_y + s->height - 1;
3665
3666 left_p = (s->first_glyph->left_box_line_p
3667 || (s->hl == DRAW_MOUSE_FACE
3668 && (s->prev == NULL
3669 || s->prev->hl != s->hl)));
3670 right_p = (last_glyph->right_box_line_p
3671 || (s->hl == DRAW_MOUSE_FACE
3672 && (s->next == NULL
3673 || s->next->hl != s->hl)));
3674
3675 x_get_glyph_string_clip_rect (s, &clip_rect);
3676
3677 if (s->face->box == FACE_SIMPLE_BOX)
3678 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3679 left_p, right_p, &clip_rect);
3680 else
3681 {
3682 x_setup_relief_colors (s);
3683 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3684 width, raised_p, left_p, right_p, &clip_rect);
3685 }
3686 }
3687
3688
3689 /* Draw foreground of image glyph string S. */
3690
3691 static void
3692 x_draw_image_foreground (s)
3693 struct glyph_string *s;
3694 {
3695 int x;
3696 int y = s->ybase - image_ascent (s->img, s->face);
3697
3698 /* If first glyph of S has a left box line, start drawing it to the
3699 right of that line. */
3700 if (s->face->box != FACE_NO_BOX
3701 && s->first_glyph->left_box_line_p)
3702 x = s->x + s->face->box_line_width;
3703 else
3704 x = s->x;
3705
3706 /* If there is a margin around the image, adjust x- and y-position
3707 by that margin. */
3708 if (s->img->margin)
3709 {
3710 x += s->img->margin;
3711 y += s->img->margin;
3712 }
3713
3714 if (s->img->pixmap)
3715 {
3716 if (s->img->mask)
3717 {
3718 /* We can't set both a clip mask and use XSetClipRectangles
3719 because the latter also sets a clip mask. We also can't
3720 trust on the shape extension to be available
3721 (XShapeCombineRegion). So, compute the rectangle to draw
3722 manually. */
3723 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3724 | GCFunction);
3725 XGCValues xgcv;
3726 XRectangle clip_rect, image_rect, r;
3727
3728 xgcv.clip_mask = s->img->mask;
3729 xgcv.clip_x_origin = x;
3730 xgcv.clip_y_origin = y;
3731 xgcv.function = GXcopy;
3732 XChangeGC (s->display, s->gc, mask, &xgcv);
3733
3734 x_get_glyph_string_clip_rect (s, &clip_rect);
3735 image_rect.x = x;
3736 image_rect.y = y;
3737 image_rect.width = s->img->width;
3738 image_rect.height = s->img->height;
3739 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3740 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3741 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3742 }
3743 else
3744 {
3745 unsigned long mask = GCClipXOrigin | GCClipYOrigin | GCFunction;
3746 XGCValues xgcv;
3747 XRectangle clip_rect, image_rect, r;
3748
3749 x_get_glyph_string_clip_rect (s, &clip_rect);
3750 image_rect.x = x;
3751 image_rect.y = y;
3752 image_rect.width = s->img->width;
3753 image_rect.height = s->img->height;
3754 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3755 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3756 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3757
3758 /* When the image has a mask, we can expect that at
3759 least part of a mouse highlight or a block cursor will
3760 be visible. If the image doesn't have a mask, make
3761 a block cursor visible by drawing a rectangle around
3762 the image. I believe it's looking better if we do
3763 nothing here for mouse-face. */
3764 if (s->hl == DRAW_CURSOR)
3765 XDrawRectangle (s->display, s->window, s->gc, x, y,
3766 s->img->width - 1, s->img->height - 1);
3767 }
3768 }
3769 else
3770 /* Draw a rectangle if image could not be loaded. */
3771 XDrawRectangle (s->display, s->window, s->gc, x, y,
3772 s->img->width - 1, s->img->height - 1);
3773 }
3774
3775
3776 /* Draw a relief around the image glyph string S. */
3777
3778 static void
3779 x_draw_image_relief (s)
3780 struct glyph_string *s;
3781 {
3782 int x0, y0, x1, y1, thick, raised_p;
3783 XRectangle r;
3784 int x;
3785 int y = s->ybase - image_ascent (s->img, s->face);
3786
3787 /* If first glyph of S has a left box line, start drawing it to the
3788 right of that line. */
3789 if (s->face->box != FACE_NO_BOX
3790 && s->first_glyph->left_box_line_p)
3791 x = s->x + s->face->box_line_width;
3792 else
3793 x = s->x;
3794
3795 /* If there is a margin around the image, adjust x- and y-position
3796 by that margin. */
3797 if (s->img->margin)
3798 {
3799 x += s->img->margin;
3800 y += s->img->margin;
3801 }
3802
3803 if (s->hl == DRAW_IMAGE_SUNKEN
3804 || s->hl == DRAW_IMAGE_RAISED)
3805 {
3806 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
3807 raised_p = s->hl == DRAW_IMAGE_RAISED;
3808 }
3809 else
3810 {
3811 thick = abs (s->img->relief);
3812 raised_p = s->img->relief > 0;
3813 }
3814
3815 x0 = x - thick;
3816 y0 = y - thick;
3817 x1 = x + s->img->width + thick - 1;
3818 y1 = y + s->img->height + thick - 1;
3819
3820 x_setup_relief_colors (s);
3821 x_get_glyph_string_clip_rect (s, &r);
3822 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3823 }
3824
3825
3826 /* Draw the foreground of image glyph string S to PIXMAP. */
3827
3828 static void
3829 x_draw_image_foreground_1 (s, pixmap)
3830 struct glyph_string *s;
3831 Pixmap pixmap;
3832 {
3833 int x;
3834 int y = s->ybase - s->y - image_ascent (s->img, s->face);
3835
3836 /* If first glyph of S has a left box line, start drawing it to the
3837 right of that line. */
3838 if (s->face->box != FACE_NO_BOX
3839 && s->first_glyph->left_box_line_p)
3840 x = s->face->box_line_width;
3841 else
3842 x = 0;
3843
3844 /* If there is a margin around the image, adjust x- and y-position
3845 by that margin. */
3846 if (s->img->margin)
3847 {
3848 x += s->img->margin;
3849 y += s->img->margin;
3850 }
3851
3852 if (s->img->pixmap)
3853 {
3854 if (s->img->mask)
3855 {
3856 /* We can't set both a clip mask and use XSetClipRectangles
3857 because the latter also sets a clip mask. We also can't
3858 trust on the shape extension to be available
3859 (XShapeCombineRegion). So, compute the rectangle to draw
3860 manually. */
3861 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3862 | GCFunction);
3863 XGCValues xgcv;
3864
3865 xgcv.clip_mask = s->img->mask;
3866 xgcv.clip_x_origin = x;
3867 xgcv.clip_y_origin = y;
3868 xgcv.function = GXcopy;
3869 XChangeGC (s->display, s->gc, mask, &xgcv);
3870
3871 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3872 0, 0, s->img->width, s->img->height, x, y);
3873 XSetClipMask (s->display, s->gc, None);
3874 }
3875 else
3876 {
3877 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3878 0, 0, s->img->width, s->img->height, x, y);
3879
3880 /* When the image has a mask, we can expect that at
3881 least part of a mouse highlight or a block cursor will
3882 be visible. If the image doesn't have a mask, make
3883 a block cursor visible by drawing a rectangle around
3884 the image. I believe it's looking better if we do
3885 nothing here for mouse-face. */
3886 if (s->hl == DRAW_CURSOR)
3887 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3888 s->img->width - 1, s->img->height - 1);
3889 }
3890 }
3891 else
3892 /* Draw a rectangle if image could not be loaded. */
3893 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3894 s->img->width - 1, s->img->height - 1);
3895 }
3896
3897
3898 /* Draw part of the background of glyph string S. X, Y, W, and H
3899 give the rectangle to draw. */
3900
3901 static void
3902 x_draw_glyph_string_bg_rect (s, x, y, w, h)
3903 struct glyph_string *s;
3904 int x, y, w, h;
3905 {
3906 if (s->stippled_p)
3907 {
3908 /* Fill background with a stipple pattern. */
3909 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3910 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3911 XSetFillStyle (s->display, s->gc, FillSolid);
3912 }
3913 else
3914 x_clear_glyph_string_rect (s, x, y, w, h);
3915 }
3916
3917
3918 /* Draw image glyph string S.
3919
3920 s->y
3921 s->x +-------------------------
3922 | s->face->box
3923 |
3924 | +-------------------------
3925 | | s->img->margin
3926 | |
3927 | | +-------------------
3928 | | | the image
3929
3930 */
3931
3932 static void
3933 x_draw_image_glyph_string (s)
3934 struct glyph_string *s;
3935 {
3936 int x, y;
3937 int box_line_width = s->face->box_line_width;
3938 int margin = s->img->margin;
3939 int height;
3940 Pixmap pixmap = None;
3941
3942 height = s->height - 2 * box_line_width;
3943
3944 /* Fill background with face under the image. Do it only if row is
3945 taller than image or if image has a clip mask to reduce
3946 flickering. */
3947 s->stippled_p = s->face->stipple != 0;
3948 if (height > s->img->height
3949 || margin
3950 || s->img->mask
3951 || s->img->pixmap == 0
3952 || s->width != s->background_width)
3953 {
3954 if (box_line_width && s->first_glyph->left_box_line_p)
3955 x = s->x + box_line_width;
3956 else
3957 x = s->x;
3958
3959 y = s->y + box_line_width;
3960
3961 if (s->img->mask)
3962 {
3963 /* Create a pixmap as large as the glyph string Fill it with
3964 the background color. Copy the image to it, using its
3965 mask. Copy the temporary pixmap to the display. */
3966 Screen *screen = FRAME_X_SCREEN (s->f);
3967 int depth = DefaultDepthOfScreen (screen);
3968
3969 /* Create a pixmap as large as the glyph string. */
3970 pixmap = XCreatePixmap (s->display, s->window,
3971 s->background_width,
3972 s->height, depth);
3973
3974 /* Don't clip in the following because we're working on the
3975 pixmap. */
3976 XSetClipMask (s->display, s->gc, None);
3977
3978 /* Fill the pixmap with the background color/stipple. */
3979 if (s->stippled_p)
3980 {
3981 /* Fill background with a stipple pattern. */
3982 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3983 XFillRectangle (s->display, pixmap, s->gc,
3984 0, 0, s->background_width, s->height);
3985 XSetFillStyle (s->display, s->gc, FillSolid);
3986 }
3987 else
3988 {
3989 XGCValues xgcv;
3990 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3991 &xgcv);
3992 XSetForeground (s->display, s->gc, xgcv.background);
3993 XFillRectangle (s->display, pixmap, s->gc,
3994 0, 0, s->background_width, s->height);
3995 XSetForeground (s->display, s->gc, xgcv.foreground);
3996 }
3997 }
3998 else
3999 /* Implementation idea: Is it possible to construct a mask?
4000 We could look at the color at the margins of the image, and
4001 say that this color is probably the background color of the
4002 image. */
4003 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4004
4005 s->background_filled_p = 1;
4006 }
4007
4008 /* Draw the foreground. */
4009 if (pixmap != None)
4010 {
4011 x_draw_image_foreground_1 (s, pixmap);
4012 x_set_glyph_string_clipping (s);
4013 XCopyArea (s->display, pixmap, s->window, s->gc,
4014 0, 0, s->background_width, s->height, s->x, s->y);
4015 XFreePixmap (s->display, pixmap);
4016 }
4017 else
4018 x_draw_image_foreground (s);
4019
4020 /* If we must draw a relief around the image, do it. */
4021 if (s->img->relief
4022 || s->hl == DRAW_IMAGE_RAISED
4023 || s->hl == DRAW_IMAGE_SUNKEN)
4024 x_draw_image_relief (s);
4025 }
4026
4027
4028 /* Draw stretch glyph string S. */
4029
4030 static void
4031 x_draw_stretch_glyph_string (s)
4032 struct glyph_string *s;
4033 {
4034 xassert (s->first_glyph->type == STRETCH_GLYPH);
4035 s->stippled_p = s->face->stipple != 0;
4036
4037 if (s->hl == DRAW_CURSOR
4038 && !x_stretch_cursor_p)
4039 {
4040 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4041 as wide as the stretch glyph. */
4042 int width = min (CANON_X_UNIT (s->f), s->background_width);
4043
4044 /* Draw cursor. */
4045 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4046
4047 /* Clear rest using the GC of the original non-cursor face. */
4048 if (width < s->background_width)
4049 {
4050 GC gc = s->face->gc;
4051 int x = s->x + width, y = s->y;
4052 int w = s->background_width - width, h = s->height;
4053 XRectangle r;
4054
4055 x_get_glyph_string_clip_rect (s, &r);
4056 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4057
4058 if (s->face->stipple)
4059 {
4060 /* Fill background with a stipple pattern. */
4061 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4062 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4063 XSetFillStyle (s->display, gc, FillSolid);
4064 }
4065 else
4066 {
4067 XGCValues xgcv;
4068 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4069 XSetForeground (s->display, gc, xgcv.background);
4070 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4071 XSetForeground (s->display, gc, xgcv.foreground);
4072 }
4073 }
4074 }
4075 else
4076 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4077 s->height);
4078
4079 s->background_filled_p = 1;
4080 }
4081
4082
4083 /* Draw glyph string S. */
4084
4085 static void
4086 x_draw_glyph_string (s)
4087 struct glyph_string *s;
4088 {
4089 /* If S draws into the background of its successor, draw the
4090 background of the successor first so that S can draw into it.
4091 This makes S->next use XDrawString instead of XDrawImageString. */
4092 if (s->next && s->right_overhang && !s->for_overlaps_p)
4093 {
4094 xassert (s->next->img == NULL);
4095 x_set_glyph_string_gc (s->next);
4096 x_set_glyph_string_clipping (s->next);
4097 x_draw_glyph_string_background (s->next, 1);
4098 }
4099
4100 /* Set up S->gc, set clipping and draw S. */
4101 x_set_glyph_string_gc (s);
4102 x_set_glyph_string_clipping (s);
4103
4104 switch (s->first_glyph->type)
4105 {
4106 case IMAGE_GLYPH:
4107 x_draw_image_glyph_string (s);
4108 break;
4109
4110 case STRETCH_GLYPH:
4111 x_draw_stretch_glyph_string (s);
4112 break;
4113
4114 case CHAR_GLYPH:
4115 if (s->for_overlaps_p)
4116 s->background_filled_p = 1;
4117 else
4118 x_draw_glyph_string_background (s, 0);
4119 x_draw_glyph_string_foreground (s);
4120 break;
4121
4122 case COMPOSITE_GLYPH:
4123 if (s->for_overlaps_p || s->gidx > 0)
4124 s->background_filled_p = 1;
4125 else
4126 x_draw_glyph_string_background (s, 1);
4127 x_draw_composite_glyph_string_foreground (s);
4128 break;
4129
4130 default:
4131 abort ();
4132 }
4133
4134 if (!s->for_overlaps_p)
4135 {
4136 /* Draw underline. */
4137 if (s->face->underline_p)
4138 {
4139 unsigned long dy, h;
4140
4141 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4142 h = 1;
4143 if (!XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &dy))
4144 dy = s->height - h;
4145
4146 if (s->face->underline_defaulted_p)
4147 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4148 s->width, h);
4149 else
4150 {
4151 XGCValues xgcv;
4152 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4153 XSetForeground (s->display, s->gc, s->face->underline_color);
4154 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4155 s->width, h);
4156 XSetForeground (s->display, s->gc, xgcv.foreground);
4157 }
4158 }
4159
4160 /* Draw overline. */
4161 if (s->face->overline_p)
4162 {
4163 unsigned long dy = 0, h = 1;
4164
4165 if (s->face->overline_color_defaulted_p)
4166 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4167 s->width, h);
4168 else
4169 {
4170 XGCValues xgcv;
4171 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4172 XSetForeground (s->display, s->gc, s->face->overline_color);
4173 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4174 s->width, h);
4175 XSetForeground (s->display, s->gc, xgcv.foreground);
4176 }
4177 }
4178
4179 /* Draw strike-through. */
4180 if (s->face->strike_through_p)
4181 {
4182 unsigned long h = 1;
4183 unsigned long dy = (s->height - h) / 2;
4184
4185 if (s->face->strike_through_color_defaulted_p)
4186 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4187 s->width, h);
4188 else
4189 {
4190 XGCValues xgcv;
4191 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4192 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4193 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4194 s->width, h);
4195 XSetForeground (s->display, s->gc, xgcv.foreground);
4196 }
4197 }
4198
4199 /* Draw relief. */
4200 if (s->face->box != FACE_NO_BOX)
4201 x_draw_glyph_string_box (s);
4202 }
4203
4204 /* Reset clipping. */
4205 XSetClipMask (s->display, s->gc, None);
4206 }
4207
4208
4209 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4210 struct face **, int));
4211
4212
4213 /* Fill glyph string S with composition components specified by S->cmp.
4214
4215 FACES is an array of faces for all components of this composition.
4216 S->gidx is the index of the first component for S.
4217 OVERLAPS_P non-zero means S should draw the foreground only, and
4218 use its physical height for clipping.
4219
4220 Value is the index of a component not in S. */
4221
4222 static int
4223 x_fill_composite_glyph_string (s, faces, overlaps_p)
4224 struct glyph_string *s;
4225 struct face **faces;
4226 int overlaps_p;
4227 {
4228 int i;
4229
4230 xassert (s);
4231
4232 s->for_overlaps_p = overlaps_p;
4233
4234 s->face = faces[s->gidx];
4235 s->font = s->face->font;
4236 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4237
4238 /* For all glyphs of this composition, starting at the offset
4239 S->gidx, until we reach the end of the definition or encounter a
4240 glyph that requires the different face, add it to S. */
4241 ++s->nchars;
4242 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4243 ++s->nchars;
4244
4245 /* All glyph strings for the same composition has the same width,
4246 i.e. the width set for the first component of the composition. */
4247
4248 s->width = s->first_glyph->pixel_width;
4249
4250 /* If the specified font could not be loaded, use the frame's
4251 default font, but record the fact that we couldn't load it in
4252 the glyph string so that we can draw rectangles for the
4253 characters of the glyph string. */
4254 if (s->font == NULL)
4255 {
4256 s->font_not_found_p = 1;
4257 s->font = FRAME_FONT (s->f);
4258 }
4259
4260 /* Adjust base line for subscript/superscript text. */
4261 s->ybase += s->first_glyph->voffset;
4262
4263 xassert (s->face && s->face->gc);
4264
4265 /* This glyph string must always be drawn with 16-bit functions. */
4266 s->two_byte_p = 1;
4267
4268 return s->gidx + s->nchars;
4269 }
4270
4271
4272 /* Fill glyph string S from a sequence of character glyphs.
4273
4274 FACE_ID is the face id of the string. START is the index of the
4275 first glyph to consider, END is the index of the last + 1.
4276 OVERLAPS_P non-zero means S should draw the foreground only, and
4277 use its physical height for clipping.
4278
4279 Value is the index of the first glyph not in S. */
4280
4281 static int
4282 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4283 struct glyph_string *s;
4284 int face_id;
4285 int start, end, overlaps_p;
4286 {
4287 struct glyph *glyph, *last;
4288 int voffset;
4289 int glyph_not_available_p;
4290
4291 xassert (s->f == XFRAME (s->w->frame));
4292 xassert (s->nchars == 0);
4293 xassert (start >= 0 && end > start);
4294
4295 s->for_overlaps_p = overlaps_p,
4296 glyph = s->row->glyphs[s->area] + start;
4297 last = s->row->glyphs[s->area] + end;
4298 voffset = glyph->voffset;
4299
4300 glyph_not_available_p = glyph->glyph_not_available_p;
4301
4302 while (glyph < last
4303 && glyph->type == CHAR_GLYPH
4304 && glyph->voffset == voffset
4305 /* Same face id implies same font, nowadays. */
4306 && glyph->face_id == face_id
4307 && glyph->glyph_not_available_p == glyph_not_available_p)
4308 {
4309 int two_byte_p;
4310
4311 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4312 s->char2b + s->nchars,
4313 &two_byte_p);
4314 s->two_byte_p = two_byte_p;
4315 ++s->nchars;
4316 xassert (s->nchars <= end - start);
4317 s->width += glyph->pixel_width;
4318 ++glyph;
4319 }
4320
4321 s->font = s->face->font;
4322 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4323
4324 /* If the specified font could not be loaded, use the frame's font,
4325 but record the fact that we couldn't load it in
4326 S->font_not_found_p so that we can draw rectangles for the
4327 characters of the glyph string. */
4328 if (s->font == NULL || glyph_not_available_p)
4329 {
4330 s->font_not_found_p = 1;
4331 s->font = FRAME_FONT (s->f);
4332 }
4333
4334 /* Adjust base line for subscript/superscript text. */
4335 s->ybase += voffset;
4336
4337 xassert (s->face && s->face->gc);
4338 return glyph - s->row->glyphs[s->area];
4339 }
4340
4341
4342 /* Fill glyph string S from image glyph S->first_glyph. */
4343
4344 static void
4345 x_fill_image_glyph_string (s)
4346 struct glyph_string *s;
4347 {
4348 xassert (s->first_glyph->type == IMAGE_GLYPH);
4349 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4350 xassert (s->img);
4351 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4352 s->font = s->face->font;
4353 s->width = s->first_glyph->pixel_width;
4354
4355 /* Adjust base line for subscript/superscript text. */
4356 s->ybase += s->first_glyph->voffset;
4357 }
4358
4359
4360 /* Fill glyph string S from a sequence of stretch glyphs.
4361
4362 ROW is the glyph row in which the glyphs are found, AREA is the
4363 area within the row. START is the index of the first glyph to
4364 consider, END is the index of the last + 1.
4365
4366 Value is the index of the first glyph not in S. */
4367
4368 static int
4369 x_fill_stretch_glyph_string (s, row, area, start, end)
4370 struct glyph_string *s;
4371 struct glyph_row *row;
4372 enum glyph_row_area area;
4373 int start, end;
4374 {
4375 struct glyph *glyph, *last;
4376 int voffset, face_id;
4377
4378 xassert (s->first_glyph->type == STRETCH_GLYPH);
4379
4380 glyph = s->row->glyphs[s->area] + start;
4381 last = s->row->glyphs[s->area] + end;
4382 face_id = glyph->face_id;
4383 s->face = FACE_FROM_ID (s->f, face_id);
4384 s->font = s->face->font;
4385 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4386 s->width = glyph->pixel_width;
4387 voffset = glyph->voffset;
4388
4389 for (++glyph;
4390 (glyph < last
4391 && glyph->type == STRETCH_GLYPH
4392 && glyph->voffset == voffset
4393 && glyph->face_id == face_id);
4394 ++glyph)
4395 s->width += glyph->pixel_width;
4396
4397 /* Adjust base line for subscript/superscript text. */
4398 s->ybase += voffset;
4399
4400 xassert (s->face && s->face->gc);
4401 return glyph - s->row->glyphs[s->area];
4402 }
4403
4404
4405 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4406 of XChar2b structures for S; it can't be allocated in
4407 x_init_glyph_string because it must be allocated via `alloca'. W
4408 is the window on which S is drawn. ROW and AREA are the glyph row
4409 and area within the row from which S is constructed. START is the
4410 index of the first glyph structure covered by S. HL is a
4411 face-override for drawing S. */
4412
4413 static void
4414 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4415 struct glyph_string *s;
4416 XChar2b *char2b;
4417 struct window *w;
4418 struct glyph_row *row;
4419 enum glyph_row_area area;
4420 int start;
4421 enum draw_glyphs_face hl;
4422 {
4423 bzero (s, sizeof *s);
4424 s->w = w;
4425 s->f = XFRAME (w->frame);
4426 s->display = FRAME_X_DISPLAY (s->f);
4427 s->window = FRAME_X_WINDOW (s->f);
4428 s->char2b = char2b;
4429 s->hl = hl;
4430 s->row = row;
4431 s->area = area;
4432 s->first_glyph = row->glyphs[area] + start;
4433 s->height = row->height;
4434 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4435
4436 /* Display the internal border below the tool-bar window. */
4437 if (s->w == XWINDOW (s->f->tool_bar_window))
4438 s->y -= s->f->output_data.x->internal_border_width;
4439
4440 s->ybase = s->y + row->ascent;
4441 }
4442
4443
4444 /* Set background width of glyph string S. START is the index of the
4445 first glyph following S. LAST_X is the right-most x-position + 1
4446 in the drawing area. */
4447
4448 static INLINE void
4449 x_set_glyph_string_background_width (s, start, last_x)
4450 struct glyph_string *s;
4451 int start;
4452 int last_x;
4453 {
4454 /* If the face of this glyph string has to be drawn to the end of
4455 the drawing area, set S->extends_to_end_of_line_p. */
4456 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4457
4458 if (start == s->row->used[s->area]
4459 && s->hl == DRAW_NORMAL_TEXT
4460 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4461 || s->face->background != default_face->background
4462 || s->face->stipple != default_face->stipple))
4463 s->extends_to_end_of_line_p = 1;
4464
4465 /* If S extends its face to the end of the line, set its
4466 background_width to the distance to the right edge of the drawing
4467 area. */
4468 if (s->extends_to_end_of_line_p)
4469 s->background_width = last_x - s->x + 1;
4470 else
4471 s->background_width = s->width;
4472 }
4473
4474
4475 /* Add a glyph string for a stretch glyph to the list of strings
4476 between HEAD and TAIL. START is the index of the stretch glyph in
4477 row area AREA of glyph row ROW. END is the index of the last glyph
4478 in that glyph row area. X is the current output position assigned
4479 to the new glyph string constructed. HL overrides that face of the
4480 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4481 is the right-most x-position of the drawing area. */
4482
4483 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4484 and below -- keep them on one line. */
4485 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4486 do \
4487 { \
4488 s = (struct glyph_string *) alloca (sizeof *s); \
4489 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4490 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4491 x_append_glyph_string (&HEAD, &TAIL, s); \
4492 s->x = (X); \
4493 } \
4494 while (0)
4495
4496
4497 /* Add a glyph string for an image glyph to the list of strings
4498 between HEAD and TAIL. START is the index of the image glyph in
4499 row area AREA of glyph row ROW. END is the index of the last glyph
4500 in that glyph row area. X is the current output position assigned
4501 to the new glyph string constructed. HL overrides that face of the
4502 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4503 is the right-most x-position of the drawing area. */
4504
4505 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4506 do \
4507 { \
4508 s = (struct glyph_string *) alloca (sizeof *s); \
4509 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4510 x_fill_image_glyph_string (s); \
4511 x_append_glyph_string (&HEAD, &TAIL, s); \
4512 ++START; \
4513 s->x = (X); \
4514 } \
4515 while (0)
4516
4517
4518 /* Add a glyph string for a sequence of character glyphs to the list
4519 of strings between HEAD and TAIL. START is the index of the first
4520 glyph in row area AREA of glyph row ROW that is part of the new
4521 glyph string. END is the index of the last glyph in that glyph row
4522 area. X is the current output position assigned to the new glyph
4523 string constructed. HL overrides that face of the glyph; e.g. it
4524 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4525 right-most x-position of the drawing area. */
4526
4527 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4528 do \
4529 { \
4530 int c, face_id; \
4531 XChar2b *char2b; \
4532 \
4533 c = (ROW)->glyphs[AREA][START].u.ch; \
4534 face_id = (ROW)->glyphs[AREA][START].face_id; \
4535 \
4536 s = (struct glyph_string *) alloca (sizeof *s); \
4537 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4538 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4539 x_append_glyph_string (&HEAD, &TAIL, s); \
4540 s->x = (X); \
4541 START = x_fill_glyph_string (s, face_id, START, END, \
4542 OVERLAPS_P); \
4543 } \
4544 while (0)
4545
4546
4547 /* Add a glyph string for a composite sequence to the list of strings
4548 between HEAD and TAIL. START is the index of the first glyph in
4549 row area AREA of glyph row ROW that is part of the new glyph
4550 string. END is the index of the last glyph in that glyph row area.
4551 X is the current output position assigned to the new glyph string
4552 constructed. HL overrides that face of the glyph; e.g. it is
4553 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4554 x-position of the drawing area. */
4555
4556 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4557 do { \
4558 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4559 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4560 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4561 struct composition *cmp = composition_table[cmp_id]; \
4562 int glyph_len = cmp->glyph_len; \
4563 XChar2b *char2b; \
4564 struct face **faces; \
4565 struct glyph_string *first_s = NULL; \
4566 int n; \
4567 \
4568 base_face = base_face->ascii_face; \
4569 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4570 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4571 /* At first, fill in `char2b' and `faces'. */ \
4572 for (n = 0; n < glyph_len; n++) \
4573 { \
4574 int c = COMPOSITION_GLYPH (cmp, n); \
4575 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4576 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4577 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4578 this_face_id, char2b + n, 1); \
4579 } \
4580 \
4581 /* Make glyph_strings for each glyph sequence that is drawable by \
4582 the same face, and append them to HEAD/TAIL. */ \
4583 for (n = 0; n < cmp->glyph_len;) \
4584 { \
4585 s = (struct glyph_string *) alloca (sizeof *s); \
4586 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4587 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4588 s->cmp = cmp; \
4589 s->gidx = n; \
4590 s->x = (X); \
4591 \
4592 if (n == 0) \
4593 first_s = s; \
4594 \
4595 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4596 } \
4597 \
4598 ++START; \
4599 s = first_s; \
4600 } while (0)
4601
4602
4603 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4604 of AREA of glyph row ROW on window W between indices START and END.
4605 HL overrides the face for drawing glyph strings, e.g. it is
4606 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4607 x-positions of the drawing area.
4608
4609 This is an ugly monster macro construct because we must use alloca
4610 to allocate glyph strings (because x_draw_glyphs can be called
4611 asynchronously). */
4612
4613 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4614 do \
4615 { \
4616 HEAD = TAIL = NULL; \
4617 while (START < END) \
4618 { \
4619 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4620 switch (first_glyph->type) \
4621 { \
4622 case CHAR_GLYPH: \
4623 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
4624 TAIL, HL, X, LAST_X, \
4625 OVERLAPS_P); \
4626 break; \
4627 \
4628 case COMPOSITE_GLYPH: \
4629 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4630 HEAD, TAIL, HL, X, LAST_X,\
4631 OVERLAPS_P); \
4632 break; \
4633 \
4634 case STRETCH_GLYPH: \
4635 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4636 HEAD, TAIL, HL, X, LAST_X); \
4637 break; \
4638 \
4639 case IMAGE_GLYPH: \
4640 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4641 TAIL, HL, X, LAST_X); \
4642 break; \
4643 \
4644 default: \
4645 abort (); \
4646 } \
4647 \
4648 x_set_glyph_string_background_width (s, START, LAST_X); \
4649 (X) += s->width; \
4650 } \
4651 } \
4652 while (0)
4653
4654
4655 /* Draw glyphs between START and END in AREA of ROW on window W,
4656 starting at x-position X. X is relative to AREA in W. HL is a
4657 face-override with the following meaning:
4658
4659 DRAW_NORMAL_TEXT draw normally
4660 DRAW_CURSOR draw in cursor face
4661 DRAW_MOUSE_FACE draw in mouse face.
4662 DRAW_INVERSE_VIDEO draw in mode line face
4663 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4664 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4665
4666 If REAL_START is non-null, return in *REAL_START the real starting
4667 position for display. This can be different from START in case
4668 overlapping glyphs must be displayed. If REAL_END is non-null,
4669 return in *REAL_END the real end position for display. This can be
4670 different from END in case overlapping glyphs must be displayed.
4671
4672 If OVERLAPS_P is non-zero, draw only the foreground of characters
4673 and clip to the physical height of ROW.
4674
4675 Value is the x-position reached, relative to AREA of W. */
4676
4677 static int
4678 x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4679 overlaps_p)
4680 struct window *w;
4681 int x;
4682 struct glyph_row *row;
4683 enum glyph_row_area area;
4684 int start, end;
4685 enum draw_glyphs_face hl;
4686 int *real_start, *real_end;
4687 int overlaps_p;
4688 {
4689 struct glyph_string *head, *tail;
4690 struct glyph_string *s;
4691 int last_x, area_width;
4692 int x_reached;
4693 int i, j;
4694
4695 /* Let's rather be paranoid than getting a SEGV. */
4696 start = max (0, start);
4697 end = min (end, row->used[area]);
4698 if (real_start)
4699 *real_start = start;
4700 if (real_end)
4701 *real_end = end;
4702
4703 /* Translate X to frame coordinates. Set last_x to the right
4704 end of the drawing area. */
4705 if (row->full_width_p)
4706 {
4707 /* X is relative to the left edge of W, without scroll bars
4708 or flag areas. */
4709 struct frame *f = XFRAME (w->frame);
4710 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
4711 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
4712
4713 x += window_left_x;
4714 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4715 last_x = window_left_x + area_width;
4716
4717 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4718 {
4719 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4720 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4721 last_x += width;
4722 else
4723 x -= width;
4724 }
4725
4726 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4727 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
4728 }
4729 else
4730 {
4731 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4732 area_width = window_box_width (w, area);
4733 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
4734 }
4735
4736 /* Build a doubly-linked list of glyph_string structures between
4737 head and tail from what we have to draw. Note that the macro
4738 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4739 the reason we use a separate variable `i'. */
4740 i = start;
4741 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4742 overlaps_p);
4743 if (tail)
4744 x_reached = tail->x + tail->background_width;
4745 else
4746 x_reached = x;
4747
4748 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4749 the row, redraw some glyphs in front or following the glyph
4750 strings built above. */
4751 if (!overlaps_p && row->contains_overlapping_glyphs_p)
4752 {
4753 int dummy_x = 0;
4754 struct glyph_string *h, *t;
4755
4756 /* Compute overhangs for all glyph strings. */
4757 for (s = head; s; s = s->next)
4758 x_compute_glyph_string_overhangs (s);
4759
4760 /* Prepend glyph strings for glyphs in front of the first glyph
4761 string that are overwritten because of the first glyph
4762 string's left overhang. The background of all strings
4763 prepended must be drawn because the first glyph string
4764 draws over it. */
4765 i = x_left_overwritten (head);
4766 if (i >= 0)
4767 {
4768 j = i;
4769 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
4770 DRAW_NORMAL_TEXT, dummy_x, last_x,
4771 overlaps_p);
4772 start = i;
4773 if (real_start)
4774 *real_start = start;
4775 x_compute_overhangs_and_x (t, head->x, 1);
4776 x_prepend_glyph_string_lists (&head, &tail, h, t);
4777 }
4778
4779 /* Prepend glyph strings for glyphs in front of the first glyph
4780 string that overwrite that glyph string because of their
4781 right overhang. For these strings, only the foreground must
4782 be drawn, because it draws over the glyph string at `head'.
4783 The background must not be drawn because this would overwrite
4784 right overhangs of preceding glyphs for which no glyph
4785 strings exist. */
4786 i = x_left_overwriting (head);
4787 if (i >= 0)
4788 {
4789 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
4790 DRAW_NORMAL_TEXT, dummy_x, last_x,
4791 overlaps_p);
4792 for (s = h; s; s = s->next)
4793 s->background_filled_p = 1;
4794 if (real_start)
4795 *real_start = i;
4796 x_compute_overhangs_and_x (t, head->x, 1);
4797 x_prepend_glyph_string_lists (&head, &tail, h, t);
4798 }
4799
4800 /* Append glyphs strings for glyphs following the last glyph
4801 string tail that are overwritten by tail. The background of
4802 these strings has to be drawn because tail's foreground draws
4803 over it. */
4804 i = x_right_overwritten (tail);
4805 if (i >= 0)
4806 {
4807 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4808 DRAW_NORMAL_TEXT, x, last_x,
4809 overlaps_p);
4810 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4811 x_append_glyph_string_lists (&head, &tail, h, t);
4812 if (real_end)
4813 *real_end = i;
4814 }
4815
4816 /* Append glyph strings for glyphs following the last glyph
4817 string tail that overwrite tail. The foreground of such
4818 glyphs has to be drawn because it writes into the background
4819 of tail. The background must not be drawn because it could
4820 paint over the foreground of following glyphs. */
4821 i = x_right_overwriting (tail);
4822 if (i >= 0)
4823 {
4824 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4825 DRAW_NORMAL_TEXT, x, last_x,
4826 overlaps_p);
4827 for (s = h; s; s = s->next)
4828 s->background_filled_p = 1;
4829 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4830 x_append_glyph_string_lists (&head, &tail, h, t);
4831 if (real_end)
4832 *real_end = i;
4833 }
4834 }
4835
4836 /* Draw all strings. */
4837 for (s = head; s; s = s->next)
4838 x_draw_glyph_string (s);
4839
4840 /* Value is the x-position up to which drawn, relative to AREA of W.
4841 This doesn't include parts drawn because of overhangs. */
4842 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4843 if (!row->full_width_p)
4844 {
4845 if (area > LEFT_MARGIN_AREA)
4846 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4847 if (area > TEXT_AREA)
4848 x_reached -= window_box_width (w, TEXT_AREA);
4849 }
4850 return x_reached;
4851 }
4852
4853
4854 /* Fix the display of area AREA of overlapping row ROW in window W. */
4855
4856 static void
4857 x_fix_overlapping_area (w, row, area)
4858 struct window *w;
4859 struct glyph_row *row;
4860 enum glyph_row_area area;
4861 {
4862 int i, x;
4863
4864 BLOCK_INPUT;
4865
4866 if (area == LEFT_MARGIN_AREA)
4867 x = 0;
4868 else if (area == TEXT_AREA)
4869 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
4870 else
4871 x = (window_box_width (w, LEFT_MARGIN_AREA)
4872 + window_box_width (w, TEXT_AREA));
4873
4874 for (i = 0; i < row->used[area];)
4875 {
4876 if (row->glyphs[area][i].overlaps_vertically_p)
4877 {
4878 int start = i, start_x = x;
4879
4880 do
4881 {
4882 x += row->glyphs[area][i].pixel_width;
4883 ++i;
4884 }
4885 while (i < row->used[area]
4886 && row->glyphs[area][i].overlaps_vertically_p);
4887
4888 x_draw_glyphs (w, start_x, row, area, start, i,
4889 (row->inverse_p
4890 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4891 NULL, NULL, 1);
4892 }
4893 else
4894 {
4895 x += row->glyphs[area][i].pixel_width;
4896 ++i;
4897 }
4898 }
4899
4900 UNBLOCK_INPUT;
4901 }
4902
4903
4904 /* Output LEN glyphs starting at START at the nominal cursor position.
4905 Advance the nominal cursor over the text. The global variable
4906 updated_window contains the window being updated, updated_row is
4907 the glyph row being updated, and updated_area is the area of that
4908 row being updated. */
4909
4910 static void
4911 x_write_glyphs (start, len)
4912 struct glyph *start;
4913 int len;
4914 {
4915 int x, hpos, real_start, real_end;
4916
4917 xassert (updated_window && updated_row);
4918 BLOCK_INPUT;
4919
4920 /* Write glyphs. */
4921
4922 hpos = start - updated_row->glyphs[updated_area];
4923 x = x_draw_glyphs (updated_window, output_cursor.x,
4924 updated_row, updated_area,
4925 hpos, hpos + len,
4926 (updated_row->inverse_p
4927 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4928 &real_start, &real_end, 0);
4929
4930 /* If we drew over the cursor, note that it is not visible any more. */
4931 note_overwritten_text_cursor (updated_window, real_start,
4932 real_end - real_start);
4933
4934 UNBLOCK_INPUT;
4935
4936 /* Advance the output cursor. */
4937 output_cursor.hpos += len;
4938 output_cursor.x = x;
4939 }
4940
4941
4942 /* Insert LEN glyphs from START at the nominal cursor position. */
4943
4944 static void
4945 x_insert_glyphs (start, len)
4946 struct glyph *start;
4947 register int len;
4948 {
4949 struct frame *f;
4950 struct window *w;
4951 int line_height, shift_by_width, shifted_region_width;
4952 struct glyph_row *row;
4953 struct glyph *glyph;
4954 int frame_x, frame_y, hpos, real_start, real_end;
4955
4956 xassert (updated_window && updated_row);
4957 BLOCK_INPUT;
4958 w = updated_window;
4959 f = XFRAME (WINDOW_FRAME (w));
4960
4961 /* Get the height of the line we are in. */
4962 row = updated_row;
4963 line_height = row->height;
4964
4965 /* Get the width of the glyphs to insert. */
4966 shift_by_width = 0;
4967 for (glyph = start; glyph < start + len; ++glyph)
4968 shift_by_width += glyph->pixel_width;
4969
4970 /* Get the width of the region to shift right. */
4971 shifted_region_width = (window_box_width (w, updated_area)
4972 - output_cursor.x
4973 - shift_by_width);
4974
4975 /* Shift right. */
4976 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
4977 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
4978 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4979 f->output_data.x->normal_gc,
4980 frame_x, frame_y,
4981 shifted_region_width, line_height,
4982 frame_x + shift_by_width, frame_y);
4983
4984 /* Write the glyphs. */
4985 hpos = start - row->glyphs[updated_area];
4986 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
4987 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
4988 note_overwritten_text_cursor (w, real_start, real_end - real_start);
4989
4990 /* Advance the output cursor. */
4991 output_cursor.hpos += len;
4992 output_cursor.x += shift_by_width;
4993 UNBLOCK_INPUT;
4994 }
4995
4996
4997 /* Delete N glyphs at the nominal cursor position. Not implemented
4998 for X frames. */
4999
5000 static void
5001 x_delete_glyphs (n)
5002 register int n;
5003 {
5004 abort ();
5005 }
5006
5007
5008 /* Erase the current text line from the nominal cursor position
5009 (inclusive) to pixel column TO_X (exclusive). The idea is that
5010 everything from TO_X onward is already erased.
5011
5012 TO_X is a pixel position relative to updated_area of
5013 updated_window. TO_X == -1 means clear to the end of this area. */
5014
5015 static void
5016 x_clear_end_of_line (to_x)
5017 int to_x;
5018 {
5019 struct frame *f;
5020 struct window *w = updated_window;
5021 int max_x, min_y, max_y;
5022 int from_x, from_y, to_y;
5023
5024 xassert (updated_window && updated_row);
5025 f = XFRAME (w->frame);
5026
5027 if (updated_row->full_width_p)
5028 {
5029 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5030 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5031 && !w->pseudo_window_p)
5032 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5033 }
5034 else
5035 max_x = window_box_width (w, updated_area);
5036 max_y = window_text_bottom_y (w);
5037
5038 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5039 of window. For TO_X > 0, truncate to end of drawing area. */
5040 if (to_x == 0)
5041 return;
5042 else if (to_x < 0)
5043 to_x = max_x;
5044 else
5045 to_x = min (to_x, max_x);
5046
5047 to_y = min (max_y, output_cursor.y + updated_row->height);
5048
5049 /* Notice if the cursor will be cleared by this operation. */
5050 if (!updated_row->full_width_p)
5051 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
5052
5053 from_x = output_cursor.x;
5054
5055 /* Translate to frame coordinates. */
5056 if (updated_row->full_width_p)
5057 {
5058 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5059 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5060 }
5061 else
5062 {
5063 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5064 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5065 }
5066
5067 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5068 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5069 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5070
5071 /* Prevent inadvertently clearing to end of the X window. */
5072 if (to_x > from_x && to_y > from_y)
5073 {
5074 BLOCK_INPUT;
5075 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5076 from_x, from_y, to_x - from_x, to_y - from_y,
5077 False);
5078 UNBLOCK_INPUT;
5079 }
5080 }
5081
5082
5083 /* Clear entire frame. If updating_frame is non-null, clear that
5084 frame. Otherwise clear the selected frame. */
5085
5086 static void
5087 x_clear_frame ()
5088 {
5089 struct frame *f;
5090
5091 if (updating_frame)
5092 f = updating_frame;
5093 else
5094 f = SELECTED_FRAME ();
5095
5096 /* Clearing the frame will erase any cursor, so mark them all as no
5097 longer visible. */
5098 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5099 output_cursor.hpos = output_cursor.vpos = 0;
5100 output_cursor.x = -1;
5101
5102 /* We don't set the output cursor here because there will always
5103 follow an explicit cursor_to. */
5104 BLOCK_INPUT;
5105 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5106
5107 /* We have to clear the scroll bars, too. If we have changed
5108 colors or something like that, then they should be notified. */
5109 x_scroll_bar_clear (f);
5110
5111 XFlush (FRAME_X_DISPLAY (f));
5112 UNBLOCK_INPUT;
5113 }
5114
5115
5116 \f
5117 /* Invert the middle quarter of the frame for .15 sec. */
5118
5119 /* We use the select system call to do the waiting, so we have to make
5120 sure it's available. If it isn't, we just won't do visual bells. */
5121
5122 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5123
5124
5125 /* Subtract the `struct timeval' values X and Y, storing the result in
5126 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5127
5128 static int
5129 timeval_subtract (result, x, y)
5130 struct timeval *result, x, y;
5131 {
5132 /* Perform the carry for the later subtraction by updating y. This
5133 is safer because on some systems the tv_sec member is unsigned. */
5134 if (x.tv_usec < y.tv_usec)
5135 {
5136 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5137 y.tv_usec -= 1000000 * nsec;
5138 y.tv_sec += nsec;
5139 }
5140
5141 if (x.tv_usec - y.tv_usec > 1000000)
5142 {
5143 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5144 y.tv_usec += 1000000 * nsec;
5145 y.tv_sec -= nsec;
5146 }
5147
5148 /* Compute the time remaining to wait. tv_usec is certainly
5149 positive. */
5150 result->tv_sec = x.tv_sec - y.tv_sec;
5151 result->tv_usec = x.tv_usec - y.tv_usec;
5152
5153 /* Return indication of whether the result should be considered
5154 negative. */
5155 return x.tv_sec < y.tv_sec;
5156 }
5157
5158 void
5159 XTflash (f)
5160 struct frame *f;
5161 {
5162 BLOCK_INPUT;
5163
5164 {
5165 GC gc;
5166
5167 /* Create a GC that will use the GXxor function to flip foreground
5168 pixels into background pixels. */
5169 {
5170 XGCValues values;
5171
5172 values.function = GXxor;
5173 values.foreground = (f->output_data.x->foreground_pixel
5174 ^ f->output_data.x->background_pixel);
5175
5176 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5177 GCFunction | GCForeground, &values);
5178 }
5179
5180 {
5181 /* Get the height not including a menu bar widget. */
5182 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5183 /* Height of each line to flash. */
5184 int flash_height = FRAME_LINE_HEIGHT (f);
5185 /* These will be the left and right margins of the rectangles. */
5186 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5187 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5188
5189 int width;
5190
5191 /* Don't flash the area between a scroll bar and the frame
5192 edge it is next to. */
5193 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5194 {
5195 case vertical_scroll_bar_left:
5196 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5197 break;
5198
5199 case vertical_scroll_bar_right:
5200 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5201 break;
5202
5203 default:
5204 break;
5205 }
5206
5207 width = flash_right - flash_left;
5208
5209 /* If window is tall, flash top and bottom line. */
5210 if (height > 3 * FRAME_LINE_HEIGHT (f))
5211 {
5212 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5213 flash_left,
5214 (FRAME_INTERNAL_BORDER_WIDTH (f)
5215 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5216 width, flash_height);
5217 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5218 flash_left,
5219 (height - flash_height
5220 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5221 width, flash_height);
5222 }
5223 else
5224 /* If it is short, flash it all. */
5225 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5226 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5227 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5228
5229 x_flush (f);
5230
5231 {
5232 struct timeval wakeup;
5233
5234 EMACS_GET_TIME (wakeup);
5235
5236 /* Compute time to wait until, propagating carry from usecs. */
5237 wakeup.tv_usec += 150000;
5238 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5239 wakeup.tv_usec %= 1000000;
5240
5241 /* Keep waiting until past the time wakeup. */
5242 while (1)
5243 {
5244 struct timeval timeout;
5245
5246 EMACS_GET_TIME (timeout);
5247
5248 /* In effect, timeout = wakeup - timeout.
5249 Break if result would be negative. */
5250 if (timeval_subtract (&timeout, wakeup, timeout))
5251 break;
5252
5253 /* Try to wait that long--but we might wake up sooner. */
5254 select (0, NULL, NULL, NULL, &timeout);
5255 }
5256 }
5257
5258 /* If window is tall, flash top and bottom line. */
5259 if (height > 3 * FRAME_LINE_HEIGHT (f))
5260 {
5261 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5262 flash_left,
5263 (FRAME_INTERNAL_BORDER_WIDTH (f)
5264 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5265 width, flash_height);
5266 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5267 flash_left,
5268 (height - flash_height
5269 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5270 width, flash_height);
5271 }
5272 else
5273 /* If it is short, flash it all. */
5274 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5275 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5276 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5277
5278 XFreeGC (FRAME_X_DISPLAY (f), gc);
5279 x_flush (f);
5280 }
5281 }
5282
5283 UNBLOCK_INPUT;
5284 }
5285
5286 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5287
5288
5289 /* Make audible bell. */
5290
5291 void
5292 XTring_bell ()
5293 {
5294 struct frame *f = SELECTED_FRAME ();
5295
5296 if (FRAME_X_DISPLAY (f))
5297 {
5298 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5299 if (visible_bell)
5300 XTflash (f);
5301 else
5302 #endif
5303 {
5304 BLOCK_INPUT;
5305 XBell (FRAME_X_DISPLAY (f), 0);
5306 XFlush (FRAME_X_DISPLAY (f));
5307 UNBLOCK_INPUT;
5308 }
5309 }
5310 }
5311
5312 \f
5313 /* Specify how many text lines, from the top of the window,
5314 should be affected by insert-lines and delete-lines operations.
5315 This, and those operations, are used only within an update
5316 that is bounded by calls to x_update_begin and x_update_end. */
5317
5318 static void
5319 XTset_terminal_window (n)
5320 register int n;
5321 {
5322 /* This function intentionally left blank. */
5323 }
5324
5325
5326 \f
5327 /***********************************************************************
5328 Line Dance
5329 ***********************************************************************/
5330
5331 /* Perform an insert-lines or delete-lines operation, inserting N
5332 lines or deleting -N lines at vertical position VPOS. */
5333
5334 static void
5335 x_ins_del_lines (vpos, n)
5336 int vpos, n;
5337 {
5338 abort ();
5339 }
5340
5341
5342 /* Scroll part of the display as described by RUN. */
5343
5344 static void
5345 x_scroll_run (w, run)
5346 struct window *w;
5347 struct run *run;
5348 {
5349 struct frame *f = XFRAME (w->frame);
5350 int x, y, width, height, from_y, to_y, bottom_y;
5351
5352 /* Get frame-relative bounding box of the text display area of W,
5353 without mode lines. Include in this box the flags areas to the
5354 left and right of W. */
5355 window_box (w, -1, &x, &y, &width, &height);
5356 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5357 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
5358
5359 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5360 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5361 bottom_y = y + height;
5362
5363 if (to_y < from_y)
5364 {
5365 /* Scrolling up. Make sure we don't copy part of the mode
5366 line at the bottom. */
5367 if (from_y + run->height > bottom_y)
5368 height = bottom_y - from_y;
5369 else
5370 height = run->height;
5371 }
5372 else
5373 {
5374 /* Scolling down. Make sure we don't copy over the mode line.
5375 at the bottom. */
5376 if (to_y + run->height > bottom_y)
5377 height = bottom_y - to_y;
5378 else
5379 height = run->height;
5380 }
5381
5382 BLOCK_INPUT;
5383
5384 /* Cursor off. Will be switched on again in x_update_window_end. */
5385 updated_window = w;
5386 x_clear_cursor (w);
5387
5388 XCopyArea (FRAME_X_DISPLAY (f),
5389 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5390 f->output_data.x->normal_gc,
5391 x, from_y,
5392 width, height,
5393 x, to_y);
5394
5395 UNBLOCK_INPUT;
5396 }
5397
5398
5399 \f
5400 /***********************************************************************
5401 Exposure Events
5402 ***********************************************************************/
5403
5404 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5405 corner of the exposed rectangle. W and H are width and height of
5406 the exposed area. All are pixel values. W or H zero means redraw
5407 the entire frame. */
5408
5409 static void
5410 expose_frame (f, x, y, w, h)
5411 struct frame *f;
5412 int x, y, w, h;
5413 {
5414 XRectangle r;
5415
5416 TRACE ((stderr, "expose_frame "));
5417
5418 /* No need to redraw if frame will be redrawn soon. */
5419 if (FRAME_GARBAGED_P (f))
5420 {
5421 TRACE ((stderr, " garbaged\n"));
5422 return;
5423 }
5424
5425 /* If basic faces haven't been realized yet, there is no point in
5426 trying to redraw anything. This can happen when we get an expose
5427 event while Emacs is starting, e.g. by moving another window. */
5428 if (FRAME_FACE_CACHE (f) == NULL
5429 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5430 {
5431 TRACE ((stderr, " no faces\n"));
5432 return;
5433 }
5434
5435 if (w == 0 || h == 0)
5436 {
5437 r.x = r.y = 0;
5438 r.width = CANON_X_UNIT (f) * f->width;
5439 r.height = CANON_Y_UNIT (f) * f->height;
5440 }
5441 else
5442 {
5443 r.x = x;
5444 r.y = y;
5445 r.width = w;
5446 r.height = h;
5447 }
5448
5449 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5450 expose_window_tree (XWINDOW (f->root_window), &r);
5451
5452 if (WINDOWP (f->tool_bar_window))
5453 {
5454 struct window *w = XWINDOW (f->tool_bar_window);
5455 XRectangle window_rect;
5456 XRectangle intersection_rect;
5457 int window_x, window_y, window_width, window_height;
5458
5459
5460 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5461 window_rect.x = window_x;
5462 window_rect.y = window_y;
5463 window_rect.width = window_width;
5464 window_rect.height = window_height;
5465
5466 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5467 expose_window (w, &intersection_rect);
5468 }
5469
5470 #ifndef USE_X_TOOLKIT
5471 if (WINDOWP (f->menu_bar_window))
5472 {
5473 struct window *w = XWINDOW (f->menu_bar_window);
5474 XRectangle window_rect;
5475 XRectangle intersection_rect;
5476 int window_x, window_y, window_width, window_height;
5477
5478
5479 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5480 window_rect.x = window_x;
5481 window_rect.y = window_y;
5482 window_rect.width = window_width;
5483 window_rect.height = window_height;
5484
5485 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5486 expose_window (w, &intersection_rect);
5487 }
5488 #endif /* not USE_X_TOOLKIT */
5489 }
5490
5491
5492 /* Redraw (parts) of all windows in the window tree rooted at W that
5493 intersect R. R contains frame pixel coordinates. */
5494
5495 static void
5496 expose_window_tree (w, r)
5497 struct window *w;
5498 XRectangle *r;
5499 {
5500 while (w)
5501 {
5502 if (!NILP (w->hchild))
5503 expose_window_tree (XWINDOW (w->hchild), r);
5504 else if (!NILP (w->vchild))
5505 expose_window_tree (XWINDOW (w->vchild), r);
5506 else
5507 {
5508 XRectangle window_rect;
5509 XRectangle intersection_rect;
5510 struct frame *f = XFRAME (w->frame);
5511 int window_x, window_y, window_width, window_height;
5512
5513 /* Frame-relative pixel rectangle of W. */
5514 window_box (w, -1, &window_x, &window_y, &window_width,
5515 &window_height);
5516 window_rect.x
5517 = (window_x
5518 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
5519 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5520 window_rect.y = window_y;
5521 window_rect.width
5522 = (window_width
5523 + FRAME_X_FLAGS_AREA_WIDTH (f)
5524 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5525 window_rect.height
5526 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5527
5528 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5529 expose_window (w, &intersection_rect);
5530 }
5531
5532 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5533 }
5534 }
5535
5536
5537 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5538 which intersects rectangle R. R is in window-relative coordinates. */
5539
5540 static void
5541 expose_area (w, row, r, area)
5542 struct window *w;
5543 struct glyph_row *row;
5544 XRectangle *r;
5545 enum glyph_row_area area;
5546 {
5547 int x;
5548 struct glyph *first = row->glyphs[area];
5549 struct glyph *end = row->glyphs[area] + row->used[area];
5550 struct glyph *last;
5551 int first_x;
5552
5553 /* Set x to the window-relative start position for drawing glyphs of
5554 AREA. The first glyph of the text area can be partially visible.
5555 The first glyphs of other areas cannot. */
5556 if (area == LEFT_MARGIN_AREA)
5557 x = 0;
5558 else if (area == TEXT_AREA)
5559 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5560 else
5561 x = (window_box_width (w, LEFT_MARGIN_AREA)
5562 + window_box_width (w, TEXT_AREA));
5563
5564 if (area == TEXT_AREA && row->fill_line_p)
5565 /* If row extends face to end of line write the whole line. */
5566 x_draw_glyphs (w, x, row, area,
5567 0, row->used[area],
5568 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5569 NULL, NULL, 0);
5570 else
5571 {
5572 /* Find the first glyph that must be redrawn. */
5573 while (first < end
5574 && x + first->pixel_width < r->x)
5575 {
5576 x += first->pixel_width;
5577 ++first;
5578 }
5579
5580 /* Find the last one. */
5581 last = first;
5582 first_x = x;
5583 while (last < end
5584 && x < r->x + r->width)
5585 {
5586 x += last->pixel_width;
5587 ++last;
5588 }
5589
5590 /* Repaint. */
5591 if (last > first)
5592 x_draw_glyphs (w, first_x, row, area,
5593 first - row->glyphs[area],
5594 last - row->glyphs[area],
5595 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5596 NULL, NULL, 0);
5597 }
5598 }
5599
5600
5601 /* Redraw the parts of the glyph row ROW on window W intersecting
5602 rectangle R. R is in window-relative coordinates. */
5603
5604 static void
5605 expose_line (w, row, r)
5606 struct window *w;
5607 struct glyph_row *row;
5608 XRectangle *r;
5609 {
5610 xassert (row->enabled_p);
5611
5612 if (row->mode_line_p || w->pseudo_window_p)
5613 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5614 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5615 NULL, NULL, 0);
5616 else
5617 {
5618 if (row->used[LEFT_MARGIN_AREA])
5619 expose_area (w, row, r, LEFT_MARGIN_AREA);
5620 if (row->used[TEXT_AREA])
5621 expose_area (w, row, r, TEXT_AREA);
5622 if (row->used[RIGHT_MARGIN_AREA])
5623 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5624 x_draw_row_bitmaps (w, row);
5625 }
5626 }
5627
5628
5629 /* Return non-zero if W's cursor intersects rectangle R. */
5630
5631 static int
5632 x_phys_cursor_in_rect_p (w, r)
5633 struct window *w;
5634 XRectangle *r;
5635 {
5636 XRectangle cr, result;
5637 struct glyph *cursor_glyph;
5638
5639 cursor_glyph = get_phys_cursor_glyph (w);
5640 if (cursor_glyph)
5641 {
5642 cr.x = w->phys_cursor.x;
5643 cr.y = w->phys_cursor.y;
5644 cr.width = cursor_glyph->pixel_width;
5645 cr.height = w->phys_cursor_height;
5646 return x_intersect_rectangles (&cr, r, &result);
5647 }
5648 else
5649 return 0;
5650 }
5651
5652
5653 /* Redraw a rectangle of window W. R is a rectangle in window
5654 relative coordinates. Call this function with input blocked. */
5655
5656 static void
5657 expose_window (w, r)
5658 struct window *w;
5659 XRectangle *r;
5660 {
5661 struct glyph_row *row;
5662 int y;
5663 int yb = window_text_bottom_y (w);
5664 int cursor_cleared_p;
5665
5666 /* If window is not yet fully initialized, do nothing. This can
5667 happen when toolkit scroll bars are used and a window is split.
5668 Reconfiguring the scroll bar will generate an expose for a newly
5669 created window. */
5670 if (w->current_matrix == NULL)
5671 return;
5672
5673 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5674 r->x, r->y, r->width, r->height));
5675
5676 /* Convert to window coordinates. */
5677 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5678 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
5679
5680 /* Turn off the cursor. */
5681 if (!w->pseudo_window_p
5682 && x_phys_cursor_in_rect_p (w, r))
5683 {
5684 x_clear_cursor (w);
5685 cursor_cleared_p = 1;
5686 }
5687 else
5688 cursor_cleared_p = 0;
5689
5690 /* Find the first row intersecting the rectangle R. */
5691 row = w->current_matrix->rows;
5692 y = 0;
5693 while (row->enabled_p
5694 && y < yb
5695 && y + row->height < r->y)
5696 {
5697 y += row->height;
5698 ++row;
5699 }
5700
5701 /* Display the text in the rectangle, one text line at a time. */
5702 while (row->enabled_p
5703 && y < yb
5704 && y < r->y + r->height)
5705 {
5706 expose_line (w, row, r);
5707 y += row->height;
5708 ++row;
5709 }
5710
5711 /* Display the mode line if there is one. */
5712 if (WINDOW_WANTS_MODELINE_P (w)
5713 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5714 row->enabled_p)
5715 && row->y < r->y + r->height)
5716 expose_line (w, row, r);
5717
5718 if (!w->pseudo_window_p)
5719 {
5720 /* Draw border between windows. */
5721 x_draw_vertical_border (w);
5722
5723 /* Turn the cursor on again. */
5724 if (cursor_cleared_p)
5725 x_update_window_cursor (w, 1);
5726 }
5727 }
5728
5729
5730 /* Determine the intersection of two rectangles R1 and R2. Return
5731 the intersection in *RESULT. Value is non-zero if RESULT is not
5732 empty. */
5733
5734 static int
5735 x_intersect_rectangles (r1, r2, result)
5736 XRectangle *r1, *r2, *result;
5737 {
5738 XRectangle *left, *right;
5739 XRectangle *upper, *lower;
5740 int intersection_p = 0;
5741
5742 /* Rearrange so that R1 is the left-most rectangle. */
5743 if (r1->x < r2->x)
5744 left = r1, right = r2;
5745 else
5746 left = r2, right = r1;
5747
5748 /* X0 of the intersection is right.x0, if this is inside R1,
5749 otherwise there is no intersection. */
5750 if (right->x <= left->x + left->width)
5751 {
5752 result->x = right->x;
5753
5754 /* The right end of the intersection is the minimum of the
5755 the right ends of left and right. */
5756 result->width = (min (left->x + left->width, right->x + right->width)
5757 - result->x);
5758
5759 /* Same game for Y. */
5760 if (r1->y < r2->y)
5761 upper = r1, lower = r2;
5762 else
5763 upper = r2, lower = r1;
5764
5765 /* The upper end of the intersection is lower.y0, if this is inside
5766 of upper. Otherwise, there is no intersection. */
5767 if (lower->y <= upper->y + upper->height)
5768 {
5769 result->y = lower->y;
5770
5771 /* The lower end of the intersection is the minimum of the lower
5772 ends of upper and lower. */
5773 result->height = (min (lower->y + lower->height,
5774 upper->y + upper->height)
5775 - result->y);
5776 intersection_p = 1;
5777 }
5778 }
5779
5780 return intersection_p;
5781 }
5782
5783
5784
5785
5786 \f
5787 static void
5788 frame_highlight (f)
5789 struct frame *f;
5790 {
5791 /* We used to only do this if Vx_no_window_manager was non-nil, but
5792 the ICCCM (section 4.1.6) says that the window's border pixmap
5793 and border pixel are window attributes which are "private to the
5794 client", so we can always change it to whatever we want. */
5795 BLOCK_INPUT;
5796 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5797 f->output_data.x->border_pixel);
5798 UNBLOCK_INPUT;
5799 x_update_cursor (f, 1);
5800 }
5801
5802 static void
5803 frame_unhighlight (f)
5804 struct frame *f;
5805 {
5806 /* We used to only do this if Vx_no_window_manager was non-nil, but
5807 the ICCCM (section 4.1.6) says that the window's border pixmap
5808 and border pixel are window attributes which are "private to the
5809 client", so we can always change it to whatever we want. */
5810 BLOCK_INPUT;
5811 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5812 f->output_data.x->border_tile);
5813 UNBLOCK_INPUT;
5814 x_update_cursor (f, 1);
5815 }
5816
5817 /* The focus has changed. Update the frames as necessary to reflect
5818 the new situation. Note that we can't change the selected frame
5819 here, because the Lisp code we are interrupting might become confused.
5820 Each event gets marked with the frame in which it occurred, so the
5821 Lisp code can tell when the switch took place by examining the events. */
5822
5823 static void
5824 x_new_focus_frame (dpyinfo, frame)
5825 struct x_display_info *dpyinfo;
5826 struct frame *frame;
5827 {
5828 struct frame *old_focus = dpyinfo->x_focus_frame;
5829
5830 if (frame != dpyinfo->x_focus_frame)
5831 {
5832 /* Set this before calling other routines, so that they see
5833 the correct value of x_focus_frame. */
5834 dpyinfo->x_focus_frame = frame;
5835
5836 if (old_focus && old_focus->auto_lower)
5837 x_lower_frame (old_focus);
5838
5839 #if 0
5840 selected_frame = frame;
5841 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5842 selected_frame);
5843 Fselect_window (selected_frame->selected_window);
5844 choose_minibuf_frame ();
5845 #endif /* ! 0 */
5846
5847 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5848 pending_autoraise_frame = dpyinfo->x_focus_frame;
5849 else
5850 pending_autoraise_frame = 0;
5851 }
5852
5853 x_frame_rehighlight (dpyinfo);
5854 }
5855
5856 /* Handle an event saying the mouse has moved out of an Emacs frame. */
5857
5858 void
5859 x_mouse_leave (dpyinfo)
5860 struct x_display_info *dpyinfo;
5861 {
5862 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
5863 }
5864
5865 /* The focus has changed, or we have redirected a frame's focus to
5866 another frame (this happens when a frame uses a surrogate
5867 mini-buffer frame). Shift the highlight as appropriate.
5868
5869 The FRAME argument doesn't necessarily have anything to do with which
5870 frame is being highlighted or un-highlighted; we only use it to find
5871 the appropriate X display info. */
5872
5873 static void
5874 XTframe_rehighlight (frame)
5875 struct frame *frame;
5876 {
5877 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
5878 }
5879
5880 static void
5881 x_frame_rehighlight (dpyinfo)
5882 struct x_display_info *dpyinfo;
5883 {
5884 struct frame *old_highlight = dpyinfo->x_highlight_frame;
5885
5886 if (dpyinfo->x_focus_frame)
5887 {
5888 dpyinfo->x_highlight_frame
5889 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
5890 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
5891 : dpyinfo->x_focus_frame);
5892 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
5893 {
5894 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
5895 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
5896 }
5897 }
5898 else
5899 dpyinfo->x_highlight_frame = 0;
5900
5901 if (dpyinfo->x_highlight_frame != old_highlight)
5902 {
5903 if (old_highlight)
5904 frame_unhighlight (old_highlight);
5905 if (dpyinfo->x_highlight_frame)
5906 frame_highlight (dpyinfo->x_highlight_frame);
5907 }
5908 }
5909
5910
5911 \f
5912 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
5913
5914 /* Initialize mode_switch_bit and modifier_meaning. */
5915 static void
5916 x_find_modifier_meanings (dpyinfo)
5917 struct x_display_info *dpyinfo;
5918 {
5919 int min_code, max_code;
5920 KeySym *syms;
5921 int syms_per_code;
5922 XModifierKeymap *mods;
5923
5924 dpyinfo->meta_mod_mask = 0;
5925 dpyinfo->shift_lock_mask = 0;
5926 dpyinfo->alt_mod_mask = 0;
5927 dpyinfo->super_mod_mask = 0;
5928 dpyinfo->hyper_mod_mask = 0;
5929
5930 #ifdef HAVE_X11R4
5931 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
5932 #else
5933 min_code = dpyinfo->display->min_keycode;
5934 max_code = dpyinfo->display->max_keycode;
5935 #endif
5936
5937 syms = XGetKeyboardMapping (dpyinfo->display,
5938 min_code, max_code - min_code + 1,
5939 &syms_per_code);
5940 mods = XGetModifierMapping (dpyinfo->display);
5941
5942 /* Scan the modifier table to see which modifier bits the Meta and
5943 Alt keysyms are on. */
5944 {
5945 int row, col; /* The row and column in the modifier table. */
5946
5947 for (row = 3; row < 8; row++)
5948 for (col = 0; col < mods->max_keypermod; col++)
5949 {
5950 KeyCode code
5951 = mods->modifiermap[(row * mods->max_keypermod) + col];
5952
5953 /* Zeroes are used for filler. Skip them. */
5954 if (code == 0)
5955 continue;
5956
5957 /* Are any of this keycode's keysyms a meta key? */
5958 {
5959 int code_col;
5960
5961 for (code_col = 0; code_col < syms_per_code; code_col++)
5962 {
5963 int sym = syms[((code - min_code) * syms_per_code) + code_col];
5964
5965 switch (sym)
5966 {
5967 case XK_Meta_L:
5968 case XK_Meta_R:
5969 dpyinfo->meta_mod_mask |= (1 << row);
5970 break;
5971
5972 case XK_Alt_L:
5973 case XK_Alt_R:
5974 dpyinfo->alt_mod_mask |= (1 << row);
5975 break;
5976
5977 case XK_Hyper_L:
5978 case XK_Hyper_R:
5979 dpyinfo->hyper_mod_mask |= (1 << row);
5980 break;
5981
5982 case XK_Super_L:
5983 case XK_Super_R:
5984 dpyinfo->super_mod_mask |= (1 << row);
5985 break;
5986
5987 case XK_Shift_Lock:
5988 /* Ignore this if it's not on the lock modifier. */
5989 if ((1 << row) == LockMask)
5990 dpyinfo->shift_lock_mask = LockMask;
5991 break;
5992 }
5993 }
5994 }
5995 }
5996 }
5997
5998 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
5999 if (! dpyinfo->meta_mod_mask)
6000 {
6001 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6002 dpyinfo->alt_mod_mask = 0;
6003 }
6004
6005 /* If some keys are both alt and meta,
6006 make them just meta, not alt. */
6007 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6008 {
6009 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6010 }
6011
6012 XFree ((char *) syms);
6013 XFreeModifiermap (mods);
6014 }
6015
6016 /* Convert between the modifier bits X uses and the modifier bits
6017 Emacs uses. */
6018
6019 static unsigned int
6020 x_x_to_emacs_modifiers (dpyinfo, state)
6021 struct x_display_info *dpyinfo;
6022 unsigned int state;
6023 {
6024 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6025 | ((state & ControlMask) ? ctrl_modifier : 0)
6026 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6027 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6028 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6029 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
6030 }
6031
6032 static unsigned int
6033 x_emacs_to_x_modifiers (dpyinfo, state)
6034 struct x_display_info *dpyinfo;
6035 unsigned int state;
6036 {
6037 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6038 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6039 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6040 | ((state & shift_modifier) ? ShiftMask : 0)
6041 | ((state & ctrl_modifier) ? ControlMask : 0)
6042 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
6043 }
6044
6045 /* Convert a keysym to its name. */
6046
6047 char *
6048 x_get_keysym_name (keysym)
6049 KeySym keysym;
6050 {
6051 char *value;
6052
6053 BLOCK_INPUT;
6054 value = XKeysymToString (keysym);
6055 UNBLOCK_INPUT;
6056
6057 return value;
6058 }
6059
6060
6061 \f
6062 /* Mouse clicks and mouse movement. Rah. */
6063
6064 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6065 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6066 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6067 not force the value into range. */
6068
6069 void
6070 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6071 FRAME_PTR f;
6072 register int pix_x, pix_y;
6073 register int *x, *y;
6074 XRectangle *bounds;
6075 int noclip;
6076 {
6077 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6078 even for negative values. */
6079 if (pix_x < 0)
6080 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6081 if (pix_y < 0)
6082 pix_y -= (f)->output_data.x->line_height - 1;
6083
6084 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6085 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6086
6087 if (bounds)
6088 {
6089 bounds->width = FONT_WIDTH (f->output_data.x->font);
6090 bounds->height = f->output_data.x->line_height;
6091 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6092 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6093 }
6094
6095 if (!noclip)
6096 {
6097 if (pix_x < 0)
6098 pix_x = 0;
6099 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6100 pix_x = FRAME_WINDOW_WIDTH (f);
6101
6102 if (pix_y < 0)
6103 pix_y = 0;
6104 else if (pix_y > f->height)
6105 pix_y = f->height;
6106 }
6107
6108 *x = pix_x;
6109 *y = pix_y;
6110 }
6111
6112
6113 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6114 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6115 can't tell the positions because W's display is not up to date,
6116 return 0. */
6117
6118 int
6119 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6120 struct window *w;
6121 int hpos, vpos;
6122 int *frame_x, *frame_y;
6123 {
6124 int success_p;
6125
6126 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6127 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6128
6129 if (display_completed)
6130 {
6131 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6132 struct glyph *glyph = row->glyphs[TEXT_AREA];
6133 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6134
6135 *frame_y = row->y;
6136 *frame_x = row->x;
6137 while (glyph < end)
6138 {
6139 *frame_x += glyph->pixel_width;
6140 ++glyph;
6141 }
6142
6143 success_p = 1;
6144 }
6145 else
6146 {
6147 *frame_y = *frame_x = 0;
6148 success_p = 0;
6149 }
6150
6151 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6152 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6153 return success_p;
6154 }
6155
6156
6157 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6158
6159 If the event is a button press, then note that we have grabbed
6160 the mouse. */
6161
6162 static Lisp_Object
6163 construct_mouse_click (result, event, f)
6164 struct input_event *result;
6165 XButtonEvent *event;
6166 struct frame *f;
6167 {
6168 /* Make the event type no_event; we'll change that when we decide
6169 otherwise. */
6170 result->kind = mouse_click;
6171 result->code = event->button - Button1;
6172 result->timestamp = event->time;
6173 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6174 event->state)
6175 | (event->type == ButtonRelease
6176 ? up_modifier
6177 : down_modifier));
6178
6179 XSETINT (result->x, event->x);
6180 XSETINT (result->y, event->y);
6181 XSETFRAME (result->frame_or_window, f);
6182 result->arg = Qnil;
6183 return Qnil;
6184 }
6185
6186 \f
6187 /* Function to report a mouse movement to the mainstream Emacs code.
6188 The input handler calls this.
6189
6190 We have received a mouse movement event, which is given in *event.
6191 If the mouse is over a different glyph than it was last time, tell
6192 the mainstream emacs code by setting mouse_moved. If not, ask for
6193 another motion event, so we can check again the next time it moves. */
6194
6195 static XMotionEvent last_mouse_motion_event;
6196 static Lisp_Object last_mouse_motion_frame;
6197
6198 static void
6199 note_mouse_movement (frame, event)
6200 FRAME_PTR frame;
6201 XMotionEvent *event;
6202 {
6203 last_mouse_movement_time = event->time;
6204 last_mouse_motion_event = *event;
6205 XSETFRAME (last_mouse_motion_frame, frame);
6206
6207 if (event->window != FRAME_X_WINDOW (frame))
6208 {
6209 frame->mouse_moved = 1;
6210 last_mouse_scroll_bar = Qnil;
6211 note_mouse_highlight (frame, -1, -1);
6212 }
6213
6214 /* Has the mouse moved off the glyph it was on at the last sighting? */
6215 else if (event->x < last_mouse_glyph.x
6216 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6217 || event->y < last_mouse_glyph.y
6218 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6219 {
6220 frame->mouse_moved = 1;
6221 last_mouse_scroll_bar = Qnil;
6222 note_mouse_highlight (frame, event->x, event->y);
6223 }
6224 }
6225
6226 /* This is used for debugging, to turn off note_mouse_highlight. */
6227
6228 int disable_mouse_highlight;
6229
6230
6231 \f
6232 /************************************************************************
6233 Mouse Face
6234 ************************************************************************/
6235
6236 /* Find the glyph under window-relative coordinates X/Y in window W.
6237 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6238 strings. Return in *HPOS and *VPOS the row and column number of
6239 the glyph found. Return in *AREA the glyph area containing X.
6240 Value is a pointer to the glyph found or null if X/Y is not on
6241 text, or we can't tell because W's current matrix is not up to
6242 date. */
6243
6244 static struct glyph *
6245 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6246 struct window *w;
6247 int x, y;
6248 int *hpos, *vpos, *area;
6249 {
6250 struct glyph *glyph, *end;
6251 struct glyph_row *row = NULL;
6252 int x0, i, left_area_width;
6253
6254 /* Find row containing Y. Give up if some row is not enabled. */
6255 for (i = 0; i < w->current_matrix->nrows; ++i)
6256 {
6257 row = MATRIX_ROW (w->current_matrix, i);
6258 if (!row->enabled_p)
6259 return NULL;
6260 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6261 break;
6262 }
6263
6264 *vpos = i;
6265 *hpos = 0;
6266
6267 /* Give up if Y is not in the window. */
6268 if (i == w->current_matrix->nrows)
6269 return NULL;
6270
6271 /* Get the glyph area containing X. */
6272 if (w->pseudo_window_p)
6273 {
6274 *area = TEXT_AREA;
6275 x0 = 0;
6276 }
6277 else
6278 {
6279 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6280 if (x < left_area_width)
6281 {
6282 *area = LEFT_MARGIN_AREA;
6283 x0 = 0;
6284 }
6285 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6286 {
6287 *area = TEXT_AREA;
6288 x0 = row->x + left_area_width;
6289 }
6290 else
6291 {
6292 *area = RIGHT_MARGIN_AREA;
6293 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6294 }
6295 }
6296
6297 /* Find glyph containing X. */
6298 glyph = row->glyphs[*area];
6299 end = glyph + row->used[*area];
6300 while (glyph < end)
6301 {
6302 if (x < x0 + glyph->pixel_width)
6303 {
6304 if (w->pseudo_window_p)
6305 break;
6306 else if (BUFFERP (glyph->object))
6307 break;
6308 }
6309
6310 x0 += glyph->pixel_width;
6311 ++glyph;
6312 }
6313
6314 if (glyph == end)
6315 return NULL;
6316
6317 *hpos = glyph - row->glyphs[*area];
6318 return glyph;
6319 }
6320
6321
6322 /* Convert frame-relative x/y to coordinates relative to window W.
6323 Takes pseudo-windows into account. */
6324
6325 static void
6326 frame_to_window_pixel_xy (w, x, y)
6327 struct window *w;
6328 int *x, *y;
6329 {
6330 if (w->pseudo_window_p)
6331 {
6332 /* A pseudo-window is always full-width, and starts at the
6333 left edge of the frame, plus a frame border. */
6334 struct frame *f = XFRAME (w->frame);
6335 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6336 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6337 }
6338 else
6339 {
6340 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6341 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6342 }
6343 }
6344
6345
6346 /* Take proper action when mouse has moved to the mode or top line of
6347 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6348 mode line. X is relative to the start of the text display area of
6349 W, so the width of bitmap areas and scroll bars must be subtracted
6350 to get a position relative to the start of the mode line. */
6351
6352 static void
6353 note_mode_line_highlight (w, x, mode_line_p)
6354 struct window *w;
6355 int x, mode_line_p;
6356 {
6357 struct frame *f = XFRAME (w->frame);
6358 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6359 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6360 struct glyph_row *row;
6361
6362 if (mode_line_p)
6363 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6364 else
6365 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6366
6367 if (row->enabled_p)
6368 {
6369 struct glyph *glyph, *end;
6370 Lisp_Object help, map;
6371 int x0;
6372
6373 /* Find the glyph under X. */
6374 glyph = row->glyphs[TEXT_AREA];
6375 end = glyph + row->used[TEXT_AREA];
6376 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6377 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
6378 while (glyph < end
6379 && x >= x0 + glyph->pixel_width)
6380 {
6381 x0 += glyph->pixel_width;
6382 ++glyph;
6383 }
6384
6385 if (glyph < end
6386 && STRINGP (glyph->object)
6387 && XSTRING (glyph->object)->intervals
6388 && glyph->charpos >= 0
6389 && glyph->charpos < XSTRING (glyph->object)->size)
6390 {
6391 /* If we're on a string with `help-echo' text property,
6392 arrange for the help to be displayed. This is done by
6393 setting the global variable help_echo to the help string. */
6394 help = Fget_text_property (make_number (glyph->charpos),
6395 Qhelp_echo, glyph->object);
6396 if (!NILP (help))
6397 {
6398 help_echo = help;
6399 XSETWINDOW (help_echo_window, w);
6400 help_echo_object = glyph->object;
6401 help_echo_pos = glyph->charpos;
6402 }
6403
6404 /* Change the mouse pointer according to what is under X/Y. */
6405 map = Fget_text_property (make_number (glyph->charpos),
6406 Qlocal_map, glyph->object);
6407 if (!NILP (Fkeymapp (map)))
6408 cursor = f->output_data.x->nontext_cursor;
6409 else
6410 {
6411 map = Fget_text_property (make_number (glyph->charpos),
6412 Qkeymap, glyph->object);
6413 if (!NILP (Fkeymapp (map)))
6414 cursor = f->output_data.x->nontext_cursor;
6415 }
6416 }
6417 }
6418
6419 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6420 }
6421
6422
6423 /* Take proper action when the mouse has moved to position X, Y on
6424 frame F as regards highlighting characters that have mouse-face
6425 properties. Also de-highlighting chars where the mouse was before.
6426 X and Y can be negative or out of range. */
6427
6428 static void
6429 note_mouse_highlight (f, x, y)
6430 struct frame *f;
6431 int x, y;
6432 {
6433 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6434 int portion;
6435 Lisp_Object window;
6436 struct window *w;
6437
6438 /* When a menu is active, don't highlight because this looks odd. */
6439 #ifdef USE_X_TOOLKIT
6440 if (popup_activated ())
6441 return;
6442 #endif
6443
6444 if (disable_mouse_highlight
6445 || !f->glyphs_initialized_p)
6446 return;
6447
6448 dpyinfo->mouse_face_mouse_x = x;
6449 dpyinfo->mouse_face_mouse_y = y;
6450 dpyinfo->mouse_face_mouse_frame = f;
6451
6452 if (dpyinfo->mouse_face_defer)
6453 return;
6454
6455 if (gc_in_progress)
6456 {
6457 dpyinfo->mouse_face_deferred_gc = 1;
6458 return;
6459 }
6460
6461 /* Which window is that in? */
6462 window = window_from_coordinates (f, x, y, &portion, 1);
6463
6464 /* If we were displaying active text in another window, clear that. */
6465 if (! EQ (window, dpyinfo->mouse_face_window))
6466 clear_mouse_face (dpyinfo);
6467
6468 /* Not on a window -> return. */
6469 if (!WINDOWP (window))
6470 return;
6471
6472 /* Convert to window-relative pixel coordinates. */
6473 w = XWINDOW (window);
6474 frame_to_window_pixel_xy (w, &x, &y);
6475
6476 /* Handle tool-bar window differently since it doesn't display a
6477 buffer. */
6478 if (EQ (window, f->tool_bar_window))
6479 {
6480 note_tool_bar_highlight (f, x, y);
6481 return;
6482 }
6483
6484 if (portion == 1 || portion == 3)
6485 {
6486 /* Mouse is on the mode or top line. */
6487 note_mode_line_highlight (w, x, portion == 1);
6488 return;
6489 }
6490 else
6491 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6492 f->output_data.x->text_cursor);
6493
6494 /* Are we in a window whose display is up to date?
6495 And verify the buffer's text has not changed. */
6496 if (/* Within text portion of the window. */
6497 portion == 0
6498 && EQ (w->window_end_valid, w->buffer)
6499 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6500 && (XFASTINT (w->last_overlay_modified)
6501 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
6502 {
6503 int hpos, vpos, pos, i, area;
6504 struct glyph *glyph;
6505
6506 /* Find the glyph under X/Y. */
6507 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6508
6509 /* Clear mouse face if X/Y not over text. */
6510 if (glyph == NULL
6511 || area != TEXT_AREA
6512 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6513 {
6514 clear_mouse_face (dpyinfo);
6515 return;
6516 }
6517
6518 pos = glyph->charpos;
6519 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6520
6521 /* Check for mouse-face and help-echo. */
6522 {
6523 Lisp_Object mouse_face, overlay, position;
6524 Lisp_Object *overlay_vec;
6525 int len, noverlays;
6526 struct buffer *obuf;
6527 int obegv, ozv;
6528
6529 /* If we get an out-of-range value, return now; avoid an error. */
6530 if (pos > BUF_Z (XBUFFER (w->buffer)))
6531 return;
6532
6533 /* Make the window's buffer temporarily current for
6534 overlays_at and compute_char_face. */
6535 obuf = current_buffer;
6536 current_buffer = XBUFFER (w->buffer);
6537 obegv = BEGV;
6538 ozv = ZV;
6539 BEGV = BEG;
6540 ZV = Z;
6541
6542 /* Is this char mouse-active or does it have help-echo? */
6543 XSETINT (position, pos);
6544
6545 /* Put all the overlays we want in a vector in overlay_vec.
6546 Store the length in len. If there are more than 10, make
6547 enough space for all, and try again. */
6548 len = 10;
6549 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6550 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6551 if (noverlays > len)
6552 {
6553 len = noverlays;
6554 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6555 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6556 }
6557
6558 /* Sort overlays into increasing priority order. */
6559 noverlays = sort_overlays (overlay_vec, noverlays, w);
6560
6561 /* Check mouse-face highlighting. */
6562 if (! (EQ (window, dpyinfo->mouse_face_window)
6563 && vpos >= dpyinfo->mouse_face_beg_row
6564 && vpos <= dpyinfo->mouse_face_end_row
6565 && (vpos > dpyinfo->mouse_face_beg_row
6566 || hpos >= dpyinfo->mouse_face_beg_col)
6567 && (vpos < dpyinfo->mouse_face_end_row
6568 || hpos < dpyinfo->mouse_face_end_col
6569 || dpyinfo->mouse_face_past_end)))
6570 {
6571 /* Clear the display of the old active region, if any. */
6572 clear_mouse_face (dpyinfo);
6573
6574 /* Find the highest priority overlay that has a mouse-face prop. */
6575 overlay = Qnil;
6576 for (i = noverlays - 1; i >= 0; --i)
6577 {
6578 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6579 if (!NILP (mouse_face))
6580 {
6581 overlay = overlay_vec[i];
6582 break;
6583 }
6584 }
6585
6586 /* If no overlay applies, get a text property. */
6587 if (NILP (overlay))
6588 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6589
6590 /* Handle the overlay case. */
6591 if (! NILP (overlay))
6592 {
6593 /* Find the range of text around this char that
6594 should be active. */
6595 Lisp_Object before, after;
6596 int ignore;
6597
6598 before = Foverlay_start (overlay);
6599 after = Foverlay_end (overlay);
6600 /* Record this as the current active region. */
6601 fast_find_position (w, XFASTINT (before),
6602 &dpyinfo->mouse_face_beg_col,
6603 &dpyinfo->mouse_face_beg_row,
6604 &dpyinfo->mouse_face_beg_x,
6605 &dpyinfo->mouse_face_beg_y);
6606 dpyinfo->mouse_face_past_end
6607 = !fast_find_position (w, XFASTINT (after),
6608 &dpyinfo->mouse_face_end_col,
6609 &dpyinfo->mouse_face_end_row,
6610 &dpyinfo->mouse_face_end_x,
6611 &dpyinfo->mouse_face_end_y);
6612 dpyinfo->mouse_face_window = window;
6613 dpyinfo->mouse_face_face_id
6614 = face_at_buffer_position (w, pos, 0, 0,
6615 &ignore, pos + 1, 1);
6616
6617 /* Display it as active. */
6618 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6619 }
6620 /* Handle the text property case. */
6621 else if (! NILP (mouse_face))
6622 {
6623 /* Find the range of text around this char that
6624 should be active. */
6625 Lisp_Object before, after, beginning, end;
6626 int ignore;
6627
6628 beginning = Fmarker_position (w->start);
6629 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6630 - XFASTINT (w->window_end_pos)));
6631 before
6632 = Fprevious_single_property_change (make_number (pos + 1),
6633 Qmouse_face,
6634 w->buffer, beginning);
6635 after
6636 = Fnext_single_property_change (position, Qmouse_face,
6637 w->buffer, end);
6638 /* Record this as the current active region. */
6639 fast_find_position (w, XFASTINT (before),
6640 &dpyinfo->mouse_face_beg_col,
6641 &dpyinfo->mouse_face_beg_row,
6642 &dpyinfo->mouse_face_beg_x,
6643 &dpyinfo->mouse_face_beg_y);
6644 dpyinfo->mouse_face_past_end
6645 = !fast_find_position (w, XFASTINT (after),
6646 &dpyinfo->mouse_face_end_col,
6647 &dpyinfo->mouse_face_end_row,
6648 &dpyinfo->mouse_face_end_x,
6649 &dpyinfo->mouse_face_end_y);
6650 dpyinfo->mouse_face_window = window;
6651 dpyinfo->mouse_face_face_id
6652 = face_at_buffer_position (w, pos, 0, 0,
6653 &ignore, pos + 1, 1);
6654
6655 /* Display it as active. */
6656 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6657 }
6658 }
6659
6660 /* Look for a `help-echo' property. */
6661 {
6662 Lisp_Object help, overlay;
6663
6664 /* Check overlays first. */
6665 help = Qnil;
6666 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
6667 {
6668 overlay = overlay_vec[i];
6669 help = Foverlay_get (overlay, Qhelp_echo);
6670 }
6671
6672 if (!NILP (help))
6673 {
6674 help_echo = help;
6675 help_echo_window = window;
6676 help_echo_object = overlay;
6677 help_echo_pos = pos;
6678 }
6679 else
6680 {
6681 /* Try text properties. */
6682 if ((STRINGP (glyph->object)
6683 && glyph->charpos >= 0
6684 && glyph->charpos < XSTRING (glyph->object)->size)
6685 || (BUFFERP (glyph->object)
6686 && glyph->charpos >= BEGV
6687 && glyph->charpos < ZV))
6688 help = Fget_text_property (make_number (glyph->charpos),
6689 Qhelp_echo, glyph->object);
6690
6691 if (!NILP (help))
6692 {
6693 help_echo = help;
6694 help_echo_window = window;
6695 help_echo_object = glyph->object;
6696 help_echo_pos = glyph->charpos;
6697 }
6698 }
6699 }
6700
6701 BEGV = obegv;
6702 ZV = ozv;
6703 current_buffer = obuf;
6704 }
6705 }
6706 }
6707
6708 static void
6709 redo_mouse_highlight ()
6710 {
6711 if (!NILP (last_mouse_motion_frame)
6712 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6713 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6714 last_mouse_motion_event.x,
6715 last_mouse_motion_event.y);
6716 }
6717
6718
6719 \f
6720 /***********************************************************************
6721 Tool-bars
6722 ***********************************************************************/
6723
6724 static int x_tool_bar_item P_ ((struct frame *, int, int,
6725 struct glyph **, int *, int *, int *));
6726
6727 /* Tool-bar item index of the item on which a mouse button was pressed
6728 or -1. */
6729
6730 static int last_tool_bar_item;
6731
6732
6733 /* Get information about the tool-bar item at position X/Y on frame F.
6734 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6735 the current matrix of the tool-bar window of F, or NULL if not
6736 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6737 item in F->current_tool_bar_items. Value is
6738
6739 -1 if X/Y is not on a tool-bar item
6740 0 if X/Y is on the same item that was highlighted before.
6741 1 otherwise. */
6742
6743 static int
6744 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
6745 struct frame *f;
6746 int x, y;
6747 struct glyph **glyph;
6748 int *hpos, *vpos, *prop_idx;
6749 {
6750 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6751 struct window *w = XWINDOW (f->tool_bar_window);
6752 int area;
6753
6754 /* Find the glyph under X/Y. */
6755 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6756 if (*glyph == NULL)
6757 return -1;
6758
6759 /* Get the start of this tool-bar item's properties in
6760 f->current_tool_bar_items. */
6761 if (!tool_bar_item_info (f, *glyph, prop_idx))
6762 return -1;
6763
6764 /* Is mouse on the highlighted item? */
6765 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
6766 && *vpos >= dpyinfo->mouse_face_beg_row
6767 && *vpos <= dpyinfo->mouse_face_end_row
6768 && (*vpos > dpyinfo->mouse_face_beg_row
6769 || *hpos >= dpyinfo->mouse_face_beg_col)
6770 && (*vpos < dpyinfo->mouse_face_end_row
6771 || *hpos < dpyinfo->mouse_face_end_col
6772 || dpyinfo->mouse_face_past_end))
6773 return 0;
6774
6775 return 1;
6776 }
6777
6778
6779 /* Handle mouse button event on the tool-bar of frame F, at
6780 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6781 or ButtonRelase. */
6782
6783 static void
6784 x_handle_tool_bar_click (f, button_event)
6785 struct frame *f;
6786 XButtonEvent *button_event;
6787 {
6788 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6789 struct window *w = XWINDOW (f->tool_bar_window);
6790 int hpos, vpos, prop_idx;
6791 struct glyph *glyph;
6792 Lisp_Object enabled_p;
6793 int x = button_event->x;
6794 int y = button_event->y;
6795
6796 /* If not on the highlighted tool-bar item, return. */
6797 frame_to_window_pixel_xy (w, &x, &y);
6798 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
6799 return;
6800
6801 /* If item is disabled, do nothing. */
6802 enabled_p = (XVECTOR (f->current_tool_bar_items)
6803 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
6804 if (NILP (enabled_p))
6805 return;
6806
6807 if (button_event->type == ButtonPress)
6808 {
6809 /* Show item in pressed state. */
6810 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6811 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
6812 last_tool_bar_item = prop_idx;
6813 }
6814 else
6815 {
6816 Lisp_Object key, frame;
6817 struct input_event event;
6818
6819 /* Show item in released state. */
6820 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6821 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6822
6823 key = (XVECTOR (f->current_tool_bar_items)
6824 ->contents[prop_idx + TOOL_BAR_ITEM_KEY]);
6825
6826 XSETFRAME (frame, f);
6827 event.kind = TOOL_BAR_EVENT;
6828 event.frame_or_window = frame;
6829 event.arg = frame;
6830 kbd_buffer_store_event (&event);
6831
6832 event.kind = TOOL_BAR_EVENT;
6833 event.frame_or_window = frame;
6834 event.arg = key;
6835 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6836 button_event->state);
6837 kbd_buffer_store_event (&event);
6838 last_tool_bar_item = -1;
6839 }
6840 }
6841
6842
6843 /* Possibly highlight a tool-bar item on frame F when mouse moves to
6844 tool-bar window-relative coordinates X/Y. Called from
6845 note_mouse_highlight. */
6846
6847 static void
6848 note_tool_bar_highlight (f, x, y)
6849 struct frame *f;
6850 int x, y;
6851 {
6852 Lisp_Object window = f->tool_bar_window;
6853 struct window *w = XWINDOW (window);
6854 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6855 int hpos, vpos;
6856 struct glyph *glyph;
6857 struct glyph_row *row;
6858 int i;
6859 Lisp_Object enabled_p;
6860 int prop_idx;
6861 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
6862 int mouse_down_p, rc;
6863
6864 /* Function note_mouse_highlight is called with negative x(y
6865 values when mouse moves outside of the frame. */
6866 if (x <= 0 || y <= 0)
6867 {
6868 clear_mouse_face (dpyinfo);
6869 return;
6870 }
6871
6872 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
6873 if (rc < 0)
6874 {
6875 /* Not on tool-bar item. */
6876 clear_mouse_face (dpyinfo);
6877 return;
6878 }
6879 else if (rc == 0)
6880 /* On same tool-bar item as before. */
6881 goto set_help_echo;
6882
6883 clear_mouse_face (dpyinfo);
6884
6885 /* Mouse is down, but on different tool-bar item? */
6886 mouse_down_p = (dpyinfo->grabbed
6887 && f == last_mouse_frame
6888 && FRAME_LIVE_P (f));
6889 if (mouse_down_p
6890 && last_tool_bar_item != prop_idx)
6891 return;
6892
6893 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6894 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6895
6896 /* If tool-bar item is not enabled, don't highlight it. */
6897 enabled_p = (XVECTOR (f->current_tool_bar_items)
6898 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
6899 if (!NILP (enabled_p))
6900 {
6901 /* Compute the x-position of the glyph. In front and past the
6902 image is a space. We include this is the highlighted area. */
6903 row = MATRIX_ROW (w->current_matrix, vpos);
6904 for (i = x = 0; i < hpos; ++i)
6905 x += row->glyphs[TEXT_AREA][i].pixel_width;
6906
6907 /* Record this as the current active region. */
6908 dpyinfo->mouse_face_beg_col = hpos;
6909 dpyinfo->mouse_face_beg_row = vpos;
6910 dpyinfo->mouse_face_beg_x = x;
6911 dpyinfo->mouse_face_beg_y = row->y;
6912 dpyinfo->mouse_face_past_end = 0;
6913
6914 dpyinfo->mouse_face_end_col = hpos + 1;
6915 dpyinfo->mouse_face_end_row = vpos;
6916 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6917 dpyinfo->mouse_face_end_y = row->y;
6918 dpyinfo->mouse_face_window = window;
6919 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
6920
6921 /* Display it as active. */
6922 show_mouse_face (dpyinfo, draw);
6923 dpyinfo->mouse_face_image_state = draw;
6924 }
6925
6926 set_help_echo:
6927
6928 /* Set help_echo to a help string.to display for this tool-bar item.
6929 XTread_socket does the rest. */
6930 help_echo_object = help_echo_window = Qnil;
6931 help_echo_pos = -1;
6932 help_echo = (XVECTOR (f->current_tool_bar_items)
6933 ->contents[prop_idx + TOOL_BAR_ITEM_HELP]);
6934 if (NILP (help_echo))
6935 help_echo = (XVECTOR (f->current_tool_bar_items)
6936 ->contents[prop_idx + TOOL_BAR_ITEM_CAPTION]);
6937 }
6938
6939
6940 \f
6941 /* Find the glyph matrix position of buffer position POS in window W.
6942 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6943 current glyphs must be up to date. If POS is above window start
6944 return (0, 0, 0, 0). If POS is after end of W, return end of
6945 last line in W. */
6946
6947 static int
6948 fast_find_position (w, pos, hpos, vpos, x, y)
6949 struct window *w;
6950 int pos;
6951 int *hpos, *vpos, *x, *y;
6952 {
6953 int i;
6954 int lastcol;
6955 int maybe_next_line_p = 0;
6956 int line_start_position;
6957 int yb = window_text_bottom_y (w);
6958 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6959 struct glyph_row *best_row = row;
6960 int row_vpos = 0, best_row_vpos = 0;
6961 int current_x;
6962
6963 while (row->y < yb)
6964 {
6965 if (row->used[TEXT_AREA])
6966 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6967 else
6968 line_start_position = 0;
6969
6970 if (line_start_position > pos)
6971 break;
6972 /* If the position sought is the end of the buffer,
6973 don't include the blank lines at the bottom of the window. */
6974 else if (line_start_position == pos
6975 && pos == BUF_ZV (XBUFFER (w->buffer)))
6976 {
6977 maybe_next_line_p = 1;
6978 break;
6979 }
6980 else if (line_start_position > 0)
6981 {
6982 best_row = row;
6983 best_row_vpos = row_vpos;
6984 }
6985
6986 if (row->y + row->height >= yb)
6987 break;
6988
6989 ++row;
6990 ++row_vpos;
6991 }
6992
6993 /* Find the right column within BEST_ROW. */
6994 lastcol = 0;
6995 current_x = best_row->x;
6996 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
6997 {
6998 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6999 int charpos;
7000
7001 charpos = glyph->charpos;
7002 if (charpos == pos)
7003 {
7004 *hpos = i;
7005 *vpos = best_row_vpos;
7006 *x = current_x;
7007 *y = best_row->y;
7008 return 1;
7009 }
7010 else if (charpos > pos)
7011 break;
7012 else if (charpos > 0)
7013 lastcol = i;
7014
7015 current_x += glyph->pixel_width;
7016 }
7017
7018 /* If we're looking for the end of the buffer,
7019 and we didn't find it in the line we scanned,
7020 use the start of the following line. */
7021 if (maybe_next_line_p)
7022 {
7023 ++best_row;
7024 ++best_row_vpos;
7025 lastcol = 0;
7026 current_x = best_row->x;
7027 }
7028
7029 *vpos = best_row_vpos;
7030 *hpos = lastcol + 1;
7031 *x = current_x;
7032 *y = best_row->y;
7033 return 0;
7034 }
7035
7036
7037 /* Display the active region described by mouse_face_*
7038 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7039
7040 static void
7041 show_mouse_face (dpyinfo, draw)
7042 struct x_display_info *dpyinfo;
7043 enum draw_glyphs_face draw;
7044 {
7045 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7046 struct frame *f = XFRAME (WINDOW_FRAME (w));
7047 int i;
7048 int cursor_off_p = 0;
7049 struct cursor_pos saved_cursor;
7050
7051 saved_cursor = output_cursor;
7052
7053 /* If window is in the process of being destroyed, don't bother
7054 to do anything. */
7055 if (w->current_matrix == NULL)
7056 goto set_x_cursor;
7057
7058 /* Recognize when we are called to operate on rows that don't exist
7059 anymore. This can happen when a window is split. */
7060 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7061 goto set_x_cursor;
7062
7063 set_output_cursor (&w->phys_cursor);
7064
7065 /* Note that mouse_face_beg_row etc. are window relative. */
7066 for (i = dpyinfo->mouse_face_beg_row;
7067 i <= dpyinfo->mouse_face_end_row;
7068 i++)
7069 {
7070 int start_hpos, end_hpos, start_x;
7071 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7072
7073 /* Don't do anything if row doesn't have valid contents. */
7074 if (!row->enabled_p)
7075 continue;
7076
7077 /* For all but the first row, the highlight starts at column 0. */
7078 if (i == dpyinfo->mouse_face_beg_row)
7079 {
7080 start_hpos = dpyinfo->mouse_face_beg_col;
7081 start_x = dpyinfo->mouse_face_beg_x;
7082 }
7083 else
7084 {
7085 start_hpos = 0;
7086 start_x = 0;
7087 }
7088
7089 if (i == dpyinfo->mouse_face_end_row)
7090 end_hpos = dpyinfo->mouse_face_end_col;
7091 else
7092 end_hpos = row->used[TEXT_AREA];
7093
7094 /* If the cursor's in the text we are about to rewrite, turn the
7095 cursor off. */
7096 if (!w->pseudo_window_p
7097 && i == output_cursor.vpos
7098 && output_cursor.hpos >= start_hpos - 1
7099 && output_cursor.hpos <= end_hpos)
7100 {
7101 x_update_window_cursor (w, 0);
7102 cursor_off_p = 1;
7103 }
7104
7105 if (end_hpos > start_hpos)
7106 {
7107 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
7108 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7109 start_hpos, end_hpos, draw, NULL, NULL, 0);
7110 }
7111 }
7112
7113 /* If we turned the cursor off, turn it back on. */
7114 if (cursor_off_p)
7115 x_display_cursor (w, 1,
7116 output_cursor.hpos, output_cursor.vpos,
7117 output_cursor.x, output_cursor.y);
7118
7119 output_cursor = saved_cursor;
7120
7121 set_x_cursor:
7122
7123 /* Change the mouse cursor. */
7124 if (draw == DRAW_NORMAL_TEXT)
7125 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7126 f->output_data.x->text_cursor);
7127 else if (draw == DRAW_MOUSE_FACE)
7128 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7129 f->output_data.x->cross_cursor);
7130 else
7131 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7132 f->output_data.x->nontext_cursor);
7133 }
7134
7135 /* Clear out the mouse-highlighted active region.
7136 Redraw it un-highlighted first. */
7137
7138 void
7139 clear_mouse_face (dpyinfo)
7140 struct x_display_info *dpyinfo;
7141 {
7142 if (tip_frame)
7143 return;
7144
7145 if (! NILP (dpyinfo->mouse_face_window))
7146 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7147
7148 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7149 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7150 dpyinfo->mouse_face_window = Qnil;
7151 }
7152
7153
7154 /* Clear any mouse-face on window W. This function is part of the
7155 redisplay interface, and is called from try_window_id and similar
7156 functions to ensure the mouse-highlight is off. */
7157
7158 static void
7159 x_clear_mouse_face (w)
7160 struct window *w;
7161 {
7162 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7163 Lisp_Object window;
7164
7165 XSETWINDOW (window, w);
7166 if (EQ (window, dpyinfo->mouse_face_window))
7167 clear_mouse_face (dpyinfo);
7168 }
7169
7170
7171 /* Just discard the mouse face information for frame F, if any.
7172 This is used when the size of F is changed. */
7173
7174 void
7175 cancel_mouse_face (f)
7176 FRAME_PTR f;
7177 {
7178 Lisp_Object window;
7179 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7180
7181 window = dpyinfo->mouse_face_window;
7182 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7183 {
7184 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7185 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7186 dpyinfo->mouse_face_window = Qnil;
7187 }
7188 }
7189 \f
7190 static struct scroll_bar *x_window_to_scroll_bar ();
7191 static void x_scroll_bar_report_motion ();
7192
7193 /* Return the current position of the mouse.
7194 *fp should be a frame which indicates which display to ask about.
7195
7196 If the mouse movement started in a scroll bar, set *fp, *bar_window,
7197 and *part to the frame, window, and scroll bar part that the mouse
7198 is over. Set *x and *y to the portion and whole of the mouse's
7199 position on the scroll bar.
7200
7201 If the mouse movement started elsewhere, set *fp to the frame the
7202 mouse is on, *bar_window to nil, and *x and *y to the character cell
7203 the mouse is over.
7204
7205 Set *time to the server time-stamp for the time at which the mouse
7206 was at this position.
7207
7208 Don't store anything if we don't have a valid set of values to report.
7209
7210 This clears the mouse_moved flag, so we can wait for the next mouse
7211 movement. */
7212
7213 static void
7214 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7215 FRAME_PTR *fp;
7216 int insist;
7217 Lisp_Object *bar_window;
7218 enum scroll_bar_part *part;
7219 Lisp_Object *x, *y;
7220 unsigned long *time;
7221 {
7222 FRAME_PTR f1;
7223
7224 BLOCK_INPUT;
7225
7226 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7227 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7228 else
7229 {
7230 Window root;
7231 int root_x, root_y;
7232
7233 Window dummy_window;
7234 int dummy;
7235
7236 Lisp_Object frame, tail;
7237
7238 /* Clear the mouse-moved flag for every frame on this display. */
7239 FOR_EACH_FRAME (tail, frame)
7240 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7241 XFRAME (frame)->mouse_moved = 0;
7242
7243 last_mouse_scroll_bar = Qnil;
7244
7245 /* Figure out which root window we're on. */
7246 XQueryPointer (FRAME_X_DISPLAY (*fp),
7247 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
7248
7249 /* The root window which contains the pointer. */
7250 &root,
7251
7252 /* Trash which we can't trust if the pointer is on
7253 a different screen. */
7254 &dummy_window,
7255
7256 /* The position on that root window. */
7257 &root_x, &root_y,
7258
7259 /* More trash we can't trust. */
7260 &dummy, &dummy,
7261
7262 /* Modifier keys and pointer buttons, about which
7263 we don't care. */
7264 (unsigned int *) &dummy);
7265
7266 /* Now we have a position on the root; find the innermost window
7267 containing the pointer. */
7268 {
7269 Window win, child;
7270 int win_x, win_y;
7271 int parent_x = 0, parent_y = 0;
7272 int count;
7273
7274 win = root;
7275
7276 /* XTranslateCoordinates can get errors if the window
7277 structure is changing at the same time this function
7278 is running. So at least we must not crash from them. */
7279
7280 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
7281
7282 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
7283 && FRAME_LIVE_P (last_mouse_frame))
7284 {
7285 /* If mouse was grabbed on a frame, give coords for that frame
7286 even if the mouse is now outside it. */
7287 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7288
7289 /* From-window, to-window. */
7290 root, FRAME_X_WINDOW (last_mouse_frame),
7291
7292 /* From-position, to-position. */
7293 root_x, root_y, &win_x, &win_y,
7294
7295 /* Child of win. */
7296 &child);
7297 f1 = last_mouse_frame;
7298 }
7299 else
7300 {
7301 while (1)
7302 {
7303 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7304
7305 /* From-window, to-window. */
7306 root, win,
7307
7308 /* From-position, to-position. */
7309 root_x, root_y, &win_x, &win_y,
7310
7311 /* Child of win. */
7312 &child);
7313
7314 if (child == None || child == win)
7315 break;
7316
7317 win = child;
7318 parent_x = win_x;
7319 parent_y = win_y;
7320 }
7321
7322 /* Now we know that:
7323 win is the innermost window containing the pointer
7324 (XTC says it has no child containing the pointer),
7325 win_x and win_y are the pointer's position in it
7326 (XTC did this the last time through), and
7327 parent_x and parent_y are the pointer's position in win's parent.
7328 (They are what win_x and win_y were when win was child.
7329 If win is the root window, it has no parent, and
7330 parent_{x,y} are invalid, but that's okay, because we'll
7331 never use them in that case.) */
7332
7333 /* Is win one of our frames? */
7334 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
7335 }
7336
7337 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7338 f1 = 0;
7339
7340 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
7341
7342 /* If not, is it one of our scroll bars? */
7343 if (! f1)
7344 {
7345 struct scroll_bar *bar = x_window_to_scroll_bar (win);
7346
7347 if (bar)
7348 {
7349 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7350 win_x = parent_x;
7351 win_y = parent_y;
7352 }
7353 }
7354
7355 if (f1 == 0 && insist > 0)
7356 f1 = SELECTED_FRAME ();
7357
7358 if (f1)
7359 {
7360 /* Ok, we found a frame. Store all the values.
7361 last_mouse_glyph is a rectangle used to reduce the
7362 generation of mouse events. To not miss any motion
7363 events, we must divide the frame into rectangles of the
7364 size of the smallest character that could be displayed
7365 on it, i.e. into the same rectangles that matrices on
7366 the frame are divided into. */
7367
7368 #if OLD_REDISPLAY_CODE
7369 int ignore1, ignore2;
7370 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
7371 &last_mouse_glyph,
7372 FRAME_X_DISPLAY_INFO (f1)->grabbed
7373 || insist);
7374 #else
7375 {
7376 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7377 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7378 int x = win_x;
7379 int y = win_y;
7380
7381 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7382 round down even for negative values. */
7383 if (x < 0)
7384 x -= width - 1;
7385 if (y < 0)
7386 y -= height - 1;
7387
7388 last_mouse_glyph.width = width;
7389 last_mouse_glyph.height = height;
7390 last_mouse_glyph.x = (x + width - 1) / width * width;
7391 last_mouse_glyph.y = (y + height - 1) / height * height;
7392 }
7393 #endif
7394
7395 *bar_window = Qnil;
7396 *part = 0;
7397 *fp = f1;
7398 XSETINT (*x, win_x);
7399 XSETINT (*y, win_y);
7400 *time = last_mouse_movement_time;
7401 }
7402 }
7403 }
7404
7405 UNBLOCK_INPUT;
7406 }
7407
7408
7409 #ifdef USE_X_TOOLKIT
7410
7411 /* Atimer callback function for TIMER. Called every 0.1s to process
7412 Xt timeouts, if needed. We must avoid calling XtAppPending as
7413 much as possible because that function does an implicit XFlush
7414 that slows us down. */
7415
7416 static void
7417 x_process_timeouts (timer)
7418 struct atimer *timer;
7419 {
7420 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7421 {
7422 BLOCK_INPUT;
7423 while (XtAppPending (Xt_app_con) & XtIMTimer)
7424 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7425 UNBLOCK_INPUT;
7426 }
7427 }
7428
7429 #endif /* USE_X_TOOLKIT */
7430
7431 \f
7432 /* Scroll bar support. */
7433
7434 /* Given an X window ID, find the struct scroll_bar which manages it.
7435 This can be called in GC, so we have to make sure to strip off mark
7436 bits. */
7437
7438 static struct scroll_bar *
7439 x_window_to_scroll_bar (window_id)
7440 Window window_id;
7441 {
7442 Lisp_Object tail;
7443
7444 for (tail = Vframe_list;
7445 XGCTYPE (tail) == Lisp_Cons;
7446 tail = XCDR (tail))
7447 {
7448 Lisp_Object frame, bar, condemned;
7449
7450 frame = XCAR (tail);
7451 /* All elements of Vframe_list should be frames. */
7452 if (! GC_FRAMEP (frame))
7453 abort ();
7454
7455 /* Scan this frame's scroll bar list for a scroll bar with the
7456 right window ID. */
7457 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7458 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7459 /* This trick allows us to search both the ordinary and
7460 condemned scroll bar lists with one loop. */
7461 ! GC_NILP (bar) || (bar = condemned,
7462 condemned = Qnil,
7463 ! GC_NILP (bar));
7464 bar = XSCROLL_BAR (bar)->next)
7465 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7466 return XSCROLL_BAR (bar);
7467 }
7468
7469 return 0;
7470 }
7471
7472
7473 \f
7474 /************************************************************************
7475 Toolkit scroll bars
7476 ************************************************************************/
7477
7478 #if USE_TOOLKIT_SCROLL_BARS
7479
7480 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7481 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7482 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7483 struct scroll_bar *));
7484 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7485 int, int, int));
7486
7487
7488 /* Id of action hook installed for scroll bars. */
7489
7490 static XtActionHookId action_hook_id;
7491
7492 /* Lisp window being scrolled. Set when starting to interact with
7493 a toolkit scroll bar, reset to nil when ending the interaction. */
7494
7495 static Lisp_Object window_being_scrolled;
7496
7497 /* Last scroll bar part sent in xm_scroll_callback. */
7498
7499 static int last_scroll_bar_part;
7500
7501 /* Whether this is an Xaw with arrow-scrollbars. This should imply
7502 that movements of 1/20 of the screen size are mapped to up/down. */
7503
7504 static Boolean xaw3d_arrow_scroll;
7505
7506 /* Whether the drag scrolling maintains the mouse at the top of the
7507 thumb. If not, resizing the thumb needs to be done more carefully
7508 to avoid jerkyness. */
7509
7510 static Boolean xaw3d_pick_top;
7511
7512
7513 /* Action hook installed via XtAppAddActionHook when toolkit scroll
7514 bars are used.. The hook is responsible for detecting when
7515 the user ends an interaction with the scroll bar, and generates
7516 a `end-scroll' scroll_bar_click' event if so. */
7517
7518 static void
7519 xt_action_hook (widget, client_data, action_name, event, params,
7520 num_params)
7521 Widget widget;
7522 XtPointer client_data;
7523 String action_name;
7524 XEvent *event;
7525 String *params;
7526 Cardinal *num_params;
7527 {
7528 int scroll_bar_p;
7529 char *end_action;
7530
7531 #ifdef USE_MOTIF
7532 scroll_bar_p = XmIsScrollBar (widget);
7533 end_action = "Release";
7534 #else /* !USE_MOTIF i.e. use Xaw */
7535 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7536 end_action = "EndScroll";
7537 #endif /* USE_MOTIF */
7538
7539 if (scroll_bar_p
7540 && strcmp (action_name, end_action) == 0
7541 && WINDOWP (window_being_scrolled))
7542 {
7543 struct window *w;
7544
7545 x_send_scroll_bar_event (window_being_scrolled,
7546 scroll_bar_end_scroll, 0, 0);
7547 w = XWINDOW (window_being_scrolled);
7548 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7549 window_being_scrolled = Qnil;
7550 last_scroll_bar_part = -1;
7551
7552 /* Xt timeouts no longer needed. */
7553 toolkit_scroll_bar_interaction = 0;
7554 }
7555 }
7556
7557
7558 /* Send a client message with message type Xatom_Scrollbar for a
7559 scroll action to the frame of WINDOW. PART is a value identifying
7560 the part of the scroll bar that was clicked on. PORTION is the
7561 amount to scroll of a whole of WHOLE. */
7562
7563 static void
7564 x_send_scroll_bar_event (window, part, portion, whole)
7565 Lisp_Object window;
7566 int part, portion, whole;
7567 {
7568 XEvent event;
7569 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7570 struct frame *f = XFRAME (XWINDOW (window)->frame);
7571
7572 /* Construct a ClientMessage event to send to the frame. */
7573 ev->type = ClientMessage;
7574 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7575 ev->display = FRAME_X_DISPLAY (f);
7576 ev->window = FRAME_X_WINDOW (f);
7577 ev->format = 32;
7578 ev->data.l[0] = (long) XFASTINT (window);
7579 ev->data.l[1] = (long) part;
7580 ev->data.l[2] = (long) 0;
7581 ev->data.l[3] = (long) portion;
7582 ev->data.l[4] = (long) whole;
7583
7584 /* Make Xt timeouts work while the scroll bar is active. */
7585 toolkit_scroll_bar_interaction = 1;
7586
7587 /* Setting the event mask to zero means that the message will
7588 be sent to the client that created the window, and if that
7589 window no longer exists, no event will be sent. */
7590 BLOCK_INPUT;
7591 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7592 UNBLOCK_INPUT;
7593 }
7594
7595
7596 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7597 in *IEVENT. */
7598
7599 static void
7600 x_scroll_bar_to_input_event (event, ievent)
7601 XEvent *event;
7602 struct input_event *ievent;
7603 {
7604 XClientMessageEvent *ev = (XClientMessageEvent *) event;
7605 Lisp_Object window;
7606 struct frame *f;
7607
7608 XSETFASTINT (window, ev->data.l[0]);
7609 f = XFRAME (XWINDOW (window)->frame);
7610
7611 ievent->kind = scroll_bar_click;
7612 ievent->frame_or_window = window;
7613 ievent->arg = Qnil;
7614 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7615 ievent->part = ev->data.l[1];
7616 ievent->code = ev->data.l[2];
7617 ievent->x = make_number ((int) ev->data.l[3]);
7618 ievent->y = make_number ((int) ev->data.l[4]);
7619 ievent->modifiers = 0;
7620 }
7621
7622
7623 #ifdef USE_MOTIF
7624
7625 /* Minimum and maximum values used for Motif scroll bars. */
7626
7627 #define XM_SB_MIN 1
7628 #define XM_SB_MAX 10000000
7629 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7630
7631
7632 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7633 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7634 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7635
7636 static void
7637 xm_scroll_callback (widget, client_data, call_data)
7638 Widget widget;
7639 XtPointer client_data, call_data;
7640 {
7641 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7642 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7643 double percent;
7644 int part = -1, whole = 0, portion = 0;
7645
7646 switch (cs->reason)
7647 {
7648 case XmCR_DECREMENT:
7649 bar->dragging = Qnil;
7650 part = scroll_bar_up_arrow;
7651 break;
7652
7653 case XmCR_INCREMENT:
7654 bar->dragging = Qnil;
7655 part = scroll_bar_down_arrow;
7656 break;
7657
7658 case XmCR_PAGE_DECREMENT:
7659 bar->dragging = Qnil;
7660 part = scroll_bar_above_handle;
7661 break;
7662
7663 case XmCR_PAGE_INCREMENT:
7664 bar->dragging = Qnil;
7665 part = scroll_bar_below_handle;
7666 break;
7667
7668 case XmCR_TO_TOP:
7669 bar->dragging = Qnil;
7670 part = scroll_bar_to_top;
7671 break;
7672
7673 case XmCR_TO_BOTTOM:
7674 bar->dragging = Qnil;
7675 part = scroll_bar_to_bottom;
7676 break;
7677
7678 case XmCR_DRAG:
7679 {
7680 int slider_size;
7681 int dragging_down_p = (INTEGERP (bar->dragging)
7682 && XINT (bar->dragging) <= cs->value);
7683
7684 /* Get the slider size. */
7685 BLOCK_INPUT;
7686 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7687 UNBLOCK_INPUT;
7688
7689 /* At the max position of the scroll bar, do a line-wise
7690 movement. Without doing anything, the LessTif scroll bar
7691 calls us with the same cs->value again and again. If we
7692 want to make sure that we can reach the end of the buffer,
7693 we have to do something.
7694
7695 Implementation note: setting bar->dragging always to
7696 cs->value gives a smoother movement at the max position.
7697 Setting it to nil when doing line-wise movement gives
7698 a better slider behavior. */
7699
7700 if (cs->value + slider_size == XM_SB_MAX
7701 || (dragging_down_p
7702 && last_scroll_bar_part == scroll_bar_down_arrow))
7703 {
7704 part = scroll_bar_down_arrow;
7705 bar->dragging = Qnil;
7706 }
7707 else
7708 {
7709 whole = XM_SB_RANGE;
7710 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7711 part = scroll_bar_handle;
7712 bar->dragging = make_number (cs->value);
7713 }
7714 }
7715 break;
7716
7717 case XmCR_VALUE_CHANGED:
7718 break;
7719 };
7720
7721 if (part >= 0)
7722 {
7723 window_being_scrolled = bar->window;
7724 last_scroll_bar_part = part;
7725 x_send_scroll_bar_event (bar->window, part, portion, whole);
7726 }
7727 }
7728
7729
7730 #else /* !USE_MOTIF, i.e. Xaw. */
7731
7732
7733 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
7734 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7735 scroll bar struct. CALL_DATA is a pointer to a float saying where
7736 the thumb is. */
7737
7738 static void
7739 xaw_jump_callback (widget, client_data, call_data)
7740 Widget widget;
7741 XtPointer client_data, call_data;
7742 {
7743 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7744 float top = *(float *) call_data;
7745 float shown;
7746 int whole, portion, height;
7747 int part;
7748
7749 /* Get the size of the thumb, a value between 0 and 1. */
7750 BLOCK_INPUT;
7751 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
7752 UNBLOCK_INPUT;
7753
7754 whole = 10000000;
7755 portion = shown < 1 ? top * whole : 0;
7756
7757 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7758 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7759 the bottom, so we force the scrolling whenever we see that we're
7760 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7761 we try to ensure that we always stay two pixels away from the
7762 bottom). */
7763 part = scroll_bar_down_arrow;
7764 else
7765 part = scroll_bar_handle;
7766
7767 window_being_scrolled = bar->window;
7768 bar->dragging = make_number (portion);
7769 last_scroll_bar_part = part;
7770 x_send_scroll_bar_event (bar->window, part, portion, whole);
7771 }
7772
7773
7774 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
7775 i.e. line or page up or down. WIDGET is the Xaw scroll bar
7776 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7777 the scroll bar. CALL_DATA is an integer specifying the action that
7778 has taken place. It's magnitude is in the range 0..height of the
7779 scroll bar. Negative values mean scroll towards buffer start.
7780 Values < height of scroll bar mean line-wise movement. */
7781
7782 static void
7783 xaw_scroll_callback (widget, client_data, call_data)
7784 Widget widget;
7785 XtPointer client_data, call_data;
7786 {
7787 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7788 int position = (int) call_data;
7789 Dimension height;
7790 int part;
7791
7792 /* Get the height of the scroll bar. */
7793 BLOCK_INPUT;
7794 XtVaGetValues (widget, XtNheight, &height, NULL);
7795 UNBLOCK_INPUT;
7796
7797 if (abs (position) >= height)
7798 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7799
7800 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
7801 it maps line-movement to call_data = max(5, height/20). */
7802 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
7803 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
7804 else
7805 part = scroll_bar_move_ratio;
7806
7807 window_being_scrolled = bar->window;
7808 bar->dragging = Qnil;
7809 last_scroll_bar_part = part;
7810 x_send_scroll_bar_event (bar->window, part, position, height);
7811 }
7812
7813
7814 #endif /* not USE_MOTIF */
7815
7816
7817 /* Create the widget for scroll bar BAR on frame F. Record the widget
7818 and X window of the scroll bar in BAR. */
7819
7820 static void
7821 x_create_toolkit_scroll_bar (f, bar)
7822 struct frame *f;
7823 struct scroll_bar *bar;
7824 {
7825 Window xwindow;
7826 Widget widget;
7827 Arg av[20];
7828 int ac = 0;
7829 char *scroll_bar_name = "verticalScrollBar";
7830 unsigned long pixel;
7831
7832 BLOCK_INPUT;
7833
7834 #ifdef USE_MOTIF
7835 /* LessTif 0.85, problems:
7836
7837 1. When the mouse if over the scroll bar, the scroll bar will
7838 get keyboard events. I didn't find a way to turn this off.
7839
7840 2. Do we have to explicitly set the cursor to get an arrow
7841 cursor (see below)? */
7842
7843 /* Set resources. Create the widget. */
7844 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7845 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
7846 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
7847 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
7848 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
7849 XtSetArg (av[ac], XmNincrement, 1); ++ac;
7850 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
7851
7852 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7853 if (pixel != -1)
7854 {
7855 XtSetArg (av[ac], XmNforeground, pixel);
7856 ++ac;
7857 }
7858
7859 pixel = f->output_data.x->scroll_bar_background_pixel;
7860 if (pixel != -1)
7861 {
7862 XtSetArg (av[ac], XmNbackground, pixel);
7863 ++ac;
7864 }
7865
7866 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
7867 scroll_bar_name, av, ac);
7868
7869 /* Add one callback for everything that can happen. */
7870 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
7871 (XtPointer) bar);
7872 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
7873 (XtPointer) bar);
7874 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
7875 (XtPointer) bar);
7876 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
7877 (XtPointer) bar);
7878 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
7879 (XtPointer) bar);
7880 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
7881 (XtPointer) bar);
7882 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
7883 (XtPointer) bar);
7884
7885 /* Realize the widget. Only after that is the X window created. */
7886 XtRealizeWidget (widget);
7887
7888 /* Set the cursor to an arrow. I didn't find a resource to do that.
7889 And I'm wondering why it hasn't an arrow cursor by default. */
7890 XDefineCursor (XtDisplay (widget), XtWindow (widget),
7891 f->output_data.x->nontext_cursor);
7892
7893 #else /* !USE_MOTIF i.e. use Xaw */
7894
7895 /* Set resources. Create the widget. The background of the
7896 Xaw3d scroll bar widget is a little bit light for my taste.
7897 We don't alter it here to let users change it according
7898 to their taste with `emacs*verticalScrollBar.background: xxx'. */
7899 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7900 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
7901 /* For smoother scrolling with Xaw3d -sm */
7902 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
7903 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
7904
7905 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7906 if (pixel != -1)
7907 {
7908 XtSetArg (av[ac], XtNforeground, pixel);
7909 ++ac;
7910 }
7911
7912 pixel = f->output_data.x->scroll_bar_background_pixel;
7913 if (pixel != -1)
7914 {
7915 XtSetArg (av[ac], XtNbackground, pixel);
7916 ++ac;
7917 }
7918
7919 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
7920 f->output_data.x->edit_widget, av, ac);
7921
7922 {
7923 char *initial = "";
7924 char *val = initial;
7925 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
7926 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
7927 if (val == initial)
7928 { /* ARROW_SCROLL */
7929 xaw3d_arrow_scroll = True;
7930 /* Isn't that just a personal preference ? -sm */
7931 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
7932 }
7933 }
7934
7935 /* Define callbacks. */
7936 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
7937 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
7938 (XtPointer) bar);
7939
7940 /* Realize the widget. Only after that is the X window created. */
7941 XtRealizeWidget (widget);
7942
7943 #endif /* !USE_MOTIF */
7944
7945 /* Install an action hook that let's us detect when the user
7946 finishes interacting with a scroll bar. */
7947 if (action_hook_id == 0)
7948 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7949
7950 /* Remember X window and widget in the scroll bar vector. */
7951 SET_SCROLL_BAR_X_WIDGET (bar, widget);
7952 xwindow = XtWindow (widget);
7953 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
7954
7955 UNBLOCK_INPUT;
7956 }
7957
7958
7959 /* Set the thumb size and position of scroll bar BAR. We are currently
7960 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7961
7962 static void
7963 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
7964 struct scroll_bar *bar;
7965 int portion, position, whole;
7966 {
7967 float top, shown;
7968 Widget widget = SCROLL_BAR_X_WIDGET (bar);
7969
7970 if (whole == 0)
7971 top = 0, shown = 1;
7972 else
7973 {
7974 top = (float) position / whole;
7975 shown = (float) portion / whole;
7976 }
7977
7978 BLOCK_INPUT;
7979
7980 #ifdef USE_MOTIF
7981 {
7982 int size, value;
7983 Boolean arrow1_selected, arrow2_selected;
7984 unsigned char flags;
7985 XmScrollBarWidget sb;
7986
7987 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
7988 is the scroll bar's maximum and MIN is the scroll bar's minimum
7989 value. */
7990 size = shown * XM_SB_RANGE;
7991 size = min (size, XM_SB_RANGE);
7992 size = max (size, 1);
7993
7994 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
7995 value = top * XM_SB_RANGE;
7996 value = min (value, XM_SB_MAX - size);
7997 value = max (value, XM_SB_MIN);
7998
7999 /* LessTif: Calling XmScrollBarSetValues after an increment or
8000 decrement turns off auto-repeat LessTif-internally. This can
8001 be seen in ScrollBar.c which resets Arrow1Selected and
8002 Arrow2Selected. It also sets internal flags so that LessTif
8003 believes the mouse is in the slider. We either have to change
8004 our code, or work around that by accessing private data. */
8005
8006 sb = (XmScrollBarWidget) widget;
8007 arrow1_selected = sb->scrollBar.arrow1_selected;
8008 arrow2_selected = sb->scrollBar.arrow2_selected;
8009 flags = sb->scrollBar.flags;
8010
8011 if (NILP (bar->dragging))
8012 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8013 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8014 /* This has the negative side effect that the slider value is
8015 not what it would be if we scrolled here using line-wise or
8016 page-wise movement. */
8017 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8018 else
8019 {
8020 /* If currently dragging, only update the slider size.
8021 This reduces flicker effects. */
8022 int old_value, old_size, increment, page_increment;
8023
8024 XmScrollBarGetValues (widget, &old_value, &old_size,
8025 &increment, &page_increment);
8026 XmScrollBarSetValues (widget, old_value,
8027 min (size, XM_SB_RANGE - old_value),
8028 0, 0, False);
8029 }
8030
8031 sb->scrollBar.arrow1_selected = arrow1_selected;
8032 sb->scrollBar.arrow2_selected = arrow2_selected;
8033 sb->scrollBar.flags = flags;
8034 }
8035 #else /* !USE_MOTIF i.e. use Xaw */
8036 {
8037 float old_top, old_shown;
8038 Dimension height;
8039 XtVaGetValues (widget,
8040 XtNtopOfThumb, &old_top,
8041 XtNshown, &old_shown,
8042 XtNheight, &height,
8043 NULL);
8044
8045 /* Massage the top+shown values. */
8046 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8047 top = max (0, min (1, top));
8048 else
8049 top = old_top;
8050 /* Keep two pixels available for moving the thumb down. */
8051 shown = max (0, min (1 - top - (2.0 / height), shown));
8052
8053 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8054 check that your system's configuration file contains a define
8055 for `NARROWPROTO'. See s/freebsd.h for an example. */
8056 if (top != old_top || shown != old_shown)
8057 {
8058 if (NILP (bar->dragging))
8059 XawScrollbarSetThumb (widget, top, shown);
8060 else
8061 {
8062 #ifdef HAVE_XAW3D
8063 ScrollbarWidget sb = (ScrollbarWidget) widget;
8064 int scroll_mode = 0;
8065
8066 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8067 if (xaw3d_arrow_scroll)
8068 {
8069 /* Xaw3d stupidly ignores resize requests while dragging
8070 so we have to make it believe it's not in dragging mode. */
8071 scroll_mode = sb->scrollbar.scroll_mode;
8072 if (scroll_mode == 2)
8073 sb->scrollbar.scroll_mode = 0;
8074 }
8075 #endif
8076 /* Try to make the scrolling a tad smoother. */
8077 if (!xaw3d_pick_top)
8078 shown = min (shown, old_shown);
8079
8080 XawScrollbarSetThumb (widget, top, shown);
8081
8082 #ifdef HAVE_XAW3D
8083 if (xaw3d_arrow_scroll && scroll_mode == 2)
8084 sb->scrollbar.scroll_mode = scroll_mode;
8085 #endif
8086 }
8087 }
8088 }
8089 #endif /* !USE_MOTIF */
8090
8091 UNBLOCK_INPUT;
8092 }
8093
8094 #endif /* USE_TOOLKIT_SCROLL_BARS */
8095
8096
8097 \f
8098 /************************************************************************
8099 Scroll bars, general
8100 ************************************************************************/
8101
8102 /* Create a scroll bar and return the scroll bar vector for it. W is
8103 the Emacs window on which to create the scroll bar. TOP, LEFT,
8104 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8105 scroll bar. */
8106
8107 static struct scroll_bar *
8108 x_scroll_bar_create (w, top, left, width, height)
8109 struct window *w;
8110 int top, left, width, height;
8111 {
8112 struct frame *f = XFRAME (w->frame);
8113 struct scroll_bar *bar
8114 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8115
8116 BLOCK_INPUT;
8117
8118 #if USE_TOOLKIT_SCROLL_BARS
8119 x_create_toolkit_scroll_bar (f, bar);
8120 #else /* not USE_TOOLKIT_SCROLL_BARS */
8121 {
8122 XSetWindowAttributes a;
8123 unsigned long mask;
8124 Window window;
8125
8126 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8127 if (a.background_pixel == -1)
8128 a.background_pixel = f->output_data.x->background_pixel;
8129
8130 a.event_mask = (ButtonPressMask | ButtonReleaseMask
8131 | ButtonMotionMask | PointerMotionHintMask
8132 | ExposureMask);
8133 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
8134
8135 mask = (CWBackPixel | CWEventMask | CWCursor);
8136
8137 /* Clear the area of W that will serve as a scroll bar. This is
8138 for the case that a window has been split horizontally. In
8139 this case, no clear_frame is generated to reduce flickering. */
8140 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8141 left, top, width,
8142 window_box_height (w), False);
8143
8144 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8145 /* Position and size of scroll bar. */
8146 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8147 top,
8148 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8149 height,
8150 /* Border width, depth, class, and visual. */
8151 0,
8152 CopyFromParent,
8153 CopyFromParent,
8154 CopyFromParent,
8155 /* Attributes. */
8156 mask, &a);
8157 SET_SCROLL_BAR_X_WINDOW (bar, window);
8158 }
8159 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8160
8161 XSETWINDOW (bar->window, w);
8162 XSETINT (bar->top, top);
8163 XSETINT (bar->left, left);
8164 XSETINT (bar->width, width);
8165 XSETINT (bar->height, height);
8166 XSETINT (bar->start, 0);
8167 XSETINT (bar->end, 0);
8168 bar->dragging = Qnil;
8169
8170 /* Add bar to its frame's list of scroll bars. */
8171 bar->next = FRAME_SCROLL_BARS (f);
8172 bar->prev = Qnil;
8173 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8174 if (!NILP (bar->next))
8175 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8176
8177 /* Map the window/widget. */
8178 #if USE_TOOLKIT_SCROLL_BARS
8179 XtMapWidget (SCROLL_BAR_X_WIDGET (bar));
8180 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
8181 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8182 top,
8183 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8184 height, 0);
8185 #else /* not USE_TOOLKIT_SCROLL_BARS */
8186 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8187 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8188
8189 UNBLOCK_INPUT;
8190 return bar;
8191 }
8192
8193
8194 /* Draw BAR's handle in the proper position.
8195
8196 If the handle is already drawn from START to END, don't bother
8197 redrawing it, unless REBUILD is non-zero; in that case, always
8198 redraw it. (REBUILD is handy for drawing the handle after expose
8199 events.)
8200
8201 Normally, we want to constrain the start and end of the handle to
8202 fit inside its rectangle, but if the user is dragging the scroll
8203 bar handle, we want to let them drag it down all the way, so that
8204 the bar's top is as far down as it goes; otherwise, there's no way
8205 to move to the very end of the buffer. */
8206
8207 #ifndef USE_TOOLKIT_SCROLL_BARS
8208
8209 static void
8210 x_scroll_bar_set_handle (bar, start, end, rebuild)
8211 struct scroll_bar *bar;
8212 int start, end;
8213 int rebuild;
8214 {
8215 int dragging = ! NILP (bar->dragging);
8216 Window w = SCROLL_BAR_X_WINDOW (bar);
8217 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8218 GC gc = f->output_data.x->normal_gc;
8219
8220 /* If the display is already accurate, do nothing. */
8221 if (! rebuild
8222 && start == XINT (bar->start)
8223 && end == XINT (bar->end))
8224 return;
8225
8226 BLOCK_INPUT;
8227
8228 {
8229 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8230 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8231 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8232
8233 /* Make sure the values are reasonable, and try to preserve
8234 the distance between start and end. */
8235 {
8236 int length = end - start;
8237
8238 if (start < 0)
8239 start = 0;
8240 else if (start > top_range)
8241 start = top_range;
8242 end = start + length;
8243
8244 if (end < start)
8245 end = start;
8246 else if (end > top_range && ! dragging)
8247 end = top_range;
8248 }
8249
8250 /* Store the adjusted setting in the scroll bar. */
8251 XSETINT (bar->start, start);
8252 XSETINT (bar->end, end);
8253
8254 /* Clip the end position, just for display. */
8255 if (end > top_range)
8256 end = top_range;
8257
8258 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
8259 below top positions, to make sure the handle is always at least
8260 that many pixels tall. */
8261 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
8262
8263 /* Draw the empty space above the handle. Note that we can't clear
8264 zero-height areas; that means "clear to end of window." */
8265 if (0 < start)
8266 XClearArea (FRAME_X_DISPLAY (f), w,
8267
8268 /* x, y, width, height, and exposures. */
8269 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8270 VERTICAL_SCROLL_BAR_TOP_BORDER,
8271 inside_width, start,
8272 False);
8273
8274 /* Change to proper foreground color if one is specified. */
8275 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8276 XSetForeground (FRAME_X_DISPLAY (f), gc,
8277 f->output_data.x->scroll_bar_foreground_pixel);
8278
8279 /* Draw the handle itself. */
8280 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
8281
8282 /* x, y, width, height */
8283 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8284 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
8285 inside_width, end - start);
8286
8287 /* Restore the foreground color of the GC if we changed it above. */
8288 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8289 XSetForeground (FRAME_X_DISPLAY (f), gc,
8290 f->output_data.x->foreground_pixel);
8291
8292 /* Draw the empty space below the handle. Note that we can't
8293 clear zero-height areas; that means "clear to end of window." */
8294 if (end < inside_height)
8295 XClearArea (FRAME_X_DISPLAY (f), w,
8296
8297 /* x, y, width, height, and exposures. */
8298 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8299 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
8300 inside_width, inside_height - end,
8301 False);
8302
8303 }
8304
8305 UNBLOCK_INPUT;
8306 }
8307
8308 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8309
8310 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8311 nil. */
8312
8313 static void
8314 x_scroll_bar_remove (bar)
8315 struct scroll_bar *bar;
8316 {
8317 BLOCK_INPUT;
8318
8319 #if USE_TOOLKIT_SCROLL_BARS
8320 XtDestroyWidget (SCROLL_BAR_X_WIDGET (bar));
8321 #else /* not USE_TOOLKIT_SCROLL_BARS */
8322 {
8323 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8324 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8325 }
8326 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8327
8328 /* Disassociate this scroll bar from its window. */
8329 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
8330
8331 UNBLOCK_INPUT;
8332 }
8333
8334
8335 /* Set the handle of the vertical scroll bar for WINDOW to indicate
8336 that we are displaying PORTION characters out of a total of WHOLE
8337 characters, starting at POSITION. If WINDOW has no scroll bar,
8338 create one. */
8339
8340 static void
8341 XTset_vertical_scroll_bar (w, portion, whole, position)
8342 struct window *w;
8343 int portion, whole, position;
8344 {
8345 struct frame *f = XFRAME (w->frame);
8346 struct scroll_bar *bar;
8347 int top, height, left, sb_left, width, sb_width;
8348 int window_x, window_y, window_width, window_height;
8349
8350 /* Get window dimensions. */
8351 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8352 top = window_y;
8353 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8354 height = window_height;
8355
8356 /* Compute the left edge of the scroll bar area. */
8357 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8358 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8359 else
8360 left = XFASTINT (w->left);
8361 left *= CANON_X_UNIT (f);
8362 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8363
8364 /* Compute the width of the scroll bar which might be less than
8365 the width of the area reserved for the scroll bar. */
8366 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8367 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
8368 else
8369 sb_width = width;
8370
8371 /* Compute the left edge of the scroll bar. */
8372 #ifdef USE_TOOLKIT_SCROLL_BARS
8373 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8374 sb_left = left + width - sb_width - (width - sb_width) / 2;
8375 else
8376 sb_left = left + (width - sb_width) / 2;
8377 #else
8378 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8379 sb_left = left + width - sb_width;
8380 else
8381 sb_left = left;
8382 #endif
8383
8384 /* Does the scroll bar exist yet? */
8385 if (NILP (w->vertical_scroll_bar))
8386 {
8387 BLOCK_INPUT;
8388 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8389 left, top, width, height, False);
8390 UNBLOCK_INPUT;
8391 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8392 }
8393 else
8394 {
8395 /* It may just need to be moved and resized. */
8396 unsigned int mask = 0;
8397
8398 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8399
8400 BLOCK_INPUT;
8401
8402 if (sb_left != XINT (bar->left))
8403 mask |= CWX;
8404 if (top != XINT (bar->top))
8405 mask |= CWY;
8406 if (sb_width != XINT (bar->width))
8407 mask |= CWWidth;
8408 if (height != XINT (bar->height))
8409 mask |= CWHeight;
8410
8411 #ifdef USE_TOOLKIT_SCROLL_BARS
8412
8413 /* Since toolkit scroll bars are smaller than the space reserved
8414 for them on the frame, we have to clear "under" them. */
8415 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8416 left, top, width, height, False);
8417
8418 /* Move/size the scroll bar widget. */
8419 if (mask)
8420 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
8421 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8422 top,
8423 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8424 height, 0);
8425
8426 #else /* not USE_TOOLKIT_SCROLL_BARS */
8427
8428 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8429 {
8430 /* Clear areas not covered by the scroll bar. This makes sure a
8431 previous mode line display is cleared after C-x 2 C-x 1, for
8432 example. Non-toolkit scroll bars are as wide as the area
8433 reserved for scroll bars - trim at both sides. */
8434 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8435 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8436 height, False);
8437 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8438 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8439 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8440 height, False);
8441 }
8442
8443 /* Move/size the scroll bar window. */
8444 if (mask)
8445 {
8446 XWindowChanges wc;
8447
8448 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8449 wc.y = top;
8450 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8451 wc.height = height;
8452 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8453 mask, &wc);
8454 }
8455
8456 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8457
8458 /* Remember new settings. */
8459 XSETINT (bar->left, sb_left);
8460 XSETINT (bar->top, top);
8461 XSETINT (bar->width, sb_width);
8462 XSETINT (bar->height, height);
8463
8464 UNBLOCK_INPUT;
8465 }
8466
8467 #if USE_TOOLKIT_SCROLL_BARS
8468 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8469 #else /* not USE_TOOLKIT_SCROLL_BARS */
8470 /* Set the scroll bar's current state, unless we're currently being
8471 dragged. */
8472 if (NILP (bar->dragging))
8473 {
8474 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
8475
8476 if (whole == 0)
8477 x_scroll_bar_set_handle (bar, 0, top_range, 0);
8478 else
8479 {
8480 int start = ((double) position * top_range) / whole;
8481 int end = ((double) (position + portion) * top_range) / whole;
8482 x_scroll_bar_set_handle (bar, start, end, 0);
8483 }
8484 }
8485 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8486
8487 XSETVECTOR (w->vertical_scroll_bar, bar);
8488 }
8489
8490
8491 /* The following three hooks are used when we're doing a thorough
8492 redisplay of the frame. We don't explicitly know which scroll bars
8493 are going to be deleted, because keeping track of when windows go
8494 away is a real pain - "Can you say set-window-configuration, boys
8495 and girls?" Instead, we just assert at the beginning of redisplay
8496 that *all* scroll bars are to be removed, and then save a scroll bar
8497 from the fiery pit when we actually redisplay its window. */
8498
8499 /* Arrange for all scroll bars on FRAME to be removed at the next call
8500 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
8501 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8502
8503 static void
8504 XTcondemn_scroll_bars (frame)
8505 FRAME_PTR frame;
8506 {
8507 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8508 while (! NILP (FRAME_SCROLL_BARS (frame)))
8509 {
8510 Lisp_Object bar;
8511 bar = FRAME_SCROLL_BARS (frame);
8512 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8513 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8514 XSCROLL_BAR (bar)->prev = Qnil;
8515 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8516 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8517 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8518 }
8519 }
8520
8521 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
8522 Note that WINDOW isn't necessarily condemned at all. */
8523 static void
8524 XTredeem_scroll_bar (window)
8525 struct window *window;
8526 {
8527 struct scroll_bar *bar;
8528
8529 /* We can't redeem this window's scroll bar if it doesn't have one. */
8530 if (NILP (window->vertical_scroll_bar))
8531 abort ();
8532
8533 bar = XSCROLL_BAR (window->vertical_scroll_bar);
8534
8535 /* Unlink it from the condemned list. */
8536 {
8537 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
8538
8539 if (NILP (bar->prev))
8540 {
8541 /* If the prev pointer is nil, it must be the first in one of
8542 the lists. */
8543 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8544 /* It's not condemned. Everything's fine. */
8545 return;
8546 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8547 window->vertical_scroll_bar))
8548 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8549 else
8550 /* If its prev pointer is nil, it must be at the front of
8551 one or the other! */
8552 abort ();
8553 }
8554 else
8555 XSCROLL_BAR (bar->prev)->next = bar->next;
8556
8557 if (! NILP (bar->next))
8558 XSCROLL_BAR (bar->next)->prev = bar->prev;
8559
8560 bar->next = FRAME_SCROLL_BARS (f);
8561 bar->prev = Qnil;
8562 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8563 if (! NILP (bar->next))
8564 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8565 }
8566 }
8567
8568 /* Remove all scroll bars on FRAME that haven't been saved since the
8569 last call to `*condemn_scroll_bars_hook'. */
8570
8571 static void
8572 XTjudge_scroll_bars (f)
8573 FRAME_PTR f;
8574 {
8575 Lisp_Object bar, next;
8576
8577 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
8578
8579 /* Clear out the condemned list now so we won't try to process any
8580 more events on the hapless scroll bars. */
8581 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
8582
8583 for (; ! NILP (bar); bar = next)
8584 {
8585 struct scroll_bar *b = XSCROLL_BAR (bar);
8586
8587 x_scroll_bar_remove (b);
8588
8589 next = b->next;
8590 b->next = b->prev = Qnil;
8591 }
8592
8593 /* Now there should be no references to the condemned scroll bars,
8594 and they should get garbage-collected. */
8595 }
8596
8597
8598 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
8599 is a no-op when using toolkit scroll bars.
8600
8601 This may be called from a signal handler, so we have to ignore GC
8602 mark bits. */
8603
8604 static void
8605 x_scroll_bar_expose (bar, event)
8606 struct scroll_bar *bar;
8607 XEvent *event;
8608 {
8609 #ifndef USE_TOOLKIT_SCROLL_BARS
8610
8611 Window w = SCROLL_BAR_X_WINDOW (bar);
8612 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8613 GC gc = f->output_data.x->normal_gc;
8614 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8615
8616 BLOCK_INPUT;
8617
8618 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
8619
8620 /* Draw a one-pixel border just inside the edges of the scroll bar. */
8621 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
8622
8623 /* x, y, width, height */
8624 0, 0,
8625 XINT (bar->width) - 1 - width_trim - width_trim,
8626 XINT (bar->height) - 1);
8627
8628 UNBLOCK_INPUT;
8629
8630 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8631 }
8632
8633 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8634 is set to something other than no_event, it is enqueued.
8635
8636 This may be called from a signal handler, so we have to ignore GC
8637 mark bits. */
8638
8639 #ifndef USE_TOOLKIT_SCROLL_BARS
8640
8641 static void
8642 x_scroll_bar_handle_click (bar, event, emacs_event)
8643 struct scroll_bar *bar;
8644 XEvent *event;
8645 struct input_event *emacs_event;
8646 {
8647 if (! GC_WINDOWP (bar->window))
8648 abort ();
8649
8650 emacs_event->kind = scroll_bar_click;
8651 emacs_event->code = event->xbutton.button - Button1;
8652 emacs_event->modifiers
8653 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8654 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8655 event->xbutton.state)
8656 | (event->type == ButtonRelease
8657 ? up_modifier
8658 : down_modifier));
8659 emacs_event->frame_or_window = bar->window;
8660 emacs_event->arg = Qnil;
8661 emacs_event->timestamp = event->xbutton.time;
8662 {
8663 #if 0
8664 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8665 int internal_height
8666 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8667 #endif
8668 int top_range
8669 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8670 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
8671
8672 if (y < 0) y = 0;
8673 if (y > top_range) y = top_range;
8674
8675 if (y < XINT (bar->start))
8676 emacs_event->part = scroll_bar_above_handle;
8677 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8678 emacs_event->part = scroll_bar_handle;
8679 else
8680 emacs_event->part = scroll_bar_below_handle;
8681
8682 /* Just because the user has clicked on the handle doesn't mean
8683 they want to drag it. Lisp code needs to be able to decide
8684 whether or not we're dragging. */
8685 #if 0
8686 /* If the user has just clicked on the handle, record where they're
8687 holding it. */
8688 if (event->type == ButtonPress
8689 && emacs_event->part == scroll_bar_handle)
8690 XSETINT (bar->dragging, y - XINT (bar->start));
8691 #endif
8692
8693 /* If the user has released the handle, set it to its final position. */
8694 if (event->type == ButtonRelease
8695 && ! NILP (bar->dragging))
8696 {
8697 int new_start = y - XINT (bar->dragging);
8698 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8699
8700 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8701 bar->dragging = Qnil;
8702 }
8703
8704 /* Same deal here as the other #if 0. */
8705 #if 0
8706 /* Clicks on the handle are always reported as occurring at the top of
8707 the handle. */
8708 if (emacs_event->part == scroll_bar_handle)
8709 emacs_event->x = bar->start;
8710 else
8711 XSETINT (emacs_event->x, y);
8712 #else
8713 XSETINT (emacs_event->x, y);
8714 #endif
8715
8716 XSETINT (emacs_event->y, top_range);
8717 }
8718 }
8719
8720 /* Handle some mouse motion while someone is dragging the scroll bar.
8721
8722 This may be called from a signal handler, so we have to ignore GC
8723 mark bits. */
8724
8725 static void
8726 x_scroll_bar_note_movement (bar, event)
8727 struct scroll_bar *bar;
8728 XEvent *event;
8729 {
8730 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8731
8732 last_mouse_movement_time = event->xmotion.time;
8733
8734 f->mouse_moved = 1;
8735 XSETVECTOR (last_mouse_scroll_bar, bar);
8736
8737 /* If we're dragging the bar, display it. */
8738 if (! GC_NILP (bar->dragging))
8739 {
8740 /* Where should the handle be now? */
8741 int new_start = event->xmotion.y - XINT (bar->dragging);
8742
8743 if (new_start != XINT (bar->start))
8744 {
8745 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8746
8747 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8748 }
8749 }
8750 }
8751
8752 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8753
8754 /* Return information to the user about the current position of the mouse
8755 on the scroll bar. */
8756
8757 static void
8758 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8759 FRAME_PTR *fp;
8760 Lisp_Object *bar_window;
8761 enum scroll_bar_part *part;
8762 Lisp_Object *x, *y;
8763 unsigned long *time;
8764 {
8765 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
8766 Window w = SCROLL_BAR_X_WINDOW (bar);
8767 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8768 int win_x, win_y;
8769 Window dummy_window;
8770 int dummy_coord;
8771 unsigned int dummy_mask;
8772
8773 BLOCK_INPUT;
8774
8775 /* Get the mouse's position relative to the scroll bar window, and
8776 report that. */
8777 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
8778
8779 /* Root, child, root x and root y. */
8780 &dummy_window, &dummy_window,
8781 &dummy_coord, &dummy_coord,
8782
8783 /* Position relative to scroll bar. */
8784 &win_x, &win_y,
8785
8786 /* Mouse buttons and modifier keys. */
8787 &dummy_mask))
8788 ;
8789 else
8790 {
8791 #if 0
8792 int inside_height
8793 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8794 #endif
8795 int top_range
8796 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8797
8798 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8799
8800 if (! NILP (bar->dragging))
8801 win_y -= XINT (bar->dragging);
8802
8803 if (win_y < 0)
8804 win_y = 0;
8805 if (win_y > top_range)
8806 win_y = top_range;
8807
8808 *fp = f;
8809 *bar_window = bar->window;
8810
8811 if (! NILP (bar->dragging))
8812 *part = scroll_bar_handle;
8813 else if (win_y < XINT (bar->start))
8814 *part = scroll_bar_above_handle;
8815 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8816 *part = scroll_bar_handle;
8817 else
8818 *part = scroll_bar_below_handle;
8819
8820 XSETINT (*x, win_y);
8821 XSETINT (*y, top_range);
8822
8823 f->mouse_moved = 0;
8824 last_mouse_scroll_bar = Qnil;
8825 }
8826
8827 *time = last_mouse_movement_time;
8828
8829 UNBLOCK_INPUT;
8830 }
8831
8832
8833 /* The screen has been cleared so we may have changed foreground or
8834 background colors, and the scroll bars may need to be redrawn.
8835 Clear out the scroll bars, and ask for expose events, so we can
8836 redraw them. */
8837
8838 void
8839 x_scroll_bar_clear (f)
8840 FRAME_PTR f;
8841 {
8842 #ifndef USE_TOOLKIT_SCROLL_BARS
8843 Lisp_Object bar;
8844
8845 /* We can have scroll bars even if this is 0,
8846 if we just turned off scroll bar mode.
8847 But in that case we should not clear them. */
8848 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8849 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8850 bar = XSCROLL_BAR (bar)->next)
8851 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
8852 0, 0, 0, 0, True);
8853 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8854 }
8855
8856 /* This processes Expose events from the menu-bar specific X event
8857 loop in xmenu.c. This allows to redisplay the frame if necessary
8858 when handling menu-bar or pop-up items. */
8859
8860 int
8861 process_expose_from_menu (event)
8862 XEvent event;
8863 {
8864 FRAME_PTR f;
8865 struct x_display_info *dpyinfo;
8866 int frame_exposed_p = 0;
8867
8868 BLOCK_INPUT;
8869
8870 dpyinfo = x_display_info_for_display (event.xexpose.display);
8871 f = x_window_to_frame (dpyinfo, event.xexpose.window);
8872 if (f)
8873 {
8874 if (f->async_visible == 0)
8875 {
8876 f->async_visible = 1;
8877 f->async_iconified = 0;
8878 f->output_data.x->has_been_visible = 1;
8879 SET_FRAME_GARBAGED (f);
8880 }
8881 else
8882 {
8883 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
8884 event.xexpose.x, event.xexpose.y,
8885 event.xexpose.width, event.xexpose.height);
8886 frame_exposed_p = 1;
8887 }
8888 }
8889 else
8890 {
8891 struct scroll_bar *bar
8892 = x_window_to_scroll_bar (event.xexpose.window);
8893
8894 if (bar)
8895 x_scroll_bar_expose (bar, &event);
8896 }
8897
8898 UNBLOCK_INPUT;
8899 return frame_exposed_p;
8900 }
8901 \f
8902 /* Define a queue to save up SelectionRequest events for later handling. */
8903
8904 struct selection_event_queue
8905 {
8906 XEvent event;
8907 struct selection_event_queue *next;
8908 };
8909
8910 static struct selection_event_queue *queue;
8911
8912 /* Nonzero means queue up certain events--don't process them yet. */
8913
8914 static int x_queue_selection_requests;
8915
8916 /* Queue up an X event *EVENT, to be processed later. */
8917
8918 static void
8919 x_queue_event (f, event)
8920 FRAME_PTR f;
8921 XEvent *event;
8922 {
8923 struct selection_event_queue *queue_tmp
8924 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
8925
8926 if (queue_tmp != NULL)
8927 {
8928 queue_tmp->event = *event;
8929 queue_tmp->next = queue;
8930 queue = queue_tmp;
8931 }
8932 }
8933
8934 /* Take all the queued events and put them back
8935 so that they get processed afresh. */
8936
8937 static void
8938 x_unqueue_events (display)
8939 Display *display;
8940 {
8941 while (queue != NULL)
8942 {
8943 struct selection_event_queue *queue_tmp = queue;
8944 XPutBackEvent (display, &queue_tmp->event);
8945 queue = queue_tmp->next;
8946 xfree ((char *)queue_tmp);
8947 }
8948 }
8949
8950 /* Start queuing SelectionRequest events. */
8951
8952 void
8953 x_start_queuing_selection_requests (display)
8954 Display *display;
8955 {
8956 x_queue_selection_requests++;
8957 }
8958
8959 /* Stop queuing SelectionRequest events. */
8960
8961 void
8962 x_stop_queuing_selection_requests (display)
8963 Display *display;
8964 {
8965 x_queue_selection_requests--;
8966 x_unqueue_events (display);
8967 }
8968 \f
8969 /* The main X event-reading loop - XTread_socket. */
8970
8971 /* Time stamp of enter window event. This is only used by XTread_socket,
8972 but we have to put it out here, since static variables within functions
8973 sometimes don't work. */
8974
8975 static Time enter_timestamp;
8976
8977 /* This holds the state XLookupString needs to implement dead keys
8978 and other tricks known as "compose processing". _X Window System_
8979 says that a portable program can't use this, but Stephen Gildea assures
8980 me that letting the compiler initialize it to zeros will work okay.
8981
8982 This must be defined outside of XTread_socket, for the same reasons
8983 given for enter_time stamp, above. */
8984
8985 static XComposeStatus compose_status;
8986
8987 /* Record the last 100 characters stored
8988 to help debug the loss-of-chars-during-GC problem. */
8989
8990 static int temp_index;
8991 static short temp_buffer[100];
8992
8993 /* Set this to nonzero to fake an "X I/O error"
8994 on a particular display. */
8995
8996 struct x_display_info *XTread_socket_fake_io_error;
8997
8998 /* When we find no input here, we occasionally do a no-op command
8999 to verify that the X server is still running and we can still talk with it.
9000 We try all the open displays, one by one.
9001 This variable is used for cycling thru the displays. */
9002
9003 static struct x_display_info *next_noop_dpyinfo;
9004
9005 #define SET_SAVED_MENU_EVENT(size) \
9006 do \
9007 { \
9008 if (f->output_data.x->saved_menu_event == 0) \
9009 f->output_data.x->saved_menu_event \
9010 = (XEvent *) xmalloc (sizeof (XEvent)); \
9011 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9012 if (numchars >= 1) \
9013 { \
9014 bufp->kind = menu_bar_activate_event; \
9015 XSETFRAME (bufp->frame_or_window, f); \
9016 bufp->arg = Qnil; \
9017 bufp++; \
9018 count++; \
9019 numchars--; \
9020 } \
9021 } \
9022 while (0)
9023
9024 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9025 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9026
9027 /* Read events coming from the X server.
9028 This routine is called by the SIGIO handler.
9029 We return as soon as there are no more events to be read.
9030
9031 Events representing keys are stored in buffer BUFP,
9032 which can hold up to NUMCHARS characters.
9033 We return the number of characters stored into the buffer,
9034 thus pretending to be `read'.
9035
9036 EXPECTED is nonzero if the caller knows input is available. */
9037
9038 int
9039 XTread_socket (sd, bufp, numchars, expected)
9040 register int sd;
9041 /* register */ struct input_event *bufp;
9042 /* register */ int numchars;
9043 int expected;
9044 {
9045 int count = 0;
9046 int nbytes = 0;
9047 XEvent event;
9048 struct frame *f;
9049 int event_found = 0;
9050 struct x_display_info *dpyinfo;
9051 struct coding_system coding;
9052
9053 if (interrupt_input_blocked)
9054 {
9055 interrupt_input_pending = 1;
9056 return -1;
9057 }
9058
9059 interrupt_input_pending = 0;
9060 BLOCK_INPUT;
9061
9062 /* So people can tell when we have read the available input. */
9063 input_signal_count++;
9064
9065 if (numchars <= 0)
9066 abort (); /* Don't think this happens. */
9067
9068 ++handling_signal;
9069
9070 /* The input should be decoded if it is from XIM. Currently the
9071 locale of XIM is the same as that of the system. So, we can use
9072 Vlocale_coding_system which is initialized properly at Emacs
9073 startup time. */
9074 setup_coding_system (Vlocale_coding_system, &coding);
9075 coding.src_multibyte = 0;
9076 coding.dst_multibyte = 1;
9077 /* The input is converted to events, thus we can't handle
9078 composition. Anyway, there's no XIM that gives us composition
9079 information. */
9080 coding.composing = COMPOSITION_DISABLED;
9081
9082 /* Find the display we are supposed to read input for.
9083 It's the one communicating on descriptor SD. */
9084 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9085 {
9086 #if 0 /* This ought to be unnecessary; let's verify it. */
9087 #ifdef FIOSNBIO
9088 /* If available, Xlib uses FIOSNBIO to make the socket
9089 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
9090 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
9091 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
9092 fcntl (dpyinfo->connection, F_SETFL, 0);
9093 #endif /* ! defined (FIOSNBIO) */
9094 #endif
9095
9096 #if 0 /* This code can't be made to work, with multiple displays,
9097 and appears not to be used on any system any more.
9098 Also keyboard.c doesn't turn O_NDELAY on and off
9099 for X connections. */
9100 #ifndef SIGIO
9101 #ifndef HAVE_SELECT
9102 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9103 {
9104 extern int read_alarm_should_throw;
9105 read_alarm_should_throw = 1;
9106 XPeekEvent (dpyinfo->display, &event);
9107 read_alarm_should_throw = 0;
9108 }
9109 #endif /* HAVE_SELECT */
9110 #endif /* SIGIO */
9111 #endif
9112
9113 /* For debugging, this gives a way to fake an I/O error. */
9114 if (dpyinfo == XTread_socket_fake_io_error)
9115 {
9116 XTread_socket_fake_io_error = 0;
9117 x_io_error_quitter (dpyinfo->display);
9118 }
9119
9120 while (XPending (dpyinfo->display))
9121 {
9122 XNextEvent (dpyinfo->display, &event);
9123
9124 #ifdef HAVE_X_I18N
9125 {
9126 /* Filter events for the current X input method.
9127 XFilterEvent returns non-zero if the input method has
9128 consumed the event. We pass the frame's X window to
9129 XFilterEvent because that's the one for which the IC
9130 was created. */
9131 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9132 event.xclient.window);
9133 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
9134 break;
9135 }
9136 #endif
9137 event_found = 1;
9138
9139 switch (event.type)
9140 {
9141 case ClientMessage:
9142 {
9143 if (event.xclient.message_type
9144 == dpyinfo->Xatom_wm_protocols
9145 && event.xclient.format == 32)
9146 {
9147 if (event.xclient.data.l[0]
9148 == dpyinfo->Xatom_wm_take_focus)
9149 {
9150 /* Use x_any_window_to_frame because this
9151 could be the shell widget window
9152 if the frame has no title bar. */
9153 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
9154 #ifdef HAVE_X_I18N
9155 /* Not quite sure this is needed -pd */
9156 if (f && FRAME_XIC (f))
9157 XSetICFocus (FRAME_XIC (f));
9158 #endif
9159 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9160 instructs the WM to set the input focus automatically for
9161 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9162 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9163 it has set the focus. So, XSetInputFocus below is not
9164 needed.
9165
9166 The call to XSetInputFocus below has also caused trouble. In
9167 cases where the XSetInputFocus done by the WM and the one
9168 below are temporally close (on a fast machine), the call
9169 below can generate additional FocusIn events which confuse
9170 Emacs. */
9171
9172 /* Since we set WM_TAKE_FOCUS, we must call
9173 XSetInputFocus explicitly. But not if f is null,
9174 since that might be an event for a deleted frame. */
9175 if (f)
9176 {
9177 Display *d = event.xclient.display;
9178 /* Catch and ignore errors, in case window has been
9179 iconified by a window manager such as GWM. */
9180 int count = x_catch_errors (d);
9181 XSetInputFocus (d, event.xclient.window,
9182 /* The ICCCM says this is
9183 the only valid choice. */
9184 RevertToParent,
9185 event.xclient.data.l[1]);
9186 /* This is needed to detect the error
9187 if there is an error. */
9188 XSync (d, False);
9189 x_uncatch_errors (d, count);
9190 }
9191 /* Not certain about handling scroll bars here */
9192 #endif /* 0 */
9193 }
9194 else if (event.xclient.data.l[0]
9195 == dpyinfo->Xatom_wm_save_yourself)
9196 {
9197 /* Save state modify the WM_COMMAND property to
9198 something which can reinstate us. This notifies
9199 the session manager, who's looking for such a
9200 PropertyNotify. Can restart processing when
9201 a keyboard or mouse event arrives. */
9202 if (numchars > 0)
9203 {
9204 f = x_top_window_to_frame (dpyinfo,
9205 event.xclient.window);
9206
9207 /* This is just so we only give real data once
9208 for a single Emacs process. */
9209 if (f == SELECTED_FRAME ())
9210 XSetCommand (FRAME_X_DISPLAY (f),
9211 event.xclient.window,
9212 initial_argv, initial_argc);
9213 else if (f)
9214 XSetCommand (FRAME_X_DISPLAY (f),
9215 event.xclient.window,
9216 0, 0);
9217 }
9218 }
9219 else if (event.xclient.data.l[0]
9220 == dpyinfo->Xatom_wm_delete_window)
9221 {
9222 struct frame *f
9223 = x_any_window_to_frame (dpyinfo,
9224 event.xclient.window);
9225
9226 if (f)
9227 {
9228 if (numchars == 0)
9229 abort ();
9230
9231 bufp->kind = delete_window_event;
9232 XSETFRAME (bufp->frame_or_window, f);
9233 bufp->arg = Qnil;
9234 bufp++;
9235
9236 count += 1;
9237 numchars -= 1;
9238 }
9239 }
9240 }
9241 else if (event.xclient.message_type
9242 == dpyinfo->Xatom_wm_configure_denied)
9243 {
9244 }
9245 else if (event.xclient.message_type
9246 == dpyinfo->Xatom_wm_window_moved)
9247 {
9248 int new_x, new_y;
9249 struct frame *f
9250 = x_window_to_frame (dpyinfo, event.xclient.window);
9251
9252 new_x = event.xclient.data.s[0];
9253 new_y = event.xclient.data.s[1];
9254
9255 if (f)
9256 {
9257 f->output_data.x->left_pos = new_x;
9258 f->output_data.x->top_pos = new_y;
9259 }
9260 }
9261 #ifdef HACK_EDITRES
9262 else if (event.xclient.message_type
9263 == dpyinfo->Xatom_editres)
9264 {
9265 struct frame *f
9266 = x_any_window_to_frame (dpyinfo, event.xclient.window);
9267 _XEditResCheckMessages (f->output_data.x->widget, NULL,
9268 &event, NULL);
9269 }
9270 #endif /* HACK_EDITRES */
9271 else if ((event.xclient.message_type
9272 == dpyinfo->Xatom_DONE)
9273 || (event.xclient.message_type
9274 == dpyinfo->Xatom_PAGE))
9275 {
9276 /* Ghostview job completed. Kill it. We could
9277 reply with "Next" if we received "Page", but we
9278 currently never do because we are interested in
9279 images, only, which should have 1 page. */
9280 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9281 struct frame *f
9282 = x_window_to_frame (dpyinfo, event.xclient.window);
9283 x_kill_gs_process (pixmap, f);
9284 expose_frame (f, 0, 0, 0, 0);
9285 }
9286 #ifdef USE_TOOLKIT_SCROLL_BARS
9287 /* Scroll bar callbacks send a ClientMessage from which
9288 we construct an input_event. */
9289 else if (event.xclient.message_type
9290 == dpyinfo->Xatom_Scrollbar)
9291 {
9292 x_scroll_bar_to_input_event (&event, bufp);
9293 ++bufp, ++count, --numchars;
9294 goto out;
9295 }
9296 #endif /* USE_TOOLKIT_SCROLL_BARS */
9297 else
9298 goto OTHER;
9299 }
9300 break;
9301
9302 case SelectionNotify:
9303 #ifdef USE_X_TOOLKIT
9304 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
9305 goto OTHER;
9306 #endif /* not USE_X_TOOLKIT */
9307 x_handle_selection_notify (&event.xselection);
9308 break;
9309
9310 case SelectionClear: /* Someone has grabbed ownership. */
9311 #ifdef USE_X_TOOLKIT
9312 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
9313 goto OTHER;
9314 #endif /* USE_X_TOOLKIT */
9315 {
9316 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
9317
9318 if (numchars == 0)
9319 abort ();
9320
9321 bufp->kind = selection_clear_event;
9322 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9323 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9324 SELECTION_EVENT_TIME (bufp) = eventp->time;
9325 bufp->frame_or_window = Qnil;
9326 bufp->arg = Qnil;
9327 bufp++;
9328
9329 count += 1;
9330 numchars -= 1;
9331 }
9332 break;
9333
9334 case SelectionRequest: /* Someone wants our selection. */
9335 #ifdef USE_X_TOOLKIT
9336 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
9337 goto OTHER;
9338 #endif /* USE_X_TOOLKIT */
9339 if (x_queue_selection_requests)
9340 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
9341 &event);
9342 else
9343 {
9344 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
9345
9346 if (numchars == 0)
9347 abort ();
9348
9349 bufp->kind = selection_request_event;
9350 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9351 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9352 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9353 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9354 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9355 SELECTION_EVENT_TIME (bufp) = eventp->time;
9356 bufp->frame_or_window = Qnil;
9357 bufp->arg = Qnil;
9358 bufp++;
9359
9360 count += 1;
9361 numchars -= 1;
9362 }
9363 break;
9364
9365 case PropertyNotify:
9366 #ifdef USE_X_TOOLKIT
9367 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
9368 goto OTHER;
9369 #endif /* not USE_X_TOOLKIT */
9370 x_handle_property_notify (&event.xproperty);
9371 break;
9372
9373 case ReparentNotify:
9374 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
9375 if (f)
9376 {
9377 int x, y;
9378 f->output_data.x->parent_desc = event.xreparent.parent;
9379 x_real_positions (f, &x, &y);
9380 f->output_data.x->left_pos = x;
9381 f->output_data.x->top_pos = y;
9382 }
9383 break;
9384
9385 case Expose:
9386 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9387 if (f)
9388 {
9389 if (f->async_visible == 0)
9390 {
9391 f->async_visible = 1;
9392 f->async_iconified = 0;
9393 f->output_data.x->has_been_visible = 1;
9394 SET_FRAME_GARBAGED (f);
9395 }
9396 else
9397 expose_frame (x_window_to_frame (dpyinfo,
9398 event.xexpose.window),
9399 event.xexpose.x, event.xexpose.y,
9400 event.xexpose.width, event.xexpose.height);
9401 }
9402 else
9403 {
9404 #ifdef USE_TOOLKIT_SCROLL_BARS
9405 /* Dispatch event to the widget. */
9406 goto OTHER;
9407 #else /* not USE_TOOLKIT_SCROLL_BARS */
9408 struct scroll_bar *bar
9409 = x_window_to_scroll_bar (event.xexpose.window);
9410
9411 if (bar)
9412 x_scroll_bar_expose (bar, &event);
9413 #ifdef USE_X_TOOLKIT
9414 else
9415 goto OTHER;
9416 #endif /* USE_X_TOOLKIT */
9417 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9418 }
9419 break;
9420
9421 case GraphicsExpose: /* This occurs when an XCopyArea's
9422 source area was obscured or not
9423 available.*/
9424 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
9425 if (f)
9426 {
9427 expose_frame (f,
9428 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9429 event.xgraphicsexpose.width,
9430 event.xgraphicsexpose.height);
9431 }
9432 #ifdef USE_X_TOOLKIT
9433 else
9434 goto OTHER;
9435 #endif /* USE_X_TOOLKIT */
9436 break;
9437
9438 case NoExpose: /* This occurs when an XCopyArea's
9439 source area was completely
9440 available */
9441 break;
9442
9443 case UnmapNotify:
9444 /* Redo the mouse-highlight after the tooltip has gone. */
9445 if (event.xmap.window == tip_window)
9446 {
9447 tip_window = 0;
9448 redo_mouse_highlight ();
9449 }
9450
9451 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
9452 if (f) /* F may no longer exist if
9453 the frame was deleted. */
9454 {
9455 /* While a frame is unmapped, display generation is
9456 disabled; you don't want to spend time updating a
9457 display that won't ever be seen. */
9458 f->async_visible = 0;
9459 /* We can't distinguish, from the event, whether the window
9460 has become iconified or invisible. So assume, if it
9461 was previously visible, than now it is iconified.
9462 But x_make_frame_invisible clears both
9463 the visible flag and the iconified flag;
9464 and that way, we know the window is not iconified now. */
9465 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
9466 {
9467 f->async_iconified = 1;
9468
9469 bufp->kind = iconify_event;
9470 XSETFRAME (bufp->frame_or_window, f);
9471 bufp->arg = Qnil;
9472 bufp++;
9473 count++;
9474 numchars--;
9475 }
9476 }
9477 goto OTHER;
9478
9479 case MapNotify:
9480 if (event.xmap.window == tip_window)
9481 /* The tooltip has been drawn already. Avoid
9482 the SET_FRAME_GARBAGED below. */
9483 goto OTHER;
9484
9485 /* We use x_top_window_to_frame because map events can
9486 come for sub-windows and they don't mean that the
9487 frame is visible. */
9488 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
9489 if (f)
9490 {
9491 f->async_visible = 1;
9492 f->async_iconified = 0;
9493 f->output_data.x->has_been_visible = 1;
9494
9495 /* wait_reading_process_input will notice this and update
9496 the frame's display structures. */
9497 SET_FRAME_GARBAGED (f);
9498
9499 if (f->iconified)
9500 {
9501 bufp->kind = deiconify_event;
9502 XSETFRAME (bufp->frame_or_window, f);
9503 bufp->arg = Qnil;
9504 bufp++;
9505 count++;
9506 numchars--;
9507 }
9508 else if (! NILP (Vframe_list)
9509 && ! NILP (XCDR (Vframe_list)))
9510 /* Force a redisplay sooner or later
9511 to update the frame titles
9512 in case this is the second frame. */
9513 record_asynch_buffer_change ();
9514 }
9515 goto OTHER;
9516
9517 case KeyPress:
9518 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
9519
9520 #ifdef USE_MOTIF
9521 /* I couldn't find a way to prevent LessTif scroll bars
9522 from consuming key events. */
9523 if (f == 0)
9524 {
9525 Widget widget = XtWindowToWidget (dpyinfo->display,
9526 event.xkey.window);
9527 if (widget && XmIsScrollBar (widget))
9528 {
9529 widget = XtParent (widget);
9530 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9531 }
9532 }
9533 #endif /* USE_MOTIF */
9534
9535 if (f != 0)
9536 {
9537 KeySym keysym, orig_keysym;
9538 /* al%imercury@uunet.uu.net says that making this 81
9539 instead of 80 fixed a bug whereby meta chars made
9540 his Emacs hang.
9541
9542 It seems that some version of XmbLookupString has
9543 a bug of not returning XBufferOverflow in
9544 status_return even if the input is too long to
9545 fit in 81 bytes. So, we must prepare sufficient
9546 bytes for copy_buffer. 513 bytes (256 chars for
9547 two-byte character set) seems to be a faily good
9548 approximation. -- 2000.8.10 handa@etl.go.jp */
9549 unsigned char copy_buffer[513];
9550 unsigned char *copy_bufptr = copy_buffer;
9551 int copy_bufsiz = sizeof (copy_buffer);
9552 int modifiers;
9553
9554 event.xkey.state
9555 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9556 extra_keyboard_modifiers);
9557 modifiers = event.xkey.state;
9558
9559 /* This will have to go some day... */
9560
9561 /* make_lispy_event turns chars into control chars.
9562 Don't do it here because XLookupString is too eager. */
9563 event.xkey.state &= ~ControlMask;
9564 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9565 | dpyinfo->super_mod_mask
9566 | dpyinfo->hyper_mod_mask
9567 | dpyinfo->alt_mod_mask);
9568
9569 /* In case Meta is ComposeCharacter,
9570 clear its status. According to Markus Ehrnsperger
9571 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9572 this enables ComposeCharacter to work whether or
9573 not it is combined with Meta. */
9574 if (modifiers & dpyinfo->meta_mod_mask)
9575 bzero (&compose_status, sizeof (compose_status));
9576
9577 #ifdef HAVE_X_I18N
9578 if (FRAME_XIC (f))
9579 {
9580 Status status_return;
9581
9582 nbytes = XmbLookupString (FRAME_XIC (f),
9583 &event.xkey, copy_bufptr,
9584 copy_bufsiz, &keysym,
9585 &status_return);
9586 if (status_return == XBufferOverflow)
9587 {
9588 copy_bufsiz = nbytes + 1;
9589 copy_bufptr = (char *) alloca (copy_bufsiz);
9590 nbytes = XmbLookupString (FRAME_XIC (f),
9591 &event.xkey, copy_bufptr,
9592 copy_bufsiz, &keysym,
9593 &status_return);
9594 }
9595
9596 if (status_return == XLookupNone)
9597 break;
9598 else if (status_return == XLookupChars)
9599 {
9600 keysym = NoSymbol;
9601 modifiers = 0;
9602 }
9603 else if (status_return != XLookupKeySym
9604 && status_return != XLookupBoth)
9605 abort ();
9606 }
9607 else
9608 nbytes = XLookupString (&event.xkey, copy_bufptr,
9609 copy_bufsiz, &keysym,
9610 &compose_status);
9611 #else
9612 nbytes = XLookupString (&event.xkey, copy_bufptr,
9613 copy_bufsiz, &keysym,
9614 &compose_status);
9615 #endif
9616
9617 orig_keysym = keysym;
9618
9619 if (numchars > 1)
9620 {
9621 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9622 || keysym == XK_Delete
9623 #ifdef XK_ISO_Left_Tab
9624 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
9625 #endif
9626 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
9627 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9628 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
9629 #ifdef HPUX
9630 /* This recognizes the "extended function keys".
9631 It seems there's no cleaner way.
9632 Test IsModifierKey to avoid handling mode_switch
9633 incorrectly. */
9634 || ((unsigned) (keysym) >= XK_Select
9635 && (unsigned)(keysym) < XK_KP_Space)
9636 #endif
9637 #ifdef XK_dead_circumflex
9638 || orig_keysym == XK_dead_circumflex
9639 #endif
9640 #ifdef XK_dead_grave
9641 || orig_keysym == XK_dead_grave
9642 #endif
9643 #ifdef XK_dead_tilde
9644 || orig_keysym == XK_dead_tilde
9645 #endif
9646 #ifdef XK_dead_diaeresis
9647 || orig_keysym == XK_dead_diaeresis
9648 #endif
9649 #ifdef XK_dead_macron
9650 || orig_keysym == XK_dead_macron
9651 #endif
9652 #ifdef XK_dead_degree
9653 || orig_keysym == XK_dead_degree
9654 #endif
9655 #ifdef XK_dead_acute
9656 || orig_keysym == XK_dead_acute
9657 #endif
9658 #ifdef XK_dead_cedilla
9659 || orig_keysym == XK_dead_cedilla
9660 #endif
9661 #ifdef XK_dead_breve
9662 || orig_keysym == XK_dead_breve
9663 #endif
9664 #ifdef XK_dead_ogonek
9665 || orig_keysym == XK_dead_ogonek
9666 #endif
9667 #ifdef XK_dead_caron
9668 || orig_keysym == XK_dead_caron
9669 #endif
9670 #ifdef XK_dead_doubleacute
9671 || orig_keysym == XK_dead_doubleacute
9672 #endif
9673 #ifdef XK_dead_abovedot
9674 || orig_keysym == XK_dead_abovedot
9675 #endif
9676 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9677 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9678 /* Any "vendor-specific" key is ok. */
9679 || (orig_keysym & (1 << 28)))
9680 && ! (IsModifierKey (orig_keysym)
9681 #ifndef HAVE_X11R5
9682 #ifdef XK_Mode_switch
9683 || ((unsigned)(orig_keysym) == XK_Mode_switch)
9684 #endif
9685 #ifdef XK_Num_Lock
9686 || ((unsigned)(orig_keysym) == XK_Num_Lock)
9687 #endif
9688 #endif /* not HAVE_X11R5 */
9689 ))
9690 {
9691 if (temp_index == sizeof temp_buffer / sizeof (short))
9692 temp_index = 0;
9693 temp_buffer[temp_index++] = keysym;
9694 bufp->kind = non_ascii_keystroke;
9695 bufp->code = keysym;
9696 XSETFRAME (bufp->frame_or_window, f);
9697 bufp->arg = Qnil;
9698 bufp->modifiers
9699 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9700 modifiers);
9701 bufp->timestamp = event.xkey.time;
9702 bufp++;
9703 count++;
9704 numchars--;
9705 }
9706 else if (numchars > nbytes)
9707 {
9708 register int i;
9709 register int c;
9710 unsigned char *p, *pend;
9711 int nchars, len;
9712
9713 for (i = 0; i < nbytes; i++)
9714 {
9715 if (temp_index == (sizeof temp_buffer
9716 / sizeof (short)))
9717 temp_index = 0;
9718 temp_buffer[temp_index++] = copy_bufptr[i];
9719 }
9720
9721 if (/* If the event is not from XIM, */
9722 event.xkey.keycode != 0
9723 /* or the current locale doesn't request
9724 decoding of the intup data, ... */
9725 || coding.type == coding_type_raw_text
9726 || coding.type == coding_type_no_conversion)
9727 {
9728 /* ... we can use the input data as is. */
9729 nchars = nbytes;
9730 }
9731 else
9732 {
9733 /* We have to decode the input data. */
9734 int require;
9735 unsigned char *p;
9736
9737 require = decoding_buffer_size (&coding, nbytes);
9738 p = (unsigned char *) alloca (require);
9739 coding.mode |= CODING_MODE_LAST_BLOCK;
9740 decode_coding (&coding, copy_bufptr, p,
9741 nbytes, require);
9742 nbytes = coding.produced;
9743 nchars = coding.produced_char;
9744 copy_bufptr = p;
9745 }
9746
9747 /* Convert the input data to a sequence of
9748 character events. */
9749 for (i = 0; i < nbytes; i += len)
9750 {
9751 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
9752 nbytes - i, len);
9753 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
9754 ? ascii_keystroke
9755 : multibyte_char_keystroke);
9756 bufp->code = c;
9757 XSETFRAME (bufp->frame_or_window, f);
9758 bufp->arg = Qnil;
9759 bufp->modifiers
9760 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9761 modifiers);
9762 bufp->timestamp = event.xkey.time;
9763 bufp++;
9764 }
9765
9766 count += nchars;
9767 numchars -= nchars;
9768
9769 if (keysym == NoSymbol)
9770 break;
9771 }
9772 else
9773 abort ();
9774 }
9775 else
9776 abort ();
9777 }
9778 #ifdef HAVE_X_I18N
9779 /* Don't dispatch this event since XtDispatchEvent calls
9780 XFilterEvent, and two calls in a row may freeze the
9781 client. */
9782 break;
9783 #else
9784 goto OTHER;
9785 #endif
9786
9787 case KeyRelease:
9788 #ifdef HAVE_X_I18N
9789 /* Don't dispatch this event since XtDispatchEvent calls
9790 XFilterEvent, and two calls in a row may freeze the
9791 client. */
9792 break;
9793 #else
9794 goto OTHER;
9795 #endif
9796
9797 /* Here's a possible interpretation of the whole
9798 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9799 you get a FocusIn event, you have to get a FocusOut
9800 event before you relinquish the focus. If you
9801 haven't received a FocusIn event, then a mere
9802 LeaveNotify is enough to free you. */
9803
9804 case EnterNotify:
9805 {
9806 int from_menu_bar_p = 0;
9807
9808 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
9809
9810 #ifdef LESSTIF_VERSION
9811 /* When clicking outside of a menu bar popup to close
9812 it, we get a FocusIn/ EnterNotify sequence of
9813 events. The flag event.xcrossing.focus is not set
9814 in the EnterNotify event of that sequence because
9815 the focus is in the menu bar,
9816 event.xcrossing.window is the frame's X window.
9817 Unconditionally setting the focus frame to null in
9818 this case is not the right thing, because no event
9819 follows that could set the focus frame to the right
9820 value.
9821
9822 This could be a LessTif bug, but I wasn't able to
9823 reproduce the behavior in a simple test program.
9824
9825 (gerd, LessTif 0.88.1). */
9826
9827 if (!event.xcrossing.focus
9828 && f
9829 && f->output_data.x->menubar_widget)
9830 {
9831 Window focus;
9832 int revert;
9833
9834 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9835 if (focus == XtWindow (f->output_data.x->menubar_widget))
9836 from_menu_bar_p = 1;
9837 }
9838 #endif /* LESSTIF_VERSION */
9839
9840 if (event.xcrossing.focus || from_menu_bar_p)
9841 {
9842 /* Avoid nasty pop/raise loops. */
9843 if (f && (!(f->auto_raise)
9844 || !(f->auto_lower)
9845 || (event.xcrossing.time - enter_timestamp) > 500))
9846 {
9847 x_new_focus_frame (dpyinfo, f);
9848 enter_timestamp = event.xcrossing.time;
9849 }
9850 }
9851 else if (f == dpyinfo->x_focus_frame)
9852 x_new_focus_frame (dpyinfo, 0);
9853
9854 /* EnterNotify counts as mouse movement,
9855 so update things that depend on mouse position. */
9856 if (f && !f->output_data.x->busy_p)
9857 note_mouse_movement (f, &event.xmotion);
9858 goto OTHER;
9859 }
9860
9861 case FocusIn:
9862 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
9863 if (event.xfocus.detail != NotifyPointer)
9864 dpyinfo->x_focus_event_frame = f;
9865 if (f)
9866 {
9867 x_new_focus_frame (dpyinfo, f);
9868
9869 /* Don't stop displaying the initial startup message
9870 for a switch-frame event we don't need. */
9871 if (GC_NILP (Vterminal_frame)
9872 && GC_CONSP (Vframe_list)
9873 && !GC_NILP (XCDR (Vframe_list)))
9874 {
9875 bufp->kind = FOCUS_IN_EVENT;
9876 XSETFRAME (bufp->frame_or_window, f);
9877 bufp->arg = Qnil;
9878 ++bufp, ++count, --numchars;
9879 }
9880 }
9881
9882 #ifdef HAVE_X_I18N
9883 if (f && FRAME_XIC (f))
9884 XSetICFocus (FRAME_XIC (f));
9885 #endif
9886
9887 goto OTHER;
9888
9889 case LeaveNotify:
9890 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
9891 if (f)
9892 {
9893 Lisp_Object frame;
9894 int from_menu_bar_p = 0;
9895
9896 if (f == dpyinfo->mouse_face_mouse_frame)
9897 {
9898 /* If we move outside the frame, then we're
9899 certainly no longer on any text in the frame. */
9900 clear_mouse_face (dpyinfo);
9901 dpyinfo->mouse_face_mouse_frame = 0;
9902 }
9903
9904 /* Generate a nil HELP_EVENT to cancel a help-echo.
9905 Do it only if there's something to cancel.
9906 Otherwise, the startup message is cleared when
9907 the mouse leaves the frame. */
9908 if (any_help_event_p)
9909 {
9910 Lisp_Object frame;
9911 int n;
9912
9913 XSETFRAME (frame, f);
9914 n = gen_help_event (bufp, numchars,
9915 Qnil, frame, Qnil, Qnil, 0);
9916 bufp += n, count += n, numchars -= n;
9917 }
9918
9919 #ifdef LESSTIF_VERSION
9920 /* Please see the comment at the start of the
9921 EnterNotify case. */
9922 if (!event.xcrossing.focus
9923 && f->output_data.x->menubar_widget)
9924 {
9925 Window focus;
9926 int revert;
9927 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9928 if (focus == XtWindow (f->output_data.x->menubar_widget))
9929 from_menu_bar_p = 1;
9930 }
9931 #endif /* LESSTIF_VERSION */
9932
9933 if (event.xcrossing.focus || from_menu_bar_p)
9934 x_mouse_leave (dpyinfo);
9935 else
9936 {
9937 if (f == dpyinfo->x_focus_event_frame)
9938 dpyinfo->x_focus_event_frame = 0;
9939 if (f == dpyinfo->x_focus_frame)
9940 x_new_focus_frame (dpyinfo, 0);
9941 }
9942 }
9943 goto OTHER;
9944
9945 case FocusOut:
9946 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
9947 if (event.xfocus.detail != NotifyPointer
9948 && f == dpyinfo->x_focus_event_frame)
9949 dpyinfo->x_focus_event_frame = 0;
9950 if (f && f == dpyinfo->x_focus_frame)
9951 x_new_focus_frame (dpyinfo, 0);
9952
9953 #ifdef HAVE_X_I18N
9954 if (f && FRAME_XIC (f))
9955 XUnsetICFocus (FRAME_XIC (f));
9956 #endif
9957
9958 goto OTHER;
9959
9960 case MotionNotify:
9961 {
9962 previous_help_echo = help_echo;
9963 help_echo = help_echo_object = help_echo_window = Qnil;
9964 help_echo_pos = -1;
9965
9966 if (dpyinfo->grabbed && last_mouse_frame
9967 && FRAME_LIVE_P (last_mouse_frame))
9968 f = last_mouse_frame;
9969 else
9970 f = x_window_to_frame (dpyinfo, event.xmotion.window);
9971
9972 if (f)
9973 note_mouse_movement (f, &event.xmotion);
9974 else
9975 {
9976 #ifndef USE_TOOLKIT_SCROLL_BARS
9977 struct scroll_bar *bar
9978 = x_window_to_scroll_bar (event.xmotion.window);
9979
9980 if (bar)
9981 x_scroll_bar_note_movement (bar, &event);
9982 #endif /* USE_TOOLKIT_SCROLL_BARS */
9983
9984 /* If we move outside the frame, then we're
9985 certainly no longer on any text in the frame. */
9986 clear_mouse_face (dpyinfo);
9987 }
9988
9989 /* If the contents of the global variable help_echo
9990 has changed, generate a HELP_EVENT. */
9991 if (!NILP (help_echo)
9992 || !NILP (previous_help_echo))
9993 {
9994 Lisp_Object frame;
9995 int n;
9996
9997 if (f)
9998 XSETFRAME (frame, f);
9999 else
10000 frame = Qnil;
10001
10002 any_help_event_p = 1;
10003 n = gen_help_event (bufp, numchars, help_echo, frame,
10004 help_echo_window, help_echo_object,
10005 help_echo_pos);
10006 bufp += n, count += n, numchars -= n;
10007 }
10008
10009 goto OTHER;
10010 }
10011
10012 case ConfigureNotify:
10013 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10014 if (f)
10015 {
10016 #ifndef USE_X_TOOLKIT
10017 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10018 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
10019
10020 /* In the toolkit version, change_frame_size
10021 is called by the code that handles resizing
10022 of the EmacsFrame widget. */
10023
10024 /* Even if the number of character rows and columns has
10025 not changed, the font size may have changed, so we need
10026 to check the pixel dimensions as well. */
10027 if (columns != f->width
10028 || rows != f->height
10029 || event.xconfigure.width != f->output_data.x->pixel_width
10030 || event.xconfigure.height != f->output_data.x->pixel_height)
10031 {
10032 change_frame_size (f, rows, columns, 0, 1, 0);
10033 SET_FRAME_GARBAGED (f);
10034 cancel_mouse_face (f);
10035 }
10036 #endif
10037
10038 f->output_data.x->pixel_width = event.xconfigure.width;
10039 f->output_data.x->pixel_height = event.xconfigure.height;
10040
10041 /* What we have now is the position of Emacs's own window.
10042 Convert that to the position of the window manager window. */
10043 x_real_positions (f, &f->output_data.x->left_pos,
10044 &f->output_data.x->top_pos);
10045
10046 #ifdef HAVE_X_I18N
10047 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10048 xic_set_statusarea (f);
10049 #endif
10050
10051 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10052 {
10053 /* Since the WM decorations come below top_pos now,
10054 we must put them below top_pos in the future. */
10055 f->output_data.x->win_gravity = NorthWestGravity;
10056 x_wm_set_size_hint (f, (long) 0, 0);
10057 }
10058 #ifdef USE_MOTIF
10059 /* Some window managers pass (0,0) as the location of
10060 the window, and the Motif event handler stores it
10061 in the emacs widget, which messes up Motif menus. */
10062 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10063 {
10064 event.xconfigure.x = f->output_data.x->widget->core.x;
10065 event.xconfigure.y = f->output_data.x->widget->core.y;
10066 }
10067 #endif /* USE_MOTIF */
10068 }
10069 goto OTHER;
10070
10071 case ButtonPress:
10072 case ButtonRelease:
10073 {
10074 /* If we decide we want to generate an event to be seen
10075 by the rest of Emacs, we put it here. */
10076 struct input_event emacs_event;
10077 int tool_bar_p = 0;
10078
10079 emacs_event.kind = no_event;
10080 bzero (&compose_status, sizeof (compose_status));
10081
10082 if (dpyinfo->grabbed
10083 && last_mouse_frame
10084 && FRAME_LIVE_P (last_mouse_frame))
10085 f = last_mouse_frame;
10086 else
10087 f = x_window_to_frame (dpyinfo, event.xbutton.window);
10088
10089 if (f)
10090 {
10091 /* Is this in the tool-bar? */
10092 if (WINDOWP (f->tool_bar_window)
10093 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
10094 {
10095 Lisp_Object window;
10096 int p, x, y;
10097
10098 x = event.xbutton.x;
10099 y = event.xbutton.y;
10100
10101 /* Set x and y. */
10102 window = window_from_coordinates (f, x, y, &p, 1);
10103 if (EQ (window, f->tool_bar_window))
10104 {
10105 x_handle_tool_bar_click (f, &event.xbutton);
10106 tool_bar_p = 1;
10107 }
10108 }
10109
10110 if (!tool_bar_p)
10111 if (!dpyinfo->x_focus_frame
10112 || f == dpyinfo->x_focus_frame)
10113 construct_mouse_click (&emacs_event, &event, f);
10114 }
10115 else
10116 {
10117 #ifndef USE_TOOLKIT_SCROLL_BARS
10118 struct scroll_bar *bar
10119 = x_window_to_scroll_bar (event.xbutton.window);
10120
10121 if (bar)
10122 x_scroll_bar_handle_click (bar, &event, &emacs_event);
10123 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10124 }
10125
10126 if (event.type == ButtonPress)
10127 {
10128 dpyinfo->grabbed |= (1 << event.xbutton.button);
10129 last_mouse_frame = f;
10130 /* Ignore any mouse motion that happened
10131 before this event; any subsequent mouse-movement
10132 Emacs events should reflect only motion after
10133 the ButtonPress. */
10134 if (f != 0)
10135 f->mouse_moved = 0;
10136
10137 if (!tool_bar_p)
10138 last_tool_bar_item = -1;
10139 }
10140 else
10141 {
10142 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
10143 }
10144
10145 if (numchars >= 1 && emacs_event.kind != no_event)
10146 {
10147 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10148 bufp++;
10149 count++;
10150 numchars--;
10151 }
10152
10153 #ifdef USE_X_TOOLKIT
10154 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10155 /* For a down-event in the menu bar,
10156 don't pass it to Xt right now.
10157 Instead, save it away
10158 and we will pass it to Xt from kbd_buffer_get_event.
10159 That way, we can run some Lisp code first. */
10160 if (f && event.type == ButtonPress
10161 /* Verify the event is really within the menu bar
10162 and not just sent to it due to grabbing. */
10163 && event.xbutton.x >= 0
10164 && event.xbutton.x < f->output_data.x->pixel_width
10165 && event.xbutton.y >= 0
10166 && event.xbutton.y < f->output_data.x->menubar_height
10167 && event.xbutton.same_screen)
10168 {
10169 SET_SAVED_BUTTON_EVENT;
10170 XSETFRAME (last_mouse_press_frame, f);
10171 }
10172 else if (event.type == ButtonPress)
10173 {
10174 last_mouse_press_frame = Qnil;
10175 goto OTHER;
10176 }
10177
10178 #ifdef USE_MOTIF /* This should do not harm for Lucid,
10179 but I am trying to be cautious. */
10180 else if (event.type == ButtonRelease)
10181 {
10182 if (!NILP (last_mouse_press_frame))
10183 {
10184 f = XFRAME (last_mouse_press_frame);
10185 if (f->output_data.x)
10186 SET_SAVED_BUTTON_EVENT;
10187 }
10188 else
10189 goto OTHER;
10190 }
10191 #endif /* USE_MOTIF */
10192 else
10193 goto OTHER;
10194 #endif /* USE_X_TOOLKIT */
10195 }
10196 break;
10197
10198 case CirculateNotify:
10199 goto OTHER;
10200
10201 case CirculateRequest:
10202 goto OTHER;
10203
10204 case VisibilityNotify:
10205 goto OTHER;
10206
10207 case MappingNotify:
10208 /* Someone has changed the keyboard mapping - update the
10209 local cache. */
10210 switch (event.xmapping.request)
10211 {
10212 case MappingModifier:
10213 x_find_modifier_meanings (dpyinfo);
10214 /* This is meant to fall through. */
10215 case MappingKeyboard:
10216 XRefreshKeyboardMapping (&event.xmapping);
10217 }
10218 goto OTHER;
10219
10220 default:
10221 OTHER:
10222 #ifdef USE_X_TOOLKIT
10223 BLOCK_INPUT;
10224 XtDispatchEvent (&event);
10225 UNBLOCK_INPUT;
10226 #endif /* USE_X_TOOLKIT */
10227 break;
10228 }
10229 }
10230 }
10231
10232 out:;
10233
10234 /* On some systems, an X bug causes Emacs to get no more events
10235 when the window is destroyed. Detect that. (1994.) */
10236 if (! event_found)
10237 {
10238 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10239 One XNOOP in 100 loops will make Emacs terminate.
10240 B. Bretthauer, 1994 */
10241 x_noop_count++;
10242 if (x_noop_count >= 100)
10243 {
10244 x_noop_count=0;
10245
10246 if (next_noop_dpyinfo == 0)
10247 next_noop_dpyinfo = x_display_list;
10248
10249 XNoOp (next_noop_dpyinfo->display);
10250
10251 /* Each time we get here, cycle through the displays now open. */
10252 next_noop_dpyinfo = next_noop_dpyinfo->next;
10253 }
10254 }
10255
10256 /* If the focus was just given to an auto-raising frame,
10257 raise it now. */
10258 /* ??? This ought to be able to handle more than one such frame. */
10259 if (pending_autoraise_frame)
10260 {
10261 x_raise_frame (pending_autoraise_frame);
10262 pending_autoraise_frame = 0;
10263 }
10264
10265 UNBLOCK_INPUT;
10266 --handling_signal;
10267 return count;
10268 }
10269
10270
10271
10272 \f
10273 /***********************************************************************
10274 Text Cursor
10275 ***********************************************************************/
10276
10277 /* Note if the text cursor of window W has been overwritten by a
10278 drawing operation that outputs N glyphs starting at HPOS in the
10279 line given by output_cursor.vpos. N < 0 means all the rest of the
10280 line after HPOS has been written. */
10281
10282 static void
10283 note_overwritten_text_cursor (w, hpos, n)
10284 struct window *w;
10285 int hpos, n;
10286 {
10287 if (updated_area == TEXT_AREA
10288 && output_cursor.vpos == w->phys_cursor.vpos
10289 && hpos <= w->phys_cursor.hpos
10290 && (n < 0
10291 || hpos + n > w->phys_cursor.hpos))
10292 w->phys_cursor_on_p = 0;
10293 }
10294
10295
10296 /* Set clipping for output in glyph row ROW. W is the window in which
10297 we operate. GC is the graphics context to set clipping in.
10298 WHOLE_LINE_P non-zero means include the areas used for truncation
10299 mark display and alike in the clipping rectangle.
10300
10301 ROW may be a text row or, e.g., a mode line. Text rows must be
10302 clipped to the interior of the window dedicated to text display,
10303 mode lines must be clipped to the whole window. */
10304
10305 static void
10306 x_clip_to_row (w, row, gc, whole_line_p)
10307 struct window *w;
10308 struct glyph_row *row;
10309 GC gc;
10310 int whole_line_p;
10311 {
10312 struct frame *f = XFRAME (WINDOW_FRAME (w));
10313 XRectangle clip_rect;
10314 int window_x, window_y, window_width, window_height;
10315
10316 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
10317
10318 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10319 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10320 clip_rect.y = max (clip_rect.y, window_y);
10321 clip_rect.width = window_width;
10322 clip_rect.height = row->visible_height;
10323
10324 /* If clipping to the whole line, including trunc marks, extend
10325 the rectangle to the left and increase its width. */
10326 if (whole_line_p)
10327 {
10328 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10329 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
10330 }
10331
10332 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
10333 }
10334
10335
10336 /* Draw a hollow box cursor on window W in glyph row ROW. */
10337
10338 static void
10339 x_draw_hollow_cursor (w, row)
10340 struct window *w;
10341 struct glyph_row *row;
10342 {
10343 struct frame *f = XFRAME (WINDOW_FRAME (w));
10344 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10345 Display *dpy = FRAME_X_DISPLAY (f);
10346 int x, y, wd, h;
10347 XGCValues xgcv;
10348 struct glyph *cursor_glyph;
10349 GC gc;
10350
10351 /* Compute frame-relative coordinates from window-relative
10352 coordinates. */
10353 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10354 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10355 + row->ascent - w->phys_cursor_ascent);
10356 h = row->height - 1;
10357
10358 /* Get the glyph the cursor is on. If we can't tell because
10359 the current matrix is invalid or such, give up. */
10360 cursor_glyph = get_phys_cursor_glyph (w);
10361 if (cursor_glyph == NULL)
10362 return;
10363
10364 /* Compute the width of the rectangle to draw. If on a stretch
10365 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10366 rectangle as wide as the glyph, but use a canonical character
10367 width instead. */
10368 wd = cursor_glyph->pixel_width - 1;
10369 if (cursor_glyph->type == STRETCH_GLYPH
10370 && !x_stretch_cursor_p)
10371 wd = min (CANON_X_UNIT (f), wd);
10372
10373 /* The foreground of cursor_gc is typically the same as the normal
10374 background color, which can cause the cursor box to be invisible. */
10375 xgcv.foreground = f->output_data.x->cursor_pixel;
10376 if (dpyinfo->scratch_cursor_gc)
10377 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10378 else
10379 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10380 GCForeground, &xgcv);
10381 gc = dpyinfo->scratch_cursor_gc;
10382
10383 /* Set clipping, draw the rectangle, and reset clipping again. */
10384 x_clip_to_row (w, row, gc, 0);
10385 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10386 XSetClipMask (dpy, gc, None);
10387 }
10388
10389
10390 /* Draw a bar cursor on window W in glyph row ROW.
10391
10392 Implementation note: One would like to draw a bar cursor with an
10393 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10394 Unfortunately, I didn't find a font yet that has this property set.
10395 --gerd. */
10396
10397 static void
10398 x_draw_bar_cursor (w, row, width)
10399 struct window *w;
10400 struct glyph_row *row;
10401 int width;
10402 {
10403 struct frame *f = XFRAME (w->frame);
10404 struct glyph *cursor_glyph;
10405 GC gc;
10406 int x;
10407 unsigned long mask;
10408 XGCValues xgcv;
10409 Display *dpy;
10410 Window window;
10411
10412 /* If cursor is out of bounds, don't draw garbage. This can happen
10413 in mini-buffer windows when switching between echo area glyphs
10414 and mini-buffer. */
10415 cursor_glyph = get_phys_cursor_glyph (w);
10416 if (cursor_glyph == NULL)
10417 return;
10418
10419 /* If on an image, draw like a normal cursor. That's usually better
10420 visible than drawing a bar, esp. if the image is large so that
10421 the bar might not be in the window. */
10422 if (cursor_glyph->type == IMAGE_GLYPH)
10423 {
10424 struct glyph_row *row;
10425 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
10426 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
10427 }
10428 else
10429 {
10430 xgcv.background = f->output_data.x->cursor_pixel;
10431 xgcv.foreground = f->output_data.x->cursor_pixel;
10432 xgcv.graphics_exposures = 0;
10433 mask = GCForeground | GCBackground | GCGraphicsExposures;
10434 dpy = FRAME_X_DISPLAY (f);
10435 window = FRAME_X_WINDOW (f);
10436 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
10437
10438 if (gc)
10439 XChangeGC (dpy, gc, mask, &xgcv);
10440 else
10441 {
10442 gc = XCreateGC (dpy, window, mask, &xgcv);
10443 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10444 }
10445
10446 if (width < 0)
10447 width = f->output_data.x->cursor_width;
10448
10449 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10450 x_clip_to_row (w, row, gc, 0);
10451 XFillRectangle (dpy, window, gc,
10452 x,
10453 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
10454 min (cursor_glyph->pixel_width, width),
10455 row->height);
10456 XSetClipMask (dpy, gc, None);
10457 }
10458 }
10459
10460
10461 /* Clear the cursor of window W to background color, and mark the
10462 cursor as not shown. This is used when the text where the cursor
10463 is is about to be rewritten. */
10464
10465 static void
10466 x_clear_cursor (w)
10467 struct window *w;
10468 {
10469 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10470 x_update_window_cursor (w, 0);
10471 }
10472
10473
10474 /* Draw the cursor glyph of window W in glyph row ROW. See the
10475 comment of x_draw_glyphs for the meaning of HL. */
10476
10477 static void
10478 x_draw_phys_cursor_glyph (w, row, hl)
10479 struct window *w;
10480 struct glyph_row *row;
10481 enum draw_glyphs_face hl;
10482 {
10483 /* If cursor hpos is out of bounds, don't draw garbage. This can
10484 happen in mini-buffer windows when switching between echo area
10485 glyphs and mini-buffer. */
10486 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10487 {
10488 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10489 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10490 hl, 0, 0, 0);
10491
10492 /* When we erase the cursor, and ROW is overlapped by other
10493 rows, make sure that these overlapping parts of other rows
10494 are redrawn. */
10495 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10496 {
10497 if (row > w->current_matrix->rows
10498 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10499 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10500
10501 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10502 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10503 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10504 }
10505 }
10506 }
10507
10508
10509 /* Erase the image of a cursor of window W from the screen. */
10510
10511 static void
10512 x_erase_phys_cursor (w)
10513 struct window *w;
10514 {
10515 struct frame *f = XFRAME (w->frame);
10516 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10517 int hpos = w->phys_cursor.hpos;
10518 int vpos = w->phys_cursor.vpos;
10519 int mouse_face_here_p = 0;
10520 struct glyph_matrix *active_glyphs = w->current_matrix;
10521 struct glyph_row *cursor_row;
10522 struct glyph *cursor_glyph;
10523 enum draw_glyphs_face hl;
10524
10525 /* No cursor displayed or row invalidated => nothing to do on the
10526 screen. */
10527 if (w->phys_cursor_type == NO_CURSOR)
10528 goto mark_cursor_off;
10529
10530 /* VPOS >= active_glyphs->nrows means that window has been resized.
10531 Don't bother to erase the cursor. */
10532 if (vpos >= active_glyphs->nrows)
10533 goto mark_cursor_off;
10534
10535 /* If row containing cursor is marked invalid, there is nothing we
10536 can do. */
10537 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10538 if (!cursor_row->enabled_p)
10539 goto mark_cursor_off;
10540
10541 /* This can happen when the new row is shorter than the old one.
10542 In this case, either x_draw_glyphs or clear_end_of_line
10543 should have cleared the cursor. Note that we wouldn't be
10544 able to erase the cursor in this case because we don't have a
10545 cursor glyph at hand. */
10546 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10547 goto mark_cursor_off;
10548
10549 /* If the cursor is in the mouse face area, redisplay that when
10550 we clear the cursor. */
10551 if (! NILP (dpyinfo->mouse_face_window)
10552 && w == XWINDOW (dpyinfo->mouse_face_window)
10553 && (vpos > dpyinfo->mouse_face_beg_row
10554 || (vpos == dpyinfo->mouse_face_beg_row
10555 && hpos >= dpyinfo->mouse_face_beg_col))
10556 && (vpos < dpyinfo->mouse_face_end_row
10557 || (vpos == dpyinfo->mouse_face_end_row
10558 && hpos < dpyinfo->mouse_face_end_col))
10559 /* Don't redraw the cursor's spot in mouse face if it is at the
10560 end of a line (on a newline). The cursor appears there, but
10561 mouse highlighting does not. */
10562 && cursor_row->used[TEXT_AREA] > hpos)
10563 mouse_face_here_p = 1;
10564
10565 /* Maybe clear the display under the cursor. */
10566 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10567 {
10568 int x;
10569 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10570
10571 cursor_glyph = get_phys_cursor_glyph (w);
10572 if (cursor_glyph == NULL)
10573 goto mark_cursor_off;
10574
10575 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10576
10577 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10578 x,
10579 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
10580 cursor_row->y)),
10581 cursor_glyph->pixel_width,
10582 cursor_row->visible_height,
10583 False);
10584 }
10585
10586 /* Erase the cursor by redrawing the character underneath it. */
10587 if (mouse_face_here_p)
10588 hl = DRAW_MOUSE_FACE;
10589 else if (cursor_row->inverse_p)
10590 hl = DRAW_INVERSE_VIDEO;
10591 else
10592 hl = DRAW_NORMAL_TEXT;
10593 x_draw_phys_cursor_glyph (w, cursor_row, hl);
10594
10595 mark_cursor_off:
10596 w->phys_cursor_on_p = 0;
10597 w->phys_cursor_type = NO_CURSOR;
10598 }
10599
10600
10601 /* Display or clear cursor of window W. If ON is zero, clear the
10602 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10603 where to put the cursor is specified by HPOS, VPOS, X and Y. */
10604
10605 void
10606 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10607 struct window *w;
10608 int on, hpos, vpos, x, y;
10609 {
10610 struct frame *f = XFRAME (w->frame);
10611 int new_cursor_type;
10612 int new_cursor_width;
10613 struct glyph_matrix *current_glyphs;
10614 struct glyph_row *glyph_row;
10615 struct glyph *glyph;
10616
10617 /* This is pointless on invisible frames, and dangerous on garbaged
10618 windows and frames; in the latter case, the frame or window may
10619 be in the midst of changing its size, and x and y may be off the
10620 window. */
10621 if (! FRAME_VISIBLE_P (f)
10622 || FRAME_GARBAGED_P (f)
10623 || vpos >= w->current_matrix->nrows
10624 || hpos >= w->current_matrix->matrix_w)
10625 return;
10626
10627 /* If cursor is off and we want it off, return quickly. */
10628 if (!on && !w->phys_cursor_on_p)
10629 return;
10630
10631 current_glyphs = w->current_matrix;
10632 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10633 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10634
10635 /* If cursor row is not enabled, we don't really know where to
10636 display the cursor. */
10637 if (!glyph_row->enabled_p)
10638 {
10639 w->phys_cursor_on_p = 0;
10640 return;
10641 }
10642
10643 xassert (interrupt_input_blocked);
10644
10645 /* Set new_cursor_type to the cursor we want to be displayed. In a
10646 mini-buffer window, we want the cursor only to appear if we are
10647 reading input from this window. For the selected window, we want
10648 the cursor type given by the frame parameter. If explicitly
10649 marked off, draw no cursor. In all other cases, we want a hollow
10650 box cursor. */
10651 new_cursor_width = -1;
10652 if (cursor_in_echo_area
10653 && FRAME_HAS_MINIBUF_P (f)
10654 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
10655 {
10656 if (w == XWINDOW (echo_area_window))
10657 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10658 else
10659 new_cursor_type = HOLLOW_BOX_CURSOR;
10660 }
10661 else
10662 {
10663 if (w != XWINDOW (selected_window)
10664 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10665 {
10666 extern int cursor_in_non_selected_windows;
10667
10668 if (MINI_WINDOW_P (w) || !cursor_in_non_selected_windows)
10669 new_cursor_type = NO_CURSOR;
10670 else
10671 new_cursor_type = HOLLOW_BOX_CURSOR;
10672 }
10673 else if (w->cursor_off_p)
10674 new_cursor_type = NO_CURSOR;
10675 else
10676 {
10677 struct buffer *b = XBUFFER (w->buffer);
10678
10679 if (EQ (b->cursor_type, Qt))
10680 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10681 else
10682 new_cursor_type = x_specified_cursor_type (b->cursor_type,
10683 &new_cursor_width);
10684 }
10685 }
10686
10687 /* If cursor is currently being shown and we don't want it to be or
10688 it is in the wrong place, or the cursor type is not what we want,
10689 erase it. */
10690 if (w->phys_cursor_on_p
10691 && (!on
10692 || w->phys_cursor.x != x
10693 || w->phys_cursor.y != y
10694 || new_cursor_type != w->phys_cursor_type))
10695 x_erase_phys_cursor (w);
10696
10697 /* If the cursor is now invisible and we want it to be visible,
10698 display it. */
10699 if (on && !w->phys_cursor_on_p)
10700 {
10701 w->phys_cursor_ascent = glyph_row->ascent;
10702 w->phys_cursor_height = glyph_row->height;
10703
10704 /* Set phys_cursor_.* before x_draw_.* is called because some
10705 of them may need the information. */
10706 w->phys_cursor.x = x;
10707 w->phys_cursor.y = glyph_row->y;
10708 w->phys_cursor.hpos = hpos;
10709 w->phys_cursor.vpos = vpos;
10710 w->phys_cursor_type = new_cursor_type;
10711 w->phys_cursor_on_p = 1;
10712
10713 switch (new_cursor_type)
10714 {
10715 case HOLLOW_BOX_CURSOR:
10716 x_draw_hollow_cursor (w, glyph_row);
10717 break;
10718
10719 case FILLED_BOX_CURSOR:
10720 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10721 break;
10722
10723 case BAR_CURSOR:
10724 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
10725 break;
10726
10727 case NO_CURSOR:
10728 break;
10729
10730 default:
10731 abort ();
10732 }
10733
10734 #ifdef HAVE_X_I18N
10735 if (w == XWINDOW (f->selected_window))
10736 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10737 xic_set_preeditarea (w, x, y);
10738 #endif
10739 }
10740
10741 #ifndef XFlush
10742 if (updating_frame != f)
10743 XFlush (FRAME_X_DISPLAY (f));
10744 #endif
10745 }
10746
10747
10748 /* Display the cursor on window W, or clear it. X and Y are window
10749 relative pixel coordinates. HPOS and VPOS are glyph matrix
10750 positions. If W is not the selected window, display a hollow
10751 cursor. ON non-zero means display the cursor at X, Y which
10752 correspond to HPOS, VPOS, otherwise it is cleared. */
10753
10754 void
10755 x_display_cursor (w, on, hpos, vpos, x, y)
10756 struct window *w;
10757 int on, hpos, vpos, x, y;
10758 {
10759 BLOCK_INPUT;
10760 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
10761 UNBLOCK_INPUT;
10762 }
10763
10764
10765 /* Display the cursor on window W, or clear it, according to ON_P.
10766 Don't change the cursor's position. */
10767
10768 void
10769 x_update_cursor (f, on_p)
10770 struct frame *f;
10771 {
10772 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10773 }
10774
10775
10776 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
10777 in the window tree rooted at W. */
10778
10779 static void
10780 x_update_cursor_in_window_tree (w, on_p)
10781 struct window *w;
10782 int on_p;
10783 {
10784 while (w)
10785 {
10786 if (!NILP (w->hchild))
10787 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10788 else if (!NILP (w->vchild))
10789 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10790 else
10791 x_update_window_cursor (w, on_p);
10792
10793 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10794 }
10795 }
10796
10797
10798 /* Switch the display of W's cursor on or off, according to the value
10799 of ON. */
10800
10801 static void
10802 x_update_window_cursor (w, on)
10803 struct window *w;
10804 int on;
10805 {
10806 /* Don't update cursor in windows whose frame is in the process
10807 of being deleted. */
10808 if (w->current_matrix)
10809 {
10810 BLOCK_INPUT;
10811 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
10812 w->phys_cursor.x, w->phys_cursor.y);
10813 UNBLOCK_INPUT;
10814 }
10815 }
10816
10817
10818
10819 \f
10820 /* Icons. */
10821
10822 /* Refresh bitmap kitchen sink icon for frame F
10823 when we get an expose event for it. */
10824
10825 void
10826 refreshicon (f)
10827 struct frame *f;
10828 {
10829 /* Normally, the window manager handles this function. */
10830 }
10831
10832 /* Make the x-window of frame F use the gnu icon bitmap. */
10833
10834 int
10835 x_bitmap_icon (f, file)
10836 struct frame *f;
10837 Lisp_Object file;
10838 {
10839 int bitmap_id;
10840
10841 if (FRAME_X_WINDOW (f) == 0)
10842 return 1;
10843
10844 /* Free up our existing icon bitmap if any. */
10845 if (f->output_data.x->icon_bitmap > 0)
10846 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10847 f->output_data.x->icon_bitmap = 0;
10848
10849 if (STRINGP (file))
10850 bitmap_id = x_create_bitmap_from_file (f, file);
10851 else
10852 {
10853 /* Create the GNU bitmap if necessary. */
10854 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
10855 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
10856 = x_create_bitmap_from_data (f, gnu_bits,
10857 gnu_width, gnu_height);
10858
10859 /* The first time we create the GNU bitmap,
10860 this increments the ref-count one extra time.
10861 As a result, the GNU bitmap is never freed.
10862 That way, we don't have to worry about allocating it again. */
10863 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
10864
10865 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
10866 }
10867
10868 x_wm_set_icon_pixmap (f, bitmap_id);
10869 f->output_data.x->icon_bitmap = bitmap_id;
10870
10871 return 0;
10872 }
10873
10874
10875 /* Make the x-window of frame F use a rectangle with text.
10876 Use ICON_NAME as the text. */
10877
10878 int
10879 x_text_icon (f, icon_name)
10880 struct frame *f;
10881 char *icon_name;
10882 {
10883 if (FRAME_X_WINDOW (f) == 0)
10884 return 1;
10885
10886 #ifdef HAVE_X11R4
10887 {
10888 XTextProperty text;
10889 text.value = (unsigned char *) icon_name;
10890 text.encoding = XA_STRING;
10891 text.format = 8;
10892 text.nitems = strlen (icon_name);
10893 #ifdef USE_X_TOOLKIT
10894 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
10895 &text);
10896 #else /* not USE_X_TOOLKIT */
10897 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
10898 #endif /* not USE_X_TOOLKIT */
10899 }
10900 #else /* not HAVE_X11R4 */
10901 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
10902 #endif /* not HAVE_X11R4 */
10903
10904 if (f->output_data.x->icon_bitmap > 0)
10905 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10906 f->output_data.x->icon_bitmap = 0;
10907 x_wm_set_icon_pixmap (f, 0);
10908
10909 return 0;
10910 }
10911 \f
10912 #define X_ERROR_MESSAGE_SIZE 200
10913
10914 /* If non-nil, this should be a string.
10915 It means catch X errors and store the error message in this string. */
10916
10917 static Lisp_Object x_error_message_string;
10918
10919 /* An X error handler which stores the error message in
10920 x_error_message_string. This is called from x_error_handler if
10921 x_catch_errors is in effect. */
10922
10923 static void
10924 x_error_catcher (display, error)
10925 Display *display;
10926 XErrorEvent *error;
10927 {
10928 XGetErrorText (display, error->error_code,
10929 XSTRING (x_error_message_string)->data,
10930 X_ERROR_MESSAGE_SIZE);
10931 }
10932
10933 /* Begin trapping X errors for display DPY. Actually we trap X errors
10934 for all displays, but DPY should be the display you are actually
10935 operating on.
10936
10937 After calling this function, X protocol errors no longer cause
10938 Emacs to exit; instead, they are recorded in the string
10939 stored in x_error_message_string.
10940
10941 Calling x_check_errors signals an Emacs error if an X error has
10942 occurred since the last call to x_catch_errors or x_check_errors.
10943
10944 Calling x_uncatch_errors resumes the normal error handling. */
10945
10946 void x_check_errors ();
10947 static Lisp_Object x_catch_errors_unwind ();
10948
10949 int
10950 x_catch_errors (dpy)
10951 Display *dpy;
10952 {
10953 int count = specpdl_ptr - specpdl;
10954
10955 /* Make sure any errors from previous requests have been dealt with. */
10956 XSync (dpy, False);
10957
10958 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
10959
10960 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
10961 XSTRING (x_error_message_string)->data[0] = 0;
10962
10963 return count;
10964 }
10965
10966 /* Unbind the binding that we made to check for X errors. */
10967
10968 static Lisp_Object
10969 x_catch_errors_unwind (old_val)
10970 Lisp_Object old_val;
10971 {
10972 x_error_message_string = old_val;
10973 return Qnil;
10974 }
10975
10976 /* If any X protocol errors have arrived since the last call to
10977 x_catch_errors or x_check_errors, signal an Emacs error using
10978 sprintf (a buffer, FORMAT, the x error message text) as the text. */
10979
10980 void
10981 x_check_errors (dpy, format)
10982 Display *dpy;
10983 char *format;
10984 {
10985 /* Make sure to catch any errors incurred so far. */
10986 XSync (dpy, False);
10987
10988 if (XSTRING (x_error_message_string)->data[0])
10989 error (format, XSTRING (x_error_message_string)->data);
10990 }
10991
10992 /* Nonzero if we had any X protocol errors
10993 since we did x_catch_errors on DPY. */
10994
10995 int
10996 x_had_errors_p (dpy)
10997 Display *dpy;
10998 {
10999 /* Make sure to catch any errors incurred so far. */
11000 XSync (dpy, False);
11001
11002 return XSTRING (x_error_message_string)->data[0] != 0;
11003 }
11004
11005 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11006
11007 void
11008 x_clear_errors (dpy)
11009 Display *dpy;
11010 {
11011 XSTRING (x_error_message_string)->data[0] = 0;
11012 }
11013
11014 /* Stop catching X protocol errors and let them make Emacs die.
11015 DPY should be the display that was passed to x_catch_errors.
11016 COUNT should be the value that was returned by
11017 the corresponding call to x_catch_errors. */
11018
11019 void
11020 x_uncatch_errors (dpy, count)
11021 Display *dpy;
11022 int count;
11023 {
11024 unbind_to (count, Qnil);
11025 }
11026
11027 #if 0
11028 static unsigned int x_wire_count;
11029 x_trace_wire ()
11030 {
11031 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11032 }
11033 #endif /* ! 0 */
11034
11035 \f
11036 /* Handle SIGPIPE, which can happen when the connection to a server
11037 simply goes away. SIGPIPE is handled by x_connection_signal.
11038 Don't need to do anything, because the write which caused the
11039 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
11040 which will do the appropriate cleanup for us. */
11041
11042 static SIGTYPE
11043 x_connection_signal (signalnum) /* If we don't have an argument, */
11044 int signalnum; /* some compilers complain in signal calls. */
11045 {
11046 #ifdef USG
11047 /* USG systems forget handlers when they are used;
11048 must reestablish each time */
11049 signal (signalnum, x_connection_signal);
11050 #endif /* USG */
11051 }
11052 \f
11053 /* Handling X errors. */
11054
11055 /* Handle the loss of connection to display DISPLAY. */
11056
11057 static SIGTYPE
11058 x_connection_closed (display, error_message)
11059 Display *display;
11060 char *error_message;
11061 {
11062 struct x_display_info *dpyinfo = x_display_info_for_display (display);
11063 Lisp_Object frame, tail;
11064
11065 /* Indicate that this display is dead. */
11066
11067 #if 0 /* Closing the display caused a bus error on OpenWindows. */
11068 #ifdef USE_X_TOOLKIT
11069 XtCloseDisplay (display);
11070 #endif
11071 #endif
11072
11073 if (dpyinfo)
11074 dpyinfo->display = 0;
11075
11076 /* First delete frames whose mini-buffers are on frames
11077 that are on the dead display. */
11078 FOR_EACH_FRAME (tail, frame)
11079 {
11080 Lisp_Object minibuf_frame;
11081 minibuf_frame
11082 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
11083 if (FRAME_X_P (XFRAME (frame))
11084 && FRAME_X_P (XFRAME (minibuf_frame))
11085 && ! EQ (frame, minibuf_frame)
11086 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
11087 Fdelete_frame (frame, Qt);
11088 }
11089
11090 /* Now delete all remaining frames on the dead display.
11091 We are now sure none of these is used as the mini-buffer
11092 for another frame that we need to delete. */
11093 FOR_EACH_FRAME (tail, frame)
11094 if (FRAME_X_P (XFRAME (frame))
11095 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
11096 {
11097 /* Set this to t so that Fdelete_frame won't get confused
11098 trying to find a replacement. */
11099 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11100 Fdelete_frame (frame, Qt);
11101 }
11102
11103 if (dpyinfo)
11104 x_delete_display (dpyinfo);
11105
11106 if (x_display_list == 0)
11107 {
11108 fprintf (stderr, "%s\n", error_message);
11109 shut_down_emacs (0, 0, Qnil);
11110 exit (70);
11111 }
11112
11113 /* Ordinary stack unwind doesn't deal with these. */
11114 #ifdef SIGIO
11115 sigunblock (sigmask (SIGIO));
11116 #endif
11117 sigunblock (sigmask (SIGALRM));
11118 TOTALLY_UNBLOCK_INPUT;
11119
11120 clear_waiting_for_input ();
11121 error ("%s", error_message);
11122 }
11123
11124 /* This is the usual handler for X protocol errors.
11125 It kills all frames on the display that we got the error for.
11126 If that was the only one, it prints an error message and kills Emacs. */
11127
11128 static void
11129 x_error_quitter (display, error)
11130 Display *display;
11131 XErrorEvent *error;
11132 {
11133 char buf[256], buf1[356];
11134
11135 /* Note that there is no real way portable across R3/R4 to get the
11136 original error handler. */
11137
11138 XGetErrorText (display, error->error_code, buf, sizeof (buf));
11139 sprintf (buf1, "X protocol error: %s on protocol request %d",
11140 buf, error->request_code);
11141 x_connection_closed (display, buf1);
11142 }
11143
11144 /* This is the first-level handler for X protocol errors.
11145 It calls x_error_quitter or x_error_catcher. */
11146
11147 static int
11148 x_error_handler (display, error)
11149 Display *display;
11150 XErrorEvent *error;
11151 {
11152 if (! NILP (x_error_message_string))
11153 x_error_catcher (display, error);
11154 else
11155 x_error_quitter (display, error);
11156 return 0;
11157 }
11158
11159 /* This is the handler for X IO errors, always.
11160 It kills all frames on the display that we lost touch with.
11161 If that was the only one, it prints an error message and kills Emacs. */
11162
11163 static int
11164 x_io_error_quitter (display)
11165 Display *display;
11166 {
11167 char buf[256];
11168
11169 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11170 x_connection_closed (display, buf);
11171 return 0;
11172 }
11173 \f
11174 /* Changing the font of the frame. */
11175
11176 /* Give frame F the font named FONTNAME as its default font, and
11177 return the full name of that font. FONTNAME may be a wildcard
11178 pattern; in that case, we choose some font that fits the pattern.
11179 The return value shows which font we chose. */
11180
11181 Lisp_Object
11182 x_new_font (f, fontname)
11183 struct frame *f;
11184 register char *fontname;
11185 {
11186 struct font_info *fontp
11187 = FS_LOAD_FONT (f, 0, fontname, -1);
11188
11189 if (!fontp)
11190 return Qnil;
11191
11192 f->output_data.x->font = (XFontStruct *) (fontp->font);
11193 f->output_data.x->baseline_offset = fontp->baseline_offset;
11194 f->output_data.x->fontset = -1;
11195
11196 /* Compute the scroll bar width in character columns. */
11197 if (f->scroll_bar_pixel_width > 0)
11198 {
11199 int wid = FONT_WIDTH (f->output_data.x->font);
11200 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11201 }
11202 else
11203 {
11204 int wid = FONT_WIDTH (f->output_data.x->font);
11205 f->scroll_bar_cols = (14 + wid - 1) / wid;
11206 }
11207
11208 /* Now make the frame display the given font. */
11209 if (FRAME_X_WINDOW (f) != 0)
11210 {
11211 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11212 f->output_data.x->font->fid);
11213 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11214 f->output_data.x->font->fid);
11215 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11216 f->output_data.x->font->fid);
11217
11218 frame_update_line_height (f);
11219 x_set_window_size (f, 0, f->width, f->height);
11220 }
11221 else
11222 /* If we are setting a new frame's font for the first time,
11223 there are no faces yet, so this font's height is the line height. */
11224 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
11225
11226 return build_string (fontp->full_name);
11227 }
11228
11229 /* Give frame F the fontset named FONTSETNAME as its default font, and
11230 return the full name of that fontset. FONTSETNAME may be a wildcard
11231 pattern; in that case, we choose some fontset that fits the pattern.
11232 The return value shows which fontset we chose. */
11233
11234 Lisp_Object
11235 x_new_fontset (f, fontsetname)
11236 struct frame *f;
11237 char *fontsetname;
11238 {
11239 int fontset = fs_query_fontset (build_string (fontsetname), 0);
11240 Lisp_Object result;
11241
11242 if (fontset < 0)
11243 return Qnil;
11244
11245 if (f->output_data.x->fontset == fontset)
11246 /* This fontset is already set in frame F. There's nothing more
11247 to do. */
11248 return fontset_name (fontset);
11249
11250 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
11251
11252 if (!STRINGP (result))
11253 /* Can't load ASCII font. */
11254 return Qnil;
11255
11256 /* Since x_new_font doesn't update any fontset information, do it now. */
11257 f->output_data.x->fontset = fontset;
11258
11259 #ifdef HAVE_X_I18N
11260 if (FRAME_XIC (f)
11261 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
11262 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
11263 #endif
11264
11265 return build_string (fontsetname);
11266 }
11267
11268 \f
11269 /***********************************************************************
11270 X Input Methods
11271 ***********************************************************************/
11272
11273 #ifdef HAVE_X_I18N
11274
11275 #ifdef HAVE_X11R6
11276
11277 /* XIM destroy callback function, which is called whenever the
11278 connection to input method XIM dies. CLIENT_DATA contains a
11279 pointer to the x_display_info structure corresponding to XIM. */
11280
11281 static void
11282 xim_destroy_callback (xim, client_data, call_data)
11283 XIM xim;
11284 XPointer client_data;
11285 XPointer call_data;
11286 {
11287 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11288 Lisp_Object frame, tail;
11289
11290 BLOCK_INPUT;
11291
11292 /* No need to call XDestroyIC.. */
11293 FOR_EACH_FRAME (tail, frame)
11294 {
11295 struct frame *f = XFRAME (frame);
11296 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11297 {
11298 FRAME_XIC (f) = NULL;
11299 if (FRAME_XIC_FONTSET (f))
11300 {
11301 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11302 FRAME_XIC_FONTSET (f) = NULL;
11303 }
11304 }
11305 }
11306
11307 /* No need to call XCloseIM. */
11308 dpyinfo->xim = NULL;
11309 XFree (dpyinfo->xim_styles);
11310 UNBLOCK_INPUT;
11311 }
11312
11313 #endif /* HAVE_X11R6 */
11314
11315 /* Open the connection to the XIM server on display DPYINFO.
11316 RESOURCE_NAME is the resource name Emacs uses. */
11317
11318 static void
11319 xim_open_dpy (dpyinfo, resource_name)
11320 struct x_display_info *dpyinfo;
11321 char *resource_name;
11322 {
11323 #ifdef USE_XIM
11324 XIM xim;
11325
11326 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11327 dpyinfo->xim = xim;
11328
11329 if (xim)
11330 {
11331 #ifdef HAVE_X11R6
11332 XIMCallback destroy;
11333 #endif
11334
11335 /* Get supported styles and XIM values. */
11336 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11337
11338 #ifdef HAVE_X11R6
11339 destroy.callback = xim_destroy_callback;
11340 destroy.client_data = (XPointer)dpyinfo;
11341 /* This isn't prptotyped in OSF 5.0. */
11342 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11343 #endif
11344 }
11345
11346 #else /* not USE_XIM */
11347 dpyinfo->xim = NULL;
11348 #endif /* not USE_XIM */
11349 }
11350
11351
11352 #ifdef HAVE_X11R6_XIM
11353
11354 struct xim_inst_t
11355 {
11356 struct x_display_info *dpyinfo;
11357 char *resource_name;
11358 };
11359
11360 /* XIM instantiate callback function, which is called whenever an XIM
11361 server is available. DISPLAY is teh display of the XIM.
11362 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11363 when the callback was registered. */
11364
11365 static void
11366 xim_instantiate_callback (display, client_data, call_data)
11367 Display *display;
11368 XPointer client_data;
11369 XPointer call_data;
11370 {
11371 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11372 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11373
11374 /* We don't support multiple XIM connections. */
11375 if (dpyinfo->xim)
11376 return;
11377
11378 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11379
11380 /* Create XIC for the existing frames on the same display, as long
11381 as they have no XIC. */
11382 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11383 {
11384 Lisp_Object tail, frame;
11385
11386 BLOCK_INPUT;
11387 FOR_EACH_FRAME (tail, frame)
11388 {
11389 struct frame *f = XFRAME (frame);
11390
11391 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11392 if (FRAME_XIC (f) == NULL)
11393 {
11394 create_frame_xic (f);
11395 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11396 xic_set_statusarea (f);
11397 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11398 {
11399 struct window *w = XWINDOW (f->selected_window);
11400 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11401 }
11402 }
11403 }
11404
11405 UNBLOCK_INPUT;
11406 }
11407 }
11408
11409 #endif /* HAVE_X11R6_XIM */
11410
11411
11412 /* Open a connection to the XIM server on display DPYINFO.
11413 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11414 connection only at the first time. On X11R6, open the connection
11415 in the XIM instantiate callback function. */
11416
11417 static void
11418 xim_initialize (dpyinfo, resource_name)
11419 struct x_display_info *dpyinfo;
11420 char *resource_name;
11421 {
11422 #ifdef USE_XIM
11423 #ifdef HAVE_X11R6_XIM
11424 struct xim_inst_t *xim_inst;
11425 int len;
11426
11427 dpyinfo->xim = NULL;
11428 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11429 xim_inst->dpyinfo = dpyinfo;
11430 len = strlen (resource_name);
11431 xim_inst->resource_name = (char *) xmalloc (len + 1);
11432 bcopy (resource_name, xim_inst->resource_name, len + 1);
11433 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11434 resource_name, EMACS_CLASS,
11435 xim_instantiate_callback,
11436 /* Fixme: This is XPointer in
11437 XFree86 but (XPointer *) on
11438 Tru64, at least. */
11439 (XPointer) xim_inst);
11440 #else /* not HAVE_X11R6_XIM */
11441 dpyinfo->xim = NULL;
11442 xim_open_dpy (dpyinfo, resource_name);
11443 #endif /* not HAVE_X11R6_XIM */
11444
11445 #else /* not USE_XIM */
11446 dpyinfo->xim = NULL;
11447 #endif /* not USE_XIM */
11448 }
11449
11450
11451 /* Close the connection to the XIM server on display DPYINFO. */
11452
11453 static void
11454 xim_close_dpy (dpyinfo)
11455 struct x_display_info *dpyinfo;
11456 {
11457 #ifdef USE_XIM
11458 #ifdef HAVE_X11R6_XIM
11459 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11460 NULL, EMACS_CLASS,
11461 xim_instantiate_callback, NULL);
11462 #endif /* not HAVE_X11R6_XIM */
11463 XCloseIM (dpyinfo->xim);
11464 dpyinfo->xim = NULL;
11465 XFree (dpyinfo->xim_styles);
11466 #endif /* USE_XIM */
11467 }
11468
11469 #endif /* not HAVE_X11R6_XIM */
11470
11471
11472 \f
11473 /* Calculate the absolute position in frame F
11474 from its current recorded position values and gravity. */
11475
11476 void
11477 x_calc_absolute_position (f)
11478 struct frame *f;
11479 {
11480 Window child;
11481 int win_x = 0, win_y = 0;
11482 int flags = f->output_data.x->size_hint_flags;
11483 int this_window;
11484
11485 /* We have nothing to do if the current position
11486 is already for the top-left corner. */
11487 if (! ((flags & XNegative) || (flags & YNegative)))
11488 return;
11489
11490 #ifdef USE_X_TOOLKIT
11491 this_window = XtWindow (f->output_data.x->widget);
11492 #else
11493 this_window = FRAME_X_WINDOW (f);
11494 #endif
11495
11496 /* Find the position of the outside upper-left corner of
11497 the inner window, with respect to the outer window.
11498 But do this only if we will need the results. */
11499 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
11500 {
11501 int count;
11502
11503 BLOCK_INPUT;
11504 count = x_catch_errors (FRAME_X_DISPLAY (f));
11505 while (1)
11506 {
11507 x_clear_errors (FRAME_X_DISPLAY (f));
11508 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11509
11510 /* From-window, to-window. */
11511 this_window,
11512 f->output_data.x->parent_desc,
11513
11514 /* From-position, to-position. */
11515 0, 0, &win_x, &win_y,
11516
11517 /* Child of win. */
11518 &child);
11519 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11520 {
11521 Window newroot, newparent = 0xdeadbeef;
11522 Window *newchildren;
11523 unsigned int nchildren;
11524
11525 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11526 &newparent, &newchildren, &nchildren))
11527 break;
11528
11529 XFree ((char *) newchildren);
11530
11531 f->output_data.x->parent_desc = newparent;
11532 }
11533 else
11534 break;
11535 }
11536
11537 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
11538 UNBLOCK_INPUT;
11539 }
11540
11541 /* Treat negative positions as relative to the leftmost bottommost
11542 position that fits on the screen. */
11543 if (flags & XNegative)
11544 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
11545 - 2 * f->output_data.x->border_width - win_x
11546 - PIXEL_WIDTH (f)
11547 + f->output_data.x->left_pos);
11548
11549 if (flags & YNegative)
11550 {
11551 int menubar_height = 0;
11552
11553 #ifdef USE_X_TOOLKIT
11554 if (f->output_data.x->menubar_widget)
11555 menubar_height
11556 = (f->output_data.x->menubar_widget->core.height
11557 + f->output_data.x->menubar_widget->core.border_width);
11558 #endif
11559
11560 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11561 - 2 * f->output_data.x->border_width
11562 - win_y
11563 - PIXEL_HEIGHT (f)
11564 - menubar_height
11565 + f->output_data.x->top_pos);
11566 }
11567
11568 /* The left_pos and top_pos
11569 are now relative to the top and left screen edges,
11570 so the flags should correspond. */
11571 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11572 }
11573
11574 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11575 to really change the position, and 0 when calling from
11576 x_make_frame_visible (in that case, XOFF and YOFF are the current
11577 position values). It is -1 when calling from x_set_frame_parameters,
11578 which means, do adjust for borders but don't change the gravity. */
11579
11580 void
11581 x_set_offset (f, xoff, yoff, change_gravity)
11582 struct frame *f;
11583 register int xoff, yoff;
11584 int change_gravity;
11585 {
11586 int modified_top, modified_left;
11587
11588 if (change_gravity > 0)
11589 {
11590 f->output_data.x->top_pos = yoff;
11591 f->output_data.x->left_pos = xoff;
11592 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11593 if (xoff < 0)
11594 f->output_data.x->size_hint_flags |= XNegative;
11595 if (yoff < 0)
11596 f->output_data.x->size_hint_flags |= YNegative;
11597 f->output_data.x->win_gravity = NorthWestGravity;
11598 }
11599 x_calc_absolute_position (f);
11600
11601 BLOCK_INPUT;
11602 x_wm_set_size_hint (f, (long) 0, 0);
11603
11604 modified_left = f->output_data.x->left_pos;
11605 modified_top = f->output_data.x->top_pos;
11606 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11607 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11608 /* It is a mystery why we need to add the border_width here
11609 when the frame is already visible, but experiment says we do. */
11610 if (change_gravity != 0)
11611 {
11612 modified_left += f->output_data.x->border_width;
11613 modified_top += f->output_data.x->border_width;
11614 }
11615 #endif
11616
11617 #ifdef USE_X_TOOLKIT
11618 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11619 modified_left, modified_top);
11620 #else /* not USE_X_TOOLKIT */
11621 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11622 modified_left, modified_top);
11623 #endif /* not USE_X_TOOLKIT */
11624 UNBLOCK_INPUT;
11625 }
11626
11627 /* Call this to change the size of frame F's x-window.
11628 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11629 for this size change and subsequent size changes.
11630 Otherwise we leave the window gravity unchanged. */
11631
11632 void
11633 x_set_window_size (f, change_gravity, cols, rows)
11634 struct frame *f;
11635 int change_gravity;
11636 int cols, rows;
11637 {
11638 #ifndef USE_X_TOOLKIT
11639 int pixelwidth, pixelheight;
11640 #endif
11641
11642 BLOCK_INPUT;
11643
11644 #ifdef USE_X_TOOLKIT
11645 {
11646 /* The x and y position of the widget is clobbered by the
11647 call to XtSetValues within EmacsFrameSetCharSize.
11648 This is a real kludge, but I don't understand Xt so I can't
11649 figure out a correct fix. Can anyone else tell me? -- rms. */
11650 int xpos = f->output_data.x->widget->core.x;
11651 int ypos = f->output_data.x->widget->core.y;
11652 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11653 f->output_data.x->widget->core.x = xpos;
11654 f->output_data.x->widget->core.y = ypos;
11655 }
11656
11657 #else /* not USE_X_TOOLKIT */
11658
11659 check_frame_size (f, &rows, &cols);
11660 f->output_data.x->vertical_scroll_bar_extra
11661 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11662 ? 0
11663 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
11664 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
11665 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
11666 f->output_data.x->flags_areas_extra
11667 = FRAME_FLAGS_AREA_WIDTH (f);
11668 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11669 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
11670
11671 f->output_data.x->win_gravity = NorthWestGravity;
11672 x_wm_set_size_hint (f, (long) 0, 0);
11673
11674 XSync (FRAME_X_DISPLAY (f), False);
11675 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11676 pixelwidth, pixelheight);
11677
11678 /* Now, strictly speaking, we can't be sure that this is accurate,
11679 but the window manager will get around to dealing with the size
11680 change request eventually, and we'll hear how it went when the
11681 ConfigureNotify event gets here.
11682
11683 We could just not bother storing any of this information here,
11684 and let the ConfigureNotify event set everything up, but that
11685 might be kind of confusing to the Lisp code, since size changes
11686 wouldn't be reported in the frame parameters until some random
11687 point in the future when the ConfigureNotify event arrives.
11688
11689 We pass 1 for DELAY since we can't run Lisp code inside of
11690 a BLOCK_INPUT. */
11691 change_frame_size (f, rows, cols, 0, 1, 0);
11692 PIXEL_WIDTH (f) = pixelwidth;
11693 PIXEL_HEIGHT (f) = pixelheight;
11694
11695 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11696 receive in the ConfigureNotify event; if we get what we asked
11697 for, then the event won't cause the screen to become garbaged, so
11698 we have to make sure to do it here. */
11699 SET_FRAME_GARBAGED (f);
11700
11701 XFlush (FRAME_X_DISPLAY (f));
11702
11703 #endif /* not USE_X_TOOLKIT */
11704
11705 /* If cursor was outside the new size, mark it as off. */
11706 mark_window_cursors_off (XWINDOW (f->root_window));
11707
11708 /* Clear out any recollection of where the mouse highlighting was,
11709 since it might be in a place that's outside the new frame size.
11710 Actually checking whether it is outside is a pain in the neck,
11711 so don't try--just let the highlighting be done afresh with new size. */
11712 cancel_mouse_face (f);
11713
11714 UNBLOCK_INPUT;
11715 }
11716 \f
11717 /* Mouse warping. */
11718
11719 void
11720 x_set_mouse_position (f, x, y)
11721 struct frame *f;
11722 int x, y;
11723 {
11724 int pix_x, pix_y;
11725
11726 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11727 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
11728
11729 if (pix_x < 0) pix_x = 0;
11730 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11731
11732 if (pix_y < 0) pix_y = 0;
11733 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
11734
11735 BLOCK_INPUT;
11736
11737 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11738 0, 0, 0, 0, pix_x, pix_y);
11739 UNBLOCK_INPUT;
11740 }
11741
11742 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11743
11744 void
11745 x_set_mouse_pixel_position (f, pix_x, pix_y)
11746 struct frame *f;
11747 int pix_x, pix_y;
11748 {
11749 BLOCK_INPUT;
11750
11751 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11752 0, 0, 0, 0, pix_x, pix_y);
11753 UNBLOCK_INPUT;
11754 }
11755 \f
11756 /* focus shifting, raising and lowering. */
11757
11758 void
11759 x_focus_on_frame (f)
11760 struct frame *f;
11761 {
11762 #if 0 /* This proves to be unpleasant. */
11763 x_raise_frame (f);
11764 #endif
11765 #if 0
11766 /* I don't think that the ICCCM allows programs to do things like this
11767 without the interaction of the window manager. Whatever you end up
11768 doing with this code, do it to x_unfocus_frame too. */
11769 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11770 RevertToPointerRoot, CurrentTime);
11771 #endif /* ! 0 */
11772 }
11773
11774 void
11775 x_unfocus_frame (f)
11776 struct frame *f;
11777 {
11778 #if 0
11779 /* Look at the remarks in x_focus_on_frame. */
11780 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
11781 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
11782 RevertToPointerRoot, CurrentTime);
11783 #endif /* ! 0 */
11784 }
11785
11786 /* Raise frame F. */
11787
11788 void
11789 x_raise_frame (f)
11790 struct frame *f;
11791 {
11792 if (f->async_visible)
11793 {
11794 BLOCK_INPUT;
11795 #ifdef USE_X_TOOLKIT
11796 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
11797 #else /* not USE_X_TOOLKIT */
11798 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11799 #endif /* not USE_X_TOOLKIT */
11800 XFlush (FRAME_X_DISPLAY (f));
11801 UNBLOCK_INPUT;
11802 }
11803 }
11804
11805 /* Lower frame F. */
11806
11807 void
11808 x_lower_frame (f)
11809 struct frame *f;
11810 {
11811 if (f->async_visible)
11812 {
11813 BLOCK_INPUT;
11814 #ifdef USE_X_TOOLKIT
11815 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
11816 #else /* not USE_X_TOOLKIT */
11817 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11818 #endif /* not USE_X_TOOLKIT */
11819 XFlush (FRAME_X_DISPLAY (f));
11820 UNBLOCK_INPUT;
11821 }
11822 }
11823
11824 static void
11825 XTframe_raise_lower (f, raise_flag)
11826 FRAME_PTR f;
11827 int raise_flag;
11828 {
11829 if (raise_flag)
11830 x_raise_frame (f);
11831 else
11832 x_lower_frame (f);
11833 }
11834 \f
11835 /* Change of visibility. */
11836
11837 /* This tries to wait until the frame is really visible.
11838 However, if the window manager asks the user where to position
11839 the frame, this will return before the user finishes doing that.
11840 The frame will not actually be visible at that time,
11841 but it will become visible later when the window manager
11842 finishes with it. */
11843
11844 void
11845 x_make_frame_visible (f)
11846 struct frame *f;
11847 {
11848 Lisp_Object type;
11849 int original_top, original_left;
11850
11851 BLOCK_INPUT;
11852
11853 type = x_icon_type (f);
11854 if (!NILP (type))
11855 x_bitmap_icon (f, type);
11856
11857 if (! FRAME_VISIBLE_P (f))
11858 {
11859 /* We test FRAME_GARBAGED_P here to make sure we don't
11860 call x_set_offset a second time
11861 if we get to x_make_frame_visible a second time
11862 before the window gets really visible. */
11863 if (! FRAME_ICONIFIED_P (f)
11864 && ! f->output_data.x->asked_for_visible)
11865 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11866
11867 f->output_data.x->asked_for_visible = 1;
11868
11869 if (! EQ (Vx_no_window_manager, Qt))
11870 x_wm_set_window_state (f, NormalState);
11871 #ifdef USE_X_TOOLKIT
11872 /* This was XtPopup, but that did nothing for an iconified frame. */
11873 XtMapWidget (f->output_data.x->widget);
11874 #else /* not USE_X_TOOLKIT */
11875 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11876 #endif /* not USE_X_TOOLKIT */
11877 #if 0 /* This seems to bring back scroll bars in the wrong places
11878 if the window configuration has changed. They seem
11879 to come back ok without this. */
11880 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
11881 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11882 #endif
11883 }
11884
11885 XFlush (FRAME_X_DISPLAY (f));
11886
11887 /* Synchronize to ensure Emacs knows the frame is visible
11888 before we do anything else. We do this loop with input not blocked
11889 so that incoming events are handled. */
11890 {
11891 Lisp_Object frame;
11892 int count;
11893 /* This must be before UNBLOCK_INPUT
11894 since events that arrive in response to the actions above
11895 will set it when they are handled. */
11896 int previously_visible = f->output_data.x->has_been_visible;
11897
11898 original_left = f->output_data.x->left_pos;
11899 original_top = f->output_data.x->top_pos;
11900
11901 /* This must come after we set COUNT. */
11902 UNBLOCK_INPUT;
11903
11904 /* We unblock here so that arriving X events are processed. */
11905
11906 /* Now move the window back to where it was "supposed to be".
11907 But don't do it if the gravity is negative.
11908 When the gravity is negative, this uses a position
11909 that is 3 pixels too low. Perhaps that's really the border width.
11910
11911 Don't do this if the window has never been visible before,
11912 because the window manager may choose the position
11913 and we don't want to override it. */
11914
11915 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
11916 && f->output_data.x->win_gravity == NorthWestGravity
11917 && previously_visible)
11918 {
11919 Drawable rootw;
11920 int x, y;
11921 unsigned int width, height, border, depth;
11922
11923 BLOCK_INPUT;
11924
11925 /* On some window managers (such as FVWM) moving an existing
11926 window, even to the same place, causes the window manager
11927 to introduce an offset. This can cause the window to move
11928 to an unexpected location. Check the geometry (a little
11929 slow here) and then verify that the window is in the right
11930 place. If the window is not in the right place, move it
11931 there, and take the potential window manager hit. */
11932 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11933 &rootw, &x, &y, &width, &height, &border, &depth);
11934
11935 if (original_left != x || original_top != y)
11936 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11937 original_left, original_top);
11938
11939 UNBLOCK_INPUT;
11940 }
11941
11942 XSETFRAME (frame, f);
11943
11944 /* Wait until the frame is visible. Process X events until a
11945 MapNotify event has been seen, or until we think we won't get a
11946 MapNotify at all.. */
11947 for (count = input_signal_count + 10;
11948 input_signal_count < count && !FRAME_VISIBLE_P (f);)
11949 {
11950 /* Force processing of queued events. */
11951 x_sync (f);
11952
11953 /* Machines that do polling rather than SIGIO have been
11954 observed to go into a busy-wait here. So we'll fake an
11955 alarm signal to let the handler know that there's something
11956 to be read. We used to raise a real alarm, but it seems
11957 that the handler isn't always enabled here. This is
11958 probably a bug. */
11959 if (input_polling_used ())
11960 {
11961 /* It could be confusing if a real alarm arrives while
11962 processing the fake one. Turn it off and let the
11963 handler reset it. */
11964 extern void poll_for_input_1 P_ ((void));
11965 int old_poll_suppress_count = poll_suppress_count;
11966 poll_suppress_count = 1;
11967 poll_for_input_1 ();
11968 poll_suppress_count = old_poll_suppress_count;
11969 }
11970
11971 /* See if a MapNotify event has been processed. */
11972 FRAME_SAMPLE_VISIBILITY (f);
11973 }
11974 }
11975 }
11976
11977 /* Change from mapped state to withdrawn state. */
11978
11979 /* Make the frame visible (mapped and not iconified). */
11980
11981 void
11982 x_make_frame_invisible (f)
11983 struct frame *f;
11984 {
11985 Window window;
11986
11987 #ifdef USE_X_TOOLKIT
11988 /* Use the frame's outermost window, not the one we normally draw on. */
11989 window = XtWindow (f->output_data.x->widget);
11990 #else /* not USE_X_TOOLKIT */
11991 window = FRAME_X_WINDOW (f);
11992 #endif /* not USE_X_TOOLKIT */
11993
11994 /* Don't keep the highlight on an invisible frame. */
11995 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11996 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
11997
11998 #if 0/* This might add unreliability; I don't trust it -- rms. */
11999 if (! f->async_visible && ! f->async_iconified)
12000 return;
12001 #endif
12002
12003 BLOCK_INPUT;
12004
12005 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
12006 that the current position of the window is user-specified, rather than
12007 program-specified, so that when the window is mapped again, it will be
12008 placed at the same location, without forcing the user to position it
12009 by hand again (they have already done that once for this window.) */
12010 x_wm_set_size_hint (f, (long) 0, 1);
12011
12012 #ifdef HAVE_X11R4
12013
12014 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
12015 DefaultScreen (FRAME_X_DISPLAY (f))))
12016 {
12017 UNBLOCK_INPUT_RESIGNAL;
12018 error ("Can't notify window manager of window withdrawal");
12019 }
12020 #else /* ! defined (HAVE_X11R4) */
12021
12022 /* Tell the window manager what we're going to do. */
12023 if (! EQ (Vx_no_window_manager, Qt))
12024 {
12025 XEvent unmap;
12026
12027 unmap.xunmap.type = UnmapNotify;
12028 unmap.xunmap.window = window;
12029 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
12030 unmap.xunmap.from_configure = False;
12031 if (! XSendEvent (FRAME_X_DISPLAY (f),
12032 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12033 False,
12034 SubstructureRedirectMaskSubstructureNotifyMask,
12035 &unmap))
12036 {
12037 UNBLOCK_INPUT_RESIGNAL;
12038 error ("Can't notify window manager of withdrawal");
12039 }
12040 }
12041
12042 /* Unmap the window ourselves. Cheeky! */
12043 XUnmapWindow (FRAME_X_DISPLAY (f), window);
12044 #endif /* ! defined (HAVE_X11R4) */
12045
12046 /* We can't distinguish this from iconification
12047 just by the event that we get from the server.
12048 So we can't win using the usual strategy of letting
12049 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
12050 and synchronize with the server to make sure we agree. */
12051 f->visible = 0;
12052 FRAME_ICONIFIED_P (f) = 0;
12053 f->async_visible = 0;
12054 f->async_iconified = 0;
12055
12056 x_sync (f);
12057
12058 UNBLOCK_INPUT;
12059 }
12060
12061 /* Change window state from mapped to iconified. */
12062
12063 void
12064 x_iconify_frame (f)
12065 struct frame *f;
12066 {
12067 int result;
12068 Lisp_Object type;
12069
12070 /* Don't keep the highlight on an invisible frame. */
12071 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12072 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12073
12074 if (f->async_iconified)
12075 return;
12076
12077 BLOCK_INPUT;
12078
12079 FRAME_SAMPLE_VISIBILITY (f);
12080
12081 type = x_icon_type (f);
12082 if (!NILP (type))
12083 x_bitmap_icon (f, type);
12084
12085 #ifdef USE_X_TOOLKIT
12086
12087 if (! FRAME_VISIBLE_P (f))
12088 {
12089 if (! EQ (Vx_no_window_manager, Qt))
12090 x_wm_set_window_state (f, IconicState);
12091 /* This was XtPopup, but that did nothing for an iconified frame. */
12092 XtMapWidget (f->output_data.x->widget);
12093 /* The server won't give us any event to indicate
12094 that an invisible frame was changed to an icon,
12095 so we have to record it here. */
12096 f->iconified = 1;
12097 f->visible = 1;
12098 f->async_iconified = 1;
12099 f->async_visible = 0;
12100 UNBLOCK_INPUT;
12101 return;
12102 }
12103
12104 result = XIconifyWindow (FRAME_X_DISPLAY (f),
12105 XtWindow (f->output_data.x->widget),
12106 DefaultScreen (FRAME_X_DISPLAY (f)));
12107 UNBLOCK_INPUT;
12108
12109 if (!result)
12110 error ("Can't notify window manager of iconification");
12111
12112 f->async_iconified = 1;
12113 f->async_visible = 0;
12114
12115
12116 BLOCK_INPUT;
12117 XFlush (FRAME_X_DISPLAY (f));
12118 UNBLOCK_INPUT;
12119 #else /* not USE_X_TOOLKIT */
12120
12121 /* Make sure the X server knows where the window should be positioned,
12122 in case the user deiconifies with the window manager. */
12123 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
12124 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12125
12126 /* Since we don't know which revision of X we're running, we'll use both
12127 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
12128
12129 /* X11R4: send a ClientMessage to the window manager using the
12130 WM_CHANGE_STATE type. */
12131 {
12132 XEvent message;
12133
12134 message.xclient.window = FRAME_X_WINDOW (f);
12135 message.xclient.type = ClientMessage;
12136 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
12137 message.xclient.format = 32;
12138 message.xclient.data.l[0] = IconicState;
12139
12140 if (! XSendEvent (FRAME_X_DISPLAY (f),
12141 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12142 False,
12143 SubstructureRedirectMask | SubstructureNotifyMask,
12144 &message))
12145 {
12146 UNBLOCK_INPUT_RESIGNAL;
12147 error ("Can't notify window manager of iconification");
12148 }
12149 }
12150
12151 /* X11R3: set the initial_state field of the window manager hints to
12152 IconicState. */
12153 x_wm_set_window_state (f, IconicState);
12154
12155 if (!FRAME_VISIBLE_P (f))
12156 {
12157 /* If the frame was withdrawn, before, we must map it. */
12158 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12159 }
12160
12161 f->async_iconified = 1;
12162 f->async_visible = 0;
12163
12164 XFlush (FRAME_X_DISPLAY (f));
12165 UNBLOCK_INPUT;
12166 #endif /* not USE_X_TOOLKIT */
12167 }
12168 \f
12169 /* Destroy the X window of frame F. */
12170
12171 void
12172 x_destroy_window (f)
12173 struct frame *f;
12174 {
12175 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12176
12177 BLOCK_INPUT;
12178
12179 /* If a display connection is dead, don't try sending more
12180 commands to the X server. */
12181 if (dpyinfo->display != 0)
12182 {
12183 if (f->output_data.x->icon_desc != 0)
12184 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
12185 #ifdef HAVE_X_I18N
12186 if (FRAME_XIC (f))
12187 free_frame_xic (f);
12188 #endif
12189 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->window_desc);
12190 #ifdef USE_X_TOOLKIT
12191 if (f->output_data.x->widget)
12192 XtDestroyWidget (f->output_data.x->widget);
12193 free_frame_menubar (f);
12194 #endif /* USE_X_TOOLKIT */
12195
12196 unload_color (f, f->output_data.x->foreground_pixel);
12197 unload_color (f, f->output_data.x->background_pixel);
12198 unload_color (f, f->output_data.x->cursor_pixel);
12199 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12200 unload_color (f, f->output_data.x->border_pixel);
12201 unload_color (f, f->output_data.x->mouse_pixel);
12202 if (f->output_data.x->scroll_bar_background_pixel != -1)
12203 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
12204 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
12205 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
12206 if (f->output_data.x->white_relief.allocated_p)
12207 unload_color (f, f->output_data.x->white_relief.pixel);
12208 if (f->output_data.x->black_relief.allocated_p)
12209 unload_color (f, f->output_data.x->black_relief.pixel);
12210
12211 free_frame_faces (f);
12212 XFlush (FRAME_X_DISPLAY (f));
12213 }
12214
12215 if (f->output_data.x->saved_menu_event)
12216 xfree (f->output_data.x->saved_menu_event);
12217
12218 xfree (f->output_data.x);
12219 f->output_data.x = 0;
12220 if (f == dpyinfo->x_focus_frame)
12221 dpyinfo->x_focus_frame = 0;
12222 if (f == dpyinfo->x_focus_event_frame)
12223 dpyinfo->x_focus_event_frame = 0;
12224 if (f == dpyinfo->x_highlight_frame)
12225 dpyinfo->x_highlight_frame = 0;
12226
12227 dpyinfo->reference_count--;
12228
12229 if (f == dpyinfo->mouse_face_mouse_frame)
12230 {
12231 dpyinfo->mouse_face_beg_row
12232 = dpyinfo->mouse_face_beg_col = -1;
12233 dpyinfo->mouse_face_end_row
12234 = dpyinfo->mouse_face_end_col = -1;
12235 dpyinfo->mouse_face_window = Qnil;
12236 dpyinfo->mouse_face_deferred_gc = 0;
12237 dpyinfo->mouse_face_mouse_frame = 0;
12238 }
12239
12240 UNBLOCK_INPUT;
12241 }
12242 \f
12243 /* Setting window manager hints. */
12244
12245 /* Set the normal size hints for the window manager, for frame F.
12246 FLAGS is the flags word to use--or 0 meaning preserve the flags
12247 that the window now has.
12248 If USER_POSITION is nonzero, we set the USPosition
12249 flag (this is useful when FLAGS is 0). */
12250
12251 void
12252 x_wm_set_size_hint (f, flags, user_position)
12253 struct frame *f;
12254 long flags;
12255 int user_position;
12256 {
12257 XSizeHints size_hints;
12258
12259 #ifdef USE_X_TOOLKIT
12260 Arg al[2];
12261 int ac = 0;
12262 Dimension widget_width, widget_height;
12263 Window window = XtWindow (f->output_data.x->widget);
12264 #else /* not USE_X_TOOLKIT */
12265 Window window = FRAME_X_WINDOW (f);
12266 #endif /* not USE_X_TOOLKIT */
12267
12268 /* Setting PMaxSize caused various problems. */
12269 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
12270
12271 size_hints.x = f->output_data.x->left_pos;
12272 size_hints.y = f->output_data.x->top_pos;
12273
12274 #ifdef USE_X_TOOLKIT
12275 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12276 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
12277 XtGetValues (f->output_data.x->widget, al, ac);
12278 size_hints.height = widget_height;
12279 size_hints.width = widget_width;
12280 #else /* not USE_X_TOOLKIT */
12281 size_hints.height = PIXEL_HEIGHT (f);
12282 size_hints.width = PIXEL_WIDTH (f);
12283 #endif /* not USE_X_TOOLKIT */
12284
12285 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
12286 size_hints.height_inc = f->output_data.x->line_height;
12287 size_hints.max_width
12288 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
12289 size_hints.max_height
12290 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
12291
12292 /* Calculate the base and minimum sizes.
12293
12294 (When we use the X toolkit, we don't do it here.
12295 Instead we copy the values that the widgets are using, below.) */
12296 #ifndef USE_X_TOOLKIT
12297 {
12298 int base_width, base_height;
12299 int min_rows = 0, min_cols = 0;
12300
12301 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12302 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
12303
12304 check_frame_size (f, &min_rows, &min_cols);
12305
12306 /* The window manager uses the base width hints to calculate the
12307 current number of rows and columns in the frame while
12308 resizing; min_width and min_height aren't useful for this
12309 purpose, since they might not give the dimensions for a
12310 zero-row, zero-column frame.
12311
12312 We use the base_width and base_height members if we have
12313 them; otherwise, we set the min_width and min_height members
12314 to the size for a zero x zero frame. */
12315
12316 #ifdef HAVE_X11R4
12317 size_hints.flags |= PBaseSize;
12318 size_hints.base_width = base_width;
12319 size_hints.base_height = base_height;
12320 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12321 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
12322 #else
12323 size_hints.min_width = base_width;
12324 size_hints.min_height = base_height;
12325 #endif
12326 }
12327
12328 /* If we don't need the old flags, we don't need the old hint at all. */
12329 if (flags)
12330 {
12331 size_hints.flags |= flags;
12332 goto no_read;
12333 }
12334 #endif /* not USE_X_TOOLKIT */
12335
12336 {
12337 XSizeHints hints; /* Sometimes I hate X Windows... */
12338 long supplied_return;
12339 int value;
12340
12341 #ifdef HAVE_X11R4
12342 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12343 &supplied_return);
12344 #else
12345 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
12346 #endif
12347
12348 #ifdef USE_X_TOOLKIT
12349 size_hints.base_height = hints.base_height;
12350 size_hints.base_width = hints.base_width;
12351 size_hints.min_height = hints.min_height;
12352 size_hints.min_width = hints.min_width;
12353 #endif
12354
12355 if (flags)
12356 size_hints.flags |= flags;
12357 else
12358 {
12359 if (value == 0)
12360 hints.flags = 0;
12361 if (hints.flags & PSize)
12362 size_hints.flags |= PSize;
12363 if (hints.flags & PPosition)
12364 size_hints.flags |= PPosition;
12365 if (hints.flags & USPosition)
12366 size_hints.flags |= USPosition;
12367 if (hints.flags & USSize)
12368 size_hints.flags |= USSize;
12369 }
12370 }
12371
12372 #ifndef USE_X_TOOLKIT
12373 no_read:
12374 #endif
12375
12376 #ifdef PWinGravity
12377 size_hints.win_gravity = f->output_data.x->win_gravity;
12378 size_hints.flags |= PWinGravity;
12379
12380 if (user_position)
12381 {
12382 size_hints.flags &= ~ PPosition;
12383 size_hints.flags |= USPosition;
12384 }
12385 #endif /* PWinGravity */
12386
12387 #ifdef HAVE_X11R4
12388 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12389 #else
12390 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12391 #endif
12392 }
12393
12394 /* Used for IconicState or NormalState */
12395
12396 void
12397 x_wm_set_window_state (f, state)
12398 struct frame *f;
12399 int state;
12400 {
12401 #ifdef USE_X_TOOLKIT
12402 Arg al[1];
12403
12404 XtSetArg (al[0], XtNinitialState, state);
12405 XtSetValues (f->output_data.x->widget, al, 1);
12406 #else /* not USE_X_TOOLKIT */
12407 Window window = FRAME_X_WINDOW (f);
12408
12409 f->output_data.x->wm_hints.flags |= StateHint;
12410 f->output_data.x->wm_hints.initial_state = state;
12411
12412 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12413 #endif /* not USE_X_TOOLKIT */
12414 }
12415
12416 void
12417 x_wm_set_icon_pixmap (f, pixmap_id)
12418 struct frame *f;
12419 int pixmap_id;
12420 {
12421 Pixmap icon_pixmap;
12422
12423 #ifndef USE_X_TOOLKIT
12424 Window window = FRAME_X_WINDOW (f);
12425 #endif
12426
12427 if (pixmap_id > 0)
12428 {
12429 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
12430 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
12431 }
12432 else
12433 {
12434 /* It seems there is no way to turn off use of an icon pixmap.
12435 The following line does it, only if no icon has yet been created,
12436 for some window managers. But with mwm it crashes.
12437 Some people say it should clear the IconPixmapHint bit in this case,
12438 but that doesn't work, and the X consortium said it isn't the
12439 right thing at all. Since there is no way to win,
12440 best to explicitly give up. */
12441 #if 0
12442 f->output_data.x->wm_hints.icon_pixmap = None;
12443 #else
12444 return;
12445 #endif
12446 }
12447
12448 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12449
12450 {
12451 Arg al[1];
12452 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12453 XtSetValues (f->output_data.x->widget, al, 1);
12454 }
12455
12456 #else /* not USE_X_TOOLKIT */
12457
12458 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12459 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12460
12461 #endif /* not USE_X_TOOLKIT */
12462 }
12463
12464 void
12465 x_wm_set_icon_position (f, icon_x, icon_y)
12466 struct frame *f;
12467 int icon_x, icon_y;
12468 {
12469 #ifdef USE_X_TOOLKIT
12470 Window window = XtWindow (f->output_data.x->widget);
12471 #else
12472 Window window = FRAME_X_WINDOW (f);
12473 #endif
12474
12475 f->output_data.x->wm_hints.flags |= IconPositionHint;
12476 f->output_data.x->wm_hints.icon_x = icon_x;
12477 f->output_data.x->wm_hints.icon_y = icon_y;
12478
12479 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12480 }
12481
12482 \f
12483 /***********************************************************************
12484 Fonts
12485 ***********************************************************************/
12486
12487 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
12488
12489 struct font_info *
12490 x_get_font_info (f, font_idx)
12491 FRAME_PTR f;
12492 int font_idx;
12493 {
12494 return (FRAME_X_FONT_TABLE (f) + font_idx);
12495 }
12496
12497
12498 /* Return a list of names of available fonts matching PATTERN on frame
12499 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
12500 to be listed. Frame F NULL means we have not yet created any
12501 frame on X, and consult the first display in x_display_list.
12502 MAXNAMES sets a limit on how many fonts to match. */
12503
12504 Lisp_Object
12505 x_list_fonts (f, pattern, size, maxnames)
12506 FRAME_PTR f;
12507 Lisp_Object pattern;
12508 int size;
12509 int maxnames;
12510 {
12511 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12512 Lisp_Object tem, second_best;
12513 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
12514 int try_XLoadQueryFont = 0;
12515 int count;
12516
12517 patterns = Fassoc (pattern, Valternate_fontname_alist);
12518 if (NILP (patterns))
12519 patterns = Fcons (pattern, Qnil);
12520
12521 if (maxnames == 1 && !size)
12522 /* We can return any single font matching PATTERN. */
12523 try_XLoadQueryFont = 1;
12524
12525 for (; CONSP (patterns); patterns = XCDR (patterns))
12526 {
12527 int num_fonts;
12528 char **names = NULL;
12529
12530 pattern = XCAR (patterns);
12531 /* See if we cached the result for this particular query.
12532 The cache is an alist of the form:
12533 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
12534 */
12535 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
12536 key = Fcons (pattern, make_number (maxnames)),
12537 !NILP (list = Fassoc (key, tem))))
12538 {
12539 list = Fcdr_safe (list);
12540 /* We have a cashed list. Don't have to get the list again. */
12541 goto label_cached;
12542 }
12543
12544 /* At first, put PATTERN in the cache. */
12545
12546 BLOCK_INPUT;
12547 count = x_catch_errors (dpy);
12548
12549 if (try_XLoadQueryFont)
12550 {
12551 XFontStruct *font;
12552 unsigned long value;
12553
12554 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
12555 if (x_had_errors_p (dpy))
12556 {
12557 /* This error is perhaps due to insufficient memory on X
12558 server. Let's just ignore it. */
12559 font = NULL;
12560 x_clear_errors (dpy);
12561 }
12562
12563 if (font
12564 && XGetFontProperty (font, XA_FONT, &value))
12565 {
12566 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12567 int len = strlen (name);
12568 char *tmp;
12569
12570 /* If DXPC (a Differential X Protocol Compressor)
12571 Ver.3.7 is running, XGetAtomName will return null
12572 string. We must avoid such a name. */
12573 if (len == 0)
12574 try_XLoadQueryFont = 0;
12575 else
12576 {
12577 num_fonts = 1;
12578 names = (char **) alloca (sizeof (char *));
12579 /* Some systems only allow alloca assigned to a
12580 simple var. */
12581 tmp = (char *) alloca (len + 1); names[0] = tmp;
12582 bcopy (name, names[0], len + 1);
12583 XFree (name);
12584 }
12585 }
12586 else
12587 try_XLoadQueryFont = 0;
12588
12589 if (font)
12590 XFreeFont (dpy, font);
12591 }
12592
12593 if (!try_XLoadQueryFont)
12594 {
12595 /* We try at least 10 fonts because XListFonts will return
12596 auto-scaled fonts at the head. */
12597 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12598 &num_fonts);
12599 if (x_had_errors_p (dpy))
12600 {
12601 /* This error is perhaps due to insufficient memory on X
12602 server. Let's just ignore it. */
12603 names = NULL;
12604 x_clear_errors (dpy);
12605 }
12606 }
12607
12608 x_uncatch_errors (dpy, count);
12609 UNBLOCK_INPUT;
12610
12611 if (names)
12612 {
12613 int i;
12614
12615 /* Make a list of all the fonts we got back.
12616 Store that in the font cache for the display. */
12617 for (i = 0; i < num_fonts; i++)
12618 {
12619 int width = 0;
12620 char *p = names[i];
12621 int average_width = -1, dashes = 0;
12622
12623 /* Count the number of dashes in NAMES[I]. If there are
12624 14 dashes, and the field value following 12th dash
12625 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12626 is usually too ugly to be used for editing. Let's
12627 ignore it. */
12628 while (*p)
12629 if (*p++ == '-')
12630 {
12631 dashes++;
12632 if (dashes == 7) /* PIXEL_SIZE field */
12633 width = atoi (p);
12634 else if (dashes == 12) /* AVERAGE_WIDTH field */
12635 average_width = atoi (p);
12636 }
12637 if (dashes < 14 || average_width != 0)
12638 {
12639 tem = build_string (names[i]);
12640 if (NILP (Fassoc (tem, list)))
12641 {
12642 if (STRINGP (Vx_pixel_size_width_font_regexp)
12643 && ((fast_c_string_match_ignore_case
12644 (Vx_pixel_size_width_font_regexp, names[i]))
12645 >= 0))
12646 /* We can set the value of PIXEL_SIZE to the
12647 width of this font. */
12648 list = Fcons (Fcons (tem, make_number (width)), list);
12649 else
12650 /* For the moment, width is not known. */
12651 list = Fcons (Fcons (tem, Qnil), list);
12652 }
12653 }
12654 }
12655 if (!try_XLoadQueryFont)
12656 XFreeFontNames (names);
12657 }
12658
12659 /* Now store the result in the cache. */
12660 if (f != NULL)
12661 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
12662 = Fcons (Fcons (key, list),
12663 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
12664
12665 label_cached:
12666 if (NILP (list)) continue; /* Try the remaining alternatives. */
12667
12668 newlist = second_best = Qnil;
12669 /* Make a list of the fonts that have the right width. */
12670 for (; CONSP (list); list = XCDR (list))
12671 {
12672 int found_size;
12673
12674 tem = XCAR (list);
12675
12676 if (!CONSP (tem) || NILP (XCAR (tem)))
12677 continue;
12678 if (!size)
12679 {
12680 newlist = Fcons (XCAR (tem), newlist);
12681 continue;
12682 }
12683
12684 if (!INTEGERP (XCDR (tem)))
12685 {
12686 /* Since we have not yet known the size of this font, we
12687 must try slow function call XLoadQueryFont. */
12688 XFontStruct *thisinfo;
12689
12690 BLOCK_INPUT;
12691 count = x_catch_errors (dpy);
12692 thisinfo = XLoadQueryFont (dpy,
12693 XSTRING (XCAR (tem))->data);
12694 if (x_had_errors_p (dpy))
12695 {
12696 /* This error is perhaps due to insufficient memory on X
12697 server. Let's just ignore it. */
12698 thisinfo = NULL;
12699 x_clear_errors (dpy);
12700 }
12701 x_uncatch_errors (dpy, count);
12702 UNBLOCK_INPUT;
12703
12704 if (thisinfo)
12705 {
12706 XCDR (tem)
12707 = (thisinfo->min_bounds.width == 0
12708 ? make_number (0)
12709 : make_number (thisinfo->max_bounds.width));
12710 XFreeFont (dpy, thisinfo);
12711 }
12712 else
12713 /* For unknown reason, the previous call of XListFont had
12714 returned a font which can't be opened. Record the size
12715 as 0 not to try to open it again. */
12716 XCDR (tem) = make_number (0);
12717 }
12718
12719 found_size = XINT (XCDR (tem));
12720 if (found_size == size)
12721 newlist = Fcons (XCAR (tem), newlist);
12722 else if (found_size > 0)
12723 {
12724 if (NILP (second_best))
12725 second_best = tem;
12726 else if (found_size < size)
12727 {
12728 if (XINT (XCDR (second_best)) > size
12729 || XINT (XCDR (second_best)) < found_size)
12730 second_best = tem;
12731 }
12732 else
12733 {
12734 if (XINT (XCDR (second_best)) > size
12735 && XINT (XCDR (second_best)) > found_size)
12736 second_best = tem;
12737 }
12738 }
12739 }
12740 if (!NILP (newlist))
12741 break;
12742 else if (!NILP (second_best))
12743 {
12744 newlist = Fcons (XCAR (second_best), Qnil);
12745 break;
12746 }
12747 }
12748
12749 return newlist;
12750 }
12751
12752
12753 #if GLYPH_DEBUG
12754
12755 /* Check that FONT is valid on frame F. It is if it can be found in F's
12756 font table. */
12757
12758 static void
12759 x_check_font (f, font)
12760 struct frame *f;
12761 XFontStruct *font;
12762 {
12763 int i;
12764 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12765
12766 xassert (font != NULL);
12767
12768 for (i = 0; i < dpyinfo->n_fonts; i++)
12769 if (dpyinfo->font_table[i].name
12770 && font == dpyinfo->font_table[i].font)
12771 break;
12772
12773 xassert (i < dpyinfo->n_fonts);
12774 }
12775
12776 #endif /* GLYPH_DEBUG != 0 */
12777
12778 /* Set *W to the minimum width, *H to the minimum font height of FONT.
12779 Note: There are (broken) X fonts out there with invalid XFontStruct
12780 min_bounds contents. For example, handa@etl.go.jp reports that
12781 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
12782 have font->min_bounds.width == 0. */
12783
12784 static INLINE void
12785 x_font_min_bounds (font, w, h)
12786 XFontStruct *font;
12787 int *w, *h;
12788 {
12789 *h = FONT_HEIGHT (font);
12790 *w = font->min_bounds.width;
12791
12792 /* Try to handle the case where FONT->min_bounds has invalid
12793 contents. Since the only font known to have invalid min_bounds
12794 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
12795 if (*w <= 0)
12796 *w = font->max_bounds.width;
12797 }
12798
12799
12800 /* Compute the smallest character width and smallest font height over
12801 all fonts available on frame F. Set the members smallest_char_width
12802 and smallest_font_height in F's x_display_info structure to
12803 the values computed. Value is non-zero if smallest_font_height or
12804 smallest_char_width become smaller than they were before. */
12805
12806 static int
12807 x_compute_min_glyph_bounds (f)
12808 struct frame *f;
12809 {
12810 int i;
12811 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12812 XFontStruct *font;
12813 int old_width = dpyinfo->smallest_char_width;
12814 int old_height = dpyinfo->smallest_font_height;
12815
12816 dpyinfo->smallest_font_height = 100000;
12817 dpyinfo->smallest_char_width = 100000;
12818
12819 for (i = 0; i < dpyinfo->n_fonts; ++i)
12820 if (dpyinfo->font_table[i].name)
12821 {
12822 struct font_info *fontp = dpyinfo->font_table + i;
12823 int w, h;
12824
12825 font = (XFontStruct *) fontp->font;
12826 xassert (font != (XFontStruct *) ~0);
12827 x_font_min_bounds (font, &w, &h);
12828
12829 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
12830 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
12831 }
12832
12833 xassert (dpyinfo->smallest_char_width > 0
12834 && dpyinfo->smallest_font_height > 0);
12835
12836 return (dpyinfo->n_fonts == 1
12837 || dpyinfo->smallest_char_width < old_width
12838 || dpyinfo->smallest_font_height < old_height);
12839 }
12840
12841
12842 /* Load font named FONTNAME of the size SIZE for frame F, and return a
12843 pointer to the structure font_info while allocating it dynamically.
12844 If SIZE is 0, load any size of font.
12845 If loading is failed, return NULL. */
12846
12847 struct font_info *
12848 x_load_font (f, fontname, size)
12849 struct frame *f;
12850 register char *fontname;
12851 int size;
12852 {
12853 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12854 Lisp_Object font_names;
12855 int count;
12856
12857 /* Get a list of all the fonts that match this name. Once we
12858 have a list of matching fonts, we compare them against the fonts
12859 we already have by comparing names. */
12860 font_names = x_list_fonts (f, build_string (fontname), size, 1);
12861
12862 if (!NILP (font_names))
12863 {
12864 Lisp_Object tail;
12865 int i;
12866
12867 for (i = 0; i < dpyinfo->n_fonts; i++)
12868 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
12869 if (dpyinfo->font_table[i].name
12870 && (!strcmp (dpyinfo->font_table[i].name,
12871 XSTRING (XCAR (tail))->data)
12872 || !strcmp (dpyinfo->font_table[i].full_name,
12873 XSTRING (XCAR (tail))->data)))
12874 return (dpyinfo->font_table + i);
12875 }
12876
12877 /* Load the font and add it to the table. */
12878 {
12879 char *full_name;
12880 XFontStruct *font;
12881 struct font_info *fontp;
12882 unsigned long value;
12883 int i;
12884
12885 /* If we have found fonts by x_list_font, load one of them. If
12886 not, we still try to load a font by the name given as FONTNAME
12887 because XListFonts (called in x_list_font) of some X server has
12888 a bug of not finding a font even if the font surely exists and
12889 is loadable by XLoadQueryFont. */
12890 if (size > 0 && !NILP (font_names))
12891 fontname = (char *) XSTRING (XCAR (font_names))->data;
12892
12893 BLOCK_INPUT;
12894 count = x_catch_errors (FRAME_X_DISPLAY (f));
12895 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
12896 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12897 {
12898 /* This error is perhaps due to insufficient memory on X
12899 server. Let's just ignore it. */
12900 font = NULL;
12901 x_clear_errors (FRAME_X_DISPLAY (f));
12902 }
12903 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12904 UNBLOCK_INPUT;
12905 if (!font)
12906 return NULL;
12907
12908 /* Find a free slot in the font table. */
12909 for (i = 0; i < dpyinfo->n_fonts; ++i)
12910 if (dpyinfo->font_table[i].name == NULL)
12911 break;
12912
12913 /* If no free slot found, maybe enlarge the font table. */
12914 if (i == dpyinfo->n_fonts
12915 && dpyinfo->n_fonts == dpyinfo->font_table_size)
12916 {
12917 int sz;
12918 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
12919 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
12920 dpyinfo->font_table
12921 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
12922 }
12923
12924 fontp = dpyinfo->font_table + i;
12925 if (i == dpyinfo->n_fonts)
12926 ++dpyinfo->n_fonts;
12927
12928 /* Now fill in the slots of *FONTP. */
12929 BLOCK_INPUT;
12930 fontp->font = font;
12931 fontp->font_idx = i;
12932 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
12933 bcopy (fontname, fontp->name, strlen (fontname) + 1);
12934
12935 /* Try to get the full name of FONT. Put it in FULL_NAME. */
12936 full_name = 0;
12937 if (XGetFontProperty (font, XA_FONT, &value))
12938 {
12939 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
12940 char *p = name;
12941 int dashes = 0;
12942
12943 /* Count the number of dashes in the "full name".
12944 If it is too few, this isn't really the font's full name,
12945 so don't use it.
12946 In X11R4, the fonts did not come with their canonical names
12947 stored in them. */
12948 while (*p)
12949 {
12950 if (*p == '-')
12951 dashes++;
12952 p++;
12953 }
12954
12955 if (dashes >= 13)
12956 {
12957 full_name = (char *) xmalloc (p - name + 1);
12958 bcopy (name, full_name, p - name + 1);
12959 }
12960
12961 XFree (name);
12962 }
12963
12964 if (full_name != 0)
12965 fontp->full_name = full_name;
12966 else
12967 fontp->full_name = fontp->name;
12968
12969 fontp->size = font->max_bounds.width;
12970 fontp->height = FONT_HEIGHT (font);
12971 {
12972 /* For some font, ascent and descent in max_bounds field is
12973 larger than the above value. */
12974 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
12975 if (max_height > fontp->height)
12976 fontp->height = max_height;
12977 }
12978
12979 if (NILP (font_names))
12980 {
12981 /* We come here because of a bug of XListFonts mentioned at
12982 the head of this block. Let's store this information in
12983 the cache for x_list_fonts. */
12984 Lisp_Object lispy_name = build_string (fontname);
12985 Lisp_Object lispy_full_name = build_string (fontp->full_name);
12986
12987 XCDR (dpyinfo->name_list_element)
12988 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
12989 Fcons (Fcons (lispy_full_name,
12990 make_number (fontp->size)),
12991 Qnil)),
12992 XCDR (dpyinfo->name_list_element));
12993 if (full_name)
12994 XCDR (dpyinfo->name_list_element)
12995 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
12996 Fcons (Fcons (lispy_full_name,
12997 make_number (fontp->size)),
12998 Qnil)),
12999 XCDR (dpyinfo->name_list_element));
13000 }
13001
13002 /* The slot `encoding' specifies how to map a character
13003 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
13004 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
13005 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
13006 2:0xA020..0xFF7F). For the moment, we don't know which charset
13007 uses this font. So, we set information in fontp->encoding[1]
13008 which is never used by any charset. If mapping can't be
13009 decided, set FONT_ENCODING_NOT_DECIDED. */
13010 fontp->encoding[1]
13011 = (font->max_byte1 == 0
13012 /* 1-byte font */
13013 ? (font->min_char_or_byte2 < 0x80
13014 ? (font->max_char_or_byte2 < 0x80
13015 ? 0 /* 0x20..0x7F */
13016 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
13017 : 1) /* 0xA0..0xFF */
13018 /* 2-byte font */
13019 : (font->min_byte1 < 0x80
13020 ? (font->max_byte1 < 0x80
13021 ? (font->min_char_or_byte2 < 0x80
13022 ? (font->max_char_or_byte2 < 0x80
13023 ? 0 /* 0x2020..0x7F7F */
13024 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
13025 : 3) /* 0x20A0..0x7FFF */
13026 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
13027 : (font->min_char_or_byte2 < 0x80
13028 ? (font->max_char_or_byte2 < 0x80
13029 ? 2 /* 0xA020..0xFF7F */
13030 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
13031 : 1))); /* 0xA0A0..0xFFFF */
13032
13033 fontp->baseline_offset
13034 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
13035 ? (long) value : 0);
13036 fontp->relative_compose
13037 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
13038 ? (long) value : 0);
13039 fontp->default_ascent
13040 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
13041 ? (long) value : 0);
13042
13043 /* Set global flag fonts_changed_p to non-zero if the font loaded
13044 has a character with a smaller width than any other character
13045 before, or if the font loaded has a smalle>r height than any
13046 other font loaded before. If this happens, it will make a
13047 glyph matrix reallocation necessary. */
13048 fonts_changed_p = x_compute_min_glyph_bounds (f);
13049 UNBLOCK_INPUT;
13050 return fontp;
13051 }
13052 }
13053
13054
13055 /* Return a pointer to struct font_info of a font named FONTNAME for
13056 frame F. If no such font is loaded, return NULL. */
13057
13058 struct font_info *
13059 x_query_font (f, fontname)
13060 struct frame *f;
13061 register char *fontname;
13062 {
13063 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13064 int i;
13065
13066 for (i = 0; i < dpyinfo->n_fonts; i++)
13067 if (dpyinfo->font_table[i].name
13068 && (!strcmp (dpyinfo->font_table[i].name, fontname)
13069 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
13070 return (dpyinfo->font_table + i);
13071 return NULL;
13072 }
13073
13074
13075 /* Find a CCL program for a font specified by FONTP, and set the member
13076 `encoder' of the structure. */
13077
13078 void
13079 x_find_ccl_program (fontp)
13080 struct font_info *fontp;
13081 {
13082 Lisp_Object list, elt;
13083
13084 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
13085 {
13086 elt = XCAR (list);
13087 if (CONSP (elt)
13088 && STRINGP (XCAR (elt))
13089 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
13090 >= 0))
13091 break;
13092 }
13093 if (! NILP (list))
13094 {
13095 struct ccl_program *ccl
13096 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
13097
13098 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
13099 xfree (ccl);
13100 else
13101 fontp->font_encoder = ccl;
13102 }
13103 }
13104
13105
13106 \f
13107 /***********************************************************************
13108 Initialization
13109 ***********************************************************************/
13110
13111 #ifdef USE_X_TOOLKIT
13112 static XrmOptionDescRec emacs_options[] = {
13113 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13114 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13115
13116 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13117 XrmoptionSepArg, NULL},
13118 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13119
13120 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13121 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13122 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13123 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13124 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13125 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13126 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13127 };
13128 #endif /* USE_X_TOOLKIT */
13129
13130 static int x_initialized;
13131
13132 #ifdef MULTI_KBOARD
13133 /* Test whether two display-name strings agree up to the dot that separates
13134 the screen number from the server number. */
13135 static int
13136 same_x_server (name1, name2)
13137 char *name1, *name2;
13138 {
13139 int seen_colon = 0;
13140 unsigned char *system_name = XSTRING (Vsystem_name)->data;
13141 int system_name_length = strlen (system_name);
13142 int length_until_period = 0;
13143
13144 while (system_name[length_until_period] != 0
13145 && system_name[length_until_period] != '.')
13146 length_until_period++;
13147
13148 /* Treat `unix' like an empty host name. */
13149 if (! strncmp (name1, "unix:", 5))
13150 name1 += 4;
13151 if (! strncmp (name2, "unix:", 5))
13152 name2 += 4;
13153 /* Treat this host's name like an empty host name. */
13154 if (! strncmp (name1, system_name, system_name_length)
13155 && name1[system_name_length] == ':')
13156 name1 += system_name_length;
13157 if (! strncmp (name2, system_name, system_name_length)
13158 && name2[system_name_length] == ':')
13159 name2 += system_name_length;
13160 /* Treat this host's domainless name like an empty host name. */
13161 if (! strncmp (name1, system_name, length_until_period)
13162 && name1[length_until_period] == ':')
13163 name1 += length_until_period;
13164 if (! strncmp (name2, system_name, length_until_period)
13165 && name2[length_until_period] == ':')
13166 name2 += length_until_period;
13167
13168 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13169 {
13170 if (*name1 == ':')
13171 seen_colon++;
13172 if (seen_colon && *name1 == '.')
13173 return 1;
13174 }
13175 return (seen_colon
13176 && (*name1 == '.' || *name1 == '\0')
13177 && (*name2 == '.' || *name2 == '\0'));
13178 }
13179 #endif
13180
13181 struct x_display_info *
13182 x_term_init (display_name, xrm_option, resource_name)
13183 Lisp_Object display_name;
13184 char *xrm_option;
13185 char *resource_name;
13186 {
13187 int connection;
13188 Display *dpy;
13189 struct x_display_info *dpyinfo;
13190 XrmDatabase xrdb;
13191
13192 BLOCK_INPUT;
13193
13194 if (!x_initialized)
13195 {
13196 x_initialize ();
13197 x_initialized = 1;
13198 }
13199
13200 #ifdef USE_X_TOOLKIT
13201 /* weiner@footloose.sps.mot.com reports that this causes
13202 errors with X11R5:
13203 X protocol error: BadAtom (invalid Atom parameter)
13204 on protocol request 18skiloaf.
13205 So let's not use it until R6. */
13206 #ifdef HAVE_X11XTR6
13207 XtSetLanguageProc (NULL, NULL, NULL);
13208 #endif
13209
13210 {
13211 int argc = 0;
13212 char *argv[3];
13213
13214 argv[0] = "";
13215 argc = 1;
13216 if (xrm_option)
13217 {
13218 argv[argc++] = "-xrm";
13219 argv[argc++] = xrm_option;
13220 }
13221 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
13222 resource_name, EMACS_CLASS,
13223 emacs_options, XtNumber (emacs_options),
13224 &argc, argv);
13225
13226 #ifdef HAVE_X11XTR6
13227 /* I think this is to compensate for XtSetLanguageProc. */
13228 fixup_locale ();
13229 #endif
13230 }
13231
13232 #else /* not USE_X_TOOLKIT */
13233 #ifdef HAVE_X11R5
13234 XSetLocaleModifiers ("");
13235 #endif
13236 dpy = XOpenDisplay (XSTRING (display_name)->data);
13237 #endif /* not USE_X_TOOLKIT */
13238
13239 /* Detect failure. */
13240 if (dpy == 0)
13241 {
13242 UNBLOCK_INPUT;
13243 return 0;
13244 }
13245
13246 /* We have definitely succeeded. Record the new connection. */
13247
13248 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
13249
13250 #ifdef MULTI_KBOARD
13251 {
13252 struct x_display_info *share;
13253 Lisp_Object tail;
13254
13255 for (share = x_display_list, tail = x_display_name_list; share;
13256 share = share->next, tail = XCDR (tail))
13257 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
13258 XSTRING (display_name)->data))
13259 break;
13260 if (share)
13261 dpyinfo->kboard = share->kboard;
13262 else
13263 {
13264 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
13265 init_kboard (dpyinfo->kboard);
13266 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
13267 {
13268 char *vendor = ServerVendor (dpy);
13269 UNBLOCK_INPUT;
13270 dpyinfo->kboard->Vsystem_key_alist
13271 = call1 (Qvendor_specific_keysyms,
13272 build_string (vendor ? vendor : ""));
13273 BLOCK_INPUT;
13274 }
13275
13276 dpyinfo->kboard->next_kboard = all_kboards;
13277 all_kboards = dpyinfo->kboard;
13278 /* Don't let the initial kboard remain current longer than necessary.
13279 That would cause problems if a file loaded on startup tries to
13280 prompt in the mini-buffer. */
13281 if (current_kboard == initial_kboard)
13282 current_kboard = dpyinfo->kboard;
13283 }
13284 dpyinfo->kboard->reference_count++;
13285 }
13286 #endif
13287
13288 /* Put this display on the chain. */
13289 dpyinfo->next = x_display_list;
13290 x_display_list = dpyinfo;
13291
13292 /* Put it on x_display_name_list as well, to keep them parallel. */
13293 x_display_name_list = Fcons (Fcons (display_name, Qnil),
13294 x_display_name_list);
13295 dpyinfo->name_list_element = XCAR (x_display_name_list);
13296
13297 dpyinfo->display = dpy;
13298
13299 #if 0
13300 XSetAfterFunction (x_current_display, x_trace_wire);
13301 #endif /* ! 0 */
13302
13303 dpyinfo->x_id_name
13304 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13305 + STRING_BYTES (XSTRING (Vsystem_name))
13306 + 2);
13307 sprintf (dpyinfo->x_id_name, "%s@%s",
13308 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
13309
13310 /* Figure out which modifier bits mean what. */
13311 x_find_modifier_meanings (dpyinfo);
13312
13313 /* Get the scroll bar cursor. */
13314 dpyinfo->vertical_scroll_bar_cursor
13315 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
13316
13317 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13318 resource_name, EMACS_CLASS);
13319 #ifdef HAVE_XRMSETDATABASE
13320 XrmSetDatabase (dpyinfo->display, xrdb);
13321 #else
13322 dpyinfo->display->db = xrdb;
13323 #endif
13324 /* Put the rdb where we can find it in a way that works on
13325 all versions. */
13326 dpyinfo->xrdb = xrdb;
13327
13328 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13329 DefaultScreen (dpyinfo->display));
13330 select_visual (dpyinfo);
13331 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
13332 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13333 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13334 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13335 dpyinfo->grabbed = 0;
13336 dpyinfo->reference_count = 0;
13337 dpyinfo->icon_bitmap_id = -1;
13338 dpyinfo->font_table = NULL;
13339 dpyinfo->n_fonts = 0;
13340 dpyinfo->font_table_size = 0;
13341 dpyinfo->bitmaps = 0;
13342 dpyinfo->bitmaps_size = 0;
13343 dpyinfo->bitmaps_last = 0;
13344 dpyinfo->scratch_cursor_gc = 0;
13345 dpyinfo->mouse_face_mouse_frame = 0;
13346 dpyinfo->mouse_face_deferred_gc = 0;
13347 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13348 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
13349 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
13350 dpyinfo->mouse_face_window = Qnil;
13351 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13352 dpyinfo->mouse_face_defer = 0;
13353 dpyinfo->x_focus_frame = 0;
13354 dpyinfo->x_focus_event_frame = 0;
13355 dpyinfo->x_highlight_frame = 0;
13356 dpyinfo->image_cache = make_image_cache ();
13357
13358 /* See if a private colormap is requested. */
13359 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13360 {
13361 if (dpyinfo->visual->class == PseudoColor)
13362 {
13363 Lisp_Object value;
13364 value = display_x_get_resource (dpyinfo,
13365 build_string ("privateColormap"),
13366 build_string ("PrivateColormap"),
13367 Qnil, Qnil);
13368 if (STRINGP (value)
13369 && (!strcmp (XSTRING (value)->data, "true")
13370 || !strcmp (XSTRING (value)->data, "on")))
13371 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13372 }
13373 }
13374 else
13375 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13376 dpyinfo->visual, AllocNone);
13377
13378 {
13379 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13380 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13381 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13382 dpyinfo->resy = pixels * 25.4 / mm;
13383 pixels = DisplayWidth (dpyinfo->display, screen_number);
13384 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13385 dpyinfo->resx = pixels * 25.4 / mm;
13386 }
13387
13388 dpyinfo->Xatom_wm_protocols
13389 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13390 dpyinfo->Xatom_wm_take_focus
13391 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13392 dpyinfo->Xatom_wm_save_yourself
13393 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13394 dpyinfo->Xatom_wm_delete_window
13395 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13396 dpyinfo->Xatom_wm_change_state
13397 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13398 dpyinfo->Xatom_wm_configure_denied
13399 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13400 dpyinfo->Xatom_wm_window_moved
13401 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13402 dpyinfo->Xatom_editres
13403 = XInternAtom (dpyinfo->display, "Editres", False);
13404 dpyinfo->Xatom_CLIPBOARD
13405 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13406 dpyinfo->Xatom_TIMESTAMP
13407 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13408 dpyinfo->Xatom_TEXT
13409 = XInternAtom (dpyinfo->display, "TEXT", False);
13410 dpyinfo->Xatom_COMPOUND_TEXT
13411 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
13412 dpyinfo->Xatom_DELETE
13413 = XInternAtom (dpyinfo->display, "DELETE", False);
13414 dpyinfo->Xatom_MULTIPLE
13415 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13416 dpyinfo->Xatom_INCR
13417 = XInternAtom (dpyinfo->display, "INCR", False);
13418 dpyinfo->Xatom_EMACS_TMP
13419 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13420 dpyinfo->Xatom_TARGETS
13421 = XInternAtom (dpyinfo->display, "TARGETS", False);
13422 dpyinfo->Xatom_NULL
13423 = XInternAtom (dpyinfo->display, "NULL", False);
13424 dpyinfo->Xatom_ATOM_PAIR
13425 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
13426 /* For properties of font. */
13427 dpyinfo->Xatom_PIXEL_SIZE
13428 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13429 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13430 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13431 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13432 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
13433 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13434 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
13435
13436 /* Ghostscript support. */
13437 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13438 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13439
13440 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13441 False);
13442
13443 dpyinfo->cut_buffers_initialized = 0;
13444
13445 connection = ConnectionNumber (dpyinfo->display);
13446 dpyinfo->connection = connection;
13447
13448 {
13449 char null_bits[1];
13450
13451 null_bits[0] = 0x00;
13452
13453 dpyinfo->null_pixel
13454 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13455 null_bits, 1, 1, (long) 0, (long) 0,
13456 1);
13457 }
13458
13459 {
13460 extern int gray_bitmap_width, gray_bitmap_height;
13461 extern unsigned char *gray_bitmap_bits;
13462 dpyinfo->gray
13463 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13464 gray_bitmap_bits,
13465 gray_bitmap_width, gray_bitmap_height,
13466 (unsigned long) 1, (unsigned long) 0, 1);
13467 }
13468
13469 #ifdef HAVE_X_I18N
13470 xim_initialize (dpyinfo, resource_name);
13471 #endif
13472
13473 #ifdef subprocesses
13474 /* This is only needed for distinguishing keyboard and process input. */
13475 if (connection != 0)
13476 add_keyboard_wait_descriptor (connection);
13477 #endif
13478
13479 #ifndef F_SETOWN_BUG
13480 #ifdef F_SETOWN
13481 #ifdef F_SETOWN_SOCK_NEG
13482 /* stdin is a socket here */
13483 fcntl (connection, F_SETOWN, -getpid ());
13484 #else /* ! defined (F_SETOWN_SOCK_NEG) */
13485 fcntl (connection, F_SETOWN, getpid ());
13486 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
13487 #endif /* ! defined (F_SETOWN) */
13488 #endif /* F_SETOWN_BUG */
13489
13490 #ifdef SIGIO
13491 if (interrupt_input)
13492 init_sigio (connection);
13493 #endif /* ! defined (SIGIO) */
13494
13495 #ifdef USE_LUCID
13496 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
13497 /* Make sure that we have a valid font for dialog boxes
13498 so that Xt does not crash. */
13499 {
13500 Display *dpy = dpyinfo->display;
13501 XrmValue d, fr, to;
13502 Font font;
13503 int count;
13504
13505 d.addr = (XPointer)&dpy;
13506 d.size = sizeof (Display *);
13507 fr.addr = XtDefaultFont;
13508 fr.size = sizeof (XtDefaultFont);
13509 to.size = sizeof (Font *);
13510 to.addr = (XPointer)&font;
13511 count = x_catch_errors (dpy);
13512 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13513 abort ();
13514 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13515 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
13516 x_uncatch_errors (dpy, count);
13517 }
13518 #endif
13519 #endif
13520
13521 /* See if we should run in synchronous mode. This is useful
13522 for debugging X code. */
13523 {
13524 Lisp_Object value;
13525 value = display_x_get_resource (dpyinfo,
13526 build_string ("synchronous"),
13527 build_string ("Synchronous"),
13528 Qnil, Qnil);
13529 if (STRINGP (value)
13530 && (!strcmp (XSTRING (value)->data, "true")
13531 || !strcmp (XSTRING (value)->data, "on")))
13532 XSynchronize (dpyinfo->display, True);
13533 }
13534
13535 UNBLOCK_INPUT;
13536
13537 return dpyinfo;
13538 }
13539 \f
13540 /* Get rid of display DPYINFO, assuming all frames are already gone,
13541 and without sending any more commands to the X server. */
13542
13543 void
13544 x_delete_display (dpyinfo)
13545 struct x_display_info *dpyinfo;
13546 {
13547 delete_keyboard_wait_descriptor (dpyinfo->connection);
13548
13549 /* Discard this display from x_display_name_list and x_display_list.
13550 We can't use Fdelq because that can quit. */
13551 if (! NILP (x_display_name_list)
13552 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13553 x_display_name_list = XCDR (x_display_name_list);
13554 else
13555 {
13556 Lisp_Object tail;
13557
13558 tail = x_display_name_list;
13559 while (CONSP (tail) && CONSP (XCDR (tail)))
13560 {
13561 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
13562 {
13563 XCDR (tail) = XCDR (XCDR (tail));
13564 break;
13565 }
13566 tail = XCDR (tail);
13567 }
13568 }
13569
13570 if (next_noop_dpyinfo == dpyinfo)
13571 next_noop_dpyinfo = dpyinfo->next;
13572
13573 if (x_display_list == dpyinfo)
13574 x_display_list = dpyinfo->next;
13575 else
13576 {
13577 struct x_display_info *tail;
13578
13579 for (tail = x_display_list; tail; tail = tail->next)
13580 if (tail->next == dpyinfo)
13581 tail->next = tail->next->next;
13582 }
13583
13584 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13585 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
13586 XrmDestroyDatabase (dpyinfo->xrdb);
13587 #endif
13588 #endif
13589 #ifdef MULTI_KBOARD
13590 if (--dpyinfo->kboard->reference_count == 0)
13591 delete_kboard (dpyinfo->kboard);
13592 #endif
13593 #ifdef HAVE_X_I18N
13594 if (dpyinfo->xim)
13595 xim_close_dpy (dpyinfo);
13596 #endif
13597
13598 xfree (dpyinfo->font_table);
13599 xfree (dpyinfo->x_id_name);
13600 xfree (dpyinfo);
13601 }
13602 \f
13603 /* Set up use of X before we make the first connection. */
13604
13605 static struct redisplay_interface x_redisplay_interface =
13606 {
13607 x_produce_glyphs,
13608 x_write_glyphs,
13609 x_insert_glyphs,
13610 x_clear_end_of_line,
13611 x_scroll_run,
13612 x_after_update_window_line,
13613 x_update_window_begin,
13614 x_update_window_end,
13615 XTcursor_to,
13616 x_flush,
13617 x_clear_mouse_face,
13618 x_get_glyph_overhangs,
13619 x_fix_overlapping_area
13620 };
13621
13622 void
13623 x_initialize ()
13624 {
13625 rif = &x_redisplay_interface;
13626
13627 clear_frame_hook = x_clear_frame;
13628 ins_del_lines_hook = x_ins_del_lines;
13629 change_line_highlight_hook = x_change_line_highlight;
13630 delete_glyphs_hook = x_delete_glyphs;
13631 ring_bell_hook = XTring_bell;
13632 reset_terminal_modes_hook = XTreset_terminal_modes;
13633 set_terminal_modes_hook = XTset_terminal_modes;
13634 update_begin_hook = x_update_begin;
13635 update_end_hook = x_update_end;
13636 set_terminal_window_hook = XTset_terminal_window;
13637 read_socket_hook = XTread_socket;
13638 frame_up_to_date_hook = XTframe_up_to_date;
13639 reassert_line_highlight_hook = XTreassert_line_highlight;
13640 mouse_position_hook = XTmouse_position;
13641 frame_rehighlight_hook = XTframe_rehighlight;
13642 frame_raise_lower_hook = XTframe_raise_lower;
13643 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13644 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13645 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13646 judge_scroll_bars_hook = XTjudge_scroll_bars;
13647 estimate_mode_line_height_hook = x_estimate_mode_line_height;
13648
13649 scroll_region_ok = 1; /* we'll scroll partial frames */
13650 char_ins_del_ok = 0; /* just as fast to write the line */
13651 line_ins_del_ok = 1; /* we'll just blt 'em */
13652 fast_clear_end_of_line = 1; /* X does this well */
13653 memory_below_frame = 0; /* we don't remember what scrolls
13654 off the bottom */
13655 baud_rate = 19200;
13656
13657 x_noop_count = 0;
13658 last_tool_bar_item = -1;
13659 any_help_event_p = 0;
13660
13661 /* Try to use interrupt input; if we can't, then start polling. */
13662 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13663
13664 #ifdef USE_X_TOOLKIT
13665 XtToolkitInitialize ();
13666 Xt_app_con = XtCreateApplicationContext ();
13667 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
13668
13669 /* Install an asynchronous timer that processes Xt timeout events
13670 every 0.1s. This is necessary because some widget sets use
13671 timeouts internally, for example the LessTif menu bar, or the
13672 Xaw3d scroll bar. When Xt timouts aren't processed, these
13673 widgets don't behave normally. */
13674 {
13675 EMACS_TIME interval;
13676 EMACS_SET_SECS_USECS (interval, 0, 100000);
13677 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13678 }
13679 #endif
13680
13681 #if USE_TOOLKIT_SCROLL_BARS
13682 xaw3d_arrow_scroll = False;
13683 xaw3d_pick_top = True;
13684 #endif
13685
13686 /* Note that there is no real way portable across R3/R4 to get the
13687 original error handler. */
13688 XSetErrorHandler (x_error_handler);
13689 XSetIOErrorHandler (x_io_error_quitter);
13690
13691 /* Disable Window Change signals; they are handled by X events. */
13692 #ifdef SIGWINCH
13693 signal (SIGWINCH, SIG_DFL);
13694 #endif /* ! defined (SIGWINCH) */
13695
13696 signal (SIGPIPE, x_connection_signal);
13697 }
13698
13699
13700 void
13701 syms_of_xterm ()
13702 {
13703 staticpro (&x_error_message_string);
13704 x_error_message_string = Qnil;
13705
13706 staticpro (&x_display_name_list);
13707 x_display_name_list = Qnil;
13708
13709 staticpro (&last_mouse_scroll_bar);
13710 last_mouse_scroll_bar = Qnil;
13711
13712 staticpro (&Qvendor_specific_keysyms);
13713 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
13714
13715 staticpro (&last_mouse_press_frame);
13716 last_mouse_press_frame = Qnil;
13717
13718 help_echo = Qnil;
13719 staticpro (&help_echo);
13720 help_echo_object = Qnil;
13721 staticpro (&help_echo_object);
13722 help_echo_window = Qnil;
13723 staticpro (&help_echo_window);
13724 previous_help_echo = Qnil;
13725 staticpro (&previous_help_echo);
13726 help_echo_pos = -1;
13727
13728 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
13729 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
13730 For example, if a block cursor is over a tab, it will be drawn as\n\
13731 wide as that tab on the display.");
13732 x_stretch_cursor_p = 0;
13733
13734 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
13735 "If not nil, Emacs uses toolkit scroll bars.");
13736 #if USE_TOOLKIT_SCROLL_BARS
13737 x_toolkit_scroll_bars_p = 1;
13738 #else
13739 x_toolkit_scroll_bars_p = 0;
13740 #endif
13741
13742 staticpro (&last_mouse_motion_frame);
13743 last_mouse_motion_frame = Qnil;
13744 }
13745
13746 #endif /* not HAVE_X_WINDOWS */
13747