(XTread_socket): Change calls to gen_help_event.
[bpt/emacs.git] / 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 #endif /* USE_X_TOOLKIT */
3256
3257
3258 /* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3259 CMAP. If an exact match can't be allocated, try the nearest color
3260 available. Value is non-zero if successful. Set *COLOR to the
3261 color allocated. */
3262
3263 int
3264 x_alloc_nearest_color (f, cmap, color)
3265 struct frame *f;
3266 Colormap cmap;
3267 XColor *color;
3268 {
3269 Display *display = FRAME_X_DISPLAY (f);
3270 Screen *screen = FRAME_X_SCREEN (f);
3271 int rc;
3272
3273 gamma_correct (f, color);
3274 rc = XAllocColor (display, cmap, color);
3275 if (rc == 0)
3276 {
3277 /* If we got to this point, the colormap is full, so we're going
3278 to try to get the next closest color. The algorithm used is
3279 a least-squares matching, which is what X uses for closest
3280 color matching with StaticColor visuals. */
3281 int nearest, i;
3282 unsigned long nearest_delta = ~0;
3283 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
3284 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
3285
3286 for (i = 0; i < ncells; ++i)
3287 cells[i].pixel = i;
3288 XQueryColors (display, cmap, cells, ncells);
3289
3290 for (nearest = i = 0; i < ncells; ++i)
3291 {
3292 long dred = (color->red >> 8) - (cells[i].red >> 8);
3293 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3294 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3295 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3296
3297 if (delta < nearest_delta)
3298 {
3299 nearest = i;
3300 nearest_delta = delta;
3301 }
3302 }
3303
3304 color->red = cells[nearest].red;
3305 color->green = cells[nearest].green;
3306 color->blue = cells[nearest].blue;
3307 rc = XAllocColor (display, cmap, color);
3308 }
3309
3310 #ifdef DEBUG_X_COLORS
3311 if (rc)
3312 register_color (color->pixel);
3313 #endif /* DEBUG_X_COLORS */
3314
3315 return rc;
3316 }
3317
3318
3319 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3320 It's necessary to do this instead of just using PIXEL directly to
3321 get color reference counts right. */
3322
3323 unsigned long
3324 x_copy_color (f, pixel)
3325 struct frame *f;
3326 unsigned long pixel;
3327 {
3328 XColor color;
3329
3330 color.pixel = pixel;
3331 BLOCK_INPUT;
3332 XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3333 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3334 UNBLOCK_INPUT;
3335 #ifdef DEBUG_X_COLORS
3336 register_color (pixel);
3337 #endif
3338 return color.pixel;
3339 }
3340
3341
3342 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3343 It's necessary to do this instead of just using PIXEL directly to
3344 get color reference counts right. */
3345
3346 unsigned long
3347 x_copy_dpy_color (dpy, cmap, pixel)
3348 Display *dpy;
3349 Colormap cmap;
3350 unsigned long pixel;
3351 {
3352 XColor color;
3353
3354 color.pixel = pixel;
3355 BLOCK_INPUT;
3356 XQueryColor (dpy, cmap, &color);
3357 XAllocColor (dpy, cmap, &color);
3358 UNBLOCK_INPUT;
3359 #ifdef DEBUG_X_COLORS
3360 register_color (pixel);
3361 #endif
3362 return color.pixel;
3363 }
3364
3365
3366 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3367 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3368 If this produces the same color as PIXEL, try a color where all RGB
3369 values have DELTA added. Return the allocated color in *PIXEL.
3370 DISPLAY is the X display, CMAP is the colormap to operate on.
3371 Value is non-zero if successful. */
3372
3373 static int
3374 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3375 struct frame *f;
3376 Display *display;
3377 Colormap cmap;
3378 unsigned long *pixel;
3379 double factor;
3380 int delta;
3381 {
3382 XColor color, new;
3383 int success_p;
3384
3385 /* Get RGB color values. */
3386 color.pixel = *pixel;
3387 XQueryColor (display, cmap, &color);
3388
3389 /* Change RGB values by specified FACTOR. Avoid overflow! */
3390 xassert (factor >= 0);
3391 new.red = min (0xffff, factor * color.red);
3392 new.green = min (0xffff, factor * color.green);
3393 new.blue = min (0xffff, factor * color.blue);
3394
3395 /* Try to allocate the color. */
3396 success_p = x_alloc_nearest_color (f, cmap, &new);
3397 if (success_p)
3398 {
3399 if (new.pixel == *pixel)
3400 {
3401 /* If we end up with the same color as before, try adding
3402 delta to the RGB values. */
3403 x_free_colors (f, &new.pixel, 1);
3404
3405 new.red = min (0xffff, delta + color.red);
3406 new.green = min (0xffff, delta + color.green);
3407 new.blue = min (0xffff, delta + color.blue);
3408 success_p = x_alloc_nearest_color (f, cmap, &new);
3409 }
3410 else
3411 success_p = 1;
3412 *pixel = new.pixel;
3413 }
3414
3415 return success_p;
3416 }
3417
3418
3419 /* Set up the foreground color for drawing relief lines of glyph
3420 string S. RELIEF is a pointer to a struct relief containing the GC
3421 with which lines will be drawn. Use a color that is FACTOR or
3422 DELTA lighter or darker than the relief's background which is found
3423 in S->f->output_data.x->relief_background. If such a color cannot
3424 be allocated, use DEFAULT_PIXEL, instead. */
3425
3426 static void
3427 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3428 struct frame *f;
3429 struct relief *relief;
3430 double factor;
3431 int delta;
3432 unsigned long default_pixel;
3433 {
3434 XGCValues xgcv;
3435 struct x_output *di = f->output_data.x;
3436 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3437 unsigned long pixel;
3438 unsigned long background = di->relief_background;
3439 Colormap cmap = FRAME_X_COLORMAP (f);
3440 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3441 Display *dpy = FRAME_X_DISPLAY (f);
3442
3443 xgcv.graphics_exposures = False;
3444 xgcv.line_width = 1;
3445
3446 /* Free previously allocated color. The color cell will be reused
3447 when it has been freed as many times as it was allocated, so this
3448 doesn't affect faces using the same colors. */
3449 if (relief->gc
3450 && relief->allocated_p)
3451 {
3452 x_free_colors (f, &relief->pixel, 1);
3453 relief->allocated_p = 0;
3454 }
3455
3456 /* Allocate new color. */
3457 xgcv.foreground = default_pixel;
3458 pixel = background;
3459 if (dpyinfo->n_planes != 1
3460 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3461 {
3462 relief->allocated_p = 1;
3463 xgcv.foreground = relief->pixel = pixel;
3464 }
3465
3466 if (relief->gc == 0)
3467 {
3468 xgcv.stipple = dpyinfo->gray;
3469 mask |= GCStipple;
3470 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3471 }
3472 else
3473 XChangeGC (dpy, relief->gc, mask, &xgcv);
3474 }
3475
3476
3477 /* Set up colors for the relief lines around glyph string S. */
3478
3479 static void
3480 x_setup_relief_colors (s)
3481 struct glyph_string *s;
3482 {
3483 struct x_output *di = s->f->output_data.x;
3484 unsigned long color;
3485
3486 if (s->face->use_box_color_for_shadows_p)
3487 color = s->face->box_color;
3488 else
3489 {
3490 XGCValues xgcv;
3491
3492 /* Get the background color of the face. */
3493 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3494 color = xgcv.background;
3495 }
3496
3497 if (di->white_relief.gc == 0
3498 || color != di->relief_background)
3499 {
3500 di->relief_background = color;
3501 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3502 WHITE_PIX_DEFAULT (s->f));
3503 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3504 BLACK_PIX_DEFAULT (s->f));
3505 }
3506 }
3507
3508
3509 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3510 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3511 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3512 relief. LEFT_P non-zero means draw a relief on the left side of
3513 the rectangle. RIGHT_P non-zero means draw a relief on the right
3514 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3515 when drawing. */
3516
3517 static void
3518 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3519 raised_p, left_p, right_p, clip_rect)
3520 struct frame *f;
3521 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3522 XRectangle *clip_rect;
3523 {
3524 int i;
3525 GC gc;
3526
3527 if (raised_p)
3528 gc = f->output_data.x->white_relief.gc;
3529 else
3530 gc = f->output_data.x->black_relief.gc;
3531 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3532
3533 /* Top. */
3534 for (i = 0; i < width; ++i)
3535 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3536 left_x + i * left_p, top_y + i,
3537 right_x + 1 - i * right_p, top_y + i);
3538
3539 /* Left. */
3540 if (left_p)
3541 for (i = 0; i < width; ++i)
3542 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3543 left_x + i, top_y + i, left_x + i, bottom_y - i);
3544
3545 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3546 if (raised_p)
3547 gc = f->output_data.x->black_relief.gc;
3548 else
3549 gc = f->output_data.x->white_relief.gc;
3550 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3551
3552 /* Bottom. */
3553 for (i = 0; i < width; ++i)
3554 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3555 left_x + i * left_p, bottom_y - i,
3556 right_x + 1 - i * right_p, bottom_y - i);
3557
3558 /* Right. */
3559 if (right_p)
3560 for (i = 0; i < width; ++i)
3561 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3562 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3563
3564 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3565 }
3566
3567
3568 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3569 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3570 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3571 left side of the rectangle. RIGHT_P non-zero means draw a line
3572 on the right side of the rectangle. CLIP_RECT is the clipping
3573 rectangle to use when drawing. */
3574
3575 static void
3576 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3577 left_p, right_p, clip_rect)
3578 struct glyph_string *s;
3579 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3580 XRectangle *clip_rect;
3581 {
3582 XGCValues xgcv;
3583
3584 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3585 XSetForeground (s->display, s->gc, s->face->box_color);
3586 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3587
3588 /* Top. */
3589 XFillRectangle (s->display, s->window, s->gc,
3590 left_x, top_y, right_x - left_x, width);
3591
3592 /* Left. */
3593 if (left_p)
3594 XFillRectangle (s->display, s->window, s->gc,
3595 left_x, top_y, width, bottom_y - top_y);
3596
3597 /* Bottom. */
3598 XFillRectangle (s->display, s->window, s->gc,
3599 left_x, bottom_y - width, right_x - left_x, width);
3600
3601 /* Right. */
3602 if (right_p)
3603 XFillRectangle (s->display, s->window, s->gc,
3604 right_x - width, top_y, width, bottom_y - top_y);
3605
3606 XSetForeground (s->display, s->gc, xgcv.foreground);
3607 XSetClipMask (s->display, s->gc, None);
3608 }
3609
3610
3611 /* Draw a box around glyph string S. */
3612
3613 static void
3614 x_draw_glyph_string_box (s)
3615 struct glyph_string *s;
3616 {
3617 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3618 int left_p, right_p;
3619 struct glyph *last_glyph;
3620 XRectangle clip_rect;
3621
3622 last_x = window_box_right (s->w, s->area);
3623 if (s->row->full_width_p
3624 && !s->w->pseudo_window_p)
3625 {
3626 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3627 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3628 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3629 }
3630
3631 /* The glyph that may have a right box line. */
3632 last_glyph = (s->cmp || s->img
3633 ? s->first_glyph
3634 : s->first_glyph + s->nchars - 1);
3635
3636 width = s->face->box_line_width;
3637 raised_p = s->face->box == FACE_RAISED_BOX;
3638 left_x = s->x;
3639 right_x = ((s->row->full_width_p
3640 ? last_x - 1
3641 : min (last_x, s->x + s->background_width) - 1));
3642 top_y = s->y;
3643 bottom_y = top_y + s->height - 1;
3644
3645 left_p = (s->first_glyph->left_box_line_p
3646 || (s->hl == DRAW_MOUSE_FACE
3647 && (s->prev == NULL
3648 || s->prev->hl != s->hl)));
3649 right_p = (last_glyph->right_box_line_p
3650 || (s->hl == DRAW_MOUSE_FACE
3651 && (s->next == NULL
3652 || s->next->hl != s->hl)));
3653
3654 x_get_glyph_string_clip_rect (s, &clip_rect);
3655
3656 if (s->face->box == FACE_SIMPLE_BOX)
3657 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3658 left_p, right_p, &clip_rect);
3659 else
3660 {
3661 x_setup_relief_colors (s);
3662 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3663 width, raised_p, left_p, right_p, &clip_rect);
3664 }
3665 }
3666
3667
3668 /* Draw foreground of image glyph string S. */
3669
3670 static void
3671 x_draw_image_foreground (s)
3672 struct glyph_string *s;
3673 {
3674 int x;
3675 int y = s->ybase - image_ascent (s->img, s->face);
3676
3677 /* If first glyph of S has a left box line, start drawing it to the
3678 right of that line. */
3679 if (s->face->box != FACE_NO_BOX
3680 && s->first_glyph->left_box_line_p)
3681 x = s->x + s->face->box_line_width;
3682 else
3683 x = s->x;
3684
3685 /* If there is a margin around the image, adjust x- and y-position
3686 by that margin. */
3687 if (s->img->margin)
3688 {
3689 x += s->img->margin;
3690 y += s->img->margin;
3691 }
3692
3693 if (s->img->pixmap)
3694 {
3695 if (s->img->mask)
3696 {
3697 /* We can't set both a clip mask and use XSetClipRectangles
3698 because the latter also sets a clip mask. We also can't
3699 trust on the shape extension to be available
3700 (XShapeCombineRegion). So, compute the rectangle to draw
3701 manually. */
3702 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3703 | GCFunction);
3704 XGCValues xgcv;
3705 XRectangle clip_rect, image_rect, r;
3706
3707 xgcv.clip_mask = s->img->mask;
3708 xgcv.clip_x_origin = x;
3709 xgcv.clip_y_origin = y;
3710 xgcv.function = GXcopy;
3711 XChangeGC (s->display, s->gc, mask, &xgcv);
3712
3713 x_get_glyph_string_clip_rect (s, &clip_rect);
3714 image_rect.x = x;
3715 image_rect.y = y;
3716 image_rect.width = s->img->width;
3717 image_rect.height = s->img->height;
3718 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3719 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3720 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3721 }
3722 else
3723 {
3724 unsigned long mask = GCClipXOrigin | GCClipYOrigin | GCFunction;
3725 XGCValues xgcv;
3726 XRectangle clip_rect, image_rect, r;
3727
3728 x_get_glyph_string_clip_rect (s, &clip_rect);
3729 image_rect.x = x;
3730 image_rect.y = y;
3731 image_rect.width = s->img->width;
3732 image_rect.height = s->img->height;
3733 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3734 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3735 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3736
3737 /* When the image has a mask, we can expect that at
3738 least part of a mouse highlight or a block cursor will
3739 be visible. If the image doesn't have a mask, make
3740 a block cursor visible by drawing a rectangle around
3741 the image. I believe it's looking better if we do
3742 nothing here for mouse-face. */
3743 if (s->hl == DRAW_CURSOR)
3744 XDrawRectangle (s->display, s->window, s->gc, x, y,
3745 s->img->width - 1, s->img->height - 1);
3746 }
3747 }
3748 else
3749 /* Draw a rectangle if image could not be loaded. */
3750 XDrawRectangle (s->display, s->window, s->gc, x, y,
3751 s->img->width - 1, s->img->height - 1);
3752 }
3753
3754
3755 /* Draw a relief around the image glyph string S. */
3756
3757 static void
3758 x_draw_image_relief (s)
3759 struct glyph_string *s;
3760 {
3761 int x0, y0, x1, y1, thick, raised_p;
3762 XRectangle r;
3763 int x;
3764 int y = s->ybase - image_ascent (s->img, s->face);
3765
3766 /* If first glyph of S has a left box line, start drawing it to the
3767 right of that line. */
3768 if (s->face->box != FACE_NO_BOX
3769 && s->first_glyph->left_box_line_p)
3770 x = s->x + s->face->box_line_width;
3771 else
3772 x = s->x;
3773
3774 /* If there is a margin around the image, adjust x- and y-position
3775 by that margin. */
3776 if (s->img->margin)
3777 {
3778 x += s->img->margin;
3779 y += s->img->margin;
3780 }
3781
3782 if (s->hl == DRAW_IMAGE_SUNKEN
3783 || s->hl == DRAW_IMAGE_RAISED)
3784 {
3785 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
3786 raised_p = s->hl == DRAW_IMAGE_RAISED;
3787 }
3788 else
3789 {
3790 thick = abs (s->img->relief);
3791 raised_p = s->img->relief > 0;
3792 }
3793
3794 x0 = x - thick;
3795 y0 = y - thick;
3796 x1 = x + s->img->width + thick - 1;
3797 y1 = y + s->img->height + thick - 1;
3798
3799 x_setup_relief_colors (s);
3800 x_get_glyph_string_clip_rect (s, &r);
3801 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3802 }
3803
3804
3805 /* Draw the foreground of image glyph string S to PIXMAP. */
3806
3807 static void
3808 x_draw_image_foreground_1 (s, pixmap)
3809 struct glyph_string *s;
3810 Pixmap pixmap;
3811 {
3812 int x;
3813 int y = s->ybase - s->y - image_ascent (s->img, s->face);
3814
3815 /* If first glyph of S has a left box line, start drawing it to the
3816 right of that line. */
3817 if (s->face->box != FACE_NO_BOX
3818 && s->first_glyph->left_box_line_p)
3819 x = s->face->box_line_width;
3820 else
3821 x = 0;
3822
3823 /* If there is a margin around the image, adjust x- and y-position
3824 by that margin. */
3825 if (s->img->margin)
3826 {
3827 x += s->img->margin;
3828 y += s->img->margin;
3829 }
3830
3831 if (s->img->pixmap)
3832 {
3833 if (s->img->mask)
3834 {
3835 /* We can't set both a clip mask and use XSetClipRectangles
3836 because the latter also sets a clip mask. We also can't
3837 trust on the shape extension to be available
3838 (XShapeCombineRegion). So, compute the rectangle to draw
3839 manually. */
3840 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3841 | GCFunction);
3842 XGCValues xgcv;
3843
3844 xgcv.clip_mask = s->img->mask;
3845 xgcv.clip_x_origin = x;
3846 xgcv.clip_y_origin = y;
3847 xgcv.function = GXcopy;
3848 XChangeGC (s->display, s->gc, mask, &xgcv);
3849
3850 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3851 0, 0, s->img->width, s->img->height, x, y);
3852 XSetClipMask (s->display, s->gc, None);
3853 }
3854 else
3855 {
3856 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3857 0, 0, s->img->width, s->img->height, x, y);
3858
3859 /* When the image has a mask, we can expect that at
3860 least part of a mouse highlight or a block cursor will
3861 be visible. If the image doesn't have a mask, make
3862 a block cursor visible by drawing a rectangle around
3863 the image. I believe it's looking better if we do
3864 nothing here for mouse-face. */
3865 if (s->hl == DRAW_CURSOR)
3866 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3867 s->img->width - 1, s->img->height - 1);
3868 }
3869 }
3870 else
3871 /* Draw a rectangle if image could not be loaded. */
3872 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3873 s->img->width - 1, s->img->height - 1);
3874 }
3875
3876
3877 /* Draw part of the background of glyph string S. X, Y, W, and H
3878 give the rectangle to draw. */
3879
3880 static void
3881 x_draw_glyph_string_bg_rect (s, x, y, w, h)
3882 struct glyph_string *s;
3883 int x, y, w, h;
3884 {
3885 if (s->stippled_p)
3886 {
3887 /* Fill background with a stipple pattern. */
3888 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3889 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3890 XSetFillStyle (s->display, s->gc, FillSolid);
3891 }
3892 else
3893 x_clear_glyph_string_rect (s, x, y, w, h);
3894 }
3895
3896
3897 /* Draw image glyph string S.
3898
3899 s->y
3900 s->x +-------------------------
3901 | s->face->box
3902 |
3903 | +-------------------------
3904 | | s->img->margin
3905 | |
3906 | | +-------------------
3907 | | | the image
3908
3909 */
3910
3911 static void
3912 x_draw_image_glyph_string (s)
3913 struct glyph_string *s;
3914 {
3915 int x, y;
3916 int box_line_width = s->face->box_line_width;
3917 int margin = s->img->margin;
3918 int height;
3919 Pixmap pixmap = None;
3920
3921 height = s->height - 2 * box_line_width;
3922
3923 /* Fill background with face under the image. Do it only if row is
3924 taller than image or if image has a clip mask to reduce
3925 flickering. */
3926 s->stippled_p = s->face->stipple != 0;
3927 if (height > s->img->height
3928 || margin
3929 || s->img->mask
3930 || s->img->pixmap == 0
3931 || s->width != s->background_width)
3932 {
3933 if (box_line_width && s->first_glyph->left_box_line_p)
3934 x = s->x + box_line_width;
3935 else
3936 x = s->x;
3937
3938 y = s->y + box_line_width;
3939
3940 if (s->img->mask)
3941 {
3942 /* Create a pixmap as large as the glyph string Fill it with
3943 the background color. Copy the image to it, using its
3944 mask. Copy the temporary pixmap to the display. */
3945 Screen *screen = FRAME_X_SCREEN (s->f);
3946 int depth = DefaultDepthOfScreen (screen);
3947
3948 /* Create a pixmap as large as the glyph string. */
3949 pixmap = XCreatePixmap (s->display, s->window,
3950 s->background_width,
3951 s->height, depth);
3952
3953 /* Don't clip in the following because we're working on the
3954 pixmap. */
3955 XSetClipMask (s->display, s->gc, None);
3956
3957 /* Fill the pixmap with the background color/stipple. */
3958 if (s->stippled_p)
3959 {
3960 /* Fill background with a stipple pattern. */
3961 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3962 XFillRectangle (s->display, pixmap, s->gc,
3963 0, 0, s->background_width, s->height);
3964 XSetFillStyle (s->display, s->gc, FillSolid);
3965 }
3966 else
3967 {
3968 XGCValues xgcv;
3969 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3970 &xgcv);
3971 XSetForeground (s->display, s->gc, xgcv.background);
3972 XFillRectangle (s->display, pixmap, s->gc,
3973 0, 0, s->background_width, s->height);
3974 XSetForeground (s->display, s->gc, xgcv.foreground);
3975 }
3976 }
3977 else
3978 /* Implementation idea: Is it possible to construct a mask?
3979 We could look at the color at the margins of the image, and
3980 say that this color is probably the background color of the
3981 image. */
3982 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
3983
3984 s->background_filled_p = 1;
3985 }
3986
3987 /* Draw the foreground. */
3988 if (pixmap != None)
3989 {
3990 x_draw_image_foreground_1 (s, pixmap);
3991 x_set_glyph_string_clipping (s);
3992 XCopyArea (s->display, pixmap, s->window, s->gc,
3993 0, 0, s->background_width, s->height, s->x, s->y);
3994 XFreePixmap (s->display, pixmap);
3995 }
3996 else
3997 x_draw_image_foreground (s);
3998
3999 /* If we must draw a relief around the image, do it. */
4000 if (s->img->relief
4001 || s->hl == DRAW_IMAGE_RAISED
4002 || s->hl == DRAW_IMAGE_SUNKEN)
4003 x_draw_image_relief (s);
4004 }
4005
4006
4007 /* Draw stretch glyph string S. */
4008
4009 static void
4010 x_draw_stretch_glyph_string (s)
4011 struct glyph_string *s;
4012 {
4013 xassert (s->first_glyph->type == STRETCH_GLYPH);
4014 s->stippled_p = s->face->stipple != 0;
4015
4016 if (s->hl == DRAW_CURSOR
4017 && !x_stretch_cursor_p)
4018 {
4019 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4020 as wide as the stretch glyph. */
4021 int width = min (CANON_X_UNIT (s->f), s->background_width);
4022
4023 /* Draw cursor. */
4024 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4025
4026 /* Clear rest using the GC of the original non-cursor face. */
4027 if (width < s->background_width)
4028 {
4029 GC gc = s->face->gc;
4030 int x = s->x + width, y = s->y;
4031 int w = s->background_width - width, h = s->height;
4032 XRectangle r;
4033
4034 x_get_glyph_string_clip_rect (s, &r);
4035 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4036
4037 if (s->face->stipple)
4038 {
4039 /* Fill background with a stipple pattern. */
4040 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4041 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4042 XSetFillStyle (s->display, gc, FillSolid);
4043 }
4044 else
4045 {
4046 XGCValues xgcv;
4047 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4048 XSetForeground (s->display, gc, xgcv.background);
4049 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4050 XSetForeground (s->display, gc, xgcv.foreground);
4051 }
4052 }
4053 }
4054 else
4055 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4056 s->height);
4057
4058 s->background_filled_p = 1;
4059 }
4060
4061
4062 /* Draw glyph string S. */
4063
4064 static void
4065 x_draw_glyph_string (s)
4066 struct glyph_string *s;
4067 {
4068 /* If S draws into the background of its successor, draw the
4069 background of the successor first so that S can draw into it.
4070 This makes S->next use XDrawString instead of XDrawImageString. */
4071 if (s->next && s->right_overhang && !s->for_overlaps_p)
4072 {
4073 xassert (s->next->img == NULL);
4074 x_set_glyph_string_gc (s->next);
4075 x_set_glyph_string_clipping (s->next);
4076 x_draw_glyph_string_background (s->next, 1);
4077 }
4078
4079 /* Set up S->gc, set clipping and draw S. */
4080 x_set_glyph_string_gc (s);
4081 x_set_glyph_string_clipping (s);
4082
4083 switch (s->first_glyph->type)
4084 {
4085 case IMAGE_GLYPH:
4086 x_draw_image_glyph_string (s);
4087 break;
4088
4089 case STRETCH_GLYPH:
4090 x_draw_stretch_glyph_string (s);
4091 break;
4092
4093 case CHAR_GLYPH:
4094 if (s->for_overlaps_p)
4095 s->background_filled_p = 1;
4096 else
4097 x_draw_glyph_string_background (s, 0);
4098 x_draw_glyph_string_foreground (s);
4099 break;
4100
4101 case COMPOSITE_GLYPH:
4102 if (s->for_overlaps_p || s->gidx > 0)
4103 s->background_filled_p = 1;
4104 else
4105 x_draw_glyph_string_background (s, 1);
4106 x_draw_composite_glyph_string_foreground (s);
4107 break;
4108
4109 default:
4110 abort ();
4111 }
4112
4113 if (!s->for_overlaps_p)
4114 {
4115 /* Draw underline. */
4116 if (s->face->underline_p)
4117 {
4118 unsigned long dy, h;
4119
4120 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4121 h = 1;
4122 if (!XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &dy))
4123 dy = s->height - h;
4124
4125 if (s->face->underline_defaulted_p)
4126 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4127 s->width, h);
4128 else
4129 {
4130 XGCValues xgcv;
4131 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4132 XSetForeground (s->display, s->gc, s->face->underline_color);
4133 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4134 s->width, h);
4135 XSetForeground (s->display, s->gc, xgcv.foreground);
4136 }
4137 }
4138
4139 /* Draw overline. */
4140 if (s->face->overline_p)
4141 {
4142 unsigned long dy = 0, h = 1;
4143
4144 if (s->face->overline_color_defaulted_p)
4145 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4146 s->width, h);
4147 else
4148 {
4149 XGCValues xgcv;
4150 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4151 XSetForeground (s->display, s->gc, s->face->overline_color);
4152 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4153 s->width, h);
4154 XSetForeground (s->display, s->gc, xgcv.foreground);
4155 }
4156 }
4157
4158 /* Draw strike-through. */
4159 if (s->face->strike_through_p)
4160 {
4161 unsigned long h = 1;
4162 unsigned long dy = (s->height - h) / 2;
4163
4164 if (s->face->strike_through_color_defaulted_p)
4165 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4166 s->width, h);
4167 else
4168 {
4169 XGCValues xgcv;
4170 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4171 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4172 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4173 s->width, h);
4174 XSetForeground (s->display, s->gc, xgcv.foreground);
4175 }
4176 }
4177
4178 /* Draw relief. */
4179 if (s->face->box != FACE_NO_BOX)
4180 x_draw_glyph_string_box (s);
4181 }
4182
4183 /* Reset clipping. */
4184 XSetClipMask (s->display, s->gc, None);
4185 }
4186
4187
4188 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4189 struct face **, int));
4190
4191
4192 /* Fill glyph string S with composition components specified by S->cmp.
4193
4194 FACES is an array of faces for all components of this composition.
4195 S->gidx is the index of the first component for S.
4196 OVERLAPS_P non-zero means S should draw the foreground only, and
4197 use its physical height for clipping.
4198
4199 Value is the index of a component not in S. */
4200
4201 static int
4202 x_fill_composite_glyph_string (s, faces, overlaps_p)
4203 struct glyph_string *s;
4204 struct face **faces;
4205 int overlaps_p;
4206 {
4207 int i;
4208
4209 xassert (s);
4210
4211 s->for_overlaps_p = overlaps_p;
4212
4213 s->face = faces[s->gidx];
4214 s->font = s->face->font;
4215 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4216
4217 /* For all glyphs of this composition, starting at the offset
4218 S->gidx, until we reach the end of the definition or encounter a
4219 glyph that requires the different face, add it to S. */
4220 ++s->nchars;
4221 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4222 ++s->nchars;
4223
4224 /* All glyph strings for the same composition has the same width,
4225 i.e. the width set for the first component of the composition. */
4226
4227 s->width = s->first_glyph->pixel_width;
4228
4229 /* If the specified font could not be loaded, use the frame's
4230 default font, but record the fact that we couldn't load it in
4231 the glyph string so that we can draw rectangles for the
4232 characters of the glyph string. */
4233 if (s->font == NULL)
4234 {
4235 s->font_not_found_p = 1;
4236 s->font = FRAME_FONT (s->f);
4237 }
4238
4239 /* Adjust base line for subscript/superscript text. */
4240 s->ybase += s->first_glyph->voffset;
4241
4242 xassert (s->face && s->face->gc);
4243
4244 /* This glyph string must always be drawn with 16-bit functions. */
4245 s->two_byte_p = 1;
4246
4247 return s->gidx + s->nchars;
4248 }
4249
4250
4251 /* Fill glyph string S from a sequence of character glyphs.
4252
4253 FACE_ID is the face id of the string. START is the index of the
4254 first glyph to consider, END is the index of the last + 1.
4255 OVERLAPS_P non-zero means S should draw the foreground only, and
4256 use its physical height for clipping.
4257
4258 Value is the index of the first glyph not in S. */
4259
4260 static int
4261 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4262 struct glyph_string *s;
4263 int face_id;
4264 int start, end, overlaps_p;
4265 {
4266 struct glyph *glyph, *last;
4267 int voffset;
4268 int glyph_not_available_p;
4269
4270 xassert (s->f == XFRAME (s->w->frame));
4271 xassert (s->nchars == 0);
4272 xassert (start >= 0 && end > start);
4273
4274 s->for_overlaps_p = overlaps_p,
4275 glyph = s->row->glyphs[s->area] + start;
4276 last = s->row->glyphs[s->area] + end;
4277 voffset = glyph->voffset;
4278
4279 glyph_not_available_p = glyph->glyph_not_available_p;
4280
4281 while (glyph < last
4282 && glyph->type == CHAR_GLYPH
4283 && glyph->voffset == voffset
4284 /* Same face id implies same font, nowadays. */
4285 && glyph->face_id == face_id
4286 && glyph->glyph_not_available_p == glyph_not_available_p)
4287 {
4288 int two_byte_p;
4289
4290 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4291 s->char2b + s->nchars,
4292 &two_byte_p);
4293 s->two_byte_p = two_byte_p;
4294 ++s->nchars;
4295 xassert (s->nchars <= end - start);
4296 s->width += glyph->pixel_width;
4297 ++glyph;
4298 }
4299
4300 s->font = s->face->font;
4301 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4302
4303 /* If the specified font could not be loaded, use the frame's font,
4304 but record the fact that we couldn't load it in
4305 S->font_not_found_p so that we can draw rectangles for the
4306 characters of the glyph string. */
4307 if (s->font == NULL || glyph_not_available_p)
4308 {
4309 s->font_not_found_p = 1;
4310 s->font = FRAME_FONT (s->f);
4311 }
4312
4313 /* Adjust base line for subscript/superscript text. */
4314 s->ybase += voffset;
4315
4316 xassert (s->face && s->face->gc);
4317 return glyph - s->row->glyphs[s->area];
4318 }
4319
4320
4321 /* Fill glyph string S from image glyph S->first_glyph. */
4322
4323 static void
4324 x_fill_image_glyph_string (s)
4325 struct glyph_string *s;
4326 {
4327 xassert (s->first_glyph->type == IMAGE_GLYPH);
4328 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4329 xassert (s->img);
4330 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4331 s->font = s->face->font;
4332 s->width = s->first_glyph->pixel_width;
4333
4334 /* Adjust base line for subscript/superscript text. */
4335 s->ybase += s->first_glyph->voffset;
4336 }
4337
4338
4339 /* Fill glyph string S from a sequence of stretch glyphs.
4340
4341 ROW is the glyph row in which the glyphs are found, AREA is the
4342 area within the row. START is the index of the first glyph to
4343 consider, END is the index of the last + 1.
4344
4345 Value is the index of the first glyph not in S. */
4346
4347 static int
4348 x_fill_stretch_glyph_string (s, row, area, start, end)
4349 struct glyph_string *s;
4350 struct glyph_row *row;
4351 enum glyph_row_area area;
4352 int start, end;
4353 {
4354 struct glyph *glyph, *last;
4355 int voffset, face_id;
4356
4357 xassert (s->first_glyph->type == STRETCH_GLYPH);
4358
4359 glyph = s->row->glyphs[s->area] + start;
4360 last = s->row->glyphs[s->area] + end;
4361 face_id = glyph->face_id;
4362 s->face = FACE_FROM_ID (s->f, face_id);
4363 s->font = s->face->font;
4364 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4365 s->width = glyph->pixel_width;
4366 voffset = glyph->voffset;
4367
4368 for (++glyph;
4369 (glyph < last
4370 && glyph->type == STRETCH_GLYPH
4371 && glyph->voffset == voffset
4372 && glyph->face_id == face_id);
4373 ++glyph)
4374 s->width += glyph->pixel_width;
4375
4376 /* Adjust base line for subscript/superscript text. */
4377 s->ybase += voffset;
4378
4379 xassert (s->face && s->face->gc);
4380 return glyph - s->row->glyphs[s->area];
4381 }
4382
4383
4384 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4385 of XChar2b structures for S; it can't be allocated in
4386 x_init_glyph_string because it must be allocated via `alloca'. W
4387 is the window on which S is drawn. ROW and AREA are the glyph row
4388 and area within the row from which S is constructed. START is the
4389 index of the first glyph structure covered by S. HL is a
4390 face-override for drawing S. */
4391
4392 static void
4393 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4394 struct glyph_string *s;
4395 XChar2b *char2b;
4396 struct window *w;
4397 struct glyph_row *row;
4398 enum glyph_row_area area;
4399 int start;
4400 enum draw_glyphs_face hl;
4401 {
4402 bzero (s, sizeof *s);
4403 s->w = w;
4404 s->f = XFRAME (w->frame);
4405 s->display = FRAME_X_DISPLAY (s->f);
4406 s->window = FRAME_X_WINDOW (s->f);
4407 s->char2b = char2b;
4408 s->hl = hl;
4409 s->row = row;
4410 s->area = area;
4411 s->first_glyph = row->glyphs[area] + start;
4412 s->height = row->height;
4413 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4414
4415 /* Display the internal border below the tool-bar window. */
4416 if (s->w == XWINDOW (s->f->tool_bar_window))
4417 s->y -= s->f->output_data.x->internal_border_width;
4418
4419 s->ybase = s->y + row->ascent;
4420 }
4421
4422
4423 /* Set background width of glyph string S. START is the index of the
4424 first glyph following S. LAST_X is the right-most x-position + 1
4425 in the drawing area. */
4426
4427 static INLINE void
4428 x_set_glyph_string_background_width (s, start, last_x)
4429 struct glyph_string *s;
4430 int start;
4431 int last_x;
4432 {
4433 /* If the face of this glyph string has to be drawn to the end of
4434 the drawing area, set S->extends_to_end_of_line_p. */
4435 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4436
4437 if (start == s->row->used[s->area]
4438 && s->hl == DRAW_NORMAL_TEXT
4439 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4440 || s->face->background != default_face->background
4441 || s->face->stipple != default_face->stipple))
4442 s->extends_to_end_of_line_p = 1;
4443
4444 /* If S extends its face to the end of the line, set its
4445 background_width to the distance to the right edge of the drawing
4446 area. */
4447 if (s->extends_to_end_of_line_p)
4448 s->background_width = last_x - s->x + 1;
4449 else
4450 s->background_width = s->width;
4451 }
4452
4453
4454 /* Add a glyph string for a stretch glyph to the list of strings
4455 between HEAD and TAIL. START is the index of the stretch glyph in
4456 row area AREA of glyph row ROW. END is the index of the last glyph
4457 in that glyph row area. X is the current output position assigned
4458 to the new glyph string constructed. HL overrides that face of the
4459 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4460 is the right-most x-position of the drawing area. */
4461
4462 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4463 and below -- keep them on one line. */
4464 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4465 do \
4466 { \
4467 s = (struct glyph_string *) alloca (sizeof *s); \
4468 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4469 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4470 x_append_glyph_string (&HEAD, &TAIL, s); \
4471 s->x = (X); \
4472 } \
4473 while (0)
4474
4475
4476 /* Add a glyph string for an image glyph to the list of strings
4477 between HEAD and TAIL. START is the index of the image glyph in
4478 row area AREA of glyph row ROW. END is the index of the last glyph
4479 in that glyph row area. X is the current output position assigned
4480 to the new glyph string constructed. HL overrides that face of the
4481 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4482 is the right-most x-position of the drawing area. */
4483
4484 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4485 do \
4486 { \
4487 s = (struct glyph_string *) alloca (sizeof *s); \
4488 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4489 x_fill_image_glyph_string (s); \
4490 x_append_glyph_string (&HEAD, &TAIL, s); \
4491 ++START; \
4492 s->x = (X); \
4493 } \
4494 while (0)
4495
4496
4497 /* Add a glyph string for a sequence of character glyphs to the list
4498 of strings between HEAD and TAIL. START is the index of the first
4499 glyph in row area AREA of glyph row ROW that is part of the new
4500 glyph string. END is the index of the last glyph in that glyph row
4501 area. X is the current output position assigned to the new glyph
4502 string constructed. HL overrides that face of the glyph; e.g. it
4503 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4504 right-most x-position of the drawing area. */
4505
4506 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4507 do \
4508 { \
4509 int c, face_id; \
4510 XChar2b *char2b; \
4511 \
4512 c = (ROW)->glyphs[AREA][START].u.ch; \
4513 face_id = (ROW)->glyphs[AREA][START].face_id; \
4514 \
4515 s = (struct glyph_string *) alloca (sizeof *s); \
4516 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4517 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4518 x_append_glyph_string (&HEAD, &TAIL, s); \
4519 s->x = (X); \
4520 START = x_fill_glyph_string (s, face_id, START, END, \
4521 OVERLAPS_P); \
4522 } \
4523 while (0)
4524
4525
4526 /* Add a glyph string for a composite sequence to the list of strings
4527 between HEAD and TAIL. START is the index of the first glyph in
4528 row area AREA of glyph row ROW that is part of the new glyph
4529 string. END is the index of the last glyph in that glyph row area.
4530 X is the current output position assigned to the new glyph string
4531 constructed. HL overrides that face of the glyph; e.g. it is
4532 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4533 x-position of the drawing area. */
4534
4535 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4536 do { \
4537 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4538 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4539 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4540 struct composition *cmp = composition_table[cmp_id]; \
4541 int glyph_len = cmp->glyph_len; \
4542 XChar2b *char2b; \
4543 struct face **faces; \
4544 struct glyph_string *first_s = NULL; \
4545 int n; \
4546 \
4547 base_face = base_face->ascii_face; \
4548 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4549 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4550 /* At first, fill in `char2b' and `faces'. */ \
4551 for (n = 0; n < glyph_len; n++) \
4552 { \
4553 int c = COMPOSITION_GLYPH (cmp, n); \
4554 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4555 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4556 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4557 this_face_id, char2b + n, 1); \
4558 } \
4559 \
4560 /* Make glyph_strings for each glyph sequence that is drawable by \
4561 the same face, and append them to HEAD/TAIL. */ \
4562 for (n = 0; n < cmp->glyph_len;) \
4563 { \
4564 s = (struct glyph_string *) alloca (sizeof *s); \
4565 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4566 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4567 s->cmp = cmp; \
4568 s->gidx = n; \
4569 s->x = (X); \
4570 \
4571 if (n == 0) \
4572 first_s = s; \
4573 \
4574 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4575 } \
4576 \
4577 ++START; \
4578 s = first_s; \
4579 } while (0)
4580
4581
4582 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4583 of AREA of glyph row ROW on window W between indices START and END.
4584 HL overrides the face for drawing glyph strings, e.g. it is
4585 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4586 x-positions of the drawing area.
4587
4588 This is an ugly monster macro construct because we must use alloca
4589 to allocate glyph strings (because x_draw_glyphs can be called
4590 asynchronously). */
4591
4592 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4593 do \
4594 { \
4595 HEAD = TAIL = NULL; \
4596 while (START < END) \
4597 { \
4598 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4599 switch (first_glyph->type) \
4600 { \
4601 case CHAR_GLYPH: \
4602 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
4603 TAIL, HL, X, LAST_X, \
4604 OVERLAPS_P); \
4605 break; \
4606 \
4607 case COMPOSITE_GLYPH: \
4608 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4609 HEAD, TAIL, HL, X, LAST_X,\
4610 OVERLAPS_P); \
4611 break; \
4612 \
4613 case STRETCH_GLYPH: \
4614 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4615 HEAD, TAIL, HL, X, LAST_X); \
4616 break; \
4617 \
4618 case IMAGE_GLYPH: \
4619 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4620 TAIL, HL, X, LAST_X); \
4621 break; \
4622 \
4623 default: \
4624 abort (); \
4625 } \
4626 \
4627 x_set_glyph_string_background_width (s, START, LAST_X); \
4628 (X) += s->width; \
4629 } \
4630 } \
4631 while (0)
4632
4633
4634 /* Draw glyphs between START and END in AREA of ROW on window W,
4635 starting at x-position X. X is relative to AREA in W. HL is a
4636 face-override with the following meaning:
4637
4638 DRAW_NORMAL_TEXT draw normally
4639 DRAW_CURSOR draw in cursor face
4640 DRAW_MOUSE_FACE draw in mouse face.
4641 DRAW_INVERSE_VIDEO draw in mode line face
4642 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4643 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4644
4645 If REAL_START is non-null, return in *REAL_START the real starting
4646 position for display. This can be different from START in case
4647 overlapping glyphs must be displayed. If REAL_END is non-null,
4648 return in *REAL_END the real end position for display. This can be
4649 different from END in case overlapping glyphs must be displayed.
4650
4651 If OVERLAPS_P is non-zero, draw only the foreground of characters
4652 and clip to the physical height of ROW.
4653
4654 Value is the x-position reached, relative to AREA of W. */
4655
4656 static int
4657 x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4658 overlaps_p)
4659 struct window *w;
4660 int x;
4661 struct glyph_row *row;
4662 enum glyph_row_area area;
4663 int start, end;
4664 enum draw_glyphs_face hl;
4665 int *real_start, *real_end;
4666 int overlaps_p;
4667 {
4668 struct glyph_string *head, *tail;
4669 struct glyph_string *s;
4670 int last_x, area_width;
4671 int x_reached;
4672 int i, j;
4673
4674 /* Let's rather be paranoid than getting a SEGV. */
4675 start = max (0, start);
4676 end = min (end, row->used[area]);
4677 if (real_start)
4678 *real_start = start;
4679 if (real_end)
4680 *real_end = end;
4681
4682 /* Translate X to frame coordinates. Set last_x to the right
4683 end of the drawing area. */
4684 if (row->full_width_p)
4685 {
4686 /* X is relative to the left edge of W, without scroll bars
4687 or flag areas. */
4688 struct frame *f = XFRAME (w->frame);
4689 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
4690 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
4691
4692 x += window_left_x;
4693 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4694 last_x = window_left_x + area_width;
4695
4696 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4697 {
4698 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4699 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4700 last_x += width;
4701 else
4702 x -= width;
4703 }
4704
4705 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4706 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
4707 }
4708 else
4709 {
4710 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4711 area_width = window_box_width (w, area);
4712 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
4713 }
4714
4715 /* Build a doubly-linked list of glyph_string structures between
4716 head and tail from what we have to draw. Note that the macro
4717 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4718 the reason we use a separate variable `i'. */
4719 i = start;
4720 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4721 overlaps_p);
4722 if (tail)
4723 x_reached = tail->x + tail->background_width;
4724 else
4725 x_reached = x;
4726
4727 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4728 the row, redraw some glyphs in front or following the glyph
4729 strings built above. */
4730 if (!overlaps_p && row->contains_overlapping_glyphs_p)
4731 {
4732 int dummy_x = 0;
4733 struct glyph_string *h, *t;
4734
4735 /* Compute overhangs for all glyph strings. */
4736 for (s = head; s; s = s->next)
4737 x_compute_glyph_string_overhangs (s);
4738
4739 /* Prepend glyph strings for glyphs in front of the first glyph
4740 string that are overwritten because of the first glyph
4741 string's left overhang. The background of all strings
4742 prepended must be drawn because the first glyph string
4743 draws over it. */
4744 i = x_left_overwritten (head);
4745 if (i >= 0)
4746 {
4747 j = i;
4748 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
4749 DRAW_NORMAL_TEXT, dummy_x, last_x,
4750 overlaps_p);
4751 start = i;
4752 if (real_start)
4753 *real_start = start;
4754 x_compute_overhangs_and_x (t, head->x, 1);
4755 x_prepend_glyph_string_lists (&head, &tail, h, t);
4756 }
4757
4758 /* Prepend glyph strings for glyphs in front of the first glyph
4759 string that overwrite that glyph string because of their
4760 right overhang. For these strings, only the foreground must
4761 be drawn, because it draws over the glyph string at `head'.
4762 The background must not be drawn because this would overwrite
4763 right overhangs of preceding glyphs for which no glyph
4764 strings exist. */
4765 i = x_left_overwriting (head);
4766 if (i >= 0)
4767 {
4768 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
4769 DRAW_NORMAL_TEXT, dummy_x, last_x,
4770 overlaps_p);
4771 for (s = h; s; s = s->next)
4772 s->background_filled_p = 1;
4773 if (real_start)
4774 *real_start = i;
4775 x_compute_overhangs_and_x (t, head->x, 1);
4776 x_prepend_glyph_string_lists (&head, &tail, h, t);
4777 }
4778
4779 /* Append glyphs strings for glyphs following the last glyph
4780 string tail that are overwritten by tail. The background of
4781 these strings has to be drawn because tail's foreground draws
4782 over it. */
4783 i = x_right_overwritten (tail);
4784 if (i >= 0)
4785 {
4786 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4787 DRAW_NORMAL_TEXT, x, last_x,
4788 overlaps_p);
4789 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4790 x_append_glyph_string_lists (&head, &tail, h, t);
4791 if (real_end)
4792 *real_end = i;
4793 }
4794
4795 /* Append glyph strings for glyphs following the last glyph
4796 string tail that overwrite tail. The foreground of such
4797 glyphs has to be drawn because it writes into the background
4798 of tail. The background must not be drawn because it could
4799 paint over the foreground of following glyphs. */
4800 i = x_right_overwriting (tail);
4801 if (i >= 0)
4802 {
4803 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4804 DRAW_NORMAL_TEXT, x, last_x,
4805 overlaps_p);
4806 for (s = h; s; s = s->next)
4807 s->background_filled_p = 1;
4808 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4809 x_append_glyph_string_lists (&head, &tail, h, t);
4810 if (real_end)
4811 *real_end = i;
4812 }
4813 }
4814
4815 /* Draw all strings. */
4816 for (s = head; s; s = s->next)
4817 x_draw_glyph_string (s);
4818
4819 /* Value is the x-position up to which drawn, relative to AREA of W.
4820 This doesn't include parts drawn because of overhangs. */
4821 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4822 if (!row->full_width_p)
4823 {
4824 if (area > LEFT_MARGIN_AREA)
4825 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4826 if (area > TEXT_AREA)
4827 x_reached -= window_box_width (w, TEXT_AREA);
4828 }
4829 return x_reached;
4830 }
4831
4832
4833 /* Fix the display of area AREA of overlapping row ROW in window W. */
4834
4835 static void
4836 x_fix_overlapping_area (w, row, area)
4837 struct window *w;
4838 struct glyph_row *row;
4839 enum glyph_row_area area;
4840 {
4841 int i, x;
4842
4843 BLOCK_INPUT;
4844
4845 if (area == LEFT_MARGIN_AREA)
4846 x = 0;
4847 else if (area == TEXT_AREA)
4848 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
4849 else
4850 x = (window_box_width (w, LEFT_MARGIN_AREA)
4851 + window_box_width (w, TEXT_AREA));
4852
4853 for (i = 0; i < row->used[area];)
4854 {
4855 if (row->glyphs[area][i].overlaps_vertically_p)
4856 {
4857 int start = i, start_x = x;
4858
4859 do
4860 {
4861 x += row->glyphs[area][i].pixel_width;
4862 ++i;
4863 }
4864 while (i < row->used[area]
4865 && row->glyphs[area][i].overlaps_vertically_p);
4866
4867 x_draw_glyphs (w, start_x, row, area, start, i,
4868 (row->inverse_p
4869 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4870 NULL, NULL, 1);
4871 }
4872 else
4873 {
4874 x += row->glyphs[area][i].pixel_width;
4875 ++i;
4876 }
4877 }
4878
4879 UNBLOCK_INPUT;
4880 }
4881
4882
4883 /* Output LEN glyphs starting at START at the nominal cursor position.
4884 Advance the nominal cursor over the text. The global variable
4885 updated_window contains the window being updated, updated_row is
4886 the glyph row being updated, and updated_area is the area of that
4887 row being updated. */
4888
4889 static void
4890 x_write_glyphs (start, len)
4891 struct glyph *start;
4892 int len;
4893 {
4894 int x, hpos, real_start, real_end;
4895
4896 xassert (updated_window && updated_row);
4897 BLOCK_INPUT;
4898
4899 /* Write glyphs. */
4900
4901 hpos = start - updated_row->glyphs[updated_area];
4902 x = x_draw_glyphs (updated_window, output_cursor.x,
4903 updated_row, updated_area,
4904 hpos, hpos + len,
4905 (updated_row->inverse_p
4906 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4907 &real_start, &real_end, 0);
4908
4909 /* If we drew over the cursor, note that it is not visible any more. */
4910 note_overwritten_text_cursor (updated_window, real_start,
4911 real_end - real_start);
4912
4913 UNBLOCK_INPUT;
4914
4915 /* Advance the output cursor. */
4916 output_cursor.hpos += len;
4917 output_cursor.x = x;
4918 }
4919
4920
4921 /* Insert LEN glyphs from START at the nominal cursor position. */
4922
4923 static void
4924 x_insert_glyphs (start, len)
4925 struct glyph *start;
4926 register int len;
4927 {
4928 struct frame *f;
4929 struct window *w;
4930 int line_height, shift_by_width, shifted_region_width;
4931 struct glyph_row *row;
4932 struct glyph *glyph;
4933 int frame_x, frame_y, hpos, real_start, real_end;
4934
4935 xassert (updated_window && updated_row);
4936 BLOCK_INPUT;
4937 w = updated_window;
4938 f = XFRAME (WINDOW_FRAME (w));
4939
4940 /* Get the height of the line we are in. */
4941 row = updated_row;
4942 line_height = row->height;
4943
4944 /* Get the width of the glyphs to insert. */
4945 shift_by_width = 0;
4946 for (glyph = start; glyph < start + len; ++glyph)
4947 shift_by_width += glyph->pixel_width;
4948
4949 /* Get the width of the region to shift right. */
4950 shifted_region_width = (window_box_width (w, updated_area)
4951 - output_cursor.x
4952 - shift_by_width);
4953
4954 /* Shift right. */
4955 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
4956 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
4957 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4958 f->output_data.x->normal_gc,
4959 frame_x, frame_y,
4960 shifted_region_width, line_height,
4961 frame_x + shift_by_width, frame_y);
4962
4963 /* Write the glyphs. */
4964 hpos = start - row->glyphs[updated_area];
4965 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
4966 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
4967 note_overwritten_text_cursor (w, real_start, real_end - real_start);
4968
4969 /* Advance the output cursor. */
4970 output_cursor.hpos += len;
4971 output_cursor.x += shift_by_width;
4972 UNBLOCK_INPUT;
4973 }
4974
4975
4976 /* Delete N glyphs at the nominal cursor position. Not implemented
4977 for X frames. */
4978
4979 static void
4980 x_delete_glyphs (n)
4981 register int n;
4982 {
4983 abort ();
4984 }
4985
4986
4987 /* Erase the current text line from the nominal cursor position
4988 (inclusive) to pixel column TO_X (exclusive). The idea is that
4989 everything from TO_X onward is already erased.
4990
4991 TO_X is a pixel position relative to updated_area of
4992 updated_window. TO_X == -1 means clear to the end of this area. */
4993
4994 static void
4995 x_clear_end_of_line (to_x)
4996 int to_x;
4997 {
4998 struct frame *f;
4999 struct window *w = updated_window;
5000 int max_x, min_y, max_y;
5001 int from_x, from_y, to_y;
5002
5003 xassert (updated_window && updated_row);
5004 f = XFRAME (w->frame);
5005
5006 if (updated_row->full_width_p)
5007 {
5008 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5009 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5010 && !w->pseudo_window_p)
5011 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5012 }
5013 else
5014 max_x = window_box_width (w, updated_area);
5015 max_y = window_text_bottom_y (w);
5016
5017 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5018 of window. For TO_X > 0, truncate to end of drawing area. */
5019 if (to_x == 0)
5020 return;
5021 else if (to_x < 0)
5022 to_x = max_x;
5023 else
5024 to_x = min (to_x, max_x);
5025
5026 to_y = min (max_y, output_cursor.y + updated_row->height);
5027
5028 /* Notice if the cursor will be cleared by this operation. */
5029 if (!updated_row->full_width_p)
5030 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
5031
5032 from_x = output_cursor.x;
5033
5034 /* Translate to frame coordinates. */
5035 if (updated_row->full_width_p)
5036 {
5037 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5038 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5039 }
5040 else
5041 {
5042 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5043 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5044 }
5045
5046 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5047 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5048 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5049
5050 /* Prevent inadvertently clearing to end of the X window. */
5051 if (to_x > from_x && to_y > from_y)
5052 {
5053 BLOCK_INPUT;
5054 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5055 from_x, from_y, to_x - from_x, to_y - from_y,
5056 False);
5057 UNBLOCK_INPUT;
5058 }
5059 }
5060
5061
5062 /* Clear entire frame. If updating_frame is non-null, clear that
5063 frame. Otherwise clear the selected frame. */
5064
5065 static void
5066 x_clear_frame ()
5067 {
5068 struct frame *f;
5069
5070 if (updating_frame)
5071 f = updating_frame;
5072 else
5073 f = SELECTED_FRAME ();
5074
5075 /* Clearing the frame will erase any cursor, so mark them all as no
5076 longer visible. */
5077 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5078 output_cursor.hpos = output_cursor.vpos = 0;
5079 output_cursor.x = -1;
5080
5081 /* We don't set the output cursor here because there will always
5082 follow an explicit cursor_to. */
5083 BLOCK_INPUT;
5084 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5085
5086 /* We have to clear the scroll bars, too. If we have changed
5087 colors or something like that, then they should be notified. */
5088 x_scroll_bar_clear (f);
5089
5090 XFlush (FRAME_X_DISPLAY (f));
5091 UNBLOCK_INPUT;
5092 }
5093
5094
5095 \f
5096 /* Invert the middle quarter of the frame for .15 sec. */
5097
5098 /* We use the select system call to do the waiting, so we have to make
5099 sure it's available. If it isn't, we just won't do visual bells. */
5100
5101 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5102
5103
5104 /* Subtract the `struct timeval' values X and Y, storing the result in
5105 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5106
5107 static int
5108 timeval_subtract (result, x, y)
5109 struct timeval *result, x, y;
5110 {
5111 /* Perform the carry for the later subtraction by updating y. This
5112 is safer because on some systems the tv_sec member is unsigned. */
5113 if (x.tv_usec < y.tv_usec)
5114 {
5115 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5116 y.tv_usec -= 1000000 * nsec;
5117 y.tv_sec += nsec;
5118 }
5119
5120 if (x.tv_usec - y.tv_usec > 1000000)
5121 {
5122 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5123 y.tv_usec += 1000000 * nsec;
5124 y.tv_sec -= nsec;
5125 }
5126
5127 /* Compute the time remaining to wait. tv_usec is certainly
5128 positive. */
5129 result->tv_sec = x.tv_sec - y.tv_sec;
5130 result->tv_usec = x.tv_usec - y.tv_usec;
5131
5132 /* Return indication of whether the result should be considered
5133 negative. */
5134 return x.tv_sec < y.tv_sec;
5135 }
5136
5137 void
5138 XTflash (f)
5139 struct frame *f;
5140 {
5141 BLOCK_INPUT;
5142
5143 {
5144 GC gc;
5145
5146 /* Create a GC that will use the GXxor function to flip foreground
5147 pixels into background pixels. */
5148 {
5149 XGCValues values;
5150
5151 values.function = GXxor;
5152 values.foreground = (f->output_data.x->foreground_pixel
5153 ^ f->output_data.x->background_pixel);
5154
5155 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5156 GCFunction | GCForeground, &values);
5157 }
5158
5159 {
5160 /* Get the height not including a menu bar widget. */
5161 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5162 /* Height of each line to flash. */
5163 int flash_height = FRAME_LINE_HEIGHT (f);
5164 /* These will be the left and right margins of the rectangles. */
5165 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5166 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5167
5168 int width;
5169
5170 /* Don't flash the area between a scroll bar and the frame
5171 edge it is next to. */
5172 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5173 {
5174 case vertical_scroll_bar_left:
5175 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5176 break;
5177
5178 case vertical_scroll_bar_right:
5179 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5180 break;
5181
5182 default:
5183 break;
5184 }
5185
5186 width = flash_right - flash_left;
5187
5188 /* If window is tall, flash top and bottom line. */
5189 if (height > 3 * FRAME_LINE_HEIGHT (f))
5190 {
5191 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5192 flash_left,
5193 (FRAME_INTERNAL_BORDER_WIDTH (f)
5194 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5195 width, flash_height);
5196 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5197 flash_left,
5198 (height - flash_height
5199 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5200 width, flash_height);
5201 }
5202 else
5203 /* If it is short, flash it all. */
5204 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5205 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5206 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5207
5208 x_flush (f);
5209
5210 {
5211 struct timeval wakeup;
5212
5213 EMACS_GET_TIME (wakeup);
5214
5215 /* Compute time to wait until, propagating carry from usecs. */
5216 wakeup.tv_usec += 150000;
5217 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5218 wakeup.tv_usec %= 1000000;
5219
5220 /* Keep waiting until past the time wakeup. */
5221 while (1)
5222 {
5223 struct timeval timeout;
5224
5225 EMACS_GET_TIME (timeout);
5226
5227 /* In effect, timeout = wakeup - timeout.
5228 Break if result would be negative. */
5229 if (timeval_subtract (&timeout, wakeup, timeout))
5230 break;
5231
5232 /* Try to wait that long--but we might wake up sooner. */
5233 select (0, NULL, NULL, NULL, &timeout);
5234 }
5235 }
5236
5237 /* If window is tall, flash top and bottom line. */
5238 if (height > 3 * FRAME_LINE_HEIGHT (f))
5239 {
5240 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5241 flash_left,
5242 (FRAME_INTERNAL_BORDER_WIDTH (f)
5243 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5244 width, flash_height);
5245 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5246 flash_left,
5247 (height - flash_height
5248 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5249 width, flash_height);
5250 }
5251 else
5252 /* If it is short, flash it all. */
5253 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5254 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5255 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5256
5257 XFreeGC (FRAME_X_DISPLAY (f), gc);
5258 x_flush (f);
5259 }
5260 }
5261
5262 UNBLOCK_INPUT;
5263 }
5264
5265 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5266
5267
5268 /* Make audible bell. */
5269
5270 void
5271 XTring_bell ()
5272 {
5273 struct frame *f = SELECTED_FRAME ();
5274
5275 if (FRAME_X_DISPLAY (f))
5276 {
5277 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5278 if (visible_bell)
5279 XTflash (f);
5280 else
5281 #endif
5282 {
5283 BLOCK_INPUT;
5284 XBell (FRAME_X_DISPLAY (f), 0);
5285 XFlush (FRAME_X_DISPLAY (f));
5286 UNBLOCK_INPUT;
5287 }
5288 }
5289 }
5290
5291 \f
5292 /* Specify how many text lines, from the top of the window,
5293 should be affected by insert-lines and delete-lines operations.
5294 This, and those operations, are used only within an update
5295 that is bounded by calls to x_update_begin and x_update_end. */
5296
5297 static void
5298 XTset_terminal_window (n)
5299 register int n;
5300 {
5301 /* This function intentionally left blank. */
5302 }
5303
5304
5305 \f
5306 /***********************************************************************
5307 Line Dance
5308 ***********************************************************************/
5309
5310 /* Perform an insert-lines or delete-lines operation, inserting N
5311 lines or deleting -N lines at vertical position VPOS. */
5312
5313 static void
5314 x_ins_del_lines (vpos, n)
5315 int vpos, n;
5316 {
5317 abort ();
5318 }
5319
5320
5321 /* Scroll part of the display as described by RUN. */
5322
5323 static void
5324 x_scroll_run (w, run)
5325 struct window *w;
5326 struct run *run;
5327 {
5328 struct frame *f = XFRAME (w->frame);
5329 int x, y, width, height, from_y, to_y, bottom_y;
5330
5331 /* Get frame-relative bounding box of the text display area of W,
5332 without mode lines. Include in this box the flags areas to the
5333 left and right of W. */
5334 window_box (w, -1, &x, &y, &width, &height);
5335 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5336 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
5337
5338 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5339 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5340 bottom_y = y + height;
5341
5342 if (to_y < from_y)
5343 {
5344 /* Scrolling up. Make sure we don't copy part of the mode
5345 line at the bottom. */
5346 if (from_y + run->height > bottom_y)
5347 height = bottom_y - from_y;
5348 else
5349 height = run->height;
5350 }
5351 else
5352 {
5353 /* Scolling down. Make sure we don't copy over the mode line.
5354 at the bottom. */
5355 if (to_y + run->height > bottom_y)
5356 height = bottom_y - to_y;
5357 else
5358 height = run->height;
5359 }
5360
5361 BLOCK_INPUT;
5362
5363 /* Cursor off. Will be switched on again in x_update_window_end. */
5364 updated_window = w;
5365 x_clear_cursor (w);
5366
5367 XCopyArea (FRAME_X_DISPLAY (f),
5368 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5369 f->output_data.x->normal_gc,
5370 x, from_y,
5371 width, height,
5372 x, to_y);
5373
5374 UNBLOCK_INPUT;
5375 }
5376
5377
5378 \f
5379 /***********************************************************************
5380 Exposure Events
5381 ***********************************************************************/
5382
5383 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5384 corner of the exposed rectangle. W and H are width and height of
5385 the exposed area. All are pixel values. W or H zero means redraw
5386 the entire frame. */
5387
5388 static void
5389 expose_frame (f, x, y, w, h)
5390 struct frame *f;
5391 int x, y, w, h;
5392 {
5393 XRectangle r;
5394
5395 TRACE ((stderr, "expose_frame "));
5396
5397 /* No need to redraw if frame will be redrawn soon. */
5398 if (FRAME_GARBAGED_P (f))
5399 {
5400 TRACE ((stderr, " garbaged\n"));
5401 return;
5402 }
5403
5404 /* If basic faces haven't been realized yet, there is no point in
5405 trying to redraw anything. This can happen when we get an expose
5406 event while Emacs is starting, e.g. by moving another window. */
5407 if (FRAME_FACE_CACHE (f) == NULL
5408 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5409 {
5410 TRACE ((stderr, " no faces\n"));
5411 return;
5412 }
5413
5414 if (w == 0 || h == 0)
5415 {
5416 r.x = r.y = 0;
5417 r.width = CANON_X_UNIT (f) * f->width;
5418 r.height = CANON_Y_UNIT (f) * f->height;
5419 }
5420 else
5421 {
5422 r.x = x;
5423 r.y = y;
5424 r.width = w;
5425 r.height = h;
5426 }
5427
5428 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5429 expose_window_tree (XWINDOW (f->root_window), &r);
5430
5431 if (WINDOWP (f->tool_bar_window))
5432 {
5433 struct window *w = XWINDOW (f->tool_bar_window);
5434 XRectangle window_rect;
5435 XRectangle intersection_rect;
5436 int window_x, window_y, window_width, window_height;
5437
5438
5439 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5440 window_rect.x = window_x;
5441 window_rect.y = window_y;
5442 window_rect.width = window_width;
5443 window_rect.height = window_height;
5444
5445 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5446 expose_window (w, &intersection_rect);
5447 }
5448
5449 #ifndef USE_X_TOOLKIT
5450 if (WINDOWP (f->menu_bar_window))
5451 {
5452 struct window *w = XWINDOW (f->menu_bar_window);
5453 XRectangle window_rect;
5454 XRectangle intersection_rect;
5455 int window_x, window_y, window_width, window_height;
5456
5457
5458 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5459 window_rect.x = window_x;
5460 window_rect.y = window_y;
5461 window_rect.width = window_width;
5462 window_rect.height = window_height;
5463
5464 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5465 expose_window (w, &intersection_rect);
5466 }
5467 #endif /* not USE_X_TOOLKIT */
5468 }
5469
5470
5471 /* Redraw (parts) of all windows in the window tree rooted at W that
5472 intersect R. R contains frame pixel coordinates. */
5473
5474 static void
5475 expose_window_tree (w, r)
5476 struct window *w;
5477 XRectangle *r;
5478 {
5479 while (w)
5480 {
5481 if (!NILP (w->hchild))
5482 expose_window_tree (XWINDOW (w->hchild), r);
5483 else if (!NILP (w->vchild))
5484 expose_window_tree (XWINDOW (w->vchild), r);
5485 else
5486 {
5487 XRectangle window_rect;
5488 XRectangle intersection_rect;
5489 struct frame *f = XFRAME (w->frame);
5490 int window_x, window_y, window_width, window_height;
5491
5492 /* Frame-relative pixel rectangle of W. */
5493 window_box (w, -1, &window_x, &window_y, &window_width,
5494 &window_height);
5495 window_rect.x
5496 = (window_x
5497 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
5498 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5499 window_rect.y = window_y;
5500 window_rect.width
5501 = (window_width
5502 + FRAME_X_FLAGS_AREA_WIDTH (f)
5503 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5504 window_rect.height
5505 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5506
5507 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5508 expose_window (w, &intersection_rect);
5509 }
5510
5511 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5512 }
5513 }
5514
5515
5516 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5517 which intersects rectangle R. R is in window-relative coordinates. */
5518
5519 static void
5520 expose_area (w, row, r, area)
5521 struct window *w;
5522 struct glyph_row *row;
5523 XRectangle *r;
5524 enum glyph_row_area area;
5525 {
5526 int x;
5527 struct glyph *first = row->glyphs[area];
5528 struct glyph *end = row->glyphs[area] + row->used[area];
5529 struct glyph *last;
5530 int first_x;
5531
5532 /* Set x to the window-relative start position for drawing glyphs of
5533 AREA. The first glyph of the text area can be partially visible.
5534 The first glyphs of other areas cannot. */
5535 if (area == LEFT_MARGIN_AREA)
5536 x = 0;
5537 else if (area == TEXT_AREA)
5538 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5539 else
5540 x = (window_box_width (w, LEFT_MARGIN_AREA)
5541 + window_box_width (w, TEXT_AREA));
5542
5543 if (area == TEXT_AREA && row->fill_line_p)
5544 /* If row extends face to end of line write the whole line. */
5545 x_draw_glyphs (w, x, row, area,
5546 0, row->used[area],
5547 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5548 NULL, NULL, 0);
5549 else
5550 {
5551 /* Find the first glyph that must be redrawn. */
5552 while (first < end
5553 && x + first->pixel_width < r->x)
5554 {
5555 x += first->pixel_width;
5556 ++first;
5557 }
5558
5559 /* Find the last one. */
5560 last = first;
5561 first_x = x;
5562 while (last < end
5563 && x < r->x + r->width)
5564 {
5565 x += last->pixel_width;
5566 ++last;
5567 }
5568
5569 /* Repaint. */
5570 if (last > first)
5571 x_draw_glyphs (w, first_x, row, area,
5572 first - row->glyphs[area],
5573 last - row->glyphs[area],
5574 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5575 NULL, NULL, 0);
5576 }
5577 }
5578
5579
5580 /* Redraw the parts of the glyph row ROW on window W intersecting
5581 rectangle R. R is in window-relative coordinates. */
5582
5583 static void
5584 expose_line (w, row, r)
5585 struct window *w;
5586 struct glyph_row *row;
5587 XRectangle *r;
5588 {
5589 xassert (row->enabled_p);
5590
5591 if (row->mode_line_p || w->pseudo_window_p)
5592 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5593 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5594 NULL, NULL, 0);
5595 else
5596 {
5597 if (row->used[LEFT_MARGIN_AREA])
5598 expose_area (w, row, r, LEFT_MARGIN_AREA);
5599 if (row->used[TEXT_AREA])
5600 expose_area (w, row, r, TEXT_AREA);
5601 if (row->used[RIGHT_MARGIN_AREA])
5602 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5603 x_draw_row_bitmaps (w, row);
5604 }
5605 }
5606
5607
5608 /* Return non-zero if W's cursor intersects rectangle R. */
5609
5610 static int
5611 x_phys_cursor_in_rect_p (w, r)
5612 struct window *w;
5613 XRectangle *r;
5614 {
5615 XRectangle cr, result;
5616 struct glyph *cursor_glyph;
5617
5618 cursor_glyph = get_phys_cursor_glyph (w);
5619 if (cursor_glyph)
5620 {
5621 cr.x = w->phys_cursor.x;
5622 cr.y = w->phys_cursor.y;
5623 cr.width = cursor_glyph->pixel_width;
5624 cr.height = w->phys_cursor_height;
5625 return x_intersect_rectangles (&cr, r, &result);
5626 }
5627 else
5628 return 0;
5629 }
5630
5631
5632 /* Redraw a rectangle of window W. R is a rectangle in window
5633 relative coordinates. Call this function with input blocked. */
5634
5635 static void
5636 expose_window (w, r)
5637 struct window *w;
5638 XRectangle *r;
5639 {
5640 struct glyph_row *row;
5641 int y;
5642 int yb = window_text_bottom_y (w);
5643 int cursor_cleared_p;
5644
5645 /* If window is not yet fully initialized, do nothing. This can
5646 happen when toolkit scroll bars are used and a window is split.
5647 Reconfiguring the scroll bar will generate an expose for a newly
5648 created window. */
5649 if (w->current_matrix == NULL)
5650 return;
5651
5652 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5653 r->x, r->y, r->width, r->height));
5654
5655 /* Convert to window coordinates. */
5656 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5657 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
5658
5659 /* Turn off the cursor. */
5660 if (!w->pseudo_window_p
5661 && x_phys_cursor_in_rect_p (w, r))
5662 {
5663 x_clear_cursor (w);
5664 cursor_cleared_p = 1;
5665 }
5666 else
5667 cursor_cleared_p = 0;
5668
5669 /* Find the first row intersecting the rectangle R. */
5670 row = w->current_matrix->rows;
5671 y = 0;
5672 while (row->enabled_p
5673 && y < yb
5674 && y + row->height < r->y)
5675 {
5676 y += row->height;
5677 ++row;
5678 }
5679
5680 /* Display the text in the rectangle, one text line at a time. */
5681 while (row->enabled_p
5682 && y < yb
5683 && y < r->y + r->height)
5684 {
5685 expose_line (w, row, r);
5686 y += row->height;
5687 ++row;
5688 }
5689
5690 /* Display the mode line if there is one. */
5691 if (WINDOW_WANTS_MODELINE_P (w)
5692 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5693 row->enabled_p)
5694 && row->y < r->y + r->height)
5695 expose_line (w, row, r);
5696
5697 if (!w->pseudo_window_p)
5698 {
5699 /* Draw border between windows. */
5700 x_draw_vertical_border (w);
5701
5702 /* Turn the cursor on again. */
5703 if (cursor_cleared_p)
5704 x_update_window_cursor (w, 1);
5705 }
5706 }
5707
5708
5709 /* Determine the intersection of two rectangles R1 and R2. Return
5710 the intersection in *RESULT. Value is non-zero if RESULT is not
5711 empty. */
5712
5713 static int
5714 x_intersect_rectangles (r1, r2, result)
5715 XRectangle *r1, *r2, *result;
5716 {
5717 XRectangle *left, *right;
5718 XRectangle *upper, *lower;
5719 int intersection_p = 0;
5720
5721 /* Rearrange so that R1 is the left-most rectangle. */
5722 if (r1->x < r2->x)
5723 left = r1, right = r2;
5724 else
5725 left = r2, right = r1;
5726
5727 /* X0 of the intersection is right.x0, if this is inside R1,
5728 otherwise there is no intersection. */
5729 if (right->x <= left->x + left->width)
5730 {
5731 result->x = right->x;
5732
5733 /* The right end of the intersection is the minimum of the
5734 the right ends of left and right. */
5735 result->width = (min (left->x + left->width, right->x + right->width)
5736 - result->x);
5737
5738 /* Same game for Y. */
5739 if (r1->y < r2->y)
5740 upper = r1, lower = r2;
5741 else
5742 upper = r2, lower = r1;
5743
5744 /* The upper end of the intersection is lower.y0, if this is inside
5745 of upper. Otherwise, there is no intersection. */
5746 if (lower->y <= upper->y + upper->height)
5747 {
5748 result->y = lower->y;
5749
5750 /* The lower end of the intersection is the minimum of the lower
5751 ends of upper and lower. */
5752 result->height = (min (lower->y + lower->height,
5753 upper->y + upper->height)
5754 - result->y);
5755 intersection_p = 1;
5756 }
5757 }
5758
5759 return intersection_p;
5760 }
5761
5762
5763
5764
5765 \f
5766 static void
5767 frame_highlight (f)
5768 struct frame *f;
5769 {
5770 /* We used to only do this if Vx_no_window_manager was non-nil, but
5771 the ICCCM (section 4.1.6) says that the window's border pixmap
5772 and border pixel are window attributes which are "private to the
5773 client", so we can always change it to whatever we want. */
5774 BLOCK_INPUT;
5775 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5776 f->output_data.x->border_pixel);
5777 UNBLOCK_INPUT;
5778 x_update_cursor (f, 1);
5779 }
5780
5781 static void
5782 frame_unhighlight (f)
5783 struct frame *f;
5784 {
5785 /* We used to only do this if Vx_no_window_manager was non-nil, but
5786 the ICCCM (section 4.1.6) says that the window's border pixmap
5787 and border pixel are window attributes which are "private to the
5788 client", so we can always change it to whatever we want. */
5789 BLOCK_INPUT;
5790 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5791 f->output_data.x->border_tile);
5792 UNBLOCK_INPUT;
5793 x_update_cursor (f, 1);
5794 }
5795
5796 /* The focus has changed. Update the frames as necessary to reflect
5797 the new situation. Note that we can't change the selected frame
5798 here, because the Lisp code we are interrupting might become confused.
5799 Each event gets marked with the frame in which it occurred, so the
5800 Lisp code can tell when the switch took place by examining the events. */
5801
5802 static void
5803 x_new_focus_frame (dpyinfo, frame)
5804 struct x_display_info *dpyinfo;
5805 struct frame *frame;
5806 {
5807 struct frame *old_focus = dpyinfo->x_focus_frame;
5808
5809 if (frame != dpyinfo->x_focus_frame)
5810 {
5811 /* Set this before calling other routines, so that they see
5812 the correct value of x_focus_frame. */
5813 dpyinfo->x_focus_frame = frame;
5814
5815 if (old_focus && old_focus->auto_lower)
5816 x_lower_frame (old_focus);
5817
5818 #if 0
5819 selected_frame = frame;
5820 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5821 selected_frame);
5822 Fselect_window (selected_frame->selected_window);
5823 choose_minibuf_frame ();
5824 #endif /* ! 0 */
5825
5826 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5827 pending_autoraise_frame = dpyinfo->x_focus_frame;
5828 else
5829 pending_autoraise_frame = 0;
5830 }
5831
5832 x_frame_rehighlight (dpyinfo);
5833 }
5834
5835 /* Handle an event saying the mouse has moved out of an Emacs frame. */
5836
5837 void
5838 x_mouse_leave (dpyinfo)
5839 struct x_display_info *dpyinfo;
5840 {
5841 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
5842 }
5843
5844 /* The focus has changed, or we have redirected a frame's focus to
5845 another frame (this happens when a frame uses a surrogate
5846 mini-buffer frame). Shift the highlight as appropriate.
5847
5848 The FRAME argument doesn't necessarily have anything to do with which
5849 frame is being highlighted or un-highlighted; we only use it to find
5850 the appropriate X display info. */
5851
5852 static void
5853 XTframe_rehighlight (frame)
5854 struct frame *frame;
5855 {
5856 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
5857 }
5858
5859 static void
5860 x_frame_rehighlight (dpyinfo)
5861 struct x_display_info *dpyinfo;
5862 {
5863 struct frame *old_highlight = dpyinfo->x_highlight_frame;
5864
5865 if (dpyinfo->x_focus_frame)
5866 {
5867 dpyinfo->x_highlight_frame
5868 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
5869 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
5870 : dpyinfo->x_focus_frame);
5871 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
5872 {
5873 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
5874 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
5875 }
5876 }
5877 else
5878 dpyinfo->x_highlight_frame = 0;
5879
5880 if (dpyinfo->x_highlight_frame != old_highlight)
5881 {
5882 if (old_highlight)
5883 frame_unhighlight (old_highlight);
5884 if (dpyinfo->x_highlight_frame)
5885 frame_highlight (dpyinfo->x_highlight_frame);
5886 }
5887 }
5888
5889
5890 \f
5891 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
5892
5893 /* Initialize mode_switch_bit and modifier_meaning. */
5894 static void
5895 x_find_modifier_meanings (dpyinfo)
5896 struct x_display_info *dpyinfo;
5897 {
5898 int min_code, max_code;
5899 KeySym *syms;
5900 int syms_per_code;
5901 XModifierKeymap *mods;
5902
5903 dpyinfo->meta_mod_mask = 0;
5904 dpyinfo->shift_lock_mask = 0;
5905 dpyinfo->alt_mod_mask = 0;
5906 dpyinfo->super_mod_mask = 0;
5907 dpyinfo->hyper_mod_mask = 0;
5908
5909 #ifdef HAVE_X11R4
5910 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
5911 #else
5912 min_code = dpyinfo->display->min_keycode;
5913 max_code = dpyinfo->display->max_keycode;
5914 #endif
5915
5916 syms = XGetKeyboardMapping (dpyinfo->display,
5917 min_code, max_code - min_code + 1,
5918 &syms_per_code);
5919 mods = XGetModifierMapping (dpyinfo->display);
5920
5921 /* Scan the modifier table to see which modifier bits the Meta and
5922 Alt keysyms are on. */
5923 {
5924 int row, col; /* The row and column in the modifier table. */
5925
5926 for (row = 3; row < 8; row++)
5927 for (col = 0; col < mods->max_keypermod; col++)
5928 {
5929 KeyCode code
5930 = mods->modifiermap[(row * mods->max_keypermod) + col];
5931
5932 /* Zeroes are used for filler. Skip them. */
5933 if (code == 0)
5934 continue;
5935
5936 /* Are any of this keycode's keysyms a meta key? */
5937 {
5938 int code_col;
5939
5940 for (code_col = 0; code_col < syms_per_code; code_col++)
5941 {
5942 int sym = syms[((code - min_code) * syms_per_code) + code_col];
5943
5944 switch (sym)
5945 {
5946 case XK_Meta_L:
5947 case XK_Meta_R:
5948 dpyinfo->meta_mod_mask |= (1 << row);
5949 break;
5950
5951 case XK_Alt_L:
5952 case XK_Alt_R:
5953 dpyinfo->alt_mod_mask |= (1 << row);
5954 break;
5955
5956 case XK_Hyper_L:
5957 case XK_Hyper_R:
5958 dpyinfo->hyper_mod_mask |= (1 << row);
5959 break;
5960
5961 case XK_Super_L:
5962 case XK_Super_R:
5963 dpyinfo->super_mod_mask |= (1 << row);
5964 break;
5965
5966 case XK_Shift_Lock:
5967 /* Ignore this if it's not on the lock modifier. */
5968 if ((1 << row) == LockMask)
5969 dpyinfo->shift_lock_mask = LockMask;
5970 break;
5971 }
5972 }
5973 }
5974 }
5975 }
5976
5977 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
5978 if (! dpyinfo->meta_mod_mask)
5979 {
5980 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
5981 dpyinfo->alt_mod_mask = 0;
5982 }
5983
5984 /* If some keys are both alt and meta,
5985 make them just meta, not alt. */
5986 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
5987 {
5988 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
5989 }
5990
5991 XFree ((char *) syms);
5992 XFreeModifiermap (mods);
5993 }
5994
5995 /* Convert between the modifier bits X uses and the modifier bits
5996 Emacs uses. */
5997
5998 static unsigned int
5999 x_x_to_emacs_modifiers (dpyinfo, state)
6000 struct x_display_info *dpyinfo;
6001 unsigned int state;
6002 {
6003 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6004 | ((state & ControlMask) ? ctrl_modifier : 0)
6005 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6006 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6007 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6008 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
6009 }
6010
6011 static unsigned int
6012 x_emacs_to_x_modifiers (dpyinfo, state)
6013 struct x_display_info *dpyinfo;
6014 unsigned int state;
6015 {
6016 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6017 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6018 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6019 | ((state & shift_modifier) ? ShiftMask : 0)
6020 | ((state & ctrl_modifier) ? ControlMask : 0)
6021 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
6022 }
6023
6024 /* Convert a keysym to its name. */
6025
6026 char *
6027 x_get_keysym_name (keysym)
6028 KeySym keysym;
6029 {
6030 char *value;
6031
6032 BLOCK_INPUT;
6033 value = XKeysymToString (keysym);
6034 UNBLOCK_INPUT;
6035
6036 return value;
6037 }
6038
6039
6040 \f
6041 /* Mouse clicks and mouse movement. Rah. */
6042
6043 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6044 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6045 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6046 not force the value into range. */
6047
6048 void
6049 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6050 FRAME_PTR f;
6051 register int pix_x, pix_y;
6052 register int *x, *y;
6053 XRectangle *bounds;
6054 int noclip;
6055 {
6056 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6057 even for negative values. */
6058 if (pix_x < 0)
6059 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6060 if (pix_y < 0)
6061 pix_y -= (f)->output_data.x->line_height - 1;
6062
6063 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6064 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6065
6066 if (bounds)
6067 {
6068 bounds->width = FONT_WIDTH (f->output_data.x->font);
6069 bounds->height = f->output_data.x->line_height;
6070 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6071 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6072 }
6073
6074 if (!noclip)
6075 {
6076 if (pix_x < 0)
6077 pix_x = 0;
6078 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6079 pix_x = FRAME_WINDOW_WIDTH (f);
6080
6081 if (pix_y < 0)
6082 pix_y = 0;
6083 else if (pix_y > f->height)
6084 pix_y = f->height;
6085 }
6086
6087 *x = pix_x;
6088 *y = pix_y;
6089 }
6090
6091
6092 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6093 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6094 can't tell the positions because W's display is not up to date,
6095 return 0. */
6096
6097 int
6098 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6099 struct window *w;
6100 int hpos, vpos;
6101 int *frame_x, *frame_y;
6102 {
6103 int success_p;
6104
6105 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6106 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6107
6108 if (display_completed)
6109 {
6110 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6111 struct glyph *glyph = row->glyphs[TEXT_AREA];
6112 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6113
6114 *frame_y = row->y;
6115 *frame_x = row->x;
6116 while (glyph < end)
6117 {
6118 *frame_x += glyph->pixel_width;
6119 ++glyph;
6120 }
6121
6122 success_p = 1;
6123 }
6124 else
6125 {
6126 *frame_y = *frame_x = 0;
6127 success_p = 0;
6128 }
6129
6130 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6131 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6132 return success_p;
6133 }
6134
6135
6136 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6137
6138 If the event is a button press, then note that we have grabbed
6139 the mouse. */
6140
6141 static Lisp_Object
6142 construct_mouse_click (result, event, f)
6143 struct input_event *result;
6144 XButtonEvent *event;
6145 struct frame *f;
6146 {
6147 /* Make the event type no_event; we'll change that when we decide
6148 otherwise. */
6149 result->kind = mouse_click;
6150 result->code = event->button - Button1;
6151 result->timestamp = event->time;
6152 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6153 event->state)
6154 | (event->type == ButtonRelease
6155 ? up_modifier
6156 : down_modifier));
6157
6158 XSETINT (result->x, event->x);
6159 XSETINT (result->y, event->y);
6160 XSETFRAME (result->frame_or_window, f);
6161 result->arg = Qnil;
6162 return Qnil;
6163 }
6164
6165 \f
6166 /* Function to report a mouse movement to the mainstream Emacs code.
6167 The input handler calls this.
6168
6169 We have received a mouse movement event, which is given in *event.
6170 If the mouse is over a different glyph than it was last time, tell
6171 the mainstream emacs code by setting mouse_moved. If not, ask for
6172 another motion event, so we can check again the next time it moves. */
6173
6174 static XMotionEvent last_mouse_motion_event;
6175 static Lisp_Object last_mouse_motion_frame;
6176
6177 static void
6178 note_mouse_movement (frame, event)
6179 FRAME_PTR frame;
6180 XMotionEvent *event;
6181 {
6182 last_mouse_movement_time = event->time;
6183 last_mouse_motion_event = *event;
6184 XSETFRAME (last_mouse_motion_frame, frame);
6185
6186 if (event->window != FRAME_X_WINDOW (frame))
6187 {
6188 frame->mouse_moved = 1;
6189 last_mouse_scroll_bar = Qnil;
6190 note_mouse_highlight (frame, -1, -1);
6191 }
6192
6193 /* Has the mouse moved off the glyph it was on at the last sighting? */
6194 else if (event->x < last_mouse_glyph.x
6195 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6196 || event->y < last_mouse_glyph.y
6197 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6198 {
6199 frame->mouse_moved = 1;
6200 last_mouse_scroll_bar = Qnil;
6201 note_mouse_highlight (frame, event->x, event->y);
6202 }
6203 }
6204
6205 /* This is used for debugging, to turn off note_mouse_highlight. */
6206
6207 int disable_mouse_highlight;
6208
6209
6210 \f
6211 /************************************************************************
6212 Mouse Face
6213 ************************************************************************/
6214
6215 /* Find the glyph under window-relative coordinates X/Y in window W.
6216 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6217 strings. Return in *HPOS and *VPOS the row and column number of
6218 the glyph found. Return in *AREA the glyph area containing X.
6219 Value is a pointer to the glyph found or null if X/Y is not on
6220 text, or we can't tell because W's current matrix is not up to
6221 date. */
6222
6223 static struct glyph *
6224 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6225 struct window *w;
6226 int x, y;
6227 int *hpos, *vpos, *area;
6228 {
6229 struct glyph *glyph, *end;
6230 struct glyph_row *row = NULL;
6231 int x0, i, left_area_width;
6232
6233 /* Find row containing Y. Give up if some row is not enabled. */
6234 for (i = 0; i < w->current_matrix->nrows; ++i)
6235 {
6236 row = MATRIX_ROW (w->current_matrix, i);
6237 if (!row->enabled_p)
6238 return NULL;
6239 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6240 break;
6241 }
6242
6243 *vpos = i;
6244 *hpos = 0;
6245
6246 /* Give up if Y is not in the window. */
6247 if (i == w->current_matrix->nrows)
6248 return NULL;
6249
6250 /* Get the glyph area containing X. */
6251 if (w->pseudo_window_p)
6252 {
6253 *area = TEXT_AREA;
6254 x0 = 0;
6255 }
6256 else
6257 {
6258 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6259 if (x < left_area_width)
6260 {
6261 *area = LEFT_MARGIN_AREA;
6262 x0 = 0;
6263 }
6264 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6265 {
6266 *area = TEXT_AREA;
6267 x0 = row->x + left_area_width;
6268 }
6269 else
6270 {
6271 *area = RIGHT_MARGIN_AREA;
6272 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6273 }
6274 }
6275
6276 /* Find glyph containing X. */
6277 glyph = row->glyphs[*area];
6278 end = glyph + row->used[*area];
6279 while (glyph < end)
6280 {
6281 if (x < x0 + glyph->pixel_width)
6282 {
6283 if (w->pseudo_window_p)
6284 break;
6285 else if (BUFFERP (glyph->object))
6286 break;
6287 }
6288
6289 x0 += glyph->pixel_width;
6290 ++glyph;
6291 }
6292
6293 if (glyph == end)
6294 return NULL;
6295
6296 *hpos = glyph - row->glyphs[*area];
6297 return glyph;
6298 }
6299
6300
6301 /* Convert frame-relative x/y to coordinates relative to window W.
6302 Takes pseudo-windows into account. */
6303
6304 static void
6305 frame_to_window_pixel_xy (w, x, y)
6306 struct window *w;
6307 int *x, *y;
6308 {
6309 if (w->pseudo_window_p)
6310 {
6311 /* A pseudo-window is always full-width, and starts at the
6312 left edge of the frame, plus a frame border. */
6313 struct frame *f = XFRAME (w->frame);
6314 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6315 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6316 }
6317 else
6318 {
6319 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6320 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6321 }
6322 }
6323
6324
6325 /* Take proper action when mouse has moved to the mode or top line of
6326 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6327 mode line. X is relative to the start of the text display area of
6328 W, so the width of bitmap areas and scroll bars must be subtracted
6329 to get a position relative to the start of the mode line. */
6330
6331 static void
6332 note_mode_line_highlight (w, x, mode_line_p)
6333 struct window *w;
6334 int x, mode_line_p;
6335 {
6336 struct frame *f = XFRAME (w->frame);
6337 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6338 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6339 struct glyph_row *row;
6340
6341 if (mode_line_p)
6342 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6343 else
6344 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6345
6346 if (row->enabled_p)
6347 {
6348 struct glyph *glyph, *end;
6349 Lisp_Object help, map;
6350 int x0;
6351
6352 /* Find the glyph under X. */
6353 glyph = row->glyphs[TEXT_AREA];
6354 end = glyph + row->used[TEXT_AREA];
6355 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6356 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
6357 while (glyph < end
6358 && x >= x0 + glyph->pixel_width)
6359 {
6360 x0 += glyph->pixel_width;
6361 ++glyph;
6362 }
6363
6364 if (glyph < end
6365 && STRINGP (glyph->object)
6366 && XSTRING (glyph->object)->intervals
6367 && glyph->charpos >= 0
6368 && glyph->charpos < XSTRING (glyph->object)->size)
6369 {
6370 /* If we're on a string with `help-echo' text property,
6371 arrange for the help to be displayed. This is done by
6372 setting the global variable help_echo to the help string. */
6373 help = Fget_text_property (make_number (glyph->charpos),
6374 Qhelp_echo, glyph->object);
6375 if (!NILP (help))
6376 {
6377 help_echo = help;
6378 XSETWINDOW (help_echo_window, w);
6379 help_echo_object = glyph->object;
6380 help_echo_pos = glyph->charpos;
6381 }
6382
6383 /* Change the mouse pointer according to what is under X/Y. */
6384 map = Fget_text_property (make_number (glyph->charpos),
6385 Qlocal_map, glyph->object);
6386 if (!NILP (Fkeymapp (map)))
6387 cursor = f->output_data.x->nontext_cursor;
6388 else
6389 {
6390 map = Fget_text_property (make_number (glyph->charpos),
6391 Qkeymap, glyph->object);
6392 if (!NILP (Fkeymapp (map)))
6393 cursor = f->output_data.x->nontext_cursor;
6394 }
6395 }
6396 }
6397
6398 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6399 }
6400
6401
6402 /* Take proper action when the mouse has moved to position X, Y on
6403 frame F as regards highlighting characters that have mouse-face
6404 properties. Also de-highlighting chars where the mouse was before.
6405 X and Y can be negative or out of range. */
6406
6407 static void
6408 note_mouse_highlight (f, x, y)
6409 struct frame *f;
6410 int x, y;
6411 {
6412 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6413 int portion;
6414 Lisp_Object window;
6415 struct window *w;
6416
6417 /* When a menu is active, don't highlight because this looks odd. */
6418 #ifdef USE_X_TOOLKIT
6419 if (popup_activated ())
6420 return;
6421 #endif
6422
6423 if (disable_mouse_highlight
6424 || !f->glyphs_initialized_p)
6425 return;
6426
6427 dpyinfo->mouse_face_mouse_x = x;
6428 dpyinfo->mouse_face_mouse_y = y;
6429 dpyinfo->mouse_face_mouse_frame = f;
6430
6431 if (dpyinfo->mouse_face_defer)
6432 return;
6433
6434 if (gc_in_progress)
6435 {
6436 dpyinfo->mouse_face_deferred_gc = 1;
6437 return;
6438 }
6439
6440 /* Which window is that in? */
6441 window = window_from_coordinates (f, x, y, &portion, 1);
6442
6443 /* If we were displaying active text in another window, clear that. */
6444 if (! EQ (window, dpyinfo->mouse_face_window))
6445 clear_mouse_face (dpyinfo);
6446
6447 /* Not on a window -> return. */
6448 if (!WINDOWP (window))
6449 return;
6450
6451 /* Convert to window-relative pixel coordinates. */
6452 w = XWINDOW (window);
6453 frame_to_window_pixel_xy (w, &x, &y);
6454
6455 /* Handle tool-bar window differently since it doesn't display a
6456 buffer. */
6457 if (EQ (window, f->tool_bar_window))
6458 {
6459 note_tool_bar_highlight (f, x, y);
6460 return;
6461 }
6462
6463 if (portion == 1 || portion == 3)
6464 {
6465 /* Mouse is on the mode or top line. */
6466 note_mode_line_highlight (w, x, portion == 1);
6467 return;
6468 }
6469 else
6470 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6471 f->output_data.x->text_cursor);
6472
6473 /* Are we in a window whose display is up to date?
6474 And verify the buffer's text has not changed. */
6475 if (/* Within text portion of the window. */
6476 portion == 0
6477 && EQ (w->window_end_valid, w->buffer)
6478 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6479 && (XFASTINT (w->last_overlay_modified)
6480 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
6481 {
6482 int hpos, vpos, pos, i, area;
6483 struct glyph *glyph;
6484
6485 /* Find the glyph under X/Y. */
6486 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6487
6488 /* Clear mouse face if X/Y not over text. */
6489 if (glyph == NULL
6490 || area != TEXT_AREA
6491 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6492 {
6493 clear_mouse_face (dpyinfo);
6494 return;
6495 }
6496
6497 pos = glyph->charpos;
6498 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6499
6500 /* Check for mouse-face and help-echo. */
6501 {
6502 Lisp_Object mouse_face, overlay, position;
6503 Lisp_Object *overlay_vec;
6504 int len, noverlays;
6505 struct buffer *obuf;
6506 int obegv, ozv;
6507
6508 /* If we get an out-of-range value, return now; avoid an error. */
6509 if (pos > BUF_Z (XBUFFER (w->buffer)))
6510 return;
6511
6512 /* Make the window's buffer temporarily current for
6513 overlays_at and compute_char_face. */
6514 obuf = current_buffer;
6515 current_buffer = XBUFFER (w->buffer);
6516 obegv = BEGV;
6517 ozv = ZV;
6518 BEGV = BEG;
6519 ZV = Z;
6520
6521 /* Is this char mouse-active or does it have help-echo? */
6522 XSETINT (position, pos);
6523
6524 /* Put all the overlays we want in a vector in overlay_vec.
6525 Store the length in len. If there are more than 10, make
6526 enough space for all, and try again. */
6527 len = 10;
6528 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6529 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6530 if (noverlays > len)
6531 {
6532 len = noverlays;
6533 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6534 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6535 }
6536
6537 /* Sort overlays into increasing priority order. */
6538 noverlays = sort_overlays (overlay_vec, noverlays, w);
6539
6540 /* Check mouse-face highlighting. */
6541 if (! (EQ (window, dpyinfo->mouse_face_window)
6542 && vpos >= dpyinfo->mouse_face_beg_row
6543 && vpos <= dpyinfo->mouse_face_end_row
6544 && (vpos > dpyinfo->mouse_face_beg_row
6545 || hpos >= dpyinfo->mouse_face_beg_col)
6546 && (vpos < dpyinfo->mouse_face_end_row
6547 || hpos < dpyinfo->mouse_face_end_col
6548 || dpyinfo->mouse_face_past_end)))
6549 {
6550 /* Clear the display of the old active region, if any. */
6551 clear_mouse_face (dpyinfo);
6552
6553 /* Find the highest priority overlay that has a mouse-face prop. */
6554 overlay = Qnil;
6555 for (i = noverlays - 1; i >= 0; --i)
6556 {
6557 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6558 if (!NILP (mouse_face))
6559 {
6560 overlay = overlay_vec[i];
6561 break;
6562 }
6563 }
6564
6565 /* If no overlay applies, get a text property. */
6566 if (NILP (overlay))
6567 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6568
6569 /* Handle the overlay case. */
6570 if (! NILP (overlay))
6571 {
6572 /* Find the range of text around this char that
6573 should be active. */
6574 Lisp_Object before, after;
6575 int ignore;
6576
6577 before = Foverlay_start (overlay);
6578 after = Foverlay_end (overlay);
6579 /* Record this as the current active region. */
6580 fast_find_position (w, XFASTINT (before),
6581 &dpyinfo->mouse_face_beg_col,
6582 &dpyinfo->mouse_face_beg_row,
6583 &dpyinfo->mouse_face_beg_x,
6584 &dpyinfo->mouse_face_beg_y);
6585 dpyinfo->mouse_face_past_end
6586 = !fast_find_position (w, XFASTINT (after),
6587 &dpyinfo->mouse_face_end_col,
6588 &dpyinfo->mouse_face_end_row,
6589 &dpyinfo->mouse_face_end_x,
6590 &dpyinfo->mouse_face_end_y);
6591 dpyinfo->mouse_face_window = window;
6592 dpyinfo->mouse_face_face_id
6593 = face_at_buffer_position (w, pos, 0, 0,
6594 &ignore, pos + 1, 1);
6595
6596 /* Display it as active. */
6597 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6598 }
6599 /* Handle the text property case. */
6600 else if (! NILP (mouse_face))
6601 {
6602 /* Find the range of text around this char that
6603 should be active. */
6604 Lisp_Object before, after, beginning, end;
6605 int ignore;
6606
6607 beginning = Fmarker_position (w->start);
6608 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6609 - XFASTINT (w->window_end_pos)));
6610 before
6611 = Fprevious_single_property_change (make_number (pos + 1),
6612 Qmouse_face,
6613 w->buffer, beginning);
6614 after
6615 = Fnext_single_property_change (position, Qmouse_face,
6616 w->buffer, end);
6617 /* Record this as the current active region. */
6618 fast_find_position (w, XFASTINT (before),
6619 &dpyinfo->mouse_face_beg_col,
6620 &dpyinfo->mouse_face_beg_row,
6621 &dpyinfo->mouse_face_beg_x,
6622 &dpyinfo->mouse_face_beg_y);
6623 dpyinfo->mouse_face_past_end
6624 = !fast_find_position (w, XFASTINT (after),
6625 &dpyinfo->mouse_face_end_col,
6626 &dpyinfo->mouse_face_end_row,
6627 &dpyinfo->mouse_face_end_x,
6628 &dpyinfo->mouse_face_end_y);
6629 dpyinfo->mouse_face_window = window;
6630 dpyinfo->mouse_face_face_id
6631 = face_at_buffer_position (w, pos, 0, 0,
6632 &ignore, pos + 1, 1);
6633
6634 /* Display it as active. */
6635 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6636 }
6637 }
6638
6639 /* Look for a `help-echo' property. */
6640 {
6641 Lisp_Object help, overlay;
6642
6643 /* Check overlays first. */
6644 help = Qnil;
6645 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
6646 {
6647 overlay = overlay_vec[i];
6648 help = Foverlay_get (overlay, Qhelp_echo);
6649 }
6650
6651 if (!NILP (help))
6652 {
6653 help_echo = help;
6654 help_echo_window = window;
6655 help_echo_object = overlay;
6656 help_echo_pos = pos;
6657 }
6658 else
6659 {
6660 /* Try text properties. */
6661 if ((STRINGP (glyph->object)
6662 && glyph->charpos >= 0
6663 && glyph->charpos < XSTRING (glyph->object)->size)
6664 || (BUFFERP (glyph->object)
6665 && glyph->charpos >= BEGV
6666 && glyph->charpos < ZV))
6667 help = Fget_text_property (make_number (glyph->charpos),
6668 Qhelp_echo, glyph->object);
6669
6670 if (!NILP (help))
6671 {
6672 help_echo = help;
6673 help_echo_window = window;
6674 help_echo_object = glyph->object;
6675 help_echo_pos = glyph->charpos;
6676 }
6677 }
6678 }
6679
6680 BEGV = obegv;
6681 ZV = ozv;
6682 current_buffer = obuf;
6683 }
6684 }
6685 }
6686
6687 static void
6688 redo_mouse_highlight ()
6689 {
6690 if (!NILP (last_mouse_motion_frame)
6691 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6692 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6693 last_mouse_motion_event.x,
6694 last_mouse_motion_event.y);
6695 }
6696
6697
6698 \f
6699 /***********************************************************************
6700 Tool-bars
6701 ***********************************************************************/
6702
6703 static int x_tool_bar_item P_ ((struct frame *, int, int,
6704 struct glyph **, int *, int *, int *));
6705
6706 /* Tool-bar item index of the item on which a mouse button was pressed
6707 or -1. */
6708
6709 static int last_tool_bar_item;
6710
6711
6712 /* Get information about the tool-bar item at position X/Y on frame F.
6713 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6714 the current matrix of the tool-bar window of F, or NULL if not
6715 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6716 item in F->current_tool_bar_items. Value is
6717
6718 -1 if X/Y is not on a tool-bar item
6719 0 if X/Y is on the same item that was highlighted before.
6720 1 otherwise. */
6721
6722 static int
6723 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
6724 struct frame *f;
6725 int x, y;
6726 struct glyph **glyph;
6727 int *hpos, *vpos, *prop_idx;
6728 {
6729 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6730 struct window *w = XWINDOW (f->tool_bar_window);
6731 int area;
6732
6733 /* Find the glyph under X/Y. */
6734 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6735 if (*glyph == NULL)
6736 return -1;
6737
6738 /* Get the start of this tool-bar item's properties in
6739 f->current_tool_bar_items. */
6740 if (!tool_bar_item_info (f, *glyph, prop_idx))
6741 return -1;
6742
6743 /* Is mouse on the highlighted item? */
6744 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
6745 && *vpos >= dpyinfo->mouse_face_beg_row
6746 && *vpos <= dpyinfo->mouse_face_end_row
6747 && (*vpos > dpyinfo->mouse_face_beg_row
6748 || *hpos >= dpyinfo->mouse_face_beg_col)
6749 && (*vpos < dpyinfo->mouse_face_end_row
6750 || *hpos < dpyinfo->mouse_face_end_col
6751 || dpyinfo->mouse_face_past_end))
6752 return 0;
6753
6754 return 1;
6755 }
6756
6757
6758 /* Handle mouse button event on the tool-bar of frame F, at
6759 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6760 or ButtonRelase. */
6761
6762 static void
6763 x_handle_tool_bar_click (f, button_event)
6764 struct frame *f;
6765 XButtonEvent *button_event;
6766 {
6767 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6768 struct window *w = XWINDOW (f->tool_bar_window);
6769 int hpos, vpos, prop_idx;
6770 struct glyph *glyph;
6771 Lisp_Object enabled_p;
6772 int x = button_event->x;
6773 int y = button_event->y;
6774
6775 /* If not on the highlighted tool-bar item, return. */
6776 frame_to_window_pixel_xy (w, &x, &y);
6777 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
6778 return;
6779
6780 /* If item is disabled, do nothing. */
6781 enabled_p = (XVECTOR (f->current_tool_bar_items)
6782 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
6783 if (NILP (enabled_p))
6784 return;
6785
6786 if (button_event->type == ButtonPress)
6787 {
6788 /* Show item in pressed state. */
6789 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6790 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
6791 last_tool_bar_item = prop_idx;
6792 }
6793 else
6794 {
6795 Lisp_Object key, frame;
6796 struct input_event event;
6797
6798 /* Show item in released state. */
6799 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6800 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6801
6802 key = (XVECTOR (f->current_tool_bar_items)
6803 ->contents[prop_idx + TOOL_BAR_ITEM_KEY]);
6804
6805 XSETFRAME (frame, f);
6806 event.kind = TOOL_BAR_EVENT;
6807 event.frame_or_window = frame;
6808 event.arg = frame;
6809 kbd_buffer_store_event (&event);
6810
6811 event.kind = TOOL_BAR_EVENT;
6812 event.frame_or_window = frame;
6813 event.arg = key;
6814 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6815 button_event->state);
6816 kbd_buffer_store_event (&event);
6817 last_tool_bar_item = -1;
6818 }
6819 }
6820
6821
6822 /* Possibly highlight a tool-bar item on frame F when mouse moves to
6823 tool-bar window-relative coordinates X/Y. Called from
6824 note_mouse_highlight. */
6825
6826 static void
6827 note_tool_bar_highlight (f, x, y)
6828 struct frame *f;
6829 int x, y;
6830 {
6831 Lisp_Object window = f->tool_bar_window;
6832 struct window *w = XWINDOW (window);
6833 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6834 int hpos, vpos;
6835 struct glyph *glyph;
6836 struct glyph_row *row;
6837 int i;
6838 Lisp_Object enabled_p;
6839 int prop_idx;
6840 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
6841 int mouse_down_p, rc;
6842
6843 /* Function note_mouse_highlight is called with negative x(y
6844 values when mouse moves outside of the frame. */
6845 if (x <= 0 || y <= 0)
6846 {
6847 clear_mouse_face (dpyinfo);
6848 return;
6849 }
6850
6851 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
6852 if (rc < 0)
6853 {
6854 /* Not on tool-bar item. */
6855 clear_mouse_face (dpyinfo);
6856 return;
6857 }
6858 else if (rc == 0)
6859 /* On same tool-bar item as before. */
6860 goto set_help_echo;
6861
6862 clear_mouse_face (dpyinfo);
6863
6864 /* Mouse is down, but on different tool-bar item? */
6865 mouse_down_p = (dpyinfo->grabbed
6866 && f == last_mouse_frame
6867 && FRAME_LIVE_P (f));
6868 if (mouse_down_p
6869 && last_tool_bar_item != prop_idx)
6870 return;
6871
6872 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6873 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6874
6875 /* If tool-bar item is not enabled, don't highlight it. */
6876 enabled_p = (XVECTOR (f->current_tool_bar_items)
6877 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
6878 if (!NILP (enabled_p))
6879 {
6880 /* Compute the x-position of the glyph. In front and past the
6881 image is a space. We include this is the highlighted area. */
6882 row = MATRIX_ROW (w->current_matrix, vpos);
6883 for (i = x = 0; i < hpos; ++i)
6884 x += row->glyphs[TEXT_AREA][i].pixel_width;
6885
6886 /* Record this as the current active region. */
6887 dpyinfo->mouse_face_beg_col = hpos;
6888 dpyinfo->mouse_face_beg_row = vpos;
6889 dpyinfo->mouse_face_beg_x = x;
6890 dpyinfo->mouse_face_beg_y = row->y;
6891 dpyinfo->mouse_face_past_end = 0;
6892
6893 dpyinfo->mouse_face_end_col = hpos + 1;
6894 dpyinfo->mouse_face_end_row = vpos;
6895 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6896 dpyinfo->mouse_face_end_y = row->y;
6897 dpyinfo->mouse_face_window = window;
6898 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
6899
6900 /* Display it as active. */
6901 show_mouse_face (dpyinfo, draw);
6902 dpyinfo->mouse_face_image_state = draw;
6903 }
6904
6905 set_help_echo:
6906
6907 /* Set help_echo to a help string.to display for this tool-bar item.
6908 XTread_socket does the rest. */
6909 help_echo_object = help_echo_window = Qnil;
6910 help_echo_pos = -1;
6911 help_echo = (XVECTOR (f->current_tool_bar_items)
6912 ->contents[prop_idx + TOOL_BAR_ITEM_HELP]);
6913 if (NILP (help_echo))
6914 help_echo = (XVECTOR (f->current_tool_bar_items)
6915 ->contents[prop_idx + TOOL_BAR_ITEM_CAPTION]);
6916 }
6917
6918
6919 \f
6920 /* Find the glyph matrix position of buffer position POS in window W.
6921 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6922 current glyphs must be up to date. If POS is above window start
6923 return (0, 0, 0, 0). If POS is after end of W, return end of
6924 last line in W. */
6925
6926 static int
6927 fast_find_position (w, pos, hpos, vpos, x, y)
6928 struct window *w;
6929 int pos;
6930 int *hpos, *vpos, *x, *y;
6931 {
6932 int i;
6933 int lastcol;
6934 int maybe_next_line_p = 0;
6935 int line_start_position;
6936 int yb = window_text_bottom_y (w);
6937 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6938 struct glyph_row *best_row = row;
6939 int row_vpos = 0, best_row_vpos = 0;
6940 int current_x;
6941
6942 while (row->y < yb)
6943 {
6944 if (row->used[TEXT_AREA])
6945 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6946 else
6947 line_start_position = 0;
6948
6949 if (line_start_position > pos)
6950 break;
6951 /* If the position sought is the end of the buffer,
6952 don't include the blank lines at the bottom of the window. */
6953 else if (line_start_position == pos
6954 && pos == BUF_ZV (XBUFFER (w->buffer)))
6955 {
6956 maybe_next_line_p = 1;
6957 break;
6958 }
6959 else if (line_start_position > 0)
6960 {
6961 best_row = row;
6962 best_row_vpos = row_vpos;
6963 }
6964
6965 if (row->y + row->height >= yb)
6966 break;
6967
6968 ++row;
6969 ++row_vpos;
6970 }
6971
6972 /* Find the right column within BEST_ROW. */
6973 lastcol = 0;
6974 current_x = best_row->x;
6975 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
6976 {
6977 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6978 int charpos;
6979
6980 charpos = glyph->charpos;
6981 if (charpos == pos)
6982 {
6983 *hpos = i;
6984 *vpos = best_row_vpos;
6985 *x = current_x;
6986 *y = best_row->y;
6987 return 1;
6988 }
6989 else if (charpos > pos)
6990 break;
6991 else if (charpos > 0)
6992 lastcol = i;
6993
6994 current_x += glyph->pixel_width;
6995 }
6996
6997 /* If we're looking for the end of the buffer,
6998 and we didn't find it in the line we scanned,
6999 use the start of the following line. */
7000 if (maybe_next_line_p)
7001 {
7002 ++best_row;
7003 ++best_row_vpos;
7004 lastcol = 0;
7005 current_x = best_row->x;
7006 }
7007
7008 *vpos = best_row_vpos;
7009 *hpos = lastcol + 1;
7010 *x = current_x;
7011 *y = best_row->y;
7012 return 0;
7013 }
7014
7015
7016 /* Display the active region described by mouse_face_*
7017 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7018
7019 static void
7020 show_mouse_face (dpyinfo, draw)
7021 struct x_display_info *dpyinfo;
7022 enum draw_glyphs_face draw;
7023 {
7024 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7025 struct frame *f = XFRAME (WINDOW_FRAME (w));
7026 int i;
7027 int cursor_off_p = 0;
7028 struct cursor_pos saved_cursor;
7029
7030 saved_cursor = output_cursor;
7031
7032 /* If window is in the process of being destroyed, don't bother
7033 to do anything. */
7034 if (w->current_matrix == NULL)
7035 goto set_x_cursor;
7036
7037 /* Recognize when we are called to operate on rows that don't exist
7038 anymore. This can happen when a window is split. */
7039 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7040 goto set_x_cursor;
7041
7042 set_output_cursor (&w->phys_cursor);
7043
7044 /* Note that mouse_face_beg_row etc. are window relative. */
7045 for (i = dpyinfo->mouse_face_beg_row;
7046 i <= dpyinfo->mouse_face_end_row;
7047 i++)
7048 {
7049 int start_hpos, end_hpos, start_x;
7050 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7051
7052 /* Don't do anything if row doesn't have valid contents. */
7053 if (!row->enabled_p)
7054 continue;
7055
7056 /* For all but the first row, the highlight starts at column 0. */
7057 if (i == dpyinfo->mouse_face_beg_row)
7058 {
7059 start_hpos = dpyinfo->mouse_face_beg_col;
7060 start_x = dpyinfo->mouse_face_beg_x;
7061 }
7062 else
7063 {
7064 start_hpos = 0;
7065 start_x = 0;
7066 }
7067
7068 if (i == dpyinfo->mouse_face_end_row)
7069 end_hpos = dpyinfo->mouse_face_end_col;
7070 else
7071 end_hpos = row->used[TEXT_AREA];
7072
7073 /* If the cursor's in the text we are about to rewrite, turn the
7074 cursor off. */
7075 if (!w->pseudo_window_p
7076 && i == output_cursor.vpos
7077 && output_cursor.hpos >= start_hpos - 1
7078 && output_cursor.hpos <= end_hpos)
7079 {
7080 x_update_window_cursor (w, 0);
7081 cursor_off_p = 1;
7082 }
7083
7084 if (end_hpos > start_hpos)
7085 {
7086 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
7087 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7088 start_hpos, end_hpos, draw, NULL, NULL, 0);
7089 }
7090 }
7091
7092 /* If we turned the cursor off, turn it back on. */
7093 if (cursor_off_p)
7094 x_display_cursor (w, 1,
7095 output_cursor.hpos, output_cursor.vpos,
7096 output_cursor.x, output_cursor.y);
7097
7098 output_cursor = saved_cursor;
7099
7100 set_x_cursor:
7101
7102 /* Change the mouse cursor. */
7103 if (draw == DRAW_NORMAL_TEXT)
7104 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7105 f->output_data.x->text_cursor);
7106 else if (draw == DRAW_MOUSE_FACE)
7107 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7108 f->output_data.x->cross_cursor);
7109 else
7110 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7111 f->output_data.x->nontext_cursor);
7112 }
7113
7114 /* Clear out the mouse-highlighted active region.
7115 Redraw it un-highlighted first. */
7116
7117 void
7118 clear_mouse_face (dpyinfo)
7119 struct x_display_info *dpyinfo;
7120 {
7121 if (tip_frame)
7122 return;
7123
7124 if (! NILP (dpyinfo->mouse_face_window))
7125 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7126
7127 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7128 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7129 dpyinfo->mouse_face_window = Qnil;
7130 }
7131
7132
7133 /* Clear any mouse-face on window W. This function is part of the
7134 redisplay interface, and is called from try_window_id and similar
7135 functions to ensure the mouse-highlight is off. */
7136
7137 static void
7138 x_clear_mouse_face (w)
7139 struct window *w;
7140 {
7141 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7142 Lisp_Object window;
7143
7144 XSETWINDOW (window, w);
7145 if (EQ (window, dpyinfo->mouse_face_window))
7146 clear_mouse_face (dpyinfo);
7147 }
7148
7149
7150 /* Just discard the mouse face information for frame F, if any.
7151 This is used when the size of F is changed. */
7152
7153 void
7154 cancel_mouse_face (f)
7155 FRAME_PTR f;
7156 {
7157 Lisp_Object window;
7158 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7159
7160 window = dpyinfo->mouse_face_window;
7161 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7162 {
7163 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7164 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7165 dpyinfo->mouse_face_window = Qnil;
7166 }
7167 }
7168 \f
7169 static struct scroll_bar *x_window_to_scroll_bar ();
7170 static void x_scroll_bar_report_motion ();
7171
7172 /* Return the current position of the mouse.
7173 *fp should be a frame which indicates which display to ask about.
7174
7175 If the mouse movement started in a scroll bar, set *fp, *bar_window,
7176 and *part to the frame, window, and scroll bar part that the mouse
7177 is over. Set *x and *y to the portion and whole of the mouse's
7178 position on the scroll bar.
7179
7180 If the mouse movement started elsewhere, set *fp to the frame the
7181 mouse is on, *bar_window to nil, and *x and *y to the character cell
7182 the mouse is over.
7183
7184 Set *time to the server time-stamp for the time at which the mouse
7185 was at this position.
7186
7187 Don't store anything if we don't have a valid set of values to report.
7188
7189 This clears the mouse_moved flag, so we can wait for the next mouse
7190 movement. */
7191
7192 static void
7193 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7194 FRAME_PTR *fp;
7195 int insist;
7196 Lisp_Object *bar_window;
7197 enum scroll_bar_part *part;
7198 Lisp_Object *x, *y;
7199 unsigned long *time;
7200 {
7201 FRAME_PTR f1;
7202
7203 BLOCK_INPUT;
7204
7205 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7206 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7207 else
7208 {
7209 Window root;
7210 int root_x, root_y;
7211
7212 Window dummy_window;
7213 int dummy;
7214
7215 Lisp_Object frame, tail;
7216
7217 /* Clear the mouse-moved flag for every frame on this display. */
7218 FOR_EACH_FRAME (tail, frame)
7219 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7220 XFRAME (frame)->mouse_moved = 0;
7221
7222 last_mouse_scroll_bar = Qnil;
7223
7224 /* Figure out which root window we're on. */
7225 XQueryPointer (FRAME_X_DISPLAY (*fp),
7226 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
7227
7228 /* The root window which contains the pointer. */
7229 &root,
7230
7231 /* Trash which we can't trust if the pointer is on
7232 a different screen. */
7233 &dummy_window,
7234
7235 /* The position on that root window. */
7236 &root_x, &root_y,
7237
7238 /* More trash we can't trust. */
7239 &dummy, &dummy,
7240
7241 /* Modifier keys and pointer buttons, about which
7242 we don't care. */
7243 (unsigned int *) &dummy);
7244
7245 /* Now we have a position on the root; find the innermost window
7246 containing the pointer. */
7247 {
7248 Window win, child;
7249 int win_x, win_y;
7250 int parent_x = 0, parent_y = 0;
7251 int count;
7252
7253 win = root;
7254
7255 /* XTranslateCoordinates can get errors if the window
7256 structure is changing at the same time this function
7257 is running. So at least we must not crash from them. */
7258
7259 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
7260
7261 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
7262 && FRAME_LIVE_P (last_mouse_frame))
7263 {
7264 /* If mouse was grabbed on a frame, give coords for that frame
7265 even if the mouse is now outside it. */
7266 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7267
7268 /* From-window, to-window. */
7269 root, FRAME_X_WINDOW (last_mouse_frame),
7270
7271 /* From-position, to-position. */
7272 root_x, root_y, &win_x, &win_y,
7273
7274 /* Child of win. */
7275 &child);
7276 f1 = last_mouse_frame;
7277 }
7278 else
7279 {
7280 while (1)
7281 {
7282 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7283
7284 /* From-window, to-window. */
7285 root, win,
7286
7287 /* From-position, to-position. */
7288 root_x, root_y, &win_x, &win_y,
7289
7290 /* Child of win. */
7291 &child);
7292
7293 if (child == None || child == win)
7294 break;
7295
7296 win = child;
7297 parent_x = win_x;
7298 parent_y = win_y;
7299 }
7300
7301 /* Now we know that:
7302 win is the innermost window containing the pointer
7303 (XTC says it has no child containing the pointer),
7304 win_x and win_y are the pointer's position in it
7305 (XTC did this the last time through), and
7306 parent_x and parent_y are the pointer's position in win's parent.
7307 (They are what win_x and win_y were when win was child.
7308 If win is the root window, it has no parent, and
7309 parent_{x,y} are invalid, but that's okay, because we'll
7310 never use them in that case.) */
7311
7312 /* Is win one of our frames? */
7313 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
7314 }
7315
7316 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7317 f1 = 0;
7318
7319 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
7320
7321 /* If not, is it one of our scroll bars? */
7322 if (! f1)
7323 {
7324 struct scroll_bar *bar = x_window_to_scroll_bar (win);
7325
7326 if (bar)
7327 {
7328 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7329 win_x = parent_x;
7330 win_y = parent_y;
7331 }
7332 }
7333
7334 if (f1 == 0 && insist > 0)
7335 f1 = SELECTED_FRAME ();
7336
7337 if (f1)
7338 {
7339 /* Ok, we found a frame. Store all the values.
7340 last_mouse_glyph is a rectangle used to reduce the
7341 generation of mouse events. To not miss any motion
7342 events, we must divide the frame into rectangles of the
7343 size of the smallest character that could be displayed
7344 on it, i.e. into the same rectangles that matrices on
7345 the frame are divided into. */
7346
7347 #if OLD_REDISPLAY_CODE
7348 int ignore1, ignore2;
7349 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
7350 &last_mouse_glyph,
7351 FRAME_X_DISPLAY_INFO (f1)->grabbed
7352 || insist);
7353 #else
7354 {
7355 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7356 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7357 int x = win_x;
7358 int y = win_y;
7359
7360 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7361 round down even for negative values. */
7362 if (x < 0)
7363 x -= width - 1;
7364 if (y < 0)
7365 y -= height - 1;
7366
7367 last_mouse_glyph.width = width;
7368 last_mouse_glyph.height = height;
7369 last_mouse_glyph.x = (x + width - 1) / width * width;
7370 last_mouse_glyph.y = (y + height - 1) / height * height;
7371 }
7372 #endif
7373
7374 *bar_window = Qnil;
7375 *part = 0;
7376 *fp = f1;
7377 XSETINT (*x, win_x);
7378 XSETINT (*y, win_y);
7379 *time = last_mouse_movement_time;
7380 }
7381 }
7382 }
7383
7384 UNBLOCK_INPUT;
7385 }
7386
7387
7388 #ifdef USE_X_TOOLKIT
7389
7390 /* Atimer callback function for TIMER. Called every 0.1s to process
7391 Xt timeouts, if needed. We must avoid calling XtAppPending as
7392 much as possible because that function does an implicit XFlush
7393 that slows us down. */
7394
7395 static void
7396 x_process_timeouts (timer)
7397 struct atimer *timer;
7398 {
7399 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7400 {
7401 BLOCK_INPUT;
7402 while (XtAppPending (Xt_app_con) & XtIMTimer)
7403 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7404 UNBLOCK_INPUT;
7405 }
7406 }
7407
7408 #endif /* USE_X_TOOLKIT */
7409
7410 \f
7411 /* Scroll bar support. */
7412
7413 /* Given an X window ID, find the struct scroll_bar which manages it.
7414 This can be called in GC, so we have to make sure to strip off mark
7415 bits. */
7416
7417 static struct scroll_bar *
7418 x_window_to_scroll_bar (window_id)
7419 Window window_id;
7420 {
7421 Lisp_Object tail;
7422
7423 for (tail = Vframe_list;
7424 XGCTYPE (tail) == Lisp_Cons;
7425 tail = XCDR (tail))
7426 {
7427 Lisp_Object frame, bar, condemned;
7428
7429 frame = XCAR (tail);
7430 /* All elements of Vframe_list should be frames. */
7431 if (! GC_FRAMEP (frame))
7432 abort ();
7433
7434 /* Scan this frame's scroll bar list for a scroll bar with the
7435 right window ID. */
7436 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7437 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7438 /* This trick allows us to search both the ordinary and
7439 condemned scroll bar lists with one loop. */
7440 ! GC_NILP (bar) || (bar = condemned,
7441 condemned = Qnil,
7442 ! GC_NILP (bar));
7443 bar = XSCROLL_BAR (bar)->next)
7444 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7445 return XSCROLL_BAR (bar);
7446 }
7447
7448 return 0;
7449 }
7450
7451
7452 \f
7453 /************************************************************************
7454 Toolkit scroll bars
7455 ************************************************************************/
7456
7457 #if USE_TOOLKIT_SCROLL_BARS
7458
7459 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7460 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7461 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7462 struct scroll_bar *));
7463 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7464 int, int, int));
7465
7466
7467 /* Id of action hook installed for scroll bars. */
7468
7469 static XtActionHookId action_hook_id;
7470
7471 /* Lisp window being scrolled. Set when starting to interact with
7472 a toolkit scroll bar, reset to nil when ending the interaction. */
7473
7474 static Lisp_Object window_being_scrolled;
7475
7476 /* Last scroll bar part sent in xm_scroll_callback. */
7477
7478 static int last_scroll_bar_part;
7479
7480 /* Whether this is an Xaw with arrow-scrollbars. This should imply
7481 that movements of 1/20 of the screen size are mapped to up/down. */
7482
7483 static Boolean xaw3d_arrow_scroll;
7484
7485 /* Whether the drag scrolling maintains the mouse at the top of the
7486 thumb. If not, resizing the thumb needs to be done more carefully
7487 to avoid jerkyness. */
7488
7489 static Boolean xaw3d_pick_top;
7490
7491
7492 /* Action hook installed via XtAppAddActionHook when toolkit scroll
7493 bars are used.. The hook is responsible for detecting when
7494 the user ends an interaction with the scroll bar, and generates
7495 a `end-scroll' scroll_bar_click' event if so. */
7496
7497 static void
7498 xt_action_hook (widget, client_data, action_name, event, params,
7499 num_params)
7500 Widget widget;
7501 XtPointer client_data;
7502 String action_name;
7503 XEvent *event;
7504 String *params;
7505 Cardinal *num_params;
7506 {
7507 int scroll_bar_p;
7508 char *end_action;
7509
7510 #ifdef USE_MOTIF
7511 scroll_bar_p = XmIsScrollBar (widget);
7512 end_action = "Release";
7513 #else /* !USE_MOTIF i.e. use Xaw */
7514 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7515 end_action = "EndScroll";
7516 #endif /* USE_MOTIF */
7517
7518 if (scroll_bar_p
7519 && strcmp (action_name, end_action) == 0
7520 && WINDOWP (window_being_scrolled))
7521 {
7522 struct window *w;
7523
7524 x_send_scroll_bar_event (window_being_scrolled,
7525 scroll_bar_end_scroll, 0, 0);
7526 w = XWINDOW (window_being_scrolled);
7527 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7528 window_being_scrolled = Qnil;
7529 last_scroll_bar_part = -1;
7530
7531 /* Xt timeouts no longer needed. */
7532 toolkit_scroll_bar_interaction = 0;
7533 }
7534 }
7535
7536
7537 /* Send a client message with message type Xatom_Scrollbar for a
7538 scroll action to the frame of WINDOW. PART is a value identifying
7539 the part of the scroll bar that was clicked on. PORTION is the
7540 amount to scroll of a whole of WHOLE. */
7541
7542 static void
7543 x_send_scroll_bar_event (window, part, portion, whole)
7544 Lisp_Object window;
7545 int part, portion, whole;
7546 {
7547 XEvent event;
7548 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7549 struct frame *f = XFRAME (XWINDOW (window)->frame);
7550
7551 /* Construct a ClientMessage event to send to the frame. */
7552 ev->type = ClientMessage;
7553 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7554 ev->display = FRAME_X_DISPLAY (f);
7555 ev->window = FRAME_X_WINDOW (f);
7556 ev->format = 32;
7557 ev->data.l[0] = (long) XFASTINT (window);
7558 ev->data.l[1] = (long) part;
7559 ev->data.l[2] = (long) 0;
7560 ev->data.l[3] = (long) portion;
7561 ev->data.l[4] = (long) whole;
7562
7563 /* Make Xt timeouts work while the scroll bar is active. */
7564 toolkit_scroll_bar_interaction = 1;
7565
7566 /* Setting the event mask to zero means that the message will
7567 be sent to the client that created the window, and if that
7568 window no longer exists, no event will be sent. */
7569 BLOCK_INPUT;
7570 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7571 UNBLOCK_INPUT;
7572 }
7573
7574
7575 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7576 in *IEVENT. */
7577
7578 static void
7579 x_scroll_bar_to_input_event (event, ievent)
7580 XEvent *event;
7581 struct input_event *ievent;
7582 {
7583 XClientMessageEvent *ev = (XClientMessageEvent *) event;
7584 Lisp_Object window;
7585 struct frame *f;
7586
7587 XSETFASTINT (window, ev->data.l[0]);
7588 f = XFRAME (XWINDOW (window)->frame);
7589
7590 ievent->kind = scroll_bar_click;
7591 ievent->frame_or_window = window;
7592 ievent->arg = Qnil;
7593 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7594 ievent->part = ev->data.l[1];
7595 ievent->code = ev->data.l[2];
7596 ievent->x = make_number ((int) ev->data.l[3]);
7597 ievent->y = make_number ((int) ev->data.l[4]);
7598 ievent->modifiers = 0;
7599 }
7600
7601
7602 #ifdef USE_MOTIF
7603
7604 /* Minimum and maximum values used for Motif scroll bars. */
7605
7606 #define XM_SB_MIN 1
7607 #define XM_SB_MAX 10000000
7608 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7609
7610
7611 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7612 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7613 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7614
7615 static void
7616 xm_scroll_callback (widget, client_data, call_data)
7617 Widget widget;
7618 XtPointer client_data, call_data;
7619 {
7620 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7621 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7622 double percent;
7623 int part = -1, whole = 0, portion = 0;
7624
7625 switch (cs->reason)
7626 {
7627 case XmCR_DECREMENT:
7628 bar->dragging = Qnil;
7629 part = scroll_bar_up_arrow;
7630 break;
7631
7632 case XmCR_INCREMENT:
7633 bar->dragging = Qnil;
7634 part = scroll_bar_down_arrow;
7635 break;
7636
7637 case XmCR_PAGE_DECREMENT:
7638 bar->dragging = Qnil;
7639 part = scroll_bar_above_handle;
7640 break;
7641
7642 case XmCR_PAGE_INCREMENT:
7643 bar->dragging = Qnil;
7644 part = scroll_bar_below_handle;
7645 break;
7646
7647 case XmCR_TO_TOP:
7648 bar->dragging = Qnil;
7649 part = scroll_bar_to_top;
7650 break;
7651
7652 case XmCR_TO_BOTTOM:
7653 bar->dragging = Qnil;
7654 part = scroll_bar_to_bottom;
7655 break;
7656
7657 case XmCR_DRAG:
7658 {
7659 int slider_size;
7660 int dragging_down_p = (INTEGERP (bar->dragging)
7661 && XINT (bar->dragging) <= cs->value);
7662
7663 /* Get the slider size. */
7664 BLOCK_INPUT;
7665 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7666 UNBLOCK_INPUT;
7667
7668 /* At the max position of the scroll bar, do a line-wise
7669 movement. Without doing anything, the LessTif scroll bar
7670 calls us with the same cs->value again and again. If we
7671 want to make sure that we can reach the end of the buffer,
7672 we have to do something.
7673
7674 Implementation note: setting bar->dragging always to
7675 cs->value gives a smoother movement at the max position.
7676 Setting it to nil when doing line-wise movement gives
7677 a better slider behavior. */
7678
7679 if (cs->value + slider_size == XM_SB_MAX
7680 || (dragging_down_p
7681 && last_scroll_bar_part == scroll_bar_down_arrow))
7682 {
7683 part = scroll_bar_down_arrow;
7684 bar->dragging = Qnil;
7685 }
7686 else
7687 {
7688 whole = XM_SB_RANGE;
7689 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7690 part = scroll_bar_handle;
7691 bar->dragging = make_number (cs->value);
7692 }
7693 }
7694 break;
7695
7696 case XmCR_VALUE_CHANGED:
7697 break;
7698 };
7699
7700 if (part >= 0)
7701 {
7702 window_being_scrolled = bar->window;
7703 last_scroll_bar_part = part;
7704 x_send_scroll_bar_event (bar->window, part, portion, whole);
7705 }
7706 }
7707
7708
7709 #else /* !USE_MOTIF, i.e. Xaw. */
7710
7711
7712 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
7713 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7714 scroll bar struct. CALL_DATA is a pointer to a float saying where
7715 the thumb is. */
7716
7717 static void
7718 xaw_jump_callback (widget, client_data, call_data)
7719 Widget widget;
7720 XtPointer client_data, call_data;
7721 {
7722 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7723 float top = *(float *) call_data;
7724 float shown;
7725 int whole, portion, height;
7726 int part;
7727
7728 /* Get the size of the thumb, a value between 0 and 1. */
7729 BLOCK_INPUT;
7730 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
7731 UNBLOCK_INPUT;
7732
7733 whole = 10000000;
7734 portion = shown < 1 ? top * whole : 0;
7735
7736 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7737 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7738 the bottom, so we force the scrolling whenever we see that we're
7739 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7740 we try to ensure that we always stay two pixels away from the
7741 bottom). */
7742 part = scroll_bar_down_arrow;
7743 else
7744 part = scroll_bar_handle;
7745
7746 window_being_scrolled = bar->window;
7747 bar->dragging = make_number (portion);
7748 last_scroll_bar_part = part;
7749 x_send_scroll_bar_event (bar->window, part, portion, whole);
7750 }
7751
7752
7753 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
7754 i.e. line or page up or down. WIDGET is the Xaw scroll bar
7755 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7756 the scroll bar. CALL_DATA is an integer specifying the action that
7757 has taken place. It's magnitude is in the range 0..height of the
7758 scroll bar. Negative values mean scroll towards buffer start.
7759 Values < height of scroll bar mean line-wise movement. */
7760
7761 static void
7762 xaw_scroll_callback (widget, client_data, call_data)
7763 Widget widget;
7764 XtPointer client_data, call_data;
7765 {
7766 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7767 int position = (int) call_data;
7768 Dimension height;
7769 int part;
7770
7771 /* Get the height of the scroll bar. */
7772 BLOCK_INPUT;
7773 XtVaGetValues (widget, XtNheight, &height, NULL);
7774 UNBLOCK_INPUT;
7775
7776 if (abs (position) >= height)
7777 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7778
7779 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
7780 it maps line-movement to call_data = max(5, height/20). */
7781 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
7782 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
7783 else
7784 part = scroll_bar_move_ratio;
7785
7786 window_being_scrolled = bar->window;
7787 bar->dragging = Qnil;
7788 last_scroll_bar_part = part;
7789 x_send_scroll_bar_event (bar->window, part, position, height);
7790 }
7791
7792
7793 #endif /* not USE_MOTIF */
7794
7795
7796 /* Create the widget for scroll bar BAR on frame F. Record the widget
7797 and X window of the scroll bar in BAR. */
7798
7799 static void
7800 x_create_toolkit_scroll_bar (f, bar)
7801 struct frame *f;
7802 struct scroll_bar *bar;
7803 {
7804 Window xwindow;
7805 Widget widget;
7806 Arg av[20];
7807 int ac = 0;
7808 char *scroll_bar_name = "verticalScrollBar";
7809 unsigned long pixel;
7810
7811 BLOCK_INPUT;
7812
7813 #ifdef USE_MOTIF
7814 /* LessTif 0.85, problems:
7815
7816 1. When the mouse if over the scroll bar, the scroll bar will
7817 get keyboard events. I didn't find a way to turn this off.
7818
7819 2. Do we have to explicitly set the cursor to get an arrow
7820 cursor (see below)? */
7821
7822 /* Set resources. Create the widget. */
7823 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7824 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
7825 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
7826 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
7827 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
7828 XtSetArg (av[ac], XmNincrement, 1); ++ac;
7829 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
7830
7831 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7832 if (pixel != -1)
7833 {
7834 XtSetArg (av[ac], XmNforeground, pixel);
7835 ++ac;
7836 }
7837
7838 pixel = f->output_data.x->scroll_bar_background_pixel;
7839 if (pixel != -1)
7840 {
7841 XtSetArg (av[ac], XmNbackground, pixel);
7842 ++ac;
7843 }
7844
7845 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
7846 scroll_bar_name, av, ac);
7847
7848 /* Add one callback for everything that can happen. */
7849 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
7850 (XtPointer) bar);
7851 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
7852 (XtPointer) bar);
7853 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
7854 (XtPointer) bar);
7855 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
7856 (XtPointer) bar);
7857 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
7858 (XtPointer) bar);
7859 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
7860 (XtPointer) bar);
7861 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
7862 (XtPointer) bar);
7863
7864 /* Realize the widget. Only after that is the X window created. */
7865 XtRealizeWidget (widget);
7866
7867 /* Set the cursor to an arrow. I didn't find a resource to do that.
7868 And I'm wondering why it hasn't an arrow cursor by default. */
7869 XDefineCursor (XtDisplay (widget), XtWindow (widget),
7870 f->output_data.x->nontext_cursor);
7871
7872 #else /* !USE_MOTIF i.e. use Xaw */
7873
7874 /* Set resources. Create the widget. The background of the
7875 Xaw3d scroll bar widget is a little bit light for my taste.
7876 We don't alter it here to let users change it according
7877 to their taste with `emacs*verticalScrollBar.background: xxx'. */
7878 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7879 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
7880 /* For smoother scrolling with Xaw3d -sm */
7881 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
7882 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
7883
7884 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7885 if (pixel != -1)
7886 {
7887 XtSetArg (av[ac], XtNforeground, pixel);
7888 ++ac;
7889 }
7890
7891 pixel = f->output_data.x->scroll_bar_background_pixel;
7892 if (pixel != -1)
7893 {
7894 XtSetArg (av[ac], XtNbackground, pixel);
7895 ++ac;
7896 }
7897
7898 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
7899 f->output_data.x->edit_widget, av, ac);
7900
7901 {
7902 char *initial = "";
7903 char *val = initial;
7904 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
7905 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
7906 if (val == initial)
7907 { /* ARROW_SCROLL */
7908 xaw3d_arrow_scroll = True;
7909 /* Isn't that just a personal preference ? -sm */
7910 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
7911 }
7912 }
7913
7914 /* Define callbacks. */
7915 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
7916 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
7917 (XtPointer) bar);
7918
7919 /* Realize the widget. Only after that is the X window created. */
7920 XtRealizeWidget (widget);
7921
7922 #endif /* !USE_MOTIF */
7923
7924 /* Install an action hook that let's us detect when the user
7925 finishes interacting with a scroll bar. */
7926 if (action_hook_id == 0)
7927 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7928
7929 /* Remember X window and widget in the scroll bar vector. */
7930 SET_SCROLL_BAR_X_WIDGET (bar, widget);
7931 xwindow = XtWindow (widget);
7932 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
7933
7934 UNBLOCK_INPUT;
7935 }
7936
7937
7938 /* Set the thumb size and position of scroll bar BAR. We are currently
7939 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7940
7941 static void
7942 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
7943 struct scroll_bar *bar;
7944 int portion, position, whole;
7945 {
7946 float top, shown;
7947 Widget widget = SCROLL_BAR_X_WIDGET (bar);
7948
7949 if (whole == 0)
7950 top = 0, shown = 1;
7951 else
7952 {
7953 top = (float) position / whole;
7954 shown = (float) portion / whole;
7955 }
7956
7957 BLOCK_INPUT;
7958
7959 #ifdef USE_MOTIF
7960 {
7961 int size, value;
7962 Boolean arrow1_selected, arrow2_selected;
7963 unsigned char flags;
7964 XmScrollBarWidget sb;
7965
7966 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
7967 is the scroll bar's maximum and MIN is the scroll bar's minimum
7968 value. */
7969 size = shown * XM_SB_RANGE;
7970 size = min (size, XM_SB_RANGE);
7971 size = max (size, 1);
7972
7973 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
7974 value = top * XM_SB_RANGE;
7975 value = min (value, XM_SB_MAX - size);
7976 value = max (value, XM_SB_MIN);
7977
7978 /* LessTif: Calling XmScrollBarSetValues after an increment or
7979 decrement turns off auto-repeat LessTif-internally. This can
7980 be seen in ScrollBar.c which resets Arrow1Selected and
7981 Arrow2Selected. It also sets internal flags so that LessTif
7982 believes the mouse is in the slider. We either have to change
7983 our code, or work around that by accessing private data. */
7984
7985 sb = (XmScrollBarWidget) widget;
7986 arrow1_selected = sb->scrollBar.arrow1_selected;
7987 arrow2_selected = sb->scrollBar.arrow2_selected;
7988 flags = sb->scrollBar.flags;
7989
7990 if (NILP (bar->dragging))
7991 XmScrollBarSetValues (widget, value, size, 0, 0, False);
7992 else if (last_scroll_bar_part == scroll_bar_down_arrow)
7993 /* This has the negative side effect that the slider value is
7994 not what it would be if we scrolled here using line-wise or
7995 page-wise movement. */
7996 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
7997 else
7998 {
7999 /* If currently dragging, only update the slider size.
8000 This reduces flicker effects. */
8001 int old_value, old_size, increment, page_increment;
8002
8003 XmScrollBarGetValues (widget, &old_value, &old_size,
8004 &increment, &page_increment);
8005 XmScrollBarSetValues (widget, old_value,
8006 min (size, XM_SB_RANGE - old_value),
8007 0, 0, False);
8008 }
8009
8010 sb->scrollBar.arrow1_selected = arrow1_selected;
8011 sb->scrollBar.arrow2_selected = arrow2_selected;
8012 sb->scrollBar.flags = flags;
8013 }
8014 #else /* !USE_MOTIF i.e. use Xaw */
8015 {
8016 float old_top, old_shown;
8017 Dimension height;
8018 XtVaGetValues (widget,
8019 XtNtopOfThumb, &old_top,
8020 XtNshown, &old_shown,
8021 XtNheight, &height,
8022 NULL);
8023
8024 /* Massage the top+shown values. */
8025 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8026 top = max (0, min (1, top));
8027 else
8028 top = old_top;
8029 /* Keep two pixels available for moving the thumb down. */
8030 shown = max (0, min (1 - top - (2.0 / height), shown));
8031
8032 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8033 check that your system's configuration file contains a define
8034 for `NARROWPROTO'. See s/freebsd.h for an example. */
8035 if (top != old_top || shown != old_shown)
8036 {
8037 if (NILP (bar->dragging))
8038 XawScrollbarSetThumb (widget, top, shown);
8039 else
8040 {
8041 #ifdef HAVE_XAW3D
8042 ScrollbarWidget sb = (ScrollbarWidget) widget;
8043 int scroll_mode = 0;
8044
8045 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8046 if (xaw3d_arrow_scroll)
8047 {
8048 /* Xaw3d stupidly ignores resize requests while dragging
8049 so we have to make it believe it's not in dragging mode. */
8050 scroll_mode = sb->scrollbar.scroll_mode;
8051 if (scroll_mode == 2)
8052 sb->scrollbar.scroll_mode = 0;
8053 }
8054 #endif
8055 /* Try to make the scrolling a tad smoother. */
8056 if (!xaw3d_pick_top)
8057 shown = min (shown, old_shown);
8058
8059 XawScrollbarSetThumb (widget, top, shown);
8060
8061 #ifdef HAVE_XAW3D
8062 if (xaw3d_arrow_scroll && scroll_mode == 2)
8063 sb->scrollbar.scroll_mode = scroll_mode;
8064 #endif
8065 }
8066 }
8067 }
8068 #endif /* !USE_MOTIF */
8069
8070 UNBLOCK_INPUT;
8071 }
8072
8073 #endif /* USE_TOOLKIT_SCROLL_BARS */
8074
8075
8076 \f
8077 /************************************************************************
8078 Scroll bars, general
8079 ************************************************************************/
8080
8081 /* Create a scroll bar and return the scroll bar vector for it. W is
8082 the Emacs window on which to create the scroll bar. TOP, LEFT,
8083 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8084 scroll bar. */
8085
8086 static struct scroll_bar *
8087 x_scroll_bar_create (w, top, left, width, height)
8088 struct window *w;
8089 int top, left, width, height;
8090 {
8091 struct frame *f = XFRAME (w->frame);
8092 struct scroll_bar *bar
8093 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8094
8095 BLOCK_INPUT;
8096
8097 #if USE_TOOLKIT_SCROLL_BARS
8098 x_create_toolkit_scroll_bar (f, bar);
8099 #else /* not USE_TOOLKIT_SCROLL_BARS */
8100 {
8101 XSetWindowAttributes a;
8102 unsigned long mask;
8103 Window window;
8104
8105 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8106 if (a.background_pixel == -1)
8107 a.background_pixel = f->output_data.x->background_pixel;
8108
8109 a.event_mask = (ButtonPressMask | ButtonReleaseMask
8110 | ButtonMotionMask | PointerMotionHintMask
8111 | ExposureMask);
8112 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
8113
8114 mask = (CWBackPixel | CWEventMask | CWCursor);
8115
8116 /* Clear the area of W that will serve as a scroll bar. This is
8117 for the case that a window has been split horizontally. In
8118 this case, no clear_frame is generated to reduce flickering. */
8119 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8120 left, top, width,
8121 window_box_height (w), False);
8122
8123 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8124 /* Position and size of scroll bar. */
8125 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8126 top,
8127 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8128 height,
8129 /* Border width, depth, class, and visual. */
8130 0,
8131 CopyFromParent,
8132 CopyFromParent,
8133 CopyFromParent,
8134 /* Attributes. */
8135 mask, &a);
8136 SET_SCROLL_BAR_X_WINDOW (bar, window);
8137 }
8138 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8139
8140 XSETWINDOW (bar->window, w);
8141 XSETINT (bar->top, top);
8142 XSETINT (bar->left, left);
8143 XSETINT (bar->width, width);
8144 XSETINT (bar->height, height);
8145 XSETINT (bar->start, 0);
8146 XSETINT (bar->end, 0);
8147 bar->dragging = Qnil;
8148
8149 /* Add bar to its frame's list of scroll bars. */
8150 bar->next = FRAME_SCROLL_BARS (f);
8151 bar->prev = Qnil;
8152 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8153 if (!NILP (bar->next))
8154 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8155
8156 /* Map the window/widget. */
8157 #if USE_TOOLKIT_SCROLL_BARS
8158 XtMapWidget (SCROLL_BAR_X_WIDGET (bar));
8159 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
8160 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8161 top,
8162 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8163 height, 0);
8164 #else /* not USE_TOOLKIT_SCROLL_BARS */
8165 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8166 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8167
8168 UNBLOCK_INPUT;
8169 return bar;
8170 }
8171
8172
8173 /* Draw BAR's handle in the proper position.
8174
8175 If the handle is already drawn from START to END, don't bother
8176 redrawing it, unless REBUILD is non-zero; in that case, always
8177 redraw it. (REBUILD is handy for drawing the handle after expose
8178 events.)
8179
8180 Normally, we want to constrain the start and end of the handle to
8181 fit inside its rectangle, but if the user is dragging the scroll
8182 bar handle, we want to let them drag it down all the way, so that
8183 the bar's top is as far down as it goes; otherwise, there's no way
8184 to move to the very end of the buffer. */
8185
8186 #ifndef USE_TOOLKIT_SCROLL_BARS
8187
8188 static void
8189 x_scroll_bar_set_handle (bar, start, end, rebuild)
8190 struct scroll_bar *bar;
8191 int start, end;
8192 int rebuild;
8193 {
8194 int dragging = ! NILP (bar->dragging);
8195 Window w = SCROLL_BAR_X_WINDOW (bar);
8196 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8197 GC gc = f->output_data.x->normal_gc;
8198
8199 /* If the display is already accurate, do nothing. */
8200 if (! rebuild
8201 && start == XINT (bar->start)
8202 && end == XINT (bar->end))
8203 return;
8204
8205 BLOCK_INPUT;
8206
8207 {
8208 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8209 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8210 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8211
8212 /* Make sure the values are reasonable, and try to preserve
8213 the distance between start and end. */
8214 {
8215 int length = end - start;
8216
8217 if (start < 0)
8218 start = 0;
8219 else if (start > top_range)
8220 start = top_range;
8221 end = start + length;
8222
8223 if (end < start)
8224 end = start;
8225 else if (end > top_range && ! dragging)
8226 end = top_range;
8227 }
8228
8229 /* Store the adjusted setting in the scroll bar. */
8230 XSETINT (bar->start, start);
8231 XSETINT (bar->end, end);
8232
8233 /* Clip the end position, just for display. */
8234 if (end > top_range)
8235 end = top_range;
8236
8237 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
8238 below top positions, to make sure the handle is always at least
8239 that many pixels tall. */
8240 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
8241
8242 /* Draw the empty space above the handle. Note that we can't clear
8243 zero-height areas; that means "clear to end of window." */
8244 if (0 < start)
8245 XClearArea (FRAME_X_DISPLAY (f), w,
8246
8247 /* x, y, width, height, and exposures. */
8248 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8249 VERTICAL_SCROLL_BAR_TOP_BORDER,
8250 inside_width, start,
8251 False);
8252
8253 /* Change to proper foreground color if one is specified. */
8254 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8255 XSetForeground (FRAME_X_DISPLAY (f), gc,
8256 f->output_data.x->scroll_bar_foreground_pixel);
8257
8258 /* Draw the handle itself. */
8259 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
8260
8261 /* x, y, width, height */
8262 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8263 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
8264 inside_width, end - start);
8265
8266 /* Restore the foreground color of the GC if we changed it above. */
8267 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8268 XSetForeground (FRAME_X_DISPLAY (f), gc,
8269 f->output_data.x->foreground_pixel);
8270
8271 /* Draw the empty space below the handle. Note that we can't
8272 clear zero-height areas; that means "clear to end of window." */
8273 if (end < inside_height)
8274 XClearArea (FRAME_X_DISPLAY (f), w,
8275
8276 /* x, y, width, height, and exposures. */
8277 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8278 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
8279 inside_width, inside_height - end,
8280 False);
8281
8282 }
8283
8284 UNBLOCK_INPUT;
8285 }
8286
8287 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8288
8289 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8290 nil. */
8291
8292 static void
8293 x_scroll_bar_remove (bar)
8294 struct scroll_bar *bar;
8295 {
8296 BLOCK_INPUT;
8297
8298 #if USE_TOOLKIT_SCROLL_BARS
8299 XtDestroyWidget (SCROLL_BAR_X_WIDGET (bar));
8300 #else /* not USE_TOOLKIT_SCROLL_BARS */
8301 {
8302 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8303 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8304 }
8305 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8306
8307 /* Disassociate this scroll bar from its window. */
8308 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
8309
8310 UNBLOCK_INPUT;
8311 }
8312
8313
8314 /* Set the handle of the vertical scroll bar for WINDOW to indicate
8315 that we are displaying PORTION characters out of a total of WHOLE
8316 characters, starting at POSITION. If WINDOW has no scroll bar,
8317 create one. */
8318
8319 static void
8320 XTset_vertical_scroll_bar (w, portion, whole, position)
8321 struct window *w;
8322 int portion, whole, position;
8323 {
8324 struct frame *f = XFRAME (w->frame);
8325 struct scroll_bar *bar;
8326 int top, height, left, sb_left, width, sb_width;
8327 int window_x, window_y, window_width, window_height;
8328
8329 /* Get window dimensions. */
8330 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8331 top = window_y;
8332 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8333 height = window_height;
8334
8335 /* Compute the left edge of the scroll bar area. */
8336 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8337 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8338 else
8339 left = XFASTINT (w->left);
8340 left *= CANON_X_UNIT (f);
8341 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8342
8343 /* Compute the width of the scroll bar which might be less than
8344 the width of the area reserved for the scroll bar. */
8345 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8346 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
8347 else
8348 sb_width = width;
8349
8350 /* Compute the left edge of the scroll bar. */
8351 #ifdef USE_TOOLKIT_SCROLL_BARS
8352 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8353 sb_left = left + width - sb_width - (width - sb_width) / 2;
8354 else
8355 sb_left = left + (width - sb_width) / 2;
8356 #else
8357 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8358 sb_left = left + width - sb_width;
8359 else
8360 sb_left = left;
8361 #endif
8362
8363 /* Does the scroll bar exist yet? */
8364 if (NILP (w->vertical_scroll_bar))
8365 {
8366 BLOCK_INPUT;
8367 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8368 left, top, width, height, False);
8369 UNBLOCK_INPUT;
8370 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8371 }
8372 else
8373 {
8374 /* It may just need to be moved and resized. */
8375 unsigned int mask = 0;
8376
8377 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8378
8379 BLOCK_INPUT;
8380
8381 if (sb_left != XINT (bar->left))
8382 mask |= CWX;
8383 if (top != XINT (bar->top))
8384 mask |= CWY;
8385 if (sb_width != XINT (bar->width))
8386 mask |= CWWidth;
8387 if (height != XINT (bar->height))
8388 mask |= CWHeight;
8389
8390 #ifdef USE_TOOLKIT_SCROLL_BARS
8391
8392 /* Since toolkit scroll bars are smaller than the space reserved
8393 for them on the frame, we have to clear "under" them. */
8394 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8395 left, top, width, height, False);
8396
8397 /* Move/size the scroll bar widget. */
8398 if (mask)
8399 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
8400 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8401 top,
8402 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8403 height, 0);
8404
8405 #else /* not USE_TOOLKIT_SCROLL_BARS */
8406
8407 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8408 {
8409 /* Clear areas not covered by the scroll bar. This makes sure a
8410 previous mode line display is cleared after C-x 2 C-x 1, for
8411 example. Non-toolkit scroll bars are as wide as the area
8412 reserved for scroll bars - trim at both sides. */
8413 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8414 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8415 height, False);
8416 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8417 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8418 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8419 height, False);
8420 }
8421
8422 /* Move/size the scroll bar window. */
8423 if (mask)
8424 {
8425 XWindowChanges wc;
8426
8427 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8428 wc.y = top;
8429 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8430 wc.height = height;
8431 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8432 mask, &wc);
8433 }
8434
8435 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8436
8437 /* Remember new settings. */
8438 XSETINT (bar->left, sb_left);
8439 XSETINT (bar->top, top);
8440 XSETINT (bar->width, sb_width);
8441 XSETINT (bar->height, height);
8442
8443 UNBLOCK_INPUT;
8444 }
8445
8446 #if USE_TOOLKIT_SCROLL_BARS
8447 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8448 #else /* not USE_TOOLKIT_SCROLL_BARS */
8449 /* Set the scroll bar's current state, unless we're currently being
8450 dragged. */
8451 if (NILP (bar->dragging))
8452 {
8453 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
8454
8455 if (whole == 0)
8456 x_scroll_bar_set_handle (bar, 0, top_range, 0);
8457 else
8458 {
8459 int start = ((double) position * top_range) / whole;
8460 int end = ((double) (position + portion) * top_range) / whole;
8461 x_scroll_bar_set_handle (bar, start, end, 0);
8462 }
8463 }
8464 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8465
8466 XSETVECTOR (w->vertical_scroll_bar, bar);
8467 }
8468
8469
8470 /* The following three hooks are used when we're doing a thorough
8471 redisplay of the frame. We don't explicitly know which scroll bars
8472 are going to be deleted, because keeping track of when windows go
8473 away is a real pain - "Can you say set-window-configuration, boys
8474 and girls?" Instead, we just assert at the beginning of redisplay
8475 that *all* scroll bars are to be removed, and then save a scroll bar
8476 from the fiery pit when we actually redisplay its window. */
8477
8478 /* Arrange for all scroll bars on FRAME to be removed at the next call
8479 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
8480 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8481
8482 static void
8483 XTcondemn_scroll_bars (frame)
8484 FRAME_PTR frame;
8485 {
8486 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8487 while (! NILP (FRAME_SCROLL_BARS (frame)))
8488 {
8489 Lisp_Object bar;
8490 bar = FRAME_SCROLL_BARS (frame);
8491 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8492 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8493 XSCROLL_BAR (bar)->prev = Qnil;
8494 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8495 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8496 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8497 }
8498 }
8499
8500 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
8501 Note that WINDOW isn't necessarily condemned at all. */
8502 static void
8503 XTredeem_scroll_bar (window)
8504 struct window *window;
8505 {
8506 struct scroll_bar *bar;
8507
8508 /* We can't redeem this window's scroll bar if it doesn't have one. */
8509 if (NILP (window->vertical_scroll_bar))
8510 abort ();
8511
8512 bar = XSCROLL_BAR (window->vertical_scroll_bar);
8513
8514 /* Unlink it from the condemned list. */
8515 {
8516 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
8517
8518 if (NILP (bar->prev))
8519 {
8520 /* If the prev pointer is nil, it must be the first in one of
8521 the lists. */
8522 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8523 /* It's not condemned. Everything's fine. */
8524 return;
8525 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8526 window->vertical_scroll_bar))
8527 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8528 else
8529 /* If its prev pointer is nil, it must be at the front of
8530 one or the other! */
8531 abort ();
8532 }
8533 else
8534 XSCROLL_BAR (bar->prev)->next = bar->next;
8535
8536 if (! NILP (bar->next))
8537 XSCROLL_BAR (bar->next)->prev = bar->prev;
8538
8539 bar->next = FRAME_SCROLL_BARS (f);
8540 bar->prev = Qnil;
8541 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8542 if (! NILP (bar->next))
8543 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8544 }
8545 }
8546
8547 /* Remove all scroll bars on FRAME that haven't been saved since the
8548 last call to `*condemn_scroll_bars_hook'. */
8549
8550 static void
8551 XTjudge_scroll_bars (f)
8552 FRAME_PTR f;
8553 {
8554 Lisp_Object bar, next;
8555
8556 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
8557
8558 /* Clear out the condemned list now so we won't try to process any
8559 more events on the hapless scroll bars. */
8560 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
8561
8562 for (; ! NILP (bar); bar = next)
8563 {
8564 struct scroll_bar *b = XSCROLL_BAR (bar);
8565
8566 x_scroll_bar_remove (b);
8567
8568 next = b->next;
8569 b->next = b->prev = Qnil;
8570 }
8571
8572 /* Now there should be no references to the condemned scroll bars,
8573 and they should get garbage-collected. */
8574 }
8575
8576
8577 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
8578 is a no-op when using toolkit scroll bars.
8579
8580 This may be called from a signal handler, so we have to ignore GC
8581 mark bits. */
8582
8583 static void
8584 x_scroll_bar_expose (bar, event)
8585 struct scroll_bar *bar;
8586 XEvent *event;
8587 {
8588 #ifndef USE_TOOLKIT_SCROLL_BARS
8589
8590 Window w = SCROLL_BAR_X_WINDOW (bar);
8591 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8592 GC gc = f->output_data.x->normal_gc;
8593 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8594
8595 BLOCK_INPUT;
8596
8597 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
8598
8599 /* Draw a one-pixel border just inside the edges of the scroll bar. */
8600 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
8601
8602 /* x, y, width, height */
8603 0, 0,
8604 XINT (bar->width) - 1 - width_trim - width_trim,
8605 XINT (bar->height) - 1);
8606
8607 UNBLOCK_INPUT;
8608
8609 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8610 }
8611
8612 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8613 is set to something other than no_event, it is enqueued.
8614
8615 This may be called from a signal handler, so we have to ignore GC
8616 mark bits. */
8617
8618 #ifndef USE_TOOLKIT_SCROLL_BARS
8619
8620 static void
8621 x_scroll_bar_handle_click (bar, event, emacs_event)
8622 struct scroll_bar *bar;
8623 XEvent *event;
8624 struct input_event *emacs_event;
8625 {
8626 if (! GC_WINDOWP (bar->window))
8627 abort ();
8628
8629 emacs_event->kind = scroll_bar_click;
8630 emacs_event->code = event->xbutton.button - Button1;
8631 emacs_event->modifiers
8632 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8633 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8634 event->xbutton.state)
8635 | (event->type == ButtonRelease
8636 ? up_modifier
8637 : down_modifier));
8638 emacs_event->frame_or_window = bar->window;
8639 emacs_event->arg = Qnil;
8640 emacs_event->timestamp = event->xbutton.time;
8641 {
8642 #if 0
8643 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8644 int internal_height
8645 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8646 #endif
8647 int top_range
8648 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8649 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
8650
8651 if (y < 0) y = 0;
8652 if (y > top_range) y = top_range;
8653
8654 if (y < XINT (bar->start))
8655 emacs_event->part = scroll_bar_above_handle;
8656 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8657 emacs_event->part = scroll_bar_handle;
8658 else
8659 emacs_event->part = scroll_bar_below_handle;
8660
8661 /* Just because the user has clicked on the handle doesn't mean
8662 they want to drag it. Lisp code needs to be able to decide
8663 whether or not we're dragging. */
8664 #if 0
8665 /* If the user has just clicked on the handle, record where they're
8666 holding it. */
8667 if (event->type == ButtonPress
8668 && emacs_event->part == scroll_bar_handle)
8669 XSETINT (bar->dragging, y - XINT (bar->start));
8670 #endif
8671
8672 /* If the user has released the handle, set it to its final position. */
8673 if (event->type == ButtonRelease
8674 && ! NILP (bar->dragging))
8675 {
8676 int new_start = y - XINT (bar->dragging);
8677 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8678
8679 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8680 bar->dragging = Qnil;
8681 }
8682
8683 /* Same deal here as the other #if 0. */
8684 #if 0
8685 /* Clicks on the handle are always reported as occurring at the top of
8686 the handle. */
8687 if (emacs_event->part == scroll_bar_handle)
8688 emacs_event->x = bar->start;
8689 else
8690 XSETINT (emacs_event->x, y);
8691 #else
8692 XSETINT (emacs_event->x, y);
8693 #endif
8694
8695 XSETINT (emacs_event->y, top_range);
8696 }
8697 }
8698
8699 /* Handle some mouse motion while someone is dragging the scroll bar.
8700
8701 This may be called from a signal handler, so we have to ignore GC
8702 mark bits. */
8703
8704 static void
8705 x_scroll_bar_note_movement (bar, event)
8706 struct scroll_bar *bar;
8707 XEvent *event;
8708 {
8709 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8710
8711 last_mouse_movement_time = event->xmotion.time;
8712
8713 f->mouse_moved = 1;
8714 XSETVECTOR (last_mouse_scroll_bar, bar);
8715
8716 /* If we're dragging the bar, display it. */
8717 if (! GC_NILP (bar->dragging))
8718 {
8719 /* Where should the handle be now? */
8720 int new_start = event->xmotion.y - XINT (bar->dragging);
8721
8722 if (new_start != XINT (bar->start))
8723 {
8724 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8725
8726 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8727 }
8728 }
8729 }
8730
8731 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8732
8733 /* Return information to the user about the current position of the mouse
8734 on the scroll bar. */
8735
8736 static void
8737 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8738 FRAME_PTR *fp;
8739 Lisp_Object *bar_window;
8740 enum scroll_bar_part *part;
8741 Lisp_Object *x, *y;
8742 unsigned long *time;
8743 {
8744 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
8745 Window w = SCROLL_BAR_X_WINDOW (bar);
8746 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8747 int win_x, win_y;
8748 Window dummy_window;
8749 int dummy_coord;
8750 unsigned int dummy_mask;
8751
8752 BLOCK_INPUT;
8753
8754 /* Get the mouse's position relative to the scroll bar window, and
8755 report that. */
8756 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
8757
8758 /* Root, child, root x and root y. */
8759 &dummy_window, &dummy_window,
8760 &dummy_coord, &dummy_coord,
8761
8762 /* Position relative to scroll bar. */
8763 &win_x, &win_y,
8764
8765 /* Mouse buttons and modifier keys. */
8766 &dummy_mask))
8767 ;
8768 else
8769 {
8770 #if 0
8771 int inside_height
8772 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8773 #endif
8774 int top_range
8775 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8776
8777 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8778
8779 if (! NILP (bar->dragging))
8780 win_y -= XINT (bar->dragging);
8781
8782 if (win_y < 0)
8783 win_y = 0;
8784 if (win_y > top_range)
8785 win_y = top_range;
8786
8787 *fp = f;
8788 *bar_window = bar->window;
8789
8790 if (! NILP (bar->dragging))
8791 *part = scroll_bar_handle;
8792 else if (win_y < XINT (bar->start))
8793 *part = scroll_bar_above_handle;
8794 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8795 *part = scroll_bar_handle;
8796 else
8797 *part = scroll_bar_below_handle;
8798
8799 XSETINT (*x, win_y);
8800 XSETINT (*y, top_range);
8801
8802 f->mouse_moved = 0;
8803 last_mouse_scroll_bar = Qnil;
8804 }
8805
8806 *time = last_mouse_movement_time;
8807
8808 UNBLOCK_INPUT;
8809 }
8810
8811
8812 /* The screen has been cleared so we may have changed foreground or
8813 background colors, and the scroll bars may need to be redrawn.
8814 Clear out the scroll bars, and ask for expose events, so we can
8815 redraw them. */
8816
8817 void
8818 x_scroll_bar_clear (f)
8819 FRAME_PTR f;
8820 {
8821 #ifndef USE_TOOLKIT_SCROLL_BARS
8822 Lisp_Object bar;
8823
8824 /* We can have scroll bars even if this is 0,
8825 if we just turned off scroll bar mode.
8826 But in that case we should not clear them. */
8827 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8828 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8829 bar = XSCROLL_BAR (bar)->next)
8830 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
8831 0, 0, 0, 0, True);
8832 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8833 }
8834
8835 /* This processes Expose events from the menu-bar specific X event
8836 loop in xmenu.c. This allows to redisplay the frame if necessary
8837 when handling menu-bar or pop-up items. */
8838
8839 int
8840 process_expose_from_menu (event)
8841 XEvent event;
8842 {
8843 FRAME_PTR f;
8844 struct x_display_info *dpyinfo;
8845 int frame_exposed_p = 0;
8846
8847 BLOCK_INPUT;
8848
8849 dpyinfo = x_display_info_for_display (event.xexpose.display);
8850 f = x_window_to_frame (dpyinfo, event.xexpose.window);
8851 if (f)
8852 {
8853 if (f->async_visible == 0)
8854 {
8855 f->async_visible = 1;
8856 f->async_iconified = 0;
8857 f->output_data.x->has_been_visible = 1;
8858 SET_FRAME_GARBAGED (f);
8859 }
8860 else
8861 {
8862 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
8863 event.xexpose.x, event.xexpose.y,
8864 event.xexpose.width, event.xexpose.height);
8865 frame_exposed_p = 1;
8866 }
8867 }
8868 else
8869 {
8870 struct scroll_bar *bar
8871 = x_window_to_scroll_bar (event.xexpose.window);
8872
8873 if (bar)
8874 x_scroll_bar_expose (bar, &event);
8875 }
8876
8877 UNBLOCK_INPUT;
8878 return frame_exposed_p;
8879 }
8880 \f
8881 /* Define a queue to save up SelectionRequest events for later handling. */
8882
8883 struct selection_event_queue
8884 {
8885 XEvent event;
8886 struct selection_event_queue *next;
8887 };
8888
8889 static struct selection_event_queue *queue;
8890
8891 /* Nonzero means queue up certain events--don't process them yet. */
8892
8893 static int x_queue_selection_requests;
8894
8895 /* Queue up an X event *EVENT, to be processed later. */
8896
8897 static void
8898 x_queue_event (f, event)
8899 FRAME_PTR f;
8900 XEvent *event;
8901 {
8902 struct selection_event_queue *queue_tmp
8903 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
8904
8905 if (queue_tmp != NULL)
8906 {
8907 queue_tmp->event = *event;
8908 queue_tmp->next = queue;
8909 queue = queue_tmp;
8910 }
8911 }
8912
8913 /* Take all the queued events and put them back
8914 so that they get processed afresh. */
8915
8916 static void
8917 x_unqueue_events (display)
8918 Display *display;
8919 {
8920 while (queue != NULL)
8921 {
8922 struct selection_event_queue *queue_tmp = queue;
8923 XPutBackEvent (display, &queue_tmp->event);
8924 queue = queue_tmp->next;
8925 xfree ((char *)queue_tmp);
8926 }
8927 }
8928
8929 /* Start queuing SelectionRequest events. */
8930
8931 void
8932 x_start_queuing_selection_requests (display)
8933 Display *display;
8934 {
8935 x_queue_selection_requests++;
8936 }
8937
8938 /* Stop queuing SelectionRequest events. */
8939
8940 void
8941 x_stop_queuing_selection_requests (display)
8942 Display *display;
8943 {
8944 x_queue_selection_requests--;
8945 x_unqueue_events (display);
8946 }
8947 \f
8948 /* The main X event-reading loop - XTread_socket. */
8949
8950 /* Time stamp of enter window event. This is only used by XTread_socket,
8951 but we have to put it out here, since static variables within functions
8952 sometimes don't work. */
8953
8954 static Time enter_timestamp;
8955
8956 /* This holds the state XLookupString needs to implement dead keys
8957 and other tricks known as "compose processing". _X Window System_
8958 says that a portable program can't use this, but Stephen Gildea assures
8959 me that letting the compiler initialize it to zeros will work okay.
8960
8961 This must be defined outside of XTread_socket, for the same reasons
8962 given for enter_time stamp, above. */
8963
8964 static XComposeStatus compose_status;
8965
8966 /* Record the last 100 characters stored
8967 to help debug the loss-of-chars-during-GC problem. */
8968
8969 static int temp_index;
8970 static short temp_buffer[100];
8971
8972 /* Set this to nonzero to fake an "X I/O error"
8973 on a particular display. */
8974
8975 struct x_display_info *XTread_socket_fake_io_error;
8976
8977 /* When we find no input here, we occasionally do a no-op command
8978 to verify that the X server is still running and we can still talk with it.
8979 We try all the open displays, one by one.
8980 This variable is used for cycling thru the displays. */
8981
8982 static struct x_display_info *next_noop_dpyinfo;
8983
8984 #define SET_SAVED_MENU_EVENT(size) \
8985 do \
8986 { \
8987 if (f->output_data.x->saved_menu_event == 0) \
8988 f->output_data.x->saved_menu_event \
8989 = (XEvent *) xmalloc (sizeof (XEvent)); \
8990 bcopy (&event, f->output_data.x->saved_menu_event, size); \
8991 if (numchars >= 1) \
8992 { \
8993 bufp->kind = menu_bar_activate_event; \
8994 XSETFRAME (bufp->frame_or_window, f); \
8995 bufp->arg = Qnil; \
8996 bufp++; \
8997 count++; \
8998 numchars--; \
8999 } \
9000 } \
9001 while (0)
9002
9003 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9004 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9005
9006 /* Read events coming from the X server.
9007 This routine is called by the SIGIO handler.
9008 We return as soon as there are no more events to be read.
9009
9010 Events representing keys are stored in buffer BUFP,
9011 which can hold up to NUMCHARS characters.
9012 We return the number of characters stored into the buffer,
9013 thus pretending to be `read'.
9014
9015 EXPECTED is nonzero if the caller knows input is available. */
9016
9017 int
9018 XTread_socket (sd, bufp, numchars, expected)
9019 register int sd;
9020 /* register */ struct input_event *bufp;
9021 /* register */ int numchars;
9022 int expected;
9023 {
9024 int count = 0;
9025 int nbytes = 0;
9026 XEvent event;
9027 struct frame *f;
9028 int event_found = 0;
9029 struct x_display_info *dpyinfo;
9030 struct coding_system coding;
9031
9032 if (interrupt_input_blocked)
9033 {
9034 interrupt_input_pending = 1;
9035 return -1;
9036 }
9037
9038 interrupt_input_pending = 0;
9039 BLOCK_INPUT;
9040
9041 /* So people can tell when we have read the available input. */
9042 input_signal_count++;
9043
9044 if (numchars <= 0)
9045 abort (); /* Don't think this happens. */
9046
9047 ++handling_signal;
9048
9049 /* The input should be decoded if it is from XIM. Currently the
9050 locale of XIM is the same as that of the system. So, we can use
9051 Vlocale_coding_system which is initialized properly at Emacs
9052 startup time. */
9053 setup_coding_system (Vlocale_coding_system, &coding);
9054 coding.src_multibyte = 0;
9055 coding.dst_multibyte = 1;
9056 /* The input is converted to events, thus we can't handle
9057 composition. Anyway, there's no XIM that gives us composition
9058 information. */
9059 coding.composing = COMPOSITION_DISABLED;
9060
9061 /* Find the display we are supposed to read input for.
9062 It's the one communicating on descriptor SD. */
9063 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9064 {
9065 #if 0 /* This ought to be unnecessary; let's verify it. */
9066 #ifdef FIOSNBIO
9067 /* If available, Xlib uses FIOSNBIO to make the socket
9068 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
9069 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
9070 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
9071 fcntl (dpyinfo->connection, F_SETFL, 0);
9072 #endif /* ! defined (FIOSNBIO) */
9073 #endif
9074
9075 #if 0 /* This code can't be made to work, with multiple displays,
9076 and appears not to be used on any system any more.
9077 Also keyboard.c doesn't turn O_NDELAY on and off
9078 for X connections. */
9079 #ifndef SIGIO
9080 #ifndef HAVE_SELECT
9081 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9082 {
9083 extern int read_alarm_should_throw;
9084 read_alarm_should_throw = 1;
9085 XPeekEvent (dpyinfo->display, &event);
9086 read_alarm_should_throw = 0;
9087 }
9088 #endif /* HAVE_SELECT */
9089 #endif /* SIGIO */
9090 #endif
9091
9092 /* For debugging, this gives a way to fake an I/O error. */
9093 if (dpyinfo == XTread_socket_fake_io_error)
9094 {
9095 XTread_socket_fake_io_error = 0;
9096 x_io_error_quitter (dpyinfo->display);
9097 }
9098
9099 while (XPending (dpyinfo->display))
9100 {
9101 XNextEvent (dpyinfo->display, &event);
9102
9103 #ifdef HAVE_X_I18N
9104 {
9105 /* Filter events for the current X input method.
9106 XFilterEvent returns non-zero if the input method has
9107 consumed the event. We pass the frame's X window to
9108 XFilterEvent because that's the one for which the IC
9109 was created. */
9110 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9111 event.xclient.window);
9112 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
9113 break;
9114 }
9115 #endif
9116 event_found = 1;
9117
9118 switch (event.type)
9119 {
9120 case ClientMessage:
9121 {
9122 if (event.xclient.message_type
9123 == dpyinfo->Xatom_wm_protocols
9124 && event.xclient.format == 32)
9125 {
9126 if (event.xclient.data.l[0]
9127 == dpyinfo->Xatom_wm_take_focus)
9128 {
9129 /* Use x_any_window_to_frame because this
9130 could be the shell widget window
9131 if the frame has no title bar. */
9132 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
9133 #ifdef HAVE_X_I18N
9134 /* Not quite sure this is needed -pd */
9135 if (f && FRAME_XIC (f))
9136 XSetICFocus (FRAME_XIC (f));
9137 #endif
9138 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9139 instructs the WM to set the input focus automatically for
9140 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9141 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9142 it has set the focus. So, XSetInputFocus below is not
9143 needed.
9144
9145 The call to XSetInputFocus below has also caused trouble. In
9146 cases where the XSetInputFocus done by the WM and the one
9147 below are temporally close (on a fast machine), the call
9148 below can generate additional FocusIn events which confuse
9149 Emacs. */
9150
9151 /* Since we set WM_TAKE_FOCUS, we must call
9152 XSetInputFocus explicitly. But not if f is null,
9153 since that might be an event for a deleted frame. */
9154 if (f)
9155 {
9156 Display *d = event.xclient.display;
9157 /* Catch and ignore errors, in case window has been
9158 iconified by a window manager such as GWM. */
9159 int count = x_catch_errors (d);
9160 XSetInputFocus (d, event.xclient.window,
9161 /* The ICCCM says this is
9162 the only valid choice. */
9163 RevertToParent,
9164 event.xclient.data.l[1]);
9165 /* This is needed to detect the error
9166 if there is an error. */
9167 XSync (d, False);
9168 x_uncatch_errors (d, count);
9169 }
9170 /* Not certain about handling scroll bars here */
9171 #endif /* 0 */
9172 }
9173 else if (event.xclient.data.l[0]
9174 == dpyinfo->Xatom_wm_save_yourself)
9175 {
9176 /* Save state modify the WM_COMMAND property to
9177 something which can reinstate us. This notifies
9178 the session manager, who's looking for such a
9179 PropertyNotify. Can restart processing when
9180 a keyboard or mouse event arrives. */
9181 if (numchars > 0)
9182 {
9183 f = x_top_window_to_frame (dpyinfo,
9184 event.xclient.window);
9185
9186 /* This is just so we only give real data once
9187 for a single Emacs process. */
9188 if (f == SELECTED_FRAME ())
9189 XSetCommand (FRAME_X_DISPLAY (f),
9190 event.xclient.window,
9191 initial_argv, initial_argc);
9192 else if (f)
9193 XSetCommand (FRAME_X_DISPLAY (f),
9194 event.xclient.window,
9195 0, 0);
9196 }
9197 }
9198 else if (event.xclient.data.l[0]
9199 == dpyinfo->Xatom_wm_delete_window)
9200 {
9201 struct frame *f
9202 = x_any_window_to_frame (dpyinfo,
9203 event.xclient.window);
9204
9205 if (f)
9206 {
9207 if (numchars == 0)
9208 abort ();
9209
9210 bufp->kind = delete_window_event;
9211 XSETFRAME (bufp->frame_or_window, f);
9212 bufp->arg = Qnil;
9213 bufp++;
9214
9215 count += 1;
9216 numchars -= 1;
9217 }
9218 }
9219 }
9220 else if (event.xclient.message_type
9221 == dpyinfo->Xatom_wm_configure_denied)
9222 {
9223 }
9224 else if (event.xclient.message_type
9225 == dpyinfo->Xatom_wm_window_moved)
9226 {
9227 int new_x, new_y;
9228 struct frame *f
9229 = x_window_to_frame (dpyinfo, event.xclient.window);
9230
9231 new_x = event.xclient.data.s[0];
9232 new_y = event.xclient.data.s[1];
9233
9234 if (f)
9235 {
9236 f->output_data.x->left_pos = new_x;
9237 f->output_data.x->top_pos = new_y;
9238 }
9239 }
9240 #ifdef HACK_EDITRES
9241 else if (event.xclient.message_type
9242 == dpyinfo->Xatom_editres)
9243 {
9244 struct frame *f
9245 = x_any_window_to_frame (dpyinfo, event.xclient.window);
9246 _XEditResCheckMessages (f->output_data.x->widget, NULL,
9247 &event, NULL);
9248 }
9249 #endif /* HACK_EDITRES */
9250 else if ((event.xclient.message_type
9251 == dpyinfo->Xatom_DONE)
9252 || (event.xclient.message_type
9253 == dpyinfo->Xatom_PAGE))
9254 {
9255 /* Ghostview job completed. Kill it. We could
9256 reply with "Next" if we received "Page", but we
9257 currently never do because we are interested in
9258 images, only, which should have 1 page. */
9259 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9260 struct frame *f
9261 = x_window_to_frame (dpyinfo, event.xclient.window);
9262 x_kill_gs_process (pixmap, f);
9263 expose_frame (f, 0, 0, 0, 0);
9264 }
9265 #ifdef USE_TOOLKIT_SCROLL_BARS
9266 /* Scroll bar callbacks send a ClientMessage from which
9267 we construct an input_event. */
9268 else if (event.xclient.message_type
9269 == dpyinfo->Xatom_Scrollbar)
9270 {
9271 x_scroll_bar_to_input_event (&event, bufp);
9272 ++bufp, ++count, --numchars;
9273 goto out;
9274 }
9275 #endif /* USE_TOOLKIT_SCROLL_BARS */
9276 else
9277 goto OTHER;
9278 }
9279 break;
9280
9281 case SelectionNotify:
9282 #ifdef USE_X_TOOLKIT
9283 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
9284 goto OTHER;
9285 #endif /* not USE_X_TOOLKIT */
9286 x_handle_selection_notify (&event.xselection);
9287 break;
9288
9289 case SelectionClear: /* Someone has grabbed ownership. */
9290 #ifdef USE_X_TOOLKIT
9291 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
9292 goto OTHER;
9293 #endif /* USE_X_TOOLKIT */
9294 {
9295 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
9296
9297 if (numchars == 0)
9298 abort ();
9299
9300 bufp->kind = selection_clear_event;
9301 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9302 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9303 SELECTION_EVENT_TIME (bufp) = eventp->time;
9304 bufp->frame_or_window = Qnil;
9305 bufp->arg = Qnil;
9306 bufp++;
9307
9308 count += 1;
9309 numchars -= 1;
9310 }
9311 break;
9312
9313 case SelectionRequest: /* Someone wants our selection. */
9314 #ifdef USE_X_TOOLKIT
9315 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
9316 goto OTHER;
9317 #endif /* USE_X_TOOLKIT */
9318 if (x_queue_selection_requests)
9319 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
9320 &event);
9321 else
9322 {
9323 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
9324
9325 if (numchars == 0)
9326 abort ();
9327
9328 bufp->kind = selection_request_event;
9329 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9330 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9331 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9332 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9333 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9334 SELECTION_EVENT_TIME (bufp) = eventp->time;
9335 bufp->frame_or_window = Qnil;
9336 bufp->arg = Qnil;
9337 bufp++;
9338
9339 count += 1;
9340 numchars -= 1;
9341 }
9342 break;
9343
9344 case PropertyNotify:
9345 #ifdef USE_X_TOOLKIT
9346 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
9347 goto OTHER;
9348 #endif /* not USE_X_TOOLKIT */
9349 x_handle_property_notify (&event.xproperty);
9350 break;
9351
9352 case ReparentNotify:
9353 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
9354 if (f)
9355 {
9356 int x, y;
9357 f->output_data.x->parent_desc = event.xreparent.parent;
9358 x_real_positions (f, &x, &y);
9359 f->output_data.x->left_pos = x;
9360 f->output_data.x->top_pos = y;
9361 }
9362 break;
9363
9364 case Expose:
9365 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9366 if (f)
9367 {
9368 if (f->async_visible == 0)
9369 {
9370 f->async_visible = 1;
9371 f->async_iconified = 0;
9372 f->output_data.x->has_been_visible = 1;
9373 SET_FRAME_GARBAGED (f);
9374 }
9375 else
9376 expose_frame (x_window_to_frame (dpyinfo,
9377 event.xexpose.window),
9378 event.xexpose.x, event.xexpose.y,
9379 event.xexpose.width, event.xexpose.height);
9380 }
9381 else
9382 {
9383 #ifdef USE_TOOLKIT_SCROLL_BARS
9384 /* Dispatch event to the widget. */
9385 goto OTHER;
9386 #else /* not USE_TOOLKIT_SCROLL_BARS */
9387 struct scroll_bar *bar
9388 = x_window_to_scroll_bar (event.xexpose.window);
9389
9390 if (bar)
9391 x_scroll_bar_expose (bar, &event);
9392 #ifdef USE_X_TOOLKIT
9393 else
9394 goto OTHER;
9395 #endif /* USE_X_TOOLKIT */
9396 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9397 }
9398 break;
9399
9400 case GraphicsExpose: /* This occurs when an XCopyArea's
9401 source area was obscured or not
9402 available.*/
9403 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
9404 if (f)
9405 {
9406 expose_frame (f,
9407 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9408 event.xgraphicsexpose.width,
9409 event.xgraphicsexpose.height);
9410 }
9411 #ifdef USE_X_TOOLKIT
9412 else
9413 goto OTHER;
9414 #endif /* USE_X_TOOLKIT */
9415 break;
9416
9417 case NoExpose: /* This occurs when an XCopyArea's
9418 source area was completely
9419 available */
9420 break;
9421
9422 case UnmapNotify:
9423 /* Redo the mouse-highlight after the tooltip has gone. */
9424 if (event.xmap.window == tip_window)
9425 {
9426 tip_window = 0;
9427 redo_mouse_highlight ();
9428 }
9429
9430 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
9431 if (f) /* F may no longer exist if
9432 the frame was deleted. */
9433 {
9434 /* While a frame is unmapped, display generation is
9435 disabled; you don't want to spend time updating a
9436 display that won't ever be seen. */
9437 f->async_visible = 0;
9438 /* We can't distinguish, from the event, whether the window
9439 has become iconified or invisible. So assume, if it
9440 was previously visible, than now it is iconified.
9441 But x_make_frame_invisible clears both
9442 the visible flag and the iconified flag;
9443 and that way, we know the window is not iconified now. */
9444 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
9445 {
9446 f->async_iconified = 1;
9447
9448 bufp->kind = iconify_event;
9449 XSETFRAME (bufp->frame_or_window, f);
9450 bufp->arg = Qnil;
9451 bufp++;
9452 count++;
9453 numchars--;
9454 }
9455 }
9456 goto OTHER;
9457
9458 case MapNotify:
9459 if (event.xmap.window == tip_window)
9460 /* The tooltip has been drawn already. Avoid
9461 the SET_FRAME_GARBAGED below. */
9462 goto OTHER;
9463
9464 /* We use x_top_window_to_frame because map events can
9465 come for sub-windows and they don't mean that the
9466 frame is visible. */
9467 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
9468 if (f)
9469 {
9470 f->async_visible = 1;
9471 f->async_iconified = 0;
9472 f->output_data.x->has_been_visible = 1;
9473
9474 /* wait_reading_process_input will notice this and update
9475 the frame's display structures. */
9476 SET_FRAME_GARBAGED (f);
9477
9478 if (f->iconified)
9479 {
9480 bufp->kind = deiconify_event;
9481 XSETFRAME (bufp->frame_or_window, f);
9482 bufp->arg = Qnil;
9483 bufp++;
9484 count++;
9485 numchars--;
9486 }
9487 else if (! NILP (Vframe_list)
9488 && ! NILP (XCDR (Vframe_list)))
9489 /* Force a redisplay sooner or later
9490 to update the frame titles
9491 in case this is the second frame. */
9492 record_asynch_buffer_change ();
9493 }
9494 goto OTHER;
9495
9496 case KeyPress:
9497 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
9498
9499 #ifdef USE_MOTIF
9500 /* I couldn't find a way to prevent LessTif scroll bars
9501 from consuming key events. */
9502 if (f == 0)
9503 {
9504 Widget widget = XtWindowToWidget (dpyinfo->display,
9505 event.xkey.window);
9506 if (widget && XmIsScrollBar (widget))
9507 {
9508 widget = XtParent (widget);
9509 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9510 }
9511 }
9512 #endif /* USE_MOTIF */
9513
9514 if (f != 0)
9515 {
9516 KeySym keysym, orig_keysym;
9517 /* al%imercury@uunet.uu.net says that making this 81
9518 instead of 80 fixed a bug whereby meta chars made
9519 his Emacs hang.
9520
9521 It seems that some version of XmbLookupString has
9522 a bug of not returning XBufferOverflow in
9523 status_return even if the input is too long to
9524 fit in 81 bytes. So, we must prepare sufficient
9525 bytes for copy_buffer. 513 bytes (256 chars for
9526 two-byte character set) seems to be a faily good
9527 approximation. -- 2000.8.10 handa@etl.go.jp */
9528 unsigned char copy_buffer[513];
9529 unsigned char *copy_bufptr = copy_buffer;
9530 int copy_bufsiz = sizeof (copy_buffer);
9531 int modifiers;
9532
9533 event.xkey.state
9534 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9535 extra_keyboard_modifiers);
9536 modifiers = event.xkey.state;
9537
9538 /* This will have to go some day... */
9539
9540 /* make_lispy_event turns chars into control chars.
9541 Don't do it here because XLookupString is too eager. */
9542 event.xkey.state &= ~ControlMask;
9543 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9544 | dpyinfo->super_mod_mask
9545 | dpyinfo->hyper_mod_mask
9546 | dpyinfo->alt_mod_mask);
9547
9548 /* In case Meta is ComposeCharacter,
9549 clear its status. According to Markus Ehrnsperger
9550 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9551 this enables ComposeCharacter to work whether or
9552 not it is combined with Meta. */
9553 if (modifiers & dpyinfo->meta_mod_mask)
9554 bzero (&compose_status, sizeof (compose_status));
9555
9556 #ifdef HAVE_X_I18N
9557 if (FRAME_XIC (f))
9558 {
9559 Status status_return;
9560
9561 nbytes = XmbLookupString (FRAME_XIC (f),
9562 &event.xkey, copy_bufptr,
9563 copy_bufsiz, &keysym,
9564 &status_return);
9565 if (status_return == XBufferOverflow)
9566 {
9567 copy_bufsiz = nbytes + 1;
9568 copy_bufptr = (char *) alloca (copy_bufsiz);
9569 nbytes = XmbLookupString (FRAME_XIC (f),
9570 &event.xkey, copy_bufptr,
9571 copy_bufsiz, &keysym,
9572 &status_return);
9573 }
9574
9575 if (status_return == XLookupNone)
9576 break;
9577 else if (status_return == XLookupChars)
9578 {
9579 keysym = NoSymbol;
9580 modifiers = 0;
9581 }
9582 else if (status_return != XLookupKeySym
9583 && status_return != XLookupBoth)
9584 abort ();
9585 }
9586 else
9587 nbytes = XLookupString (&event.xkey, copy_bufptr,
9588 copy_bufsiz, &keysym,
9589 &compose_status);
9590 #else
9591 nbytes = XLookupString (&event.xkey, copy_bufptr,
9592 copy_bufsiz, &keysym,
9593 &compose_status);
9594 #endif
9595
9596 orig_keysym = keysym;
9597
9598 if (numchars > 1)
9599 {
9600 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9601 || keysym == XK_Delete
9602 #ifdef XK_ISO_Left_Tab
9603 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
9604 #endif
9605 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
9606 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9607 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
9608 #ifdef HPUX
9609 /* This recognizes the "extended function keys".
9610 It seems there's no cleaner way.
9611 Test IsModifierKey to avoid handling mode_switch
9612 incorrectly. */
9613 || ((unsigned) (keysym) >= XK_Select
9614 && (unsigned)(keysym) < XK_KP_Space)
9615 #endif
9616 #ifdef XK_dead_circumflex
9617 || orig_keysym == XK_dead_circumflex
9618 #endif
9619 #ifdef XK_dead_grave
9620 || orig_keysym == XK_dead_grave
9621 #endif
9622 #ifdef XK_dead_tilde
9623 || orig_keysym == XK_dead_tilde
9624 #endif
9625 #ifdef XK_dead_diaeresis
9626 || orig_keysym == XK_dead_diaeresis
9627 #endif
9628 #ifdef XK_dead_macron
9629 || orig_keysym == XK_dead_macron
9630 #endif
9631 #ifdef XK_dead_degree
9632 || orig_keysym == XK_dead_degree
9633 #endif
9634 #ifdef XK_dead_acute
9635 || orig_keysym == XK_dead_acute
9636 #endif
9637 #ifdef XK_dead_cedilla
9638 || orig_keysym == XK_dead_cedilla
9639 #endif
9640 #ifdef XK_dead_breve
9641 || orig_keysym == XK_dead_breve
9642 #endif
9643 #ifdef XK_dead_ogonek
9644 || orig_keysym == XK_dead_ogonek
9645 #endif
9646 #ifdef XK_dead_caron
9647 || orig_keysym == XK_dead_caron
9648 #endif
9649 #ifdef XK_dead_doubleacute
9650 || orig_keysym == XK_dead_doubleacute
9651 #endif
9652 #ifdef XK_dead_abovedot
9653 || orig_keysym == XK_dead_abovedot
9654 #endif
9655 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9656 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9657 /* Any "vendor-specific" key is ok. */
9658 || (orig_keysym & (1 << 28)))
9659 && ! (IsModifierKey (orig_keysym)
9660 #ifndef HAVE_X11R5
9661 #ifdef XK_Mode_switch
9662 || ((unsigned)(orig_keysym) == XK_Mode_switch)
9663 #endif
9664 #ifdef XK_Num_Lock
9665 || ((unsigned)(orig_keysym) == XK_Num_Lock)
9666 #endif
9667 #endif /* not HAVE_X11R5 */
9668 ))
9669 {
9670 if (temp_index == sizeof temp_buffer / sizeof (short))
9671 temp_index = 0;
9672 temp_buffer[temp_index++] = keysym;
9673 bufp->kind = non_ascii_keystroke;
9674 bufp->code = keysym;
9675 XSETFRAME (bufp->frame_or_window, f);
9676 bufp->arg = Qnil;
9677 bufp->modifiers
9678 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9679 modifiers);
9680 bufp->timestamp = event.xkey.time;
9681 bufp++;
9682 count++;
9683 numchars--;
9684 }
9685 else if (numchars > nbytes)
9686 {
9687 register int i;
9688 register int c;
9689 unsigned char *p, *pend;
9690 int nchars, len;
9691
9692 for (i = 0; i < nbytes; i++)
9693 {
9694 if (temp_index == (sizeof temp_buffer
9695 / sizeof (short)))
9696 temp_index = 0;
9697 temp_buffer[temp_index++] = copy_bufptr[i];
9698 }
9699
9700 if (/* If the event is not from XIM, */
9701 event.xkey.keycode != 0
9702 /* or the current locale doesn't request
9703 decoding of the intup data, ... */
9704 || coding.type == coding_type_raw_text
9705 || coding.type == coding_type_no_conversion)
9706 {
9707 /* ... we can use the input data as is. */
9708 nchars = nbytes;
9709 }
9710 else
9711 {
9712 /* We have to decode the input data. */
9713 int require;
9714 unsigned char *p;
9715
9716 require = decoding_buffer_size (&coding, nbytes);
9717 p = (unsigned char *) alloca (require);
9718 coding.mode |= CODING_MODE_LAST_BLOCK;
9719 decode_coding (&coding, copy_bufptr, p,
9720 nbytes, require);
9721 nbytes = coding.produced;
9722 nchars = coding.produced_char;
9723 copy_bufptr = p;
9724 }
9725
9726 /* Convert the input data to a sequence of
9727 character events. */
9728 for (i = 0; i < nbytes; i += len)
9729 {
9730 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
9731 nbytes - i, len);
9732 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
9733 ? ascii_keystroke
9734 : multibyte_char_keystroke);
9735 bufp->code = c;
9736 XSETFRAME (bufp->frame_or_window, f);
9737 bufp->arg = Qnil;
9738 bufp->modifiers
9739 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9740 modifiers);
9741 bufp->timestamp = event.xkey.time;
9742 bufp++;
9743 }
9744
9745 count += nchars;
9746 numchars -= nchars;
9747
9748 if (keysym == NoSymbol)
9749 break;
9750 }
9751 else
9752 abort ();
9753 }
9754 else
9755 abort ();
9756 }
9757 #ifdef HAVE_X_I18N
9758 /* Don't dispatch this event since XtDispatchEvent calls
9759 XFilterEvent, and two calls in a row may freeze the
9760 client. */
9761 break;
9762 #else
9763 goto OTHER;
9764 #endif
9765
9766 case KeyRelease:
9767 #ifdef HAVE_X_I18N
9768 /* Don't dispatch this event since XtDispatchEvent calls
9769 XFilterEvent, and two calls in a row may freeze the
9770 client. */
9771 break;
9772 #else
9773 goto OTHER;
9774 #endif
9775
9776 /* Here's a possible interpretation of the whole
9777 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9778 you get a FocusIn event, you have to get a FocusOut
9779 event before you relinquish the focus. If you
9780 haven't received a FocusIn event, then a mere
9781 LeaveNotify is enough to free you. */
9782
9783 case EnterNotify:
9784 {
9785 int from_menu_bar_p = 0;
9786
9787 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
9788
9789 #ifdef LESSTIF_VERSION
9790 /* When clicking outside of a menu bar popup to close
9791 it, we get a FocusIn/ EnterNotify sequence of
9792 events. The flag event.xcrossing.focus is not set
9793 in the EnterNotify event of that sequence because
9794 the focus is in the menu bar,
9795 event.xcrossing.window is the frame's X window.
9796 Unconditionally setting the focus frame to null in
9797 this case is not the right thing, because no event
9798 follows that could set the focus frame to the right
9799 value.
9800
9801 This could be a LessTif bug, but I wasn't able to
9802 reproduce the behavior in a simple test program.
9803
9804 (gerd, LessTif 0.88.1). */
9805
9806 if (!event.xcrossing.focus
9807 && f
9808 && f->output_data.x->menubar_widget)
9809 {
9810 Window focus;
9811 int revert;
9812
9813 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9814 if (focus == XtWindow (f->output_data.x->menubar_widget))
9815 from_menu_bar_p = 1;
9816 }
9817 #endif /* LESSTIF_VERSION */
9818
9819 if (event.xcrossing.focus || from_menu_bar_p)
9820 {
9821 /* Avoid nasty pop/raise loops. */
9822 if (f && (!(f->auto_raise)
9823 || !(f->auto_lower)
9824 || (event.xcrossing.time - enter_timestamp) > 500))
9825 {
9826 x_new_focus_frame (dpyinfo, f);
9827 enter_timestamp = event.xcrossing.time;
9828 }
9829 }
9830 else if (f == dpyinfo->x_focus_frame)
9831 x_new_focus_frame (dpyinfo, 0);
9832
9833 /* EnterNotify counts as mouse movement,
9834 so update things that depend on mouse position. */
9835 if (f && !f->output_data.x->busy_p)
9836 note_mouse_movement (f, &event.xmotion);
9837 goto OTHER;
9838 }
9839
9840 case FocusIn:
9841 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
9842 if (event.xfocus.detail != NotifyPointer)
9843 dpyinfo->x_focus_event_frame = f;
9844 if (f)
9845 {
9846 x_new_focus_frame (dpyinfo, f);
9847
9848 /* Don't stop displaying the initial startup message
9849 for a switch-frame event we don't need. */
9850 if (GC_NILP (Vterminal_frame)
9851 && GC_CONSP (Vframe_list)
9852 && !GC_NILP (XCDR (Vframe_list)))
9853 {
9854 bufp->kind = FOCUS_IN_EVENT;
9855 XSETFRAME (bufp->frame_or_window, f);
9856 bufp->arg = Qnil;
9857 ++bufp, ++count, --numchars;
9858 }
9859 }
9860
9861 #ifdef HAVE_X_I18N
9862 if (f && FRAME_XIC (f))
9863 XSetICFocus (FRAME_XIC (f));
9864 #endif
9865
9866 goto OTHER;
9867
9868 case LeaveNotify:
9869 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
9870 if (f)
9871 {
9872 Lisp_Object frame;
9873 int from_menu_bar_p = 0;
9874
9875 if (f == dpyinfo->mouse_face_mouse_frame)
9876 {
9877 /* If we move outside the frame, then we're
9878 certainly no longer on any text in the frame. */
9879 clear_mouse_face (dpyinfo);
9880 dpyinfo->mouse_face_mouse_frame = 0;
9881 }
9882
9883 /* Generate a nil HELP_EVENT to cancel a help-echo.
9884 Do it only if there's something to cancel.
9885 Otherwise, the startup message is cleared when
9886 the mouse leaves the frame. */
9887 if (any_help_event_p)
9888 {
9889 Lisp_Object frame;
9890 int n;
9891
9892 XSETFRAME (frame, f);
9893 n = gen_help_event (bufp, numchars,
9894 Qnil, frame, Qnil, Qnil, 0);
9895 bufp += n, count += n, numchars -= n;
9896 }
9897
9898 #ifdef LESSTIF_VERSION
9899 /* Please see the comment at the start of the
9900 EnterNotify case. */
9901 if (!event.xcrossing.focus
9902 && f->output_data.x->menubar_widget)
9903 {
9904 Window focus;
9905 int revert;
9906 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9907 if (focus == XtWindow (f->output_data.x->menubar_widget))
9908 from_menu_bar_p = 1;
9909 }
9910 #endif /* LESSTIF_VERSION */
9911
9912 if (event.xcrossing.focus || from_menu_bar_p)
9913 x_mouse_leave (dpyinfo);
9914 else
9915 {
9916 if (f == dpyinfo->x_focus_event_frame)
9917 dpyinfo->x_focus_event_frame = 0;
9918 if (f == dpyinfo->x_focus_frame)
9919 x_new_focus_frame (dpyinfo, 0);
9920 }
9921 }
9922 goto OTHER;
9923
9924 case FocusOut:
9925 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
9926 if (event.xfocus.detail != NotifyPointer
9927 && f == dpyinfo->x_focus_event_frame)
9928 dpyinfo->x_focus_event_frame = 0;
9929 if (f && f == dpyinfo->x_focus_frame)
9930 x_new_focus_frame (dpyinfo, 0);
9931
9932 #ifdef HAVE_X_I18N
9933 if (f && FRAME_XIC (f))
9934 XUnsetICFocus (FRAME_XIC (f));
9935 #endif
9936
9937 goto OTHER;
9938
9939 case MotionNotify:
9940 {
9941 previous_help_echo = help_echo;
9942 help_echo = help_echo_object = help_echo_window = Qnil;
9943 help_echo_pos = -1;
9944
9945 if (dpyinfo->grabbed && last_mouse_frame
9946 && FRAME_LIVE_P (last_mouse_frame))
9947 f = last_mouse_frame;
9948 else
9949 f = x_window_to_frame (dpyinfo, event.xmotion.window);
9950
9951 if (f)
9952 note_mouse_movement (f, &event.xmotion);
9953 else
9954 {
9955 #ifndef USE_TOOLKIT_SCROLL_BARS
9956 struct scroll_bar *bar
9957 = x_window_to_scroll_bar (event.xmotion.window);
9958
9959 if (bar)
9960 x_scroll_bar_note_movement (bar, &event);
9961 #endif /* USE_TOOLKIT_SCROLL_BARS */
9962
9963 /* If we move outside the frame, then we're
9964 certainly no longer on any text in the frame. */
9965 clear_mouse_face (dpyinfo);
9966 }
9967
9968 /* If the contents of the global variable help_echo
9969 has changed, generate a HELP_EVENT. */
9970 if (!NILP (help_echo)
9971 || !NILP (previous_help_echo))
9972 {
9973 Lisp_Object frame;
9974 int n;
9975
9976 if (f)
9977 XSETFRAME (frame, f);
9978 else
9979 frame = Qnil;
9980
9981 any_help_event_p = 1;
9982 n = gen_help_event (bufp, numchars, help_echo, frame,
9983 help_echo_window, help_echo_object,
9984 help_echo_pos);
9985 bufp += n, count += n, numchars -= n;
9986 }
9987
9988 goto OTHER;
9989 }
9990
9991 case ConfigureNotify:
9992 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
9993 if (f)
9994 {
9995 #ifndef USE_X_TOOLKIT
9996 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
9997 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
9998
9999 /* In the toolkit version, change_frame_size
10000 is called by the code that handles resizing
10001 of the EmacsFrame widget. */
10002
10003 /* Even if the number of character rows and columns has
10004 not changed, the font size may have changed, so we need
10005 to check the pixel dimensions as well. */
10006 if (columns != f->width
10007 || rows != f->height
10008 || event.xconfigure.width != f->output_data.x->pixel_width
10009 || event.xconfigure.height != f->output_data.x->pixel_height)
10010 {
10011 change_frame_size (f, rows, columns, 0, 1, 0);
10012 SET_FRAME_GARBAGED (f);
10013 cancel_mouse_face (f);
10014 }
10015 #endif
10016
10017 f->output_data.x->pixel_width = event.xconfigure.width;
10018 f->output_data.x->pixel_height = event.xconfigure.height;
10019
10020 /* What we have now is the position of Emacs's own window.
10021 Convert that to the position of the window manager window. */
10022 x_real_positions (f, &f->output_data.x->left_pos,
10023 &f->output_data.x->top_pos);
10024
10025 #ifdef HAVE_X_I18N
10026 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10027 xic_set_statusarea (f);
10028 #endif
10029
10030 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10031 {
10032 /* Since the WM decorations come below top_pos now,
10033 we must put them below top_pos in the future. */
10034 f->output_data.x->win_gravity = NorthWestGravity;
10035 x_wm_set_size_hint (f, (long) 0, 0);
10036 }
10037 #ifdef USE_MOTIF
10038 /* Some window managers pass (0,0) as the location of
10039 the window, and the Motif event handler stores it
10040 in the emacs widget, which messes up Motif menus. */
10041 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10042 {
10043 event.xconfigure.x = f->output_data.x->widget->core.x;
10044 event.xconfigure.y = f->output_data.x->widget->core.y;
10045 }
10046 #endif /* USE_MOTIF */
10047 }
10048 goto OTHER;
10049
10050 case ButtonPress:
10051 case ButtonRelease:
10052 {
10053 /* If we decide we want to generate an event to be seen
10054 by the rest of Emacs, we put it here. */
10055 struct input_event emacs_event;
10056 int tool_bar_p = 0;
10057
10058 emacs_event.kind = no_event;
10059 bzero (&compose_status, sizeof (compose_status));
10060
10061 if (dpyinfo->grabbed
10062 && last_mouse_frame
10063 && FRAME_LIVE_P (last_mouse_frame))
10064 f = last_mouse_frame;
10065 else
10066 f = x_window_to_frame (dpyinfo, event.xbutton.window);
10067
10068 if (f)
10069 {
10070 /* Is this in the tool-bar? */
10071 if (WINDOWP (f->tool_bar_window)
10072 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
10073 {
10074 Lisp_Object window;
10075 int p, x, y;
10076
10077 x = event.xbutton.x;
10078 y = event.xbutton.y;
10079
10080 /* Set x and y. */
10081 window = window_from_coordinates (f, x, y, &p, 1);
10082 if (EQ (window, f->tool_bar_window))
10083 {
10084 x_handle_tool_bar_click (f, &event.xbutton);
10085 tool_bar_p = 1;
10086 }
10087 }
10088
10089 if (!tool_bar_p)
10090 if (!dpyinfo->x_focus_frame
10091 || f == dpyinfo->x_focus_frame)
10092 construct_mouse_click (&emacs_event, &event, f);
10093 }
10094 else
10095 {
10096 #ifndef USE_TOOLKIT_SCROLL_BARS
10097 struct scroll_bar *bar
10098 = x_window_to_scroll_bar (event.xbutton.window);
10099
10100 if (bar)
10101 x_scroll_bar_handle_click (bar, &event, &emacs_event);
10102 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10103 }
10104
10105 if (event.type == ButtonPress)
10106 {
10107 dpyinfo->grabbed |= (1 << event.xbutton.button);
10108 last_mouse_frame = f;
10109 /* Ignore any mouse motion that happened
10110 before this event; any subsequent mouse-movement
10111 Emacs events should reflect only motion after
10112 the ButtonPress. */
10113 if (f != 0)
10114 f->mouse_moved = 0;
10115
10116 if (!tool_bar_p)
10117 last_tool_bar_item = -1;
10118 }
10119 else
10120 {
10121 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
10122 }
10123
10124 if (numchars >= 1 && emacs_event.kind != no_event)
10125 {
10126 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10127 bufp++;
10128 count++;
10129 numchars--;
10130 }
10131
10132 #ifdef USE_X_TOOLKIT
10133 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10134 /* For a down-event in the menu bar,
10135 don't pass it to Xt right now.
10136 Instead, save it away
10137 and we will pass it to Xt from kbd_buffer_get_event.
10138 That way, we can run some Lisp code first. */
10139 if (f && event.type == ButtonPress
10140 /* Verify the event is really within the menu bar
10141 and not just sent to it due to grabbing. */
10142 && event.xbutton.x >= 0
10143 && event.xbutton.x < f->output_data.x->pixel_width
10144 && event.xbutton.y >= 0
10145 && event.xbutton.y < f->output_data.x->menubar_height
10146 && event.xbutton.same_screen)
10147 {
10148 SET_SAVED_BUTTON_EVENT;
10149 XSETFRAME (last_mouse_press_frame, f);
10150 }
10151 else if (event.type == ButtonPress)
10152 {
10153 last_mouse_press_frame = Qnil;
10154 goto OTHER;
10155 }
10156
10157 #ifdef USE_MOTIF /* This should do not harm for Lucid,
10158 but I am trying to be cautious. */
10159 else if (event.type == ButtonRelease)
10160 {
10161 if (!NILP (last_mouse_press_frame))
10162 {
10163 f = XFRAME (last_mouse_press_frame);
10164 if (f->output_data.x)
10165 SET_SAVED_BUTTON_EVENT;
10166 }
10167 else
10168 goto OTHER;
10169 }
10170 #endif /* USE_MOTIF */
10171 else
10172 goto OTHER;
10173 #endif /* USE_X_TOOLKIT */
10174 }
10175 break;
10176
10177 case CirculateNotify:
10178 goto OTHER;
10179
10180 case CirculateRequest:
10181 goto OTHER;
10182
10183 case VisibilityNotify:
10184 goto OTHER;
10185
10186 case MappingNotify:
10187 /* Someone has changed the keyboard mapping - update the
10188 local cache. */
10189 switch (event.xmapping.request)
10190 {
10191 case MappingModifier:
10192 x_find_modifier_meanings (dpyinfo);
10193 /* This is meant to fall through. */
10194 case MappingKeyboard:
10195 XRefreshKeyboardMapping (&event.xmapping);
10196 }
10197 goto OTHER;
10198
10199 default:
10200 OTHER:
10201 #ifdef USE_X_TOOLKIT
10202 BLOCK_INPUT;
10203 XtDispatchEvent (&event);
10204 UNBLOCK_INPUT;
10205 #endif /* USE_X_TOOLKIT */
10206 break;
10207 }
10208 }
10209 }
10210
10211 out:;
10212
10213 /* On some systems, an X bug causes Emacs to get no more events
10214 when the window is destroyed. Detect that. (1994.) */
10215 if (! event_found)
10216 {
10217 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10218 One XNOOP in 100 loops will make Emacs terminate.
10219 B. Bretthauer, 1994 */
10220 x_noop_count++;
10221 if (x_noop_count >= 100)
10222 {
10223 x_noop_count=0;
10224
10225 if (next_noop_dpyinfo == 0)
10226 next_noop_dpyinfo = x_display_list;
10227
10228 XNoOp (next_noop_dpyinfo->display);
10229
10230 /* Each time we get here, cycle through the displays now open. */
10231 next_noop_dpyinfo = next_noop_dpyinfo->next;
10232 }
10233 }
10234
10235 /* If the focus was just given to an auto-raising frame,
10236 raise it now. */
10237 /* ??? This ought to be able to handle more than one such frame. */
10238 if (pending_autoraise_frame)
10239 {
10240 x_raise_frame (pending_autoraise_frame);
10241 pending_autoraise_frame = 0;
10242 }
10243
10244 UNBLOCK_INPUT;
10245 --handling_signal;
10246 return count;
10247 }
10248
10249
10250
10251 \f
10252 /***********************************************************************
10253 Text Cursor
10254 ***********************************************************************/
10255
10256 /* Note if the text cursor of window W has been overwritten by a
10257 drawing operation that outputs N glyphs starting at HPOS in the
10258 line given by output_cursor.vpos. N < 0 means all the rest of the
10259 line after HPOS has been written. */
10260
10261 static void
10262 note_overwritten_text_cursor (w, hpos, n)
10263 struct window *w;
10264 int hpos, n;
10265 {
10266 if (updated_area == TEXT_AREA
10267 && output_cursor.vpos == w->phys_cursor.vpos
10268 && hpos <= w->phys_cursor.hpos
10269 && (n < 0
10270 || hpos + n > w->phys_cursor.hpos))
10271 w->phys_cursor_on_p = 0;
10272 }
10273
10274
10275 /* Set clipping for output in glyph row ROW. W is the window in which
10276 we operate. GC is the graphics context to set clipping in.
10277 WHOLE_LINE_P non-zero means include the areas used for truncation
10278 mark display and alike in the clipping rectangle.
10279
10280 ROW may be a text row or, e.g., a mode line. Text rows must be
10281 clipped to the interior of the window dedicated to text display,
10282 mode lines must be clipped to the whole window. */
10283
10284 static void
10285 x_clip_to_row (w, row, gc, whole_line_p)
10286 struct window *w;
10287 struct glyph_row *row;
10288 GC gc;
10289 int whole_line_p;
10290 {
10291 struct frame *f = XFRAME (WINDOW_FRAME (w));
10292 XRectangle clip_rect;
10293 int window_x, window_y, window_width, window_height;
10294
10295 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
10296
10297 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10298 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10299 clip_rect.y = max (clip_rect.y, window_y);
10300 clip_rect.width = window_width;
10301 clip_rect.height = row->visible_height;
10302
10303 /* If clipping to the whole line, including trunc marks, extend
10304 the rectangle to the left and increase its width. */
10305 if (whole_line_p)
10306 {
10307 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10308 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
10309 }
10310
10311 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
10312 }
10313
10314
10315 /* Draw a hollow box cursor on window W in glyph row ROW. */
10316
10317 static void
10318 x_draw_hollow_cursor (w, row)
10319 struct window *w;
10320 struct glyph_row *row;
10321 {
10322 struct frame *f = XFRAME (WINDOW_FRAME (w));
10323 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10324 Display *dpy = FRAME_X_DISPLAY (f);
10325 int x, y, wd, h;
10326 XGCValues xgcv;
10327 struct glyph *cursor_glyph;
10328 GC gc;
10329
10330 /* Compute frame-relative coordinates from window-relative
10331 coordinates. */
10332 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10333 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10334 + row->ascent - w->phys_cursor_ascent);
10335 h = row->height - 1;
10336
10337 /* Get the glyph the cursor is on. If we can't tell because
10338 the current matrix is invalid or such, give up. */
10339 cursor_glyph = get_phys_cursor_glyph (w);
10340 if (cursor_glyph == NULL)
10341 return;
10342
10343 /* Compute the width of the rectangle to draw. If on a stretch
10344 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10345 rectangle as wide as the glyph, but use a canonical character
10346 width instead. */
10347 wd = cursor_glyph->pixel_width - 1;
10348 if (cursor_glyph->type == STRETCH_GLYPH
10349 && !x_stretch_cursor_p)
10350 wd = min (CANON_X_UNIT (f), wd);
10351
10352 /* The foreground of cursor_gc is typically the same as the normal
10353 background color, which can cause the cursor box to be invisible. */
10354 xgcv.foreground = f->output_data.x->cursor_pixel;
10355 if (dpyinfo->scratch_cursor_gc)
10356 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10357 else
10358 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10359 GCForeground, &xgcv);
10360 gc = dpyinfo->scratch_cursor_gc;
10361
10362 /* Set clipping, draw the rectangle, and reset clipping again. */
10363 x_clip_to_row (w, row, gc, 0);
10364 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10365 XSetClipMask (dpy, gc, None);
10366 }
10367
10368
10369 /* Draw a bar cursor on window W in glyph row ROW.
10370
10371 Implementation note: One would like to draw a bar cursor with an
10372 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10373 Unfortunately, I didn't find a font yet that has this property set.
10374 --gerd. */
10375
10376 static void
10377 x_draw_bar_cursor (w, row, width)
10378 struct window *w;
10379 struct glyph_row *row;
10380 int width;
10381 {
10382 struct frame *f = XFRAME (w->frame);
10383 struct glyph *cursor_glyph;
10384 GC gc;
10385 int x;
10386 unsigned long mask;
10387 XGCValues xgcv;
10388 Display *dpy;
10389 Window window;
10390
10391 /* If cursor is out of bounds, don't draw garbage. This can happen
10392 in mini-buffer windows when switching between echo area glyphs
10393 and mini-buffer. */
10394 cursor_glyph = get_phys_cursor_glyph (w);
10395 if (cursor_glyph == NULL)
10396 return;
10397
10398 /* If on an image, draw like a normal cursor. That's usually better
10399 visible than drawing a bar, esp. if the image is large so that
10400 the bar might not be in the window. */
10401 if (cursor_glyph->type == IMAGE_GLYPH)
10402 {
10403 struct glyph_row *row;
10404 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
10405 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
10406 }
10407 else
10408 {
10409 xgcv.background = f->output_data.x->cursor_pixel;
10410 xgcv.foreground = f->output_data.x->cursor_pixel;
10411 xgcv.graphics_exposures = 0;
10412 mask = GCForeground | GCBackground | GCGraphicsExposures;
10413 dpy = FRAME_X_DISPLAY (f);
10414 window = FRAME_X_WINDOW (f);
10415 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
10416
10417 if (gc)
10418 XChangeGC (dpy, gc, mask, &xgcv);
10419 else
10420 {
10421 gc = XCreateGC (dpy, window, mask, &xgcv);
10422 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10423 }
10424
10425 if (width < 0)
10426 width = f->output_data.x->cursor_width;
10427
10428 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10429 x_clip_to_row (w, row, gc, 0);
10430 XFillRectangle (dpy, window, gc,
10431 x,
10432 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
10433 min (cursor_glyph->pixel_width, width),
10434 row->height);
10435 XSetClipMask (dpy, gc, None);
10436 }
10437 }
10438
10439
10440 /* Clear the cursor of window W to background color, and mark the
10441 cursor as not shown. This is used when the text where the cursor
10442 is is about to be rewritten. */
10443
10444 static void
10445 x_clear_cursor (w)
10446 struct window *w;
10447 {
10448 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10449 x_update_window_cursor (w, 0);
10450 }
10451
10452
10453 /* Draw the cursor glyph of window W in glyph row ROW. See the
10454 comment of x_draw_glyphs for the meaning of HL. */
10455
10456 static void
10457 x_draw_phys_cursor_glyph (w, row, hl)
10458 struct window *w;
10459 struct glyph_row *row;
10460 enum draw_glyphs_face hl;
10461 {
10462 /* If cursor hpos is out of bounds, don't draw garbage. This can
10463 happen in mini-buffer windows when switching between echo area
10464 glyphs and mini-buffer. */
10465 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10466 {
10467 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10468 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10469 hl, 0, 0, 0);
10470
10471 /* When we erase the cursor, and ROW is overlapped by other
10472 rows, make sure that these overlapping parts of other rows
10473 are redrawn. */
10474 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10475 {
10476 if (row > w->current_matrix->rows
10477 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10478 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10479
10480 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10481 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10482 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10483 }
10484 }
10485 }
10486
10487
10488 /* Erase the image of a cursor of window W from the screen. */
10489
10490 static void
10491 x_erase_phys_cursor (w)
10492 struct window *w;
10493 {
10494 struct frame *f = XFRAME (w->frame);
10495 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10496 int hpos = w->phys_cursor.hpos;
10497 int vpos = w->phys_cursor.vpos;
10498 int mouse_face_here_p = 0;
10499 struct glyph_matrix *active_glyphs = w->current_matrix;
10500 struct glyph_row *cursor_row;
10501 struct glyph *cursor_glyph;
10502 enum draw_glyphs_face hl;
10503
10504 /* No cursor displayed or row invalidated => nothing to do on the
10505 screen. */
10506 if (w->phys_cursor_type == NO_CURSOR)
10507 goto mark_cursor_off;
10508
10509 /* VPOS >= active_glyphs->nrows means that window has been resized.
10510 Don't bother to erase the cursor. */
10511 if (vpos >= active_glyphs->nrows)
10512 goto mark_cursor_off;
10513
10514 /* If row containing cursor is marked invalid, there is nothing we
10515 can do. */
10516 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10517 if (!cursor_row->enabled_p)
10518 goto mark_cursor_off;
10519
10520 /* This can happen when the new row is shorter than the old one.
10521 In this case, either x_draw_glyphs or clear_end_of_line
10522 should have cleared the cursor. Note that we wouldn't be
10523 able to erase the cursor in this case because we don't have a
10524 cursor glyph at hand. */
10525 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10526 goto mark_cursor_off;
10527
10528 /* If the cursor is in the mouse face area, redisplay that when
10529 we clear the cursor. */
10530 if (! NILP (dpyinfo->mouse_face_window)
10531 && w == XWINDOW (dpyinfo->mouse_face_window)
10532 && (vpos > dpyinfo->mouse_face_beg_row
10533 || (vpos == dpyinfo->mouse_face_beg_row
10534 && hpos >= dpyinfo->mouse_face_beg_col))
10535 && (vpos < dpyinfo->mouse_face_end_row
10536 || (vpos == dpyinfo->mouse_face_end_row
10537 && hpos < dpyinfo->mouse_face_end_col))
10538 /* Don't redraw the cursor's spot in mouse face if it is at the
10539 end of a line (on a newline). The cursor appears there, but
10540 mouse highlighting does not. */
10541 && cursor_row->used[TEXT_AREA] > hpos)
10542 mouse_face_here_p = 1;
10543
10544 /* Maybe clear the display under the cursor. */
10545 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10546 {
10547 int x;
10548 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10549
10550 cursor_glyph = get_phys_cursor_glyph (w);
10551 if (cursor_glyph == NULL)
10552 goto mark_cursor_off;
10553
10554 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10555
10556 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10557 x,
10558 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
10559 cursor_row->y)),
10560 cursor_glyph->pixel_width,
10561 cursor_row->visible_height,
10562 False);
10563 }
10564
10565 /* Erase the cursor by redrawing the character underneath it. */
10566 if (mouse_face_here_p)
10567 hl = DRAW_MOUSE_FACE;
10568 else if (cursor_row->inverse_p)
10569 hl = DRAW_INVERSE_VIDEO;
10570 else
10571 hl = DRAW_NORMAL_TEXT;
10572 x_draw_phys_cursor_glyph (w, cursor_row, hl);
10573
10574 mark_cursor_off:
10575 w->phys_cursor_on_p = 0;
10576 w->phys_cursor_type = NO_CURSOR;
10577 }
10578
10579
10580 /* Display or clear cursor of window W. If ON is zero, clear the
10581 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10582 where to put the cursor is specified by HPOS, VPOS, X and Y. */
10583
10584 void
10585 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10586 struct window *w;
10587 int on, hpos, vpos, x, y;
10588 {
10589 struct frame *f = XFRAME (w->frame);
10590 int new_cursor_type;
10591 int new_cursor_width;
10592 struct glyph_matrix *current_glyphs;
10593 struct glyph_row *glyph_row;
10594 struct glyph *glyph;
10595
10596 /* This is pointless on invisible frames, and dangerous on garbaged
10597 windows and frames; in the latter case, the frame or window may
10598 be in the midst of changing its size, and x and y may be off the
10599 window. */
10600 if (! FRAME_VISIBLE_P (f)
10601 || FRAME_GARBAGED_P (f)
10602 || vpos >= w->current_matrix->nrows
10603 || hpos >= w->current_matrix->matrix_w)
10604 return;
10605
10606 /* If cursor is off and we want it off, return quickly. */
10607 if (!on && !w->phys_cursor_on_p)
10608 return;
10609
10610 current_glyphs = w->current_matrix;
10611 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10612 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10613
10614 /* If cursor row is not enabled, we don't really know where to
10615 display the cursor. */
10616 if (!glyph_row->enabled_p)
10617 {
10618 w->phys_cursor_on_p = 0;
10619 return;
10620 }
10621
10622 xassert (interrupt_input_blocked);
10623
10624 /* Set new_cursor_type to the cursor we want to be displayed. In a
10625 mini-buffer window, we want the cursor only to appear if we are
10626 reading input from this window. For the selected window, we want
10627 the cursor type given by the frame parameter. If explicitly
10628 marked off, draw no cursor. In all other cases, we want a hollow
10629 box cursor. */
10630 new_cursor_width = -1;
10631 if (cursor_in_echo_area
10632 && FRAME_HAS_MINIBUF_P (f)
10633 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
10634 {
10635 if (w == XWINDOW (echo_area_window))
10636 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10637 else
10638 new_cursor_type = HOLLOW_BOX_CURSOR;
10639 }
10640 else
10641 {
10642 if (w != XWINDOW (selected_window)
10643 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10644 {
10645 extern int cursor_in_non_selected_windows;
10646
10647 if (MINI_WINDOW_P (w) || !cursor_in_non_selected_windows)
10648 new_cursor_type = NO_CURSOR;
10649 else
10650 new_cursor_type = HOLLOW_BOX_CURSOR;
10651 }
10652 else if (w->cursor_off_p)
10653 new_cursor_type = NO_CURSOR;
10654 else
10655 {
10656 struct buffer *b = XBUFFER (w->buffer);
10657
10658 if (EQ (b->cursor_type, Qt))
10659 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10660 else
10661 new_cursor_type = x_specified_cursor_type (b->cursor_type,
10662 &new_cursor_width);
10663 }
10664 }
10665
10666 /* If cursor is currently being shown and we don't want it to be or
10667 it is in the wrong place, or the cursor type is not what we want,
10668 erase it. */
10669 if (w->phys_cursor_on_p
10670 && (!on
10671 || w->phys_cursor.x != x
10672 || w->phys_cursor.y != y
10673 || new_cursor_type != w->phys_cursor_type))
10674 x_erase_phys_cursor (w);
10675
10676 /* If the cursor is now invisible and we want it to be visible,
10677 display it. */
10678 if (on && !w->phys_cursor_on_p)
10679 {
10680 w->phys_cursor_ascent = glyph_row->ascent;
10681 w->phys_cursor_height = glyph_row->height;
10682
10683 /* Set phys_cursor_.* before x_draw_.* is called because some
10684 of them may need the information. */
10685 w->phys_cursor.x = x;
10686 w->phys_cursor.y = glyph_row->y;
10687 w->phys_cursor.hpos = hpos;
10688 w->phys_cursor.vpos = vpos;
10689 w->phys_cursor_type = new_cursor_type;
10690 w->phys_cursor_on_p = 1;
10691
10692 switch (new_cursor_type)
10693 {
10694 case HOLLOW_BOX_CURSOR:
10695 x_draw_hollow_cursor (w, glyph_row);
10696 break;
10697
10698 case FILLED_BOX_CURSOR:
10699 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10700 break;
10701
10702 case BAR_CURSOR:
10703 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
10704 break;
10705
10706 case NO_CURSOR:
10707 break;
10708
10709 default:
10710 abort ();
10711 }
10712
10713 #ifdef HAVE_X_I18N
10714 if (w == XWINDOW (f->selected_window))
10715 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10716 xic_set_preeditarea (w, x, y);
10717 #endif
10718 }
10719
10720 #ifndef XFlush
10721 if (updating_frame != f)
10722 XFlush (FRAME_X_DISPLAY (f));
10723 #endif
10724 }
10725
10726
10727 /* Display the cursor on window W, or clear it. X and Y are window
10728 relative pixel coordinates. HPOS and VPOS are glyph matrix
10729 positions. If W is not the selected window, display a hollow
10730 cursor. ON non-zero means display the cursor at X, Y which
10731 correspond to HPOS, VPOS, otherwise it is cleared. */
10732
10733 void
10734 x_display_cursor (w, on, hpos, vpos, x, y)
10735 struct window *w;
10736 int on, hpos, vpos, x, y;
10737 {
10738 BLOCK_INPUT;
10739 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
10740 UNBLOCK_INPUT;
10741 }
10742
10743
10744 /* Display the cursor on window W, or clear it, according to ON_P.
10745 Don't change the cursor's position. */
10746
10747 void
10748 x_update_cursor (f, on_p)
10749 struct frame *f;
10750 {
10751 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10752 }
10753
10754
10755 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
10756 in the window tree rooted at W. */
10757
10758 static void
10759 x_update_cursor_in_window_tree (w, on_p)
10760 struct window *w;
10761 int on_p;
10762 {
10763 while (w)
10764 {
10765 if (!NILP (w->hchild))
10766 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10767 else if (!NILP (w->vchild))
10768 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10769 else
10770 x_update_window_cursor (w, on_p);
10771
10772 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10773 }
10774 }
10775
10776
10777 /* Switch the display of W's cursor on or off, according to the value
10778 of ON. */
10779
10780 static void
10781 x_update_window_cursor (w, on)
10782 struct window *w;
10783 int on;
10784 {
10785 /* Don't update cursor in windows whose frame is in the process
10786 of being deleted. */
10787 if (w->current_matrix)
10788 {
10789 BLOCK_INPUT;
10790 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
10791 w->phys_cursor.x, w->phys_cursor.y);
10792 UNBLOCK_INPUT;
10793 }
10794 }
10795
10796
10797
10798 \f
10799 /* Icons. */
10800
10801 /* Refresh bitmap kitchen sink icon for frame F
10802 when we get an expose event for it. */
10803
10804 void
10805 refreshicon (f)
10806 struct frame *f;
10807 {
10808 /* Normally, the window manager handles this function. */
10809 }
10810
10811 /* Make the x-window of frame F use the gnu icon bitmap. */
10812
10813 int
10814 x_bitmap_icon (f, file)
10815 struct frame *f;
10816 Lisp_Object file;
10817 {
10818 int bitmap_id;
10819
10820 if (FRAME_X_WINDOW (f) == 0)
10821 return 1;
10822
10823 /* Free up our existing icon bitmap if any. */
10824 if (f->output_data.x->icon_bitmap > 0)
10825 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10826 f->output_data.x->icon_bitmap = 0;
10827
10828 if (STRINGP (file))
10829 bitmap_id = x_create_bitmap_from_file (f, file);
10830 else
10831 {
10832 /* Create the GNU bitmap if necessary. */
10833 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
10834 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
10835 = x_create_bitmap_from_data (f, gnu_bits,
10836 gnu_width, gnu_height);
10837
10838 /* The first time we create the GNU bitmap,
10839 this increments the ref-count one extra time.
10840 As a result, the GNU bitmap is never freed.
10841 That way, we don't have to worry about allocating it again. */
10842 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
10843
10844 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
10845 }
10846
10847 x_wm_set_icon_pixmap (f, bitmap_id);
10848 f->output_data.x->icon_bitmap = bitmap_id;
10849
10850 return 0;
10851 }
10852
10853
10854 /* Make the x-window of frame F use a rectangle with text.
10855 Use ICON_NAME as the text. */
10856
10857 int
10858 x_text_icon (f, icon_name)
10859 struct frame *f;
10860 char *icon_name;
10861 {
10862 if (FRAME_X_WINDOW (f) == 0)
10863 return 1;
10864
10865 #ifdef HAVE_X11R4
10866 {
10867 XTextProperty text;
10868 text.value = (unsigned char *) icon_name;
10869 text.encoding = XA_STRING;
10870 text.format = 8;
10871 text.nitems = strlen (icon_name);
10872 #ifdef USE_X_TOOLKIT
10873 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
10874 &text);
10875 #else /* not USE_X_TOOLKIT */
10876 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
10877 #endif /* not USE_X_TOOLKIT */
10878 }
10879 #else /* not HAVE_X11R4 */
10880 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
10881 #endif /* not HAVE_X11R4 */
10882
10883 if (f->output_data.x->icon_bitmap > 0)
10884 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10885 f->output_data.x->icon_bitmap = 0;
10886 x_wm_set_icon_pixmap (f, 0);
10887
10888 return 0;
10889 }
10890 \f
10891 #define X_ERROR_MESSAGE_SIZE 200
10892
10893 /* If non-nil, this should be a string.
10894 It means catch X errors and store the error message in this string. */
10895
10896 static Lisp_Object x_error_message_string;
10897
10898 /* An X error handler which stores the error message in
10899 x_error_message_string. This is called from x_error_handler if
10900 x_catch_errors is in effect. */
10901
10902 static void
10903 x_error_catcher (display, error)
10904 Display *display;
10905 XErrorEvent *error;
10906 {
10907 XGetErrorText (display, error->error_code,
10908 XSTRING (x_error_message_string)->data,
10909 X_ERROR_MESSAGE_SIZE);
10910 }
10911
10912 /* Begin trapping X errors for display DPY. Actually we trap X errors
10913 for all displays, but DPY should be the display you are actually
10914 operating on.
10915
10916 After calling this function, X protocol errors no longer cause
10917 Emacs to exit; instead, they are recorded in the string
10918 stored in x_error_message_string.
10919
10920 Calling x_check_errors signals an Emacs error if an X error has
10921 occurred since the last call to x_catch_errors or x_check_errors.
10922
10923 Calling x_uncatch_errors resumes the normal error handling. */
10924
10925 void x_check_errors ();
10926 static Lisp_Object x_catch_errors_unwind ();
10927
10928 int
10929 x_catch_errors (dpy)
10930 Display *dpy;
10931 {
10932 int count = specpdl_ptr - specpdl;
10933
10934 /* Make sure any errors from previous requests have been dealt with. */
10935 XSync (dpy, False);
10936
10937 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
10938
10939 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
10940 XSTRING (x_error_message_string)->data[0] = 0;
10941
10942 return count;
10943 }
10944
10945 /* Unbind the binding that we made to check for X errors. */
10946
10947 static Lisp_Object
10948 x_catch_errors_unwind (old_val)
10949 Lisp_Object old_val;
10950 {
10951 x_error_message_string = old_val;
10952 return Qnil;
10953 }
10954
10955 /* If any X protocol errors have arrived since the last call to
10956 x_catch_errors or x_check_errors, signal an Emacs error using
10957 sprintf (a buffer, FORMAT, the x error message text) as the text. */
10958
10959 void
10960 x_check_errors (dpy, format)
10961 Display *dpy;
10962 char *format;
10963 {
10964 /* Make sure to catch any errors incurred so far. */
10965 XSync (dpy, False);
10966
10967 if (XSTRING (x_error_message_string)->data[0])
10968 error (format, XSTRING (x_error_message_string)->data);
10969 }
10970
10971 /* Nonzero if we had any X protocol errors
10972 since we did x_catch_errors on DPY. */
10973
10974 int
10975 x_had_errors_p (dpy)
10976 Display *dpy;
10977 {
10978 /* Make sure to catch any errors incurred so far. */
10979 XSync (dpy, False);
10980
10981 return XSTRING (x_error_message_string)->data[0] != 0;
10982 }
10983
10984 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
10985
10986 void
10987 x_clear_errors (dpy)
10988 Display *dpy;
10989 {
10990 XSTRING (x_error_message_string)->data[0] = 0;
10991 }
10992
10993 /* Stop catching X protocol errors and let them make Emacs die.
10994 DPY should be the display that was passed to x_catch_errors.
10995 COUNT should be the value that was returned by
10996 the corresponding call to x_catch_errors. */
10997
10998 void
10999 x_uncatch_errors (dpy, count)
11000 Display *dpy;
11001 int count;
11002 {
11003 unbind_to (count, Qnil);
11004 }
11005
11006 #if 0
11007 static unsigned int x_wire_count;
11008 x_trace_wire ()
11009 {
11010 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11011 }
11012 #endif /* ! 0 */
11013
11014 \f
11015 /* Handle SIGPIPE, which can happen when the connection to a server
11016 simply goes away. SIGPIPE is handled by x_connection_signal.
11017 Don't need to do anything, because the write which caused the
11018 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
11019 which will do the appropriate cleanup for us. */
11020
11021 static SIGTYPE
11022 x_connection_signal (signalnum) /* If we don't have an argument, */
11023 int signalnum; /* some compilers complain in signal calls. */
11024 {
11025 #ifdef USG
11026 /* USG systems forget handlers when they are used;
11027 must reestablish each time */
11028 signal (signalnum, x_connection_signal);
11029 #endif /* USG */
11030 }
11031 \f
11032 /* Handling X errors. */
11033
11034 /* Handle the loss of connection to display DISPLAY. */
11035
11036 static SIGTYPE
11037 x_connection_closed (display, error_message)
11038 Display *display;
11039 char *error_message;
11040 {
11041 struct x_display_info *dpyinfo = x_display_info_for_display (display);
11042 Lisp_Object frame, tail;
11043
11044 /* Indicate that this display is dead. */
11045
11046 #if 0 /* Closing the display caused a bus error on OpenWindows. */
11047 #ifdef USE_X_TOOLKIT
11048 XtCloseDisplay (display);
11049 #endif
11050 #endif
11051
11052 if (dpyinfo)
11053 dpyinfo->display = 0;
11054
11055 /* First delete frames whose mini-buffers are on frames
11056 that are on the dead display. */
11057 FOR_EACH_FRAME (tail, frame)
11058 {
11059 Lisp_Object minibuf_frame;
11060 minibuf_frame
11061 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
11062 if (FRAME_X_P (XFRAME (frame))
11063 && FRAME_X_P (XFRAME (minibuf_frame))
11064 && ! EQ (frame, minibuf_frame)
11065 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
11066 Fdelete_frame (frame, Qt);
11067 }
11068
11069 /* Now delete all remaining frames on the dead display.
11070 We are now sure none of these is used as the mini-buffer
11071 for another frame that we need to delete. */
11072 FOR_EACH_FRAME (tail, frame)
11073 if (FRAME_X_P (XFRAME (frame))
11074 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
11075 {
11076 /* Set this to t so that Fdelete_frame won't get confused
11077 trying to find a replacement. */
11078 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11079 Fdelete_frame (frame, Qt);
11080 }
11081
11082 if (dpyinfo)
11083 x_delete_display (dpyinfo);
11084
11085 if (x_display_list == 0)
11086 {
11087 fprintf (stderr, "%s\n", error_message);
11088 shut_down_emacs (0, 0, Qnil);
11089 exit (70);
11090 }
11091
11092 /* Ordinary stack unwind doesn't deal with these. */
11093 #ifdef SIGIO
11094 sigunblock (sigmask (SIGIO));
11095 #endif
11096 sigunblock (sigmask (SIGALRM));
11097 TOTALLY_UNBLOCK_INPUT;
11098
11099 clear_waiting_for_input ();
11100 error ("%s", error_message);
11101 }
11102
11103 /* This is the usual handler for X protocol errors.
11104 It kills all frames on the display that we got the error for.
11105 If that was the only one, it prints an error message and kills Emacs. */
11106
11107 static void
11108 x_error_quitter (display, error)
11109 Display *display;
11110 XErrorEvent *error;
11111 {
11112 char buf[256], buf1[356];
11113
11114 /* Note that there is no real way portable across R3/R4 to get the
11115 original error handler. */
11116
11117 XGetErrorText (display, error->error_code, buf, sizeof (buf));
11118 sprintf (buf1, "X protocol error: %s on protocol request %d",
11119 buf, error->request_code);
11120 x_connection_closed (display, buf1);
11121 }
11122
11123 /* This is the first-level handler for X protocol errors.
11124 It calls x_error_quitter or x_error_catcher. */
11125
11126 static int
11127 x_error_handler (display, error)
11128 Display *display;
11129 XErrorEvent *error;
11130 {
11131 if (! NILP (x_error_message_string))
11132 x_error_catcher (display, error);
11133 else
11134 x_error_quitter (display, error);
11135 return 0;
11136 }
11137
11138 /* This is the handler for X IO errors, always.
11139 It kills all frames on the display that we lost touch with.
11140 If that was the only one, it prints an error message and kills Emacs. */
11141
11142 static int
11143 x_io_error_quitter (display)
11144 Display *display;
11145 {
11146 char buf[256];
11147
11148 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11149 x_connection_closed (display, buf);
11150 return 0;
11151 }
11152 \f
11153 /* Changing the font of the frame. */
11154
11155 /* Give frame F the font named FONTNAME as its default font, and
11156 return the full name of that font. FONTNAME may be a wildcard
11157 pattern; in that case, we choose some font that fits the pattern.
11158 The return value shows which font we chose. */
11159
11160 Lisp_Object
11161 x_new_font (f, fontname)
11162 struct frame *f;
11163 register char *fontname;
11164 {
11165 struct font_info *fontp
11166 = FS_LOAD_FONT (f, 0, fontname, -1);
11167
11168 if (!fontp)
11169 return Qnil;
11170
11171 f->output_data.x->font = (XFontStruct *) (fontp->font);
11172 f->output_data.x->baseline_offset = fontp->baseline_offset;
11173 f->output_data.x->fontset = -1;
11174
11175 /* Compute the scroll bar width in character columns. */
11176 if (f->scroll_bar_pixel_width > 0)
11177 {
11178 int wid = FONT_WIDTH (f->output_data.x->font);
11179 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11180 }
11181 else
11182 {
11183 int wid = FONT_WIDTH (f->output_data.x->font);
11184 f->scroll_bar_cols = (14 + wid - 1) / wid;
11185 }
11186
11187 /* Now make the frame display the given font. */
11188 if (FRAME_X_WINDOW (f) != 0)
11189 {
11190 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11191 f->output_data.x->font->fid);
11192 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11193 f->output_data.x->font->fid);
11194 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11195 f->output_data.x->font->fid);
11196
11197 frame_update_line_height (f);
11198 x_set_window_size (f, 0, f->width, f->height);
11199 }
11200 else
11201 /* If we are setting a new frame's font for the first time,
11202 there are no faces yet, so this font's height is the line height. */
11203 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
11204
11205 return build_string (fontp->full_name);
11206 }
11207
11208 /* Give frame F the fontset named FONTSETNAME as its default font, and
11209 return the full name of that fontset. FONTSETNAME may be a wildcard
11210 pattern; in that case, we choose some fontset that fits the pattern.
11211 The return value shows which fontset we chose. */
11212
11213 Lisp_Object
11214 x_new_fontset (f, fontsetname)
11215 struct frame *f;
11216 char *fontsetname;
11217 {
11218 int fontset = fs_query_fontset (build_string (fontsetname), 0);
11219 Lisp_Object result;
11220
11221 if (fontset < 0)
11222 return Qnil;
11223
11224 if (f->output_data.x->fontset == fontset)
11225 /* This fontset is already set in frame F. There's nothing more
11226 to do. */
11227 return fontset_name (fontset);
11228
11229 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
11230
11231 if (!STRINGP (result))
11232 /* Can't load ASCII font. */
11233 return Qnil;
11234
11235 /* Since x_new_font doesn't update any fontset information, do it now. */
11236 f->output_data.x->fontset = fontset;
11237
11238 #ifdef HAVE_X_I18N
11239 if (FRAME_XIC (f)
11240 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
11241 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
11242 #endif
11243
11244 return build_string (fontsetname);
11245 }
11246
11247 \f
11248 /***********************************************************************
11249 X Input Methods
11250 ***********************************************************************/
11251
11252 #ifdef HAVE_X_I18N
11253
11254 #ifdef HAVE_X11R6
11255
11256 /* XIM destroy callback function, which is called whenever the
11257 connection to input method XIM dies. CLIENT_DATA contains a
11258 pointer to the x_display_info structure corresponding to XIM. */
11259
11260 static void
11261 xim_destroy_callback (xim, client_data, call_data)
11262 XIM xim;
11263 XPointer client_data;
11264 XPointer call_data;
11265 {
11266 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11267 Lisp_Object frame, tail;
11268
11269 BLOCK_INPUT;
11270
11271 /* No need to call XDestroyIC.. */
11272 FOR_EACH_FRAME (tail, frame)
11273 {
11274 struct frame *f = XFRAME (frame);
11275 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11276 {
11277 FRAME_XIC (f) = NULL;
11278 if (FRAME_XIC_FONTSET (f))
11279 {
11280 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11281 FRAME_XIC_FONTSET (f) = NULL;
11282 }
11283 }
11284 }
11285
11286 /* No need to call XCloseIM. */
11287 dpyinfo->xim = NULL;
11288 XFree (dpyinfo->xim_styles);
11289 UNBLOCK_INPUT;
11290 }
11291
11292 #endif /* HAVE_X11R6 */
11293
11294 /* Open the connection to the XIM server on display DPYINFO.
11295 RESOURCE_NAME is the resource name Emacs uses. */
11296
11297 static void
11298 xim_open_dpy (dpyinfo, resource_name)
11299 struct x_display_info *dpyinfo;
11300 char *resource_name;
11301 {
11302 #ifdef USE_XIM
11303 XIM xim;
11304
11305 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11306 dpyinfo->xim = xim;
11307
11308 if (xim)
11309 {
11310 #ifdef HAVE_X11R6
11311 XIMCallback destroy;
11312 #endif
11313
11314 /* Get supported styles and XIM values. */
11315 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11316
11317 #ifdef HAVE_X11R6
11318 destroy.callback = xim_destroy_callback;
11319 destroy.client_data = (XPointer)dpyinfo;
11320 /* This isn't prptotyped in OSF 5.0. */
11321 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11322 #endif
11323 }
11324
11325 #else /* not USE_XIM */
11326 dpyinfo->xim = NULL;
11327 #endif /* not USE_XIM */
11328 }
11329
11330
11331 #ifdef HAVE_X11R6_XIM
11332
11333 struct xim_inst_t
11334 {
11335 struct x_display_info *dpyinfo;
11336 char *resource_name;
11337 };
11338
11339 /* XIM instantiate callback function, which is called whenever an XIM
11340 server is available. DISPLAY is teh display of the XIM.
11341 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11342 when the callback was registered. */
11343
11344 static void
11345 xim_instantiate_callback (display, client_data, call_data)
11346 Display *display;
11347 XPointer client_data;
11348 XPointer call_data;
11349 {
11350 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11351 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11352
11353 /* We don't support multiple XIM connections. */
11354 if (dpyinfo->xim)
11355 return;
11356
11357 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11358
11359 /* Create XIC for the existing frames on the same display, as long
11360 as they have no XIC. */
11361 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11362 {
11363 Lisp_Object tail, frame;
11364
11365 BLOCK_INPUT;
11366 FOR_EACH_FRAME (tail, frame)
11367 {
11368 struct frame *f = XFRAME (frame);
11369
11370 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11371 if (FRAME_XIC (f) == NULL)
11372 {
11373 create_frame_xic (f);
11374 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11375 xic_set_statusarea (f);
11376 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11377 {
11378 struct window *w = XWINDOW (f->selected_window);
11379 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11380 }
11381 }
11382 }
11383
11384 UNBLOCK_INPUT;
11385 }
11386 }
11387
11388 #endif /* HAVE_X11R6_XIM */
11389
11390
11391 /* Open a connection to the XIM server on display DPYINFO.
11392 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11393 connection only at the first time. On X11R6, open the connection
11394 in the XIM instantiate callback function. */
11395
11396 static void
11397 xim_initialize (dpyinfo, resource_name)
11398 struct x_display_info *dpyinfo;
11399 char *resource_name;
11400 {
11401 #ifdef USE_XIM
11402 #ifdef HAVE_X11R6_XIM
11403 struct xim_inst_t *xim_inst;
11404 int len;
11405
11406 dpyinfo->xim = NULL;
11407 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11408 xim_inst->dpyinfo = dpyinfo;
11409 len = strlen (resource_name);
11410 xim_inst->resource_name = (char *) xmalloc (len + 1);
11411 bcopy (resource_name, xim_inst->resource_name, len + 1);
11412 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11413 resource_name, EMACS_CLASS,
11414 xim_instantiate_callback,
11415 /* Fixme: This is XPointer in
11416 XFree86 but (XPointer *) on
11417 Tru64, at least. */
11418 (XPointer) xim_inst);
11419 #else /* not HAVE_X11R6_XIM */
11420 dpyinfo->xim = NULL;
11421 xim_open_dpy (dpyinfo, resource_name);
11422 #endif /* not HAVE_X11R6_XIM */
11423
11424 #else /* not USE_XIM */
11425 dpyinfo->xim = NULL;
11426 #endif /* not USE_XIM */
11427 }
11428
11429
11430 /* Close the connection to the XIM server on display DPYINFO. */
11431
11432 static void
11433 xim_close_dpy (dpyinfo)
11434 struct x_display_info *dpyinfo;
11435 {
11436 #ifdef USE_XIM
11437 #ifdef HAVE_X11R6_XIM
11438 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11439 NULL, EMACS_CLASS,
11440 xim_instantiate_callback, NULL);
11441 #endif /* not HAVE_X11R6_XIM */
11442 XCloseIM (dpyinfo->xim);
11443 dpyinfo->xim = NULL;
11444 XFree (dpyinfo->xim_styles);
11445 #endif /* USE_XIM */
11446 }
11447
11448 #endif /* not HAVE_X11R6_XIM */
11449
11450
11451 \f
11452 /* Calculate the absolute position in frame F
11453 from its current recorded position values and gravity. */
11454
11455 void
11456 x_calc_absolute_position (f)
11457 struct frame *f;
11458 {
11459 Window child;
11460 int win_x = 0, win_y = 0;
11461 int flags = f->output_data.x->size_hint_flags;
11462 int this_window;
11463
11464 /* We have nothing to do if the current position
11465 is already for the top-left corner. */
11466 if (! ((flags & XNegative) || (flags & YNegative)))
11467 return;
11468
11469 #ifdef USE_X_TOOLKIT
11470 this_window = XtWindow (f->output_data.x->widget);
11471 #else
11472 this_window = FRAME_X_WINDOW (f);
11473 #endif
11474
11475 /* Find the position of the outside upper-left corner of
11476 the inner window, with respect to the outer window.
11477 But do this only if we will need the results. */
11478 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
11479 {
11480 int count;
11481
11482 BLOCK_INPUT;
11483 count = x_catch_errors (FRAME_X_DISPLAY (f));
11484 while (1)
11485 {
11486 x_clear_errors (FRAME_X_DISPLAY (f));
11487 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11488
11489 /* From-window, to-window. */
11490 this_window,
11491 f->output_data.x->parent_desc,
11492
11493 /* From-position, to-position. */
11494 0, 0, &win_x, &win_y,
11495
11496 /* Child of win. */
11497 &child);
11498 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11499 {
11500 Window newroot, newparent = 0xdeadbeef;
11501 Window *newchildren;
11502 unsigned int nchildren;
11503
11504 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11505 &newparent, &newchildren, &nchildren))
11506 break;
11507
11508 XFree ((char *) newchildren);
11509
11510 f->output_data.x->parent_desc = newparent;
11511 }
11512 else
11513 break;
11514 }
11515
11516 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
11517 UNBLOCK_INPUT;
11518 }
11519
11520 /* Treat negative positions as relative to the leftmost bottommost
11521 position that fits on the screen. */
11522 if (flags & XNegative)
11523 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
11524 - 2 * f->output_data.x->border_width - win_x
11525 - PIXEL_WIDTH (f)
11526 + f->output_data.x->left_pos);
11527
11528 if (flags & YNegative)
11529 {
11530 int menubar_height = 0;
11531
11532 #ifdef USE_X_TOOLKIT
11533 if (f->output_data.x->menubar_widget)
11534 menubar_height
11535 = (f->output_data.x->menubar_widget->core.height
11536 + f->output_data.x->menubar_widget->core.border_width);
11537 #endif
11538
11539 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11540 - 2 * f->output_data.x->border_width
11541 - win_y
11542 - PIXEL_HEIGHT (f)
11543 - menubar_height
11544 + f->output_data.x->top_pos);
11545 }
11546
11547 /* The left_pos and top_pos
11548 are now relative to the top and left screen edges,
11549 so the flags should correspond. */
11550 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11551 }
11552
11553 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11554 to really change the position, and 0 when calling from
11555 x_make_frame_visible (in that case, XOFF and YOFF are the current
11556 position values). It is -1 when calling from x_set_frame_parameters,
11557 which means, do adjust for borders but don't change the gravity. */
11558
11559 void
11560 x_set_offset (f, xoff, yoff, change_gravity)
11561 struct frame *f;
11562 register int xoff, yoff;
11563 int change_gravity;
11564 {
11565 int modified_top, modified_left;
11566
11567 if (change_gravity > 0)
11568 {
11569 f->output_data.x->top_pos = yoff;
11570 f->output_data.x->left_pos = xoff;
11571 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11572 if (xoff < 0)
11573 f->output_data.x->size_hint_flags |= XNegative;
11574 if (yoff < 0)
11575 f->output_data.x->size_hint_flags |= YNegative;
11576 f->output_data.x->win_gravity = NorthWestGravity;
11577 }
11578 x_calc_absolute_position (f);
11579
11580 BLOCK_INPUT;
11581 x_wm_set_size_hint (f, (long) 0, 0);
11582
11583 modified_left = f->output_data.x->left_pos;
11584 modified_top = f->output_data.x->top_pos;
11585 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11586 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11587 /* It is a mystery why we need to add the border_width here
11588 when the frame is already visible, but experiment says we do. */
11589 if (change_gravity != 0)
11590 {
11591 modified_left += f->output_data.x->border_width;
11592 modified_top += f->output_data.x->border_width;
11593 }
11594 #endif
11595
11596 #ifdef USE_X_TOOLKIT
11597 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11598 modified_left, modified_top);
11599 #else /* not USE_X_TOOLKIT */
11600 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11601 modified_left, modified_top);
11602 #endif /* not USE_X_TOOLKIT */
11603 UNBLOCK_INPUT;
11604 }
11605
11606 /* Call this to change the size of frame F's x-window.
11607 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11608 for this size change and subsequent size changes.
11609 Otherwise we leave the window gravity unchanged. */
11610
11611 void
11612 x_set_window_size (f, change_gravity, cols, rows)
11613 struct frame *f;
11614 int change_gravity;
11615 int cols, rows;
11616 {
11617 #ifndef USE_X_TOOLKIT
11618 int pixelwidth, pixelheight;
11619 #endif
11620
11621 BLOCK_INPUT;
11622
11623 #ifdef USE_X_TOOLKIT
11624 {
11625 /* The x and y position of the widget is clobbered by the
11626 call to XtSetValues within EmacsFrameSetCharSize.
11627 This is a real kludge, but I don't understand Xt so I can't
11628 figure out a correct fix. Can anyone else tell me? -- rms. */
11629 int xpos = f->output_data.x->widget->core.x;
11630 int ypos = f->output_data.x->widget->core.y;
11631 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11632 f->output_data.x->widget->core.x = xpos;
11633 f->output_data.x->widget->core.y = ypos;
11634 }
11635
11636 #else /* not USE_X_TOOLKIT */
11637
11638 check_frame_size (f, &rows, &cols);
11639 f->output_data.x->vertical_scroll_bar_extra
11640 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11641 ? 0
11642 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
11643 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
11644 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
11645 f->output_data.x->flags_areas_extra
11646 = FRAME_FLAGS_AREA_WIDTH (f);
11647 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11648 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
11649
11650 f->output_data.x->win_gravity = NorthWestGravity;
11651 x_wm_set_size_hint (f, (long) 0, 0);
11652
11653 XSync (FRAME_X_DISPLAY (f), False);
11654 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11655 pixelwidth, pixelheight);
11656
11657 /* Now, strictly speaking, we can't be sure that this is accurate,
11658 but the window manager will get around to dealing with the size
11659 change request eventually, and we'll hear how it went when the
11660 ConfigureNotify event gets here.
11661
11662 We could just not bother storing any of this information here,
11663 and let the ConfigureNotify event set everything up, but that
11664 might be kind of confusing to the Lisp code, since size changes
11665 wouldn't be reported in the frame parameters until some random
11666 point in the future when the ConfigureNotify event arrives.
11667
11668 We pass 1 for DELAY since we can't run Lisp code inside of
11669 a BLOCK_INPUT. */
11670 change_frame_size (f, rows, cols, 0, 1, 0);
11671 PIXEL_WIDTH (f) = pixelwidth;
11672 PIXEL_HEIGHT (f) = pixelheight;
11673
11674 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11675 receive in the ConfigureNotify event; if we get what we asked
11676 for, then the event won't cause the screen to become garbaged, so
11677 we have to make sure to do it here. */
11678 SET_FRAME_GARBAGED (f);
11679
11680 XFlush (FRAME_X_DISPLAY (f));
11681
11682 #endif /* not USE_X_TOOLKIT */
11683
11684 /* If cursor was outside the new size, mark it as off. */
11685 mark_window_cursors_off (XWINDOW (f->root_window));
11686
11687 /* Clear out any recollection of where the mouse highlighting was,
11688 since it might be in a place that's outside the new frame size.
11689 Actually checking whether it is outside is a pain in the neck,
11690 so don't try--just let the highlighting be done afresh with new size. */
11691 cancel_mouse_face (f);
11692
11693 UNBLOCK_INPUT;
11694 }
11695 \f
11696 /* Mouse warping. */
11697
11698 void
11699 x_set_mouse_position (f, x, y)
11700 struct frame *f;
11701 int x, y;
11702 {
11703 int pix_x, pix_y;
11704
11705 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11706 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
11707
11708 if (pix_x < 0) pix_x = 0;
11709 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11710
11711 if (pix_y < 0) pix_y = 0;
11712 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
11713
11714 BLOCK_INPUT;
11715
11716 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11717 0, 0, 0, 0, pix_x, pix_y);
11718 UNBLOCK_INPUT;
11719 }
11720
11721 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11722
11723 void
11724 x_set_mouse_pixel_position (f, pix_x, pix_y)
11725 struct frame *f;
11726 int pix_x, pix_y;
11727 {
11728 BLOCK_INPUT;
11729
11730 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11731 0, 0, 0, 0, pix_x, pix_y);
11732 UNBLOCK_INPUT;
11733 }
11734 \f
11735 /* focus shifting, raising and lowering. */
11736
11737 void
11738 x_focus_on_frame (f)
11739 struct frame *f;
11740 {
11741 #if 0 /* This proves to be unpleasant. */
11742 x_raise_frame (f);
11743 #endif
11744 #if 0
11745 /* I don't think that the ICCCM allows programs to do things like this
11746 without the interaction of the window manager. Whatever you end up
11747 doing with this code, do it to x_unfocus_frame too. */
11748 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11749 RevertToPointerRoot, CurrentTime);
11750 #endif /* ! 0 */
11751 }
11752
11753 void
11754 x_unfocus_frame (f)
11755 struct frame *f;
11756 {
11757 #if 0
11758 /* Look at the remarks in x_focus_on_frame. */
11759 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
11760 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
11761 RevertToPointerRoot, CurrentTime);
11762 #endif /* ! 0 */
11763 }
11764
11765 /* Raise frame F. */
11766
11767 void
11768 x_raise_frame (f)
11769 struct frame *f;
11770 {
11771 if (f->async_visible)
11772 {
11773 BLOCK_INPUT;
11774 #ifdef USE_X_TOOLKIT
11775 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
11776 #else /* not USE_X_TOOLKIT */
11777 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11778 #endif /* not USE_X_TOOLKIT */
11779 XFlush (FRAME_X_DISPLAY (f));
11780 UNBLOCK_INPUT;
11781 }
11782 }
11783
11784 /* Lower frame F. */
11785
11786 void
11787 x_lower_frame (f)
11788 struct frame *f;
11789 {
11790 if (f->async_visible)
11791 {
11792 BLOCK_INPUT;
11793 #ifdef USE_X_TOOLKIT
11794 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
11795 #else /* not USE_X_TOOLKIT */
11796 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11797 #endif /* not USE_X_TOOLKIT */
11798 XFlush (FRAME_X_DISPLAY (f));
11799 UNBLOCK_INPUT;
11800 }
11801 }
11802
11803 static void
11804 XTframe_raise_lower (f, raise_flag)
11805 FRAME_PTR f;
11806 int raise_flag;
11807 {
11808 if (raise_flag)
11809 x_raise_frame (f);
11810 else
11811 x_lower_frame (f);
11812 }
11813 \f
11814 /* Change of visibility. */
11815
11816 /* This tries to wait until the frame is really visible.
11817 However, if the window manager asks the user where to position
11818 the frame, this will return before the user finishes doing that.
11819 The frame will not actually be visible at that time,
11820 but it will become visible later when the window manager
11821 finishes with it. */
11822
11823 void
11824 x_make_frame_visible (f)
11825 struct frame *f;
11826 {
11827 Lisp_Object type;
11828 int original_top, original_left;
11829
11830 BLOCK_INPUT;
11831
11832 type = x_icon_type (f);
11833 if (!NILP (type))
11834 x_bitmap_icon (f, type);
11835
11836 if (! FRAME_VISIBLE_P (f))
11837 {
11838 /* We test FRAME_GARBAGED_P here to make sure we don't
11839 call x_set_offset a second time
11840 if we get to x_make_frame_visible a second time
11841 before the window gets really visible. */
11842 if (! FRAME_ICONIFIED_P (f)
11843 && ! f->output_data.x->asked_for_visible)
11844 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11845
11846 f->output_data.x->asked_for_visible = 1;
11847
11848 if (! EQ (Vx_no_window_manager, Qt))
11849 x_wm_set_window_state (f, NormalState);
11850 #ifdef USE_X_TOOLKIT
11851 /* This was XtPopup, but that did nothing for an iconified frame. */
11852 XtMapWidget (f->output_data.x->widget);
11853 #else /* not USE_X_TOOLKIT */
11854 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11855 #endif /* not USE_X_TOOLKIT */
11856 #if 0 /* This seems to bring back scroll bars in the wrong places
11857 if the window configuration has changed. They seem
11858 to come back ok without this. */
11859 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
11860 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11861 #endif
11862 }
11863
11864 XFlush (FRAME_X_DISPLAY (f));
11865
11866 /* Synchronize to ensure Emacs knows the frame is visible
11867 before we do anything else. We do this loop with input not blocked
11868 so that incoming events are handled. */
11869 {
11870 Lisp_Object frame;
11871 int count;
11872 /* This must be before UNBLOCK_INPUT
11873 since events that arrive in response to the actions above
11874 will set it when they are handled. */
11875 int previously_visible = f->output_data.x->has_been_visible;
11876
11877 original_left = f->output_data.x->left_pos;
11878 original_top = f->output_data.x->top_pos;
11879
11880 /* This must come after we set COUNT. */
11881 UNBLOCK_INPUT;
11882
11883 /* We unblock here so that arriving X events are processed. */
11884
11885 /* Now move the window back to where it was "supposed to be".
11886 But don't do it if the gravity is negative.
11887 When the gravity is negative, this uses a position
11888 that is 3 pixels too low. Perhaps that's really the border width.
11889
11890 Don't do this if the window has never been visible before,
11891 because the window manager may choose the position
11892 and we don't want to override it. */
11893
11894 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
11895 && f->output_data.x->win_gravity == NorthWestGravity
11896 && previously_visible)
11897 {
11898 Drawable rootw;
11899 int x, y;
11900 unsigned int width, height, border, depth;
11901
11902 BLOCK_INPUT;
11903
11904 /* On some window managers (such as FVWM) moving an existing
11905 window, even to the same place, causes the window manager
11906 to introduce an offset. This can cause the window to move
11907 to an unexpected location. Check the geometry (a little
11908 slow here) and then verify that the window is in the right
11909 place. If the window is not in the right place, move it
11910 there, and take the potential window manager hit. */
11911 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11912 &rootw, &x, &y, &width, &height, &border, &depth);
11913
11914 if (original_left != x || original_top != y)
11915 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11916 original_left, original_top);
11917
11918 UNBLOCK_INPUT;
11919 }
11920
11921 XSETFRAME (frame, f);
11922
11923 /* Wait until the frame is visible. Process X events until a
11924 MapNotify event has been seen, or until we think we won't get a
11925 MapNotify at all.. */
11926 for (count = input_signal_count + 10;
11927 input_signal_count < count && !FRAME_VISIBLE_P (f);)
11928 {
11929 /* Force processing of queued events. */
11930 x_sync (f);
11931
11932 /* Machines that do polling rather than SIGIO have been
11933 observed to go into a busy-wait here. So we'll fake an
11934 alarm signal to let the handler know that there's something
11935 to be read. We used to raise a real alarm, but it seems
11936 that the handler isn't always enabled here. This is
11937 probably a bug. */
11938 if (input_polling_used ())
11939 {
11940 /* It could be confusing if a real alarm arrives while
11941 processing the fake one. Turn it off and let the
11942 handler reset it. */
11943 extern void poll_for_input_1 P_ ((void));
11944 int old_poll_suppress_count = poll_suppress_count;
11945 poll_suppress_count = 1;
11946 poll_for_input_1 ();
11947 poll_suppress_count = old_poll_suppress_count;
11948 }
11949
11950 /* See if a MapNotify event has been processed. */
11951 FRAME_SAMPLE_VISIBILITY (f);
11952 }
11953 }
11954 }
11955
11956 /* Change from mapped state to withdrawn state. */
11957
11958 /* Make the frame visible (mapped and not iconified). */
11959
11960 void
11961 x_make_frame_invisible (f)
11962 struct frame *f;
11963 {
11964 Window window;
11965
11966 #ifdef USE_X_TOOLKIT
11967 /* Use the frame's outermost window, not the one we normally draw on. */
11968 window = XtWindow (f->output_data.x->widget);
11969 #else /* not USE_X_TOOLKIT */
11970 window = FRAME_X_WINDOW (f);
11971 #endif /* not USE_X_TOOLKIT */
11972
11973 /* Don't keep the highlight on an invisible frame. */
11974 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11975 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
11976
11977 #if 0/* This might add unreliability; I don't trust it -- rms. */
11978 if (! f->async_visible && ! f->async_iconified)
11979 return;
11980 #endif
11981
11982 BLOCK_INPUT;
11983
11984 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11985 that the current position of the window is user-specified, rather than
11986 program-specified, so that when the window is mapped again, it will be
11987 placed at the same location, without forcing the user to position it
11988 by hand again (they have already done that once for this window.) */
11989 x_wm_set_size_hint (f, (long) 0, 1);
11990
11991 #ifdef HAVE_X11R4
11992
11993 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11994 DefaultScreen (FRAME_X_DISPLAY (f))))
11995 {
11996 UNBLOCK_INPUT_RESIGNAL;
11997 error ("Can't notify window manager of window withdrawal");
11998 }
11999 #else /* ! defined (HAVE_X11R4) */
12000
12001 /* Tell the window manager what we're going to do. */
12002 if (! EQ (Vx_no_window_manager, Qt))
12003 {
12004 XEvent unmap;
12005
12006 unmap.xunmap.type = UnmapNotify;
12007 unmap.xunmap.window = window;
12008 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
12009 unmap.xunmap.from_configure = False;
12010 if (! XSendEvent (FRAME_X_DISPLAY (f),
12011 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12012 False,
12013 SubstructureRedirectMaskSubstructureNotifyMask,
12014 &unmap))
12015 {
12016 UNBLOCK_INPUT_RESIGNAL;
12017 error ("Can't notify window manager of withdrawal");
12018 }
12019 }
12020
12021 /* Unmap the window ourselves. Cheeky! */
12022 XUnmapWindow (FRAME_X_DISPLAY (f), window);
12023 #endif /* ! defined (HAVE_X11R4) */
12024
12025 /* We can't distinguish this from iconification
12026 just by the event that we get from the server.
12027 So we can't win using the usual strategy of letting
12028 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
12029 and synchronize with the server to make sure we agree. */
12030 f->visible = 0;
12031 FRAME_ICONIFIED_P (f) = 0;
12032 f->async_visible = 0;
12033 f->async_iconified = 0;
12034
12035 x_sync (f);
12036
12037 UNBLOCK_INPUT;
12038 }
12039
12040 /* Change window state from mapped to iconified. */
12041
12042 void
12043 x_iconify_frame (f)
12044 struct frame *f;
12045 {
12046 int result;
12047 Lisp_Object type;
12048
12049 /* Don't keep the highlight on an invisible frame. */
12050 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12051 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12052
12053 if (f->async_iconified)
12054 return;
12055
12056 BLOCK_INPUT;
12057
12058 FRAME_SAMPLE_VISIBILITY (f);
12059
12060 type = x_icon_type (f);
12061 if (!NILP (type))
12062 x_bitmap_icon (f, type);
12063
12064 #ifdef USE_X_TOOLKIT
12065
12066 if (! FRAME_VISIBLE_P (f))
12067 {
12068 if (! EQ (Vx_no_window_manager, Qt))
12069 x_wm_set_window_state (f, IconicState);
12070 /* This was XtPopup, but that did nothing for an iconified frame. */
12071 XtMapWidget (f->output_data.x->widget);
12072 /* The server won't give us any event to indicate
12073 that an invisible frame was changed to an icon,
12074 so we have to record it here. */
12075 f->iconified = 1;
12076 f->visible = 1;
12077 f->async_iconified = 1;
12078 f->async_visible = 0;
12079 UNBLOCK_INPUT;
12080 return;
12081 }
12082
12083 result = XIconifyWindow (FRAME_X_DISPLAY (f),
12084 XtWindow (f->output_data.x->widget),
12085 DefaultScreen (FRAME_X_DISPLAY (f)));
12086 UNBLOCK_INPUT;
12087
12088 if (!result)
12089 error ("Can't notify window manager of iconification");
12090
12091 f->async_iconified = 1;
12092 f->async_visible = 0;
12093
12094
12095 BLOCK_INPUT;
12096 XFlush (FRAME_X_DISPLAY (f));
12097 UNBLOCK_INPUT;
12098 #else /* not USE_X_TOOLKIT */
12099
12100 /* Make sure the X server knows where the window should be positioned,
12101 in case the user deiconifies with the window manager. */
12102 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
12103 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12104
12105 /* Since we don't know which revision of X we're running, we'll use both
12106 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
12107
12108 /* X11R4: send a ClientMessage to the window manager using the
12109 WM_CHANGE_STATE type. */
12110 {
12111 XEvent message;
12112
12113 message.xclient.window = FRAME_X_WINDOW (f);
12114 message.xclient.type = ClientMessage;
12115 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
12116 message.xclient.format = 32;
12117 message.xclient.data.l[0] = IconicState;
12118
12119 if (! XSendEvent (FRAME_X_DISPLAY (f),
12120 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12121 False,
12122 SubstructureRedirectMask | SubstructureNotifyMask,
12123 &message))
12124 {
12125 UNBLOCK_INPUT_RESIGNAL;
12126 error ("Can't notify window manager of iconification");
12127 }
12128 }
12129
12130 /* X11R3: set the initial_state field of the window manager hints to
12131 IconicState. */
12132 x_wm_set_window_state (f, IconicState);
12133
12134 if (!FRAME_VISIBLE_P (f))
12135 {
12136 /* If the frame was withdrawn, before, we must map it. */
12137 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12138 }
12139
12140 f->async_iconified = 1;
12141 f->async_visible = 0;
12142
12143 XFlush (FRAME_X_DISPLAY (f));
12144 UNBLOCK_INPUT;
12145 #endif /* not USE_X_TOOLKIT */
12146 }
12147 \f
12148 /* Destroy the X window of frame F. */
12149
12150 void
12151 x_destroy_window (f)
12152 struct frame *f;
12153 {
12154 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12155
12156 BLOCK_INPUT;
12157
12158 /* If a display connection is dead, don't try sending more
12159 commands to the X server. */
12160 if (dpyinfo->display != 0)
12161 {
12162 if (f->output_data.x->icon_desc != 0)
12163 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
12164 #ifdef HAVE_X_I18N
12165 if (FRAME_XIC (f))
12166 free_frame_xic (f);
12167 #endif
12168 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->window_desc);
12169 #ifdef USE_X_TOOLKIT
12170 if (f->output_data.x->widget)
12171 XtDestroyWidget (f->output_data.x->widget);
12172 free_frame_menubar (f);
12173 #endif /* USE_X_TOOLKIT */
12174
12175 unload_color (f, f->output_data.x->foreground_pixel);
12176 unload_color (f, f->output_data.x->background_pixel);
12177 unload_color (f, f->output_data.x->cursor_pixel);
12178 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12179 unload_color (f, f->output_data.x->border_pixel);
12180 unload_color (f, f->output_data.x->mouse_pixel);
12181 if (f->output_data.x->scroll_bar_background_pixel != -1)
12182 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
12183 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
12184 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
12185 if (f->output_data.x->white_relief.allocated_p)
12186 unload_color (f, f->output_data.x->white_relief.pixel);
12187 if (f->output_data.x->black_relief.allocated_p)
12188 unload_color (f, f->output_data.x->black_relief.pixel);
12189
12190 free_frame_faces (f);
12191 XFlush (FRAME_X_DISPLAY (f));
12192 }
12193
12194 if (f->output_data.x->saved_menu_event)
12195 xfree (f->output_data.x->saved_menu_event);
12196
12197 xfree (f->output_data.x);
12198 f->output_data.x = 0;
12199 if (f == dpyinfo->x_focus_frame)
12200 dpyinfo->x_focus_frame = 0;
12201 if (f == dpyinfo->x_focus_event_frame)
12202 dpyinfo->x_focus_event_frame = 0;
12203 if (f == dpyinfo->x_highlight_frame)
12204 dpyinfo->x_highlight_frame = 0;
12205
12206 dpyinfo->reference_count--;
12207
12208 if (f == dpyinfo->mouse_face_mouse_frame)
12209 {
12210 dpyinfo->mouse_face_beg_row
12211 = dpyinfo->mouse_face_beg_col = -1;
12212 dpyinfo->mouse_face_end_row
12213 = dpyinfo->mouse_face_end_col = -1;
12214 dpyinfo->mouse_face_window = Qnil;
12215 dpyinfo->mouse_face_deferred_gc = 0;
12216 dpyinfo->mouse_face_mouse_frame = 0;
12217 }
12218
12219 UNBLOCK_INPUT;
12220 }
12221 \f
12222 /* Setting window manager hints. */
12223
12224 /* Set the normal size hints for the window manager, for frame F.
12225 FLAGS is the flags word to use--or 0 meaning preserve the flags
12226 that the window now has.
12227 If USER_POSITION is nonzero, we set the USPosition
12228 flag (this is useful when FLAGS is 0). */
12229
12230 void
12231 x_wm_set_size_hint (f, flags, user_position)
12232 struct frame *f;
12233 long flags;
12234 int user_position;
12235 {
12236 XSizeHints size_hints;
12237
12238 #ifdef USE_X_TOOLKIT
12239 Arg al[2];
12240 int ac = 0;
12241 Dimension widget_width, widget_height;
12242 Window window = XtWindow (f->output_data.x->widget);
12243 #else /* not USE_X_TOOLKIT */
12244 Window window = FRAME_X_WINDOW (f);
12245 #endif /* not USE_X_TOOLKIT */
12246
12247 /* Setting PMaxSize caused various problems. */
12248 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
12249
12250 size_hints.x = f->output_data.x->left_pos;
12251 size_hints.y = f->output_data.x->top_pos;
12252
12253 #ifdef USE_X_TOOLKIT
12254 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12255 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
12256 XtGetValues (f->output_data.x->widget, al, ac);
12257 size_hints.height = widget_height;
12258 size_hints.width = widget_width;
12259 #else /* not USE_X_TOOLKIT */
12260 size_hints.height = PIXEL_HEIGHT (f);
12261 size_hints.width = PIXEL_WIDTH (f);
12262 #endif /* not USE_X_TOOLKIT */
12263
12264 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
12265 size_hints.height_inc = f->output_data.x->line_height;
12266 size_hints.max_width
12267 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
12268 size_hints.max_height
12269 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
12270
12271 /* Calculate the base and minimum sizes.
12272
12273 (When we use the X toolkit, we don't do it here.
12274 Instead we copy the values that the widgets are using, below.) */
12275 #ifndef USE_X_TOOLKIT
12276 {
12277 int base_width, base_height;
12278 int min_rows = 0, min_cols = 0;
12279
12280 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12281 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
12282
12283 check_frame_size (f, &min_rows, &min_cols);
12284
12285 /* The window manager uses the base width hints to calculate the
12286 current number of rows and columns in the frame while
12287 resizing; min_width and min_height aren't useful for this
12288 purpose, since they might not give the dimensions for a
12289 zero-row, zero-column frame.
12290
12291 We use the base_width and base_height members if we have
12292 them; otherwise, we set the min_width and min_height members
12293 to the size for a zero x zero frame. */
12294
12295 #ifdef HAVE_X11R4
12296 size_hints.flags |= PBaseSize;
12297 size_hints.base_width = base_width;
12298 size_hints.base_height = base_height;
12299 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12300 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
12301 #else
12302 size_hints.min_width = base_width;
12303 size_hints.min_height = base_height;
12304 #endif
12305 }
12306
12307 /* If we don't need the old flags, we don't need the old hint at all. */
12308 if (flags)
12309 {
12310 size_hints.flags |= flags;
12311 goto no_read;
12312 }
12313 #endif /* not USE_X_TOOLKIT */
12314
12315 {
12316 XSizeHints hints; /* Sometimes I hate X Windows... */
12317 long supplied_return;
12318 int value;
12319
12320 #ifdef HAVE_X11R4
12321 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12322 &supplied_return);
12323 #else
12324 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
12325 #endif
12326
12327 #ifdef USE_X_TOOLKIT
12328 size_hints.base_height = hints.base_height;
12329 size_hints.base_width = hints.base_width;
12330 size_hints.min_height = hints.min_height;
12331 size_hints.min_width = hints.min_width;
12332 #endif
12333
12334 if (flags)
12335 size_hints.flags |= flags;
12336 else
12337 {
12338 if (value == 0)
12339 hints.flags = 0;
12340 if (hints.flags & PSize)
12341 size_hints.flags |= PSize;
12342 if (hints.flags & PPosition)
12343 size_hints.flags |= PPosition;
12344 if (hints.flags & USPosition)
12345 size_hints.flags |= USPosition;
12346 if (hints.flags & USSize)
12347 size_hints.flags |= USSize;
12348 }
12349 }
12350
12351 #ifndef USE_X_TOOLKIT
12352 no_read:
12353 #endif
12354
12355 #ifdef PWinGravity
12356 size_hints.win_gravity = f->output_data.x->win_gravity;
12357 size_hints.flags |= PWinGravity;
12358
12359 if (user_position)
12360 {
12361 size_hints.flags &= ~ PPosition;
12362 size_hints.flags |= USPosition;
12363 }
12364 #endif /* PWinGravity */
12365
12366 #ifdef HAVE_X11R4
12367 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12368 #else
12369 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12370 #endif
12371 }
12372
12373 /* Used for IconicState or NormalState */
12374
12375 void
12376 x_wm_set_window_state (f, state)
12377 struct frame *f;
12378 int state;
12379 {
12380 #ifdef USE_X_TOOLKIT
12381 Arg al[1];
12382
12383 XtSetArg (al[0], XtNinitialState, state);
12384 XtSetValues (f->output_data.x->widget, al, 1);
12385 #else /* not USE_X_TOOLKIT */
12386 Window window = FRAME_X_WINDOW (f);
12387
12388 f->output_data.x->wm_hints.flags |= StateHint;
12389 f->output_data.x->wm_hints.initial_state = state;
12390
12391 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12392 #endif /* not USE_X_TOOLKIT */
12393 }
12394
12395 void
12396 x_wm_set_icon_pixmap (f, pixmap_id)
12397 struct frame *f;
12398 int pixmap_id;
12399 {
12400 Pixmap icon_pixmap;
12401
12402 #ifndef USE_X_TOOLKIT
12403 Window window = FRAME_X_WINDOW (f);
12404 #endif
12405
12406 if (pixmap_id > 0)
12407 {
12408 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
12409 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
12410 }
12411 else
12412 {
12413 /* It seems there is no way to turn off use of an icon pixmap.
12414 The following line does it, only if no icon has yet been created,
12415 for some window managers. But with mwm it crashes.
12416 Some people say it should clear the IconPixmapHint bit in this case,
12417 but that doesn't work, and the X consortium said it isn't the
12418 right thing at all. Since there is no way to win,
12419 best to explicitly give up. */
12420 #if 0
12421 f->output_data.x->wm_hints.icon_pixmap = None;
12422 #else
12423 return;
12424 #endif
12425 }
12426
12427 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12428
12429 {
12430 Arg al[1];
12431 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12432 XtSetValues (f->output_data.x->widget, al, 1);
12433 }
12434
12435 #else /* not USE_X_TOOLKIT */
12436
12437 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12438 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12439
12440 #endif /* not USE_X_TOOLKIT */
12441 }
12442
12443 void
12444 x_wm_set_icon_position (f, icon_x, icon_y)
12445 struct frame *f;
12446 int icon_x, icon_y;
12447 {
12448 #ifdef USE_X_TOOLKIT
12449 Window window = XtWindow (f->output_data.x->widget);
12450 #else
12451 Window window = FRAME_X_WINDOW (f);
12452 #endif
12453
12454 f->output_data.x->wm_hints.flags |= IconPositionHint;
12455 f->output_data.x->wm_hints.icon_x = icon_x;
12456 f->output_data.x->wm_hints.icon_y = icon_y;
12457
12458 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12459 }
12460
12461 \f
12462 /***********************************************************************
12463 Fonts
12464 ***********************************************************************/
12465
12466 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
12467
12468 struct font_info *
12469 x_get_font_info (f, font_idx)
12470 FRAME_PTR f;
12471 int font_idx;
12472 {
12473 return (FRAME_X_FONT_TABLE (f) + font_idx);
12474 }
12475
12476
12477 /* Return a list of names of available fonts matching PATTERN on frame
12478 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
12479 to be listed. Frame F NULL means we have not yet created any
12480 frame on X, and consult the first display in x_display_list.
12481 MAXNAMES sets a limit on how many fonts to match. */
12482
12483 Lisp_Object
12484 x_list_fonts (f, pattern, size, maxnames)
12485 FRAME_PTR f;
12486 Lisp_Object pattern;
12487 int size;
12488 int maxnames;
12489 {
12490 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12491 Lisp_Object tem, second_best;
12492 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
12493 int try_XLoadQueryFont = 0;
12494 int count;
12495
12496 patterns = Fassoc (pattern, Valternate_fontname_alist);
12497 if (NILP (patterns))
12498 patterns = Fcons (pattern, Qnil);
12499
12500 if (maxnames == 1 && !size)
12501 /* We can return any single font matching PATTERN. */
12502 try_XLoadQueryFont = 1;
12503
12504 for (; CONSP (patterns); patterns = XCDR (patterns))
12505 {
12506 int num_fonts;
12507 char **names = NULL;
12508
12509 pattern = XCAR (patterns);
12510 /* See if we cached the result for this particular query.
12511 The cache is an alist of the form:
12512 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
12513 */
12514 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
12515 key = Fcons (pattern, make_number (maxnames)),
12516 !NILP (list = Fassoc (key, tem))))
12517 {
12518 list = Fcdr_safe (list);
12519 /* We have a cashed list. Don't have to get the list again. */
12520 goto label_cached;
12521 }
12522
12523 /* At first, put PATTERN in the cache. */
12524
12525 BLOCK_INPUT;
12526 count = x_catch_errors (dpy);
12527
12528 if (try_XLoadQueryFont)
12529 {
12530 XFontStruct *font;
12531 unsigned long value;
12532
12533 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
12534 if (x_had_errors_p (dpy))
12535 {
12536 /* This error is perhaps due to insufficient memory on X
12537 server. Let's just ignore it. */
12538 font = NULL;
12539 x_clear_errors (dpy);
12540 }
12541
12542 if (font
12543 && XGetFontProperty (font, XA_FONT, &value))
12544 {
12545 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12546 int len = strlen (name);
12547 char *tmp;
12548
12549 /* If DXPC (a Differential X Protocol Compressor)
12550 Ver.3.7 is running, XGetAtomName will return null
12551 string. We must avoid such a name. */
12552 if (len == 0)
12553 try_XLoadQueryFont = 0;
12554 else
12555 {
12556 num_fonts = 1;
12557 names = (char **) alloca (sizeof (char *));
12558 /* Some systems only allow alloca assigned to a
12559 simple var. */
12560 tmp = (char *) alloca (len + 1); names[0] = tmp;
12561 bcopy (name, names[0], len + 1);
12562 XFree (name);
12563 }
12564 }
12565 else
12566 try_XLoadQueryFont = 0;
12567
12568 if (font)
12569 XFreeFont (dpy, font);
12570 }
12571
12572 if (!try_XLoadQueryFont)
12573 {
12574 /* We try at least 10 fonts because XListFonts will return
12575 auto-scaled fonts at the head. */
12576 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12577 &num_fonts);
12578 if (x_had_errors_p (dpy))
12579 {
12580 /* This error is perhaps due to insufficient memory on X
12581 server. Let's just ignore it. */
12582 names = NULL;
12583 x_clear_errors (dpy);
12584 }
12585 }
12586
12587 x_uncatch_errors (dpy, count);
12588 UNBLOCK_INPUT;
12589
12590 if (names)
12591 {
12592 int i;
12593
12594 /* Make a list of all the fonts we got back.
12595 Store that in the font cache for the display. */
12596 for (i = 0; i < num_fonts; i++)
12597 {
12598 int width = 0;
12599 char *p = names[i];
12600 int average_width = -1, dashes = 0;
12601
12602 /* Count the number of dashes in NAMES[I]. If there are
12603 14 dashes, and the field value following 12th dash
12604 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12605 is usually too ugly to be used for editing. Let's
12606 ignore it. */
12607 while (*p)
12608 if (*p++ == '-')
12609 {
12610 dashes++;
12611 if (dashes == 7) /* PIXEL_SIZE field */
12612 width = atoi (p);
12613 else if (dashes == 12) /* AVERAGE_WIDTH field */
12614 average_width = atoi (p);
12615 }
12616 if (dashes < 14 || average_width != 0)
12617 {
12618 tem = build_string (names[i]);
12619 if (NILP (Fassoc (tem, list)))
12620 {
12621 if (STRINGP (Vx_pixel_size_width_font_regexp)
12622 && ((fast_c_string_match_ignore_case
12623 (Vx_pixel_size_width_font_regexp, names[i]))
12624 >= 0))
12625 /* We can set the value of PIXEL_SIZE to the
12626 width of this font. */
12627 list = Fcons (Fcons (tem, make_number (width)), list);
12628 else
12629 /* For the moment, width is not known. */
12630 list = Fcons (Fcons (tem, Qnil), list);
12631 }
12632 }
12633 }
12634 if (!try_XLoadQueryFont)
12635 XFreeFontNames (names);
12636 }
12637
12638 /* Now store the result in the cache. */
12639 if (f != NULL)
12640 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
12641 = Fcons (Fcons (key, list),
12642 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
12643
12644 label_cached:
12645 if (NILP (list)) continue; /* Try the remaining alternatives. */
12646
12647 newlist = second_best = Qnil;
12648 /* Make a list of the fonts that have the right width. */
12649 for (; CONSP (list); list = XCDR (list))
12650 {
12651 int found_size;
12652
12653 tem = XCAR (list);
12654
12655 if (!CONSP (tem) || NILP (XCAR (tem)))
12656 continue;
12657 if (!size)
12658 {
12659 newlist = Fcons (XCAR (tem), newlist);
12660 continue;
12661 }
12662
12663 if (!INTEGERP (XCDR (tem)))
12664 {
12665 /* Since we have not yet known the size of this font, we
12666 must try slow function call XLoadQueryFont. */
12667 XFontStruct *thisinfo;
12668
12669 BLOCK_INPUT;
12670 count = x_catch_errors (dpy);
12671 thisinfo = XLoadQueryFont (dpy,
12672 XSTRING (XCAR (tem))->data);
12673 if (x_had_errors_p (dpy))
12674 {
12675 /* This error is perhaps due to insufficient memory on X
12676 server. Let's just ignore it. */
12677 thisinfo = NULL;
12678 x_clear_errors (dpy);
12679 }
12680 x_uncatch_errors (dpy, count);
12681 UNBLOCK_INPUT;
12682
12683 if (thisinfo)
12684 {
12685 XCDR (tem)
12686 = (thisinfo->min_bounds.width == 0
12687 ? make_number (0)
12688 : make_number (thisinfo->max_bounds.width));
12689 XFreeFont (dpy, thisinfo);
12690 }
12691 else
12692 /* For unknown reason, the previous call of XListFont had
12693 returned a font which can't be opened. Record the size
12694 as 0 not to try to open it again. */
12695 XCDR (tem) = make_number (0);
12696 }
12697
12698 found_size = XINT (XCDR (tem));
12699 if (found_size == size)
12700 newlist = Fcons (XCAR (tem), newlist);
12701 else if (found_size > 0)
12702 {
12703 if (NILP (second_best))
12704 second_best = tem;
12705 else if (found_size < size)
12706 {
12707 if (XINT (XCDR (second_best)) > size
12708 || XINT (XCDR (second_best)) < found_size)
12709 second_best = tem;
12710 }
12711 else
12712 {
12713 if (XINT (XCDR (second_best)) > size
12714 && XINT (XCDR (second_best)) > found_size)
12715 second_best = tem;
12716 }
12717 }
12718 }
12719 if (!NILP (newlist))
12720 break;
12721 else if (!NILP (second_best))
12722 {
12723 newlist = Fcons (XCAR (second_best), Qnil);
12724 break;
12725 }
12726 }
12727
12728 return newlist;
12729 }
12730
12731
12732 #if GLYPH_DEBUG
12733
12734 /* Check that FONT is valid on frame F. It is if it can be found in F's
12735 font table. */
12736
12737 static void
12738 x_check_font (f, font)
12739 struct frame *f;
12740 XFontStruct *font;
12741 {
12742 int i;
12743 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12744
12745 xassert (font != NULL);
12746
12747 for (i = 0; i < dpyinfo->n_fonts; i++)
12748 if (dpyinfo->font_table[i].name
12749 && font == dpyinfo->font_table[i].font)
12750 break;
12751
12752 xassert (i < dpyinfo->n_fonts);
12753 }
12754
12755 #endif /* GLYPH_DEBUG != 0 */
12756
12757 /* Set *W to the minimum width, *H to the minimum font height of FONT.
12758 Note: There are (broken) X fonts out there with invalid XFontStruct
12759 min_bounds contents. For example, handa@etl.go.jp reports that
12760 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
12761 have font->min_bounds.width == 0. */
12762
12763 static INLINE void
12764 x_font_min_bounds (font, w, h)
12765 XFontStruct *font;
12766 int *w, *h;
12767 {
12768 *h = FONT_HEIGHT (font);
12769 *w = font->min_bounds.width;
12770
12771 /* Try to handle the case where FONT->min_bounds has invalid
12772 contents. Since the only font known to have invalid min_bounds
12773 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
12774 if (*w <= 0)
12775 *w = font->max_bounds.width;
12776 }
12777
12778
12779 /* Compute the smallest character width and smallest font height over
12780 all fonts available on frame F. Set the members smallest_char_width
12781 and smallest_font_height in F's x_display_info structure to
12782 the values computed. Value is non-zero if smallest_font_height or
12783 smallest_char_width become smaller than they were before. */
12784
12785 static int
12786 x_compute_min_glyph_bounds (f)
12787 struct frame *f;
12788 {
12789 int i;
12790 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12791 XFontStruct *font;
12792 int old_width = dpyinfo->smallest_char_width;
12793 int old_height = dpyinfo->smallest_font_height;
12794
12795 dpyinfo->smallest_font_height = 100000;
12796 dpyinfo->smallest_char_width = 100000;
12797
12798 for (i = 0; i < dpyinfo->n_fonts; ++i)
12799 if (dpyinfo->font_table[i].name)
12800 {
12801 struct font_info *fontp = dpyinfo->font_table + i;
12802 int w, h;
12803
12804 font = (XFontStruct *) fontp->font;
12805 xassert (font != (XFontStruct *) ~0);
12806 x_font_min_bounds (font, &w, &h);
12807
12808 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
12809 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
12810 }
12811
12812 xassert (dpyinfo->smallest_char_width > 0
12813 && dpyinfo->smallest_font_height > 0);
12814
12815 return (dpyinfo->n_fonts == 1
12816 || dpyinfo->smallest_char_width < old_width
12817 || dpyinfo->smallest_font_height < old_height);
12818 }
12819
12820
12821 /* Load font named FONTNAME of the size SIZE for frame F, and return a
12822 pointer to the structure font_info while allocating it dynamically.
12823 If SIZE is 0, load any size of font.
12824 If loading is failed, return NULL. */
12825
12826 struct font_info *
12827 x_load_font (f, fontname, size)
12828 struct frame *f;
12829 register char *fontname;
12830 int size;
12831 {
12832 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12833 Lisp_Object font_names;
12834 int count;
12835
12836 /* Get a list of all the fonts that match this name. Once we
12837 have a list of matching fonts, we compare them against the fonts
12838 we already have by comparing names. */
12839 font_names = x_list_fonts (f, build_string (fontname), size, 1);
12840
12841 if (!NILP (font_names))
12842 {
12843 Lisp_Object tail;
12844 int i;
12845
12846 for (i = 0; i < dpyinfo->n_fonts; i++)
12847 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
12848 if (dpyinfo->font_table[i].name
12849 && (!strcmp (dpyinfo->font_table[i].name,
12850 XSTRING (XCAR (tail))->data)
12851 || !strcmp (dpyinfo->font_table[i].full_name,
12852 XSTRING (XCAR (tail))->data)))
12853 return (dpyinfo->font_table + i);
12854 }
12855
12856 /* Load the font and add it to the table. */
12857 {
12858 char *full_name;
12859 XFontStruct *font;
12860 struct font_info *fontp;
12861 unsigned long value;
12862 int i;
12863
12864 /* If we have found fonts by x_list_font, load one of them. If
12865 not, we still try to load a font by the name given as FONTNAME
12866 because XListFonts (called in x_list_font) of some X server has
12867 a bug of not finding a font even if the font surely exists and
12868 is loadable by XLoadQueryFont. */
12869 if (size > 0 && !NILP (font_names))
12870 fontname = (char *) XSTRING (XCAR (font_names))->data;
12871
12872 BLOCK_INPUT;
12873 count = x_catch_errors (FRAME_X_DISPLAY (f));
12874 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
12875 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12876 {
12877 /* This error is perhaps due to insufficient memory on X
12878 server. Let's just ignore it. */
12879 font = NULL;
12880 x_clear_errors (FRAME_X_DISPLAY (f));
12881 }
12882 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12883 UNBLOCK_INPUT;
12884 if (!font)
12885 return NULL;
12886
12887 /* Find a free slot in the font table. */
12888 for (i = 0; i < dpyinfo->n_fonts; ++i)
12889 if (dpyinfo->font_table[i].name == NULL)
12890 break;
12891
12892 /* If no free slot found, maybe enlarge the font table. */
12893 if (i == dpyinfo->n_fonts
12894 && dpyinfo->n_fonts == dpyinfo->font_table_size)
12895 {
12896 int sz;
12897 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
12898 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
12899 dpyinfo->font_table
12900 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
12901 }
12902
12903 fontp = dpyinfo->font_table + i;
12904 if (i == dpyinfo->n_fonts)
12905 ++dpyinfo->n_fonts;
12906
12907 /* Now fill in the slots of *FONTP. */
12908 BLOCK_INPUT;
12909 fontp->font = font;
12910 fontp->font_idx = i;
12911 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
12912 bcopy (fontname, fontp->name, strlen (fontname) + 1);
12913
12914 /* Try to get the full name of FONT. Put it in FULL_NAME. */
12915 full_name = 0;
12916 if (XGetFontProperty (font, XA_FONT, &value))
12917 {
12918 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
12919 char *p = name;
12920 int dashes = 0;
12921
12922 /* Count the number of dashes in the "full name".
12923 If it is too few, this isn't really the font's full name,
12924 so don't use it.
12925 In X11R4, the fonts did not come with their canonical names
12926 stored in them. */
12927 while (*p)
12928 {
12929 if (*p == '-')
12930 dashes++;
12931 p++;
12932 }
12933
12934 if (dashes >= 13)
12935 {
12936 full_name = (char *) xmalloc (p - name + 1);
12937 bcopy (name, full_name, p - name + 1);
12938 }
12939
12940 XFree (name);
12941 }
12942
12943 if (full_name != 0)
12944 fontp->full_name = full_name;
12945 else
12946 fontp->full_name = fontp->name;
12947
12948 fontp->size = font->max_bounds.width;
12949 fontp->height = FONT_HEIGHT (font);
12950 {
12951 /* For some font, ascent and descent in max_bounds field is
12952 larger than the above value. */
12953 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
12954 if (max_height > fontp->height)
12955 fontp->height = max_height;
12956 }
12957
12958 if (NILP (font_names))
12959 {
12960 /* We come here because of a bug of XListFonts mentioned at
12961 the head of this block. Let's store this information in
12962 the cache for x_list_fonts. */
12963 Lisp_Object lispy_name = build_string (fontname);
12964 Lisp_Object lispy_full_name = build_string (fontp->full_name);
12965
12966 XCDR (dpyinfo->name_list_element)
12967 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
12968 Fcons (Fcons (lispy_full_name,
12969 make_number (fontp->size)),
12970 Qnil)),
12971 XCDR (dpyinfo->name_list_element));
12972 if (full_name)
12973 XCDR (dpyinfo->name_list_element)
12974 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
12975 Fcons (Fcons (lispy_full_name,
12976 make_number (fontp->size)),
12977 Qnil)),
12978 XCDR (dpyinfo->name_list_element));
12979 }
12980
12981 /* The slot `encoding' specifies how to map a character
12982 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
12983 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
12984 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
12985 2:0xA020..0xFF7F). For the moment, we don't know which charset
12986 uses this font. So, we set information in fontp->encoding[1]
12987 which is never used by any charset. If mapping can't be
12988 decided, set FONT_ENCODING_NOT_DECIDED. */
12989 fontp->encoding[1]
12990 = (font->max_byte1 == 0
12991 /* 1-byte font */
12992 ? (font->min_char_or_byte2 < 0x80
12993 ? (font->max_char_or_byte2 < 0x80
12994 ? 0 /* 0x20..0x7F */
12995 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
12996 : 1) /* 0xA0..0xFF */
12997 /* 2-byte font */
12998 : (font->min_byte1 < 0x80
12999 ? (font->max_byte1 < 0x80
13000 ? (font->min_char_or_byte2 < 0x80
13001 ? (font->max_char_or_byte2 < 0x80
13002 ? 0 /* 0x2020..0x7F7F */
13003 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
13004 : 3) /* 0x20A0..0x7FFF */
13005 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
13006 : (font->min_char_or_byte2 < 0x80
13007 ? (font->max_char_or_byte2 < 0x80
13008 ? 2 /* 0xA020..0xFF7F */
13009 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
13010 : 1))); /* 0xA0A0..0xFFFF */
13011
13012 fontp->baseline_offset
13013 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
13014 ? (long) value : 0);
13015 fontp->relative_compose
13016 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
13017 ? (long) value : 0);
13018 fontp->default_ascent
13019 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
13020 ? (long) value : 0);
13021
13022 /* Set global flag fonts_changed_p to non-zero if the font loaded
13023 has a character with a smaller width than any other character
13024 before, or if the font loaded has a smalle>r height than any
13025 other font loaded before. If this happens, it will make a
13026 glyph matrix reallocation necessary. */
13027 fonts_changed_p = x_compute_min_glyph_bounds (f);
13028 UNBLOCK_INPUT;
13029 return fontp;
13030 }
13031 }
13032
13033
13034 /* Return a pointer to struct font_info of a font named FONTNAME for
13035 frame F. If no such font is loaded, return NULL. */
13036
13037 struct font_info *
13038 x_query_font (f, fontname)
13039 struct frame *f;
13040 register char *fontname;
13041 {
13042 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13043 int i;
13044
13045 for (i = 0; i < dpyinfo->n_fonts; i++)
13046 if (dpyinfo->font_table[i].name
13047 && (!strcmp (dpyinfo->font_table[i].name, fontname)
13048 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
13049 return (dpyinfo->font_table + i);
13050 return NULL;
13051 }
13052
13053
13054 /* Find a CCL program for a font specified by FONTP, and set the member
13055 `encoder' of the structure. */
13056
13057 void
13058 x_find_ccl_program (fontp)
13059 struct font_info *fontp;
13060 {
13061 Lisp_Object list, elt;
13062
13063 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
13064 {
13065 elt = XCAR (list);
13066 if (CONSP (elt)
13067 && STRINGP (XCAR (elt))
13068 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
13069 >= 0))
13070 break;
13071 }
13072 if (! NILP (list))
13073 {
13074 struct ccl_program *ccl
13075 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
13076
13077 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
13078 xfree (ccl);
13079 else
13080 fontp->font_encoder = ccl;
13081 }
13082 }
13083
13084
13085 \f
13086 /***********************************************************************
13087 Initialization
13088 ***********************************************************************/
13089
13090 #ifdef USE_X_TOOLKIT
13091 static XrmOptionDescRec emacs_options[] = {
13092 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13093 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13094
13095 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13096 XrmoptionSepArg, NULL},
13097 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13098
13099 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13100 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13101 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13102 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13103 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13104 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13105 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13106 };
13107 #endif /* USE_X_TOOLKIT */
13108
13109 static int x_initialized;
13110
13111 #ifdef MULTI_KBOARD
13112 /* Test whether two display-name strings agree up to the dot that separates
13113 the screen number from the server number. */
13114 static int
13115 same_x_server (name1, name2)
13116 char *name1, *name2;
13117 {
13118 int seen_colon = 0;
13119 unsigned char *system_name = XSTRING (Vsystem_name)->data;
13120 int system_name_length = strlen (system_name);
13121 int length_until_period = 0;
13122
13123 while (system_name[length_until_period] != 0
13124 && system_name[length_until_period] != '.')
13125 length_until_period++;
13126
13127 /* Treat `unix' like an empty host name. */
13128 if (! strncmp (name1, "unix:", 5))
13129 name1 += 4;
13130 if (! strncmp (name2, "unix:", 5))
13131 name2 += 4;
13132 /* Treat this host's name like an empty host name. */
13133 if (! strncmp (name1, system_name, system_name_length)
13134 && name1[system_name_length] == ':')
13135 name1 += system_name_length;
13136 if (! strncmp (name2, system_name, system_name_length)
13137 && name2[system_name_length] == ':')
13138 name2 += system_name_length;
13139 /* Treat this host's domainless name like an empty host name. */
13140 if (! strncmp (name1, system_name, length_until_period)
13141 && name1[length_until_period] == ':')
13142 name1 += length_until_period;
13143 if (! strncmp (name2, system_name, length_until_period)
13144 && name2[length_until_period] == ':')
13145 name2 += length_until_period;
13146
13147 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13148 {
13149 if (*name1 == ':')
13150 seen_colon++;
13151 if (seen_colon && *name1 == '.')
13152 return 1;
13153 }
13154 return (seen_colon
13155 && (*name1 == '.' || *name1 == '\0')
13156 && (*name2 == '.' || *name2 == '\0'));
13157 }
13158 #endif
13159
13160 struct x_display_info *
13161 x_term_init (display_name, xrm_option, resource_name)
13162 Lisp_Object display_name;
13163 char *xrm_option;
13164 char *resource_name;
13165 {
13166 int connection;
13167 Display *dpy;
13168 struct x_display_info *dpyinfo;
13169 XrmDatabase xrdb;
13170
13171 BLOCK_INPUT;
13172
13173 if (!x_initialized)
13174 {
13175 x_initialize ();
13176 x_initialized = 1;
13177 }
13178
13179 #ifdef USE_X_TOOLKIT
13180 /* weiner@footloose.sps.mot.com reports that this causes
13181 errors with X11R5:
13182 X protocol error: BadAtom (invalid Atom parameter)
13183 on protocol request 18skiloaf.
13184 So let's not use it until R6. */
13185 #ifdef HAVE_X11XTR6
13186 XtSetLanguageProc (NULL, NULL, NULL);
13187 #endif
13188
13189 {
13190 int argc = 0;
13191 char *argv[3];
13192
13193 argv[0] = "";
13194 argc = 1;
13195 if (xrm_option)
13196 {
13197 argv[argc++] = "-xrm";
13198 argv[argc++] = xrm_option;
13199 }
13200 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
13201 resource_name, EMACS_CLASS,
13202 emacs_options, XtNumber (emacs_options),
13203 &argc, argv);
13204
13205 #ifdef HAVE_X11XTR6
13206 /* I think this is to compensate for XtSetLanguageProc. */
13207 fixup_locale ();
13208 #endif
13209 }
13210
13211 #else /* not USE_X_TOOLKIT */
13212 #ifdef HAVE_X11R5
13213 XSetLocaleModifiers ("");
13214 #endif
13215 dpy = XOpenDisplay (XSTRING (display_name)->data);
13216 #endif /* not USE_X_TOOLKIT */
13217
13218 /* Detect failure. */
13219 if (dpy == 0)
13220 {
13221 UNBLOCK_INPUT;
13222 return 0;
13223 }
13224
13225 /* We have definitely succeeded. Record the new connection. */
13226
13227 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
13228
13229 #ifdef MULTI_KBOARD
13230 {
13231 struct x_display_info *share;
13232 Lisp_Object tail;
13233
13234 for (share = x_display_list, tail = x_display_name_list; share;
13235 share = share->next, tail = XCDR (tail))
13236 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
13237 XSTRING (display_name)->data))
13238 break;
13239 if (share)
13240 dpyinfo->kboard = share->kboard;
13241 else
13242 {
13243 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
13244 init_kboard (dpyinfo->kboard);
13245 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
13246 {
13247 char *vendor = ServerVendor (dpy);
13248 UNBLOCK_INPUT;
13249 dpyinfo->kboard->Vsystem_key_alist
13250 = call1 (Qvendor_specific_keysyms,
13251 build_string (vendor ? vendor : ""));
13252 BLOCK_INPUT;
13253 }
13254
13255 dpyinfo->kboard->next_kboard = all_kboards;
13256 all_kboards = dpyinfo->kboard;
13257 /* Don't let the initial kboard remain current longer than necessary.
13258 That would cause problems if a file loaded on startup tries to
13259 prompt in the mini-buffer. */
13260 if (current_kboard == initial_kboard)
13261 current_kboard = dpyinfo->kboard;
13262 }
13263 dpyinfo->kboard->reference_count++;
13264 }
13265 #endif
13266
13267 /* Put this display on the chain. */
13268 dpyinfo->next = x_display_list;
13269 x_display_list = dpyinfo;
13270
13271 /* Put it on x_display_name_list as well, to keep them parallel. */
13272 x_display_name_list = Fcons (Fcons (display_name, Qnil),
13273 x_display_name_list);
13274 dpyinfo->name_list_element = XCAR (x_display_name_list);
13275
13276 dpyinfo->display = dpy;
13277
13278 #if 0
13279 XSetAfterFunction (x_current_display, x_trace_wire);
13280 #endif /* ! 0 */
13281
13282 dpyinfo->x_id_name
13283 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13284 + STRING_BYTES (XSTRING (Vsystem_name))
13285 + 2);
13286 sprintf (dpyinfo->x_id_name, "%s@%s",
13287 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
13288
13289 /* Figure out which modifier bits mean what. */
13290 x_find_modifier_meanings (dpyinfo);
13291
13292 /* Get the scroll bar cursor. */
13293 dpyinfo->vertical_scroll_bar_cursor
13294 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
13295
13296 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13297 resource_name, EMACS_CLASS);
13298 #ifdef HAVE_XRMSETDATABASE
13299 XrmSetDatabase (dpyinfo->display, xrdb);
13300 #else
13301 dpyinfo->display->db = xrdb;
13302 #endif
13303 /* Put the rdb where we can find it in a way that works on
13304 all versions. */
13305 dpyinfo->xrdb = xrdb;
13306
13307 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13308 DefaultScreen (dpyinfo->display));
13309 select_visual (dpyinfo);
13310 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
13311 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13312 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13313 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13314 dpyinfo->grabbed = 0;
13315 dpyinfo->reference_count = 0;
13316 dpyinfo->icon_bitmap_id = -1;
13317 dpyinfo->font_table = NULL;
13318 dpyinfo->n_fonts = 0;
13319 dpyinfo->font_table_size = 0;
13320 dpyinfo->bitmaps = 0;
13321 dpyinfo->bitmaps_size = 0;
13322 dpyinfo->bitmaps_last = 0;
13323 dpyinfo->scratch_cursor_gc = 0;
13324 dpyinfo->mouse_face_mouse_frame = 0;
13325 dpyinfo->mouse_face_deferred_gc = 0;
13326 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13327 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
13328 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
13329 dpyinfo->mouse_face_window = Qnil;
13330 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13331 dpyinfo->mouse_face_defer = 0;
13332 dpyinfo->x_focus_frame = 0;
13333 dpyinfo->x_focus_event_frame = 0;
13334 dpyinfo->x_highlight_frame = 0;
13335 dpyinfo->image_cache = make_image_cache ();
13336
13337 /* See if a private colormap is requested. */
13338 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13339 {
13340 if (dpyinfo->visual->class == PseudoColor)
13341 {
13342 Lisp_Object value;
13343 value = display_x_get_resource (dpyinfo,
13344 build_string ("privateColormap"),
13345 build_string ("PrivateColormap"),
13346 Qnil, Qnil);
13347 if (STRINGP (value)
13348 && (!strcmp (XSTRING (value)->data, "true")
13349 || !strcmp (XSTRING (value)->data, "on")))
13350 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13351 }
13352 }
13353 else
13354 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13355 dpyinfo->visual, AllocNone);
13356
13357 {
13358 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13359 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13360 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13361 dpyinfo->resy = pixels * 25.4 / mm;
13362 pixels = DisplayWidth (dpyinfo->display, screen_number);
13363 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13364 dpyinfo->resx = pixels * 25.4 / mm;
13365 }
13366
13367 dpyinfo->Xatom_wm_protocols
13368 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13369 dpyinfo->Xatom_wm_take_focus
13370 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13371 dpyinfo->Xatom_wm_save_yourself
13372 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13373 dpyinfo->Xatom_wm_delete_window
13374 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13375 dpyinfo->Xatom_wm_change_state
13376 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13377 dpyinfo->Xatom_wm_configure_denied
13378 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13379 dpyinfo->Xatom_wm_window_moved
13380 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13381 dpyinfo->Xatom_editres
13382 = XInternAtom (dpyinfo->display, "Editres", False);
13383 dpyinfo->Xatom_CLIPBOARD
13384 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13385 dpyinfo->Xatom_TIMESTAMP
13386 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13387 dpyinfo->Xatom_TEXT
13388 = XInternAtom (dpyinfo->display, "TEXT", False);
13389 dpyinfo->Xatom_COMPOUND_TEXT
13390 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
13391 dpyinfo->Xatom_DELETE
13392 = XInternAtom (dpyinfo->display, "DELETE", False);
13393 dpyinfo->Xatom_MULTIPLE
13394 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13395 dpyinfo->Xatom_INCR
13396 = XInternAtom (dpyinfo->display, "INCR", False);
13397 dpyinfo->Xatom_EMACS_TMP
13398 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13399 dpyinfo->Xatom_TARGETS
13400 = XInternAtom (dpyinfo->display, "TARGETS", False);
13401 dpyinfo->Xatom_NULL
13402 = XInternAtom (dpyinfo->display, "NULL", False);
13403 dpyinfo->Xatom_ATOM_PAIR
13404 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
13405 /* For properties of font. */
13406 dpyinfo->Xatom_PIXEL_SIZE
13407 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13408 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13409 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13410 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13411 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
13412 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13413 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
13414
13415 /* Ghostscript support. */
13416 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13417 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13418
13419 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13420 False);
13421
13422 dpyinfo->cut_buffers_initialized = 0;
13423
13424 connection = ConnectionNumber (dpyinfo->display);
13425 dpyinfo->connection = connection;
13426
13427 {
13428 char null_bits[1];
13429
13430 null_bits[0] = 0x00;
13431
13432 dpyinfo->null_pixel
13433 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13434 null_bits, 1, 1, (long) 0, (long) 0,
13435 1);
13436 }
13437
13438 {
13439 extern int gray_bitmap_width, gray_bitmap_height;
13440 extern unsigned char *gray_bitmap_bits;
13441 dpyinfo->gray
13442 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13443 gray_bitmap_bits,
13444 gray_bitmap_width, gray_bitmap_height,
13445 (unsigned long) 1, (unsigned long) 0, 1);
13446 }
13447
13448 #ifdef HAVE_X_I18N
13449 xim_initialize (dpyinfo, resource_name);
13450 #endif
13451
13452 #ifdef subprocesses
13453 /* This is only needed for distinguishing keyboard and process input. */
13454 if (connection != 0)
13455 add_keyboard_wait_descriptor (connection);
13456 #endif
13457
13458 #ifndef F_SETOWN_BUG
13459 #ifdef F_SETOWN
13460 #ifdef F_SETOWN_SOCK_NEG
13461 /* stdin is a socket here */
13462 fcntl (connection, F_SETOWN, -getpid ());
13463 #else /* ! defined (F_SETOWN_SOCK_NEG) */
13464 fcntl (connection, F_SETOWN, getpid ());
13465 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
13466 #endif /* ! defined (F_SETOWN) */
13467 #endif /* F_SETOWN_BUG */
13468
13469 #ifdef SIGIO
13470 if (interrupt_input)
13471 init_sigio (connection);
13472 #endif /* ! defined (SIGIO) */
13473
13474 #ifdef USE_LUCID
13475 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
13476 /* Make sure that we have a valid font for dialog boxes
13477 so that Xt does not crash. */
13478 {
13479 Display *dpy = dpyinfo->display;
13480 XrmValue d, fr, to;
13481 Font font;
13482 int count;
13483
13484 d.addr = (XPointer)&dpy;
13485 d.size = sizeof (Display *);
13486 fr.addr = XtDefaultFont;
13487 fr.size = sizeof (XtDefaultFont);
13488 to.size = sizeof (Font *);
13489 to.addr = (XPointer)&font;
13490 count = x_catch_errors (dpy);
13491 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13492 abort ();
13493 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13494 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
13495 x_uncatch_errors (dpy, count);
13496 }
13497 #endif
13498 #endif
13499
13500 /* See if we should run in synchronous mode. This is useful
13501 for debugging X code. */
13502 {
13503 Lisp_Object value;
13504 value = display_x_get_resource (dpyinfo,
13505 build_string ("synchronous"),
13506 build_string ("Synchronous"),
13507 Qnil, Qnil);
13508 if (STRINGP (value)
13509 && (!strcmp (XSTRING (value)->data, "true")
13510 || !strcmp (XSTRING (value)->data, "on")))
13511 XSynchronize (dpyinfo->display, True);
13512 }
13513
13514 UNBLOCK_INPUT;
13515
13516 return dpyinfo;
13517 }
13518 \f
13519 /* Get rid of display DPYINFO, assuming all frames are already gone,
13520 and without sending any more commands to the X server. */
13521
13522 void
13523 x_delete_display (dpyinfo)
13524 struct x_display_info *dpyinfo;
13525 {
13526 delete_keyboard_wait_descriptor (dpyinfo->connection);
13527
13528 /* Discard this display from x_display_name_list and x_display_list.
13529 We can't use Fdelq because that can quit. */
13530 if (! NILP (x_display_name_list)
13531 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13532 x_display_name_list = XCDR (x_display_name_list);
13533 else
13534 {
13535 Lisp_Object tail;
13536
13537 tail = x_display_name_list;
13538 while (CONSP (tail) && CONSP (XCDR (tail)))
13539 {
13540 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
13541 {
13542 XCDR (tail) = XCDR (XCDR (tail));
13543 break;
13544 }
13545 tail = XCDR (tail);
13546 }
13547 }
13548
13549 if (next_noop_dpyinfo == dpyinfo)
13550 next_noop_dpyinfo = dpyinfo->next;
13551
13552 if (x_display_list == dpyinfo)
13553 x_display_list = dpyinfo->next;
13554 else
13555 {
13556 struct x_display_info *tail;
13557
13558 for (tail = x_display_list; tail; tail = tail->next)
13559 if (tail->next == dpyinfo)
13560 tail->next = tail->next->next;
13561 }
13562
13563 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13564 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
13565 XrmDestroyDatabase (dpyinfo->xrdb);
13566 #endif
13567 #endif
13568 #ifdef MULTI_KBOARD
13569 if (--dpyinfo->kboard->reference_count == 0)
13570 delete_kboard (dpyinfo->kboard);
13571 #endif
13572 #ifdef HAVE_X_I18N
13573 if (dpyinfo->xim)
13574 xim_close_dpy (dpyinfo);
13575 #endif
13576
13577 xfree (dpyinfo->font_table);
13578 xfree (dpyinfo->x_id_name);
13579 xfree (dpyinfo);
13580 }
13581 \f
13582 /* Set up use of X before we make the first connection. */
13583
13584 static struct redisplay_interface x_redisplay_interface =
13585 {
13586 x_produce_glyphs,
13587 x_write_glyphs,
13588 x_insert_glyphs,
13589 x_clear_end_of_line,
13590 x_scroll_run,
13591 x_after_update_window_line,
13592 x_update_window_begin,
13593 x_update_window_end,
13594 XTcursor_to,
13595 x_flush,
13596 x_clear_mouse_face,
13597 x_get_glyph_overhangs,
13598 x_fix_overlapping_area
13599 };
13600
13601 void
13602 x_initialize ()
13603 {
13604 rif = &x_redisplay_interface;
13605
13606 clear_frame_hook = x_clear_frame;
13607 ins_del_lines_hook = x_ins_del_lines;
13608 change_line_highlight_hook = x_change_line_highlight;
13609 delete_glyphs_hook = x_delete_glyphs;
13610 ring_bell_hook = XTring_bell;
13611 reset_terminal_modes_hook = XTreset_terminal_modes;
13612 set_terminal_modes_hook = XTset_terminal_modes;
13613 update_begin_hook = x_update_begin;
13614 update_end_hook = x_update_end;
13615 set_terminal_window_hook = XTset_terminal_window;
13616 read_socket_hook = XTread_socket;
13617 frame_up_to_date_hook = XTframe_up_to_date;
13618 reassert_line_highlight_hook = XTreassert_line_highlight;
13619 mouse_position_hook = XTmouse_position;
13620 frame_rehighlight_hook = XTframe_rehighlight;
13621 frame_raise_lower_hook = XTframe_raise_lower;
13622 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13623 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13624 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13625 judge_scroll_bars_hook = XTjudge_scroll_bars;
13626 estimate_mode_line_height_hook = x_estimate_mode_line_height;
13627
13628 scroll_region_ok = 1; /* we'll scroll partial frames */
13629 char_ins_del_ok = 0; /* just as fast to write the line */
13630 line_ins_del_ok = 1; /* we'll just blt 'em */
13631 fast_clear_end_of_line = 1; /* X does this well */
13632 memory_below_frame = 0; /* we don't remember what scrolls
13633 off the bottom */
13634 baud_rate = 19200;
13635
13636 x_noop_count = 0;
13637 last_tool_bar_item = -1;
13638 any_help_event_p = 0;
13639
13640 /* Try to use interrupt input; if we can't, then start polling. */
13641 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13642
13643 #ifdef USE_X_TOOLKIT
13644 XtToolkitInitialize ();
13645 Xt_app_con = XtCreateApplicationContext ();
13646 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
13647
13648 /* Install an asynchronous timer that processes Xt timeout events
13649 every 0.1s. This is necessary because some widget sets use
13650 timeouts internally, for example the LessTif menu bar, or the
13651 Xaw3d scroll bar. When Xt timouts aren't processed, these
13652 widgets don't behave normally. */
13653 {
13654 EMACS_TIME interval;
13655 EMACS_SET_SECS_USECS (interval, 0, 100000);
13656 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13657 }
13658 #endif
13659
13660 #if USE_TOOLKIT_SCROLL_BARS
13661 xaw3d_arrow_scroll = False;
13662 xaw3d_pick_top = True;
13663 #endif
13664
13665 /* Note that there is no real way portable across R3/R4 to get the
13666 original error handler. */
13667 XSetErrorHandler (x_error_handler);
13668 XSetIOErrorHandler (x_io_error_quitter);
13669
13670 /* Disable Window Change signals; they are handled by X events. */
13671 #ifdef SIGWINCH
13672 signal (SIGWINCH, SIG_DFL);
13673 #endif /* ! defined (SIGWINCH) */
13674
13675 signal (SIGPIPE, x_connection_signal);
13676 }
13677
13678
13679 void
13680 syms_of_xterm ()
13681 {
13682 staticpro (&x_error_message_string);
13683 x_error_message_string = Qnil;
13684
13685 staticpro (&x_display_name_list);
13686 x_display_name_list = Qnil;
13687
13688 staticpro (&last_mouse_scroll_bar);
13689 last_mouse_scroll_bar = Qnil;
13690
13691 staticpro (&Qvendor_specific_keysyms);
13692 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
13693
13694 staticpro (&last_mouse_press_frame);
13695 last_mouse_press_frame = Qnil;
13696
13697 help_echo = Qnil;
13698 staticpro (&help_echo);
13699 help_echo_object = Qnil;
13700 staticpro (&help_echo_object);
13701 help_echo_window = Qnil;
13702 staticpro (&help_echo_window);
13703 previous_help_echo = Qnil;
13704 staticpro (&previous_help_echo);
13705 help_echo_pos = -1;
13706
13707 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
13708 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
13709 For example, if a block cursor is over a tab, it will be drawn as\n\
13710 wide as that tab on the display.");
13711 x_stretch_cursor_p = 0;
13712
13713 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
13714 "If not nil, Emacs uses toolkit scroll bars.");
13715 #if USE_TOOLKIT_SCROLL_BARS
13716 x_toolkit_scroll_bars_p = 1;
13717 #else
13718 x_toolkit_scroll_bars_p = 0;
13719 #endif
13720
13721 staticpro (&last_mouse_motion_frame);
13722 last_mouse_motion_frame = Qnil;
13723 }
13724
13725 #endif /* not HAVE_X_WINDOWS */
13726