]> code.delx.au - gnu-emacs/blob - src/xterm.c
(buffer_posn_from_coords): Adjust prototype.
[gnu-emacs] / src / xterm.c
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
23 /* Xt features made by Fred Pierresteguy. */
24
25 #include <config.h>
26
27 /* On 4.3 these lose if they come after xterm.h. */
28 /* Putting these at the beginning seems to be standard for other .c files. */
29 #include <signal.h>
30
31 #include <stdio.h>
32
33 #ifdef HAVE_X_WINDOWS
34
35 #include "lisp.h"
36 #include "blockinput.h"
37
38 /* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40 #include "syssignal.h"
41
42 /* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44 #include "xterm.h"
45 #include <X11/cursorfont.h>
46
47 /* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49 #ifndef makedev
50 #include <sys/types.h>
51 #endif /* makedev */
52
53 #ifdef BSD_SYSTEM
54 #include <sys/ioctl.h>
55 #endif /* ! defined (BSD_SYSTEM) */
56
57 #include "systty.h"
58 #include "systime.h"
59
60 #ifndef INCLUDED_FCNTL
61 #include <fcntl.h>
62 #endif
63 #include <ctype.h>
64 #include <errno.h>
65 #include <setjmp.h>
66 #include <sys/stat.h>
67 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68 /* #include <sys/param.h> */
69
70 #include "charset.h"
71 #include "coding.h"
72 #include "ccl.h"
73 #include "frame.h"
74 #include "dispextern.h"
75 #include "fontset.h"
76 #include "termhooks.h"
77 #include "termopts.h"
78 #include "termchar.h"
79 #if 0
80 #include "sink.h"
81 #include "sinkmask.h"
82 #endif /* ! 0 */
83 #include "gnu.h"
84 #include "disptab.h"
85 #include "buffer.h"
86 #include "window.h"
87 #include "keyboard.h"
88 #include "intervals.h"
89 #include "process.h"
90 #include "atimer.h"
91
92 #ifdef USE_X_TOOLKIT
93 #include <X11/Shell.h>
94 #endif
95
96 #ifdef HAVE_SYS_TIME_H
97 #include <sys/time.h>
98 #endif
99 #ifdef HAVE_UNISTD_H
100 #include <unistd.h>
101 #endif
102
103 #ifdef USE_X_TOOLKIT
104
105 extern void free_frame_menubar P_ ((struct frame *));
106 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
107 int));
108
109 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
110 #define HACK_EDITRES
111 extern void _XEditResCheckMessages ();
112 #endif /* not NO_EDITRES */
113
114 /* Include toolkit specific headers for the scroll bar widget. */
115
116 #ifdef USE_TOOLKIT_SCROLL_BARS
117 #if defined USE_MOTIF
118 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
119 #include <Xm/ScrollBar.h>
120 #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 #ifndef min
153 #define min(a,b) ((a) < (b) ? (a) : (b))
154 #endif
155 #ifndef max
156 #define max(a,b) ((a) > (b) ? (a) : (b))
157 #endif
158
159 #define abs(x) ((x) < 0 ? -(x) : (x))
160
161 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
162
163 \f
164 /* Bitmaps for truncated lines. */
165
166 enum bitmap_type
167 {
168 NO_BITMAP,
169 LEFT_TRUNCATION_BITMAP,
170 RIGHT_TRUNCATION_BITMAP,
171 OVERLAY_ARROW_BITMAP,
172 CONTINUED_LINE_BITMAP,
173 CONTINUATION_LINE_BITMAP,
174 ZV_LINE_BITMAP
175 };
176
177 /* Bitmap drawn to indicate lines not displaying text if
178 `indicate-empty-lines' is non-nil. */
179
180 #define zv_width 8
181 #define zv_height 8
182 static unsigned char zv_bits[] = {
183 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
184
185 /* An arrow like this: `<-'. */
186
187 #define left_width 8
188 #define left_height 8
189 static unsigned char left_bits[] = {
190 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
191
192 /* Right truncation arrow bitmap `->'. */
193
194 #define right_width 8
195 #define right_height 8
196 static unsigned char right_bits[] = {
197 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
198
199 /* Marker for continued lines. */
200
201 #define continued_width 8
202 #define continued_height 8
203 static unsigned char continued_bits[] = {
204 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
205
206 /* Marker for continuation lines. */
207
208 #define continuation_width 8
209 #define continuation_height 8
210 static unsigned char continuation_bits[] = {
211 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
212
213 /* Overlay arrow bitmap. */
214
215 #if 0
216 /* A bomb. */
217 #define ov_width 8
218 #define ov_height 8
219 static unsigned char ov_bits[] = {
220 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
221 #else
222 /* A triangular arrow. */
223 #define ov_width 8
224 #define ov_height 8
225 static unsigned char ov_bits[] = {
226 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
227
228 #endif
229
230 extern Lisp_Object Qhelp_echo;
231
232 \f
233 /* Non-nil means Emacs uses toolkit scroll bars. */
234
235 Lisp_Object Vx_toolkit_scroll_bars;
236
237 /* If a string, XTread_socket generates an event to display that string.
238 (The display is done in read_char.) */
239
240 static Lisp_Object help_echo;
241 static Lisp_Object help_echo_window;
242 static Lisp_Object help_echo_object;
243 static int help_echo_pos;
244
245 /* Temporary variable for XTread_socket. */
246
247 static Lisp_Object previous_help_echo;
248
249 /* Non-zero means that a HELP_EVENT has been generated since Emacs
250 start. */
251
252 static int any_help_event_p;
253
254 /* Non-zero means draw block and hollow cursor as wide as the glyph
255 under it. For example, if a block cursor is over a tab, it will be
256 drawn as wide as that tab on the display. */
257
258 int x_stretch_cursor_p;
259
260 /* This is a chain of structures for all the X displays currently in
261 use. */
262
263 struct x_display_info *x_display_list;
264
265 /* This is a list of cons cells, each of the form (NAME
266 . FONT-LIST-CACHE), one for each element of x_display_list and in
267 the same order. NAME is the name of the frame. FONT-LIST-CACHE
268 records previous values returned by x-list-fonts. */
269
270 Lisp_Object x_display_name_list;
271
272 /* Frame being updated by update_frame. This is declared in term.c.
273 This is set by update_begin and looked at by all the XT functions.
274 It is zero while not inside an update. In that case, the XT
275 functions assume that `selected_frame' is the frame to apply to. */
276
277 extern struct frame *updating_frame;
278
279 extern int waiting_for_input;
280
281 /* This is a frame waiting to be auto-raised, within XTread_socket. */
282
283 struct frame *pending_autoraise_frame;
284
285 #ifdef USE_X_TOOLKIT
286 /* The application context for Xt use. */
287 XtAppContext Xt_app_con;
288 static String Xt_default_resources[] = {0};
289 #endif /* USE_X_TOOLKIT */
290
291 /* Nominal cursor position -- where to draw output.
292 HPOS and VPOS are window relative glyph matrix coordinates.
293 X and Y are window relative pixel coordinates. */
294
295 struct cursor_pos output_cursor;
296
297 /* Non-zero means user is interacting with a toolkit scroll bar. */
298
299 static int toolkit_scroll_bar_interaction;
300
301 /* Mouse movement.
302
303 Formerly, we used PointerMotionHintMask (in standard_event_mask)
304 so that we would have to call XQueryPointer after each MotionNotify
305 event to ask for another such event. However, this made mouse tracking
306 slow, and there was a bug that made it eventually stop.
307
308 Simply asking for MotionNotify all the time seems to work better.
309
310 In order to avoid asking for motion events and then throwing most
311 of them away or busy-polling the server for mouse positions, we ask
312 the server for pointer motion hints. This means that we get only
313 one event per group of mouse movements. "Groups" are delimited by
314 other kinds of events (focus changes and button clicks, for
315 example), or by XQueryPointer calls; when one of these happens, we
316 get another MotionNotify event the next time the mouse moves. This
317 is at least as efficient as getting motion events when mouse
318 tracking is on, and I suspect only negligibly worse when tracking
319 is off. */
320
321 /* Where the mouse was last time we reported a mouse event. */
322
323 FRAME_PTR last_mouse_frame;
324 static XRectangle last_mouse_glyph;
325 static Lisp_Object last_mouse_press_frame;
326
327 /* The scroll bar in which the last X motion event occurred.
328
329 If the last X motion event occurred in a scroll bar, we set this so
330 XTmouse_position can know whether to report a scroll bar motion or
331 an ordinary motion.
332
333 If the last X motion event didn't occur in a scroll bar, we set
334 this to Qnil, to tell XTmouse_position to return an ordinary motion
335 event. */
336
337 static Lisp_Object last_mouse_scroll_bar;
338
339 /* This is a hack. We would really prefer that XTmouse_position would
340 return the time associated with the position it returns, but there
341 doesn't seem to be any way to wrest the time-stamp from the server
342 along with the position query. So, we just keep track of the time
343 of the last movement we received, and return that in hopes that
344 it's somewhat accurate. */
345
346 static Time last_mouse_movement_time;
347
348 /* Incremented by XTread_socket whenever it really tries to read
349 events. */
350
351 #ifdef __STDC__
352 static int volatile input_signal_count;
353 #else
354 static int input_signal_count;
355 #endif
356
357 /* Used locally within XTread_socket. */
358
359 static int x_noop_count;
360
361 /* Initial values of argv and argc. */
362
363 extern char **initial_argv;
364 extern int initial_argc;
365
366 extern Lisp_Object Vcommand_line_args, Vsystem_name;
367
368 /* Tells if a window manager is present or not. */
369
370 extern Lisp_Object Vx_no_window_manager;
371
372 extern Lisp_Object Qface, Qmouse_face;
373
374 extern int errno;
375
376 /* A mask of extra modifier bits to put into every keyboard char. */
377
378 extern int extra_keyboard_modifiers;
379
380 static Lisp_Object Qvendor_specific_keysyms;
381
382 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
383 extern Lisp_Object x_icon_type P_ ((struct frame *));
384
385
386 /* Enumeration for overriding/changing the face to use for drawing
387 glyphs in x_draw_glyphs. */
388
389 enum draw_glyphs_face
390 {
391 DRAW_NORMAL_TEXT,
392 DRAW_INVERSE_VIDEO,
393 DRAW_CURSOR,
394 DRAW_MOUSE_FACE,
395 DRAW_IMAGE_RAISED,
396 DRAW_IMAGE_SUNKEN
397 };
398
399 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
400 static const XColor *x_color_cells P_ ((struct frame *, int *));
401 static void x_update_window_end P_ ((struct window *, int, int));
402 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
403 void x_delete_display P_ ((struct x_display_info *));
404 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
405 unsigned));
406 static int fast_find_position P_ ((struct window *, int, int *, int *,
407 int *, int *));
408 static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
409 int *, int *, int *, int *, int));
410 static void set_output_cursor P_ ((struct cursor_pos *));
411 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
412 int *, int *, int *, int));
413 static void note_mode_line_highlight P_ ((struct window *, int, int));
414 static void note_mouse_highlight P_ ((struct frame *, int, int));
415 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
416 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
417 static void show_mouse_face P_ ((struct x_display_info *,
418 enum draw_glyphs_face));
419 static int x_io_error_quitter P_ ((Display *));
420 int x_catch_errors P_ ((Display *));
421 void x_uncatch_errors P_ ((Display *, int));
422 void x_lower_frame P_ ((struct frame *));
423 void x_scroll_bar_clear P_ ((struct frame *));
424 int x_had_errors_p P_ ((Display *));
425 void x_wm_set_size_hint P_ ((struct frame *, long, int));
426 void x_raise_frame P_ ((struct frame *));
427 void x_set_window_size P_ ((struct frame *, int, int, int));
428 void x_wm_set_window_state P_ ((struct frame *, int));
429 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
430 void x_initialize P_ ((void));
431 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
432 static int x_compute_min_glyph_bounds P_ ((struct frame *));
433 static void x_draw_phys_cursor_glyph P_ ((struct window *,
434 struct glyph_row *,
435 enum draw_glyphs_face));
436 static void x_update_end P_ ((struct frame *));
437 static void XTframe_up_to_date P_ ((struct frame *));
438 static void XTreassert_line_highlight P_ ((int, int));
439 static void x_change_line_highlight P_ ((int, int, int, int));
440 static void XTset_terminal_modes P_ ((void));
441 static void XTreset_terminal_modes P_ ((void));
442 static void XTcursor_to P_ ((int, int, int, int));
443 static void x_write_glyphs P_ ((struct glyph *, int));
444 static void x_clear_end_of_line P_ ((int));
445 static void x_clear_frame P_ ((void));
446 static void x_clear_cursor P_ ((struct window *));
447 static void frame_highlight P_ ((struct frame *));
448 static void frame_unhighlight P_ ((struct frame *));
449 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
450 static void XTframe_rehighlight P_ ((struct frame *));
451 static void x_frame_rehighlight P_ ((struct x_display_info *));
452 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
453 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
454 static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
455 XRectangle *));
456 static void expose_frame P_ ((struct frame *, int, int, int, int));
457 static void expose_window_tree P_ ((struct window *, XRectangle *));
458 static void expose_window P_ ((struct window *, XRectangle *));
459 static void expose_area P_ ((struct window *, struct glyph_row *,
460 XRectangle *, enum glyph_row_area));
461 static void expose_line P_ ((struct window *, struct glyph_row *,
462 XRectangle *));
463 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
464 static void x_update_window_cursor P_ ((struct window *, int));
465 static void x_erase_phys_cursor P_ ((struct window *));
466 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
467 static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
468 enum bitmap_type));
469
470 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
471 GC, int));
472 static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
473 static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
474 static void note_overwritten_text_cursor P_ ((struct window *, int, int));
475 static void x_flush P_ ((struct frame *f));
476 static void x_update_begin P_ ((struct frame *));
477 static void x_update_window_begin P_ ((struct window *));
478 static void x_draw_vertical_border P_ ((struct window *));
479 static void x_after_update_window_line P_ ((struct glyph_row *));
480 static INLINE void take_vertical_position_into_account P_ ((struct it *));
481 static void x_produce_stretch_glyph P_ ((struct it *));
482 static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
483 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
484 enum scroll_bar_part *,
485 Lisp_Object *, Lisp_Object *,
486 unsigned long *));
487
488 /* Flush display of frame F, or of all frames if F is null. */
489
490 static void
491 x_flush (f)
492 struct frame *f;
493 {
494 BLOCK_INPUT;
495 if (f == NULL)
496 {
497 Lisp_Object rest, frame;
498 FOR_EACH_FRAME (rest, frame)
499 x_flush (XFRAME (frame));
500 }
501 else if (FRAME_X_P (f))
502 XFlush (FRAME_X_DISPLAY (f));
503 UNBLOCK_INPUT;
504 }
505
506
507 /* Remove calls to XFlush by defining XFlush to an empty replacement.
508 Calls to XFlush should be unnecessary because the X output buffer
509 is flushed automatically as needed by calls to XPending,
510 XNextEvent, or XWindowEvent according to the XFlush man page.
511 XTread_socket calls XPending. Removing XFlush improves
512 performance. */
513
514 #define XFlush(DISPLAY) (void) 0
515
516 \f
517 /***********************************************************************
518 Debugging
519 ***********************************************************************/
520
521 #if 0
522
523 /* This is a function useful for recording debugging information about
524 the sequence of occurrences in this file. */
525
526 struct record
527 {
528 char *locus;
529 int type;
530 };
531
532 struct record event_record[100];
533
534 int event_record_index;
535
536 record_event (locus, type)
537 char *locus;
538 int type;
539 {
540 if (event_record_index == sizeof (event_record) / sizeof (struct record))
541 event_record_index = 0;
542
543 event_record[event_record_index].locus = locus;
544 event_record[event_record_index].type = type;
545 event_record_index++;
546 }
547
548 #endif /* 0 */
549
550
551 \f
552 /* Return the struct x_display_info corresponding to DPY. */
553
554 struct x_display_info *
555 x_display_info_for_display (dpy)
556 Display *dpy;
557 {
558 struct x_display_info *dpyinfo;
559
560 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
561 if (dpyinfo->display == dpy)
562 return dpyinfo;
563
564 return 0;
565 }
566
567
568 \f
569 /***********************************************************************
570 Starting and ending an update
571 ***********************************************************************/
572
573 /* Start an update of frame F. This function is installed as a hook
574 for update_begin, i.e. it is called when update_begin is called.
575 This function is called prior to calls to x_update_window_begin for
576 each window being updated. Currently, there is nothing to do here
577 because all interesting stuff is done on a window basis. */
578
579 static void
580 x_update_begin (f)
581 struct frame *f;
582 {
583 /* Nothing to do. */
584 }
585
586
587 /* Start update of window W. Set the global variable updated_window
588 to the window being updated and set output_cursor to the cursor
589 position of W. */
590
591 static void
592 x_update_window_begin (w)
593 struct window *w;
594 {
595 struct frame *f = XFRAME (WINDOW_FRAME (w));
596 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
597
598 updated_window = w;
599 set_output_cursor (&w->cursor);
600
601 BLOCK_INPUT;
602
603 if (f == display_info->mouse_face_mouse_frame)
604 {
605 /* Don't do highlighting for mouse motion during the update. */
606 display_info->mouse_face_defer = 1;
607
608 /* If F needs to be redrawn, simply forget about any prior mouse
609 highlighting. */
610 if (FRAME_GARBAGED_P (f))
611 display_info->mouse_face_window = Qnil;
612
613 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
614 their mouse_face_p flag set, which means that they are always
615 unequal to rows in a desired matrix which never have that
616 flag set. So, rows containing mouse-face glyphs are never
617 scrolled, and we don't have to switch the mouse highlight off
618 here to prevent it from being scrolled. */
619
620 /* Can we tell that this update does not affect the window
621 where the mouse highlight is? If so, no need to turn off.
622 Likewise, don't do anything if the frame is garbaged;
623 in that case, the frame's current matrix that we would use
624 is all wrong, and we will redisplay that line anyway. */
625 if (!NILP (display_info->mouse_face_window)
626 && w == XWINDOW (display_info->mouse_face_window))
627 {
628 int i;
629
630 for (i = 0; i < w->desired_matrix->nrows; ++i)
631 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
632 break;
633
634 if (i < w->desired_matrix->nrows)
635 clear_mouse_face (display_info);
636 }
637 #endif /* 0 */
638 }
639
640 UNBLOCK_INPUT;
641 }
642
643
644 /* Draw a vertical window border to the right of window W if W doesn't
645 have vertical scroll bars. */
646
647 static void
648 x_draw_vertical_border (w)
649 struct window *w;
650 {
651 struct frame *f = XFRAME (WINDOW_FRAME (w));
652
653 /* Redraw borders between horizontally adjacent windows. Don't
654 do it for frames with vertical scroll bars because either the
655 right scroll bar of a window, or the left scroll bar of its
656 neighbor will suffice as a border. */
657 if (!WINDOW_RIGHTMOST_P (w)
658 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
659 {
660 int x0, x1, y0, y1;
661
662 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
663 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
664 y1 -= 1;
665
666 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
667 f->output_data.x->normal_gc, x1, y0, x1, y1);
668 }
669 }
670
671
672 /* End update of window W (which is equal to updated_window).
673
674 Draw vertical borders between horizontally adjacent windows, and
675 display W's cursor if CURSOR_ON_P is non-zero.
676
677 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
678 glyphs in mouse-face were overwritten. In that case we have to
679 make sure that the mouse-highlight is properly redrawn.
680
681 W may be a menu bar pseudo-window in case we don't have X toolkit
682 support. Such windows don't have a cursor, so don't display it
683 here. */
684
685 static void
686 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
687 struct window *w;
688 int cursor_on_p, mouse_face_overwritten_p;
689 {
690 if (!w->pseudo_window_p)
691 {
692 struct x_display_info *dpyinfo
693 = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
694
695 BLOCK_INPUT;
696
697 /* If a row with mouse-face was overwritten, arrange for
698 XTframe_up_to_date to redisplay the mouse highlight. */
699 if (mouse_face_overwritten_p)
700 {
701 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
702 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
703 dpyinfo->mouse_face_window = Qnil;
704 }
705
706 if (cursor_on_p)
707 x_display_and_set_cursor (w, 1, output_cursor.hpos,
708 output_cursor.vpos,
709 output_cursor.x, output_cursor.y);
710
711 x_draw_vertical_border (w);
712 UNBLOCK_INPUT;
713 }
714
715 updated_window = NULL;
716 }
717
718
719 /* End update of frame F. This function is installed as a hook in
720 update_end. */
721
722 static void
723 x_update_end (f)
724 struct frame *f;
725 {
726 /* Mouse highlight may be displayed again. */
727 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
728
729 BLOCK_INPUT;
730 XFlush (FRAME_X_DISPLAY (f));
731 UNBLOCK_INPUT;
732 }
733
734
735 /* This function is called from various places in xdisp.c whenever a
736 complete update has been performed. The global variable
737 updated_window is not available here. */
738
739 static void
740 XTframe_up_to_date (f)
741 struct frame *f;
742 {
743 if (FRAME_X_P (f))
744 {
745 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
746
747 if (dpyinfo->mouse_face_deferred_gc
748 || f == dpyinfo->mouse_face_mouse_frame)
749 {
750 BLOCK_INPUT;
751 if (dpyinfo->mouse_face_mouse_frame)
752 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
753 dpyinfo->mouse_face_mouse_x,
754 dpyinfo->mouse_face_mouse_y);
755 dpyinfo->mouse_face_deferred_gc = 0;
756 UNBLOCK_INPUT;
757 }
758 }
759 }
760
761
762 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
763 arrow bitmaps, or clear the areas where they would be displayed
764 before DESIRED_ROW is made current. The window being updated is
765 found in updated_window. This function It is called from
766 update_window_line only if it is known that there are differences
767 between bitmaps to be drawn between current row and DESIRED_ROW. */
768
769 static void
770 x_after_update_window_line (desired_row)
771 struct glyph_row *desired_row;
772 {
773 struct window *w = updated_window;
774
775 xassert (w);
776
777 if (!desired_row->mode_line_p && !w->pseudo_window_p)
778 {
779 struct frame *f;
780 int width;
781
782 BLOCK_INPUT;
783 x_draw_row_bitmaps (w, desired_row);
784
785 /* When a window has disappeared, make sure that no rest of
786 full-width rows stays visible in the internal border. */
787 if (windows_or_buffers_changed
788 && (f = XFRAME (w->frame),
789 width = FRAME_INTERNAL_BORDER_WIDTH (f),
790 width != 0))
791 {
792 int height = desired_row->visible_height;
793 int x = (window_box_right (w, -1)
794 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
795 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
796
797 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
798 x, y, width, height, False);
799 }
800
801 UNBLOCK_INPUT;
802 }
803 }
804
805
806 /* Draw the bitmap WHICH in one of the areas to the left or right of
807 window W. ROW is the glyph row for which to display the bitmap; it
808 determines the vertical position at which the bitmap has to be
809 drawn. */
810
811 static void
812 x_draw_bitmap (w, row, which)
813 struct window *w;
814 struct glyph_row *row;
815 enum bitmap_type which;
816 {
817 struct frame *f = XFRAME (WINDOW_FRAME (w));
818 Display *display = FRAME_X_DISPLAY (f);
819 Window window = FRAME_X_WINDOW (f);
820 int x, y, wd, h, dy;
821 unsigned char *bits;
822 Pixmap pixmap;
823 GC gc = f->output_data.x->normal_gc;
824 struct face *face;
825 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
826
827 /* Must clip because of partially visible lines. */
828 x_clip_to_row (w, row, gc, 1);
829
830 switch (which)
831 {
832 case LEFT_TRUNCATION_BITMAP:
833 wd = left_width;
834 h = left_height;
835 bits = left_bits;
836 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
837 - wd
838 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
839 break;
840
841 case OVERLAY_ARROW_BITMAP:
842 wd = left_width;
843 h = left_height;
844 bits = ov_bits;
845 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
846 - wd
847 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
848 break;
849
850 case RIGHT_TRUNCATION_BITMAP:
851 wd = right_width;
852 h = right_height;
853 bits = right_bits;
854 x = window_box_right (w, -1);
855 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
856 break;
857
858 case CONTINUED_LINE_BITMAP:
859 wd = right_width;
860 h = right_height;
861 bits = continued_bits;
862 x = window_box_right (w, -1);
863 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
864 break;
865
866 case CONTINUATION_LINE_BITMAP:
867 wd = continuation_width;
868 h = continuation_height;
869 bits = continuation_bits;
870 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
871 - wd
872 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
873 break;
874
875 case ZV_LINE_BITMAP:
876 wd = zv_width;
877 h = zv_height;
878 bits = zv_bits;
879 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
880 - wd
881 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
882 break;
883
884 default:
885 abort ();
886 }
887
888 /* Convert to frame coordinates. Set dy to the offset in the row to
889 start drawing the bitmap. */
890 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
891 dy = (row->height - h) / 2;
892
893 /* Draw the bitmap. I believe these small pixmaps can be cached
894 by the server. */
895 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
896 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
897 face->foreground,
898 face->background, depth);
899 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
900 XFreePixmap (display, pixmap);
901 XSetClipMask (display, gc, None);
902 }
903
904
905 /* Draw flags bitmaps for glyph row ROW on window W. Call this
906 function with input blocked. */
907
908 static void
909 x_draw_row_bitmaps (w, row)
910 struct window *w;
911 struct glyph_row *row;
912 {
913 struct frame *f = XFRAME (w->frame);
914 enum bitmap_type bitmap;
915 struct face *face;
916 int header_line_height = -1;
917
918 xassert (interrupt_input_blocked);
919
920 /* If row is completely invisible, because of vscrolling, we
921 don't have to draw anything. */
922 if (row->visible_height <= 0)
923 return;
924
925 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
926 PREPARE_FACE_FOR_DISPLAY (f, face);
927
928 /* Decide which bitmap to draw at the left side. */
929 if (row->overlay_arrow_p)
930 bitmap = OVERLAY_ARROW_BITMAP;
931 else if (row->truncated_on_left_p)
932 bitmap = LEFT_TRUNCATION_BITMAP;
933 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
934 bitmap = CONTINUATION_LINE_BITMAP;
935 else if (row->indicate_empty_line_p)
936 bitmap = ZV_LINE_BITMAP;
937 else
938 bitmap = NO_BITMAP;
939
940 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
941 the flags area. */
942 if (bitmap == NO_BITMAP
943 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
944 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
945 {
946 /* If W has a vertical border to its left, don't draw over it. */
947 int border = ((XFASTINT (w->left) > 0
948 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
949 ? 1 : 0);
950 int left = window_box_left (w, -1);
951
952 if (header_line_height < 0)
953 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
954
955 /* In case the same realized face is used for bitmap areas and
956 for something displayed in the text (e.g. face `region' on
957 mono-displays, the fill style may have been changed to
958 FillSolid in x_draw_glyph_string_background. */
959 if (face->stipple)
960 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
961 else
962 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
963
964 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
965 face->gc,
966 (left
967 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
968 + border),
969 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
970 row->y)),
971 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
972 row->visible_height);
973 if (!face->stipple)
974 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
975 }
976
977 /* Draw the left bitmap. */
978 if (bitmap != NO_BITMAP)
979 x_draw_bitmap (w, row, bitmap);
980
981 /* Decide which bitmap to draw at the right side. */
982 if (row->truncated_on_right_p)
983 bitmap = RIGHT_TRUNCATION_BITMAP;
984 else if (row->continued_p)
985 bitmap = CONTINUED_LINE_BITMAP;
986 else
987 bitmap = NO_BITMAP;
988
989 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
990 the flags area. */
991 if (bitmap == NO_BITMAP
992 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
993 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
994 {
995 int right = window_box_right (w, -1);
996
997 if (header_line_height < 0)
998 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
999
1000 /* In case the same realized face is used for bitmap areas and
1001 for something displayed in the text (e.g. face `region' on
1002 mono-displays, the fill style may have been changed to
1003 FillSolid in x_draw_glyph_string_background. */
1004 if (face->stipple)
1005 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
1006 else
1007 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
1008 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1009 face->gc,
1010 right,
1011 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
1012 row->y)),
1013 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
1014 row->visible_height);
1015 if (!face->stipple)
1016 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
1017 }
1018
1019 /* Draw the right bitmap. */
1020 if (bitmap != NO_BITMAP)
1021 x_draw_bitmap (w, row, bitmap);
1022 }
1023
1024 \f
1025 /***********************************************************************
1026 Line Highlighting
1027 ***********************************************************************/
1028
1029 /* External interface to control of standout mode. Not used for X
1030 frames. Aborts when called. */
1031
1032 static void
1033 XTreassert_line_highlight (new, vpos)
1034 int new, vpos;
1035 {
1036 abort ();
1037 }
1038
1039
1040 /* Call this when about to modify line at position VPOS and change
1041 whether it is highlighted. Not used for X frames. Aborts when
1042 called. */
1043
1044 static void
1045 x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1046 int new_highlight, vpos, y, first_unused_hpos;
1047 {
1048 abort ();
1049 }
1050
1051
1052 /* This is called when starting Emacs and when restarting after
1053 suspend. When starting Emacs, no X window is mapped. And nothing
1054 must be done to Emacs's own window if it is suspended (though that
1055 rarely happens). */
1056
1057 static void
1058 XTset_terminal_modes ()
1059 {
1060 }
1061
1062 /* This is called when exiting or suspending Emacs. Exiting will make
1063 the X-windows go away, and suspending requires no action. */
1064
1065 static void
1066 XTreset_terminal_modes ()
1067 {
1068 }
1069
1070
1071 \f
1072 /***********************************************************************
1073 Output Cursor
1074 ***********************************************************************/
1075
1076 /* Set the global variable output_cursor to CURSOR. All cursor
1077 positions are relative to updated_window. */
1078
1079 static void
1080 set_output_cursor (cursor)
1081 struct cursor_pos *cursor;
1082 {
1083 output_cursor.hpos = cursor->hpos;
1084 output_cursor.vpos = cursor->vpos;
1085 output_cursor.x = cursor->x;
1086 output_cursor.y = cursor->y;
1087 }
1088
1089
1090 /* Set a nominal cursor position.
1091
1092 HPOS and VPOS are column/row positions in a window glyph matrix. X
1093 and Y are window text area relative pixel positions.
1094
1095 If this is done during an update, updated_window will contain the
1096 window that is being updated and the position is the future output
1097 cursor position for that window. If updated_window is null, use
1098 selected_window and display the cursor at the given position. */
1099
1100 static void
1101 XTcursor_to (vpos, hpos, y, x)
1102 int vpos, hpos, y, x;
1103 {
1104 struct window *w;
1105
1106 /* If updated_window is not set, work on selected_window. */
1107 if (updated_window)
1108 w = updated_window;
1109 else
1110 w = XWINDOW (selected_window);
1111
1112 /* Set the output cursor. */
1113 output_cursor.hpos = hpos;
1114 output_cursor.vpos = vpos;
1115 output_cursor.x = x;
1116 output_cursor.y = y;
1117
1118 /* If not called as part of an update, really display the cursor.
1119 This will also set the cursor position of W. */
1120 if (updated_window == NULL)
1121 {
1122 BLOCK_INPUT;
1123 x_display_cursor (w, 1, hpos, vpos, x, y);
1124 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1125 UNBLOCK_INPUT;
1126 }
1127 }
1128
1129
1130 \f
1131 /***********************************************************************
1132 Display Iterator
1133 ***********************************************************************/
1134
1135 /* Function prototypes of this page. */
1136
1137 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1138 struct glyph *,
1139 XChar2b *,
1140 int *));
1141 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1142 int, XChar2b *, int));
1143 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1144 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1145 static void x_append_glyph P_ ((struct it *));
1146 static void x_append_composite_glyph P_ ((struct it *));
1147 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1148 int, int, double));
1149 static void x_produce_glyphs P_ ((struct it *));
1150 static void x_produce_image_glyph P_ ((struct it *it));
1151
1152
1153 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1154 is not contained in the font. */
1155
1156 static INLINE XCharStruct *
1157 x_per_char_metric (font, char2b)
1158 XFontStruct *font;
1159 XChar2b *char2b;
1160 {
1161 /* The result metric information. */
1162 XCharStruct *pcm = NULL;
1163
1164 xassert (font && char2b);
1165
1166 if (font->per_char != NULL)
1167 {
1168 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1169 {
1170 /* min_char_or_byte2 specifies the linear character index
1171 corresponding to the first element of the per_char array,
1172 max_char_or_byte2 is the index of the last character. A
1173 character with non-zero CHAR2B->byte1 is not in the font.
1174 A character with byte2 less than min_char_or_byte2 or
1175 greater max_char_or_byte2 is not in the font. */
1176 if (char2b->byte1 == 0
1177 && char2b->byte2 >= font->min_char_or_byte2
1178 && char2b->byte2 <= font->max_char_or_byte2)
1179 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1180 }
1181 else
1182 {
1183 /* If either min_byte1 or max_byte1 are nonzero, both
1184 min_char_or_byte2 and max_char_or_byte2 are less than
1185 256, and the 2-byte character index values corresponding
1186 to the per_char array element N (counting from 0) are:
1187
1188 byte1 = N/D + min_byte1
1189 byte2 = N\D + min_char_or_byte2
1190
1191 where:
1192
1193 D = max_char_or_byte2 - min_char_or_byte2 + 1
1194 / = integer division
1195 \ = integer modulus */
1196 if (char2b->byte1 >= font->min_byte1
1197 && char2b->byte1 <= font->max_byte1
1198 && char2b->byte2 >= font->min_char_or_byte2
1199 && char2b->byte2 <= font->max_char_or_byte2)
1200 {
1201 pcm = (font->per_char
1202 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1203 * (char2b->byte1 - font->min_byte1))
1204 + (char2b->byte2 - font->min_char_or_byte2));
1205 }
1206 }
1207 }
1208 else
1209 {
1210 /* If the per_char pointer is null, all glyphs between the first
1211 and last character indexes inclusive have the same
1212 information, as given by both min_bounds and max_bounds. */
1213 if (char2b->byte2 >= font->min_char_or_byte2
1214 && char2b->byte2 <= font->max_char_or_byte2)
1215 pcm = &font->max_bounds;
1216 }
1217
1218 return ((pcm == NULL
1219 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1220 ? NULL : pcm);
1221 }
1222
1223
1224 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1225 the two-byte form of C. Encoding is returned in *CHAR2B. */
1226
1227 static INLINE void
1228 x_encode_char (c, char2b, font_info)
1229 int c;
1230 XChar2b *char2b;
1231 struct font_info *font_info;
1232 {
1233 int charset = CHAR_CHARSET (c);
1234 XFontStruct *font = font_info->font;
1235
1236 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1237 This may be either a program in a special encoder language or a
1238 fixed encoding. */
1239 if (font_info->font_encoder)
1240 {
1241 /* It's a program. */
1242 struct ccl_program *ccl = font_info->font_encoder;
1243
1244 if (CHARSET_DIMENSION (charset) == 1)
1245 {
1246 ccl->reg[0] = charset;
1247 ccl->reg[1] = char2b->byte2;
1248 }
1249 else
1250 {
1251 ccl->reg[0] = charset;
1252 ccl->reg[1] = char2b->byte1;
1253 ccl->reg[2] = char2b->byte2;
1254 }
1255
1256 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1257
1258 /* We assume that MSBs are appropriately set/reset by CCL
1259 program. */
1260 if (font->max_byte1 == 0) /* 1-byte font */
1261 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1262 else
1263 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1264 }
1265 else if (font_info->encoding[charset])
1266 {
1267 /* Fixed encoding scheme. See fontset.h for the meaning of the
1268 encoding numbers. */
1269 int enc = font_info->encoding[charset];
1270
1271 if ((enc == 1 || enc == 2)
1272 && CHARSET_DIMENSION (charset) == 2)
1273 char2b->byte1 |= 0x80;
1274
1275 if (enc == 1 || enc == 3)
1276 char2b->byte2 |= 0x80;
1277 }
1278 }
1279
1280
1281 /* Get face and two-byte form of character C in face FACE_ID on frame
1282 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1283 means we want to display multibyte text. Value is a pointer to a
1284 realized face that is ready for display. */
1285
1286 static INLINE struct face *
1287 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1288 struct frame *f;
1289 int c, face_id;
1290 XChar2b *char2b;
1291 int multibyte_p;
1292 {
1293 struct face *face = FACE_FROM_ID (f, face_id);
1294
1295 if (!multibyte_p)
1296 {
1297 /* Unibyte case. We don't have to encode, but we have to make
1298 sure to use a face suitable for unibyte. */
1299 char2b->byte1 = 0;
1300 char2b->byte2 = c;
1301 face_id = FACE_FOR_CHAR (f, face, c);
1302 face = FACE_FROM_ID (f, face_id);
1303 }
1304 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1305 {
1306 /* Case of ASCII in a face known to fit ASCII. */
1307 char2b->byte1 = 0;
1308 char2b->byte2 = c;
1309 }
1310 else
1311 {
1312 int c1, c2, charset;
1313
1314 /* Split characters into bytes. If c2 is -1 afterwards, C is
1315 really a one-byte character so that byte1 is zero. */
1316 SPLIT_CHAR (c, charset, c1, c2);
1317 if (c2 > 0)
1318 char2b->byte1 = c1, char2b->byte2 = c2;
1319 else
1320 char2b->byte1 = 0, char2b->byte2 = c1;
1321
1322 /* Maybe encode the character in *CHAR2B. */
1323 if (face->font != NULL)
1324 {
1325 struct font_info *font_info
1326 = FONT_INFO_FROM_ID (f, face->font_info_id);
1327 if (font_info)
1328 x_encode_char (c, char2b, font_info);
1329 }
1330 }
1331
1332 /* Make sure X resources of the face are allocated. */
1333 xassert (face != NULL);
1334 PREPARE_FACE_FOR_DISPLAY (f, face);
1335
1336 return face;
1337 }
1338
1339
1340 /* Get face and two-byte form of character glyph GLYPH on frame F.
1341 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1342 a pointer to a realized face that is ready for display. */
1343
1344 static INLINE struct face *
1345 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1346 struct frame *f;
1347 struct glyph *glyph;
1348 XChar2b *char2b;
1349 int *two_byte_p;
1350 {
1351 struct face *face;
1352
1353 xassert (glyph->type == CHAR_GLYPH);
1354 face = FACE_FROM_ID (f, glyph->face_id);
1355
1356 if (two_byte_p)
1357 *two_byte_p = 0;
1358
1359 if (!glyph->multibyte_p)
1360 {
1361 /* Unibyte case. We don't have to encode, but we have to make
1362 sure to use a face suitable for unibyte. */
1363 char2b->byte1 = 0;
1364 char2b->byte2 = glyph->u.ch;
1365 }
1366 else if (glyph->u.ch < 128
1367 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1368 {
1369 /* Case of ASCII in a face known to fit ASCII. */
1370 char2b->byte1 = 0;
1371 char2b->byte2 = glyph->u.ch;
1372 }
1373 else
1374 {
1375 int c1, c2, charset;
1376
1377 /* Split characters into bytes. If c2 is -1 afterwards, C is
1378 really a one-byte character so that byte1 is zero. */
1379 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1380 if (c2 > 0)
1381 char2b->byte1 = c1, char2b->byte2 = c2;
1382 else
1383 char2b->byte1 = 0, char2b->byte2 = c1;
1384
1385 /* Maybe encode the character in *CHAR2B. */
1386 if (charset != CHARSET_ASCII)
1387 {
1388 struct font_info *font_info
1389 = FONT_INFO_FROM_ID (f, face->font_info_id);
1390 if (font_info)
1391 {
1392 x_encode_char (glyph->u.ch, char2b, font_info);
1393 if (two_byte_p)
1394 *two_byte_p
1395 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1396 }
1397 }
1398 }
1399
1400 /* Make sure X resources of the face are allocated. */
1401 xassert (face != NULL);
1402 PREPARE_FACE_FOR_DISPLAY (f, face);
1403 return face;
1404 }
1405
1406
1407 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1408 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1409
1410 static INLINE void
1411 x_append_glyph (it)
1412 struct it *it;
1413 {
1414 struct glyph *glyph;
1415 enum glyph_row_area area = it->area;
1416
1417 xassert (it->glyph_row);
1418 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1419
1420 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1421 if (glyph < it->glyph_row->glyphs[area + 1])
1422 {
1423 glyph->charpos = CHARPOS (it->position);
1424 glyph->object = it->object;
1425 glyph->pixel_width = it->pixel_width;
1426 glyph->voffset = it->voffset;
1427 glyph->type = CHAR_GLYPH;
1428 glyph->multibyte_p = it->multibyte_p;
1429 glyph->left_box_line_p = it->start_of_box_run_p;
1430 glyph->right_box_line_p = it->end_of_box_run_p;
1431 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1432 || it->phys_descent > it->descent);
1433 glyph->padding_p = 0;
1434 glyph->glyph_not_available_p = it->glyph_not_available_p;
1435 glyph->face_id = it->face_id;
1436 glyph->u.ch = it->char_to_display;
1437 ++it->glyph_row->used[area];
1438 }
1439 }
1440
1441 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1442 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1443
1444 static INLINE void
1445 x_append_composite_glyph (it)
1446 struct it *it;
1447 {
1448 struct glyph *glyph;
1449 enum glyph_row_area area = it->area;
1450
1451 xassert (it->glyph_row);
1452
1453 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1454 if (glyph < it->glyph_row->glyphs[area + 1])
1455 {
1456 glyph->charpos = CHARPOS (it->position);
1457 glyph->object = it->object;
1458 glyph->pixel_width = it->pixel_width;
1459 glyph->voffset = it->voffset;
1460 glyph->type = COMPOSITE_GLYPH;
1461 glyph->multibyte_p = it->multibyte_p;
1462 glyph->left_box_line_p = it->start_of_box_run_p;
1463 glyph->right_box_line_p = it->end_of_box_run_p;
1464 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1465 || it->phys_descent > it->descent);
1466 glyph->padding_p = 0;
1467 glyph->glyph_not_available_p = 0;
1468 glyph->face_id = it->face_id;
1469 glyph->u.cmp_id = it->cmp_id;
1470 ++it->glyph_row->used[area];
1471 }
1472 }
1473
1474
1475 /* Change IT->ascent and IT->height according to the setting of
1476 IT->voffset. */
1477
1478 static INLINE void
1479 take_vertical_position_into_account (it)
1480 struct it *it;
1481 {
1482 if (it->voffset)
1483 {
1484 if (it->voffset < 0)
1485 /* Increase the ascent so that we can display the text higher
1486 in the line. */
1487 it->ascent += abs (it->voffset);
1488 else
1489 /* Increase the descent so that we can display the text lower
1490 in the line. */
1491 it->descent += it->voffset;
1492 }
1493 }
1494
1495
1496 /* Produce glyphs/get display metrics for the image IT is loaded with.
1497 See the description of struct display_iterator in dispextern.h for
1498 an overview of struct display_iterator. */
1499
1500 static void
1501 x_produce_image_glyph (it)
1502 struct it *it;
1503 {
1504 struct image *img;
1505 struct face *face;
1506
1507 xassert (it->what == IT_IMAGE);
1508
1509 face = FACE_FROM_ID (it->f, it->face_id);
1510 img = IMAGE_FROM_ID (it->f, it->image_id);
1511 xassert (img);
1512
1513 /* Make sure X resources of the face and image are loaded. */
1514 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1515 prepare_image_for_display (it->f, img);
1516
1517 it->ascent = it->phys_ascent = image_ascent (img, face);
1518 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1519 it->pixel_width = img->width + 2 * img->hmargin;
1520
1521 it->nglyphs = 1;
1522
1523 if (face->box != FACE_NO_BOX)
1524 {
1525 if (face->box_line_width > 0)
1526 {
1527 it->ascent += face->box_line_width;
1528 it->descent += face->box_line_width;
1529 }
1530
1531 if (it->start_of_box_run_p)
1532 it->pixel_width += abs (face->box_line_width);
1533 if (it->end_of_box_run_p)
1534 it->pixel_width += abs (face->box_line_width);
1535 }
1536
1537 take_vertical_position_into_account (it);
1538
1539 if (it->glyph_row)
1540 {
1541 struct glyph *glyph;
1542 enum glyph_row_area area = it->area;
1543
1544 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1545 if (glyph < it->glyph_row->glyphs[area + 1])
1546 {
1547 glyph->charpos = CHARPOS (it->position);
1548 glyph->object = it->object;
1549 glyph->pixel_width = it->pixel_width;
1550 glyph->voffset = it->voffset;
1551 glyph->type = IMAGE_GLYPH;
1552 glyph->multibyte_p = it->multibyte_p;
1553 glyph->left_box_line_p = it->start_of_box_run_p;
1554 glyph->right_box_line_p = it->end_of_box_run_p;
1555 glyph->overlaps_vertically_p = 0;
1556 glyph->padding_p = 0;
1557 glyph->glyph_not_available_p = 0;
1558 glyph->face_id = it->face_id;
1559 glyph->u.img_id = img->id;
1560 ++it->glyph_row->used[area];
1561 }
1562 }
1563 }
1564
1565
1566 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1567 of the glyph, WIDTH and HEIGHT are the width and height of the
1568 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1569 ascent of the glyph (0 <= ASCENT <= 1). */
1570
1571 static void
1572 x_append_stretch_glyph (it, object, width, height, ascent)
1573 struct it *it;
1574 Lisp_Object object;
1575 int width, height;
1576 double ascent;
1577 {
1578 struct glyph *glyph;
1579 enum glyph_row_area area = it->area;
1580
1581 xassert (ascent >= 0 && ascent <= 1);
1582
1583 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1584 if (glyph < it->glyph_row->glyphs[area + 1])
1585 {
1586 glyph->charpos = CHARPOS (it->position);
1587 glyph->object = object;
1588 glyph->pixel_width = width;
1589 glyph->voffset = it->voffset;
1590 glyph->type = STRETCH_GLYPH;
1591 glyph->multibyte_p = it->multibyte_p;
1592 glyph->left_box_line_p = it->start_of_box_run_p;
1593 glyph->right_box_line_p = it->end_of_box_run_p;
1594 glyph->overlaps_vertically_p = 0;
1595 glyph->padding_p = 0;
1596 glyph->glyph_not_available_p = 0;
1597 glyph->face_id = it->face_id;
1598 glyph->u.stretch.ascent = height * ascent;
1599 glyph->u.stretch.height = height;
1600 ++it->glyph_row->used[area];
1601 }
1602 }
1603
1604
1605 /* Produce a stretch glyph for iterator IT. IT->object is the value
1606 of the glyph property displayed. The value must be a list
1607 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1608 being recognized:
1609
1610 1. `:width WIDTH' specifies that the space should be WIDTH *
1611 canonical char width wide. WIDTH may be an integer or floating
1612 point number.
1613
1614 2. `:relative-width FACTOR' specifies that the width of the stretch
1615 should be computed from the width of the first character having the
1616 `glyph' property, and should be FACTOR times that width.
1617
1618 3. `:align-to HPOS' specifies that the space should be wide enough
1619 to reach HPOS, a value in canonical character units.
1620
1621 Exactly one of the above pairs must be present.
1622
1623 4. `:height HEIGHT' specifies that the height of the stretch produced
1624 should be HEIGHT, measured in canonical character units.
1625
1626 5. `:relative-height FACTOR' specifies that the height of the the
1627 stretch should be FACTOR times the height of the characters having
1628 the glyph property.
1629
1630 Either none or exactly one of 4 or 5 must be present.
1631
1632 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1633 of the stretch should be used for the ascent of the stretch.
1634 ASCENT must be in the range 0 <= ASCENT <= 100. */
1635
1636 #define NUMVAL(X) \
1637 ((INTEGERP (X) || FLOATP (X)) \
1638 ? XFLOATINT (X) \
1639 : - 1)
1640
1641
1642 static void
1643 x_produce_stretch_glyph (it)
1644 struct it *it;
1645 {
1646 /* (space :width WIDTH :height HEIGHT. */
1647 #if GLYPH_DEBUG
1648 extern Lisp_Object Qspace;
1649 #endif
1650 extern Lisp_Object QCwidth, QCheight, QCascent;
1651 extern Lisp_Object QCrelative_width, QCrelative_height;
1652 extern Lisp_Object QCalign_to;
1653 Lisp_Object prop, plist;
1654 double width = 0, height = 0, ascent = 0;
1655 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1656 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1657
1658 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1659
1660 /* List should start with `space'. */
1661 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1662 plist = XCDR (it->object);
1663
1664 /* Compute the width of the stretch. */
1665 if (prop = Fplist_get (plist, QCwidth),
1666 NUMVAL (prop) > 0)
1667 /* Absolute width `:width WIDTH' specified and valid. */
1668 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1669 else if (prop = Fplist_get (plist, QCrelative_width),
1670 NUMVAL (prop) > 0)
1671 {
1672 /* Relative width `:relative-width FACTOR' specified and valid.
1673 Compute the width of the characters having the `glyph'
1674 property. */
1675 struct it it2;
1676 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1677
1678 it2 = *it;
1679 if (it->multibyte_p)
1680 {
1681 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1682 - IT_BYTEPOS (*it));
1683 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1684 }
1685 else
1686 it2.c = *p, it2.len = 1;
1687
1688 it2.glyph_row = NULL;
1689 it2.what = IT_CHARACTER;
1690 x_produce_glyphs (&it2);
1691 width = NUMVAL (prop) * it2.pixel_width;
1692 }
1693 else if (prop = Fplist_get (plist, QCalign_to),
1694 NUMVAL (prop) > 0)
1695 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1696 else
1697 /* Nothing specified -> width defaults to canonical char width. */
1698 width = CANON_X_UNIT (it->f);
1699
1700 /* Compute height. */
1701 if (prop = Fplist_get (plist, QCheight),
1702 NUMVAL (prop) > 0)
1703 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1704 else if (prop = Fplist_get (plist, QCrelative_height),
1705 NUMVAL (prop) > 0)
1706 height = FONT_HEIGHT (font) * NUMVAL (prop);
1707 else
1708 height = FONT_HEIGHT (font);
1709
1710 /* Compute percentage of height used for ascent. If
1711 `:ascent ASCENT' is present and valid, use that. Otherwise,
1712 derive the ascent from the font in use. */
1713 if (prop = Fplist_get (plist, QCascent),
1714 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1715 ascent = NUMVAL (prop) / 100.0;
1716 else
1717 ascent = (double) font->ascent / FONT_HEIGHT (font);
1718
1719 if (width <= 0)
1720 width = 1;
1721 if (height <= 0)
1722 height = 1;
1723
1724 if (it->glyph_row)
1725 {
1726 Lisp_Object object = it->stack[it->sp - 1].string;
1727 if (!STRINGP (object))
1728 object = it->w->buffer;
1729 x_append_stretch_glyph (it, object, width, height, ascent);
1730 }
1731
1732 it->pixel_width = width;
1733 it->ascent = it->phys_ascent = height * ascent;
1734 it->descent = it->phys_descent = height - it->ascent;
1735 it->nglyphs = 1;
1736
1737 if (face->box != FACE_NO_BOX)
1738 {
1739 if (face->box_line_width > 0)
1740 {
1741 it->ascent += face->box_line_width;
1742 it->descent += face->box_line_width;
1743 }
1744
1745 if (it->start_of_box_run_p)
1746 it->pixel_width += abs (face->box_line_width);
1747 if (it->end_of_box_run_p)
1748 it->pixel_width += abs (face->box_line_width);
1749 }
1750
1751 take_vertical_position_into_account (it);
1752 }
1753
1754 /* Return proper value to be used as baseline offset of font that has
1755 ASCENT and DESCENT to draw characters by the font at the vertical
1756 center of the line of frame F.
1757
1758 Here, out task is to find the value of BOFF in the following figure;
1759
1760 -------------------------+-----------+-
1761 -+-+---------+-+ | |
1762 | | | | | |
1763 | | | | F_ASCENT F_HEIGHT
1764 | | | ASCENT | |
1765 HEIGHT | | | | |
1766 | | |-|-+------+-----------|------- baseline
1767 | | | | BOFF | |
1768 | |---------|-+-+ | |
1769 | | | DESCENT | |
1770 -+-+---------+-+ F_DESCENT |
1771 -------------------------+-----------+-
1772
1773 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1774 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1775 DESCENT = FONT->descent
1776 HEIGHT = FONT_HEIGHT (FONT)
1777 F_DESCENT = (F->output_data.x->font->descent
1778 - F->output_data.x->baseline_offset)
1779 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1780 */
1781
1782 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1783 ((FONT)->descent \
1784 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1785 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1786 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1787
1788 /* Produce glyphs/get display metrics for the display element IT is
1789 loaded with. See the description of struct display_iterator in
1790 dispextern.h for an overview of struct display_iterator. */
1791
1792 static void
1793 x_produce_glyphs (it)
1794 struct it *it;
1795 {
1796 it->glyph_not_available_p = 0;
1797
1798 if (it->what == IT_CHARACTER)
1799 {
1800 XChar2b char2b;
1801 XFontStruct *font;
1802 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1803 XCharStruct *pcm;
1804 int font_not_found_p;
1805 struct font_info *font_info;
1806 int boff; /* baseline offset */
1807 /* We may change it->multibyte_p upon unibyte<->multibyte
1808 conversion. So, save the current value now and restore it
1809 later.
1810
1811 Note: It seems that we don't have to record multibyte_p in
1812 struct glyph because the character code itself tells if or
1813 not the character is multibyte. Thus, in the future, we must
1814 consider eliminating the field `multibyte_p' in the struct
1815 glyph.
1816 */
1817 int saved_multibyte_p = it->multibyte_p;
1818
1819 /* Maybe translate single-byte characters to multibyte, or the
1820 other way. */
1821 it->char_to_display = it->c;
1822 if (!ASCII_BYTE_P (it->c))
1823 {
1824 if (unibyte_display_via_language_environment
1825 && SINGLE_BYTE_CHAR_P (it->c)
1826 && (it->c >= 0240
1827 || !NILP (Vnonascii_translation_table)))
1828 {
1829 it->char_to_display = unibyte_char_to_multibyte (it->c);
1830 it->multibyte_p = 1;
1831 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1832 face = FACE_FROM_ID (it->f, it->face_id);
1833 }
1834 else if (!SINGLE_BYTE_CHAR_P (it->c)
1835 && !it->multibyte_p)
1836 {
1837 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
1838 it->multibyte_p = 0;
1839 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1840 face = FACE_FROM_ID (it->f, it->face_id);
1841 }
1842 }
1843
1844 /* Get font to use. Encode IT->char_to_display. */
1845 x_get_char_face_and_encoding (it->f, it->char_to_display,
1846 it->face_id, &char2b,
1847 it->multibyte_p);
1848 font = face->font;
1849
1850 /* When no suitable font found, use the default font. */
1851 font_not_found_p = font == NULL;
1852 if (font_not_found_p)
1853 {
1854 font = FRAME_FONT (it->f);
1855 boff = it->f->output_data.x->baseline_offset;
1856 font_info = NULL;
1857 }
1858 else
1859 {
1860 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1861 boff = font_info->baseline_offset;
1862 if (font_info->vertical_centering)
1863 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1864 }
1865
1866 if (it->char_to_display >= ' '
1867 && (!it->multibyte_p || it->char_to_display < 128))
1868 {
1869 /* Either unibyte or ASCII. */
1870 int stretched_p;
1871
1872 it->nglyphs = 1;
1873
1874 pcm = x_per_char_metric (font, &char2b);
1875 it->ascent = font->ascent + boff;
1876 it->descent = font->descent - boff;
1877
1878 if (pcm)
1879 {
1880 it->phys_ascent = pcm->ascent + boff;
1881 it->phys_descent = pcm->descent - boff;
1882 it->pixel_width = pcm->width;
1883 }
1884 else
1885 {
1886 it->glyph_not_available_p = 1;
1887 it->phys_ascent = font->ascent + boff;
1888 it->phys_descent = font->descent - boff;
1889 it->pixel_width = FONT_WIDTH (font);
1890 }
1891
1892 /* If this is a space inside a region of text with
1893 `space-width' property, change its width. */
1894 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1895 if (stretched_p)
1896 it->pixel_width *= XFLOATINT (it->space_width);
1897
1898 /* If face has a box, add the box thickness to the character
1899 height. If character has a box line to the left and/or
1900 right, add the box line width to the character's width. */
1901 if (face->box != FACE_NO_BOX)
1902 {
1903 int thick = face->box_line_width;
1904
1905 if (thick > 0)
1906 {
1907 it->ascent += thick;
1908 it->descent += thick;
1909 }
1910 else
1911 thick = -thick;
1912
1913 if (it->start_of_box_run_p)
1914 it->pixel_width += thick;
1915 if (it->end_of_box_run_p)
1916 it->pixel_width += thick;
1917 }
1918
1919 /* If face has an overline, add the height of the overline
1920 (1 pixel) and a 1 pixel margin to the character height. */
1921 if (face->overline_p)
1922 it->ascent += 2;
1923
1924 take_vertical_position_into_account (it);
1925
1926 /* If we have to actually produce glyphs, do it. */
1927 if (it->glyph_row)
1928 {
1929 if (stretched_p)
1930 {
1931 /* Translate a space with a `space-width' property
1932 into a stretch glyph. */
1933 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1934 x_append_stretch_glyph (it, it->object, it->pixel_width,
1935 it->ascent + it->descent, ascent);
1936 }
1937 else
1938 x_append_glyph (it);
1939
1940 /* If characters with lbearing or rbearing are displayed
1941 in this line, record that fact in a flag of the
1942 glyph row. This is used to optimize X output code. */
1943 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1944 it->glyph_row->contains_overlapping_glyphs_p = 1;
1945 }
1946 }
1947 else if (it->char_to_display == '\n')
1948 {
1949 /* A newline has no width but we need the height of the line. */
1950 it->pixel_width = 0;
1951 it->nglyphs = 0;
1952 it->ascent = it->phys_ascent = font->ascent + boff;
1953 it->descent = it->phys_descent = font->descent - boff;
1954
1955 if (face->box != FACE_NO_BOX
1956 && face->box_line_width > 0)
1957 {
1958 it->ascent += face->box_line_width;
1959 it->descent += face->box_line_width;
1960 }
1961 }
1962 else if (it->char_to_display == '\t')
1963 {
1964 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1965 int x = it->current_x + it->continuation_lines_width;
1966 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1967
1968 /* If the distance from the current position to the next tab
1969 stop is less than a canonical character width, use the
1970 tab stop after that. */
1971 if (next_tab_x - x < CANON_X_UNIT (it->f))
1972 next_tab_x += tab_width;
1973
1974 it->pixel_width = next_tab_x - x;
1975 it->nglyphs = 1;
1976 it->ascent = it->phys_ascent = font->ascent + boff;
1977 it->descent = it->phys_descent = font->descent - boff;
1978
1979 if (it->glyph_row)
1980 {
1981 double ascent = (double) it->ascent / (it->ascent + it->descent);
1982 x_append_stretch_glyph (it, it->object, it->pixel_width,
1983 it->ascent + it->descent, ascent);
1984 }
1985 }
1986 else
1987 {
1988 /* A multi-byte character. Assume that the display width of the
1989 character is the width of the character multiplied by the
1990 width of the font. */
1991
1992 /* If we found a font, this font should give us the right
1993 metrics. If we didn't find a font, use the frame's
1994 default font and calculate the width of the character
1995 from the charset width; this is what old redisplay code
1996 did. */
1997 pcm = x_per_char_metric (font, &char2b);
1998 if (font_not_found_p || !pcm)
1999 {
2000 int charset = CHAR_CHARSET (it->char_to_display);
2001
2002 it->glyph_not_available_p = 1;
2003 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
2004 * CHARSET_WIDTH (charset));
2005 it->phys_ascent = font->ascent + boff;
2006 it->phys_descent = font->descent - boff;
2007 }
2008 else
2009 {
2010 it->pixel_width = pcm->width;
2011 it->phys_ascent = pcm->ascent + boff;
2012 it->phys_descent = pcm->descent - boff;
2013 if (it->glyph_row
2014 && (pcm->lbearing < 0
2015 || pcm->rbearing > pcm->width))
2016 it->glyph_row->contains_overlapping_glyphs_p = 1;
2017 }
2018 it->nglyphs = 1;
2019 it->ascent = font->ascent + boff;
2020 it->descent = font->descent - boff;
2021 if (face->box != FACE_NO_BOX)
2022 {
2023 int thick = face->box_line_width;
2024
2025 if (thick > 0)
2026 {
2027 it->ascent += thick;
2028 it->descent += thick;
2029 }
2030 else
2031 thick = - thick;
2032
2033 if (it->start_of_box_run_p)
2034 it->pixel_width += thick;
2035 if (it->end_of_box_run_p)
2036 it->pixel_width += thick;
2037 }
2038
2039 /* If face has an overline, add the height of the overline
2040 (1 pixel) and a 1 pixel margin to the character height. */
2041 if (face->overline_p)
2042 it->ascent += 2;
2043
2044 take_vertical_position_into_account (it);
2045
2046 if (it->glyph_row)
2047 x_append_glyph (it);
2048 }
2049 it->multibyte_p = saved_multibyte_p;
2050 }
2051 else if (it->what == IT_COMPOSITION)
2052 {
2053 /* Note: A composition is represented as one glyph in the
2054 glyph matrix. There are no padding glyphs. */
2055 XChar2b char2b;
2056 XFontStruct *font;
2057 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2058 XCharStruct *pcm;
2059 int font_not_found_p;
2060 struct font_info *font_info;
2061 int boff; /* baseline offset */
2062 struct composition *cmp = composition_table[it->cmp_id];
2063
2064 /* Maybe translate single-byte characters to multibyte. */
2065 it->char_to_display = it->c;
2066 if (unibyte_display_via_language_environment
2067 && SINGLE_BYTE_CHAR_P (it->c)
2068 && (it->c >= 0240
2069 || (it->c >= 0200
2070 && !NILP (Vnonascii_translation_table))))
2071 {
2072 it->char_to_display = unibyte_char_to_multibyte (it->c);
2073 }
2074
2075 /* Get face and font to use. Encode IT->char_to_display. */
2076 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2077 face = FACE_FROM_ID (it->f, it->face_id);
2078 x_get_char_face_and_encoding (it->f, it->char_to_display,
2079 it->face_id, &char2b, it->multibyte_p);
2080 font = face->font;
2081
2082 /* When no suitable font found, use the default font. */
2083 font_not_found_p = font == NULL;
2084 if (font_not_found_p)
2085 {
2086 font = FRAME_FONT (it->f);
2087 boff = it->f->output_data.x->baseline_offset;
2088 font_info = NULL;
2089 }
2090 else
2091 {
2092 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2093 boff = font_info->baseline_offset;
2094 if (font_info->vertical_centering)
2095 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2096 }
2097
2098 /* There are no padding glyphs, so there is only one glyph to
2099 produce for the composition. Important is that pixel_width,
2100 ascent and descent are the values of what is drawn by
2101 draw_glyphs (i.e. the values of the overall glyphs composed). */
2102 it->nglyphs = 1;
2103
2104 /* If we have not yet calculated pixel size data of glyphs of
2105 the composition for the current face font, calculate them
2106 now. Theoretically, we have to check all fonts for the
2107 glyphs, but that requires much time and memory space. So,
2108 here we check only the font of the first glyph. This leads
2109 to incorrect display very rarely, and C-l (recenter) can
2110 correct the display anyway. */
2111 if (cmp->font != (void *) font)
2112 {
2113 /* Ascent and descent of the font of the first character of
2114 this composition (adjusted by baseline offset). Ascent
2115 and descent of overall glyphs should not be less than
2116 them respectively. */
2117 int font_ascent = font->ascent + boff;
2118 int font_descent = font->descent - boff;
2119 /* Bounding box of the overall glyphs. */
2120 int leftmost, rightmost, lowest, highest;
2121 int i, width, ascent, descent;
2122
2123 cmp->font = (void *) font;
2124
2125 /* Initialize the bounding box. */
2126 if (font_info
2127 && (pcm = x_per_char_metric (font, &char2b)))
2128 {
2129 width = pcm->width;
2130 ascent = pcm->ascent;
2131 descent = pcm->descent;
2132 }
2133 else
2134 {
2135 width = FONT_WIDTH (font);
2136 ascent = font->ascent;
2137 descent = font->descent;
2138 }
2139
2140 rightmost = width;
2141 lowest = - descent + boff;
2142 highest = ascent + boff;
2143 leftmost = 0;
2144
2145 if (font_info
2146 && font_info->default_ascent
2147 && CHAR_TABLE_P (Vuse_default_ascent)
2148 && !NILP (Faref (Vuse_default_ascent,
2149 make_number (it->char_to_display))))
2150 highest = font_info->default_ascent + boff;
2151
2152 /* Draw the first glyph at the normal position. It may be
2153 shifted to right later if some other glyphs are drawn at
2154 the left. */
2155 cmp->offsets[0] = 0;
2156 cmp->offsets[1] = boff;
2157
2158 /* Set cmp->offsets for the remaining glyphs. */
2159 for (i = 1; i < cmp->glyph_len; i++)
2160 {
2161 int left, right, btm, top;
2162 int ch = COMPOSITION_GLYPH (cmp, i);
2163 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2164
2165 face = FACE_FROM_ID (it->f, face_id);
2166 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2167 it->multibyte_p);
2168 font = face->font;
2169 if (font == NULL)
2170 {
2171 font = FRAME_FONT (it->f);
2172 boff = it->f->output_data.x->baseline_offset;
2173 font_info = NULL;
2174 }
2175 else
2176 {
2177 font_info
2178 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2179 boff = font_info->baseline_offset;
2180 if (font_info->vertical_centering)
2181 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2182 }
2183
2184 if (font_info
2185 && (pcm = x_per_char_metric (font, &char2b)))
2186 {
2187 width = pcm->width;
2188 ascent = pcm->ascent;
2189 descent = pcm->descent;
2190 }
2191 else
2192 {
2193 width = FONT_WIDTH (font);
2194 ascent = 1;
2195 descent = 0;
2196 }
2197
2198 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2199 {
2200 /* Relative composition with or without
2201 alternate chars. */
2202 left = (leftmost + rightmost - width) / 2;
2203 btm = - descent + boff;
2204 if (font_info && font_info->relative_compose
2205 && (! CHAR_TABLE_P (Vignore_relative_composition)
2206 || NILP (Faref (Vignore_relative_composition,
2207 make_number (ch)))))
2208 {
2209
2210 if (- descent >= font_info->relative_compose)
2211 /* One extra pixel between two glyphs. */
2212 btm = highest + 1;
2213 else if (ascent <= 0)
2214 /* One extra pixel between two glyphs. */
2215 btm = lowest - 1 - ascent - descent;
2216 }
2217 }
2218 else
2219 {
2220 /* A composition rule is specified by an integer
2221 value that encodes global and new reference
2222 points (GREF and NREF). GREF and NREF are
2223 specified by numbers as below:
2224
2225 0---1---2 -- ascent
2226 | |
2227 | |
2228 | |
2229 9--10--11 -- center
2230 | |
2231 ---3---4---5--- baseline
2232 | |
2233 6---7---8 -- descent
2234 */
2235 int rule = COMPOSITION_RULE (cmp, i);
2236 int gref, nref, grefx, grefy, nrefx, nrefy;
2237
2238 COMPOSITION_DECODE_RULE (rule, gref, nref);
2239 grefx = gref % 3, nrefx = nref % 3;
2240 grefy = gref / 3, nrefy = nref / 3;
2241
2242 left = (leftmost
2243 + grefx * (rightmost - leftmost) / 2
2244 - nrefx * width / 2);
2245 btm = ((grefy == 0 ? highest
2246 : grefy == 1 ? 0
2247 : grefy == 2 ? lowest
2248 : (highest + lowest) / 2)
2249 - (nrefy == 0 ? ascent + descent
2250 : nrefy == 1 ? descent - boff
2251 : nrefy == 2 ? 0
2252 : (ascent + descent) / 2));
2253 }
2254
2255 cmp->offsets[i * 2] = left;
2256 cmp->offsets[i * 2 + 1] = btm + descent;
2257
2258 /* Update the bounding box of the overall glyphs. */
2259 right = left + width;
2260 top = btm + descent + ascent;
2261 if (left < leftmost)
2262 leftmost = left;
2263 if (right > rightmost)
2264 rightmost = right;
2265 if (top > highest)
2266 highest = top;
2267 if (btm < lowest)
2268 lowest = btm;
2269 }
2270
2271 /* If there are glyphs whose x-offsets are negative,
2272 shift all glyphs to the right and make all x-offsets
2273 non-negative. */
2274 if (leftmost < 0)
2275 {
2276 for (i = 0; i < cmp->glyph_len; i++)
2277 cmp->offsets[i * 2] -= leftmost;
2278 rightmost -= leftmost;
2279 }
2280
2281 cmp->pixel_width = rightmost;
2282 cmp->ascent = highest;
2283 cmp->descent = - lowest;
2284 if (cmp->ascent < font_ascent)
2285 cmp->ascent = font_ascent;
2286 if (cmp->descent < font_descent)
2287 cmp->descent = font_descent;
2288 }
2289
2290 it->pixel_width = cmp->pixel_width;
2291 it->ascent = it->phys_ascent = cmp->ascent;
2292 it->descent = it->phys_descent = cmp->descent;
2293
2294 if (face->box != FACE_NO_BOX)
2295 {
2296 int thick = face->box_line_width;
2297
2298 if (thick > 0)
2299 {
2300 it->ascent += thick;
2301 it->descent += thick;
2302 }
2303 else
2304 thick = - thick;
2305
2306 if (it->start_of_box_run_p)
2307 it->pixel_width += thick;
2308 if (it->end_of_box_run_p)
2309 it->pixel_width += thick;
2310 }
2311
2312 /* If face has an overline, add the height of the overline
2313 (1 pixel) and a 1 pixel margin to the character height. */
2314 if (face->overline_p)
2315 it->ascent += 2;
2316
2317 take_vertical_position_into_account (it);
2318
2319 if (it->glyph_row)
2320 x_append_composite_glyph (it);
2321 }
2322 else if (it->what == IT_IMAGE)
2323 x_produce_image_glyph (it);
2324 else if (it->what == IT_STRETCH)
2325 x_produce_stretch_glyph (it);
2326
2327 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2328 because this isn't true for images with `:ascent 100'. */
2329 xassert (it->ascent >= 0 && it->descent >= 0);
2330 if (it->area == TEXT_AREA)
2331 it->current_x += it->pixel_width;
2332
2333 it->descent += it->extra_line_spacing;
2334
2335 it->max_ascent = max (it->max_ascent, it->ascent);
2336 it->max_descent = max (it->max_descent, it->descent);
2337 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2338 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2339 }
2340
2341
2342 /* Estimate the pixel height of the mode or top line on frame F.
2343 FACE_ID specifies what line's height to estimate. */
2344
2345 int
2346 x_estimate_mode_line_height (f, face_id)
2347 struct frame *f;
2348 enum face_id face_id;
2349 {
2350 int height = FONT_HEIGHT (FRAME_FONT (f));
2351
2352 /* This function is called so early when Emacs starts that the face
2353 cache and mode line face are not yet initialized. */
2354 if (FRAME_FACE_CACHE (f))
2355 {
2356 struct face *face = FACE_FROM_ID (f, face_id);
2357 if (face)
2358 {
2359 if (face->font)
2360 height = FONT_HEIGHT (face->font);
2361 if (face->box_line_width > 0)
2362 height += 2 * face->box_line_width;
2363 }
2364 }
2365
2366 return height;
2367 }
2368
2369 \f
2370 /***********************************************************************
2371 Glyph display
2372 ***********************************************************************/
2373
2374 /* A sequence of glyphs to be drawn in the same face.
2375
2376 This data structure is not really completely X specific, so it
2377 could possibly, at least partially, be useful for other systems. It
2378 is currently not part of the external redisplay interface because
2379 it's not clear what other systems will need. */
2380
2381 struct glyph_string
2382 {
2383 /* X-origin of the string. */
2384 int x;
2385
2386 /* Y-origin and y-position of the base line of this string. */
2387 int y, ybase;
2388
2389 /* The width of the string, not including a face extension. */
2390 int width;
2391
2392 /* The width of the string, including a face extension. */
2393 int background_width;
2394
2395 /* The height of this string. This is the height of the line this
2396 string is drawn in, and can be different from the height of the
2397 font the string is drawn in. */
2398 int height;
2399
2400 /* Number of pixels this string overwrites in front of its x-origin.
2401 This number is zero if the string has an lbearing >= 0; it is
2402 -lbearing, if the string has an lbearing < 0. */
2403 int left_overhang;
2404
2405 /* Number of pixels this string overwrites past its right-most
2406 nominal x-position, i.e. x + width. Zero if the string's
2407 rbearing is <= its nominal width, rbearing - width otherwise. */
2408 int right_overhang;
2409
2410 /* The frame on which the glyph string is drawn. */
2411 struct frame *f;
2412
2413 /* The window on which the glyph string is drawn. */
2414 struct window *w;
2415
2416 /* X display and window for convenience. */
2417 Display *display;
2418 Window window;
2419
2420 /* The glyph row for which this string was built. It determines the
2421 y-origin and height of the string. */
2422 struct glyph_row *row;
2423
2424 /* The area within row. */
2425 enum glyph_row_area area;
2426
2427 /* Characters to be drawn, and number of characters. */
2428 XChar2b *char2b;
2429 int nchars;
2430
2431 /* A face-override for drawing cursors, mouse face and similar. */
2432 enum draw_glyphs_face hl;
2433
2434 /* Face in which this string is to be drawn. */
2435 struct face *face;
2436
2437 /* Font in which this string is to be drawn. */
2438 XFontStruct *font;
2439
2440 /* Font info for this string. */
2441 struct font_info *font_info;
2442
2443 /* Non-null means this string describes (part of) a composition.
2444 All characters from char2b are drawn composed. */
2445 struct composition *cmp;
2446
2447 /* Index of this glyph string's first character in the glyph
2448 definition of CMP. If this is zero, this glyph string describes
2449 the first character of a composition. */
2450 int gidx;
2451
2452 /* 1 means this glyph strings face has to be drawn to the right end
2453 of the window's drawing area. */
2454 unsigned extends_to_end_of_line_p : 1;
2455
2456 /* 1 means the background of this string has been drawn. */
2457 unsigned background_filled_p : 1;
2458
2459 /* 1 means glyph string must be drawn with 16-bit functions. */
2460 unsigned two_byte_p : 1;
2461
2462 /* 1 means that the original font determined for drawing this glyph
2463 string could not be loaded. The member `font' has been set to
2464 the frame's default font in this case. */
2465 unsigned font_not_found_p : 1;
2466
2467 /* 1 means that the face in which this glyph string is drawn has a
2468 stipple pattern. */
2469 unsigned stippled_p : 1;
2470
2471 /* 1 means only the foreground of this glyph string must be drawn,
2472 and we should use the physical height of the line this glyph
2473 string appears in as clip rect. */
2474 unsigned for_overlaps_p : 1;
2475
2476 /* The GC to use for drawing this glyph string. */
2477 GC gc;
2478
2479 /* A pointer to the first glyph in the string. This glyph
2480 corresponds to char2b[0]. Needed to draw rectangles if
2481 font_not_found_p is 1. */
2482 struct glyph *first_glyph;
2483
2484 /* Image, if any. */
2485 struct image *img;
2486
2487 struct glyph_string *next, *prev;
2488 };
2489
2490
2491 #if 0
2492
2493 static void
2494 x_dump_glyph_string (s)
2495 struct glyph_string *s;
2496 {
2497 fprintf (stderr, "glyph string\n");
2498 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2499 s->x, s->y, s->width, s->height);
2500 fprintf (stderr, " ybase = %d\n", s->ybase);
2501 fprintf (stderr, " hl = %d\n", s->hl);
2502 fprintf (stderr, " left overhang = %d, right = %d\n",
2503 s->left_overhang, s->right_overhang);
2504 fprintf (stderr, " nchars = %d\n", s->nchars);
2505 fprintf (stderr, " extends to end of line = %d\n",
2506 s->extends_to_end_of_line_p);
2507 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2508 fprintf (stderr, " bg width = %d\n", s->background_width);
2509 }
2510
2511 #endif /* GLYPH_DEBUG */
2512
2513
2514
2515 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2516 struct glyph_string **,
2517 struct glyph_string *,
2518 struct glyph_string *));
2519 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2520 struct glyph_string **,
2521 struct glyph_string *,
2522 struct glyph_string *));
2523 static void x_append_glyph_string P_ ((struct glyph_string **,
2524 struct glyph_string **,
2525 struct glyph_string *));
2526 static int x_left_overwritten P_ ((struct glyph_string *));
2527 static int x_left_overwriting P_ ((struct glyph_string *));
2528 static int x_right_overwritten P_ ((struct glyph_string *));
2529 static int x_right_overwriting P_ ((struct glyph_string *));
2530 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2531 int));
2532 static void x_init_glyph_string P_ ((struct glyph_string *,
2533 XChar2b *, struct window *,
2534 struct glyph_row *,
2535 enum glyph_row_area, int,
2536 enum draw_glyphs_face));
2537 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2538 enum glyph_row_area, int, int,
2539 enum draw_glyphs_face, int *, int *, int));
2540 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2541 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2542 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2543 int));
2544 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2545 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2546 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2547 static void x_draw_glyph_string P_ ((struct glyph_string *));
2548 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2549 static void x_set_cursor_gc P_ ((struct glyph_string *));
2550 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2551 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2552 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2553 int *, int *));
2554 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2555 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2556 unsigned long *, double, int));
2557 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2558 double, int, unsigned long));
2559 static void x_setup_relief_colors P_ ((struct glyph_string *));
2560 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2561 static void x_draw_image_relief P_ ((struct glyph_string *));
2562 static void x_draw_image_foreground P_ ((struct glyph_string *));
2563 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2564 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2565 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2566 int, int, int));
2567 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2568 int, int, int, int, XRectangle *));
2569 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2570 int, int, int, XRectangle *));
2571 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2572 enum glyph_row_area));
2573 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2574 struct glyph_row *,
2575 enum glyph_row_area, int, int));
2576
2577 #if GLYPH_DEBUG
2578 static void x_check_font P_ ((struct frame *, XFontStruct *));
2579 #endif
2580
2581
2582 /* Append the list of glyph strings with head H and tail T to the list
2583 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2584
2585 static INLINE void
2586 x_append_glyph_string_lists (head, tail, h, t)
2587 struct glyph_string **head, **tail;
2588 struct glyph_string *h, *t;
2589 {
2590 if (h)
2591 {
2592 if (*head)
2593 (*tail)->next = h;
2594 else
2595 *head = h;
2596 h->prev = *tail;
2597 *tail = t;
2598 }
2599 }
2600
2601
2602 /* Prepend the list of glyph strings with head H and tail T to the
2603 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2604 result. */
2605
2606 static INLINE void
2607 x_prepend_glyph_string_lists (head, tail, h, t)
2608 struct glyph_string **head, **tail;
2609 struct glyph_string *h, *t;
2610 {
2611 if (h)
2612 {
2613 if (*head)
2614 (*head)->prev = t;
2615 else
2616 *tail = t;
2617 t->next = *head;
2618 *head = h;
2619 }
2620 }
2621
2622
2623 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2624 Set *HEAD and *TAIL to the resulting list. */
2625
2626 static INLINE void
2627 x_append_glyph_string (head, tail, s)
2628 struct glyph_string **head, **tail;
2629 struct glyph_string *s;
2630 {
2631 s->next = s->prev = NULL;
2632 x_append_glyph_string_lists (head, tail, s, s);
2633 }
2634
2635
2636 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2637 face. */
2638
2639 static void
2640 x_set_cursor_gc (s)
2641 struct glyph_string *s;
2642 {
2643 if (s->font == FRAME_FONT (s->f)
2644 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2645 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2646 && !s->cmp)
2647 s->gc = s->f->output_data.x->cursor_gc;
2648 else
2649 {
2650 /* Cursor on non-default face: must merge. */
2651 XGCValues xgcv;
2652 unsigned long mask;
2653
2654 xgcv.background = s->f->output_data.x->cursor_pixel;
2655 xgcv.foreground = s->face->background;
2656
2657 /* If the glyph would be invisible, try a different foreground. */
2658 if (xgcv.foreground == xgcv.background)
2659 xgcv.foreground = s->face->foreground;
2660 if (xgcv.foreground == xgcv.background)
2661 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2662 if (xgcv.foreground == xgcv.background)
2663 xgcv.foreground = s->face->foreground;
2664
2665 /* Make sure the cursor is distinct from text in this face. */
2666 if (xgcv.background == s->face->background
2667 && xgcv.foreground == s->face->foreground)
2668 {
2669 xgcv.background = s->face->foreground;
2670 xgcv.foreground = s->face->background;
2671 }
2672
2673 IF_DEBUG (x_check_font (s->f, s->font));
2674 xgcv.font = s->font->fid;
2675 xgcv.graphics_exposures = False;
2676 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2677
2678 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2679 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2680 mask, &xgcv);
2681 else
2682 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2683 = XCreateGC (s->display, s->window, mask, &xgcv);
2684
2685 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2686 }
2687 }
2688
2689
2690 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2691
2692 static void
2693 x_set_mouse_face_gc (s)
2694 struct glyph_string *s;
2695 {
2696 int face_id;
2697 struct face *face;
2698
2699 /* What face has to be used last for the mouse face? */
2700 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2701 face = FACE_FROM_ID (s->f, face_id);
2702 if (face == NULL)
2703 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2704
2705 if (s->first_glyph->type == CHAR_GLYPH)
2706 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2707 else
2708 face_id = FACE_FOR_CHAR (s->f, face, 0);
2709 s->face = FACE_FROM_ID (s->f, face_id);
2710 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2711
2712 /* If font in this face is same as S->font, use it. */
2713 if (s->font == s->face->font)
2714 s->gc = s->face->gc;
2715 else
2716 {
2717 /* Otherwise construct scratch_cursor_gc with values from FACE
2718 but font FONT. */
2719 XGCValues xgcv;
2720 unsigned long mask;
2721
2722 xgcv.background = s->face->background;
2723 xgcv.foreground = s->face->foreground;
2724 IF_DEBUG (x_check_font (s->f, s->font));
2725 xgcv.font = s->font->fid;
2726 xgcv.graphics_exposures = False;
2727 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2728
2729 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2730 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2731 mask, &xgcv);
2732 else
2733 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2734 = XCreateGC (s->display, s->window, mask, &xgcv);
2735
2736 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2737 }
2738
2739 xassert (s->gc != 0);
2740 }
2741
2742
2743 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2744 Faces to use in the mode line have already been computed when the
2745 matrix was built, so there isn't much to do, here. */
2746
2747 static INLINE void
2748 x_set_mode_line_face_gc (s)
2749 struct glyph_string *s;
2750 {
2751 s->gc = s->face->gc;
2752 }
2753
2754
2755 /* Set S->gc of glyph string S for drawing that glyph string. Set
2756 S->stippled_p to a non-zero value if the face of S has a stipple
2757 pattern. */
2758
2759 static INLINE void
2760 x_set_glyph_string_gc (s)
2761 struct glyph_string *s;
2762 {
2763 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2764
2765 if (s->hl == DRAW_NORMAL_TEXT)
2766 {
2767 s->gc = s->face->gc;
2768 s->stippled_p = s->face->stipple != 0;
2769 }
2770 else if (s->hl == DRAW_INVERSE_VIDEO)
2771 {
2772 x_set_mode_line_face_gc (s);
2773 s->stippled_p = s->face->stipple != 0;
2774 }
2775 else if (s->hl == DRAW_CURSOR)
2776 {
2777 x_set_cursor_gc (s);
2778 s->stippled_p = 0;
2779 }
2780 else if (s->hl == DRAW_MOUSE_FACE)
2781 {
2782 x_set_mouse_face_gc (s);
2783 s->stippled_p = s->face->stipple != 0;
2784 }
2785 else if (s->hl == DRAW_IMAGE_RAISED
2786 || s->hl == DRAW_IMAGE_SUNKEN)
2787 {
2788 s->gc = s->face->gc;
2789 s->stippled_p = s->face->stipple != 0;
2790 }
2791 else
2792 {
2793 s->gc = s->face->gc;
2794 s->stippled_p = s->face->stipple != 0;
2795 }
2796
2797 /* GC must have been set. */
2798 xassert (s->gc != 0);
2799 }
2800
2801
2802 /* Return in *R the clipping rectangle for glyph string S. */
2803
2804 static void
2805 x_get_glyph_string_clip_rect (s, r)
2806 struct glyph_string *s;
2807 XRectangle *r;
2808 {
2809 if (s->row->full_width_p)
2810 {
2811 /* Draw full-width. X coordinates are relative to S->w->left. */
2812 int canon_x = CANON_X_UNIT (s->f);
2813
2814 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2815 r->width = XFASTINT (s->w->width) * canon_x;
2816
2817 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2818 {
2819 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2820 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2821 r->x -= width;
2822 }
2823
2824 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2825
2826 /* Unless displaying a mode or menu bar line, which are always
2827 fully visible, clip to the visible part of the row. */
2828 if (s->w->pseudo_window_p)
2829 r->height = s->row->visible_height;
2830 else
2831 r->height = s->height;
2832 }
2833 else
2834 {
2835 /* This is a text line that may be partially visible. */
2836 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2837 r->width = window_box_width (s->w, s->area);
2838 r->height = s->row->visible_height;
2839 }
2840
2841 /* Don't use S->y for clipping because it doesn't take partially
2842 visible lines into account. For example, it can be negative for
2843 partially visible lines at the top of a window. */
2844 if (!s->row->full_width_p
2845 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2846 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2847 else
2848 r->y = max (0, s->row->y);
2849
2850 /* If drawing a tool-bar window, draw it over the internal border
2851 at the top of the window. */
2852 if (s->w == XWINDOW (s->f->tool_bar_window))
2853 r->y -= s->f->output_data.x->internal_border_width;
2854
2855 /* If S draws overlapping rows, it's sufficient to use the top and
2856 bottom of the window for clipping because this glyph string
2857 intentionally draws over other lines. */
2858 if (s->for_overlaps_p)
2859 {
2860 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2861 r->height = window_text_bottom_y (s->w) - r->y;
2862 }
2863
2864 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2865 }
2866
2867
2868 /* Set clipping for output of glyph string S. S may be part of a mode
2869 line or menu if we don't have X toolkit support. */
2870
2871 static INLINE void
2872 x_set_glyph_string_clipping (s)
2873 struct glyph_string *s;
2874 {
2875 XRectangle r;
2876 x_get_glyph_string_clip_rect (s, &r);
2877 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2878 }
2879
2880
2881 /* Compute left and right overhang of glyph string S. If S is a glyph
2882 string for a composition, assume overhangs don't exist. */
2883
2884 static INLINE void
2885 x_compute_glyph_string_overhangs (s)
2886 struct glyph_string *s;
2887 {
2888 if (s->cmp == NULL
2889 && s->first_glyph->type == CHAR_GLYPH)
2890 {
2891 XCharStruct cs;
2892 int direction, font_ascent, font_descent;
2893 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2894 &font_ascent, &font_descent, &cs);
2895 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2896 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2897 }
2898 }
2899
2900
2901 /* Compute overhangs and x-positions for glyph string S and its
2902 predecessors, or successors. X is the starting x-position for S.
2903 BACKWARD_P non-zero means process predecessors. */
2904
2905 static void
2906 x_compute_overhangs_and_x (s, x, backward_p)
2907 struct glyph_string *s;
2908 int x;
2909 int backward_p;
2910 {
2911 if (backward_p)
2912 {
2913 while (s)
2914 {
2915 x_compute_glyph_string_overhangs (s);
2916 x -= s->width;
2917 s->x = x;
2918 s = s->prev;
2919 }
2920 }
2921 else
2922 {
2923 while (s)
2924 {
2925 x_compute_glyph_string_overhangs (s);
2926 s->x = x;
2927 x += s->width;
2928 s = s->next;
2929 }
2930 }
2931 }
2932
2933
2934 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2935 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2936 assumed to be zero. */
2937
2938 static void
2939 x_get_glyph_overhangs (glyph, f, left, right)
2940 struct glyph *glyph;
2941 struct frame *f;
2942 int *left, *right;
2943 {
2944 *left = *right = 0;
2945
2946 if (glyph->type == CHAR_GLYPH)
2947 {
2948 XFontStruct *font;
2949 struct face *face;
2950 struct font_info *font_info;
2951 XChar2b char2b;
2952 XCharStruct *pcm;
2953
2954 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2955 font = face->font;
2956 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2957 if (font
2958 && (pcm = x_per_char_metric (font, &char2b)))
2959 {
2960 if (pcm->rbearing > pcm->width)
2961 *right = pcm->rbearing - pcm->width;
2962 if (pcm->lbearing < 0)
2963 *left = -pcm->lbearing;
2964 }
2965 }
2966 }
2967
2968
2969 /* Return the index of the first glyph preceding glyph string S that
2970 is overwritten by S because of S's left overhang. Value is -1
2971 if no glyphs are overwritten. */
2972
2973 static int
2974 x_left_overwritten (s)
2975 struct glyph_string *s;
2976 {
2977 int k;
2978
2979 if (s->left_overhang)
2980 {
2981 int x = 0, i;
2982 struct glyph *glyphs = s->row->glyphs[s->area];
2983 int first = s->first_glyph - glyphs;
2984
2985 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2986 x -= glyphs[i].pixel_width;
2987
2988 k = i + 1;
2989 }
2990 else
2991 k = -1;
2992
2993 return k;
2994 }
2995
2996
2997 /* Return the index of the first glyph preceding glyph string S that
2998 is overwriting S because of its right overhang. Value is -1 if no
2999 glyph in front of S overwrites S. */
3000
3001 static int
3002 x_left_overwriting (s)
3003 struct glyph_string *s;
3004 {
3005 int i, k, x;
3006 struct glyph *glyphs = s->row->glyphs[s->area];
3007 int first = s->first_glyph - glyphs;
3008
3009 k = -1;
3010 x = 0;
3011 for (i = first - 1; i >= 0; --i)
3012 {
3013 int left, right;
3014 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3015 if (x + right > 0)
3016 k = i;
3017 x -= glyphs[i].pixel_width;
3018 }
3019
3020 return k;
3021 }
3022
3023
3024 /* Return the index of the last glyph following glyph string S that is
3025 not overwritten by S because of S's right overhang. Value is -1 if
3026 no such glyph is found. */
3027
3028 static int
3029 x_right_overwritten (s)
3030 struct glyph_string *s;
3031 {
3032 int k = -1;
3033
3034 if (s->right_overhang)
3035 {
3036 int x = 0, i;
3037 struct glyph *glyphs = s->row->glyphs[s->area];
3038 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3039 int end = s->row->used[s->area];
3040
3041 for (i = first; i < end && s->right_overhang > x; ++i)
3042 x += glyphs[i].pixel_width;
3043
3044 k = i;
3045 }
3046
3047 return k;
3048 }
3049
3050
3051 /* Return the index of the last glyph following glyph string S that
3052 overwrites S because of its left overhang. Value is negative
3053 if no such glyph is found. */
3054
3055 static int
3056 x_right_overwriting (s)
3057 struct glyph_string *s;
3058 {
3059 int i, k, x;
3060 int end = s->row->used[s->area];
3061 struct glyph *glyphs = s->row->glyphs[s->area];
3062 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3063
3064 k = -1;
3065 x = 0;
3066 for (i = first; i < end; ++i)
3067 {
3068 int left, right;
3069 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3070 if (x - left < 0)
3071 k = i;
3072 x += glyphs[i].pixel_width;
3073 }
3074
3075 return k;
3076 }
3077
3078
3079 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3080
3081 static INLINE void
3082 x_clear_glyph_string_rect (s, x, y, w, h)
3083 struct glyph_string *s;
3084 int x, y, w, h;
3085 {
3086 XGCValues xgcv;
3087 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3088 XSetForeground (s->display, s->gc, xgcv.background);
3089 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3090 XSetForeground (s->display, s->gc, xgcv.foreground);
3091 }
3092
3093
3094 /* Draw the background of glyph_string S. If S->background_filled_p
3095 is non-zero don't draw it. FORCE_P non-zero means draw the
3096 background even if it wouldn't be drawn normally. This is used
3097 when a string preceding S draws into the background of S, or S
3098 contains the first component of a composition. */
3099
3100 static void
3101 x_draw_glyph_string_background (s, force_p)
3102 struct glyph_string *s;
3103 int force_p;
3104 {
3105 /* Nothing to do if background has already been drawn or if it
3106 shouldn't be drawn in the first place. */
3107 if (!s->background_filled_p)
3108 {
3109 int box_line_width = max (s->face->box_line_width, 0);
3110
3111 if (s->stippled_p)
3112 {
3113 /* Fill background with a stipple pattern. */
3114 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3115 XFillRectangle (s->display, s->window, s->gc, s->x,
3116 s->y + box_line_width,
3117 s->background_width,
3118 s->height - 2 * box_line_width);
3119 XSetFillStyle (s->display, s->gc, FillSolid);
3120 s->background_filled_p = 1;
3121 }
3122 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
3123 || s->font_not_found_p
3124 || s->extends_to_end_of_line_p
3125 || force_p)
3126 {
3127 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
3128 s->background_width,
3129 s->height - 2 * box_line_width);
3130 s->background_filled_p = 1;
3131 }
3132 }
3133 }
3134
3135
3136 /* Draw the foreground of glyph string S. */
3137
3138 static void
3139 x_draw_glyph_string_foreground (s)
3140 struct glyph_string *s;
3141 {
3142 int i, x;
3143
3144 /* If first glyph of S has a left box line, start drawing the text
3145 of S to the right of that box line. */
3146 if (s->face->box != FACE_NO_BOX
3147 && s->first_glyph->left_box_line_p)
3148 x = s->x + abs (s->face->box_line_width);
3149 else
3150 x = s->x;
3151
3152 /* Draw characters of S as rectangles if S's font could not be
3153 loaded. */
3154 if (s->font_not_found_p)
3155 {
3156 for (i = 0; i < s->nchars; ++i)
3157 {
3158 struct glyph *g = s->first_glyph + i;
3159 XDrawRectangle (s->display, s->window,
3160 s->gc, x, s->y, g->pixel_width - 1,
3161 s->height - 1);
3162 x += g->pixel_width;
3163 }
3164 }
3165 else
3166 {
3167 char *char1b = (char *) s->char2b;
3168 int boff = s->font_info->baseline_offset;
3169
3170 if (s->font_info->vertical_centering)
3171 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3172
3173 /* If we can use 8-bit functions, condense S->char2b. */
3174 if (!s->two_byte_p)
3175 for (i = 0; i < s->nchars; ++i)
3176 char1b[i] = s->char2b[i].byte2;
3177
3178 /* Draw text with XDrawString if background has already been
3179 filled. Otherwise, use XDrawImageString. (Note that
3180 XDrawImageString is usually faster than XDrawString.) Always
3181 use XDrawImageString when drawing the cursor so that there is
3182 no chance that characters under a box cursor are invisible. */
3183 if (s->for_overlaps_p
3184 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3185 {
3186 /* Draw characters with 16-bit or 8-bit functions. */
3187 if (s->two_byte_p)
3188 XDrawString16 (s->display, s->window, s->gc, x,
3189 s->ybase - boff, s->char2b, s->nchars);
3190 else
3191 XDrawString (s->display, s->window, s->gc, x,
3192 s->ybase - boff, char1b, s->nchars);
3193 }
3194 else
3195 {
3196 if (s->two_byte_p)
3197 XDrawImageString16 (s->display, s->window, s->gc, x,
3198 s->ybase - boff, s->char2b, s->nchars);
3199 else
3200 XDrawImageString (s->display, s->window, s->gc, x,
3201 s->ybase - boff, char1b, s->nchars);
3202 }
3203 }
3204 }
3205
3206 /* Draw the foreground of composite glyph string S. */
3207
3208 static void
3209 x_draw_composite_glyph_string_foreground (s)
3210 struct glyph_string *s;
3211 {
3212 int i, x;
3213
3214 /* If first glyph of S has a left box line, start drawing the text
3215 of S to the right of that box line. */
3216 if (s->face->box != FACE_NO_BOX
3217 && s->first_glyph->left_box_line_p)
3218 x = s->x + abs (s->face->box_line_width);
3219 else
3220 x = s->x;
3221
3222 /* S is a glyph string for a composition. S->gidx is the index of
3223 the first character drawn for glyphs of this composition.
3224 S->gidx == 0 means we are drawing the very first character of
3225 this composition. */
3226
3227 /* Draw a rectangle for the composition if the font for the very
3228 first character of the composition could not be loaded. */
3229 if (s->font_not_found_p)
3230 {
3231 if (s->gidx == 0)
3232 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3233 s->width - 1, s->height - 1);
3234 }
3235 else
3236 {
3237 for (i = 0; i < s->nchars; i++, ++s->gidx)
3238 XDrawString16 (s->display, s->window, s->gc,
3239 x + s->cmp->offsets[s->gidx * 2],
3240 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3241 s->char2b + i, 1);
3242 }
3243 }
3244
3245
3246 #ifdef USE_X_TOOLKIT
3247
3248 static struct frame *x_frame_of_widget P_ ((Widget));
3249
3250
3251 /* Return the frame on which widget WIDGET is used.. Abort if frame
3252 cannot be determined. */
3253
3254 static struct frame *
3255 x_frame_of_widget (widget)
3256 Widget widget;
3257 {
3258 struct x_display_info *dpyinfo;
3259 Lisp_Object tail;
3260 struct frame *f;
3261
3262 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3263
3264 /* Find the top-level shell of the widget. Note that this function
3265 can be called when the widget is not yet realized, so XtWindow
3266 (widget) == 0. That's the reason we can't simply use
3267 x_any_window_to_frame. */
3268 while (!XtIsTopLevelShell (widget))
3269 widget = XtParent (widget);
3270
3271 /* Look for a frame with that top-level widget. Allocate the color
3272 on that frame to get the right gamma correction value. */
3273 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3274 if (GC_FRAMEP (XCAR (tail))
3275 && (f = XFRAME (XCAR (tail)),
3276 (f->output_data.nothing != 1
3277 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3278 && f->output_data.x->widget == widget)
3279 return f;
3280
3281 abort ();
3282 }
3283
3284
3285 /* Allocate the color COLOR->pixel on the screen and display of
3286 widget WIDGET in colormap CMAP. If an exact match cannot be
3287 allocated, try the nearest color available. Value is non-zero
3288 if successful. This is called from lwlib. */
3289
3290 int
3291 x_alloc_nearest_color_for_widget (widget, cmap, color)
3292 Widget widget;
3293 Colormap cmap;
3294 XColor *color;
3295 {
3296 struct frame *f = x_frame_of_widget (widget);
3297 return x_alloc_nearest_color (f, cmap, color);
3298 }
3299
3300
3301 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3302 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3303 If this produces the same color as PIXEL, try a color where all RGB
3304 values have DELTA added. Return the allocated color in *PIXEL.
3305 DISPLAY is the X display, CMAP is the colormap to operate on.
3306 Value is non-zero if successful. */
3307
3308 int
3309 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3310 Widget widget;
3311 Display *display;
3312 Colormap cmap;
3313 unsigned long *pixel;
3314 double factor;
3315 int delta;
3316 {
3317 struct frame *f = x_frame_of_widget (widget);
3318 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3319 }
3320
3321
3322 #endif /* USE_X_TOOLKIT */
3323
3324
3325 /* Value is an array of XColor structures for the contents of the
3326 color map of frame F. Set *NCELLS to the size of the array.
3327 Note that this probably shouldn't be called for large color maps,
3328 say a 24-bit TrueColor map. */
3329
3330 static const XColor *
3331 x_color_cells (f, ncells)
3332 struct frame *f;
3333 int *ncells;
3334 {
3335 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3336
3337 if (dpyinfo->color_cells == NULL)
3338 {
3339 Display *display = FRAME_X_DISPLAY (f);
3340 Screen *screen = FRAME_X_SCREEN (f);
3341 int i;
3342
3343 dpyinfo->ncolor_cells
3344 = XDisplayCells (display, XScreenNumberOfScreen (screen));
3345 dpyinfo->color_cells
3346 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3347 * sizeof *dpyinfo->color_cells);
3348
3349 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3350 dpyinfo->color_cells[i].pixel = i;
3351
3352 XQueryColors (display, FRAME_X_COLORMAP (f),
3353 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3354 }
3355
3356 *ncells = dpyinfo->ncolor_cells;
3357 return dpyinfo->color_cells;
3358 }
3359
3360
3361 /* On frame F, translate pixel colors to RGB values for the NCOLORS
3362 colors in COLORS. Use cached information, if available. */
3363
3364 void
3365 x_query_colors (f, colors, ncolors)
3366 struct frame *f;
3367 XColor *colors;
3368 int ncolors;
3369 {
3370 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3371
3372 if (dpyinfo->color_cells)
3373 {
3374 int i;
3375 for (i = 0; i < ncolors; ++i)
3376 {
3377 unsigned long pixel = colors[i].pixel;
3378 xassert (pixel < dpyinfo->ncolor_cells);
3379 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3380 colors[i] = dpyinfo->color_cells[pixel];
3381 }
3382 }
3383 else
3384 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3385 }
3386
3387
3388 /* On frame F, translate pixel color to RGB values for the color in
3389 COLOR. Use cached information, if available. */
3390
3391 void
3392 x_query_color (f, color)
3393 struct frame *f;
3394 XColor *color;
3395 {
3396 x_query_colors (f, color, 1);
3397 }
3398
3399
3400 /* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3401 CMAP. If an exact match can't be allocated, try the nearest color
3402 available. Value is non-zero if successful. Set *COLOR to the
3403 color allocated. */
3404
3405 int
3406 x_alloc_nearest_color (f, cmap, color)
3407 struct frame *f;
3408 Colormap cmap;
3409 XColor *color;
3410 {
3411 Display *display = FRAME_X_DISPLAY (f);
3412 Screen *screen = FRAME_X_SCREEN (f);
3413 int rc;
3414
3415 gamma_correct (f, color);
3416 rc = XAllocColor (display, cmap, color);
3417 if (rc == 0)
3418 {
3419 /* If we got to this point, the colormap is full, so we're going
3420 to try to get the next closest color. The algorithm used is
3421 a least-squares matching, which is what X uses for closest
3422 color matching with StaticColor visuals. */
3423 int nearest, i;
3424 unsigned long nearest_delta = ~0;
3425 int ncells;
3426 const XColor *cells = x_color_cells (f, &ncells);
3427
3428 for (nearest = i = 0; i < ncells; ++i)
3429 {
3430 long dred = (color->red >> 8) - (cells[i].red >> 8);
3431 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3432 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3433 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3434
3435 if (delta < nearest_delta)
3436 {
3437 nearest = i;
3438 nearest_delta = delta;
3439 }
3440 }
3441
3442 color->red = cells[nearest].red;
3443 color->green = cells[nearest].green;
3444 color->blue = cells[nearest].blue;
3445 rc = XAllocColor (display, cmap, color);
3446 }
3447 else
3448 {
3449 /* If allocation succeeded, and the allocated pixel color is not
3450 equal to a cached pixel color recorded earlier, there was a
3451 change in the colormap, so clear the color cache. */
3452 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3453 XColor *cached_color;
3454
3455 if (dpyinfo->color_cells
3456 && (cached_color = &dpyinfo->color_cells[color->pixel],
3457 (cached_color->red != color->red
3458 || cached_color->blue != color->blue
3459 || cached_color->green != color->green)))
3460 {
3461 xfree (dpyinfo->color_cells);
3462 dpyinfo->color_cells = NULL;
3463 dpyinfo->ncolor_cells = 0;
3464 }
3465 }
3466
3467 #ifdef DEBUG_X_COLORS
3468 if (rc)
3469 register_color (color->pixel);
3470 #endif /* DEBUG_X_COLORS */
3471
3472 return rc;
3473 }
3474
3475
3476 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3477 It's necessary to do this instead of just using PIXEL directly to
3478 get color reference counts right. */
3479
3480 unsigned long
3481 x_copy_color (f, pixel)
3482 struct frame *f;
3483 unsigned long pixel;
3484 {
3485 XColor color;
3486
3487 color.pixel = pixel;
3488 BLOCK_INPUT;
3489 x_query_color (f, &color);
3490 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3491 UNBLOCK_INPUT;
3492 #ifdef DEBUG_X_COLORS
3493 register_color (pixel);
3494 #endif
3495 return color.pixel;
3496 }
3497
3498
3499 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3500 It's necessary to do this instead of just using PIXEL directly to
3501 get color reference counts right. */
3502
3503 unsigned long
3504 x_copy_dpy_color (dpy, cmap, pixel)
3505 Display *dpy;
3506 Colormap cmap;
3507 unsigned long pixel;
3508 {
3509 XColor color;
3510
3511 color.pixel = pixel;
3512 BLOCK_INPUT;
3513 XQueryColor (dpy, cmap, &color);
3514 XAllocColor (dpy, cmap, &color);
3515 UNBLOCK_INPUT;
3516 #ifdef DEBUG_X_COLORS
3517 register_color (pixel);
3518 #endif
3519 return color.pixel;
3520 }
3521
3522
3523 /* Brightness beyond which a color won't have its highlight brightness
3524 boosted.
3525
3526 Nominally, highlight colors for `3d' faces are calculated by
3527 brightening an object's color by a constant scale factor, but this
3528 doesn't yield good results for dark colors, so for colors who's
3529 brightness is less than this value (on a scale of 0-65535) have an
3530 use an additional additive factor.
3531
3532 The value here is set so that the default menu-bar/mode-line color
3533 (grey75) will not have its highlights changed at all. */
3534 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3535
3536
3537 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3538 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3539 If this produces the same color as PIXEL, try a color where all RGB
3540 values have DELTA added. Return the allocated color in *PIXEL.
3541 DISPLAY is the X display, CMAP is the colormap to operate on.
3542 Value is non-zero if successful. */
3543
3544 static int
3545 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3546 struct frame *f;
3547 Display *display;
3548 Colormap cmap;
3549 unsigned long *pixel;
3550 double factor;
3551 int delta;
3552 {
3553 XColor color, new;
3554 long bright;
3555 int success_p;
3556
3557 /* Get RGB color values. */
3558 color.pixel = *pixel;
3559 x_query_color (f, &color);
3560
3561 /* Change RGB values by specified FACTOR. Avoid overflow! */
3562 xassert (factor >= 0);
3563 new.red = min (0xffff, factor * color.red);
3564 new.green = min (0xffff, factor * color.green);
3565 new.blue = min (0xffff, factor * color.blue);
3566
3567 /* Calculate brightness of COLOR. */
3568 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
3569
3570 /* We only boost colors that are darker than
3571 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3572 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3573 /* Make an additive adjustment to NEW, because it's dark enough so
3574 that scaling by FACTOR alone isn't enough. */
3575 {
3576 /* How far below the limit this color is (0 - 1, 1 being darker). */
3577 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3578 /* The additive adjustment. */
3579 int min_delta = delta * dimness * factor / 2;
3580
3581 if (factor < 1)
3582 {
3583 new.red = max (0, new.red - min_delta);
3584 new.green = max (0, new.green - min_delta);
3585 new.blue = max (0, new.blue - min_delta);
3586 }
3587 else
3588 {
3589 new.red = min (0xffff, min_delta + new.red);
3590 new.green = min (0xffff, min_delta + new.green);
3591 new.blue = min (0xffff, min_delta + new.blue);
3592 }
3593 }
3594
3595 /* Try to allocate the color. */
3596 success_p = x_alloc_nearest_color (f, cmap, &new);
3597 if (success_p)
3598 {
3599 if (new.pixel == *pixel)
3600 {
3601 /* If we end up with the same color as before, try adding
3602 delta to the RGB values. */
3603 x_free_colors (f, &new.pixel, 1);
3604
3605 new.red = min (0xffff, delta + color.red);
3606 new.green = min (0xffff, delta + color.green);
3607 new.blue = min (0xffff, delta + color.blue);
3608 success_p = x_alloc_nearest_color (f, cmap, &new);
3609 }
3610 else
3611 success_p = 1;
3612 *pixel = new.pixel;
3613 }
3614
3615 return success_p;
3616 }
3617
3618
3619 /* Set up the foreground color for drawing relief lines of glyph
3620 string S. RELIEF is a pointer to a struct relief containing the GC
3621 with which lines will be drawn. Use a color that is FACTOR or
3622 DELTA lighter or darker than the relief's background which is found
3623 in S->f->output_data.x->relief_background. If such a color cannot
3624 be allocated, use DEFAULT_PIXEL, instead. */
3625
3626 static void
3627 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3628 struct frame *f;
3629 struct relief *relief;
3630 double factor;
3631 int delta;
3632 unsigned long default_pixel;
3633 {
3634 XGCValues xgcv;
3635 struct x_output *di = f->output_data.x;
3636 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3637 unsigned long pixel;
3638 unsigned long background = di->relief_background;
3639 Colormap cmap = FRAME_X_COLORMAP (f);
3640 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3641 Display *dpy = FRAME_X_DISPLAY (f);
3642
3643 xgcv.graphics_exposures = False;
3644 xgcv.line_width = 1;
3645
3646 /* Free previously allocated color. The color cell will be reused
3647 when it has been freed as many times as it was allocated, so this
3648 doesn't affect faces using the same colors. */
3649 if (relief->gc
3650 && relief->allocated_p)
3651 {
3652 x_free_colors (f, &relief->pixel, 1);
3653 relief->allocated_p = 0;
3654 }
3655
3656 /* Allocate new color. */
3657 xgcv.foreground = default_pixel;
3658 pixel = background;
3659 if (dpyinfo->n_planes != 1
3660 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3661 {
3662 relief->allocated_p = 1;
3663 xgcv.foreground = relief->pixel = pixel;
3664 }
3665
3666 if (relief->gc == 0)
3667 {
3668 xgcv.stipple = dpyinfo->gray;
3669 mask |= GCStipple;
3670 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3671 }
3672 else
3673 XChangeGC (dpy, relief->gc, mask, &xgcv);
3674 }
3675
3676
3677 /* Set up colors for the relief lines around glyph string S. */
3678
3679 static void
3680 x_setup_relief_colors (s)
3681 struct glyph_string *s;
3682 {
3683 struct x_output *di = s->f->output_data.x;
3684 unsigned long color;
3685
3686 if (s->face->use_box_color_for_shadows_p)
3687 color = s->face->box_color;
3688 else
3689 {
3690 XGCValues xgcv;
3691
3692 /* Get the background color of the face. */
3693 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3694 color = xgcv.background;
3695 }
3696
3697 if (di->white_relief.gc == 0
3698 || color != di->relief_background)
3699 {
3700 di->relief_background = color;
3701 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3702 WHITE_PIX_DEFAULT (s->f));
3703 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3704 BLACK_PIX_DEFAULT (s->f));
3705 }
3706 }
3707
3708
3709 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3710 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3711 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3712 relief. LEFT_P non-zero means draw a relief on the left side of
3713 the rectangle. RIGHT_P non-zero means draw a relief on the right
3714 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3715 when drawing. */
3716
3717 static void
3718 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3719 raised_p, left_p, right_p, clip_rect)
3720 struct frame *f;
3721 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3722 XRectangle *clip_rect;
3723 {
3724 int i;
3725 GC gc;
3726
3727 if (raised_p)
3728 gc = f->output_data.x->white_relief.gc;
3729 else
3730 gc = f->output_data.x->black_relief.gc;
3731 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3732
3733 /* Top. */
3734 for (i = 0; i < width; ++i)
3735 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3736 left_x + i * left_p, top_y + i,
3737 right_x + 1 - i * right_p, top_y + i);
3738
3739 /* Left. */
3740 if (left_p)
3741 for (i = 0; i < width; ++i)
3742 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3743 left_x + i, top_y + i, left_x + i, bottom_y - i);
3744
3745 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3746 if (raised_p)
3747 gc = f->output_data.x->black_relief.gc;
3748 else
3749 gc = f->output_data.x->white_relief.gc;
3750 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3751
3752 /* Bottom. */
3753 for (i = 0; i < width; ++i)
3754 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3755 left_x + i * left_p, bottom_y - i,
3756 right_x + 1 - i * right_p, bottom_y - i);
3757
3758 /* Right. */
3759 if (right_p)
3760 for (i = 0; i < width; ++i)
3761 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3762 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3763
3764 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3765 }
3766
3767
3768 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3769 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3770 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3771 left side of the rectangle. RIGHT_P non-zero means draw a line
3772 on the right side of the rectangle. CLIP_RECT is the clipping
3773 rectangle to use when drawing. */
3774
3775 static void
3776 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3777 left_p, right_p, clip_rect)
3778 struct glyph_string *s;
3779 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3780 XRectangle *clip_rect;
3781 {
3782 XGCValues xgcv;
3783
3784 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3785 XSetForeground (s->display, s->gc, s->face->box_color);
3786 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3787
3788 /* Top. */
3789 XFillRectangle (s->display, s->window, s->gc,
3790 left_x, top_y, right_x - left_x + 1, width);
3791
3792 /* Left. */
3793 if (left_p)
3794 XFillRectangle (s->display, s->window, s->gc,
3795 left_x, top_y, width, bottom_y - top_y + 1);
3796
3797 /* Bottom. */
3798 XFillRectangle (s->display, s->window, s->gc,
3799 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3800
3801 /* Right. */
3802 if (right_p)
3803 XFillRectangle (s->display, s->window, s->gc,
3804 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3805
3806 XSetForeground (s->display, s->gc, xgcv.foreground);
3807 XSetClipMask (s->display, s->gc, None);
3808 }
3809
3810
3811 /* Draw a box around glyph string S. */
3812
3813 static void
3814 x_draw_glyph_string_box (s)
3815 struct glyph_string *s;
3816 {
3817 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3818 int left_p, right_p;
3819 struct glyph *last_glyph;
3820 XRectangle clip_rect;
3821
3822 last_x = window_box_right (s->w, s->area);
3823 if (s->row->full_width_p
3824 && !s->w->pseudo_window_p)
3825 {
3826 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3827 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3828 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3829 }
3830
3831 /* The glyph that may have a right box line. */
3832 last_glyph = (s->cmp || s->img
3833 ? s->first_glyph
3834 : s->first_glyph + s->nchars - 1);
3835
3836 width = abs (s->face->box_line_width);
3837 raised_p = s->face->box == FACE_RAISED_BOX;
3838 left_x = s->x;
3839 right_x = ((s->row->full_width_p
3840 ? last_x - 1
3841 : min (last_x, s->x + s->background_width) - 1));
3842 top_y = s->y;
3843 bottom_y = top_y + s->height - 1;
3844
3845 left_p = (s->first_glyph->left_box_line_p
3846 || (s->hl == DRAW_MOUSE_FACE
3847 && (s->prev == NULL
3848 || s->prev->hl != s->hl)));
3849 right_p = (last_glyph->right_box_line_p
3850 || (s->hl == DRAW_MOUSE_FACE
3851 && (s->next == NULL
3852 || s->next->hl != s->hl)));
3853
3854 x_get_glyph_string_clip_rect (s, &clip_rect);
3855
3856 if (s->face->box == FACE_SIMPLE_BOX)
3857 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3858 left_p, right_p, &clip_rect);
3859 else
3860 {
3861 x_setup_relief_colors (s);
3862 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3863 width, raised_p, left_p, right_p, &clip_rect);
3864 }
3865 }
3866
3867
3868 /* Draw foreground of image glyph string S. */
3869
3870 static void
3871 x_draw_image_foreground (s)
3872 struct glyph_string *s;
3873 {
3874 int x;
3875 int y = s->ybase - image_ascent (s->img, s->face);
3876
3877 /* If first glyph of S has a left box line, start drawing it to the
3878 right of that line. */
3879 if (s->face->box != FACE_NO_BOX
3880 && s->first_glyph->left_box_line_p)
3881 x = s->x + abs (s->face->box_line_width);
3882 else
3883 x = s->x;
3884
3885 /* If there is a margin around the image, adjust x- and y-position
3886 by that margin. */
3887 x += s->img->hmargin;
3888 y += s->img->vmargin;
3889
3890 if (s->img->pixmap)
3891 {
3892 if (s->img->mask)
3893 {
3894 /* We can't set both a clip mask and use XSetClipRectangles
3895 because the latter also sets a clip mask. We also can't
3896 trust on the shape extension to be available
3897 (XShapeCombineRegion). So, compute the rectangle to draw
3898 manually. */
3899 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3900 | GCFunction);
3901 XGCValues xgcv;
3902 XRectangle clip_rect, image_rect, r;
3903
3904 xgcv.clip_mask = s->img->mask;
3905 xgcv.clip_x_origin = x;
3906 xgcv.clip_y_origin = y;
3907 xgcv.function = GXcopy;
3908 XChangeGC (s->display, s->gc, mask, &xgcv);
3909
3910 x_get_glyph_string_clip_rect (s, &clip_rect);
3911 image_rect.x = x;
3912 image_rect.y = y;
3913 image_rect.width = s->img->width;
3914 image_rect.height = s->img->height;
3915 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3916 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3917 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3918 }
3919 else
3920 {
3921 unsigned long mask = GCClipXOrigin | GCClipYOrigin | GCFunction;
3922 XGCValues xgcv;
3923 XRectangle clip_rect, image_rect, r;
3924
3925 x_get_glyph_string_clip_rect (s, &clip_rect);
3926 image_rect.x = x;
3927 image_rect.y = y;
3928 image_rect.width = s->img->width;
3929 image_rect.height = s->img->height;
3930 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3931 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3932 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3933
3934 /* When the image has a mask, we can expect that at
3935 least part of a mouse highlight or a block cursor will
3936 be visible. If the image doesn't have a mask, make
3937 a block cursor visible by drawing a rectangle around
3938 the image. I believe it's looking better if we do
3939 nothing here for mouse-face. */
3940 if (s->hl == DRAW_CURSOR)
3941 XDrawRectangle (s->display, s->window, s->gc, x, y,
3942 s->img->width - 1, s->img->height - 1);
3943 }
3944 }
3945 else
3946 /* Draw a rectangle if image could not be loaded. */
3947 XDrawRectangle (s->display, s->window, s->gc, x, y,
3948 s->img->width - 1, s->img->height - 1);
3949 }
3950
3951
3952 /* Draw a relief around the image glyph string S. */
3953
3954 static void
3955 x_draw_image_relief (s)
3956 struct glyph_string *s;
3957 {
3958 int x0, y0, x1, y1, thick, raised_p;
3959 XRectangle r;
3960 int x;
3961 int y = s->ybase - image_ascent (s->img, s->face);
3962
3963 /* If first glyph of S has a left box line, start drawing it to the
3964 right of that line. */
3965 if (s->face->box != FACE_NO_BOX
3966 && s->first_glyph->left_box_line_p)
3967 x = s->x + abs (s->face->box_line_width);
3968 else
3969 x = s->x;
3970
3971 /* If there is a margin around the image, adjust x- and y-position
3972 by that margin. */
3973 x += s->img->hmargin;
3974 y += s->img->vmargin;
3975
3976 if (s->hl == DRAW_IMAGE_SUNKEN
3977 || s->hl == DRAW_IMAGE_RAISED)
3978 {
3979 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
3980 raised_p = s->hl == DRAW_IMAGE_RAISED;
3981 }
3982 else
3983 {
3984 thick = abs (s->img->relief);
3985 raised_p = s->img->relief > 0;
3986 }
3987
3988 x0 = x - thick;
3989 y0 = y - thick;
3990 x1 = x + s->img->width + thick - 1;
3991 y1 = y + s->img->height + thick - 1;
3992
3993 x_setup_relief_colors (s);
3994 x_get_glyph_string_clip_rect (s, &r);
3995 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3996 }
3997
3998
3999 /* Draw the foreground of image glyph string S to PIXMAP. */
4000
4001 static void
4002 x_draw_image_foreground_1 (s, pixmap)
4003 struct glyph_string *s;
4004 Pixmap pixmap;
4005 {
4006 int x;
4007 int y = s->ybase - s->y - image_ascent (s->img, s->face);
4008
4009 /* If first glyph of S has a left box line, start drawing it to the
4010 right of that line. */
4011 if (s->face->box != FACE_NO_BOX
4012 && s->first_glyph->left_box_line_p)
4013 x = abs (s->face->box_line_width);
4014 else
4015 x = 0;
4016
4017 /* If there is a margin around the image, adjust x- and y-position
4018 by that margin. */
4019 x += s->img->hmargin;
4020 y += s->img->vmargin;
4021
4022 if (s->img->pixmap)
4023 {
4024 if (s->img->mask)
4025 {
4026 /* We can't set both a clip mask and use XSetClipRectangles
4027 because the latter also sets a clip mask. We also can't
4028 trust on the shape extension to be available
4029 (XShapeCombineRegion). So, compute the rectangle to draw
4030 manually. */
4031 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4032 | GCFunction);
4033 XGCValues xgcv;
4034
4035 xgcv.clip_mask = s->img->mask;
4036 xgcv.clip_x_origin = x;
4037 xgcv.clip_y_origin = y;
4038 xgcv.function = GXcopy;
4039 XChangeGC (s->display, s->gc, mask, &xgcv);
4040
4041 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4042 0, 0, s->img->width, s->img->height, x, y);
4043 XSetClipMask (s->display, s->gc, None);
4044 }
4045 else
4046 {
4047 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4048 0, 0, s->img->width, s->img->height, x, y);
4049
4050 /* When the image has a mask, we can expect that at
4051 least part of a mouse highlight or a block cursor will
4052 be visible. If the image doesn't have a mask, make
4053 a block cursor visible by drawing a rectangle around
4054 the image. I believe it's looking better if we do
4055 nothing here for mouse-face. */
4056 if (s->hl == DRAW_CURSOR)
4057 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4058 s->img->width - 1, s->img->height - 1);
4059 }
4060 }
4061 else
4062 /* Draw a rectangle if image could not be loaded. */
4063 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4064 s->img->width - 1, s->img->height - 1);
4065 }
4066
4067
4068 /* Draw part of the background of glyph string S. X, Y, W, and H
4069 give the rectangle to draw. */
4070
4071 static void
4072 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4073 struct glyph_string *s;
4074 int x, y, w, h;
4075 {
4076 if (s->stippled_p)
4077 {
4078 /* Fill background with a stipple pattern. */
4079 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4080 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4081 XSetFillStyle (s->display, s->gc, FillSolid);
4082 }
4083 else
4084 x_clear_glyph_string_rect (s, x, y, w, h);
4085 }
4086
4087
4088 /* Draw image glyph string S.
4089
4090 s->y
4091 s->x +-------------------------
4092 | s->face->box
4093 |
4094 | +-------------------------
4095 | | s->img->margin
4096 | |
4097 | | +-------------------
4098 | | | the image
4099
4100 */
4101
4102 static void
4103 x_draw_image_glyph_string (s)
4104 struct glyph_string *s;
4105 {
4106 int x, y;
4107 int box_line_hwidth = abs (s->face->box_line_width);
4108 int box_line_vwidth = max (s->face->box_line_width, 0);
4109 int height;
4110 Pixmap pixmap = None;
4111
4112 height = s->height - 2 * box_line_vwidth;
4113
4114 /* Fill background with face under the image. Do it only if row is
4115 taller than image or if image has a clip mask to reduce
4116 flickering. */
4117 s->stippled_p = s->face->stipple != 0;
4118 if (height > s->img->height
4119 || s->img->hmargin
4120 || s->img->vmargin
4121 || s->img->mask
4122 || s->img->pixmap == 0
4123 || s->width != s->background_width)
4124 {
4125 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4126 x = s->x + box_line_hwidth;
4127 else
4128 x = s->x;
4129
4130 y = s->y + box_line_vwidth;
4131
4132 if (s->img->mask)
4133 {
4134 /* Create a pixmap as large as the glyph string. Fill it
4135 with the background color. Copy the image to it, using
4136 its mask. Copy the temporary pixmap to the display. */
4137 Screen *screen = FRAME_X_SCREEN (s->f);
4138 int depth = DefaultDepthOfScreen (screen);
4139
4140 /* Create a pixmap as large as the glyph string. */
4141 pixmap = XCreatePixmap (s->display, s->window,
4142 s->background_width,
4143 s->height, depth);
4144
4145 /* Don't clip in the following because we're working on the
4146 pixmap. */
4147 XSetClipMask (s->display, s->gc, None);
4148
4149 /* Fill the pixmap with the background color/stipple. */
4150 if (s->stippled_p)
4151 {
4152 /* Fill background with a stipple pattern. */
4153 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4154 XFillRectangle (s->display, pixmap, s->gc,
4155 0, 0, s->background_width, s->height);
4156 XSetFillStyle (s->display, s->gc, FillSolid);
4157 }
4158 else
4159 {
4160 XGCValues xgcv;
4161 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4162 &xgcv);
4163 XSetForeground (s->display, s->gc, xgcv.background);
4164 XFillRectangle (s->display, pixmap, s->gc,
4165 0, 0, s->background_width, s->height);
4166 XSetForeground (s->display, s->gc, xgcv.foreground);
4167 }
4168 }
4169 else
4170 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4171
4172 s->background_filled_p = 1;
4173 }
4174
4175 /* Draw the foreground. */
4176 if (pixmap != None)
4177 {
4178 x_draw_image_foreground_1 (s, pixmap);
4179 x_set_glyph_string_clipping (s);
4180 XCopyArea (s->display, pixmap, s->window, s->gc,
4181 0, 0, s->background_width, s->height, s->x, s->y);
4182 XFreePixmap (s->display, pixmap);
4183 }
4184 else
4185 x_draw_image_foreground (s);
4186
4187 /* If we must draw a relief around the image, do it. */
4188 if (s->img->relief
4189 || s->hl == DRAW_IMAGE_RAISED
4190 || s->hl == DRAW_IMAGE_SUNKEN)
4191 x_draw_image_relief (s);
4192 }
4193
4194
4195 /* Draw stretch glyph string S. */
4196
4197 static void
4198 x_draw_stretch_glyph_string (s)
4199 struct glyph_string *s;
4200 {
4201 xassert (s->first_glyph->type == STRETCH_GLYPH);
4202 s->stippled_p = s->face->stipple != 0;
4203
4204 if (s->hl == DRAW_CURSOR
4205 && !x_stretch_cursor_p)
4206 {
4207 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4208 as wide as the stretch glyph. */
4209 int width = min (CANON_X_UNIT (s->f), s->background_width);
4210
4211 /* Draw cursor. */
4212 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4213
4214 /* Clear rest using the GC of the original non-cursor face. */
4215 if (width < s->background_width)
4216 {
4217 GC gc = s->face->gc;
4218 int x = s->x + width, y = s->y;
4219 int w = s->background_width - width, h = s->height;
4220 XRectangle r;
4221
4222 x_get_glyph_string_clip_rect (s, &r);
4223 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4224
4225 if (s->face->stipple)
4226 {
4227 /* Fill background with a stipple pattern. */
4228 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4229 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4230 XSetFillStyle (s->display, gc, FillSolid);
4231 }
4232 else
4233 {
4234 XGCValues xgcv;
4235 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4236 XSetForeground (s->display, gc, xgcv.background);
4237 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4238 XSetForeground (s->display, gc, xgcv.foreground);
4239 }
4240 }
4241 }
4242 else
4243 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4244 s->height);
4245
4246 s->background_filled_p = 1;
4247 }
4248
4249
4250 /* Draw glyph string S. */
4251
4252 static void
4253 x_draw_glyph_string (s)
4254 struct glyph_string *s;
4255 {
4256 /* If S draws into the background of its successor, draw the
4257 background of the successor first so that S can draw into it.
4258 This makes S->next use XDrawString instead of XDrawImageString. */
4259 if (s->next && s->right_overhang && !s->for_overlaps_p)
4260 {
4261 xassert (s->next->img == NULL);
4262 x_set_glyph_string_gc (s->next);
4263 x_set_glyph_string_clipping (s->next);
4264 x_draw_glyph_string_background (s->next, 1);
4265 }
4266
4267 /* Set up S->gc, set clipping and draw S. */
4268 x_set_glyph_string_gc (s);
4269 x_set_glyph_string_clipping (s);
4270
4271 switch (s->first_glyph->type)
4272 {
4273 case IMAGE_GLYPH:
4274 x_draw_image_glyph_string (s);
4275 break;
4276
4277 case STRETCH_GLYPH:
4278 x_draw_stretch_glyph_string (s);
4279 break;
4280
4281 case CHAR_GLYPH:
4282 if (s->for_overlaps_p)
4283 s->background_filled_p = 1;
4284 else
4285 x_draw_glyph_string_background (s, 0);
4286 x_draw_glyph_string_foreground (s);
4287 break;
4288
4289 case COMPOSITE_GLYPH:
4290 if (s->for_overlaps_p || s->gidx > 0)
4291 s->background_filled_p = 1;
4292 else
4293 x_draw_glyph_string_background (s, 1);
4294 x_draw_composite_glyph_string_foreground (s);
4295 break;
4296
4297 default:
4298 abort ();
4299 }
4300
4301 if (!s->for_overlaps_p)
4302 {
4303 /* Draw underline. */
4304 if (s->face->underline_p)
4305 {
4306 unsigned long tem, h;
4307 int y;
4308
4309 /* Get the underline thickness. Default is 1 pixel. */
4310 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4311 h = 1;
4312
4313 /* Get the underline position. This is the recommended
4314 vertical offset in pixels from the baseline to the top of
4315 the underline. This is a signed value according to the
4316 specs, and its default is
4317
4318 ROUND ((maximum descent) / 2), with
4319 ROUND(x) = floor (x + 0.5) */
4320
4321 if (XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4322 y = s->ybase + (long) tem;
4323 else if (s->face->font)
4324 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4325 else
4326 y = s->height - h;
4327
4328 if (s->face->underline_defaulted_p)
4329 XFillRectangle (s->display, s->window, s->gc,
4330 s->x, y, s->width, h);
4331 else
4332 {
4333 XGCValues xgcv;
4334 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4335 XSetForeground (s->display, s->gc, s->face->underline_color);
4336 XFillRectangle (s->display, s->window, s->gc,
4337 s->x, y, s->width, h);
4338 XSetForeground (s->display, s->gc, xgcv.foreground);
4339 }
4340 }
4341
4342 /* Draw overline. */
4343 if (s->face->overline_p)
4344 {
4345 unsigned long dy = 0, h = 1;
4346
4347 if (s->face->overline_color_defaulted_p)
4348 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4349 s->width, h);
4350 else
4351 {
4352 XGCValues xgcv;
4353 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4354 XSetForeground (s->display, s->gc, s->face->overline_color);
4355 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4356 s->width, h);
4357 XSetForeground (s->display, s->gc, xgcv.foreground);
4358 }
4359 }
4360
4361 /* Draw strike-through. */
4362 if (s->face->strike_through_p)
4363 {
4364 unsigned long h = 1;
4365 unsigned long dy = (s->height - h) / 2;
4366
4367 if (s->face->strike_through_color_defaulted_p)
4368 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4369 s->width, h);
4370 else
4371 {
4372 XGCValues xgcv;
4373 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4374 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4375 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4376 s->width, h);
4377 XSetForeground (s->display, s->gc, xgcv.foreground);
4378 }
4379 }
4380
4381 /* Draw relief. */
4382 if (s->face->box != FACE_NO_BOX)
4383 x_draw_glyph_string_box (s);
4384 }
4385
4386 /* Reset clipping. */
4387 XSetClipMask (s->display, s->gc, None);
4388 }
4389
4390
4391 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4392 struct face **, int));
4393
4394
4395 /* Fill glyph string S with composition components specified by S->cmp.
4396
4397 FACES is an array of faces for all components of this composition.
4398 S->gidx is the index of the first component for S.
4399 OVERLAPS_P non-zero means S should draw the foreground only, and
4400 use its physical height for clipping.
4401
4402 Value is the index of a component not in S. */
4403
4404 static int
4405 x_fill_composite_glyph_string (s, faces, overlaps_p)
4406 struct glyph_string *s;
4407 struct face **faces;
4408 int overlaps_p;
4409 {
4410 int i;
4411
4412 xassert (s);
4413
4414 s->for_overlaps_p = overlaps_p;
4415
4416 s->face = faces[s->gidx];
4417 s->font = s->face->font;
4418 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4419
4420 /* For all glyphs of this composition, starting at the offset
4421 S->gidx, until we reach the end of the definition or encounter a
4422 glyph that requires the different face, add it to S. */
4423 ++s->nchars;
4424 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4425 ++s->nchars;
4426
4427 /* All glyph strings for the same composition has the same width,
4428 i.e. the width set for the first component of the composition. */
4429
4430 s->width = s->first_glyph->pixel_width;
4431
4432 /* If the specified font could not be loaded, use the frame's
4433 default font, but record the fact that we couldn't load it in
4434 the glyph string so that we can draw rectangles for the
4435 characters of the glyph string. */
4436 if (s->font == NULL)
4437 {
4438 s->font_not_found_p = 1;
4439 s->font = FRAME_FONT (s->f);
4440 }
4441
4442 /* Adjust base line for subscript/superscript text. */
4443 s->ybase += s->first_glyph->voffset;
4444
4445 xassert (s->face && s->face->gc);
4446
4447 /* This glyph string must always be drawn with 16-bit functions. */
4448 s->two_byte_p = 1;
4449
4450 return s->gidx + s->nchars;
4451 }
4452
4453
4454 /* Fill glyph string S from a sequence of character glyphs.
4455
4456 FACE_ID is the face id of the string. START is the index of the
4457 first glyph to consider, END is the index of the last + 1.
4458 OVERLAPS_P non-zero means S should draw the foreground only, and
4459 use its physical height for clipping.
4460
4461 Value is the index of the first glyph not in S. */
4462
4463 static int
4464 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4465 struct glyph_string *s;
4466 int face_id;
4467 int start, end, overlaps_p;
4468 {
4469 struct glyph *glyph, *last;
4470 int voffset;
4471 int glyph_not_available_p;
4472
4473 xassert (s->f == XFRAME (s->w->frame));
4474 xassert (s->nchars == 0);
4475 xassert (start >= 0 && end > start);
4476
4477 s->for_overlaps_p = overlaps_p,
4478 glyph = s->row->glyphs[s->area] + start;
4479 last = s->row->glyphs[s->area] + end;
4480 voffset = glyph->voffset;
4481
4482 glyph_not_available_p = glyph->glyph_not_available_p;
4483
4484 while (glyph < last
4485 && glyph->type == CHAR_GLYPH
4486 && glyph->voffset == voffset
4487 /* Same face id implies same font, nowadays. */
4488 && glyph->face_id == face_id
4489 && glyph->glyph_not_available_p == glyph_not_available_p)
4490 {
4491 int two_byte_p;
4492
4493 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4494 s->char2b + s->nchars,
4495 &two_byte_p);
4496 s->two_byte_p = two_byte_p;
4497 ++s->nchars;
4498 xassert (s->nchars <= end - start);
4499 s->width += glyph->pixel_width;
4500 ++glyph;
4501 }
4502
4503 s->font = s->face->font;
4504 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4505
4506 /* If the specified font could not be loaded, use the frame's font,
4507 but record the fact that we couldn't load it in
4508 S->font_not_found_p so that we can draw rectangles for the
4509 characters of the glyph string. */
4510 if (s->font == NULL || glyph_not_available_p)
4511 {
4512 s->font_not_found_p = 1;
4513 s->font = FRAME_FONT (s->f);
4514 }
4515
4516 /* Adjust base line for subscript/superscript text. */
4517 s->ybase += voffset;
4518
4519 xassert (s->face && s->face->gc);
4520 return glyph - s->row->glyphs[s->area];
4521 }
4522
4523
4524 /* Fill glyph string S from image glyph S->first_glyph. */
4525
4526 static void
4527 x_fill_image_glyph_string (s)
4528 struct glyph_string *s;
4529 {
4530 xassert (s->first_glyph->type == IMAGE_GLYPH);
4531 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4532 xassert (s->img);
4533 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4534 s->font = s->face->font;
4535 s->width = s->first_glyph->pixel_width;
4536
4537 /* Adjust base line for subscript/superscript text. */
4538 s->ybase += s->first_glyph->voffset;
4539 }
4540
4541
4542 /* Fill glyph string S from a sequence of stretch glyphs.
4543
4544 ROW is the glyph row in which the glyphs are found, AREA is the
4545 area within the row. START is the index of the first glyph to
4546 consider, END is the index of the last + 1.
4547
4548 Value is the index of the first glyph not in S. */
4549
4550 static int
4551 x_fill_stretch_glyph_string (s, row, area, start, end)
4552 struct glyph_string *s;
4553 struct glyph_row *row;
4554 enum glyph_row_area area;
4555 int start, end;
4556 {
4557 struct glyph *glyph, *last;
4558 int voffset, face_id;
4559
4560 xassert (s->first_glyph->type == STRETCH_GLYPH);
4561
4562 glyph = s->row->glyphs[s->area] + start;
4563 last = s->row->glyphs[s->area] + end;
4564 face_id = glyph->face_id;
4565 s->face = FACE_FROM_ID (s->f, face_id);
4566 s->font = s->face->font;
4567 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4568 s->width = glyph->pixel_width;
4569 voffset = glyph->voffset;
4570
4571 for (++glyph;
4572 (glyph < last
4573 && glyph->type == STRETCH_GLYPH
4574 && glyph->voffset == voffset
4575 && glyph->face_id == face_id);
4576 ++glyph)
4577 s->width += glyph->pixel_width;
4578
4579 /* Adjust base line for subscript/superscript text. */
4580 s->ybase += voffset;
4581
4582 xassert (s->face && s->face->gc);
4583 return glyph - s->row->glyphs[s->area];
4584 }
4585
4586
4587 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4588 of XChar2b structures for S; it can't be allocated in
4589 x_init_glyph_string because it must be allocated via `alloca'. W
4590 is the window on which S is drawn. ROW and AREA are the glyph row
4591 and area within the row from which S is constructed. START is the
4592 index of the first glyph structure covered by S. HL is a
4593 face-override for drawing S. */
4594
4595 static void
4596 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4597 struct glyph_string *s;
4598 XChar2b *char2b;
4599 struct window *w;
4600 struct glyph_row *row;
4601 enum glyph_row_area area;
4602 int start;
4603 enum draw_glyphs_face hl;
4604 {
4605 bzero (s, sizeof *s);
4606 s->w = w;
4607 s->f = XFRAME (w->frame);
4608 s->display = FRAME_X_DISPLAY (s->f);
4609 s->window = FRAME_X_WINDOW (s->f);
4610 s->char2b = char2b;
4611 s->hl = hl;
4612 s->row = row;
4613 s->area = area;
4614 s->first_glyph = row->glyphs[area] + start;
4615 s->height = row->height;
4616 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4617
4618 /* Display the internal border below the tool-bar window. */
4619 if (s->w == XWINDOW (s->f->tool_bar_window))
4620 s->y -= s->f->output_data.x->internal_border_width;
4621
4622 s->ybase = s->y + row->ascent;
4623 }
4624
4625
4626 /* Set background width of glyph string S. START is the index of the
4627 first glyph following S. LAST_X is the right-most x-position + 1
4628 in the drawing area. */
4629
4630 static INLINE void
4631 x_set_glyph_string_background_width (s, start, last_x)
4632 struct glyph_string *s;
4633 int start;
4634 int last_x;
4635 {
4636 /* If the face of this glyph string has to be drawn to the end of
4637 the drawing area, set S->extends_to_end_of_line_p. */
4638 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4639
4640 if (start == s->row->used[s->area]
4641 && s->hl == DRAW_NORMAL_TEXT
4642 && s->area == TEXT_AREA
4643 && (s->row->fill_line_p
4644 || s->face->background != default_face->background
4645 || s->face->stipple != default_face->stipple))
4646 s->extends_to_end_of_line_p = 1;
4647
4648 /* If S extends its face to the end of the line, set its
4649 background_width to the distance to the right edge of the drawing
4650 area. */
4651 if (s->extends_to_end_of_line_p)
4652 s->background_width = last_x - s->x + 1;
4653 else
4654 s->background_width = s->width;
4655 }
4656
4657
4658 /* Add a glyph string for a stretch glyph to the list of strings
4659 between HEAD and TAIL. START is the index of the stretch glyph in
4660 row area AREA of glyph row ROW. END is the index of the last glyph
4661 in that glyph row area. X is the current output position assigned
4662 to the new glyph string constructed. HL overrides that face of the
4663 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4664 is the right-most x-position of the drawing area. */
4665
4666 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4667 and below -- keep them on one line. */
4668 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4669 do \
4670 { \
4671 s = (struct glyph_string *) alloca (sizeof *s); \
4672 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4673 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4674 x_append_glyph_string (&HEAD, &TAIL, s); \
4675 s->x = (X); \
4676 } \
4677 while (0)
4678
4679
4680 /* Add a glyph string for an image glyph to the list of strings
4681 between HEAD and TAIL. START is the index of the image glyph in
4682 row area AREA of glyph row ROW. END is the index of the last glyph
4683 in that glyph row area. X is the current output position assigned
4684 to the new glyph string constructed. HL overrides that face of the
4685 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4686 is the right-most x-position of the drawing area. */
4687
4688 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4689 do \
4690 { \
4691 s = (struct glyph_string *) alloca (sizeof *s); \
4692 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4693 x_fill_image_glyph_string (s); \
4694 x_append_glyph_string (&HEAD, &TAIL, s); \
4695 ++START; \
4696 s->x = (X); \
4697 } \
4698 while (0)
4699
4700
4701 /* Add a glyph string for a sequence of character glyphs to the list
4702 of strings between HEAD and TAIL. START is the index of the first
4703 glyph in row area AREA of glyph row ROW that is part of the new
4704 glyph string. END is the index of the last glyph in that glyph row
4705 area. X is the current output position assigned to the new glyph
4706 string constructed. HL overrides that face of the glyph; e.g. it
4707 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4708 right-most x-position of the drawing area. */
4709
4710 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4711 do \
4712 { \
4713 int c, face_id; \
4714 XChar2b *char2b; \
4715 \
4716 c = (ROW)->glyphs[AREA][START].u.ch; \
4717 face_id = (ROW)->glyphs[AREA][START].face_id; \
4718 \
4719 s = (struct glyph_string *) alloca (sizeof *s); \
4720 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4721 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4722 x_append_glyph_string (&HEAD, &TAIL, s); \
4723 s->x = (X); \
4724 START = x_fill_glyph_string (s, face_id, START, END, \
4725 OVERLAPS_P); \
4726 } \
4727 while (0)
4728
4729
4730 /* Add a glyph string for a composite sequence to the list of strings
4731 between HEAD and TAIL. START is the index of the first glyph in
4732 row area AREA of glyph row ROW that is part of the new glyph
4733 string. END is the index of the last glyph in that glyph row area.
4734 X is the current output position assigned to the new glyph string
4735 constructed. HL overrides that face of the glyph; e.g. it is
4736 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4737 x-position of the drawing area. */
4738
4739 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4740 do { \
4741 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4742 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4743 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4744 struct composition *cmp = composition_table[cmp_id]; \
4745 int glyph_len = cmp->glyph_len; \
4746 XChar2b *char2b; \
4747 struct face **faces; \
4748 struct glyph_string *first_s = NULL; \
4749 int n; \
4750 \
4751 base_face = base_face->ascii_face; \
4752 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4753 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4754 /* At first, fill in `char2b' and `faces'. */ \
4755 for (n = 0; n < glyph_len; n++) \
4756 { \
4757 int c = COMPOSITION_GLYPH (cmp, n); \
4758 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4759 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4760 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4761 this_face_id, char2b + n, 1); \
4762 } \
4763 \
4764 /* Make glyph_strings for each glyph sequence that is drawable by \
4765 the same face, and append them to HEAD/TAIL. */ \
4766 for (n = 0; n < cmp->glyph_len;) \
4767 { \
4768 s = (struct glyph_string *) alloca (sizeof *s); \
4769 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4770 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4771 s->cmp = cmp; \
4772 s->gidx = n; \
4773 s->x = (X); \
4774 \
4775 if (n == 0) \
4776 first_s = s; \
4777 \
4778 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4779 } \
4780 \
4781 ++START; \
4782 s = first_s; \
4783 } while (0)
4784
4785
4786 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4787 of AREA of glyph row ROW on window W between indices START and END.
4788 HL overrides the face for drawing glyph strings, e.g. it is
4789 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4790 x-positions of the drawing area.
4791
4792 This is an ugly monster macro construct because we must use alloca
4793 to allocate glyph strings (because x_draw_glyphs can be called
4794 asynchronously). */
4795
4796 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4797 do \
4798 { \
4799 HEAD = TAIL = NULL; \
4800 while (START < END) \
4801 { \
4802 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4803 switch (first_glyph->type) \
4804 { \
4805 case CHAR_GLYPH: \
4806 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
4807 TAIL, HL, X, LAST_X, \
4808 OVERLAPS_P); \
4809 break; \
4810 \
4811 case COMPOSITE_GLYPH: \
4812 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4813 HEAD, TAIL, HL, X, LAST_X,\
4814 OVERLAPS_P); \
4815 break; \
4816 \
4817 case STRETCH_GLYPH: \
4818 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4819 HEAD, TAIL, HL, X, LAST_X); \
4820 break; \
4821 \
4822 case IMAGE_GLYPH: \
4823 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4824 TAIL, HL, X, LAST_X); \
4825 break; \
4826 \
4827 default: \
4828 abort (); \
4829 } \
4830 \
4831 x_set_glyph_string_background_width (s, START, LAST_X); \
4832 (X) += s->width; \
4833 } \
4834 } \
4835 while (0)
4836
4837
4838 /* Draw glyphs between START and END in AREA of ROW on window W,
4839 starting at x-position X. X is relative to AREA in W. HL is a
4840 face-override with the following meaning:
4841
4842 DRAW_NORMAL_TEXT draw normally
4843 DRAW_CURSOR draw in cursor face
4844 DRAW_MOUSE_FACE draw in mouse face.
4845 DRAW_INVERSE_VIDEO draw in mode line face
4846 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4847 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4848
4849 If REAL_START is non-null, return in *REAL_START the real starting
4850 position for display. This can be different from START in case
4851 overlapping glyphs must be displayed. If REAL_END is non-null,
4852 return in *REAL_END the real end position for display. This can be
4853 different from END in case overlapping glyphs must be displayed.
4854
4855 If OVERLAPS_P is non-zero, draw only the foreground of characters
4856 and clip to the physical height of ROW.
4857
4858 Value is the x-position reached, relative to AREA of W. */
4859
4860 static int
4861 x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4862 overlaps_p)
4863 struct window *w;
4864 int x;
4865 struct glyph_row *row;
4866 enum glyph_row_area area;
4867 int start, end;
4868 enum draw_glyphs_face hl;
4869 int *real_start, *real_end;
4870 int overlaps_p;
4871 {
4872 struct glyph_string *head, *tail;
4873 struct glyph_string *s;
4874 int last_x, area_width;
4875 int x_reached;
4876 int i, j;
4877
4878 /* Let's rather be paranoid than getting a SEGV. */
4879 end = min (end, row->used[area]);
4880 start = max (0, start);
4881 start = min (end, start);
4882 if (real_start)
4883 *real_start = start;
4884 if (real_end)
4885 *real_end = end;
4886
4887 /* Translate X to frame coordinates. Set last_x to the right
4888 end of the drawing area. */
4889 if (row->full_width_p)
4890 {
4891 /* X is relative to the left edge of W, without scroll bars
4892 or flag areas. */
4893 struct frame *f = XFRAME (w->frame);
4894 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
4895 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
4896
4897 x += window_left_x;
4898 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4899 last_x = window_left_x + area_width;
4900
4901 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4902 {
4903 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4904 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4905 last_x += width;
4906 else
4907 x -= width;
4908 }
4909
4910 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4911 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
4912 }
4913 else
4914 {
4915 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4916 area_width = window_box_width (w, area);
4917 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
4918 }
4919
4920 /* Build a doubly-linked list of glyph_string structures between
4921 head and tail from what we have to draw. Note that the macro
4922 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4923 the reason we use a separate variable `i'. */
4924 i = start;
4925 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4926 overlaps_p);
4927 if (tail)
4928 x_reached = tail->x + tail->background_width;
4929 else
4930 x_reached = x;
4931
4932 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4933 the row, redraw some glyphs in front or following the glyph
4934 strings built above. */
4935 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
4936 {
4937 int dummy_x = 0;
4938 struct glyph_string *h, *t;
4939
4940 /* Compute overhangs for all glyph strings. */
4941 for (s = head; s; s = s->next)
4942 x_compute_glyph_string_overhangs (s);
4943
4944 /* Prepend glyph strings for glyphs in front of the first glyph
4945 string that are overwritten because of the first glyph
4946 string's left overhang. The background of all strings
4947 prepended must be drawn because the first glyph string
4948 draws over it. */
4949 i = x_left_overwritten (head);
4950 if (i >= 0)
4951 {
4952 j = i;
4953 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
4954 DRAW_NORMAL_TEXT, dummy_x, last_x,
4955 overlaps_p);
4956 start = i;
4957 if (real_start)
4958 *real_start = start;
4959 x_compute_overhangs_and_x (t, head->x, 1);
4960 x_prepend_glyph_string_lists (&head, &tail, h, t);
4961 }
4962
4963 /* Prepend glyph strings for glyphs in front of the first glyph
4964 string that overwrite that glyph string because of their
4965 right overhang. For these strings, only the foreground must
4966 be drawn, because it draws over the glyph string at `head'.
4967 The background must not be drawn because this would overwrite
4968 right overhangs of preceding glyphs for which no glyph
4969 strings exist. */
4970 i = x_left_overwriting (head);
4971 if (i >= 0)
4972 {
4973 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
4974 DRAW_NORMAL_TEXT, dummy_x, last_x,
4975 overlaps_p);
4976 for (s = h; s; s = s->next)
4977 s->background_filled_p = 1;
4978 if (real_start)
4979 *real_start = i;
4980 x_compute_overhangs_and_x (t, head->x, 1);
4981 x_prepend_glyph_string_lists (&head, &tail, h, t);
4982 }
4983
4984 /* Append glyphs strings for glyphs following the last glyph
4985 string tail that are overwritten by tail. The background of
4986 these strings has to be drawn because tail's foreground draws
4987 over it. */
4988 i = x_right_overwritten (tail);
4989 if (i >= 0)
4990 {
4991 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4992 DRAW_NORMAL_TEXT, x, last_x,
4993 overlaps_p);
4994 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4995 x_append_glyph_string_lists (&head, &tail, h, t);
4996 if (real_end)
4997 *real_end = i;
4998 }
4999
5000 /* Append glyph strings for glyphs following the last glyph
5001 string tail that overwrite tail. The foreground of such
5002 glyphs has to be drawn because it writes into the background
5003 of tail. The background must not be drawn because it could
5004 paint over the foreground of following glyphs. */
5005 i = x_right_overwriting (tail);
5006 if (i >= 0)
5007 {
5008 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5009 DRAW_NORMAL_TEXT, x, last_x,
5010 overlaps_p);
5011 for (s = h; s; s = s->next)
5012 s->background_filled_p = 1;
5013 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5014 x_append_glyph_string_lists (&head, &tail, h, t);
5015 if (real_end)
5016 *real_end = i;
5017 }
5018 }
5019
5020 /* Draw all strings. */
5021 for (s = head; s; s = s->next)
5022 x_draw_glyph_string (s);
5023
5024 /* Value is the x-position up to which drawn, relative to AREA of W.
5025 This doesn't include parts drawn because of overhangs. */
5026 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5027 if (!row->full_width_p)
5028 {
5029 if (area > LEFT_MARGIN_AREA)
5030 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5031 if (area > TEXT_AREA)
5032 x_reached -= window_box_width (w, TEXT_AREA);
5033 }
5034
5035 return x_reached;
5036 }
5037
5038
5039 /* Fix the display of area AREA of overlapping row ROW in window W. */
5040
5041 static void
5042 x_fix_overlapping_area (w, row, area)
5043 struct window *w;
5044 struct glyph_row *row;
5045 enum glyph_row_area area;
5046 {
5047 int i, x;
5048
5049 BLOCK_INPUT;
5050
5051 if (area == LEFT_MARGIN_AREA)
5052 x = 0;
5053 else if (area == TEXT_AREA)
5054 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5055 else
5056 x = (window_box_width (w, LEFT_MARGIN_AREA)
5057 + window_box_width (w, TEXT_AREA));
5058
5059 for (i = 0; i < row->used[area];)
5060 {
5061 if (row->glyphs[area][i].overlaps_vertically_p)
5062 {
5063 int start = i, start_x = x;
5064
5065 do
5066 {
5067 x += row->glyphs[area][i].pixel_width;
5068 ++i;
5069 }
5070 while (i < row->used[area]
5071 && row->glyphs[area][i].overlaps_vertically_p);
5072
5073 x_draw_glyphs (w, start_x, row, area, start, i,
5074 (row->inverse_p
5075 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5076 NULL, NULL, 1);
5077 }
5078 else
5079 {
5080 x += row->glyphs[area][i].pixel_width;
5081 ++i;
5082 }
5083 }
5084
5085 UNBLOCK_INPUT;
5086 }
5087
5088
5089 /* Output LEN glyphs starting at START at the nominal cursor position.
5090 Advance the nominal cursor over the text. The global variable
5091 updated_window contains the window being updated, updated_row is
5092 the glyph row being updated, and updated_area is the area of that
5093 row being updated. */
5094
5095 static void
5096 x_write_glyphs (start, len)
5097 struct glyph *start;
5098 int len;
5099 {
5100 int x, hpos, real_start, real_end;
5101
5102 xassert (updated_window && updated_row);
5103 BLOCK_INPUT;
5104
5105 /* Write glyphs. */
5106
5107 hpos = start - updated_row->glyphs[updated_area];
5108 x = x_draw_glyphs (updated_window, output_cursor.x,
5109 updated_row, updated_area,
5110 hpos, hpos + len,
5111 (updated_row->inverse_p
5112 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5113 &real_start, &real_end, 0);
5114
5115 /* If we drew over the cursor, note that it is not visible any more. */
5116 note_overwritten_text_cursor (updated_window, real_start,
5117 real_end - real_start);
5118
5119 UNBLOCK_INPUT;
5120
5121 /* Advance the output cursor. */
5122 output_cursor.hpos += len;
5123 output_cursor.x = x;
5124 }
5125
5126
5127 /* Insert LEN glyphs from START at the nominal cursor position. */
5128
5129 static void
5130 x_insert_glyphs (start, len)
5131 struct glyph *start;
5132 register int len;
5133 {
5134 struct frame *f;
5135 struct window *w;
5136 int line_height, shift_by_width, shifted_region_width;
5137 struct glyph_row *row;
5138 struct glyph *glyph;
5139 int frame_x, frame_y, hpos, real_start, real_end;
5140
5141 xassert (updated_window && updated_row);
5142 BLOCK_INPUT;
5143 w = updated_window;
5144 f = XFRAME (WINDOW_FRAME (w));
5145
5146 /* Get the height of the line we are in. */
5147 row = updated_row;
5148 line_height = row->height;
5149
5150 /* Get the width of the glyphs to insert. */
5151 shift_by_width = 0;
5152 for (glyph = start; glyph < start + len; ++glyph)
5153 shift_by_width += glyph->pixel_width;
5154
5155 /* Get the width of the region to shift right. */
5156 shifted_region_width = (window_box_width (w, updated_area)
5157 - output_cursor.x
5158 - shift_by_width);
5159
5160 /* Shift right. */
5161 frame_x = window_box_left (w, updated_area) + output_cursor.x;
5162 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5163 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5164 f->output_data.x->normal_gc,
5165 frame_x, frame_y,
5166 shifted_region_width, line_height,
5167 frame_x + shift_by_width, frame_y);
5168
5169 /* Write the glyphs. */
5170 hpos = start - row->glyphs[updated_area];
5171 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5172 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
5173 note_overwritten_text_cursor (w, real_start, real_end - real_start);
5174
5175 /* Advance the output cursor. */
5176 output_cursor.hpos += len;
5177 output_cursor.x += shift_by_width;
5178 UNBLOCK_INPUT;
5179 }
5180
5181
5182 /* Delete N glyphs at the nominal cursor position. Not implemented
5183 for X frames. */
5184
5185 static void
5186 x_delete_glyphs (n)
5187 register int n;
5188 {
5189 abort ();
5190 }
5191
5192
5193 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5194 If they are <= 0, this is probably an error. */
5195
5196 void
5197 x_clear_area (dpy, window, x, y, width, height, exposures)
5198 Display *dpy;
5199 Window window;
5200 int x, y;
5201 int width, height;
5202 int exposures;
5203 {
5204 xassert (width > 0 && height > 0);
5205 XClearArea (dpy, window, x, y, width, height, exposures);
5206 }
5207
5208
5209 /* Erase the current text line from the nominal cursor position
5210 (inclusive) to pixel column TO_X (exclusive). The idea is that
5211 everything from TO_X onward is already erased.
5212
5213 TO_X is a pixel position relative to updated_area of
5214 updated_window. TO_X == -1 means clear to the end of this area. */
5215
5216 static void
5217 x_clear_end_of_line (to_x)
5218 int to_x;
5219 {
5220 struct frame *f;
5221 struct window *w = updated_window;
5222 int max_x, min_y, max_y;
5223 int from_x, from_y, to_y;
5224
5225 xassert (updated_window && updated_row);
5226 f = XFRAME (w->frame);
5227
5228 if (updated_row->full_width_p)
5229 {
5230 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5231 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5232 && !w->pseudo_window_p)
5233 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5234 }
5235 else
5236 max_x = window_box_width (w, updated_area);
5237 max_y = window_text_bottom_y (w);
5238
5239 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5240 of window. For TO_X > 0, truncate to end of drawing area. */
5241 if (to_x == 0)
5242 return;
5243 else if (to_x < 0)
5244 to_x = max_x;
5245 else
5246 to_x = min (to_x, max_x);
5247
5248 to_y = min (max_y, output_cursor.y + updated_row->height);
5249
5250 /* Notice if the cursor will be cleared by this operation. */
5251 if (!updated_row->full_width_p)
5252 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
5253
5254 from_x = output_cursor.x;
5255
5256 /* Translate to frame coordinates. */
5257 if (updated_row->full_width_p)
5258 {
5259 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5260 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5261 }
5262 else
5263 {
5264 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5265 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5266 }
5267
5268 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5269 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5270 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5271
5272 /* Prevent inadvertently clearing to end of the X window. */
5273 if (to_x > from_x && to_y > from_y)
5274 {
5275 BLOCK_INPUT;
5276 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5277 from_x, from_y, to_x - from_x, to_y - from_y,
5278 False);
5279 UNBLOCK_INPUT;
5280 }
5281 }
5282
5283
5284 /* Clear entire frame. If updating_frame is non-null, clear that
5285 frame. Otherwise clear the selected frame. */
5286
5287 static void
5288 x_clear_frame ()
5289 {
5290 struct frame *f;
5291
5292 if (updating_frame)
5293 f = updating_frame;
5294 else
5295 f = SELECTED_FRAME ();
5296
5297 /* Clearing the frame will erase any cursor, so mark them all as no
5298 longer visible. */
5299 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5300 output_cursor.hpos = output_cursor.vpos = 0;
5301 output_cursor.x = -1;
5302
5303 /* We don't set the output cursor here because there will always
5304 follow an explicit cursor_to. */
5305 BLOCK_INPUT;
5306 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5307
5308 /* We have to clear the scroll bars, too. If we have changed
5309 colors or something like that, then they should be notified. */
5310 x_scroll_bar_clear (f);
5311
5312 XFlush (FRAME_X_DISPLAY (f));
5313 UNBLOCK_INPUT;
5314 }
5315
5316
5317 \f
5318 /* Invert the middle quarter of the frame for .15 sec. */
5319
5320 /* We use the select system call to do the waiting, so we have to make
5321 sure it's available. If it isn't, we just won't do visual bells. */
5322
5323 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5324
5325
5326 /* Subtract the `struct timeval' values X and Y, storing the result in
5327 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5328
5329 static int
5330 timeval_subtract (result, x, y)
5331 struct timeval *result, x, y;
5332 {
5333 /* Perform the carry for the later subtraction by updating y. This
5334 is safer because on some systems the tv_sec member is unsigned. */
5335 if (x.tv_usec < y.tv_usec)
5336 {
5337 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5338 y.tv_usec -= 1000000 * nsec;
5339 y.tv_sec += nsec;
5340 }
5341
5342 if (x.tv_usec - y.tv_usec > 1000000)
5343 {
5344 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5345 y.tv_usec += 1000000 * nsec;
5346 y.tv_sec -= nsec;
5347 }
5348
5349 /* Compute the time remaining to wait. tv_usec is certainly
5350 positive. */
5351 result->tv_sec = x.tv_sec - y.tv_sec;
5352 result->tv_usec = x.tv_usec - y.tv_usec;
5353
5354 /* Return indication of whether the result should be considered
5355 negative. */
5356 return x.tv_sec < y.tv_sec;
5357 }
5358
5359 void
5360 XTflash (f)
5361 struct frame *f;
5362 {
5363 BLOCK_INPUT;
5364
5365 {
5366 GC gc;
5367
5368 /* Create a GC that will use the GXxor function to flip foreground
5369 pixels into background pixels. */
5370 {
5371 XGCValues values;
5372
5373 values.function = GXxor;
5374 values.foreground = (f->output_data.x->foreground_pixel
5375 ^ f->output_data.x->background_pixel);
5376
5377 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5378 GCFunction | GCForeground, &values);
5379 }
5380
5381 {
5382 /* Get the height not including a menu bar widget. */
5383 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5384 /* Height of each line to flash. */
5385 int flash_height = FRAME_LINE_HEIGHT (f);
5386 /* These will be the left and right margins of the rectangles. */
5387 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5388 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5389
5390 int width;
5391
5392 /* Don't flash the area between a scroll bar and the frame
5393 edge it is next to. */
5394 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5395 {
5396 case vertical_scroll_bar_left:
5397 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5398 break;
5399
5400 case vertical_scroll_bar_right:
5401 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5402 break;
5403
5404 default:
5405 break;
5406 }
5407
5408 width = flash_right - flash_left;
5409
5410 /* If window is tall, flash top and bottom line. */
5411 if (height > 3 * FRAME_LINE_HEIGHT (f))
5412 {
5413 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5414 flash_left,
5415 (FRAME_INTERNAL_BORDER_WIDTH (f)
5416 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5417 width, flash_height);
5418 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5419 flash_left,
5420 (height - flash_height
5421 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5422 width, flash_height);
5423 }
5424 else
5425 /* If it is short, flash it all. */
5426 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5427 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5428 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5429
5430 x_flush (f);
5431
5432 {
5433 struct timeval wakeup;
5434
5435 EMACS_GET_TIME (wakeup);
5436
5437 /* Compute time to wait until, propagating carry from usecs. */
5438 wakeup.tv_usec += 150000;
5439 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5440 wakeup.tv_usec %= 1000000;
5441
5442 /* Keep waiting until past the time wakeup or any input gets
5443 available. */
5444 while (! detect_input_pending ())
5445 {
5446 struct timeval current;
5447 struct timeval timeout;
5448
5449 EMACS_GET_TIME (current);
5450
5451 /* Break if result would be negative. */
5452 if (timeval_subtract (&current, wakeup, current))
5453 break;
5454
5455 /* How long `select' should wait. */
5456 timeout.tv_sec = 0;
5457 timeout.tv_usec = 10000;
5458
5459 /* Try to wait that long--but we might wake up sooner. */
5460 select (0, NULL, NULL, NULL, &timeout);
5461 }
5462 }
5463
5464 /* If window is tall, flash top and bottom line. */
5465 if (height > 3 * FRAME_LINE_HEIGHT (f))
5466 {
5467 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5468 flash_left,
5469 (FRAME_INTERNAL_BORDER_WIDTH (f)
5470 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5471 width, flash_height);
5472 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5473 flash_left,
5474 (height - flash_height
5475 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5476 width, flash_height);
5477 }
5478 else
5479 /* If it is short, flash it all. */
5480 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5481 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5482 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5483
5484 XFreeGC (FRAME_X_DISPLAY (f), gc);
5485 x_flush (f);
5486 }
5487 }
5488
5489 UNBLOCK_INPUT;
5490 }
5491
5492 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5493
5494
5495 /* Make audible bell. */
5496
5497 void
5498 XTring_bell ()
5499 {
5500 struct frame *f = SELECTED_FRAME ();
5501
5502 if (FRAME_X_DISPLAY (f))
5503 {
5504 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5505 if (visible_bell)
5506 XTflash (f);
5507 else
5508 #endif
5509 {
5510 BLOCK_INPUT;
5511 XBell (FRAME_X_DISPLAY (f), 0);
5512 XFlush (FRAME_X_DISPLAY (f));
5513 UNBLOCK_INPUT;
5514 }
5515 }
5516 }
5517
5518 \f
5519 /* Specify how many text lines, from the top of the window,
5520 should be affected by insert-lines and delete-lines operations.
5521 This, and those operations, are used only within an update
5522 that is bounded by calls to x_update_begin and x_update_end. */
5523
5524 static void
5525 XTset_terminal_window (n)
5526 register int n;
5527 {
5528 /* This function intentionally left blank. */
5529 }
5530
5531
5532 \f
5533 /***********************************************************************
5534 Line Dance
5535 ***********************************************************************/
5536
5537 /* Perform an insert-lines or delete-lines operation, inserting N
5538 lines or deleting -N lines at vertical position VPOS. */
5539
5540 static void
5541 x_ins_del_lines (vpos, n)
5542 int vpos, n;
5543 {
5544 abort ();
5545 }
5546
5547
5548 /* Scroll part of the display as described by RUN. */
5549
5550 static void
5551 x_scroll_run (w, run)
5552 struct window *w;
5553 struct run *run;
5554 {
5555 struct frame *f = XFRAME (w->frame);
5556 int x, y, width, height, from_y, to_y, bottom_y;
5557
5558 /* Get frame-relative bounding box of the text display area of W,
5559 without mode lines. Include in this box the flags areas to the
5560 left and right of W. */
5561 window_box (w, -1, &x, &y, &width, &height);
5562 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5563 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
5564
5565 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5566 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5567 bottom_y = y + height;
5568
5569 if (to_y < from_y)
5570 {
5571 /* Scrolling up. Make sure we don't copy part of the mode
5572 line at the bottom. */
5573 if (from_y + run->height > bottom_y)
5574 height = bottom_y - from_y;
5575 else
5576 height = run->height;
5577 }
5578 else
5579 {
5580 /* Scolling down. Make sure we don't copy over the mode line.
5581 at the bottom. */
5582 if (to_y + run->height > bottom_y)
5583 height = bottom_y - to_y;
5584 else
5585 height = run->height;
5586 }
5587
5588 BLOCK_INPUT;
5589
5590 /* Cursor off. Will be switched on again in x_update_window_end. */
5591 updated_window = w;
5592 x_clear_cursor (w);
5593
5594 XCopyArea (FRAME_X_DISPLAY (f),
5595 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5596 f->output_data.x->normal_gc,
5597 x, from_y,
5598 width, height,
5599 x, to_y);
5600
5601 UNBLOCK_INPUT;
5602 }
5603
5604
5605 \f
5606 /***********************************************************************
5607 Exposure Events
5608 ***********************************************************************/
5609
5610 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5611 corner of the exposed rectangle. W and H are width and height of
5612 the exposed area. All are pixel values. W or H zero means redraw
5613 the entire frame. */
5614
5615 static void
5616 expose_frame (f, x, y, w, h)
5617 struct frame *f;
5618 int x, y, w, h;
5619 {
5620 XRectangle r;
5621
5622 TRACE ((stderr, "expose_frame "));
5623
5624 /* No need to redraw if frame will be redrawn soon. */
5625 if (FRAME_GARBAGED_P (f))
5626 {
5627 TRACE ((stderr, " garbaged\n"));
5628 return;
5629 }
5630
5631 /* If basic faces haven't been realized yet, there is no point in
5632 trying to redraw anything. This can happen when we get an expose
5633 event while Emacs is starting, e.g. by moving another window. */
5634 if (FRAME_FACE_CACHE (f) == NULL
5635 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5636 {
5637 TRACE ((stderr, " no faces\n"));
5638 return;
5639 }
5640
5641 if (w == 0 || h == 0)
5642 {
5643 r.x = r.y = 0;
5644 r.width = CANON_X_UNIT (f) * f->width;
5645 r.height = CANON_Y_UNIT (f) * f->height;
5646 }
5647 else
5648 {
5649 r.x = x;
5650 r.y = y;
5651 r.width = w;
5652 r.height = h;
5653 }
5654
5655 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5656 expose_window_tree (XWINDOW (f->root_window), &r);
5657
5658 if (WINDOWP (f->tool_bar_window))
5659 {
5660 struct window *w = XWINDOW (f->tool_bar_window);
5661 XRectangle window_rect;
5662 XRectangle intersection_rect;
5663 int window_x, window_y, window_width, window_height;
5664
5665
5666 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5667 window_rect.x = window_x;
5668 window_rect.y = window_y;
5669 window_rect.width = window_width;
5670 window_rect.height = window_height;
5671
5672 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5673 expose_window (w, &intersection_rect);
5674 }
5675
5676 #ifndef USE_X_TOOLKIT
5677 if (WINDOWP (f->menu_bar_window))
5678 {
5679 struct window *w = XWINDOW (f->menu_bar_window);
5680 XRectangle window_rect;
5681 XRectangle intersection_rect;
5682 int window_x, window_y, window_width, window_height;
5683
5684
5685 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5686 window_rect.x = window_x;
5687 window_rect.y = window_y;
5688 window_rect.width = window_width;
5689 window_rect.height = window_height;
5690
5691 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5692 expose_window (w, &intersection_rect);
5693 }
5694 #endif /* not USE_X_TOOLKIT */
5695 }
5696
5697
5698 /* Redraw (parts) of all windows in the window tree rooted at W that
5699 intersect R. R contains frame pixel coordinates. */
5700
5701 static void
5702 expose_window_tree (w, r)
5703 struct window *w;
5704 XRectangle *r;
5705 {
5706 while (w)
5707 {
5708 if (!NILP (w->hchild))
5709 expose_window_tree (XWINDOW (w->hchild), r);
5710 else if (!NILP (w->vchild))
5711 expose_window_tree (XWINDOW (w->vchild), r);
5712 else
5713 {
5714 XRectangle window_rect;
5715 XRectangle intersection_rect;
5716 struct frame *f = XFRAME (w->frame);
5717 int window_x, window_y, window_width, window_height;
5718
5719 /* Frame-relative pixel rectangle of W. */
5720 window_box (w, -1, &window_x, &window_y, &window_width,
5721 &window_height);
5722 window_rect.x
5723 = (window_x
5724 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
5725 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5726 window_rect.y = window_y;
5727 window_rect.width
5728 = (window_width
5729 + FRAME_X_FLAGS_AREA_WIDTH (f)
5730 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5731 window_rect.height
5732 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5733
5734 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5735 expose_window (w, &intersection_rect);
5736 }
5737
5738 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5739 }
5740 }
5741
5742
5743 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5744 which intersects rectangle R. R is in window-relative coordinates. */
5745
5746 static void
5747 expose_area (w, row, r, area)
5748 struct window *w;
5749 struct glyph_row *row;
5750 XRectangle *r;
5751 enum glyph_row_area area;
5752 {
5753 struct glyph *first = row->glyphs[area];
5754 struct glyph *end = row->glyphs[area] + row->used[area];
5755 struct glyph *last;
5756 int first_x, start_x, x;
5757
5758 if (area == TEXT_AREA && row->fill_line_p)
5759 /* If row extends face to end of line write the whole line. */
5760 x_draw_glyphs (w, 0, row, area,
5761 0, row->used[area],
5762 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5763 NULL, NULL, 0);
5764 else
5765 {
5766 /* Set START_X to the window-relative start position for drawing glyphs of
5767 AREA. The first glyph of the text area can be partially visible.
5768 The first glyphs of other areas cannot. */
5769 if (area == LEFT_MARGIN_AREA)
5770 start_x = 0;
5771 else if (area == TEXT_AREA)
5772 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5773 else
5774 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5775 + window_box_width (w, TEXT_AREA));
5776 x = start_x;
5777
5778 /* Find the first glyph that must be redrawn. */
5779 while (first < end
5780 && x + first->pixel_width < r->x)
5781 {
5782 x += first->pixel_width;
5783 ++first;
5784 }
5785
5786 /* Find the last one. */
5787 last = first;
5788 first_x = x;
5789 while (last < end
5790 && x < r->x + r->width)
5791 {
5792 x += last->pixel_width;
5793 ++last;
5794 }
5795
5796 /* Repaint. */
5797 if (last > first)
5798 x_draw_glyphs (w, first_x - start_x, row, area,
5799 first - row->glyphs[area],
5800 last - row->glyphs[area],
5801 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5802 NULL, NULL, 0);
5803 }
5804 }
5805
5806
5807 /* Redraw the parts of the glyph row ROW on window W intersecting
5808 rectangle R. R is in window-relative coordinates. */
5809
5810 static void
5811 expose_line (w, row, r)
5812 struct window *w;
5813 struct glyph_row *row;
5814 XRectangle *r;
5815 {
5816 xassert (row->enabled_p);
5817
5818 if (row->mode_line_p || w->pseudo_window_p)
5819 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5820 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5821 NULL, NULL, 0);
5822 else
5823 {
5824 if (row->used[LEFT_MARGIN_AREA])
5825 expose_area (w, row, r, LEFT_MARGIN_AREA);
5826 if (row->used[TEXT_AREA])
5827 expose_area (w, row, r, TEXT_AREA);
5828 if (row->used[RIGHT_MARGIN_AREA])
5829 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5830 x_draw_row_bitmaps (w, row);
5831 }
5832 }
5833
5834
5835 /* Return non-zero if W's cursor intersects rectangle R. */
5836
5837 static int
5838 x_phys_cursor_in_rect_p (w, r)
5839 struct window *w;
5840 XRectangle *r;
5841 {
5842 XRectangle cr, result;
5843 struct glyph *cursor_glyph;
5844
5845 cursor_glyph = get_phys_cursor_glyph (w);
5846 if (cursor_glyph)
5847 {
5848 cr.x = w->phys_cursor.x;
5849 cr.y = w->phys_cursor.y;
5850 cr.width = cursor_glyph->pixel_width;
5851 cr.height = w->phys_cursor_height;
5852 return x_intersect_rectangles (&cr, r, &result);
5853 }
5854 else
5855 return 0;
5856 }
5857
5858
5859 /* Redraw a rectangle of window W. R is a rectangle in window
5860 relative coordinates. Call this function with input blocked. */
5861
5862 static void
5863 expose_window (w, r)
5864 struct window *w;
5865 XRectangle *r;
5866 {
5867 struct glyph_row *row;
5868 int y;
5869 int yb = window_text_bottom_y (w);
5870 int cursor_cleared_p;
5871
5872 /* If window is not yet fully initialized, do nothing. This can
5873 happen when toolkit scroll bars are used and a window is split.
5874 Reconfiguring the scroll bar will generate an expose for a newly
5875 created window. */
5876 if (w->current_matrix == NULL || w == updated_window)
5877 return;
5878
5879 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5880 r->x, r->y, r->width, r->height));
5881
5882 /* Convert to window coordinates. */
5883 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5884 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
5885
5886 /* Turn off the cursor. */
5887 if (!w->pseudo_window_p
5888 && x_phys_cursor_in_rect_p (w, r))
5889 {
5890 x_clear_cursor (w);
5891 cursor_cleared_p = 1;
5892 }
5893 else
5894 cursor_cleared_p = 0;
5895
5896 /* Find the first row intersecting the rectangle R. */
5897 row = w->current_matrix->rows;
5898 y = 0;
5899 while (row->enabled_p
5900 && y < yb
5901 && y + row->height < r->y)
5902 {
5903 y += row->height;
5904 ++row;
5905 }
5906
5907 /* Display the text in the rectangle, one text line at a time. */
5908 while (row->enabled_p
5909 && y < yb
5910 && y < r->y + r->height)
5911 {
5912 expose_line (w, row, r);
5913 y += row->height;
5914 ++row;
5915 }
5916
5917 /* Display the mode line if there is one. */
5918 if (WINDOW_WANTS_MODELINE_P (w)
5919 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5920 row->enabled_p)
5921 && row->y < r->y + r->height)
5922 expose_line (w, row, r);
5923
5924 if (!w->pseudo_window_p)
5925 {
5926 /* Draw border between windows. */
5927 x_draw_vertical_border (w);
5928
5929 /* Turn the cursor on again. */
5930 if (cursor_cleared_p)
5931 x_update_window_cursor (w, 1);
5932 }
5933 }
5934
5935
5936 /* Determine the intersection of two rectangles R1 and R2. Return
5937 the intersection in *RESULT. Value is non-zero if RESULT is not
5938 empty. */
5939
5940 static int
5941 x_intersect_rectangles (r1, r2, result)
5942 XRectangle *r1, *r2, *result;
5943 {
5944 XRectangle *left, *right;
5945 XRectangle *upper, *lower;
5946 int intersection_p = 0;
5947
5948 /* Rearrange so that R1 is the left-most rectangle. */
5949 if (r1->x < r2->x)
5950 left = r1, right = r2;
5951 else
5952 left = r2, right = r1;
5953
5954 /* X0 of the intersection is right.x0, if this is inside R1,
5955 otherwise there is no intersection. */
5956 if (right->x <= left->x + left->width)
5957 {
5958 result->x = right->x;
5959
5960 /* The right end of the intersection is the minimum of the
5961 the right ends of left and right. */
5962 result->width = (min (left->x + left->width, right->x + right->width)
5963 - result->x);
5964
5965 /* Same game for Y. */
5966 if (r1->y < r2->y)
5967 upper = r1, lower = r2;
5968 else
5969 upper = r2, lower = r1;
5970
5971 /* The upper end of the intersection is lower.y0, if this is inside
5972 of upper. Otherwise, there is no intersection. */
5973 if (lower->y <= upper->y + upper->height)
5974 {
5975 result->y = lower->y;
5976
5977 /* The lower end of the intersection is the minimum of the lower
5978 ends of upper and lower. */
5979 result->height = (min (lower->y + lower->height,
5980 upper->y + upper->height)
5981 - result->y);
5982 intersection_p = 1;
5983 }
5984 }
5985
5986 return intersection_p;
5987 }
5988
5989
5990
5991
5992 \f
5993 static void
5994 frame_highlight (f)
5995 struct frame *f;
5996 {
5997 /* We used to only do this if Vx_no_window_manager was non-nil, but
5998 the ICCCM (section 4.1.6) says that the window's border pixmap
5999 and border pixel are window attributes which are "private to the
6000 client", so we can always change it to whatever we want. */
6001 BLOCK_INPUT;
6002 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6003 f->output_data.x->border_pixel);
6004 UNBLOCK_INPUT;
6005 x_update_cursor (f, 1);
6006 }
6007
6008 static void
6009 frame_unhighlight (f)
6010 struct frame *f;
6011 {
6012 /* We used to only do this if Vx_no_window_manager was non-nil, but
6013 the ICCCM (section 4.1.6) says that the window's border pixmap
6014 and border pixel are window attributes which are "private to the
6015 client", so we can always change it to whatever we want. */
6016 BLOCK_INPUT;
6017 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6018 f->output_data.x->border_tile);
6019 UNBLOCK_INPUT;
6020 x_update_cursor (f, 1);
6021 }
6022
6023 /* The focus has changed. Update the frames as necessary to reflect
6024 the new situation. Note that we can't change the selected frame
6025 here, because the Lisp code we are interrupting might become confused.
6026 Each event gets marked with the frame in which it occurred, so the
6027 Lisp code can tell when the switch took place by examining the events. */
6028
6029 static void
6030 x_new_focus_frame (dpyinfo, frame)
6031 struct x_display_info *dpyinfo;
6032 struct frame *frame;
6033 {
6034 struct frame *old_focus = dpyinfo->x_focus_frame;
6035
6036 if (frame != dpyinfo->x_focus_frame)
6037 {
6038 /* Set this before calling other routines, so that they see
6039 the correct value of x_focus_frame. */
6040 dpyinfo->x_focus_frame = frame;
6041
6042 if (old_focus && old_focus->auto_lower)
6043 x_lower_frame (old_focus);
6044
6045 #if 0
6046 selected_frame = frame;
6047 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6048 selected_frame);
6049 Fselect_window (selected_frame->selected_window);
6050 choose_minibuf_frame ();
6051 #endif /* ! 0 */
6052
6053 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6054 pending_autoraise_frame = dpyinfo->x_focus_frame;
6055 else
6056 pending_autoraise_frame = 0;
6057 }
6058
6059 x_frame_rehighlight (dpyinfo);
6060 }
6061
6062 /* Handle an event saying the mouse has moved out of an Emacs frame. */
6063
6064 void
6065 x_mouse_leave (dpyinfo)
6066 struct x_display_info *dpyinfo;
6067 {
6068 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
6069 }
6070
6071 /* The focus has changed, or we have redirected a frame's focus to
6072 another frame (this happens when a frame uses a surrogate
6073 mini-buffer frame). Shift the highlight as appropriate.
6074
6075 The FRAME argument doesn't necessarily have anything to do with which
6076 frame is being highlighted or un-highlighted; we only use it to find
6077 the appropriate X display info. */
6078
6079 static void
6080 XTframe_rehighlight (frame)
6081 struct frame *frame;
6082 {
6083 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6084 }
6085
6086 static void
6087 x_frame_rehighlight (dpyinfo)
6088 struct x_display_info *dpyinfo;
6089 {
6090 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6091
6092 if (dpyinfo->x_focus_frame)
6093 {
6094 dpyinfo->x_highlight_frame
6095 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6096 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6097 : dpyinfo->x_focus_frame);
6098 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
6099 {
6100 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6101 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
6102 }
6103 }
6104 else
6105 dpyinfo->x_highlight_frame = 0;
6106
6107 if (dpyinfo->x_highlight_frame != old_highlight)
6108 {
6109 if (old_highlight)
6110 frame_unhighlight (old_highlight);
6111 if (dpyinfo->x_highlight_frame)
6112 frame_highlight (dpyinfo->x_highlight_frame);
6113 }
6114 }
6115
6116
6117 \f
6118 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
6119
6120 /* Initialize mode_switch_bit and modifier_meaning. */
6121 static void
6122 x_find_modifier_meanings (dpyinfo)
6123 struct x_display_info *dpyinfo;
6124 {
6125 int min_code, max_code;
6126 KeySym *syms;
6127 int syms_per_code;
6128 XModifierKeymap *mods;
6129
6130 dpyinfo->meta_mod_mask = 0;
6131 dpyinfo->shift_lock_mask = 0;
6132 dpyinfo->alt_mod_mask = 0;
6133 dpyinfo->super_mod_mask = 0;
6134 dpyinfo->hyper_mod_mask = 0;
6135
6136 #ifdef HAVE_X11R4
6137 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
6138 #else
6139 min_code = dpyinfo->display->min_keycode;
6140 max_code = dpyinfo->display->max_keycode;
6141 #endif
6142
6143 syms = XGetKeyboardMapping (dpyinfo->display,
6144 min_code, max_code - min_code + 1,
6145 &syms_per_code);
6146 mods = XGetModifierMapping (dpyinfo->display);
6147
6148 /* Scan the modifier table to see which modifier bits the Meta and
6149 Alt keysyms are on. */
6150 {
6151 int row, col; /* The row and column in the modifier table. */
6152
6153 for (row = 3; row < 8; row++)
6154 for (col = 0; col < mods->max_keypermod; col++)
6155 {
6156 KeyCode code
6157 = mods->modifiermap[(row * mods->max_keypermod) + col];
6158
6159 /* Zeroes are used for filler. Skip them. */
6160 if (code == 0)
6161 continue;
6162
6163 /* Are any of this keycode's keysyms a meta key? */
6164 {
6165 int code_col;
6166
6167 for (code_col = 0; code_col < syms_per_code; code_col++)
6168 {
6169 int sym = syms[((code - min_code) * syms_per_code) + code_col];
6170
6171 switch (sym)
6172 {
6173 case XK_Meta_L:
6174 case XK_Meta_R:
6175 dpyinfo->meta_mod_mask |= (1 << row);
6176 break;
6177
6178 case XK_Alt_L:
6179 case XK_Alt_R:
6180 dpyinfo->alt_mod_mask |= (1 << row);
6181 break;
6182
6183 case XK_Hyper_L:
6184 case XK_Hyper_R:
6185 dpyinfo->hyper_mod_mask |= (1 << row);
6186 break;
6187
6188 case XK_Super_L:
6189 case XK_Super_R:
6190 dpyinfo->super_mod_mask |= (1 << row);
6191 break;
6192
6193 case XK_Shift_Lock:
6194 /* Ignore this if it's not on the lock modifier. */
6195 if ((1 << row) == LockMask)
6196 dpyinfo->shift_lock_mask = LockMask;
6197 break;
6198 }
6199 }
6200 }
6201 }
6202 }
6203
6204 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
6205 if (! dpyinfo->meta_mod_mask)
6206 {
6207 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6208 dpyinfo->alt_mod_mask = 0;
6209 }
6210
6211 /* If some keys are both alt and meta,
6212 make them just meta, not alt. */
6213 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6214 {
6215 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6216 }
6217
6218 XFree ((char *) syms);
6219 XFreeModifiermap (mods);
6220 }
6221
6222 /* Convert between the modifier bits X uses and the modifier bits
6223 Emacs uses. */
6224
6225 static unsigned int
6226 x_x_to_emacs_modifiers (dpyinfo, state)
6227 struct x_display_info *dpyinfo;
6228 unsigned int state;
6229 {
6230 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6231 | ((state & ControlMask) ? ctrl_modifier : 0)
6232 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6233 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6234 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6235 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
6236 }
6237
6238 static unsigned int
6239 x_emacs_to_x_modifiers (dpyinfo, state)
6240 struct x_display_info *dpyinfo;
6241 unsigned int state;
6242 {
6243 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6244 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6245 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6246 | ((state & shift_modifier) ? ShiftMask : 0)
6247 | ((state & ctrl_modifier) ? ControlMask : 0)
6248 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
6249 }
6250
6251 /* Convert a keysym to its name. */
6252
6253 char *
6254 x_get_keysym_name (keysym)
6255 KeySym keysym;
6256 {
6257 char *value;
6258
6259 BLOCK_INPUT;
6260 value = XKeysymToString (keysym);
6261 UNBLOCK_INPUT;
6262
6263 return value;
6264 }
6265
6266
6267 \f
6268 /* Mouse clicks and mouse movement. Rah. */
6269
6270 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6271 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6272 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6273 not force the value into range. */
6274
6275 void
6276 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6277 FRAME_PTR f;
6278 register int pix_x, pix_y;
6279 register int *x, *y;
6280 XRectangle *bounds;
6281 int noclip;
6282 {
6283 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6284 even for negative values. */
6285 if (pix_x < 0)
6286 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6287 if (pix_y < 0)
6288 pix_y -= (f)->output_data.x->line_height - 1;
6289
6290 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6291 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6292
6293 if (bounds)
6294 {
6295 bounds->width = FONT_WIDTH (f->output_data.x->font);
6296 bounds->height = f->output_data.x->line_height;
6297 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6298 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6299 }
6300
6301 if (!noclip)
6302 {
6303 if (pix_x < 0)
6304 pix_x = 0;
6305 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6306 pix_x = FRAME_WINDOW_WIDTH (f);
6307
6308 if (pix_y < 0)
6309 pix_y = 0;
6310 else if (pix_y > f->height)
6311 pix_y = f->height;
6312 }
6313
6314 *x = pix_x;
6315 *y = pix_y;
6316 }
6317
6318
6319 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6320 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6321 can't tell the positions because W's display is not up to date,
6322 return 0. */
6323
6324 int
6325 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6326 struct window *w;
6327 int hpos, vpos;
6328 int *frame_x, *frame_y;
6329 {
6330 int success_p;
6331
6332 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6333 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6334
6335 if (display_completed)
6336 {
6337 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6338 struct glyph *glyph = row->glyphs[TEXT_AREA];
6339 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6340
6341 *frame_y = row->y;
6342 *frame_x = row->x;
6343 while (glyph < end)
6344 {
6345 *frame_x += glyph->pixel_width;
6346 ++glyph;
6347 }
6348
6349 success_p = 1;
6350 }
6351 else
6352 {
6353 *frame_y = *frame_x = 0;
6354 success_p = 0;
6355 }
6356
6357 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6358 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6359 return success_p;
6360 }
6361
6362
6363 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6364
6365 If the event is a button press, then note that we have grabbed
6366 the mouse. */
6367
6368 static Lisp_Object
6369 construct_mouse_click (result, event, f)
6370 struct input_event *result;
6371 XButtonEvent *event;
6372 struct frame *f;
6373 {
6374 /* Make the event type no_event; we'll change that when we decide
6375 otherwise. */
6376 result->kind = mouse_click;
6377 result->code = event->button - Button1;
6378 result->timestamp = event->time;
6379 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6380 event->state)
6381 | (event->type == ButtonRelease
6382 ? up_modifier
6383 : down_modifier));
6384
6385 XSETINT (result->x, event->x);
6386 XSETINT (result->y, event->y);
6387 XSETFRAME (result->frame_or_window, f);
6388 result->arg = Qnil;
6389 return Qnil;
6390 }
6391
6392 \f
6393 /* Function to report a mouse movement to the mainstream Emacs code.
6394 The input handler calls this.
6395
6396 We have received a mouse movement event, which is given in *event.
6397 If the mouse is over a different glyph than it was last time, tell
6398 the mainstream emacs code by setting mouse_moved. If not, ask for
6399 another motion event, so we can check again the next time it moves. */
6400
6401 static XMotionEvent last_mouse_motion_event;
6402 static Lisp_Object last_mouse_motion_frame;
6403
6404 static void
6405 note_mouse_movement (frame, event)
6406 FRAME_PTR frame;
6407 XMotionEvent *event;
6408 {
6409 last_mouse_movement_time = event->time;
6410 last_mouse_motion_event = *event;
6411 XSETFRAME (last_mouse_motion_frame, frame);
6412
6413 if (event->window != FRAME_X_WINDOW (frame))
6414 {
6415 frame->mouse_moved = 1;
6416 last_mouse_scroll_bar = Qnil;
6417 note_mouse_highlight (frame, -1, -1);
6418 }
6419
6420 /* Has the mouse moved off the glyph it was on at the last sighting? */
6421 else if (event->x < last_mouse_glyph.x
6422 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6423 || event->y < last_mouse_glyph.y
6424 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6425 {
6426 frame->mouse_moved = 1;
6427 last_mouse_scroll_bar = Qnil;
6428 note_mouse_highlight (frame, event->x, event->y);
6429 }
6430 }
6431
6432 /* This is used for debugging, to turn off note_mouse_highlight. */
6433
6434 int disable_mouse_highlight;
6435
6436
6437 \f
6438 /************************************************************************
6439 Mouse Face
6440 ************************************************************************/
6441
6442 /* Find the glyph under window-relative coordinates X/Y in window W.
6443 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6444 strings. Return in *HPOS and *VPOS the row and column number of
6445 the glyph found. Return in *AREA the glyph area containing X.
6446 Value is a pointer to the glyph found or null if X/Y is not on
6447 text, or we can't tell because W's current matrix is not up to
6448 date. */
6449
6450 static struct glyph *
6451 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
6452 struct window *w;
6453 int x, y;
6454 int *hpos, *vpos, *area;
6455 int buffer_only_p;
6456 {
6457 struct glyph *glyph, *end;
6458 struct glyph_row *row = NULL;
6459 int x0, i, left_area_width;
6460
6461 /* Find row containing Y. Give up if some row is not enabled. */
6462 for (i = 0; i < w->current_matrix->nrows; ++i)
6463 {
6464 row = MATRIX_ROW (w->current_matrix, i);
6465 if (!row->enabled_p)
6466 return NULL;
6467 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6468 break;
6469 }
6470
6471 *vpos = i;
6472 *hpos = 0;
6473
6474 /* Give up if Y is not in the window. */
6475 if (i == w->current_matrix->nrows)
6476 return NULL;
6477
6478 /* Get the glyph area containing X. */
6479 if (w->pseudo_window_p)
6480 {
6481 *area = TEXT_AREA;
6482 x0 = 0;
6483 }
6484 else
6485 {
6486 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6487 if (x < left_area_width)
6488 {
6489 *area = LEFT_MARGIN_AREA;
6490 x0 = 0;
6491 }
6492 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6493 {
6494 *area = TEXT_AREA;
6495 x0 = row->x + left_area_width;
6496 }
6497 else
6498 {
6499 *area = RIGHT_MARGIN_AREA;
6500 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6501 }
6502 }
6503
6504 /* Find glyph containing X. */
6505 glyph = row->glyphs[*area];
6506 end = glyph + row->used[*area];
6507 while (glyph < end)
6508 {
6509 if (x < x0 + glyph->pixel_width)
6510 {
6511 if (w->pseudo_window_p)
6512 break;
6513 else if (!buffer_only_p || BUFFERP (glyph->object))
6514 break;
6515 }
6516
6517 x0 += glyph->pixel_width;
6518 ++glyph;
6519 }
6520
6521 if (glyph == end)
6522 return NULL;
6523
6524 *hpos = glyph - row->glyphs[*area];
6525 return glyph;
6526 }
6527
6528
6529 /* Convert frame-relative x/y to coordinates relative to window W.
6530 Takes pseudo-windows into account. */
6531
6532 static void
6533 frame_to_window_pixel_xy (w, x, y)
6534 struct window *w;
6535 int *x, *y;
6536 {
6537 if (w->pseudo_window_p)
6538 {
6539 /* A pseudo-window is always full-width, and starts at the
6540 left edge of the frame, plus a frame border. */
6541 struct frame *f = XFRAME (w->frame);
6542 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6543 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6544 }
6545 else
6546 {
6547 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6548 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6549 }
6550 }
6551
6552
6553 /* Take proper action when mouse has moved to the mode or header line of
6554 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6555 mode line. X is relative to the start of the text display area of
6556 W, so the width of bitmap areas and scroll bars must be subtracted
6557 to get a position relative to the start of the mode line. */
6558
6559 static void
6560 note_mode_line_highlight (w, x, mode_line_p)
6561 struct window *w;
6562 int x, mode_line_p;
6563 {
6564 struct frame *f = XFRAME (w->frame);
6565 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6566 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6567 struct glyph_row *row;
6568
6569 if (mode_line_p)
6570 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6571 else
6572 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6573
6574 if (row->enabled_p)
6575 {
6576 struct glyph *glyph, *end;
6577 Lisp_Object help, map;
6578 int x0;
6579
6580 /* Find the glyph under X. */
6581 glyph = row->glyphs[TEXT_AREA];
6582 end = glyph + row->used[TEXT_AREA];
6583 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6584 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
6585
6586 while (glyph < end
6587 && x >= x0 + glyph->pixel_width)
6588 {
6589 x0 += glyph->pixel_width;
6590 ++glyph;
6591 }
6592
6593 if (glyph < end
6594 && STRINGP (glyph->object)
6595 && XSTRING (glyph->object)->intervals
6596 && glyph->charpos >= 0
6597 && glyph->charpos < XSTRING (glyph->object)->size)
6598 {
6599 /* If we're on a string with `help-echo' text property,
6600 arrange for the help to be displayed. This is done by
6601 setting the global variable help_echo to the help string. */
6602 help = Fget_text_property (make_number (glyph->charpos),
6603 Qhelp_echo, glyph->object);
6604 if (!NILP (help))
6605 {
6606 help_echo = help;
6607 XSETWINDOW (help_echo_window, w);
6608 help_echo_object = glyph->object;
6609 help_echo_pos = glyph->charpos;
6610 }
6611
6612 /* Change the mouse pointer according to what is under X/Y. */
6613 map = Fget_text_property (make_number (glyph->charpos),
6614 Qlocal_map, glyph->object);
6615 if (KEYMAPP (map))
6616 cursor = f->output_data.x->nontext_cursor;
6617 else
6618 {
6619 map = Fget_text_property (make_number (glyph->charpos),
6620 Qkeymap, glyph->object);
6621 if (KEYMAPP (map))
6622 cursor = f->output_data.x->nontext_cursor;
6623 }
6624 }
6625 }
6626
6627 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6628 }
6629
6630
6631 /* Take proper action when the mouse has moved to position X, Y on
6632 frame F as regards highlighting characters that have mouse-face
6633 properties. Also de-highlighting chars where the mouse was before.
6634 X and Y can be negative or out of range. */
6635
6636 static void
6637 note_mouse_highlight (f, x, y)
6638 struct frame *f;
6639 int x, y;
6640 {
6641 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6642 int portion;
6643 Lisp_Object window;
6644 struct window *w;
6645
6646 /* When a menu is active, don't highlight because this looks odd. */
6647 #ifdef USE_X_TOOLKIT
6648 if (popup_activated ())
6649 return;
6650 #endif
6651
6652 if (disable_mouse_highlight
6653 || !f->glyphs_initialized_p)
6654 return;
6655
6656 dpyinfo->mouse_face_mouse_x = x;
6657 dpyinfo->mouse_face_mouse_y = y;
6658 dpyinfo->mouse_face_mouse_frame = f;
6659
6660 if (dpyinfo->mouse_face_defer)
6661 return;
6662
6663 if (gc_in_progress)
6664 {
6665 dpyinfo->mouse_face_deferred_gc = 1;
6666 return;
6667 }
6668
6669 /* Which window is that in? */
6670 window = window_from_coordinates (f, x, y, &portion, 1);
6671
6672 /* If we were displaying active text in another window, clear that. */
6673 if (! EQ (window, dpyinfo->mouse_face_window))
6674 clear_mouse_face (dpyinfo);
6675
6676 /* Not on a window -> return. */
6677 if (!WINDOWP (window))
6678 return;
6679
6680 /* Convert to window-relative pixel coordinates. */
6681 w = XWINDOW (window);
6682 frame_to_window_pixel_xy (w, &x, &y);
6683
6684 /* Handle tool-bar window differently since it doesn't display a
6685 buffer. */
6686 if (EQ (window, f->tool_bar_window))
6687 {
6688 note_tool_bar_highlight (f, x, y);
6689 return;
6690 }
6691
6692 if (portion == 1 || portion == 3)
6693 {
6694 /* Mouse is on the mode or top line. */
6695 note_mode_line_highlight (w, x, portion == 1);
6696 return;
6697 }
6698 else if (portion == 2)
6699 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6700 f->output_data.x->horizontal_drag_cursor);
6701 else
6702 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6703 f->output_data.x->text_cursor);
6704
6705 /* Are we in a window whose display is up to date?
6706 And verify the buffer's text has not changed. */
6707 if (/* Within text portion of the window. */
6708 portion == 0
6709 && EQ (w->window_end_valid, w->buffer)
6710 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6711 && (XFASTINT (w->last_overlay_modified)
6712 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
6713 {
6714 int hpos, vpos, pos, i, area;
6715 struct glyph *glyph;
6716 Lisp_Object object;
6717
6718 /* Find the glyph under X/Y. */
6719 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
6720
6721 /* Clear mouse face if X/Y not over text. */
6722 if (glyph == NULL
6723 || area != TEXT_AREA
6724 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6725 {
6726 clear_mouse_face (dpyinfo);
6727 return;
6728 }
6729
6730 pos = glyph->charpos;
6731 object = glyph->object;
6732 if (!STRINGP (object) && !BUFFERP (object))
6733 return;
6734
6735 {
6736 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
6737 Lisp_Object *overlay_vec = NULL;
6738 int len, noverlays;
6739 struct buffer *obuf;
6740 int obegv, ozv;
6741
6742 /* If we get an out-of-range value, return now; avoid an error. */
6743 if (BUFFERP (object) && pos > BUF_Z (XBUFFER (w->buffer)))
6744 return;
6745
6746 /* Make the window's buffer temporarily current for
6747 overlays_at and compute_char_face. */
6748 obuf = current_buffer;
6749 current_buffer = XBUFFER (w->buffer);
6750 obegv = BEGV;
6751 ozv = ZV;
6752 BEGV = BEG;
6753 ZV = Z;
6754
6755 /* Is this char mouse-active or does it have help-echo? */
6756 position = make_number (pos);
6757
6758 if (BUFFERP (object))
6759 {
6760 /* Put all the overlays we want in a vector in overlay_vec.
6761 Store the length in len. If there are more than 10, make
6762 enough space for all, and try again. */
6763 len = 10;
6764 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6765 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6766 if (noverlays > len)
6767 {
6768 len = noverlays;
6769 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6770 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6771 }
6772
6773 /* Sort overlays into increasing priority order. */
6774 noverlays = sort_overlays (overlay_vec, noverlays, w);
6775 }
6776 else
6777 noverlays = 0;
6778
6779 /* Check mouse-face highlighting. */
6780 if (! (EQ (window, dpyinfo->mouse_face_window)
6781 && vpos >= dpyinfo->mouse_face_beg_row
6782 && vpos <= dpyinfo->mouse_face_end_row
6783 && (vpos > dpyinfo->mouse_face_beg_row
6784 || hpos >= dpyinfo->mouse_face_beg_col)
6785 && (vpos < dpyinfo->mouse_face_end_row
6786 || hpos < dpyinfo->mouse_face_end_col
6787 || dpyinfo->mouse_face_past_end))
6788 /* If there exists an overlay with mouse-face overlapping
6789 the one we are currently highlighting, we have to
6790 check if we enter the overlapping overlay, and then
6791 highlight only that. */
6792 || (OVERLAYP (dpyinfo->mouse_face_overlay)
6793 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
6794
6795 {
6796 /* Clear the display of the old active region, if any. */
6797 clear_mouse_face (dpyinfo);
6798
6799 /* Find the highest priority overlay that has a mouse-face
6800 property. */
6801 overlay = Qnil;
6802 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
6803 {
6804 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6805 if (!NILP (mouse_face))
6806 overlay = overlay_vec[i];
6807 }
6808 dpyinfo->mouse_face_overlay = overlay;
6809
6810 /* If no overlay applies, get a text property. */
6811 if (NILP (overlay))
6812 mouse_face = Fget_text_property (position, Qmouse_face, object);
6813
6814 /* Handle the overlay case. */
6815 if (!NILP (overlay))
6816 {
6817 /* Find the range of text around this char that
6818 should be active. */
6819 Lisp_Object before, after;
6820 int ignore;
6821
6822 before = Foverlay_start (overlay);
6823 after = Foverlay_end (overlay);
6824 /* Record this as the current active region. */
6825 fast_find_position (w, XFASTINT (before),
6826 &dpyinfo->mouse_face_beg_col,
6827 &dpyinfo->mouse_face_beg_row,
6828 &dpyinfo->mouse_face_beg_x,
6829 &dpyinfo->mouse_face_beg_y);
6830 dpyinfo->mouse_face_past_end
6831 = !fast_find_position (w, XFASTINT (after),
6832 &dpyinfo->mouse_face_end_col,
6833 &dpyinfo->mouse_face_end_row,
6834 &dpyinfo->mouse_face_end_x,
6835 &dpyinfo->mouse_face_end_y);
6836 dpyinfo->mouse_face_window = window;
6837 dpyinfo->mouse_face_face_id
6838 = face_at_buffer_position (w, pos, 0, 0,
6839 &ignore, pos + 1, 1);
6840
6841 /* Display it as active. */
6842 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6843 }
6844 /* Handle the text property case. */
6845 else if (!NILP (mouse_face) && BUFFERP (object))
6846 {
6847 /* Find the range of text around this char that
6848 should be active. */
6849 Lisp_Object before, after, beginning, end;
6850 int ignore;
6851
6852 beginning = Fmarker_position (w->start);
6853 end = make_number (BUF_Z (XBUFFER (object))
6854 - XFASTINT (w->window_end_pos));
6855 before
6856 = Fprevious_single_property_change (make_number (pos + 1),
6857 Qmouse_face,
6858 object, beginning);
6859 after
6860 = Fnext_single_property_change (position, Qmouse_face,
6861 object, end);
6862
6863 /* Record this as the current active region. */
6864 fast_find_position (w, XFASTINT (before),
6865 &dpyinfo->mouse_face_beg_col,
6866 &dpyinfo->mouse_face_beg_row,
6867 &dpyinfo->mouse_face_beg_x,
6868 &dpyinfo->mouse_face_beg_y);
6869 dpyinfo->mouse_face_past_end
6870 = !fast_find_position (w, XFASTINT (after),
6871 &dpyinfo->mouse_face_end_col,
6872 &dpyinfo->mouse_face_end_row,
6873 &dpyinfo->mouse_face_end_x,
6874 &dpyinfo->mouse_face_end_y);
6875 dpyinfo->mouse_face_window = window;
6876
6877 if (BUFFERP (object))
6878 dpyinfo->mouse_face_face_id
6879 = face_at_buffer_position (w, pos, 0, 0,
6880 &ignore, pos + 1, 1);
6881
6882 /* Display it as active. */
6883 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6884 }
6885 else if (!NILP (mouse_face) && STRINGP (object))
6886 {
6887 Lisp_Object b, e;
6888 int ignore;
6889
6890 b = Fprevious_single_property_change (make_number (pos + 1),
6891 Qmouse_face,
6892 object, Qnil);
6893 e = Fnext_single_property_change (position, Qmouse_face,
6894 object, Qnil);
6895 if (NILP (b))
6896 b = make_number (0);
6897 if (NILP (e))
6898 e = make_number (XSTRING (object)->size) - 1;
6899 fast_find_string_pos (w, XINT (b), object,
6900 &dpyinfo->mouse_face_beg_col,
6901 &dpyinfo->mouse_face_beg_row,
6902 &dpyinfo->mouse_face_beg_x,
6903 &dpyinfo->mouse_face_beg_y, 0);
6904 fast_find_string_pos (w, XINT (e), object,
6905 &dpyinfo->mouse_face_end_col,
6906 &dpyinfo->mouse_face_end_row,
6907 &dpyinfo->mouse_face_end_x,
6908 &dpyinfo->mouse_face_end_y, 1);
6909 dpyinfo->mouse_face_past_end = 0;
6910 dpyinfo->mouse_face_window = window;
6911 dpyinfo->mouse_face_face_id
6912 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
6913 glyph->face_id, 1);
6914 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6915 }
6916 }
6917
6918 /* Look for a `help-echo' property. */
6919 {
6920 Lisp_Object help, overlay;
6921
6922 /* Check overlays first. */
6923 help = overlay = Qnil;
6924 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
6925 {
6926 overlay = overlay_vec[i];
6927 help = Foverlay_get (overlay, Qhelp_echo);
6928 }
6929
6930 if (!NILP (help))
6931 {
6932 help_echo = help;
6933 help_echo_window = window;
6934 help_echo_object = overlay;
6935 help_echo_pos = pos;
6936 }
6937 else
6938 {
6939 /* Try text properties. */
6940 if ((STRINGP (glyph->object)
6941 && glyph->charpos >= 0
6942 && glyph->charpos < XSTRING (glyph->object)->size)
6943 || (BUFFERP (glyph->object)
6944 && glyph->charpos >= BEGV
6945 && glyph->charpos < ZV))
6946 help = Fget_text_property (make_number (glyph->charpos),
6947 Qhelp_echo, glyph->object);
6948
6949 if (!NILP (help))
6950 {
6951 help_echo = help;
6952 help_echo_window = window;
6953 help_echo_object = glyph->object;
6954 help_echo_pos = glyph->charpos;
6955 }
6956 }
6957 }
6958
6959 BEGV = obegv;
6960 ZV = ozv;
6961 current_buffer = obuf;
6962 }
6963 }
6964 }
6965
6966 static void
6967 redo_mouse_highlight ()
6968 {
6969 if (!NILP (last_mouse_motion_frame)
6970 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6971 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6972 last_mouse_motion_event.x,
6973 last_mouse_motion_event.y);
6974 }
6975
6976
6977 \f
6978 /***********************************************************************
6979 Tool-bars
6980 ***********************************************************************/
6981
6982 static int x_tool_bar_item P_ ((struct frame *, int, int,
6983 struct glyph **, int *, int *, int *));
6984
6985 /* Tool-bar item index of the item on which a mouse button was pressed
6986 or -1. */
6987
6988 static int last_tool_bar_item;
6989
6990
6991 /* Get information about the tool-bar item at position X/Y on frame F.
6992 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6993 the current matrix of the tool-bar window of F, or NULL if not
6994 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6995 item in F->tool_bar_items. Value is
6996
6997 -1 if X/Y is not on a tool-bar item
6998 0 if X/Y is on the same item that was highlighted before.
6999 1 otherwise. */
7000
7001 static int
7002 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
7003 struct frame *f;
7004 int x, y;
7005 struct glyph **glyph;
7006 int *hpos, *vpos, *prop_idx;
7007 {
7008 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7009 struct window *w = XWINDOW (f->tool_bar_window);
7010 int area;
7011
7012 /* Find the glyph under X/Y. */
7013 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
7014 if (*glyph == NULL)
7015 return -1;
7016
7017 /* Get the start of this tool-bar item's properties in
7018 f->tool_bar_items. */
7019 if (!tool_bar_item_info (f, *glyph, prop_idx))
7020 return -1;
7021
7022 /* Is mouse on the highlighted item? */
7023 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
7024 && *vpos >= dpyinfo->mouse_face_beg_row
7025 && *vpos <= dpyinfo->mouse_face_end_row
7026 && (*vpos > dpyinfo->mouse_face_beg_row
7027 || *hpos >= dpyinfo->mouse_face_beg_col)
7028 && (*vpos < dpyinfo->mouse_face_end_row
7029 || *hpos < dpyinfo->mouse_face_end_col
7030 || dpyinfo->mouse_face_past_end))
7031 return 0;
7032
7033 return 1;
7034 }
7035
7036
7037 /* Handle mouse button event on the tool-bar of frame F, at
7038 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
7039 or ButtonRelase. */
7040
7041 static void
7042 x_handle_tool_bar_click (f, button_event)
7043 struct frame *f;
7044 XButtonEvent *button_event;
7045 {
7046 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7047 struct window *w = XWINDOW (f->tool_bar_window);
7048 int hpos, vpos, prop_idx;
7049 struct glyph *glyph;
7050 Lisp_Object enabled_p;
7051 int x = button_event->x;
7052 int y = button_event->y;
7053
7054 /* If not on the highlighted tool-bar item, return. */
7055 frame_to_window_pixel_xy (w, &x, &y);
7056 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
7057 return;
7058
7059 /* If item is disabled, do nothing. */
7060 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7061 if (NILP (enabled_p))
7062 return;
7063
7064 if (button_event->type == ButtonPress)
7065 {
7066 /* Show item in pressed state. */
7067 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7068 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
7069 last_tool_bar_item = prop_idx;
7070 }
7071 else
7072 {
7073 Lisp_Object key, frame;
7074 struct input_event event;
7075
7076 /* Show item in released state. */
7077 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7078 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7079
7080 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
7081
7082 XSETFRAME (frame, f);
7083 event.kind = TOOL_BAR_EVENT;
7084 event.frame_or_window = frame;
7085 event.arg = frame;
7086 kbd_buffer_store_event (&event);
7087
7088 event.kind = TOOL_BAR_EVENT;
7089 event.frame_or_window = frame;
7090 event.arg = key;
7091 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7092 button_event->state);
7093 kbd_buffer_store_event (&event);
7094 last_tool_bar_item = -1;
7095 }
7096 }
7097
7098
7099 /* Possibly highlight a tool-bar item on frame F when mouse moves to
7100 tool-bar window-relative coordinates X/Y. Called from
7101 note_mouse_highlight. */
7102
7103 static void
7104 note_tool_bar_highlight (f, x, y)
7105 struct frame *f;
7106 int x, y;
7107 {
7108 Lisp_Object window = f->tool_bar_window;
7109 struct window *w = XWINDOW (window);
7110 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7111 int hpos, vpos;
7112 struct glyph *glyph;
7113 struct glyph_row *row;
7114 int i;
7115 Lisp_Object enabled_p;
7116 int prop_idx;
7117 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
7118 int mouse_down_p, rc;
7119
7120 /* Function note_mouse_highlight is called with negative x(y
7121 values when mouse moves outside of the frame. */
7122 if (x <= 0 || y <= 0)
7123 {
7124 clear_mouse_face (dpyinfo);
7125 return;
7126 }
7127
7128 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7129 if (rc < 0)
7130 {
7131 /* Not on tool-bar item. */
7132 clear_mouse_face (dpyinfo);
7133 return;
7134 }
7135 else if (rc == 0)
7136 /* On same tool-bar item as before. */
7137 goto set_help_echo;
7138
7139 clear_mouse_face (dpyinfo);
7140
7141 /* Mouse is down, but on different tool-bar item? */
7142 mouse_down_p = (dpyinfo->grabbed
7143 && f == last_mouse_frame
7144 && FRAME_LIVE_P (f));
7145 if (mouse_down_p
7146 && last_tool_bar_item != prop_idx)
7147 return;
7148
7149 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7150 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7151
7152 /* If tool-bar item is not enabled, don't highlight it. */
7153 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7154 if (!NILP (enabled_p))
7155 {
7156 /* Compute the x-position of the glyph. In front and past the
7157 image is a space. We include this is the highlighted area. */
7158 row = MATRIX_ROW (w->current_matrix, vpos);
7159 for (i = x = 0; i < hpos; ++i)
7160 x += row->glyphs[TEXT_AREA][i].pixel_width;
7161
7162 /* Record this as the current active region. */
7163 dpyinfo->mouse_face_beg_col = hpos;
7164 dpyinfo->mouse_face_beg_row = vpos;
7165 dpyinfo->mouse_face_beg_x = x;
7166 dpyinfo->mouse_face_beg_y = row->y;
7167 dpyinfo->mouse_face_past_end = 0;
7168
7169 dpyinfo->mouse_face_end_col = hpos + 1;
7170 dpyinfo->mouse_face_end_row = vpos;
7171 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7172 dpyinfo->mouse_face_end_y = row->y;
7173 dpyinfo->mouse_face_window = window;
7174 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7175
7176 /* Display it as active. */
7177 show_mouse_face (dpyinfo, draw);
7178 dpyinfo->mouse_face_image_state = draw;
7179 }
7180
7181 set_help_echo:
7182
7183 /* Set help_echo to a help string.to display for this tool-bar item.
7184 XTread_socket does the rest. */
7185 help_echo_object = help_echo_window = Qnil;
7186 help_echo_pos = -1;
7187 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
7188 if (NILP (help_echo))
7189 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
7190 }
7191
7192
7193 \f
7194 /* Find the glyph matrix position of buffer position POS in window W.
7195 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7196 current glyphs must be up to date. If POS is above window start
7197 return (0, 0, 0, 0). If POS is after end of W, return end of
7198 last line in W. */
7199
7200 static int
7201 fast_find_position (w, pos, hpos, vpos, x, y)
7202 struct window *w;
7203 int pos;
7204 int *hpos, *vpos, *x, *y;
7205 {
7206 int i;
7207 int lastcol;
7208 int maybe_next_line_p = 0;
7209 int line_start_position;
7210 int yb = window_text_bottom_y (w);
7211 struct glyph_row *row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7212 struct glyph_row *best_row = row;
7213 int row_vpos = 0, best_row_vpos = 0;
7214 int current_x;
7215
7216 while (row->y < yb)
7217 {
7218 if (row->used[TEXT_AREA])
7219 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7220 else
7221 line_start_position = 0;
7222
7223 if (line_start_position > pos)
7224 break;
7225 /* If the position sought is the end of the buffer,
7226 don't include the blank lines at the bottom of the window. */
7227 else if (line_start_position == pos
7228 && pos == BUF_ZV (XBUFFER (w->buffer)))
7229 {
7230 maybe_next_line_p = 1;
7231 break;
7232 }
7233 else if (line_start_position > 0)
7234 {
7235 best_row = row;
7236 best_row_vpos = row_vpos;
7237 }
7238
7239 if (row->y + row->height >= yb)
7240 break;
7241
7242 ++row;
7243 ++row_vpos;
7244 }
7245
7246 /* Find the right column within BEST_ROW. */
7247 lastcol = 0;
7248 current_x = best_row->x;
7249 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
7250 {
7251 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7252 int charpos;
7253
7254 charpos = glyph->charpos;
7255 if (charpos == pos)
7256 {
7257 *hpos = i;
7258 *vpos = best_row_vpos;
7259 *x = current_x;
7260 *y = best_row->y;
7261 return 1;
7262 }
7263 else if (charpos > pos)
7264 break;
7265 else if (charpos > 0)
7266 lastcol = i;
7267
7268 current_x += glyph->pixel_width;
7269 }
7270
7271 /* If we're looking for the end of the buffer,
7272 and we didn't find it in the line we scanned,
7273 use the start of the following line. */
7274 if (maybe_next_line_p)
7275 {
7276 ++best_row;
7277 ++best_row_vpos;
7278 lastcol = 0;
7279 current_x = best_row->x;
7280 }
7281
7282 *vpos = best_row_vpos;
7283 *hpos = lastcol + 1;
7284 *x = current_x;
7285 *y = best_row->y;
7286 return 0;
7287 }
7288
7289
7290 /* Find the position of the the glyph for position POS in OBJECT in
7291 window W's current matrix, and return in *X/*Y the pixel
7292 coordinates, and return in *HPOS/*VPOS the column/row of the glyph.
7293
7294 RIGHT_P non-zero means return the position of the right edge of the
7295 glyph, RIGHT_P zero means return the left edge position.
7296
7297 If no glyph for POS exists in the matrix, return the position of
7298 the glyph with the next smaller position that is in the matrix, if
7299 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7300 exists in the matrix, return the position of the glyph with the
7301 next larger position in OBJECT.
7302
7303 Value is non-zero if a glyph was found. */
7304
7305 static int
7306 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7307 struct window *w;
7308 int pos;
7309 Lisp_Object object;
7310 int *hpos, *vpos, *x, *y;
7311 int right_p;
7312 {
7313 int yb = window_text_bottom_y (w);
7314 struct glyph_row *r;
7315 struct glyph *best_glyph = NULL;
7316 struct glyph_row *best_row = NULL;
7317 int best_x = 0;
7318
7319 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7320 r->enabled_p && r->y < yb;
7321 ++r)
7322 {
7323 struct glyph *g = r->glyphs[TEXT_AREA];
7324 struct glyph *e = g + r->used[TEXT_AREA];
7325 int gx;
7326
7327 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7328 if (EQ (g->object, object))
7329 {
7330 if (g->charpos == pos)
7331 {
7332 best_glyph = g;
7333 best_x = gx;
7334 best_row = r;
7335 goto found;
7336 }
7337 else if (best_glyph == NULL
7338 || ((abs (g->charpos - pos)
7339 < abs (best_glyph->charpos - pos))
7340 && (right_p
7341 ? g->charpos < pos
7342 : g->charpos > pos)))
7343 {
7344 best_glyph = g;
7345 best_x = gx;
7346 best_row = r;
7347 }
7348 }
7349 }
7350
7351 found:
7352
7353 if (best_glyph)
7354 {
7355 *x = best_x;
7356 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7357
7358 if (right_p)
7359 {
7360 *x += best_glyph->pixel_width;
7361 ++*hpos;
7362 }
7363
7364 *y = best_row->y;
7365 *vpos = best_row - w->current_matrix->rows;
7366 }
7367
7368 return best_glyph != NULL;
7369 }
7370
7371
7372 /* Display the active region described by mouse_face_*
7373 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7374
7375 static void
7376 show_mouse_face (dpyinfo, draw)
7377 struct x_display_info *dpyinfo;
7378 enum draw_glyphs_face draw;
7379 {
7380 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7381 struct frame *f = XFRAME (WINDOW_FRAME (w));
7382 int i;
7383 int cursor_off_p = 0;
7384 struct cursor_pos saved_cursor;
7385
7386 saved_cursor = output_cursor;
7387
7388 /* If window is in the process of being destroyed, don't bother
7389 to do anything. */
7390 if (w->current_matrix == NULL)
7391 goto set_x_cursor;
7392
7393 /* Recognize when we are called to operate on rows that don't exist
7394 anymore. This can happen when a window is split. */
7395 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7396 goto set_x_cursor;
7397
7398 set_output_cursor (&w->phys_cursor);
7399
7400 /* Note that mouse_face_beg_row etc. are window relative. */
7401 for (i = dpyinfo->mouse_face_beg_row;
7402 i <= dpyinfo->mouse_face_end_row;
7403 i++)
7404 {
7405 int start_hpos, end_hpos, start_x;
7406 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7407
7408 /* Don't do anything if row doesn't have valid contents. */
7409 if (!row->enabled_p)
7410 continue;
7411
7412 /* For all but the first row, the highlight starts at column 0. */
7413 if (i == dpyinfo->mouse_face_beg_row)
7414 {
7415 start_hpos = dpyinfo->mouse_face_beg_col;
7416 start_x = dpyinfo->mouse_face_beg_x;
7417 }
7418 else
7419 {
7420 start_hpos = 0;
7421 start_x = 0;
7422 }
7423
7424 if (i == dpyinfo->mouse_face_end_row)
7425 end_hpos = dpyinfo->mouse_face_end_col;
7426 else
7427 end_hpos = row->used[TEXT_AREA];
7428
7429 /* If the cursor's in the text we are about to rewrite, turn the
7430 cursor off. */
7431 if (!w->pseudo_window_p
7432 && i == output_cursor.vpos
7433 && output_cursor.hpos >= start_hpos - 1
7434 && output_cursor.hpos <= end_hpos)
7435 {
7436 x_update_window_cursor (w, 0);
7437 cursor_off_p = 1;
7438 }
7439
7440 if (end_hpos > start_hpos)
7441 {
7442 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
7443 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7444 start_hpos, end_hpos, draw, NULL, NULL, 0);
7445 }
7446 }
7447
7448 /* If we turned the cursor off, turn it back on. */
7449 if (cursor_off_p)
7450 x_display_cursor (w, 1,
7451 output_cursor.hpos, output_cursor.vpos,
7452 output_cursor.x, output_cursor.y);
7453
7454 output_cursor = saved_cursor;
7455
7456 set_x_cursor:
7457
7458 /* Change the mouse cursor. */
7459 if (draw == DRAW_NORMAL_TEXT)
7460 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7461 f->output_data.x->text_cursor);
7462 else if (draw == DRAW_MOUSE_FACE)
7463 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7464 f->output_data.x->cross_cursor);
7465 else
7466 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7467 f->output_data.x->nontext_cursor);
7468 }
7469
7470 /* Clear out the mouse-highlighted active region.
7471 Redraw it un-highlighted first. */
7472
7473 void
7474 clear_mouse_face (dpyinfo)
7475 struct x_display_info *dpyinfo;
7476 {
7477 #if 0 /* This prevents redrawing tool bar items when changing from one
7478 to another while a tooltip is open, so don't do it. */
7479 if (!NILP (tip_frame))
7480 return;
7481 #endif
7482
7483 if (! NILP (dpyinfo->mouse_face_window))
7484 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7485
7486 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7487 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7488 dpyinfo->mouse_face_window = Qnil;
7489 }
7490
7491
7492 /* Clear any mouse-face on window W. This function is part of the
7493 redisplay interface, and is called from try_window_id and similar
7494 functions to ensure the mouse-highlight is off. */
7495
7496 static void
7497 x_clear_mouse_face (w)
7498 struct window *w;
7499 {
7500 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7501 Lisp_Object window;
7502
7503 BLOCK_INPUT;
7504 XSETWINDOW (window, w);
7505 if (EQ (window, dpyinfo->mouse_face_window))
7506 clear_mouse_face (dpyinfo);
7507 UNBLOCK_INPUT;
7508 }
7509
7510
7511 /* Just discard the mouse face information for frame F, if any.
7512 This is used when the size of F is changed. */
7513
7514 void
7515 cancel_mouse_face (f)
7516 FRAME_PTR f;
7517 {
7518 Lisp_Object window;
7519 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7520
7521 window = dpyinfo->mouse_face_window;
7522 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7523 {
7524 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7525 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7526 dpyinfo->mouse_face_window = Qnil;
7527 }
7528 }
7529
7530 \f
7531 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
7532
7533
7534 /* Try to determine frame pixel position and size of the glyph under
7535 frame pixel coordinates X/Y on frame F . Return the position and
7536 size in *RECT. Value is non-zero if we could compute these
7537 values. */
7538
7539 static int
7540 glyph_rect (f, x, y, rect)
7541 struct frame *f;
7542 int x, y;
7543 XRectangle *rect;
7544 {
7545 Lisp_Object window;
7546 int part, found = 0;
7547
7548 window = window_from_coordinates (f, x, y, &part, 0);
7549 if (!NILP (window))
7550 {
7551 struct window *w = XWINDOW (window);
7552 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7553 struct glyph_row *end = r + w->current_matrix->nrows - 1;
7554 int area;
7555
7556 frame_to_window_pixel_xy (w, &x, &y);
7557
7558 for (; !found && r < end && r->enabled_p; ++r)
7559 if (r->y >= y)
7560 {
7561 struct glyph *g = r->glyphs[TEXT_AREA];
7562 struct glyph *end = g + r->used[TEXT_AREA];
7563 int gx;
7564
7565 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
7566 if (gx >= x)
7567 {
7568 rect->width = g->pixel_width;
7569 rect->height = r->height;
7570 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
7571 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
7572 found = 1;
7573 }
7574 }
7575 }
7576
7577 return found;
7578 }
7579
7580
7581 /* Return the current position of the mouse.
7582 *FP should be a frame which indicates which display to ask about.
7583
7584 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
7585 and *PART to the frame, window, and scroll bar part that the mouse
7586 is over. Set *X and *Y to the portion and whole of the mouse's
7587 position on the scroll bar.
7588
7589 If the mouse movement started elsewhere, set *FP to the frame the
7590 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
7591 the mouse is over.
7592
7593 Set *TIME to the server time-stamp for the time at which the mouse
7594 was at this position.
7595
7596 Don't store anything if we don't have a valid set of values to report.
7597
7598 This clears the mouse_moved flag, so we can wait for the next mouse
7599 movement. */
7600
7601 static void
7602 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7603 FRAME_PTR *fp;
7604 int insist;
7605 Lisp_Object *bar_window;
7606 enum scroll_bar_part *part;
7607 Lisp_Object *x, *y;
7608 unsigned long *time;
7609 {
7610 FRAME_PTR f1;
7611
7612 BLOCK_INPUT;
7613
7614 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7615 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7616 else
7617 {
7618 Window root;
7619 int root_x, root_y;
7620
7621 Window dummy_window;
7622 int dummy;
7623
7624 Lisp_Object frame, tail;
7625
7626 /* Clear the mouse-moved flag for every frame on this display. */
7627 FOR_EACH_FRAME (tail, frame)
7628 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7629 XFRAME (frame)->mouse_moved = 0;
7630
7631 last_mouse_scroll_bar = Qnil;
7632
7633 /* Figure out which root window we're on. */
7634 XQueryPointer (FRAME_X_DISPLAY (*fp),
7635 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
7636
7637 /* The root window which contains the pointer. */
7638 &root,
7639
7640 /* Trash which we can't trust if the pointer is on
7641 a different screen. */
7642 &dummy_window,
7643
7644 /* The position on that root window. */
7645 &root_x, &root_y,
7646
7647 /* More trash we can't trust. */
7648 &dummy, &dummy,
7649
7650 /* Modifier keys and pointer buttons, about which
7651 we don't care. */
7652 (unsigned int *) &dummy);
7653
7654 /* Now we have a position on the root; find the innermost window
7655 containing the pointer. */
7656 {
7657 Window win, child;
7658 int win_x, win_y;
7659 int parent_x = 0, parent_y = 0;
7660 int count;
7661
7662 win = root;
7663
7664 /* XTranslateCoordinates can get errors if the window
7665 structure is changing at the same time this function
7666 is running. So at least we must not crash from them. */
7667
7668 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
7669
7670 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
7671 && FRAME_LIVE_P (last_mouse_frame))
7672 {
7673 /* If mouse was grabbed on a frame, give coords for that frame
7674 even if the mouse is now outside it. */
7675 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7676
7677 /* From-window, to-window. */
7678 root, FRAME_X_WINDOW (last_mouse_frame),
7679
7680 /* From-position, to-position. */
7681 root_x, root_y, &win_x, &win_y,
7682
7683 /* Child of win. */
7684 &child);
7685 f1 = last_mouse_frame;
7686 }
7687 else
7688 {
7689 while (1)
7690 {
7691 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7692
7693 /* From-window, to-window. */
7694 root, win,
7695
7696 /* From-position, to-position. */
7697 root_x, root_y, &win_x, &win_y,
7698
7699 /* Child of win. */
7700 &child);
7701
7702 if (child == None || child == win)
7703 break;
7704
7705 win = child;
7706 parent_x = win_x;
7707 parent_y = win_y;
7708 }
7709
7710 /* Now we know that:
7711 win is the innermost window containing the pointer
7712 (XTC says it has no child containing the pointer),
7713 win_x and win_y are the pointer's position in it
7714 (XTC did this the last time through), and
7715 parent_x and parent_y are the pointer's position in win's parent.
7716 (They are what win_x and win_y were when win was child.
7717 If win is the root window, it has no parent, and
7718 parent_{x,y} are invalid, but that's okay, because we'll
7719 never use them in that case.) */
7720
7721 /* Is win one of our frames? */
7722 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
7723
7724 #ifdef USE_X_TOOLKIT
7725 /* If we end up with the menu bar window, say it's not
7726 on the frame. */
7727 if (f1 != NULL
7728 && f1->output_data.x->menubar_widget
7729 && win == XtWindow (f1->output_data.x->menubar_widget))
7730 f1 = NULL;
7731 #endif /* USE_X_TOOLKIT */
7732 }
7733
7734 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7735 f1 = 0;
7736
7737 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
7738
7739 /* If not, is it one of our scroll bars? */
7740 if (! f1)
7741 {
7742 struct scroll_bar *bar = x_window_to_scroll_bar (win);
7743
7744 if (bar)
7745 {
7746 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7747 win_x = parent_x;
7748 win_y = parent_y;
7749 }
7750 }
7751
7752 if (f1 == 0 && insist > 0)
7753 f1 = SELECTED_FRAME ();
7754
7755 if (f1)
7756 {
7757 /* Ok, we found a frame. Store all the values.
7758 last_mouse_glyph is a rectangle used to reduce the
7759 generation of mouse events. To not miss any motion
7760 events, we must divide the frame into rectangles of the
7761 size of the smallest character that could be displayed
7762 on it, i.e. into the same rectangles that matrices on
7763 the frame are divided into. */
7764
7765 int width, height, gx, gy;
7766 XRectangle rect;
7767
7768 if (glyph_rect (f1, win_x, win_y, &rect))
7769 last_mouse_glyph = rect;
7770 else
7771 {
7772 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7773 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7774 gx = win_x;
7775 gy = win_y;
7776
7777 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7778 round down even for negative values. */
7779 if (gx < 0)
7780 gx -= width - 1;
7781 if (gy < 0)
7782 gy -= height - 1;
7783 gx = (gx + width - 1) / width * width;
7784 gy = (gy + height - 1) / height * height;
7785
7786 last_mouse_glyph.width = width;
7787 last_mouse_glyph.height = height;
7788 last_mouse_glyph.x = gx;
7789 last_mouse_glyph.y = gy;
7790 }
7791
7792 *bar_window = Qnil;
7793 *part = 0;
7794 *fp = f1;
7795 XSETINT (*x, win_x);
7796 XSETINT (*y, win_y);
7797 *time = last_mouse_movement_time;
7798 }
7799 }
7800 }
7801
7802 UNBLOCK_INPUT;
7803 }
7804
7805
7806 #ifdef USE_X_TOOLKIT
7807
7808 /* Atimer callback function for TIMER. Called every 0.1s to process
7809 Xt timeouts, if needed. We must avoid calling XtAppPending as
7810 much as possible because that function does an implicit XFlush
7811 that slows us down. */
7812
7813 static void
7814 x_process_timeouts (timer)
7815 struct atimer *timer;
7816 {
7817 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7818 {
7819 BLOCK_INPUT;
7820 while (XtAppPending (Xt_app_con) & XtIMTimer)
7821 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7822 UNBLOCK_INPUT;
7823 }
7824 }
7825
7826 #endif /* USE_X_TOOLKIT */
7827
7828 \f
7829 /* Scroll bar support. */
7830
7831 /* Given an X window ID, find the struct scroll_bar which manages it.
7832 This can be called in GC, so we have to make sure to strip off mark
7833 bits. */
7834
7835 static struct scroll_bar *
7836 x_window_to_scroll_bar (window_id)
7837 Window window_id;
7838 {
7839 Lisp_Object tail;
7840
7841 for (tail = Vframe_list;
7842 XGCTYPE (tail) == Lisp_Cons;
7843 tail = XCDR (tail))
7844 {
7845 Lisp_Object frame, bar, condemned;
7846
7847 frame = XCAR (tail);
7848 /* All elements of Vframe_list should be frames. */
7849 if (! GC_FRAMEP (frame))
7850 abort ();
7851
7852 /* Scan this frame's scroll bar list for a scroll bar with the
7853 right window ID. */
7854 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7855 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7856 /* This trick allows us to search both the ordinary and
7857 condemned scroll bar lists with one loop. */
7858 ! GC_NILP (bar) || (bar = condemned,
7859 condemned = Qnil,
7860 ! GC_NILP (bar));
7861 bar = XSCROLL_BAR (bar)->next)
7862 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7863 return XSCROLL_BAR (bar);
7864 }
7865
7866 return 0;
7867 }
7868
7869
7870 \f
7871 /************************************************************************
7872 Toolkit scroll bars
7873 ************************************************************************/
7874
7875 #ifdef USE_TOOLKIT_SCROLL_BARS
7876
7877 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7878 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7879 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7880 struct scroll_bar *));
7881 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7882 int, int, int));
7883
7884
7885 /* Id of action hook installed for scroll bars. */
7886
7887 static XtActionHookId action_hook_id;
7888
7889 /* Lisp window being scrolled. Set when starting to interact with
7890 a toolkit scroll bar, reset to nil when ending the interaction. */
7891
7892 static Lisp_Object window_being_scrolled;
7893
7894 /* Last scroll bar part sent in xm_scroll_callback. */
7895
7896 static int last_scroll_bar_part;
7897
7898 /* Whether this is an Xaw with arrow-scrollbars. This should imply
7899 that movements of 1/20 of the screen size are mapped to up/down. */
7900
7901 static Boolean xaw3d_arrow_scroll;
7902
7903 /* Whether the drag scrolling maintains the mouse at the top of the
7904 thumb. If not, resizing the thumb needs to be done more carefully
7905 to avoid jerkyness. */
7906
7907 static Boolean xaw3d_pick_top;
7908
7909
7910 /* Action hook installed via XtAppAddActionHook when toolkit scroll
7911 bars are used.. The hook is responsible for detecting when
7912 the user ends an interaction with the scroll bar, and generates
7913 a `end-scroll' scroll_bar_click' event if so. */
7914
7915 static void
7916 xt_action_hook (widget, client_data, action_name, event, params,
7917 num_params)
7918 Widget widget;
7919 XtPointer client_data;
7920 String action_name;
7921 XEvent *event;
7922 String *params;
7923 Cardinal *num_params;
7924 {
7925 int scroll_bar_p;
7926 char *end_action;
7927
7928 #ifdef USE_MOTIF
7929 scroll_bar_p = XmIsScrollBar (widget);
7930 end_action = "Release";
7931 #else /* !USE_MOTIF i.e. use Xaw */
7932 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7933 end_action = "EndScroll";
7934 #endif /* USE_MOTIF */
7935
7936 if (scroll_bar_p
7937 && strcmp (action_name, end_action) == 0
7938 && WINDOWP (window_being_scrolled))
7939 {
7940 struct window *w;
7941
7942 x_send_scroll_bar_event (window_being_scrolled,
7943 scroll_bar_end_scroll, 0, 0);
7944 w = XWINDOW (window_being_scrolled);
7945 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7946 window_being_scrolled = Qnil;
7947 last_scroll_bar_part = -1;
7948
7949 /* Xt timeouts no longer needed. */
7950 toolkit_scroll_bar_interaction = 0;
7951 }
7952 }
7953
7954 /* A vector of windows used for communication between
7955 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
7956
7957 static struct window **scroll_bar_windows;
7958 static int scroll_bar_windows_size;
7959
7960
7961 /* Send a client message with message type Xatom_Scrollbar for a
7962 scroll action to the frame of WINDOW. PART is a value identifying
7963 the part of the scroll bar that was clicked on. PORTION is the
7964 amount to scroll of a whole of WHOLE. */
7965
7966 static void
7967 x_send_scroll_bar_event (window, part, portion, whole)
7968 Lisp_Object window;
7969 int part, portion, whole;
7970 {
7971 XEvent event;
7972 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7973 struct window *w = XWINDOW (window);
7974 struct frame *f = XFRAME (w->frame);
7975 int i;
7976
7977 BLOCK_INPUT;
7978
7979 /* Construct a ClientMessage event to send to the frame. */
7980 ev->type = ClientMessage;
7981 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7982 ev->display = FRAME_X_DISPLAY (f);
7983 ev->window = FRAME_X_WINDOW (f);
7984 ev->format = 32;
7985
7986 /* We can only transfer 32 bits in the XClientMessageEvent, which is
7987 not enough to store a pointer or Lisp_Object on a 64 bit system.
7988 So, store the window in scroll_bar_windows and pass the index
7989 into that array in the event. */
7990 for (i = 0; i < scroll_bar_windows_size; ++i)
7991 if (scroll_bar_windows[i] == NULL)
7992 break;
7993
7994 if (i == scroll_bar_windows_size)
7995 {
7996 int new_size = max (10, 2 * scroll_bar_windows_size);
7997 size_t nbytes = new_size * sizeof *scroll_bar_windows;
7998 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
7999
8000 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
8001 nbytes);
8002 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
8003 scroll_bar_windows_size = new_size;
8004 }
8005
8006 scroll_bar_windows[i] = w;
8007 ev->data.l[0] = (long) i;
8008 ev->data.l[1] = (long) part;
8009 ev->data.l[2] = (long) 0;
8010 ev->data.l[3] = (long) portion;
8011 ev->data.l[4] = (long) whole;
8012
8013 /* Make Xt timeouts work while the scroll bar is active. */
8014 toolkit_scroll_bar_interaction = 1;
8015
8016 /* Setting the event mask to zero means that the message will
8017 be sent to the client that created the window, and if that
8018 window no longer exists, no event will be sent. */
8019 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
8020 UNBLOCK_INPUT;
8021 }
8022
8023
8024 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
8025 in *IEVENT. */
8026
8027 static void
8028 x_scroll_bar_to_input_event (event, ievent)
8029 XEvent *event;
8030 struct input_event *ievent;
8031 {
8032 XClientMessageEvent *ev = (XClientMessageEvent *) event;
8033 Lisp_Object window;
8034 struct frame *f;
8035 struct window *w;
8036
8037 w = scroll_bar_windows[ev->data.l[0]];
8038 scroll_bar_windows[ev->data.l[0]] = NULL;
8039
8040 XSETWINDOW (window, w);
8041 f = XFRAME (w->frame);
8042
8043 ievent->kind = scroll_bar_click;
8044 ievent->frame_or_window = window;
8045 ievent->arg = Qnil;
8046 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
8047 ievent->part = ev->data.l[1];
8048 ievent->code = ev->data.l[2];
8049 ievent->x = make_number ((int) ev->data.l[3]);
8050 ievent->y = make_number ((int) ev->data.l[4]);
8051 ievent->modifiers = 0;
8052 }
8053
8054
8055 #ifdef USE_MOTIF
8056
8057 /* Minimum and maximum values used for Motif scroll bars. */
8058
8059 #define XM_SB_MIN 1
8060 #define XM_SB_MAX 10000000
8061 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
8062
8063
8064 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
8065 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
8066 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
8067
8068 static void
8069 xm_scroll_callback (widget, client_data, call_data)
8070 Widget widget;
8071 XtPointer client_data, call_data;
8072 {
8073 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8074 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
8075 double percent;
8076 int part = -1, whole = 0, portion = 0;
8077
8078 switch (cs->reason)
8079 {
8080 case XmCR_DECREMENT:
8081 bar->dragging = Qnil;
8082 part = scroll_bar_up_arrow;
8083 break;
8084
8085 case XmCR_INCREMENT:
8086 bar->dragging = Qnil;
8087 part = scroll_bar_down_arrow;
8088 break;
8089
8090 case XmCR_PAGE_DECREMENT:
8091 bar->dragging = Qnil;
8092 part = scroll_bar_above_handle;
8093 break;
8094
8095 case XmCR_PAGE_INCREMENT:
8096 bar->dragging = Qnil;
8097 part = scroll_bar_below_handle;
8098 break;
8099
8100 case XmCR_TO_TOP:
8101 bar->dragging = Qnil;
8102 part = scroll_bar_to_top;
8103 break;
8104
8105 case XmCR_TO_BOTTOM:
8106 bar->dragging = Qnil;
8107 part = scroll_bar_to_bottom;
8108 break;
8109
8110 case XmCR_DRAG:
8111 {
8112 int slider_size;
8113 int dragging_down_p = (INTEGERP (bar->dragging)
8114 && XINT (bar->dragging) <= cs->value);
8115
8116 /* Get the slider size. */
8117 BLOCK_INPUT;
8118 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
8119 UNBLOCK_INPUT;
8120
8121 /* At the max position of the scroll bar, do a line-wise
8122 movement. Without doing anything, the LessTif scroll bar
8123 calls us with the same cs->value again and again. If we
8124 want to make sure that we can reach the end of the buffer,
8125 we have to do something.
8126
8127 Implementation note: setting bar->dragging always to
8128 cs->value gives a smoother movement at the max position.
8129 Setting it to nil when doing line-wise movement gives
8130 a better slider behavior. */
8131
8132 if (cs->value + slider_size == XM_SB_MAX
8133 || (dragging_down_p
8134 && last_scroll_bar_part == scroll_bar_down_arrow))
8135 {
8136 part = scroll_bar_down_arrow;
8137 bar->dragging = Qnil;
8138 }
8139 else
8140 {
8141 whole = XM_SB_RANGE;
8142 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
8143 part = scroll_bar_handle;
8144 bar->dragging = make_number (cs->value);
8145 }
8146 }
8147 break;
8148
8149 case XmCR_VALUE_CHANGED:
8150 break;
8151 };
8152
8153 if (part >= 0)
8154 {
8155 window_being_scrolled = bar->window;
8156 last_scroll_bar_part = part;
8157 x_send_scroll_bar_event (bar->window, part, portion, whole);
8158 }
8159 }
8160
8161
8162 #else /* !USE_MOTIF, i.e. Xaw. */
8163
8164
8165 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
8166 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8167 scroll bar struct. CALL_DATA is a pointer to a float saying where
8168 the thumb is. */
8169
8170 static void
8171 xaw_jump_callback (widget, client_data, call_data)
8172 Widget widget;
8173 XtPointer client_data, call_data;
8174 {
8175 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8176 float top = *(float *) call_data;
8177 float shown;
8178 int whole, portion, height;
8179 int part;
8180
8181 /* Get the size of the thumb, a value between 0 and 1. */
8182 BLOCK_INPUT;
8183 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
8184 UNBLOCK_INPUT;
8185
8186 whole = 10000000;
8187 portion = shown < 1 ? top * whole : 0;
8188
8189 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8190 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8191 the bottom, so we force the scrolling whenever we see that we're
8192 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8193 we try to ensure that we always stay two pixels away from the
8194 bottom). */
8195 part = scroll_bar_down_arrow;
8196 else
8197 part = scroll_bar_handle;
8198
8199 window_being_scrolled = bar->window;
8200 bar->dragging = make_number (portion);
8201 last_scroll_bar_part = part;
8202 x_send_scroll_bar_event (bar->window, part, portion, whole);
8203 }
8204
8205
8206 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
8207 i.e. line or page up or down. WIDGET is the Xaw scroll bar
8208 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8209 the scroll bar. CALL_DATA is an integer specifying the action that
8210 has taken place. It's magnitude is in the range 0..height of the
8211 scroll bar. Negative values mean scroll towards buffer start.
8212 Values < height of scroll bar mean line-wise movement. */
8213
8214 static void
8215 xaw_scroll_callback (widget, client_data, call_data)
8216 Widget widget;
8217 XtPointer client_data, call_data;
8218 {
8219 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8220 int position = (int) call_data;
8221 Dimension height;
8222 int part;
8223
8224 /* Get the height of the scroll bar. */
8225 BLOCK_INPUT;
8226 XtVaGetValues (widget, XtNheight, &height, NULL);
8227 UNBLOCK_INPUT;
8228
8229 if (abs (position) >= height)
8230 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8231
8232 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8233 it maps line-movement to call_data = max(5, height/20). */
8234 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8235 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
8236 else
8237 part = scroll_bar_move_ratio;
8238
8239 window_being_scrolled = bar->window;
8240 bar->dragging = Qnil;
8241 last_scroll_bar_part = part;
8242 x_send_scroll_bar_event (bar->window, part, position, height);
8243 }
8244
8245
8246 #endif /* not USE_MOTIF */
8247
8248
8249 /* Create the widget for scroll bar BAR on frame F. Record the widget
8250 and X window of the scroll bar in BAR. */
8251
8252 static void
8253 x_create_toolkit_scroll_bar (f, bar)
8254 struct frame *f;
8255 struct scroll_bar *bar;
8256 {
8257 Window xwindow;
8258 Widget widget;
8259 Arg av[20];
8260 int ac = 0;
8261 char *scroll_bar_name = "verticalScrollBar";
8262 unsigned long pixel;
8263
8264 BLOCK_INPUT;
8265
8266 #ifdef USE_MOTIF
8267 /* LessTif 0.85, problems:
8268
8269 1. When the mouse if over the scroll bar, the scroll bar will
8270 get keyboard events. I didn't find a way to turn this off.
8271
8272 2. Do we have to explicitly set the cursor to get an arrow
8273 cursor (see below)? */
8274
8275 /* Set resources. Create the widget. */
8276 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8277 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8278 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8279 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8280 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8281 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8282 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8283
8284 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8285 if (pixel != -1)
8286 {
8287 XtSetArg (av[ac], XmNforeground, pixel);
8288 ++ac;
8289 }
8290
8291 pixel = f->output_data.x->scroll_bar_background_pixel;
8292 if (pixel != -1)
8293 {
8294 XtSetArg (av[ac], XmNbackground, pixel);
8295 ++ac;
8296 }
8297
8298 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8299 scroll_bar_name, av, ac);
8300
8301 /* Add one callback for everything that can happen. */
8302 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8303 (XtPointer) bar);
8304 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8305 (XtPointer) bar);
8306 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8307 (XtPointer) bar);
8308 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8309 (XtPointer) bar);
8310 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8311 (XtPointer) bar);
8312 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8313 (XtPointer) bar);
8314 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8315 (XtPointer) bar);
8316
8317 /* Realize the widget. Only after that is the X window created. */
8318 XtRealizeWidget (widget);
8319
8320 /* Set the cursor to an arrow. I didn't find a resource to do that.
8321 And I'm wondering why it hasn't an arrow cursor by default. */
8322 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8323 f->output_data.x->nontext_cursor);
8324
8325 #else /* !USE_MOTIF i.e. use Xaw */
8326
8327 /* Set resources. Create the widget. The background of the
8328 Xaw3d scroll bar widget is a little bit light for my taste.
8329 We don't alter it here to let users change it according
8330 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8331 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8332 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
8333 /* For smoother scrolling with Xaw3d -sm */
8334 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8335 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
8336
8337 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8338 if (pixel != -1)
8339 {
8340 XtSetArg (av[ac], XtNforeground, pixel);
8341 ++ac;
8342 }
8343
8344 pixel = f->output_data.x->scroll_bar_background_pixel;
8345 if (pixel != -1)
8346 {
8347 XtSetArg (av[ac], XtNbackground, pixel);
8348 ++ac;
8349 }
8350
8351 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8352 f->output_data.x->edit_widget, av, ac);
8353
8354 {
8355 char *initial = "";
8356 char *val = initial;
8357 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8358 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8359 if (val == initial)
8360 { /* ARROW_SCROLL */
8361 xaw3d_arrow_scroll = True;
8362 /* Isn't that just a personal preference ? -sm */
8363 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8364 }
8365 }
8366
8367 /* Define callbacks. */
8368 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8369 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
8370 (XtPointer) bar);
8371
8372 /* Realize the widget. Only after that is the X window created. */
8373 XtRealizeWidget (widget);
8374
8375 #endif /* !USE_MOTIF */
8376
8377 /* Install an action hook that let's us detect when the user
8378 finishes interacting with a scroll bar. */
8379 if (action_hook_id == 0)
8380 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8381
8382 /* Remember X window and widget in the scroll bar vector. */
8383 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8384 xwindow = XtWindow (widget);
8385 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8386
8387 UNBLOCK_INPUT;
8388 }
8389
8390
8391 /* Set the thumb size and position of scroll bar BAR. We are currently
8392 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8393
8394 static void
8395 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8396 struct scroll_bar *bar;
8397 int portion, position, whole;
8398 {
8399 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8400 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8401 float top, shown;
8402
8403 if (whole == 0)
8404 top = 0, shown = 1;
8405 else
8406 {
8407 top = (float) position / whole;
8408 shown = (float) portion / whole;
8409 }
8410
8411 BLOCK_INPUT;
8412
8413 #ifdef USE_MOTIF
8414 {
8415 int size, value;
8416 Boolean arrow1_selected, arrow2_selected;
8417 unsigned char flags;
8418 XmScrollBarWidget sb;
8419
8420 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
8421 is the scroll bar's maximum and MIN is the scroll bar's minimum
8422 value. */
8423 size = shown * XM_SB_RANGE;
8424 size = min (size, XM_SB_RANGE);
8425 size = max (size, 1);
8426
8427 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8428 value = top * XM_SB_RANGE;
8429 value = min (value, XM_SB_MAX - size);
8430 value = max (value, XM_SB_MIN);
8431
8432 /* LessTif: Calling XmScrollBarSetValues after an increment or
8433 decrement turns off auto-repeat LessTif-internally. This can
8434 be seen in ScrollBar.c which resets Arrow1Selected and
8435 Arrow2Selected. It also sets internal flags so that LessTif
8436 believes the mouse is in the slider. We either have to change
8437 our code, or work around that by accessing private data. */
8438
8439 sb = (XmScrollBarWidget) widget;
8440 arrow1_selected = sb->scrollBar.arrow1_selected;
8441 arrow2_selected = sb->scrollBar.arrow2_selected;
8442 flags = sb->scrollBar.flags;
8443
8444 if (NILP (bar->dragging))
8445 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8446 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8447 /* This has the negative side effect that the slider value is
8448 not what it would be if we scrolled here using line-wise or
8449 page-wise movement. */
8450 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8451 else
8452 {
8453 /* If currently dragging, only update the slider size.
8454 This reduces flicker effects. */
8455 int old_value, old_size, increment, page_increment;
8456
8457 XmScrollBarGetValues (widget, &old_value, &old_size,
8458 &increment, &page_increment);
8459 XmScrollBarSetValues (widget, old_value,
8460 min (size, XM_SB_RANGE - old_value),
8461 0, 0, False);
8462 }
8463
8464 sb->scrollBar.arrow1_selected = arrow1_selected;
8465 sb->scrollBar.arrow2_selected = arrow2_selected;
8466 sb->scrollBar.flags = flags;
8467 }
8468 #else /* !USE_MOTIF i.e. use Xaw */
8469 {
8470 float old_top, old_shown;
8471 Dimension height;
8472 XtVaGetValues (widget,
8473 XtNtopOfThumb, &old_top,
8474 XtNshown, &old_shown,
8475 XtNheight, &height,
8476 NULL);
8477
8478 /* Massage the top+shown values. */
8479 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8480 top = max (0, min (1, top));
8481 else
8482 top = old_top;
8483 /* Keep two pixels available for moving the thumb down. */
8484 shown = max (0, min (1 - top - (2.0 / height), shown));
8485
8486 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8487 check that your system's configuration file contains a define
8488 for `NARROWPROTO'. See s/freebsd.h for an example. */
8489 if (top != old_top || shown != old_shown)
8490 {
8491 if (NILP (bar->dragging))
8492 XawScrollbarSetThumb (widget, top, shown);
8493 else
8494 {
8495 #ifdef HAVE_XAW3D
8496 ScrollbarWidget sb = (ScrollbarWidget) widget;
8497 int scroll_mode = 0;
8498
8499 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8500 if (xaw3d_arrow_scroll)
8501 {
8502 /* Xaw3d stupidly ignores resize requests while dragging
8503 so we have to make it believe it's not in dragging mode. */
8504 scroll_mode = sb->scrollbar.scroll_mode;
8505 if (scroll_mode == 2)
8506 sb->scrollbar.scroll_mode = 0;
8507 }
8508 #endif
8509 /* Try to make the scrolling a tad smoother. */
8510 if (!xaw3d_pick_top)
8511 shown = min (shown, old_shown);
8512
8513 XawScrollbarSetThumb (widget, top, shown);
8514
8515 #ifdef HAVE_XAW3D
8516 if (xaw3d_arrow_scroll && scroll_mode == 2)
8517 sb->scrollbar.scroll_mode = scroll_mode;
8518 #endif
8519 }
8520 }
8521 }
8522 #endif /* !USE_MOTIF */
8523
8524 UNBLOCK_INPUT;
8525 }
8526
8527 #endif /* USE_TOOLKIT_SCROLL_BARS */
8528
8529
8530 \f
8531 /************************************************************************
8532 Scroll bars, general
8533 ************************************************************************/
8534
8535 /* Create a scroll bar and return the scroll bar vector for it. W is
8536 the Emacs window on which to create the scroll bar. TOP, LEFT,
8537 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8538 scroll bar. */
8539
8540 static struct scroll_bar *
8541 x_scroll_bar_create (w, top, left, width, height)
8542 struct window *w;
8543 int top, left, width, height;
8544 {
8545 struct frame *f = XFRAME (w->frame);
8546 struct scroll_bar *bar
8547 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8548
8549 BLOCK_INPUT;
8550
8551 #ifdef USE_TOOLKIT_SCROLL_BARS
8552 x_create_toolkit_scroll_bar (f, bar);
8553 #else /* not USE_TOOLKIT_SCROLL_BARS */
8554 {
8555 XSetWindowAttributes a;
8556 unsigned long mask;
8557 Window window;
8558
8559 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8560 if (a.background_pixel == -1)
8561 a.background_pixel = f->output_data.x->background_pixel;
8562
8563 a.event_mask = (ButtonPressMask | ButtonReleaseMask
8564 | ButtonMotionMask | PointerMotionHintMask
8565 | ExposureMask);
8566 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
8567
8568 mask = (CWBackPixel | CWEventMask | CWCursor);
8569
8570 /* Clear the area of W that will serve as a scroll bar. This is
8571 for the case that a window has been split horizontally. In
8572 this case, no clear_frame is generated to reduce flickering. */
8573 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8574 left, top, width,
8575 window_box_height (w), False);
8576
8577 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8578 /* Position and size of scroll bar. */
8579 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8580 top,
8581 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8582 height,
8583 /* Border width, depth, class, and visual. */
8584 0,
8585 CopyFromParent,
8586 CopyFromParent,
8587 CopyFromParent,
8588 /* Attributes. */
8589 mask, &a);
8590 SET_SCROLL_BAR_X_WINDOW (bar, window);
8591 }
8592 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8593
8594 XSETWINDOW (bar->window, w);
8595 XSETINT (bar->top, top);
8596 XSETINT (bar->left, left);
8597 XSETINT (bar->width, width);
8598 XSETINT (bar->height, height);
8599 XSETINT (bar->start, 0);
8600 XSETINT (bar->end, 0);
8601 bar->dragging = Qnil;
8602
8603 /* Add bar to its frame's list of scroll bars. */
8604 bar->next = FRAME_SCROLL_BARS (f);
8605 bar->prev = Qnil;
8606 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8607 if (!NILP (bar->next))
8608 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8609
8610 /* Map the window/widget. */
8611 #ifdef USE_TOOLKIT_SCROLL_BARS
8612 {
8613 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8614 XtConfigureWidget (scroll_bar,
8615 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8616 top,
8617 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8618 max (height, 1), 0);
8619 XtMapWidget (scroll_bar);
8620 }
8621 #else /* not USE_TOOLKIT_SCROLL_BARS */
8622 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8623 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8624
8625 UNBLOCK_INPUT;
8626 return bar;
8627 }
8628
8629
8630 /* Draw BAR's handle in the proper position.
8631
8632 If the handle is already drawn from START to END, don't bother
8633 redrawing it, unless REBUILD is non-zero; in that case, always
8634 redraw it. (REBUILD is handy for drawing the handle after expose
8635 events.)
8636
8637 Normally, we want to constrain the start and end of the handle to
8638 fit inside its rectangle, but if the user is dragging the scroll
8639 bar handle, we want to let them drag it down all the way, so that
8640 the bar's top is as far down as it goes; otherwise, there's no way
8641 to move to the very end of the buffer. */
8642
8643 #ifndef USE_TOOLKIT_SCROLL_BARS
8644
8645 static void
8646 x_scroll_bar_set_handle (bar, start, end, rebuild)
8647 struct scroll_bar *bar;
8648 int start, end;
8649 int rebuild;
8650 {
8651 int dragging = ! NILP (bar->dragging);
8652 Window w = SCROLL_BAR_X_WINDOW (bar);
8653 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8654 GC gc = f->output_data.x->normal_gc;
8655
8656 /* If the display is already accurate, do nothing. */
8657 if (! rebuild
8658 && start == XINT (bar->start)
8659 && end == XINT (bar->end))
8660 return;
8661
8662 BLOCK_INPUT;
8663
8664 {
8665 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8666 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8667 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8668
8669 /* Make sure the values are reasonable, and try to preserve
8670 the distance between start and end. */
8671 {
8672 int length = end - start;
8673
8674 if (start < 0)
8675 start = 0;
8676 else if (start > top_range)
8677 start = top_range;
8678 end = start + length;
8679
8680 if (end < start)
8681 end = start;
8682 else if (end > top_range && ! dragging)
8683 end = top_range;
8684 }
8685
8686 /* Store the adjusted setting in the scroll bar. */
8687 XSETINT (bar->start, start);
8688 XSETINT (bar->end, end);
8689
8690 /* Clip the end position, just for display. */
8691 if (end > top_range)
8692 end = top_range;
8693
8694 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
8695 below top positions, to make sure the handle is always at least
8696 that many pixels tall. */
8697 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
8698
8699 /* Draw the empty space above the handle. Note that we can't clear
8700 zero-height areas; that means "clear to end of window." */
8701 if (0 < start)
8702 x_clear_area (FRAME_X_DISPLAY (f), w,
8703 /* x, y, width, height, and exposures. */
8704 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8705 VERTICAL_SCROLL_BAR_TOP_BORDER,
8706 inside_width, start,
8707 False);
8708
8709 /* Change to proper foreground color if one is specified. */
8710 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8711 XSetForeground (FRAME_X_DISPLAY (f), gc,
8712 f->output_data.x->scroll_bar_foreground_pixel);
8713
8714 /* Draw the handle itself. */
8715 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
8716
8717 /* x, y, width, height */
8718 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8719 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
8720 inside_width, end - start);
8721
8722 /* Restore the foreground color of the GC if we changed it above. */
8723 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8724 XSetForeground (FRAME_X_DISPLAY (f), gc,
8725 f->output_data.x->foreground_pixel);
8726
8727 /* Draw the empty space below the handle. Note that we can't
8728 clear zero-height areas; that means "clear to end of window." */
8729 if (end < inside_height)
8730 x_clear_area (FRAME_X_DISPLAY (f), w,
8731 /* x, y, width, height, and exposures. */
8732 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8733 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
8734 inside_width, inside_height - end,
8735 False);
8736
8737 }
8738
8739 UNBLOCK_INPUT;
8740 }
8741
8742 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8743
8744 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8745 nil. */
8746
8747 static void
8748 x_scroll_bar_remove (bar)
8749 struct scroll_bar *bar;
8750 {
8751 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8752 BLOCK_INPUT;
8753
8754 #ifdef USE_TOOLKIT_SCROLL_BARS
8755 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
8756 #else
8757 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8758 #endif
8759
8760 /* Disassociate this scroll bar from its window. */
8761 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
8762
8763 UNBLOCK_INPUT;
8764 }
8765
8766
8767 /* Set the handle of the vertical scroll bar for WINDOW to indicate
8768 that we are displaying PORTION characters out of a total of WHOLE
8769 characters, starting at POSITION. If WINDOW has no scroll bar,
8770 create one. */
8771
8772 static void
8773 XTset_vertical_scroll_bar (w, portion, whole, position)
8774 struct window *w;
8775 int portion, whole, position;
8776 {
8777 struct frame *f = XFRAME (w->frame);
8778 struct scroll_bar *bar;
8779 int top, height, left, sb_left, width, sb_width;
8780 int window_x, window_y, window_width, window_height;
8781
8782 /* Get window dimensions. */
8783 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8784 top = window_y;
8785 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8786 height = window_height;
8787
8788 /* Compute the left edge of the scroll bar area. */
8789 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8790 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8791 else
8792 left = XFASTINT (w->left);
8793 left *= CANON_X_UNIT (f);
8794 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8795
8796 /* Compute the width of the scroll bar which might be less than
8797 the width of the area reserved for the scroll bar. */
8798 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8799 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
8800 else
8801 sb_width = width;
8802
8803 /* Compute the left edge of the scroll bar. */
8804 #ifdef USE_TOOLKIT_SCROLL_BARS
8805 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8806 sb_left = left + width - sb_width - (width - sb_width) / 2;
8807 else
8808 sb_left = left + (width - sb_width) / 2;
8809 #else
8810 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8811 sb_left = left + width - sb_width;
8812 else
8813 sb_left = left;
8814 #endif
8815
8816 /* Does the scroll bar exist yet? */
8817 if (NILP (w->vertical_scroll_bar))
8818 {
8819 BLOCK_INPUT;
8820 if (width && height)
8821 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8822 left, top, width, height, False);
8823 UNBLOCK_INPUT;
8824 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8825 }
8826 else
8827 {
8828 /* It may just need to be moved and resized. */
8829 unsigned int mask = 0;
8830
8831 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8832
8833 BLOCK_INPUT;
8834
8835 if (sb_left != XINT (bar->left))
8836 mask |= CWX;
8837 if (top != XINT (bar->top))
8838 mask |= CWY;
8839 if (sb_width != XINT (bar->width))
8840 mask |= CWWidth;
8841 if (height != XINT (bar->height))
8842 mask |= CWHeight;
8843
8844 #ifdef USE_TOOLKIT_SCROLL_BARS
8845
8846 /* Since toolkit scroll bars are smaller than the space reserved
8847 for them on the frame, we have to clear "under" them. */
8848 if (width && height)
8849 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8850 left, top, width, height, False);
8851
8852 /* Move/size the scroll bar widget. */
8853 if (mask)
8854 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
8855 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8856 top,
8857 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8858 max (height, 1), 0);
8859
8860 #else /* not USE_TOOLKIT_SCROLL_BARS */
8861
8862 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8863 {
8864 /* Clear areas not covered by the scroll bar. This makes sure a
8865 previous mode line display is cleared after C-x 2 C-x 1, for
8866 example. Non-toolkit scroll bars are as wide as the area
8867 reserved for scroll bars - trim at both sides. */
8868 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8869 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8870 height, False);
8871 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8872 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8873 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8874 height, False);
8875 }
8876
8877 /* Move/size the scroll bar window. */
8878 if (mask)
8879 {
8880 XWindowChanges wc;
8881
8882 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8883 wc.y = top;
8884 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8885 wc.height = height;
8886 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8887 mask, &wc);
8888 }
8889
8890 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8891
8892 /* Remember new settings. */
8893 XSETINT (bar->left, sb_left);
8894 XSETINT (bar->top, top);
8895 XSETINT (bar->width, sb_width);
8896 XSETINT (bar->height, height);
8897
8898 UNBLOCK_INPUT;
8899 }
8900
8901 #ifdef USE_TOOLKIT_SCROLL_BARS
8902 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8903 #else /* not USE_TOOLKIT_SCROLL_BARS */
8904 /* Set the scroll bar's current state, unless we're currently being
8905 dragged. */
8906 if (NILP (bar->dragging))
8907 {
8908 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
8909
8910 if (whole == 0)
8911 x_scroll_bar_set_handle (bar, 0, top_range, 0);
8912 else
8913 {
8914 int start = ((double) position * top_range) / whole;
8915 int end = ((double) (position + portion) * top_range) / whole;
8916 x_scroll_bar_set_handle (bar, start, end, 0);
8917 }
8918 }
8919 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8920
8921 XSETVECTOR (w->vertical_scroll_bar, bar);
8922 }
8923
8924
8925 /* The following three hooks are used when we're doing a thorough
8926 redisplay of the frame. We don't explicitly know which scroll bars
8927 are going to be deleted, because keeping track of when windows go
8928 away is a real pain - "Can you say set-window-configuration, boys
8929 and girls?" Instead, we just assert at the beginning of redisplay
8930 that *all* scroll bars are to be removed, and then save a scroll bar
8931 from the fiery pit when we actually redisplay its window. */
8932
8933 /* Arrange for all scroll bars on FRAME to be removed at the next call
8934 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
8935 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8936
8937 static void
8938 XTcondemn_scroll_bars (frame)
8939 FRAME_PTR frame;
8940 {
8941 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8942 while (! NILP (FRAME_SCROLL_BARS (frame)))
8943 {
8944 Lisp_Object bar;
8945 bar = FRAME_SCROLL_BARS (frame);
8946 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8947 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8948 XSCROLL_BAR (bar)->prev = Qnil;
8949 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8950 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8951 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8952 }
8953 }
8954
8955
8956 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
8957 Note that WINDOW isn't necessarily condemned at all. */
8958
8959 static void
8960 XTredeem_scroll_bar (window)
8961 struct window *window;
8962 {
8963 struct scroll_bar *bar;
8964 struct frame *f;
8965
8966 /* We can't redeem this window's scroll bar if it doesn't have one. */
8967 if (NILP (window->vertical_scroll_bar))
8968 abort ();
8969
8970 bar = XSCROLL_BAR (window->vertical_scroll_bar);
8971
8972 /* Unlink it from the condemned list. */
8973 f = XFRAME (WINDOW_FRAME (window));
8974 if (NILP (bar->prev))
8975 {
8976 /* If the prev pointer is nil, it must be the first in one of
8977 the lists. */
8978 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8979 /* It's not condemned. Everything's fine. */
8980 return;
8981 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8982 window->vertical_scroll_bar))
8983 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8984 else
8985 /* If its prev pointer is nil, it must be at the front of
8986 one or the other! */
8987 abort ();
8988 }
8989 else
8990 XSCROLL_BAR (bar->prev)->next = bar->next;
8991
8992 if (! NILP (bar->next))
8993 XSCROLL_BAR (bar->next)->prev = bar->prev;
8994
8995 bar->next = FRAME_SCROLL_BARS (f);
8996 bar->prev = Qnil;
8997 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8998 if (! NILP (bar->next))
8999 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9000 }
9001
9002 /* Remove all scroll bars on FRAME that haven't been saved since the
9003 last call to `*condemn_scroll_bars_hook'. */
9004
9005 static void
9006 XTjudge_scroll_bars (f)
9007 FRAME_PTR f;
9008 {
9009 Lisp_Object bar, next;
9010
9011 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
9012
9013 /* Clear out the condemned list now so we won't try to process any
9014 more events on the hapless scroll bars. */
9015 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
9016
9017 for (; ! NILP (bar); bar = next)
9018 {
9019 struct scroll_bar *b = XSCROLL_BAR (bar);
9020
9021 x_scroll_bar_remove (b);
9022
9023 next = b->next;
9024 b->next = b->prev = Qnil;
9025 }
9026
9027 /* Now there should be no references to the condemned scroll bars,
9028 and they should get garbage-collected. */
9029 }
9030
9031
9032 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
9033 is a no-op when using toolkit scroll bars.
9034
9035 This may be called from a signal handler, so we have to ignore GC
9036 mark bits. */
9037
9038 static void
9039 x_scroll_bar_expose (bar, event)
9040 struct scroll_bar *bar;
9041 XEvent *event;
9042 {
9043 #ifndef USE_TOOLKIT_SCROLL_BARS
9044
9045 Window w = SCROLL_BAR_X_WINDOW (bar);
9046 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9047 GC gc = f->output_data.x->normal_gc;
9048 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9049
9050 BLOCK_INPUT;
9051
9052 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
9053
9054 /* Draw a one-pixel border just inside the edges of the scroll bar. */
9055 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
9056
9057 /* x, y, width, height */
9058 0, 0,
9059 XINT (bar->width) - 1 - width_trim - width_trim,
9060 XINT (bar->height) - 1);
9061
9062 UNBLOCK_INPUT;
9063
9064 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9065 }
9066
9067 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
9068 is set to something other than no_event, it is enqueued.
9069
9070 This may be called from a signal handler, so we have to ignore GC
9071 mark bits. */
9072
9073 #ifndef USE_TOOLKIT_SCROLL_BARS
9074
9075 static void
9076 x_scroll_bar_handle_click (bar, event, emacs_event)
9077 struct scroll_bar *bar;
9078 XEvent *event;
9079 struct input_event *emacs_event;
9080 {
9081 if (! GC_WINDOWP (bar->window))
9082 abort ();
9083
9084 emacs_event->kind = scroll_bar_click;
9085 emacs_event->code = event->xbutton.button - Button1;
9086 emacs_event->modifiers
9087 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
9088 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
9089 event->xbutton.state)
9090 | (event->type == ButtonRelease
9091 ? up_modifier
9092 : down_modifier));
9093 emacs_event->frame_or_window = bar->window;
9094 emacs_event->arg = Qnil;
9095 emacs_event->timestamp = event->xbutton.time;
9096 {
9097 #if 0
9098 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9099 int internal_height
9100 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9101 #endif
9102 int top_range
9103 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9104 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
9105
9106 if (y < 0) y = 0;
9107 if (y > top_range) y = top_range;
9108
9109 if (y < XINT (bar->start))
9110 emacs_event->part = scroll_bar_above_handle;
9111 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9112 emacs_event->part = scroll_bar_handle;
9113 else
9114 emacs_event->part = scroll_bar_below_handle;
9115
9116 /* Just because the user has clicked on the handle doesn't mean
9117 they want to drag it. Lisp code needs to be able to decide
9118 whether or not we're dragging. */
9119 #if 0
9120 /* If the user has just clicked on the handle, record where they're
9121 holding it. */
9122 if (event->type == ButtonPress
9123 && emacs_event->part == scroll_bar_handle)
9124 XSETINT (bar->dragging, y - XINT (bar->start));
9125 #endif
9126
9127 /* If the user has released the handle, set it to its final position. */
9128 if (event->type == ButtonRelease
9129 && ! NILP (bar->dragging))
9130 {
9131 int new_start = y - XINT (bar->dragging);
9132 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9133
9134 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9135 bar->dragging = Qnil;
9136 }
9137
9138 /* Same deal here as the other #if 0. */
9139 #if 0
9140 /* Clicks on the handle are always reported as occurring at the top of
9141 the handle. */
9142 if (emacs_event->part == scroll_bar_handle)
9143 emacs_event->x = bar->start;
9144 else
9145 XSETINT (emacs_event->x, y);
9146 #else
9147 XSETINT (emacs_event->x, y);
9148 #endif
9149
9150 XSETINT (emacs_event->y, top_range);
9151 }
9152 }
9153
9154 /* Handle some mouse motion while someone is dragging the scroll bar.
9155
9156 This may be called from a signal handler, so we have to ignore GC
9157 mark bits. */
9158
9159 static void
9160 x_scroll_bar_note_movement (bar, event)
9161 struct scroll_bar *bar;
9162 XEvent *event;
9163 {
9164 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9165
9166 last_mouse_movement_time = event->xmotion.time;
9167
9168 f->mouse_moved = 1;
9169 XSETVECTOR (last_mouse_scroll_bar, bar);
9170
9171 /* If we're dragging the bar, display it. */
9172 if (! GC_NILP (bar->dragging))
9173 {
9174 /* Where should the handle be now? */
9175 int new_start = event->xmotion.y - XINT (bar->dragging);
9176
9177 if (new_start != XINT (bar->start))
9178 {
9179 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9180
9181 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9182 }
9183 }
9184 }
9185
9186 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9187
9188 /* Return information to the user about the current position of the mouse
9189 on the scroll bar. */
9190
9191 static void
9192 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9193 FRAME_PTR *fp;
9194 Lisp_Object *bar_window;
9195 enum scroll_bar_part *part;
9196 Lisp_Object *x, *y;
9197 unsigned long *time;
9198 {
9199 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
9200 Window w = SCROLL_BAR_X_WINDOW (bar);
9201 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9202 int win_x, win_y;
9203 Window dummy_window;
9204 int dummy_coord;
9205 unsigned int dummy_mask;
9206
9207 BLOCK_INPUT;
9208
9209 /* Get the mouse's position relative to the scroll bar window, and
9210 report that. */
9211 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
9212
9213 /* Root, child, root x and root y. */
9214 &dummy_window, &dummy_window,
9215 &dummy_coord, &dummy_coord,
9216
9217 /* Position relative to scroll bar. */
9218 &win_x, &win_y,
9219
9220 /* Mouse buttons and modifier keys. */
9221 &dummy_mask))
9222 ;
9223 else
9224 {
9225 #if 0
9226 int inside_height
9227 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9228 #endif
9229 int top_range
9230 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9231
9232 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9233
9234 if (! NILP (bar->dragging))
9235 win_y -= XINT (bar->dragging);
9236
9237 if (win_y < 0)
9238 win_y = 0;
9239 if (win_y > top_range)
9240 win_y = top_range;
9241
9242 *fp = f;
9243 *bar_window = bar->window;
9244
9245 if (! NILP (bar->dragging))
9246 *part = scroll_bar_handle;
9247 else if (win_y < XINT (bar->start))
9248 *part = scroll_bar_above_handle;
9249 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9250 *part = scroll_bar_handle;
9251 else
9252 *part = scroll_bar_below_handle;
9253
9254 XSETINT (*x, win_y);
9255 XSETINT (*y, top_range);
9256
9257 f->mouse_moved = 0;
9258 last_mouse_scroll_bar = Qnil;
9259 }
9260
9261 *time = last_mouse_movement_time;
9262
9263 UNBLOCK_INPUT;
9264 }
9265
9266
9267 /* The screen has been cleared so we may have changed foreground or
9268 background colors, and the scroll bars may need to be redrawn.
9269 Clear out the scroll bars, and ask for expose events, so we can
9270 redraw them. */
9271
9272 void
9273 x_scroll_bar_clear (f)
9274 FRAME_PTR f;
9275 {
9276 #ifndef USE_TOOLKIT_SCROLL_BARS
9277 Lisp_Object bar;
9278
9279 /* We can have scroll bars even if this is 0,
9280 if we just turned off scroll bar mode.
9281 But in that case we should not clear them. */
9282 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9283 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9284 bar = XSCROLL_BAR (bar)->next)
9285 XClearArea (FRAME_X_DISPLAY (f),
9286 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9287 0, 0, 0, 0, True);
9288 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9289 }
9290
9291 /* This processes Expose events from the menu-bar specific X event
9292 loop in xmenu.c. This allows to redisplay the frame if necessary
9293 when handling menu-bar or pop-up items. */
9294
9295 int
9296 process_expose_from_menu (event)
9297 XEvent event;
9298 {
9299 FRAME_PTR f;
9300 struct x_display_info *dpyinfo;
9301 int frame_exposed_p = 0;
9302
9303 BLOCK_INPUT;
9304
9305 dpyinfo = x_display_info_for_display (event.xexpose.display);
9306 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9307 if (f)
9308 {
9309 if (f->async_visible == 0)
9310 {
9311 f->async_visible = 1;
9312 f->async_iconified = 0;
9313 f->output_data.x->has_been_visible = 1;
9314 SET_FRAME_GARBAGED (f);
9315 }
9316 else
9317 {
9318 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9319 event.xexpose.x, event.xexpose.y,
9320 event.xexpose.width, event.xexpose.height);
9321 frame_exposed_p = 1;
9322 }
9323 }
9324 else
9325 {
9326 struct scroll_bar *bar
9327 = x_window_to_scroll_bar (event.xexpose.window);
9328
9329 if (bar)
9330 x_scroll_bar_expose (bar, &event);
9331 }
9332
9333 UNBLOCK_INPUT;
9334 return frame_exposed_p;
9335 }
9336 \f
9337 /* Define a queue to save up SelectionRequest events for later handling. */
9338
9339 struct selection_event_queue
9340 {
9341 XEvent event;
9342 struct selection_event_queue *next;
9343 };
9344
9345 static struct selection_event_queue *queue;
9346
9347 /* Nonzero means queue up certain events--don't process them yet. */
9348
9349 static int x_queue_selection_requests;
9350
9351 /* Queue up an X event *EVENT, to be processed later. */
9352
9353 static void
9354 x_queue_event (f, event)
9355 FRAME_PTR f;
9356 XEvent *event;
9357 {
9358 struct selection_event_queue *queue_tmp
9359 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
9360
9361 if (queue_tmp != NULL)
9362 {
9363 queue_tmp->event = *event;
9364 queue_tmp->next = queue;
9365 queue = queue_tmp;
9366 }
9367 }
9368
9369 /* Take all the queued events and put them back
9370 so that they get processed afresh. */
9371
9372 static void
9373 x_unqueue_events (display)
9374 Display *display;
9375 {
9376 while (queue != NULL)
9377 {
9378 struct selection_event_queue *queue_tmp = queue;
9379 XPutBackEvent (display, &queue_tmp->event);
9380 queue = queue_tmp->next;
9381 xfree ((char *)queue_tmp);
9382 }
9383 }
9384
9385 /* Start queuing SelectionRequest events. */
9386
9387 void
9388 x_start_queuing_selection_requests (display)
9389 Display *display;
9390 {
9391 x_queue_selection_requests++;
9392 }
9393
9394 /* Stop queuing SelectionRequest events. */
9395
9396 void
9397 x_stop_queuing_selection_requests (display)
9398 Display *display;
9399 {
9400 x_queue_selection_requests--;
9401 x_unqueue_events (display);
9402 }
9403 \f
9404 /* The main X event-reading loop - XTread_socket. */
9405
9406 /* Time stamp of enter window event. This is only used by XTread_socket,
9407 but we have to put it out here, since static variables within functions
9408 sometimes don't work. */
9409
9410 static Time enter_timestamp;
9411
9412 /* This holds the state XLookupString needs to implement dead keys
9413 and other tricks known as "compose processing". _X Window System_
9414 says that a portable program can't use this, but Stephen Gildea assures
9415 me that letting the compiler initialize it to zeros will work okay.
9416
9417 This must be defined outside of XTread_socket, for the same reasons
9418 given for enter_time stamp, above. */
9419
9420 static XComposeStatus compose_status;
9421
9422 /* Record the last 100 characters stored
9423 to help debug the loss-of-chars-during-GC problem. */
9424
9425 static int temp_index;
9426 static short temp_buffer[100];
9427
9428 /* Set this to nonzero to fake an "X I/O error"
9429 on a particular display. */
9430
9431 struct x_display_info *XTread_socket_fake_io_error;
9432
9433 /* When we find no input here, we occasionally do a no-op command
9434 to verify that the X server is still running and we can still talk with it.
9435 We try all the open displays, one by one.
9436 This variable is used for cycling thru the displays. */
9437
9438 static struct x_display_info *next_noop_dpyinfo;
9439
9440 #define SET_SAVED_MENU_EVENT(size) \
9441 do \
9442 { \
9443 if (f->output_data.x->saved_menu_event == 0) \
9444 f->output_data.x->saved_menu_event \
9445 = (XEvent *) xmalloc (sizeof (XEvent)); \
9446 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9447 if (numchars >= 1) \
9448 { \
9449 bufp->kind = menu_bar_activate_event; \
9450 XSETFRAME (bufp->frame_or_window, f); \
9451 bufp->arg = Qnil; \
9452 bufp++; \
9453 count++; \
9454 numchars--; \
9455 } \
9456 } \
9457 while (0)
9458
9459 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9460 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9461
9462 /* Read events coming from the X server.
9463 This routine is called by the SIGIO handler.
9464 We return as soon as there are no more events to be read.
9465
9466 Events representing keys are stored in buffer BUFP,
9467 which can hold up to NUMCHARS characters.
9468 We return the number of characters stored into the buffer,
9469 thus pretending to be `read'.
9470
9471 EXPECTED is nonzero if the caller knows input is available. */
9472
9473 int
9474 XTread_socket (sd, bufp, numchars, expected)
9475 register int sd;
9476 /* register */ struct input_event *bufp;
9477 /* register */ int numchars;
9478 int expected;
9479 {
9480 int count = 0;
9481 int nbytes = 0;
9482 XEvent event;
9483 struct frame *f;
9484 int event_found = 0;
9485 struct x_display_info *dpyinfo;
9486 struct coding_system coding;
9487
9488 if (interrupt_input_blocked)
9489 {
9490 interrupt_input_pending = 1;
9491 return -1;
9492 }
9493
9494 interrupt_input_pending = 0;
9495 BLOCK_INPUT;
9496
9497 /* So people can tell when we have read the available input. */
9498 input_signal_count++;
9499
9500 if (numchars <= 0)
9501 abort (); /* Don't think this happens. */
9502
9503 ++handling_signal;
9504
9505 /* The input should be decoded if it is from XIM. Currently the
9506 locale of XIM is the same as that of the system. So, we can use
9507 Vlocale_coding_system which is initialized properly at Emacs
9508 startup time. */
9509 setup_coding_system (Vlocale_coding_system, &coding);
9510 coding.src_multibyte = 0;
9511 coding.dst_multibyte = 1;
9512 /* The input is converted to events, thus we can't handle
9513 composition. Anyway, there's no XIM that gives us composition
9514 information. */
9515 coding.composing = COMPOSITION_DISABLED;
9516
9517 /* Find the display we are supposed to read input for.
9518 It's the one communicating on descriptor SD. */
9519 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9520 {
9521 #if 0 /* This ought to be unnecessary; let's verify it. */
9522 #ifdef FIOSNBIO
9523 /* If available, Xlib uses FIOSNBIO to make the socket
9524 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
9525 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
9526 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
9527 fcntl (dpyinfo->connection, F_SETFL, 0);
9528 #endif /* ! defined (FIOSNBIO) */
9529 #endif
9530
9531 #if 0 /* This code can't be made to work, with multiple displays,
9532 and appears not to be used on any system any more.
9533 Also keyboard.c doesn't turn O_NDELAY on and off
9534 for X connections. */
9535 #ifndef SIGIO
9536 #ifndef HAVE_SELECT
9537 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9538 {
9539 extern int read_alarm_should_throw;
9540 read_alarm_should_throw = 1;
9541 XPeekEvent (dpyinfo->display, &event);
9542 read_alarm_should_throw = 0;
9543 }
9544 #endif /* HAVE_SELECT */
9545 #endif /* SIGIO */
9546 #endif
9547
9548 /* For debugging, this gives a way to fake an I/O error. */
9549 if (dpyinfo == XTread_socket_fake_io_error)
9550 {
9551 XTread_socket_fake_io_error = 0;
9552 x_io_error_quitter (dpyinfo->display);
9553 }
9554
9555 while (XPending (dpyinfo->display))
9556 {
9557 XNextEvent (dpyinfo->display, &event);
9558
9559 #ifdef HAVE_X_I18N
9560 {
9561 /* Filter events for the current X input method.
9562 XFilterEvent returns non-zero if the input method has
9563 consumed the event. We pass the frame's X window to
9564 XFilterEvent because that's the one for which the IC
9565 was created. */
9566 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9567 event.xclient.window);
9568 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
9569 break;
9570 }
9571 #endif
9572 event_found = 1;
9573
9574 switch (event.type)
9575 {
9576 case ClientMessage:
9577 {
9578 if (event.xclient.message_type
9579 == dpyinfo->Xatom_wm_protocols
9580 && event.xclient.format == 32)
9581 {
9582 if (event.xclient.data.l[0]
9583 == dpyinfo->Xatom_wm_take_focus)
9584 {
9585 /* Use x_any_window_to_frame because this
9586 could be the shell widget window
9587 if the frame has no title bar. */
9588 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
9589 #ifdef HAVE_X_I18N
9590 /* Not quite sure this is needed -pd */
9591 if (f && FRAME_XIC (f))
9592 XSetICFocus (FRAME_XIC (f));
9593 #endif
9594 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9595 instructs the WM to set the input focus automatically for
9596 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9597 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9598 it has set the focus. So, XSetInputFocus below is not
9599 needed.
9600
9601 The call to XSetInputFocus below has also caused trouble. In
9602 cases where the XSetInputFocus done by the WM and the one
9603 below are temporally close (on a fast machine), the call
9604 below can generate additional FocusIn events which confuse
9605 Emacs. */
9606
9607 /* Since we set WM_TAKE_FOCUS, we must call
9608 XSetInputFocus explicitly. But not if f is null,
9609 since that might be an event for a deleted frame. */
9610 if (f)
9611 {
9612 Display *d = event.xclient.display;
9613 /* Catch and ignore errors, in case window has been
9614 iconified by a window manager such as GWM. */
9615 int count = x_catch_errors (d);
9616 XSetInputFocus (d, event.xclient.window,
9617 /* The ICCCM says this is
9618 the only valid choice. */
9619 RevertToParent,
9620 event.xclient.data.l[1]);
9621 /* This is needed to detect the error
9622 if there is an error. */
9623 XSync (d, False);
9624 x_uncatch_errors (d, count);
9625 }
9626 /* Not certain about handling scroll bars here */
9627 #endif /* 0 */
9628 }
9629 else if (event.xclient.data.l[0]
9630 == dpyinfo->Xatom_wm_save_yourself)
9631 {
9632 /* Save state modify the WM_COMMAND property to
9633 something which can reinstate us. This notifies
9634 the session manager, who's looking for such a
9635 PropertyNotify. Can restart processing when
9636 a keyboard or mouse event arrives. */
9637 if (numchars > 0)
9638 {
9639 f = x_top_window_to_frame (dpyinfo,
9640 event.xclient.window);
9641
9642 /* This is just so we only give real data once
9643 for a single Emacs process. */
9644 if (f == SELECTED_FRAME ())
9645 XSetCommand (FRAME_X_DISPLAY (f),
9646 event.xclient.window,
9647 initial_argv, initial_argc);
9648 else if (f)
9649 XSetCommand (FRAME_X_DISPLAY (f),
9650 event.xclient.window,
9651 0, 0);
9652 }
9653 }
9654 else if (event.xclient.data.l[0]
9655 == dpyinfo->Xatom_wm_delete_window)
9656 {
9657 struct frame *f
9658 = x_any_window_to_frame (dpyinfo,
9659 event.xclient.window);
9660
9661 if (f)
9662 {
9663 if (numchars == 0)
9664 abort ();
9665
9666 bufp->kind = delete_window_event;
9667 XSETFRAME (bufp->frame_or_window, f);
9668 bufp->arg = Qnil;
9669 bufp++;
9670
9671 count += 1;
9672 numchars -= 1;
9673 }
9674 }
9675 }
9676 else if (event.xclient.message_type
9677 == dpyinfo->Xatom_wm_configure_denied)
9678 {
9679 }
9680 else if (event.xclient.message_type
9681 == dpyinfo->Xatom_wm_window_moved)
9682 {
9683 int new_x, new_y;
9684 struct frame *f
9685 = x_window_to_frame (dpyinfo, event.xclient.window);
9686
9687 new_x = event.xclient.data.s[0];
9688 new_y = event.xclient.data.s[1];
9689
9690 if (f)
9691 {
9692 f->output_data.x->left_pos = new_x;
9693 f->output_data.x->top_pos = new_y;
9694 }
9695 }
9696 #ifdef HACK_EDITRES
9697 else if (event.xclient.message_type
9698 == dpyinfo->Xatom_editres)
9699 {
9700 struct frame *f
9701 = x_any_window_to_frame (dpyinfo, event.xclient.window);
9702 _XEditResCheckMessages (f->output_data.x->widget, NULL,
9703 &event, NULL);
9704 }
9705 #endif /* HACK_EDITRES */
9706 else if ((event.xclient.message_type
9707 == dpyinfo->Xatom_DONE)
9708 || (event.xclient.message_type
9709 == dpyinfo->Xatom_PAGE))
9710 {
9711 /* Ghostview job completed. Kill it. We could
9712 reply with "Next" if we received "Page", but we
9713 currently never do because we are interested in
9714 images, only, which should have 1 page. */
9715 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9716 struct frame *f
9717 = x_window_to_frame (dpyinfo, event.xclient.window);
9718 x_kill_gs_process (pixmap, f);
9719 expose_frame (f, 0, 0, 0, 0);
9720 }
9721 #ifdef USE_TOOLKIT_SCROLL_BARS
9722 /* Scroll bar callbacks send a ClientMessage from which
9723 we construct an input_event. */
9724 else if (event.xclient.message_type
9725 == dpyinfo->Xatom_Scrollbar)
9726 {
9727 x_scroll_bar_to_input_event (&event, bufp);
9728 ++bufp, ++count, --numchars;
9729 goto out;
9730 }
9731 #endif /* USE_TOOLKIT_SCROLL_BARS */
9732 else
9733 goto OTHER;
9734 }
9735 break;
9736
9737 case SelectionNotify:
9738 #ifdef USE_X_TOOLKIT
9739 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
9740 goto OTHER;
9741 #endif /* not USE_X_TOOLKIT */
9742 x_handle_selection_notify (&event.xselection);
9743 break;
9744
9745 case SelectionClear: /* Someone has grabbed ownership. */
9746 #ifdef USE_X_TOOLKIT
9747 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
9748 goto OTHER;
9749 #endif /* USE_X_TOOLKIT */
9750 {
9751 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
9752
9753 if (numchars == 0)
9754 abort ();
9755
9756 bufp->kind = selection_clear_event;
9757 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9758 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9759 SELECTION_EVENT_TIME (bufp) = eventp->time;
9760 bufp->frame_or_window = Qnil;
9761 bufp->arg = Qnil;
9762 bufp++;
9763
9764 count += 1;
9765 numchars -= 1;
9766 }
9767 break;
9768
9769 case SelectionRequest: /* Someone wants our selection. */
9770 #ifdef USE_X_TOOLKIT
9771 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
9772 goto OTHER;
9773 #endif /* USE_X_TOOLKIT */
9774 if (x_queue_selection_requests)
9775 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
9776 &event);
9777 else
9778 {
9779 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
9780
9781 if (numchars == 0)
9782 abort ();
9783
9784 bufp->kind = selection_request_event;
9785 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9786 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9787 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9788 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9789 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9790 SELECTION_EVENT_TIME (bufp) = eventp->time;
9791 bufp->frame_or_window = Qnil;
9792 bufp->arg = Qnil;
9793 bufp++;
9794
9795 count += 1;
9796 numchars -= 1;
9797 }
9798 break;
9799
9800 case PropertyNotify:
9801 #ifdef USE_X_TOOLKIT
9802 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
9803 goto OTHER;
9804 #endif /* not USE_X_TOOLKIT */
9805 x_handle_property_notify (&event.xproperty);
9806 break;
9807
9808 case ReparentNotify:
9809 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
9810 if (f)
9811 {
9812 int x, y;
9813 f->output_data.x->parent_desc = event.xreparent.parent;
9814 x_real_positions (f, &x, &y);
9815 f->output_data.x->left_pos = x;
9816 f->output_data.x->top_pos = y;
9817 }
9818 break;
9819
9820 case Expose:
9821 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9822 if (f)
9823 {
9824 if (f->async_visible == 0)
9825 {
9826 f->async_visible = 1;
9827 f->async_iconified = 0;
9828 f->output_data.x->has_been_visible = 1;
9829 SET_FRAME_GARBAGED (f);
9830 }
9831 else
9832 expose_frame (x_window_to_frame (dpyinfo,
9833 event.xexpose.window),
9834 event.xexpose.x, event.xexpose.y,
9835 event.xexpose.width, event.xexpose.height);
9836 }
9837 else
9838 {
9839 #ifdef USE_TOOLKIT_SCROLL_BARS
9840 /* Dispatch event to the widget. */
9841 goto OTHER;
9842 #else /* not USE_TOOLKIT_SCROLL_BARS */
9843 struct scroll_bar *bar
9844 = x_window_to_scroll_bar (event.xexpose.window);
9845
9846 if (bar)
9847 x_scroll_bar_expose (bar, &event);
9848 #ifdef USE_X_TOOLKIT
9849 else
9850 goto OTHER;
9851 #endif /* USE_X_TOOLKIT */
9852 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9853 }
9854 break;
9855
9856 case GraphicsExpose: /* This occurs when an XCopyArea's
9857 source area was obscured or not
9858 available.*/
9859 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
9860 if (f)
9861 {
9862 expose_frame (f,
9863 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9864 event.xgraphicsexpose.width,
9865 event.xgraphicsexpose.height);
9866 }
9867 #ifdef USE_X_TOOLKIT
9868 else
9869 goto OTHER;
9870 #endif /* USE_X_TOOLKIT */
9871 break;
9872
9873 case NoExpose: /* This occurs when an XCopyArea's
9874 source area was completely
9875 available */
9876 break;
9877
9878 case UnmapNotify:
9879 /* Redo the mouse-highlight after the tooltip has gone. */
9880 if (event.xmap.window == tip_window)
9881 {
9882 tip_window = 0;
9883 redo_mouse_highlight ();
9884 }
9885
9886 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
9887 if (f) /* F may no longer exist if
9888 the frame was deleted. */
9889 {
9890 /* While a frame is unmapped, display generation is
9891 disabled; you don't want to spend time updating a
9892 display that won't ever be seen. */
9893 f->async_visible = 0;
9894 /* We can't distinguish, from the event, whether the window
9895 has become iconified or invisible. So assume, if it
9896 was previously visible, than now it is iconified.
9897 But x_make_frame_invisible clears both
9898 the visible flag and the iconified flag;
9899 and that way, we know the window is not iconified now. */
9900 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
9901 {
9902 f->async_iconified = 1;
9903
9904 bufp->kind = iconify_event;
9905 XSETFRAME (bufp->frame_or_window, f);
9906 bufp->arg = Qnil;
9907 bufp++;
9908 count++;
9909 numchars--;
9910 }
9911 }
9912 goto OTHER;
9913
9914 case MapNotify:
9915 if (event.xmap.window == tip_window)
9916 /* The tooltip has been drawn already. Avoid
9917 the SET_FRAME_GARBAGED below. */
9918 goto OTHER;
9919
9920 /* We use x_top_window_to_frame because map events can
9921 come for sub-windows and they don't mean that the
9922 frame is visible. */
9923 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
9924 if (f)
9925 {
9926 f->async_visible = 1;
9927 f->async_iconified = 0;
9928 f->output_data.x->has_been_visible = 1;
9929
9930 /* wait_reading_process_input will notice this and update
9931 the frame's display structures. */
9932 SET_FRAME_GARBAGED (f);
9933
9934 if (f->iconified)
9935 {
9936 bufp->kind = deiconify_event;
9937 XSETFRAME (bufp->frame_or_window, f);
9938 bufp->arg = Qnil;
9939 bufp++;
9940 count++;
9941 numchars--;
9942 }
9943 else if (! NILP (Vframe_list)
9944 && ! NILP (XCDR (Vframe_list)))
9945 /* Force a redisplay sooner or later
9946 to update the frame titles
9947 in case this is the second frame. */
9948 record_asynch_buffer_change ();
9949 }
9950 goto OTHER;
9951
9952 case KeyPress:
9953 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
9954
9955 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
9956 /* I couldn't find a way to prevent LessTif scroll bars
9957 from consuming key events. */
9958 if (f == 0)
9959 {
9960 Widget widget = XtWindowToWidget (dpyinfo->display,
9961 event.xkey.window);
9962 if (widget && XmIsScrollBar (widget))
9963 {
9964 widget = XtParent (widget);
9965 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9966 }
9967 }
9968 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
9969
9970 if (f != 0)
9971 {
9972 KeySym keysym, orig_keysym;
9973 /* al%imercury@uunet.uu.net says that making this 81
9974 instead of 80 fixed a bug whereby meta chars made
9975 his Emacs hang.
9976
9977 It seems that some version of XmbLookupString has
9978 a bug of not returning XBufferOverflow in
9979 status_return even if the input is too long to
9980 fit in 81 bytes. So, we must prepare sufficient
9981 bytes for copy_buffer. 513 bytes (256 chars for
9982 two-byte character set) seems to be a faily good
9983 approximation. -- 2000.8.10 handa@etl.go.jp */
9984 unsigned char copy_buffer[513];
9985 unsigned char *copy_bufptr = copy_buffer;
9986 int copy_bufsiz = sizeof (copy_buffer);
9987 int modifiers;
9988
9989 event.xkey.state
9990 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9991 extra_keyboard_modifiers);
9992 modifiers = event.xkey.state;
9993
9994 /* This will have to go some day... */
9995
9996 /* make_lispy_event turns chars into control chars.
9997 Don't do it here because XLookupString is too eager. */
9998 event.xkey.state &= ~ControlMask;
9999 event.xkey.state &= ~(dpyinfo->meta_mod_mask
10000 | dpyinfo->super_mod_mask
10001 | dpyinfo->hyper_mod_mask
10002 | dpyinfo->alt_mod_mask);
10003
10004 /* In case Meta is ComposeCharacter,
10005 clear its status. According to Markus Ehrnsperger
10006 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
10007 this enables ComposeCharacter to work whether or
10008 not it is combined with Meta. */
10009 if (modifiers & dpyinfo->meta_mod_mask)
10010 bzero (&compose_status, sizeof (compose_status));
10011
10012 #ifdef HAVE_X_I18N
10013 if (FRAME_XIC (f))
10014 {
10015 Status status_return;
10016
10017 nbytes = XmbLookupString (FRAME_XIC (f),
10018 &event.xkey, copy_bufptr,
10019 copy_bufsiz, &keysym,
10020 &status_return);
10021 if (status_return == XBufferOverflow)
10022 {
10023 copy_bufsiz = nbytes + 1;
10024 copy_bufptr = (char *) alloca (copy_bufsiz);
10025 nbytes = XmbLookupString (FRAME_XIC (f),
10026 &event.xkey, copy_bufptr,
10027 copy_bufsiz, &keysym,
10028 &status_return);
10029 }
10030
10031 if (status_return == XLookupNone)
10032 break;
10033 else if (status_return == XLookupChars)
10034 {
10035 keysym = NoSymbol;
10036 modifiers = 0;
10037 }
10038 else if (status_return != XLookupKeySym
10039 && status_return != XLookupBoth)
10040 abort ();
10041 }
10042 else
10043 nbytes = XLookupString (&event.xkey, copy_bufptr,
10044 copy_bufsiz, &keysym,
10045 &compose_status);
10046 #else
10047 nbytes = XLookupString (&event.xkey, copy_bufptr,
10048 copy_bufsiz, &keysym,
10049 &compose_status);
10050 #endif
10051
10052 orig_keysym = keysym;
10053
10054 if (numchars > 1)
10055 {
10056 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
10057 || keysym == XK_Delete
10058 #ifdef XK_ISO_Left_Tab
10059 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
10060 #endif
10061 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
10062 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
10063 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
10064 #ifdef HPUX
10065 /* This recognizes the "extended function keys".
10066 It seems there's no cleaner way.
10067 Test IsModifierKey to avoid handling mode_switch
10068 incorrectly. */
10069 || ((unsigned) (keysym) >= XK_Select
10070 && (unsigned)(keysym) < XK_KP_Space)
10071 #endif
10072 #ifdef XK_dead_circumflex
10073 || orig_keysym == XK_dead_circumflex
10074 #endif
10075 #ifdef XK_dead_grave
10076 || orig_keysym == XK_dead_grave
10077 #endif
10078 #ifdef XK_dead_tilde
10079 || orig_keysym == XK_dead_tilde
10080 #endif
10081 #ifdef XK_dead_diaeresis
10082 || orig_keysym == XK_dead_diaeresis
10083 #endif
10084 #ifdef XK_dead_macron
10085 || orig_keysym == XK_dead_macron
10086 #endif
10087 #ifdef XK_dead_degree
10088 || orig_keysym == XK_dead_degree
10089 #endif
10090 #ifdef XK_dead_acute
10091 || orig_keysym == XK_dead_acute
10092 #endif
10093 #ifdef XK_dead_cedilla
10094 || orig_keysym == XK_dead_cedilla
10095 #endif
10096 #ifdef XK_dead_breve
10097 || orig_keysym == XK_dead_breve
10098 #endif
10099 #ifdef XK_dead_ogonek
10100 || orig_keysym == XK_dead_ogonek
10101 #endif
10102 #ifdef XK_dead_caron
10103 || orig_keysym == XK_dead_caron
10104 #endif
10105 #ifdef XK_dead_doubleacute
10106 || orig_keysym == XK_dead_doubleacute
10107 #endif
10108 #ifdef XK_dead_abovedot
10109 || orig_keysym == XK_dead_abovedot
10110 #endif
10111 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10112 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10113 /* Any "vendor-specific" key is ok. */
10114 || (orig_keysym & (1 << 28)))
10115 && ! (IsModifierKey (orig_keysym)
10116 #ifndef HAVE_X11R5
10117 #ifdef XK_Mode_switch
10118 || ((unsigned)(orig_keysym) == XK_Mode_switch)
10119 #endif
10120 #ifdef XK_Num_Lock
10121 || ((unsigned)(orig_keysym) == XK_Num_Lock)
10122 #endif
10123 #endif /* not HAVE_X11R5 */
10124 ))
10125 {
10126 if (temp_index == sizeof temp_buffer / sizeof (short))
10127 temp_index = 0;
10128 temp_buffer[temp_index++] = keysym;
10129 bufp->kind = non_ascii_keystroke;
10130 bufp->code = keysym;
10131 XSETFRAME (bufp->frame_or_window, f);
10132 bufp->arg = Qnil;
10133 bufp->modifiers
10134 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10135 modifiers);
10136 bufp->timestamp = event.xkey.time;
10137 bufp++;
10138 count++;
10139 numchars--;
10140 }
10141 else if (numchars > nbytes)
10142 {
10143 register int i;
10144 register int c;
10145 int nchars, len;
10146
10147 for (i = 0; i < nbytes; i++)
10148 {
10149 if (temp_index == (sizeof temp_buffer
10150 / sizeof (short)))
10151 temp_index = 0;
10152 temp_buffer[temp_index++] = copy_bufptr[i];
10153 }
10154
10155 if (/* If the event is not from XIM, */
10156 event.xkey.keycode != 0
10157 /* or the current locale doesn't request
10158 decoding of the intup data, ... */
10159 || coding.type == coding_type_raw_text
10160 || coding.type == coding_type_no_conversion)
10161 {
10162 /* ... we can use the input data as is. */
10163 nchars = nbytes;
10164 }
10165 else
10166 {
10167 /* We have to decode the input data. */
10168 int require;
10169 unsigned char *p;
10170
10171 require = decoding_buffer_size (&coding, nbytes);
10172 p = (unsigned char *) alloca (require);
10173 coding.mode |= CODING_MODE_LAST_BLOCK;
10174 decode_coding (&coding, copy_bufptr, p,
10175 nbytes, require);
10176 nbytes = coding.produced;
10177 nchars = coding.produced_char;
10178 copy_bufptr = p;
10179 }
10180
10181 /* Convert the input data to a sequence of
10182 character events. */
10183 for (i = 0; i < nbytes; i += len)
10184 {
10185 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10186 nbytes - i, len);
10187 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
10188 ? ascii_keystroke
10189 : multibyte_char_keystroke);
10190 bufp->code = c;
10191 XSETFRAME (bufp->frame_or_window, f);
10192 bufp->arg = Qnil;
10193 bufp->modifiers
10194 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10195 modifiers);
10196 bufp->timestamp = event.xkey.time;
10197 bufp++;
10198 }
10199
10200 count += nchars;
10201 numchars -= nchars;
10202
10203 if (keysym == NoSymbol)
10204 break;
10205 }
10206 else
10207 abort ();
10208 }
10209 else
10210 abort ();
10211 }
10212 #ifdef HAVE_X_I18N
10213 /* Don't dispatch this event since XtDispatchEvent calls
10214 XFilterEvent, and two calls in a row may freeze the
10215 client. */
10216 break;
10217 #else
10218 goto OTHER;
10219 #endif
10220
10221 case KeyRelease:
10222 #ifdef HAVE_X_I18N
10223 /* Don't dispatch this event since XtDispatchEvent calls
10224 XFilterEvent, and two calls in a row may freeze the
10225 client. */
10226 break;
10227 #else
10228 goto OTHER;
10229 #endif
10230
10231 /* Here's a possible interpretation of the whole
10232 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
10233 you get a FocusIn event, you have to get a FocusOut
10234 event before you relinquish the focus. If you
10235 haven't received a FocusIn event, then a mere
10236 LeaveNotify is enough to free you. */
10237
10238 case EnterNotify:
10239 {
10240 int from_menu_bar_p = 0;
10241
10242 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10243
10244 #ifdef LESSTIF_VERSION
10245 /* When clicking outside of a menu bar popup to close
10246 it, we get a FocusIn/ EnterNotify sequence of
10247 events. The flag event.xcrossing.focus is not set
10248 in the EnterNotify event of that sequence because
10249 the focus is in the menu bar,
10250 event.xcrossing.window is the frame's X window.
10251 Unconditionally setting the focus frame to null in
10252 this case is not the right thing, because no event
10253 follows that could set the focus frame to the right
10254 value.
10255
10256 This could be a LessTif bug, but I wasn't able to
10257 reproduce the behavior in a simple test program.
10258 On the other hand, Motif seems to not have this
10259 problem.
10260
10261 (gerd, LessTif 0.92). */
10262
10263 if (!event.xcrossing.focus
10264 && f
10265 && f->output_data.x->menubar_widget)
10266 {
10267 Window focus;
10268 int revert;
10269
10270 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10271 if (focus == XtWindow (f->output_data.x->menubar_widget))
10272 from_menu_bar_p = 1;
10273 }
10274 #endif /* LESSTIF_VERSION */
10275
10276 if (event.xcrossing.focus || from_menu_bar_p)
10277 {
10278 /* Avoid nasty pop/raise loops. */
10279 if (f && (!(f->auto_raise)
10280 || !(f->auto_lower)
10281 || (event.xcrossing.time - enter_timestamp) > 500))
10282 {
10283 x_new_focus_frame (dpyinfo, f);
10284 enter_timestamp = event.xcrossing.time;
10285 }
10286 }
10287 else if (f == dpyinfo->x_focus_frame)
10288 x_new_focus_frame (dpyinfo, 0);
10289
10290 /* EnterNotify counts as mouse movement,
10291 so update things that depend on mouse position. */
10292 if (f && !f->output_data.x->hourglass_p)
10293 note_mouse_movement (f, &event.xmotion);
10294 goto OTHER;
10295 }
10296
10297 case FocusIn:
10298 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10299 if (event.xfocus.detail != NotifyPointer)
10300 dpyinfo->x_focus_event_frame = f;
10301 if (f)
10302 {
10303 x_new_focus_frame (dpyinfo, f);
10304
10305 /* Don't stop displaying the initial startup message
10306 for a switch-frame event we don't need. */
10307 if (GC_NILP (Vterminal_frame)
10308 && GC_CONSP (Vframe_list)
10309 && !GC_NILP (XCDR (Vframe_list)))
10310 {
10311 bufp->kind = FOCUS_IN_EVENT;
10312 XSETFRAME (bufp->frame_or_window, f);
10313 bufp->arg = Qnil;
10314 ++bufp, ++count, --numchars;
10315 }
10316 }
10317
10318 #ifdef HAVE_X_I18N
10319 if (f && FRAME_XIC (f))
10320 XSetICFocus (FRAME_XIC (f));
10321 #endif
10322
10323 goto OTHER;
10324
10325 case LeaveNotify:
10326 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
10327 if (f)
10328 {
10329 int from_menu_bar_p = 0;
10330
10331 if (f == dpyinfo->mouse_face_mouse_frame)
10332 {
10333 /* If we move outside the frame, then we're
10334 certainly no longer on any text in the frame. */
10335 clear_mouse_face (dpyinfo);
10336 dpyinfo->mouse_face_mouse_frame = 0;
10337 }
10338
10339 /* Generate a nil HELP_EVENT to cancel a help-echo.
10340 Do it only if there's something to cancel.
10341 Otherwise, the startup message is cleared when
10342 the mouse leaves the frame. */
10343 if (any_help_event_p)
10344 {
10345 Lisp_Object frame;
10346 int n;
10347
10348 XSETFRAME (frame, f);
10349 help_echo = Qnil;
10350 n = gen_help_event (bufp, numchars,
10351 Qnil, frame, Qnil, Qnil, 0);
10352 bufp += n, count += n, numchars -= n;
10353 }
10354
10355 #ifdef LESSTIF_VERSION
10356 /* Please see the comment at the start of the
10357 EnterNotify case. */
10358 if (!event.xcrossing.focus
10359 && f->output_data.x->menubar_widget)
10360 {
10361 Window focus;
10362 int revert;
10363 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10364 if (focus == XtWindow (f->output_data.x->menubar_widget))
10365 from_menu_bar_p = 1;
10366 }
10367 #endif /* LESSTIF_VERSION */
10368
10369 if (event.xcrossing.focus || from_menu_bar_p)
10370 x_mouse_leave (dpyinfo);
10371 else
10372 {
10373 if (f == dpyinfo->x_focus_event_frame)
10374 dpyinfo->x_focus_event_frame = 0;
10375 if (f == dpyinfo->x_focus_frame)
10376 x_new_focus_frame (dpyinfo, 0);
10377 }
10378 }
10379 goto OTHER;
10380
10381 case FocusOut:
10382 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10383 if (event.xfocus.detail != NotifyPointer
10384 && f == dpyinfo->x_focus_event_frame)
10385 dpyinfo->x_focus_event_frame = 0;
10386 if (f && f == dpyinfo->x_focus_frame)
10387 x_new_focus_frame (dpyinfo, 0);
10388
10389 #ifdef HAVE_X_I18N
10390 if (f && FRAME_XIC (f))
10391 XUnsetICFocus (FRAME_XIC (f));
10392 #endif
10393
10394 goto OTHER;
10395
10396 case MotionNotify:
10397 {
10398 previous_help_echo = help_echo;
10399 help_echo = help_echo_object = help_echo_window = Qnil;
10400 help_echo_pos = -1;
10401
10402 if (dpyinfo->grabbed && last_mouse_frame
10403 && FRAME_LIVE_P (last_mouse_frame))
10404 f = last_mouse_frame;
10405 else
10406 f = x_window_to_frame (dpyinfo, event.xmotion.window);
10407
10408 if (f)
10409 note_mouse_movement (f, &event.xmotion);
10410 else
10411 {
10412 #ifndef USE_TOOLKIT_SCROLL_BARS
10413 struct scroll_bar *bar
10414 = x_window_to_scroll_bar (event.xmotion.window);
10415
10416 if (bar)
10417 x_scroll_bar_note_movement (bar, &event);
10418 #endif /* USE_TOOLKIT_SCROLL_BARS */
10419
10420 /* If we move outside the frame, then we're
10421 certainly no longer on any text in the frame. */
10422 clear_mouse_face (dpyinfo);
10423 }
10424
10425 /* If the contents of the global variable help_echo
10426 has changed, generate a HELP_EVENT. */
10427 if (!NILP (help_echo)
10428 || !NILP (previous_help_echo))
10429 {
10430 Lisp_Object frame;
10431 int n;
10432
10433 if (f)
10434 XSETFRAME (frame, f);
10435 else
10436 frame = Qnil;
10437
10438 any_help_event_p = 1;
10439 n = gen_help_event (bufp, numchars, help_echo, frame,
10440 help_echo_window, help_echo_object,
10441 help_echo_pos);
10442 bufp += n, count += n, numchars -= n;
10443 }
10444
10445 goto OTHER;
10446 }
10447
10448 case ConfigureNotify:
10449 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10450 if (f)
10451 {
10452 #ifndef USE_X_TOOLKIT
10453 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10454 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
10455
10456 /* In the toolkit version, change_frame_size
10457 is called by the code that handles resizing
10458 of the EmacsFrame widget. */
10459
10460 /* Even if the number of character rows and columns has
10461 not changed, the font size may have changed, so we need
10462 to check the pixel dimensions as well. */
10463 if (columns != f->width
10464 || rows != f->height
10465 || event.xconfigure.width != f->output_data.x->pixel_width
10466 || event.xconfigure.height != f->output_data.x->pixel_height)
10467 {
10468 change_frame_size (f, rows, columns, 0, 1, 0);
10469 SET_FRAME_GARBAGED (f);
10470 cancel_mouse_face (f);
10471 }
10472 #endif
10473
10474 f->output_data.x->pixel_width = event.xconfigure.width;
10475 f->output_data.x->pixel_height = event.xconfigure.height;
10476
10477 /* What we have now is the position of Emacs's own window.
10478 Convert that to the position of the window manager window. */
10479 x_real_positions (f, &f->output_data.x->left_pos,
10480 &f->output_data.x->top_pos);
10481
10482 #ifdef HAVE_X_I18N
10483 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10484 xic_set_statusarea (f);
10485 #endif
10486
10487 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10488 {
10489 /* Since the WM decorations come below top_pos now,
10490 we must put them below top_pos in the future. */
10491 f->output_data.x->win_gravity = NorthWestGravity;
10492 x_wm_set_size_hint (f, (long) 0, 0);
10493 }
10494 #ifdef USE_MOTIF
10495 /* Some window managers pass (0,0) as the location of
10496 the window, and the Motif event handler stores it
10497 in the emacs widget, which messes up Motif menus. */
10498 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10499 {
10500 event.xconfigure.x = f->output_data.x->widget->core.x;
10501 event.xconfigure.y = f->output_data.x->widget->core.y;
10502 }
10503 #endif /* USE_MOTIF */
10504 }
10505 goto OTHER;
10506
10507 case ButtonPress:
10508 case ButtonRelease:
10509 {
10510 /* If we decide we want to generate an event to be seen
10511 by the rest of Emacs, we put it here. */
10512 struct input_event emacs_event;
10513 int tool_bar_p = 0;
10514
10515 emacs_event.kind = no_event;
10516 bzero (&compose_status, sizeof (compose_status));
10517
10518 if (dpyinfo->grabbed
10519 && last_mouse_frame
10520 && FRAME_LIVE_P (last_mouse_frame))
10521 f = last_mouse_frame;
10522 else
10523 f = x_window_to_frame (dpyinfo, event.xbutton.window);
10524
10525 if (f)
10526 {
10527 /* Is this in the tool-bar? */
10528 if (WINDOWP (f->tool_bar_window)
10529 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
10530 {
10531 Lisp_Object window;
10532 int p, x, y;
10533
10534 x = event.xbutton.x;
10535 y = event.xbutton.y;
10536
10537 /* Set x and y. */
10538 window = window_from_coordinates (f, x, y, &p, 1);
10539 if (EQ (window, f->tool_bar_window))
10540 {
10541 x_handle_tool_bar_click (f, &event.xbutton);
10542 tool_bar_p = 1;
10543 }
10544 }
10545
10546 if (!tool_bar_p)
10547 if (!dpyinfo->x_focus_frame
10548 || f == dpyinfo->x_focus_frame)
10549 construct_mouse_click (&emacs_event, &event, f);
10550 }
10551 else
10552 {
10553 #ifndef USE_TOOLKIT_SCROLL_BARS
10554 struct scroll_bar *bar
10555 = x_window_to_scroll_bar (event.xbutton.window);
10556
10557 if (bar)
10558 x_scroll_bar_handle_click (bar, &event, &emacs_event);
10559 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10560 }
10561
10562 if (event.type == ButtonPress)
10563 {
10564 dpyinfo->grabbed |= (1 << event.xbutton.button);
10565 last_mouse_frame = f;
10566 /* Ignore any mouse motion that happened
10567 before this event; any subsequent mouse-movement
10568 Emacs events should reflect only motion after
10569 the ButtonPress. */
10570 if (f != 0)
10571 f->mouse_moved = 0;
10572
10573 if (!tool_bar_p)
10574 last_tool_bar_item = -1;
10575 }
10576 else
10577 {
10578 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
10579 }
10580
10581 if (numchars >= 1 && emacs_event.kind != no_event)
10582 {
10583 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10584 bufp++;
10585 count++;
10586 numchars--;
10587 }
10588
10589 #ifdef USE_X_TOOLKIT
10590 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10591 /* For a down-event in the menu bar,
10592 don't pass it to Xt right now.
10593 Instead, save it away
10594 and we will pass it to Xt from kbd_buffer_get_event.
10595 That way, we can run some Lisp code first. */
10596 if (f && event.type == ButtonPress
10597 /* Verify the event is really within the menu bar
10598 and not just sent to it due to grabbing. */
10599 && event.xbutton.x >= 0
10600 && event.xbutton.x < f->output_data.x->pixel_width
10601 && event.xbutton.y >= 0
10602 && event.xbutton.y < f->output_data.x->menubar_height
10603 && event.xbutton.same_screen)
10604 {
10605 SET_SAVED_BUTTON_EVENT;
10606 XSETFRAME (last_mouse_press_frame, f);
10607 }
10608 else if (event.type == ButtonPress)
10609 {
10610 last_mouse_press_frame = Qnil;
10611 goto OTHER;
10612 }
10613
10614 #ifdef USE_MOTIF /* This should do not harm for Lucid,
10615 but I am trying to be cautious. */
10616 else if (event.type == ButtonRelease)
10617 {
10618 if (!NILP (last_mouse_press_frame))
10619 {
10620 f = XFRAME (last_mouse_press_frame);
10621 if (f->output_data.x)
10622 SET_SAVED_BUTTON_EVENT;
10623 }
10624 else
10625 goto OTHER;
10626 }
10627 #endif /* USE_MOTIF */
10628 else
10629 goto OTHER;
10630 #endif /* USE_X_TOOLKIT */
10631 }
10632 break;
10633
10634 case CirculateNotify:
10635 goto OTHER;
10636
10637 case CirculateRequest:
10638 goto OTHER;
10639
10640 case VisibilityNotify:
10641 goto OTHER;
10642
10643 case MappingNotify:
10644 /* Someone has changed the keyboard mapping - update the
10645 local cache. */
10646 switch (event.xmapping.request)
10647 {
10648 case MappingModifier:
10649 x_find_modifier_meanings (dpyinfo);
10650 /* This is meant to fall through. */
10651 case MappingKeyboard:
10652 XRefreshKeyboardMapping (&event.xmapping);
10653 }
10654 goto OTHER;
10655
10656 default:
10657 OTHER:
10658 #ifdef USE_X_TOOLKIT
10659 BLOCK_INPUT;
10660 XtDispatchEvent (&event);
10661 UNBLOCK_INPUT;
10662 #endif /* USE_X_TOOLKIT */
10663 break;
10664 }
10665 }
10666 }
10667
10668 out:;
10669
10670 /* On some systems, an X bug causes Emacs to get no more events
10671 when the window is destroyed. Detect that. (1994.) */
10672 if (! event_found)
10673 {
10674 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10675 One XNOOP in 100 loops will make Emacs terminate.
10676 B. Bretthauer, 1994 */
10677 x_noop_count++;
10678 if (x_noop_count >= 100)
10679 {
10680 x_noop_count=0;
10681
10682 if (next_noop_dpyinfo == 0)
10683 next_noop_dpyinfo = x_display_list;
10684
10685 XNoOp (next_noop_dpyinfo->display);
10686
10687 /* Each time we get here, cycle through the displays now open. */
10688 next_noop_dpyinfo = next_noop_dpyinfo->next;
10689 }
10690 }
10691
10692 /* If the focus was just given to an auto-raising frame,
10693 raise it now. */
10694 /* ??? This ought to be able to handle more than one such frame. */
10695 if (pending_autoraise_frame)
10696 {
10697 x_raise_frame (pending_autoraise_frame);
10698 pending_autoraise_frame = 0;
10699 }
10700
10701 UNBLOCK_INPUT;
10702 --handling_signal;
10703 return count;
10704 }
10705
10706
10707
10708 \f
10709 /***********************************************************************
10710 Text Cursor
10711 ***********************************************************************/
10712
10713 /* Note if the text cursor of window W has been overwritten by a
10714 drawing operation that outputs N glyphs starting at HPOS in the
10715 line given by output_cursor.vpos. N < 0 means all the rest of the
10716 line after HPOS has been written. */
10717
10718 static void
10719 note_overwritten_text_cursor (w, hpos, n)
10720 struct window *w;
10721 int hpos, n;
10722 {
10723 if (updated_area == TEXT_AREA
10724 && output_cursor.vpos == w->phys_cursor.vpos
10725 && hpos <= w->phys_cursor.hpos
10726 && (n < 0
10727 || hpos + n > w->phys_cursor.hpos))
10728 w->phys_cursor_on_p = 0;
10729 }
10730
10731
10732 /* Set clipping for output in glyph row ROW. W is the window in which
10733 we operate. GC is the graphics context to set clipping in.
10734 WHOLE_LINE_P non-zero means include the areas used for truncation
10735 mark display and alike in the clipping rectangle.
10736
10737 ROW may be a text row or, e.g., a mode line. Text rows must be
10738 clipped to the interior of the window dedicated to text display,
10739 mode lines must be clipped to the whole window. */
10740
10741 static void
10742 x_clip_to_row (w, row, gc, whole_line_p)
10743 struct window *w;
10744 struct glyph_row *row;
10745 GC gc;
10746 int whole_line_p;
10747 {
10748 struct frame *f = XFRAME (WINDOW_FRAME (w));
10749 XRectangle clip_rect;
10750 int window_x, window_y, window_width, window_height;
10751
10752 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
10753
10754 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10755 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10756 clip_rect.y = max (clip_rect.y, window_y);
10757 clip_rect.width = window_width;
10758 clip_rect.height = row->visible_height;
10759
10760 /* If clipping to the whole line, including trunc marks, extend
10761 the rectangle to the left and increase its width. */
10762 if (whole_line_p)
10763 {
10764 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10765 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
10766 }
10767
10768 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
10769 }
10770
10771
10772 /* Draw a hollow box cursor on window W in glyph row ROW. */
10773
10774 static void
10775 x_draw_hollow_cursor (w, row)
10776 struct window *w;
10777 struct glyph_row *row;
10778 {
10779 struct frame *f = XFRAME (WINDOW_FRAME (w));
10780 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10781 Display *dpy = FRAME_X_DISPLAY (f);
10782 int x, y, wd, h;
10783 XGCValues xgcv;
10784 struct glyph *cursor_glyph;
10785 GC gc;
10786
10787 /* Compute frame-relative coordinates from window-relative
10788 coordinates. */
10789 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10790 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10791 + row->ascent - w->phys_cursor_ascent);
10792 h = row->height - 1;
10793
10794 /* Get the glyph the cursor is on. If we can't tell because
10795 the current matrix is invalid or such, give up. */
10796 cursor_glyph = get_phys_cursor_glyph (w);
10797 if (cursor_glyph == NULL)
10798 return;
10799
10800 /* Compute the width of the rectangle to draw. If on a stretch
10801 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10802 rectangle as wide as the glyph, but use a canonical character
10803 width instead. */
10804 wd = cursor_glyph->pixel_width - 1;
10805 if (cursor_glyph->type == STRETCH_GLYPH
10806 && !x_stretch_cursor_p)
10807 wd = min (CANON_X_UNIT (f), wd);
10808
10809 /* The foreground of cursor_gc is typically the same as the normal
10810 background color, which can cause the cursor box to be invisible. */
10811 xgcv.foreground = f->output_data.x->cursor_pixel;
10812 if (dpyinfo->scratch_cursor_gc)
10813 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10814 else
10815 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10816 GCForeground, &xgcv);
10817 gc = dpyinfo->scratch_cursor_gc;
10818
10819 /* Set clipping, draw the rectangle, and reset clipping again. */
10820 x_clip_to_row (w, row, gc, 0);
10821 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10822 XSetClipMask (dpy, gc, None);
10823 }
10824
10825
10826 /* Draw a bar cursor on window W in glyph row ROW.
10827
10828 Implementation note: One would like to draw a bar cursor with an
10829 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10830 Unfortunately, I didn't find a font yet that has this property set.
10831 --gerd. */
10832
10833 static void
10834 x_draw_bar_cursor (w, row, width)
10835 struct window *w;
10836 struct glyph_row *row;
10837 int width;
10838 {
10839 struct frame *f = XFRAME (w->frame);
10840 struct glyph *cursor_glyph;
10841 GC gc;
10842 int x;
10843 unsigned long mask;
10844 XGCValues xgcv;
10845 Display *dpy;
10846 Window window;
10847
10848 /* If cursor is out of bounds, don't draw garbage. This can happen
10849 in mini-buffer windows when switching between echo area glyphs
10850 and mini-buffer. */
10851 cursor_glyph = get_phys_cursor_glyph (w);
10852 if (cursor_glyph == NULL)
10853 return;
10854
10855 /* If on an image, draw like a normal cursor. That's usually better
10856 visible than drawing a bar, esp. if the image is large so that
10857 the bar might not be in the window. */
10858 if (cursor_glyph->type == IMAGE_GLYPH)
10859 {
10860 struct glyph_row *row;
10861 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
10862 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
10863 }
10864 else
10865 {
10866 xgcv.background = f->output_data.x->cursor_pixel;
10867 xgcv.foreground = f->output_data.x->cursor_pixel;
10868 xgcv.graphics_exposures = 0;
10869 mask = GCForeground | GCBackground | GCGraphicsExposures;
10870 dpy = FRAME_X_DISPLAY (f);
10871 window = FRAME_X_WINDOW (f);
10872 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
10873
10874 if (gc)
10875 XChangeGC (dpy, gc, mask, &xgcv);
10876 else
10877 {
10878 gc = XCreateGC (dpy, window, mask, &xgcv);
10879 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10880 }
10881
10882 if (width < 0)
10883 width = f->output_data.x->cursor_width;
10884
10885 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10886 x_clip_to_row (w, row, gc, 0);
10887 XFillRectangle (dpy, window, gc,
10888 x,
10889 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
10890 min (cursor_glyph->pixel_width, width),
10891 row->height);
10892 XSetClipMask (dpy, gc, None);
10893 }
10894 }
10895
10896
10897 /* Clear the cursor of window W to background color, and mark the
10898 cursor as not shown. This is used when the text where the cursor
10899 is is about to be rewritten. */
10900
10901 static void
10902 x_clear_cursor (w)
10903 struct window *w;
10904 {
10905 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10906 x_update_window_cursor (w, 0);
10907 }
10908
10909
10910 /* Draw the cursor glyph of window W in glyph row ROW. See the
10911 comment of x_draw_glyphs for the meaning of HL. */
10912
10913 static void
10914 x_draw_phys_cursor_glyph (w, row, hl)
10915 struct window *w;
10916 struct glyph_row *row;
10917 enum draw_glyphs_face hl;
10918 {
10919 /* If cursor hpos is out of bounds, don't draw garbage. This can
10920 happen in mini-buffer windows when switching between echo area
10921 glyphs and mini-buffer. */
10922 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10923 {
10924 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10925 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10926 hl, 0, 0, 0);
10927
10928 /* When we erase the cursor, and ROW is overlapped by other
10929 rows, make sure that these overlapping parts of other rows
10930 are redrawn. */
10931 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10932 {
10933 if (row > w->current_matrix->rows
10934 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10935 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10936
10937 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10938 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10939 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10940 }
10941 }
10942 }
10943
10944
10945 /* Erase the image of a cursor of window W from the screen. */
10946
10947 static void
10948 x_erase_phys_cursor (w)
10949 struct window *w;
10950 {
10951 struct frame *f = XFRAME (w->frame);
10952 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10953 int hpos = w->phys_cursor.hpos;
10954 int vpos = w->phys_cursor.vpos;
10955 int mouse_face_here_p = 0;
10956 struct glyph_matrix *active_glyphs = w->current_matrix;
10957 struct glyph_row *cursor_row;
10958 struct glyph *cursor_glyph;
10959 enum draw_glyphs_face hl;
10960
10961 /* No cursor displayed or row invalidated => nothing to do on the
10962 screen. */
10963 if (w->phys_cursor_type == NO_CURSOR)
10964 goto mark_cursor_off;
10965
10966 /* VPOS >= active_glyphs->nrows means that window has been resized.
10967 Don't bother to erase the cursor. */
10968 if (vpos >= active_glyphs->nrows)
10969 goto mark_cursor_off;
10970
10971 /* If row containing cursor is marked invalid, there is nothing we
10972 can do. */
10973 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10974 if (!cursor_row->enabled_p)
10975 goto mark_cursor_off;
10976
10977 /* This can happen when the new row is shorter than the old one.
10978 In this case, either x_draw_glyphs or clear_end_of_line
10979 should have cleared the cursor. Note that we wouldn't be
10980 able to erase the cursor in this case because we don't have a
10981 cursor glyph at hand. */
10982 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10983 goto mark_cursor_off;
10984
10985 /* If the cursor is in the mouse face area, redisplay that when
10986 we clear the cursor. */
10987 if (! NILP (dpyinfo->mouse_face_window)
10988 && w == XWINDOW (dpyinfo->mouse_face_window)
10989 && (vpos > dpyinfo->mouse_face_beg_row
10990 || (vpos == dpyinfo->mouse_face_beg_row
10991 && hpos >= dpyinfo->mouse_face_beg_col))
10992 && (vpos < dpyinfo->mouse_face_end_row
10993 || (vpos == dpyinfo->mouse_face_end_row
10994 && hpos < dpyinfo->mouse_face_end_col))
10995 /* Don't redraw the cursor's spot in mouse face if it is at the
10996 end of a line (on a newline). The cursor appears there, but
10997 mouse highlighting does not. */
10998 && cursor_row->used[TEXT_AREA] > hpos)
10999 mouse_face_here_p = 1;
11000
11001 /* Maybe clear the display under the cursor. */
11002 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
11003 {
11004 int x;
11005 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11006
11007 cursor_glyph = get_phys_cursor_glyph (w);
11008 if (cursor_glyph == NULL)
11009 goto mark_cursor_off;
11010
11011 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11012
11013 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11014 x,
11015 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
11016 cursor_row->y)),
11017 cursor_glyph->pixel_width,
11018 cursor_row->visible_height,
11019 False);
11020 }
11021
11022 /* Erase the cursor by redrawing the character underneath it. */
11023 if (mouse_face_here_p)
11024 hl = DRAW_MOUSE_FACE;
11025 else if (cursor_row->inverse_p)
11026 hl = DRAW_INVERSE_VIDEO;
11027 else
11028 hl = DRAW_NORMAL_TEXT;
11029 x_draw_phys_cursor_glyph (w, cursor_row, hl);
11030
11031 mark_cursor_off:
11032 w->phys_cursor_on_p = 0;
11033 w->phys_cursor_type = NO_CURSOR;
11034 }
11035
11036
11037 /* Display or clear cursor of window W. If ON is zero, clear the
11038 cursor. If it is non-zero, display the cursor. If ON is nonzero,
11039 where to put the cursor is specified by HPOS, VPOS, X and Y. */
11040
11041 void
11042 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
11043 struct window *w;
11044 int on, hpos, vpos, x, y;
11045 {
11046 struct frame *f = XFRAME (w->frame);
11047 int new_cursor_type;
11048 int new_cursor_width;
11049 struct glyph_matrix *current_glyphs;
11050 struct glyph_row *glyph_row;
11051 struct glyph *glyph;
11052
11053 /* This is pointless on invisible frames, and dangerous on garbaged
11054 windows and frames; in the latter case, the frame or window may
11055 be in the midst of changing its size, and x and y may be off the
11056 window. */
11057 if (! FRAME_VISIBLE_P (f)
11058 || FRAME_GARBAGED_P (f)
11059 || vpos >= w->current_matrix->nrows
11060 || hpos >= w->current_matrix->matrix_w)
11061 return;
11062
11063 /* If cursor is off and we want it off, return quickly. */
11064 if (!on && !w->phys_cursor_on_p)
11065 return;
11066
11067 current_glyphs = w->current_matrix;
11068 glyph_row = MATRIX_ROW (current_glyphs, vpos);
11069 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
11070
11071 /* If cursor row is not enabled, we don't really know where to
11072 display the cursor. */
11073 if (!glyph_row->enabled_p)
11074 {
11075 w->phys_cursor_on_p = 0;
11076 return;
11077 }
11078
11079 xassert (interrupt_input_blocked);
11080
11081 /* Set new_cursor_type to the cursor we want to be displayed. In a
11082 mini-buffer window, we want the cursor only to appear if we are
11083 reading input from this window. For the selected window, we want
11084 the cursor type given by the frame parameter. If explicitly
11085 marked off, draw no cursor. In all other cases, we want a hollow
11086 box cursor. */
11087 new_cursor_width = -1;
11088 if (cursor_in_echo_area
11089 && FRAME_HAS_MINIBUF_P (f)
11090 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
11091 {
11092 if (w == XWINDOW (echo_area_window))
11093 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11094 else
11095 new_cursor_type = HOLLOW_BOX_CURSOR;
11096 }
11097 else
11098 {
11099 if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
11100 || w != XWINDOW (f->selected_window))
11101 {
11102 extern int cursor_in_non_selected_windows;
11103
11104 if (MINI_WINDOW_P (w)
11105 || !cursor_in_non_selected_windows
11106 || NILP (XBUFFER (w->buffer)->cursor_type))
11107 new_cursor_type = NO_CURSOR;
11108 else
11109 new_cursor_type = HOLLOW_BOX_CURSOR;
11110 }
11111 else if (w->cursor_off_p)
11112 new_cursor_type = NO_CURSOR;
11113 else
11114 {
11115 struct buffer *b = XBUFFER (w->buffer);
11116
11117 if (EQ (b->cursor_type, Qt))
11118 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11119 else
11120 new_cursor_type = x_specified_cursor_type (b->cursor_type,
11121 &new_cursor_width);
11122 }
11123 }
11124
11125 /* If cursor is currently being shown and we don't want it to be or
11126 it is in the wrong place, or the cursor type is not what we want,
11127 erase it. */
11128 if (w->phys_cursor_on_p
11129 && (!on
11130 || w->phys_cursor.x != x
11131 || w->phys_cursor.y != y
11132 || new_cursor_type != w->phys_cursor_type))
11133 x_erase_phys_cursor (w);
11134
11135 /* If the cursor is now invisible and we want it to be visible,
11136 display it. */
11137 if (on && !w->phys_cursor_on_p)
11138 {
11139 w->phys_cursor_ascent = glyph_row->ascent;
11140 w->phys_cursor_height = glyph_row->height;
11141
11142 /* Set phys_cursor_.* before x_draw_.* is called because some
11143 of them may need the information. */
11144 w->phys_cursor.x = x;
11145 w->phys_cursor.y = glyph_row->y;
11146 w->phys_cursor.hpos = hpos;
11147 w->phys_cursor.vpos = vpos;
11148 w->phys_cursor_type = new_cursor_type;
11149 w->phys_cursor_on_p = 1;
11150
11151 switch (new_cursor_type)
11152 {
11153 case HOLLOW_BOX_CURSOR:
11154 x_draw_hollow_cursor (w, glyph_row);
11155 break;
11156
11157 case FILLED_BOX_CURSOR:
11158 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
11159 break;
11160
11161 case BAR_CURSOR:
11162 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
11163 break;
11164
11165 case NO_CURSOR:
11166 break;
11167
11168 default:
11169 abort ();
11170 }
11171
11172 #ifdef HAVE_X_I18N
11173 if (w == XWINDOW (f->selected_window))
11174 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
11175 xic_set_preeditarea (w, x, y);
11176 #endif
11177 }
11178
11179 #ifndef XFlush
11180 if (updating_frame != f)
11181 XFlush (FRAME_X_DISPLAY (f));
11182 #endif
11183 }
11184
11185
11186 /* Display the cursor on window W, or clear it. X and Y are window
11187 relative pixel coordinates. HPOS and VPOS are glyph matrix
11188 positions. If W is not the selected window, display a hollow
11189 cursor. ON non-zero means display the cursor at X, Y which
11190 correspond to HPOS, VPOS, otherwise it is cleared. */
11191
11192 void
11193 x_display_cursor (w, on, hpos, vpos, x, y)
11194 struct window *w;
11195 int on, hpos, vpos, x, y;
11196 {
11197 BLOCK_INPUT;
11198 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
11199 UNBLOCK_INPUT;
11200 }
11201
11202
11203 /* Display the cursor on window W, or clear it, according to ON_P.
11204 Don't change the cursor's position. */
11205
11206 void
11207 x_update_cursor (f, on_p)
11208 struct frame *f;
11209 {
11210 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11211 }
11212
11213
11214 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
11215 in the window tree rooted at W. */
11216
11217 static void
11218 x_update_cursor_in_window_tree (w, on_p)
11219 struct window *w;
11220 int on_p;
11221 {
11222 while (w)
11223 {
11224 if (!NILP (w->hchild))
11225 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11226 else if (!NILP (w->vchild))
11227 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11228 else
11229 x_update_window_cursor (w, on_p);
11230
11231 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11232 }
11233 }
11234
11235
11236 /* Switch the display of W's cursor on or off, according to the value
11237 of ON. */
11238
11239 static void
11240 x_update_window_cursor (w, on)
11241 struct window *w;
11242 int on;
11243 {
11244 /* Don't update cursor in windows whose frame is in the process
11245 of being deleted. */
11246 if (w->current_matrix)
11247 {
11248 BLOCK_INPUT;
11249 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11250 w->phys_cursor.x, w->phys_cursor.y);
11251 UNBLOCK_INPUT;
11252 }
11253 }
11254
11255
11256
11257 \f
11258 /* Icons. */
11259
11260 /* Refresh bitmap kitchen sink icon for frame F
11261 when we get an expose event for it. */
11262
11263 void
11264 refreshicon (f)
11265 struct frame *f;
11266 {
11267 /* Normally, the window manager handles this function. */
11268 }
11269
11270 /* Make the x-window of frame F use the gnu icon bitmap. */
11271
11272 int
11273 x_bitmap_icon (f, file)
11274 struct frame *f;
11275 Lisp_Object file;
11276 {
11277 int bitmap_id;
11278
11279 if (FRAME_X_WINDOW (f) == 0)
11280 return 1;
11281
11282 /* Free up our existing icon bitmap if any. */
11283 if (f->output_data.x->icon_bitmap > 0)
11284 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11285 f->output_data.x->icon_bitmap = 0;
11286
11287 if (STRINGP (file))
11288 bitmap_id = x_create_bitmap_from_file (f, file);
11289 else
11290 {
11291 /* Create the GNU bitmap if necessary. */
11292 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
11293 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11294 = x_create_bitmap_from_data (f, gnu_bits,
11295 gnu_width, gnu_height);
11296
11297 /* The first time we create the GNU bitmap,
11298 this increments the ref-count one extra time.
11299 As a result, the GNU bitmap is never freed.
11300 That way, we don't have to worry about allocating it again. */
11301 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
11302
11303 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
11304 }
11305
11306 x_wm_set_icon_pixmap (f, bitmap_id);
11307 f->output_data.x->icon_bitmap = bitmap_id;
11308
11309 return 0;
11310 }
11311
11312
11313 /* Make the x-window of frame F use a rectangle with text.
11314 Use ICON_NAME as the text. */
11315
11316 int
11317 x_text_icon (f, icon_name)
11318 struct frame *f;
11319 char *icon_name;
11320 {
11321 if (FRAME_X_WINDOW (f) == 0)
11322 return 1;
11323
11324 #ifdef HAVE_X11R4
11325 {
11326 XTextProperty text;
11327 text.value = (unsigned char *) icon_name;
11328 text.encoding = XA_STRING;
11329 text.format = 8;
11330 text.nitems = strlen (icon_name);
11331 #ifdef USE_X_TOOLKIT
11332 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11333 &text);
11334 #else /* not USE_X_TOOLKIT */
11335 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11336 #endif /* not USE_X_TOOLKIT */
11337 }
11338 #else /* not HAVE_X11R4 */
11339 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11340 #endif /* not HAVE_X11R4 */
11341
11342 if (f->output_data.x->icon_bitmap > 0)
11343 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11344 f->output_data.x->icon_bitmap = 0;
11345 x_wm_set_icon_pixmap (f, 0);
11346
11347 return 0;
11348 }
11349 \f
11350 #define X_ERROR_MESSAGE_SIZE 200
11351
11352 /* If non-nil, this should be a string.
11353 It means catch X errors and store the error message in this string. */
11354
11355 static Lisp_Object x_error_message_string;
11356
11357 /* An X error handler which stores the error message in
11358 x_error_message_string. This is called from x_error_handler if
11359 x_catch_errors is in effect. */
11360
11361 static void
11362 x_error_catcher (display, error)
11363 Display *display;
11364 XErrorEvent *error;
11365 {
11366 XGetErrorText (display, error->error_code,
11367 XSTRING (x_error_message_string)->data,
11368 X_ERROR_MESSAGE_SIZE);
11369 }
11370
11371 /* Begin trapping X errors for display DPY. Actually we trap X errors
11372 for all displays, but DPY should be the display you are actually
11373 operating on.
11374
11375 After calling this function, X protocol errors no longer cause
11376 Emacs to exit; instead, they are recorded in the string
11377 stored in x_error_message_string.
11378
11379 Calling x_check_errors signals an Emacs error if an X error has
11380 occurred since the last call to x_catch_errors or x_check_errors.
11381
11382 Calling x_uncatch_errors resumes the normal error handling. */
11383
11384 void x_check_errors ();
11385 static Lisp_Object x_catch_errors_unwind ();
11386
11387 int
11388 x_catch_errors (dpy)
11389 Display *dpy;
11390 {
11391 int count = specpdl_ptr - specpdl;
11392
11393 /* Make sure any errors from previous requests have been dealt with. */
11394 XSync (dpy, False);
11395
11396 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11397
11398 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11399 XSTRING (x_error_message_string)->data[0] = 0;
11400
11401 return count;
11402 }
11403
11404 /* Unbind the binding that we made to check for X errors. */
11405
11406 static Lisp_Object
11407 x_catch_errors_unwind (old_val)
11408 Lisp_Object old_val;
11409 {
11410 x_error_message_string = old_val;
11411 return Qnil;
11412 }
11413
11414 /* If any X protocol errors have arrived since the last call to
11415 x_catch_errors or x_check_errors, signal an Emacs error using
11416 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11417
11418 void
11419 x_check_errors (dpy, format)
11420 Display *dpy;
11421 char *format;
11422 {
11423 /* Make sure to catch any errors incurred so far. */
11424 XSync (dpy, False);
11425
11426 if (XSTRING (x_error_message_string)->data[0])
11427 error (format, XSTRING (x_error_message_string)->data);
11428 }
11429
11430 /* Nonzero if we had any X protocol errors
11431 since we did x_catch_errors on DPY. */
11432
11433 int
11434 x_had_errors_p (dpy)
11435 Display *dpy;
11436 {
11437 /* Make sure to catch any errors incurred so far. */
11438 XSync (dpy, False);
11439
11440 return XSTRING (x_error_message_string)->data[0] != 0;
11441 }
11442
11443 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11444
11445 void
11446 x_clear_errors (dpy)
11447 Display *dpy;
11448 {
11449 XSTRING (x_error_message_string)->data[0] = 0;
11450 }
11451
11452 /* Stop catching X protocol errors and let them make Emacs die.
11453 DPY should be the display that was passed to x_catch_errors.
11454 COUNT should be the value that was returned by
11455 the corresponding call to x_catch_errors. */
11456
11457 void
11458 x_uncatch_errors (dpy, count)
11459 Display *dpy;
11460 int count;
11461 {
11462 unbind_to (count, Qnil);
11463 }
11464
11465 #if 0
11466 static unsigned int x_wire_count;
11467 x_trace_wire ()
11468 {
11469 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11470 }
11471 #endif /* ! 0 */
11472
11473 \f
11474 /* Handle SIGPIPE, which can happen when the connection to a server
11475 simply goes away. SIGPIPE is handled by x_connection_signal.
11476 Don't need to do anything, because the write which caused the
11477 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
11478 which will do the appropriate cleanup for us. */
11479
11480 static SIGTYPE
11481 x_connection_signal (signalnum) /* If we don't have an argument, */
11482 int signalnum; /* some compilers complain in signal calls. */
11483 {
11484 #ifdef USG
11485 /* USG systems forget handlers when they are used;
11486 must reestablish each time */
11487 signal (signalnum, x_connection_signal);
11488 #endif /* USG */
11489 }
11490
11491 \f
11492 /************************************************************************
11493 Handling X errors
11494 ************************************************************************/
11495
11496 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
11497 the text of an error message that lead to the connection loss. */
11498
11499 static SIGTYPE
11500 x_connection_closed (dpy, error_message)
11501 Display *dpy;
11502 char *error_message;
11503 {
11504 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
11505 Lisp_Object frame, tail;
11506 int count;
11507 char *msg;
11508
11509 msg = (char *) alloca (strlen (error_message) + 1);
11510 strcpy (msg, error_message);
11511 handling_signal = 0;
11512
11513 /* Prevent being called recursively because of an error condition
11514 below. Otherwise, we might end up with printing ``can't find per
11515 display information'' in the recursive call instead of printing
11516 the original message here. */
11517 count = x_catch_errors (dpy);
11518
11519 /* We have to close the display to inform Xt that it doesn't
11520 exist anymore. If we don't, Xt will continue to wait for
11521 events from the display. As a consequence, a sequence of
11522
11523 M-x make-frame-on-display RET :1 RET
11524 ...kill the new frame, so that we get an IO error...
11525 M-x make-frame-on-display RET :1 RET
11526
11527 will indefinitely wait in Xt for events for display `:1', opened
11528 in the first class to make-frame-on-display.
11529
11530 Closing the display is reported to lead to a bus error on
11531 OpenWindows in certain situations. I suspect that is a bug
11532 in OpenWindows. I don't know how to cicumvent it here. */
11533
11534 #ifdef USE_X_TOOLKIT
11535 /* If DPYINFO is null, this means we didn't open the display
11536 in the first place, so don't try to close it. */
11537 if (dpyinfo)
11538 XtCloseDisplay (dpy);
11539 #endif
11540
11541 /* Indicate that this display is dead. */
11542 if (dpyinfo)
11543 dpyinfo->display = 0;
11544
11545 /* First delete frames whose mini-buffers are on frames
11546 that are on the dead display. */
11547 FOR_EACH_FRAME (tail, frame)
11548 {
11549 Lisp_Object minibuf_frame;
11550 minibuf_frame
11551 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
11552 if (FRAME_X_P (XFRAME (frame))
11553 && FRAME_X_P (XFRAME (minibuf_frame))
11554 && ! EQ (frame, minibuf_frame)
11555 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
11556 Fdelete_frame (frame, Qt);
11557 }
11558
11559 /* Now delete all remaining frames on the dead display.
11560 We are now sure none of these is used as the mini-buffer
11561 for another frame that we need to delete. */
11562 FOR_EACH_FRAME (tail, frame)
11563 if (FRAME_X_P (XFRAME (frame))
11564 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
11565 {
11566 /* Set this to t so that Fdelete_frame won't get confused
11567 trying to find a replacement. */
11568 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11569 Fdelete_frame (frame, Qt);
11570 }
11571
11572 if (dpyinfo)
11573 x_delete_display (dpyinfo);
11574
11575 x_uncatch_errors (dpy, count);
11576
11577 if (x_display_list == 0)
11578 {
11579 fprintf (stderr, "%s\n", msg);
11580 shut_down_emacs (0, 0, Qnil);
11581 exit (70);
11582 }
11583
11584 /* Ordinary stack unwind doesn't deal with these. */
11585 #ifdef SIGIO
11586 sigunblock (sigmask (SIGIO));
11587 #endif
11588 sigunblock (sigmask (SIGALRM));
11589 TOTALLY_UNBLOCK_INPUT;
11590
11591 clear_waiting_for_input ();
11592 error ("%s", msg);
11593 }
11594
11595
11596 /* This is the usual handler for X protocol errors.
11597 It kills all frames on the display that we got the error for.
11598 If that was the only one, it prints an error message and kills Emacs. */
11599
11600 static void
11601 x_error_quitter (display, error)
11602 Display *display;
11603 XErrorEvent *error;
11604 {
11605 char buf[256], buf1[356];
11606
11607 /* Note that there is no real way portable across R3/R4 to get the
11608 original error handler. */
11609
11610 XGetErrorText (display, error->error_code, buf, sizeof (buf));
11611 sprintf (buf1, "X protocol error: %s on protocol request %d",
11612 buf, error->request_code);
11613 x_connection_closed (display, buf1);
11614 }
11615
11616
11617 /* This is the first-level handler for X protocol errors.
11618 It calls x_error_quitter or x_error_catcher. */
11619
11620 static int
11621 x_error_handler (display, error)
11622 Display *display;
11623 XErrorEvent *error;
11624 {
11625 if (! NILP (x_error_message_string))
11626 x_error_catcher (display, error);
11627 else
11628 x_error_quitter (display, error);
11629 return 0;
11630 }
11631
11632 /* This is the handler for X IO errors, always.
11633 It kills all frames on the display that we lost touch with.
11634 If that was the only one, it prints an error message and kills Emacs. */
11635
11636 static int
11637 x_io_error_quitter (display)
11638 Display *display;
11639 {
11640 char buf[256];
11641
11642 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11643 x_connection_closed (display, buf);
11644 return 0;
11645 }
11646 \f
11647 /* Changing the font of the frame. */
11648
11649 /* Give frame F the font named FONTNAME as its default font, and
11650 return the full name of that font. FONTNAME may be a wildcard
11651 pattern; in that case, we choose some font that fits the pattern.
11652 The return value shows which font we chose. */
11653
11654 Lisp_Object
11655 x_new_font (f, fontname)
11656 struct frame *f;
11657 register char *fontname;
11658 {
11659 struct font_info *fontp
11660 = FS_LOAD_FONT (f, 0, fontname, -1);
11661
11662 if (!fontp)
11663 return Qnil;
11664
11665 f->output_data.x->font = (XFontStruct *) (fontp->font);
11666 f->output_data.x->baseline_offset = fontp->baseline_offset;
11667 f->output_data.x->fontset = -1;
11668
11669 /* Compute the scroll bar width in character columns. */
11670 if (f->scroll_bar_pixel_width > 0)
11671 {
11672 int wid = FONT_WIDTH (f->output_data.x->font);
11673 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11674 }
11675 else
11676 {
11677 int wid = FONT_WIDTH (f->output_data.x->font);
11678 f->scroll_bar_cols = (14 + wid - 1) / wid;
11679 }
11680
11681 /* Now make the frame display the given font. */
11682 if (FRAME_X_WINDOW (f) != 0)
11683 {
11684 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11685 f->output_data.x->font->fid);
11686 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11687 f->output_data.x->font->fid);
11688 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11689 f->output_data.x->font->fid);
11690
11691 frame_update_line_height (f);
11692
11693 /* Don't change the size of a tip frame; there's no point in
11694 doing it because it's done in Fx_show_tip, and it leads to
11695 problems because the tip frame has no widget. */
11696 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
11697 x_set_window_size (f, 0, f->width, f->height);
11698 }
11699 else
11700 /* If we are setting a new frame's font for the first time,
11701 there are no faces yet, so this font's height is the line height. */
11702 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
11703
11704 return build_string (fontp->full_name);
11705 }
11706
11707 /* Give frame F the fontset named FONTSETNAME as its default font, and
11708 return the full name of that fontset. FONTSETNAME may be a wildcard
11709 pattern; in that case, we choose some fontset that fits the pattern.
11710 The return value shows which fontset we chose. */
11711
11712 Lisp_Object
11713 x_new_fontset (f, fontsetname)
11714 struct frame *f;
11715 char *fontsetname;
11716 {
11717 int fontset = fs_query_fontset (build_string (fontsetname), 0);
11718 Lisp_Object result;
11719
11720 if (fontset < 0)
11721 return Qnil;
11722
11723 if (f->output_data.x->fontset == fontset)
11724 /* This fontset is already set in frame F. There's nothing more
11725 to do. */
11726 return fontset_name (fontset);
11727
11728 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
11729
11730 if (!STRINGP (result))
11731 /* Can't load ASCII font. */
11732 return Qnil;
11733
11734 /* Since x_new_font doesn't update any fontset information, do it now. */
11735 f->output_data.x->fontset = fontset;
11736
11737 #ifdef HAVE_X_I18N
11738 if (FRAME_XIC (f)
11739 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
11740 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
11741 #endif
11742
11743 return build_string (fontsetname);
11744 }
11745
11746 \f
11747 /***********************************************************************
11748 X Input Methods
11749 ***********************************************************************/
11750
11751 #ifdef HAVE_X_I18N
11752
11753 #ifdef HAVE_X11R6
11754
11755 /* XIM destroy callback function, which is called whenever the
11756 connection to input method XIM dies. CLIENT_DATA contains a
11757 pointer to the x_display_info structure corresponding to XIM. */
11758
11759 static void
11760 xim_destroy_callback (xim, client_data, call_data)
11761 XIM xim;
11762 XPointer client_data;
11763 XPointer call_data;
11764 {
11765 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11766 Lisp_Object frame, tail;
11767
11768 BLOCK_INPUT;
11769
11770 /* No need to call XDestroyIC.. */
11771 FOR_EACH_FRAME (tail, frame)
11772 {
11773 struct frame *f = XFRAME (frame);
11774 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11775 {
11776 FRAME_XIC (f) = NULL;
11777 if (FRAME_XIC_FONTSET (f))
11778 {
11779 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11780 FRAME_XIC_FONTSET (f) = NULL;
11781 }
11782 }
11783 }
11784
11785 /* No need to call XCloseIM. */
11786 dpyinfo->xim = NULL;
11787 XFree (dpyinfo->xim_styles);
11788 UNBLOCK_INPUT;
11789 }
11790
11791 #endif /* HAVE_X11R6 */
11792
11793 /* Open the connection to the XIM server on display DPYINFO.
11794 RESOURCE_NAME is the resource name Emacs uses. */
11795
11796 static void
11797 xim_open_dpy (dpyinfo, resource_name)
11798 struct x_display_info *dpyinfo;
11799 char *resource_name;
11800 {
11801 #ifdef USE_XIM
11802 XIM xim;
11803
11804 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11805 dpyinfo->xim = xim;
11806
11807 if (xim)
11808 {
11809 #ifdef HAVE_X11R6
11810 XIMCallback destroy;
11811 #endif
11812
11813 /* Get supported styles and XIM values. */
11814 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11815
11816 #ifdef HAVE_X11R6
11817 destroy.callback = xim_destroy_callback;
11818 destroy.client_data = (XPointer)dpyinfo;
11819 /* This isn't prptotyped in OSF 5.0. */
11820 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11821 #endif
11822 }
11823
11824 #else /* not USE_XIM */
11825 dpyinfo->xim = NULL;
11826 #endif /* not USE_XIM */
11827 }
11828
11829
11830 #ifdef HAVE_X11R6_XIM
11831
11832 struct xim_inst_t
11833 {
11834 struct x_display_info *dpyinfo;
11835 char *resource_name;
11836 };
11837
11838 /* XIM instantiate callback function, which is called whenever an XIM
11839 server is available. DISPLAY is teh display of the XIM.
11840 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11841 when the callback was registered. */
11842
11843 static void
11844 xim_instantiate_callback (display, client_data, call_data)
11845 Display *display;
11846 XPointer client_data;
11847 XPointer call_data;
11848 {
11849 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11850 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11851
11852 /* We don't support multiple XIM connections. */
11853 if (dpyinfo->xim)
11854 return;
11855
11856 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11857
11858 /* Create XIC for the existing frames on the same display, as long
11859 as they have no XIC. */
11860 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11861 {
11862 Lisp_Object tail, frame;
11863
11864 BLOCK_INPUT;
11865 FOR_EACH_FRAME (tail, frame)
11866 {
11867 struct frame *f = XFRAME (frame);
11868
11869 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11870 if (FRAME_XIC (f) == NULL)
11871 {
11872 create_frame_xic (f);
11873 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11874 xic_set_statusarea (f);
11875 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11876 {
11877 struct window *w = XWINDOW (f->selected_window);
11878 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11879 }
11880 }
11881 }
11882
11883 UNBLOCK_INPUT;
11884 }
11885 }
11886
11887 #endif /* HAVE_X11R6_XIM */
11888
11889
11890 /* Open a connection to the XIM server on display DPYINFO.
11891 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11892 connection only at the first time. On X11R6, open the connection
11893 in the XIM instantiate callback function. */
11894
11895 static void
11896 xim_initialize (dpyinfo, resource_name)
11897 struct x_display_info *dpyinfo;
11898 char *resource_name;
11899 {
11900 #ifdef USE_XIM
11901 #ifdef HAVE_X11R6_XIM
11902 struct xim_inst_t *xim_inst;
11903 int len;
11904
11905 dpyinfo->xim = NULL;
11906 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11907 xim_inst->dpyinfo = dpyinfo;
11908 len = strlen (resource_name);
11909 xim_inst->resource_name = (char *) xmalloc (len + 1);
11910 bcopy (resource_name, xim_inst->resource_name, len + 1);
11911 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11912 resource_name, EMACS_CLASS,
11913 xim_instantiate_callback,
11914 /* Fixme: This is XPointer in
11915 XFree86 but (XPointer *) on
11916 Tru64, at least. */
11917 (XPointer) xim_inst);
11918 #else /* not HAVE_X11R6_XIM */
11919 dpyinfo->xim = NULL;
11920 xim_open_dpy (dpyinfo, resource_name);
11921 #endif /* not HAVE_X11R6_XIM */
11922
11923 #else /* not USE_XIM */
11924 dpyinfo->xim = NULL;
11925 #endif /* not USE_XIM */
11926 }
11927
11928
11929 /* Close the connection to the XIM server on display DPYINFO. */
11930
11931 static void
11932 xim_close_dpy (dpyinfo)
11933 struct x_display_info *dpyinfo;
11934 {
11935 #ifdef USE_XIM
11936 #ifdef HAVE_X11R6_XIM
11937 if (dpyinfo->display)
11938 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11939 NULL, EMACS_CLASS,
11940 xim_instantiate_callback, NULL);
11941 #endif /* not HAVE_X11R6_XIM */
11942 if (dpyinfo->display)
11943 XCloseIM (dpyinfo->xim);
11944 dpyinfo->xim = NULL;
11945 XFree (dpyinfo->xim_styles);
11946 #endif /* USE_XIM */
11947 }
11948
11949 #endif /* not HAVE_X11R6_XIM */
11950
11951
11952 \f
11953 /* Calculate the absolute position in frame F
11954 from its current recorded position values and gravity. */
11955
11956 void
11957 x_calc_absolute_position (f)
11958 struct frame *f;
11959 {
11960 Window child;
11961 int win_x = 0, win_y = 0;
11962 int flags = f->output_data.x->size_hint_flags;
11963 int this_window;
11964
11965 /* We have nothing to do if the current position
11966 is already for the top-left corner. */
11967 if (! ((flags & XNegative) || (flags & YNegative)))
11968 return;
11969
11970 #ifdef USE_X_TOOLKIT
11971 this_window = XtWindow (f->output_data.x->widget);
11972 #else
11973 this_window = FRAME_X_WINDOW (f);
11974 #endif
11975
11976 /* Find the position of the outside upper-left corner of
11977 the inner window, with respect to the outer window.
11978 But do this only if we will need the results. */
11979 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
11980 {
11981 int count;
11982
11983 BLOCK_INPUT;
11984 count = x_catch_errors (FRAME_X_DISPLAY (f));
11985 while (1)
11986 {
11987 x_clear_errors (FRAME_X_DISPLAY (f));
11988 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11989
11990 /* From-window, to-window. */
11991 this_window,
11992 f->output_data.x->parent_desc,
11993
11994 /* From-position, to-position. */
11995 0, 0, &win_x, &win_y,
11996
11997 /* Child of win. */
11998 &child);
11999 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12000 {
12001 Window newroot, newparent = 0xdeadbeef;
12002 Window *newchildren;
12003 unsigned int nchildren;
12004
12005 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
12006 &newparent, &newchildren, &nchildren))
12007 break;
12008
12009 XFree ((char *) newchildren);
12010
12011 f->output_data.x->parent_desc = newparent;
12012 }
12013 else
12014 break;
12015 }
12016
12017 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12018 UNBLOCK_INPUT;
12019 }
12020
12021 /* Treat negative positions as relative to the leftmost bottommost
12022 position that fits on the screen. */
12023 if (flags & XNegative)
12024 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
12025 - 2 * f->output_data.x->border_width - win_x
12026 - PIXEL_WIDTH (f)
12027 + f->output_data.x->left_pos);
12028
12029 {
12030 int height = PIXEL_HEIGHT (f);
12031
12032 #if defined USE_X_TOOLKIT && defined USE_MOTIF
12033 /* Something is fishy here. When using Motif, starting Emacs with
12034 `-g -0-0', the frame appears too low by a few pixels.
12035
12036 This seems to be so because initially, while Emacs is starting,
12037 the column widget's height and the frame's pixel height are
12038 different. The column widget's height is the right one. In
12039 later invocations, when Emacs is up, the frame's pixel height
12040 is right, though.
12041
12042 It's not obvious where the initial small difference comes from.
12043 2000-12-01, gerd. */
12044
12045 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
12046 #endif
12047
12048 if (flags & YNegative)
12049 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
12050 - 2 * f->output_data.x->border_width
12051 - win_y
12052 - height
12053 + f->output_data.x->top_pos);
12054 }
12055
12056 /* The left_pos and top_pos
12057 are now relative to the top and left screen edges,
12058 so the flags should correspond. */
12059 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12060 }
12061
12062 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
12063 to really change the position, and 0 when calling from
12064 x_make_frame_visible (in that case, XOFF and YOFF are the current
12065 position values). It is -1 when calling from x_set_frame_parameters,
12066 which means, do adjust for borders but don't change the gravity. */
12067
12068 void
12069 x_set_offset (f, xoff, yoff, change_gravity)
12070 struct frame *f;
12071 register int xoff, yoff;
12072 int change_gravity;
12073 {
12074 int modified_top, modified_left;
12075
12076 if (change_gravity > 0)
12077 {
12078 f->output_data.x->top_pos = yoff;
12079 f->output_data.x->left_pos = xoff;
12080 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12081 if (xoff < 0)
12082 f->output_data.x->size_hint_flags |= XNegative;
12083 if (yoff < 0)
12084 f->output_data.x->size_hint_flags |= YNegative;
12085 f->output_data.x->win_gravity = NorthWestGravity;
12086 }
12087 x_calc_absolute_position (f);
12088
12089 BLOCK_INPUT;
12090 x_wm_set_size_hint (f, (long) 0, 0);
12091
12092 modified_left = f->output_data.x->left_pos;
12093 modified_top = f->output_data.x->top_pos;
12094 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
12095 this seems to be unnecessary and incorrect. rms, 4/17/97. */
12096 /* It is a mystery why we need to add the border_width here
12097 when the frame is already visible, but experiment says we do. */
12098 if (change_gravity != 0)
12099 {
12100 modified_left += f->output_data.x->border_width;
12101 modified_top += f->output_data.x->border_width;
12102 }
12103 #endif
12104
12105 #ifdef USE_X_TOOLKIT
12106 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
12107 modified_left, modified_top);
12108 #else /* not USE_X_TOOLKIT */
12109 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12110 modified_left, modified_top);
12111 #endif /* not USE_X_TOOLKIT */
12112 UNBLOCK_INPUT;
12113 }
12114
12115
12116 /* Change the size of frame F's X window to COLS/ROWS in the case F
12117 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
12118 top-left-corner window gravity for this size change and subsequent
12119 size changes. Otherwise we leave the window gravity unchanged. */
12120
12121 static void
12122 x_set_window_size_1 (f, change_gravity, cols, rows)
12123 struct frame *f;
12124 int change_gravity;
12125 int cols, rows;
12126 {
12127 int pixelwidth, pixelheight;
12128
12129 check_frame_size (f, &rows, &cols);
12130 f->output_data.x->vertical_scroll_bar_extra
12131 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
12132 ? 0
12133 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
12134 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
12135 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
12136 f->output_data.x->flags_areas_extra
12137 = FRAME_FLAGS_AREA_WIDTH (f);
12138 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
12139 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
12140
12141 f->output_data.x->win_gravity = NorthWestGravity;
12142 x_wm_set_size_hint (f, (long) 0, 0);
12143
12144 XSync (FRAME_X_DISPLAY (f), False);
12145 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12146 pixelwidth, pixelheight);
12147
12148 /* Now, strictly speaking, we can't be sure that this is accurate,
12149 but the window manager will get around to dealing with the size
12150 change request eventually, and we'll hear how it went when the
12151 ConfigureNotify event gets here.
12152
12153 We could just not bother storing any of this information here,
12154 and let the ConfigureNotify event set everything up, but that
12155 might be kind of confusing to the Lisp code, since size changes
12156 wouldn't be reported in the frame parameters until some random
12157 point in the future when the ConfigureNotify event arrives.
12158
12159 We pass 1 for DELAY since we can't run Lisp code inside of
12160 a BLOCK_INPUT. */
12161 change_frame_size (f, rows, cols, 0, 1, 0);
12162 PIXEL_WIDTH (f) = pixelwidth;
12163 PIXEL_HEIGHT (f) = pixelheight;
12164
12165 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
12166 receive in the ConfigureNotify event; if we get what we asked
12167 for, then the event won't cause the screen to become garbaged, so
12168 we have to make sure to do it here. */
12169 SET_FRAME_GARBAGED (f);
12170
12171 XFlush (FRAME_X_DISPLAY (f));
12172 }
12173
12174
12175 /* Call this to change the size of frame F's x-window.
12176 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
12177 for this size change and subsequent size changes.
12178 Otherwise we leave the window gravity unchanged. */
12179
12180 void
12181 x_set_window_size (f, change_gravity, cols, rows)
12182 struct frame *f;
12183 int change_gravity;
12184 int cols, rows;
12185 {
12186 BLOCK_INPUT;
12187
12188 #ifdef USE_X_TOOLKIT
12189
12190 if (f->output_data.x->widget != NULL)
12191 {
12192 /* The x and y position of the widget is clobbered by the
12193 call to XtSetValues within EmacsFrameSetCharSize.
12194 This is a real kludge, but I don't understand Xt so I can't
12195 figure out a correct fix. Can anyone else tell me? -- rms. */
12196 int xpos = f->output_data.x->widget->core.x;
12197 int ypos = f->output_data.x->widget->core.y;
12198 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
12199 f->output_data.x->widget->core.x = xpos;
12200 f->output_data.x->widget->core.y = ypos;
12201 }
12202 else
12203 x_set_window_size_1 (f, change_gravity, cols, rows);
12204
12205 #else /* not USE_X_TOOLKIT */
12206
12207 x_set_window_size_1 (f, change_gravity, cols, rows);
12208
12209 #endif /* not USE_X_TOOLKIT */
12210
12211 /* If cursor was outside the new size, mark it as off. */
12212 mark_window_cursors_off (XWINDOW (f->root_window));
12213
12214 /* Clear out any recollection of where the mouse highlighting was,
12215 since it might be in a place that's outside the new frame size.
12216 Actually checking whether it is outside is a pain in the neck,
12217 so don't try--just let the highlighting be done afresh with new size. */
12218 cancel_mouse_face (f);
12219
12220 UNBLOCK_INPUT;
12221 }
12222 \f
12223 /* Mouse warping. */
12224
12225 void
12226 x_set_mouse_position (f, x, y)
12227 struct frame *f;
12228 int x, y;
12229 {
12230 int pix_x, pix_y;
12231
12232 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
12233 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
12234
12235 if (pix_x < 0) pix_x = 0;
12236 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
12237
12238 if (pix_y < 0) pix_y = 0;
12239 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
12240
12241 BLOCK_INPUT;
12242
12243 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12244 0, 0, 0, 0, pix_x, pix_y);
12245 UNBLOCK_INPUT;
12246 }
12247
12248 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
12249
12250 void
12251 x_set_mouse_pixel_position (f, pix_x, pix_y)
12252 struct frame *f;
12253 int pix_x, pix_y;
12254 {
12255 BLOCK_INPUT;
12256
12257 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12258 0, 0, 0, 0, pix_x, pix_y);
12259 UNBLOCK_INPUT;
12260 }
12261 \f
12262 /* focus shifting, raising and lowering. */
12263
12264 void
12265 x_focus_on_frame (f)
12266 struct frame *f;
12267 {
12268 #if 0 /* This proves to be unpleasant. */
12269 x_raise_frame (f);
12270 #endif
12271 #if 0
12272 /* I don't think that the ICCCM allows programs to do things like this
12273 without the interaction of the window manager. Whatever you end up
12274 doing with this code, do it to x_unfocus_frame too. */
12275 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12276 RevertToPointerRoot, CurrentTime);
12277 #endif /* ! 0 */
12278 }
12279
12280 void
12281 x_unfocus_frame (f)
12282 struct frame *f;
12283 {
12284 #if 0
12285 /* Look at the remarks in x_focus_on_frame. */
12286 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
12287 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
12288 RevertToPointerRoot, CurrentTime);
12289 #endif /* ! 0 */
12290 }
12291
12292 /* Raise frame F. */
12293
12294 void
12295 x_raise_frame (f)
12296 struct frame *f;
12297 {
12298 if (f->async_visible)
12299 {
12300 BLOCK_INPUT;
12301 #ifdef USE_X_TOOLKIT
12302 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12303 #else /* not USE_X_TOOLKIT */
12304 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12305 #endif /* not USE_X_TOOLKIT */
12306 XFlush (FRAME_X_DISPLAY (f));
12307 UNBLOCK_INPUT;
12308 }
12309 }
12310
12311 /* Lower frame F. */
12312
12313 void
12314 x_lower_frame (f)
12315 struct frame *f;
12316 {
12317 if (f->async_visible)
12318 {
12319 BLOCK_INPUT;
12320 #ifdef USE_X_TOOLKIT
12321 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12322 #else /* not USE_X_TOOLKIT */
12323 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12324 #endif /* not USE_X_TOOLKIT */
12325 XFlush (FRAME_X_DISPLAY (f));
12326 UNBLOCK_INPUT;
12327 }
12328 }
12329
12330 static void
12331 XTframe_raise_lower (f, raise_flag)
12332 FRAME_PTR f;
12333 int raise_flag;
12334 {
12335 if (raise_flag)
12336 x_raise_frame (f);
12337 else
12338 x_lower_frame (f);
12339 }
12340 \f
12341 /* Change of visibility. */
12342
12343 /* This tries to wait until the frame is really visible.
12344 However, if the window manager asks the user where to position
12345 the frame, this will return before the user finishes doing that.
12346 The frame will not actually be visible at that time,
12347 but it will become visible later when the window manager
12348 finishes with it. */
12349
12350 void
12351 x_make_frame_visible (f)
12352 struct frame *f;
12353 {
12354 Lisp_Object type;
12355 int original_top, original_left;
12356 int retry_count = 2;
12357
12358 retry:
12359
12360 BLOCK_INPUT;
12361
12362 type = x_icon_type (f);
12363 if (!NILP (type))
12364 x_bitmap_icon (f, type);
12365
12366 if (! FRAME_VISIBLE_P (f))
12367 {
12368 /* We test FRAME_GARBAGED_P here to make sure we don't
12369 call x_set_offset a second time
12370 if we get to x_make_frame_visible a second time
12371 before the window gets really visible. */
12372 if (! FRAME_ICONIFIED_P (f)
12373 && ! f->output_data.x->asked_for_visible)
12374 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12375
12376 f->output_data.x->asked_for_visible = 1;
12377
12378 if (! EQ (Vx_no_window_manager, Qt))
12379 x_wm_set_window_state (f, NormalState);
12380 #ifdef USE_X_TOOLKIT
12381 /* This was XtPopup, but that did nothing for an iconified frame. */
12382 XtMapWidget (f->output_data.x->widget);
12383 #else /* not USE_X_TOOLKIT */
12384 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12385 #endif /* not USE_X_TOOLKIT */
12386 #if 0 /* This seems to bring back scroll bars in the wrong places
12387 if the window configuration has changed. They seem
12388 to come back ok without this. */
12389 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
12390 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12391 #endif
12392 }
12393
12394 XFlush (FRAME_X_DISPLAY (f));
12395
12396 /* Synchronize to ensure Emacs knows the frame is visible
12397 before we do anything else. We do this loop with input not blocked
12398 so that incoming events are handled. */
12399 {
12400 Lisp_Object frame;
12401 int count;
12402 /* This must be before UNBLOCK_INPUT
12403 since events that arrive in response to the actions above
12404 will set it when they are handled. */
12405 int previously_visible = f->output_data.x->has_been_visible;
12406
12407 original_left = f->output_data.x->left_pos;
12408 original_top = f->output_data.x->top_pos;
12409
12410 /* This must come after we set COUNT. */
12411 UNBLOCK_INPUT;
12412
12413 /* We unblock here so that arriving X events are processed. */
12414
12415 /* Now move the window back to where it was "supposed to be".
12416 But don't do it if the gravity is negative.
12417 When the gravity is negative, this uses a position
12418 that is 3 pixels too low. Perhaps that's really the border width.
12419
12420 Don't do this if the window has never been visible before,
12421 because the window manager may choose the position
12422 and we don't want to override it. */
12423
12424 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
12425 && f->output_data.x->win_gravity == NorthWestGravity
12426 && previously_visible)
12427 {
12428 Drawable rootw;
12429 int x, y;
12430 unsigned int width, height, border, depth;
12431
12432 BLOCK_INPUT;
12433
12434 /* On some window managers (such as FVWM) moving an existing
12435 window, even to the same place, causes the window manager
12436 to introduce an offset. This can cause the window to move
12437 to an unexpected location. Check the geometry (a little
12438 slow here) and then verify that the window is in the right
12439 place. If the window is not in the right place, move it
12440 there, and take the potential window manager hit. */
12441 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12442 &rootw, &x, &y, &width, &height, &border, &depth);
12443
12444 if (original_left != x || original_top != y)
12445 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12446 original_left, original_top);
12447
12448 UNBLOCK_INPUT;
12449 }
12450
12451 XSETFRAME (frame, f);
12452
12453 /* Wait until the frame is visible. Process X events until a
12454 MapNotify event has been seen, or until we think we won't get a
12455 MapNotify at all.. */
12456 for (count = input_signal_count + 10;
12457 input_signal_count < count && !FRAME_VISIBLE_P (f);)
12458 {
12459 /* Force processing of queued events. */
12460 x_sync (f);
12461
12462 /* Machines that do polling rather than SIGIO have been
12463 observed to go into a busy-wait here. So we'll fake an
12464 alarm signal to let the handler know that there's something
12465 to be read. We used to raise a real alarm, but it seems
12466 that the handler isn't always enabled here. This is
12467 probably a bug. */
12468 if (input_polling_used ())
12469 {
12470 /* It could be confusing if a real alarm arrives while
12471 processing the fake one. Turn it off and let the
12472 handler reset it. */
12473 extern void poll_for_input_1 P_ ((void));
12474 int old_poll_suppress_count = poll_suppress_count;
12475 poll_suppress_count = 1;
12476 poll_for_input_1 ();
12477 poll_suppress_count = old_poll_suppress_count;
12478 }
12479
12480 /* See if a MapNotify event has been processed. */
12481 FRAME_SAMPLE_VISIBILITY (f);
12482 }
12483
12484 /* 2000-09-28: In
12485
12486 (let ((f (selected-frame)))
12487 (iconify-frame f)
12488 (raise-frame f))
12489
12490 the frame is not raised with various window managers on
12491 FreeBSD, Linux and Solaris. It turns out that, for some
12492 unknown reason, the call to XtMapWidget is completely ignored.
12493 Mapping the widget a second time works. */
12494
12495 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
12496 goto retry;
12497 }
12498 }
12499
12500 /* Change from mapped state to withdrawn state. */
12501
12502 /* Make the frame visible (mapped and not iconified). */
12503
12504 void
12505 x_make_frame_invisible (f)
12506 struct frame *f;
12507 {
12508 Window window;
12509
12510 #ifdef USE_X_TOOLKIT
12511 /* Use the frame's outermost window, not the one we normally draw on. */
12512 window = XtWindow (f->output_data.x->widget);
12513 #else /* not USE_X_TOOLKIT */
12514 window = FRAME_X_WINDOW (f);
12515 #endif /* not USE_X_TOOLKIT */
12516
12517 /* Don't keep the highlight on an invisible frame. */
12518 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12519 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12520
12521 #if 0/* This might add unreliability; I don't trust it -- rms. */
12522 if (! f->async_visible && ! f->async_iconified)
12523 return;
12524 #endif
12525
12526 BLOCK_INPUT;
12527
12528 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
12529 that the current position of the window is user-specified, rather than
12530 program-specified, so that when the window is mapped again, it will be
12531 placed at the same location, without forcing the user to position it
12532 by hand again (they have already done that once for this window.) */
12533 x_wm_set_size_hint (f, (long) 0, 1);
12534
12535 #ifdef HAVE_X11R4
12536
12537 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
12538 DefaultScreen (FRAME_X_DISPLAY (f))))
12539 {
12540 UNBLOCK_INPUT_RESIGNAL;
12541 error ("Can't notify window manager of window withdrawal");
12542 }
12543 #else /* ! defined (HAVE_X11R4) */
12544
12545 /* Tell the window manager what we're going to do. */
12546 if (! EQ (Vx_no_window_manager, Qt))
12547 {
12548 XEvent unmap;
12549
12550 unmap.xunmap.type = UnmapNotify;
12551 unmap.xunmap.window = window;
12552 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
12553 unmap.xunmap.from_configure = False;
12554 if (! XSendEvent (FRAME_X_DISPLAY (f),
12555 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12556 False,
12557 SubstructureRedirectMaskSubstructureNotifyMask,
12558 &unmap))
12559 {
12560 UNBLOCK_INPUT_RESIGNAL;
12561 error ("Can't notify window manager of withdrawal");
12562 }
12563 }
12564
12565 /* Unmap the window ourselves. Cheeky! */
12566 XUnmapWindow (FRAME_X_DISPLAY (f), window);
12567 #endif /* ! defined (HAVE_X11R4) */
12568
12569 /* We can't distinguish this from iconification
12570 just by the event that we get from the server.
12571 So we can't win using the usual strategy of letting
12572 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
12573 and synchronize with the server to make sure we agree. */
12574 f->visible = 0;
12575 FRAME_ICONIFIED_P (f) = 0;
12576 f->async_visible = 0;
12577 f->async_iconified = 0;
12578
12579 x_sync (f);
12580
12581 UNBLOCK_INPUT;
12582 }
12583
12584 /* Change window state from mapped to iconified. */
12585
12586 void
12587 x_iconify_frame (f)
12588 struct frame *f;
12589 {
12590 int result;
12591 Lisp_Object type;
12592
12593 /* Don't keep the highlight on an invisible frame. */
12594 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12595 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12596
12597 if (f->async_iconified)
12598 return;
12599
12600 BLOCK_INPUT;
12601
12602 FRAME_SAMPLE_VISIBILITY (f);
12603
12604 type = x_icon_type (f);
12605 if (!NILP (type))
12606 x_bitmap_icon (f, type);
12607
12608 #ifdef USE_X_TOOLKIT
12609
12610 if (! FRAME_VISIBLE_P (f))
12611 {
12612 if (! EQ (Vx_no_window_manager, Qt))
12613 x_wm_set_window_state (f, IconicState);
12614 /* This was XtPopup, but that did nothing for an iconified frame. */
12615 XtMapWidget (f->output_data.x->widget);
12616 /* The server won't give us any event to indicate
12617 that an invisible frame was changed to an icon,
12618 so we have to record it here. */
12619 f->iconified = 1;
12620 f->visible = 1;
12621 f->async_iconified = 1;
12622 f->async_visible = 0;
12623 UNBLOCK_INPUT;
12624 return;
12625 }
12626
12627 result = XIconifyWindow (FRAME_X_DISPLAY (f),
12628 XtWindow (f->output_data.x->widget),
12629 DefaultScreen (FRAME_X_DISPLAY (f)));
12630 UNBLOCK_INPUT;
12631
12632 if (!result)
12633 error ("Can't notify window manager of iconification");
12634
12635 f->async_iconified = 1;
12636 f->async_visible = 0;
12637
12638
12639 BLOCK_INPUT;
12640 XFlush (FRAME_X_DISPLAY (f));
12641 UNBLOCK_INPUT;
12642 #else /* not USE_X_TOOLKIT */
12643
12644 /* Make sure the X server knows where the window should be positioned,
12645 in case the user deiconifies with the window manager. */
12646 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
12647 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12648
12649 /* Since we don't know which revision of X we're running, we'll use both
12650 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
12651
12652 /* X11R4: send a ClientMessage to the window manager using the
12653 WM_CHANGE_STATE type. */
12654 {
12655 XEvent message;
12656
12657 message.xclient.window = FRAME_X_WINDOW (f);
12658 message.xclient.type = ClientMessage;
12659 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
12660 message.xclient.format = 32;
12661 message.xclient.data.l[0] = IconicState;
12662
12663 if (! XSendEvent (FRAME_X_DISPLAY (f),
12664 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12665 False,
12666 SubstructureRedirectMask | SubstructureNotifyMask,
12667 &message))
12668 {
12669 UNBLOCK_INPUT_RESIGNAL;
12670 error ("Can't notify window manager of iconification");
12671 }
12672 }
12673
12674 /* X11R3: set the initial_state field of the window manager hints to
12675 IconicState. */
12676 x_wm_set_window_state (f, IconicState);
12677
12678 if (!FRAME_VISIBLE_P (f))
12679 {
12680 /* If the frame was withdrawn, before, we must map it. */
12681 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12682 }
12683
12684 f->async_iconified = 1;
12685 f->async_visible = 0;
12686
12687 XFlush (FRAME_X_DISPLAY (f));
12688 UNBLOCK_INPUT;
12689 #endif /* not USE_X_TOOLKIT */
12690 }
12691
12692 \f
12693 /* Free X resources of frame F. */
12694
12695 void
12696 x_free_frame_resources (f)
12697 struct frame *f;
12698 {
12699 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12700
12701 BLOCK_INPUT;
12702
12703 /* If a display connection is dead, don't try sending more
12704 commands to the X server. */
12705 if (dpyinfo->display)
12706 {
12707 if (f->output_data.x->icon_desc)
12708 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
12709
12710 #ifdef HAVE_X_I18N
12711 if (FRAME_XIC (f))
12712 free_frame_xic (f);
12713 #endif
12714
12715 if (FRAME_X_WINDOW (f))
12716 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12717
12718 #ifdef USE_X_TOOLKIT
12719 if (f->output_data.x->widget)
12720 XtDestroyWidget (f->output_data.x->widget);
12721 free_frame_menubar (f);
12722 #endif /* USE_X_TOOLKIT */
12723
12724 unload_color (f, f->output_data.x->foreground_pixel);
12725 unload_color (f, f->output_data.x->background_pixel);
12726 unload_color (f, f->output_data.x->cursor_pixel);
12727 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12728 unload_color (f, f->output_data.x->border_pixel);
12729 unload_color (f, f->output_data.x->mouse_pixel);
12730
12731 if (f->output_data.x->scroll_bar_background_pixel != -1)
12732 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
12733 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
12734 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
12735 if (f->output_data.x->white_relief.allocated_p)
12736 unload_color (f, f->output_data.x->white_relief.pixel);
12737 if (f->output_data.x->black_relief.allocated_p)
12738 unload_color (f, f->output_data.x->black_relief.pixel);
12739
12740 if (FRAME_FACE_CACHE (f))
12741 free_frame_faces (f);
12742
12743 x_free_gcs (f);
12744 XFlush (FRAME_X_DISPLAY (f));
12745 }
12746
12747 if (f->output_data.x->saved_menu_event)
12748 xfree (f->output_data.x->saved_menu_event);
12749
12750 xfree (f->output_data.x);
12751 f->output_data.x = NULL;
12752
12753 if (f == dpyinfo->x_focus_frame)
12754 dpyinfo->x_focus_frame = 0;
12755 if (f == dpyinfo->x_focus_event_frame)
12756 dpyinfo->x_focus_event_frame = 0;
12757 if (f == dpyinfo->x_highlight_frame)
12758 dpyinfo->x_highlight_frame = 0;
12759
12760 if (f == dpyinfo->mouse_face_mouse_frame)
12761 {
12762 dpyinfo->mouse_face_beg_row
12763 = dpyinfo->mouse_face_beg_col = -1;
12764 dpyinfo->mouse_face_end_row
12765 = dpyinfo->mouse_face_end_col = -1;
12766 dpyinfo->mouse_face_window = Qnil;
12767 dpyinfo->mouse_face_deferred_gc = 0;
12768 dpyinfo->mouse_face_mouse_frame = 0;
12769 }
12770
12771 UNBLOCK_INPUT;
12772 }
12773
12774
12775 /* Destroy the X window of frame F. */
12776
12777 void
12778 x_destroy_window (f)
12779 struct frame *f;
12780 {
12781 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12782
12783 /* If a display connection is dead, don't try sending more
12784 commands to the X server. */
12785 if (dpyinfo->display != 0)
12786 x_free_frame_resources (f);
12787
12788 dpyinfo->reference_count--;
12789 }
12790
12791 \f
12792 /* Setting window manager hints. */
12793
12794 /* Set the normal size hints for the window manager, for frame F.
12795 FLAGS is the flags word to use--or 0 meaning preserve the flags
12796 that the window now has.
12797 If USER_POSITION is nonzero, we set the USPosition
12798 flag (this is useful when FLAGS is 0). */
12799
12800 void
12801 x_wm_set_size_hint (f, flags, user_position)
12802 struct frame *f;
12803 long flags;
12804 int user_position;
12805 {
12806 XSizeHints size_hints;
12807
12808 #ifdef USE_X_TOOLKIT
12809 Arg al[2];
12810 int ac = 0;
12811 Dimension widget_width, widget_height;
12812 Window window = XtWindow (f->output_data.x->widget);
12813 #else /* not USE_X_TOOLKIT */
12814 Window window = FRAME_X_WINDOW (f);
12815 #endif /* not USE_X_TOOLKIT */
12816
12817 /* Setting PMaxSize caused various problems. */
12818 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
12819
12820 size_hints.x = f->output_data.x->left_pos;
12821 size_hints.y = f->output_data.x->top_pos;
12822
12823 #ifdef USE_X_TOOLKIT
12824 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12825 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
12826 XtGetValues (f->output_data.x->widget, al, ac);
12827 size_hints.height = widget_height;
12828 size_hints.width = widget_width;
12829 #else /* not USE_X_TOOLKIT */
12830 size_hints.height = PIXEL_HEIGHT (f);
12831 size_hints.width = PIXEL_WIDTH (f);
12832 #endif /* not USE_X_TOOLKIT */
12833
12834 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
12835 size_hints.height_inc = f->output_data.x->line_height;
12836 size_hints.max_width
12837 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
12838 size_hints.max_height
12839 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
12840
12841 /* Calculate the base and minimum sizes.
12842
12843 (When we use the X toolkit, we don't do it here.
12844 Instead we copy the values that the widgets are using, below.) */
12845 #ifndef USE_X_TOOLKIT
12846 {
12847 int base_width, base_height;
12848 int min_rows = 0, min_cols = 0;
12849
12850 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12851 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
12852
12853 check_frame_size (f, &min_rows, &min_cols);
12854
12855 /* The window manager uses the base width hints to calculate the
12856 current number of rows and columns in the frame while
12857 resizing; min_width and min_height aren't useful for this
12858 purpose, since they might not give the dimensions for a
12859 zero-row, zero-column frame.
12860
12861 We use the base_width and base_height members if we have
12862 them; otherwise, we set the min_width and min_height members
12863 to the size for a zero x zero frame. */
12864
12865 #ifdef HAVE_X11R4
12866 size_hints.flags |= PBaseSize;
12867 size_hints.base_width = base_width;
12868 size_hints.base_height = base_height;
12869 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12870 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
12871 #else
12872 size_hints.min_width = base_width;
12873 size_hints.min_height = base_height;
12874 #endif
12875 }
12876
12877 /* If we don't need the old flags, we don't need the old hint at all. */
12878 if (flags)
12879 {
12880 size_hints.flags |= flags;
12881 goto no_read;
12882 }
12883 #endif /* not USE_X_TOOLKIT */
12884
12885 {
12886 XSizeHints hints; /* Sometimes I hate X Windows... */
12887 long supplied_return;
12888 int value;
12889
12890 #ifdef HAVE_X11R4
12891 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12892 &supplied_return);
12893 #else
12894 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
12895 #endif
12896
12897 #ifdef USE_X_TOOLKIT
12898 size_hints.base_height = hints.base_height;
12899 size_hints.base_width = hints.base_width;
12900 size_hints.min_height = hints.min_height;
12901 size_hints.min_width = hints.min_width;
12902 #endif
12903
12904 if (flags)
12905 size_hints.flags |= flags;
12906 else
12907 {
12908 if (value == 0)
12909 hints.flags = 0;
12910 if (hints.flags & PSize)
12911 size_hints.flags |= PSize;
12912 if (hints.flags & PPosition)
12913 size_hints.flags |= PPosition;
12914 if (hints.flags & USPosition)
12915 size_hints.flags |= USPosition;
12916 if (hints.flags & USSize)
12917 size_hints.flags |= USSize;
12918 }
12919 }
12920
12921 #ifndef USE_X_TOOLKIT
12922 no_read:
12923 #endif
12924
12925 #ifdef PWinGravity
12926 size_hints.win_gravity = f->output_data.x->win_gravity;
12927 size_hints.flags |= PWinGravity;
12928
12929 if (user_position)
12930 {
12931 size_hints.flags &= ~ PPosition;
12932 size_hints.flags |= USPosition;
12933 }
12934 #endif /* PWinGravity */
12935
12936 #ifdef HAVE_X11R4
12937 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12938 #else
12939 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12940 #endif
12941 }
12942
12943 /* Used for IconicState or NormalState */
12944
12945 void
12946 x_wm_set_window_state (f, state)
12947 struct frame *f;
12948 int state;
12949 {
12950 #ifdef USE_X_TOOLKIT
12951 Arg al[1];
12952
12953 XtSetArg (al[0], XtNinitialState, state);
12954 XtSetValues (f->output_data.x->widget, al, 1);
12955 #else /* not USE_X_TOOLKIT */
12956 Window window = FRAME_X_WINDOW (f);
12957
12958 f->output_data.x->wm_hints.flags |= StateHint;
12959 f->output_data.x->wm_hints.initial_state = state;
12960
12961 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12962 #endif /* not USE_X_TOOLKIT */
12963 }
12964
12965 void
12966 x_wm_set_icon_pixmap (f, pixmap_id)
12967 struct frame *f;
12968 int pixmap_id;
12969 {
12970 Pixmap icon_pixmap;
12971
12972 #ifndef USE_X_TOOLKIT
12973 Window window = FRAME_X_WINDOW (f);
12974 #endif
12975
12976 if (pixmap_id > 0)
12977 {
12978 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
12979 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
12980 }
12981 else
12982 {
12983 /* It seems there is no way to turn off use of an icon pixmap.
12984 The following line does it, only if no icon has yet been created,
12985 for some window managers. But with mwm it crashes.
12986 Some people say it should clear the IconPixmapHint bit in this case,
12987 but that doesn't work, and the X consortium said it isn't the
12988 right thing at all. Since there is no way to win,
12989 best to explicitly give up. */
12990 #if 0
12991 f->output_data.x->wm_hints.icon_pixmap = None;
12992 #else
12993 return;
12994 #endif
12995 }
12996
12997 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12998
12999 {
13000 Arg al[1];
13001 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
13002 XtSetValues (f->output_data.x->widget, al, 1);
13003 }
13004
13005 #else /* not USE_X_TOOLKIT */
13006
13007 f->output_data.x->wm_hints.flags |= IconPixmapHint;
13008 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13009
13010 #endif /* not USE_X_TOOLKIT */
13011 }
13012
13013 void
13014 x_wm_set_icon_position (f, icon_x, icon_y)
13015 struct frame *f;
13016 int icon_x, icon_y;
13017 {
13018 #ifdef USE_X_TOOLKIT
13019 Window window = XtWindow (f->output_data.x->widget);
13020 #else
13021 Window window = FRAME_X_WINDOW (f);
13022 #endif
13023
13024 f->output_data.x->wm_hints.flags |= IconPositionHint;
13025 f->output_data.x->wm_hints.icon_x = icon_x;
13026 f->output_data.x->wm_hints.icon_y = icon_y;
13027
13028 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13029 }
13030
13031 \f
13032 /***********************************************************************
13033 Fonts
13034 ***********************************************************************/
13035
13036 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
13037
13038 struct font_info *
13039 x_get_font_info (f, font_idx)
13040 FRAME_PTR f;
13041 int font_idx;
13042 {
13043 return (FRAME_X_FONT_TABLE (f) + font_idx);
13044 }
13045
13046
13047 /* Return a list of names of available fonts matching PATTERN on frame F.
13048
13049 If SIZE is > 0, it is the size (maximum bounds width) of fonts
13050 to be listed.
13051
13052 SIZE < 0 means include scalable fonts.
13053
13054 Frame F null means we have not yet created any frame on X, and
13055 consult the first display in x_display_list. MAXNAMES sets a limit
13056 on how many fonts to match. */
13057
13058 Lisp_Object
13059 x_list_fonts (f, pattern, size, maxnames)
13060 struct frame *f;
13061 Lisp_Object pattern;
13062 int size;
13063 int maxnames;
13064 {
13065 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
13066 Lisp_Object tem, second_best;
13067 struct x_display_info *dpyinfo
13068 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
13069 Display *dpy = dpyinfo->display;
13070 int try_XLoadQueryFont = 0;
13071 int count;
13072 int allow_scalable_fonts_p = 0;
13073
13074 if (size < 0)
13075 {
13076 allow_scalable_fonts_p = 1;
13077 size = 0;
13078 }
13079
13080 patterns = Fassoc (pattern, Valternate_fontname_alist);
13081 if (NILP (patterns))
13082 patterns = Fcons (pattern, Qnil);
13083
13084 if (maxnames == 1 && !size)
13085 /* We can return any single font matching PATTERN. */
13086 try_XLoadQueryFont = 1;
13087
13088 for (; CONSP (patterns); patterns = XCDR (patterns))
13089 {
13090 int num_fonts;
13091 char **names = NULL;
13092
13093 pattern = XCAR (patterns);
13094 /* See if we cached the result for this particular query.
13095 The cache is an alist of the form:
13096 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
13097 tem = XCDR (dpyinfo->name_list_element);
13098 key = Fcons (Fcons (pattern, make_number (maxnames)),
13099 allow_scalable_fonts_p ? Qt : Qnil);
13100 list = Fassoc (key, tem);
13101 if (!NILP (list))
13102 {
13103 list = Fcdr_safe (list);
13104 /* We have a cashed list. Don't have to get the list again. */
13105 goto label_cached;
13106 }
13107
13108 /* At first, put PATTERN in the cache. */
13109
13110 BLOCK_INPUT;
13111 count = x_catch_errors (dpy);
13112
13113 if (try_XLoadQueryFont)
13114 {
13115 XFontStruct *font;
13116 unsigned long value;
13117
13118 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
13119 if (x_had_errors_p (dpy))
13120 {
13121 /* This error is perhaps due to insufficient memory on X
13122 server. Let's just ignore it. */
13123 font = NULL;
13124 x_clear_errors (dpy);
13125 }
13126
13127 if (font
13128 && XGetFontProperty (font, XA_FONT, &value))
13129 {
13130 char *name = (char *) XGetAtomName (dpy, (Atom) value);
13131 int len = strlen (name);
13132 char *tmp;
13133
13134 /* If DXPC (a Differential X Protocol Compressor)
13135 Ver.3.7 is running, XGetAtomName will return null
13136 string. We must avoid such a name. */
13137 if (len == 0)
13138 try_XLoadQueryFont = 0;
13139 else
13140 {
13141 num_fonts = 1;
13142 names = (char **) alloca (sizeof (char *));
13143 /* Some systems only allow alloca assigned to a
13144 simple var. */
13145 tmp = (char *) alloca (len + 1); names[0] = tmp;
13146 bcopy (name, names[0], len + 1);
13147 XFree (name);
13148 }
13149 }
13150 else
13151 try_XLoadQueryFont = 0;
13152
13153 if (font)
13154 XFreeFont (dpy, font);
13155 }
13156
13157 if (!try_XLoadQueryFont)
13158 {
13159 /* We try at least 10 fonts because XListFonts will return
13160 auto-scaled fonts at the head. */
13161 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
13162 &num_fonts);
13163 if (x_had_errors_p (dpy))
13164 {
13165 /* This error is perhaps due to insufficient memory on X
13166 server. Let's just ignore it. */
13167 names = NULL;
13168 x_clear_errors (dpy);
13169 }
13170 }
13171
13172 x_uncatch_errors (dpy, count);
13173 UNBLOCK_INPUT;
13174
13175 if (names)
13176 {
13177 int i;
13178
13179 /* Make a list of all the fonts we got back.
13180 Store that in the font cache for the display. */
13181 for (i = 0; i < num_fonts; i++)
13182 {
13183 int width = 0;
13184 char *p = names[i];
13185 int average_width = -1, dashes = 0;
13186
13187 /* Count the number of dashes in NAMES[I]. If there are
13188 14 dashes, and the field value following 12th dash
13189 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
13190 is usually too ugly to be used for editing. Let's
13191 ignore it. */
13192 while (*p)
13193 if (*p++ == '-')
13194 {
13195 dashes++;
13196 if (dashes == 7) /* PIXEL_SIZE field */
13197 width = atoi (p);
13198 else if (dashes == 12) /* AVERAGE_WIDTH field */
13199 average_width = atoi (p);
13200 }
13201
13202 if (allow_scalable_fonts_p
13203 || dashes < 14 || average_width != 0)
13204 {
13205 tem = build_string (names[i]);
13206 if (NILP (Fassoc (tem, list)))
13207 {
13208 if (STRINGP (Vx_pixel_size_width_font_regexp)
13209 && ((fast_c_string_match_ignore_case
13210 (Vx_pixel_size_width_font_regexp, names[i]))
13211 >= 0))
13212 /* We can set the value of PIXEL_SIZE to the
13213 width of this font. */
13214 list = Fcons (Fcons (tem, make_number (width)), list);
13215 else
13216 /* For the moment, width is not known. */
13217 list = Fcons (Fcons (tem, Qnil), list);
13218 }
13219 }
13220 }
13221 if (!try_XLoadQueryFont)
13222 XFreeFontNames (names);
13223 }
13224
13225 /* Now store the result in the cache. */
13226 XCDR (dpyinfo->name_list_element)
13227 = Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element));
13228
13229 label_cached:
13230 if (NILP (list)) continue; /* Try the remaining alternatives. */
13231
13232 newlist = second_best = Qnil;
13233 /* Make a list of the fonts that have the right width. */
13234 for (; CONSP (list); list = XCDR (list))
13235 {
13236 int found_size;
13237
13238 tem = XCAR (list);
13239
13240 if (!CONSP (tem) || NILP (XCAR (tem)))
13241 continue;
13242 if (!size)
13243 {
13244 newlist = Fcons (XCAR (tem), newlist);
13245 continue;
13246 }
13247
13248 if (!INTEGERP (XCDR (tem)))
13249 {
13250 /* Since we have not yet known the size of this font, we
13251 must try slow function call XLoadQueryFont. */
13252 XFontStruct *thisinfo;
13253
13254 BLOCK_INPUT;
13255 count = x_catch_errors (dpy);
13256 thisinfo = XLoadQueryFont (dpy,
13257 XSTRING (XCAR (tem))->data);
13258 if (x_had_errors_p (dpy))
13259 {
13260 /* This error is perhaps due to insufficient memory on X
13261 server. Let's just ignore it. */
13262 thisinfo = NULL;
13263 x_clear_errors (dpy);
13264 }
13265 x_uncatch_errors (dpy, count);
13266 UNBLOCK_INPUT;
13267
13268 if (thisinfo)
13269 {
13270 XCDR (tem)
13271 = (thisinfo->min_bounds.width == 0
13272 ? make_number (0)
13273 : make_number (thisinfo->max_bounds.width));
13274 XFreeFont (dpy, thisinfo);
13275 }
13276 else
13277 /* For unknown reason, the previous call of XListFont had
13278 returned a font which can't be opened. Record the size
13279 as 0 not to try to open it again. */
13280 XCDR (tem) = make_number (0);
13281 }
13282
13283 found_size = XINT (XCDR (tem));
13284 if (found_size == size)
13285 newlist = Fcons (XCAR (tem), newlist);
13286 else if (found_size > 0)
13287 {
13288 if (NILP (second_best))
13289 second_best = tem;
13290 else if (found_size < size)
13291 {
13292 if (XINT (XCDR (second_best)) > size
13293 || XINT (XCDR (second_best)) < found_size)
13294 second_best = tem;
13295 }
13296 else
13297 {
13298 if (XINT (XCDR (second_best)) > size
13299 && XINT (XCDR (second_best)) > found_size)
13300 second_best = tem;
13301 }
13302 }
13303 }
13304 if (!NILP (newlist))
13305 break;
13306 else if (!NILP (second_best))
13307 {
13308 newlist = Fcons (XCAR (second_best), Qnil);
13309 break;
13310 }
13311 }
13312
13313 return newlist;
13314 }
13315
13316
13317 #if GLYPH_DEBUG
13318
13319 /* Check that FONT is valid on frame F. It is if it can be found in F's
13320 font table. */
13321
13322 static void
13323 x_check_font (f, font)
13324 struct frame *f;
13325 XFontStruct *font;
13326 {
13327 int i;
13328 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13329
13330 xassert (font != NULL);
13331
13332 for (i = 0; i < dpyinfo->n_fonts; i++)
13333 if (dpyinfo->font_table[i].name
13334 && font == dpyinfo->font_table[i].font)
13335 break;
13336
13337 xassert (i < dpyinfo->n_fonts);
13338 }
13339
13340 #endif /* GLYPH_DEBUG != 0 */
13341
13342 /* Set *W to the minimum width, *H to the minimum font height of FONT.
13343 Note: There are (broken) X fonts out there with invalid XFontStruct
13344 min_bounds contents. For example, handa@etl.go.jp reports that
13345 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
13346 have font->min_bounds.width == 0. */
13347
13348 static INLINE void
13349 x_font_min_bounds (font, w, h)
13350 XFontStruct *font;
13351 int *w, *h;
13352 {
13353 *h = FONT_HEIGHT (font);
13354 *w = font->min_bounds.width;
13355
13356 /* Try to handle the case where FONT->min_bounds has invalid
13357 contents. Since the only font known to have invalid min_bounds
13358 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
13359 if (*w <= 0)
13360 *w = font->max_bounds.width;
13361 }
13362
13363
13364 /* Compute the smallest character width and smallest font height over
13365 all fonts available on frame F. Set the members smallest_char_width
13366 and smallest_font_height in F's x_display_info structure to
13367 the values computed. Value is non-zero if smallest_font_height or
13368 smallest_char_width become smaller than they were before. */
13369
13370 static int
13371 x_compute_min_glyph_bounds (f)
13372 struct frame *f;
13373 {
13374 int i;
13375 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13376 XFontStruct *font;
13377 int old_width = dpyinfo->smallest_char_width;
13378 int old_height = dpyinfo->smallest_font_height;
13379
13380 dpyinfo->smallest_font_height = 100000;
13381 dpyinfo->smallest_char_width = 100000;
13382
13383 for (i = 0; i < dpyinfo->n_fonts; ++i)
13384 if (dpyinfo->font_table[i].name)
13385 {
13386 struct font_info *fontp = dpyinfo->font_table + i;
13387 int w, h;
13388
13389 font = (XFontStruct *) fontp->font;
13390 xassert (font != (XFontStruct *) ~0);
13391 x_font_min_bounds (font, &w, &h);
13392
13393 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
13394 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
13395 }
13396
13397 xassert (dpyinfo->smallest_char_width > 0
13398 && dpyinfo->smallest_font_height > 0);
13399
13400 return (dpyinfo->n_fonts == 1
13401 || dpyinfo->smallest_char_width < old_width
13402 || dpyinfo->smallest_font_height < old_height);
13403 }
13404
13405
13406 /* Load font named FONTNAME of the size SIZE for frame F, and return a
13407 pointer to the structure font_info while allocating it dynamically.
13408 If SIZE is 0, load any size of font.
13409 If loading is failed, return NULL. */
13410
13411 struct font_info *
13412 x_load_font (f, fontname, size)
13413 struct frame *f;
13414 register char *fontname;
13415 int size;
13416 {
13417 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13418 Lisp_Object font_names;
13419 int count;
13420
13421 /* Get a list of all the fonts that match this name. Once we
13422 have a list of matching fonts, we compare them against the fonts
13423 we already have by comparing names. */
13424 font_names = x_list_fonts (f, build_string (fontname), size, 1);
13425
13426 if (!NILP (font_names))
13427 {
13428 Lisp_Object tail;
13429 int i;
13430
13431 for (i = 0; i < dpyinfo->n_fonts; i++)
13432 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
13433 if (dpyinfo->font_table[i].name
13434 && (!strcmp (dpyinfo->font_table[i].name,
13435 XSTRING (XCAR (tail))->data)
13436 || !strcmp (dpyinfo->font_table[i].full_name,
13437 XSTRING (XCAR (tail))->data)))
13438 return (dpyinfo->font_table + i);
13439 }
13440
13441 /* Load the font and add it to the table. */
13442 {
13443 char *full_name;
13444 XFontStruct *font;
13445 struct font_info *fontp;
13446 unsigned long value;
13447 int i;
13448
13449 /* If we have found fonts by x_list_font, load one of them. If
13450 not, we still try to load a font by the name given as FONTNAME
13451 because XListFonts (called in x_list_font) of some X server has
13452 a bug of not finding a font even if the font surely exists and
13453 is loadable by XLoadQueryFont. */
13454 if (size > 0 && !NILP (font_names))
13455 fontname = (char *) XSTRING (XCAR (font_names))->data;
13456
13457 BLOCK_INPUT;
13458 count = x_catch_errors (FRAME_X_DISPLAY (f));
13459 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
13460 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
13461 {
13462 /* This error is perhaps due to insufficient memory on X
13463 server. Let's just ignore it. */
13464 font = NULL;
13465 x_clear_errors (FRAME_X_DISPLAY (f));
13466 }
13467 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
13468 UNBLOCK_INPUT;
13469 if (!font)
13470 return NULL;
13471
13472 /* Find a free slot in the font table. */
13473 for (i = 0; i < dpyinfo->n_fonts; ++i)
13474 if (dpyinfo->font_table[i].name == NULL)
13475 break;
13476
13477 /* If no free slot found, maybe enlarge the font table. */
13478 if (i == dpyinfo->n_fonts
13479 && dpyinfo->n_fonts == dpyinfo->font_table_size)
13480 {
13481 int sz;
13482 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
13483 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
13484 dpyinfo->font_table
13485 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
13486 }
13487
13488 fontp = dpyinfo->font_table + i;
13489 if (i == dpyinfo->n_fonts)
13490 ++dpyinfo->n_fonts;
13491
13492 /* Now fill in the slots of *FONTP. */
13493 BLOCK_INPUT;
13494 fontp->font = font;
13495 fontp->font_idx = i;
13496 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
13497 bcopy (fontname, fontp->name, strlen (fontname) + 1);
13498
13499 /* Try to get the full name of FONT. Put it in FULL_NAME. */
13500 full_name = 0;
13501 if (XGetFontProperty (font, XA_FONT, &value))
13502 {
13503 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
13504 char *p = name;
13505 int dashes = 0;
13506
13507 /* Count the number of dashes in the "full name".
13508 If it is too few, this isn't really the font's full name,
13509 so don't use it.
13510 In X11R4, the fonts did not come with their canonical names
13511 stored in them. */
13512 while (*p)
13513 {
13514 if (*p == '-')
13515 dashes++;
13516 p++;
13517 }
13518
13519 if (dashes >= 13)
13520 {
13521 full_name = (char *) xmalloc (p - name + 1);
13522 bcopy (name, full_name, p - name + 1);
13523 }
13524
13525 XFree (name);
13526 }
13527
13528 if (full_name != 0)
13529 fontp->full_name = full_name;
13530 else
13531 fontp->full_name = fontp->name;
13532
13533 fontp->size = font->max_bounds.width;
13534 fontp->height = FONT_HEIGHT (font);
13535
13536 if (NILP (font_names))
13537 {
13538 /* We come here because of a bug of XListFonts mentioned at
13539 the head of this block. Let's store this information in
13540 the cache for x_list_fonts. */
13541 Lisp_Object lispy_name = build_string (fontname);
13542 Lisp_Object lispy_full_name = build_string (fontp->full_name);
13543 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
13544 Qnil);
13545
13546 XCDR (dpyinfo->name_list_element)
13547 = Fcons (Fcons (key,
13548 Fcons (Fcons (lispy_full_name,
13549 make_number (fontp->size)),
13550 Qnil)),
13551 XCDR (dpyinfo->name_list_element));
13552 if (full_name)
13553 {
13554 key = Fcons (Fcons (lispy_full_name, make_number (256)),
13555 Qnil);
13556 XCDR (dpyinfo->name_list_element)
13557 = Fcons (Fcons (key,
13558 Fcons (Fcons (lispy_full_name,
13559 make_number (fontp->size)),
13560 Qnil)),
13561 XCDR (dpyinfo->name_list_element));
13562 }
13563 }
13564
13565 /* The slot `encoding' specifies how to map a character
13566 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
13567 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
13568 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
13569 2:0xA020..0xFF7F). For the moment, we don't know which charset
13570 uses this font. So, we set information in fontp->encoding[1]
13571 which is never used by any charset. If mapping can't be
13572 decided, set FONT_ENCODING_NOT_DECIDED. */
13573 fontp->encoding[1]
13574 = (font->max_byte1 == 0
13575 /* 1-byte font */
13576 ? (font->min_char_or_byte2 < 0x80
13577 ? (font->max_char_or_byte2 < 0x80
13578 ? 0 /* 0x20..0x7F */
13579 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
13580 : 1) /* 0xA0..0xFF */
13581 /* 2-byte font */
13582 : (font->min_byte1 < 0x80
13583 ? (font->max_byte1 < 0x80
13584 ? (font->min_char_or_byte2 < 0x80
13585 ? (font->max_char_or_byte2 < 0x80
13586 ? 0 /* 0x2020..0x7F7F */
13587 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
13588 : 3) /* 0x20A0..0x7FFF */
13589 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
13590 : (font->min_char_or_byte2 < 0x80
13591 ? (font->max_char_or_byte2 < 0x80
13592 ? 2 /* 0xA020..0xFF7F */
13593 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
13594 : 1))); /* 0xA0A0..0xFFFF */
13595
13596 fontp->baseline_offset
13597 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
13598 ? (long) value : 0);
13599 fontp->relative_compose
13600 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
13601 ? (long) value : 0);
13602 fontp->default_ascent
13603 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
13604 ? (long) value : 0);
13605
13606 /* Set global flag fonts_changed_p to non-zero if the font loaded
13607 has a character with a smaller width than any other character
13608 before, or if the font loaded has a smalle>r height than any
13609 other font loaded before. If this happens, it will make a
13610 glyph matrix reallocation necessary. */
13611 fonts_changed_p = x_compute_min_glyph_bounds (f);
13612 UNBLOCK_INPUT;
13613 return fontp;
13614 }
13615 }
13616
13617
13618 /* Return a pointer to struct font_info of a font named FONTNAME for
13619 frame F. If no such font is loaded, return NULL. */
13620
13621 struct font_info *
13622 x_query_font (f, fontname)
13623 struct frame *f;
13624 register char *fontname;
13625 {
13626 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13627 int i;
13628
13629 for (i = 0; i < dpyinfo->n_fonts; i++)
13630 if (dpyinfo->font_table[i].name
13631 && (!strcmp (dpyinfo->font_table[i].name, fontname)
13632 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
13633 return (dpyinfo->font_table + i);
13634 return NULL;
13635 }
13636
13637
13638 /* Find a CCL program for a font specified by FONTP, and set the member
13639 `encoder' of the structure. */
13640
13641 void
13642 x_find_ccl_program (fontp)
13643 struct font_info *fontp;
13644 {
13645 Lisp_Object list, elt;
13646
13647 elt = Qnil;
13648 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
13649 {
13650 elt = XCAR (list);
13651 if (CONSP (elt)
13652 && STRINGP (XCAR (elt))
13653 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
13654 >= 0)
13655 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
13656 >= 0)))
13657 break;
13658 }
13659
13660 if (! NILP (list))
13661 {
13662 struct ccl_program *ccl
13663 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
13664
13665 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
13666 xfree (ccl);
13667 else
13668 fontp->font_encoder = ccl;
13669 }
13670 }
13671
13672
13673 \f
13674 /***********************************************************************
13675 Initialization
13676 ***********************************************************************/
13677
13678 #ifdef USE_X_TOOLKIT
13679 static XrmOptionDescRec emacs_options[] = {
13680 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13681 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13682
13683 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13684 XrmoptionSepArg, NULL},
13685 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13686
13687 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13688 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13689 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13690 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13691 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13692 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13693 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13694 };
13695 #endif /* USE_X_TOOLKIT */
13696
13697 static int x_initialized;
13698
13699 #ifdef MULTI_KBOARD
13700 /* Test whether two display-name strings agree up to the dot that separates
13701 the screen number from the server number. */
13702 static int
13703 same_x_server (name1, name2)
13704 char *name1, *name2;
13705 {
13706 int seen_colon = 0;
13707 unsigned char *system_name = XSTRING (Vsystem_name)->data;
13708 int system_name_length = strlen (system_name);
13709 int length_until_period = 0;
13710
13711 while (system_name[length_until_period] != 0
13712 && system_name[length_until_period] != '.')
13713 length_until_period++;
13714
13715 /* Treat `unix' like an empty host name. */
13716 if (! strncmp (name1, "unix:", 5))
13717 name1 += 4;
13718 if (! strncmp (name2, "unix:", 5))
13719 name2 += 4;
13720 /* Treat this host's name like an empty host name. */
13721 if (! strncmp (name1, system_name, system_name_length)
13722 && name1[system_name_length] == ':')
13723 name1 += system_name_length;
13724 if (! strncmp (name2, system_name, system_name_length)
13725 && name2[system_name_length] == ':')
13726 name2 += system_name_length;
13727 /* Treat this host's domainless name like an empty host name. */
13728 if (! strncmp (name1, system_name, length_until_period)
13729 && name1[length_until_period] == ':')
13730 name1 += length_until_period;
13731 if (! strncmp (name2, system_name, length_until_period)
13732 && name2[length_until_period] == ':')
13733 name2 += length_until_period;
13734
13735 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13736 {
13737 if (*name1 == ':')
13738 seen_colon++;
13739 if (seen_colon && *name1 == '.')
13740 return 1;
13741 }
13742 return (seen_colon
13743 && (*name1 == '.' || *name1 == '\0')
13744 && (*name2 == '.' || *name2 == '\0'));
13745 }
13746 #endif
13747
13748 struct x_display_info *
13749 x_term_init (display_name, xrm_option, resource_name)
13750 Lisp_Object display_name;
13751 char *xrm_option;
13752 char *resource_name;
13753 {
13754 int connection;
13755 Display *dpy;
13756 struct x_display_info *dpyinfo;
13757 XrmDatabase xrdb;
13758
13759 BLOCK_INPUT;
13760
13761 if (!x_initialized)
13762 {
13763 x_initialize ();
13764 x_initialized = 1;
13765 }
13766
13767 #ifdef USE_X_TOOLKIT
13768 /* weiner@footloose.sps.mot.com reports that this causes
13769 errors with X11R5:
13770 X protocol error: BadAtom (invalid Atom parameter)
13771 on protocol request 18skiloaf.
13772 So let's not use it until R6. */
13773 #ifdef HAVE_X11XTR6
13774 XtSetLanguageProc (NULL, NULL, NULL);
13775 #endif
13776
13777 {
13778 int argc = 0;
13779 char *argv[3];
13780
13781 argv[0] = "";
13782 argc = 1;
13783 if (xrm_option)
13784 {
13785 argv[argc++] = "-xrm";
13786 argv[argc++] = xrm_option;
13787 }
13788 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
13789 resource_name, EMACS_CLASS,
13790 emacs_options, XtNumber (emacs_options),
13791 &argc, argv);
13792
13793 #ifdef HAVE_X11XTR6
13794 /* I think this is to compensate for XtSetLanguageProc. */
13795 fixup_locale ();
13796 #endif
13797 }
13798
13799 #else /* not USE_X_TOOLKIT */
13800 #ifdef HAVE_X11R5
13801 XSetLocaleModifiers ("");
13802 #endif
13803 dpy = XOpenDisplay (XSTRING (display_name)->data);
13804 #endif /* not USE_X_TOOLKIT */
13805
13806 /* Detect failure. */
13807 if (dpy == 0)
13808 {
13809 UNBLOCK_INPUT;
13810 return 0;
13811 }
13812
13813 /* We have definitely succeeded. Record the new connection. */
13814
13815 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
13816 bzero (dpyinfo, sizeof *dpyinfo);
13817
13818 #ifdef MULTI_KBOARD
13819 {
13820 struct x_display_info *share;
13821 Lisp_Object tail;
13822
13823 for (share = x_display_list, tail = x_display_name_list; share;
13824 share = share->next, tail = XCDR (tail))
13825 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
13826 XSTRING (display_name)->data))
13827 break;
13828 if (share)
13829 dpyinfo->kboard = share->kboard;
13830 else
13831 {
13832 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
13833 init_kboard (dpyinfo->kboard);
13834 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
13835 {
13836 char *vendor = ServerVendor (dpy);
13837 UNBLOCK_INPUT;
13838 dpyinfo->kboard->Vsystem_key_alist
13839 = call1 (Qvendor_specific_keysyms,
13840 build_string (vendor ? vendor : ""));
13841 BLOCK_INPUT;
13842 }
13843
13844 dpyinfo->kboard->next_kboard = all_kboards;
13845 all_kboards = dpyinfo->kboard;
13846 /* Don't let the initial kboard remain current longer than necessary.
13847 That would cause problems if a file loaded on startup tries to
13848 prompt in the mini-buffer. */
13849 if (current_kboard == initial_kboard)
13850 current_kboard = dpyinfo->kboard;
13851 }
13852 dpyinfo->kboard->reference_count++;
13853 }
13854 #endif
13855
13856 /* Put this display on the chain. */
13857 dpyinfo->next = x_display_list;
13858 x_display_list = dpyinfo;
13859
13860 /* Put it on x_display_name_list as well, to keep them parallel. */
13861 x_display_name_list = Fcons (Fcons (display_name, Qnil),
13862 x_display_name_list);
13863 dpyinfo->name_list_element = XCAR (x_display_name_list);
13864
13865 dpyinfo->display = dpy;
13866
13867 #if 0
13868 XSetAfterFunction (x_current_display, x_trace_wire);
13869 #endif /* ! 0 */
13870
13871 dpyinfo->x_id_name
13872 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13873 + STRING_BYTES (XSTRING (Vsystem_name))
13874 + 2);
13875 sprintf (dpyinfo->x_id_name, "%s@%s",
13876 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
13877
13878 /* Figure out which modifier bits mean what. */
13879 x_find_modifier_meanings (dpyinfo);
13880
13881 /* Get the scroll bar cursor. */
13882 dpyinfo->vertical_scroll_bar_cursor
13883 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
13884
13885 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13886 resource_name, EMACS_CLASS);
13887 #ifdef HAVE_XRMSETDATABASE
13888 XrmSetDatabase (dpyinfo->display, xrdb);
13889 #else
13890 dpyinfo->display->db = xrdb;
13891 #endif
13892 /* Put the rdb where we can find it in a way that works on
13893 all versions. */
13894 dpyinfo->xrdb = xrdb;
13895
13896 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13897 DefaultScreen (dpyinfo->display));
13898 select_visual (dpyinfo);
13899 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
13900 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13901 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13902 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13903 dpyinfo->grabbed = 0;
13904 dpyinfo->reference_count = 0;
13905 dpyinfo->icon_bitmap_id = -1;
13906 dpyinfo->font_table = NULL;
13907 dpyinfo->n_fonts = 0;
13908 dpyinfo->font_table_size = 0;
13909 dpyinfo->bitmaps = 0;
13910 dpyinfo->bitmaps_size = 0;
13911 dpyinfo->bitmaps_last = 0;
13912 dpyinfo->scratch_cursor_gc = 0;
13913 dpyinfo->mouse_face_mouse_frame = 0;
13914 dpyinfo->mouse_face_deferred_gc = 0;
13915 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13916 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
13917 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
13918 dpyinfo->mouse_face_window = Qnil;
13919 dpyinfo->mouse_face_overlay = Qnil;
13920 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13921 dpyinfo->mouse_face_defer = 0;
13922 dpyinfo->x_focus_frame = 0;
13923 dpyinfo->x_focus_event_frame = 0;
13924 dpyinfo->x_highlight_frame = 0;
13925 dpyinfo->image_cache = make_image_cache ();
13926
13927 /* See if a private colormap is requested. */
13928 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13929 {
13930 if (dpyinfo->visual->class == PseudoColor)
13931 {
13932 Lisp_Object value;
13933 value = display_x_get_resource (dpyinfo,
13934 build_string ("privateColormap"),
13935 build_string ("PrivateColormap"),
13936 Qnil, Qnil);
13937 if (STRINGP (value)
13938 && (!strcmp (XSTRING (value)->data, "true")
13939 || !strcmp (XSTRING (value)->data, "on")))
13940 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13941 }
13942 }
13943 else
13944 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13945 dpyinfo->visual, AllocNone);
13946
13947 {
13948 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13949 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13950 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13951 dpyinfo->resy = pixels * 25.4 / mm;
13952 pixels = DisplayWidth (dpyinfo->display, screen_number);
13953 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13954 dpyinfo->resx = pixels * 25.4 / mm;
13955 }
13956
13957 dpyinfo->Xatom_wm_protocols
13958 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13959 dpyinfo->Xatom_wm_take_focus
13960 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13961 dpyinfo->Xatom_wm_save_yourself
13962 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13963 dpyinfo->Xatom_wm_delete_window
13964 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13965 dpyinfo->Xatom_wm_change_state
13966 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13967 dpyinfo->Xatom_wm_configure_denied
13968 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13969 dpyinfo->Xatom_wm_window_moved
13970 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13971 dpyinfo->Xatom_editres
13972 = XInternAtom (dpyinfo->display, "Editres", False);
13973 dpyinfo->Xatom_CLIPBOARD
13974 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13975 dpyinfo->Xatom_TIMESTAMP
13976 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13977 dpyinfo->Xatom_TEXT
13978 = XInternAtom (dpyinfo->display, "TEXT", False);
13979 dpyinfo->Xatom_COMPOUND_TEXT
13980 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
13981 dpyinfo->Xatom_DELETE
13982 = XInternAtom (dpyinfo->display, "DELETE", False);
13983 dpyinfo->Xatom_MULTIPLE
13984 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13985 dpyinfo->Xatom_INCR
13986 = XInternAtom (dpyinfo->display, "INCR", False);
13987 dpyinfo->Xatom_EMACS_TMP
13988 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13989 dpyinfo->Xatom_TARGETS
13990 = XInternAtom (dpyinfo->display, "TARGETS", False);
13991 dpyinfo->Xatom_NULL
13992 = XInternAtom (dpyinfo->display, "NULL", False);
13993 dpyinfo->Xatom_ATOM_PAIR
13994 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
13995 /* For properties of font. */
13996 dpyinfo->Xatom_PIXEL_SIZE
13997 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13998 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13999 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
14000 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
14001 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
14002 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
14003 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
14004
14005 /* Ghostscript support. */
14006 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
14007 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
14008
14009 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
14010 False);
14011
14012 dpyinfo->cut_buffers_initialized = 0;
14013
14014 connection = ConnectionNumber (dpyinfo->display);
14015 dpyinfo->connection = connection;
14016
14017 {
14018 char null_bits[1];
14019
14020 null_bits[0] = 0x00;
14021
14022 dpyinfo->null_pixel
14023 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14024 null_bits, 1, 1, (long) 0, (long) 0,
14025 1);
14026 }
14027
14028 {
14029 extern int gray_bitmap_width, gray_bitmap_height;
14030 extern char *gray_bitmap_bits;
14031 dpyinfo->gray
14032 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14033 gray_bitmap_bits,
14034 gray_bitmap_width, gray_bitmap_height,
14035 (unsigned long) 1, (unsigned long) 0, 1);
14036 }
14037
14038 #ifdef HAVE_X_I18N
14039 xim_initialize (dpyinfo, resource_name);
14040 #endif
14041
14042 #ifdef subprocesses
14043 /* This is only needed for distinguishing keyboard and process input. */
14044 if (connection != 0)
14045 add_keyboard_wait_descriptor (connection);
14046 #endif
14047
14048 #ifndef F_SETOWN_BUG
14049 #ifdef F_SETOWN
14050 #ifdef F_SETOWN_SOCK_NEG
14051 /* stdin is a socket here */
14052 fcntl (connection, F_SETOWN, -getpid ());
14053 #else /* ! defined (F_SETOWN_SOCK_NEG) */
14054 fcntl (connection, F_SETOWN, getpid ());
14055 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
14056 #endif /* ! defined (F_SETOWN) */
14057 #endif /* F_SETOWN_BUG */
14058
14059 #ifdef SIGIO
14060 if (interrupt_input)
14061 init_sigio (connection);
14062 #endif /* ! defined (SIGIO) */
14063
14064 #ifdef USE_LUCID
14065 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
14066 /* Make sure that we have a valid font for dialog boxes
14067 so that Xt does not crash. */
14068 {
14069 Display *dpy = dpyinfo->display;
14070 XrmValue d, fr, to;
14071 Font font;
14072 int count;
14073
14074 d.addr = (XPointer)&dpy;
14075 d.size = sizeof (Display *);
14076 fr.addr = XtDefaultFont;
14077 fr.size = sizeof (XtDefaultFont);
14078 to.size = sizeof (Font *);
14079 to.addr = (XPointer)&font;
14080 count = x_catch_errors (dpy);
14081 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
14082 abort ();
14083 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
14084 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
14085 x_uncatch_errors (dpy, count);
14086 }
14087 #endif
14088 #endif
14089
14090 /* See if we should run in synchronous mode. This is useful
14091 for debugging X code. */
14092 {
14093 Lisp_Object value;
14094 value = display_x_get_resource (dpyinfo,
14095 build_string ("synchronous"),
14096 build_string ("Synchronous"),
14097 Qnil, Qnil);
14098 if (STRINGP (value)
14099 && (!strcmp (XSTRING (value)->data, "true")
14100 || !strcmp (XSTRING (value)->data, "on")))
14101 XSynchronize (dpyinfo->display, True);
14102 }
14103
14104 UNBLOCK_INPUT;
14105
14106 return dpyinfo;
14107 }
14108 \f
14109 /* Get rid of display DPYINFO, assuming all frames are already gone,
14110 and without sending any more commands to the X server. */
14111
14112 void
14113 x_delete_display (dpyinfo)
14114 struct x_display_info *dpyinfo;
14115 {
14116 delete_keyboard_wait_descriptor (dpyinfo->connection);
14117
14118 /* Discard this display from x_display_name_list and x_display_list.
14119 We can't use Fdelq because that can quit. */
14120 if (! NILP (x_display_name_list)
14121 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
14122 x_display_name_list = XCDR (x_display_name_list);
14123 else
14124 {
14125 Lisp_Object tail;
14126
14127 tail = x_display_name_list;
14128 while (CONSP (tail) && CONSP (XCDR (tail)))
14129 {
14130 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
14131 {
14132 XCDR (tail) = XCDR (XCDR (tail));
14133 break;
14134 }
14135 tail = XCDR (tail);
14136 }
14137 }
14138
14139 if (next_noop_dpyinfo == dpyinfo)
14140 next_noop_dpyinfo = dpyinfo->next;
14141
14142 if (x_display_list == dpyinfo)
14143 x_display_list = dpyinfo->next;
14144 else
14145 {
14146 struct x_display_info *tail;
14147
14148 for (tail = x_display_list; tail; tail = tail->next)
14149 if (tail->next == dpyinfo)
14150 tail->next = tail->next->next;
14151 }
14152
14153 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
14154 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
14155 XrmDestroyDatabase (dpyinfo->xrdb);
14156 #endif
14157 #endif
14158 #ifdef MULTI_KBOARD
14159 if (--dpyinfo->kboard->reference_count == 0)
14160 delete_kboard (dpyinfo->kboard);
14161 #endif
14162 #ifdef HAVE_X_I18N
14163 if (dpyinfo->xim)
14164 xim_close_dpy (dpyinfo);
14165 #endif
14166
14167 xfree (dpyinfo->font_table);
14168 xfree (dpyinfo->x_id_name);
14169 xfree (dpyinfo->color_cells);
14170 xfree (dpyinfo);
14171 }
14172
14173 \f
14174 /* Set up use of X before we make the first connection. */
14175
14176 static struct redisplay_interface x_redisplay_interface =
14177 {
14178 x_produce_glyphs,
14179 x_write_glyphs,
14180 x_insert_glyphs,
14181 x_clear_end_of_line,
14182 x_scroll_run,
14183 x_after_update_window_line,
14184 x_update_window_begin,
14185 x_update_window_end,
14186 XTcursor_to,
14187 x_flush,
14188 x_clear_mouse_face,
14189 x_get_glyph_overhangs,
14190 x_fix_overlapping_area
14191 };
14192
14193 void
14194 x_initialize ()
14195 {
14196 rif = &x_redisplay_interface;
14197
14198 clear_frame_hook = x_clear_frame;
14199 ins_del_lines_hook = x_ins_del_lines;
14200 change_line_highlight_hook = x_change_line_highlight;
14201 delete_glyphs_hook = x_delete_glyphs;
14202 ring_bell_hook = XTring_bell;
14203 reset_terminal_modes_hook = XTreset_terminal_modes;
14204 set_terminal_modes_hook = XTset_terminal_modes;
14205 update_begin_hook = x_update_begin;
14206 update_end_hook = x_update_end;
14207 set_terminal_window_hook = XTset_terminal_window;
14208 read_socket_hook = XTread_socket;
14209 frame_up_to_date_hook = XTframe_up_to_date;
14210 reassert_line_highlight_hook = XTreassert_line_highlight;
14211 mouse_position_hook = XTmouse_position;
14212 frame_rehighlight_hook = XTframe_rehighlight;
14213 frame_raise_lower_hook = XTframe_raise_lower;
14214 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
14215 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
14216 redeem_scroll_bar_hook = XTredeem_scroll_bar;
14217 judge_scroll_bars_hook = XTjudge_scroll_bars;
14218 estimate_mode_line_height_hook = x_estimate_mode_line_height;
14219
14220 scroll_region_ok = 1; /* we'll scroll partial frames */
14221 char_ins_del_ok = 1;
14222 line_ins_del_ok = 1; /* we'll just blt 'em */
14223 fast_clear_end_of_line = 1; /* X does this well */
14224 memory_below_frame = 0; /* we don't remember what scrolls
14225 off the bottom */
14226 baud_rate = 19200;
14227
14228 x_noop_count = 0;
14229 last_tool_bar_item = -1;
14230 any_help_event_p = 0;
14231
14232 /* Try to use interrupt input; if we can't, then start polling. */
14233 Fset_input_mode (Qt, Qnil, Qt, Qnil);
14234
14235 #ifdef USE_X_TOOLKIT
14236 XtToolkitInitialize ();
14237 Xt_app_con = XtCreateApplicationContext ();
14238 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
14239
14240 /* Install an asynchronous timer that processes Xt timeout events
14241 every 0.1s. This is necessary because some widget sets use
14242 timeouts internally, for example the LessTif menu bar, or the
14243 Xaw3d scroll bar. When Xt timouts aren't processed, these
14244 widgets don't behave normally. */
14245 {
14246 EMACS_TIME interval;
14247 EMACS_SET_SECS_USECS (interval, 0, 100000);
14248 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
14249 }
14250 #endif
14251
14252 #ifdef USE_TOOLKIT_SCROLL_BARS
14253 xaw3d_arrow_scroll = False;
14254 xaw3d_pick_top = True;
14255 #endif
14256
14257 /* Note that there is no real way portable across R3/R4 to get the
14258 original error handler. */
14259 XSetErrorHandler (x_error_handler);
14260 XSetIOErrorHandler (x_io_error_quitter);
14261
14262 /* Disable Window Change signals; they are handled by X events. */
14263 #ifdef SIGWINCH
14264 signal (SIGWINCH, SIG_DFL);
14265 #endif /* ! defined (SIGWINCH) */
14266
14267 signal (SIGPIPE, x_connection_signal);
14268 }
14269
14270
14271 void
14272 syms_of_xterm ()
14273 {
14274 staticpro (&x_error_message_string);
14275 x_error_message_string = Qnil;
14276
14277 staticpro (&x_display_name_list);
14278 x_display_name_list = Qnil;
14279
14280 staticpro (&last_mouse_scroll_bar);
14281 last_mouse_scroll_bar = Qnil;
14282
14283 staticpro (&Qvendor_specific_keysyms);
14284 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
14285
14286 staticpro (&last_mouse_press_frame);
14287 last_mouse_press_frame = Qnil;
14288
14289 help_echo = Qnil;
14290 staticpro (&help_echo);
14291 help_echo_object = Qnil;
14292 staticpro (&help_echo_object);
14293 help_echo_window = Qnil;
14294 staticpro (&help_echo_window);
14295 previous_help_echo = Qnil;
14296 staticpro (&previous_help_echo);
14297 help_echo_pos = -1;
14298
14299 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
14300 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
14301 For example, if a block cursor is over a tab, it will be drawn as\n\
14302 wide as that tab on the display.");
14303 x_stretch_cursor_p = 0;
14304
14305 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
14306 "What X toolkit scroll bars Emacs uses.\n\
14307 A value of nil means Emacs doesn't use X toolkit scroll bars.\n\
14308 Otherwise, value is a symbol describing the X toolkit.");
14309 #ifdef USE_TOOLKIT_SCROLL_BARS
14310 #ifdef USE_MOTIF
14311 Vx_toolkit_scroll_bars = intern ("motif");
14312 #elif defined HAVE_XAW3D
14313 Vx_toolkit_scroll_bars = intern ("xaw3d");
14314 #else
14315 Vx_toolkit_scroll_bars = intern ("xaw");
14316 #endif
14317 #else
14318 Vx_toolkit_scroll_bars = Qnil;
14319 #endif
14320
14321 staticpro (&last_mouse_motion_frame);
14322 last_mouse_motion_frame = Qnil;
14323 }
14324
14325 #endif /* not HAVE_X_WINDOWS */