]> code.delx.au - gnu-emacs/blob - src/xterm.c
(XTread_socket): Check Lisp types for Vx_keysym_table
[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, 01, 02
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 "character.h"
72 #include "coding.h"
73 #include "ccl.h"
74 #include "frame.h"
75 #include "dispextern.h"
76 #include "fontset.h"
77 #include "termhooks.h"
78 #include "termopts.h"
79 #include "termchar.h"
80 #include "gnu.h"
81 #include "disptab.h"
82 #include "buffer.h"
83 #include "window.h"
84 #include "keyboard.h"
85 #include "intervals.h"
86 #include "process.h"
87 #include "atimer.h"
88 #include "keymap.h"
89
90 #ifdef USE_X_TOOLKIT
91 #include <X11/Shell.h>
92 #endif
93
94 #ifdef HAVE_SYS_TIME_H
95 #include <sys/time.h>
96 #endif
97 #ifdef HAVE_UNISTD_H
98 #include <unistd.h>
99 #endif
100
101 #ifdef USE_LUCID
102 extern int xlwmenu_window_p P_ ((Widget w, Window window));
103 extern void xlwmenu_redisplay P_ ((Widget));
104 #endif
105
106 #ifdef USE_X_TOOLKIT
107
108 extern void free_frame_menubar P_ ((struct frame *));
109 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
110 int));
111
112 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
113 #define HACK_EDITRES
114 extern void _XEditResCheckMessages ();
115 #endif /* not NO_EDITRES */
116
117 /* Include toolkit specific headers for the scroll bar widget. */
118
119 #ifdef USE_TOOLKIT_SCROLL_BARS
120 #if defined USE_MOTIF
121 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
122 #include <Xm/ScrollBar.h>
123 #else /* !USE_MOTIF i.e. use Xaw */
124
125 #ifdef HAVE_XAW3D
126 #include <X11/Xaw3d/Simple.h>
127 #include <X11/Xaw3d/Scrollbar.h>
128 #define ARROW_SCROLLBAR
129 #include <X11/Xaw3d/ScrollbarP.h>
130 #else /* !HAVE_XAW3D */
131 #include <X11/Xaw/Simple.h>
132 #include <X11/Xaw/Scrollbar.h>
133 #endif /* !HAVE_XAW3D */
134 #ifndef XtNpickTop
135 #define XtNpickTop "pickTop"
136 #endif /* !XtNpickTop */
137 #endif /* !USE_MOTIF */
138 #endif /* USE_TOOLKIT_SCROLL_BARS */
139
140 #endif /* USE_X_TOOLKIT */
141
142 #ifndef USE_X_TOOLKIT
143 #define x_any_window_to_frame x_window_to_frame
144 #define x_top_window_to_frame x_window_to_frame
145 #endif
146
147 #ifdef USE_X_TOOLKIT
148 #include "widget.h"
149 #ifndef XtNinitialState
150 #define XtNinitialState "initialState"
151 #endif
152 #endif
153
154 #ifdef HAVE_X11R6
155 /* This isn't prototyped in OSF 5.0 or or XFree 4.1. */
156 extern char * XSetIMValues P_ ((XIM, ...));
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 /* Fringe bitmaps. */
165
166 enum fringe_bitmap_type
167 {
168 NO_FRINGE_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 72
182 #define zv_period 3
183 static unsigned char zv_bits[] = {
184 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
185 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
186 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
187 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
188 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
189 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
190 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
191 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
192
193 /* An arrow like this: `<-'. */
194
195 #define left_width 8
196 #define left_height 8
197 static unsigned char left_bits[] = {
198 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
199
200 /* Right truncation arrow bitmap `->'. */
201
202 #define right_width 8
203 #define right_height 8
204 static unsigned char right_bits[] = {
205 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
206
207 /* Marker for continued lines. */
208
209 #define continued_width 8
210 #define continued_height 8
211 static unsigned char continued_bits[] = {
212 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
213
214 /* Marker for continuation lines. */
215
216 #define continuation_width 8
217 #define continuation_height 8
218 static unsigned char continuation_bits[] = {
219 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
220
221 /* Overlay arrow bitmap. */
222
223 #if 0
224 /* A bomb. */
225 #define ov_width 8
226 #define ov_height 8
227 static unsigned char ov_bits[] = {
228 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
229 #else
230 /* A triangular arrow. */
231 #define ov_width 8
232 #define ov_height 8
233 static unsigned char ov_bits[] = {
234 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
235
236 #endif
237
238 extern Lisp_Object Qhelp_echo;
239
240 /* Default to using XIM if available. */
241 int use_xim = 1;
242 \f
243 /* Non-nil means Emacs uses toolkit scroll bars. */
244
245 Lisp_Object Vx_toolkit_scroll_bars;
246
247 /* If a string, XTread_socket generates an event to display that string.
248 (The display is done in read_char.) */
249
250 static Lisp_Object help_echo;
251 static Lisp_Object help_echo_window;
252 static Lisp_Object help_echo_object;
253 static int help_echo_pos;
254
255 /* Temporary variable for XTread_socket. */
256
257 static Lisp_Object previous_help_echo;
258
259 /* Non-zero means that a HELP_EVENT has been generated since Emacs
260 start. */
261
262 static int any_help_event_p;
263
264 /* Non-zero means autoselect window with the mouse cursor. */
265
266 int x_autoselect_window_p;
267
268 /* Non-zero means draw block and hollow cursor as wide as the glyph
269 under it. For example, if a block cursor is over a tab, it will be
270 drawn as wide as that tab on the display. */
271
272 int x_stretch_cursor_p;
273
274 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
275
276 int x_use_underline_position_properties;
277
278 /* This is a chain of structures for all the X displays currently in
279 use. */
280
281 struct x_display_info *x_display_list;
282
283 /* This is a list of cons cells, each of the form (NAME
284 . FONT-LIST-CACHE), one for each element of x_display_list and in
285 the same order. NAME is the name of the frame. FONT-LIST-CACHE
286 records previous values returned by x-list-fonts. */
287
288 Lisp_Object x_display_name_list;
289
290 /* Frame being updated by update_frame. This is declared in term.c.
291 This is set by update_begin and looked at by all the XT functions.
292 It is zero while not inside an update. In that case, the XT
293 functions assume that `selected_frame' is the frame to apply to. */
294
295 extern struct frame *updating_frame;
296
297 /* This is a frame waiting to be auto-raised, within XTread_socket. */
298
299 struct frame *pending_autoraise_frame;
300
301 #ifdef USE_X_TOOLKIT
302 /* The application context for Xt use. */
303 XtAppContext Xt_app_con;
304 static String Xt_default_resources[] = {0};
305 #endif /* USE_X_TOOLKIT */
306
307 /* Nominal cursor position -- where to draw output.
308 HPOS and VPOS are window relative glyph matrix coordinates.
309 X and Y are window relative pixel coordinates. */
310
311 struct cursor_pos output_cursor;
312
313 /* Non-zero means user is interacting with a toolkit scroll bar. */
314
315 static int toolkit_scroll_bar_interaction;
316
317 /* Mouse movement.
318
319 Formerly, we used PointerMotionHintMask (in standard_event_mask)
320 so that we would have to call XQueryPointer after each MotionNotify
321 event to ask for another such event. However, this made mouse tracking
322 slow, and there was a bug that made it eventually stop.
323
324 Simply asking for MotionNotify all the time seems to work better.
325
326 In order to avoid asking for motion events and then throwing most
327 of them away or busy-polling the server for mouse positions, we ask
328 the server for pointer motion hints. This means that we get only
329 one event per group of mouse movements. "Groups" are delimited by
330 other kinds of events (focus changes and button clicks, for
331 example), or by XQueryPointer calls; when one of these happens, we
332 get another MotionNotify event the next time the mouse moves. This
333 is at least as efficient as getting motion events when mouse
334 tracking is on, and I suspect only negligibly worse when tracking
335 is off. */
336
337 /* Where the mouse was last time we reported a mouse event. */
338
339 FRAME_PTR last_mouse_frame;
340 static XRectangle last_mouse_glyph;
341 static Lisp_Object last_mouse_press_frame;
342
343 /* The scroll bar in which the last X motion event occurred.
344
345 If the last X motion event occurred in a scroll bar, we set this so
346 XTmouse_position can know whether to report a scroll bar motion or
347 an ordinary motion.
348
349 If the last X motion event didn't occur in a scroll bar, we set
350 this to Qnil, to tell XTmouse_position to return an ordinary motion
351 event. */
352
353 static Lisp_Object last_mouse_scroll_bar;
354
355 /* This is a hack. We would really prefer that XTmouse_position would
356 return the time associated with the position it returns, but there
357 doesn't seem to be any way to wrest the time-stamp from the server
358 along with the position query. So, we just keep track of the time
359 of the last movement we received, and return that in hopes that
360 it's somewhat accurate. */
361
362 static Time last_mouse_movement_time;
363
364 /* Incremented by XTread_socket whenever it really tries to read
365 events. */
366
367 #ifdef __STDC__
368 static int volatile input_signal_count;
369 #else
370 static int input_signal_count;
371 #endif
372
373 /* Used locally within XTread_socket. */
374
375 static int x_noop_count;
376
377 /* Initial values of argv and argc. */
378
379 extern char **initial_argv;
380 extern int initial_argc;
381
382 extern Lisp_Object Vcommand_line_args, Vsystem_name;
383
384 /* Tells if a window manager is present or not. */
385
386 extern Lisp_Object Vx_no_window_manager;
387
388 extern Lisp_Object Qface, Qmouse_face, Qeql;
389
390 extern int errno;
391
392 /* A mask of extra modifier bits to put into every keyboard char. */
393
394 extern int extra_keyboard_modifiers;
395
396 /* The keysyms to use for the various modifiers. */
397
398 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
399 Lisp_Object Vx_keysym_table;
400 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
401
402 static Lisp_Object Qvendor_specific_keysyms;
403
404 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
405 extern Lisp_Object x_icon_type P_ ((struct frame *));
406
407
408 /* Enumeration for overriding/changing the face to use for drawing
409 glyphs in x_draw_glyphs. */
410
411 enum draw_glyphs_face
412 {
413 DRAW_NORMAL_TEXT,
414 DRAW_INVERSE_VIDEO,
415 DRAW_CURSOR,
416 DRAW_MOUSE_FACE,
417 DRAW_IMAGE_RAISED,
418 DRAW_IMAGE_SUNKEN
419 };
420
421 static int cursor_in_mouse_face_p P_ ((struct window *));
422 static int clear_mouse_face P_ ((struct x_display_info *));
423 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
424 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
425 static const XColor *x_color_cells P_ ((Display *, int *));
426 static void x_update_window_end P_ ((struct window *, int, int));
427 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
428 void x_delete_display P_ ((struct x_display_info *));
429 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
430 unsigned));
431 static int fast_find_position P_ ((struct window *, int, int *, int *,
432 int *, int *, Lisp_Object));
433 static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
434 int *, int *, int *, int *, int));
435 static void set_output_cursor P_ ((struct cursor_pos *));
436 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
437 int *, int *, int *, int));
438 static void note_mode_line_highlight P_ ((struct window *, int, int));
439 static void note_mouse_highlight P_ ((struct frame *, int, int));
440 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
441 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
442 static void show_mouse_face P_ ((struct x_display_info *,
443 enum draw_glyphs_face));
444 static int x_io_error_quitter P_ ((Display *));
445 int x_catch_errors P_ ((Display *));
446 void x_uncatch_errors P_ ((Display *, int));
447 void x_lower_frame P_ ((struct frame *));
448 void x_scroll_bar_clear P_ ((struct frame *));
449 int x_had_errors_p P_ ((Display *));
450 void x_wm_set_size_hint P_ ((struct frame *, long, int));
451 void x_raise_frame P_ ((struct frame *));
452 void x_set_window_size P_ ((struct frame *, int, int, int));
453 void x_wm_set_window_state P_ ((struct frame *, int));
454 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
455 void x_initialize P_ ((void));
456 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
457 static int x_compute_min_glyph_bounds P_ ((struct frame *));
458 static void x_draw_phys_cursor_glyph P_ ((struct window *,
459 struct glyph_row *,
460 enum draw_glyphs_face));
461 static void x_update_end P_ ((struct frame *));
462 static void XTframe_up_to_date P_ ((struct frame *));
463 static void XTset_terminal_modes P_ ((void));
464 static void XTreset_terminal_modes P_ ((void));
465 static void XTcursor_to P_ ((int, int, int, int));
466 static void x_write_glyphs P_ ((struct glyph *, int));
467 static void x_clear_end_of_line P_ ((int));
468 static void x_clear_frame P_ ((void));
469 static void x_clear_cursor P_ ((struct window *));
470 static void frame_highlight P_ ((struct frame *));
471 static void frame_unhighlight P_ ((struct frame *));
472 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
473 static void XTframe_rehighlight P_ ((struct frame *));
474 static void x_frame_rehighlight P_ ((struct x_display_info *));
475 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
476 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
477 static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
478 XRectangle *));
479 static void expose_frame P_ ((struct frame *, int, int, int, int));
480 static int expose_window_tree P_ ((struct window *, XRectangle *));
481 static int expose_window P_ ((struct window *, XRectangle *));
482 static void expose_area P_ ((struct window *, struct glyph_row *,
483 XRectangle *, enum glyph_row_area));
484 static int expose_line P_ ((struct window *, struct glyph_row *,
485 XRectangle *));
486 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
487 static void x_update_window_cursor P_ ((struct window *, int));
488 static void x_erase_phys_cursor P_ ((struct window *));
489 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
490 static void x_draw_fringe_bitmap P_ ((struct window *, struct glyph_row *,
491 enum fringe_bitmap_type, int left_p));
492
493 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
494 GC, int));
495 static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
496 static void x_draw_row_fringe_bitmaps P_ ((struct window *, struct glyph_row *));
497 static void notice_overwritten_cursor P_ ((struct window *, enum glyph_row_area,
498 int, int, int, int));
499 static void x_flush P_ ((struct frame *f));
500 static void x_update_begin P_ ((struct frame *));
501 static void x_update_window_begin P_ ((struct window *));
502 static void x_draw_vertical_border P_ ((struct window *));
503 static void x_after_update_window_line P_ ((struct glyph_row *));
504 static INLINE void take_vertical_position_into_account P_ ((struct it *));
505 static void x_produce_stretch_glyph P_ ((struct it *));
506 static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
507 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
508 enum scroll_bar_part *,
509 Lisp_Object *, Lisp_Object *,
510 unsigned long *));
511 static void x_check_fullscreen P_ ((struct frame *));
512 static void x_check_fullscreen_move P_ ((struct frame *));
513
514 /* Flush display of frame F, or of all frames if F is null. */
515
516 static void
517 x_flush (f)
518 struct frame *f;
519 {
520 BLOCK_INPUT;
521 if (f == NULL)
522 {
523 Lisp_Object rest, frame;
524 FOR_EACH_FRAME (rest, frame)
525 x_flush (XFRAME (frame));
526 }
527 else if (FRAME_X_P (f))
528 XFlush (FRAME_X_DISPLAY (f));
529 UNBLOCK_INPUT;
530 }
531
532
533 /* Remove calls to XFlush by defining XFlush to an empty replacement.
534 Calls to XFlush should be unnecessary because the X output buffer
535 is flushed automatically as needed by calls to XPending,
536 XNextEvent, or XWindowEvent according to the XFlush man page.
537 XTread_socket calls XPending. Removing XFlush improves
538 performance. */
539
540 #define XFlush(DISPLAY) (void) 0
541
542 \f
543 /***********************************************************************
544 Debugging
545 ***********************************************************************/
546
547 #if 0
548
549 /* This is a function useful for recording debugging information about
550 the sequence of occurrences in this file. */
551
552 struct record
553 {
554 char *locus;
555 int type;
556 };
557
558 struct record event_record[100];
559
560 int event_record_index;
561
562 record_event (locus, type)
563 char *locus;
564 int type;
565 {
566 if (event_record_index == sizeof (event_record) / sizeof (struct record))
567 event_record_index = 0;
568
569 event_record[event_record_index].locus = locus;
570 event_record[event_record_index].type = type;
571 event_record_index++;
572 }
573
574 #endif /* 0 */
575
576
577 \f
578 /* Return the struct x_display_info corresponding to DPY. */
579
580 struct x_display_info *
581 x_display_info_for_display (dpy)
582 Display *dpy;
583 {
584 struct x_display_info *dpyinfo;
585
586 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
587 if (dpyinfo->display == dpy)
588 return dpyinfo;
589
590 return 0;
591 }
592
593
594 \f
595 /***********************************************************************
596 Starting and ending an update
597 ***********************************************************************/
598
599 /* Start an update of frame F. This function is installed as a hook
600 for update_begin, i.e. it is called when update_begin is called.
601 This function is called prior to calls to x_update_window_begin for
602 each window being updated. Currently, there is nothing to do here
603 because all interesting stuff is done on a window basis. */
604
605 static void
606 x_update_begin (f)
607 struct frame *f;
608 {
609 /* Nothing to do. */
610 }
611
612
613 /* Start update of window W. Set the global variable updated_window
614 to the window being updated and set output_cursor to the cursor
615 position of W. */
616
617 static void
618 x_update_window_begin (w)
619 struct window *w;
620 {
621 struct frame *f = XFRAME (WINDOW_FRAME (w));
622 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
623
624 updated_window = w;
625 set_output_cursor (&w->cursor);
626
627 BLOCK_INPUT;
628
629 if (f == display_info->mouse_face_mouse_frame)
630 {
631 /* Don't do highlighting for mouse motion during the update. */
632 display_info->mouse_face_defer = 1;
633
634 /* If F needs to be redrawn, simply forget about any prior mouse
635 highlighting. */
636 if (FRAME_GARBAGED_P (f))
637 display_info->mouse_face_window = Qnil;
638
639 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
640 their mouse_face_p flag set, which means that they are always
641 unequal to rows in a desired matrix which never have that
642 flag set. So, rows containing mouse-face glyphs are never
643 scrolled, and we don't have to switch the mouse highlight off
644 here to prevent it from being scrolled. */
645
646 /* Can we tell that this update does not affect the window
647 where the mouse highlight is? If so, no need to turn off.
648 Likewise, don't do anything if the frame is garbaged;
649 in that case, the frame's current matrix that we would use
650 is all wrong, and we will redisplay that line anyway. */
651 if (!NILP (display_info->mouse_face_window)
652 && w == XWINDOW (display_info->mouse_face_window))
653 {
654 int i;
655
656 for (i = 0; i < w->desired_matrix->nrows; ++i)
657 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
658 break;
659
660 if (i < w->desired_matrix->nrows)
661 clear_mouse_face (display_info);
662 }
663 #endif /* 0 */
664 }
665
666 UNBLOCK_INPUT;
667 }
668
669
670 /* Draw a vertical window border to the right of window W if W doesn't
671 have vertical scroll bars. */
672
673 static void
674 x_draw_vertical_border (w)
675 struct window *w;
676 {
677 struct frame *f = XFRAME (WINDOW_FRAME (w));
678
679 /* Redraw borders between horizontally adjacent windows. Don't
680 do it for frames with vertical scroll bars because either the
681 right scroll bar of a window, or the left scroll bar of its
682 neighbor will suffice as a border. */
683 if (!WINDOW_RIGHTMOST_P (w)
684 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
685 {
686 int x0, x1, y0, y1;
687
688 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
689 x1 += FRAME_X_RIGHT_FRINGE_WIDTH (f);
690 y1 -= 1;
691
692 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
693 f->output_data.x->normal_gc, x1, y0, x1, y1);
694 }
695 }
696
697
698 /* End update of window W (which is equal to updated_window).
699
700 Draw vertical borders between horizontally adjacent windows, and
701 display W's cursor if CURSOR_ON_P is non-zero.
702
703 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
704 glyphs in mouse-face were overwritten. In that case we have to
705 make sure that the mouse-highlight is properly redrawn.
706
707 W may be a menu bar pseudo-window in case we don't have X toolkit
708 support. Such windows don't have a cursor, so don't display it
709 here. */
710
711 static void
712 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
713 struct window *w;
714 int cursor_on_p, mouse_face_overwritten_p;
715 {
716 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
717
718 if (!w->pseudo_window_p)
719 {
720 BLOCK_INPUT;
721
722 if (cursor_on_p)
723 x_display_and_set_cursor (w, 1, output_cursor.hpos,
724 output_cursor.vpos,
725 output_cursor.x, output_cursor.y);
726
727 x_draw_vertical_border (w);
728 UNBLOCK_INPUT;
729 }
730
731 /* If a row with mouse-face was overwritten, arrange for
732 XTframe_up_to_date to redisplay the mouse highlight. */
733 if (mouse_face_overwritten_p)
734 {
735 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
736 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
737 dpyinfo->mouse_face_window = Qnil;
738 }
739
740 updated_window = NULL;
741 }
742
743
744 /* End update of frame F. This function is installed as a hook in
745 update_end. */
746
747 static void
748 x_update_end (f)
749 struct frame *f;
750 {
751 /* Mouse highlight may be displayed again. */
752 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
753
754 BLOCK_INPUT;
755 XFlush (FRAME_X_DISPLAY (f));
756 UNBLOCK_INPUT;
757 }
758
759
760 /* This function is called from various places in xdisp.c whenever a
761 complete update has been performed. The global variable
762 updated_window is not available here. */
763
764 static void
765 XTframe_up_to_date (f)
766 struct frame *f;
767 {
768 if (FRAME_X_P (f))
769 {
770 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
771
772 if (dpyinfo->mouse_face_deferred_gc
773 || f == dpyinfo->mouse_face_mouse_frame)
774 {
775 BLOCK_INPUT;
776 if (dpyinfo->mouse_face_mouse_frame)
777 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
778 dpyinfo->mouse_face_mouse_x,
779 dpyinfo->mouse_face_mouse_y);
780 dpyinfo->mouse_face_deferred_gc = 0;
781 UNBLOCK_INPUT;
782 }
783 }
784 }
785
786
787 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
788 arrow bitmaps, or clear the fringes if no bitmaps are required
789 before DESIRED_ROW is made current. The window being updated is
790 found in updated_window. This function It is called from
791 update_window_line only if it is known that there are differences
792 between bitmaps to be drawn between current row and DESIRED_ROW. */
793
794 static void
795 x_after_update_window_line (desired_row)
796 struct glyph_row *desired_row;
797 {
798 struct window *w = updated_window;
799 struct frame *f;
800 int width, height;
801
802 xassert (w);
803
804 if (!desired_row->mode_line_p && !w->pseudo_window_p)
805 {
806 BLOCK_INPUT;
807 x_draw_row_fringe_bitmaps (w, desired_row);
808 UNBLOCK_INPUT;
809 }
810
811 /* When a window has disappeared, make sure that no rest of
812 full-width rows stays visible in the internal border. Could
813 check here if updated_window is the leftmost/rightmost window,
814 but I guess it's not worth doing since vertically split windows
815 are almost never used, internal border is rarely set, and the
816 overhead is very small. */
817 if (windows_or_buffers_changed
818 && desired_row->full_width_p
819 && (f = XFRAME (w->frame),
820 width = FRAME_INTERNAL_BORDER_WIDTH (f),
821 width != 0)
822 && (height = desired_row->visible_height,
823 height > 0))
824 {
825 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
826
827 /* Internal border is drawn below the tool bar. */
828 if (WINDOWP (f->tool_bar_window)
829 && w == XWINDOW (f->tool_bar_window))
830 y -= width;
831
832 BLOCK_INPUT;
833 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
834 0, y, width, height, False);
835 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
836 f->output_data.x->pixel_width - width,
837 y, width, height, False);
838 UNBLOCK_INPUT;
839 }
840 }
841
842
843 /* Draw the bitmap WHICH in one of the left or right fringes of
844 window W. ROW is the glyph row for which to display the bitmap; it
845 determines the vertical position at which the bitmap has to be
846 drawn. */
847
848 static void
849 x_draw_fringe_bitmap (w, row, which, left_p)
850 struct window *w;
851 struct glyph_row *row;
852 enum fringe_bitmap_type which;
853 int left_p;
854 {
855 struct frame *f = XFRAME (WINDOW_FRAME (w));
856 Display *display = FRAME_X_DISPLAY (f);
857 Window window = FRAME_X_WINDOW (f);
858 int x, y, wd, h, dy;
859 int b1, b2;
860 unsigned char *bits;
861 Pixmap pixmap;
862 GC gc = f->output_data.x->normal_gc;
863 struct face *face;
864 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
865
866 /* Must clip because of partially visible lines. */
867 x_clip_to_row (w, row, gc, 1);
868
869 /* Convert row to frame coordinates. */
870 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
871
872 switch (which)
873 {
874 case NO_FRINGE_BITMAP:
875 wd = 0;
876 h = 0;
877 break;
878
879 case LEFT_TRUNCATION_BITMAP:
880 wd = left_width;
881 h = left_height;
882 bits = left_bits;
883 break;
884
885 case OVERLAY_ARROW_BITMAP:
886 wd = ov_width;
887 h = ov_height;
888 bits = ov_bits;
889 break;
890
891 case RIGHT_TRUNCATION_BITMAP:
892 wd = right_width;
893 h = right_height;
894 bits = right_bits;
895 break;
896
897 case CONTINUED_LINE_BITMAP:
898 wd = continued_width;
899 h = continued_height;
900 bits = continued_bits;
901 break;
902
903 case CONTINUATION_LINE_BITMAP:
904 wd = continuation_width;
905 h = continuation_height;
906 bits = continuation_bits;
907 break;
908
909 case ZV_LINE_BITMAP:
910 wd = zv_width;
911 h = zv_height - (y % zv_period);
912 bits = zv_bits + (y % zv_period);
913 break;
914
915 default:
916 abort ();
917 }
918
919 /* Clip bitmap if too high. */
920 if (h > row->height)
921 h = row->height;
922
923 /* Set dy to the offset in the row to start drawing the bitmap. */
924 dy = (row->height - h) / 2;
925
926 face = FACE_FROM_ID (f, FRINGE_FACE_ID);
927 PREPARE_FACE_FOR_DISPLAY (f, face);
928
929 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
930 the fringe. */
931 b1 = b2 = -1;
932 if (left_p)
933 {
934 if (wd > FRAME_X_LEFT_FRINGE_WIDTH (f))
935 wd = FRAME_X_LEFT_FRINGE_WIDTH (f);
936 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
937 - wd
938 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - wd) / 2);
939 if (wd < FRAME_X_LEFT_FRINGE_WIDTH (f) || row->height > h)
940 {
941 /* If W has a vertical border to its left, don't draw over it. */
942 int border = ((XFASTINT (w->left) > 0
943 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
944 ? 1 : 0);
945 b1 = (window_box_left (w, -1)
946 - FRAME_X_LEFT_FRINGE_WIDTH (f)
947 + border);
948 b2 = (FRAME_X_LEFT_FRINGE_WIDTH (f) - border);
949 }
950 }
951 else
952 {
953 if (wd > FRAME_X_RIGHT_FRINGE_WIDTH (f))
954 wd = FRAME_X_RIGHT_FRINGE_WIDTH (f);
955 x = (window_box_right (w, -1)
956 + (FRAME_X_RIGHT_FRINGE_WIDTH (f) - wd) / 2);
957 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
958 the fringe. */
959 if (wd < FRAME_X_RIGHT_FRINGE_WIDTH (f) || row->height > h)
960 {
961 b1 = window_box_right (w, -1);
962 b2 = FRAME_X_RIGHT_FRINGE_WIDTH (f);
963 }
964 }
965
966 if (b1 >= 0)
967 {
968 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
969
970 /* In case the same realized face is used for fringes and
971 for something displayed in the text (e.g. face `region' on
972 mono-displays, the fill style may have been changed to
973 FillSolid in x_draw_glyph_string_background. */
974 if (face->stipple)
975 XSetFillStyle (display, face->gc, FillOpaqueStippled);
976 else
977 XSetForeground (display, face->gc, face->background);
978
979 XFillRectangle (display, window, face->gc,
980 b1,
981 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
982 row->y)),
983 b2,
984 row->visible_height);
985 if (!face->stipple)
986 XSetForeground (display, face->gc, face->foreground);
987 }
988
989 if (which != NO_FRINGE_BITMAP)
990 {
991 /* Draw the bitmap. I believe these small pixmaps can be cached
992 by the server. */
993 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
994 face->foreground,
995 face->background, depth);
996 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
997 XFreePixmap (display, pixmap);
998 }
999
1000 XSetClipMask (display, gc, None);
1001 }
1002
1003
1004 /* Draw fringe bitmaps for glyph row ROW on window W. Call this
1005 function with input blocked. */
1006
1007 static void
1008 x_draw_row_fringe_bitmaps (w, row)
1009 struct window *w;
1010 struct glyph_row *row;
1011 {
1012 struct frame *f = XFRAME (w->frame);
1013 enum fringe_bitmap_type bitmap;
1014
1015 xassert (interrupt_input_blocked);
1016
1017 /* If row is completely invisible, because of vscrolling, we
1018 don't have to draw anything. */
1019 if (row->visible_height <= 0)
1020 return;
1021
1022 if (FRAME_X_LEFT_FRINGE_WIDTH (f) != 0)
1023 {
1024 /* Decide which bitmap to draw in the left fringe. */
1025 if (row->overlay_arrow_p)
1026 bitmap = OVERLAY_ARROW_BITMAP;
1027 else if (row->truncated_on_left_p)
1028 bitmap = LEFT_TRUNCATION_BITMAP;
1029 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
1030 bitmap = CONTINUATION_LINE_BITMAP;
1031 else if (row->indicate_empty_line_p)
1032 bitmap = ZV_LINE_BITMAP;
1033 else
1034 bitmap = NO_FRINGE_BITMAP;
1035
1036 x_draw_fringe_bitmap (w, row, bitmap, 1);
1037 }
1038
1039 if (FRAME_X_RIGHT_FRINGE_WIDTH (f) != 0)
1040 {
1041 /* Decide which bitmap to draw in the right fringe. */
1042 if (row->truncated_on_right_p)
1043 bitmap = RIGHT_TRUNCATION_BITMAP;
1044 else if (row->continued_p)
1045 bitmap = CONTINUED_LINE_BITMAP;
1046 else if (row->indicate_empty_line_p && FRAME_X_LEFT_FRINGE_WIDTH (f) == 0)
1047 bitmap = ZV_LINE_BITMAP;
1048 else
1049 bitmap = NO_FRINGE_BITMAP;
1050
1051 x_draw_fringe_bitmap (w, row, bitmap, 0);
1052 }
1053 }
1054
1055 \f
1056
1057 /* This is called when starting Emacs and when restarting after
1058 suspend. When starting Emacs, no X window is mapped. And nothing
1059 must be done to Emacs's own window if it is suspended (though that
1060 rarely happens). */
1061
1062 static void
1063 XTset_terminal_modes ()
1064 {
1065 }
1066
1067 /* This is called when exiting or suspending Emacs. Exiting will make
1068 the X-windows go away, and suspending requires no action. */
1069
1070 static void
1071 XTreset_terminal_modes ()
1072 {
1073 }
1074
1075
1076 \f
1077 /***********************************************************************
1078 Output Cursor
1079 ***********************************************************************/
1080
1081 /* Set the global variable output_cursor to CURSOR. All cursor
1082 positions are relative to updated_window. */
1083
1084 static void
1085 set_output_cursor (cursor)
1086 struct cursor_pos *cursor;
1087 {
1088 output_cursor.hpos = cursor->hpos;
1089 output_cursor.vpos = cursor->vpos;
1090 output_cursor.x = cursor->x;
1091 output_cursor.y = cursor->y;
1092 }
1093
1094
1095 /* Set a nominal cursor position.
1096
1097 HPOS and VPOS are column/row positions in a window glyph matrix. X
1098 and Y are window text area relative pixel positions.
1099
1100 If this is done during an update, updated_window will contain the
1101 window that is being updated and the position is the future output
1102 cursor position for that window. If updated_window is null, use
1103 selected_window and display the cursor at the given position. */
1104
1105 static void
1106 XTcursor_to (vpos, hpos, y, x)
1107 int vpos, hpos, y, x;
1108 {
1109 struct window *w;
1110
1111 /* If updated_window is not set, work on selected_window. */
1112 if (updated_window)
1113 w = updated_window;
1114 else
1115 w = XWINDOW (selected_window);
1116
1117 /* Set the output cursor. */
1118 output_cursor.hpos = hpos;
1119 output_cursor.vpos = vpos;
1120 output_cursor.x = x;
1121 output_cursor.y = y;
1122
1123 /* If not called as part of an update, really display the cursor.
1124 This will also set the cursor position of W. */
1125 if (updated_window == NULL)
1126 {
1127 BLOCK_INPUT;
1128 x_display_cursor (w, 1, hpos, vpos, x, y);
1129 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1130 UNBLOCK_INPUT;
1131 }
1132 }
1133
1134
1135 \f
1136 /***********************************************************************
1137 Display Iterator
1138 ***********************************************************************/
1139
1140 /* Function prototypes of this page. */
1141
1142 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1143 struct glyph *,
1144 XChar2b *,
1145 int *));
1146 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1147 int, XChar2b *, int));
1148 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1149 static void x_encode_char P_ ((int, XChar2b *, struct font_info *,
1150 struct charset *));
1151 static void x_append_glyph P_ ((struct it *));
1152 static void x_append_composite_glyph P_ ((struct it *));
1153 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1154 int, int, double));
1155 static void x_produce_glyphs P_ ((struct it *));
1156 static void x_produce_image_glyph P_ ((struct it *it));
1157
1158
1159 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1160 is not contained in the font. */
1161
1162 static INLINE XCharStruct *
1163 x_per_char_metric (font, char2b)
1164 XFontStruct *font;
1165 XChar2b *char2b;
1166 {
1167 /* The result metric information. */
1168 XCharStruct *pcm = NULL;
1169
1170 xassert (font && char2b);
1171
1172 if (font->per_char != NULL)
1173 {
1174 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1175 {
1176 /* min_char_or_byte2 specifies the linear character index
1177 corresponding to the first element of the per_char array,
1178 max_char_or_byte2 is the index of the last character. A
1179 character with non-zero CHAR2B->byte1 is not in the font.
1180 A character with byte2 less than min_char_or_byte2 or
1181 greater max_char_or_byte2 is not in the font. */
1182 if (char2b->byte1 == 0
1183 && char2b->byte2 >= font->min_char_or_byte2
1184 && char2b->byte2 <= font->max_char_or_byte2)
1185 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1186 }
1187 else
1188 {
1189 /* If either min_byte1 or max_byte1 are nonzero, both
1190 min_char_or_byte2 and max_char_or_byte2 are less than
1191 256, and the 2-byte character index values corresponding
1192 to the per_char array element N (counting from 0) are:
1193
1194 byte1 = N/D + min_byte1
1195 byte2 = N\D + min_char_or_byte2
1196
1197 where:
1198
1199 D = max_char_or_byte2 - min_char_or_byte2 + 1
1200 / = integer division
1201 \ = integer modulus */
1202 if (char2b->byte1 >= font->min_byte1
1203 && char2b->byte1 <= font->max_byte1
1204 && char2b->byte2 >= font->min_char_or_byte2
1205 && char2b->byte2 <= font->max_char_or_byte2)
1206 {
1207 pcm = (font->per_char
1208 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1209 * (char2b->byte1 - font->min_byte1))
1210 + (char2b->byte2 - font->min_char_or_byte2));
1211 }
1212 }
1213 }
1214 else
1215 {
1216 /* If the per_char pointer is null, all glyphs between the first
1217 and last character indexes inclusive have the same
1218 information, as given by both min_bounds and max_bounds. */
1219 if (char2b->byte2 >= font->min_char_or_byte2
1220 && char2b->byte2 <= font->max_char_or_byte2)
1221 pcm = &font->max_bounds;
1222 }
1223
1224 return ((pcm == NULL
1225 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1226 ? NULL : pcm);
1227 }
1228
1229
1230 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1231 the two-byte form of C. Encoding is returned in *CHAR2B. */
1232
1233 static INLINE void
1234 x_encode_char (c, char2b, font_info, charset)
1235 int c;
1236 XChar2b *char2b;
1237 struct font_info *font_info;
1238 struct charset *charset;
1239 {
1240 XFontStruct *font = font_info->font;
1241
1242 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1243 This may be either a program in a special encoder language or a
1244 fixed encoding. */
1245 if (font_info->font_encoder)
1246 {
1247 /* It's a program. */
1248 struct ccl_program *ccl = font_info->font_encoder;
1249
1250 if (CHARSET_DIMENSION (charset) == 1)
1251 {
1252 ccl->reg[0] = CHARSET_ID (charset);
1253 ccl->reg[1] = char2b->byte2;
1254 }
1255 else
1256 {
1257 ccl->reg[0] = CHARSET_ID (charset);
1258 ccl->reg[1] = char2b->byte1;
1259 ccl->reg[2] = char2b->byte2;
1260 }
1261
1262 ccl_driver (ccl, NULL, NULL, 0, 0, Qnil);
1263
1264 /* We assume that MSBs are appropriately set/reset by CCL
1265 program. */
1266 if (font->max_byte1 == 0) /* 1-byte font */
1267 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1268 else
1269 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1270 }
1271 else if (font_info->encoding_type)
1272 {
1273 /* Fixed encoding scheme. See fontset.h for the meaning of the
1274 encoding numbers. */
1275 unsigned char enc = font_info->encoding_type;
1276
1277 if ((enc == 1 || enc == 2)
1278 && CHARSET_DIMENSION (charset) == 2)
1279 char2b->byte1 |= 0x80;
1280
1281 if (enc == 1 || enc == 3)
1282 char2b->byte2 |= 0x80;
1283 }
1284 }
1285
1286
1287 /* Get face and two-byte form of character C in face FACE_ID on frame
1288 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1289 means we want to display multibyte text. Value is a pointer to a
1290 realized face that is ready for display. */
1291
1292 static INLINE struct face *
1293 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1294 struct frame *f;
1295 int c, face_id;
1296 XChar2b *char2b;
1297 int multibyte_p;
1298 {
1299 struct face *face = FACE_FROM_ID (f, face_id);
1300
1301 if (!multibyte_p)
1302 {
1303 /* Unibyte case. We don't have to encode, but we have to make
1304 sure to use a face suitable for unibyte. */
1305 char2b->byte1 = 0;
1306 char2b->byte2 = c;
1307 face_id = FACE_FOR_CHAR (f, face, c);
1308 face = FACE_FROM_ID (f, face_id);
1309 }
1310 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1311 {
1312 /* Case of ASCII in a face known to fit ASCII. */
1313 char2b->byte1 = 0;
1314 char2b->byte2 = c;
1315 }
1316 else if (face->font != NULL)
1317 {
1318 struct font_info *font_info
1319 = FONT_INFO_FROM_ID (f, face->font_info_id);
1320 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
1321 unsigned code = ENCODE_CHAR (charset, c);
1322
1323 if (CHARSET_DIMENSION (charset) == 1)
1324 char2b->byte1 = 0, char2b->byte2 = code;
1325 else
1326 char2b->byte1 = code >> 8, char2b->byte2 = code & 0xFF;
1327
1328 /* Maybe encode the character in *CHAR2B. */
1329 x_encode_char (c, char2b, font_info, charset);
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 struct font_info *font_info
1376 = FONT_INFO_FROM_ID (f, face->font_info_id);
1377 if (font_info)
1378 {
1379 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
1380 unsigned code = ENCODE_CHAR (charset, glyph->u.ch);
1381
1382 if (CHARSET_DIMENSION (charset) == 1)
1383 char2b->byte1 = 0, char2b->byte2 = code;
1384 else
1385 char2b->byte1 = code >> 8, char2b->byte2 = code & 0xFF;
1386
1387 /* Maybe encode the character in *CHAR2B. */
1388 if (CHARSET_ID (charset) != charset_ascii)
1389 {
1390 x_encode_char (glyph->u.ch, char2b, font_info, charset);
1391 if (two_byte_p)
1392 *two_byte_p
1393 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1394 }
1395 }
1396 }
1397
1398 /* Make sure X resources of the face are allocated. */
1399 xassert (face != NULL);
1400 PREPARE_FACE_FOR_DISPLAY (f, face);
1401 return face;
1402 }
1403
1404
1405 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1406 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1407
1408 static INLINE void
1409 x_append_glyph (it)
1410 struct it *it;
1411 {
1412 struct glyph *glyph;
1413 enum glyph_row_area area = it->area;
1414
1415 xassert (it->glyph_row);
1416 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1417
1418 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1419 if (glyph < it->glyph_row->glyphs[area + 1])
1420 {
1421 glyph->charpos = CHARPOS (it->position);
1422 glyph->object = it->object;
1423 glyph->pixel_width = it->pixel_width;
1424 glyph->voffset = it->voffset;
1425 glyph->type = CHAR_GLYPH;
1426 glyph->multibyte_p = it->multibyte_p;
1427 glyph->left_box_line_p = it->start_of_box_run_p;
1428 glyph->right_box_line_p = it->end_of_box_run_p;
1429 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1430 || it->phys_descent > it->descent);
1431 glyph->padding_p = 0;
1432 glyph->glyph_not_available_p = it->glyph_not_available_p;
1433 glyph->face_id = it->face_id;
1434 glyph->u.ch = it->char_to_display;
1435 ++it->glyph_row->used[area];
1436 }
1437 }
1438
1439 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1440 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1441
1442 static INLINE void
1443 x_append_composite_glyph (it)
1444 struct it *it;
1445 {
1446 struct glyph *glyph;
1447 enum glyph_row_area area = it->area;
1448
1449 xassert (it->glyph_row);
1450
1451 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1452 if (glyph < it->glyph_row->glyphs[area + 1])
1453 {
1454 glyph->charpos = CHARPOS (it->position);
1455 glyph->object = it->object;
1456 glyph->pixel_width = it->pixel_width;
1457 glyph->voffset = it->voffset;
1458 glyph->type = COMPOSITE_GLYPH;
1459 glyph->multibyte_p = it->multibyte_p;
1460 glyph->left_box_line_p = it->start_of_box_run_p;
1461 glyph->right_box_line_p = it->end_of_box_run_p;
1462 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1463 || it->phys_descent > it->descent);
1464 glyph->padding_p = 0;
1465 glyph->glyph_not_available_p = 0;
1466 glyph->face_id = it->face_id;
1467 glyph->u.cmp_id = it->cmp_id;
1468 ++it->glyph_row->used[area];
1469 }
1470 }
1471
1472
1473 /* Change IT->ascent and IT->height according to the setting of
1474 IT->voffset. */
1475
1476 static INLINE void
1477 take_vertical_position_into_account (it)
1478 struct it *it;
1479 {
1480 if (it->voffset)
1481 {
1482 if (it->voffset < 0)
1483 /* Increase the ascent so that we can display the text higher
1484 in the line. */
1485 it->ascent += abs (it->voffset);
1486 else
1487 /* Increase the descent so that we can display the text lower
1488 in the line. */
1489 it->descent += it->voffset;
1490 }
1491 }
1492
1493
1494 /* Produce glyphs/get display metrics for the image IT is loaded with.
1495 See the description of struct display_iterator in dispextern.h for
1496 an overview of struct display_iterator. */
1497
1498 static void
1499 x_produce_image_glyph (it)
1500 struct it *it;
1501 {
1502 struct image *img;
1503 struct face *face;
1504
1505 xassert (it->what == IT_IMAGE);
1506
1507 face = FACE_FROM_ID (it->f, it->face_id);
1508 img = IMAGE_FROM_ID (it->f, it->image_id);
1509 xassert (img);
1510
1511 /* Make sure X resources of the face and image are loaded. */
1512 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1513 prepare_image_for_display (it->f, img);
1514
1515 it->ascent = it->phys_ascent = image_ascent (img, face);
1516 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1517 it->pixel_width = img->width + 2 * img->hmargin;
1518
1519 it->nglyphs = 1;
1520
1521 if (face->box != FACE_NO_BOX)
1522 {
1523 if (face->box_line_width > 0)
1524 {
1525 it->ascent += face->box_line_width;
1526 it->descent += face->box_line_width;
1527 }
1528
1529 if (it->start_of_box_run_p)
1530 it->pixel_width += abs (face->box_line_width);
1531 if (it->end_of_box_run_p)
1532 it->pixel_width += abs (face->box_line_width);
1533 }
1534
1535 take_vertical_position_into_account (it);
1536
1537 if (it->glyph_row)
1538 {
1539 struct glyph *glyph;
1540 enum glyph_row_area area = it->area;
1541
1542 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1543 if (glyph < it->glyph_row->glyphs[area + 1])
1544 {
1545 glyph->charpos = CHARPOS (it->position);
1546 glyph->object = it->object;
1547 glyph->pixel_width = it->pixel_width;
1548 glyph->voffset = it->voffset;
1549 glyph->type = IMAGE_GLYPH;
1550 glyph->multibyte_p = it->multibyte_p;
1551 glyph->left_box_line_p = it->start_of_box_run_p;
1552 glyph->right_box_line_p = it->end_of_box_run_p;
1553 glyph->overlaps_vertically_p = 0;
1554 glyph->padding_p = 0;
1555 glyph->glyph_not_available_p = 0;
1556 glyph->face_id = it->face_id;
1557 glyph->u.img_id = img->id;
1558 ++it->glyph_row->used[area];
1559 }
1560 }
1561 }
1562
1563
1564 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1565 of the glyph, WIDTH and HEIGHT are the width and height of the
1566 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1567 ascent of the glyph (0 <= ASCENT <= 1). */
1568
1569 static void
1570 x_append_stretch_glyph (it, object, width, height, ascent)
1571 struct it *it;
1572 Lisp_Object object;
1573 int width, height;
1574 double ascent;
1575 {
1576 struct glyph *glyph;
1577 enum glyph_row_area area = it->area;
1578
1579 xassert (ascent >= 0 && ascent <= 1);
1580
1581 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1582 if (glyph < it->glyph_row->glyphs[area + 1])
1583 {
1584 glyph->charpos = CHARPOS (it->position);
1585 glyph->object = object;
1586 glyph->pixel_width = width;
1587 glyph->voffset = it->voffset;
1588 glyph->type = STRETCH_GLYPH;
1589 glyph->multibyte_p = it->multibyte_p;
1590 glyph->left_box_line_p = it->start_of_box_run_p;
1591 glyph->right_box_line_p = it->end_of_box_run_p;
1592 glyph->overlaps_vertically_p = 0;
1593 glyph->padding_p = 0;
1594 glyph->glyph_not_available_p = 0;
1595 glyph->face_id = it->face_id;
1596 glyph->u.stretch.ascent = height * ascent;
1597 glyph->u.stretch.height = height;
1598 ++it->glyph_row->used[area];
1599 }
1600 }
1601
1602
1603 /* Produce a stretch glyph for iterator IT. IT->object is the value
1604 of the glyph property displayed. The value must be a list
1605 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1606 being recognized:
1607
1608 1. `:width WIDTH' specifies that the space should be WIDTH *
1609 canonical char width wide. WIDTH may be an integer or floating
1610 point number.
1611
1612 2. `:relative-width FACTOR' specifies that the width of the stretch
1613 should be computed from the width of the first character having the
1614 `glyph' property, and should be FACTOR times that width.
1615
1616 3. `:align-to HPOS' specifies that the space should be wide enough
1617 to reach HPOS, a value in canonical character units.
1618
1619 Exactly one of the above pairs must be present.
1620
1621 4. `:height HEIGHT' specifies that the height of the stretch produced
1622 should be HEIGHT, measured in canonical character units.
1623
1624 5. `:relative-height FACTOR' specifies that the height of the
1625 stretch should be FACTOR times the height of the characters having
1626 the glyph property.
1627
1628 Either none or exactly one of 4 or 5 must be present.
1629
1630 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1631 of the stretch should be used for the ascent of the stretch.
1632 ASCENT must be in the range 0 <= ASCENT <= 100. */
1633
1634 #define NUMVAL(X) \
1635 ((INTEGERP (X) || FLOATP (X)) \
1636 ? XFLOATINT (X) \
1637 : - 1)
1638
1639
1640 static void
1641 x_produce_stretch_glyph (it)
1642 struct it *it;
1643 {
1644 /* (space :width WIDTH :height HEIGHT. */
1645 #if GLYPH_DEBUG
1646 extern Lisp_Object Qspace;
1647 #endif
1648 extern Lisp_Object QCwidth, QCheight, QCascent;
1649 extern Lisp_Object QCrelative_width, QCrelative_height;
1650 extern Lisp_Object QCalign_to;
1651 Lisp_Object prop, plist;
1652 double width = 0, height = 0, ascent = 0;
1653 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1654 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1655
1656 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1657
1658 /* List should start with `space'. */
1659 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1660 plist = XCDR (it->object);
1661
1662 /* Compute the width of the stretch. */
1663 if (prop = Fplist_get (plist, QCwidth),
1664 NUMVAL (prop) > 0)
1665 /* Absolute width `:width WIDTH' specified and valid. */
1666 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1667 else if (prop = Fplist_get (plist, QCrelative_width),
1668 NUMVAL (prop) > 0)
1669 {
1670 /* Relative width `:relative-width FACTOR' specified and valid.
1671 Compute the width of the characters having the `glyph'
1672 property. */
1673 struct it it2;
1674 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1675
1676 it2 = *it;
1677 if (it->multibyte_p)
1678 {
1679 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1680 - IT_BYTEPOS (*it));
1681 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1682 }
1683 else
1684 it2.c = *p, it2.len = 1;
1685
1686 it2.glyph_row = NULL;
1687 it2.what = IT_CHARACTER;
1688 x_produce_glyphs (&it2);
1689 width = NUMVAL (prop) * it2.pixel_width;
1690 }
1691 else if (prop = Fplist_get (plist, QCalign_to),
1692 NUMVAL (prop) > 0)
1693 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1694 else
1695 /* Nothing specified -> width defaults to canonical char width. */
1696 width = CANON_X_UNIT (it->f);
1697
1698 /* Compute height. */
1699 if (prop = Fplist_get (plist, QCheight),
1700 NUMVAL (prop) > 0)
1701 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1702 else if (prop = Fplist_get (plist, QCrelative_height),
1703 NUMVAL (prop) > 0)
1704 height = FONT_HEIGHT (font) * NUMVAL (prop);
1705 else
1706 height = FONT_HEIGHT (font);
1707
1708 /* Compute percentage of height used for ascent. If
1709 `:ascent ASCENT' is present and valid, use that. Otherwise,
1710 derive the ascent from the font in use. */
1711 if (prop = Fplist_get (plist, QCascent),
1712 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1713 ascent = NUMVAL (prop) / 100.0;
1714 else
1715 ascent = (double) font->ascent / FONT_HEIGHT (font);
1716
1717 if (width <= 0)
1718 width = 1;
1719 if (height <= 0)
1720 height = 1;
1721
1722 if (it->glyph_row)
1723 {
1724 Lisp_Object object = it->stack[it->sp - 1].string;
1725 if (!STRINGP (object))
1726 object = it->w->buffer;
1727 x_append_stretch_glyph (it, object, width, height, ascent);
1728 }
1729
1730 it->pixel_width = width;
1731 it->ascent = it->phys_ascent = height * ascent;
1732 it->descent = it->phys_descent = height - it->ascent;
1733 it->nglyphs = 1;
1734
1735 if (face->box != FACE_NO_BOX)
1736 {
1737 if (face->box_line_width > 0)
1738 {
1739 it->ascent += face->box_line_width;
1740 it->descent += face->box_line_width;
1741 }
1742
1743 if (it->start_of_box_run_p)
1744 it->pixel_width += abs (face->box_line_width);
1745 if (it->end_of_box_run_p)
1746 it->pixel_width += abs (face->box_line_width);
1747 }
1748
1749 take_vertical_position_into_account (it);
1750 }
1751
1752 /* Return proper value to be used as baseline offset of font that has
1753 ASCENT and DESCENT to draw characters by the font at the vertical
1754 center of the line of frame F.
1755
1756 Here, out task is to find the value of BOFF in the following figure;
1757
1758 -------------------------+-----------+-
1759 -+-+---------+-+ | |
1760 | | | | | |
1761 | | | | F_ASCENT F_HEIGHT
1762 | | | ASCENT | |
1763 HEIGHT | | | | |
1764 | | |-|-+------+-----------|------- baseline
1765 | | | | BOFF | |
1766 | |---------|-+-+ | |
1767 | | | DESCENT | |
1768 -+-+---------+-+ F_DESCENT |
1769 -------------------------+-----------+-
1770
1771 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1772 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1773 DESCENT = FONT->descent
1774 HEIGHT = FONT_HEIGHT (FONT)
1775 F_DESCENT = (F->output_data.x->font->descent
1776 - F->output_data.x->baseline_offset)
1777 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1778 */
1779
1780 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1781 ((FONT)->descent \
1782 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1783 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1784 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1785
1786 /* Produce glyphs/get display metrics for the display element IT is
1787 loaded with. See the description of struct display_iterator in
1788 dispextern.h for an overview of struct display_iterator. */
1789
1790 static void
1791 x_produce_glyphs (it)
1792 struct it *it;
1793 {
1794 it->glyph_not_available_p = 0;
1795
1796 if (it->what == IT_CHARACTER)
1797 {
1798 XChar2b char2b;
1799 XFontStruct *font;
1800 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1801 XCharStruct *pcm;
1802 int font_not_found_p;
1803 struct font_info *font_info;
1804 int boff; /* baseline offset */
1805 /* We may change it->multibyte_p upon unibyte<->multibyte
1806 conversion. So, save the current value now and restore it
1807 later.
1808
1809 Note: It seems that we don't have to record multibyte_p in
1810 struct glyph because the character code itself tells if or
1811 not the character is multibyte. Thus, in the future, we must
1812 consider eliminating the field `multibyte_p' in the struct
1813 glyph. */
1814 int saved_multibyte_p = it->multibyte_p;
1815
1816 /* Maybe translate single-byte characters to multibyte, or the
1817 other way. */
1818 it->char_to_display = it->c;
1819 if (! ASCII_CHAR_P (it->c)
1820 && ! it->multibyte_p)
1821 {
1822 if (SINGLE_BYTE_CHAR_P (it->c)
1823 && unibyte_display_via_language_environment)
1824 it->char_to_display = unibyte_char_to_multibyte (it->c);
1825 if (! SINGLE_BYTE_CHAR_P (it->c))
1826 {
1827 it->multibyte_p = 1;
1828 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1829 face = FACE_FROM_ID (it->f, it->face_id);
1830 }
1831 }
1832
1833 /* Get font to use. Encode IT->char_to_display. */
1834 x_get_char_face_and_encoding (it->f, it->char_to_display,
1835 it->face_id, &char2b,
1836 it->multibyte_p);
1837 font = face->font;
1838
1839 /* When no suitable font found, use the default font. */
1840 font_not_found_p = font == NULL;
1841 if (font_not_found_p)
1842 {
1843 font = FRAME_FONT (it->f);
1844 boff = it->f->output_data.x->baseline_offset;
1845 font_info = NULL;
1846 }
1847 else
1848 {
1849 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1850 boff = font_info->baseline_offset;
1851 if (font_info->vertical_centering)
1852 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1853 }
1854
1855 if (it->char_to_display >= ' '
1856 && (!it->multibyte_p || it->char_to_display < 128))
1857 {
1858 /* Either unibyte or ASCII. */
1859 int stretched_p;
1860
1861 it->nglyphs = 1;
1862
1863 pcm = x_per_char_metric (font, &char2b);
1864 it->ascent = font->ascent + boff;
1865 it->descent = font->descent - boff;
1866
1867 if (pcm)
1868 {
1869 it->phys_ascent = pcm->ascent + boff;
1870 it->phys_descent = pcm->descent - boff;
1871 it->pixel_width = pcm->width;
1872 }
1873 else
1874 {
1875 it->glyph_not_available_p = 1;
1876 it->phys_ascent = font->ascent + boff;
1877 it->phys_descent = font->descent - boff;
1878 it->pixel_width = FONT_WIDTH (font);
1879 }
1880
1881 /* If this is a space inside a region of text with
1882 `space-width' property, change its width. */
1883 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1884 if (stretched_p)
1885 it->pixel_width *= XFLOATINT (it->space_width);
1886
1887 /* If face has a box, add the box thickness to the character
1888 height. If character has a box line to the left and/or
1889 right, add the box line width to the character's width. */
1890 if (face->box != FACE_NO_BOX)
1891 {
1892 int thick = face->box_line_width;
1893
1894 if (thick > 0)
1895 {
1896 it->ascent += thick;
1897 it->descent += thick;
1898 }
1899 else
1900 thick = -thick;
1901
1902 if (it->start_of_box_run_p)
1903 it->pixel_width += thick;
1904 if (it->end_of_box_run_p)
1905 it->pixel_width += thick;
1906 }
1907
1908 /* If face has an overline, add the height of the overline
1909 (1 pixel) and a 1 pixel margin to the character height. */
1910 if (face->overline_p)
1911 it->ascent += 2;
1912
1913 take_vertical_position_into_account (it);
1914
1915 /* If we have to actually produce glyphs, do it. */
1916 if (it->glyph_row)
1917 {
1918 if (stretched_p)
1919 {
1920 /* Translate a space with a `space-width' property
1921 into a stretch glyph. */
1922 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1923 x_append_stretch_glyph (it, it->object, it->pixel_width,
1924 it->ascent + it->descent, ascent);
1925 }
1926 else
1927 x_append_glyph (it);
1928
1929 /* If characters with lbearing or rbearing are displayed
1930 in this line, record that fact in a flag of the
1931 glyph row. This is used to optimize X output code. */
1932 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1933 it->glyph_row->contains_overlapping_glyphs_p = 1;
1934 }
1935 }
1936 else if (it->char_to_display == '\n')
1937 {
1938 /* A newline has no width but we need the height of the line. */
1939 it->pixel_width = 0;
1940 it->nglyphs = 0;
1941 it->ascent = it->phys_ascent = font->ascent + boff;
1942 it->descent = it->phys_descent = font->descent - boff;
1943
1944 if (face->box != FACE_NO_BOX
1945 && face->box_line_width > 0)
1946 {
1947 it->ascent += face->box_line_width;
1948 it->descent += face->box_line_width;
1949 }
1950 }
1951 else if (it->char_to_display == '\t')
1952 {
1953 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1954 int x = it->current_x + it->continuation_lines_width;
1955 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1956
1957 /* If the distance from the current position to the next tab
1958 stop is less than a canonical character width, use the
1959 tab stop after that. */
1960 if (next_tab_x - x < CANON_X_UNIT (it->f))
1961 next_tab_x += tab_width;
1962
1963 it->pixel_width = next_tab_x - x;
1964 it->nglyphs = 1;
1965 it->ascent = it->phys_ascent = font->ascent + boff;
1966 it->descent = it->phys_descent = font->descent - boff;
1967
1968 if (it->glyph_row)
1969 {
1970 double ascent = (double) it->ascent / (it->ascent + it->descent);
1971 x_append_stretch_glyph (it, it->object, it->pixel_width,
1972 it->ascent + it->descent, ascent);
1973 }
1974 }
1975 else
1976 {
1977 /* A multi-byte character. Assume that the display width of the
1978 character is the width of the character multiplied by the
1979 width of the font. */
1980
1981 /* If we found a font, this font should give us the right
1982 metrics. If we didn't find a font, use the frame's
1983 default font and calculate the width of the character by
1984 multiplying the width of font by the width of the
1985 character. */
1986 pcm = x_per_char_metric (font, &char2b);
1987 if (font_not_found_p || !pcm)
1988 {
1989 it->glyph_not_available_p = 1;
1990 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1991 * CHAR_WIDTH (it->char_to_display));
1992 it->phys_ascent = font->ascent + boff;
1993 it->phys_descent = font->descent - boff;
1994 }
1995 else
1996 {
1997 it->pixel_width = pcm->width;
1998 it->phys_ascent = pcm->ascent + boff;
1999 it->phys_descent = pcm->descent - boff;
2000 if (it->glyph_row
2001 && (pcm->lbearing < 0
2002 || pcm->rbearing > pcm->width))
2003 it->glyph_row->contains_overlapping_glyphs_p = 1;
2004 }
2005 it->nglyphs = 1;
2006 it->ascent = font->ascent + boff;
2007 it->descent = font->descent - boff;
2008 if (face->box != FACE_NO_BOX)
2009 {
2010 int thick = face->box_line_width;
2011
2012 if (thick > 0)
2013 {
2014 it->ascent += thick;
2015 it->descent += thick;
2016 }
2017 else
2018 thick = - thick;
2019
2020 if (it->start_of_box_run_p)
2021 it->pixel_width += thick;
2022 if (it->end_of_box_run_p)
2023 it->pixel_width += thick;
2024 }
2025
2026 /* If face has an overline, add the height of the overline
2027 (1 pixel) and a 1 pixel margin to the character height. */
2028 if (face->overline_p)
2029 it->ascent += 2;
2030
2031 take_vertical_position_into_account (it);
2032
2033 if (it->glyph_row)
2034 x_append_glyph (it);
2035 }
2036 it->multibyte_p = saved_multibyte_p;
2037 }
2038 else if (it->what == IT_COMPOSITION)
2039 {
2040 /* Note: A composition is represented as one glyph in the
2041 glyph matrix. There are no padding glyphs. */
2042 XChar2b char2b;
2043 XFontStruct *font;
2044 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2045 XCharStruct *pcm;
2046 int font_not_found_p;
2047 struct font_info *font_info;
2048 int boff; /* baseline offset */
2049 struct composition *cmp = composition_table[it->cmp_id];
2050
2051 /* Maybe translate single-byte characters to multibyte. */
2052 it->char_to_display = it->c;
2053 if (unibyte_display_via_language_environment
2054 && it->c >= 0200)
2055 {
2056 it->char_to_display = unibyte_char_to_multibyte (it->c);
2057 }
2058
2059 /* Get face and font to use. Encode IT->char_to_display. */
2060 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2061 face = FACE_FROM_ID (it->f, it->face_id);
2062 x_get_char_face_and_encoding (it->f, it->char_to_display,
2063 it->face_id, &char2b, it->multibyte_p);
2064 font = face->font;
2065
2066 /* When no suitable font found, use the default font. */
2067 font_not_found_p = font == NULL;
2068 if (font_not_found_p)
2069 {
2070 font = FRAME_FONT (it->f);
2071 boff = it->f->output_data.x->baseline_offset;
2072 font_info = NULL;
2073 }
2074 else
2075 {
2076 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2077 boff = font_info->baseline_offset;
2078 if (font_info->vertical_centering)
2079 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2080 }
2081
2082 /* There are no padding glyphs, so there is only one glyph to
2083 produce for the composition. Important is that pixel_width,
2084 ascent and descent are the values of what is drawn by
2085 draw_glyphs (i.e. the values of the overall glyphs composed). */
2086 it->nglyphs = 1;
2087
2088 /* If we have not yet calculated pixel size data of glyphs of
2089 the composition for the current face font, calculate them
2090 now. Theoretically, we have to check all fonts for the
2091 glyphs, but that requires much time and memory space. So,
2092 here we check only the font of the first glyph. This leads
2093 to incorrect display very rarely, and C-l (recenter) can
2094 correct the display anyway. */
2095 if (cmp->font != (void *) font)
2096 {
2097 /* Ascent and descent of the font of the first character of
2098 this composition (adjusted by baseline offset). Ascent
2099 and descent of overall glyphs should not be less than
2100 them respectively. */
2101 int font_ascent = font->ascent + boff;
2102 int font_descent = font->descent - boff;
2103 /* Bounding box of the overall glyphs. */
2104 int leftmost, rightmost, lowest, highest;
2105 int i, width, ascent, descent;
2106
2107 cmp->font = (void *) font;
2108
2109 /* Initialize the bounding box. */
2110 if (font_info
2111 && (pcm = x_per_char_metric (font, &char2b)))
2112 {
2113 width = pcm->width;
2114 ascent = pcm->ascent;
2115 descent = pcm->descent;
2116 }
2117 else
2118 {
2119 width = FONT_WIDTH (font);
2120 ascent = font->ascent;
2121 descent = font->descent;
2122 }
2123
2124 rightmost = width;
2125 lowest = - descent + boff;
2126 highest = ascent + boff;
2127 leftmost = 0;
2128
2129 if (font_info
2130 && font_info->default_ascent
2131 && CHAR_TABLE_P (Vuse_default_ascent)
2132 && !NILP (Faref (Vuse_default_ascent,
2133 make_number (it->char_to_display))))
2134 highest = font_info->default_ascent + boff;
2135
2136 /* Draw the first glyph at the normal position. It may be
2137 shifted to right later if some other glyphs are drawn at
2138 the left. */
2139 cmp->offsets[0] = 0;
2140 cmp->offsets[1] = boff;
2141
2142 /* Set cmp->offsets for the remaining glyphs. */
2143 for (i = 1; i < cmp->glyph_len; i++)
2144 {
2145 int left, right, btm, top;
2146 int ch = COMPOSITION_GLYPH (cmp, i);
2147 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2148
2149 face = FACE_FROM_ID (it->f, face_id);
2150 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2151 it->multibyte_p);
2152 font = face->font;
2153 if (font == NULL)
2154 {
2155 font = FRAME_FONT (it->f);
2156 boff = it->f->output_data.x->baseline_offset;
2157 font_info = NULL;
2158 }
2159 else
2160 {
2161 font_info
2162 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2163 boff = font_info->baseline_offset;
2164 if (font_info->vertical_centering)
2165 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2166 }
2167
2168 if (font_info
2169 && (pcm = x_per_char_metric (font, &char2b)))
2170 {
2171 width = pcm->width;
2172 ascent = pcm->ascent;
2173 descent = pcm->descent;
2174 }
2175 else
2176 {
2177 width = FONT_WIDTH (font);
2178 ascent = 1;
2179 descent = 0;
2180 }
2181
2182 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2183 {
2184 /* Relative composition with or without
2185 alternate chars. */
2186 left = (leftmost + rightmost - width) / 2;
2187 btm = - descent + boff;
2188 if (font_info && font_info->relative_compose
2189 && (! CHAR_TABLE_P (Vignore_relative_composition)
2190 || NILP (Faref (Vignore_relative_composition,
2191 make_number (ch)))))
2192 {
2193
2194 if (- descent >= font_info->relative_compose)
2195 /* One extra pixel between two glyphs. */
2196 btm = highest + 1;
2197 else if (ascent <= 0)
2198 /* One extra pixel between two glyphs. */
2199 btm = lowest - 1 - ascent - descent;
2200 }
2201 }
2202 else
2203 {
2204 /* A composition rule is specified by an integer
2205 value that encodes global and new reference
2206 points (GREF and NREF). GREF and NREF are
2207 specified by numbers as below:
2208
2209 0---1---2 -- ascent
2210 | |
2211 | |
2212 | |
2213 9--10--11 -- center
2214 | |
2215 ---3---4---5--- baseline
2216 | |
2217 6---7---8 -- descent
2218 */
2219 int rule = COMPOSITION_RULE (cmp, i);
2220 int gref, nref, grefx, grefy, nrefx, nrefy;
2221
2222 COMPOSITION_DECODE_RULE (rule, gref, nref);
2223 grefx = gref % 3, nrefx = nref % 3;
2224 grefy = gref / 3, nrefy = nref / 3;
2225
2226 left = (leftmost
2227 + grefx * (rightmost - leftmost) / 2
2228 - nrefx * width / 2);
2229 btm = ((grefy == 0 ? highest
2230 : grefy == 1 ? 0
2231 : grefy == 2 ? lowest
2232 : (highest + lowest) / 2)
2233 - (nrefy == 0 ? ascent + descent
2234 : nrefy == 1 ? descent - boff
2235 : nrefy == 2 ? 0
2236 : (ascent + descent) / 2));
2237 }
2238
2239 cmp->offsets[i * 2] = left;
2240 cmp->offsets[i * 2 + 1] = btm + descent;
2241
2242 /* Update the bounding box of the overall glyphs. */
2243 right = left + width;
2244 top = btm + descent + ascent;
2245 if (left < leftmost)
2246 leftmost = left;
2247 if (right > rightmost)
2248 rightmost = right;
2249 if (top > highest)
2250 highest = top;
2251 if (btm < lowest)
2252 lowest = btm;
2253 }
2254
2255 /* If there are glyphs whose x-offsets are negative,
2256 shift all glyphs to the right and make all x-offsets
2257 non-negative. */
2258 if (leftmost < 0)
2259 {
2260 for (i = 0; i < cmp->glyph_len; i++)
2261 cmp->offsets[i * 2] -= leftmost;
2262 rightmost -= leftmost;
2263 }
2264
2265 cmp->pixel_width = rightmost;
2266 cmp->ascent = highest;
2267 cmp->descent = - lowest;
2268 if (cmp->ascent < font_ascent)
2269 cmp->ascent = font_ascent;
2270 if (cmp->descent < font_descent)
2271 cmp->descent = font_descent;
2272 }
2273
2274 it->pixel_width = cmp->pixel_width;
2275 it->ascent = it->phys_ascent = cmp->ascent;
2276 it->descent = it->phys_descent = cmp->descent;
2277
2278 if (face->box != FACE_NO_BOX)
2279 {
2280 int thick = face->box_line_width;
2281
2282 if (thick > 0)
2283 {
2284 it->ascent += thick;
2285 it->descent += thick;
2286 }
2287 else
2288 thick = - thick;
2289
2290 if (it->start_of_box_run_p)
2291 it->pixel_width += thick;
2292 if (it->end_of_box_run_p)
2293 it->pixel_width += thick;
2294 }
2295
2296 /* If face has an overline, add the height of the overline
2297 (1 pixel) and a 1 pixel margin to the character height. */
2298 if (face->overline_p)
2299 it->ascent += 2;
2300
2301 take_vertical_position_into_account (it);
2302
2303 if (it->glyph_row)
2304 x_append_composite_glyph (it);
2305 }
2306 else if (it->what == IT_IMAGE)
2307 x_produce_image_glyph (it);
2308 else if (it->what == IT_STRETCH)
2309 x_produce_stretch_glyph (it);
2310
2311 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2312 because this isn't true for images with `:ascent 100'. */
2313 xassert (it->ascent >= 0 && it->descent >= 0);
2314 if (it->area == TEXT_AREA)
2315 it->current_x += it->pixel_width;
2316
2317 it->descent += it->extra_line_spacing;
2318
2319 it->max_ascent = max (it->max_ascent, it->ascent);
2320 it->max_descent = max (it->max_descent, it->descent);
2321 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2322 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2323 }
2324
2325
2326 /* Estimate the pixel height of the mode or top line on frame F.
2327 FACE_ID specifies what line's height to estimate. */
2328
2329 int
2330 x_estimate_mode_line_height (f, face_id)
2331 struct frame *f;
2332 enum face_id face_id;
2333 {
2334 int height = FONT_HEIGHT (FRAME_FONT (f));
2335
2336 /* This function is called so early when Emacs starts that the face
2337 cache and mode line face are not yet initialized. */
2338 if (FRAME_FACE_CACHE (f))
2339 {
2340 struct face *face = FACE_FROM_ID (f, face_id);
2341 if (face)
2342 {
2343 if (face->font)
2344 height = FONT_HEIGHT (face->font);
2345 if (face->box_line_width > 0)
2346 height += 2 * face->box_line_width;
2347 }
2348 }
2349
2350 return height;
2351 }
2352
2353 \f
2354 /***********************************************************************
2355 Glyph display
2356 ***********************************************************************/
2357
2358 /* A sequence of glyphs to be drawn in the same face.
2359
2360 This data structure is not really completely X specific, so it
2361 could possibly, at least partially, be useful for other systems. It
2362 is currently not part of the external redisplay interface because
2363 it's not clear what other systems will need. */
2364
2365 struct glyph_string
2366 {
2367 /* X-origin of the string. */
2368 int x;
2369
2370 /* Y-origin and y-position of the base line of this string. */
2371 int y, ybase;
2372
2373 /* The width of the string, not including a face extension. */
2374 int width;
2375
2376 /* The width of the string, including a face extension. */
2377 int background_width;
2378
2379 /* The height of this string. This is the height of the line this
2380 string is drawn in, and can be different from the height of the
2381 font the string is drawn in. */
2382 int height;
2383
2384 /* Number of pixels this string overwrites in front of its x-origin.
2385 This number is zero if the string has an lbearing >= 0; it is
2386 -lbearing, if the string has an lbearing < 0. */
2387 int left_overhang;
2388
2389 /* Number of pixels this string overwrites past its right-most
2390 nominal x-position, i.e. x + width. Zero if the string's
2391 rbearing is <= its nominal width, rbearing - width otherwise. */
2392 int right_overhang;
2393
2394 /* The frame on which the glyph string is drawn. */
2395 struct frame *f;
2396
2397 /* The window on which the glyph string is drawn. */
2398 struct window *w;
2399
2400 /* X display and window for convenience. */
2401 Display *display;
2402 Window window;
2403
2404 /* The glyph row for which this string was built. It determines the
2405 y-origin and height of the string. */
2406 struct glyph_row *row;
2407
2408 /* The area within row. */
2409 enum glyph_row_area area;
2410
2411 /* Characters to be drawn, and number of characters. */
2412 XChar2b *char2b;
2413 int nchars;
2414
2415 /* A face-override for drawing cursors, mouse face and similar. */
2416 enum draw_glyphs_face hl;
2417
2418 /* Face in which this string is to be drawn. */
2419 struct face *face;
2420
2421 /* Font in which this string is to be drawn. */
2422 XFontStruct *font;
2423
2424 /* Font info for this string. */
2425 struct font_info *font_info;
2426
2427 /* Non-null means this string describes (part of) a composition.
2428 All characters from char2b are drawn composed. */
2429 struct composition *cmp;
2430
2431 /* Index of this glyph string's first character in the glyph
2432 definition of CMP. If this is zero, this glyph string describes
2433 the first character of a composition. */
2434 int gidx;
2435
2436 /* 1 means this glyph strings face has to be drawn to the right end
2437 of the window's drawing area. */
2438 unsigned extends_to_end_of_line_p : 1;
2439
2440 /* 1 means the background of this string has been drawn. */
2441 unsigned background_filled_p : 1;
2442
2443 /* 1 means glyph string must be drawn with 16-bit functions. */
2444 unsigned two_byte_p : 1;
2445
2446 /* 1 means that the original font determined for drawing this glyph
2447 string could not be loaded. The member `font' has been set to
2448 the frame's default font in this case. */
2449 unsigned font_not_found_p : 1;
2450
2451 /* 1 means that the face in which this glyph string is drawn has a
2452 stipple pattern. */
2453 unsigned stippled_p : 1;
2454
2455 /* 1 means only the foreground of this glyph string must be drawn,
2456 and we should use the physical height of the line this glyph
2457 string appears in as clip rect. */
2458 unsigned for_overlaps_p : 1;
2459
2460 /* The GC to use for drawing this glyph string. */
2461 GC gc;
2462
2463 /* A pointer to the first glyph in the string. This glyph
2464 corresponds to char2b[0]. Needed to draw rectangles if
2465 font_not_found_p is 1. */
2466 struct glyph *first_glyph;
2467
2468 /* Image, if any. */
2469 struct image *img;
2470
2471 struct glyph_string *next, *prev;
2472 };
2473
2474
2475 #if GLYPH_DEBUG
2476
2477 static void
2478 x_dump_glyph_string (s)
2479 struct glyph_string *s;
2480 {
2481 fprintf (stderr, "glyph string\n");
2482 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2483 s->x, s->y, s->width, s->height);
2484 fprintf (stderr, " ybase = %d\n", s->ybase);
2485 fprintf (stderr, " hl = %d\n", s->hl);
2486 fprintf (stderr, " left overhang = %d, right = %d\n",
2487 s->left_overhang, s->right_overhang);
2488 fprintf (stderr, " nchars = %d\n", s->nchars);
2489 fprintf (stderr, " extends to end of line = %d\n",
2490 s->extends_to_end_of_line_p);
2491 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2492 fprintf (stderr, " bg width = %d\n", s->background_width);
2493 }
2494
2495 #endif /* GLYPH_DEBUG */
2496
2497
2498
2499 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2500 struct glyph_string **,
2501 struct glyph_string *,
2502 struct glyph_string *));
2503 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2504 struct glyph_string **,
2505 struct glyph_string *,
2506 struct glyph_string *));
2507 static void x_append_glyph_string P_ ((struct glyph_string **,
2508 struct glyph_string **,
2509 struct glyph_string *));
2510 static int x_left_overwritten P_ ((struct glyph_string *));
2511 static int x_left_overwriting P_ ((struct glyph_string *));
2512 static int x_right_overwritten P_ ((struct glyph_string *));
2513 static int x_right_overwriting P_ ((struct glyph_string *));
2514 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2515 int));
2516 static void x_init_glyph_string P_ ((struct glyph_string *,
2517 XChar2b *, struct window *,
2518 struct glyph_row *,
2519 enum glyph_row_area, int,
2520 enum draw_glyphs_face));
2521 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2522 enum glyph_row_area, int, int,
2523 enum draw_glyphs_face, int));
2524 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2525 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2526 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2527 int));
2528 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2529 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2530 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2531 static void x_draw_glyph_string P_ ((struct glyph_string *));
2532 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2533 static void x_set_cursor_gc P_ ((struct glyph_string *));
2534 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2535 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2536 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2537 int *, int *));
2538 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2539 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2540 unsigned long *, double, int));
2541 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2542 double, int, unsigned long));
2543 static void x_setup_relief_colors P_ ((struct glyph_string *));
2544 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2545 static void x_draw_image_relief P_ ((struct glyph_string *));
2546 static void x_draw_image_foreground P_ ((struct glyph_string *));
2547 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2548 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2549 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2550 int, int, int));
2551 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2552 int, int, int, int, XRectangle *));
2553 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2554 int, int, int, XRectangle *));
2555 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2556 enum glyph_row_area));
2557 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2558 struct glyph_row *,
2559 enum glyph_row_area, int, int));
2560
2561 #if GLYPH_DEBUG
2562 static void x_check_font P_ ((struct frame *, XFontStruct *));
2563 #endif
2564
2565
2566 /* Append the list of glyph strings with head H and tail T to the list
2567 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2568
2569 static INLINE void
2570 x_append_glyph_string_lists (head, tail, h, t)
2571 struct glyph_string **head, **tail;
2572 struct glyph_string *h, *t;
2573 {
2574 if (h)
2575 {
2576 if (*head)
2577 (*tail)->next = h;
2578 else
2579 *head = h;
2580 h->prev = *tail;
2581 *tail = t;
2582 }
2583 }
2584
2585
2586 /* Prepend the list of glyph strings with head H and tail T to the
2587 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2588 result. */
2589
2590 static INLINE void
2591 x_prepend_glyph_string_lists (head, tail, h, t)
2592 struct glyph_string **head, **tail;
2593 struct glyph_string *h, *t;
2594 {
2595 if (h)
2596 {
2597 if (*head)
2598 (*head)->prev = t;
2599 else
2600 *tail = t;
2601 t->next = *head;
2602 *head = h;
2603 }
2604 }
2605
2606
2607 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2608 Set *HEAD and *TAIL to the resulting list. */
2609
2610 static INLINE void
2611 x_append_glyph_string (head, tail, s)
2612 struct glyph_string **head, **tail;
2613 struct glyph_string *s;
2614 {
2615 s->next = s->prev = NULL;
2616 x_append_glyph_string_lists (head, tail, s, s);
2617 }
2618
2619
2620 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2621 face. */
2622
2623 static void
2624 x_set_cursor_gc (s)
2625 struct glyph_string *s;
2626 {
2627 if (s->font == FRAME_FONT (s->f)
2628 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2629 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2630 && !s->cmp)
2631 s->gc = s->f->output_data.x->cursor_gc;
2632 else
2633 {
2634 /* Cursor on non-default face: must merge. */
2635 XGCValues xgcv;
2636 unsigned long mask;
2637
2638 xgcv.background = s->f->output_data.x->cursor_pixel;
2639 xgcv.foreground = s->face->background;
2640
2641 /* If the glyph would be invisible, try a different foreground. */
2642 if (xgcv.foreground == xgcv.background)
2643 xgcv.foreground = s->face->foreground;
2644 if (xgcv.foreground == xgcv.background)
2645 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2646 if (xgcv.foreground == xgcv.background)
2647 xgcv.foreground = s->face->foreground;
2648
2649 /* Make sure the cursor is distinct from text in this face. */
2650 if (xgcv.background == s->face->background
2651 && xgcv.foreground == s->face->foreground)
2652 {
2653 xgcv.background = s->face->foreground;
2654 xgcv.foreground = s->face->background;
2655 }
2656
2657 IF_DEBUG (x_check_font (s->f, s->font));
2658 xgcv.font = s->font->fid;
2659 xgcv.graphics_exposures = False;
2660 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2661
2662 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2663 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2664 mask, &xgcv);
2665 else
2666 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2667 = XCreateGC (s->display, s->window, mask, &xgcv);
2668
2669 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2670 }
2671 }
2672
2673
2674 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2675
2676 static void
2677 x_set_mouse_face_gc (s)
2678 struct glyph_string *s;
2679 {
2680 int face_id;
2681 struct face *face;
2682
2683 /* What face has to be used last for the mouse face? */
2684 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2685 face = FACE_FROM_ID (s->f, face_id);
2686 if (face == NULL)
2687 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2688
2689 if (s->first_glyph->type == CHAR_GLYPH)
2690 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2691 else
2692 face_id = FACE_FOR_CHAR (s->f, face, 0);
2693 s->face = FACE_FROM_ID (s->f, face_id);
2694 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2695
2696 /* If font in this face is same as S->font, use it. */
2697 if (s->font == s->face->font)
2698 s->gc = s->face->gc;
2699 else
2700 {
2701 /* Otherwise construct scratch_cursor_gc with values from FACE
2702 but font FONT. */
2703 XGCValues xgcv;
2704 unsigned long mask;
2705
2706 xgcv.background = s->face->background;
2707 xgcv.foreground = s->face->foreground;
2708 IF_DEBUG (x_check_font (s->f, s->font));
2709 xgcv.font = s->font->fid;
2710 xgcv.graphics_exposures = False;
2711 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2712
2713 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2714 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2715 mask, &xgcv);
2716 else
2717 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2718 = XCreateGC (s->display, s->window, mask, &xgcv);
2719
2720 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2721 }
2722
2723 xassert (s->gc != 0);
2724 }
2725
2726
2727 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2728 Faces to use in the mode line have already been computed when the
2729 matrix was built, so there isn't much to do, here. */
2730
2731 static INLINE void
2732 x_set_mode_line_face_gc (s)
2733 struct glyph_string *s;
2734 {
2735 s->gc = s->face->gc;
2736 }
2737
2738
2739 /* Set S->gc of glyph string S for drawing that glyph string. Set
2740 S->stippled_p to a non-zero value if the face of S has a stipple
2741 pattern. */
2742
2743 static INLINE void
2744 x_set_glyph_string_gc (s)
2745 struct glyph_string *s;
2746 {
2747 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2748
2749 if (s->hl == DRAW_NORMAL_TEXT)
2750 {
2751 s->gc = s->face->gc;
2752 s->stippled_p = s->face->stipple != 0;
2753 }
2754 else if (s->hl == DRAW_INVERSE_VIDEO)
2755 {
2756 x_set_mode_line_face_gc (s);
2757 s->stippled_p = s->face->stipple != 0;
2758 }
2759 else if (s->hl == DRAW_CURSOR)
2760 {
2761 x_set_cursor_gc (s);
2762 s->stippled_p = 0;
2763 }
2764 else if (s->hl == DRAW_MOUSE_FACE)
2765 {
2766 x_set_mouse_face_gc (s);
2767 s->stippled_p = s->face->stipple != 0;
2768 }
2769 else if (s->hl == DRAW_IMAGE_RAISED
2770 || s->hl == DRAW_IMAGE_SUNKEN)
2771 {
2772 s->gc = s->face->gc;
2773 s->stippled_p = s->face->stipple != 0;
2774 }
2775 else
2776 {
2777 s->gc = s->face->gc;
2778 s->stippled_p = s->face->stipple != 0;
2779 }
2780
2781 /* GC must have been set. */
2782 xassert (s->gc != 0);
2783 }
2784
2785
2786 /* Return in *R the clipping rectangle for glyph string S. */
2787
2788 static void
2789 x_get_glyph_string_clip_rect (s, r)
2790 struct glyph_string *s;
2791 XRectangle *r;
2792 {
2793 if (s->row->full_width_p)
2794 {
2795 /* Draw full-width. X coordinates are relative to S->w->left. */
2796 int canon_x = CANON_X_UNIT (s->f);
2797
2798 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2799 r->width = XFASTINT (s->w->width) * canon_x;
2800
2801 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2802 {
2803 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2804 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2805 r->x -= width;
2806 }
2807
2808 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2809
2810 /* Unless displaying a mode or menu bar line, which are always
2811 fully visible, clip to the visible part of the row. */
2812 if (s->w->pseudo_window_p)
2813 r->height = s->row->visible_height;
2814 else
2815 r->height = s->height;
2816 }
2817 else
2818 {
2819 /* This is a text line that may be partially visible. */
2820 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2821 r->width = window_box_width (s->w, s->area);
2822 r->height = s->row->visible_height;
2823 }
2824
2825 /* If S draws overlapping rows, it's sufficient to use the top and
2826 bottom of the window for clipping because this glyph string
2827 intentionally draws over other lines. */
2828 if (s->for_overlaps_p)
2829 {
2830 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2831 r->height = window_text_bottom_y (s->w) - r->y;
2832 }
2833 else
2834 {
2835 /* Don't use S->y for clipping because it doesn't take partially
2836 visible lines into account. For example, it can be negative for
2837 partially visible lines at the top of a window. */
2838 if (!s->row->full_width_p
2839 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2840 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2841 else
2842 r->y = max (0, s->row->y);
2843
2844 /* If drawing a tool-bar window, draw it over the internal border
2845 at the top of the window. */
2846 if (s->w == XWINDOW (s->f->tool_bar_window))
2847 r->y -= s->f->output_data.x->internal_border_width;
2848 }
2849
2850 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2851 }
2852
2853
2854 /* Set clipping for output of glyph string S. S may be part of a mode
2855 line or menu if we don't have X toolkit support. */
2856
2857 static INLINE void
2858 x_set_glyph_string_clipping (s)
2859 struct glyph_string *s;
2860 {
2861 XRectangle r;
2862 x_get_glyph_string_clip_rect (s, &r);
2863 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2864 }
2865
2866
2867 /* Compute left and right overhang of glyph string S. If S is a glyph
2868 string for a composition, assume overhangs don't exist. */
2869
2870 static INLINE void
2871 x_compute_glyph_string_overhangs (s)
2872 struct glyph_string *s;
2873 {
2874 if (s->cmp == NULL
2875 && s->first_glyph->type == CHAR_GLYPH)
2876 {
2877 XCharStruct cs;
2878 int direction, font_ascent, font_descent;
2879 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2880 &font_ascent, &font_descent, &cs);
2881 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2882 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2883 }
2884 }
2885
2886
2887 /* Compute overhangs and x-positions for glyph string S and its
2888 predecessors, or successors. X is the starting x-position for S.
2889 BACKWARD_P non-zero means process predecessors. */
2890
2891 static void
2892 x_compute_overhangs_and_x (s, x, backward_p)
2893 struct glyph_string *s;
2894 int x;
2895 int backward_p;
2896 {
2897 if (backward_p)
2898 {
2899 while (s)
2900 {
2901 x_compute_glyph_string_overhangs (s);
2902 x -= s->width;
2903 s->x = x;
2904 s = s->prev;
2905 }
2906 }
2907 else
2908 {
2909 while (s)
2910 {
2911 x_compute_glyph_string_overhangs (s);
2912 s->x = x;
2913 x += s->width;
2914 s = s->next;
2915 }
2916 }
2917 }
2918
2919
2920 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2921 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2922 assumed to be zero. */
2923
2924 static void
2925 x_get_glyph_overhangs (glyph, f, left, right)
2926 struct glyph *glyph;
2927 struct frame *f;
2928 int *left, *right;
2929 {
2930 *left = *right = 0;
2931
2932 if (glyph->type == CHAR_GLYPH)
2933 {
2934 XFontStruct *font;
2935 struct face *face;
2936 struct font_info *font_info;
2937 XChar2b char2b;
2938 XCharStruct *pcm;
2939
2940 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2941 font = face->font;
2942 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2943 if (font
2944 && (pcm = x_per_char_metric (font, &char2b)))
2945 {
2946 if (pcm->rbearing > pcm->width)
2947 *right = pcm->rbearing - pcm->width;
2948 if (pcm->lbearing < 0)
2949 *left = -pcm->lbearing;
2950 }
2951 }
2952 }
2953
2954
2955 /* Return the index of the first glyph preceding glyph string S that
2956 is overwritten by S because of S's left overhang. Value is -1
2957 if no glyphs are overwritten. */
2958
2959 static int
2960 x_left_overwritten (s)
2961 struct glyph_string *s;
2962 {
2963 int k;
2964
2965 if (s->left_overhang)
2966 {
2967 int x = 0, i;
2968 struct glyph *glyphs = s->row->glyphs[s->area];
2969 int first = s->first_glyph - glyphs;
2970
2971 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2972 x -= glyphs[i].pixel_width;
2973
2974 k = i + 1;
2975 }
2976 else
2977 k = -1;
2978
2979 return k;
2980 }
2981
2982
2983 /* Return the index of the first glyph preceding glyph string S that
2984 is overwriting S because of its right overhang. Value is -1 if no
2985 glyph in front of S overwrites S. */
2986
2987 static int
2988 x_left_overwriting (s)
2989 struct glyph_string *s;
2990 {
2991 int i, k, x;
2992 struct glyph *glyphs = s->row->glyphs[s->area];
2993 int first = s->first_glyph - glyphs;
2994
2995 k = -1;
2996 x = 0;
2997 for (i = first - 1; i >= 0; --i)
2998 {
2999 int left, right;
3000 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3001 if (x + right > 0)
3002 k = i;
3003 x -= glyphs[i].pixel_width;
3004 }
3005
3006 return k;
3007 }
3008
3009
3010 /* Return the index of the last glyph following glyph string S that is
3011 not overwritten by S because of S's right overhang. Value is -1 if
3012 no such glyph is found. */
3013
3014 static int
3015 x_right_overwritten (s)
3016 struct glyph_string *s;
3017 {
3018 int k = -1;
3019
3020 if (s->right_overhang)
3021 {
3022 int x = 0, i;
3023 struct glyph *glyphs = s->row->glyphs[s->area];
3024 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3025 int end = s->row->used[s->area];
3026
3027 for (i = first; i < end && s->right_overhang > x; ++i)
3028 x += glyphs[i].pixel_width;
3029
3030 k = i;
3031 }
3032
3033 return k;
3034 }
3035
3036
3037 /* Return the index of the last glyph following glyph string S that
3038 overwrites S because of its left overhang. Value is negative
3039 if no such glyph is found. */
3040
3041 static int
3042 x_right_overwriting (s)
3043 struct glyph_string *s;
3044 {
3045 int i, k, x;
3046 int end = s->row->used[s->area];
3047 struct glyph *glyphs = s->row->glyphs[s->area];
3048 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3049
3050 k = -1;
3051 x = 0;
3052 for (i = first; i < end; ++i)
3053 {
3054 int left, right;
3055 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3056 if (x - left < 0)
3057 k = i;
3058 x += glyphs[i].pixel_width;
3059 }
3060
3061 return k;
3062 }
3063
3064
3065 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3066
3067 static INLINE void
3068 x_clear_glyph_string_rect (s, x, y, w, h)
3069 struct glyph_string *s;
3070 int x, y, w, h;
3071 {
3072 XGCValues xgcv;
3073 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3074 XSetForeground (s->display, s->gc, xgcv.background);
3075 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3076 XSetForeground (s->display, s->gc, xgcv.foreground);
3077 }
3078
3079
3080 /* Draw the background of glyph_string S. If S->background_filled_p
3081 is non-zero don't draw it. FORCE_P non-zero means draw the
3082 background even if it wouldn't be drawn normally. This is used
3083 when a string preceding S draws into the background of S, or S
3084 contains the first component of a composition. */
3085
3086 static void
3087 x_draw_glyph_string_background (s, force_p)
3088 struct glyph_string *s;
3089 int force_p;
3090 {
3091 /* Nothing to do if background has already been drawn or if it
3092 shouldn't be drawn in the first place. */
3093 if (!s->background_filled_p)
3094 {
3095 int box_line_width = max (s->face->box_line_width, 0);
3096
3097 if (s->stippled_p)
3098 {
3099 /* Fill background with a stipple pattern. */
3100 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3101 XFillRectangle (s->display, s->window, s->gc, s->x,
3102 s->y + box_line_width,
3103 s->background_width,
3104 s->height - 2 * box_line_width);
3105 XSetFillStyle (s->display, s->gc, FillSolid);
3106 s->background_filled_p = 1;
3107 }
3108 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
3109 || s->font_not_found_p
3110 || s->extends_to_end_of_line_p
3111 || force_p)
3112 {
3113 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
3114 s->background_width,
3115 s->height - 2 * box_line_width);
3116 s->background_filled_p = 1;
3117 }
3118 }
3119 }
3120
3121
3122 /* Draw the foreground of glyph string S. */
3123
3124 static void
3125 x_draw_glyph_string_foreground (s)
3126 struct glyph_string *s;
3127 {
3128 int i, x;
3129
3130 /* If first glyph of S has a left box line, start drawing the text
3131 of S to the right of that box line. */
3132 if (s->face->box != FACE_NO_BOX
3133 && s->first_glyph->left_box_line_p)
3134 x = s->x + abs (s->face->box_line_width);
3135 else
3136 x = s->x;
3137
3138 /* Draw characters of S as rectangles if S's font could not be
3139 loaded. */
3140 if (s->font_not_found_p)
3141 {
3142 for (i = 0; i < s->nchars; ++i)
3143 {
3144 struct glyph *g = s->first_glyph + i;
3145 XDrawRectangle (s->display, s->window,
3146 s->gc, x, s->y, g->pixel_width - 1,
3147 s->height - 1);
3148 x += g->pixel_width;
3149 }
3150 }
3151 else
3152 {
3153 char *char1b = (char *) s->char2b;
3154 int boff = s->font_info->baseline_offset;
3155
3156 if (s->font_info->vertical_centering)
3157 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3158
3159 /* If we can use 8-bit functions, condense S->char2b. */
3160 if (!s->two_byte_p)
3161 for (i = 0; i < s->nchars; ++i)
3162 char1b[i] = s->char2b[i].byte2;
3163
3164 /* Draw text with XDrawString if background has already been
3165 filled. Otherwise, use XDrawImageString. (Note that
3166 XDrawImageString is usually faster than XDrawString.) Always
3167 use XDrawImageString when drawing the cursor so that there is
3168 no chance that characters under a box cursor are invisible. */
3169 if (s->for_overlaps_p
3170 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3171 {
3172 /* Draw characters with 16-bit or 8-bit functions. */
3173 if (s->two_byte_p)
3174 XDrawString16 (s->display, s->window, s->gc, x,
3175 s->ybase - boff, s->char2b, s->nchars);
3176 else
3177 XDrawString (s->display, s->window, s->gc, x,
3178 s->ybase - boff, char1b, s->nchars);
3179 }
3180 else
3181 {
3182 if (s->two_byte_p)
3183 XDrawImageString16 (s->display, s->window, s->gc, x,
3184 s->ybase - boff, s->char2b, s->nchars);
3185 else
3186 XDrawImageString (s->display, s->window, s->gc, x,
3187 s->ybase - boff, char1b, s->nchars);
3188 }
3189 }
3190 }
3191
3192 /* Draw the foreground of composite glyph string S. */
3193
3194 static void
3195 x_draw_composite_glyph_string_foreground (s)
3196 struct glyph_string *s;
3197 {
3198 int i, x;
3199
3200 /* If first glyph of S has a left box line, start drawing the text
3201 of S to the right of that box line. */
3202 if (s->face->box != FACE_NO_BOX
3203 && s->first_glyph->left_box_line_p)
3204 x = s->x + abs (s->face->box_line_width);
3205 else
3206 x = s->x;
3207
3208 /* S is a glyph string for a composition. S->gidx is the index of
3209 the first character drawn for glyphs of this composition.
3210 S->gidx == 0 means we are drawing the very first character of
3211 this composition. */
3212
3213 /* Draw a rectangle for the composition if the font for the very
3214 first character of the composition could not be loaded. */
3215 if (s->font_not_found_p)
3216 {
3217 if (s->gidx == 0)
3218 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3219 s->width - 1, s->height - 1);
3220 }
3221 else
3222 {
3223 for (i = 0; i < s->nchars; i++, ++s->gidx)
3224 XDrawString16 (s->display, s->window, s->gc,
3225 x + s->cmp->offsets[s->gidx * 2],
3226 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3227 s->char2b + i, 1);
3228 }
3229 }
3230
3231
3232 #ifdef USE_X_TOOLKIT
3233
3234 static struct frame *x_frame_of_widget P_ ((Widget));
3235 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
3236 XrmValue *, XrmValue *, XtPointer *));
3237 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
3238 XrmValue *, Cardinal *));
3239
3240
3241 /* Return the frame on which widget WIDGET is used.. Abort if frame
3242 cannot be determined. */
3243
3244 static struct frame *
3245 x_frame_of_widget (widget)
3246 Widget widget;
3247 {
3248 struct x_display_info *dpyinfo;
3249 Lisp_Object tail;
3250 struct frame *f;
3251
3252 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3253
3254 /* Find the top-level shell of the widget. Note that this function
3255 can be called when the widget is not yet realized, so XtWindow
3256 (widget) == 0. That's the reason we can't simply use
3257 x_any_window_to_frame. */
3258 while (!XtIsTopLevelShell (widget))
3259 widget = XtParent (widget);
3260
3261 /* Look for a frame with that top-level widget. Allocate the color
3262 on that frame to get the right gamma correction value. */
3263 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3264 if (GC_FRAMEP (XCAR (tail))
3265 && (f = XFRAME (XCAR (tail)),
3266 (f->output_data.nothing != 1
3267 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3268 && f->output_data.x->widget == widget)
3269 return f;
3270
3271 abort ();
3272 }
3273
3274
3275 /* Allocate the color COLOR->pixel on the screen and display of
3276 widget WIDGET in colormap CMAP. If an exact match cannot be
3277 allocated, try the nearest color available. Value is non-zero
3278 if successful. This is called from lwlib. */
3279
3280 int
3281 x_alloc_nearest_color_for_widget (widget, cmap, color)
3282 Widget widget;
3283 Colormap cmap;
3284 XColor *color;
3285 {
3286 struct frame *f = x_frame_of_widget (widget);
3287 return x_alloc_nearest_color (f, cmap, color);
3288 }
3289
3290
3291 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3292 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3293 If this produces the same color as PIXEL, try a color where all RGB
3294 values have DELTA added. Return the allocated color in *PIXEL.
3295 DISPLAY is the X display, CMAP is the colormap to operate on.
3296 Value is non-zero if successful. */
3297
3298 int
3299 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3300 Widget widget;
3301 Display *display;
3302 Colormap cmap;
3303 unsigned long *pixel;
3304 double factor;
3305 int delta;
3306 {
3307 struct frame *f = x_frame_of_widget (widget);
3308 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3309 }
3310
3311
3312 /* Structure specifying which arguments should be passed by Xt to
3313 cvt_string_to_pixel. We want the widget's screen and colormap. */
3314
3315 static XtConvertArgRec cvt_string_to_pixel_args[] =
3316 {
3317 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
3318 sizeof (Screen *)},
3319 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
3320 sizeof (Colormap)}
3321 };
3322
3323
3324 /* The address of this variable is returned by
3325 cvt_string_to_pixel. */
3326
3327 static Pixel cvt_string_to_pixel_value;
3328
3329
3330 /* Convert a color name to a pixel color.
3331
3332 DPY is the display we are working on.
3333
3334 ARGS is an array of *NARGS XrmValue structures holding additional
3335 information about the widget for which the conversion takes place.
3336 The contents of this array are determined by the specification
3337 in cvt_string_to_pixel_args.
3338
3339 FROM is a pointer to an XrmValue which points to the color name to
3340 convert. TO is an XrmValue in which to return the pixel color.
3341
3342 CLOSURE_RET is a pointer to user-data, in which we record if
3343 we allocated the color or not.
3344
3345 Value is True if successful, False otherwise. */
3346
3347 static Boolean
3348 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
3349 Display *dpy;
3350 XrmValue *args;
3351 Cardinal *nargs;
3352 XrmValue *from, *to;
3353 XtPointer *closure_ret;
3354 {
3355 Screen *screen;
3356 Colormap cmap;
3357 Pixel pixel;
3358 String color_name;
3359 XColor color;
3360
3361 if (*nargs != 2)
3362 {
3363 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3364 "wrongParameters", "cvt_string_to_pixel",
3365 "XtToolkitError",
3366 "Screen and colormap args required", NULL, NULL);
3367 return False;
3368 }
3369
3370 screen = *(Screen **) args[0].addr;
3371 cmap = *(Colormap *) args[1].addr;
3372 color_name = (String) from->addr;
3373
3374 if (strcmp (color_name, XtDefaultBackground) == 0)
3375 {
3376 *closure_ret = (XtPointer) False;
3377 pixel = WhitePixelOfScreen (screen);
3378 }
3379 else if (strcmp (color_name, XtDefaultForeground) == 0)
3380 {
3381 *closure_ret = (XtPointer) False;
3382 pixel = BlackPixelOfScreen (screen);
3383 }
3384 else if (XParseColor (dpy, cmap, color_name, &color)
3385 && x_alloc_nearest_color_1 (dpy, cmap, &color))
3386 {
3387 pixel = color.pixel;
3388 *closure_ret = (XtPointer) True;
3389 }
3390 else
3391 {
3392 String params[1];
3393 Cardinal nparams = 1;
3394
3395 params[0] = color_name;
3396 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3397 "badValue", "cvt_string_to_pixel",
3398 "XtToolkitError", "Invalid color `%s'",
3399 params, &nparams);
3400 return False;
3401 }
3402
3403 if (to->addr != NULL)
3404 {
3405 if (to->size < sizeof (Pixel))
3406 {
3407 to->size = sizeof (Pixel);
3408 return False;
3409 }
3410
3411 *(Pixel *) to->addr = pixel;
3412 }
3413 else
3414 {
3415 cvt_string_to_pixel_value = pixel;
3416 to->addr = (XtPointer) &cvt_string_to_pixel_value;
3417 }
3418
3419 to->size = sizeof (Pixel);
3420 return True;
3421 }
3422
3423
3424 /* Free a pixel color which was previously allocated via
3425 cvt_string_to_pixel. This is registered as the destructor
3426 for this type of resource via XtSetTypeConverter.
3427
3428 APP is the application context in which we work.
3429
3430 TO is a pointer to an XrmValue holding the color to free.
3431 CLOSURE is the value we stored in CLOSURE_RET for this color
3432 in cvt_string_to_pixel.
3433
3434 ARGS and NARGS are like for cvt_string_to_pixel. */
3435
3436 static void
3437 cvt_pixel_dtor (app, to, closure, args, nargs)
3438 XtAppContext app;
3439 XrmValuePtr to;
3440 XtPointer closure;
3441 XrmValuePtr args;
3442 Cardinal *nargs;
3443 {
3444 if (*nargs != 2)
3445 {
3446 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
3447 "XtToolkitError",
3448 "Screen and colormap arguments required",
3449 NULL, NULL);
3450 }
3451 else if (closure != NULL)
3452 {
3453 /* We did allocate the pixel, so free it. */
3454 Screen *screen = *(Screen **) args[0].addr;
3455 Colormap cmap = *(Colormap *) args[1].addr;
3456 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
3457 (Pixel *) to->addr, 1);
3458 }
3459 }
3460
3461
3462 #endif /* USE_X_TOOLKIT */
3463
3464
3465 /* Value is an array of XColor structures for the contents of the
3466 color map of display DPY. Set *NCELLS to the size of the array.
3467 Note that this probably shouldn't be called for large color maps,
3468 say a 24-bit TrueColor map. */
3469
3470 static const XColor *
3471 x_color_cells (dpy, ncells)
3472 Display *dpy;
3473 int *ncells;
3474 {
3475 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3476
3477 if (dpyinfo->color_cells == NULL)
3478 {
3479 Screen *screen = dpyinfo->screen;
3480 int i;
3481
3482 dpyinfo->ncolor_cells
3483 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
3484 dpyinfo->color_cells
3485 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3486 * sizeof *dpyinfo->color_cells);
3487
3488 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3489 dpyinfo->color_cells[i].pixel = i;
3490
3491 XQueryColors (dpy, dpyinfo->cmap,
3492 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3493 }
3494
3495 *ncells = dpyinfo->ncolor_cells;
3496 return dpyinfo->color_cells;
3497 }
3498
3499
3500 /* On frame F, translate pixel colors to RGB values for the NCOLORS
3501 colors in COLORS. Use cached information, if available. */
3502
3503 void
3504 x_query_colors (f, colors, ncolors)
3505 struct frame *f;
3506 XColor *colors;
3507 int ncolors;
3508 {
3509 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3510
3511 if (dpyinfo->color_cells)
3512 {
3513 int i;
3514 for (i = 0; i < ncolors; ++i)
3515 {
3516 unsigned long pixel = colors[i].pixel;
3517 xassert (pixel < dpyinfo->ncolor_cells);
3518 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3519 colors[i] = dpyinfo->color_cells[pixel];
3520 }
3521 }
3522 else
3523 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3524 }
3525
3526
3527 /* On frame F, translate pixel color to RGB values for the color in
3528 COLOR. Use cached information, if available. */
3529
3530 void
3531 x_query_color (f, color)
3532 struct frame *f;
3533 XColor *color;
3534 {
3535 x_query_colors (f, color, 1);
3536 }
3537
3538
3539 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
3540 exact match can't be allocated, try the nearest color available.
3541 Value is non-zero if successful. Set *COLOR to the color
3542 allocated. */
3543
3544 static int
3545 x_alloc_nearest_color_1 (dpy, cmap, color)
3546 Display *dpy;
3547 Colormap cmap;
3548 XColor *color;
3549 {
3550 int rc;
3551
3552 rc = XAllocColor (dpy, cmap, color);
3553 if (rc == 0)
3554 {
3555 /* If we got to this point, the colormap is full, so we're going
3556 to try to get the next closest color. The algorithm used is
3557 a least-squares matching, which is what X uses for closest
3558 color matching with StaticColor visuals. */
3559 int nearest, i;
3560 unsigned long nearest_delta = ~0;
3561 int ncells;
3562 const XColor *cells = x_color_cells (dpy, &ncells);
3563
3564 for (nearest = i = 0; i < ncells; ++i)
3565 {
3566 long dred = (color->red >> 8) - (cells[i].red >> 8);
3567 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3568 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3569 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3570
3571 if (delta < nearest_delta)
3572 {
3573 nearest = i;
3574 nearest_delta = delta;
3575 }
3576 }
3577
3578 color->red = cells[nearest].red;
3579 color->green = cells[nearest].green;
3580 color->blue = cells[nearest].blue;
3581 rc = XAllocColor (dpy, cmap, color);
3582 }
3583 else
3584 {
3585 /* If allocation succeeded, and the allocated pixel color is not
3586 equal to a cached pixel color recorded earlier, there was a
3587 change in the colormap, so clear the color cache. */
3588 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3589 XColor *cached_color;
3590
3591 if (dpyinfo->color_cells
3592 && (cached_color = &dpyinfo->color_cells[color->pixel],
3593 (cached_color->red != color->red
3594 || cached_color->blue != color->blue
3595 || cached_color->green != color->green)))
3596 {
3597 xfree (dpyinfo->color_cells);
3598 dpyinfo->color_cells = NULL;
3599 dpyinfo->ncolor_cells = 0;
3600 }
3601 }
3602
3603 #ifdef DEBUG_X_COLORS
3604 if (rc)
3605 register_color (color->pixel);
3606 #endif /* DEBUG_X_COLORS */
3607
3608 return rc;
3609 }
3610
3611
3612 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
3613 exact match can't be allocated, try the nearest color available.
3614 Value is non-zero if successful. Set *COLOR to the color
3615 allocated. */
3616
3617 int
3618 x_alloc_nearest_color (f, cmap, color)
3619 struct frame *f;
3620 Colormap cmap;
3621 XColor *color;
3622 {
3623 gamma_correct (f, color);
3624 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
3625 }
3626
3627
3628 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3629 It's necessary to do this instead of just using PIXEL directly to
3630 get color reference counts right. */
3631
3632 unsigned long
3633 x_copy_color (f, pixel)
3634 struct frame *f;
3635 unsigned long pixel;
3636 {
3637 XColor color;
3638
3639 color.pixel = pixel;
3640 BLOCK_INPUT;
3641 x_query_color (f, &color);
3642 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3643 UNBLOCK_INPUT;
3644 #ifdef DEBUG_X_COLORS
3645 register_color (pixel);
3646 #endif
3647 return color.pixel;
3648 }
3649
3650
3651 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3652 It's necessary to do this instead of just using PIXEL directly to
3653 get color reference counts right. */
3654
3655 unsigned long
3656 x_copy_dpy_color (dpy, cmap, pixel)
3657 Display *dpy;
3658 Colormap cmap;
3659 unsigned long pixel;
3660 {
3661 XColor color;
3662
3663 color.pixel = pixel;
3664 BLOCK_INPUT;
3665 XQueryColor (dpy, cmap, &color);
3666 XAllocColor (dpy, cmap, &color);
3667 UNBLOCK_INPUT;
3668 #ifdef DEBUG_X_COLORS
3669 register_color (pixel);
3670 #endif
3671 return color.pixel;
3672 }
3673
3674
3675 /* Brightness beyond which a color won't have its highlight brightness
3676 boosted.
3677
3678 Nominally, highlight colors for `3d' faces are calculated by
3679 brightening an object's color by a constant scale factor, but this
3680 doesn't yield good results for dark colors, so for colors who's
3681 brightness is less than this value (on a scale of 0-65535) have an
3682 use an additional additive factor.
3683
3684 The value here is set so that the default menu-bar/mode-line color
3685 (grey75) will not have its highlights changed at all. */
3686 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3687
3688
3689 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3690 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3691 If this produces the same color as PIXEL, try a color where all RGB
3692 values have DELTA added. Return the allocated color in *PIXEL.
3693 DISPLAY is the X display, CMAP is the colormap to operate on.
3694 Value is non-zero if successful. */
3695
3696 static int
3697 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3698 struct frame *f;
3699 Display *display;
3700 Colormap cmap;
3701 unsigned long *pixel;
3702 double factor;
3703 int delta;
3704 {
3705 XColor color, new;
3706 long bright;
3707 int success_p;
3708
3709 /* Get RGB color values. */
3710 color.pixel = *pixel;
3711 x_query_color (f, &color);
3712
3713 /* Change RGB values by specified FACTOR. Avoid overflow! */
3714 xassert (factor >= 0);
3715 new.red = min (0xffff, factor * color.red);
3716 new.green = min (0xffff, factor * color.green);
3717 new.blue = min (0xffff, factor * color.blue);
3718
3719 /* Calculate brightness of COLOR. */
3720 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
3721
3722 /* We only boost colors that are darker than
3723 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3724 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3725 /* Make an additive adjustment to NEW, because it's dark enough so
3726 that scaling by FACTOR alone isn't enough. */
3727 {
3728 /* How far below the limit this color is (0 - 1, 1 being darker). */
3729 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3730 /* The additive adjustment. */
3731 int min_delta = delta * dimness * factor / 2;
3732
3733 if (factor < 1)
3734 {
3735 new.red = max (0, new.red - min_delta);
3736 new.green = max (0, new.green - min_delta);
3737 new.blue = max (0, new.blue - min_delta);
3738 }
3739 else
3740 {
3741 new.red = min (0xffff, min_delta + new.red);
3742 new.green = min (0xffff, min_delta + new.green);
3743 new.blue = min (0xffff, min_delta + new.blue);
3744 }
3745 }
3746
3747 /* Try to allocate the color. */
3748 success_p = x_alloc_nearest_color (f, cmap, &new);
3749 if (success_p)
3750 {
3751 if (new.pixel == *pixel)
3752 {
3753 /* If we end up with the same color as before, try adding
3754 delta to the RGB values. */
3755 x_free_colors (f, &new.pixel, 1);
3756
3757 new.red = min (0xffff, delta + color.red);
3758 new.green = min (0xffff, delta + color.green);
3759 new.blue = min (0xffff, delta + color.blue);
3760 success_p = x_alloc_nearest_color (f, cmap, &new);
3761 }
3762 else
3763 success_p = 1;
3764 *pixel = new.pixel;
3765 }
3766
3767 return success_p;
3768 }
3769
3770
3771 /* Set up the foreground color for drawing relief lines of glyph
3772 string S. RELIEF is a pointer to a struct relief containing the GC
3773 with which lines will be drawn. Use a color that is FACTOR or
3774 DELTA lighter or darker than the relief's background which is found
3775 in S->f->output_data.x->relief_background. If such a color cannot
3776 be allocated, use DEFAULT_PIXEL, instead. */
3777
3778 static void
3779 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3780 struct frame *f;
3781 struct relief *relief;
3782 double factor;
3783 int delta;
3784 unsigned long default_pixel;
3785 {
3786 XGCValues xgcv;
3787 struct x_output *di = f->output_data.x;
3788 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3789 unsigned long pixel;
3790 unsigned long background = di->relief_background;
3791 Colormap cmap = FRAME_X_COLORMAP (f);
3792 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3793 Display *dpy = FRAME_X_DISPLAY (f);
3794
3795 xgcv.graphics_exposures = False;
3796 xgcv.line_width = 1;
3797
3798 /* Free previously allocated color. The color cell will be reused
3799 when it has been freed as many times as it was allocated, so this
3800 doesn't affect faces using the same colors. */
3801 if (relief->gc
3802 && relief->allocated_p)
3803 {
3804 x_free_colors (f, &relief->pixel, 1);
3805 relief->allocated_p = 0;
3806 }
3807
3808 /* Allocate new color. */
3809 xgcv.foreground = default_pixel;
3810 pixel = background;
3811 if (dpyinfo->n_planes != 1
3812 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3813 {
3814 relief->allocated_p = 1;
3815 xgcv.foreground = relief->pixel = pixel;
3816 }
3817
3818 if (relief->gc == 0)
3819 {
3820 xgcv.stipple = dpyinfo->gray;
3821 mask |= GCStipple;
3822 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3823 }
3824 else
3825 XChangeGC (dpy, relief->gc, mask, &xgcv);
3826 }
3827
3828
3829 /* Set up colors for the relief lines around glyph string S. */
3830
3831 static void
3832 x_setup_relief_colors (s)
3833 struct glyph_string *s;
3834 {
3835 struct x_output *di = s->f->output_data.x;
3836 unsigned long color;
3837
3838 if (s->face->use_box_color_for_shadows_p)
3839 color = s->face->box_color;
3840 else if (s->first_glyph->type == IMAGE_GLYPH
3841 && s->img->pixmap
3842 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
3843 color = IMAGE_BACKGROUND (s->img, s->f, 0);
3844 else
3845 {
3846 XGCValues xgcv;
3847
3848 /* Get the background color of the face. */
3849 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3850 color = xgcv.background;
3851 }
3852
3853 if (di->white_relief.gc == 0
3854 || color != di->relief_background)
3855 {
3856 di->relief_background = color;
3857 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3858 WHITE_PIX_DEFAULT (s->f));
3859 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3860 BLACK_PIX_DEFAULT (s->f));
3861 }
3862 }
3863
3864
3865 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3866 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3867 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3868 relief. LEFT_P non-zero means draw a relief on the left side of
3869 the rectangle. RIGHT_P non-zero means draw a relief on the right
3870 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3871 when drawing. */
3872
3873 static void
3874 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3875 raised_p, left_p, right_p, clip_rect)
3876 struct frame *f;
3877 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3878 XRectangle *clip_rect;
3879 {
3880 Display *dpy = FRAME_X_DISPLAY (f);
3881 Window window = FRAME_X_WINDOW (f);
3882 int i;
3883 GC gc;
3884
3885 if (raised_p)
3886 gc = f->output_data.x->white_relief.gc;
3887 else
3888 gc = f->output_data.x->black_relief.gc;
3889 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
3890
3891 /* Top. */
3892 for (i = 0; i < width; ++i)
3893 XDrawLine (dpy, window, gc,
3894 left_x + i * left_p, top_y + i,
3895 right_x + 1 - i * right_p, top_y + i);
3896
3897 /* Left. */
3898 if (left_p)
3899 for (i = 0; i < width; ++i)
3900 XDrawLine (dpy, window, gc,
3901 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
3902
3903 XSetClipMask (dpy, gc, None);
3904 if (raised_p)
3905 gc = f->output_data.x->black_relief.gc;
3906 else
3907 gc = f->output_data.x->white_relief.gc;
3908 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
3909
3910 /* Bottom. */
3911 for (i = 0; i < width; ++i)
3912 XDrawLine (dpy, window, gc,
3913 left_x + i * left_p, bottom_y - i,
3914 right_x + 1 - i * right_p, bottom_y - i);
3915
3916 /* Right. */
3917 if (right_p)
3918 for (i = 0; i < width; ++i)
3919 XDrawLine (dpy, window, gc,
3920 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3921
3922 XSetClipMask (dpy, gc, None);
3923 }
3924
3925
3926 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3927 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3928 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3929 left side of the rectangle. RIGHT_P non-zero means draw a line
3930 on the right side of the rectangle. CLIP_RECT is the clipping
3931 rectangle to use when drawing. */
3932
3933 static void
3934 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3935 left_p, right_p, clip_rect)
3936 struct glyph_string *s;
3937 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3938 XRectangle *clip_rect;
3939 {
3940 XGCValues xgcv;
3941
3942 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3943 XSetForeground (s->display, s->gc, s->face->box_color);
3944 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3945
3946 /* Top. */
3947 XFillRectangle (s->display, s->window, s->gc,
3948 left_x, top_y, right_x - left_x + 1, width);
3949
3950 /* Left. */
3951 if (left_p)
3952 XFillRectangle (s->display, s->window, s->gc,
3953 left_x, top_y, width, bottom_y - top_y + 1);
3954
3955 /* Bottom. */
3956 XFillRectangle (s->display, s->window, s->gc,
3957 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3958
3959 /* Right. */
3960 if (right_p)
3961 XFillRectangle (s->display, s->window, s->gc,
3962 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3963
3964 XSetForeground (s->display, s->gc, xgcv.foreground);
3965 XSetClipMask (s->display, s->gc, None);
3966 }
3967
3968
3969 /* Draw a box around glyph string S. */
3970
3971 static void
3972 x_draw_glyph_string_box (s)
3973 struct glyph_string *s;
3974 {
3975 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3976 int left_p, right_p;
3977 struct glyph *last_glyph;
3978 XRectangle clip_rect;
3979
3980 last_x = window_box_right (s->w, s->area);
3981 if (s->row->full_width_p
3982 && !s->w->pseudo_window_p)
3983 {
3984 last_x += FRAME_X_RIGHT_FRINGE_WIDTH (s->f);
3985 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3986 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3987 }
3988
3989 /* The glyph that may have a right box line. */
3990 last_glyph = (s->cmp || s->img
3991 ? s->first_glyph
3992 : s->first_glyph + s->nchars - 1);
3993
3994 width = abs (s->face->box_line_width);
3995 raised_p = s->face->box == FACE_RAISED_BOX;
3996 left_x = s->x;
3997 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
3998 ? last_x - 1
3999 : min (last_x, s->x + s->background_width) - 1);
4000 top_y = s->y;
4001 bottom_y = top_y + s->height - 1;
4002
4003 left_p = (s->first_glyph->left_box_line_p
4004 || (s->hl == DRAW_MOUSE_FACE
4005 && (s->prev == NULL
4006 || s->prev->hl != s->hl)));
4007 right_p = (last_glyph->right_box_line_p
4008 || (s->hl == DRAW_MOUSE_FACE
4009 && (s->next == NULL
4010 || s->next->hl != s->hl)));
4011
4012 x_get_glyph_string_clip_rect (s, &clip_rect);
4013
4014 if (s->face->box == FACE_SIMPLE_BOX)
4015 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
4016 left_p, right_p, &clip_rect);
4017 else
4018 {
4019 x_setup_relief_colors (s);
4020 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
4021 width, raised_p, left_p, right_p, &clip_rect);
4022 }
4023 }
4024
4025
4026 /* Draw foreground of image glyph string S. */
4027
4028 static void
4029 x_draw_image_foreground (s)
4030 struct glyph_string *s;
4031 {
4032 int x;
4033 int y = s->ybase - image_ascent (s->img, s->face);
4034
4035 /* If first glyph of S has a left box line, start drawing it to the
4036 right of that line. */
4037 if (s->face->box != FACE_NO_BOX
4038 && s->first_glyph->left_box_line_p)
4039 x = s->x + abs (s->face->box_line_width);
4040 else
4041 x = s->x;
4042
4043 /* If there is a margin around the image, adjust x- and y-position
4044 by that margin. */
4045 x += s->img->hmargin;
4046 y += s->img->vmargin;
4047
4048 if (s->img->pixmap)
4049 {
4050 if (s->img->mask)
4051 {
4052 /* We can't set both a clip mask and use XSetClipRectangles
4053 because the latter also sets a clip mask. We also can't
4054 trust on the shape extension to be available
4055 (XShapeCombineRegion). So, compute the rectangle to draw
4056 manually. */
4057 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4058 | GCFunction);
4059 XGCValues xgcv;
4060 XRectangle clip_rect, image_rect, r;
4061
4062 xgcv.clip_mask = s->img->mask;
4063 xgcv.clip_x_origin = x;
4064 xgcv.clip_y_origin = y;
4065 xgcv.function = GXcopy;
4066 XChangeGC (s->display, s->gc, mask, &xgcv);
4067
4068 x_get_glyph_string_clip_rect (s, &clip_rect);
4069 image_rect.x = x;
4070 image_rect.y = y;
4071 image_rect.width = s->img->width;
4072 image_rect.height = s->img->height;
4073 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4074 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4075 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4076 }
4077 else
4078 {
4079 XRectangle clip_rect, image_rect, r;
4080
4081 x_get_glyph_string_clip_rect (s, &clip_rect);
4082 image_rect.x = x;
4083 image_rect.y = y;
4084 image_rect.width = s->img->width;
4085 image_rect.height = s->img->height;
4086 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4087 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4088 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4089
4090 /* When the image has a mask, we can expect that at
4091 least part of a mouse highlight or a block cursor will
4092 be visible. If the image doesn't have a mask, make
4093 a block cursor visible by drawing a rectangle around
4094 the image. I believe it's looking better if we do
4095 nothing here for mouse-face. */
4096 if (s->hl == DRAW_CURSOR)
4097 XDrawRectangle (s->display, s->window, s->gc, x, y,
4098 s->img->width - 1, s->img->height - 1);
4099 }
4100 }
4101 else
4102 /* Draw a rectangle if image could not be loaded. */
4103 XDrawRectangle (s->display, s->window, s->gc, x, y,
4104 s->img->width - 1, s->img->height - 1);
4105 }
4106
4107
4108 /* Draw a relief around the image glyph string S. */
4109
4110 static void
4111 x_draw_image_relief (s)
4112 struct glyph_string *s;
4113 {
4114 int x0, y0, x1, y1, thick, raised_p;
4115 XRectangle r;
4116 int x;
4117 int y = s->ybase - image_ascent (s->img, s->face);
4118
4119 /* If first glyph of S has a left box line, start drawing it to the
4120 right of that line. */
4121 if (s->face->box != FACE_NO_BOX
4122 && s->first_glyph->left_box_line_p)
4123 x = s->x + abs (s->face->box_line_width);
4124 else
4125 x = s->x;
4126
4127 /* If there is a margin around the image, adjust x- and y-position
4128 by that margin. */
4129 x += s->img->hmargin;
4130 y += s->img->vmargin;
4131
4132 if (s->hl == DRAW_IMAGE_SUNKEN
4133 || s->hl == DRAW_IMAGE_RAISED)
4134 {
4135 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
4136 raised_p = s->hl == DRAW_IMAGE_RAISED;
4137 }
4138 else
4139 {
4140 thick = abs (s->img->relief);
4141 raised_p = s->img->relief > 0;
4142 }
4143
4144 x0 = x - thick;
4145 y0 = y - thick;
4146 x1 = x + s->img->width + thick - 1;
4147 y1 = y + s->img->height + thick - 1;
4148
4149 x_setup_relief_colors (s);
4150 x_get_glyph_string_clip_rect (s, &r);
4151 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
4152 }
4153
4154
4155 /* Draw the foreground of image glyph string S to PIXMAP. */
4156
4157 static void
4158 x_draw_image_foreground_1 (s, pixmap)
4159 struct glyph_string *s;
4160 Pixmap pixmap;
4161 {
4162 int x;
4163 int y = s->ybase - s->y - image_ascent (s->img, s->face);
4164
4165 /* If first glyph of S has a left box line, start drawing it to the
4166 right of that line. */
4167 if (s->face->box != FACE_NO_BOX
4168 && s->first_glyph->left_box_line_p)
4169 x = abs (s->face->box_line_width);
4170 else
4171 x = 0;
4172
4173 /* If there is a margin around the image, adjust x- and y-position
4174 by that margin. */
4175 x += s->img->hmargin;
4176 y += s->img->vmargin;
4177
4178 if (s->img->pixmap)
4179 {
4180 if (s->img->mask)
4181 {
4182 /* We can't set both a clip mask and use XSetClipRectangles
4183 because the latter also sets a clip mask. We also can't
4184 trust on the shape extension to be available
4185 (XShapeCombineRegion). So, compute the rectangle to draw
4186 manually. */
4187 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4188 | GCFunction);
4189 XGCValues xgcv;
4190
4191 xgcv.clip_mask = s->img->mask;
4192 xgcv.clip_x_origin = x;
4193 xgcv.clip_y_origin = y;
4194 xgcv.function = GXcopy;
4195 XChangeGC (s->display, s->gc, mask, &xgcv);
4196
4197 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4198 0, 0, s->img->width, s->img->height, x, y);
4199 XSetClipMask (s->display, s->gc, None);
4200 }
4201 else
4202 {
4203 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4204 0, 0, s->img->width, s->img->height, x, y);
4205
4206 /* When the image has a mask, we can expect that at
4207 least part of a mouse highlight or a block cursor will
4208 be visible. If the image doesn't have a mask, make
4209 a block cursor visible by drawing a rectangle around
4210 the image. I believe it's looking better if we do
4211 nothing here for mouse-face. */
4212 if (s->hl == DRAW_CURSOR)
4213 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4214 s->img->width - 1, s->img->height - 1);
4215 }
4216 }
4217 else
4218 /* Draw a rectangle if image could not be loaded. */
4219 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4220 s->img->width - 1, s->img->height - 1);
4221 }
4222
4223
4224 /* Draw part of the background of glyph string S. X, Y, W, and H
4225 give the rectangle to draw. */
4226
4227 static void
4228 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4229 struct glyph_string *s;
4230 int x, y, w, h;
4231 {
4232 if (s->stippled_p)
4233 {
4234 /* Fill background with a stipple pattern. */
4235 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4236 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4237 XSetFillStyle (s->display, s->gc, FillSolid);
4238 }
4239 else
4240 x_clear_glyph_string_rect (s, x, y, w, h);
4241 }
4242
4243
4244 /* Draw image glyph string S.
4245
4246 s->y
4247 s->x +-------------------------
4248 | s->face->box
4249 |
4250 | +-------------------------
4251 | | s->img->margin
4252 | |
4253 | | +-------------------
4254 | | | the image
4255
4256 */
4257
4258 static void
4259 x_draw_image_glyph_string (s)
4260 struct glyph_string *s;
4261 {
4262 int x, y;
4263 int box_line_hwidth = abs (s->face->box_line_width);
4264 int box_line_vwidth = max (s->face->box_line_width, 0);
4265 int height;
4266 Pixmap pixmap = None;
4267
4268 height = s->height - 2 * box_line_vwidth;
4269
4270 /* Fill background with face under the image. Do it only if row is
4271 taller than image or if image has a clip mask to reduce
4272 flickering. */
4273 s->stippled_p = s->face->stipple != 0;
4274 if (height > s->img->height
4275 || s->img->hmargin
4276 || s->img->vmargin
4277 || s->img->mask
4278 || s->img->pixmap == 0
4279 || s->width != s->background_width)
4280 {
4281 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4282 x = s->x + box_line_hwidth;
4283 else
4284 x = s->x;
4285
4286 y = s->y + box_line_vwidth;
4287
4288 if (s->img->mask)
4289 {
4290 /* Create a pixmap as large as the glyph string. Fill it
4291 with the background color. Copy the image to it, using
4292 its mask. Copy the temporary pixmap to the display. */
4293 Screen *screen = FRAME_X_SCREEN (s->f);
4294 int depth = DefaultDepthOfScreen (screen);
4295
4296 /* Create a pixmap as large as the glyph string. */
4297 pixmap = XCreatePixmap (s->display, s->window,
4298 s->background_width,
4299 s->height, depth);
4300
4301 /* Don't clip in the following because we're working on the
4302 pixmap. */
4303 XSetClipMask (s->display, s->gc, None);
4304
4305 /* Fill the pixmap with the background color/stipple. */
4306 if (s->stippled_p)
4307 {
4308 /* Fill background with a stipple pattern. */
4309 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4310 XFillRectangle (s->display, pixmap, s->gc,
4311 0, 0, s->background_width, s->height);
4312 XSetFillStyle (s->display, s->gc, FillSolid);
4313 }
4314 else
4315 {
4316 XGCValues xgcv;
4317 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4318 &xgcv);
4319 XSetForeground (s->display, s->gc, xgcv.background);
4320 XFillRectangle (s->display, pixmap, s->gc,
4321 0, 0, s->background_width, s->height);
4322 XSetForeground (s->display, s->gc, xgcv.foreground);
4323 }
4324 }
4325 else
4326 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4327
4328 s->background_filled_p = 1;
4329 }
4330
4331 /* Draw the foreground. */
4332 if (pixmap != None)
4333 {
4334 x_draw_image_foreground_1 (s, pixmap);
4335 x_set_glyph_string_clipping (s);
4336 XCopyArea (s->display, pixmap, s->window, s->gc,
4337 0, 0, s->background_width, s->height, s->x, s->y);
4338 XFreePixmap (s->display, pixmap);
4339 }
4340 else
4341 x_draw_image_foreground (s);
4342
4343 /* If we must draw a relief around the image, do it. */
4344 if (s->img->relief
4345 || s->hl == DRAW_IMAGE_RAISED
4346 || s->hl == DRAW_IMAGE_SUNKEN)
4347 x_draw_image_relief (s);
4348 }
4349
4350
4351 /* Draw stretch glyph string S. */
4352
4353 static void
4354 x_draw_stretch_glyph_string (s)
4355 struct glyph_string *s;
4356 {
4357 xassert (s->first_glyph->type == STRETCH_GLYPH);
4358 s->stippled_p = s->face->stipple != 0;
4359
4360 if (s->hl == DRAW_CURSOR
4361 && !x_stretch_cursor_p)
4362 {
4363 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4364 as wide as the stretch glyph. */
4365 int width = min (CANON_X_UNIT (s->f), s->background_width);
4366
4367 /* Draw cursor. */
4368 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4369
4370 /* Clear rest using the GC of the original non-cursor face. */
4371 if (width < s->background_width)
4372 {
4373 int x = s->x + width, y = s->y;
4374 int w = s->background_width - width, h = s->height;
4375 XRectangle r;
4376 GC gc;
4377
4378 if (s->row->mouse_face_p
4379 && cursor_in_mouse_face_p (s->w))
4380 {
4381 x_set_mouse_face_gc (s);
4382 gc = s->gc;
4383 }
4384 else
4385 gc = s->face->gc;
4386
4387 x_get_glyph_string_clip_rect (s, &r);
4388 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4389
4390 if (s->face->stipple)
4391 {
4392 /* Fill background with a stipple pattern. */
4393 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4394 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4395 XSetFillStyle (s->display, gc, FillSolid);
4396 }
4397 else
4398 {
4399 XGCValues xgcv;
4400 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4401 XSetForeground (s->display, gc, xgcv.background);
4402 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4403 XSetForeground (s->display, gc, xgcv.foreground);
4404 }
4405 }
4406 }
4407 else if (!s->background_filled_p)
4408 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4409 s->height);
4410
4411 s->background_filled_p = 1;
4412 }
4413
4414
4415 /* Draw glyph string S. */
4416
4417 static void
4418 x_draw_glyph_string (s)
4419 struct glyph_string *s;
4420 {
4421 int relief_drawn_p = 0;
4422
4423 /* If S draws into the background of its successor, draw the
4424 background of the successor first so that S can draw into it.
4425 This makes S->next use XDrawString instead of XDrawImageString. */
4426 if (s->next && s->right_overhang && !s->for_overlaps_p)
4427 {
4428 xassert (s->next->img == NULL);
4429 x_set_glyph_string_gc (s->next);
4430 x_set_glyph_string_clipping (s->next);
4431 x_draw_glyph_string_background (s->next, 1);
4432 }
4433
4434 /* Set up S->gc, set clipping and draw S. */
4435 x_set_glyph_string_gc (s);
4436
4437 /* Draw relief (if any) in advance for char/composition so that the
4438 glyph string can be drawn over it. */
4439 if (!s->for_overlaps_p
4440 && s->face->box != FACE_NO_BOX
4441 && (s->first_glyph->type == CHAR_GLYPH
4442 || s->first_glyph->type == COMPOSITE_GLYPH))
4443
4444 {
4445 x_set_glyph_string_clipping (s);
4446 x_draw_glyph_string_background (s, 1);
4447 x_draw_glyph_string_box (s);
4448 x_set_glyph_string_clipping (s);
4449 relief_drawn_p = 1;
4450 }
4451 else
4452 x_set_glyph_string_clipping (s);
4453
4454 switch (s->first_glyph->type)
4455 {
4456 case IMAGE_GLYPH:
4457 x_draw_image_glyph_string (s);
4458 break;
4459
4460 case STRETCH_GLYPH:
4461 x_draw_stretch_glyph_string (s);
4462 break;
4463
4464 case CHAR_GLYPH:
4465 if (s->for_overlaps_p)
4466 s->background_filled_p = 1;
4467 else
4468 x_draw_glyph_string_background (s, 0);
4469 x_draw_glyph_string_foreground (s);
4470 break;
4471
4472 case COMPOSITE_GLYPH:
4473 if (s->for_overlaps_p || s->gidx > 0)
4474 s->background_filled_p = 1;
4475 else
4476 x_draw_glyph_string_background (s, 1);
4477 x_draw_composite_glyph_string_foreground (s);
4478 break;
4479
4480 default:
4481 abort ();
4482 }
4483
4484 if (!s->for_overlaps_p)
4485 {
4486 /* Draw underline. */
4487 if (s->face->underline_p)
4488 {
4489 unsigned long tem, h;
4490 int y;
4491
4492 /* Get the underline thickness. Default is 1 pixel. */
4493 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4494 h = 1;
4495
4496 /* Get the underline position. This is the recommended
4497 vertical offset in pixels from the baseline to the top of
4498 the underline. This is a signed value according to the
4499 specs, and its default is
4500
4501 ROUND ((maximum descent) / 2), with
4502 ROUND(x) = floor (x + 0.5) */
4503
4504 if (x_use_underline_position_properties
4505 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4506 y = s->ybase + (long) tem;
4507 else if (s->face->font)
4508 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4509 else
4510 y = s->y + s->height - h;
4511
4512 if (s->face->underline_defaulted_p)
4513 XFillRectangle (s->display, s->window, s->gc,
4514 s->x, y, s->width, h);
4515 else
4516 {
4517 XGCValues xgcv;
4518 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4519 XSetForeground (s->display, s->gc, s->face->underline_color);
4520 XFillRectangle (s->display, s->window, s->gc,
4521 s->x, y, s->width, h);
4522 XSetForeground (s->display, s->gc, xgcv.foreground);
4523 }
4524 }
4525
4526 /* Draw overline. */
4527 if (s->face->overline_p)
4528 {
4529 unsigned long dy = 0, h = 1;
4530
4531 if (s->face->overline_color_defaulted_p)
4532 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4533 s->width, h);
4534 else
4535 {
4536 XGCValues xgcv;
4537 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4538 XSetForeground (s->display, s->gc, s->face->overline_color);
4539 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4540 s->width, h);
4541 XSetForeground (s->display, s->gc, xgcv.foreground);
4542 }
4543 }
4544
4545 /* Draw strike-through. */
4546 if (s->face->strike_through_p)
4547 {
4548 unsigned long h = 1;
4549 unsigned long dy = (s->height - h) / 2;
4550
4551 if (s->face->strike_through_color_defaulted_p)
4552 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4553 s->width, h);
4554 else
4555 {
4556 XGCValues xgcv;
4557 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4558 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4559 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4560 s->width, h);
4561 XSetForeground (s->display, s->gc, xgcv.foreground);
4562 }
4563 }
4564
4565 /* Draw relief if not yet drawn. */
4566 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
4567 x_draw_glyph_string_box (s);
4568 }
4569
4570 /* Reset clipping. */
4571 XSetClipMask (s->display, s->gc, None);
4572 }
4573
4574
4575 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4576 struct face **, int));
4577
4578
4579 /* Fill glyph string S with composition components specified by S->cmp.
4580
4581 FACES is an array of faces for all components of this composition.
4582 S->gidx is the index of the first component for S.
4583 OVERLAPS_P non-zero means S should draw the foreground only, and
4584 use its physical height for clipping.
4585
4586 Value is the index of a component not in S. */
4587
4588 static int
4589 x_fill_composite_glyph_string (s, faces, overlaps_p)
4590 struct glyph_string *s;
4591 struct face **faces;
4592 int overlaps_p;
4593 {
4594 int i;
4595
4596 xassert (s);
4597
4598 s->for_overlaps_p = overlaps_p;
4599
4600 s->face = faces[s->gidx];
4601 s->font = s->face->font;
4602 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4603
4604 /* For all glyphs of this composition, starting at the offset
4605 S->gidx, until we reach the end of the definition or encounter a
4606 glyph that requires the different face, add it to S. */
4607 ++s->nchars;
4608 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4609 ++s->nchars;
4610
4611 /* All glyph strings for the same composition has the same width,
4612 i.e. the width set for the first component of the composition. */
4613
4614 s->width = s->first_glyph->pixel_width;
4615
4616 /* If the specified font could not be loaded, use the frame's
4617 default font, but record the fact that we couldn't load it in
4618 the glyph string so that we can draw rectangles for the
4619 characters of the glyph string. */
4620 if (s->font == NULL)
4621 {
4622 s->font_not_found_p = 1;
4623 s->font = FRAME_FONT (s->f);
4624 }
4625
4626 /* Adjust base line for subscript/superscript text. */
4627 s->ybase += s->first_glyph->voffset;
4628
4629 xassert (s->face && s->face->gc);
4630
4631 /* This glyph string must always be drawn with 16-bit functions. */
4632 s->two_byte_p = 1;
4633
4634 return s->gidx + s->nchars;
4635 }
4636
4637
4638 /* Fill glyph string S from a sequence of character glyphs.
4639
4640 FACE_ID is the face id of the string. START is the index of the
4641 first glyph to consider, END is the index of the last + 1.
4642 OVERLAPS_P non-zero means S should draw the foreground only, and
4643 use its physical height for clipping.
4644
4645 Value is the index of the first glyph not in S. */
4646
4647 static int
4648 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4649 struct glyph_string *s;
4650 int face_id;
4651 int start, end, overlaps_p;
4652 {
4653 struct glyph *glyph, *last;
4654 int voffset;
4655 int glyph_not_available_p;
4656
4657 xassert (s->f == XFRAME (s->w->frame));
4658 xassert (s->nchars == 0);
4659 xassert (start >= 0 && end > start);
4660
4661 s->for_overlaps_p = overlaps_p,
4662 glyph = s->row->glyphs[s->area] + start;
4663 last = s->row->glyphs[s->area] + end;
4664 voffset = glyph->voffset;
4665
4666 glyph_not_available_p = glyph->glyph_not_available_p;
4667
4668 while (glyph < last
4669 && glyph->type == CHAR_GLYPH
4670 && glyph->voffset == voffset
4671 /* Same face id implies same font, nowadays. */
4672 && glyph->face_id == face_id
4673 && glyph->glyph_not_available_p == glyph_not_available_p)
4674 {
4675 int two_byte_p;
4676
4677 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4678 s->char2b + s->nchars,
4679 &two_byte_p);
4680 s->two_byte_p = two_byte_p;
4681 ++s->nchars;
4682 xassert (s->nchars <= end - start);
4683 s->width += glyph->pixel_width;
4684 ++glyph;
4685 }
4686
4687 s->font = s->face->font;
4688 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4689
4690 /* If the specified font could not be loaded, use the frame's font,
4691 but record the fact that we couldn't load it in
4692 S->font_not_found_p so that we can draw rectangles for the
4693 characters of the glyph string. */
4694 if (s->font == NULL || glyph_not_available_p)
4695 {
4696 s->font_not_found_p = 1;
4697 s->font = FRAME_FONT (s->f);
4698 }
4699
4700 /* Adjust base line for subscript/superscript text. */
4701 s->ybase += voffset;
4702
4703 xassert (s->face && s->face->gc);
4704 return glyph - s->row->glyphs[s->area];
4705 }
4706
4707
4708 /* Fill glyph string S from image glyph S->first_glyph. */
4709
4710 static void
4711 x_fill_image_glyph_string (s)
4712 struct glyph_string *s;
4713 {
4714 xassert (s->first_glyph->type == IMAGE_GLYPH);
4715 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4716 xassert (s->img);
4717 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4718 s->font = s->face->font;
4719 s->width = s->first_glyph->pixel_width;
4720
4721 /* Adjust base line for subscript/superscript text. */
4722 s->ybase += s->first_glyph->voffset;
4723 }
4724
4725
4726 /* Fill glyph string S from a sequence of stretch glyphs.
4727
4728 ROW is the glyph row in which the glyphs are found, AREA is the
4729 area within the row. START is the index of the first glyph to
4730 consider, END is the index of the last + 1.
4731
4732 Value is the index of the first glyph not in S. */
4733
4734 static int
4735 x_fill_stretch_glyph_string (s, row, area, start, end)
4736 struct glyph_string *s;
4737 struct glyph_row *row;
4738 enum glyph_row_area area;
4739 int start, end;
4740 {
4741 struct glyph *glyph, *last;
4742 int voffset, face_id;
4743
4744 xassert (s->first_glyph->type == STRETCH_GLYPH);
4745
4746 glyph = s->row->glyphs[s->area] + start;
4747 last = s->row->glyphs[s->area] + end;
4748 face_id = glyph->face_id;
4749 s->face = FACE_FROM_ID (s->f, face_id);
4750 s->font = s->face->font;
4751 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4752 s->width = glyph->pixel_width;
4753 voffset = glyph->voffset;
4754
4755 for (++glyph;
4756 (glyph < last
4757 && glyph->type == STRETCH_GLYPH
4758 && glyph->voffset == voffset
4759 && glyph->face_id == face_id);
4760 ++glyph)
4761 s->width += glyph->pixel_width;
4762
4763 /* Adjust base line for subscript/superscript text. */
4764 s->ybase += voffset;
4765
4766 /* The case that face->gc == 0 is handled when drawing the glyph
4767 string by calling PREPARE_FACE_FOR_DISPLAY. */
4768 xassert (s->face);
4769 return glyph - s->row->glyphs[s->area];
4770 }
4771
4772
4773 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4774 of XChar2b structures for S; it can't be allocated in
4775 x_init_glyph_string because it must be allocated via `alloca'. W
4776 is the window on which S is drawn. ROW and AREA are the glyph row
4777 and area within the row from which S is constructed. START is the
4778 index of the first glyph structure covered by S. HL is a
4779 face-override for drawing S. */
4780
4781 static void
4782 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4783 struct glyph_string *s;
4784 XChar2b *char2b;
4785 struct window *w;
4786 struct glyph_row *row;
4787 enum glyph_row_area area;
4788 int start;
4789 enum draw_glyphs_face hl;
4790 {
4791 bzero (s, sizeof *s);
4792 s->w = w;
4793 s->f = XFRAME (w->frame);
4794 s->display = FRAME_X_DISPLAY (s->f);
4795 s->window = FRAME_X_WINDOW (s->f);
4796 s->char2b = char2b;
4797 s->hl = hl;
4798 s->row = row;
4799 s->area = area;
4800 s->first_glyph = row->glyphs[area] + start;
4801 s->height = row->height;
4802 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4803
4804 /* Display the internal border below the tool-bar window. */
4805 if (s->w == XWINDOW (s->f->tool_bar_window))
4806 s->y -= s->f->output_data.x->internal_border_width;
4807
4808 s->ybase = s->y + row->ascent;
4809 }
4810
4811
4812 /* Set background width of glyph string S. START is the index of the
4813 first glyph following S. LAST_X is the right-most x-position + 1
4814 in the drawing area. */
4815
4816 static INLINE void
4817 x_set_glyph_string_background_width (s, start, last_x)
4818 struct glyph_string *s;
4819 int start;
4820 int last_x;
4821 {
4822 /* If the face of this glyph string has to be drawn to the end of
4823 the drawing area, set S->extends_to_end_of_line_p. */
4824 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4825
4826 if (start == s->row->used[s->area]
4827 && s->area == TEXT_AREA
4828 && ((s->hl == DRAW_NORMAL_TEXT
4829 && (s->row->fill_line_p
4830 || s->face->background != default_face->background
4831 || s->face->stipple != default_face->stipple
4832 || s->row->mouse_face_p))
4833 || s->hl == DRAW_MOUSE_FACE
4834 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
4835 && s->row->fill_line_p)))
4836 s->extends_to_end_of_line_p = 1;
4837
4838 /* If S extends its face to the end of the line, set its
4839 background_width to the distance to the right edge of the drawing
4840 area. */
4841 if (s->extends_to_end_of_line_p)
4842 s->background_width = last_x - s->x + 1;
4843 else
4844 s->background_width = s->width;
4845 }
4846
4847
4848 /* Add a glyph string for a stretch glyph to the list of strings
4849 between HEAD and TAIL. START is the index of the stretch glyph in
4850 row area AREA of glyph row ROW. END is the index of the last glyph
4851 in that glyph row area. X is the current output position assigned
4852 to the new glyph string constructed. HL overrides that face of the
4853 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4854 is the right-most x-position of the drawing area. */
4855
4856 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4857 and below -- keep them on one line. */
4858 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4859 do \
4860 { \
4861 s = (struct glyph_string *) alloca (sizeof *s); \
4862 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4863 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4864 x_append_glyph_string (&HEAD, &TAIL, s); \
4865 s->x = (X); \
4866 } \
4867 while (0)
4868
4869
4870 /* Add a glyph string for an image glyph to the list of strings
4871 between HEAD and TAIL. START is the index of the image glyph in
4872 row area AREA of glyph row ROW. END is the index of the last glyph
4873 in that glyph row area. X is the current output position assigned
4874 to the new glyph string constructed. HL overrides that face of the
4875 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4876 is the right-most x-position of the drawing area. */
4877
4878 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4879 do \
4880 { \
4881 s = (struct glyph_string *) alloca (sizeof *s); \
4882 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4883 x_fill_image_glyph_string (s); \
4884 x_append_glyph_string (&HEAD, &TAIL, s); \
4885 ++START; \
4886 s->x = (X); \
4887 } \
4888 while (0)
4889
4890
4891 /* Add a glyph string for a sequence of character glyphs to the list
4892 of strings between HEAD and TAIL. START is the index of the first
4893 glyph in row area AREA of glyph row ROW that is part of the new
4894 glyph string. END is the index of the last glyph in that glyph row
4895 area. X is the current output position assigned to the new glyph
4896 string constructed. HL overrides that face of the glyph; e.g. it
4897 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4898 right-most x-position of the drawing area. */
4899
4900 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4901 do \
4902 { \
4903 int c, face_id; \
4904 XChar2b *char2b; \
4905 \
4906 c = (ROW)->glyphs[AREA][START].u.ch; \
4907 face_id = (ROW)->glyphs[AREA][START].face_id; \
4908 \
4909 s = (struct glyph_string *) alloca (sizeof *s); \
4910 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4911 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4912 x_append_glyph_string (&HEAD, &TAIL, s); \
4913 s->x = (X); \
4914 START = x_fill_glyph_string (s, face_id, START, END, \
4915 OVERLAPS_P); \
4916 } \
4917 while (0)
4918
4919
4920 /* Add a glyph string for a composite sequence to the list of strings
4921 between HEAD and TAIL. START is the index of the first glyph in
4922 row area AREA of glyph row ROW that is part of the new glyph
4923 string. END is the index of the last glyph in that glyph row area.
4924 X is the current output position assigned to the new glyph string
4925 constructed. HL overrides that face of the glyph; e.g. it is
4926 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4927 x-position of the drawing area. */
4928
4929 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4930 do { \
4931 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4932 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4933 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4934 struct composition *cmp = composition_table[cmp_id]; \
4935 int glyph_len = cmp->glyph_len; \
4936 XChar2b *char2b; \
4937 struct face **faces; \
4938 struct glyph_string *first_s = NULL; \
4939 int n; \
4940 \
4941 base_face = base_face->ascii_face; \
4942 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4943 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4944 /* At first, fill in `char2b' and `faces'. */ \
4945 for (n = 0; n < glyph_len; n++) \
4946 { \
4947 int c = COMPOSITION_GLYPH (cmp, n); \
4948 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4949 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4950 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4951 this_face_id, char2b + n, 1); \
4952 } \
4953 \
4954 /* Make glyph_strings for each glyph sequence that is drawable by \
4955 the same face, and append them to HEAD/TAIL. */ \
4956 for (n = 0; n < cmp->glyph_len;) \
4957 { \
4958 s = (struct glyph_string *) alloca (sizeof *s); \
4959 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4960 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4961 s->cmp = cmp; \
4962 s->gidx = n; \
4963 s->x = (X); \
4964 \
4965 if (n == 0) \
4966 first_s = s; \
4967 \
4968 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4969 } \
4970 \
4971 ++START; \
4972 s = first_s; \
4973 } while (0)
4974
4975
4976 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4977 of AREA of glyph row ROW on window W between indices START and END.
4978 HL overrides the face for drawing glyph strings, e.g. it is
4979 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4980 x-positions of the drawing area.
4981
4982 This is an ugly monster macro construct because we must use alloca
4983 to allocate glyph strings (because x_draw_glyphs can be called
4984 asynchronously). */
4985
4986 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4987 do \
4988 { \
4989 HEAD = TAIL = NULL; \
4990 while (START < END) \
4991 { \
4992 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4993 switch (first_glyph->type) \
4994 { \
4995 case CHAR_GLYPH: \
4996 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
4997 TAIL, HL, X, LAST_X, \
4998 OVERLAPS_P); \
4999 break; \
5000 \
5001 case COMPOSITE_GLYPH: \
5002 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
5003 HEAD, TAIL, HL, X, LAST_X,\
5004 OVERLAPS_P); \
5005 break; \
5006 \
5007 case STRETCH_GLYPH: \
5008 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
5009 HEAD, TAIL, HL, X, LAST_X); \
5010 break; \
5011 \
5012 case IMAGE_GLYPH: \
5013 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
5014 TAIL, HL, X, LAST_X); \
5015 break; \
5016 \
5017 default: \
5018 abort (); \
5019 } \
5020 \
5021 x_set_glyph_string_background_width (s, START, LAST_X); \
5022 (X) += s->width; \
5023 } \
5024 } \
5025 while (0)
5026
5027
5028 /* Draw glyphs between START and END in AREA of ROW on window W,
5029 starting at x-position X. X is relative to AREA in W. HL is a
5030 face-override with the following meaning:
5031
5032 DRAW_NORMAL_TEXT draw normally
5033 DRAW_CURSOR draw in cursor face
5034 DRAW_MOUSE_FACE draw in mouse face.
5035 DRAW_INVERSE_VIDEO draw in mode line face
5036 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
5037 DRAW_IMAGE_RAISED draw an image with a raised relief around it
5038
5039 If OVERLAPS_P is non-zero, draw only the foreground of characters
5040 and clip to the physical height of ROW.
5041
5042 Value is the x-position reached, relative to AREA of W. */
5043
5044 static int
5045 x_draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
5046 struct window *w;
5047 int x;
5048 struct glyph_row *row;
5049 enum glyph_row_area area;
5050 int start, end;
5051 enum draw_glyphs_face hl;
5052 int overlaps_p;
5053 {
5054 struct glyph_string *head, *tail;
5055 struct glyph_string *s;
5056 int last_x, area_width;
5057 int x_reached;
5058 int i, j;
5059
5060 /* Let's rather be paranoid than getting a SEGV. */
5061 end = min (end, row->used[area]);
5062 start = max (0, start);
5063 start = min (end, start);
5064
5065 /* Translate X to frame coordinates. Set last_x to the right
5066 end of the drawing area. */
5067 if (row->full_width_p)
5068 {
5069 /* X is relative to the left edge of W, without scroll bars
5070 or fringes. */
5071 struct frame *f = XFRAME (w->frame);
5072 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
5073
5074 x += window_left_x;
5075 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
5076 last_x = window_left_x + area_width;
5077
5078 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5079 {
5080 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5081 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
5082 last_x += width;
5083 else
5084 x -= width;
5085 }
5086
5087 x += FRAME_INTERNAL_BORDER_WIDTH (f);
5088 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
5089 }
5090 else
5091 {
5092 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
5093 area_width = window_box_width (w, area);
5094 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
5095 }
5096
5097 /* Build a doubly-linked list of glyph_string structures between
5098 head and tail from what we have to draw. Note that the macro
5099 BUILD_GLYPH_STRINGS will modify its start parameter. That's
5100 the reason we use a separate variable `i'. */
5101 i = start;
5102 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
5103 overlaps_p);
5104 if (tail)
5105 x_reached = tail->x + tail->background_width;
5106 else
5107 x_reached = x;
5108
5109 /* If there are any glyphs with lbearing < 0 or rbearing > width in
5110 the row, redraw some glyphs in front or following the glyph
5111 strings built above. */
5112 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
5113 {
5114 int dummy_x = 0;
5115 struct glyph_string *h, *t;
5116
5117 /* Compute overhangs for all glyph strings. */
5118 for (s = head; s; s = s->next)
5119 x_compute_glyph_string_overhangs (s);
5120
5121 /* Prepend glyph strings for glyphs in front of the first glyph
5122 string that are overwritten because of the first glyph
5123 string's left overhang. The background of all strings
5124 prepended must be drawn because the first glyph string
5125 draws over it. */
5126 i = x_left_overwritten (head);
5127 if (i >= 0)
5128 {
5129 j = i;
5130 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
5131 DRAW_NORMAL_TEXT, dummy_x, last_x,
5132 overlaps_p);
5133 start = i;
5134 x_compute_overhangs_and_x (t, head->x, 1);
5135 x_prepend_glyph_string_lists (&head, &tail, h, t);
5136 }
5137
5138 /* Prepend glyph strings for glyphs in front of the first glyph
5139 string that overwrite that glyph string because of their
5140 right overhang. For these strings, only the foreground must
5141 be drawn, because it draws over the glyph string at `head'.
5142 The background must not be drawn because this would overwrite
5143 right overhangs of preceding glyphs for which no glyph
5144 strings exist. */
5145 i = x_left_overwriting (head);
5146 if (i >= 0)
5147 {
5148 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
5149 DRAW_NORMAL_TEXT, dummy_x, last_x,
5150 overlaps_p);
5151 for (s = h; s; s = s->next)
5152 s->background_filled_p = 1;
5153 x_compute_overhangs_and_x (t, head->x, 1);
5154 x_prepend_glyph_string_lists (&head, &tail, h, t);
5155 }
5156
5157 /* Append glyphs strings for glyphs following the last glyph
5158 string tail that are overwritten by tail. The background of
5159 these strings has to be drawn because tail's foreground draws
5160 over it. */
5161 i = x_right_overwritten (tail);
5162 if (i >= 0)
5163 {
5164 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5165 DRAW_NORMAL_TEXT, x, last_x,
5166 overlaps_p);
5167 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5168 x_append_glyph_string_lists (&head, &tail, h, t);
5169 }
5170
5171 /* Append glyph strings for glyphs following the last glyph
5172 string tail that overwrite tail. The foreground of such
5173 glyphs has to be drawn because it writes into the background
5174 of tail. The background must not be drawn because it could
5175 paint over the foreground of following glyphs. */
5176 i = x_right_overwriting (tail);
5177 if (i >= 0)
5178 {
5179 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5180 DRAW_NORMAL_TEXT, x, last_x,
5181 overlaps_p);
5182 for (s = h; s; s = s->next)
5183 s->background_filled_p = 1;
5184 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5185 x_append_glyph_string_lists (&head, &tail, h, t);
5186 }
5187 }
5188
5189 /* Draw all strings. */
5190 for (s = head; s; s = s->next)
5191 x_draw_glyph_string (s);
5192
5193 if (area == TEXT_AREA
5194 && !row->full_width_p
5195 /* When drawing overlapping rows, only the glyph strings'
5196 foreground is drawn, which doesn't erase a cursor
5197 completely. */
5198 && !overlaps_p)
5199 {
5200 int x0 = head ? head->x : x;
5201 int x1 = tail ? tail->x + tail->background_width : x;
5202
5203 x0 = FRAME_TO_WINDOW_PIXEL_X (w, x0);
5204 x1 = FRAME_TO_WINDOW_PIXEL_X (w, x1);
5205
5206 if (XFASTINT (w->left_margin_width) != 0)
5207 {
5208 int left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
5209 x0 -= left_area_width;
5210 x1 -= left_area_width;
5211 }
5212
5213 notice_overwritten_cursor (w, area, x0, x1,
5214 row->y, MATRIX_ROW_BOTTOM_Y (row));
5215 }
5216
5217 /* Value is the x-position up to which drawn, relative to AREA of W.
5218 This doesn't include parts drawn because of overhangs. */
5219 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5220 if (!row->full_width_p)
5221 {
5222 if (area > LEFT_MARGIN_AREA && XFASTINT (w->left_margin_width) != 0)
5223 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5224 if (area > TEXT_AREA)
5225 x_reached -= window_box_width (w, TEXT_AREA);
5226 }
5227
5228 return x_reached;
5229 }
5230
5231
5232 /* Fix the display of area AREA of overlapping row ROW in window W. */
5233
5234 static void
5235 x_fix_overlapping_area (w, row, area)
5236 struct window *w;
5237 struct glyph_row *row;
5238 enum glyph_row_area area;
5239 {
5240 int i, x;
5241
5242 BLOCK_INPUT;
5243
5244 if (area == LEFT_MARGIN_AREA)
5245 x = 0;
5246 else if (area == TEXT_AREA)
5247 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5248 else
5249 x = (window_box_width (w, LEFT_MARGIN_AREA)
5250 + window_box_width (w, TEXT_AREA));
5251
5252 for (i = 0; i < row->used[area];)
5253 {
5254 if (row->glyphs[area][i].overlaps_vertically_p)
5255 {
5256 int start = i, start_x = x;
5257
5258 do
5259 {
5260 x += row->glyphs[area][i].pixel_width;
5261 ++i;
5262 }
5263 while (i < row->used[area]
5264 && row->glyphs[area][i].overlaps_vertically_p);
5265
5266 x_draw_glyphs (w, start_x, row, area, start, i,
5267 DRAW_NORMAL_TEXT, 1);
5268 }
5269 else
5270 {
5271 x += row->glyphs[area][i].pixel_width;
5272 ++i;
5273 }
5274 }
5275
5276 UNBLOCK_INPUT;
5277 }
5278
5279
5280 /* Output LEN glyphs starting at START at the nominal cursor position.
5281 Advance the nominal cursor over the text. The global variable
5282 updated_window contains the window being updated, updated_row is
5283 the glyph row being updated, and updated_area is the area of that
5284 row being updated. */
5285
5286 static void
5287 x_write_glyphs (start, len)
5288 struct glyph *start;
5289 int len;
5290 {
5291 int x, hpos;
5292
5293 xassert (updated_window && updated_row);
5294 BLOCK_INPUT;
5295
5296 /* Write glyphs. */
5297
5298 hpos = start - updated_row->glyphs[updated_area];
5299 x = x_draw_glyphs (updated_window, output_cursor.x,
5300 updated_row, updated_area,
5301 hpos, hpos + len,
5302 DRAW_NORMAL_TEXT, 0);
5303
5304 UNBLOCK_INPUT;
5305
5306 /* Advance the output cursor. */
5307 output_cursor.hpos += len;
5308 output_cursor.x = x;
5309 }
5310
5311
5312 /* Insert LEN glyphs from START at the nominal cursor position. */
5313
5314 static void
5315 x_insert_glyphs (start, len)
5316 struct glyph *start;
5317 register int len;
5318 {
5319 struct frame *f;
5320 struct window *w;
5321 int line_height, shift_by_width, shifted_region_width;
5322 struct glyph_row *row;
5323 struct glyph *glyph;
5324 int frame_x, frame_y, hpos;
5325
5326 xassert (updated_window && updated_row);
5327 BLOCK_INPUT;
5328 w = updated_window;
5329 f = XFRAME (WINDOW_FRAME (w));
5330
5331 /* Get the height of the line we are in. */
5332 row = updated_row;
5333 line_height = row->height;
5334
5335 /* Get the width of the glyphs to insert. */
5336 shift_by_width = 0;
5337 for (glyph = start; glyph < start + len; ++glyph)
5338 shift_by_width += glyph->pixel_width;
5339
5340 /* Get the width of the region to shift right. */
5341 shifted_region_width = (window_box_width (w, updated_area)
5342 - output_cursor.x
5343 - shift_by_width);
5344
5345 /* Shift right. */
5346 frame_x = window_box_left (w, updated_area) + output_cursor.x;
5347 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5348 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5349 f->output_data.x->normal_gc,
5350 frame_x, frame_y,
5351 shifted_region_width, line_height,
5352 frame_x + shift_by_width, frame_y);
5353
5354 /* Write the glyphs. */
5355 hpos = start - row->glyphs[updated_area];
5356 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5357 DRAW_NORMAL_TEXT, 0);
5358
5359 /* Advance the output cursor. */
5360 output_cursor.hpos += len;
5361 output_cursor.x += shift_by_width;
5362 UNBLOCK_INPUT;
5363 }
5364
5365
5366 /* Delete N glyphs at the nominal cursor position. Not implemented
5367 for X frames. */
5368
5369 static void
5370 x_delete_glyphs (n)
5371 register int n;
5372 {
5373 abort ();
5374 }
5375
5376
5377 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5378 If they are <= 0, this is probably an error. */
5379
5380 void
5381 x_clear_area (dpy, window, x, y, width, height, exposures)
5382 Display *dpy;
5383 Window window;
5384 int x, y;
5385 int width, height;
5386 int exposures;
5387 {
5388 xassert (width > 0 && height > 0);
5389 XClearArea (dpy, window, x, y, width, height, exposures);
5390 }
5391
5392
5393 /* Erase the current text line from the nominal cursor position
5394 (inclusive) to pixel column TO_X (exclusive). The idea is that
5395 everything from TO_X onward is already erased.
5396
5397 TO_X is a pixel position relative to updated_area of
5398 updated_window. TO_X == -1 means clear to the end of this area. */
5399
5400 static void
5401 x_clear_end_of_line (to_x)
5402 int to_x;
5403 {
5404 struct frame *f;
5405 struct window *w = updated_window;
5406 int max_x, min_y, max_y;
5407 int from_x, from_y, to_y;
5408
5409 xassert (updated_window && updated_row);
5410 f = XFRAME (w->frame);
5411
5412 if (updated_row->full_width_p)
5413 {
5414 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5415 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5416 && !w->pseudo_window_p)
5417 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5418 }
5419 else
5420 max_x = window_box_width (w, updated_area);
5421 max_y = window_text_bottom_y (w);
5422
5423 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5424 of window. For TO_X > 0, truncate to end of drawing area. */
5425 if (to_x == 0)
5426 return;
5427 else if (to_x < 0)
5428 to_x = max_x;
5429 else
5430 to_x = min (to_x, max_x);
5431
5432 to_y = min (max_y, output_cursor.y + updated_row->height);
5433
5434 /* Notice if the cursor will be cleared by this operation. */
5435 if (!updated_row->full_width_p)
5436 notice_overwritten_cursor (w, updated_area,
5437 output_cursor.x, -1,
5438 updated_row->y,
5439 MATRIX_ROW_BOTTOM_Y (updated_row));
5440
5441 from_x = output_cursor.x;
5442
5443 /* Translate to frame coordinates. */
5444 if (updated_row->full_width_p)
5445 {
5446 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5447 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5448 }
5449 else
5450 {
5451 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5452 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5453 }
5454
5455 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5456 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5457 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5458
5459 /* Prevent inadvertently clearing to end of the X window. */
5460 if (to_x > from_x && to_y > from_y)
5461 {
5462 BLOCK_INPUT;
5463 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5464 from_x, from_y, to_x - from_x, to_y - from_y,
5465 False);
5466 UNBLOCK_INPUT;
5467 }
5468 }
5469
5470
5471 /* Clear entire frame. If updating_frame is non-null, clear that
5472 frame. Otherwise clear the selected frame. */
5473
5474 static void
5475 x_clear_frame ()
5476 {
5477 struct frame *f;
5478
5479 if (updating_frame)
5480 f = updating_frame;
5481 else
5482 f = SELECTED_FRAME ();
5483
5484 /* Clearing the frame will erase any cursor, so mark them all as no
5485 longer visible. */
5486 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5487 output_cursor.hpos = output_cursor.vpos = 0;
5488 output_cursor.x = -1;
5489
5490 /* We don't set the output cursor here because there will always
5491 follow an explicit cursor_to. */
5492 BLOCK_INPUT;
5493 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5494
5495 /* We have to clear the scroll bars, too. If we have changed
5496 colors or something like that, then they should be notified. */
5497 x_scroll_bar_clear (f);
5498
5499 XFlush (FRAME_X_DISPLAY (f));
5500 UNBLOCK_INPUT;
5501 }
5502
5503
5504 \f
5505 /* Invert the middle quarter of the frame for .15 sec. */
5506
5507 /* We use the select system call to do the waiting, so we have to make
5508 sure it's available. If it isn't, we just won't do visual bells. */
5509
5510 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5511
5512
5513 /* Subtract the `struct timeval' values X and Y, storing the result in
5514 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5515
5516 static int
5517 timeval_subtract (result, x, y)
5518 struct timeval *result, x, y;
5519 {
5520 /* Perform the carry for the later subtraction by updating y. This
5521 is safer because on some systems the tv_sec member is unsigned. */
5522 if (x.tv_usec < y.tv_usec)
5523 {
5524 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5525 y.tv_usec -= 1000000 * nsec;
5526 y.tv_sec += nsec;
5527 }
5528
5529 if (x.tv_usec - y.tv_usec > 1000000)
5530 {
5531 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5532 y.tv_usec += 1000000 * nsec;
5533 y.tv_sec -= nsec;
5534 }
5535
5536 /* Compute the time remaining to wait. tv_usec is certainly
5537 positive. */
5538 result->tv_sec = x.tv_sec - y.tv_sec;
5539 result->tv_usec = x.tv_usec - y.tv_usec;
5540
5541 /* Return indication of whether the result should be considered
5542 negative. */
5543 return x.tv_sec < y.tv_sec;
5544 }
5545
5546 void
5547 XTflash (f)
5548 struct frame *f;
5549 {
5550 BLOCK_INPUT;
5551
5552 {
5553 GC gc;
5554
5555 /* Create a GC that will use the GXxor function to flip foreground
5556 pixels into background pixels. */
5557 {
5558 XGCValues values;
5559
5560 values.function = GXxor;
5561 values.foreground = (f->output_data.x->foreground_pixel
5562 ^ f->output_data.x->background_pixel);
5563
5564 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5565 GCFunction | GCForeground, &values);
5566 }
5567
5568 {
5569 /* Get the height not including a menu bar widget. */
5570 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5571 /* Height of each line to flash. */
5572 int flash_height = FRAME_LINE_HEIGHT (f);
5573 /* These will be the left and right margins of the rectangles. */
5574 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5575 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5576
5577 int width;
5578
5579 /* Don't flash the area between a scroll bar and the frame
5580 edge it is next to. */
5581 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5582 {
5583 case vertical_scroll_bar_left:
5584 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5585 break;
5586
5587 case vertical_scroll_bar_right:
5588 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5589 break;
5590
5591 default:
5592 break;
5593 }
5594
5595 width = flash_right - flash_left;
5596
5597 /* If window is tall, flash top and bottom line. */
5598 if (height > 3 * FRAME_LINE_HEIGHT (f))
5599 {
5600 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5601 flash_left,
5602 (FRAME_INTERNAL_BORDER_WIDTH (f)
5603 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5604 width, flash_height);
5605 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5606 flash_left,
5607 (height - flash_height
5608 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5609 width, flash_height);
5610 }
5611 else
5612 /* If it is short, flash it all. */
5613 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5614 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5615 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5616
5617 x_flush (f);
5618
5619 {
5620 struct timeval wakeup;
5621
5622 EMACS_GET_TIME (wakeup);
5623
5624 /* Compute time to wait until, propagating carry from usecs. */
5625 wakeup.tv_usec += 150000;
5626 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5627 wakeup.tv_usec %= 1000000;
5628
5629 /* Keep waiting until past the time wakeup or any input gets
5630 available. */
5631 while (! detect_input_pending ())
5632 {
5633 struct timeval current;
5634 struct timeval timeout;
5635
5636 EMACS_GET_TIME (current);
5637
5638 /* Break if result would be negative. */
5639 if (timeval_subtract (&current, wakeup, current))
5640 break;
5641
5642 /* How long `select' should wait. */
5643 timeout.tv_sec = 0;
5644 timeout.tv_usec = 10000;
5645
5646 /* Try to wait that long--but we might wake up sooner. */
5647 select (0, NULL, NULL, NULL, &timeout);
5648 }
5649 }
5650
5651 /* If window is tall, flash top and bottom line. */
5652 if (height > 3 * FRAME_LINE_HEIGHT (f))
5653 {
5654 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5655 flash_left,
5656 (FRAME_INTERNAL_BORDER_WIDTH (f)
5657 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5658 width, flash_height);
5659 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5660 flash_left,
5661 (height - flash_height
5662 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5663 width, flash_height);
5664 }
5665 else
5666 /* If it is short, flash it all. */
5667 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5668 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5669 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5670
5671 XFreeGC (FRAME_X_DISPLAY (f), gc);
5672 x_flush (f);
5673 }
5674 }
5675
5676 UNBLOCK_INPUT;
5677 }
5678
5679 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5680
5681
5682 /* Make audible bell. */
5683
5684 void
5685 XTring_bell ()
5686 {
5687 struct frame *f = SELECTED_FRAME ();
5688
5689 if (FRAME_X_DISPLAY (f))
5690 {
5691 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5692 if (visible_bell)
5693 XTflash (f);
5694 else
5695 #endif
5696 {
5697 BLOCK_INPUT;
5698 XBell (FRAME_X_DISPLAY (f), 0);
5699 XFlush (FRAME_X_DISPLAY (f));
5700 UNBLOCK_INPUT;
5701 }
5702 }
5703 }
5704
5705 \f
5706 /* Specify how many text lines, from the top of the window,
5707 should be affected by insert-lines and delete-lines operations.
5708 This, and those operations, are used only within an update
5709 that is bounded by calls to x_update_begin and x_update_end. */
5710
5711 static void
5712 XTset_terminal_window (n)
5713 register int n;
5714 {
5715 /* This function intentionally left blank. */
5716 }
5717
5718
5719 \f
5720 /***********************************************************************
5721 Line Dance
5722 ***********************************************************************/
5723
5724 /* Perform an insert-lines or delete-lines operation, inserting N
5725 lines or deleting -N lines at vertical position VPOS. */
5726
5727 static void
5728 x_ins_del_lines (vpos, n)
5729 int vpos, n;
5730 {
5731 abort ();
5732 }
5733
5734
5735 /* Scroll part of the display as described by RUN. */
5736
5737 static void
5738 x_scroll_run (w, run)
5739 struct window *w;
5740 struct run *run;
5741 {
5742 struct frame *f = XFRAME (w->frame);
5743 int x, y, width, height, from_y, to_y, bottom_y;
5744
5745 /* Get frame-relative bounding box of the text display area of W,
5746 without mode lines. Include in this box the left and right
5747 fringe of W. */
5748 window_box (w, -1, &x, &y, &width, &height);
5749 width += FRAME_X_FRINGE_WIDTH (f);
5750 x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
5751
5752 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5753 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5754 bottom_y = y + height;
5755
5756 if (to_y < from_y)
5757 {
5758 /* Scrolling up. Make sure we don't copy part of the mode
5759 line at the bottom. */
5760 if (from_y + run->height > bottom_y)
5761 height = bottom_y - from_y;
5762 else
5763 height = run->height;
5764 }
5765 else
5766 {
5767 /* Scolling down. Make sure we don't copy over the mode line.
5768 at the bottom. */
5769 if (to_y + run->height > bottom_y)
5770 height = bottom_y - to_y;
5771 else
5772 height = run->height;
5773 }
5774
5775 BLOCK_INPUT;
5776
5777 /* Cursor off. Will be switched on again in x_update_window_end. */
5778 updated_window = w;
5779 x_clear_cursor (w);
5780
5781 XCopyArea (FRAME_X_DISPLAY (f),
5782 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5783 f->output_data.x->normal_gc,
5784 x, from_y,
5785 width, height,
5786 x, to_y);
5787
5788 UNBLOCK_INPUT;
5789 }
5790
5791
5792 \f
5793 /***********************************************************************
5794 Exposure Events
5795 ***********************************************************************/
5796
5797 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5798 corner of the exposed rectangle. W and H are width and height of
5799 the exposed area. All are pixel values. W or H zero means redraw
5800 the entire frame. */
5801
5802 static void
5803 expose_frame (f, x, y, w, h)
5804 struct frame *f;
5805 int x, y, w, h;
5806 {
5807 XRectangle r;
5808 int mouse_face_overwritten_p = 0;
5809
5810 TRACE ((stderr, "expose_frame "));
5811
5812 /* No need to redraw if frame will be redrawn soon. */
5813 if (FRAME_GARBAGED_P (f))
5814 {
5815 TRACE ((stderr, " garbaged\n"));
5816 return;
5817 }
5818
5819 /* If basic faces haven't been realized yet, there is no point in
5820 trying to redraw anything. This can happen when we get an expose
5821 event while Emacs is starting, e.g. by moving another window. */
5822 if (FRAME_FACE_CACHE (f) == NULL
5823 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5824 {
5825 TRACE ((stderr, " no faces\n"));
5826 return;
5827 }
5828
5829 if (w == 0 || h == 0)
5830 {
5831 r.x = r.y = 0;
5832 r.width = CANON_X_UNIT (f) * f->width;
5833 r.height = CANON_Y_UNIT (f) * f->height;
5834 }
5835 else
5836 {
5837 r.x = x;
5838 r.y = y;
5839 r.width = w;
5840 r.height = h;
5841 }
5842
5843 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5844 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
5845
5846 if (WINDOWP (f->tool_bar_window))
5847 mouse_face_overwritten_p
5848 |= expose_window (XWINDOW (f->tool_bar_window), &r);
5849
5850 #ifndef USE_X_TOOLKIT
5851 if (WINDOWP (f->menu_bar_window))
5852 mouse_face_overwritten_p
5853 |= expose_window (XWINDOW (f->menu_bar_window), &r);
5854 #endif /* not USE_X_TOOLKIT */
5855
5856 /* Some window managers support a focus-follows-mouse style with
5857 delayed raising of frames. Imagine a partially obscured frame,
5858 and moving the mouse into partially obscured mouse-face on that
5859 frame. The visible part of the mouse-face will be highlighted,
5860 then the WM raises the obscured frame. With at least one WM, KDE
5861 2.1, Emacs is not getting any event for the raising of the frame
5862 (even tried with SubstructureRedirectMask), only Expose events.
5863 These expose events will draw text normally, i.e. not
5864 highlighted. Which means we must redo the highlight here.
5865 Subsume it under ``we love X''. --gerd 2001-08-15 */
5866 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
5867 {
5868 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5869 if (f == dpyinfo->mouse_face_mouse_frame)
5870 {
5871 int x = dpyinfo->mouse_face_mouse_x;
5872 int y = dpyinfo->mouse_face_mouse_y;
5873 clear_mouse_face (dpyinfo);
5874 note_mouse_highlight (f, x, y);
5875 }
5876 }
5877 }
5878
5879
5880 /* Redraw (parts) of all windows in the window tree rooted at W that
5881 intersect R. R contains frame pixel coordinates. Value is
5882 non-zero if the exposure overwrites mouse-face. */
5883
5884 static int
5885 expose_window_tree (w, r)
5886 struct window *w;
5887 XRectangle *r;
5888 {
5889 struct frame *f = XFRAME (w->frame);
5890 int mouse_face_overwritten_p = 0;
5891
5892 while (w && !FRAME_GARBAGED_P (f))
5893 {
5894 if (!NILP (w->hchild))
5895 mouse_face_overwritten_p
5896 |= expose_window_tree (XWINDOW (w->hchild), r);
5897 else if (!NILP (w->vchild))
5898 mouse_face_overwritten_p
5899 |= expose_window_tree (XWINDOW (w->vchild), r);
5900 else
5901 mouse_face_overwritten_p |= expose_window (w, r);
5902
5903 w = NILP (w->next) ? NULL : XWINDOW (w->next);
5904 }
5905
5906 return mouse_face_overwritten_p;
5907 }
5908
5909
5910 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5911 which intersects rectangle R. R is in window-relative coordinates. */
5912
5913 static void
5914 expose_area (w, row, r, area)
5915 struct window *w;
5916 struct glyph_row *row;
5917 XRectangle *r;
5918 enum glyph_row_area area;
5919 {
5920 struct glyph *first = row->glyphs[area];
5921 struct glyph *end = row->glyphs[area] + row->used[area];
5922 struct glyph *last;
5923 int first_x, start_x, x;
5924
5925 if (area == TEXT_AREA && row->fill_line_p)
5926 /* If row extends face to end of line write the whole line. */
5927 x_draw_glyphs (w, 0, row, area, 0, row->used[area],
5928 DRAW_NORMAL_TEXT, 0);
5929 else
5930 {
5931 /* Set START_X to the window-relative start position for drawing glyphs of
5932 AREA. The first glyph of the text area can be partially visible.
5933 The first glyphs of other areas cannot. */
5934 if (area == LEFT_MARGIN_AREA)
5935 start_x = 0;
5936 else if (area == TEXT_AREA)
5937 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5938 else
5939 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5940 + window_box_width (w, TEXT_AREA));
5941 x = start_x;
5942
5943 /* Find the first glyph that must be redrawn. */
5944 while (first < end
5945 && x + first->pixel_width < r->x)
5946 {
5947 x += first->pixel_width;
5948 ++first;
5949 }
5950
5951 /* Find the last one. */
5952 last = first;
5953 first_x = x;
5954 while (last < end
5955 && x < r->x + r->width)
5956 {
5957 x += last->pixel_width;
5958 ++last;
5959 }
5960
5961 /* Repaint. */
5962 if (last > first)
5963 x_draw_glyphs (w, first_x - start_x, row, area,
5964 first - row->glyphs[area],
5965 last - row->glyphs[area],
5966 DRAW_NORMAL_TEXT, 0);
5967 }
5968 }
5969
5970
5971 /* Redraw the parts of the glyph row ROW on window W intersecting
5972 rectangle R. R is in window-relative coordinates. Value is
5973 non-zero if mouse-face was overwritten. */
5974
5975 static int
5976 expose_line (w, row, r)
5977 struct window *w;
5978 struct glyph_row *row;
5979 XRectangle *r;
5980 {
5981 xassert (row->enabled_p);
5982
5983 if (row->mode_line_p || w->pseudo_window_p)
5984 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5985 DRAW_NORMAL_TEXT, 0);
5986 else
5987 {
5988 if (row->used[LEFT_MARGIN_AREA])
5989 expose_area (w, row, r, LEFT_MARGIN_AREA);
5990 if (row->used[TEXT_AREA])
5991 expose_area (w, row, r, TEXT_AREA);
5992 if (row->used[RIGHT_MARGIN_AREA])
5993 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5994 x_draw_row_fringe_bitmaps (w, row);
5995 }
5996
5997 return row->mouse_face_p;
5998 }
5999
6000
6001 /* Return non-zero if W's cursor intersects rectangle R. */
6002
6003 static int
6004 x_phys_cursor_in_rect_p (w, r)
6005 struct window *w;
6006 XRectangle *r;
6007 {
6008 XRectangle cr, result;
6009 struct glyph *cursor_glyph;
6010
6011 cursor_glyph = get_phys_cursor_glyph (w);
6012 if (cursor_glyph)
6013 {
6014 cr.x = w->phys_cursor.x;
6015 cr.y = w->phys_cursor.y;
6016 cr.width = cursor_glyph->pixel_width;
6017 cr.height = w->phys_cursor_height;
6018 return x_intersect_rectangles (&cr, r, &result);
6019 }
6020 else
6021 return 0;
6022 }
6023
6024
6025 /* Redraw the part of window W intersection rectangle FR. Pixel
6026 coordinates in FR are frame-relative. Call this function with
6027 input blocked. Value is non-zero if the exposure overwrites
6028 mouse-face. */
6029
6030 static int
6031 expose_window (w, fr)
6032 struct window *w;
6033 XRectangle *fr;
6034 {
6035 struct frame *f = XFRAME (w->frame);
6036 XRectangle wr, r;
6037 int mouse_face_overwritten_p = 0;
6038
6039 /* If window is not yet fully initialized, do nothing. This can
6040 happen when toolkit scroll bars are used and a window is split.
6041 Reconfiguring the scroll bar will generate an expose for a newly
6042 created window. */
6043 if (w->current_matrix == NULL)
6044 return 0;
6045
6046 /* When we're currently updating the window, display and current
6047 matrix usually don't agree. Arrange for a thorough display
6048 later. */
6049 if (w == updated_window)
6050 {
6051 SET_FRAME_GARBAGED (f);
6052 return 0;
6053 }
6054
6055 /* Frame-relative pixel rectangle of W. */
6056 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
6057 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
6058 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
6059 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
6060
6061 if (x_intersect_rectangles (fr, &wr, &r))
6062 {
6063 int yb = window_text_bottom_y (w);
6064 struct glyph_row *row;
6065 int cursor_cleared_p;
6066
6067 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
6068 r.x, r.y, r.width, r.height));
6069
6070 /* Convert to window coordinates. */
6071 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
6072 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
6073
6074 /* Turn off the cursor. */
6075 if (!w->pseudo_window_p
6076 && x_phys_cursor_in_rect_p (w, &r))
6077 {
6078 x_clear_cursor (w);
6079 cursor_cleared_p = 1;
6080 }
6081 else
6082 cursor_cleared_p = 0;
6083
6084 /* Find the first row intersecting the rectangle R. */
6085 for (row = w->current_matrix->rows;
6086 row->enabled_p;
6087 ++row)
6088 {
6089 int y0 = row->y;
6090 int y1 = MATRIX_ROW_BOTTOM_Y (row);
6091
6092 if ((y0 >= r.y && y0 < r.y + r.height)
6093 || (y1 > r.y && y1 < r.y + r.height)
6094 || (r.y >= y0 && r.y < y1)
6095 || (r.y + r.height > y0 && r.y + r.height < y1))
6096 {
6097 if (expose_line (w, row, &r))
6098 mouse_face_overwritten_p = 1;
6099 }
6100
6101 if (y1 >= yb)
6102 break;
6103 }
6104
6105 /* Display the mode line if there is one. */
6106 if (WINDOW_WANTS_MODELINE_P (w)
6107 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
6108 row->enabled_p)
6109 && row->y < r.y + r.height)
6110 {
6111 if (expose_line (w, row, &r))
6112 mouse_face_overwritten_p = 1;
6113 }
6114
6115 if (!w->pseudo_window_p)
6116 {
6117 /* Draw border between windows. */
6118 x_draw_vertical_border (w);
6119
6120 /* Turn the cursor on again. */
6121 if (cursor_cleared_p)
6122 x_update_window_cursor (w, 1);
6123 }
6124 }
6125
6126 return mouse_face_overwritten_p;
6127 }
6128
6129
6130 /* Determine the intersection of two rectangles R1 and R2. Return
6131 the intersection in *RESULT. Value is non-zero if RESULT is not
6132 empty. */
6133
6134 static int
6135 x_intersect_rectangles (r1, r2, result)
6136 XRectangle *r1, *r2, *result;
6137 {
6138 XRectangle *left, *right;
6139 XRectangle *upper, *lower;
6140 int intersection_p = 0;
6141
6142 /* Rearrange so that R1 is the left-most rectangle. */
6143 if (r1->x < r2->x)
6144 left = r1, right = r2;
6145 else
6146 left = r2, right = r1;
6147
6148 /* X0 of the intersection is right.x0, if this is inside R1,
6149 otherwise there is no intersection. */
6150 if (right->x <= left->x + left->width)
6151 {
6152 result->x = right->x;
6153
6154 /* The right end of the intersection is the minimum of the
6155 the right ends of left and right. */
6156 result->width = (min (left->x + left->width, right->x + right->width)
6157 - result->x);
6158
6159 /* Same game for Y. */
6160 if (r1->y < r2->y)
6161 upper = r1, lower = r2;
6162 else
6163 upper = r2, lower = r1;
6164
6165 /* The upper end of the intersection is lower.y0, if this is inside
6166 of upper. Otherwise, there is no intersection. */
6167 if (lower->y <= upper->y + upper->height)
6168 {
6169 result->y = lower->y;
6170
6171 /* The lower end of the intersection is the minimum of the lower
6172 ends of upper and lower. */
6173 result->height = (min (lower->y + lower->height,
6174 upper->y + upper->height)
6175 - result->y);
6176 intersection_p = 1;
6177 }
6178 }
6179
6180 return intersection_p;
6181 }
6182
6183
6184
6185
6186 \f
6187 static void
6188 frame_highlight (f)
6189 struct frame *f;
6190 {
6191 /* We used to only do this if Vx_no_window_manager was non-nil, but
6192 the ICCCM (section 4.1.6) says that the window's border pixmap
6193 and border pixel are window attributes which are "private to the
6194 client", so we can always change it to whatever we want. */
6195 BLOCK_INPUT;
6196 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6197 f->output_data.x->border_pixel);
6198 UNBLOCK_INPUT;
6199 x_update_cursor (f, 1);
6200 }
6201
6202 static void
6203 frame_unhighlight (f)
6204 struct frame *f;
6205 {
6206 /* We used to only do this if Vx_no_window_manager was non-nil, but
6207 the ICCCM (section 4.1.6) says that the window's border pixmap
6208 and border pixel are window attributes which are "private to the
6209 client", so we can always change it to whatever we want. */
6210 BLOCK_INPUT;
6211 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6212 f->output_data.x->border_tile);
6213 UNBLOCK_INPUT;
6214 x_update_cursor (f, 1);
6215 }
6216
6217 /* The focus has changed. Update the frames as necessary to reflect
6218 the new situation. Note that we can't change the selected frame
6219 here, because the Lisp code we are interrupting might become confused.
6220 Each event gets marked with the frame in which it occurred, so the
6221 Lisp code can tell when the switch took place by examining the events. */
6222
6223 static void
6224 x_new_focus_frame (dpyinfo, frame)
6225 struct x_display_info *dpyinfo;
6226 struct frame *frame;
6227 {
6228 struct frame *old_focus = dpyinfo->x_focus_frame;
6229
6230 if (frame != dpyinfo->x_focus_frame)
6231 {
6232 /* Set this before calling other routines, so that they see
6233 the correct value of x_focus_frame. */
6234 dpyinfo->x_focus_frame = frame;
6235
6236 if (old_focus && old_focus->auto_lower)
6237 x_lower_frame (old_focus);
6238
6239 #if 0
6240 selected_frame = frame;
6241 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6242 selected_frame);
6243 Fselect_window (selected_frame->selected_window);
6244 choose_minibuf_frame ();
6245 #endif /* ! 0 */
6246
6247 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6248 pending_autoraise_frame = dpyinfo->x_focus_frame;
6249 else
6250 pending_autoraise_frame = 0;
6251 }
6252
6253 x_frame_rehighlight (dpyinfo);
6254 }
6255
6256 /* Handle an event saying the mouse has moved out of an Emacs frame. */
6257
6258 void
6259 x_mouse_leave (dpyinfo)
6260 struct x_display_info *dpyinfo;
6261 {
6262 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
6263 }
6264
6265 /* The focus has changed, or we have redirected a frame's focus to
6266 another frame (this happens when a frame uses a surrogate
6267 mini-buffer frame). Shift the highlight as appropriate.
6268
6269 The FRAME argument doesn't necessarily have anything to do with which
6270 frame is being highlighted or un-highlighted; we only use it to find
6271 the appropriate X display info. */
6272
6273 static void
6274 XTframe_rehighlight (frame)
6275 struct frame *frame;
6276 {
6277 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6278 }
6279
6280 static void
6281 x_frame_rehighlight (dpyinfo)
6282 struct x_display_info *dpyinfo;
6283 {
6284 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6285
6286 if (dpyinfo->x_focus_frame)
6287 {
6288 dpyinfo->x_highlight_frame
6289 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6290 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6291 : dpyinfo->x_focus_frame);
6292 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
6293 {
6294 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6295 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
6296 }
6297 }
6298 else
6299 dpyinfo->x_highlight_frame = 0;
6300
6301 if (dpyinfo->x_highlight_frame != old_highlight)
6302 {
6303 if (old_highlight)
6304 frame_unhighlight (old_highlight);
6305 if (dpyinfo->x_highlight_frame)
6306 frame_highlight (dpyinfo->x_highlight_frame);
6307 }
6308 }
6309
6310
6311 \f
6312 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
6313
6314 /* Initialize mode_switch_bit and modifier_meaning. */
6315 static void
6316 x_find_modifier_meanings (dpyinfo)
6317 struct x_display_info *dpyinfo;
6318 {
6319 int min_code, max_code;
6320 KeySym *syms;
6321 int syms_per_code;
6322 XModifierKeymap *mods;
6323
6324 dpyinfo->meta_mod_mask = 0;
6325 dpyinfo->shift_lock_mask = 0;
6326 dpyinfo->alt_mod_mask = 0;
6327 dpyinfo->super_mod_mask = 0;
6328 dpyinfo->hyper_mod_mask = 0;
6329
6330 #ifdef HAVE_X11R4
6331 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
6332 #else
6333 min_code = dpyinfo->display->min_keycode;
6334 max_code = dpyinfo->display->max_keycode;
6335 #endif
6336
6337 syms = XGetKeyboardMapping (dpyinfo->display,
6338 min_code, max_code - min_code + 1,
6339 &syms_per_code);
6340 mods = XGetModifierMapping (dpyinfo->display);
6341
6342 /* Scan the modifier table to see which modifier bits the Meta and
6343 Alt keysyms are on. */
6344 {
6345 int row, col; /* The row and column in the modifier table. */
6346
6347 for (row = 3; row < 8; row++)
6348 for (col = 0; col < mods->max_keypermod; col++)
6349 {
6350 KeyCode code
6351 = mods->modifiermap[(row * mods->max_keypermod) + col];
6352
6353 /* Zeroes are used for filler. Skip them. */
6354 if (code == 0)
6355 continue;
6356
6357 /* Are any of this keycode's keysyms a meta key? */
6358 {
6359 int code_col;
6360
6361 for (code_col = 0; code_col < syms_per_code; code_col++)
6362 {
6363 int sym = syms[((code - min_code) * syms_per_code) + code_col];
6364
6365 switch (sym)
6366 {
6367 case XK_Meta_L:
6368 case XK_Meta_R:
6369 dpyinfo->meta_mod_mask |= (1 << row);
6370 break;
6371
6372 case XK_Alt_L:
6373 case XK_Alt_R:
6374 dpyinfo->alt_mod_mask |= (1 << row);
6375 break;
6376
6377 case XK_Hyper_L:
6378 case XK_Hyper_R:
6379 dpyinfo->hyper_mod_mask |= (1 << row);
6380 break;
6381
6382 case XK_Super_L:
6383 case XK_Super_R:
6384 dpyinfo->super_mod_mask |= (1 << row);
6385 break;
6386
6387 case XK_Shift_Lock:
6388 /* Ignore this if it's not on the lock modifier. */
6389 if ((1 << row) == LockMask)
6390 dpyinfo->shift_lock_mask = LockMask;
6391 break;
6392 }
6393 }
6394 }
6395 }
6396 }
6397
6398 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
6399 if (! dpyinfo->meta_mod_mask)
6400 {
6401 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6402 dpyinfo->alt_mod_mask = 0;
6403 }
6404
6405 /* If some keys are both alt and meta,
6406 make them just meta, not alt. */
6407 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6408 {
6409 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6410 }
6411
6412 XFree ((char *) syms);
6413 XFreeModifiermap (mods);
6414 }
6415
6416 /* Convert between the modifier bits X uses and the modifier bits
6417 Emacs uses. */
6418
6419 static unsigned int
6420 x_x_to_emacs_modifiers (dpyinfo, state)
6421 struct x_display_info *dpyinfo;
6422 unsigned int state;
6423 {
6424 EMACS_UINT mod_meta = meta_modifier;
6425 EMACS_UINT mod_alt = alt_modifier;
6426 EMACS_UINT mod_hyper = hyper_modifier;
6427 EMACS_UINT mod_super = super_modifier;
6428 Lisp_Object tem;
6429
6430 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6431 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6432 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6433 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6434 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6435 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6436 tem = Fget (Vx_super_keysym, Qmodifier_value);
6437 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
6438
6439
6440 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6441 | ((state & ControlMask) ? ctrl_modifier : 0)
6442 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
6443 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
6444 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
6445 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
6446 }
6447
6448 static unsigned int
6449 x_emacs_to_x_modifiers (dpyinfo, state)
6450 struct x_display_info *dpyinfo;
6451 unsigned int state;
6452 {
6453 EMACS_UINT mod_meta = meta_modifier;
6454 EMACS_UINT mod_alt = alt_modifier;
6455 EMACS_UINT mod_hyper = hyper_modifier;
6456 EMACS_UINT mod_super = super_modifier;
6457
6458 Lisp_Object tem;
6459
6460 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6461 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6462 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6463 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6464 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6465 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6466 tem = Fget (Vx_super_keysym, Qmodifier_value);
6467 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
6468
6469
6470 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
6471 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
6472 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
6473 | ((state & shift_modifier) ? ShiftMask : 0)
6474 | ((state & ctrl_modifier) ? ControlMask : 0)
6475 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
6476 }
6477
6478 /* Convert a keysym to its name. */
6479
6480 char *
6481 x_get_keysym_name (keysym)
6482 KeySym keysym;
6483 {
6484 char *value;
6485
6486 BLOCK_INPUT;
6487 value = XKeysymToString (keysym);
6488 UNBLOCK_INPUT;
6489
6490 return value;
6491 }
6492
6493
6494 \f
6495 /* Mouse clicks and mouse movement. Rah. */
6496
6497 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6498 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6499 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6500 not force the value into range. */
6501
6502 void
6503 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6504 FRAME_PTR f;
6505 register int pix_x, pix_y;
6506 register int *x, *y;
6507 XRectangle *bounds;
6508 int noclip;
6509 {
6510 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6511 even for negative values. */
6512 if (pix_x < 0)
6513 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6514 if (pix_y < 0)
6515 pix_y -= (f)->output_data.x->line_height - 1;
6516
6517 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6518 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6519
6520 if (bounds)
6521 {
6522 bounds->width = FONT_WIDTH (f->output_data.x->font);
6523 bounds->height = f->output_data.x->line_height;
6524 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6525 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6526 }
6527
6528 if (!noclip)
6529 {
6530 if (pix_x < 0)
6531 pix_x = 0;
6532 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6533 pix_x = FRAME_WINDOW_WIDTH (f);
6534
6535 if (pix_y < 0)
6536 pix_y = 0;
6537 else if (pix_y > f->height)
6538 pix_y = f->height;
6539 }
6540
6541 *x = pix_x;
6542 *y = pix_y;
6543 }
6544
6545
6546 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6547 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6548 can't tell the positions because W's display is not up to date,
6549 return 0. */
6550
6551 int
6552 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6553 struct window *w;
6554 int hpos, vpos;
6555 int *frame_x, *frame_y;
6556 {
6557 int success_p;
6558
6559 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6560 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6561
6562 if (display_completed)
6563 {
6564 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6565 struct glyph *glyph = row->glyphs[TEXT_AREA];
6566 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6567
6568 *frame_y = row->y;
6569 *frame_x = row->x;
6570 while (glyph < end)
6571 {
6572 *frame_x += glyph->pixel_width;
6573 ++glyph;
6574 }
6575
6576 success_p = 1;
6577 }
6578 else
6579 {
6580 *frame_y = *frame_x = 0;
6581 success_p = 0;
6582 }
6583
6584 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6585 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6586 return success_p;
6587 }
6588
6589
6590 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6591
6592 If the event is a button press, then note that we have grabbed
6593 the mouse. */
6594
6595 static Lisp_Object
6596 construct_mouse_click (result, event, f)
6597 struct input_event *result;
6598 XButtonEvent *event;
6599 struct frame *f;
6600 {
6601 /* Make the event type no_event; we'll change that when we decide
6602 otherwise. */
6603 result->kind = mouse_click;
6604 result->code = event->button - Button1;
6605 result->timestamp = event->time;
6606 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6607 event->state)
6608 | (event->type == ButtonRelease
6609 ? up_modifier
6610 : down_modifier));
6611
6612 XSETINT (result->x, event->x);
6613 XSETINT (result->y, event->y);
6614 XSETFRAME (result->frame_or_window, f);
6615 result->arg = Qnil;
6616 return Qnil;
6617 }
6618
6619 \f
6620 /* Function to report a mouse movement to the mainstream Emacs code.
6621 The input handler calls this.
6622
6623 We have received a mouse movement event, which is given in *event.
6624 If the mouse is over a different glyph than it was last time, tell
6625 the mainstream emacs code by setting mouse_moved. If not, ask for
6626 another motion event, so we can check again the next time it moves. */
6627
6628 static XMotionEvent last_mouse_motion_event;
6629 static Lisp_Object last_mouse_motion_frame;
6630
6631 static void
6632 note_mouse_movement (frame, event)
6633 FRAME_PTR frame;
6634 XMotionEvent *event;
6635 {
6636 last_mouse_movement_time = event->time;
6637 last_mouse_motion_event = *event;
6638 XSETFRAME (last_mouse_motion_frame, frame);
6639
6640 if (x_autoselect_window_p)
6641 {
6642 int area;
6643 Lisp_Object window;
6644 static Lisp_Object last_window;
6645
6646 window = window_from_coordinates (frame, event->x, event->y, &area, 0);
6647
6648 /* Window will be selected only when it is not selected now and
6649 last mouse movement event was not in it. Minubuffer window
6650 will be selected iff it is active. */
6651 if (!EQ (window, last_window)
6652 && !EQ (window, selected_window)
6653 && (!MINI_WINDOW_P (XWINDOW (window))
6654 || (EQ (window, minibuf_window) && minibuf_level > 0)))
6655 Fselect_window (window);
6656
6657 last_window=window;
6658 }
6659
6660 if (event->window != FRAME_X_WINDOW (frame))
6661 {
6662 frame->mouse_moved = 1;
6663 last_mouse_scroll_bar = Qnil;
6664 note_mouse_highlight (frame, -1, -1);
6665 }
6666
6667 /* Has the mouse moved off the glyph it was on at the last sighting? */
6668 else if (event->x < last_mouse_glyph.x
6669 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6670 || event->y < last_mouse_glyph.y
6671 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6672 {
6673 frame->mouse_moved = 1;
6674 last_mouse_scroll_bar = Qnil;
6675 note_mouse_highlight (frame, event->x, event->y);
6676 }
6677 }
6678
6679 \f
6680 /************************************************************************
6681 Mouse Face
6682 ************************************************************************/
6683
6684 /* Find the glyph under window-relative coordinates X/Y in window W.
6685 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6686 strings. Return in *HPOS and *VPOS the row and column number of
6687 the glyph found. Return in *AREA the glyph area containing X.
6688 Value is a pointer to the glyph found or null if X/Y is not on
6689 text, or we can't tell because W's current matrix is not up to
6690 date. */
6691
6692 static struct glyph *
6693 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
6694 struct window *w;
6695 int x, y;
6696 int *hpos, *vpos, *area;
6697 int buffer_only_p;
6698 {
6699 struct glyph *glyph, *end;
6700 struct glyph_row *row = NULL;
6701 int x0, i, left_area_width;
6702
6703 /* Find row containing Y. Give up if some row is not enabled. */
6704 for (i = 0; i < w->current_matrix->nrows; ++i)
6705 {
6706 row = MATRIX_ROW (w->current_matrix, i);
6707 if (!row->enabled_p)
6708 return NULL;
6709 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6710 break;
6711 }
6712
6713 *vpos = i;
6714 *hpos = 0;
6715
6716 /* Give up if Y is not in the window. */
6717 if (i == w->current_matrix->nrows)
6718 return NULL;
6719
6720 /* Get the glyph area containing X. */
6721 if (w->pseudo_window_p)
6722 {
6723 *area = TEXT_AREA;
6724 x0 = 0;
6725 }
6726 else
6727 {
6728 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6729 if (x < left_area_width)
6730 {
6731 *area = LEFT_MARGIN_AREA;
6732 x0 = 0;
6733 }
6734 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6735 {
6736 *area = TEXT_AREA;
6737 x0 = row->x + left_area_width;
6738 }
6739 else
6740 {
6741 *area = RIGHT_MARGIN_AREA;
6742 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6743 }
6744 }
6745
6746 /* Find glyph containing X. */
6747 glyph = row->glyphs[*area];
6748 end = glyph + row->used[*area];
6749 while (glyph < end)
6750 {
6751 if (x < x0 + glyph->pixel_width)
6752 {
6753 if (w->pseudo_window_p)
6754 break;
6755 else if (!buffer_only_p || BUFFERP (glyph->object))
6756 break;
6757 }
6758
6759 x0 += glyph->pixel_width;
6760 ++glyph;
6761 }
6762
6763 if (glyph == end)
6764 return NULL;
6765
6766 *hpos = glyph - row->glyphs[*area];
6767 return glyph;
6768 }
6769
6770
6771 /* Convert frame-relative x/y to coordinates relative to window W.
6772 Takes pseudo-windows into account. */
6773
6774 static void
6775 frame_to_window_pixel_xy (w, x, y)
6776 struct window *w;
6777 int *x, *y;
6778 {
6779 if (w->pseudo_window_p)
6780 {
6781 /* A pseudo-window is always full-width, and starts at the
6782 left edge of the frame, plus a frame border. */
6783 struct frame *f = XFRAME (w->frame);
6784 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6785 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6786 }
6787 else
6788 {
6789 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6790 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6791 }
6792 }
6793
6794
6795 /* Take proper action when mouse has moved to the mode or header line of
6796 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6797 mode line. X is relative to the start of the text display area of
6798 W, so the width of fringes and scroll bars must be subtracted
6799 to get a position relative to the start of the mode line. */
6800
6801 static void
6802 note_mode_line_highlight (w, x, mode_line_p)
6803 struct window *w;
6804 int x, mode_line_p;
6805 {
6806 struct frame *f = XFRAME (w->frame);
6807 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6808 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6809 struct glyph_row *row;
6810
6811 if (mode_line_p)
6812 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6813 else
6814 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6815
6816 if (row->enabled_p)
6817 {
6818 struct glyph *glyph, *end;
6819 Lisp_Object help, map;
6820 int x0;
6821
6822 /* Find the glyph under X. */
6823 glyph = row->glyphs[TEXT_AREA];
6824 end = glyph + row->used[TEXT_AREA];
6825 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6826 + FRAME_X_LEFT_FRINGE_WIDTH (f));
6827
6828 while (glyph < end
6829 && x >= x0 + glyph->pixel_width)
6830 {
6831 x0 += glyph->pixel_width;
6832 ++glyph;
6833 }
6834
6835 if (glyph < end
6836 && STRINGP (glyph->object)
6837 && XSTRING (glyph->object)->intervals
6838 && glyph->charpos >= 0
6839 && glyph->charpos < XSTRING (glyph->object)->size)
6840 {
6841 /* If we're on a string with `help-echo' text property,
6842 arrange for the help to be displayed. This is done by
6843 setting the global variable help_echo to the help string. */
6844 help = Fget_text_property (make_number (glyph->charpos),
6845 Qhelp_echo, glyph->object);
6846 if (!NILP (help))
6847 {
6848 help_echo = help;
6849 XSETWINDOW (help_echo_window, w);
6850 help_echo_object = glyph->object;
6851 help_echo_pos = glyph->charpos;
6852 }
6853
6854 /* Change the mouse pointer according to what is under X/Y. */
6855 map = Fget_text_property (make_number (glyph->charpos),
6856 Qlocal_map, glyph->object);
6857 if (KEYMAPP (map))
6858 cursor = f->output_data.x->nontext_cursor;
6859 else
6860 {
6861 map = Fget_text_property (make_number (glyph->charpos),
6862 Qkeymap, glyph->object);
6863 if (KEYMAPP (map))
6864 cursor = f->output_data.x->nontext_cursor;
6865 }
6866 }
6867 }
6868
6869 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6870 }
6871
6872
6873 /* Take proper action when the mouse has moved to position X, Y on
6874 frame F as regards highlighting characters that have mouse-face
6875 properties. Also de-highlighting chars where the mouse was before.
6876 X and Y can be negative or out of range. */
6877
6878 static void
6879 note_mouse_highlight (f, x, y)
6880 struct frame *f;
6881 int x, y;
6882 {
6883 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6884 int portion;
6885 Lisp_Object window;
6886 struct window *w;
6887 Cursor cursor = None;
6888 struct buffer *b;
6889
6890 /* When a menu is active, don't highlight because this looks odd. */
6891 #ifdef USE_X_TOOLKIT
6892 if (popup_activated ())
6893 return;
6894 #endif
6895
6896 if (NILP (Vmouse_highlight)
6897 || !f->glyphs_initialized_p)
6898 return;
6899
6900 dpyinfo->mouse_face_mouse_x = x;
6901 dpyinfo->mouse_face_mouse_y = y;
6902 dpyinfo->mouse_face_mouse_frame = f;
6903
6904 if (dpyinfo->mouse_face_defer)
6905 return;
6906
6907 if (gc_in_progress)
6908 {
6909 dpyinfo->mouse_face_deferred_gc = 1;
6910 return;
6911 }
6912
6913 /* Which window is that in? */
6914 window = window_from_coordinates (f, x, y, &portion, 1);
6915
6916 /* If we were displaying active text in another window, clear that. */
6917 if (! EQ (window, dpyinfo->mouse_face_window))
6918 clear_mouse_face (dpyinfo);
6919
6920 /* Not on a window -> return. */
6921 if (!WINDOWP (window))
6922 return;
6923
6924 /* Convert to window-relative pixel coordinates. */
6925 w = XWINDOW (window);
6926 frame_to_window_pixel_xy (w, &x, &y);
6927
6928 /* Handle tool-bar window differently since it doesn't display a
6929 buffer. */
6930 if (EQ (window, f->tool_bar_window))
6931 {
6932 note_tool_bar_highlight (f, x, y);
6933 return;
6934 }
6935
6936 /* Mouse is on the mode or header line? */
6937 if (portion == 1 || portion == 3)
6938 {
6939 note_mode_line_highlight (w, x, portion == 1);
6940 return;
6941 }
6942
6943 if (portion == 2)
6944 cursor = f->output_data.x->horizontal_drag_cursor;
6945 else
6946 cursor = f->output_data.x->text_cursor;
6947
6948 /* Are we in a window whose display is up to date?
6949 And verify the buffer's text has not changed. */
6950 b = XBUFFER (w->buffer);
6951 if (/* Within text portion of the window. */
6952 portion == 0
6953 && EQ (w->window_end_valid, w->buffer)
6954 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
6955 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
6956 {
6957 int hpos, vpos, pos, i, area;
6958 struct glyph *glyph;
6959 Lisp_Object object;
6960 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
6961 Lisp_Object *overlay_vec = NULL;
6962 int len, noverlays;
6963 struct buffer *obuf;
6964 int obegv, ozv, same_region;
6965
6966 /* Find the glyph under X/Y. */
6967 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
6968
6969 /* Clear mouse face if X/Y not over text. */
6970 if (glyph == NULL
6971 || area != TEXT_AREA
6972 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6973 {
6974 if (clear_mouse_face (dpyinfo))
6975 cursor = None;
6976 goto set_cursor;
6977 }
6978
6979 pos = glyph->charpos;
6980 object = glyph->object;
6981 if (!STRINGP (object) && !BUFFERP (object))
6982 goto set_cursor;
6983
6984 /* If we get an out-of-range value, return now; avoid an error. */
6985 if (BUFFERP (object) && pos > BUF_Z (b))
6986 goto set_cursor;
6987
6988 /* Make the window's buffer temporarily current for
6989 overlays_at and compute_char_face. */
6990 obuf = current_buffer;
6991 current_buffer = b;
6992 obegv = BEGV;
6993 ozv = ZV;
6994 BEGV = BEG;
6995 ZV = Z;
6996
6997 /* Is this char mouse-active or does it have help-echo? */
6998 position = make_number (pos);
6999
7000 if (BUFFERP (object))
7001 {
7002 /* Put all the overlays we want in a vector in overlay_vec.
7003 Store the length in len. If there are more than 10, make
7004 enough space for all, and try again. */
7005 len = 10;
7006 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7007 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
7008 if (noverlays > len)
7009 {
7010 len = noverlays;
7011 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7012 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
7013 }
7014
7015 /* Sort overlays into increasing priority order. */
7016 noverlays = sort_overlays (overlay_vec, noverlays, w);
7017 }
7018 else
7019 noverlays = 0;
7020
7021 same_region = (EQ (window, dpyinfo->mouse_face_window)
7022 && vpos >= dpyinfo->mouse_face_beg_row
7023 && vpos <= dpyinfo->mouse_face_end_row
7024 && (vpos > dpyinfo->mouse_face_beg_row
7025 || hpos >= dpyinfo->mouse_face_beg_col)
7026 && (vpos < dpyinfo->mouse_face_end_row
7027 || hpos < dpyinfo->mouse_face_end_col
7028 || dpyinfo->mouse_face_past_end));
7029
7030 if (same_region)
7031 cursor = None;
7032
7033 /* Check mouse-face highlighting. */
7034 if (! same_region
7035 /* If there exists an overlay with mouse-face overlapping
7036 the one we are currently highlighting, we have to
7037 check if we enter the overlapping overlay, and then
7038 highlight only that. */
7039 || (OVERLAYP (dpyinfo->mouse_face_overlay)
7040 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
7041 {
7042 /* Find the highest priority overlay that has a mouse-face
7043 property. */
7044 overlay = Qnil;
7045 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
7046 {
7047 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
7048 if (!NILP (mouse_face))
7049 overlay = overlay_vec[i];
7050 }
7051
7052 /* If we're actually highlighting the same overlay as
7053 before, there's no need to do that again. */
7054 if (!NILP (overlay)
7055 && EQ (overlay, dpyinfo->mouse_face_overlay))
7056 goto check_help_echo;
7057
7058 dpyinfo->mouse_face_overlay = overlay;
7059
7060 /* Clear the display of the old active region, if any. */
7061 if (clear_mouse_face (dpyinfo))
7062 cursor = None;
7063
7064 /* If no overlay applies, get a text property. */
7065 if (NILP (overlay))
7066 mouse_face = Fget_text_property (position, Qmouse_face, object);
7067
7068 /* Handle the overlay case. */
7069 if (!NILP (overlay))
7070 {
7071 /* Find the range of text around this char that
7072 should be active. */
7073 Lisp_Object before, after;
7074 int ignore;
7075
7076 before = Foverlay_start (overlay);
7077 after = Foverlay_end (overlay);
7078 /* Record this as the current active region. */
7079 fast_find_position (w, XFASTINT (before),
7080 &dpyinfo->mouse_face_beg_col,
7081 &dpyinfo->mouse_face_beg_row,
7082 &dpyinfo->mouse_face_beg_x,
7083 &dpyinfo->mouse_face_beg_y, Qnil);
7084
7085 dpyinfo->mouse_face_past_end
7086 = !fast_find_position (w, XFASTINT (after),
7087 &dpyinfo->mouse_face_end_col,
7088 &dpyinfo->mouse_face_end_row,
7089 &dpyinfo->mouse_face_end_x,
7090 &dpyinfo->mouse_face_end_y, Qnil);
7091 dpyinfo->mouse_face_window = window;
7092 dpyinfo->mouse_face_face_id
7093 = face_at_buffer_position (w, pos, 0, 0,
7094 &ignore, pos + 1, 1);
7095
7096 /* Display it as active. */
7097 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7098 cursor = None;
7099 }
7100 /* Handle the text property case. */
7101 else if (!NILP (mouse_face) && BUFFERP (object))
7102 {
7103 /* Find the range of text around this char that
7104 should be active. */
7105 Lisp_Object before, after, beginning, end;
7106 int ignore;
7107
7108 beginning = Fmarker_position (w->start);
7109 end = make_number (BUF_Z (XBUFFER (object))
7110 - XFASTINT (w->window_end_pos));
7111 before
7112 = Fprevious_single_property_change (make_number (pos + 1),
7113 Qmouse_face,
7114 object, beginning);
7115 after
7116 = Fnext_single_property_change (position, Qmouse_face,
7117 object, end);
7118
7119 /* Record this as the current active region. */
7120 fast_find_position (w, XFASTINT (before),
7121 &dpyinfo->mouse_face_beg_col,
7122 &dpyinfo->mouse_face_beg_row,
7123 &dpyinfo->mouse_face_beg_x,
7124 &dpyinfo->mouse_face_beg_y, Qnil);
7125 dpyinfo->mouse_face_past_end
7126 = !fast_find_position (w, XFASTINT (after),
7127 &dpyinfo->mouse_face_end_col,
7128 &dpyinfo->mouse_face_end_row,
7129 &dpyinfo->mouse_face_end_x,
7130 &dpyinfo->mouse_face_end_y, Qnil);
7131 dpyinfo->mouse_face_window = window;
7132
7133 if (BUFFERP (object))
7134 dpyinfo->mouse_face_face_id
7135 = face_at_buffer_position (w, pos, 0, 0,
7136 &ignore, pos + 1, 1);
7137
7138 /* Display it as active. */
7139 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7140 cursor = None;
7141 }
7142 else if (!NILP (mouse_face) && STRINGP (object))
7143 {
7144 Lisp_Object b, e;
7145 int ignore;
7146
7147 b = Fprevious_single_property_change (make_number (pos + 1),
7148 Qmouse_face,
7149 object, Qnil);
7150 e = Fnext_single_property_change (position, Qmouse_face,
7151 object, Qnil);
7152 if (NILP (b))
7153 b = make_number (0);
7154 if (NILP (e))
7155 e = make_number (XSTRING (object)->size - 1);
7156 fast_find_string_pos (w, XINT (b), object,
7157 &dpyinfo->mouse_face_beg_col,
7158 &dpyinfo->mouse_face_beg_row,
7159 &dpyinfo->mouse_face_beg_x,
7160 &dpyinfo->mouse_face_beg_y, 0);
7161 fast_find_string_pos (w, XINT (e), object,
7162 &dpyinfo->mouse_face_end_col,
7163 &dpyinfo->mouse_face_end_row,
7164 &dpyinfo->mouse_face_end_x,
7165 &dpyinfo->mouse_face_end_y, 1);
7166 dpyinfo->mouse_face_past_end = 0;
7167 dpyinfo->mouse_face_window = window;
7168 dpyinfo->mouse_face_face_id
7169 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
7170 glyph->face_id, 1);
7171 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7172 cursor = None;
7173 }
7174 else if (STRINGP (object) && NILP (mouse_face))
7175 {
7176 /* A string which doesn't have mouse-face, but
7177 the text ``under'' it might have. */
7178 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
7179 int start = MATRIX_ROW_START_CHARPOS (r);
7180
7181 pos = string_buffer_position (w, object, start);
7182 if (pos > 0)
7183 mouse_face = get_char_property_and_overlay (make_number (pos),
7184 Qmouse_face,
7185 w->buffer,
7186 &overlay);
7187 if (!NILP (mouse_face) && !NILP (overlay))
7188 {
7189 Lisp_Object before = Foverlay_start (overlay);
7190 Lisp_Object after = Foverlay_end (overlay);
7191 int ignore;
7192
7193 /* Note that we might not be able to find position
7194 BEFORE in the glyph matrix if the overlay is
7195 entirely covered by a `display' property. In
7196 this case, we overshoot. So let's stop in
7197 the glyph matrix before glyphs for OBJECT. */
7198 fast_find_position (w, XFASTINT (before),
7199 &dpyinfo->mouse_face_beg_col,
7200 &dpyinfo->mouse_face_beg_row,
7201 &dpyinfo->mouse_face_beg_x,
7202 &dpyinfo->mouse_face_beg_y,
7203 object);
7204
7205 dpyinfo->mouse_face_past_end
7206 = !fast_find_position (w, XFASTINT (after),
7207 &dpyinfo->mouse_face_end_col,
7208 &dpyinfo->mouse_face_end_row,
7209 &dpyinfo->mouse_face_end_x,
7210 &dpyinfo->mouse_face_end_y,
7211 Qnil);
7212 dpyinfo->mouse_face_window = window;
7213 dpyinfo->mouse_face_face_id
7214 = face_at_buffer_position (w, pos, 0, 0,
7215 &ignore, pos + 1, 1);
7216
7217 /* Display it as active. */
7218 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7219 cursor = None;
7220 }
7221 }
7222 }
7223
7224 check_help_echo:
7225
7226 /* Look for a `help-echo' property. */
7227 {
7228 Lisp_Object help, overlay;
7229
7230 /* Check overlays first. */
7231 help = overlay = Qnil;
7232 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
7233 {
7234 overlay = overlay_vec[i];
7235 help = Foverlay_get (overlay, Qhelp_echo);
7236 }
7237
7238 if (!NILP (help))
7239 {
7240 help_echo = help;
7241 help_echo_window = window;
7242 help_echo_object = overlay;
7243 help_echo_pos = pos;
7244 }
7245 else
7246 {
7247 Lisp_Object object = glyph->object;
7248 int charpos = glyph->charpos;
7249
7250 /* Try text properties. */
7251 if (STRINGP (object)
7252 && charpos >= 0
7253 && charpos < XSTRING (object)->size)
7254 {
7255 help = Fget_text_property (make_number (charpos),
7256 Qhelp_echo, object);
7257 if (NILP (help))
7258 {
7259 /* If the string itself doesn't specify a help-echo,
7260 see if the buffer text ``under'' it does. */
7261 struct glyph_row *r
7262 = MATRIX_ROW (w->current_matrix, vpos);
7263 int start = MATRIX_ROW_START_CHARPOS (r);
7264 int pos = string_buffer_position (w, object, start);
7265 if (pos > 0)
7266 {
7267 help = Fget_char_property (make_number (pos),
7268 Qhelp_echo, w->buffer);
7269 if (!NILP (help))
7270 {
7271 charpos = pos;
7272 object = w->buffer;
7273 }
7274 }
7275 }
7276 }
7277 else if (BUFFERP (object)
7278 && charpos >= BEGV
7279 && charpos < ZV)
7280 help = Fget_text_property (make_number (charpos), Qhelp_echo,
7281 object);
7282
7283 if (!NILP (help))
7284 {
7285 help_echo = help;
7286 help_echo_window = window;
7287 help_echo_object = object;
7288 help_echo_pos = charpos;
7289 }
7290 }
7291 }
7292
7293 BEGV = obegv;
7294 ZV = ozv;
7295 current_buffer = obuf;
7296 }
7297
7298 set_cursor:
7299
7300 if (cursor != None)
7301 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7302 }
7303
7304 static void
7305 redo_mouse_highlight ()
7306 {
7307 if (!NILP (last_mouse_motion_frame)
7308 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
7309 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
7310 last_mouse_motion_event.x,
7311 last_mouse_motion_event.y);
7312 }
7313
7314
7315 \f
7316 /***********************************************************************
7317 Tool-bars
7318 ***********************************************************************/
7319
7320 static int x_tool_bar_item P_ ((struct frame *, int, int,
7321 struct glyph **, int *, int *, int *));
7322
7323 /* Tool-bar item index of the item on which a mouse button was pressed
7324 or -1. */
7325
7326 static int last_tool_bar_item;
7327
7328
7329 /* Get information about the tool-bar item at position X/Y on frame F.
7330 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7331 the current matrix of the tool-bar window of F, or NULL if not
7332 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
7333 item in F->tool_bar_items. Value is
7334
7335 -1 if X/Y is not on a tool-bar item
7336 0 if X/Y is on the same item that was highlighted before.
7337 1 otherwise. */
7338
7339 static int
7340 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
7341 struct frame *f;
7342 int x, y;
7343 struct glyph **glyph;
7344 int *hpos, *vpos, *prop_idx;
7345 {
7346 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7347 struct window *w = XWINDOW (f->tool_bar_window);
7348 int area;
7349
7350 /* Find the glyph under X/Y. */
7351 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
7352 if (*glyph == NULL)
7353 return -1;
7354
7355 /* Get the start of this tool-bar item's properties in
7356 f->tool_bar_items. */
7357 if (!tool_bar_item_info (f, *glyph, prop_idx))
7358 return -1;
7359
7360 /* Is mouse on the highlighted item? */
7361 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
7362 && *vpos >= dpyinfo->mouse_face_beg_row
7363 && *vpos <= dpyinfo->mouse_face_end_row
7364 && (*vpos > dpyinfo->mouse_face_beg_row
7365 || *hpos >= dpyinfo->mouse_face_beg_col)
7366 && (*vpos < dpyinfo->mouse_face_end_row
7367 || *hpos < dpyinfo->mouse_face_end_col
7368 || dpyinfo->mouse_face_past_end))
7369 return 0;
7370
7371 return 1;
7372 }
7373
7374
7375 /* Handle mouse button event on the tool-bar of frame F, at
7376 frame-relative coordinates X/Y. EVENT_TYPE is either ButtonPress
7377 or ButtonRelase. */
7378
7379 static void
7380 x_handle_tool_bar_click (f, button_event)
7381 struct frame *f;
7382 XButtonEvent *button_event;
7383 {
7384 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7385 struct window *w = XWINDOW (f->tool_bar_window);
7386 int hpos, vpos, prop_idx;
7387 struct glyph *glyph;
7388 Lisp_Object enabled_p;
7389 int x = button_event->x;
7390 int y = button_event->y;
7391
7392 /* If not on the highlighted tool-bar item, return. */
7393 frame_to_window_pixel_xy (w, &x, &y);
7394 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
7395 return;
7396
7397 /* If item is disabled, do nothing. */
7398 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7399 if (NILP (enabled_p))
7400 return;
7401
7402 if (button_event->type == ButtonPress)
7403 {
7404 /* Show item in pressed state. */
7405 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7406 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
7407 last_tool_bar_item = prop_idx;
7408 }
7409 else
7410 {
7411 Lisp_Object key, frame;
7412 struct input_event event;
7413
7414 /* Show item in released state. */
7415 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7416 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7417
7418 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
7419
7420 XSETFRAME (frame, f);
7421 event.kind = TOOL_BAR_EVENT;
7422 event.frame_or_window = frame;
7423 event.arg = frame;
7424 kbd_buffer_store_event (&event);
7425
7426 event.kind = TOOL_BAR_EVENT;
7427 event.frame_or_window = frame;
7428 event.arg = key;
7429 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7430 button_event->state);
7431 kbd_buffer_store_event (&event);
7432 last_tool_bar_item = -1;
7433 }
7434 }
7435
7436
7437 /* Possibly highlight a tool-bar item on frame F when mouse moves to
7438 tool-bar window-relative coordinates X/Y. Called from
7439 note_mouse_highlight. */
7440
7441 static void
7442 note_tool_bar_highlight (f, x, y)
7443 struct frame *f;
7444 int x, y;
7445 {
7446 Lisp_Object window = f->tool_bar_window;
7447 struct window *w = XWINDOW (window);
7448 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7449 int hpos, vpos;
7450 struct glyph *glyph;
7451 struct glyph_row *row;
7452 int i;
7453 Lisp_Object enabled_p;
7454 int prop_idx;
7455 enum draw_glyphs_face draw;
7456 int mouse_down_p, rc;
7457
7458 /* Function note_mouse_highlight is called with negative x(y
7459 values when mouse moves outside of the frame. */
7460 if (x <= 0 || y <= 0)
7461 {
7462 clear_mouse_face (dpyinfo);
7463 return;
7464 }
7465
7466 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7467 if (rc < 0)
7468 {
7469 /* Not on tool-bar item. */
7470 clear_mouse_face (dpyinfo);
7471 return;
7472 }
7473 else if (rc == 0)
7474 goto set_help_echo;
7475
7476 clear_mouse_face (dpyinfo);
7477
7478 /* Mouse is down, but on different tool-bar item? */
7479 mouse_down_p = (dpyinfo->grabbed
7480 && f == last_mouse_frame
7481 && FRAME_LIVE_P (f));
7482 if (mouse_down_p
7483 && last_tool_bar_item != prop_idx)
7484 return;
7485
7486 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7487 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7488
7489 /* If tool-bar item is not enabled, don't highlight it. */
7490 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7491 if (!NILP (enabled_p))
7492 {
7493 /* Compute the x-position of the glyph. In front and past the
7494 image is a space. We include this is the highlighted area. */
7495 row = MATRIX_ROW (w->current_matrix, vpos);
7496 for (i = x = 0; i < hpos; ++i)
7497 x += row->glyphs[TEXT_AREA][i].pixel_width;
7498
7499 /* Record this as the current active region. */
7500 dpyinfo->mouse_face_beg_col = hpos;
7501 dpyinfo->mouse_face_beg_row = vpos;
7502 dpyinfo->mouse_face_beg_x = x;
7503 dpyinfo->mouse_face_beg_y = row->y;
7504 dpyinfo->mouse_face_past_end = 0;
7505
7506 dpyinfo->mouse_face_end_col = hpos + 1;
7507 dpyinfo->mouse_face_end_row = vpos;
7508 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7509 dpyinfo->mouse_face_end_y = row->y;
7510 dpyinfo->mouse_face_window = window;
7511 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7512
7513 /* Display it as active. */
7514 show_mouse_face (dpyinfo, draw);
7515 dpyinfo->mouse_face_image_state = draw;
7516 }
7517
7518 set_help_echo:
7519
7520 /* Set help_echo to a help string to display for this tool-bar item.
7521 XTread_socket does the rest. */
7522 help_echo_object = help_echo_window = Qnil;
7523 help_echo_pos = -1;
7524 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
7525 if (NILP (help_echo))
7526 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
7527 }
7528
7529
7530 \f
7531 /* Find the glyph matrix position of buffer position CHARPOS in window
7532 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7533 current glyphs must be up to date. If CHARPOS is above window
7534 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
7535 of last line in W. In the row containing CHARPOS, stop before glyphs
7536 having STOP as object. */
7537
7538 #if 0 /* This is a version of fast_find_position that's more correct
7539 in the presence of hscrolling, for example. I didn't install
7540 it right away because the problem fixed is minor, it failed
7541 in 20.x as well, and I think it's too risky to install
7542 so near the release of 21.1. 2001-09-25 gerd. */
7543
7544 static int
7545 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
7546 struct window *w;
7547 int charpos;
7548 int *hpos, *vpos, *x, *y;
7549 Lisp_Object stop;
7550 {
7551 struct glyph_row *row, *first;
7552 struct glyph *glyph, *end;
7553 int i, past_end = 0;
7554
7555 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7556 row = row_containing_pos (w, charpos, first, NULL, 0);
7557 if (row == NULL)
7558 {
7559 if (charpos < MATRIX_ROW_START_CHARPOS (first))
7560 {
7561 *x = *y = *hpos = *vpos = 0;
7562 return 0;
7563 }
7564 else
7565 {
7566 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
7567 past_end = 1;
7568 }
7569 }
7570
7571 *x = row->x;
7572 *y = row->y;
7573 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7574
7575 glyph = row->glyphs[TEXT_AREA];
7576 end = glyph + row->used[TEXT_AREA];
7577
7578 /* Skip over glyphs not having an object at the start of the row.
7579 These are special glyphs like truncation marks on terminal
7580 frames. */
7581 if (row->displays_text_p)
7582 while (glyph < end
7583 && INTEGERP (glyph->object)
7584 && !EQ (stop, glyph->object)
7585 && glyph->charpos < 0)
7586 {
7587 *x += glyph->pixel_width;
7588 ++glyph;
7589 }
7590
7591 while (glyph < end
7592 && !INTEGERP (glyph->object)
7593 && !EQ (stop, glyph->object)
7594 && (!BUFFERP (glyph->object)
7595 || glyph->charpos < charpos))
7596 {
7597 *x += glyph->pixel_width;
7598 ++glyph;
7599 }
7600
7601 *hpos = glyph - row->glyphs[TEXT_AREA];
7602 return past_end;
7603 }
7604
7605 #else /* not 0 */
7606
7607 static int
7608 fast_find_position (w, pos, hpos, vpos, x, y, stop)
7609 struct window *w;
7610 int pos;
7611 int *hpos, *vpos, *x, *y;
7612 Lisp_Object stop;
7613 {
7614 int i;
7615 int lastcol;
7616 int maybe_next_line_p = 0;
7617 int line_start_position;
7618 int yb = window_text_bottom_y (w);
7619 struct glyph_row *row, *best_row;
7620 int row_vpos, best_row_vpos;
7621 int current_x;
7622
7623 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7624 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7625
7626 while (row->y < yb)
7627 {
7628 if (row->used[TEXT_AREA])
7629 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7630 else
7631 line_start_position = 0;
7632
7633 if (line_start_position > pos)
7634 break;
7635 /* If the position sought is the end of the buffer,
7636 don't include the blank lines at the bottom of the window. */
7637 else if (line_start_position == pos
7638 && pos == BUF_ZV (XBUFFER (w->buffer)))
7639 {
7640 maybe_next_line_p = 1;
7641 break;
7642 }
7643 else if (line_start_position > 0)
7644 {
7645 best_row = row;
7646 best_row_vpos = row_vpos;
7647 }
7648
7649 if (row->y + row->height >= yb)
7650 break;
7651
7652 ++row;
7653 ++row_vpos;
7654 }
7655
7656 /* Find the right column within BEST_ROW. */
7657 lastcol = 0;
7658 current_x = best_row->x;
7659 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
7660 {
7661 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7662 int charpos = glyph->charpos;
7663
7664 if (BUFFERP (glyph->object))
7665 {
7666 if (charpos == pos)
7667 {
7668 *hpos = i;
7669 *vpos = best_row_vpos;
7670 *x = current_x;
7671 *y = best_row->y;
7672 return 1;
7673 }
7674 else if (charpos > pos)
7675 break;
7676 }
7677 else if (EQ (glyph->object, stop))
7678 break;
7679
7680 if (charpos > 0)
7681 lastcol = i;
7682 current_x += glyph->pixel_width;
7683 }
7684
7685 /* If we're looking for the end of the buffer,
7686 and we didn't find it in the line we scanned,
7687 use the start of the following line. */
7688 if (maybe_next_line_p)
7689 {
7690 ++best_row;
7691 ++best_row_vpos;
7692 lastcol = 0;
7693 current_x = best_row->x;
7694 }
7695
7696 *vpos = best_row_vpos;
7697 *hpos = lastcol + 1;
7698 *x = current_x;
7699 *y = best_row->y;
7700 return 0;
7701 }
7702
7703 #endif /* not 0 */
7704
7705
7706 /* Find the position of the glyph for position POS in OBJECT in
7707 window W's current matrix, and return in *X, *Y the pixel
7708 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
7709
7710 RIGHT_P non-zero means return the position of the right edge of the
7711 glyph, RIGHT_P zero means return the left edge position.
7712
7713 If no glyph for POS exists in the matrix, return the position of
7714 the glyph with the next smaller position that is in the matrix, if
7715 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7716 exists in the matrix, return the position of the glyph with the
7717 next larger position in OBJECT.
7718
7719 Value is non-zero if a glyph was found. */
7720
7721 static int
7722 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7723 struct window *w;
7724 int pos;
7725 Lisp_Object object;
7726 int *hpos, *vpos, *x, *y;
7727 int right_p;
7728 {
7729 int yb = window_text_bottom_y (w);
7730 struct glyph_row *r;
7731 struct glyph *best_glyph = NULL;
7732 struct glyph_row *best_row = NULL;
7733 int best_x = 0;
7734
7735 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7736 r->enabled_p && r->y < yb;
7737 ++r)
7738 {
7739 struct glyph *g = r->glyphs[TEXT_AREA];
7740 struct glyph *e = g + r->used[TEXT_AREA];
7741 int gx;
7742
7743 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7744 if (EQ (g->object, object))
7745 {
7746 if (g->charpos == pos)
7747 {
7748 best_glyph = g;
7749 best_x = gx;
7750 best_row = r;
7751 goto found;
7752 }
7753 else if (best_glyph == NULL
7754 || ((abs (g->charpos - pos)
7755 < abs (best_glyph->charpos - pos))
7756 && (right_p
7757 ? g->charpos < pos
7758 : g->charpos > pos)))
7759 {
7760 best_glyph = g;
7761 best_x = gx;
7762 best_row = r;
7763 }
7764 }
7765 }
7766
7767 found:
7768
7769 if (best_glyph)
7770 {
7771 *x = best_x;
7772 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7773
7774 if (right_p)
7775 {
7776 *x += best_glyph->pixel_width;
7777 ++*hpos;
7778 }
7779
7780 *y = best_row->y;
7781 *vpos = best_row - w->current_matrix->rows;
7782 }
7783
7784 return best_glyph != NULL;
7785 }
7786
7787
7788 /* Display the active region described by mouse_face_*
7789 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7790
7791 static void
7792 show_mouse_face (dpyinfo, draw)
7793 struct x_display_info *dpyinfo;
7794 enum draw_glyphs_face draw;
7795 {
7796 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7797 struct frame *f = XFRAME (WINDOW_FRAME (w));
7798
7799 if (/* If window is in the process of being destroyed, don't bother
7800 to do anything. */
7801 w->current_matrix != NULL
7802 /* Don't update mouse highlight if hidden */
7803 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
7804 /* Recognize when we are called to operate on rows that don't exist
7805 anymore. This can happen when a window is split. */
7806 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
7807 {
7808 int phys_cursor_on_p = w->phys_cursor_on_p;
7809 struct glyph_row *row, *first, *last;
7810
7811 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
7812 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
7813
7814 for (row = first; row <= last && row->enabled_p; ++row)
7815 {
7816 int start_hpos, end_hpos, start_x;
7817
7818 /* For all but the first row, the highlight starts at column 0. */
7819 if (row == first)
7820 {
7821 start_hpos = dpyinfo->mouse_face_beg_col;
7822 start_x = dpyinfo->mouse_face_beg_x;
7823 }
7824 else
7825 {
7826 start_hpos = 0;
7827 start_x = 0;
7828 }
7829
7830 if (row == last)
7831 end_hpos = dpyinfo->mouse_face_end_col;
7832 else
7833 end_hpos = row->used[TEXT_AREA];
7834
7835 if (end_hpos > start_hpos)
7836 {
7837 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7838 start_hpos, end_hpos, draw, 0);
7839
7840 row->mouse_face_p
7841 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
7842 }
7843 }
7844
7845 /* When we've written over the cursor, arrange for it to
7846 be displayed again. */
7847 if (phys_cursor_on_p && !w->phys_cursor_on_p)
7848 x_display_cursor (w, 1,
7849 w->phys_cursor.hpos, w->phys_cursor.vpos,
7850 w->phys_cursor.x, w->phys_cursor.y);
7851 }
7852
7853 /* Change the mouse cursor. */
7854 if (draw == DRAW_NORMAL_TEXT)
7855 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7856 f->output_data.x->text_cursor);
7857 else if (draw == DRAW_MOUSE_FACE)
7858 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7859 f->output_data.x->cross_cursor);
7860 else
7861 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7862 f->output_data.x->nontext_cursor);
7863 }
7864
7865 /* Clear out the mouse-highlighted active region.
7866 Redraw it un-highlighted first. Value is non-zero if mouse
7867 face was actually drawn unhighlighted. */
7868
7869 static int
7870 clear_mouse_face (dpyinfo)
7871 struct x_display_info *dpyinfo;
7872 {
7873 int cleared = 0;
7874
7875 if (!NILP (dpyinfo->mouse_face_window))
7876 {
7877 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7878 cleared = 1;
7879 }
7880
7881 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7882 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7883 dpyinfo->mouse_face_window = Qnil;
7884 dpyinfo->mouse_face_overlay = Qnil;
7885 return cleared;
7886 }
7887
7888
7889 /* Clear any mouse-face on window W. This function is part of the
7890 redisplay interface, and is called from try_window_id and similar
7891 functions to ensure the mouse-highlight is off. */
7892
7893 static void
7894 x_clear_mouse_face (w)
7895 struct window *w;
7896 {
7897 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7898 Lisp_Object window;
7899
7900 BLOCK_INPUT;
7901 XSETWINDOW (window, w);
7902 if (EQ (window, dpyinfo->mouse_face_window))
7903 clear_mouse_face (dpyinfo);
7904 UNBLOCK_INPUT;
7905 }
7906
7907
7908 /* Just discard the mouse face information for frame F, if any.
7909 This is used when the size of F is changed. */
7910
7911 void
7912 cancel_mouse_face (f)
7913 FRAME_PTR f;
7914 {
7915 Lisp_Object window;
7916 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7917
7918 window = dpyinfo->mouse_face_window;
7919 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7920 {
7921 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7922 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7923 dpyinfo->mouse_face_window = Qnil;
7924 }
7925 }
7926
7927 \f
7928 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
7929
7930
7931 /* Try to determine frame pixel position and size of the glyph under
7932 frame pixel coordinates X/Y on frame F . Return the position and
7933 size in *RECT. Value is non-zero if we could compute these
7934 values. */
7935
7936 static int
7937 glyph_rect (f, x, y, rect)
7938 struct frame *f;
7939 int x, y;
7940 XRectangle *rect;
7941 {
7942 Lisp_Object window;
7943 int part, found = 0;
7944
7945 window = window_from_coordinates (f, x, y, &part, 0);
7946 if (!NILP (window))
7947 {
7948 struct window *w = XWINDOW (window);
7949 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7950 struct glyph_row *end = r + w->current_matrix->nrows - 1;
7951
7952 frame_to_window_pixel_xy (w, &x, &y);
7953
7954 for (; !found && r < end && r->enabled_p; ++r)
7955 if (r->y >= y)
7956 {
7957 struct glyph *g = r->glyphs[TEXT_AREA];
7958 struct glyph *end = g + r->used[TEXT_AREA];
7959 int gx;
7960
7961 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
7962 if (gx >= x)
7963 {
7964 rect->width = g->pixel_width;
7965 rect->height = r->height;
7966 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
7967 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
7968 found = 1;
7969 }
7970 }
7971 }
7972
7973 return found;
7974 }
7975
7976
7977 /* Return the current position of the mouse.
7978 *FP should be a frame which indicates which display to ask about.
7979
7980 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
7981 and *PART to the frame, window, and scroll bar part that the mouse
7982 is over. Set *X and *Y to the portion and whole of the mouse's
7983 position on the scroll bar.
7984
7985 If the mouse movement started elsewhere, set *FP to the frame the
7986 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
7987 the mouse is over.
7988
7989 Set *TIME to the server time-stamp for the time at which the mouse
7990 was at this position.
7991
7992 Don't store anything if we don't have a valid set of values to report.
7993
7994 This clears the mouse_moved flag, so we can wait for the next mouse
7995 movement. */
7996
7997 static void
7998 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7999 FRAME_PTR *fp;
8000 int insist;
8001 Lisp_Object *bar_window;
8002 enum scroll_bar_part *part;
8003 Lisp_Object *x, *y;
8004 unsigned long *time;
8005 {
8006 FRAME_PTR f1;
8007
8008 BLOCK_INPUT;
8009
8010 if (! NILP (last_mouse_scroll_bar) && insist == 0)
8011 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
8012 else
8013 {
8014 Window root;
8015 int root_x, root_y;
8016
8017 Window dummy_window;
8018 int dummy;
8019
8020 Lisp_Object frame, tail;
8021
8022 /* Clear the mouse-moved flag for every frame on this display. */
8023 FOR_EACH_FRAME (tail, frame)
8024 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
8025 XFRAME (frame)->mouse_moved = 0;
8026
8027 last_mouse_scroll_bar = Qnil;
8028
8029 /* Figure out which root window we're on. */
8030 XQueryPointer (FRAME_X_DISPLAY (*fp),
8031 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
8032
8033 /* The root window which contains the pointer. */
8034 &root,
8035
8036 /* Trash which we can't trust if the pointer is on
8037 a different screen. */
8038 &dummy_window,
8039
8040 /* The position on that root window. */
8041 &root_x, &root_y,
8042
8043 /* More trash we can't trust. */
8044 &dummy, &dummy,
8045
8046 /* Modifier keys and pointer buttons, about which
8047 we don't care. */
8048 (unsigned int *) &dummy);
8049
8050 /* Now we have a position on the root; find the innermost window
8051 containing the pointer. */
8052 {
8053 Window win, child;
8054 int win_x, win_y;
8055 int parent_x = 0, parent_y = 0;
8056 int count;
8057
8058 win = root;
8059
8060 /* XTranslateCoordinates can get errors if the window
8061 structure is changing at the same time this function
8062 is running. So at least we must not crash from them. */
8063
8064 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
8065
8066 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
8067 && FRAME_LIVE_P (last_mouse_frame))
8068 {
8069 /* If mouse was grabbed on a frame, give coords for that frame
8070 even if the mouse is now outside it. */
8071 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
8072
8073 /* From-window, to-window. */
8074 root, FRAME_X_WINDOW (last_mouse_frame),
8075
8076 /* From-position, to-position. */
8077 root_x, root_y, &win_x, &win_y,
8078
8079 /* Child of win. */
8080 &child);
8081 f1 = last_mouse_frame;
8082 }
8083 else
8084 {
8085 while (1)
8086 {
8087 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
8088
8089 /* From-window, to-window. */
8090 root, win,
8091
8092 /* From-position, to-position. */
8093 root_x, root_y, &win_x, &win_y,
8094
8095 /* Child of win. */
8096 &child);
8097
8098 if (child == None || child == win)
8099 break;
8100
8101 win = child;
8102 parent_x = win_x;
8103 parent_y = win_y;
8104 }
8105
8106 /* Now we know that:
8107 win is the innermost window containing the pointer
8108 (XTC says it has no child containing the pointer),
8109 win_x and win_y are the pointer's position in it
8110 (XTC did this the last time through), and
8111 parent_x and parent_y are the pointer's position in win's parent.
8112 (They are what win_x and win_y were when win was child.
8113 If win is the root window, it has no parent, and
8114 parent_{x,y} are invalid, but that's okay, because we'll
8115 never use them in that case.) */
8116
8117 /* Is win one of our frames? */
8118 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
8119
8120 #ifdef USE_X_TOOLKIT
8121 /* If we end up with the menu bar window, say it's not
8122 on the frame. */
8123 if (f1 != NULL
8124 && f1->output_data.x->menubar_widget
8125 && win == XtWindow (f1->output_data.x->menubar_widget))
8126 f1 = NULL;
8127 #endif /* USE_X_TOOLKIT */
8128 }
8129
8130 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
8131 f1 = 0;
8132
8133 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
8134
8135 /* If not, is it one of our scroll bars? */
8136 if (! f1)
8137 {
8138 struct scroll_bar *bar = x_window_to_scroll_bar (win);
8139
8140 if (bar)
8141 {
8142 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8143 win_x = parent_x;
8144 win_y = parent_y;
8145 }
8146 }
8147
8148 if (f1 == 0 && insist > 0)
8149 f1 = SELECTED_FRAME ();
8150
8151 if (f1)
8152 {
8153 /* Ok, we found a frame. Store all the values.
8154 last_mouse_glyph is a rectangle used to reduce the
8155 generation of mouse events. To not miss any motion
8156 events, we must divide the frame into rectangles of the
8157 size of the smallest character that could be displayed
8158 on it, i.e. into the same rectangles that matrices on
8159 the frame are divided into. */
8160
8161 int width, height, gx, gy;
8162 XRectangle rect;
8163
8164 if (glyph_rect (f1, win_x, win_y, &rect))
8165 last_mouse_glyph = rect;
8166 else
8167 {
8168 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
8169 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
8170 gx = win_x;
8171 gy = win_y;
8172
8173 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
8174 round down even for negative values. */
8175 if (gx < 0)
8176 gx -= width - 1;
8177 if (gy < 0)
8178 gy -= height - 1;
8179 gx = (gx + width - 1) / width * width;
8180 gy = (gy + height - 1) / height * height;
8181
8182 last_mouse_glyph.width = width;
8183 last_mouse_glyph.height = height;
8184 last_mouse_glyph.x = gx;
8185 last_mouse_glyph.y = gy;
8186 }
8187
8188 *bar_window = Qnil;
8189 *part = 0;
8190 *fp = f1;
8191 XSETINT (*x, win_x);
8192 XSETINT (*y, win_y);
8193 *time = last_mouse_movement_time;
8194 }
8195 }
8196 }
8197
8198 UNBLOCK_INPUT;
8199 }
8200
8201
8202 #ifdef USE_X_TOOLKIT
8203
8204 /* Atimer callback function for TIMER. Called every 0.1s to process
8205 Xt timeouts, if needed. We must avoid calling XtAppPending as
8206 much as possible because that function does an implicit XFlush
8207 that slows us down. */
8208
8209 static void
8210 x_process_timeouts (timer)
8211 struct atimer *timer;
8212 {
8213 if (toolkit_scroll_bar_interaction || popup_activated_flag)
8214 {
8215 BLOCK_INPUT;
8216 while (XtAppPending (Xt_app_con) & XtIMTimer)
8217 XtAppProcessEvent (Xt_app_con, XtIMTimer);
8218 UNBLOCK_INPUT;
8219 }
8220 }
8221
8222 #endif /* USE_X_TOOLKIT */
8223
8224 \f
8225 /* Scroll bar support. */
8226
8227 /* Given an X window ID, find the struct scroll_bar which manages it.
8228 This can be called in GC, so we have to make sure to strip off mark
8229 bits. */
8230
8231 static struct scroll_bar *
8232 x_window_to_scroll_bar (window_id)
8233 Window window_id;
8234 {
8235 Lisp_Object tail;
8236
8237 for (tail = Vframe_list;
8238 XGCTYPE (tail) == Lisp_Cons;
8239 tail = XCDR (tail))
8240 {
8241 Lisp_Object frame, bar, condemned;
8242
8243 frame = XCAR (tail);
8244 /* All elements of Vframe_list should be frames. */
8245 if (! GC_FRAMEP (frame))
8246 abort ();
8247
8248 /* Scan this frame's scroll bar list for a scroll bar with the
8249 right window ID. */
8250 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
8251 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
8252 /* This trick allows us to search both the ordinary and
8253 condemned scroll bar lists with one loop. */
8254 ! GC_NILP (bar) || (bar = condemned,
8255 condemned = Qnil,
8256 ! GC_NILP (bar));
8257 bar = XSCROLL_BAR (bar)->next)
8258 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
8259 return XSCROLL_BAR (bar);
8260 }
8261
8262 return 0;
8263 }
8264
8265
8266 #if defined USE_LUCID
8267
8268 /* Return the Lucid menu bar WINDOW is part of. Return null
8269 if WINDOW is not part of a menu bar. */
8270
8271 static Widget
8272 x_window_to_menu_bar (window)
8273 Window window;
8274 {
8275 Lisp_Object tail;
8276
8277 for (tail = Vframe_list;
8278 XGCTYPE (tail) == Lisp_Cons;
8279 tail = XCDR (tail))
8280 {
8281 Lisp_Object frame = XCAR (tail);
8282 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
8283
8284 if (menu_bar && xlwmenu_window_p (menu_bar, window))
8285 return menu_bar;
8286 }
8287
8288 return NULL;
8289 }
8290
8291 #endif /* USE_LUCID */
8292
8293 \f
8294 /************************************************************************
8295 Toolkit scroll bars
8296 ************************************************************************/
8297
8298 #ifdef USE_TOOLKIT_SCROLL_BARS
8299
8300 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
8301 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
8302 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
8303 struct scroll_bar *));
8304 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
8305 int, int, int));
8306
8307
8308 /* Id of action hook installed for scroll bars. */
8309
8310 static XtActionHookId action_hook_id;
8311
8312 /* Lisp window being scrolled. Set when starting to interact with
8313 a toolkit scroll bar, reset to nil when ending the interaction. */
8314
8315 static Lisp_Object window_being_scrolled;
8316
8317 /* Last scroll bar part sent in xm_scroll_callback. */
8318
8319 static int last_scroll_bar_part;
8320
8321 /* Whether this is an Xaw with arrow-scrollbars. This should imply
8322 that movements of 1/20 of the screen size are mapped to up/down. */
8323
8324 static Boolean xaw3d_arrow_scroll;
8325
8326 /* Whether the drag scrolling maintains the mouse at the top of the
8327 thumb. If not, resizing the thumb needs to be done more carefully
8328 to avoid jerkyness. */
8329
8330 static Boolean xaw3d_pick_top;
8331
8332
8333 /* Action hook installed via XtAppAddActionHook when toolkit scroll
8334 bars are used.. The hook is responsible for detecting when
8335 the user ends an interaction with the scroll bar, and generates
8336 a `end-scroll' scroll_bar_click' event if so. */
8337
8338 static void
8339 xt_action_hook (widget, client_data, action_name, event, params,
8340 num_params)
8341 Widget widget;
8342 XtPointer client_data;
8343 String action_name;
8344 XEvent *event;
8345 String *params;
8346 Cardinal *num_params;
8347 {
8348 int scroll_bar_p;
8349 char *end_action;
8350
8351 #ifdef USE_MOTIF
8352 scroll_bar_p = XmIsScrollBar (widget);
8353 end_action = "Release";
8354 #else /* !USE_MOTIF i.e. use Xaw */
8355 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
8356 end_action = "EndScroll";
8357 #endif /* USE_MOTIF */
8358
8359 if (scroll_bar_p
8360 && strcmp (action_name, end_action) == 0
8361 && WINDOWP (window_being_scrolled))
8362 {
8363 struct window *w;
8364
8365 x_send_scroll_bar_event (window_being_scrolled,
8366 scroll_bar_end_scroll, 0, 0);
8367 w = XWINDOW (window_being_scrolled);
8368 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
8369 window_being_scrolled = Qnil;
8370 last_scroll_bar_part = -1;
8371
8372 /* Xt timeouts no longer needed. */
8373 toolkit_scroll_bar_interaction = 0;
8374 }
8375 }
8376
8377 /* A vector of windows used for communication between
8378 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
8379
8380 static struct window **scroll_bar_windows;
8381 static int scroll_bar_windows_size;
8382
8383
8384 /* Send a client message with message type Xatom_Scrollbar for a
8385 scroll action to the frame of WINDOW. PART is a value identifying
8386 the part of the scroll bar that was clicked on. PORTION is the
8387 amount to scroll of a whole of WHOLE. */
8388
8389 static void
8390 x_send_scroll_bar_event (window, part, portion, whole)
8391 Lisp_Object window;
8392 int part, portion, whole;
8393 {
8394 XEvent event;
8395 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
8396 struct window *w = XWINDOW (window);
8397 struct frame *f = XFRAME (w->frame);
8398 int i;
8399
8400 BLOCK_INPUT;
8401
8402 /* Construct a ClientMessage event to send to the frame. */
8403 ev->type = ClientMessage;
8404 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
8405 ev->display = FRAME_X_DISPLAY (f);
8406 ev->window = FRAME_X_WINDOW (f);
8407 ev->format = 32;
8408
8409 /* We can only transfer 32 bits in the XClientMessageEvent, which is
8410 not enough to store a pointer or Lisp_Object on a 64 bit system.
8411 So, store the window in scroll_bar_windows and pass the index
8412 into that array in the event. */
8413 for (i = 0; i < scroll_bar_windows_size; ++i)
8414 if (scroll_bar_windows[i] == NULL)
8415 break;
8416
8417 if (i == scroll_bar_windows_size)
8418 {
8419 int new_size = max (10, 2 * scroll_bar_windows_size);
8420 size_t nbytes = new_size * sizeof *scroll_bar_windows;
8421 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
8422
8423 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
8424 nbytes);
8425 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
8426 scroll_bar_windows_size = new_size;
8427 }
8428
8429 scroll_bar_windows[i] = w;
8430 ev->data.l[0] = (long) i;
8431 ev->data.l[1] = (long) part;
8432 ev->data.l[2] = (long) 0;
8433 ev->data.l[3] = (long) portion;
8434 ev->data.l[4] = (long) whole;
8435
8436 /* Make Xt timeouts work while the scroll bar is active. */
8437 toolkit_scroll_bar_interaction = 1;
8438
8439 /* Setting the event mask to zero means that the message will
8440 be sent to the client that created the window, and if that
8441 window no longer exists, no event will be sent. */
8442 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
8443 UNBLOCK_INPUT;
8444 }
8445
8446
8447 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
8448 in *IEVENT. */
8449
8450 static void
8451 x_scroll_bar_to_input_event (event, ievent)
8452 XEvent *event;
8453 struct input_event *ievent;
8454 {
8455 XClientMessageEvent *ev = (XClientMessageEvent *) event;
8456 Lisp_Object window;
8457 struct frame *f;
8458 struct window *w;
8459
8460 w = scroll_bar_windows[ev->data.l[0]];
8461 scroll_bar_windows[ev->data.l[0]] = NULL;
8462
8463 XSETWINDOW (window, w);
8464 f = XFRAME (w->frame);
8465
8466 ievent->kind = scroll_bar_click;
8467 ievent->frame_or_window = window;
8468 ievent->arg = Qnil;
8469 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
8470 ievent->part = ev->data.l[1];
8471 ievent->code = ev->data.l[2];
8472 ievent->x = make_number ((int) ev->data.l[3]);
8473 ievent->y = make_number ((int) ev->data.l[4]);
8474 ievent->modifiers = 0;
8475 }
8476
8477
8478 #ifdef USE_MOTIF
8479
8480 /* Minimum and maximum values used for Motif scroll bars. */
8481
8482 #define XM_SB_MIN 1
8483 #define XM_SB_MAX 10000000
8484 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
8485
8486
8487 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
8488 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
8489 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
8490
8491 static void
8492 xm_scroll_callback (widget, client_data, call_data)
8493 Widget widget;
8494 XtPointer client_data, call_data;
8495 {
8496 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8497 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
8498 int part = -1, whole = 0, portion = 0;
8499
8500 switch (cs->reason)
8501 {
8502 case XmCR_DECREMENT:
8503 bar->dragging = Qnil;
8504 part = scroll_bar_up_arrow;
8505 break;
8506
8507 case XmCR_INCREMENT:
8508 bar->dragging = Qnil;
8509 part = scroll_bar_down_arrow;
8510 break;
8511
8512 case XmCR_PAGE_DECREMENT:
8513 bar->dragging = Qnil;
8514 part = scroll_bar_above_handle;
8515 break;
8516
8517 case XmCR_PAGE_INCREMENT:
8518 bar->dragging = Qnil;
8519 part = scroll_bar_below_handle;
8520 break;
8521
8522 case XmCR_TO_TOP:
8523 bar->dragging = Qnil;
8524 part = scroll_bar_to_top;
8525 break;
8526
8527 case XmCR_TO_BOTTOM:
8528 bar->dragging = Qnil;
8529 part = scroll_bar_to_bottom;
8530 break;
8531
8532 case XmCR_DRAG:
8533 {
8534 int slider_size;
8535 int dragging_down_p = (INTEGERP (bar->dragging)
8536 && XINT (bar->dragging) <= cs->value);
8537
8538 /* Get the slider size. */
8539 BLOCK_INPUT;
8540 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
8541 UNBLOCK_INPUT;
8542
8543 /* At the max position of the scroll bar, do a line-wise
8544 movement. Without doing anything, we would be called with
8545 the same cs->value again and again. If we want to make
8546 sure that we can reach the end of the buffer, we have to do
8547 something.
8548
8549 Implementation note: setting bar->dragging always to
8550 cs->value gives a smoother movement at the max position.
8551 Setting it to nil when doing line-wise movement gives
8552 a better slider behavior. */
8553
8554 if (cs->value + slider_size == XM_SB_MAX
8555 || (dragging_down_p
8556 && last_scroll_bar_part == scroll_bar_down_arrow))
8557 {
8558 part = scroll_bar_down_arrow;
8559 bar->dragging = Qnil;
8560 }
8561 else
8562 {
8563 whole = XM_SB_RANGE;
8564 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
8565 part = scroll_bar_handle;
8566 bar->dragging = make_number (cs->value);
8567 }
8568 }
8569 break;
8570
8571 case XmCR_VALUE_CHANGED:
8572 break;
8573 };
8574
8575 if (part >= 0)
8576 {
8577 window_being_scrolled = bar->window;
8578 last_scroll_bar_part = part;
8579 x_send_scroll_bar_event (bar->window, part, portion, whole);
8580 }
8581 }
8582
8583
8584 #else /* !USE_MOTIF, i.e. Xaw. */
8585
8586
8587 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
8588 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8589 scroll bar struct. CALL_DATA is a pointer to a float saying where
8590 the thumb is. */
8591
8592 static void
8593 xaw_jump_callback (widget, client_data, call_data)
8594 Widget widget;
8595 XtPointer client_data, call_data;
8596 {
8597 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8598 float top = *(float *) call_data;
8599 float shown;
8600 int whole, portion, height;
8601 int part;
8602
8603 /* Get the size of the thumb, a value between 0 and 1. */
8604 BLOCK_INPUT;
8605 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
8606 UNBLOCK_INPUT;
8607
8608 whole = 10000000;
8609 portion = shown < 1 ? top * whole : 0;
8610
8611 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8612 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8613 the bottom, so we force the scrolling whenever we see that we're
8614 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8615 we try to ensure that we always stay two pixels away from the
8616 bottom). */
8617 part = scroll_bar_down_arrow;
8618 else
8619 part = scroll_bar_handle;
8620
8621 window_being_scrolled = bar->window;
8622 bar->dragging = make_number (portion);
8623 last_scroll_bar_part = part;
8624 x_send_scroll_bar_event (bar->window, part, portion, whole);
8625 }
8626
8627
8628 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
8629 i.e. line or page up or down. WIDGET is the Xaw scroll bar
8630 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8631 the scroll bar. CALL_DATA is an integer specifying the action that
8632 has taken place. Its magnitude is in the range 0..height of the
8633 scroll bar. Negative values mean scroll towards buffer start.
8634 Values < height of scroll bar mean line-wise movement. */
8635
8636 static void
8637 xaw_scroll_callback (widget, client_data, call_data)
8638 Widget widget;
8639 XtPointer client_data, call_data;
8640 {
8641 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8642 /* The position really is stored cast to a pointer. */
8643 int position = (long) call_data;
8644 Dimension height;
8645 int part;
8646
8647 /* Get the height of the scroll bar. */
8648 BLOCK_INPUT;
8649 XtVaGetValues (widget, XtNheight, &height, NULL);
8650 UNBLOCK_INPUT;
8651
8652 if (abs (position) >= height)
8653 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8654
8655 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8656 it maps line-movement to call_data = max(5, height/20). */
8657 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8658 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
8659 else
8660 part = scroll_bar_move_ratio;
8661
8662 window_being_scrolled = bar->window;
8663 bar->dragging = Qnil;
8664 last_scroll_bar_part = part;
8665 x_send_scroll_bar_event (bar->window, part, position, height);
8666 }
8667
8668
8669 #endif /* not USE_MOTIF */
8670
8671
8672 /* Create the widget for scroll bar BAR on frame F. Record the widget
8673 and X window of the scroll bar in BAR. */
8674
8675 static void
8676 x_create_toolkit_scroll_bar (f, bar)
8677 struct frame *f;
8678 struct scroll_bar *bar;
8679 {
8680 Window xwindow;
8681 Widget widget;
8682 Arg av[20];
8683 int ac = 0;
8684 char *scroll_bar_name = "verticalScrollBar";
8685 unsigned long pixel;
8686
8687 BLOCK_INPUT;
8688
8689 #ifdef USE_MOTIF
8690 /* Set resources. Create the widget. */
8691 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8692 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8693 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8694 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8695 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8696 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8697 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8698
8699 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8700 if (pixel != -1)
8701 {
8702 XtSetArg (av[ac], XmNforeground, pixel);
8703 ++ac;
8704 }
8705
8706 pixel = f->output_data.x->scroll_bar_background_pixel;
8707 if (pixel != -1)
8708 {
8709 XtSetArg (av[ac], XmNbackground, pixel);
8710 ++ac;
8711 }
8712
8713 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8714 scroll_bar_name, av, ac);
8715
8716 /* Add one callback for everything that can happen. */
8717 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8718 (XtPointer) bar);
8719 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8720 (XtPointer) bar);
8721 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8722 (XtPointer) bar);
8723 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8724 (XtPointer) bar);
8725 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8726 (XtPointer) bar);
8727 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8728 (XtPointer) bar);
8729 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8730 (XtPointer) bar);
8731
8732 /* Realize the widget. Only after that is the X window created. */
8733 XtRealizeWidget (widget);
8734
8735 /* Set the cursor to an arrow. I didn't find a resource to do that.
8736 And I'm wondering why it hasn't an arrow cursor by default. */
8737 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8738 f->output_data.x->nontext_cursor);
8739
8740 #else /* !USE_MOTIF i.e. use Xaw */
8741
8742 /* Set resources. Create the widget. The background of the
8743 Xaw3d scroll bar widget is a little bit light for my taste.
8744 We don't alter it here to let users change it according
8745 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8746 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8747 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
8748 /* For smoother scrolling with Xaw3d -sm */
8749 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8750
8751 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8752 if (pixel != -1)
8753 {
8754 XtSetArg (av[ac], XtNforeground, pixel);
8755 ++ac;
8756 }
8757
8758 pixel = f->output_data.x->scroll_bar_background_pixel;
8759 if (pixel != -1)
8760 {
8761 XtSetArg (av[ac], XtNbackground, pixel);
8762 ++ac;
8763 }
8764
8765 /* Top/bottom shadow colors. */
8766
8767 /* Allocate them, if necessary. */
8768 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
8769 {
8770 pixel = f->output_data.x->scroll_bar_background_pixel;
8771 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8772 &pixel, 1.2, 0x8000))
8773 pixel = -1;
8774 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
8775 }
8776 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8777 {
8778 pixel = f->output_data.x->scroll_bar_background_pixel;
8779 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8780 &pixel, 0.6, 0x4000))
8781 pixel = -1;
8782 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
8783 }
8784
8785 /* Tell the toolkit about them. */
8786 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
8787 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8788 /* We tried to allocate a color for the top/bottom shadow, and
8789 failed, so tell Xaw3d to use dithering instead. */
8790 {
8791 XtSetArg (av[ac], XtNbeNiceToColormap, True);
8792 ++ac;
8793 }
8794 else
8795 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
8796 be more consistent with other emacs 3d colors, and since Xaw3d is
8797 not good at dealing with allocation failure. */
8798 {
8799 /* This tells Xaw3d to use real colors instead of dithering for
8800 the shadows. */
8801 XtSetArg (av[ac], XtNbeNiceToColormap, False);
8802 ++ac;
8803
8804 /* Specify the colors. */
8805 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
8806 if (pixel != -1)
8807 {
8808 XtSetArg (av[ac], "topShadowPixel", pixel);
8809 ++ac;
8810 }
8811 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
8812 if (pixel != -1)
8813 {
8814 XtSetArg (av[ac], "bottomShadowPixel", pixel);
8815 ++ac;
8816 }
8817 }
8818
8819 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8820 f->output_data.x->edit_widget, av, ac);
8821
8822 {
8823 char *initial = "";
8824 char *val = initial;
8825 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8826 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8827 if (val == initial)
8828 { /* ARROW_SCROLL */
8829 xaw3d_arrow_scroll = True;
8830 /* Isn't that just a personal preference ? -sm */
8831 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8832 }
8833 }
8834
8835 /* Define callbacks. */
8836 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8837 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
8838 (XtPointer) bar);
8839
8840 /* Realize the widget. Only after that is the X window created. */
8841 XtRealizeWidget (widget);
8842
8843 #endif /* !USE_MOTIF */
8844
8845 /* Install an action hook that let's us detect when the user
8846 finishes interacting with a scroll bar. */
8847 if (action_hook_id == 0)
8848 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8849
8850 /* Remember X window and widget in the scroll bar vector. */
8851 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8852 xwindow = XtWindow (widget);
8853 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8854
8855 UNBLOCK_INPUT;
8856 }
8857
8858
8859 /* Set the thumb size and position of scroll bar BAR. We are currently
8860 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8861
8862 static void
8863 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8864 struct scroll_bar *bar;
8865 int portion, position, whole;
8866 {
8867 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8868 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8869 float top, shown;
8870
8871 if (whole == 0)
8872 top = 0, shown = 1;
8873 else
8874 {
8875 top = (float) position / whole;
8876 shown = (float) portion / whole;
8877 }
8878
8879 BLOCK_INPUT;
8880
8881 #ifdef USE_MOTIF
8882 {
8883 int size, value;
8884
8885 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
8886 is the scroll bar's maximum and MIN is the scroll bar's minimum
8887 value. */
8888 size = shown * XM_SB_RANGE;
8889 size = min (size, XM_SB_RANGE);
8890 size = max (size, 1);
8891
8892 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8893 value = top * XM_SB_RANGE;
8894 value = min (value, XM_SB_MAX - size);
8895 value = max (value, XM_SB_MIN);
8896
8897 if (NILP (bar->dragging))
8898 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8899 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8900 /* This has the negative side effect that the slider value is
8901 not what it would be if we scrolled here using line-wise or
8902 page-wise movement. */
8903 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8904 else
8905 {
8906 /* If currently dragging, only update the slider size.
8907 This reduces flicker effects. */
8908 int old_value, old_size, increment, page_increment;
8909
8910 XmScrollBarGetValues (widget, &old_value, &old_size,
8911 &increment, &page_increment);
8912 XmScrollBarSetValues (widget, old_value,
8913 min (size, XM_SB_RANGE - old_value),
8914 0, 0, False);
8915 }
8916 }
8917 #else /* !USE_MOTIF i.e. use Xaw */
8918 {
8919 float old_top, old_shown;
8920 Dimension height;
8921 XtVaGetValues (widget,
8922 XtNtopOfThumb, &old_top,
8923 XtNshown, &old_shown,
8924 XtNheight, &height,
8925 NULL);
8926
8927 /* Massage the top+shown values. */
8928 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8929 top = max (0, min (1, top));
8930 else
8931 top = old_top;
8932 /* Keep two pixels available for moving the thumb down. */
8933 shown = max (0, min (1 - top - (2.0 / height), shown));
8934
8935 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8936 check that your system's configuration file contains a define
8937 for `NARROWPROTO'. See s/freebsd.h for an example. */
8938 if (top != old_top || shown != old_shown)
8939 {
8940 if (NILP (bar->dragging))
8941 XawScrollbarSetThumb (widget, top, shown);
8942 else
8943 {
8944 #ifdef HAVE_XAW3D
8945 ScrollbarWidget sb = (ScrollbarWidget) widget;
8946 int scroll_mode = 0;
8947
8948 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8949 if (xaw3d_arrow_scroll)
8950 {
8951 /* Xaw3d stupidly ignores resize requests while dragging
8952 so we have to make it believe it's not in dragging mode. */
8953 scroll_mode = sb->scrollbar.scroll_mode;
8954 if (scroll_mode == 2)
8955 sb->scrollbar.scroll_mode = 0;
8956 }
8957 #endif
8958 /* Try to make the scrolling a tad smoother. */
8959 if (!xaw3d_pick_top)
8960 shown = min (shown, old_shown);
8961
8962 XawScrollbarSetThumb (widget, top, shown);
8963
8964 #ifdef HAVE_XAW3D
8965 if (xaw3d_arrow_scroll && scroll_mode == 2)
8966 sb->scrollbar.scroll_mode = scroll_mode;
8967 #endif
8968 }
8969 }
8970 }
8971 #endif /* !USE_MOTIF */
8972
8973 UNBLOCK_INPUT;
8974 }
8975
8976 #endif /* USE_TOOLKIT_SCROLL_BARS */
8977
8978
8979 \f
8980 /************************************************************************
8981 Scroll bars, general
8982 ************************************************************************/
8983
8984 /* Create a scroll bar and return the scroll bar vector for it. W is
8985 the Emacs window on which to create the scroll bar. TOP, LEFT,
8986 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8987 scroll bar. */
8988
8989 static struct scroll_bar *
8990 x_scroll_bar_create (w, top, left, width, height)
8991 struct window *w;
8992 int top, left, width, height;
8993 {
8994 struct frame *f = XFRAME (w->frame);
8995 struct scroll_bar *bar
8996 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8997
8998 BLOCK_INPUT;
8999
9000 #ifdef USE_TOOLKIT_SCROLL_BARS
9001 x_create_toolkit_scroll_bar (f, bar);
9002 #else /* not USE_TOOLKIT_SCROLL_BARS */
9003 {
9004 XSetWindowAttributes a;
9005 unsigned long mask;
9006 Window window;
9007
9008 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
9009 if (a.background_pixel == -1)
9010 a.background_pixel = f->output_data.x->background_pixel;
9011
9012 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9013 | ButtonMotionMask | PointerMotionHintMask
9014 | ExposureMask);
9015 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
9016
9017 mask = (CWBackPixel | CWEventMask | CWCursor);
9018
9019 /* Clear the area of W that will serve as a scroll bar. This is
9020 for the case that a window has been split horizontally. In
9021 this case, no clear_frame is generated to reduce flickering. */
9022 if (width > 0 && height > 0)
9023 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9024 left, top, width,
9025 window_box_height (w), False);
9026
9027 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9028 /* Position and size of scroll bar. */
9029 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9030 top,
9031 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9032 height,
9033 /* Border width, depth, class, and visual. */
9034 0,
9035 CopyFromParent,
9036 CopyFromParent,
9037 CopyFromParent,
9038 /* Attributes. */
9039 mask, &a);
9040 SET_SCROLL_BAR_X_WINDOW (bar, window);
9041 }
9042 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9043
9044 XSETWINDOW (bar->window, w);
9045 XSETINT (bar->top, top);
9046 XSETINT (bar->left, left);
9047 XSETINT (bar->width, width);
9048 XSETINT (bar->height, height);
9049 XSETINT (bar->start, 0);
9050 XSETINT (bar->end, 0);
9051 bar->dragging = Qnil;
9052
9053 /* Add bar to its frame's list of scroll bars. */
9054 bar->next = FRAME_SCROLL_BARS (f);
9055 bar->prev = Qnil;
9056 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9057 if (!NILP (bar->next))
9058 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9059
9060 /* Map the window/widget. */
9061 #ifdef USE_TOOLKIT_SCROLL_BARS
9062 {
9063 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
9064 XtConfigureWidget (scroll_bar,
9065 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9066 top,
9067 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9068 max (height, 1), 0);
9069 XtMapWidget (scroll_bar);
9070 }
9071 #else /* not USE_TOOLKIT_SCROLL_BARS */
9072 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9073 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9074
9075 UNBLOCK_INPUT;
9076 return bar;
9077 }
9078
9079
9080 /* Draw BAR's handle in the proper position.
9081
9082 If the handle is already drawn from START to END, don't bother
9083 redrawing it, unless REBUILD is non-zero; in that case, always
9084 redraw it. (REBUILD is handy for drawing the handle after expose
9085 events.)
9086
9087 Normally, we want to constrain the start and end of the handle to
9088 fit inside its rectangle, but if the user is dragging the scroll
9089 bar handle, we want to let them drag it down all the way, so that
9090 the bar's top is as far down as it goes; otherwise, there's no way
9091 to move to the very end of the buffer. */
9092
9093 #ifndef USE_TOOLKIT_SCROLL_BARS
9094
9095 static void
9096 x_scroll_bar_set_handle (bar, start, end, rebuild)
9097 struct scroll_bar *bar;
9098 int start, end;
9099 int rebuild;
9100 {
9101 int dragging = ! NILP (bar->dragging);
9102 Window w = SCROLL_BAR_X_WINDOW (bar);
9103 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9104 GC gc = f->output_data.x->normal_gc;
9105
9106 /* If the display is already accurate, do nothing. */
9107 if (! rebuild
9108 && start == XINT (bar->start)
9109 && end == XINT (bar->end))
9110 return;
9111
9112 BLOCK_INPUT;
9113
9114 {
9115 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
9116 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9117 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9118
9119 /* Make sure the values are reasonable, and try to preserve
9120 the distance between start and end. */
9121 {
9122 int length = end - start;
9123
9124 if (start < 0)
9125 start = 0;
9126 else if (start > top_range)
9127 start = top_range;
9128 end = start + length;
9129
9130 if (end < start)
9131 end = start;
9132 else if (end > top_range && ! dragging)
9133 end = top_range;
9134 }
9135
9136 /* Store the adjusted setting in the scroll bar. */
9137 XSETINT (bar->start, start);
9138 XSETINT (bar->end, end);
9139
9140 /* Clip the end position, just for display. */
9141 if (end > top_range)
9142 end = top_range;
9143
9144 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
9145 below top positions, to make sure the handle is always at least
9146 that many pixels tall. */
9147 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
9148
9149 /* Draw the empty space above the handle. Note that we can't clear
9150 zero-height areas; that means "clear to end of window." */
9151 if (0 < start)
9152 x_clear_area (FRAME_X_DISPLAY (f), w,
9153 /* x, y, width, height, and exposures. */
9154 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9155 VERTICAL_SCROLL_BAR_TOP_BORDER,
9156 inside_width, start,
9157 False);
9158
9159 /* Change to proper foreground color if one is specified. */
9160 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9161 XSetForeground (FRAME_X_DISPLAY (f), gc,
9162 f->output_data.x->scroll_bar_foreground_pixel);
9163
9164 /* Draw the handle itself. */
9165 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
9166 /* x, y, width, height */
9167 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9168 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
9169 inside_width, end - start);
9170
9171 /* Restore the foreground color of the GC if we changed it above. */
9172 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9173 XSetForeground (FRAME_X_DISPLAY (f), gc,
9174 f->output_data.x->foreground_pixel);
9175
9176 /* Draw the empty space below the handle. Note that we can't
9177 clear zero-height areas; that means "clear to end of window." */
9178 if (end < inside_height)
9179 x_clear_area (FRAME_X_DISPLAY (f), w,
9180 /* x, y, width, height, and exposures. */
9181 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9182 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
9183 inside_width, inside_height - end,
9184 False);
9185
9186 }
9187
9188 UNBLOCK_INPUT;
9189 }
9190
9191 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9192
9193 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
9194 nil. */
9195
9196 static void
9197 x_scroll_bar_remove (bar)
9198 struct scroll_bar *bar;
9199 {
9200 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9201 BLOCK_INPUT;
9202
9203 #ifdef USE_TOOLKIT_SCROLL_BARS
9204 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
9205 #else
9206 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9207 #endif
9208
9209 /* Disassociate this scroll bar from its window. */
9210 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
9211
9212 UNBLOCK_INPUT;
9213 }
9214
9215
9216 /* Set the handle of the vertical scroll bar for WINDOW to indicate
9217 that we are displaying PORTION characters out of a total of WHOLE
9218 characters, starting at POSITION. If WINDOW has no scroll bar,
9219 create one. */
9220
9221 static void
9222 XTset_vertical_scroll_bar (w, portion, whole, position)
9223 struct window *w;
9224 int portion, whole, position;
9225 {
9226 struct frame *f = XFRAME (w->frame);
9227 struct scroll_bar *bar;
9228 int top, height, left, sb_left, width, sb_width;
9229 int window_x, window_y, window_width, window_height;
9230
9231 /* Get window dimensions. */
9232 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
9233 top = window_y;
9234 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9235 height = window_height;
9236
9237 /* Compute the left edge of the scroll bar area. */
9238 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9239 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
9240 else
9241 left = XFASTINT (w->left);
9242 left *= CANON_X_UNIT (f);
9243 left += FRAME_INTERNAL_BORDER_WIDTH (f);
9244
9245 /* Compute the width of the scroll bar which might be less than
9246 the width of the area reserved for the scroll bar. */
9247 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
9248 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
9249 else
9250 sb_width = width;
9251
9252 /* Compute the left edge of the scroll bar. */
9253 #ifdef USE_TOOLKIT_SCROLL_BARS
9254 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9255 sb_left = left + width - sb_width - (width - sb_width) / 2;
9256 else
9257 sb_left = left + (width - sb_width) / 2;
9258 #else
9259 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9260 sb_left = left + width - sb_width;
9261 else
9262 sb_left = left;
9263 #endif
9264
9265 /* Does the scroll bar exist yet? */
9266 if (NILP (w->vertical_scroll_bar))
9267 {
9268 if (width > 0 && height > 0)
9269 {
9270 BLOCK_INPUT;
9271 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9272 left, top, width, height, False);
9273 UNBLOCK_INPUT;
9274 }
9275
9276 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
9277 }
9278 else
9279 {
9280 /* It may just need to be moved and resized. */
9281 unsigned int mask = 0;
9282
9283 bar = XSCROLL_BAR (w->vertical_scroll_bar);
9284
9285 BLOCK_INPUT;
9286
9287 if (sb_left != XINT (bar->left))
9288 mask |= CWX;
9289 if (top != XINT (bar->top))
9290 mask |= CWY;
9291 if (sb_width != XINT (bar->width))
9292 mask |= CWWidth;
9293 if (height != XINT (bar->height))
9294 mask |= CWHeight;
9295
9296 #ifdef USE_TOOLKIT_SCROLL_BARS
9297
9298 /* Since toolkit scroll bars are smaller than the space reserved
9299 for them on the frame, we have to clear "under" them. */
9300 if (width > 0 && height > 0)
9301 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9302 left, top, width, height, False);
9303
9304 /* Move/size the scroll bar widget. */
9305 if (mask)
9306 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
9307 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9308 top,
9309 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9310 max (height, 1), 0);
9311
9312 #else /* not USE_TOOLKIT_SCROLL_BARS */
9313
9314 /* Clear areas not covered by the scroll bar because of
9315 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
9316 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
9317 {
9318 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9319 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9320 height, False);
9321 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9322 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9323 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9324 height, False);
9325 }
9326
9327 /* Clear areas not covered by the scroll bar because it's not as
9328 wide as the area reserved for it . This makes sure a
9329 previous mode line display is cleared after C-x 2 C-x 1, for
9330 example. */
9331 {
9332 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9333 int rest = area_width - sb_width;
9334 if (rest > 0 && height > 0)
9335 {
9336 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
9337 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9338 left + area_width - rest, top,
9339 rest, height, False);
9340 else
9341 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9342 left, top, rest, height, False);
9343 }
9344 }
9345
9346 /* Move/size the scroll bar window. */
9347 if (mask)
9348 {
9349 XWindowChanges wc;
9350
9351 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9352 wc.y = top;
9353 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
9354 wc.height = height;
9355 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
9356 mask, &wc);
9357 }
9358
9359 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9360
9361 /* Remember new settings. */
9362 XSETINT (bar->left, sb_left);
9363 XSETINT (bar->top, top);
9364 XSETINT (bar->width, sb_width);
9365 XSETINT (bar->height, height);
9366
9367 UNBLOCK_INPUT;
9368 }
9369
9370 #ifdef USE_TOOLKIT_SCROLL_BARS
9371 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
9372 #else /* not USE_TOOLKIT_SCROLL_BARS */
9373 /* Set the scroll bar's current state, unless we're currently being
9374 dragged. */
9375 if (NILP (bar->dragging))
9376 {
9377 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
9378
9379 if (whole == 0)
9380 x_scroll_bar_set_handle (bar, 0, top_range, 0);
9381 else
9382 {
9383 int start = ((double) position * top_range) / whole;
9384 int end = ((double) (position + portion) * top_range) / whole;
9385 x_scroll_bar_set_handle (bar, start, end, 0);
9386 }
9387 }
9388 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9389
9390 XSETVECTOR (w->vertical_scroll_bar, bar);
9391 }
9392
9393
9394 /* The following three hooks are used when we're doing a thorough
9395 redisplay of the frame. We don't explicitly know which scroll bars
9396 are going to be deleted, because keeping track of when windows go
9397 away is a real pain - "Can you say set-window-configuration, boys
9398 and girls?" Instead, we just assert at the beginning of redisplay
9399 that *all* scroll bars are to be removed, and then save a scroll bar
9400 from the fiery pit when we actually redisplay its window. */
9401
9402 /* Arrange for all scroll bars on FRAME to be removed at the next call
9403 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
9404 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
9405
9406 static void
9407 XTcondemn_scroll_bars (frame)
9408 FRAME_PTR frame;
9409 {
9410 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
9411 while (! NILP (FRAME_SCROLL_BARS (frame)))
9412 {
9413 Lisp_Object bar;
9414 bar = FRAME_SCROLL_BARS (frame);
9415 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
9416 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
9417 XSCROLL_BAR (bar)->prev = Qnil;
9418 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
9419 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
9420 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
9421 }
9422 }
9423
9424
9425 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
9426 Note that WINDOW isn't necessarily condemned at all. */
9427
9428 static void
9429 XTredeem_scroll_bar (window)
9430 struct window *window;
9431 {
9432 struct scroll_bar *bar;
9433 struct frame *f;
9434
9435 /* We can't redeem this window's scroll bar if it doesn't have one. */
9436 if (NILP (window->vertical_scroll_bar))
9437 abort ();
9438
9439 bar = XSCROLL_BAR (window->vertical_scroll_bar);
9440
9441 /* Unlink it from the condemned list. */
9442 f = XFRAME (WINDOW_FRAME (window));
9443 if (NILP (bar->prev))
9444 {
9445 /* If the prev pointer is nil, it must be the first in one of
9446 the lists. */
9447 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
9448 /* It's not condemned. Everything's fine. */
9449 return;
9450 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
9451 window->vertical_scroll_bar))
9452 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
9453 else
9454 /* If its prev pointer is nil, it must be at the front of
9455 one or the other! */
9456 abort ();
9457 }
9458 else
9459 XSCROLL_BAR (bar->prev)->next = bar->next;
9460
9461 if (! NILP (bar->next))
9462 XSCROLL_BAR (bar->next)->prev = bar->prev;
9463
9464 bar->next = FRAME_SCROLL_BARS (f);
9465 bar->prev = Qnil;
9466 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9467 if (! NILP (bar->next))
9468 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9469 }
9470
9471 /* Remove all scroll bars on FRAME that haven't been saved since the
9472 last call to `*condemn_scroll_bars_hook'. */
9473
9474 static void
9475 XTjudge_scroll_bars (f)
9476 FRAME_PTR f;
9477 {
9478 Lisp_Object bar, next;
9479
9480 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
9481
9482 /* Clear out the condemned list now so we won't try to process any
9483 more events on the hapless scroll bars. */
9484 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
9485
9486 for (; ! NILP (bar); bar = next)
9487 {
9488 struct scroll_bar *b = XSCROLL_BAR (bar);
9489
9490 x_scroll_bar_remove (b);
9491
9492 next = b->next;
9493 b->next = b->prev = Qnil;
9494 }
9495
9496 /* Now there should be no references to the condemned scroll bars,
9497 and they should get garbage-collected. */
9498 }
9499
9500
9501 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
9502 is a no-op when using toolkit scroll bars.
9503
9504 This may be called from a signal handler, so we have to ignore GC
9505 mark bits. */
9506
9507 static void
9508 x_scroll_bar_expose (bar, event)
9509 struct scroll_bar *bar;
9510 XEvent *event;
9511 {
9512 #ifndef USE_TOOLKIT_SCROLL_BARS
9513
9514 Window w = SCROLL_BAR_X_WINDOW (bar);
9515 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9516 GC gc = f->output_data.x->normal_gc;
9517 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9518
9519 BLOCK_INPUT;
9520
9521 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
9522
9523 /* Draw a one-pixel border just inside the edges of the scroll bar. */
9524 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
9525
9526 /* x, y, width, height */
9527 0, 0,
9528 XINT (bar->width) - 1 - width_trim - width_trim,
9529 XINT (bar->height) - 1);
9530
9531 UNBLOCK_INPUT;
9532
9533 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9534 }
9535
9536 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
9537 is set to something other than no_event, it is enqueued.
9538
9539 This may be called from a signal handler, so we have to ignore GC
9540 mark bits. */
9541
9542 #ifndef USE_TOOLKIT_SCROLL_BARS
9543
9544 static void
9545 x_scroll_bar_handle_click (bar, event, emacs_event)
9546 struct scroll_bar *bar;
9547 XEvent *event;
9548 struct input_event *emacs_event;
9549 {
9550 if (! GC_WINDOWP (bar->window))
9551 abort ();
9552
9553 emacs_event->kind = scroll_bar_click;
9554 emacs_event->code = event->xbutton.button - Button1;
9555 emacs_event->modifiers
9556 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
9557 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
9558 event->xbutton.state)
9559 | (event->type == ButtonRelease
9560 ? up_modifier
9561 : down_modifier));
9562 emacs_event->frame_or_window = bar->window;
9563 emacs_event->arg = Qnil;
9564 emacs_event->timestamp = event->xbutton.time;
9565 {
9566 #if 0
9567 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9568 int internal_height
9569 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9570 #endif
9571 int top_range
9572 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9573 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
9574
9575 if (y < 0) y = 0;
9576 if (y > top_range) y = top_range;
9577
9578 if (y < XINT (bar->start))
9579 emacs_event->part = scroll_bar_above_handle;
9580 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9581 emacs_event->part = scroll_bar_handle;
9582 else
9583 emacs_event->part = scroll_bar_below_handle;
9584
9585 /* Just because the user has clicked on the handle doesn't mean
9586 they want to drag it. Lisp code needs to be able to decide
9587 whether or not we're dragging. */
9588 #if 0
9589 /* If the user has just clicked on the handle, record where they're
9590 holding it. */
9591 if (event->type == ButtonPress
9592 && emacs_event->part == scroll_bar_handle)
9593 XSETINT (bar->dragging, y - XINT (bar->start));
9594 #endif
9595
9596 /* If the user has released the handle, set it to its final position. */
9597 if (event->type == ButtonRelease
9598 && ! NILP (bar->dragging))
9599 {
9600 int new_start = y - XINT (bar->dragging);
9601 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9602
9603 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9604 bar->dragging = Qnil;
9605 }
9606
9607 /* Same deal here as the other #if 0. */
9608 #if 0
9609 /* Clicks on the handle are always reported as occurring at the top of
9610 the handle. */
9611 if (emacs_event->part == scroll_bar_handle)
9612 emacs_event->x = bar->start;
9613 else
9614 XSETINT (emacs_event->x, y);
9615 #else
9616 XSETINT (emacs_event->x, y);
9617 #endif
9618
9619 XSETINT (emacs_event->y, top_range);
9620 }
9621 }
9622
9623 /* Handle some mouse motion while someone is dragging the scroll bar.
9624
9625 This may be called from a signal handler, so we have to ignore GC
9626 mark bits. */
9627
9628 static void
9629 x_scroll_bar_note_movement (bar, event)
9630 struct scroll_bar *bar;
9631 XEvent *event;
9632 {
9633 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9634
9635 last_mouse_movement_time = event->xmotion.time;
9636
9637 f->mouse_moved = 1;
9638 XSETVECTOR (last_mouse_scroll_bar, bar);
9639
9640 /* If we're dragging the bar, display it. */
9641 if (! GC_NILP (bar->dragging))
9642 {
9643 /* Where should the handle be now? */
9644 int new_start = event->xmotion.y - XINT (bar->dragging);
9645
9646 if (new_start != XINT (bar->start))
9647 {
9648 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9649
9650 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9651 }
9652 }
9653 }
9654
9655 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9656
9657 /* Return information to the user about the current position of the mouse
9658 on the scroll bar. */
9659
9660 static void
9661 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9662 FRAME_PTR *fp;
9663 Lisp_Object *bar_window;
9664 enum scroll_bar_part *part;
9665 Lisp_Object *x, *y;
9666 unsigned long *time;
9667 {
9668 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
9669 Window w = SCROLL_BAR_X_WINDOW (bar);
9670 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9671 int win_x, win_y;
9672 Window dummy_window;
9673 int dummy_coord;
9674 unsigned int dummy_mask;
9675
9676 BLOCK_INPUT;
9677
9678 /* Get the mouse's position relative to the scroll bar window, and
9679 report that. */
9680 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
9681
9682 /* Root, child, root x and root y. */
9683 &dummy_window, &dummy_window,
9684 &dummy_coord, &dummy_coord,
9685
9686 /* Position relative to scroll bar. */
9687 &win_x, &win_y,
9688
9689 /* Mouse buttons and modifier keys. */
9690 &dummy_mask))
9691 ;
9692 else
9693 {
9694 #if 0
9695 int inside_height
9696 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9697 #endif
9698 int top_range
9699 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9700
9701 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9702
9703 if (! NILP (bar->dragging))
9704 win_y -= XINT (bar->dragging);
9705
9706 if (win_y < 0)
9707 win_y = 0;
9708 if (win_y > top_range)
9709 win_y = top_range;
9710
9711 *fp = f;
9712 *bar_window = bar->window;
9713
9714 if (! NILP (bar->dragging))
9715 *part = scroll_bar_handle;
9716 else if (win_y < XINT (bar->start))
9717 *part = scroll_bar_above_handle;
9718 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9719 *part = scroll_bar_handle;
9720 else
9721 *part = scroll_bar_below_handle;
9722
9723 XSETINT (*x, win_y);
9724 XSETINT (*y, top_range);
9725
9726 f->mouse_moved = 0;
9727 last_mouse_scroll_bar = Qnil;
9728 }
9729
9730 *time = last_mouse_movement_time;
9731
9732 UNBLOCK_INPUT;
9733 }
9734
9735
9736 /* The screen has been cleared so we may have changed foreground or
9737 background colors, and the scroll bars may need to be redrawn.
9738 Clear out the scroll bars, and ask for expose events, so we can
9739 redraw them. */
9740
9741 void
9742 x_scroll_bar_clear (f)
9743 FRAME_PTR f;
9744 {
9745 #ifndef USE_TOOLKIT_SCROLL_BARS
9746 Lisp_Object bar;
9747
9748 /* We can have scroll bars even if this is 0,
9749 if we just turned off scroll bar mode.
9750 But in that case we should not clear them. */
9751 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9752 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9753 bar = XSCROLL_BAR (bar)->next)
9754 XClearArea (FRAME_X_DISPLAY (f),
9755 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9756 0, 0, 0, 0, True);
9757 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9758 }
9759
9760 /* This processes Expose events from the menu-bar specific X event
9761 loop in xmenu.c. This allows to redisplay the frame if necessary
9762 when handling menu-bar or pop-up items. */
9763
9764 int
9765 process_expose_from_menu (event)
9766 XEvent event;
9767 {
9768 FRAME_PTR f;
9769 struct x_display_info *dpyinfo;
9770 int frame_exposed_p = 0;
9771
9772 BLOCK_INPUT;
9773
9774 dpyinfo = x_display_info_for_display (event.xexpose.display);
9775 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9776 if (f)
9777 {
9778 if (f->async_visible == 0)
9779 {
9780 f->async_visible = 1;
9781 f->async_iconified = 0;
9782 f->output_data.x->has_been_visible = 1;
9783 SET_FRAME_GARBAGED (f);
9784 }
9785 else
9786 {
9787 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9788 event.xexpose.x, event.xexpose.y,
9789 event.xexpose.width, event.xexpose.height);
9790 frame_exposed_p = 1;
9791 }
9792 }
9793 else
9794 {
9795 struct scroll_bar *bar
9796 = x_window_to_scroll_bar (event.xexpose.window);
9797
9798 if (bar)
9799 x_scroll_bar_expose (bar, &event);
9800 }
9801
9802 UNBLOCK_INPUT;
9803 return frame_exposed_p;
9804 }
9805 \f
9806 /* Define a queue to save up SelectionRequest events for later handling. */
9807
9808 struct selection_event_queue
9809 {
9810 XEvent event;
9811 struct selection_event_queue *next;
9812 };
9813
9814 static struct selection_event_queue *queue;
9815
9816 /* Nonzero means queue up certain events--don't process them yet. */
9817
9818 static int x_queue_selection_requests;
9819
9820 /* Queue up an X event *EVENT, to be processed later. */
9821
9822 static void
9823 x_queue_event (f, event)
9824 FRAME_PTR f;
9825 XEvent *event;
9826 {
9827 struct selection_event_queue *queue_tmp
9828 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
9829
9830 if (queue_tmp != NULL)
9831 {
9832 queue_tmp->event = *event;
9833 queue_tmp->next = queue;
9834 queue = queue_tmp;
9835 }
9836 }
9837
9838 /* Take all the queued events and put them back
9839 so that they get processed afresh. */
9840
9841 static void
9842 x_unqueue_events (display)
9843 Display *display;
9844 {
9845 while (queue != NULL)
9846 {
9847 struct selection_event_queue *queue_tmp = queue;
9848 XPutBackEvent (display, &queue_tmp->event);
9849 queue = queue_tmp->next;
9850 xfree ((char *)queue_tmp);
9851 }
9852 }
9853
9854 /* Start queuing SelectionRequest events. */
9855
9856 void
9857 x_start_queuing_selection_requests (display)
9858 Display *display;
9859 {
9860 x_queue_selection_requests++;
9861 }
9862
9863 /* Stop queuing SelectionRequest events. */
9864
9865 void
9866 x_stop_queuing_selection_requests (display)
9867 Display *display;
9868 {
9869 x_queue_selection_requests--;
9870 x_unqueue_events (display);
9871 }
9872 \f
9873 /* The main X event-reading loop - XTread_socket. */
9874
9875 #if 0
9876 /* Time stamp of enter window event. This is only used by XTread_socket,
9877 but we have to put it out here, since static variables within functions
9878 sometimes don't work. */
9879
9880 static Time enter_timestamp;
9881 #endif
9882
9883 /* This holds the state XLookupString needs to implement dead keys
9884 and other tricks known as "compose processing". _X Window System_
9885 says that a portable program can't use this, but Stephen Gildea assures
9886 me that letting the compiler initialize it to zeros will work okay.
9887
9888 This must be defined outside of XTread_socket, for the same reasons
9889 given for enter_timestamp, above. */
9890
9891 static XComposeStatus compose_status;
9892
9893 /* Record the last 100 characters stored
9894 to help debug the loss-of-chars-during-GC problem. */
9895
9896 static int temp_index;
9897 static short temp_buffer[100];
9898
9899 /* Set this to nonzero to fake an "X I/O error"
9900 on a particular display. */
9901
9902 struct x_display_info *XTread_socket_fake_io_error;
9903
9904 /* When we find no input here, we occasionally do a no-op command
9905 to verify that the X server is still running and we can still talk with it.
9906 We try all the open displays, one by one.
9907 This variable is used for cycling thru the displays. */
9908
9909 static struct x_display_info *next_noop_dpyinfo;
9910
9911 #define SET_SAVED_MENU_EVENT(size) \
9912 do \
9913 { \
9914 if (f->output_data.x->saved_menu_event == 0) \
9915 f->output_data.x->saved_menu_event \
9916 = (XEvent *) xmalloc (sizeof (XEvent)); \
9917 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9918 if (numchars >= 1) \
9919 { \
9920 bufp->kind = menu_bar_activate_event; \
9921 XSETFRAME (bufp->frame_or_window, f); \
9922 bufp->arg = Qnil; \
9923 bufp++; \
9924 count++; \
9925 numchars--; \
9926 } \
9927 } \
9928 while (0)
9929
9930 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9931 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9932
9933 /* Read events coming from the X server.
9934 This routine is called by the SIGIO handler.
9935 We return as soon as there are no more events to be read.
9936
9937 Events representing keys are stored in buffer BUFP,
9938 which can hold up to NUMCHARS characters.
9939 We return the number of characters stored into the buffer,
9940 thus pretending to be `read'.
9941
9942 EXPECTED is nonzero if the caller knows input is available. */
9943
9944 int
9945 XTread_socket (sd, bufp, numchars, expected)
9946 register int sd;
9947 /* register */ struct input_event *bufp;
9948 /* register */ int numchars;
9949 int expected;
9950 {
9951 int count = 0;
9952 int nbytes = 0;
9953 XEvent event;
9954 struct frame *f;
9955 int event_found = 0;
9956 struct x_display_info *dpyinfo;
9957 struct coding_system coding;
9958
9959 if (interrupt_input_blocked)
9960 {
9961 interrupt_input_pending = 1;
9962 return -1;
9963 }
9964
9965 interrupt_input_pending = 0;
9966 BLOCK_INPUT;
9967
9968 /* So people can tell when we have read the available input. */
9969 input_signal_count++;
9970
9971 if (numchars <= 0)
9972 abort (); /* Don't think this happens. */
9973
9974 ++handling_signal;
9975
9976 /* The input should be decoded if it is from XIM. Currently the
9977 locale of XIM is the same as that of the system. So, we can use
9978 Vlocale_coding_system which is initialized properly at Emacs
9979 startup time. */
9980 setup_coding_system (Vlocale_coding_system, &coding);
9981 coding.src_multibyte = 0;
9982 coding.dst_multibyte = 1;
9983 /* The input is converted to events, thus we can't handle
9984 composition. Anyway, there's no XIM that gives us composition
9985 information. */
9986 coding.common_flags &= ~CODING_ANNOTATION_MASK;
9987
9988 /* Find the display we are supposed to read input for.
9989 It's the one communicating on descriptor SD. */
9990 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9991 {
9992 #if 0 /* This ought to be unnecessary; let's verify it. */
9993 #ifdef FIOSNBIO
9994 /* If available, Xlib uses FIOSNBIO to make the socket
9995 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
9996 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
9997 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
9998 fcntl (dpyinfo->connection, F_SETFL, 0);
9999 #endif /* ! defined (FIOSNBIO) */
10000 #endif
10001
10002 #if 0 /* This code can't be made to work, with multiple displays,
10003 and appears not to be used on any system any more.
10004 Also keyboard.c doesn't turn O_NDELAY on and off
10005 for X connections. */
10006 #ifndef SIGIO
10007 #ifndef HAVE_SELECT
10008 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
10009 {
10010 extern int read_alarm_should_throw;
10011 read_alarm_should_throw = 1;
10012 XPeekEvent (dpyinfo->display, &event);
10013 read_alarm_should_throw = 0;
10014 }
10015 #endif /* HAVE_SELECT */
10016 #endif /* SIGIO */
10017 #endif
10018
10019 /* For debugging, this gives a way to fake an I/O error. */
10020 if (dpyinfo == XTread_socket_fake_io_error)
10021 {
10022 XTread_socket_fake_io_error = 0;
10023 x_io_error_quitter (dpyinfo->display);
10024 }
10025
10026 while (XPending (dpyinfo->display))
10027 {
10028 XNextEvent (dpyinfo->display, &event);
10029
10030 #ifdef HAVE_X_I18N
10031 {
10032 /* Filter events for the current X input method.
10033 XFilterEvent returns non-zero if the input method has
10034 consumed the event. We pass the frame's X window to
10035 XFilterEvent because that's the one for which the IC
10036 was created. */
10037 struct frame *f1 = x_any_window_to_frame (dpyinfo,
10038 event.xclient.window);
10039 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
10040 break;
10041 }
10042 #endif
10043 event_found = 1;
10044
10045 switch (event.type)
10046 {
10047 case ClientMessage:
10048 {
10049 if (event.xclient.message_type
10050 == dpyinfo->Xatom_wm_protocols
10051 && event.xclient.format == 32)
10052 {
10053 if (event.xclient.data.l[0]
10054 == dpyinfo->Xatom_wm_take_focus)
10055 {
10056 /* Use x_any_window_to_frame because this
10057 could be the shell widget window
10058 if the frame has no title bar. */
10059 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
10060 #ifdef HAVE_X_I18N
10061 /* Not quite sure this is needed -pd */
10062 if (f && FRAME_XIC (f))
10063 XSetICFocus (FRAME_XIC (f));
10064 #endif
10065 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
10066 instructs the WM to set the input focus automatically for
10067 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
10068 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
10069 it has set the focus. So, XSetInputFocus below is not
10070 needed.
10071
10072 The call to XSetInputFocus below has also caused trouble. In
10073 cases where the XSetInputFocus done by the WM and the one
10074 below are temporally close (on a fast machine), the call
10075 below can generate additional FocusIn events which confuse
10076 Emacs. */
10077
10078 /* Since we set WM_TAKE_FOCUS, we must call
10079 XSetInputFocus explicitly. But not if f is null,
10080 since that might be an event for a deleted frame. */
10081 if (f)
10082 {
10083 Display *d = event.xclient.display;
10084 /* Catch and ignore errors, in case window has been
10085 iconified by a window manager such as GWM. */
10086 int count = x_catch_errors (d);
10087 XSetInputFocus (d, event.xclient.window,
10088 /* The ICCCM says this is
10089 the only valid choice. */
10090 RevertToParent,
10091 event.xclient.data.l[1]);
10092 /* This is needed to detect the error
10093 if there is an error. */
10094 XSync (d, False);
10095 x_uncatch_errors (d, count);
10096 }
10097 /* Not certain about handling scroll bars here */
10098 #endif /* 0 */
10099 }
10100 else if (event.xclient.data.l[0]
10101 == dpyinfo->Xatom_wm_save_yourself)
10102 {
10103 /* Save state modify the WM_COMMAND property to
10104 something which can reinstate us. This notifies
10105 the session manager, who's looking for such a
10106 PropertyNotify. Can restart processing when
10107 a keyboard or mouse event arrives. */
10108 if (numchars > 0)
10109 {
10110 f = x_top_window_to_frame (dpyinfo,
10111 event.xclient.window);
10112
10113 /* This is just so we only give real data once
10114 for a single Emacs process. */
10115 if (f == SELECTED_FRAME ())
10116 XSetCommand (FRAME_X_DISPLAY (f),
10117 event.xclient.window,
10118 initial_argv, initial_argc);
10119 else if (f)
10120 XSetCommand (FRAME_X_DISPLAY (f),
10121 event.xclient.window,
10122 0, 0);
10123 }
10124 }
10125 else if (event.xclient.data.l[0]
10126 == dpyinfo->Xatom_wm_delete_window)
10127 {
10128 struct frame *f
10129 = x_any_window_to_frame (dpyinfo,
10130 event.xclient.window);
10131
10132 if (f)
10133 {
10134 if (numchars == 0)
10135 abort ();
10136
10137 bufp->kind = delete_window_event;
10138 XSETFRAME (bufp->frame_or_window, f);
10139 bufp->arg = Qnil;
10140 bufp++;
10141
10142 count += 1;
10143 numchars -= 1;
10144 }
10145 }
10146 }
10147 else if (event.xclient.message_type
10148 == dpyinfo->Xatom_wm_configure_denied)
10149 {
10150 }
10151 else if (event.xclient.message_type
10152 == dpyinfo->Xatom_wm_window_moved)
10153 {
10154 int new_x, new_y;
10155 struct frame *f
10156 = x_window_to_frame (dpyinfo, event.xclient.window);
10157
10158 new_x = event.xclient.data.s[0];
10159 new_y = event.xclient.data.s[1];
10160
10161 if (f)
10162 {
10163 f->output_data.x->left_pos = new_x;
10164 f->output_data.x->top_pos = new_y;
10165 }
10166 }
10167 #ifdef HACK_EDITRES
10168 else if (event.xclient.message_type
10169 == dpyinfo->Xatom_editres)
10170 {
10171 struct frame *f
10172 = x_any_window_to_frame (dpyinfo, event.xclient.window);
10173 _XEditResCheckMessages (f->output_data.x->widget, NULL,
10174 &event, NULL);
10175 }
10176 #endif /* HACK_EDITRES */
10177 else if ((event.xclient.message_type
10178 == dpyinfo->Xatom_DONE)
10179 || (event.xclient.message_type
10180 == dpyinfo->Xatom_PAGE))
10181 {
10182 /* Ghostview job completed. Kill it. We could
10183 reply with "Next" if we received "Page", but we
10184 currently never do because we are interested in
10185 images, only, which should have 1 page. */
10186 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
10187 struct frame *f
10188 = x_window_to_frame (dpyinfo, event.xclient.window);
10189 x_kill_gs_process (pixmap, f);
10190 expose_frame (f, 0, 0, 0, 0);
10191 }
10192 #ifdef USE_TOOLKIT_SCROLL_BARS
10193 /* Scroll bar callbacks send a ClientMessage from which
10194 we construct an input_event. */
10195 else if (event.xclient.message_type
10196 == dpyinfo->Xatom_Scrollbar)
10197 {
10198 x_scroll_bar_to_input_event (&event, bufp);
10199 ++bufp, ++count, --numchars;
10200 goto out;
10201 }
10202 #endif /* USE_TOOLKIT_SCROLL_BARS */
10203 else
10204 goto OTHER;
10205 }
10206 break;
10207
10208 case SelectionNotify:
10209 #ifdef USE_X_TOOLKIT
10210 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
10211 goto OTHER;
10212 #endif /* not USE_X_TOOLKIT */
10213 x_handle_selection_notify (&event.xselection);
10214 break;
10215
10216 case SelectionClear: /* Someone has grabbed ownership. */
10217 #ifdef USE_X_TOOLKIT
10218 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
10219 goto OTHER;
10220 #endif /* USE_X_TOOLKIT */
10221 {
10222 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
10223
10224 if (numchars == 0)
10225 abort ();
10226
10227 bufp->kind = selection_clear_event;
10228 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10229 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10230 SELECTION_EVENT_TIME (bufp) = eventp->time;
10231 bufp->frame_or_window = Qnil;
10232 bufp->arg = Qnil;
10233 bufp++;
10234
10235 count += 1;
10236 numchars -= 1;
10237 }
10238 break;
10239
10240 case SelectionRequest: /* Someone wants our selection. */
10241 #ifdef USE_X_TOOLKIT
10242 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
10243 goto OTHER;
10244 #endif /* USE_X_TOOLKIT */
10245 if (x_queue_selection_requests)
10246 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
10247 &event);
10248 else
10249 {
10250 XSelectionRequestEvent *eventp
10251 = (XSelectionRequestEvent *) &event;
10252
10253 if (numchars == 0)
10254 abort ();
10255
10256 bufp->kind = selection_request_event;
10257 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10258 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
10259 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10260 SELECTION_EVENT_TARGET (bufp) = eventp->target;
10261 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
10262 SELECTION_EVENT_TIME (bufp) = eventp->time;
10263 bufp->frame_or_window = Qnil;
10264 bufp->arg = Qnil;
10265 bufp++;
10266
10267 count += 1;
10268 numchars -= 1;
10269 }
10270 break;
10271
10272 case PropertyNotify:
10273 #if 0 /* This is plain wrong. In the case that we are waiting for a
10274 PropertyNotify used as an ACK in incremental selection
10275 transfer, the property will be on the receiver's window. */
10276 #if defined USE_X_TOOLKIT
10277 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
10278 goto OTHER;
10279 #endif
10280 #endif
10281 x_handle_property_notify (&event.xproperty);
10282 goto OTHER;
10283
10284 case ReparentNotify:
10285 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
10286 if (f)
10287 {
10288 int x, y;
10289 f->output_data.x->parent_desc = event.xreparent.parent;
10290 x_real_positions (f, &x, &y);
10291 f->output_data.x->left_pos = x;
10292 f->output_data.x->top_pos = y;
10293 }
10294 break;
10295
10296 case Expose:
10297 f = x_window_to_frame (dpyinfo, event.xexpose.window);
10298 if (f)
10299 {
10300 x_check_fullscreen (f);
10301
10302 if (f->async_visible == 0)
10303 {
10304 f->async_visible = 1;
10305 f->async_iconified = 0;
10306 f->output_data.x->has_been_visible = 1;
10307 SET_FRAME_GARBAGED (f);
10308 }
10309 else
10310 expose_frame (x_window_to_frame (dpyinfo,
10311 event.xexpose.window),
10312 event.xexpose.x, event.xexpose.y,
10313 event.xexpose.width, event.xexpose.height);
10314 }
10315 else
10316 {
10317 #ifndef USE_TOOLKIT_SCROLL_BARS
10318 struct scroll_bar *bar;
10319 #endif
10320 #if defined USE_LUCID
10321 /* Submenus of the Lucid menu bar aren't widgets
10322 themselves, so there's no way to dispatch events
10323 to them. Recognize this case separately. */
10324 {
10325 Widget widget
10326 = x_window_to_menu_bar (event.xexpose.window);
10327 if (widget)
10328 xlwmenu_redisplay (widget);
10329 }
10330 #endif /* USE_LUCID */
10331
10332 #ifdef USE_TOOLKIT_SCROLL_BARS
10333 /* Dispatch event to the widget. */
10334 goto OTHER;
10335 #else /* not USE_TOOLKIT_SCROLL_BARS */
10336 bar = x_window_to_scroll_bar (event.xexpose.window);
10337
10338 if (bar)
10339 x_scroll_bar_expose (bar, &event);
10340 #ifdef USE_X_TOOLKIT
10341 else
10342 goto OTHER;
10343 #endif /* USE_X_TOOLKIT */
10344 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10345 }
10346 break;
10347
10348 case GraphicsExpose: /* This occurs when an XCopyArea's
10349 source area was obscured or not
10350 available. */
10351 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
10352 if (f)
10353 {
10354 expose_frame (f,
10355 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
10356 event.xgraphicsexpose.width,
10357 event.xgraphicsexpose.height);
10358 }
10359 #ifdef USE_X_TOOLKIT
10360 else
10361 goto OTHER;
10362 #endif /* USE_X_TOOLKIT */
10363 break;
10364
10365 case NoExpose: /* This occurs when an XCopyArea's
10366 source area was completely
10367 available. */
10368 break;
10369
10370 case UnmapNotify:
10371 /* Redo the mouse-highlight after the tooltip has gone. */
10372 if (event.xmap.window == tip_window)
10373 {
10374 tip_window = 0;
10375 redo_mouse_highlight ();
10376 }
10377
10378 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
10379 if (f) /* F may no longer exist if
10380 the frame was deleted. */
10381 {
10382 /* While a frame is unmapped, display generation is
10383 disabled; you don't want to spend time updating a
10384 display that won't ever be seen. */
10385 f->async_visible = 0;
10386 /* We can't distinguish, from the event, whether the window
10387 has become iconified or invisible. So assume, if it
10388 was previously visible, than now it is iconified.
10389 But x_make_frame_invisible clears both
10390 the visible flag and the iconified flag;
10391 and that way, we know the window is not iconified now. */
10392 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
10393 {
10394 f->async_iconified = 1;
10395
10396 bufp->kind = iconify_event;
10397 XSETFRAME (bufp->frame_or_window, f);
10398 bufp->arg = Qnil;
10399 bufp++;
10400 count++;
10401 numchars--;
10402 }
10403 }
10404 goto OTHER;
10405
10406 case MapNotify:
10407 if (event.xmap.window == tip_window)
10408 /* The tooltip has been drawn already. Avoid
10409 the SET_FRAME_GARBAGED below. */
10410 goto OTHER;
10411
10412 /* We use x_top_window_to_frame because map events can
10413 come for sub-windows and they don't mean that the
10414 frame is visible. */
10415 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
10416 if (f)
10417 {
10418 f->async_visible = 1;
10419 f->async_iconified = 0;
10420 f->output_data.x->has_been_visible = 1;
10421
10422 /* wait_reading_process_input will notice this and update
10423 the frame's display structures. */
10424 SET_FRAME_GARBAGED (f);
10425
10426 if (f->iconified)
10427 {
10428 bufp->kind = deiconify_event;
10429 XSETFRAME (bufp->frame_or_window, f);
10430 bufp->arg = Qnil;
10431 bufp++;
10432 count++;
10433 numchars--;
10434 }
10435 else if (! NILP (Vframe_list)
10436 && ! NILP (XCDR (Vframe_list)))
10437 /* Force a redisplay sooner or later
10438 to update the frame titles
10439 in case this is the second frame. */
10440 record_asynch_buffer_change ();
10441 }
10442 goto OTHER;
10443
10444 case KeyPress:
10445 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
10446
10447 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
10448 {
10449 dpyinfo->mouse_face_hidden = 1;
10450 clear_mouse_face (dpyinfo);
10451 }
10452
10453 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
10454 if (f == 0)
10455 {
10456 /* Scroll bars consume key events, but we want
10457 the keys to go to the scroll bar's frame. */
10458 Widget widget = XtWindowToWidget (dpyinfo->display,
10459 event.xkey.window);
10460 if (widget && XmIsScrollBar (widget))
10461 {
10462 widget = XtParent (widget);
10463 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
10464 }
10465 }
10466 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
10467
10468 if (f != 0)
10469 {
10470 KeySym keysym, orig_keysym;
10471 /* al%imercury@uunet.uu.net says that making this 81
10472 instead of 80 fixed a bug whereby meta chars made
10473 his Emacs hang.
10474
10475 It seems that some version of XmbLookupString has
10476 a bug of not returning XBufferOverflow in
10477 status_return even if the input is too long to
10478 fit in 81 bytes. So, we must prepare sufficient
10479 bytes for copy_buffer. 513 bytes (256 chars for
10480 two-byte character set) seems to be a fairly good
10481 approximation. -- 2000.8.10 handa@etl.go.jp */
10482 unsigned char copy_buffer[513];
10483 unsigned char *copy_bufptr = copy_buffer;
10484 int copy_bufsiz = sizeof (copy_buffer);
10485 int modifiers;
10486
10487 event.xkey.state
10488 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
10489 extra_keyboard_modifiers);
10490 modifiers = event.xkey.state;
10491
10492 /* This will have to go some day... */
10493
10494 /* make_lispy_event turns chars into control chars.
10495 Don't do it here because XLookupString is too eager. */
10496 event.xkey.state &= ~ControlMask;
10497 event.xkey.state &= ~(dpyinfo->meta_mod_mask
10498 | dpyinfo->super_mod_mask
10499 | dpyinfo->hyper_mod_mask
10500 | dpyinfo->alt_mod_mask);
10501
10502 /* In case Meta is ComposeCharacter,
10503 clear its status. According to Markus Ehrnsperger
10504 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
10505 this enables ComposeCharacter to work whether or
10506 not it is combined with Meta. */
10507 if (modifiers & dpyinfo->meta_mod_mask)
10508 bzero (&compose_status, sizeof (compose_status));
10509
10510 #ifdef HAVE_X_I18N
10511 if (FRAME_XIC (f))
10512 {
10513 Status status_return;
10514
10515 nbytes = XmbLookupString (FRAME_XIC (f),
10516 &event.xkey, copy_bufptr,
10517 copy_bufsiz, &keysym,
10518 &status_return);
10519 if (status_return == XBufferOverflow)
10520 {
10521 copy_bufsiz = nbytes + 1;
10522 copy_bufptr = (char *) alloca (copy_bufsiz);
10523 nbytes = XmbLookupString (FRAME_XIC (f),
10524 &event.xkey, copy_bufptr,
10525 copy_bufsiz, &keysym,
10526 &status_return);
10527 }
10528
10529 if (status_return == XLookupNone)
10530 break;
10531 else if (status_return == XLookupChars)
10532 {
10533 keysym = NoSymbol;
10534 modifiers = 0;
10535 }
10536 else if (status_return != XLookupKeySym
10537 && status_return != XLookupBoth)
10538 abort ();
10539 }
10540 else
10541 nbytes = XLookupString (&event.xkey, copy_bufptr,
10542 copy_bufsiz, &keysym,
10543 &compose_status);
10544 #else
10545 nbytes = XLookupString (&event.xkey, copy_bufptr,
10546 copy_bufsiz, &keysym,
10547 &compose_status);
10548 #endif
10549
10550 orig_keysym = keysym;
10551
10552 if (numchars > 1)
10553 {
10554 Lisp_Object c;
10555
10556 /* First deal with keysyms which have defined
10557 translations to characters. */
10558 if (keysym >= 32 && keysym < 128)
10559 /* Avoid explicitly decoding each ASCII character. */
10560 {
10561 bufp->kind = ascii_keystroke;
10562 bufp->code = keysym;
10563 XSETFRAME (bufp->frame_or_window, f);
10564 bufp->arg = Qnil;
10565 bufp->modifiers
10566 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10567 modifiers);
10568 bufp->timestamp = event.xkey.time;
10569 bufp++;
10570 count++;
10571 numchars--;
10572 }
10573 /* Now non-ASCII. */
10574 else if (HASH_TABLE_P (Vx_keysym_table)
10575 && (CHARACTERP (c = Fgethash (make_number (keysym),
10576 Vx_keysym_table,
10577 Qnil))))
10578 {
10579 bufp->kind = (ASCII_CHAR_P (XFASTINT (c))
10580 ? ascii_keystroke
10581 : multibyte_char_keystroke);
10582 bufp->code = XFASTINT (c);
10583 XSETFRAME (bufp->frame_or_window, f);
10584 bufp->arg = Qnil;
10585 bufp->modifiers
10586 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10587 modifiers);
10588 bufp->timestamp = event.xkey.time;
10589 bufp++;
10590 count++;
10591 numchars--;
10592 }
10593 else if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
10594 || keysym == XK_Delete
10595 #ifdef XK_ISO_Left_Tab
10596 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
10597 #endif
10598 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
10599 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
10600 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
10601 #ifdef HPUX
10602 /* This recognizes the "extended function
10603 keys". It seems there's no cleaner way.
10604 Test IsModifierKey to avoid handling
10605 mode_switch incorrectly. */
10606 || ((unsigned) (keysym) >= XK_Select
10607 && (unsigned)(keysym) < XK_KP_Space)
10608 #endif
10609 #ifdef XK_dead_circumflex
10610 || orig_keysym == XK_dead_circumflex
10611 #endif
10612 #ifdef XK_dead_grave
10613 || orig_keysym == XK_dead_grave
10614 #endif
10615 #ifdef XK_dead_tilde
10616 || orig_keysym == XK_dead_tilde
10617 #endif
10618 #ifdef XK_dead_diaeresis
10619 || orig_keysym == XK_dead_diaeresis
10620 #endif
10621 #ifdef XK_dead_macron
10622 || orig_keysym == XK_dead_macron
10623 #endif
10624 #ifdef XK_dead_degree
10625 || orig_keysym == XK_dead_degree
10626 #endif
10627 #ifdef XK_dead_acute
10628 || orig_keysym == XK_dead_acute
10629 #endif
10630 #ifdef XK_dead_cedilla
10631 || orig_keysym == XK_dead_cedilla
10632 #endif
10633 #ifdef XK_dead_breve
10634 || orig_keysym == XK_dead_breve
10635 #endif
10636 #ifdef XK_dead_ogonek
10637 || orig_keysym == XK_dead_ogonek
10638 #endif
10639 #ifdef XK_dead_caron
10640 || orig_keysym == XK_dead_caron
10641 #endif
10642 #ifdef XK_dead_doubleacute
10643 || orig_keysym == XK_dead_doubleacute
10644 #endif
10645 #ifdef XK_dead_abovedot
10646 || orig_keysym == XK_dead_abovedot
10647 #endif
10648 #ifdef XK_dead_abovering
10649 || orig_keysym == XK_dead_abovering
10650 #endif
10651 #ifdef XK_dead_iota
10652 || orig_keysym == XK_dead_iota
10653 #endif
10654 #ifdef XK_dead_belowdot
10655 || orig_keysym == XK_dead_belowdot
10656 #endif
10657 #ifdef XK_dead_voiced_sound
10658 || orig_keysym == XK_dead_voiced_sound
10659 #endif
10660 #ifdef XK_dead_semivoiced_sound
10661 || orig_keysym == XK_dead_semivoiced_sound
10662 #endif
10663 #ifdef XK_dead_hook
10664 || orig_keysym == XK_dead_hook
10665 #endif
10666 #ifdef XK_dead_horn
10667 || orig_keysym == XK_dead_horn
10668 #endif
10669 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10670 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10671 /* Any "vendor-specific" key is ok. */
10672 || (orig_keysym & (1 << 28))
10673 || (keysym != NoSymbol && nbytes == 0))
10674 && ! (IsModifierKey (orig_keysym)
10675 #ifndef HAVE_X11R5
10676 #ifdef XK_Mode_switch
10677 || ((unsigned)(orig_keysym) == XK_Mode_switch)
10678 #endif
10679 #ifdef XK_Num_Lock
10680 || ((unsigned)(orig_keysym) == XK_Num_Lock)
10681 #endif
10682 #endif /* not HAVE_X11R5 */
10683 /* The symbols from XK_ISO_Lock to
10684 XK_ISO_Last_Group_Lock doesn't have
10685 real modifiers but should be treated
10686 similarly to Mode_switch by Emacs. */
10687 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
10688 || ((unsigned)(orig_keysym) >= XK_ISO_Lock
10689 && (unsigned)(orig_keysym) <= XK_ISO_Last_Group_Lock)
10690 #endif
10691 ))
10692 {
10693 if (temp_index == sizeof temp_buffer / sizeof (short))
10694 temp_index = 0;
10695 temp_buffer[temp_index++] = keysym;
10696 /* make_lispy_event will convert this to a symbolic
10697 key. */
10698 bufp->kind = non_ascii_keystroke;
10699 bufp->code = keysym;
10700 XSETFRAME (bufp->frame_or_window, f);
10701 bufp->arg = Qnil;
10702 bufp->modifiers
10703 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10704 modifiers);
10705 bufp->timestamp = event.xkey.time;
10706 bufp++;
10707 count++;
10708 numchars--;
10709 }
10710 else if (numchars > nbytes)
10711 { /* Raw characters, not keysym. */
10712 register int i;
10713 register int c;
10714 int nchars, len;
10715
10716 for (i = 0; i < nbytes; i++)
10717 {
10718 if (temp_index == (sizeof temp_buffer
10719 / sizeof (short)))
10720 temp_index = 0;
10721 temp_buffer[temp_index++] = copy_bufptr[i];
10722 }
10723
10724 {
10725 /* Decode the input data. */
10726 coding.destination
10727 = (unsigned char *) malloc (nbytes);
10728 if (! coding.destination)
10729 break;
10730 coding.dst_bytes = nbytes;
10731 coding.mode |= CODING_MODE_LAST_BLOCK;
10732 decode_coding_c_string (&coding, copy_bufptr,
10733 nbytes, Qnil);
10734 nbytes = coding.produced;
10735 nchars = coding.produced_char;
10736 if (copy_bufsiz < nbytes)
10737 {
10738 copy_bufsiz = nbytes;
10739 copy_bufptr = (char *) alloca (nbytes);
10740 }
10741 bcopy (coding.destination, copy_bufptr, nbytes);
10742 free (coding.destination);
10743 }
10744
10745 /* Convert the input data to a sequence of
10746 character events. */
10747 for (i = 0; i < nbytes; i += len)
10748 {
10749 if (nchars == nbytes)
10750 c = copy_bufptr[i], len = 1;
10751 else
10752 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10753 nbytes - i, len);
10754
10755 bufp->kind = (ASCII_CHAR_P (c)
10756 ? ascii_keystroke
10757 : multibyte_char_keystroke);
10758 bufp->code = c;
10759 XSETFRAME (bufp->frame_or_window, f);
10760 bufp->arg = Qnil;
10761 bufp->modifiers
10762 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10763 modifiers);
10764 bufp->timestamp = event.xkey.time;
10765 bufp++;
10766 }
10767
10768 count += nchars;
10769 numchars -= nchars;
10770
10771 if (keysym == NoSymbol)
10772 break;
10773 }
10774 else
10775 abort ();
10776 }
10777 else
10778 abort ();
10779 }
10780 #ifdef HAVE_X_I18N
10781 /* Don't dispatch this event since XtDispatchEvent calls
10782 XFilterEvent, and two calls in a row may freeze the
10783 client. */
10784 break;
10785 #else
10786 goto OTHER;
10787 #endif
10788
10789 case KeyRelease:
10790 #ifdef HAVE_X_I18N
10791 /* Don't dispatch this event since XtDispatchEvent calls
10792 XFilterEvent, and two calls in a row may freeze the
10793 client. */
10794 break;
10795 #else
10796 goto OTHER;
10797 #endif
10798
10799 /* Here's a possible interpretation of the whole
10800 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
10801 you get a FocusIn event, you have to get a FocusOut
10802 event before you relinquish the focus. If you
10803 haven't received a FocusIn event, then a mere
10804 LeaveNotify is enough to free you. */
10805
10806 case EnterNotify:
10807 {
10808 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10809
10810 #if 0
10811 if (event.xcrossing.focus)
10812 {
10813 /* Avoid nasty pop/raise loops. */
10814 if (f && (!(f->auto_raise)
10815 || !(f->auto_lower)
10816 || (event.xcrossing.time - enter_timestamp) > 500))
10817 {
10818 x_new_focus_frame (dpyinfo, f);
10819 enter_timestamp = event.xcrossing.time;
10820 }
10821 }
10822 else if (f == dpyinfo->x_focus_frame)
10823 x_new_focus_frame (dpyinfo, 0);
10824 #endif
10825
10826 /* EnterNotify counts as mouse movement,
10827 so update things that depend on mouse position. */
10828 if (f && !f->output_data.x->hourglass_p)
10829 note_mouse_movement (f, &event.xmotion);
10830 goto OTHER;
10831 }
10832
10833 case FocusIn:
10834 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10835 if (event.xfocus.detail != NotifyPointer)
10836 dpyinfo->x_focus_event_frame = f;
10837 if (f)
10838 {
10839 x_new_focus_frame (dpyinfo, f);
10840
10841 /* Don't stop displaying the initial startup message
10842 for a switch-frame event we don't need. */
10843 if (GC_NILP (Vterminal_frame)
10844 && GC_CONSP (Vframe_list)
10845 && !GC_NILP (XCDR (Vframe_list)))
10846 {
10847 bufp->kind = FOCUS_IN_EVENT;
10848 XSETFRAME (bufp->frame_or_window, f);
10849 bufp->arg = Qnil;
10850 ++bufp, ++count, --numchars;
10851 }
10852 }
10853
10854 #ifdef HAVE_X_I18N
10855 if (f && FRAME_XIC (f))
10856 XSetICFocus (FRAME_XIC (f));
10857 #endif
10858
10859 goto OTHER;
10860
10861 case LeaveNotify:
10862 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
10863 if (f)
10864 {
10865 if (f == dpyinfo->mouse_face_mouse_frame)
10866 {
10867 /* If we move outside the frame, then we're
10868 certainly no longer on any text in the frame. */
10869 clear_mouse_face (dpyinfo);
10870 dpyinfo->mouse_face_mouse_frame = 0;
10871 }
10872
10873 /* Generate a nil HELP_EVENT to cancel a help-echo.
10874 Do it only if there's something to cancel.
10875 Otherwise, the startup message is cleared when
10876 the mouse leaves the frame. */
10877 if (any_help_event_p)
10878 {
10879 Lisp_Object frame;
10880 int n;
10881
10882 XSETFRAME (frame, f);
10883 help_echo = Qnil;
10884 n = gen_help_event (bufp, numchars,
10885 Qnil, frame, Qnil, Qnil, 0);
10886 bufp += n, count += n, numchars -= n;
10887 }
10888
10889 #if 0
10890 if (event.xcrossing.focus)
10891 x_mouse_leave (dpyinfo);
10892 else
10893 {
10894 if (f == dpyinfo->x_focus_event_frame)
10895 dpyinfo->x_focus_event_frame = 0;
10896 if (f == dpyinfo->x_focus_frame)
10897 x_new_focus_frame (dpyinfo, 0);
10898 }
10899 #endif
10900 }
10901 goto OTHER;
10902
10903 case FocusOut:
10904 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10905 if (event.xfocus.detail != NotifyPointer
10906 && f == dpyinfo->x_focus_event_frame)
10907 dpyinfo->x_focus_event_frame = 0;
10908 if (f && f == dpyinfo->x_focus_frame)
10909 x_new_focus_frame (dpyinfo, 0);
10910
10911 #ifdef HAVE_X_I18N
10912 if (f && FRAME_XIC (f))
10913 XUnsetICFocus (FRAME_XIC (f));
10914 #endif
10915
10916 goto OTHER;
10917
10918 case MotionNotify:
10919 {
10920 previous_help_echo = help_echo;
10921 help_echo = help_echo_object = help_echo_window = Qnil;
10922 help_echo_pos = -1;
10923
10924 if (dpyinfo->grabbed && last_mouse_frame
10925 && FRAME_LIVE_P (last_mouse_frame))
10926 f = last_mouse_frame;
10927 else
10928 f = x_window_to_frame (dpyinfo, event.xmotion.window);
10929
10930 if (dpyinfo->mouse_face_hidden)
10931 {
10932 dpyinfo->mouse_face_hidden = 0;
10933 clear_mouse_face (dpyinfo);
10934 }
10935
10936 if (f)
10937 note_mouse_movement (f, &event.xmotion);
10938 else
10939 {
10940 #ifndef USE_TOOLKIT_SCROLL_BARS
10941 struct scroll_bar *bar
10942 = x_window_to_scroll_bar (event.xmotion.window);
10943
10944 if (bar)
10945 x_scroll_bar_note_movement (bar, &event);
10946 #endif /* USE_TOOLKIT_SCROLL_BARS */
10947
10948 /* If we move outside the frame, then we're
10949 certainly no longer on any text in the frame. */
10950 clear_mouse_face (dpyinfo);
10951 }
10952
10953 /* If the contents of the global variable help_echo
10954 has changed, generate a HELP_EVENT. */
10955 if (!NILP (help_echo)
10956 || !NILP (previous_help_echo))
10957 {
10958 Lisp_Object frame;
10959 int n;
10960
10961 if (f)
10962 XSETFRAME (frame, f);
10963 else
10964 frame = Qnil;
10965
10966 any_help_event_p = 1;
10967 n = gen_help_event (bufp, numchars, help_echo, frame,
10968 help_echo_window, help_echo_object,
10969 help_echo_pos);
10970 bufp += n, count += n, numchars -= n;
10971 }
10972
10973 goto OTHER;
10974 }
10975
10976 case ConfigureNotify:
10977 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10978 if (f)
10979 {
10980 #ifndef USE_X_TOOLKIT
10981 /* If there is a pending resize for fullscreen, don't
10982 do this one, the right one will come later.
10983 The toolkit version doesn't seem to need this, but we
10984 need to reset it below. */
10985 int dont_resize =
10986 ((f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
10987 && FRAME_NEW_WIDTH (f) != 0);
10988 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10989 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
10990 if (dont_resize)
10991 goto OTHER;
10992
10993 /* In the toolkit version, change_frame_size
10994 is called by the code that handles resizing
10995 of the EmacsFrame widget. */
10996
10997 /* Even if the number of character rows and columns has
10998 not changed, the font size may have changed, so we need
10999 to check the pixel dimensions as well. */
11000 if (columns != f->width
11001 || rows != f->height
11002 || event.xconfigure.width != f->output_data.x->pixel_width
11003 || event.xconfigure.height != f->output_data.x->pixel_height)
11004 {
11005 change_frame_size (f, rows, columns, 0, 1, 0);
11006 SET_FRAME_GARBAGED (f);
11007 cancel_mouse_face (f);
11008 }
11009 #endif
11010
11011 f->output_data.x->pixel_width = event.xconfigure.width;
11012 f->output_data.x->pixel_height = event.xconfigure.height;
11013
11014 /* What we have now is the position of Emacs's own window.
11015 Convert that to the position of the window manager window. */
11016 x_real_positions (f, &f->output_data.x->left_pos,
11017 &f->output_data.x->top_pos);
11018
11019 x_check_fullscreen_move(f);
11020 if (f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
11021 f->output_data.x->want_fullscreen &=
11022 ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
11023 #ifdef HAVE_X_I18N
11024 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
11025 xic_set_statusarea (f);
11026 #endif
11027
11028 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
11029 {
11030 /* Since the WM decorations come below top_pos now,
11031 we must put them below top_pos in the future. */
11032 f->output_data.x->win_gravity = NorthWestGravity;
11033 x_wm_set_size_hint (f, (long) 0, 0);
11034 }
11035 #ifdef USE_MOTIF
11036 /* Some window managers pass (0,0) as the location of
11037 the window, and the Motif event handler stores it
11038 in the emacs widget, which messes up Motif menus. */
11039 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
11040 {
11041 event.xconfigure.x = f->output_data.x->widget->core.x;
11042 event.xconfigure.y = f->output_data.x->widget->core.y;
11043 }
11044 #endif /* USE_MOTIF */
11045 }
11046 goto OTHER;
11047
11048 case ButtonPress:
11049 case ButtonRelease:
11050 {
11051 /* If we decide we want to generate an event to be seen
11052 by the rest of Emacs, we put it here. */
11053 struct input_event emacs_event;
11054 int tool_bar_p = 0;
11055
11056 emacs_event.kind = no_event;
11057 bzero (&compose_status, sizeof (compose_status));
11058
11059 if (dpyinfo->grabbed
11060 && last_mouse_frame
11061 && FRAME_LIVE_P (last_mouse_frame))
11062 f = last_mouse_frame;
11063 else
11064 f = x_window_to_frame (dpyinfo, event.xbutton.window);
11065
11066 if (f)
11067 {
11068 /* Is this in the tool-bar? */
11069 if (WINDOWP (f->tool_bar_window)
11070 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
11071 {
11072 Lisp_Object window;
11073 int p, x, y;
11074
11075 x = event.xbutton.x;
11076 y = event.xbutton.y;
11077
11078 /* Set x and y. */
11079 window = window_from_coordinates (f, x, y, &p, 1);
11080 if (EQ (window, f->tool_bar_window))
11081 {
11082 x_handle_tool_bar_click (f, &event.xbutton);
11083 tool_bar_p = 1;
11084 }
11085 }
11086
11087 if (!tool_bar_p)
11088 if (!dpyinfo->x_focus_frame
11089 || f == dpyinfo->x_focus_frame)
11090 construct_mouse_click (&emacs_event, &event, f);
11091 }
11092 else
11093 {
11094 #ifndef USE_TOOLKIT_SCROLL_BARS
11095 struct scroll_bar *bar
11096 = x_window_to_scroll_bar (event.xbutton.window);
11097
11098 if (bar)
11099 x_scroll_bar_handle_click (bar, &event, &emacs_event);
11100 #endif /* not USE_TOOLKIT_SCROLL_BARS */
11101 }
11102
11103 if (event.type == ButtonPress)
11104 {
11105 dpyinfo->grabbed |= (1 << event.xbutton.button);
11106 last_mouse_frame = f;
11107 /* Ignore any mouse motion that happened
11108 before this event; any subsequent mouse-movement
11109 Emacs events should reflect only motion after
11110 the ButtonPress. */
11111 if (f != 0)
11112 f->mouse_moved = 0;
11113
11114 if (!tool_bar_p)
11115 last_tool_bar_item = -1;
11116 }
11117 else
11118 {
11119 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
11120 }
11121
11122 if (numchars >= 1 && emacs_event.kind != no_event)
11123 {
11124 bcopy (&emacs_event, bufp, sizeof (struct input_event));
11125 bufp++;
11126 count++;
11127 numchars--;
11128 }
11129
11130 #ifdef USE_X_TOOLKIT
11131 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
11132 /* For a down-event in the menu bar,
11133 don't pass it to Xt right now.
11134 Instead, save it away
11135 and we will pass it to Xt from kbd_buffer_get_event.
11136 That way, we can run some Lisp code first. */
11137 if (f && event.type == ButtonPress
11138 /* Verify the event is really within the menu bar
11139 and not just sent to it due to grabbing. */
11140 && event.xbutton.x >= 0
11141 && event.xbutton.x < f->output_data.x->pixel_width
11142 && event.xbutton.y >= 0
11143 && event.xbutton.y < f->output_data.x->menubar_height
11144 && event.xbutton.same_screen)
11145 {
11146 SET_SAVED_BUTTON_EVENT;
11147 XSETFRAME (last_mouse_press_frame, f);
11148 }
11149 else if (event.type == ButtonPress)
11150 {
11151 last_mouse_press_frame = Qnil;
11152 goto OTHER;
11153 }
11154
11155 #ifdef USE_MOTIF /* This should do not harm for Lucid,
11156 but I am trying to be cautious. */
11157 else if (event.type == ButtonRelease)
11158 {
11159 if (!NILP (last_mouse_press_frame))
11160 {
11161 f = XFRAME (last_mouse_press_frame);
11162 if (f->output_data.x)
11163 SET_SAVED_BUTTON_EVENT;
11164 }
11165 else
11166 goto OTHER;
11167 }
11168 #endif /* USE_MOTIF */
11169 else
11170 goto OTHER;
11171 #endif /* USE_X_TOOLKIT */
11172 }
11173 break;
11174
11175 case CirculateNotify:
11176 goto OTHER;
11177
11178 case CirculateRequest:
11179 goto OTHER;
11180
11181 case VisibilityNotify:
11182 goto OTHER;
11183
11184 case MappingNotify:
11185 /* Someone has changed the keyboard mapping - update the
11186 local cache. */
11187 switch (event.xmapping.request)
11188 {
11189 case MappingModifier:
11190 x_find_modifier_meanings (dpyinfo);
11191 /* This is meant to fall through. */
11192 case MappingKeyboard:
11193 XRefreshKeyboardMapping (&event.xmapping);
11194 }
11195 goto OTHER;
11196
11197 default:
11198 OTHER:
11199 #ifdef USE_X_TOOLKIT
11200 BLOCK_INPUT;
11201 XtDispatchEvent (&event);
11202 UNBLOCK_INPUT;
11203 #endif /* USE_X_TOOLKIT */
11204 break;
11205 }
11206 }
11207 }
11208
11209 out:;
11210
11211 /* On some systems, an X bug causes Emacs to get no more events
11212 when the window is destroyed. Detect that. (1994.) */
11213 if (! event_found)
11214 {
11215 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
11216 One XNOOP in 100 loops will make Emacs terminate.
11217 B. Bretthauer, 1994 */
11218 x_noop_count++;
11219 if (x_noop_count >= 100)
11220 {
11221 x_noop_count=0;
11222
11223 if (next_noop_dpyinfo == 0)
11224 next_noop_dpyinfo = x_display_list;
11225
11226 XNoOp (next_noop_dpyinfo->display);
11227
11228 /* Each time we get here, cycle through the displays now open. */
11229 next_noop_dpyinfo = next_noop_dpyinfo->next;
11230 }
11231 }
11232
11233 /* If the focus was just given to an auto-raising frame,
11234 raise it now. */
11235 /* ??? This ought to be able to handle more than one such frame. */
11236 if (pending_autoraise_frame)
11237 {
11238 x_raise_frame (pending_autoraise_frame);
11239 pending_autoraise_frame = 0;
11240 }
11241
11242 UNBLOCK_INPUT;
11243 --handling_signal;
11244 return count;
11245 }
11246
11247
11248
11249 \f
11250 /***********************************************************************
11251 Text Cursor
11252 ***********************************************************************/
11253
11254 /* Notice when the text cursor of window W has been completely
11255 overwritten by a drawing operation that outputs glyphs in AREA
11256 starting at X0 and ending at X1 in the line starting at Y0 and
11257 ending at Y1. X coordinates are area-relative. X1 < 0 means all
11258 the rest of the line after X0 has been written. Y coordinates
11259 are window-relative. */
11260
11261 static void
11262 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
11263 struct window *w;
11264 enum glyph_row_area area;
11265 int x0, y0, x1, y1;
11266 {
11267 if (area == TEXT_AREA
11268 && w->phys_cursor_on_p
11269 && y0 <= w->phys_cursor.y
11270 && y1 >= w->phys_cursor.y + w->phys_cursor_height
11271 && x0 <= w->phys_cursor.x
11272 && (x1 < 0 || x1 > w->phys_cursor.x))
11273 w->phys_cursor_on_p = 0;
11274 }
11275
11276
11277 /* Set clipping for output in glyph row ROW. W is the window in which
11278 we operate. GC is the graphics context to set clipping in.
11279 WHOLE_LINE_P non-zero means include the areas used for truncation
11280 mark display and alike in the clipping rectangle.
11281
11282 ROW may be a text row or, e.g., a mode line. Text rows must be
11283 clipped to the interior of the window dedicated to text display,
11284 mode lines must be clipped to the whole window. */
11285
11286 static void
11287 x_clip_to_row (w, row, gc, whole_line_p)
11288 struct window *w;
11289 struct glyph_row *row;
11290 GC gc;
11291 int whole_line_p;
11292 {
11293 struct frame *f = XFRAME (WINDOW_FRAME (w));
11294 XRectangle clip_rect;
11295 int window_x, window_y, window_width, window_height;
11296
11297 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
11298
11299 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
11300 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
11301 clip_rect.y = max (clip_rect.y, window_y);
11302 clip_rect.width = window_width;
11303 clip_rect.height = row->visible_height;
11304
11305 /* If clipping to the whole line, including trunc marks, extend
11306 the rectangle to the left and increase its width. */
11307 if (whole_line_p)
11308 {
11309 clip_rect.x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
11310 clip_rect.width += FRAME_X_FRINGE_WIDTH (f);
11311 }
11312
11313 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
11314 }
11315
11316
11317 /* Draw a hollow box cursor on window W in glyph row ROW. */
11318
11319 static void
11320 x_draw_hollow_cursor (w, row)
11321 struct window *w;
11322 struct glyph_row *row;
11323 {
11324 struct frame *f = XFRAME (WINDOW_FRAME (w));
11325 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11326 Display *dpy = FRAME_X_DISPLAY (f);
11327 int x, y, wd, h;
11328 XGCValues xgcv;
11329 struct glyph *cursor_glyph;
11330 GC gc;
11331
11332 /* Compute frame-relative coordinates from window-relative
11333 coordinates. */
11334 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11335 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
11336 + row->ascent - w->phys_cursor_ascent);
11337 h = row->height - 1;
11338
11339 /* Get the glyph the cursor is on. If we can't tell because
11340 the current matrix is invalid or such, give up. */
11341 cursor_glyph = get_phys_cursor_glyph (w);
11342 if (cursor_glyph == NULL)
11343 return;
11344
11345 /* Compute the width of the rectangle to draw. If on a stretch
11346 glyph, and `x-stretch-block-cursor' is nil, don't draw a
11347 rectangle as wide as the glyph, but use a canonical character
11348 width instead. */
11349 wd = cursor_glyph->pixel_width - 1;
11350 if (cursor_glyph->type == STRETCH_GLYPH
11351 && !x_stretch_cursor_p)
11352 wd = min (CANON_X_UNIT (f), wd);
11353
11354 /* The foreground of cursor_gc is typically the same as the normal
11355 background color, which can cause the cursor box to be invisible. */
11356 xgcv.foreground = f->output_data.x->cursor_pixel;
11357 if (dpyinfo->scratch_cursor_gc)
11358 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
11359 else
11360 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
11361 GCForeground, &xgcv);
11362 gc = dpyinfo->scratch_cursor_gc;
11363
11364 /* Set clipping, draw the rectangle, and reset clipping again. */
11365 x_clip_to_row (w, row, gc, 0);
11366 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
11367 XSetClipMask (dpy, gc, None);
11368 }
11369
11370
11371 /* Draw a bar cursor on window W in glyph row ROW.
11372
11373 Implementation note: One would like to draw a bar cursor with an
11374 angle equal to the one given by the font property XA_ITALIC_ANGLE.
11375 Unfortunately, I didn't find a font yet that has this property set.
11376 --gerd. */
11377
11378 static void
11379 x_draw_bar_cursor (w, row, width)
11380 struct window *w;
11381 struct glyph_row *row;
11382 int width;
11383 {
11384 struct frame *f = XFRAME (w->frame);
11385 struct glyph *cursor_glyph;
11386
11387 /* If cursor is out of bounds, don't draw garbage. This can happen
11388 in mini-buffer windows when switching between echo area glyphs
11389 and mini-buffer. */
11390 cursor_glyph = get_phys_cursor_glyph (w);
11391 if (cursor_glyph == NULL)
11392 return;
11393
11394 /* If on an image, draw like a normal cursor. That's usually better
11395 visible than drawing a bar, esp. if the image is large so that
11396 the bar might not be in the window. */
11397 if (cursor_glyph->type == IMAGE_GLYPH)
11398 {
11399 struct glyph_row *row;
11400 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
11401 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
11402 }
11403 else
11404 {
11405 Display *dpy = FRAME_X_DISPLAY (f);
11406 Window window = FRAME_X_WINDOW (f);
11407 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
11408 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
11409 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
11410 XGCValues xgcv;
11411
11412 /* If the glyph's background equals the color we normally draw
11413 the bar cursor in, the bar cursor in its normal color is
11414 invisible. Use the glyph's foreground color instead in this
11415 case, on the assumption that the glyph's colors are chosen so
11416 that the glyph is legible. */
11417 if (face->background == f->output_data.x->cursor_pixel)
11418 xgcv.background = xgcv.foreground = face->foreground;
11419 else
11420 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
11421 xgcv.graphics_exposures = 0;
11422
11423 if (gc)
11424 XChangeGC (dpy, gc, mask, &xgcv);
11425 else
11426 {
11427 gc = XCreateGC (dpy, window, mask, &xgcv);
11428 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
11429 }
11430
11431 if (width < 0)
11432 width = f->output_data.x->cursor_width;
11433 width = min (cursor_glyph->pixel_width, width);
11434
11435 x_clip_to_row (w, row, gc, 0);
11436 XFillRectangle (dpy, window, gc,
11437 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11438 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
11439 width, row->height);
11440 XSetClipMask (dpy, gc, None);
11441 }
11442 }
11443
11444
11445 /* Clear the cursor of window W to background color, and mark the
11446 cursor as not shown. This is used when the text where the cursor
11447 is is about to be rewritten. */
11448
11449 static void
11450 x_clear_cursor (w)
11451 struct window *w;
11452 {
11453 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
11454 x_update_window_cursor (w, 0);
11455 }
11456
11457
11458 /* Draw the cursor glyph of window W in glyph row ROW. See the
11459 comment of x_draw_glyphs for the meaning of HL. */
11460
11461 static void
11462 x_draw_phys_cursor_glyph (w, row, hl)
11463 struct window *w;
11464 struct glyph_row *row;
11465 enum draw_glyphs_face hl;
11466 {
11467 /* If cursor hpos is out of bounds, don't draw garbage. This can
11468 happen in mini-buffer windows when switching between echo area
11469 glyphs and mini-buffer. */
11470 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
11471 {
11472 int on_p = w->phys_cursor_on_p;
11473
11474 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
11475 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
11476 hl, 0);
11477 w->phys_cursor_on_p = on_p;
11478
11479 /* When we erase the cursor, and ROW is overlapped by other
11480 rows, make sure that these overlapping parts of other rows
11481 are redrawn. */
11482 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
11483 {
11484 if (row > w->current_matrix->rows
11485 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
11486 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
11487
11488 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
11489 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
11490 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
11491 }
11492 }
11493 }
11494
11495
11496 /* Erase the image of a cursor of window W from the screen. */
11497
11498 static void
11499 x_erase_phys_cursor (w)
11500 struct window *w;
11501 {
11502 struct frame *f = XFRAME (w->frame);
11503 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11504 int hpos = w->phys_cursor.hpos;
11505 int vpos = w->phys_cursor.vpos;
11506 int mouse_face_here_p = 0;
11507 struct glyph_matrix *active_glyphs = w->current_matrix;
11508 struct glyph_row *cursor_row;
11509 struct glyph *cursor_glyph;
11510 enum draw_glyphs_face hl;
11511
11512 /* No cursor displayed or row invalidated => nothing to do on the
11513 screen. */
11514 if (w->phys_cursor_type == NO_CURSOR)
11515 goto mark_cursor_off;
11516
11517 /* VPOS >= active_glyphs->nrows means that window has been resized.
11518 Don't bother to erase the cursor. */
11519 if (vpos >= active_glyphs->nrows)
11520 goto mark_cursor_off;
11521
11522 /* If row containing cursor is marked invalid, there is nothing we
11523 can do. */
11524 cursor_row = MATRIX_ROW (active_glyphs, vpos);
11525 if (!cursor_row->enabled_p)
11526 goto mark_cursor_off;
11527
11528 /* If row is completely invisible, don't attempt to delete a cursor which
11529 isn't there. This can happen if cursor is at top of a window, and
11530 we switch to a buffer with a header line in that window. */
11531 if (cursor_row->visible_height <= 0)
11532 goto mark_cursor_off;
11533
11534 /* This can happen when the new row is shorter than the old one.
11535 In this case, either x_draw_glyphs or clear_end_of_line
11536 should have cleared the cursor. Note that we wouldn't be
11537 able to erase the cursor in this case because we don't have a
11538 cursor glyph at hand. */
11539 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
11540 goto mark_cursor_off;
11541
11542 /* If the cursor is in the mouse face area, redisplay that when
11543 we clear the cursor. */
11544 if (! NILP (dpyinfo->mouse_face_window)
11545 && w == XWINDOW (dpyinfo->mouse_face_window)
11546 && (vpos > dpyinfo->mouse_face_beg_row
11547 || (vpos == dpyinfo->mouse_face_beg_row
11548 && hpos >= dpyinfo->mouse_face_beg_col))
11549 && (vpos < dpyinfo->mouse_face_end_row
11550 || (vpos == dpyinfo->mouse_face_end_row
11551 && hpos < dpyinfo->mouse_face_end_col))
11552 /* Don't redraw the cursor's spot in mouse face if it is at the
11553 end of a line (on a newline). The cursor appears there, but
11554 mouse highlighting does not. */
11555 && cursor_row->used[TEXT_AREA] > hpos)
11556 mouse_face_here_p = 1;
11557
11558 /* Maybe clear the display under the cursor. */
11559 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
11560 {
11561 int x;
11562 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11563
11564 cursor_glyph = get_phys_cursor_glyph (w);
11565 if (cursor_glyph == NULL)
11566 goto mark_cursor_off;
11567
11568 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11569
11570 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11571 x,
11572 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
11573 cursor_row->y)),
11574 cursor_glyph->pixel_width,
11575 cursor_row->visible_height,
11576 False);
11577 }
11578
11579 /* Erase the cursor by redrawing the character underneath it. */
11580 if (mouse_face_here_p)
11581 hl = DRAW_MOUSE_FACE;
11582 else
11583 hl = DRAW_NORMAL_TEXT;
11584 x_draw_phys_cursor_glyph (w, cursor_row, hl);
11585
11586 mark_cursor_off:
11587 w->phys_cursor_on_p = 0;
11588 w->phys_cursor_type = NO_CURSOR;
11589 }
11590
11591
11592 /* Non-zero if physical cursor of window W is within mouse face. */
11593
11594 static int
11595 cursor_in_mouse_face_p (w)
11596 struct window *w;
11597 {
11598 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
11599 int in_mouse_face = 0;
11600
11601 if (WINDOWP (dpyinfo->mouse_face_window)
11602 && XWINDOW (dpyinfo->mouse_face_window) == w)
11603 {
11604 int hpos = w->phys_cursor.hpos;
11605 int vpos = w->phys_cursor.vpos;
11606
11607 if (vpos >= dpyinfo->mouse_face_beg_row
11608 && vpos <= dpyinfo->mouse_face_end_row
11609 && (vpos > dpyinfo->mouse_face_beg_row
11610 || hpos >= dpyinfo->mouse_face_beg_col)
11611 && (vpos < dpyinfo->mouse_face_end_row
11612 || hpos < dpyinfo->mouse_face_end_col
11613 || dpyinfo->mouse_face_past_end))
11614 in_mouse_face = 1;
11615 }
11616
11617 return in_mouse_face;
11618 }
11619
11620
11621 /* Display or clear cursor of window W. If ON is zero, clear the
11622 cursor. If it is non-zero, display the cursor. If ON is nonzero,
11623 where to put the cursor is specified by HPOS, VPOS, X and Y. */
11624
11625 void
11626 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
11627 struct window *w;
11628 int on, hpos, vpos, x, y;
11629 {
11630 struct frame *f = XFRAME (w->frame);
11631 int new_cursor_type;
11632 int new_cursor_width;
11633 struct glyph_matrix *current_glyphs;
11634 struct glyph_row *glyph_row;
11635 struct glyph *glyph;
11636 int cursor_non_selected;
11637
11638 /* This is pointless on invisible frames, and dangerous on garbaged
11639 windows and frames; in the latter case, the frame or window may
11640 be in the midst of changing its size, and x and y may be off the
11641 window. */
11642 if (! FRAME_VISIBLE_P (f)
11643 || FRAME_GARBAGED_P (f)
11644 || vpos >= w->current_matrix->nrows
11645 || hpos >= w->current_matrix->matrix_w)
11646 return;
11647
11648 /* If cursor is off and we want it off, return quickly. */
11649 if (!on && !w->phys_cursor_on_p)
11650 return;
11651
11652 current_glyphs = w->current_matrix;
11653 glyph_row = MATRIX_ROW (current_glyphs, vpos);
11654 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
11655
11656 /* If cursor row is not enabled, we don't really know where to
11657 display the cursor. */
11658 if (!glyph_row->enabled_p)
11659 {
11660 w->phys_cursor_on_p = 0;
11661 return;
11662 }
11663
11664 xassert (interrupt_input_blocked);
11665
11666 /* Set new_cursor_type to the cursor we want to be displayed. In a
11667 mini-buffer window, we want the cursor only to appear if we are
11668 reading input from this window. For the selected window, we want
11669 the cursor type given by the frame parameter. If explicitly
11670 marked off, draw no cursor. In all other cases, we want a hollow
11671 box cursor. */
11672 cursor_non_selected
11673 = !NILP (Fbuffer_local_value (Qcursor_in_non_selected_windows,
11674 w->buffer));
11675 new_cursor_width = -1;
11676 if (cursor_in_echo_area
11677 && FRAME_HAS_MINIBUF_P (f)
11678 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
11679 {
11680 if (w == XWINDOW (echo_area_window))
11681 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11682 else if (cursor_non_selected)
11683 new_cursor_type = HOLLOW_BOX_CURSOR;
11684 else
11685 new_cursor_type = NO_CURSOR;
11686 }
11687 else
11688 {
11689 if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
11690 || w != XWINDOW (f->selected_window))
11691 {
11692 if ((MINI_WINDOW_P (w) && minibuf_level == 0)
11693 || !cursor_non_selected
11694 || NILP (XBUFFER (w->buffer)->cursor_type))
11695 new_cursor_type = NO_CURSOR;
11696 else
11697 new_cursor_type = HOLLOW_BOX_CURSOR;
11698 }
11699 else if (w->cursor_off_p)
11700 new_cursor_type = NO_CURSOR;
11701 else
11702 {
11703 struct buffer *b = XBUFFER (w->buffer);
11704
11705 if (EQ (b->cursor_type, Qt))
11706 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11707 else
11708 new_cursor_type = x_specified_cursor_type (b->cursor_type,
11709 &new_cursor_width);
11710 }
11711 }
11712
11713 /* If cursor is currently being shown and we don't want it to be or
11714 it is in the wrong place, or the cursor type is not what we want,
11715 erase it. */
11716 if (w->phys_cursor_on_p
11717 && (!on
11718 || w->phys_cursor.x != x
11719 || w->phys_cursor.y != y
11720 || new_cursor_type != w->phys_cursor_type))
11721 x_erase_phys_cursor (w);
11722
11723 /* If the cursor is now invisible and we want it to be visible,
11724 display it. */
11725 if (on && !w->phys_cursor_on_p)
11726 {
11727 w->phys_cursor_ascent = glyph_row->ascent;
11728 w->phys_cursor_height = glyph_row->height;
11729
11730 /* Set phys_cursor_.* before x_draw_.* is called because some
11731 of them may need the information. */
11732 w->phys_cursor.x = x;
11733 w->phys_cursor.y = glyph_row->y;
11734 w->phys_cursor.hpos = hpos;
11735 w->phys_cursor.vpos = vpos;
11736 w->phys_cursor_type = new_cursor_type;
11737 w->phys_cursor_on_p = 1;
11738
11739 switch (new_cursor_type)
11740 {
11741 case HOLLOW_BOX_CURSOR:
11742 x_draw_hollow_cursor (w, glyph_row);
11743 break;
11744
11745 case FILLED_BOX_CURSOR:
11746 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
11747 break;
11748
11749 case BAR_CURSOR:
11750 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
11751 break;
11752
11753 case NO_CURSOR:
11754 break;
11755
11756 default:
11757 abort ();
11758 }
11759
11760 #ifdef HAVE_X_I18N
11761 if (w == XWINDOW (f->selected_window))
11762 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
11763 xic_set_preeditarea (w, x, y);
11764 #endif
11765 }
11766
11767 #ifndef XFlush
11768 if (updating_frame != f)
11769 XFlush (FRAME_X_DISPLAY (f));
11770 #endif
11771 }
11772
11773
11774 /* Display the cursor on window W, or clear it. X and Y are window
11775 relative pixel coordinates. HPOS and VPOS are glyph matrix
11776 positions. If W is not the selected window, display a hollow
11777 cursor. ON non-zero means display the cursor at X, Y which
11778 correspond to HPOS, VPOS, otherwise it is cleared. */
11779
11780 void
11781 x_display_cursor (w, on, hpos, vpos, x, y)
11782 struct window *w;
11783 int on, hpos, vpos, x, y;
11784 {
11785 BLOCK_INPUT;
11786 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
11787 UNBLOCK_INPUT;
11788 }
11789
11790
11791 /* Display the cursor on window W, or clear it, according to ON_P.
11792 Don't change the cursor's position. */
11793
11794 void
11795 x_update_cursor (f, on_p)
11796 struct frame *f;
11797 {
11798 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11799 }
11800
11801
11802 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
11803 in the window tree rooted at W. */
11804
11805 static void
11806 x_update_cursor_in_window_tree (w, on_p)
11807 struct window *w;
11808 int on_p;
11809 {
11810 while (w)
11811 {
11812 if (!NILP (w->hchild))
11813 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11814 else if (!NILP (w->vchild))
11815 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11816 else
11817 x_update_window_cursor (w, on_p);
11818
11819 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11820 }
11821 }
11822
11823
11824 /* Switch the display of W's cursor on or off, according to the value
11825 of ON. */
11826
11827 static void
11828 x_update_window_cursor (w, on)
11829 struct window *w;
11830 int on;
11831 {
11832 /* Don't update cursor in windows whose frame is in the process
11833 of being deleted. */
11834 if (w->current_matrix)
11835 {
11836 BLOCK_INPUT;
11837 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11838 w->phys_cursor.x, w->phys_cursor.y);
11839 UNBLOCK_INPUT;
11840 }
11841 }
11842
11843
11844
11845 \f
11846 /* Icons. */
11847
11848 /* Make the x-window of frame F use the gnu icon bitmap. */
11849
11850 int
11851 x_bitmap_icon (f, file)
11852 struct frame *f;
11853 Lisp_Object file;
11854 {
11855 int bitmap_id;
11856
11857 if (FRAME_X_WINDOW (f) == 0)
11858 return 1;
11859
11860 /* Free up our existing icon bitmap if any. */
11861 if (f->output_data.x->icon_bitmap > 0)
11862 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11863 f->output_data.x->icon_bitmap = 0;
11864
11865 if (STRINGP (file))
11866 bitmap_id = x_create_bitmap_from_file (f, file);
11867 else
11868 {
11869 /* Create the GNU bitmap if necessary. */
11870 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
11871 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11872 = x_create_bitmap_from_data (f, gnu_bits,
11873 gnu_width, gnu_height);
11874
11875 /* The first time we create the GNU bitmap,
11876 this increments the ref-count one extra time.
11877 As a result, the GNU bitmap is never freed.
11878 That way, we don't have to worry about allocating it again. */
11879 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
11880
11881 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
11882 }
11883
11884 x_wm_set_icon_pixmap (f, bitmap_id);
11885 f->output_data.x->icon_bitmap = bitmap_id;
11886
11887 return 0;
11888 }
11889
11890
11891 /* Make the x-window of frame F use a rectangle with text.
11892 Use ICON_NAME as the text. */
11893
11894 int
11895 x_text_icon (f, icon_name)
11896 struct frame *f;
11897 char *icon_name;
11898 {
11899 if (FRAME_X_WINDOW (f) == 0)
11900 return 1;
11901
11902 #ifdef HAVE_X11R4
11903 {
11904 XTextProperty text;
11905 text.value = (unsigned char *) icon_name;
11906 text.encoding = XA_STRING;
11907 text.format = 8;
11908 text.nitems = strlen (icon_name);
11909 #ifdef USE_X_TOOLKIT
11910 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11911 &text);
11912 #else /* not USE_X_TOOLKIT */
11913 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11914 #endif /* not USE_X_TOOLKIT */
11915 }
11916 #else /* not HAVE_X11R4 */
11917 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11918 #endif /* not HAVE_X11R4 */
11919
11920 if (f->output_data.x->icon_bitmap > 0)
11921 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11922 f->output_data.x->icon_bitmap = 0;
11923 x_wm_set_icon_pixmap (f, 0);
11924
11925 return 0;
11926 }
11927 \f
11928 #define X_ERROR_MESSAGE_SIZE 200
11929
11930 /* If non-nil, this should be a string.
11931 It means catch X errors and store the error message in this string. */
11932
11933 static Lisp_Object x_error_message_string;
11934
11935 /* An X error handler which stores the error message in
11936 x_error_message_string. This is called from x_error_handler if
11937 x_catch_errors is in effect. */
11938
11939 static void
11940 x_error_catcher (display, error)
11941 Display *display;
11942 XErrorEvent *error;
11943 {
11944 XGetErrorText (display, error->error_code,
11945 XSTRING (x_error_message_string)->data,
11946 X_ERROR_MESSAGE_SIZE);
11947 }
11948
11949 /* Begin trapping X errors for display DPY. Actually we trap X errors
11950 for all displays, but DPY should be the display you are actually
11951 operating on.
11952
11953 After calling this function, X protocol errors no longer cause
11954 Emacs to exit; instead, they are recorded in the string
11955 stored in x_error_message_string.
11956
11957 Calling x_check_errors signals an Emacs error if an X error has
11958 occurred since the last call to x_catch_errors or x_check_errors.
11959
11960 Calling x_uncatch_errors resumes the normal error handling. */
11961
11962 void x_check_errors ();
11963 static Lisp_Object x_catch_errors_unwind ();
11964
11965 int
11966 x_catch_errors (dpy)
11967 Display *dpy;
11968 {
11969 int count = specpdl_ptr - specpdl;
11970
11971 /* Make sure any errors from previous requests have been dealt with. */
11972 XSync (dpy, False);
11973
11974 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11975
11976 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11977 XSTRING (x_error_message_string)->data[0] = 0;
11978
11979 return count;
11980 }
11981
11982 /* Unbind the binding that we made to check for X errors. */
11983
11984 static Lisp_Object
11985 x_catch_errors_unwind (old_val)
11986 Lisp_Object old_val;
11987 {
11988 x_error_message_string = old_val;
11989 return Qnil;
11990 }
11991
11992 /* If any X protocol errors have arrived since the last call to
11993 x_catch_errors or x_check_errors, signal an Emacs error using
11994 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11995
11996 void
11997 x_check_errors (dpy, format)
11998 Display *dpy;
11999 char *format;
12000 {
12001 /* Make sure to catch any errors incurred so far. */
12002 XSync (dpy, False);
12003
12004 if (XSTRING (x_error_message_string)->data[0])
12005 error (format, XSTRING (x_error_message_string)->data);
12006 }
12007
12008 /* Nonzero if we had any X protocol errors
12009 since we did x_catch_errors on DPY. */
12010
12011 int
12012 x_had_errors_p (dpy)
12013 Display *dpy;
12014 {
12015 /* Make sure to catch any errors incurred so far. */
12016 XSync (dpy, False);
12017
12018 return XSTRING (x_error_message_string)->data[0] != 0;
12019 }
12020
12021 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
12022
12023 void
12024 x_clear_errors (dpy)
12025 Display *dpy;
12026 {
12027 XSTRING (x_error_message_string)->data[0] = 0;
12028 }
12029
12030 /* Stop catching X protocol errors and let them make Emacs die.
12031 DPY should be the display that was passed to x_catch_errors.
12032 COUNT should be the value that was returned by
12033 the corresponding call to x_catch_errors. */
12034
12035 void
12036 x_uncatch_errors (dpy, count)
12037 Display *dpy;
12038 int count;
12039 {
12040 unbind_to (count, Qnil);
12041 }
12042
12043 #if 0
12044 static unsigned int x_wire_count;
12045 x_trace_wire ()
12046 {
12047 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
12048 }
12049 #endif /* ! 0 */
12050
12051 \f
12052 /* Handle SIGPIPE, which can happen when the connection to a server
12053 simply goes away. SIGPIPE is handled by x_connection_signal.
12054 Don't need to do anything, because the write which caused the
12055 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
12056 which will do the appropriate cleanup for us. */
12057
12058 static SIGTYPE
12059 x_connection_signal (signalnum) /* If we don't have an argument, */
12060 int signalnum; /* some compilers complain in signal calls. */
12061 {
12062 #ifdef USG
12063 /* USG systems forget handlers when they are used;
12064 must reestablish each time */
12065 signal (signalnum, x_connection_signal);
12066 #endif /* USG */
12067 }
12068
12069 \f
12070 /************************************************************************
12071 Handling X errors
12072 ************************************************************************/
12073
12074 /* Error message passed to x_connection_closed. */
12075
12076 static char *error_msg;
12077
12078 /* Function installed as fatal_error_signal_hook in
12079 x_connection_closed. Print the X error message, and exit normally,
12080 instead of dumping core when XtCloseDisplay fails. */
12081
12082 static void
12083 x_fatal_error_signal ()
12084 {
12085 fprintf (stderr, "%s\n", error_msg);
12086 exit (70);
12087 }
12088
12089 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
12090 the text of an error message that lead to the connection loss. */
12091
12092 static SIGTYPE
12093 x_connection_closed (dpy, error_message)
12094 Display *dpy;
12095 char *error_message;
12096 {
12097 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
12098 Lisp_Object frame, tail;
12099 int count;
12100
12101 error_msg = (char *) alloca (strlen (error_message) + 1);
12102 strcpy (error_msg, error_message);
12103 handling_signal = 0;
12104
12105 /* Prevent being called recursively because of an error condition
12106 below. Otherwise, we might end up with printing ``can't find per
12107 display information'' in the recursive call instead of printing
12108 the original message here. */
12109 count = x_catch_errors (dpy);
12110
12111 /* We have to close the display to inform Xt that it doesn't
12112 exist anymore. If we don't, Xt will continue to wait for
12113 events from the display. As a consequence, a sequence of
12114
12115 M-x make-frame-on-display RET :1 RET
12116 ...kill the new frame, so that we get an IO error...
12117 M-x make-frame-on-display RET :1 RET
12118
12119 will indefinitely wait in Xt for events for display `:1', opened
12120 in the first class to make-frame-on-display.
12121
12122 Closing the display is reported to lead to a bus error on
12123 OpenWindows in certain situations. I suspect that is a bug
12124 in OpenWindows. I don't know how to cicumvent it here. */
12125
12126 #ifdef USE_X_TOOLKIT
12127 /* If DPYINFO is null, this means we didn't open the display
12128 in the first place, so don't try to close it. */
12129 if (dpyinfo)
12130 {
12131 extern void (*fatal_error_signal_hook) P_ ((void));
12132 fatal_error_signal_hook = x_fatal_error_signal;
12133 XtCloseDisplay (dpy);
12134 fatal_error_signal_hook = NULL;
12135 }
12136 #endif
12137
12138 /* Indicate that this display is dead. */
12139 if (dpyinfo)
12140 dpyinfo->display = 0;
12141
12142 /* First delete frames whose mini-buffers are on frames
12143 that are on the dead display. */
12144 FOR_EACH_FRAME (tail, frame)
12145 {
12146 Lisp_Object minibuf_frame;
12147 minibuf_frame
12148 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
12149 if (FRAME_X_P (XFRAME (frame))
12150 && FRAME_X_P (XFRAME (minibuf_frame))
12151 && ! EQ (frame, minibuf_frame)
12152 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
12153 Fdelete_frame (frame, Qt);
12154 }
12155
12156 /* Now delete all remaining frames on the dead display.
12157 We are now sure none of these is used as the mini-buffer
12158 for another frame that we need to delete. */
12159 FOR_EACH_FRAME (tail, frame)
12160 if (FRAME_X_P (XFRAME (frame))
12161 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
12162 {
12163 /* Set this to t so that Fdelete_frame won't get confused
12164 trying to find a replacement. */
12165 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
12166 Fdelete_frame (frame, Qt);
12167 }
12168
12169 if (dpyinfo)
12170 x_delete_display (dpyinfo);
12171
12172 x_uncatch_errors (dpy, count);
12173
12174 if (x_display_list == 0)
12175 {
12176 fprintf (stderr, "%s\n", error_msg);
12177 shut_down_emacs (0, 0, Qnil);
12178 exit (70);
12179 }
12180
12181 /* Ordinary stack unwind doesn't deal with these. */
12182 #ifdef SIGIO
12183 sigunblock (sigmask (SIGIO));
12184 #endif
12185 sigunblock (sigmask (SIGALRM));
12186 TOTALLY_UNBLOCK_INPUT;
12187
12188 clear_waiting_for_input ();
12189 error ("%s", error_msg);
12190 }
12191
12192
12193 /* This is the usual handler for X protocol errors.
12194 It kills all frames on the display that we got the error for.
12195 If that was the only one, it prints an error message and kills Emacs. */
12196
12197 static void
12198 x_error_quitter (display, error)
12199 Display *display;
12200 XErrorEvent *error;
12201 {
12202 char buf[256], buf1[356];
12203
12204 /* Note that there is no real way portable across R3/R4 to get the
12205 original error handler. */
12206
12207 XGetErrorText (display, error->error_code, buf, sizeof (buf));
12208 sprintf (buf1, "X protocol error: %s on protocol request %d",
12209 buf, error->request_code);
12210 x_connection_closed (display, buf1);
12211 }
12212
12213
12214 /* This is the first-level handler for X protocol errors.
12215 It calls x_error_quitter or x_error_catcher. */
12216
12217 static int
12218 x_error_handler (display, error)
12219 Display *display;
12220 XErrorEvent *error;
12221 {
12222 if (! NILP (x_error_message_string))
12223 x_error_catcher (display, error);
12224 else
12225 x_error_quitter (display, error);
12226 return 0;
12227 }
12228
12229 /* This is the handler for X IO errors, always.
12230 It kills all frames on the display that we lost touch with.
12231 If that was the only one, it prints an error message and kills Emacs. */
12232
12233 static int
12234 x_io_error_quitter (display)
12235 Display *display;
12236 {
12237 char buf[256];
12238
12239 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
12240 x_connection_closed (display, buf);
12241 return 0;
12242 }
12243 \f
12244 /* Changing the font of the frame. */
12245
12246 /* Give frame F the font named FONTNAME as its default font, and
12247 return the full name of that font. FONTNAME may be a wildcard
12248 pattern; in that case, we choose some font that fits the pattern.
12249 The return value shows which font we chose. */
12250
12251 Lisp_Object
12252 x_new_font (f, fontname)
12253 struct frame *f;
12254 register char *fontname;
12255 {
12256 struct font_info *fontp
12257 = FS_LOAD_FONT (f, fontname);
12258
12259 if (!fontp)
12260 return Qnil;
12261
12262 if (f->output_data.x->font == (XFontStruct *) (fontp->font))
12263 /* This font is already set in frame F. There's nothing more to
12264 do. */
12265 return build_string (fontp->full_name);
12266
12267 f->output_data.x->font = (XFontStruct *) (fontp->font);
12268 f->output_data.x->baseline_offset = fontp->baseline_offset;
12269 f->output_data.x->fontset = -1;
12270
12271 x_compute_fringe_widths (f, 1);
12272
12273 /* Compute the scroll bar width in character columns. */
12274 if (f->scroll_bar_pixel_width > 0)
12275 {
12276 int wid = FONT_WIDTH (f->output_data.x->font);
12277 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
12278 }
12279 else
12280 {
12281 int wid = FONT_WIDTH (f->output_data.x->font);
12282 f->scroll_bar_cols = (14 + wid - 1) / wid;
12283 }
12284
12285 /* Now make the frame display the given font. */
12286 if (FRAME_X_WINDOW (f) != 0)
12287 {
12288 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
12289 f->output_data.x->font->fid);
12290 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
12291 f->output_data.x->font->fid);
12292 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
12293 f->output_data.x->font->fid);
12294
12295 frame_update_line_height (f);
12296
12297 /* Don't change the size of a tip frame; there's no point in
12298 doing it because it's done in Fx_show_tip, and it leads to
12299 problems because the tip frame has no widget. */
12300 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
12301 x_set_window_size (f, 0, f->width, f->height);
12302 }
12303 else
12304 /* If we are setting a new frame's font for the first time,
12305 there are no faces yet, so this font's height is the line height. */
12306 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
12307
12308 return build_string (fontp->full_name);
12309 }
12310
12311 /* Give frame F the fontset named FONTSETNAME as its default fontset,
12312 and return the full name of that fontset. FONTSETNAME may be a
12313 wildcard pattern; in that case, we choose some fontset that fits
12314 the pattern. FONTSETNAME may be a font name for ASCII characters;
12315 in that case, we create a fontset from that font name.
12316
12317 The return value shows which fontset we chose.
12318 If FONTSETNAME specifies the default fontset, return Qt.
12319 If an ASCII font in the specified fontset can't be loaded, return
12320 Qnil. */
12321
12322 Lisp_Object
12323 x_new_fontset (f, fontsetname)
12324 struct frame *f;
12325 Lisp_Object fontsetname;
12326 {
12327 int fontset = fs_query_fontset (fontsetname, 0);
12328 Lisp_Object result;
12329
12330 if (fontset > 0 && f->output_data.x->fontset == fontset)
12331 /* This fontset is already set in frame F. There's nothing more
12332 to do. */
12333 return fontset_name (fontset);
12334 else if (fontset == 0)
12335 /* The default fontset can't be the default font. */
12336 return Qt;
12337
12338 if (fontset >= 0)
12339 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
12340 else
12341 result = x_new_font (f, XSTRING (fontsetname)->data);
12342
12343 if (!STRINGP (result))
12344 /* Can't load ASCII font. */
12345 return Qnil;
12346
12347 if (fontset < 0)
12348 fontset = new_fontset_from_font_name (result);
12349
12350 /* Since x_new_font doesn't update any fontset information, do it now. */
12351 f->output_data.x->fontset = fontset;
12352
12353 #ifdef HAVE_X_I18N
12354 if (FRAME_XIC (f)
12355 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
12356 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
12357 #endif
12358
12359 return fontset_name (fontset);
12360 }
12361
12362 /* Compute actual fringe widths */
12363
12364 void
12365 x_compute_fringe_widths (f, redraw)
12366 struct frame *f;
12367 int redraw;
12368 {
12369 int o_left = f->output_data.x->left_fringe_width;
12370 int o_right = f->output_data.x->right_fringe_width;
12371 int o_cols = f->output_data.x->fringe_cols;
12372
12373 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
12374 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
12375 int left_fringe_width, right_fringe_width;
12376
12377 if (!NILP (left_fringe))
12378 left_fringe = Fcdr (left_fringe);
12379 if (!NILP (right_fringe))
12380 right_fringe = Fcdr (right_fringe);
12381
12382 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
12383 XINT (left_fringe));
12384 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
12385 XINT (right_fringe));
12386
12387 if (left_fringe_width || right_fringe_width)
12388 {
12389 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
12390 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
12391 int conf_wid = left_wid + right_wid;
12392 int font_wid = FONT_WIDTH (f->output_data.x->font);
12393 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
12394 int real_wid = cols * font_wid;
12395 if (left_wid && right_wid)
12396 {
12397 if (left_fringe_width < 0)
12398 {
12399 /* Left fringe width is fixed, adjust right fringe if necessary */
12400 f->output_data.x->left_fringe_width = left_wid;
12401 f->output_data.x->right_fringe_width = real_wid - left_wid;
12402 }
12403 else if (right_fringe_width < 0)
12404 {
12405 /* Right fringe width is fixed, adjust left fringe if necessary */
12406 f->output_data.x->left_fringe_width = real_wid - right_wid;
12407 f->output_data.x->right_fringe_width = right_wid;
12408 }
12409 else
12410 {
12411 /* Adjust both fringes with an equal amount.
12412 Note that we are doing integer arithmetic here, so don't
12413 lose a pixel if the total width is an odd number. */
12414 int fill = real_wid - conf_wid;
12415 f->output_data.x->left_fringe_width = left_wid + fill/2;
12416 f->output_data.x->right_fringe_width = right_wid + fill - fill/2;
12417 }
12418 }
12419 else if (left_fringe_width)
12420 {
12421 f->output_data.x->left_fringe_width = real_wid;
12422 f->output_data.x->right_fringe_width = 0;
12423 }
12424 else
12425 {
12426 f->output_data.x->left_fringe_width = 0;
12427 f->output_data.x->right_fringe_width = real_wid;
12428 }
12429 f->output_data.x->fringe_cols = cols;
12430 f->output_data.x->fringes_extra = real_wid;
12431 }
12432 else
12433 {
12434 f->output_data.x->left_fringe_width = 0;
12435 f->output_data.x->right_fringe_width = 0;
12436 f->output_data.x->fringe_cols = 0;
12437 f->output_data.x->fringes_extra = 0;
12438 }
12439
12440 if (redraw && FRAME_VISIBLE_P (f))
12441 if (o_left != f->output_data.x->left_fringe_width ||
12442 o_right != f->output_data.x->right_fringe_width ||
12443 o_cols != f->output_data.x->fringe_cols)
12444 redraw_frame (f);
12445 }
12446 \f
12447 /***********************************************************************
12448 X Input Methods
12449 ***********************************************************************/
12450
12451 #ifdef HAVE_X_I18N
12452
12453 #ifdef HAVE_X11R6
12454
12455 /* XIM destroy callback function, which is called whenever the
12456 connection to input method XIM dies. CLIENT_DATA contains a
12457 pointer to the x_display_info structure corresponding to XIM. */
12458
12459 static void
12460 xim_destroy_callback (xim, client_data, call_data)
12461 XIM xim;
12462 XPointer client_data;
12463 XPointer call_data;
12464 {
12465 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
12466 Lisp_Object frame, tail;
12467
12468 BLOCK_INPUT;
12469
12470 /* No need to call XDestroyIC.. */
12471 FOR_EACH_FRAME (tail, frame)
12472 {
12473 struct frame *f = XFRAME (frame);
12474 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
12475 {
12476 FRAME_XIC (f) = NULL;
12477 if (FRAME_XIC_FONTSET (f))
12478 {
12479 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
12480 FRAME_XIC_FONTSET (f) = NULL;
12481 }
12482 }
12483 }
12484
12485 /* No need to call XCloseIM. */
12486 dpyinfo->xim = NULL;
12487 XFree (dpyinfo->xim_styles);
12488 UNBLOCK_INPUT;
12489 }
12490
12491 #endif /* HAVE_X11R6 */
12492
12493 /* Open the connection to the XIM server on display DPYINFO.
12494 RESOURCE_NAME is the resource name Emacs uses. */
12495
12496 static void
12497 xim_open_dpy (dpyinfo, resource_name)
12498 struct x_display_info *dpyinfo;
12499 char *resource_name;
12500 {
12501 XIM xim;
12502
12503 if (use_xim)
12504 {
12505 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
12506 EMACS_CLASS);
12507 dpyinfo->xim = xim;
12508
12509 if (xim)
12510 {
12511 #ifdef HAVE_X11R6
12512 XIMCallback destroy;
12513 #endif
12514
12515 /* Get supported styles and XIM values. */
12516 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
12517
12518 #ifdef HAVE_X11R6
12519 destroy.callback = xim_destroy_callback;
12520 destroy.client_data = (XPointer)dpyinfo;
12521 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
12522 #endif
12523 }
12524 }
12525 else
12526 dpyinfo->xim = NULL;
12527 }
12528
12529
12530 #ifdef HAVE_X11R6_XIM
12531
12532 struct xim_inst_t
12533 {
12534 struct x_display_info *dpyinfo;
12535 char *resource_name;
12536 };
12537
12538 /* XIM instantiate callback function, which is called whenever an XIM
12539 server is available. DISPLAY is teh display of the XIM.
12540 CLIENT_DATA contains a pointer to an xim_inst_t structure created
12541 when the callback was registered. */
12542
12543 static void
12544 xim_instantiate_callback (display, client_data, call_data)
12545 Display *display;
12546 XPointer client_data;
12547 XPointer call_data;
12548 {
12549 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
12550 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
12551
12552 /* We don't support multiple XIM connections. */
12553 if (dpyinfo->xim)
12554 return;
12555
12556 xim_open_dpy (dpyinfo, xim_inst->resource_name);
12557
12558 /* Create XIC for the existing frames on the same display, as long
12559 as they have no XIC. */
12560 if (dpyinfo->xim && dpyinfo->reference_count > 0)
12561 {
12562 Lisp_Object tail, frame;
12563
12564 BLOCK_INPUT;
12565 FOR_EACH_FRAME (tail, frame)
12566 {
12567 struct frame *f = XFRAME (frame);
12568
12569 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
12570 if (FRAME_XIC (f) == NULL)
12571 {
12572 create_frame_xic (f);
12573 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
12574 xic_set_statusarea (f);
12575 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
12576 {
12577 struct window *w = XWINDOW (f->selected_window);
12578 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
12579 }
12580 }
12581 }
12582
12583 UNBLOCK_INPUT;
12584 }
12585 }
12586
12587 #endif /* HAVE_X11R6_XIM */
12588
12589
12590 /* Open a connection to the XIM server on display DPYINFO.
12591 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
12592 connection only at the first time. On X11R6, open the connection
12593 in the XIM instantiate callback function. */
12594
12595 static void
12596 xim_initialize (dpyinfo, resource_name)
12597 struct x_display_info *dpyinfo;
12598 char *resource_name;
12599 {
12600 if (use_xim)
12601 {
12602 #ifdef HAVE_X11R6_XIM
12603 struct xim_inst_t *xim_inst;
12604 int len;
12605
12606 dpyinfo->xim = NULL;
12607 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
12608 xim_inst->dpyinfo = dpyinfo;
12609 len = strlen (resource_name);
12610 xim_inst->resource_name = (char *) xmalloc (len + 1);
12611 bcopy (resource_name, xim_inst->resource_name, len + 1);
12612 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12613 resource_name, EMACS_CLASS,
12614 xim_instantiate_callback,
12615 /* Fixme: This is XPointer in
12616 XFree86 but (XPointer *) on
12617 Tru64, at least. */
12618 (XPointer) xim_inst);
12619 #else /* not HAVE_X11R6_XIM */
12620 dpyinfo->xim = NULL;
12621 xim_open_dpy (dpyinfo, resource_name);
12622 #endif /* not HAVE_X11R6_XIM */
12623 }
12624 else
12625 dpyinfo->xim = NULL;
12626 }
12627
12628
12629 /* Close the connection to the XIM server on display DPYINFO. */
12630
12631 static void
12632 xim_close_dpy (dpyinfo)
12633 struct x_display_info *dpyinfo;
12634 {
12635 if (use_xim)
12636 {
12637 #ifdef HAVE_X11R6_XIM
12638 if (dpyinfo->display)
12639 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12640 NULL, EMACS_CLASS,
12641 xim_instantiate_callback, NULL);
12642 #endif /* not HAVE_X11R6_XIM */
12643 if (dpyinfo->display)
12644 XCloseIM (dpyinfo->xim);
12645 dpyinfo->xim = NULL;
12646 XFree (dpyinfo->xim_styles);
12647 }
12648 }
12649
12650 #endif /* not HAVE_X11R6_XIM */
12651
12652
12653 \f
12654 /* Calculate the absolute position in frame F
12655 from its current recorded position values and gravity. */
12656
12657 void
12658 x_calc_absolute_position (f)
12659 struct frame *f;
12660 {
12661 Window child;
12662 int win_x = 0, win_y = 0;
12663 int flags = f->output_data.x->size_hint_flags;
12664 int this_window;
12665
12666 /* We have nothing to do if the current position
12667 is already for the top-left corner. */
12668 if (! ((flags & XNegative) || (flags & YNegative)))
12669 return;
12670
12671 #ifdef USE_X_TOOLKIT
12672 this_window = XtWindow (f->output_data.x->widget);
12673 #else
12674 this_window = FRAME_X_WINDOW (f);
12675 #endif
12676
12677 /* Find the position of the outside upper-left corner of
12678 the inner window, with respect to the outer window.
12679 But do this only if we will need the results. */
12680 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
12681 {
12682 int count;
12683
12684 BLOCK_INPUT;
12685 count = x_catch_errors (FRAME_X_DISPLAY (f));
12686 while (1)
12687 {
12688 x_clear_errors (FRAME_X_DISPLAY (f));
12689 XTranslateCoordinates (FRAME_X_DISPLAY (f),
12690
12691 /* From-window, to-window. */
12692 this_window,
12693 f->output_data.x->parent_desc,
12694
12695 /* From-position, to-position. */
12696 0, 0, &win_x, &win_y,
12697
12698 /* Child of win. */
12699 &child);
12700 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12701 {
12702 Window newroot, newparent = 0xdeadbeef;
12703 Window *newchildren;
12704 unsigned int nchildren;
12705
12706 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
12707 &newparent, &newchildren, &nchildren))
12708 break;
12709
12710 XFree ((char *) newchildren);
12711
12712 f->output_data.x->parent_desc = newparent;
12713 }
12714 else
12715 break;
12716 }
12717
12718 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12719 UNBLOCK_INPUT;
12720 }
12721
12722 /* Treat negative positions as relative to the leftmost bottommost
12723 position that fits on the screen. */
12724 if (flags & XNegative)
12725 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
12726 - 2 * f->output_data.x->border_width - win_x
12727 - PIXEL_WIDTH (f)
12728 + f->output_data.x->left_pos);
12729
12730 {
12731 int height = PIXEL_HEIGHT (f);
12732
12733 #if defined USE_X_TOOLKIT && defined USE_MOTIF
12734 /* Something is fishy here. When using Motif, starting Emacs with
12735 `-g -0-0', the frame appears too low by a few pixels.
12736
12737 This seems to be so because initially, while Emacs is starting,
12738 the column widget's height and the frame's pixel height are
12739 different. The column widget's height is the right one. In
12740 later invocations, when Emacs is up, the frame's pixel height
12741 is right, though.
12742
12743 It's not obvious where the initial small difference comes from.
12744 2000-12-01, gerd. */
12745
12746 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
12747 #endif
12748
12749 if (flags & YNegative)
12750 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
12751 - 2 * f->output_data.x->border_width
12752 - win_y
12753 - height
12754 + f->output_data.x->top_pos);
12755 }
12756
12757 /* The left_pos and top_pos
12758 are now relative to the top and left screen edges,
12759 so the flags should correspond. */
12760 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12761 }
12762
12763 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
12764 to really change the position, and 0 when calling from
12765 x_make_frame_visible (in that case, XOFF and YOFF are the current
12766 position values). It is -1 when calling from x_set_frame_parameters,
12767 which means, do adjust for borders but don't change the gravity. */
12768
12769 void
12770 x_set_offset (f, xoff, yoff, change_gravity)
12771 struct frame *f;
12772 register int xoff, yoff;
12773 int change_gravity;
12774 {
12775 int modified_top, modified_left;
12776
12777 if (change_gravity > 0)
12778 {
12779 f->output_data.x->top_pos = yoff;
12780 f->output_data.x->left_pos = xoff;
12781 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12782 if (xoff < 0)
12783 f->output_data.x->size_hint_flags |= XNegative;
12784 if (yoff < 0)
12785 f->output_data.x->size_hint_flags |= YNegative;
12786 f->output_data.x->win_gravity = NorthWestGravity;
12787 }
12788 x_calc_absolute_position (f);
12789
12790 BLOCK_INPUT;
12791 x_wm_set_size_hint (f, (long) 0, 0);
12792
12793 modified_left = f->output_data.x->left_pos;
12794 modified_top = f->output_data.x->top_pos;
12795 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
12796 this seems to be unnecessary and incorrect. rms, 4/17/97. */
12797 /* It is a mystery why we need to add the border_width here
12798 when the frame is already visible, but experiment says we do. */
12799 if (change_gravity != 0)
12800 {
12801 modified_left += f->output_data.x->border_width;
12802 modified_top += f->output_data.x->border_width;
12803 }
12804 #endif
12805
12806 #ifdef USE_X_TOOLKIT
12807 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
12808 modified_left, modified_top);
12809 #else /* not USE_X_TOOLKIT */
12810 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12811 modified_left, modified_top);
12812 #endif /* not USE_X_TOOLKIT */
12813 UNBLOCK_INPUT;
12814 }
12815
12816 /* Check if we need to resize the frame due to a fullscreen request.
12817 If so needed, resize the frame. */
12818 static void
12819 x_check_fullscreen (f)
12820 struct frame *f;
12821 {
12822 if (f->output_data.x->want_fullscreen & FULLSCREEN_BOTH)
12823 {
12824 int width, height, ign;
12825
12826 x_real_positions (f, &f->output_data.x->left_pos,
12827 &f->output_data.x->top_pos);
12828
12829 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
12830
12831 /* We do not need to move the window, it shall be taken care of
12832 when setting WM manager hints.
12833 If the frame is visible already, the position is checked by
12834 x_check_fullscreen_move. */
12835 if (f->width != width || f->height != height)
12836 {
12837 change_frame_size (f, height, width, 0, 1, 0);
12838 SET_FRAME_GARBAGED (f);
12839 cancel_mouse_face (f);
12840
12841 /* Wait for the change of frame size to occur */
12842 f->output_data.x->want_fullscreen |= FULLSCREEN_WAIT;
12843
12844 }
12845 }
12846 }
12847
12848 /* If frame parameters are set after the frame is mapped, we need to move
12849 the window. This is done in xfns.c.
12850 Some window managers moves the window to the right position, some
12851 moves the outer window manager window to the specified position.
12852 Here we check that we are in the right spot. If not, make a second
12853 move, assuming we are dealing with the second kind of window manager. */
12854 static void
12855 x_check_fullscreen_move (f)
12856 struct frame *f;
12857 {
12858 if (f->output_data.x->want_fullscreen & FULLSCREEN_MOVE_WAIT)
12859 {
12860 int expect_top = f->output_data.x->top_pos;
12861 int expect_left = f->output_data.x->left_pos;
12862
12863 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
12864 expect_top = 0;
12865 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
12866 expect_left = 0;
12867
12868 if (expect_top != f->output_data.x->top_pos
12869 || expect_left != f->output_data.x->left_pos)
12870 x_set_offset (f, expect_left, expect_top, 1);
12871
12872 /* Just do this once */
12873 f->output_data.x->want_fullscreen &= ~FULLSCREEN_MOVE_WAIT;
12874 }
12875 }
12876
12877
12878 /* Calculate fullscreen size. Return in *TOP_POS and *LEFT_POS the
12879 wanted positions of the WM window (not emacs window).
12880 Return in *WIDTH and *HEIGHT the wanted width and height of Emacs
12881 window (FRAME_X_WINDOW).
12882 */
12883 void
12884 x_fullscreen_adjust (f, width, height, top_pos, left_pos)
12885 struct frame *f;
12886 int *width;
12887 int *height;
12888 int *top_pos;
12889 int *left_pos;
12890 {
12891 int newwidth = f->width, newheight = f->height;
12892
12893 *top_pos = f->output_data.x->top_pos;
12894 *left_pos = f->output_data.x->left_pos;
12895
12896 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
12897 {
12898 int ph;
12899
12900 ph = FRAME_X_DISPLAY_INFO (f)->height;
12901 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
12902 ph = CHAR_TO_PIXEL_HEIGHT (f, newheight)
12903 - f->output_data.x->y_pixels_diff;
12904 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
12905 *top_pos = 0;
12906 }
12907
12908 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
12909 {
12910 int pw;
12911
12912 pw = FRAME_X_DISPLAY_INFO (f)->width;
12913 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
12914 pw = CHAR_TO_PIXEL_WIDTH (f, newwidth)
12915 - f->output_data.x->x_pixels_diff;
12916 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
12917 *left_pos = 0;
12918 }
12919
12920 *width = newwidth;
12921 *height = newheight;
12922 }
12923
12924
12925 /* Change the size of frame F's X window to COLS/ROWS in the case F
12926 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
12927 top-left-corner window gravity for this size change and subsequent
12928 size changes. Otherwise we leave the window gravity unchanged. */
12929
12930 static void
12931 x_set_window_size_1 (f, change_gravity, cols, rows)
12932 struct frame *f;
12933 int change_gravity;
12934 int cols, rows;
12935 {
12936 int pixelwidth, pixelheight;
12937
12938 check_frame_size (f, &rows, &cols);
12939 f->output_data.x->vertical_scroll_bar_extra
12940 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
12941 ? 0
12942 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
12943 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
12944 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
12945
12946 x_compute_fringe_widths (f, 0);
12947
12948 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
12949 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
12950
12951 f->output_data.x->win_gravity = NorthWestGravity;
12952 x_wm_set_size_hint (f, (long) 0, 0);
12953
12954 XSync (FRAME_X_DISPLAY (f), False);
12955 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12956 pixelwidth, pixelheight);
12957
12958 /* Now, strictly speaking, we can't be sure that this is accurate,
12959 but the window manager will get around to dealing with the size
12960 change request eventually, and we'll hear how it went when the
12961 ConfigureNotify event gets here.
12962
12963 We could just not bother storing any of this information here,
12964 and let the ConfigureNotify event set everything up, but that
12965 might be kind of confusing to the Lisp code, since size changes
12966 wouldn't be reported in the frame parameters until some random
12967 point in the future when the ConfigureNotify event arrives.
12968
12969 We pass 1 for DELAY since we can't run Lisp code inside of
12970 a BLOCK_INPUT. */
12971 change_frame_size (f, rows, cols, 0, 1, 0);
12972 PIXEL_WIDTH (f) = pixelwidth;
12973 PIXEL_HEIGHT (f) = pixelheight;
12974
12975 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
12976 receive in the ConfigureNotify event; if we get what we asked
12977 for, then the event won't cause the screen to become garbaged, so
12978 we have to make sure to do it here. */
12979 SET_FRAME_GARBAGED (f);
12980
12981 XFlush (FRAME_X_DISPLAY (f));
12982 }
12983
12984
12985 /* Call this to change the size of frame F's x-window.
12986 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
12987 for this size change and subsequent size changes.
12988 Otherwise we leave the window gravity unchanged. */
12989
12990 void
12991 x_set_window_size (f, change_gravity, cols, rows)
12992 struct frame *f;
12993 int change_gravity;
12994 int cols, rows;
12995 {
12996 BLOCK_INPUT;
12997
12998 #ifdef USE_X_TOOLKIT
12999
13000 if (f->output_data.x->widget != NULL)
13001 {
13002 /* The x and y position of the widget is clobbered by the
13003 call to XtSetValues within EmacsFrameSetCharSize.
13004 This is a real kludge, but I don't understand Xt so I can't
13005 figure out a correct fix. Can anyone else tell me? -- rms. */
13006 int xpos = f->output_data.x->widget->core.x;
13007 int ypos = f->output_data.x->widget->core.y;
13008 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
13009 f->output_data.x->widget->core.x = xpos;
13010 f->output_data.x->widget->core.y = ypos;
13011 }
13012 else
13013 x_set_window_size_1 (f, change_gravity, cols, rows);
13014
13015 #else /* not USE_X_TOOLKIT */
13016
13017 x_set_window_size_1 (f, change_gravity, cols, rows);
13018
13019 #endif /* not USE_X_TOOLKIT */
13020
13021 /* If cursor was outside the new size, mark it as off. */
13022 mark_window_cursors_off (XWINDOW (f->root_window));
13023
13024 /* Clear out any recollection of where the mouse highlighting was,
13025 since it might be in a place that's outside the new frame size.
13026 Actually checking whether it is outside is a pain in the neck,
13027 so don't try--just let the highlighting be done afresh with new size. */
13028 cancel_mouse_face (f);
13029
13030 UNBLOCK_INPUT;
13031 }
13032 \f
13033 /* Mouse warping. */
13034
13035 void
13036 x_set_mouse_position (f, x, y)
13037 struct frame *f;
13038 int x, y;
13039 {
13040 int pix_x, pix_y;
13041
13042 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
13043 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
13044
13045 if (pix_x < 0) pix_x = 0;
13046 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
13047
13048 if (pix_y < 0) pix_y = 0;
13049 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
13050
13051 BLOCK_INPUT;
13052
13053 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13054 0, 0, 0, 0, pix_x, pix_y);
13055 UNBLOCK_INPUT;
13056 }
13057
13058 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
13059
13060 void
13061 x_set_mouse_pixel_position (f, pix_x, pix_y)
13062 struct frame *f;
13063 int pix_x, pix_y;
13064 {
13065 BLOCK_INPUT;
13066
13067 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13068 0, 0, 0, 0, pix_x, pix_y);
13069 UNBLOCK_INPUT;
13070 }
13071 \f
13072 /* focus shifting, raising and lowering. */
13073
13074 void
13075 x_focus_on_frame (f)
13076 struct frame *f;
13077 {
13078 #if 0 /* This proves to be unpleasant. */
13079 x_raise_frame (f);
13080 #endif
13081 #if 0
13082 /* I don't think that the ICCCM allows programs to do things like this
13083 without the interaction of the window manager. Whatever you end up
13084 doing with this code, do it to x_unfocus_frame too. */
13085 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
13086 RevertToPointerRoot, CurrentTime);
13087 #endif /* ! 0 */
13088 }
13089
13090 void
13091 x_unfocus_frame (f)
13092 struct frame *f;
13093 {
13094 #if 0
13095 /* Look at the remarks in x_focus_on_frame. */
13096 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
13097 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
13098 RevertToPointerRoot, CurrentTime);
13099 #endif /* ! 0 */
13100 }
13101
13102 /* Raise frame F. */
13103
13104 void
13105 x_raise_frame (f)
13106 struct frame *f;
13107 {
13108 if (f->async_visible)
13109 {
13110 BLOCK_INPUT;
13111 #ifdef USE_X_TOOLKIT
13112 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
13113 #else /* not USE_X_TOOLKIT */
13114 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13115 #endif /* not USE_X_TOOLKIT */
13116 XFlush (FRAME_X_DISPLAY (f));
13117 UNBLOCK_INPUT;
13118 }
13119 }
13120
13121 /* Lower frame F. */
13122
13123 void
13124 x_lower_frame (f)
13125 struct frame *f;
13126 {
13127 if (f->async_visible)
13128 {
13129 BLOCK_INPUT;
13130 #ifdef USE_X_TOOLKIT
13131 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
13132 #else /* not USE_X_TOOLKIT */
13133 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13134 #endif /* not USE_X_TOOLKIT */
13135 XFlush (FRAME_X_DISPLAY (f));
13136 UNBLOCK_INPUT;
13137 }
13138 }
13139
13140 static void
13141 XTframe_raise_lower (f, raise_flag)
13142 FRAME_PTR f;
13143 int raise_flag;
13144 {
13145 if (raise_flag)
13146 x_raise_frame (f);
13147 else
13148 x_lower_frame (f);
13149 }
13150 \f
13151 /* Change of visibility. */
13152
13153 /* This tries to wait until the frame is really visible.
13154 However, if the window manager asks the user where to position
13155 the frame, this will return before the user finishes doing that.
13156 The frame will not actually be visible at that time,
13157 but it will become visible later when the window manager
13158 finishes with it. */
13159
13160 void
13161 x_make_frame_visible (f)
13162 struct frame *f;
13163 {
13164 Lisp_Object type;
13165 int original_top, original_left;
13166 int retry_count = 2;
13167
13168 retry:
13169
13170 BLOCK_INPUT;
13171
13172 type = x_icon_type (f);
13173 if (!NILP (type))
13174 x_bitmap_icon (f, type);
13175
13176 if (! FRAME_VISIBLE_P (f))
13177 {
13178 /* We test FRAME_GARBAGED_P here to make sure we don't
13179 call x_set_offset a second time
13180 if we get to x_make_frame_visible a second time
13181 before the window gets really visible. */
13182 if (! FRAME_ICONIFIED_P (f)
13183 && ! f->output_data.x->asked_for_visible)
13184 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13185
13186 f->output_data.x->asked_for_visible = 1;
13187
13188 if (! EQ (Vx_no_window_manager, Qt))
13189 x_wm_set_window_state (f, NormalState);
13190 #ifdef USE_X_TOOLKIT
13191 /* This was XtPopup, but that did nothing for an iconified frame. */
13192 XtMapWidget (f->output_data.x->widget);
13193 #else /* not USE_X_TOOLKIT */
13194 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13195 #endif /* not USE_X_TOOLKIT */
13196 #if 0 /* This seems to bring back scroll bars in the wrong places
13197 if the window configuration has changed. They seem
13198 to come back ok without this. */
13199 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
13200 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13201 #endif
13202 }
13203
13204 XFlush (FRAME_X_DISPLAY (f));
13205
13206 /* Synchronize to ensure Emacs knows the frame is visible
13207 before we do anything else. We do this loop with input not blocked
13208 so that incoming events are handled. */
13209 {
13210 Lisp_Object frame;
13211 int count;
13212 /* This must be before UNBLOCK_INPUT
13213 since events that arrive in response to the actions above
13214 will set it when they are handled. */
13215 int previously_visible = f->output_data.x->has_been_visible;
13216
13217 original_left = f->output_data.x->left_pos;
13218 original_top = f->output_data.x->top_pos;
13219
13220 /* This must come after we set COUNT. */
13221 UNBLOCK_INPUT;
13222
13223 /* We unblock here so that arriving X events are processed. */
13224
13225 /* Now move the window back to where it was "supposed to be".
13226 But don't do it if the gravity is negative.
13227 When the gravity is negative, this uses a position
13228 that is 3 pixels too low. Perhaps that's really the border width.
13229
13230 Don't do this if the window has never been visible before,
13231 because the window manager may choose the position
13232 and we don't want to override it. */
13233
13234 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
13235 && f->output_data.x->win_gravity == NorthWestGravity
13236 && previously_visible)
13237 {
13238 Drawable rootw;
13239 int x, y;
13240 unsigned int width, height, border, depth;
13241
13242 BLOCK_INPUT;
13243
13244 /* On some window managers (such as FVWM) moving an existing
13245 window, even to the same place, causes the window manager
13246 to introduce an offset. This can cause the window to move
13247 to an unexpected location. Check the geometry (a little
13248 slow here) and then verify that the window is in the right
13249 place. If the window is not in the right place, move it
13250 there, and take the potential window manager hit. */
13251 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13252 &rootw, &x, &y, &width, &height, &border, &depth);
13253
13254 if (original_left != x || original_top != y)
13255 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13256 original_left, original_top);
13257
13258 UNBLOCK_INPUT;
13259 }
13260
13261 XSETFRAME (frame, f);
13262
13263 /* Wait until the frame is visible. Process X events until a
13264 MapNotify event has been seen, or until we think we won't get a
13265 MapNotify at all.. */
13266 for (count = input_signal_count + 10;
13267 input_signal_count < count && !FRAME_VISIBLE_P (f);)
13268 {
13269 /* Force processing of queued events. */
13270 x_sync (f);
13271
13272 /* Machines that do polling rather than SIGIO have been
13273 observed to go into a busy-wait here. So we'll fake an
13274 alarm signal to let the handler know that there's something
13275 to be read. We used to raise a real alarm, but it seems
13276 that the handler isn't always enabled here. This is
13277 probably a bug. */
13278 if (input_polling_used ())
13279 {
13280 /* It could be confusing if a real alarm arrives while
13281 processing the fake one. Turn it off and let the
13282 handler reset it. */
13283 extern void poll_for_input_1 P_ ((void));
13284 int old_poll_suppress_count = poll_suppress_count;
13285 poll_suppress_count = 1;
13286 poll_for_input_1 ();
13287 poll_suppress_count = old_poll_suppress_count;
13288 }
13289
13290 /* See if a MapNotify event has been processed. */
13291 FRAME_SAMPLE_VISIBILITY (f);
13292 }
13293
13294 /* 2000-09-28: In
13295
13296 (let ((f (selected-frame)))
13297 (iconify-frame f)
13298 (raise-frame f))
13299
13300 the frame is not raised with various window managers on
13301 FreeBSD, Linux and Solaris. It turns out that, for some
13302 unknown reason, the call to XtMapWidget is completely ignored.
13303 Mapping the widget a second time works. */
13304
13305 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
13306 goto retry;
13307 }
13308 }
13309
13310 /* Change from mapped state to withdrawn state. */
13311
13312 /* Make the frame visible (mapped and not iconified). */
13313
13314 void
13315 x_make_frame_invisible (f)
13316 struct frame *f;
13317 {
13318 Window window;
13319
13320 #ifdef USE_X_TOOLKIT
13321 /* Use the frame's outermost window, not the one we normally draw on. */
13322 window = XtWindow (f->output_data.x->widget);
13323 #else /* not USE_X_TOOLKIT */
13324 window = FRAME_X_WINDOW (f);
13325 #endif /* not USE_X_TOOLKIT */
13326
13327 /* Don't keep the highlight on an invisible frame. */
13328 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13329 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
13330
13331 #if 0/* This might add unreliability; I don't trust it -- rms. */
13332 if (! f->async_visible && ! f->async_iconified)
13333 return;
13334 #endif
13335
13336 BLOCK_INPUT;
13337
13338 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
13339 that the current position of the window is user-specified, rather than
13340 program-specified, so that when the window is mapped again, it will be
13341 placed at the same location, without forcing the user to position it
13342 by hand again (they have already done that once for this window.) */
13343 x_wm_set_size_hint (f, (long) 0, 1);
13344
13345 #ifdef HAVE_X11R4
13346
13347 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
13348 DefaultScreen (FRAME_X_DISPLAY (f))))
13349 {
13350 UNBLOCK_INPUT_RESIGNAL;
13351 error ("Can't notify window manager of window withdrawal");
13352 }
13353 #else /* ! defined (HAVE_X11R4) */
13354
13355 /* Tell the window manager what we're going to do. */
13356 if (! EQ (Vx_no_window_manager, Qt))
13357 {
13358 XEvent unmap;
13359
13360 unmap.xunmap.type = UnmapNotify;
13361 unmap.xunmap.window = window;
13362 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
13363 unmap.xunmap.from_configure = False;
13364 if (! XSendEvent (FRAME_X_DISPLAY (f),
13365 DefaultRootWindow (FRAME_X_DISPLAY (f)),
13366 False,
13367 SubstructureRedirectMaskSubstructureNotifyMask,
13368 &unmap))
13369 {
13370 UNBLOCK_INPUT_RESIGNAL;
13371 error ("Can't notify window manager of withdrawal");
13372 }
13373 }
13374
13375 /* Unmap the window ourselves. Cheeky! */
13376 XUnmapWindow (FRAME_X_DISPLAY (f), window);
13377 #endif /* ! defined (HAVE_X11R4) */
13378
13379 /* We can't distinguish this from iconification
13380 just by the event that we get from the server.
13381 So we can't win using the usual strategy of letting
13382 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
13383 and synchronize with the server to make sure we agree. */
13384 f->visible = 0;
13385 FRAME_ICONIFIED_P (f) = 0;
13386 f->async_visible = 0;
13387 f->async_iconified = 0;
13388
13389 x_sync (f);
13390
13391 UNBLOCK_INPUT;
13392 }
13393
13394 /* Change window state from mapped to iconified. */
13395
13396 void
13397 x_iconify_frame (f)
13398 struct frame *f;
13399 {
13400 int result;
13401 Lisp_Object type;
13402
13403 /* Don't keep the highlight on an invisible frame. */
13404 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13405 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
13406
13407 if (f->async_iconified)
13408 return;
13409
13410 BLOCK_INPUT;
13411
13412 FRAME_SAMPLE_VISIBILITY (f);
13413
13414 type = x_icon_type (f);
13415 if (!NILP (type))
13416 x_bitmap_icon (f, type);
13417
13418 #ifdef USE_X_TOOLKIT
13419
13420 if (! FRAME_VISIBLE_P (f))
13421 {
13422 if (! EQ (Vx_no_window_manager, Qt))
13423 x_wm_set_window_state (f, IconicState);
13424 /* This was XtPopup, but that did nothing for an iconified frame. */
13425 XtMapWidget (f->output_data.x->widget);
13426 /* The server won't give us any event to indicate
13427 that an invisible frame was changed to an icon,
13428 so we have to record it here. */
13429 f->iconified = 1;
13430 f->visible = 1;
13431 f->async_iconified = 1;
13432 f->async_visible = 0;
13433 UNBLOCK_INPUT;
13434 return;
13435 }
13436
13437 result = XIconifyWindow (FRAME_X_DISPLAY (f),
13438 XtWindow (f->output_data.x->widget),
13439 DefaultScreen (FRAME_X_DISPLAY (f)));
13440 UNBLOCK_INPUT;
13441
13442 if (!result)
13443 error ("Can't notify window manager of iconification");
13444
13445 f->async_iconified = 1;
13446 f->async_visible = 0;
13447
13448
13449 BLOCK_INPUT;
13450 XFlush (FRAME_X_DISPLAY (f));
13451 UNBLOCK_INPUT;
13452 #else /* not USE_X_TOOLKIT */
13453
13454 /* Make sure the X server knows where the window should be positioned,
13455 in case the user deiconifies with the window manager. */
13456 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
13457 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13458
13459 /* Since we don't know which revision of X we're running, we'll use both
13460 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
13461
13462 /* X11R4: send a ClientMessage to the window manager using the
13463 WM_CHANGE_STATE type. */
13464 {
13465 XEvent message;
13466
13467 message.xclient.window = FRAME_X_WINDOW (f);
13468 message.xclient.type = ClientMessage;
13469 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
13470 message.xclient.format = 32;
13471 message.xclient.data.l[0] = IconicState;
13472
13473 if (! XSendEvent (FRAME_X_DISPLAY (f),
13474 DefaultRootWindow (FRAME_X_DISPLAY (f)),
13475 False,
13476 SubstructureRedirectMask | SubstructureNotifyMask,
13477 &message))
13478 {
13479 UNBLOCK_INPUT_RESIGNAL;
13480 error ("Can't notify window manager of iconification");
13481 }
13482 }
13483
13484 /* X11R3: set the initial_state field of the window manager hints to
13485 IconicState. */
13486 x_wm_set_window_state (f, IconicState);
13487
13488 if (!FRAME_VISIBLE_P (f))
13489 {
13490 /* If the frame was withdrawn, before, we must map it. */
13491 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13492 }
13493
13494 f->async_iconified = 1;
13495 f->async_visible = 0;
13496
13497 XFlush (FRAME_X_DISPLAY (f));
13498 UNBLOCK_INPUT;
13499 #endif /* not USE_X_TOOLKIT */
13500 }
13501
13502 \f
13503 /* Free X resources of frame F. */
13504
13505 void
13506 x_free_frame_resources (f)
13507 struct frame *f;
13508 {
13509 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13510 Lisp_Object bar;
13511 struct scroll_bar *b;
13512
13513 BLOCK_INPUT;
13514
13515 /* If a display connection is dead, don't try sending more
13516 commands to the X server. */
13517 if (dpyinfo->display)
13518 {
13519 if (f->output_data.x->icon_desc)
13520 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
13521
13522 #ifdef USE_X_TOOLKIT
13523 /* Explicitly destroy the scroll bars of the frame. Without
13524 this, we get "BadDrawable" errors from the toolkit later on,
13525 presumably from expose events generated for the disappearing
13526 toolkit scroll bars. */
13527 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
13528 {
13529 b = XSCROLL_BAR (bar);
13530 x_scroll_bar_remove (b);
13531 }
13532 #endif
13533
13534 #ifdef HAVE_X_I18N
13535 if (FRAME_XIC (f))
13536 free_frame_xic (f);
13537 #endif
13538
13539 #ifdef USE_X_TOOLKIT
13540 if (f->output_data.x->widget)
13541 {
13542 XtDestroyWidget (f->output_data.x->widget);
13543 f->output_data.x->widget = NULL;
13544 }
13545 /* Tooltips don't have widgets, only a simple X window, even if
13546 we are using a toolkit. */
13547 else if (FRAME_X_WINDOW (f))
13548 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13549
13550 free_frame_menubar (f);
13551 #else /* !USE_X_TOOLKIT */
13552 if (FRAME_X_WINDOW (f))
13553 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13554 #endif /* !USE_X_TOOLKIT */
13555
13556 unload_color (f, f->output_data.x->foreground_pixel);
13557 unload_color (f, f->output_data.x->background_pixel);
13558 unload_color (f, f->output_data.x->cursor_pixel);
13559 unload_color (f, f->output_data.x->cursor_foreground_pixel);
13560 unload_color (f, f->output_data.x->border_pixel);
13561 unload_color (f, f->output_data.x->mouse_pixel);
13562
13563 if (f->output_data.x->scroll_bar_background_pixel != -1)
13564 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
13565 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
13566 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
13567 #ifdef USE_TOOLKIT_SCROLL_BARS
13568 /* Scrollbar shadow colors. */
13569 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
13570 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
13571 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
13572 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
13573 #endif /* USE_TOOLKIT_SCROLL_BARS */
13574 if (f->output_data.x->white_relief.allocated_p)
13575 unload_color (f, f->output_data.x->white_relief.pixel);
13576 if (f->output_data.x->black_relief.allocated_p)
13577 unload_color (f, f->output_data.x->black_relief.pixel);
13578
13579 if (FRAME_FACE_CACHE (f))
13580 free_frame_faces (f);
13581
13582 x_free_gcs (f);
13583 XFlush (FRAME_X_DISPLAY (f));
13584 }
13585
13586 if (f->output_data.x->saved_menu_event)
13587 xfree (f->output_data.x->saved_menu_event);
13588
13589 xfree (f->output_data.x);
13590 f->output_data.x = NULL;
13591
13592 if (f == dpyinfo->x_focus_frame)
13593 dpyinfo->x_focus_frame = 0;
13594 if (f == dpyinfo->x_focus_event_frame)
13595 dpyinfo->x_focus_event_frame = 0;
13596 if (f == dpyinfo->x_highlight_frame)
13597 dpyinfo->x_highlight_frame = 0;
13598
13599 if (f == dpyinfo->mouse_face_mouse_frame)
13600 {
13601 dpyinfo->mouse_face_beg_row
13602 = dpyinfo->mouse_face_beg_col = -1;
13603 dpyinfo->mouse_face_end_row
13604 = dpyinfo->mouse_face_end_col = -1;
13605 dpyinfo->mouse_face_window = Qnil;
13606 dpyinfo->mouse_face_deferred_gc = 0;
13607 dpyinfo->mouse_face_mouse_frame = 0;
13608 }
13609
13610 UNBLOCK_INPUT;
13611 }
13612
13613
13614 /* Destroy the X window of frame F. */
13615
13616 void
13617 x_destroy_window (f)
13618 struct frame *f;
13619 {
13620 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13621
13622 /* If a display connection is dead, don't try sending more
13623 commands to the X server. */
13624 if (dpyinfo->display != 0)
13625 x_free_frame_resources (f);
13626
13627 dpyinfo->reference_count--;
13628 }
13629
13630 \f
13631 /* Setting window manager hints. */
13632
13633 /* Set the normal size hints for the window manager, for frame F.
13634 FLAGS is the flags word to use--or 0 meaning preserve the flags
13635 that the window now has.
13636 If USER_POSITION is nonzero, we set the USPosition
13637 flag (this is useful when FLAGS is 0). */
13638
13639 void
13640 x_wm_set_size_hint (f, flags, user_position)
13641 struct frame *f;
13642 long flags;
13643 int user_position;
13644 {
13645 XSizeHints size_hints;
13646
13647 #ifdef USE_X_TOOLKIT
13648 Arg al[2];
13649 int ac = 0;
13650 Dimension widget_width, widget_height;
13651 Window window = XtWindow (f->output_data.x->widget);
13652 #else /* not USE_X_TOOLKIT */
13653 Window window = FRAME_X_WINDOW (f);
13654 #endif /* not USE_X_TOOLKIT */
13655
13656 /* Setting PMaxSize caused various problems. */
13657 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
13658
13659 size_hints.x = f->output_data.x->left_pos;
13660 size_hints.y = f->output_data.x->top_pos;
13661
13662 #ifdef USE_X_TOOLKIT
13663 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
13664 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
13665 XtGetValues (f->output_data.x->widget, al, ac);
13666 size_hints.height = widget_height;
13667 size_hints.width = widget_width;
13668 #else /* not USE_X_TOOLKIT */
13669 size_hints.height = PIXEL_HEIGHT (f);
13670 size_hints.width = PIXEL_WIDTH (f);
13671 #endif /* not USE_X_TOOLKIT */
13672
13673 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
13674 size_hints.height_inc = f->output_data.x->line_height;
13675 size_hints.max_width
13676 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
13677 size_hints.max_height
13678 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
13679
13680 /* Calculate the base and minimum sizes.
13681
13682 (When we use the X toolkit, we don't do it here.
13683 Instead we copy the values that the widgets are using, below.) */
13684 #ifndef USE_X_TOOLKIT
13685 {
13686 int base_width, base_height;
13687 int min_rows = 0, min_cols = 0;
13688
13689 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
13690 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
13691
13692 check_frame_size (f, &min_rows, &min_cols);
13693
13694 /* The window manager uses the base width hints to calculate the
13695 current number of rows and columns in the frame while
13696 resizing; min_width and min_height aren't useful for this
13697 purpose, since they might not give the dimensions for a
13698 zero-row, zero-column frame.
13699
13700 We use the base_width and base_height members if we have
13701 them; otherwise, we set the min_width and min_height members
13702 to the size for a zero x zero frame. */
13703
13704 #ifdef HAVE_X11R4
13705 size_hints.flags |= PBaseSize;
13706 size_hints.base_width = base_width;
13707 size_hints.base_height = base_height;
13708 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
13709 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
13710 #else
13711 size_hints.min_width = base_width;
13712 size_hints.min_height = base_height;
13713 #endif
13714 }
13715
13716 /* If we don't need the old flags, we don't need the old hint at all. */
13717 if (flags)
13718 {
13719 size_hints.flags |= flags;
13720 goto no_read;
13721 }
13722 #endif /* not USE_X_TOOLKIT */
13723
13724 {
13725 XSizeHints hints; /* Sometimes I hate X Windows... */
13726 long supplied_return;
13727 int value;
13728
13729 #ifdef HAVE_X11R4
13730 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
13731 &supplied_return);
13732 #else
13733 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
13734 #endif
13735
13736 #ifdef USE_X_TOOLKIT
13737 size_hints.base_height = hints.base_height;
13738 size_hints.base_width = hints.base_width;
13739 size_hints.min_height = hints.min_height;
13740 size_hints.min_width = hints.min_width;
13741 #endif
13742
13743 if (flags)
13744 size_hints.flags |= flags;
13745 else
13746 {
13747 if (value == 0)
13748 hints.flags = 0;
13749 if (hints.flags & PSize)
13750 size_hints.flags |= PSize;
13751 if (hints.flags & PPosition)
13752 size_hints.flags |= PPosition;
13753 if (hints.flags & USPosition)
13754 size_hints.flags |= USPosition;
13755 if (hints.flags & USSize)
13756 size_hints.flags |= USSize;
13757 }
13758 }
13759
13760 #ifndef USE_X_TOOLKIT
13761 no_read:
13762 #endif
13763
13764 #ifdef PWinGravity
13765 size_hints.win_gravity = f->output_data.x->win_gravity;
13766 size_hints.flags |= PWinGravity;
13767
13768 if (user_position)
13769 {
13770 size_hints.flags &= ~ PPosition;
13771 size_hints.flags |= USPosition;
13772 }
13773 #endif /* PWinGravity */
13774
13775 #ifdef HAVE_X11R4
13776 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13777 #else
13778 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13779 #endif
13780 }
13781
13782 /* Used for IconicState or NormalState */
13783
13784 void
13785 x_wm_set_window_state (f, state)
13786 struct frame *f;
13787 int state;
13788 {
13789 #ifdef USE_X_TOOLKIT
13790 Arg al[1];
13791
13792 XtSetArg (al[0], XtNinitialState, state);
13793 XtSetValues (f->output_data.x->widget, al, 1);
13794 #else /* not USE_X_TOOLKIT */
13795 Window window = FRAME_X_WINDOW (f);
13796
13797 f->output_data.x->wm_hints.flags |= StateHint;
13798 f->output_data.x->wm_hints.initial_state = state;
13799
13800 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13801 #endif /* not USE_X_TOOLKIT */
13802 }
13803
13804 void
13805 x_wm_set_icon_pixmap (f, pixmap_id)
13806 struct frame *f;
13807 int pixmap_id;
13808 {
13809 Pixmap icon_pixmap;
13810
13811 #ifndef USE_X_TOOLKIT
13812 Window window = FRAME_X_WINDOW (f);
13813 #endif
13814
13815 if (pixmap_id > 0)
13816 {
13817 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
13818 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
13819 }
13820 else
13821 {
13822 /* It seems there is no way to turn off use of an icon pixmap.
13823 The following line does it, only if no icon has yet been created,
13824 for some window managers. But with mwm it crashes.
13825 Some people say it should clear the IconPixmapHint bit in this case,
13826 but that doesn't work, and the X consortium said it isn't the
13827 right thing at all. Since there is no way to win,
13828 best to explicitly give up. */
13829 #if 0
13830 f->output_data.x->wm_hints.icon_pixmap = None;
13831 #else
13832 return;
13833 #endif
13834 }
13835
13836 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
13837
13838 {
13839 Arg al[1];
13840 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
13841 XtSetValues (f->output_data.x->widget, al, 1);
13842 }
13843
13844 #else /* not USE_X_TOOLKIT */
13845
13846 f->output_data.x->wm_hints.flags |= IconPixmapHint;
13847 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13848
13849 #endif /* not USE_X_TOOLKIT */
13850 }
13851
13852 void
13853 x_wm_set_icon_position (f, icon_x, icon_y)
13854 struct frame *f;
13855 int icon_x, icon_y;
13856 {
13857 #ifdef USE_X_TOOLKIT
13858 Window window = XtWindow (f->output_data.x->widget);
13859 #else
13860 Window window = FRAME_X_WINDOW (f);
13861 #endif
13862
13863 f->output_data.x->wm_hints.flags |= IconPositionHint;
13864 f->output_data.x->wm_hints.icon_x = icon_x;
13865 f->output_data.x->wm_hints.icon_y = icon_y;
13866
13867 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13868 }
13869
13870 \f
13871 /***********************************************************************
13872 Fonts
13873 ***********************************************************************/
13874
13875 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
13876
13877 struct font_info *
13878 x_get_font_info (f, font_idx)
13879 FRAME_PTR f;
13880 int font_idx;
13881 {
13882 return (FRAME_X_FONT_TABLE (f) + font_idx);
13883 }
13884
13885
13886 /* Return a list of names of available fonts matching PATTERN on frame F.
13887
13888 If SIZE is > 0, it is the size (maximum bounds width) of fonts
13889 to be listed.
13890
13891 SIZE < 0 means include scalable fonts.
13892
13893 Frame F null means we have not yet created any frame on X, and
13894 consult the first display in x_display_list. MAXNAMES sets a limit
13895 on how many fonts to match. */
13896
13897 Lisp_Object
13898 x_list_fonts (f, pattern, size, maxnames)
13899 struct frame *f;
13900 Lisp_Object pattern;
13901 int size;
13902 int maxnames;
13903 {
13904 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
13905 Lisp_Object tem, second_best;
13906 struct x_display_info *dpyinfo
13907 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
13908 Display *dpy = dpyinfo->display;
13909 int try_XLoadQueryFont = 0;
13910 int count;
13911 int allow_scalable_fonts_p = 0;
13912
13913 if (size < 0)
13914 {
13915 allow_scalable_fonts_p = 1;
13916 size = 0;
13917 }
13918
13919 patterns = Fassoc (pattern, Valternate_fontname_alist);
13920 if (NILP (patterns))
13921 patterns = Fcons (pattern, Qnil);
13922
13923 if (maxnames == 1 && !size)
13924 /* We can return any single font matching PATTERN. */
13925 try_XLoadQueryFont = 1;
13926
13927 for (; CONSP (patterns); patterns = XCDR (patterns))
13928 {
13929 int num_fonts;
13930 char **names = NULL;
13931
13932 pattern = XCAR (patterns);
13933 /* See if we cached the result for this particular query.
13934 The cache is an alist of the form:
13935 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
13936 tem = XCDR (dpyinfo->name_list_element);
13937 key = Fcons (Fcons (pattern, make_number (maxnames)),
13938 allow_scalable_fonts_p ? Qt : Qnil);
13939 list = Fassoc (key, tem);
13940 if (!NILP (list))
13941 {
13942 list = Fcdr_safe (list);
13943 /* We have a cashed list. Don't have to get the list again. */
13944 goto label_cached;
13945 }
13946
13947 /* At first, put PATTERN in the cache. */
13948
13949 BLOCK_INPUT;
13950 count = x_catch_errors (dpy);
13951
13952 if (try_XLoadQueryFont)
13953 {
13954 XFontStruct *font;
13955 unsigned long value;
13956
13957 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
13958 if (x_had_errors_p (dpy))
13959 {
13960 /* This error is perhaps due to insufficient memory on X
13961 server. Let's just ignore it. */
13962 font = NULL;
13963 x_clear_errors (dpy);
13964 }
13965
13966 if (font
13967 && XGetFontProperty (font, XA_FONT, &value))
13968 {
13969 char *name = (char *) XGetAtomName (dpy, (Atom) value);
13970 int len = strlen (name);
13971 char *tmp;
13972
13973 /* If DXPC (a Differential X Protocol Compressor)
13974 Ver.3.7 is running, XGetAtomName will return null
13975 string. We must avoid such a name. */
13976 if (len == 0)
13977 try_XLoadQueryFont = 0;
13978 else
13979 {
13980 num_fonts = 1;
13981 names = (char **) alloca (sizeof (char *));
13982 /* Some systems only allow alloca assigned to a
13983 simple var. */
13984 tmp = (char *) alloca (len + 1); names[0] = tmp;
13985 bcopy (name, names[0], len + 1);
13986 XFree (name);
13987 }
13988 }
13989 else
13990 try_XLoadQueryFont = 0;
13991
13992 if (font)
13993 XFreeFont (dpy, font);
13994 }
13995
13996 if (!try_XLoadQueryFont)
13997 {
13998 /* We try at least 10 fonts because XListFonts will return
13999 auto-scaled fonts at the head. */
14000 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
14001 &num_fonts);
14002 if (x_had_errors_p (dpy))
14003 {
14004 /* This error is perhaps due to insufficient memory on X
14005 server. Let's just ignore it. */
14006 names = NULL;
14007 x_clear_errors (dpy);
14008 }
14009 }
14010
14011 x_uncatch_errors (dpy, count);
14012 UNBLOCK_INPUT;
14013
14014 if (names)
14015 {
14016 int i;
14017
14018 /* Make a list of all the fonts we got back.
14019 Store that in the font cache for the display. */
14020 for (i = 0; i < num_fonts; i++)
14021 {
14022 int width = 0;
14023 char *p = names[i];
14024 int average_width = -1, dashes = 0;
14025
14026 /* Count the number of dashes in NAMES[I]. If there are
14027 14 dashes, and the field value following 12th dash
14028 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
14029 is usually too ugly to be used for editing. Let's
14030 ignore it. */
14031 while (*p)
14032 if (*p++ == '-')
14033 {
14034 dashes++;
14035 if (dashes == 7) /* PIXEL_SIZE field */
14036 width = atoi (p);
14037 else if (dashes == 12) /* AVERAGE_WIDTH field */
14038 average_width = atoi (p);
14039 }
14040
14041 if (allow_scalable_fonts_p
14042 || dashes < 14 || average_width != 0)
14043 {
14044 tem = build_string (names[i]);
14045 if (NILP (Fassoc (tem, list)))
14046 {
14047 if (STRINGP (Vx_pixel_size_width_font_regexp)
14048 && ((fast_c_string_match_ignore_case
14049 (Vx_pixel_size_width_font_regexp, names[i]))
14050 >= 0))
14051 /* We can set the value of PIXEL_SIZE to the
14052 width of this font. */
14053 list = Fcons (Fcons (tem, make_number (width)), list);
14054 else
14055 /* For the moment, width is not known. */
14056 list = Fcons (Fcons (tem, Qnil), list);
14057 }
14058 }
14059 }
14060
14061 if (!try_XLoadQueryFont)
14062 {
14063 BLOCK_INPUT;
14064 XFreeFontNames (names);
14065 UNBLOCK_INPUT;
14066 }
14067 }
14068
14069 /* Now store the result in the cache. */
14070 XSETCDR (dpyinfo->name_list_element,
14071 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
14072
14073 label_cached:
14074 if (NILP (list)) continue; /* Try the remaining alternatives. */
14075
14076 newlist = second_best = Qnil;
14077 /* Make a list of the fonts that have the right width. */
14078 for (; CONSP (list); list = XCDR (list))
14079 {
14080 int found_size;
14081
14082 tem = XCAR (list);
14083
14084 if (!CONSP (tem) || NILP (XCAR (tem)))
14085 continue;
14086 if (!size)
14087 {
14088 newlist = Fcons (XCAR (tem), newlist);
14089 continue;
14090 }
14091
14092 if (!INTEGERP (XCDR (tem)))
14093 {
14094 /* Since we have not yet known the size of this font, we
14095 must try slow function call XLoadQueryFont. */
14096 XFontStruct *thisinfo;
14097
14098 BLOCK_INPUT;
14099 count = x_catch_errors (dpy);
14100 thisinfo = XLoadQueryFont (dpy,
14101 XSTRING (XCAR (tem))->data);
14102 if (x_had_errors_p (dpy))
14103 {
14104 /* This error is perhaps due to insufficient memory on X
14105 server. Let's just ignore it. */
14106 thisinfo = NULL;
14107 x_clear_errors (dpy);
14108 }
14109 x_uncatch_errors (dpy, count);
14110 UNBLOCK_INPUT;
14111
14112 if (thisinfo)
14113 {
14114 XSETCDR (tem,
14115 (thisinfo->min_bounds.width == 0
14116 ? make_number (0)
14117 : make_number (thisinfo->max_bounds.width)));
14118 BLOCK_INPUT;
14119 XFreeFont (dpy, thisinfo);
14120 UNBLOCK_INPUT;
14121 }
14122 else
14123 /* For unknown reason, the previous call of XListFont had
14124 returned a font which can't be opened. Record the size
14125 as 0 not to try to open it again. */
14126 XSETCDR (tem, make_number (0));
14127 }
14128
14129 found_size = XINT (XCDR (tem));
14130 if (found_size == size)
14131 newlist = Fcons (XCAR (tem), newlist);
14132 else if (found_size > 0)
14133 {
14134 if (NILP (second_best))
14135 second_best = tem;
14136 else if (found_size < size)
14137 {
14138 if (XINT (XCDR (second_best)) > size
14139 || XINT (XCDR (second_best)) < found_size)
14140 second_best = tem;
14141 }
14142 else
14143 {
14144 if (XINT (XCDR (second_best)) > size
14145 && XINT (XCDR (second_best)) > found_size)
14146 second_best = tem;
14147 }
14148 }
14149 }
14150 if (!NILP (newlist))
14151 break;
14152 else if (!NILP (second_best))
14153 {
14154 newlist = Fcons (XCAR (second_best), Qnil);
14155 break;
14156 }
14157 }
14158
14159 return newlist;
14160 }
14161
14162
14163 #if GLYPH_DEBUG
14164
14165 /* Check that FONT is valid on frame F. It is if it can be found in F's
14166 font table. */
14167
14168 static void
14169 x_check_font (f, font)
14170 struct frame *f;
14171 XFontStruct *font;
14172 {
14173 int i;
14174 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14175
14176 xassert (font != NULL);
14177
14178 for (i = 0; i < dpyinfo->n_fonts; i++)
14179 if (dpyinfo->font_table[i].name
14180 && font == dpyinfo->font_table[i].font)
14181 break;
14182
14183 xassert (i < dpyinfo->n_fonts);
14184 }
14185
14186 #endif /* GLYPH_DEBUG != 0 */
14187
14188 /* Set *W to the minimum width, *H to the minimum font height of FONT.
14189 Note: There are (broken) X fonts out there with invalid XFontStruct
14190 min_bounds contents. For example, handa@etl.go.jp reports that
14191 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
14192 have font->min_bounds.width == 0. */
14193
14194 static INLINE void
14195 x_font_min_bounds (font, w, h)
14196 XFontStruct *font;
14197 int *w, *h;
14198 {
14199 *h = FONT_HEIGHT (font);
14200 *w = font->min_bounds.width;
14201
14202 /* Try to handle the case where FONT->min_bounds has invalid
14203 contents. Since the only font known to have invalid min_bounds
14204 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
14205 if (*w <= 0)
14206 *w = font->max_bounds.width;
14207 }
14208
14209
14210 /* Compute the smallest character width and smallest font height over
14211 all fonts available on frame F. Set the members smallest_char_width
14212 and smallest_font_height in F's x_display_info structure to
14213 the values computed. Value is non-zero if smallest_font_height or
14214 smallest_char_width become smaller than they were before. */
14215
14216 static int
14217 x_compute_min_glyph_bounds (f)
14218 struct frame *f;
14219 {
14220 int i;
14221 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14222 XFontStruct *font;
14223 int old_width = dpyinfo->smallest_char_width;
14224 int old_height = dpyinfo->smallest_font_height;
14225
14226 dpyinfo->smallest_font_height = 100000;
14227 dpyinfo->smallest_char_width = 100000;
14228
14229 for (i = 0; i < dpyinfo->n_fonts; ++i)
14230 if (dpyinfo->font_table[i].name)
14231 {
14232 struct font_info *fontp = dpyinfo->font_table + i;
14233 int w, h;
14234
14235 font = (XFontStruct *) fontp->font;
14236 xassert (font != (XFontStruct *) ~0);
14237 x_font_min_bounds (font, &w, &h);
14238
14239 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
14240 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
14241 }
14242
14243 xassert (dpyinfo->smallest_char_width > 0
14244 && dpyinfo->smallest_font_height > 0);
14245
14246 return (dpyinfo->n_fonts == 1
14247 || dpyinfo->smallest_char_width < old_width
14248 || dpyinfo->smallest_font_height < old_height);
14249 }
14250
14251
14252 /* Load font named FONTNAME of the size SIZE for frame F, and return a
14253 pointer to the structure font_info while allocating it dynamically.
14254 If SIZE is 0, load any size of font.
14255 If loading is failed, return NULL. */
14256
14257 struct font_info *
14258 x_load_font (f, fontname, size)
14259 struct frame *f;
14260 register char *fontname;
14261 int size;
14262 {
14263 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14264 Lisp_Object font_names;
14265 int count;
14266
14267 /* Get a list of all the fonts that match this name. Once we
14268 have a list of matching fonts, we compare them against the fonts
14269 we already have by comparing names. */
14270 font_names = x_list_fonts (f, build_string (fontname), size, 1);
14271
14272 if (!NILP (font_names))
14273 {
14274 Lisp_Object tail;
14275 int i;
14276
14277 for (i = 0; i < dpyinfo->n_fonts; i++)
14278 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
14279 if (dpyinfo->font_table[i].name
14280 && (!strcmp (dpyinfo->font_table[i].name,
14281 XSTRING (XCAR (tail))->data)
14282 || !strcmp (dpyinfo->font_table[i].full_name,
14283 XSTRING (XCAR (tail))->data)))
14284 return (dpyinfo->font_table + i);
14285 }
14286
14287 /* Load the font and add it to the table. */
14288 {
14289 char *full_name;
14290 XFontStruct *font;
14291 struct font_info *fontp;
14292 unsigned long value;
14293 int i;
14294
14295 /* If we have found fonts by x_list_font, load one of them. If
14296 not, we still try to load a font by the name given as FONTNAME
14297 because XListFonts (called in x_list_font) of some X server has
14298 a bug of not finding a font even if the font surely exists and
14299 is loadable by XLoadQueryFont. */
14300 if (size > 0 && !NILP (font_names))
14301 fontname = (char *) XSTRING (XCAR (font_names))->data;
14302
14303 BLOCK_INPUT;
14304 count = x_catch_errors (FRAME_X_DISPLAY (f));
14305 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
14306 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
14307 {
14308 /* This error is perhaps due to insufficient memory on X
14309 server. Let's just ignore it. */
14310 font = NULL;
14311 x_clear_errors (FRAME_X_DISPLAY (f));
14312 }
14313 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
14314 UNBLOCK_INPUT;
14315 if (!font)
14316 return NULL;
14317
14318 /* Find a free slot in the font table. */
14319 for (i = 0; i < dpyinfo->n_fonts; ++i)
14320 if (dpyinfo->font_table[i].name == NULL)
14321 break;
14322
14323 /* If no free slot found, maybe enlarge the font table. */
14324 if (i == dpyinfo->n_fonts
14325 && dpyinfo->n_fonts == dpyinfo->font_table_size)
14326 {
14327 int sz;
14328 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
14329 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
14330 dpyinfo->font_table
14331 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
14332 }
14333
14334 fontp = dpyinfo->font_table + i;
14335 if (i == dpyinfo->n_fonts)
14336 ++dpyinfo->n_fonts;
14337
14338 /* Now fill in the slots of *FONTP. */
14339 BLOCK_INPUT;
14340 fontp->font = font;
14341 fontp->font_idx = i;
14342 fontp->charset = -1; /* fs_load_font sets it. */
14343 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
14344 bcopy (fontname, fontp->name, strlen (fontname) + 1);
14345
14346 /* Try to get the full name of FONT. Put it in FULL_NAME. */
14347 full_name = 0;
14348 if (XGetFontProperty (font, XA_FONT, &value))
14349 {
14350 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
14351 char *p = name;
14352 int dashes = 0;
14353
14354 /* Count the number of dashes in the "full name".
14355 If it is too few, this isn't really the font's full name,
14356 so don't use it.
14357 In X11R4, the fonts did not come with their canonical names
14358 stored in them. */
14359 while (*p)
14360 {
14361 if (*p == '-')
14362 dashes++;
14363 p++;
14364 }
14365
14366 if (dashes >= 13)
14367 {
14368 full_name = (char *) xmalloc (p - name + 1);
14369 bcopy (name, full_name, p - name + 1);
14370 }
14371
14372 XFree (name);
14373 }
14374
14375 if (full_name != 0)
14376 fontp->full_name = full_name;
14377 else
14378 fontp->full_name = fontp->name;
14379
14380 fontp->size = font->max_bounds.width;
14381 fontp->height = FONT_HEIGHT (font);
14382
14383 if (NILP (font_names))
14384 {
14385 /* We come here because of a bug of XListFonts mentioned at
14386 the head of this block. Let's store this information in
14387 the cache for x_list_fonts. */
14388 Lisp_Object lispy_name = build_string (fontname);
14389 Lisp_Object lispy_full_name = build_string (fontp->full_name);
14390 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
14391 Qnil);
14392
14393 XSETCDR (dpyinfo->name_list_element,
14394 Fcons (Fcons (key,
14395 Fcons (Fcons (lispy_full_name,
14396 make_number (fontp->size)),
14397 Qnil)),
14398 XCDR (dpyinfo->name_list_element)));
14399 if (full_name)
14400 {
14401 key = Fcons (Fcons (lispy_full_name, make_number (256)),
14402 Qnil);
14403 XSETCDR (dpyinfo->name_list_element,
14404 Fcons (Fcons (key,
14405 Fcons (Fcons (lispy_full_name,
14406 make_number (fontp->size)),
14407 Qnil)),
14408 XCDR (dpyinfo->name_list_element)));
14409 }
14410 }
14411
14412 /* The slot `encoding' specifies how to map a character
14413 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
14414 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
14415 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
14416 2:0xA020..0xFF7F). For the moment, we don't know which charset
14417 uses this font. So, we set information in fontp->encoding[1]
14418 which is never used by any charset. If mapping can't be
14419 decided, set FONT_ENCODING_NOT_DECIDED. */
14420 fontp->encoding_type
14421 = (font->max_byte1 == 0
14422 /* 1-byte font */
14423 ? (font->min_char_or_byte2 < 0x80
14424 ? (font->max_char_or_byte2 < 0x80
14425 ? 0 /* 0x20..0x7F */
14426 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
14427 : 1) /* 0xA0..0xFF */
14428 /* 2-byte font */
14429 : (font->min_byte1 < 0x80
14430 ? (font->max_byte1 < 0x80
14431 ? (font->min_char_or_byte2 < 0x80
14432 ? (font->max_char_or_byte2 < 0x80
14433 ? 0 /* 0x2020..0x7F7F */
14434 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
14435 : 3) /* 0x20A0..0x7FFF */
14436 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
14437 : (font->min_char_or_byte2 < 0x80
14438 ? (font->max_char_or_byte2 < 0x80
14439 ? 2 /* 0xA020..0xFF7F */
14440 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
14441 : 1))); /* 0xA0A0..0xFFFF */
14442
14443 fontp->baseline_offset
14444 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
14445 ? (long) value : 0);
14446 fontp->relative_compose
14447 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
14448 ? (long) value : 0);
14449 fontp->default_ascent
14450 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
14451 ? (long) value : 0);
14452
14453 /* Set global flag fonts_changed_p to non-zero if the font loaded
14454 has a character with a smaller width than any other character
14455 before, or if the font loaded has a smaller height than any
14456 other font loaded before. If this happens, it will make a
14457 glyph matrix reallocation necessary. */
14458 fonts_changed_p |= x_compute_min_glyph_bounds (f);
14459 UNBLOCK_INPUT;
14460 return fontp;
14461 }
14462 }
14463
14464
14465 /* Return a pointer to struct font_info of a font named FONTNAME for
14466 frame F. If no such font is loaded, return NULL. */
14467
14468 struct font_info *
14469 x_query_font (f, fontname)
14470 struct frame *f;
14471 register char *fontname;
14472 {
14473 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14474 int i;
14475
14476 for (i = 0; i < dpyinfo->n_fonts; i++)
14477 if (dpyinfo->font_table[i].name
14478 && (!strcmp (dpyinfo->font_table[i].name, fontname)
14479 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
14480 return (dpyinfo->font_table + i);
14481 return NULL;
14482 }
14483
14484
14485 /* Find a CCL program for a font specified by FONTP, and set the member
14486 `encoder' of the structure. */
14487
14488 void
14489 x_find_ccl_program (fontp)
14490 struct font_info *fontp;
14491 {
14492 Lisp_Object list, elt;
14493
14494 elt = Qnil;
14495 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
14496 {
14497 elt = XCAR (list);
14498 if (CONSP (elt)
14499 && STRINGP (XCAR (elt))
14500 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
14501 >= 0)
14502 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
14503 >= 0)))
14504 break;
14505 }
14506
14507 if (! NILP (list))
14508 {
14509 struct ccl_program *ccl
14510 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
14511
14512 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
14513 xfree (ccl);
14514 else
14515 fontp->font_encoder = ccl;
14516 }
14517 }
14518
14519
14520 /* Return a char-table whose elements are t if the font FONT_INFO
14521 contains a glyph for the corresponding character, and nil if not.
14522
14523 Fixme: For the moment, this function works only for fonts whose
14524 glyph encoding is the same as Unicode (e.g. ISO10646-1 fonts). */
14525
14526 Lisp_Object
14527 x_get_font_repertory (f, font_info)
14528 FRAME_PTR f;
14529 struct font_info *font_info;
14530 {
14531 XFontStruct *font = (XFontStruct *) font_info->font;
14532 Lisp_Object table;
14533 int min_byte1, max_byte1, min_byte2, max_byte2;
14534
14535 table = Fmake_char_table (Qnil, Qnil);
14536
14537 min_byte1 = font->min_byte1;
14538 max_byte1 = font->max_byte1;
14539 min_byte2 = font->min_char_or_byte2;
14540 max_byte2 = font->max_char_or_byte2;
14541 if (min_byte1 == 0 && max_byte1 == 0)
14542 {
14543 if (! font->per_char || font->all_chars_exist == True)
14544 char_table_set_range (table, min_byte2, max_byte2, Qt);
14545 else
14546 {
14547 XCharStruct *pcm = font->per_char;
14548 int from = -1;
14549 int i;
14550
14551 for (i = min_byte2; i <= max_byte2; i++, pcm++)
14552 {
14553 if (pcm->width == 0 && pcm->rbearing == pcm->lbearing)
14554 {
14555 if (from >= 0)
14556 {
14557 char_table_set_range (table, from, i - 1, Qt);
14558 from = -1;
14559 }
14560 }
14561 else if (from < 0)
14562 from = i;
14563 }
14564 if (from >= 0)
14565 char_table_set_range (table, from, i - 1, Qt);
14566 }
14567 }
14568 else
14569 {
14570 if (! font->per_char || font->all_chars_exist == True)
14571 {
14572 int i;
14573
14574 for (i = min_byte1; i <= max_byte1; i++)
14575 char_table_set_range (table,
14576 (i << 8) | min_byte2, (i << 8) | max_byte2,
14577 Qt);
14578 }
14579 else
14580 {
14581 XCharStruct *pcm = font->per_char;
14582 int i;
14583
14584 for (i = min_byte1; i <= max_byte1; i++)
14585 {
14586 int from = -1;
14587 int j;
14588
14589 for (j = min_byte2; j <= max_byte2; j++, pcm++)
14590 {
14591 if (pcm->width == 0 && pcm->rbearing == pcm->lbearing)
14592 {
14593 if (from >= 0)
14594 {
14595 char_table_set_range (table, (i << 8) | from,
14596 (i << 8) | (j - 1), Qt);
14597 from = -1;
14598 }
14599 }
14600 else if (from < 0)
14601 from = j;
14602 }
14603 if (from >= 0)
14604 char_table_set_range (table, (i << 8) | from,
14605 (i << 8) | (j - 1), Qt);
14606 }
14607 }
14608 }
14609
14610 return table;
14611 }
14612
14613
14614 \f
14615 /***********************************************************************
14616 Initialization
14617 ***********************************************************************/
14618
14619 #ifdef USE_X_TOOLKIT
14620 static XrmOptionDescRec emacs_options[] = {
14621 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
14622 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
14623
14624 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
14625 XrmoptionSepArg, NULL},
14626 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
14627
14628 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14629 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14630 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14631 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14632 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14633 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
14634 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
14635 };
14636 #endif /* USE_X_TOOLKIT */
14637
14638 static int x_initialized;
14639
14640 #ifdef MULTI_KBOARD
14641 /* Test whether two display-name strings agree up to the dot that separates
14642 the screen number from the server number. */
14643 static int
14644 same_x_server (name1, name2)
14645 char *name1, *name2;
14646 {
14647 int seen_colon = 0;
14648 unsigned char *system_name = XSTRING (Vsystem_name)->data;
14649 int system_name_length = strlen (system_name);
14650 int length_until_period = 0;
14651
14652 while (system_name[length_until_period] != 0
14653 && system_name[length_until_period] != '.')
14654 length_until_period++;
14655
14656 /* Treat `unix' like an empty host name. */
14657 if (! strncmp (name1, "unix:", 5))
14658 name1 += 4;
14659 if (! strncmp (name2, "unix:", 5))
14660 name2 += 4;
14661 /* Treat this host's name like an empty host name. */
14662 if (! strncmp (name1, system_name, system_name_length)
14663 && name1[system_name_length] == ':')
14664 name1 += system_name_length;
14665 if (! strncmp (name2, system_name, system_name_length)
14666 && name2[system_name_length] == ':')
14667 name2 += system_name_length;
14668 /* Treat this host's domainless name like an empty host name. */
14669 if (! strncmp (name1, system_name, length_until_period)
14670 && name1[length_until_period] == ':')
14671 name1 += length_until_period;
14672 if (! strncmp (name2, system_name, length_until_period)
14673 && name2[length_until_period] == ':')
14674 name2 += length_until_period;
14675
14676 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
14677 {
14678 if (*name1 == ':')
14679 seen_colon++;
14680 if (seen_colon && *name1 == '.')
14681 return 1;
14682 }
14683 return (seen_colon
14684 && (*name1 == '.' || *name1 == '\0')
14685 && (*name2 == '.' || *name2 == '\0'));
14686 }
14687 #endif
14688
14689 struct x_display_info *
14690 x_term_init (display_name, xrm_option, resource_name)
14691 Lisp_Object display_name;
14692 char *xrm_option;
14693 char *resource_name;
14694 {
14695 int connection;
14696 Display *dpy;
14697 struct x_display_info *dpyinfo;
14698 XrmDatabase xrdb;
14699
14700 BLOCK_INPUT;
14701
14702 if (!x_initialized)
14703 {
14704 x_initialize ();
14705 x_initialized = 1;
14706 }
14707
14708 #ifdef USE_X_TOOLKIT
14709 /* weiner@footloose.sps.mot.com reports that this causes
14710 errors with X11R5:
14711 X protocol error: BadAtom (invalid Atom parameter)
14712 on protocol request 18skiloaf.
14713 So let's not use it until R6. */
14714 #ifdef HAVE_X11XTR6
14715 XtSetLanguageProc (NULL, NULL, NULL);
14716 #endif
14717
14718 {
14719 int argc = 0;
14720 char *argv[3];
14721
14722 argv[0] = "";
14723 argc = 1;
14724 if (xrm_option)
14725 {
14726 argv[argc++] = "-xrm";
14727 argv[argc++] = xrm_option;
14728 }
14729 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
14730 resource_name, EMACS_CLASS,
14731 emacs_options, XtNumber (emacs_options),
14732 &argc, argv);
14733
14734 #ifdef HAVE_X11XTR6
14735 /* I think this is to compensate for XtSetLanguageProc. */
14736 fixup_locale ();
14737 #endif
14738 }
14739
14740 #else /* not USE_X_TOOLKIT */
14741 #ifdef HAVE_X11R5
14742 XSetLocaleModifiers ("");
14743 #endif
14744 dpy = XOpenDisplay (XSTRING (display_name)->data);
14745 #endif /* not USE_X_TOOLKIT */
14746
14747 /* Detect failure. */
14748 if (dpy == 0)
14749 {
14750 UNBLOCK_INPUT;
14751 return 0;
14752 }
14753
14754 /* We have definitely succeeded. Record the new connection. */
14755
14756 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
14757 bzero (dpyinfo, sizeof *dpyinfo);
14758
14759 #ifdef MULTI_KBOARD
14760 {
14761 struct x_display_info *share;
14762 Lisp_Object tail;
14763
14764 for (share = x_display_list, tail = x_display_name_list; share;
14765 share = share->next, tail = XCDR (tail))
14766 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
14767 XSTRING (display_name)->data))
14768 break;
14769 if (share)
14770 dpyinfo->kboard = share->kboard;
14771 else
14772 {
14773 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
14774 init_kboard (dpyinfo->kboard);
14775 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
14776 {
14777 char *vendor = ServerVendor (dpy);
14778 UNBLOCK_INPUT;
14779 dpyinfo->kboard->Vsystem_key_alist
14780 = call1 (Qvendor_specific_keysyms,
14781 build_string (vendor ? vendor : ""));
14782 BLOCK_INPUT;
14783 }
14784
14785 dpyinfo->kboard->next_kboard = all_kboards;
14786 all_kboards = dpyinfo->kboard;
14787 /* Don't let the initial kboard remain current longer than necessary.
14788 That would cause problems if a file loaded on startup tries to
14789 prompt in the mini-buffer. */
14790 if (current_kboard == initial_kboard)
14791 current_kboard = dpyinfo->kboard;
14792 }
14793 dpyinfo->kboard->reference_count++;
14794 }
14795 #endif
14796
14797 /* Put this display on the chain. */
14798 dpyinfo->next = x_display_list;
14799 x_display_list = dpyinfo;
14800
14801 /* Put it on x_display_name_list as well, to keep them parallel. */
14802 x_display_name_list = Fcons (Fcons (display_name, Qnil),
14803 x_display_name_list);
14804 dpyinfo->name_list_element = XCAR (x_display_name_list);
14805
14806 dpyinfo->display = dpy;
14807
14808 #if 0
14809 XSetAfterFunction (x_current_display, x_trace_wire);
14810 #endif /* ! 0 */
14811
14812 dpyinfo->x_id_name
14813 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
14814 + STRING_BYTES (XSTRING (Vsystem_name))
14815 + 2);
14816 sprintf (dpyinfo->x_id_name, "%s@%s",
14817 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
14818
14819 /* Figure out which modifier bits mean what. */
14820 x_find_modifier_meanings (dpyinfo);
14821
14822 /* Get the scroll bar cursor. */
14823 dpyinfo->vertical_scroll_bar_cursor
14824 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
14825
14826 xrdb = x_load_resources (dpyinfo->display, xrm_option,
14827 resource_name, EMACS_CLASS);
14828 #ifdef HAVE_XRMSETDATABASE
14829 XrmSetDatabase (dpyinfo->display, xrdb);
14830 #else
14831 dpyinfo->display->db = xrdb;
14832 #endif
14833 /* Put the rdb where we can find it in a way that works on
14834 all versions. */
14835 dpyinfo->xrdb = xrdb;
14836
14837 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
14838 DefaultScreen (dpyinfo->display));
14839 select_visual (dpyinfo);
14840 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
14841 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
14842 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
14843 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
14844 dpyinfo->grabbed = 0;
14845 dpyinfo->reference_count = 0;
14846 dpyinfo->icon_bitmap_id = -1;
14847 dpyinfo->font_table = NULL;
14848 dpyinfo->n_fonts = 0;
14849 dpyinfo->font_table_size = 0;
14850 dpyinfo->bitmaps = 0;
14851 dpyinfo->bitmaps_size = 0;
14852 dpyinfo->bitmaps_last = 0;
14853 dpyinfo->scratch_cursor_gc = 0;
14854 dpyinfo->mouse_face_mouse_frame = 0;
14855 dpyinfo->mouse_face_deferred_gc = 0;
14856 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
14857 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
14858 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
14859 dpyinfo->mouse_face_window = Qnil;
14860 dpyinfo->mouse_face_overlay = Qnil;
14861 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
14862 dpyinfo->mouse_face_defer = 0;
14863 dpyinfo->mouse_face_hidden = 0;
14864 dpyinfo->x_focus_frame = 0;
14865 dpyinfo->x_focus_event_frame = 0;
14866 dpyinfo->x_highlight_frame = 0;
14867 dpyinfo->image_cache = make_image_cache ();
14868
14869 /* See if a private colormap is requested. */
14870 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
14871 {
14872 if (dpyinfo->visual->class == PseudoColor)
14873 {
14874 Lisp_Object value;
14875 value = display_x_get_resource (dpyinfo,
14876 build_string ("privateColormap"),
14877 build_string ("PrivateColormap"),
14878 Qnil, Qnil);
14879 if (STRINGP (value)
14880 && (!strcmp (XSTRING (value)->data, "true")
14881 || !strcmp (XSTRING (value)->data, "on")))
14882 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
14883 }
14884 }
14885 else
14886 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
14887 dpyinfo->visual, AllocNone);
14888
14889 {
14890 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
14891 double pixels = DisplayHeight (dpyinfo->display, screen_number);
14892 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
14893 dpyinfo->resy = pixels * 25.4 / mm;
14894 pixels = DisplayWidth (dpyinfo->display, screen_number);
14895 mm = DisplayWidthMM (dpyinfo->display, screen_number);
14896 dpyinfo->resx = pixels * 25.4 / mm;
14897 }
14898
14899 dpyinfo->Xatom_wm_protocols
14900 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
14901 dpyinfo->Xatom_wm_take_focus
14902 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
14903 dpyinfo->Xatom_wm_save_yourself
14904 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
14905 dpyinfo->Xatom_wm_delete_window
14906 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
14907 dpyinfo->Xatom_wm_change_state
14908 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
14909 dpyinfo->Xatom_wm_configure_denied
14910 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
14911 dpyinfo->Xatom_wm_window_moved
14912 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
14913 dpyinfo->Xatom_editres
14914 = XInternAtom (dpyinfo->display, "Editres", False);
14915 dpyinfo->Xatom_CLIPBOARD
14916 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
14917 dpyinfo->Xatom_TIMESTAMP
14918 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
14919 dpyinfo->Xatom_TEXT
14920 = XInternAtom (dpyinfo->display, "TEXT", False);
14921 dpyinfo->Xatom_COMPOUND_TEXT
14922 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
14923 dpyinfo->Xatom_DELETE
14924 = XInternAtom (dpyinfo->display, "DELETE", False);
14925 dpyinfo->Xatom_MULTIPLE
14926 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
14927 dpyinfo->Xatom_INCR
14928 = XInternAtom (dpyinfo->display, "INCR", False);
14929 dpyinfo->Xatom_EMACS_TMP
14930 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
14931 dpyinfo->Xatom_TARGETS
14932 = XInternAtom (dpyinfo->display, "TARGETS", False);
14933 dpyinfo->Xatom_NULL
14934 = XInternAtom (dpyinfo->display, "NULL", False);
14935 dpyinfo->Xatom_ATOM_PAIR
14936 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
14937 /* For properties of font. */
14938 dpyinfo->Xatom_PIXEL_SIZE
14939 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
14940 dpyinfo->Xatom_MULE_BASELINE_OFFSET
14941 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
14942 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
14943 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
14944 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
14945 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
14946
14947 /* Ghostscript support. */
14948 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
14949 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
14950
14951 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
14952 False);
14953
14954 dpyinfo->cut_buffers_initialized = 0;
14955
14956 connection = ConnectionNumber (dpyinfo->display);
14957 dpyinfo->connection = connection;
14958
14959 {
14960 char null_bits[1];
14961
14962 null_bits[0] = 0x00;
14963
14964 dpyinfo->null_pixel
14965 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14966 null_bits, 1, 1, (long) 0, (long) 0,
14967 1);
14968 }
14969
14970 {
14971 extern int gray_bitmap_width, gray_bitmap_height;
14972 extern char *gray_bitmap_bits;
14973 dpyinfo->gray
14974 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14975 gray_bitmap_bits,
14976 gray_bitmap_width, gray_bitmap_height,
14977 (unsigned long) 1, (unsigned long) 0, 1);
14978 }
14979
14980 #ifdef HAVE_X_I18N
14981 xim_initialize (dpyinfo, resource_name);
14982 #endif
14983
14984 #ifdef subprocesses
14985 /* This is only needed for distinguishing keyboard and process input. */
14986 if (connection != 0)
14987 add_keyboard_wait_descriptor (connection);
14988 #endif
14989
14990 #ifndef F_SETOWN_BUG
14991 #ifdef F_SETOWN
14992 #ifdef F_SETOWN_SOCK_NEG
14993 /* stdin is a socket here */
14994 fcntl (connection, F_SETOWN, -getpid ());
14995 #else /* ! defined (F_SETOWN_SOCK_NEG) */
14996 fcntl (connection, F_SETOWN, getpid ());
14997 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
14998 #endif /* ! defined (F_SETOWN) */
14999 #endif /* F_SETOWN_BUG */
15000
15001 #ifdef SIGIO
15002 if (interrupt_input)
15003 init_sigio (connection);
15004 #endif /* ! defined (SIGIO) */
15005
15006 #ifdef USE_LUCID
15007 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
15008 /* Make sure that we have a valid font for dialog boxes
15009 so that Xt does not crash. */
15010 {
15011 Display *dpy = dpyinfo->display;
15012 XrmValue d, fr, to;
15013 Font font;
15014 int count;
15015
15016 d.addr = (XPointer)&dpy;
15017 d.size = sizeof (Display *);
15018 fr.addr = XtDefaultFont;
15019 fr.size = sizeof (XtDefaultFont);
15020 to.size = sizeof (Font *);
15021 to.addr = (XPointer)&font;
15022 count = x_catch_errors (dpy);
15023 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
15024 abort ();
15025 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
15026 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
15027 x_uncatch_errors (dpy, count);
15028 }
15029 #endif
15030 #endif
15031
15032 /* See if we should run in synchronous mode. This is useful
15033 for debugging X code. */
15034 {
15035 Lisp_Object value;
15036 value = display_x_get_resource (dpyinfo,
15037 build_string ("synchronous"),
15038 build_string ("Synchronous"),
15039 Qnil, Qnil);
15040 if (STRINGP (value)
15041 && (!strcmp (XSTRING (value)->data, "true")
15042 || !strcmp (XSTRING (value)->data, "on")))
15043 XSynchronize (dpyinfo->display, True);
15044 }
15045
15046 {
15047 Lisp_Object value;
15048 value = display_x_get_resource (dpyinfo,
15049 build_string ("useXIM"),
15050 build_string ("UseXIM"),
15051 Qnil, Qnil);
15052 if (STRINGP (value)
15053 && (!strcmp (XSTRING (value)->data, "false")
15054 || !strcmp (XSTRING (value)->data, "off")))
15055 use_xim = 0;
15056 }
15057
15058 UNBLOCK_INPUT;
15059
15060 return dpyinfo;
15061 }
15062 \f
15063 /* Get rid of display DPYINFO, assuming all frames are already gone,
15064 and without sending any more commands to the X server. */
15065
15066 void
15067 x_delete_display (dpyinfo)
15068 struct x_display_info *dpyinfo;
15069 {
15070 delete_keyboard_wait_descriptor (dpyinfo->connection);
15071
15072 /* Discard this display from x_display_name_list and x_display_list.
15073 We can't use Fdelq because that can quit. */
15074 if (! NILP (x_display_name_list)
15075 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
15076 x_display_name_list = XCDR (x_display_name_list);
15077 else
15078 {
15079 Lisp_Object tail;
15080
15081 tail = x_display_name_list;
15082 while (CONSP (tail) && CONSP (XCDR (tail)))
15083 {
15084 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
15085 {
15086 XSETCDR (tail, XCDR (XCDR (tail)));
15087 break;
15088 }
15089 tail = XCDR (tail);
15090 }
15091 }
15092
15093 if (next_noop_dpyinfo == dpyinfo)
15094 next_noop_dpyinfo = dpyinfo->next;
15095
15096 if (x_display_list == dpyinfo)
15097 x_display_list = dpyinfo->next;
15098 else
15099 {
15100 struct x_display_info *tail;
15101
15102 for (tail = x_display_list; tail; tail = tail->next)
15103 if (tail->next == dpyinfo)
15104 tail->next = tail->next->next;
15105 }
15106
15107 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
15108 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
15109 XrmDestroyDatabase (dpyinfo->xrdb);
15110 #endif
15111 #endif
15112 #ifdef MULTI_KBOARD
15113 if (--dpyinfo->kboard->reference_count == 0)
15114 delete_kboard (dpyinfo->kboard);
15115 #endif
15116 #ifdef HAVE_X_I18N
15117 if (dpyinfo->xim)
15118 xim_close_dpy (dpyinfo);
15119 #endif
15120
15121 xfree (dpyinfo->font_table);
15122 xfree (dpyinfo->x_id_name);
15123 xfree (dpyinfo->color_cells);
15124 xfree (dpyinfo);
15125 }
15126
15127 \f
15128 /* Set up use of X before we make the first connection. */
15129
15130 static struct redisplay_interface x_redisplay_interface =
15131 {
15132 x_produce_glyphs,
15133 x_write_glyphs,
15134 x_insert_glyphs,
15135 x_clear_end_of_line,
15136 x_scroll_run,
15137 x_after_update_window_line,
15138 x_update_window_begin,
15139 x_update_window_end,
15140 XTcursor_to,
15141 x_flush,
15142 x_clear_mouse_face,
15143 x_get_glyph_overhangs,
15144 x_fix_overlapping_area
15145 };
15146
15147 void
15148 x_initialize ()
15149 {
15150 rif = &x_redisplay_interface;
15151
15152 clear_frame_hook = x_clear_frame;
15153 ins_del_lines_hook = x_ins_del_lines;
15154 delete_glyphs_hook = x_delete_glyphs;
15155 ring_bell_hook = XTring_bell;
15156 reset_terminal_modes_hook = XTreset_terminal_modes;
15157 set_terminal_modes_hook = XTset_terminal_modes;
15158 update_begin_hook = x_update_begin;
15159 update_end_hook = x_update_end;
15160 set_terminal_window_hook = XTset_terminal_window;
15161 read_socket_hook = XTread_socket;
15162 frame_up_to_date_hook = XTframe_up_to_date;
15163 mouse_position_hook = XTmouse_position;
15164 frame_rehighlight_hook = XTframe_rehighlight;
15165 frame_raise_lower_hook = XTframe_raise_lower;
15166 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
15167 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
15168 redeem_scroll_bar_hook = XTredeem_scroll_bar;
15169 judge_scroll_bars_hook = XTjudge_scroll_bars;
15170 estimate_mode_line_height_hook = x_estimate_mode_line_height;
15171
15172 scroll_region_ok = 1; /* we'll scroll partial frames */
15173 char_ins_del_ok = 1;
15174 line_ins_del_ok = 1; /* we'll just blt 'em */
15175 fast_clear_end_of_line = 1; /* X does this well */
15176 memory_below_frame = 0; /* we don't remember what scrolls
15177 off the bottom */
15178 baud_rate = 19200;
15179
15180 x_noop_count = 0;
15181 last_tool_bar_item = -1;
15182 any_help_event_p = 0;
15183
15184 /* Try to use interrupt input; if we can't, then start polling. */
15185 Fset_input_mode (Qt, Qnil, Qt, Qnil);
15186
15187 #ifdef USE_X_TOOLKIT
15188 XtToolkitInitialize ();
15189
15190 Xt_app_con = XtCreateApplicationContext ();
15191
15192 /* Register a converter from strings to pixels, which uses
15193 Emacs' color allocation infrastructure. */
15194 XtAppSetTypeConverter (Xt_app_con,
15195 XtRString, XtRPixel, cvt_string_to_pixel,
15196 cvt_string_to_pixel_args,
15197 XtNumber (cvt_string_to_pixel_args),
15198 XtCacheByDisplay, cvt_pixel_dtor);
15199
15200 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
15201
15202 /* Install an asynchronous timer that processes Xt timeout events
15203 every 0.1s. This is necessary because some widget sets use
15204 timeouts internally, for example the LessTif menu bar, or the
15205 Xaw3d scroll bar. When Xt timouts aren't processed, these
15206 widgets don't behave normally. */
15207 {
15208 EMACS_TIME interval;
15209 EMACS_SET_SECS_USECS (interval, 0, 100000);
15210 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
15211 }
15212 #endif
15213
15214 #ifdef USE_TOOLKIT_SCROLL_BARS
15215 xaw3d_arrow_scroll = False;
15216 xaw3d_pick_top = True;
15217 #endif
15218
15219 /* Note that there is no real way portable across R3/R4 to get the
15220 original error handler. */
15221 XSetErrorHandler (x_error_handler);
15222 XSetIOErrorHandler (x_io_error_quitter);
15223
15224 /* Disable Window Change signals; they are handled by X events. */
15225 #ifdef SIGWINCH
15226 signal (SIGWINCH, SIG_DFL);
15227 #endif /* ! defined (SIGWINCH) */
15228
15229 signal (SIGPIPE, x_connection_signal);
15230 }
15231
15232
15233 void
15234 syms_of_xterm ()
15235 {
15236 staticpro (&x_error_message_string);
15237 x_error_message_string = Qnil;
15238
15239 staticpro (&x_display_name_list);
15240 x_display_name_list = Qnil;
15241
15242 staticpro (&last_mouse_scroll_bar);
15243 last_mouse_scroll_bar = Qnil;
15244
15245 staticpro (&Qvendor_specific_keysyms);
15246 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
15247
15248 staticpro (&last_mouse_press_frame);
15249 last_mouse_press_frame = Qnil;
15250
15251 help_echo = Qnil;
15252 staticpro (&help_echo);
15253 help_echo_object = Qnil;
15254 staticpro (&help_echo_object);
15255 help_echo_window = Qnil;
15256 staticpro (&help_echo_window);
15257 previous_help_echo = Qnil;
15258 staticpro (&previous_help_echo);
15259 help_echo_pos = -1;
15260
15261 DEFVAR_BOOL ("x-autoselect-window", &x_autoselect_window_p,
15262 doc: /* *Non-nil means autoselect window with mouse pointer. */);
15263 x_autoselect_window_p = 0;
15264
15265 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
15266 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
15267 For example, if a block cursor is over a tab, it will be drawn as
15268 wide as that tab on the display. */);
15269 x_stretch_cursor_p = 0;
15270
15271 DEFVAR_BOOL ("x-use-underline-position-properties",
15272 &x_use_underline_position_properties,
15273 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
15274 nil means ignore them. If you encounter fonts with bogus
15275 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
15276 to 4.1, set this to nil. */);
15277 x_use_underline_position_properties = 1;
15278
15279 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
15280 doc: /* What X toolkit scroll bars Emacs uses.
15281 A value of nil means Emacs doesn't use X toolkit scroll bars.
15282 Otherwise, value is a symbol describing the X toolkit. */);
15283 #ifdef USE_TOOLKIT_SCROLL_BARS
15284 #ifdef USE_MOTIF
15285 Vx_toolkit_scroll_bars = intern ("motif");
15286 #elif defined HAVE_XAW3D
15287 Vx_toolkit_scroll_bars = intern ("xaw3d");
15288 #else
15289 Vx_toolkit_scroll_bars = intern ("xaw");
15290 #endif
15291 #else
15292 Vx_toolkit_scroll_bars = Qnil;
15293 #endif
15294
15295 staticpro (&last_mouse_motion_frame);
15296 last_mouse_motion_frame = Qnil;
15297
15298 Qmodifier_value = intern ("modifier-value");
15299 Qalt = intern ("alt");
15300 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
15301 Qhyper = intern ("hyper");
15302 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
15303 Qmeta = intern ("meta");
15304 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
15305 Qsuper = intern ("super");
15306 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
15307
15308 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
15309 doc: /* Which keys Emacs uses for the alt modifier.
15310 This should be one of the symbols `alt', `hyper', `meta', `super'.
15311 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
15312 is nil, which is the same as `alt'. */);
15313 Vx_alt_keysym = Qnil;
15314
15315 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
15316 doc: /* Which keys Emacs uses for the hyper modifier.
15317 This should be one of the symbols `alt', `hyper', `meta', `super'.
15318 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
15319 default is nil, which is the same as `hyper'. */);
15320 Vx_hyper_keysym = Qnil;
15321
15322 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
15323 doc: /* Which keys Emacs uses for the meta modifier.
15324 This should be one of the symbols `alt', `hyper', `meta', `super'.
15325 For example, `meta' means use the Meta_L and Meta_R keysyms. The
15326 default is nil, which is the same as `meta'. */);
15327 Vx_meta_keysym = Qnil;
15328
15329 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
15330 doc: /* Which keys Emacs uses for the super modifier.
15331 This should be one of the symbols `alt', `hyper', `meta', `super'.
15332 For example, `super' means use the Super_L and Super_R keysyms. The
15333 default is nil, which is the same as `super'. */);
15334 Vx_super_keysym = Qnil;
15335
15336 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
15337 doc: /* Hash table of character codes indexed by X keysym codes. */);
15338 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
15339 make_float (DEFAULT_REHASH_SIZE),
15340 make_float (DEFAULT_REHASH_THRESHOLD),
15341 Qnil, Qnil, Qnil);
15342 }
15343
15344 #endif /* HAVE_X_WINDOWS */